spiffs_hydrogen.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * spiffs_hydrogen.c
  3. *
  4. * Created on: Jun 16, 2013
  5. * Author: petera
  6. */
  7. #include "spiffs.h"
  8. #include "spiffs_nucleus.h"
  9. #if SPIFFS_FILEHDL_OFFSET
  10. #define SPIFFS_FH_OFFS(fs, fh) ((fh) != 0 ? ((fh) + (fs)->cfg.fh_ix_offset) : 0)
  11. #define SPIFFS_FH_UNOFFS(fs, fh) ((fh) != 0 ? ((fh) - (fs)->cfg.fh_ix_offset) : 0)
  12. #else
  13. #define SPIFFS_FH_OFFS(fs, fh) (fh)
  14. #define SPIFFS_FH_UNOFFS(fs, fh) (fh)
  15. #endif
  16. #if SPIFFS_CACHE == 1
  17. static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh);
  18. #endif
  19. #if SPIFFS_BUFFER_HELP
  20. u32_t SPIFFS_buffer_bytes_for_filedescs(spiffs *fs, u32_t num_descs) {
  21. return num_descs * sizeof(spiffs_fd);
  22. }
  23. #if SPIFFS_CACHE
  24. u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages) {
  25. return sizeof(spiffs_cache) + num_pages * (sizeof(spiffs_cache_page) + SPIFFS_CFG_LOG_PAGE_SZ(fs));
  26. }
  27. #endif
  28. #endif
  29. u8_t SPIFFS_mounted(spiffs *fs) {
  30. return SPIFFS_CHECK_MOUNT(fs);
  31. }
  32. s32_t SPIFFS_format(spiffs *fs) {
  33. #if SPIFFS_READ_ONLY
  34. (void)fs;
  35. return SPIFFS_ERR_RO_NOT_IMPL;
  36. #else
  37. SPIFFS_API_CHECK_CFG(fs);
  38. if (SPIFFS_CHECK_MOUNT(fs)) {
  39. fs->err_code = SPIFFS_ERR_MOUNTED;
  40. return -1;
  41. }
  42. s32_t res;
  43. SPIFFS_LOCK(fs);
  44. spiffs_block_ix bix = 0;
  45. while (bix < fs->block_count) {
  46. fs->max_erase_count = 0;
  47. res = spiffs_erase_block(fs, bix);
  48. if (res != SPIFFS_OK) {
  49. res = SPIFFS_ERR_ERASE_FAIL;
  50. }
  51. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  52. bix++;
  53. }
  54. SPIFFS_UNLOCK(fs);
  55. return 0;
  56. #endif // SPIFFS_READ_ONLY
  57. }
  58. #if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
  59. s32_t SPIFFS_probe_fs(spiffs_config *config) {
  60. s32_t res = spiffs_probe(config);
  61. return res;
  62. }
  63. #endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
  64. s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
  65. u8_t *fd_space, u32_t fd_space_size,
  66. void *cache, u32_t cache_size,
  67. spiffs_check_callback check_cb_f) {
  68. void *user_data;
  69. SPIFFS_LOCK(fs);
  70. user_data = fs->user_data;
  71. memset(fs, 0, sizeof(spiffs));
  72. memcpy(&fs->cfg, config, sizeof(spiffs_config));
  73. fs->user_data = user_data;
  74. fs->block_count = SPIFFS_CFG_PHYS_SZ(fs) / SPIFFS_CFG_LOG_BLOCK_SZ(fs);
  75. fs->work = &work[0];
  76. fs->lu_work = &work[SPIFFS_CFG_LOG_PAGE_SZ(fs)];
  77. memset(fd_space, 0, fd_space_size);
  78. // align fd_space pointer to pointer size byte boundary
  79. u8_t ptr_size = sizeof(void*);
  80. u8_t addr_lsb = ((u8_t)(intptr_t)fd_space) & (ptr_size-1);
  81. if (addr_lsb) {
  82. fd_space += (ptr_size-addr_lsb);
  83. fd_space_size -= (ptr_size-addr_lsb);
  84. }
  85. fs->fd_space = fd_space;
  86. fs->fd_count = (fd_space_size/sizeof(spiffs_fd));
  87. // align cache pointer to 4 byte boundary
  88. addr_lsb = ((u8_t)(intptr_t)cache) & (ptr_size-1);
  89. if (addr_lsb) {
  90. u8_t *cache_8 = (u8_t *)cache;
  91. cache_8 += (ptr_size-addr_lsb);
  92. cache = cache_8;
  93. cache_size -= (ptr_size-addr_lsb);
  94. }
  95. if (cache_size & (ptr_size-1)) {
  96. cache_size -= (cache_size & (ptr_size-1));
  97. }
  98. #if SPIFFS_CACHE
  99. fs->cache = cache;
  100. fs->cache_size = (cache_size > (SPIFFS_CFG_LOG_PAGE_SZ(fs)*32)) ? SPIFFS_CFG_LOG_PAGE_SZ(fs)*32 : cache_size;
  101. spiffs_cache_init(fs);
  102. #endif
  103. s32_t res;
  104. #if SPIFFS_USE_MAGIC
  105. res = SPIFFS_CHECK_MAGIC_POSSIBLE(fs) ? SPIFFS_OK : SPIFFS_ERR_MAGIC_NOT_POSSIBLE;
  106. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  107. #endif
  108. fs->config_magic = SPIFFS_CONFIG_MAGIC;
  109. res = spiffs_obj_lu_scan(fs);
  110. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  111. SPIFFS_DBG("page index byte len: %i\n", SPIFFS_CFG_LOG_PAGE_SZ(fs));
  112. SPIFFS_DBG("object lookup pages: %i\n", SPIFFS_OBJ_LOOKUP_PAGES(fs));
  113. SPIFFS_DBG("page pages per block: %i\n", SPIFFS_PAGES_PER_BLOCK(fs));
  114. SPIFFS_DBG("page header length: %i\n", sizeof(spiffs_page_header));
  115. SPIFFS_DBG("object header index entries: %i\n", SPIFFS_OBJ_HDR_IX_LEN(fs));
  116. SPIFFS_DBG("object index entries: %i\n", SPIFFS_OBJ_IX_LEN(fs));
  117. SPIFFS_DBG("available file descriptors: %i\n", fs->fd_count);
  118. SPIFFS_DBG("free blocks: %i\n", fs->free_blocks);
  119. fs->check_cb_f = check_cb_f;
  120. fs->mounted = 1;
  121. SPIFFS_UNLOCK(fs);
  122. return 0;
  123. }
  124. void SPIFFS_unmount(spiffs *fs) {
  125. if (!SPIFFS_CHECK_CFG(fs) || !SPIFFS_CHECK_MOUNT(fs)) return;
  126. SPIFFS_LOCK(fs);
  127. u32_t i;
  128. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  129. for (i = 0; i < fs->fd_count; i++) {
  130. spiffs_fd *cur_fd = &fds[i];
  131. if (cur_fd->file_nbr != 0) {
  132. #if SPIFFS_CACHE
  133. (void)spiffs_fflush_cache(fs, cur_fd->file_nbr);
  134. #endif
  135. spiffs_fd_return(fs, cur_fd->file_nbr);
  136. }
  137. }
  138. fs->mounted = 0;
  139. SPIFFS_UNLOCK(fs);
  140. }
  141. s32_t SPIFFS_errno(spiffs *fs) {
  142. return fs->err_code;
  143. }
  144. void SPIFFS_clearerr(spiffs *fs) {
  145. fs->err_code = SPIFFS_OK;
  146. }
  147. s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) {
  148. #if SPIFFS_READ_ONLY
  149. (void)fs; (void)path; (void)mode;
  150. return SPIFFS_ERR_RO_NOT_IMPL;
  151. #else
  152. (void)mode;
  153. SPIFFS_API_CHECK_CFG(fs);
  154. SPIFFS_API_CHECK_MOUNT(fs);
  155. if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
  156. SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
  157. }
  158. SPIFFS_LOCK(fs);
  159. spiffs_obj_id obj_id;
  160. s32_t res;
  161. res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (const u8_t*)path);
  162. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  163. res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, 0);
  164. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  165. SPIFFS_UNLOCK(fs);
  166. return 0;
  167. #endif // SPIFFS_READ_ONLY
  168. }
  169. spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode) {
  170. (void)mode;
  171. SPIFFS_API_CHECK_CFG(fs);
  172. SPIFFS_API_CHECK_MOUNT(fs);
  173. if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
  174. SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
  175. }
  176. SPIFFS_LOCK(fs);
  177. spiffs_fd *fd;
  178. spiffs_page_ix pix;
  179. #if SPIFFS_READ_ONLY
  180. // not valid flags in read only mode
  181. flags &= ~SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_TRUNC;
  182. #endif // SPIFFS_READ_ONLY
  183. s32_t res = spiffs_fd_find_new(fs, &fd);
  184. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  185. res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);
  186. if ((flags & SPIFFS_O_CREAT) == 0) {
  187. if (res < SPIFFS_OK) {
  188. spiffs_fd_return(fs, fd->file_nbr);
  189. }
  190. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  191. }
  192. if (res == SPIFFS_OK &&
  193. (flags & (SPIFFS_O_CREAT | SPIFFS_O_EXCL)) == (SPIFFS_O_CREAT | SPIFFS_O_EXCL)) {
  194. // creat and excl and file exists - fail
  195. res = SPIFFS_ERR_FILE_EXISTS;
  196. spiffs_fd_return(fs, fd->file_nbr);
  197. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  198. }
  199. if ((flags & SPIFFS_O_CREAT) && res == SPIFFS_ERR_NOT_FOUND) {
  200. #if !SPIFFS_READ_ONLY
  201. spiffs_obj_id obj_id;
  202. // no need to enter conflicting name here, already looked for it above
  203. res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, 0);
  204. if (res < SPIFFS_OK) {
  205. spiffs_fd_return(fs, fd->file_nbr);
  206. }
  207. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  208. res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, &pix);
  209. if (res < SPIFFS_OK) {
  210. spiffs_fd_return(fs, fd->file_nbr);
  211. }
  212. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  213. flags &= ~SPIFFS_O_TRUNC;
  214. #endif // !SPIFFS_READ_ONLY
  215. } else {
  216. if (res < SPIFFS_OK) {
  217. spiffs_fd_return(fs, fd->file_nbr);
  218. }
  219. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  220. }
  221. res = spiffs_object_open_by_page(fs, pix, fd, flags, mode);
  222. if (res < SPIFFS_OK) {
  223. spiffs_fd_return(fs, fd->file_nbr);
  224. }
  225. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  226. #if !SPIFFS_READ_ONLY
  227. if (flags & SPIFFS_O_TRUNC) {
  228. res = spiffs_object_truncate(fd, 0, 0);
  229. if (res < SPIFFS_OK) {
  230. spiffs_fd_return(fs, fd->file_nbr);
  231. }
  232. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  233. }
  234. #endif // !SPIFFS_READ_ONLY
  235. fd->fdoffset = 0;
  236. SPIFFS_UNLOCK(fs);
  237. return SPIFFS_FH_OFFS(fs, fd->file_nbr);
  238. }
  239. spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) {
  240. SPIFFS_API_CHECK_CFG(fs);
  241. SPIFFS_API_CHECK_MOUNT(fs);
  242. SPIFFS_LOCK(fs);
  243. spiffs_fd *fd;
  244. s32_t res = spiffs_fd_find_new(fs, &fd);
  245. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  246. res = spiffs_object_open_by_page(fs, e->pix, fd, flags, mode);
  247. if (res < SPIFFS_OK) {
  248. spiffs_fd_return(fs, fd->file_nbr);
  249. }
  250. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  251. #if !SPIFFS_READ_ONLY
  252. if (flags & SPIFFS_O_TRUNC) {
  253. res = spiffs_object_truncate(fd, 0, 0);
  254. if (res < SPIFFS_OK) {
  255. spiffs_fd_return(fs, fd->file_nbr);
  256. }
  257. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  258. }
  259. #endif // !SPIFFS_READ_ONLY
  260. fd->fdoffset = 0;
  261. SPIFFS_UNLOCK(fs);
  262. return SPIFFS_FH_OFFS(fs, fd->file_nbr);
  263. }
  264. spiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode) {
  265. SPIFFS_API_CHECK_CFG(fs);
  266. SPIFFS_API_CHECK_MOUNT(fs);
  267. SPIFFS_LOCK(fs);
  268. spiffs_fd *fd;
  269. s32_t res = spiffs_fd_find_new(fs, &fd);
  270. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  271. if (SPIFFS_IS_LOOKUP_PAGE(fs, page_ix)) {
  272. res = SPIFFS_ERR_NOT_A_FILE;
  273. spiffs_fd_return(fs, fd->file_nbr);
  274. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  275. }
  276. res = spiffs_object_open_by_page(fs, page_ix, fd, flags, mode);
  277. if (res == SPIFFS_ERR_IS_FREE ||
  278. res == SPIFFS_ERR_DELETED ||
  279. res == SPIFFS_ERR_NOT_FINALIZED ||
  280. res == SPIFFS_ERR_NOT_INDEX ||
  281. res == SPIFFS_ERR_INDEX_SPAN_MISMATCH) {
  282. res = SPIFFS_ERR_NOT_A_FILE;
  283. }
  284. if (res < SPIFFS_OK) {
  285. spiffs_fd_return(fs, fd->file_nbr);
  286. }
  287. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  288. #if !SPIFFS_READ_ONLY
  289. if (flags & SPIFFS_O_TRUNC) {
  290. res = spiffs_object_truncate(fd, 0, 0);
  291. if (res < SPIFFS_OK) {
  292. spiffs_fd_return(fs, fd->file_nbr);
  293. }
  294. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  295. }
  296. #endif // !SPIFFS_READ_ONLY
  297. fd->fdoffset = 0;
  298. SPIFFS_UNLOCK(fs);
  299. return SPIFFS_FH_OFFS(fs, fd->file_nbr);
  300. }
  301. s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
  302. SPIFFS_API_CHECK_CFG(fs);
  303. SPIFFS_API_CHECK_MOUNT(fs);
  304. SPIFFS_LOCK(fs);
  305. spiffs_fd *fd;
  306. s32_t res;
  307. fh = SPIFFS_FH_UNOFFS(fs, fh);
  308. res = spiffs_fd_get(fs, fh, &fd);
  309. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  310. if ((fd->flags & SPIFFS_O_RDONLY) == 0) {
  311. res = SPIFFS_ERR_NOT_READABLE;
  312. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  313. }
  314. if (fd->size == SPIFFS_UNDEFINED_LEN && len > 0) {
  315. // special case for zero sized files
  316. res = SPIFFS_ERR_END_OF_OBJECT;
  317. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  318. }
  319. #if SPIFFS_CACHE_WR
  320. spiffs_fflush_cache(fs, fh);
  321. #endif
  322. if (fd->fdoffset + len >= fd->size) {
  323. // reading beyond file size
  324. s32_t avail = fd->size - fd->fdoffset;
  325. if (avail <= 0) {
  326. SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_END_OF_OBJECT);
  327. }
  328. res = spiffs_object_read(fd, fd->fdoffset, avail, (u8_t*)buf);
  329. if (res == SPIFFS_ERR_END_OF_OBJECT) {
  330. fd->fdoffset += avail;
  331. SPIFFS_UNLOCK(fs);
  332. return avail;
  333. } else {
  334. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  335. len = avail;
  336. }
  337. } else {
  338. // reading within file size
  339. res = spiffs_object_read(fd, fd->fdoffset, len, (u8_t*)buf);
  340. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  341. }
  342. fd->fdoffset += len;
  343. SPIFFS_UNLOCK(fs);
  344. return len;
  345. }
  346. #if !SPIFFS_READ_ONLY
  347. static s32_t spiffs_hydro_write(spiffs *fs, spiffs_fd *fd, void *buf, u32_t offset, s32_t len) {
  348. (void)fs;
  349. s32_t res = SPIFFS_OK;
  350. s32_t remaining = len;
  351. if (fd->size != SPIFFS_UNDEFINED_LEN && offset < fd->size) {
  352. s32_t m_len = MIN((s32_t)(fd->size - offset), len);
  353. res = spiffs_object_modify(fd, offset, (u8_t *)buf, m_len);
  354. SPIFFS_CHECK_RES(res);
  355. remaining -= m_len;
  356. u8_t *buf_8 = (u8_t *)buf;
  357. buf_8 += m_len;
  358. buf = buf_8;
  359. offset += m_len;
  360. }
  361. if (remaining > 0) {
  362. res = spiffs_object_append(fd, offset, (u8_t *)buf, remaining);
  363. SPIFFS_CHECK_RES(res);
  364. }
  365. return len;
  366. }
  367. #endif // !SPIFFS_READ_ONLY
  368. s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
  369. #if SPIFFS_READ_ONLY
  370. (void)fs; (void)fh; (void)buf; (void)len;
  371. return SPIFFS_ERR_RO_NOT_IMPL;
  372. #else
  373. SPIFFS_API_CHECK_CFG(fs);
  374. SPIFFS_API_CHECK_MOUNT(fs);
  375. SPIFFS_LOCK(fs);
  376. spiffs_fd *fd;
  377. s32_t res;
  378. u32_t offset;
  379. fh = SPIFFS_FH_UNOFFS(fs, fh);
  380. res = spiffs_fd_get(fs, fh, &fd);
  381. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  382. if ((fd->flags & SPIFFS_O_WRONLY) == 0) {
  383. res = SPIFFS_ERR_NOT_WRITABLE;
  384. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  385. }
  386. if ((fd->flags & SPIFFS_O_APPEND)) {
  387. fd->fdoffset = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size;
  388. }
  389. offset = fd->fdoffset;
  390. #if SPIFFS_CACHE_WR
  391. if (fd->cache_page == 0) {
  392. // see if object id is associated with cache already
  393. fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
  394. }
  395. #endif
  396. if (fd->flags & SPIFFS_O_APPEND) {
  397. if (fd->size == SPIFFS_UNDEFINED_LEN) {
  398. offset = 0;
  399. } else {
  400. offset = fd->size;
  401. }
  402. #if SPIFFS_CACHE_WR
  403. if (fd->cache_page) {
  404. offset = MAX(offset, fd->cache_page->offset + fd->cache_page->size);
  405. }
  406. #endif
  407. }
  408. #if SPIFFS_CACHE_WR
  409. if ((fd->flags & SPIFFS_O_DIRECT) == 0) {
  410. if (len < (s32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs)) {
  411. // small write, try to cache it
  412. u8_t alloc_cpage = 1;
  413. if (fd->cache_page) {
  414. // have a cached page for this fd already, check cache page boundaries
  415. if (offset < fd->cache_page->offset || // writing before cache
  416. offset > fd->cache_page->offset + fd->cache_page->size || // writing after cache
  417. offset + len > fd->cache_page->offset + SPIFFS_CFG_LOG_PAGE_SZ(fs)) // writing beyond cache page
  418. {
  419. // boundary violation, write back cache first and allocate new
  420. SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page %i for fd %i:%04x, boundary viol, offs:%i size:%i\n",
  421. fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->offset, fd->cache_page->size);
  422. res = spiffs_hydro_write(fs, fd,
  423. spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
  424. fd->cache_page->offset, fd->cache_page->size);
  425. spiffs_cache_fd_release(fs, fd->cache_page);
  426. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  427. } else {
  428. // writing within cache
  429. alloc_cpage = 0;
  430. }
  431. }
  432. if (alloc_cpage) {
  433. fd->cache_page = spiffs_cache_page_allocate_by_fd(fs, fd);
  434. if (fd->cache_page) {
  435. fd->cache_page->offset = offset;
  436. fd->cache_page->size = 0;
  437. SPIFFS_CACHE_DBG("CACHE_WR_ALLO: allocating cache page %i for fd %i:%04x\n",
  438. fd->cache_page->ix, fd->file_nbr, fd->obj_id);
  439. }
  440. }
  441. if (fd->cache_page) {
  442. u32_t offset_in_cpage = offset - fd->cache_page->offset;
  443. SPIFFS_CACHE_DBG("CACHE_WR_WRITE: storing to cache page %i for fd %i:%04x, offs %i:%i len %i\n",
  444. fd->cache_page->ix, fd->file_nbr, fd->obj_id,
  445. offset, offset_in_cpage, len);
  446. spiffs_cache *cache = spiffs_get_cache(fs);
  447. u8_t *cpage_data = spiffs_get_cache_page(fs, cache, fd->cache_page->ix);
  448. memcpy(&cpage_data[offset_in_cpage], buf, len);
  449. fd->cache_page->size = MAX(fd->cache_page->size, offset_in_cpage + len);
  450. fd->fdoffset += len;
  451. SPIFFS_UNLOCK(fs);
  452. return len;
  453. } else {
  454. res = spiffs_hydro_write(fs, fd, buf, offset, len);
  455. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  456. fd->fdoffset += len;
  457. SPIFFS_UNLOCK(fs);
  458. return res;
  459. }
  460. } else {
  461. // big write, no need to cache it - but first check if there is a cached write already
  462. if (fd->cache_page) {
  463. // write back cache first
  464. SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page %i for fd %i:%04x, big write, offs:%i size:%i\n",
  465. fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->offset, fd->cache_page->size);
  466. res = spiffs_hydro_write(fs, fd,
  467. spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
  468. fd->cache_page->offset, fd->cache_page->size);
  469. spiffs_cache_fd_release(fs, fd->cache_page);
  470. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  471. res = spiffs_hydro_write(fs, fd, buf, offset, len);
  472. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  473. }
  474. }
  475. }
  476. #endif
  477. res = spiffs_hydro_write(fs, fd, buf, offset, len);
  478. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  479. fd->fdoffset += len;
  480. SPIFFS_UNLOCK(fs);
  481. return res;
  482. #endif // SPIFFS_READ_ONLY
  483. }
  484. s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {
  485. SPIFFS_API_CHECK_CFG(fs);
  486. SPIFFS_API_CHECK_MOUNT(fs);
  487. SPIFFS_LOCK(fs);
  488. spiffs_fd *fd;
  489. s32_t res;
  490. fh = SPIFFS_FH_UNOFFS(fs, fh);
  491. res = spiffs_fd_get(fs, fh, &fd);
  492. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  493. #if SPIFFS_CACHE_WR
  494. spiffs_fflush_cache(fs, fh);
  495. #endif
  496. switch (whence) {
  497. case SPIFFS_SEEK_CUR:
  498. offs = fd->fdoffset+offs;
  499. break;
  500. case SPIFFS_SEEK_END:
  501. offs = (fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size) + offs;
  502. break;
  503. }
  504. if ((offs > (s32_t)fd->size) && (SPIFFS_UNDEFINED_LEN != fd->size)) {
  505. res = SPIFFS_ERR_END_OF_OBJECT;
  506. }
  507. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  508. spiffs_span_ix data_spix = offs / SPIFFS_DATA_PAGE_SIZE(fs);
  509. spiffs_span_ix objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  510. if (fd->cursor_objix_spix != objix_spix) {
  511. spiffs_page_ix pix;
  512. res = spiffs_obj_lu_find_id_and_span(
  513. fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, objix_spix, 0, &pix);
  514. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  515. fd->cursor_objix_spix = objix_spix;
  516. fd->cursor_objix_pix = pix;
  517. }
  518. fd->fdoffset = offs;
  519. SPIFFS_UNLOCK(fs);
  520. return offs;
  521. }
  522. s32_t SPIFFS_remove(spiffs *fs, const char *path) {
  523. #if SPIFFS_READ_ONLY
  524. (void)fs; (void)path;
  525. return SPIFFS_ERR_RO_NOT_IMPL;
  526. #else
  527. SPIFFS_API_CHECK_CFG(fs);
  528. SPIFFS_API_CHECK_MOUNT(fs);
  529. if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
  530. SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
  531. }
  532. SPIFFS_LOCK(fs);
  533. spiffs_fd *fd;
  534. spiffs_page_ix pix;
  535. s32_t res;
  536. res = spiffs_fd_find_new(fs, &fd);
  537. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  538. res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);
  539. if (res != SPIFFS_OK) {
  540. spiffs_fd_return(fs, fd->file_nbr);
  541. }
  542. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  543. res = spiffs_object_open_by_page(fs, pix, fd, 0,0);
  544. if (res != SPIFFS_OK) {
  545. spiffs_fd_return(fs, fd->file_nbr);
  546. }
  547. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  548. res = spiffs_object_truncate(fd, 0, 1);
  549. if (res != SPIFFS_OK) {
  550. spiffs_fd_return(fs, fd->file_nbr);
  551. }
  552. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  553. SPIFFS_UNLOCK(fs);
  554. return 0;
  555. #endif // SPIFFS_READ_ONLY
  556. }
  557. s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {
  558. #if SPIFFS_READ_ONLY
  559. (void)fs; (void)fh;
  560. return SPIFFS_ERR_RO_NOT_IMPL;
  561. #else
  562. SPIFFS_API_CHECK_CFG(fs);
  563. SPIFFS_API_CHECK_MOUNT(fs);
  564. SPIFFS_LOCK(fs);
  565. spiffs_fd *fd;
  566. s32_t res;
  567. fh = SPIFFS_FH_UNOFFS(fs, fh);
  568. res = spiffs_fd_get(fs, fh, &fd);
  569. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  570. if ((fd->flags & SPIFFS_O_WRONLY) == 0) {
  571. res = SPIFFS_ERR_NOT_WRITABLE;
  572. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  573. }
  574. #if SPIFFS_CACHE_WR
  575. spiffs_cache_fd_release(fs, fd->cache_page);
  576. #endif
  577. res = spiffs_object_truncate(fd, 0, 1);
  578. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  579. SPIFFS_UNLOCK(fs);
  580. return 0;
  581. #endif // SPIFFS_READ_ONLY
  582. }
  583. static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spiffs_stat *s) {
  584. (void)fh;
  585. spiffs_page_object_ix_header objix_hdr;
  586. spiffs_obj_id obj_id;
  587. s32_t res =_spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ, fh,
  588. SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
  589. SPIFFS_API_CHECK_RES(fs, res);
  590. u32_t obj_id_addr = SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs , pix)) +
  591. SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix) * sizeof(spiffs_obj_id);
  592. res =_spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, fh,
  593. obj_id_addr, sizeof(spiffs_obj_id), (u8_t *)&obj_id);
  594. SPIFFS_API_CHECK_RES(fs, res);
  595. s->obj_id = obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
  596. s->type = objix_hdr.type;
  597. s->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
  598. s->pix = pix;
  599. strncpy((char *)s->name, (char *)objix_hdr.name, SPIFFS_OBJ_NAME_LEN);
  600. return res;
  601. }
  602. s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s) {
  603. SPIFFS_API_CHECK_CFG(fs);
  604. SPIFFS_API_CHECK_MOUNT(fs);
  605. if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
  606. SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
  607. }
  608. SPIFFS_LOCK(fs);
  609. s32_t res;
  610. spiffs_page_ix pix;
  611. res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix);
  612. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  613. res = spiffs_stat_pix(fs, pix, 0, s);
  614. SPIFFS_UNLOCK(fs);
  615. return res;
  616. }
  617. s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s) {
  618. SPIFFS_API_CHECK_CFG(fs);
  619. SPIFFS_API_CHECK_MOUNT(fs);
  620. SPIFFS_LOCK(fs);
  621. spiffs_fd *fd;
  622. s32_t res;
  623. fh = SPIFFS_FH_UNOFFS(fs, fh);
  624. res = spiffs_fd_get(fs, fh, &fd);
  625. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  626. #if SPIFFS_CACHE_WR
  627. spiffs_fflush_cache(fs, fh);
  628. #endif
  629. res = spiffs_stat_pix(fs, fd->objix_hdr_pix, fh, s);
  630. SPIFFS_UNLOCK(fs);
  631. return res;
  632. }
  633. // Checks if there are any cached writes for the object id associated with
  634. // given filehandle. If so, these writes are flushed.
  635. #if SPIFFS_CACHE == 1
  636. static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh) {
  637. (void)fs;
  638. (void)fh;
  639. s32_t res = SPIFFS_OK;
  640. #if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR
  641. spiffs_fd *fd;
  642. res = spiffs_fd_get(fs, fh, &fd);
  643. SPIFFS_API_CHECK_RES(fs, res);
  644. if ((fd->flags & SPIFFS_O_DIRECT) == 0) {
  645. if (fd->cache_page == 0) {
  646. // see if object id is associated with cache already
  647. fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
  648. }
  649. if (fd->cache_page) {
  650. SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page %i for fd %i:%04x, flush, offs:%i size:%i\n",
  651. fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->offset, fd->cache_page->size);
  652. res = spiffs_hydro_write(fs, fd,
  653. spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
  654. fd->cache_page->offset, fd->cache_page->size);
  655. if (res < SPIFFS_OK) {
  656. fs->err_code = res;
  657. }
  658. spiffs_cache_fd_release(fs, fd->cache_page);
  659. }
  660. }
  661. #endif
  662. return res;
  663. }
  664. #endif
  665. s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) {
  666. (void)fh;
  667. SPIFFS_API_CHECK_CFG(fs);
  668. SPIFFS_API_CHECK_MOUNT(fs);
  669. s32_t res = SPIFFS_OK;
  670. #if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR
  671. SPIFFS_LOCK(fs);
  672. fh = SPIFFS_FH_UNOFFS(fs, fh);
  673. res = spiffs_fflush_cache(fs, fh);
  674. SPIFFS_API_CHECK_RES_UNLOCK(fs,res);
  675. SPIFFS_UNLOCK(fs);
  676. #endif
  677. return res;
  678. }
  679. s32_t SPIFFS_close(spiffs *fs, spiffs_file fh) {
  680. SPIFFS_API_CHECK_CFG(fs);
  681. SPIFFS_API_CHECK_MOUNT(fs);
  682. s32_t res = SPIFFS_OK;
  683. SPIFFS_LOCK(fs);
  684. fh = SPIFFS_FH_UNOFFS(fs, fh);
  685. #if SPIFFS_CACHE
  686. res = spiffs_fflush_cache(fs, fh);
  687. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  688. #endif
  689. res = spiffs_fd_return(fs, fh);
  690. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  691. SPIFFS_UNLOCK(fs);
  692. return res;
  693. }
  694. s32_t SPIFFS_rename(spiffs *fs, const char *old_path, const char *new_path) {
  695. #if SPIFFS_READ_ONLY
  696. (void)fs; (void)old_path; (void)new_path;
  697. return SPIFFS_ERR_RO_NOT_IMPL;
  698. #else
  699. SPIFFS_API_CHECK_CFG(fs);
  700. SPIFFS_API_CHECK_MOUNT(fs);
  701. if (strlen(new_path) > SPIFFS_OBJ_NAME_LEN - 1 ||
  702. strlen(old_path) > SPIFFS_OBJ_NAME_LEN - 1) {
  703. SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
  704. }
  705. SPIFFS_LOCK(fs);
  706. spiffs_page_ix pix_old, pix_dummy;
  707. spiffs_fd *fd;
  708. s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)old_path, &pix_old);
  709. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  710. res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)new_path, &pix_dummy);
  711. if (res == SPIFFS_ERR_NOT_FOUND) {
  712. res = SPIFFS_OK;
  713. } else if (res == SPIFFS_OK) {
  714. res = SPIFFS_ERR_CONFLICTING_NAME;
  715. }
  716. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  717. res = spiffs_fd_find_new(fs, &fd);
  718. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  719. res = spiffs_object_open_by_page(fs, pix_old, fd, 0, 0);
  720. if (res != SPIFFS_OK) {
  721. spiffs_fd_return(fs, fd->file_nbr);
  722. }
  723. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  724. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (const u8_t*)new_path,
  725. 0, &pix_dummy);
  726. spiffs_fd_return(fs, fd->file_nbr);
  727. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  728. SPIFFS_UNLOCK(fs);
  729. return res;
  730. #endif // SPIFFS_READ_ONLY
  731. }
  732. spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d) {
  733. (void)name;
  734. if (!SPIFFS_CHECK_CFG((fs))) {
  735. (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED;
  736. return 0;
  737. }
  738. if (!SPIFFS_CHECK_MOUNT(fs)) {
  739. fs->err_code = SPIFFS_ERR_NOT_MOUNTED;
  740. return 0;
  741. }
  742. d->fs = fs;
  743. d->block = 0;
  744. d->entry = 0;
  745. return d;
  746. }
  747. static s32_t spiffs_read_dir_v(
  748. spiffs *fs,
  749. spiffs_obj_id obj_id,
  750. spiffs_block_ix bix,
  751. int ix_entry,
  752. const void *user_const_p,
  753. void *user_var_p) {
  754. (void)user_const_p;
  755. s32_t res;
  756. spiffs_page_object_ix_header objix_hdr;
  757. if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||
  758. (obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0) {
  759. return SPIFFS_VIS_COUNTINUE;
  760. }
  761. spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
  762. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  763. 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
  764. if (res != SPIFFS_OK) return res;
  765. if ((obj_id & SPIFFS_OBJ_ID_IX_FLAG) &&
  766. objix_hdr.p_hdr.span_ix == 0 &&
  767. (objix_hdr.p_hdr.flags& (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
  768. (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {
  769. struct spiffs_dirent *e = (struct spiffs_dirent*)user_var_p;
  770. e->obj_id = obj_id;
  771. strcpy((char *)e->name, (char *)objix_hdr.name);
  772. e->type = objix_hdr.type;
  773. e->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
  774. e->pix = pix;
  775. return SPIFFS_OK;
  776. }
  777. return SPIFFS_VIS_COUNTINUE;
  778. }
  779. struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) {
  780. if (!SPIFFS_CHECK_MOUNT(d->fs)) {
  781. d->fs->err_code = SPIFFS_ERR_NOT_MOUNTED;
  782. return 0;
  783. }
  784. SPIFFS_LOCK(d->fs);
  785. spiffs_block_ix bix;
  786. int entry;
  787. s32_t res;
  788. struct spiffs_dirent *ret = 0;
  789. res = spiffs_obj_lu_find_entry_visitor(d->fs,
  790. d->block,
  791. d->entry,
  792. SPIFFS_VIS_NO_WRAP,
  793. 0,
  794. spiffs_read_dir_v,
  795. 0,
  796. e,
  797. &bix,
  798. &entry);
  799. if (res == SPIFFS_OK) {
  800. d->block = bix;
  801. d->entry = entry + 1;
  802. ret = e;
  803. } else {
  804. d->fs->err_code = res;
  805. }
  806. SPIFFS_UNLOCK(d->fs);
  807. return ret;
  808. }
  809. s32_t SPIFFS_closedir(spiffs_DIR *d) {
  810. SPIFFS_API_CHECK_CFG(d->fs);
  811. SPIFFS_API_CHECK_MOUNT(d->fs);
  812. return 0;
  813. }
  814. s32_t SPIFFS_check(spiffs *fs) {
  815. #if SPIFFS_READ_ONLY
  816. (void)fs;
  817. return SPIFFS_ERR_RO_NOT_IMPL;
  818. #else
  819. s32_t res;
  820. SPIFFS_API_CHECK_CFG(fs);
  821. SPIFFS_API_CHECK_MOUNT(fs);
  822. SPIFFS_LOCK(fs);
  823. res = spiffs_lookup_consistency_check(fs, 0);
  824. res = spiffs_object_index_consistency_check(fs);
  825. res = spiffs_page_consistency_check(fs);
  826. res = spiffs_obj_lu_scan(fs);
  827. SPIFFS_UNLOCK(fs);
  828. return res;
  829. #endif // SPIFFS_READ_ONLY
  830. }
  831. s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) {
  832. s32_t res = SPIFFS_OK;
  833. SPIFFS_API_CHECK_CFG(fs);
  834. SPIFFS_API_CHECK_MOUNT(fs);
  835. SPIFFS_LOCK(fs);
  836. u32_t pages_per_block = SPIFFS_PAGES_PER_BLOCK(fs);
  837. u32_t blocks = fs->block_count;
  838. u32_t obj_lu_pages = SPIFFS_OBJ_LOOKUP_PAGES(fs);
  839. u32_t data_page_size = SPIFFS_DATA_PAGE_SIZE(fs);
  840. u32_t total_data_pages = (blocks - 2) * (pages_per_block - obj_lu_pages) + 1; // -2 for spare blocks, +1 for emergency page
  841. if (total) {
  842. *total = total_data_pages * data_page_size;
  843. }
  844. if (used) {
  845. *used = fs->stats_p_allocated * data_page_size;
  846. }
  847. SPIFFS_UNLOCK(fs);
  848. return res;
  849. }
  850. s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages) {
  851. #if SPIFFS_READ_ONLY
  852. (void)fs; (void)max_free_pages;
  853. return SPIFFS_ERR_RO_NOT_IMPL;
  854. #else
  855. s32_t res;
  856. SPIFFS_API_CHECK_CFG(fs);
  857. SPIFFS_API_CHECK_MOUNT(fs);
  858. SPIFFS_LOCK(fs);
  859. res = spiffs_gc_quick(fs, max_free_pages);
  860. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  861. SPIFFS_UNLOCK(fs);
  862. return 0;
  863. #endif // SPIFFS_READ_ONLY
  864. }
  865. s32_t SPIFFS_gc(spiffs *fs, u32_t size) {
  866. #if SPIFFS_READ_ONLY
  867. (void)fs; (void)size;
  868. return SPIFFS_ERR_RO_NOT_IMPL;
  869. #else
  870. s32_t res;
  871. SPIFFS_API_CHECK_CFG(fs);
  872. SPIFFS_API_CHECK_MOUNT(fs);
  873. SPIFFS_LOCK(fs);
  874. res = spiffs_gc_check(fs, size);
  875. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  876. SPIFFS_UNLOCK(fs);
  877. return 0;
  878. #endif // SPIFFS_READ_ONLY
  879. }
  880. s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) {
  881. s32_t res;
  882. SPIFFS_API_CHECK_CFG(fs);
  883. SPIFFS_API_CHECK_MOUNT(fs);
  884. SPIFFS_LOCK(fs);
  885. fh = SPIFFS_FH_UNOFFS(fs, fh);
  886. spiffs_fd *fd;
  887. res = spiffs_fd_get(fs, fh, &fd);
  888. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  889. #if SPIFFS_CACHE_WR
  890. res = spiffs_fflush_cache(fs, fh);
  891. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  892. #endif
  893. res = (fd->fdoffset >= (fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size));
  894. SPIFFS_UNLOCK(fs);
  895. return res;
  896. }
  897. s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) {
  898. s32_t res;
  899. SPIFFS_API_CHECK_CFG(fs);
  900. SPIFFS_API_CHECK_MOUNT(fs);
  901. SPIFFS_LOCK(fs);
  902. fh = SPIFFS_FH_UNOFFS(fs, fh);
  903. spiffs_fd *fd;
  904. res = spiffs_fd_get(fs, fh, &fd);
  905. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  906. #if SPIFFS_CACHE_WR
  907. res = spiffs_fflush_cache(fs, fh);
  908. SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
  909. #endif
  910. res = fd->fdoffset;
  911. SPIFFS_UNLOCK(fs);
  912. return res;
  913. }
  914. s32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func) {
  915. SPIFFS_LOCK(fs);
  916. fs->file_cb_f = cb_func;
  917. SPIFFS_UNLOCK(fs);
  918. return 0;
  919. }
  920. #if SPIFFS_TEST_VISUALISATION
  921. s32_t SPIFFS_vis(spiffs *fs) {
  922. s32_t res = SPIFFS_OK;
  923. SPIFFS_API_CHECK_CFG(fs);
  924. SPIFFS_API_CHECK_MOUNT(fs);
  925. SPIFFS_LOCK(fs);
  926. int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));
  927. spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;
  928. spiffs_block_ix bix = 0;
  929. while (bix < fs->block_count) {
  930. // check each object lookup page
  931. int obj_lookup_page = 0;
  932. int cur_entry = 0;
  933. while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) {
  934. int entry_offset = obj_lookup_page * entries_per_page;
  935. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,
  936. 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);
  937. // check each entry
  938. while (res == SPIFFS_OK &&
  939. cur_entry - entry_offset < entries_per_page && cur_entry < (int)(SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs))) {
  940. spiffs_obj_id obj_id = obj_lu_buf[cur_entry-entry_offset];
  941. if (cur_entry == 0) {
  942. spiffs_printf("%4i ", bix);
  943. } else if ((cur_entry & 0x3f) == 0) {
  944. spiffs_printf(" ");
  945. }
  946. if (obj_id == SPIFFS_OBJ_ID_FREE) {
  947. spiffs_printf(SPIFFS_TEST_VIS_FREE_STR);
  948. } else if (obj_id == SPIFFS_OBJ_ID_DELETED) {
  949. spiffs_printf(SPIFFS_TEST_VIS_DELE_STR);
  950. } else if (obj_id & SPIFFS_OBJ_ID_IX_FLAG){
  951. spiffs_printf(SPIFFS_TEST_VIS_INDX_STR(obj_id));
  952. } else {
  953. spiffs_printf(SPIFFS_TEST_VIS_DATA_STR(obj_id));
  954. }
  955. cur_entry++;
  956. if ((cur_entry & 0x3f) == 0) {
  957. spiffs_printf("\n");
  958. }
  959. } // per entry
  960. obj_lookup_page++;
  961. } // per object lookup page
  962. spiffs_obj_id erase_count;
  963. res = _spiffs_rd(fs, SPIFFS_OP_C_READ | SPIFFS_OP_T_OBJ_LU2, 0,
  964. SPIFFS_ERASE_COUNT_PADDR(fs, bix),
  965. sizeof(spiffs_obj_id), (u8_t *)&erase_count);
  966. SPIFFS_CHECK_RES(res);
  967. if (erase_count != (spiffs_obj_id)-1) {
  968. spiffs_printf("\tera_cnt: %i\n", erase_count);
  969. } else {
  970. spiffs_printf("\tera_cnt: N/A\n");
  971. }
  972. bix++;
  973. } // per block
  974. spiffs_printf("era_cnt_max: %i\n", fs->max_erase_count);
  975. spiffs_printf("last_errno: %i\n", fs->err_code);
  976. spiffs_printf("blocks: %i\n", fs->block_count);
  977. spiffs_printf("free_blocks: %i\n", fs->free_blocks);
  978. spiffs_printf("page_alloc: %i\n", fs->stats_p_allocated);
  979. spiffs_printf("page_delet: %i\n", fs->stats_p_deleted);
  980. SPIFFS_UNLOCK(fs);
  981. u32_t total, used;
  982. SPIFFS_info(fs, &total, &used);
  983. spiffs_printf("used: %i of %i\n", used, total);
  984. return res;
  985. }
  986. #endif