spiffs_hydrogen.c 40 KB

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