lfs_emubd.c 5.6 KB

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