lfs_emubd.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Block device emulated on standard files
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the Apache 2.0 license
  6. */
  7. #include "emubd/lfs_emubd.h"
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <limits.h>
  13. #include <dirent.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. #include <assert.h>
  17. #include <stdbool.h>
  18. // Block device emulated on existing filesystem
  19. int lfs_emubd_create(const struct lfs_config *cfg, const char *path) {
  20. lfs_emubd_t *emu = cfg->context;
  21. emu->cfg.read_size = cfg->read_size;
  22. emu->cfg.prog_size = cfg->prog_size;
  23. emu->cfg.block_size = cfg->block_size;
  24. emu->cfg.block_count = cfg->block_count;
  25. // Allocate buffer for creating children files
  26. size_t pathlen = strlen(path);
  27. emu->path = malloc(pathlen + 1 + LFS_NAME_MAX + 1);
  28. if (!emu->path) {
  29. return -ENOMEM;
  30. }
  31. strcpy(emu->path, path);
  32. emu->path[pathlen] = '/';
  33. emu->child = &emu->path[pathlen+1];
  34. memset(emu->child, '\0', LFS_NAME_MAX+1);
  35. // Create directory if it doesn't exist
  36. int err = mkdir(path, 0777);
  37. if (err && errno != EEXIST) {
  38. return -errno;
  39. }
  40. // Load stats to continue incrementing
  41. snprintf(emu->child, LFS_NAME_MAX, "stats");
  42. FILE *f = fopen(emu->path, "r");
  43. if (!f) {
  44. return -errno;
  45. }
  46. size_t res = fread(&emu->stats, sizeof(emu->stats), 1, f);
  47. if (res < 1) {
  48. return -errno;
  49. }
  50. err = fclose(f);
  51. if (err) {
  52. return -errno;
  53. }
  54. return 0;
  55. }
  56. void lfs_emubd_destroy(const struct lfs_config *cfg) {
  57. lfs_emubd_sync(cfg);
  58. lfs_emubd_t *emu = cfg->context;
  59. free(emu->path);
  60. }
  61. int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
  62. lfs_off_t off, void *buffer, lfs_size_t size) {
  63. lfs_emubd_t *emu = cfg->context;
  64. uint8_t *data = buffer;
  65. // Check if read is valid
  66. assert(off % cfg->read_size == 0);
  67. assert(size % cfg->read_size == 0);
  68. assert(block < cfg->block_count);
  69. // Zero out buffer for debugging
  70. memset(data, 0, size);
  71. // Read data
  72. snprintf(emu->child, LFS_NAME_MAX, "%x", block);
  73. FILE *f = fopen(emu->path, "rb");
  74. if (!f && errno != ENOENT) {
  75. return -errno;
  76. }
  77. if (f) {
  78. int err = fseek(f, off, SEEK_SET);
  79. if (err) {
  80. return -errno;
  81. }
  82. size_t res = fread(data, 1, size, f);
  83. if (res < size && !feof(f)) {
  84. return -errno;
  85. }
  86. err = fclose(f);
  87. if (err) {
  88. return -errno;
  89. }
  90. }
  91. emu->stats.read_count += 1;
  92. return 0;
  93. }
  94. int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
  95. lfs_off_t off, const void *buffer, lfs_size_t size) {
  96. lfs_emubd_t *emu = cfg->context;
  97. const uint8_t *data = buffer;
  98. // Check if write is valid
  99. assert(off % cfg->prog_size == 0);
  100. assert(size % cfg->prog_size == 0);
  101. assert(block < cfg->block_count);
  102. // Program data
  103. snprintf(emu->child, LFS_NAME_MAX, "%x", block);
  104. FILE *f = fopen(emu->path, "r+b");
  105. if (!f && errno != ENOENT) {
  106. return -errno;
  107. }
  108. // Check that file was erased
  109. assert(f);
  110. int err = fseek(f, off, SEEK_SET);
  111. if (err) {
  112. return -errno;
  113. }
  114. size_t res = fwrite(data, 1, size, f);
  115. if (res < size) {
  116. return -errno;
  117. }
  118. err = fseek(f, off, SEEK_SET);
  119. if (err) {
  120. return -errno;
  121. }
  122. uint8_t dat;
  123. res = fread(&dat, 1, 1, f);
  124. if (res < 1) {
  125. return -errno;
  126. }
  127. err = fclose(f);
  128. if (err) {
  129. return -errno;
  130. }
  131. emu->stats.prog_count += 1;
  132. return 0;
  133. }
  134. int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
  135. lfs_emubd_t *emu = cfg->context;
  136. // Check if erase is valid
  137. assert(block < cfg->block_count);
  138. // Erase the block
  139. snprintf(emu->child, LFS_NAME_MAX, "%x", block);
  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. if (err || S_ISREG(st.st_mode)) {
  152. FILE *f = fopen(emu->path, "w");
  153. if (!f) {
  154. return -errno;
  155. }
  156. err = fclose(f);
  157. if (err) {
  158. return -errno;
  159. }
  160. }
  161. emu->stats.erase_count += 1;
  162. return 0;
  163. }
  164. int lfs_emubd_sync(const struct lfs_config *cfg) {
  165. lfs_emubd_t *emu = cfg->context;
  166. // Just write out info/stats for later lookup
  167. snprintf(emu->child, LFS_NAME_MAX, "config");
  168. FILE *f = fopen(emu->path, "w");
  169. if (!f) {
  170. return -errno;
  171. }
  172. size_t res = fwrite(&emu->cfg, sizeof(emu->cfg), 1, f);
  173. if (res < 1) {
  174. return -errno;
  175. }
  176. int err = fclose(f);
  177. if (err) {
  178. return -errno;
  179. }
  180. snprintf(emu->child, LFS_NAME_MAX, "stats");
  181. f = fopen(emu->path, "w");
  182. if (!f) {
  183. return -errno;
  184. }
  185. res = fwrite(&emu->stats, sizeof(emu->stats), 1, f);
  186. if (res < 1) {
  187. return -errno;
  188. }
  189. err = fclose(f);
  190. if (err) {
  191. return -errno;
  192. }
  193. return 0;
  194. }