lfs_emubd.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Block device emulated on standard files
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the MIT license
  6. */
  7. #include "emubd/lfs_emubd.h"
  8. #include "emubd/lfs_cfg.h"
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <limits.h>
  14. #include <dirent.h>
  15. #include <sys/stat.h>
  16. // Block device emulated on existing filesystem
  17. lfs_error_t lfs_emubd_create(lfs_emubd_t *emu, const char *path) {
  18. memset(&emu->info, 0, sizeof(emu->info));
  19. memset(&emu->stats, 0, sizeof(emu->stats));
  20. // Allocate buffer for creating children files
  21. size_t pathlen = strlen(path);
  22. emu->path = malloc(pathlen + 1 + LFS_NAME_MAX + 1);
  23. if (!emu->path) {
  24. return -ENOMEM;
  25. }
  26. strcpy(emu->path, path);
  27. emu->path[pathlen] = '/';
  28. emu->path[pathlen + 1 + LFS_NAME_MAX] = '\0';
  29. emu->child = &emu->path[pathlen+1];
  30. strncpy(emu->child, "config", LFS_NAME_MAX);
  31. // Load config, erroring if it doesn't exist
  32. lfs_cfg_t cfg;
  33. int err = lfs_cfg_create(&cfg, emu->path);
  34. if (err) {
  35. return err;
  36. }
  37. emu->info.read_size = lfs_cfg_getu(&cfg, "read_size", 0);
  38. emu->info.write_size = lfs_cfg_getu(&cfg, "write_size", 0);
  39. emu->info.erase_size = lfs_cfg_getu(&cfg, "erase_size", 0);
  40. emu->info.total_size = lfs_cfg_getu(&cfg, "total_size", 0);
  41. lfs_cfg_destroy(&cfg);
  42. return 0;
  43. }
  44. void lfs_emubd_destroy(lfs_emubd_t *emu) {
  45. free(emu->path);
  46. }
  47. lfs_error_t lfs_emubd_read(lfs_emubd_t *emu, uint8_t *buffer,
  48. lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
  49. // Check if read is valid
  50. if (!(off % emu->info.read_size == 0 &&
  51. size % emu->info.read_size == 0 &&
  52. ((lfs_lsize_t)ino*emu->info.erase_size + off + size
  53. < emu->info.total_size))) {
  54. return -EINVAL;
  55. }
  56. // Zero out buffer for debugging
  57. memset(buffer, 0, size);
  58. // Iterate over blocks until enough data is read
  59. while (size > 0) {
  60. snprintf(emu->child, LFS_NAME_MAX, "%d", ino);
  61. size_t count = lfs_min(emu->info.erase_size - off, size);
  62. FILE *f = fopen(emu->path, "rb");
  63. if (!f && errno != ENOENT) {
  64. return -errno;
  65. }
  66. if (f) {
  67. int err = fseek(f, off, SEEK_SET);
  68. if (err) {
  69. return -errno;
  70. }
  71. size_t res = fread(buffer, 1, count, f);
  72. if (res < count && !feof(f)) {
  73. return -errno;
  74. }
  75. err = fclose(f);
  76. if (err) {
  77. return -errno;
  78. }
  79. }
  80. size -= count;
  81. buffer += count;
  82. ino += 1;
  83. off = 0;
  84. }
  85. emu->stats.read_count += 1;
  86. return 0;
  87. }
  88. lfs_error_t lfs_emubd_write(lfs_emubd_t *emu, const uint8_t *buffer,
  89. lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
  90. // Check if write is valid
  91. if (!(off % emu->info.write_size == 0 &&
  92. size % emu->info.write_size == 0 &&
  93. ((lfs_lsize_t)ino*emu->info.erase_size + off + size
  94. < emu->info.total_size))) {
  95. return -EINVAL;
  96. }
  97. // Iterate over blocks until enough data is read
  98. while (size > 0) {
  99. snprintf(emu->child, LFS_NAME_MAX, "%d", ino);
  100. size_t count = lfs_min(emu->info.erase_size - off, size);
  101. FILE *f = fopen(emu->path, "r+b");
  102. if (!f && errno == ENOENT) {
  103. f = fopen(emu->path, "w+b");
  104. if (!f) {
  105. return -errno;
  106. }
  107. }
  108. int err = fseek(f, off, SEEK_SET);
  109. if (err) {
  110. return -errno;
  111. }
  112. size_t res = fwrite(buffer, 1, count, f);
  113. if (res < count) {
  114. return -errno;
  115. }
  116. err = fclose(f);
  117. if (err) {
  118. return -errno;
  119. }
  120. size -= count;
  121. buffer += count;
  122. ino += 1;
  123. off = 0;
  124. }
  125. emu->stats.write_count += 1;
  126. return 0;
  127. }
  128. lfs_error_t lfs_emubd_erase(lfs_emubd_t *emu,
  129. lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
  130. // Check if erase is valid
  131. if (!(off % emu->info.erase_size == 0 &&
  132. size % emu->info.erase_size == 0 &&
  133. ((lfs_lsize_t)ino*emu->info.erase_size + off + size
  134. < emu->info.total_size))) {
  135. return -EINVAL;
  136. }
  137. // Iterate and erase blocks
  138. while (size > 0) {
  139. snprintf(emu->child, LFS_NAME_MAX, "%d", ino);
  140. struct stat st;
  141. int err = stat(emu->path, &st);
  142. if (err && errno != ENOENT) {
  143. return -errno;
  144. }
  145. if (!err && S_ISREG(st.st_mode)) {
  146. int err = unlink(emu->path);
  147. if (err) {
  148. return -errno;
  149. }
  150. }
  151. size -= emu->info.erase_size;
  152. ino += 1;
  153. off = 0;
  154. }
  155. emu->stats.erase_count += 1;
  156. return 0;
  157. }
  158. lfs_error_t lfs_emubd_sync(lfs_emubd_t *emu) {
  159. // Always in sync
  160. return 0;
  161. }
  162. lfs_error_t lfs_emubd_info(lfs_emubd_t *emu, struct lfs_bd_info *info) {
  163. *info = emu->info;
  164. return 0;
  165. }
  166. lfs_error_t lfs_emubd_stats(lfs_emubd_t *emu, struct lfs_bd_stats *stats) {
  167. *stats = emu->stats;
  168. return 0;
  169. }
  170. // Wrappers for void*s
  171. static lfs_error_t lfs_emubd_bd_read(void *bd, uint8_t *buffer,
  172. lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
  173. return lfs_emubd_read((lfs_emubd_t*)bd, buffer, ino, off, size);
  174. }
  175. static lfs_error_t lfs_emubd_bd_write(void *bd, const uint8_t *buffer,
  176. lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
  177. return lfs_emubd_write((lfs_emubd_t*)bd, buffer, ino, off, size);
  178. }
  179. static lfs_error_t lfs_emubd_bd_erase(void *bd,
  180. lfs_ino_t ino, lfs_off_t off, lfs_size_t size) {
  181. return lfs_emubd_erase((lfs_emubd_t*)bd, ino, off, size);
  182. }
  183. static lfs_error_t lfs_emubd_bd_sync(void *bd) {
  184. return lfs_emubd_sync((lfs_emubd_t*)bd);
  185. }
  186. static lfs_error_t lfs_emubd_bd_info(void *bd, struct lfs_bd_info *info) {
  187. return lfs_emubd_info((lfs_emubd_t*)bd, info);
  188. }
  189. const struct lfs_bd_ops lfs_emubd_ops = {
  190. .read = lfs_emubd_bd_read,
  191. .write = lfs_emubd_bd_write,
  192. .erase = lfs_emubd_bd_erase,
  193. .sync = lfs_emubd_bd_sync,
  194. .info = lfs_emubd_bd_info,
  195. };