lfs_emubd.c 5.8 KB

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