spiffs_nucleus.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. #include "spiffs.h"
  2. #include "spiffs_nucleus.h"
  3. static s32_t spiffs_page_data_check(spiffs *fs, spiffs_fd *fd, spiffs_page_ix pix, spiffs_span_ix spix) {
  4. s32_t res = SPIFFS_OK;
  5. if (pix == (spiffs_page_ix)-1) {
  6. // referring to page 0xffff...., bad object index
  7. return SPIFFS_ERR_INDEX_REF_FREE;
  8. }
  9. if (pix % SPIFFS_PAGES_PER_BLOCK(fs) < SPIFFS_OBJ_LOOKUP_PAGES(fs)) {
  10. // referring to an object lookup page, bad object index
  11. return SPIFFS_ERR_INDEX_REF_LU;
  12. }
  13. if (pix > SPIFFS_MAX_PAGES(fs)) {
  14. // referring to a bad page
  15. return SPIFFS_ERR_INDEX_REF_INVALID;
  16. }
  17. #if SPIFFS_PAGE_CHECK
  18. spiffs_page_header ph;
  19. res = _spiffs_rd(
  20. fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,
  21. fd->file_nbr,
  22. SPIFFS_PAGE_TO_PADDR(fs, pix),
  23. sizeof(spiffs_page_header),
  24. (u8_t *)&ph);
  25. SPIFFS_CHECK_RES(res);
  26. SPIFFS_VALIDATE_DATA(ph, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG, spix);
  27. #endif
  28. return res;
  29. }
  30. #if !SPIFFS_READ_ONLY
  31. static s32_t spiffs_page_index_check(spiffs *fs, spiffs_fd *fd, spiffs_page_ix pix, spiffs_span_ix spix) {
  32. s32_t res = SPIFFS_OK;
  33. if (pix == (spiffs_page_ix)-1) {
  34. // referring to page 0xffff...., bad object index
  35. return SPIFFS_ERR_INDEX_FREE;
  36. }
  37. if (pix % SPIFFS_PAGES_PER_BLOCK(fs) < SPIFFS_OBJ_LOOKUP_PAGES(fs)) {
  38. // referring to an object lookup page, bad object index
  39. return SPIFFS_ERR_INDEX_LU;
  40. }
  41. if (pix > SPIFFS_MAX_PAGES(fs)) {
  42. // referring to a bad page
  43. return SPIFFS_ERR_INDEX_INVALID;
  44. }
  45. #if SPIFFS_PAGE_CHECK
  46. spiffs_page_header ph;
  47. res = _spiffs_rd(
  48. fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  49. fd->file_nbr,
  50. SPIFFS_PAGE_TO_PADDR(fs, pix),
  51. sizeof(spiffs_page_header),
  52. (u8_t *)&ph);
  53. SPIFFS_CHECK_RES(res);
  54. SPIFFS_VALIDATE_OBJIX(ph, fd->obj_id, spix);
  55. #endif
  56. return res;
  57. }
  58. #endif // !SPIFFS_READ_ONLY
  59. #if !SPIFFS_CACHE
  60. s32_t spiffs_phys_rd(
  61. spiffs *fs,
  62. u32_t addr,
  63. u32_t len,
  64. u8_t *dst) {
  65. return SPIFFS_HAL_READ(fs, addr, len, dst);
  66. }
  67. s32_t spiffs_phys_wr(
  68. spiffs *fs,
  69. u32_t addr,
  70. u32_t len,
  71. u8_t *src) {
  72. return SPIFFS_HAL_WRITE(fs, addr, len, src);
  73. }
  74. #endif
  75. #if !SPIFFS_READ_ONLY
  76. s32_t spiffs_phys_cpy(
  77. spiffs *fs,
  78. spiffs_file fh,
  79. u32_t dst,
  80. u32_t src,
  81. u32_t len) {
  82. (void)fh;
  83. s32_t res;
  84. u8_t b[SPIFFS_COPY_BUFFER_STACK];
  85. while (len > 0) {
  86. u32_t chunk_size = MIN(SPIFFS_COPY_BUFFER_STACK, len);
  87. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_MOVS, fh, src, chunk_size, b);
  88. SPIFFS_CHECK_RES(res);
  89. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_MOVD, fh, dst, chunk_size, b);
  90. SPIFFS_CHECK_RES(res);
  91. len -= chunk_size;
  92. src += chunk_size;
  93. dst += chunk_size;
  94. }
  95. return SPIFFS_OK;
  96. }
  97. #endif // !SPIFFS_READ_ONLY
  98. // Find object lookup entry containing given id with visitor.
  99. // Iterate over object lookup pages in each block until a given object id entry is found.
  100. // When found, the visitor function is called with block index, entry index and user data.
  101. // If visitor returns SPIFFS_VIS_CONTINUE, the search goes on. Otherwise, the search will be
  102. // ended and visitor's return code is returned to caller.
  103. // If no visitor is given (0) the search returns on first entry with matching object id.
  104. // If no match is found in all look up, SPIFFS_VIS_END is returned.
  105. // @param fs the file system
  106. // @param starting_block the starting block to start search in
  107. // @param starting_lu_entry the look up index entry to start search in
  108. // @param flags ored combination of SPIFFS_VIS_CHECK_ID, SPIFFS_VIS_CHECK_PH,
  109. // SPIFFS_VIS_NO_WRAP
  110. // @param obj_id argument object id
  111. // @param v visitor callback function
  112. // @param user_const_p any const pointer, passed to the callback visitor function
  113. // @param user_var_p any pointer, passed to the callback visitor function
  114. // @param block_ix reported block index where match was found
  115. // @param lu_entry reported look up index where match was found
  116. s32_t spiffs_obj_lu_find_entry_visitor(
  117. spiffs *fs,
  118. spiffs_block_ix starting_block,
  119. int starting_lu_entry,
  120. u8_t flags,
  121. spiffs_obj_id obj_id,
  122. spiffs_visitor_f v,
  123. const void *user_const_p,
  124. void *user_var_p,
  125. spiffs_block_ix *block_ix,
  126. int *lu_entry) {
  127. s32_t res = SPIFFS_OK;
  128. s32_t entry_count = fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs);
  129. spiffs_block_ix cur_block = starting_block;
  130. u32_t cur_block_addr = starting_block * SPIFFS_CFG_LOG_BLOCK_SZ(fs);
  131. spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;
  132. int cur_entry = starting_lu_entry;
  133. int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));
  134. // wrap initial
  135. if (cur_entry > (int)SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) - 1) {
  136. cur_entry = 0;
  137. cur_block++;
  138. cur_block_addr = cur_block * SPIFFS_CFG_LOG_BLOCK_SZ(fs);
  139. if (cur_block >= fs->block_count) {
  140. if (flags & SPIFFS_VIS_NO_WRAP) {
  141. return SPIFFS_VIS_END;
  142. } else {
  143. // block wrap
  144. cur_block = 0;
  145. cur_block_addr = 0;
  146. }
  147. }
  148. }
  149. // check each block
  150. while (res == SPIFFS_OK && entry_count > 0) {
  151. int obj_lookup_page = cur_entry / entries_per_page;
  152. // check each object lookup page
  153. while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) {
  154. int entry_offset = obj_lookup_page * entries_per_page;
  155. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,
  156. 0, cur_block_addr + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);
  157. // check each entry
  158. while (res == SPIFFS_OK &&
  159. cur_entry - entry_offset < entries_per_page && // for non-last obj lookup pages
  160. cur_entry < (int)SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) // for last obj lookup page
  161. {
  162. if ((flags & SPIFFS_VIS_CHECK_ID) == 0 || obj_lu_buf[cur_entry-entry_offset] == obj_id) {
  163. if (block_ix) *block_ix = cur_block;
  164. if (lu_entry) *lu_entry = cur_entry;
  165. if (v) {
  166. res = v(
  167. fs,
  168. (flags & SPIFFS_VIS_CHECK_PH) ? obj_id : obj_lu_buf[cur_entry-entry_offset],
  169. cur_block,
  170. cur_entry,
  171. user_const_p,
  172. user_var_p);
  173. if (res == SPIFFS_VIS_COUNTINUE || res == SPIFFS_VIS_COUNTINUE_RELOAD) {
  174. if (res == SPIFFS_VIS_COUNTINUE_RELOAD) {
  175. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,
  176. 0, cur_block_addr + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);
  177. SPIFFS_CHECK_RES(res);
  178. }
  179. res = SPIFFS_OK;
  180. cur_entry++;
  181. entry_count--;
  182. continue;
  183. } else {
  184. return res;
  185. }
  186. } else {
  187. return SPIFFS_OK;
  188. }
  189. }
  190. entry_count--;
  191. cur_entry++;
  192. } // per entry
  193. obj_lookup_page++;
  194. } // per object lookup page
  195. cur_entry = 0;
  196. cur_block++;
  197. cur_block_addr += SPIFFS_CFG_LOG_BLOCK_SZ(fs);
  198. if (cur_block >= fs->block_count) {
  199. if (flags & SPIFFS_VIS_NO_WRAP) {
  200. return SPIFFS_VIS_END;
  201. } else {
  202. // block wrap
  203. cur_block = 0;
  204. cur_block_addr = 0;
  205. }
  206. }
  207. } // per block
  208. SPIFFS_CHECK_RES(res);
  209. return SPIFFS_VIS_END;
  210. }
  211. #if !SPIFFS_READ_ONLY
  212. s32_t spiffs_erase_block(
  213. spiffs *fs,
  214. spiffs_block_ix bix) {
  215. s32_t res;
  216. u32_t addr = SPIFFS_BLOCK_TO_PADDR(fs, bix);
  217. s32_t size = SPIFFS_CFG_LOG_BLOCK_SZ(fs);
  218. // here we ignore res, just try erasing the block
  219. while (size > 0) {
  220. SPIFFS_DBG("erase "_SPIPRIad":"_SPIPRIi"\n", addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs));
  221. SPIFFS_HAL_ERASE(fs, addr, SPIFFS_CFG_PHYS_ERASE_SZ(fs));
  222. addr += SPIFFS_CFG_PHYS_ERASE_SZ(fs);
  223. size -= SPIFFS_CFG_PHYS_ERASE_SZ(fs);
  224. }
  225. fs->free_blocks++;
  226. // register erase count for this block
  227. res = _spiffs_wr(fs, SPIFFS_OP_C_WRTHRU | SPIFFS_OP_T_OBJ_LU2, 0,
  228. SPIFFS_ERASE_COUNT_PADDR(fs, bix),
  229. sizeof(spiffs_obj_id), (u8_t *)&fs->max_erase_count);
  230. SPIFFS_CHECK_RES(res);
  231. #if SPIFFS_USE_MAGIC
  232. // finally, write magic
  233. spiffs_obj_id magic = SPIFFS_MAGIC(fs, bix);
  234. res = _spiffs_wr(fs, SPIFFS_OP_C_WRTHRU | SPIFFS_OP_T_OBJ_LU2, 0,
  235. SPIFFS_MAGIC_PADDR(fs, bix),
  236. sizeof(spiffs_obj_id), (u8_t *)&magic);
  237. SPIFFS_CHECK_RES(res);
  238. #endif
  239. fs->max_erase_count++;
  240. if (fs->max_erase_count == SPIFFS_OBJ_ID_IX_FLAG) {
  241. fs->max_erase_count = 0;
  242. }
  243. return res;
  244. }
  245. #endif // !SPIFFS_READ_ONLY
  246. #if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
  247. s32_t spiffs_probe(
  248. spiffs_config *cfg) {
  249. s32_t res;
  250. u32_t paddr;
  251. spiffs dummy_fs; // create a dummy fs struct just to be able to use macros
  252. _SPIFFS_MEMCPY(&dummy_fs.cfg, cfg, sizeof(spiffs_config));
  253. dummy_fs.block_count = 0;
  254. // Read three magics, as one block may be in an aborted erase state.
  255. // At least two of these must contain magic and be in decreasing order.
  256. spiffs_obj_id magic[3];
  257. spiffs_obj_id bix_count[3];
  258. spiffs_block_ix bix;
  259. for (bix = 0; bix < 3; bix++) {
  260. paddr = SPIFFS_MAGIC_PADDR(&dummy_fs, bix);
  261. #if SPIFFS_HAL_CALLBACK_EXTRA
  262. // not any proper fs to report here, so callback with null
  263. // (cross fingers that no-one gets angry)
  264. res = cfg->hal_read_f((void *)0, paddr, sizeof(spiffs_obj_id), (u8_t *)&magic[bix]);
  265. #else
  266. res = cfg->hal_read_f(paddr, sizeof(spiffs_obj_id), (u8_t *)&magic[bix]);
  267. #endif
  268. bix_count[bix] = magic[bix] ^ SPIFFS_MAGIC(&dummy_fs, 0);
  269. SPIFFS_CHECK_RES(res);
  270. }
  271. // check that we have sane number of blocks
  272. if (bix_count[0] < 3) return SPIFFS_ERR_PROBE_TOO_FEW_BLOCKS;
  273. // check that the order is correct, take aborted erases in calculation
  274. // first block aborted erase
  275. if (magic[0] == (spiffs_obj_id)(-1) && bix_count[1] - bix_count[2] == 1) {
  276. return (bix_count[1]+1) * cfg->log_block_size;
  277. }
  278. // second block aborted erase
  279. if (magic[1] == (spiffs_obj_id)(-1) && bix_count[0] - bix_count[2] == 2) {
  280. return bix_count[0] * cfg->log_block_size;
  281. }
  282. // third block aborted erase
  283. if (magic[2] == (spiffs_obj_id)(-1) && bix_count[0] - bix_count[1] == 1) {
  284. return bix_count[0] * cfg->log_block_size;
  285. }
  286. // no block has aborted erase
  287. if (bix_count[0] - bix_count[1] == 1 && bix_count[1] - bix_count[2] == 1) {
  288. return bix_count[0] * cfg->log_block_size;
  289. }
  290. return SPIFFS_ERR_PROBE_NOT_A_FS;
  291. }
  292. #endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
  293. static s32_t spiffs_obj_lu_scan_v(
  294. spiffs *fs,
  295. spiffs_obj_id obj_id,
  296. spiffs_block_ix bix,
  297. int ix_entry,
  298. const void *user_const_p,
  299. void *user_var_p) {
  300. (void)bix;
  301. (void)user_const_p;
  302. (void)user_var_p;
  303. if (obj_id == SPIFFS_OBJ_ID_FREE) {
  304. if (ix_entry == 0) {
  305. fs->free_blocks++;
  306. // todo optimize further, return SPIFFS_NEXT_BLOCK
  307. }
  308. } else if (obj_id == SPIFFS_OBJ_ID_DELETED) {
  309. fs->stats_p_deleted++;
  310. } else {
  311. fs->stats_p_allocated++;
  312. }
  313. return SPIFFS_VIS_COUNTINUE;
  314. }
  315. // Scans thru all obj lu and counts free, deleted and used pages
  316. // Find the maximum block erase count
  317. // Checks magic if enabled
  318. s32_t spiffs_obj_lu_scan(
  319. spiffs *fs) {
  320. s32_t res;
  321. spiffs_block_ix bix;
  322. int entry;
  323. #if SPIFFS_USE_MAGIC
  324. spiffs_block_ix unerased_bix = (spiffs_block_ix)-1;
  325. #endif
  326. // find out erase count
  327. // if enabled, check magic
  328. bix = 0;
  329. spiffs_obj_id erase_count_final;
  330. spiffs_obj_id erase_count_min = SPIFFS_OBJ_ID_FREE;
  331. spiffs_obj_id erase_count_max = 0;
  332. while (bix < fs->block_count) {
  333. #if SPIFFS_USE_MAGIC
  334. spiffs_obj_id magic;
  335. res = _spiffs_rd(fs,
  336. SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  337. 0, SPIFFS_MAGIC_PADDR(fs, bix) ,
  338. sizeof(spiffs_obj_id), (u8_t *)&magic);
  339. SPIFFS_CHECK_RES(res);
  340. if (magic != SPIFFS_MAGIC(fs, bix)) {
  341. if (unerased_bix == (spiffs_block_ix)-1) {
  342. // allow one unerased block as it might be powered down during an erase
  343. unerased_bix = bix;
  344. } else {
  345. // more than one unerased block, bail out
  346. SPIFFS_CHECK_RES(SPIFFS_ERR_NOT_A_FS);
  347. }
  348. }
  349. #endif
  350. spiffs_obj_id erase_count;
  351. res = _spiffs_rd(fs,
  352. SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  353. 0, SPIFFS_ERASE_COUNT_PADDR(fs, bix) ,
  354. sizeof(spiffs_obj_id), (u8_t *)&erase_count);
  355. SPIFFS_CHECK_RES(res);
  356. if (erase_count != SPIFFS_OBJ_ID_FREE) {
  357. erase_count_min = MIN(erase_count_min, erase_count);
  358. erase_count_max = MAX(erase_count_max, erase_count);
  359. }
  360. bix++;
  361. }
  362. if (erase_count_min == 0 && erase_count_max == SPIFFS_OBJ_ID_FREE) {
  363. // clean system, set counter to zero
  364. erase_count_final = 0;
  365. } else if (erase_count_max - erase_count_min > (SPIFFS_OBJ_ID_FREE)/2) {
  366. // wrap, take min
  367. erase_count_final = erase_count_min+1;
  368. } else {
  369. erase_count_final = erase_count_max+1;
  370. }
  371. fs->max_erase_count = erase_count_final;
  372. #if SPIFFS_USE_MAGIC
  373. if (unerased_bix != (spiffs_block_ix)-1) {
  374. // found one unerased block, remedy
  375. SPIFFS_DBG("mount: erase block "_SPIPRIbl"\n", bix);
  376. #if SPIFFS_READ_ONLY
  377. res = SPIFFS_ERR_RO_ABORTED_OPERATION;
  378. #else
  379. res = spiffs_erase_block(fs, unerased_bix);
  380. #endif // SPIFFS_READ_ONLY
  381. SPIFFS_CHECK_RES(res);
  382. }
  383. #endif
  384. // count blocks
  385. fs->free_blocks = 0;
  386. fs->stats_p_allocated = 0;
  387. fs->stats_p_deleted = 0;
  388. res = spiffs_obj_lu_find_entry_visitor(fs,
  389. 0,
  390. 0,
  391. 0,
  392. 0,
  393. spiffs_obj_lu_scan_v,
  394. 0,
  395. 0,
  396. &bix,
  397. &entry);
  398. if (res == SPIFFS_VIS_END) {
  399. res = SPIFFS_OK;
  400. }
  401. SPIFFS_CHECK_RES(res);
  402. return res;
  403. }
  404. #if !SPIFFS_READ_ONLY
  405. // Find free object lookup entry
  406. // Iterate over object lookup pages in each block until a free object id entry is found
  407. s32_t spiffs_obj_lu_find_free(
  408. spiffs *fs,
  409. spiffs_block_ix starting_block,
  410. int starting_lu_entry,
  411. spiffs_block_ix *block_ix,
  412. int *lu_entry) {
  413. s32_t res;
  414. if (!fs->cleaning && fs->free_blocks < 2) {
  415. res = spiffs_gc_quick(fs, 0);
  416. if (res == SPIFFS_ERR_NO_DELETED_BLOCKS) {
  417. res = SPIFFS_OK;
  418. }
  419. SPIFFS_CHECK_RES(res);
  420. if (fs->free_blocks < 2) {
  421. return SPIFFS_ERR_FULL;
  422. }
  423. }
  424. res = spiffs_obj_lu_find_id(fs, starting_block, starting_lu_entry,
  425. SPIFFS_OBJ_ID_FREE, block_ix, lu_entry);
  426. if (res == SPIFFS_OK) {
  427. fs->free_cursor_block_ix = *block_ix;
  428. fs->free_cursor_obj_lu_entry = (*lu_entry) + 1;
  429. if (*lu_entry == 0) {
  430. fs->free_blocks--;
  431. }
  432. }
  433. if (res == SPIFFS_ERR_FULL) {
  434. SPIFFS_DBG("fs full\n");
  435. }
  436. return res;
  437. }
  438. #endif // !SPIFFS_READ_ONLY
  439. // Find object lookup entry containing given id
  440. // Iterate over object lookup pages in each block until a given object id entry is found
  441. s32_t spiffs_obj_lu_find_id(
  442. spiffs *fs,
  443. spiffs_block_ix starting_block,
  444. int starting_lu_entry,
  445. spiffs_obj_id obj_id,
  446. spiffs_block_ix *block_ix,
  447. int *lu_entry) {
  448. s32_t res = spiffs_obj_lu_find_entry_visitor(
  449. fs, starting_block, starting_lu_entry, SPIFFS_VIS_CHECK_ID, obj_id, 0, 0, 0, block_ix, lu_entry);
  450. if (res == SPIFFS_VIS_END) {
  451. res = SPIFFS_ERR_NOT_FOUND;
  452. }
  453. return res;
  454. }
  455. static s32_t spiffs_obj_lu_find_id_and_span_v(
  456. spiffs *fs,
  457. spiffs_obj_id obj_id,
  458. spiffs_block_ix bix,
  459. int ix_entry,
  460. const void *user_const_p,
  461. void *user_var_p) {
  462. s32_t res;
  463. spiffs_page_header ph;
  464. spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
  465. res = _spiffs_rd(fs, 0, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  466. SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_header), (u8_t *)&ph);
  467. SPIFFS_CHECK_RES(res);
  468. if (ph.obj_id == obj_id &&
  469. ph.span_ix == *((spiffs_span_ix*)user_var_p) &&
  470. (ph.flags & (SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED)) == SPIFFS_PH_FLAG_DELET &&
  471. !((obj_id & SPIFFS_OBJ_ID_IX_FLAG) && (ph.flags & SPIFFS_PH_FLAG_IXDELE) == 0 && ph.span_ix == 0) &&
  472. (user_const_p == 0 || *((const spiffs_page_ix*)user_const_p) != pix)) {
  473. return SPIFFS_OK;
  474. } else {
  475. return SPIFFS_VIS_COUNTINUE;
  476. }
  477. }
  478. // Find object lookup entry containing given id and span index
  479. // Iterate over object lookup pages in each block until a given object id entry is found
  480. s32_t spiffs_obj_lu_find_id_and_span(
  481. spiffs *fs,
  482. spiffs_obj_id obj_id,
  483. spiffs_span_ix spix,
  484. spiffs_page_ix exclusion_pix,
  485. spiffs_page_ix *pix) {
  486. s32_t res;
  487. spiffs_block_ix bix;
  488. int entry;
  489. res = spiffs_obj_lu_find_entry_visitor(fs,
  490. fs->cursor_block_ix,
  491. fs->cursor_obj_lu_entry,
  492. SPIFFS_VIS_CHECK_ID,
  493. obj_id,
  494. spiffs_obj_lu_find_id_and_span_v,
  495. exclusion_pix ? &exclusion_pix : 0,
  496. &spix,
  497. &bix,
  498. &entry);
  499. if (res == SPIFFS_VIS_END) {
  500. res = SPIFFS_ERR_NOT_FOUND;
  501. }
  502. SPIFFS_CHECK_RES(res);
  503. if (pix) {
  504. *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  505. }
  506. fs->cursor_block_ix = bix;
  507. fs->cursor_obj_lu_entry = entry;
  508. return res;
  509. }
  510. // Find object lookup entry containing given id and span index in page headers only
  511. // Iterate over object lookup pages in each block until a given object id entry is found
  512. s32_t spiffs_obj_lu_find_id_and_span_by_phdr(
  513. spiffs *fs,
  514. spiffs_obj_id obj_id,
  515. spiffs_span_ix spix,
  516. spiffs_page_ix exclusion_pix,
  517. spiffs_page_ix *pix) {
  518. s32_t res;
  519. spiffs_block_ix bix;
  520. int entry;
  521. res = spiffs_obj_lu_find_entry_visitor(fs,
  522. fs->cursor_block_ix,
  523. fs->cursor_obj_lu_entry,
  524. SPIFFS_VIS_CHECK_PH,
  525. obj_id,
  526. spiffs_obj_lu_find_id_and_span_v,
  527. exclusion_pix ? &exclusion_pix : 0,
  528. &spix,
  529. &bix,
  530. &entry);
  531. if (res == SPIFFS_VIS_END) {
  532. res = SPIFFS_ERR_NOT_FOUND;
  533. }
  534. SPIFFS_CHECK_RES(res);
  535. if (pix) {
  536. *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  537. }
  538. fs->cursor_block_ix = bix;
  539. fs->cursor_obj_lu_entry = entry;
  540. return res;
  541. }
  542. #if SPIFFS_IX_MAP
  543. // update index map of given fd with given object index data
  544. static void spiffs_update_ix_map(spiffs *fs,
  545. spiffs_fd *fd, spiffs_span_ix objix_spix, spiffs_page_object_ix *objix) {
  546. #if SPIFFS_SINGLETON
  547. (void)fs;
  548. #endif
  549. spiffs_ix_map *map = fd->ix_map;
  550. spiffs_span_ix map_objix_start_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, map->start_spix);
  551. spiffs_span_ix map_objix_end_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, map->end_spix);
  552. // check if updated ix is within map range
  553. if (objix_spix < map_objix_start_spix || objix_spix > map_objix_end_spix) {
  554. return;
  555. }
  556. // update memory mapped page index buffer to new pages
  557. // get range of updated object index map data span indices
  558. spiffs_span_ix objix_data_spix_start =
  559. SPIFFS_DATA_SPAN_IX_FOR_OBJ_IX_SPAN_IX(fs, objix_spix);
  560. spiffs_span_ix objix_data_spix_end = objix_data_spix_start +
  561. (objix_spix == 0 ? SPIFFS_OBJ_HDR_IX_LEN(fs) : SPIFFS_OBJ_IX_LEN(fs));
  562. // calc union of object index range and index map range array
  563. spiffs_span_ix map_spix = MAX(map->start_spix, objix_data_spix_start);
  564. spiffs_span_ix map_spix_end = MIN(map->end_spix + 1, objix_data_spix_end);
  565. while (map_spix < map_spix_end) {
  566. spiffs_page_ix objix_data_pix;
  567. if (objix_spix == 0) {
  568. // get data page from object index header page
  569. objix_data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix_header)))[map_spix];
  570. } else {
  571. // get data page from object index page
  572. objix_data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, map_spix)];
  573. }
  574. if (objix_data_pix == (spiffs_page_ix)-1) {
  575. // reached end of object, abort
  576. break;
  577. }
  578. map->map_buf[map_spix - map->start_spix] = objix_data_pix;
  579. SPIFFS_DBG("map "_SPIPRIid":"_SPIPRIsp" ("_SPIPRIsp"--"_SPIPRIsp") objix.spix:"_SPIPRIsp" to pix "_SPIPRIpg"\n",
  580. fd->obj_id, map_spix - map->start_spix,
  581. map->start_spix, map->end_spix,
  582. objix->p_hdr.span_ix,
  583. objix_data_pix);
  584. map_spix++;
  585. }
  586. }
  587. typedef struct {
  588. spiffs_fd *fd;
  589. u32_t remaining_objix_pages_to_visit;
  590. spiffs_span_ix map_objix_start_spix;
  591. spiffs_span_ix map_objix_end_spix;
  592. } spiffs_ix_map_populate_state;
  593. static s32_t spiffs_populate_ix_map_v(
  594. spiffs *fs,
  595. spiffs_obj_id obj_id,
  596. spiffs_block_ix bix,
  597. int ix_entry,
  598. const void *user_const_p,
  599. void *user_var_p) {
  600. (void)user_const_p;
  601. s32_t res;
  602. spiffs_ix_map_populate_state *state = (spiffs_ix_map_populate_state *)user_var_p;
  603. spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
  604. // load header to check it
  605. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  606. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  607. 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix), (u8_t *)objix);
  608. SPIFFS_CHECK_RES(res);
  609. SPIFFS_VALIDATE_OBJIX(objix->p_hdr, obj_id, objix->p_hdr.span_ix);
  610. // check if hdr is ok, and if objix range overlap with ix map range
  611. if ((objix->p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
  612. (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE) &&
  613. objix->p_hdr.span_ix >= state->map_objix_start_spix &&
  614. objix->p_hdr.span_ix <= state->map_objix_end_spix) {
  615. // ok, load rest of object index
  616. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  617. 0, SPIFFS_PAGE_TO_PADDR(fs, pix) + sizeof(spiffs_page_object_ix),
  618. SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix),
  619. (u8_t *)objix + sizeof(spiffs_page_object_ix));
  620. SPIFFS_CHECK_RES(res);
  621. spiffs_update_ix_map(fs, state->fd, objix->p_hdr.span_ix, objix);
  622. state->remaining_objix_pages_to_visit--;
  623. SPIFFS_DBG("map "_SPIPRIid" ("_SPIPRIsp"--"_SPIPRIsp") remaining objix pages "_SPIPRIi"\n",
  624. state->fd->obj_id,
  625. state->fd->ix_map->start_spix, state->fd->ix_map->end_spix,
  626. state->remaining_objix_pages_to_visit);
  627. }
  628. if (res == SPIFFS_OK) {
  629. res = state->remaining_objix_pages_to_visit ? SPIFFS_VIS_COUNTINUE : SPIFFS_VIS_END;
  630. }
  631. return res;
  632. }
  633. // populates index map, from vector entry start to vector entry end, inclusive
  634. s32_t spiffs_populate_ix_map(spiffs *fs, spiffs_fd *fd, u32_t vec_entry_start, u32_t vec_entry_end) {
  635. s32_t res;
  636. spiffs_ix_map *map = fd->ix_map;
  637. spiffs_ix_map_populate_state state;
  638. vec_entry_start = MIN((u32_t)(map->end_spix - map->start_spix), vec_entry_start);
  639. vec_entry_end = MAX((u32_t)(map->end_spix - map->start_spix), vec_entry_end);
  640. if (vec_entry_start > vec_entry_end) {
  641. return SPIFFS_ERR_IX_MAP_BAD_RANGE;
  642. }
  643. state.map_objix_start_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, map->start_spix + vec_entry_start);
  644. state.map_objix_end_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, map->start_spix + vec_entry_end);
  645. state.remaining_objix_pages_to_visit =
  646. state.map_objix_end_spix - state.map_objix_start_spix + 1;
  647. state.fd = fd;
  648. res = spiffs_obj_lu_find_entry_visitor(
  649. fs,
  650. SPIFFS_BLOCK_FOR_PAGE(fs, fd->objix_hdr_pix),
  651. SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, fd->objix_hdr_pix),
  652. SPIFFS_VIS_CHECK_ID,
  653. fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG,
  654. spiffs_populate_ix_map_v,
  655. 0,
  656. &state,
  657. 0,
  658. 0);
  659. if (res == SPIFFS_VIS_END) {
  660. res = SPIFFS_OK;
  661. }
  662. return res;
  663. }
  664. #endif
  665. #if !SPIFFS_READ_ONLY
  666. // Allocates a free defined page with given obj_id
  667. // Occupies object lookup entry and page
  668. // data may be NULL; where only page header is stored, len and page_offs is ignored
  669. s32_t spiffs_page_allocate_data(
  670. spiffs *fs,
  671. spiffs_obj_id obj_id,
  672. spiffs_page_header *ph,
  673. u8_t *data,
  674. u32_t len,
  675. u32_t page_offs,
  676. u8_t finalize,
  677. spiffs_page_ix *pix) {
  678. s32_t res = SPIFFS_OK;
  679. spiffs_block_ix bix;
  680. int entry;
  681. // find free entry
  682. res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);
  683. SPIFFS_CHECK_RES(res);
  684. // occupy page in object lookup
  685. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,
  686. 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t*)&obj_id);
  687. SPIFFS_CHECK_RES(res);
  688. fs->stats_p_allocated++;
  689. // write page header
  690. ph->flags &= ~SPIFFS_PH_FLAG_USED;
  691. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  692. 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry), sizeof(spiffs_page_header), (u8_t*)ph);
  693. SPIFFS_CHECK_RES(res);
  694. // write page data
  695. if (data) {
  696. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  697. 0,SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry) + sizeof(spiffs_page_header) + page_offs, len, data);
  698. SPIFFS_CHECK_RES(res);
  699. }
  700. // finalize header if necessary
  701. if (finalize && (ph->flags & SPIFFS_PH_FLAG_FINAL)) {
  702. ph->flags &= ~SPIFFS_PH_FLAG_FINAL;
  703. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  704. 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry) + offsetof(spiffs_page_header, flags),
  705. sizeof(u8_t),
  706. (u8_t *)&ph->flags);
  707. SPIFFS_CHECK_RES(res);
  708. }
  709. // return written page
  710. if (pix) {
  711. *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  712. }
  713. return res;
  714. }
  715. #endif // !SPIFFS_READ_ONLY
  716. #if !SPIFFS_READ_ONLY
  717. // Moves a page from src to a free page and finalizes it. Updates page index. Page data is given in param page.
  718. // If page data is null, provided header is used for metainfo and page data is physically copied.
  719. s32_t spiffs_page_move(
  720. spiffs *fs,
  721. spiffs_file fh,
  722. u8_t *page_data,
  723. spiffs_obj_id obj_id,
  724. spiffs_page_header *page_hdr,
  725. spiffs_page_ix src_pix,
  726. spiffs_page_ix *dst_pix) {
  727. s32_t res;
  728. u8_t was_final = 0;
  729. spiffs_page_header *p_hdr;
  730. spiffs_block_ix bix;
  731. int entry;
  732. spiffs_page_ix free_pix;
  733. // find free entry
  734. res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);
  735. SPIFFS_CHECK_RES(res);
  736. free_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  737. if (dst_pix) *dst_pix = free_pix;
  738. p_hdr = page_data ? (spiffs_page_header *)page_data : page_hdr;
  739. if (page_data) {
  740. // got page data
  741. was_final = (p_hdr->flags & SPIFFS_PH_FLAG_FINAL) == 0;
  742. // write unfinalized page
  743. p_hdr->flags |= SPIFFS_PH_FLAG_FINAL;
  744. p_hdr->flags &= ~SPIFFS_PH_FLAG_USED;
  745. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  746. 0, SPIFFS_PAGE_TO_PADDR(fs, free_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), page_data);
  747. } else {
  748. // copy page data
  749. res = spiffs_phys_cpy(fs, fh, SPIFFS_PAGE_TO_PADDR(fs, free_pix), SPIFFS_PAGE_TO_PADDR(fs, src_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs));
  750. }
  751. SPIFFS_CHECK_RES(res);
  752. // mark entry in destination object lookup
  753. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,
  754. 0, SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs, free_pix)) + SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, free_pix) * sizeof(spiffs_page_ix),
  755. sizeof(spiffs_obj_id),
  756. (u8_t *)&obj_id);
  757. SPIFFS_CHECK_RES(res);
  758. fs->stats_p_allocated++;
  759. if (was_final) {
  760. // mark finalized in destination page
  761. p_hdr->flags &= ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_USED);
  762. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  763. fh,
  764. SPIFFS_PAGE_TO_PADDR(fs, free_pix) + offsetof(spiffs_page_header, flags),
  765. sizeof(u8_t),
  766. (u8_t *)&p_hdr->flags);
  767. SPIFFS_CHECK_RES(res);
  768. }
  769. // mark source deleted
  770. res = spiffs_page_delete(fs, src_pix);
  771. return res;
  772. }
  773. #endif // !SPIFFS_READ_ONLY
  774. #if !SPIFFS_READ_ONLY
  775. // Deletes a page and removes it from object lookup.
  776. s32_t spiffs_page_delete(
  777. spiffs *fs,
  778. spiffs_page_ix pix) {
  779. s32_t res;
  780. // mark deleted entry in source object lookup
  781. spiffs_obj_id d_obj_id = SPIFFS_OBJ_ID_DELETED;
  782. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_DELE,
  783. 0,
  784. SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs, pix)) + SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix) * sizeof(spiffs_page_ix),
  785. sizeof(spiffs_obj_id),
  786. (u8_t *)&d_obj_id);
  787. SPIFFS_CHECK_RES(res);
  788. fs->stats_p_deleted++;
  789. fs->stats_p_allocated--;
  790. #if SPIFFS_SECURE_ERASE
  791. // Secure erase
  792. unsigned char data[SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header)];
  793. bzero(data, sizeof(data));
  794. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_DELE,
  795. 0,
  796. SPIFFS_PAGE_TO_PADDR(fs, pix) + sizeof(spiffs_page_header), sizeof(data), data);
  797. SPIFFS_CHECK_RES(res);
  798. #endif
  799. // mark deleted in source page
  800. u8_t flags = 0xff;
  801. #if SPIFFS_NO_BLIND_WRITES
  802. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,
  803. 0, SPIFFS_PAGE_TO_PADDR(fs, pix) + offsetof(spiffs_page_header, flags),
  804. sizeof(flags), &flags);
  805. SPIFFS_CHECK_RES(res);
  806. #endif
  807. flags &= ~(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED);
  808. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_DELE,
  809. 0,
  810. SPIFFS_PAGE_TO_PADDR(fs, pix) + offsetof(spiffs_page_header, flags),
  811. sizeof(flags), &flags);
  812. return res;
  813. }
  814. #endif // !SPIFFS_READ_ONLY
  815. #if !SPIFFS_READ_ONLY
  816. // Create an object index header page with empty index and undefined length
  817. s32_t spiffs_object_create(
  818. spiffs *fs,
  819. spiffs_obj_id obj_id,
  820. const u8_t name[],
  821. const u8_t meta[],
  822. spiffs_obj_type type,
  823. spiffs_page_ix *objix_hdr_pix) {
  824. s32_t res = SPIFFS_OK;
  825. spiffs_block_ix bix;
  826. spiffs_page_object_ix_header oix_hdr;
  827. int entry;
  828. res = spiffs_gc_check(fs, SPIFFS_DATA_PAGE_SIZE(fs));
  829. SPIFFS_CHECK_RES(res);
  830. obj_id |= SPIFFS_OBJ_ID_IX_FLAG;
  831. // find free entry
  832. res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);
  833. SPIFFS_CHECK_RES(res);
  834. SPIFFS_DBG("create: found free page @ "_SPIPRIpg" bix:"_SPIPRIbl" entry:"_SPIPRIsp"\n", (spiffs_page_ix)SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry), bix, entry);
  835. // occupy page in object lookup
  836. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,
  837. 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t*)&obj_id);
  838. SPIFFS_CHECK_RES(res);
  839. fs->stats_p_allocated++;
  840. // write empty object index page
  841. oix_hdr.p_hdr.obj_id = obj_id;
  842. oix_hdr.p_hdr.span_ix = 0;
  843. oix_hdr.p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_USED);
  844. oix_hdr.type = type;
  845. oix_hdr.size = SPIFFS_UNDEFINED_LEN; // keep ones so we can update later without wasting this page
  846. strncpy((char*)oix_hdr.name, (const char*)name, SPIFFS_OBJ_NAME_LEN);
  847. #if SPIFFS_OBJ_META_LEN
  848. if (meta) {
  849. _SPIFFS_MEMCPY(oix_hdr.meta, meta, SPIFFS_OBJ_META_LEN);
  850. } else {
  851. memset(oix_hdr.meta, 0xff, SPIFFS_OBJ_META_LEN);
  852. }
  853. #else
  854. (void) meta;
  855. #endif
  856. // update page
  857. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  858. 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry), sizeof(spiffs_page_object_ix_header), (u8_t*)&oix_hdr);
  859. SPIFFS_CHECK_RES(res);
  860. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)&oix_hdr,
  861. SPIFFS_EV_IX_NEW, obj_id, 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry), SPIFFS_UNDEFINED_LEN);
  862. if (objix_hdr_pix) {
  863. *objix_hdr_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  864. }
  865. return res;
  866. }
  867. #endif // !SPIFFS_READ_ONLY
  868. #if !SPIFFS_READ_ONLY
  869. // update object index header with any combination of name/size/index
  870. // new_objix_hdr_data may be null, if so the object index header page is loaded
  871. // name may be null, if so name is not changed
  872. // size may be null, if so size is not changed
  873. s32_t spiffs_object_update_index_hdr(
  874. spiffs *fs,
  875. spiffs_fd *fd,
  876. spiffs_obj_id obj_id,
  877. spiffs_page_ix objix_hdr_pix,
  878. u8_t *new_objix_hdr_data,
  879. const u8_t name[],
  880. const u8_t meta[],
  881. u32_t size,
  882. spiffs_page_ix *new_pix) {
  883. s32_t res = SPIFFS_OK;
  884. spiffs_page_object_ix_header *objix_hdr;
  885. spiffs_page_ix new_objix_hdr_pix;
  886. obj_id |= SPIFFS_OBJ_ID_IX_FLAG;
  887. if (new_objix_hdr_data) {
  888. // object index header page already given to us, no need to load it
  889. objix_hdr = (spiffs_page_object_ix_header *)new_objix_hdr_data;
  890. } else {
  891. // read object index header page
  892. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  893. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_hdr_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  894. SPIFFS_CHECK_RES(res);
  895. objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  896. }
  897. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, obj_id, 0);
  898. // change name
  899. if (name) {
  900. strncpy((char*)objix_hdr->name, (const char*)name, SPIFFS_OBJ_NAME_LEN);
  901. }
  902. #if SPIFFS_OBJ_META_LEN
  903. if (meta) {
  904. _SPIFFS_MEMCPY(objix_hdr->meta, meta, SPIFFS_OBJ_META_LEN);
  905. }
  906. #else
  907. (void) meta;
  908. #endif
  909. if (size) {
  910. objix_hdr->size = size;
  911. }
  912. // move and update page
  913. res = spiffs_page_move(fs, fd == 0 ? 0 : fd->file_nbr, (u8_t*)objix_hdr, obj_id, 0, objix_hdr_pix, &new_objix_hdr_pix);
  914. if (res == SPIFFS_OK) {
  915. if (new_pix) {
  916. *new_pix = new_objix_hdr_pix;
  917. }
  918. // callback on object index update
  919. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)objix_hdr,
  920. new_objix_hdr_data ? SPIFFS_EV_IX_UPD : SPIFFS_EV_IX_UPD_HDR,
  921. obj_id, objix_hdr->p_hdr.span_ix, new_objix_hdr_pix, objix_hdr->size);
  922. if (fd) fd->objix_hdr_pix = new_objix_hdr_pix; // if this is not in the registered cluster
  923. }
  924. return res;
  925. }
  926. #endif // !SPIFFS_READ_ONLY
  927. void spiffs_cb_object_event(
  928. spiffs *fs,
  929. spiffs_page_object_ix *objix,
  930. int ev,
  931. spiffs_obj_id obj_id_raw,
  932. spiffs_span_ix spix,
  933. spiffs_page_ix new_pix,
  934. u32_t new_size) {
  935. #if SPIFFS_IX_MAP == 0
  936. (void)objix;
  937. #endif
  938. // update index caches in all file descriptors
  939. spiffs_obj_id obj_id = obj_id_raw & ~SPIFFS_OBJ_ID_IX_FLAG;
  940. u32_t i;
  941. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  942. SPIFFS_DBG(" CALLBACK %s obj_id:"_SPIPRIid" spix:"_SPIPRIsp" npix:"_SPIPRIpg" nsz:"_SPIPRIi"\n", (const char *[]){"UPD", "NEW", "DEL", "MOV", "HUP","???"}[MIN(ev,5)],
  943. obj_id_raw, spix, new_pix, new_size);
  944. for (i = 0; i < fs->fd_count; i++) {
  945. spiffs_fd *cur_fd = &fds[i];
  946. if ((cur_fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) != obj_id) continue; // fd not related to updated file
  947. #if !SPIFFS_TEMPORAL_FD_CACHE
  948. if (cur_fd->file_nbr == 0) continue; // fd closed
  949. #endif
  950. if (spix == 0) { // object index header update
  951. if (ev != SPIFFS_EV_IX_DEL) {
  952. #if SPIFFS_TEMPORAL_FD_CACHE
  953. if (cur_fd->score == 0) continue; // never used fd
  954. #endif
  955. SPIFFS_DBG(" callback: setting fd "_SPIPRIfd":"_SPIPRIid"(fdoffs:"_SPIPRIi" offs:"_SPIPRIi") objix_hdr_pix to "_SPIPRIpg", size:"_SPIPRIi"\n",
  956. SPIFFS_FH_OFFS(fs, cur_fd->file_nbr), cur_fd->obj_id, cur_fd->fdoffset, cur_fd->offset, new_pix, new_size);
  957. cur_fd->objix_hdr_pix = new_pix;
  958. if (new_size != 0) {
  959. // update size and offsets for fds to this file
  960. cur_fd->size = new_size;
  961. u32_t act_new_size = new_size == SPIFFS_UNDEFINED_LEN ? 0 : new_size;
  962. #if SPIFFS_CACHE_WR
  963. if (act_new_size > 0 && cur_fd->cache_page) {
  964. act_new_size = MAX(act_new_size, cur_fd->cache_page->offset + cur_fd->cache_page->size);
  965. }
  966. #endif
  967. if (cur_fd->offset > act_new_size) {
  968. cur_fd->offset = act_new_size;
  969. }
  970. if (cur_fd->fdoffset > act_new_size) {
  971. cur_fd->fdoffset = act_new_size;
  972. }
  973. #if SPIFFS_CACHE_WR
  974. if (cur_fd->cache_page && cur_fd->cache_page->offset > act_new_size+1) {
  975. SPIFFS_CACHE_DBG("CACHE_DROP: file trunced, dropping cache page "_SPIPRIi", no writeback\n", cur_fd->cache_page->ix);
  976. spiffs_cache_fd_release(fs, cur_fd->cache_page);
  977. }
  978. #endif
  979. }
  980. } else {
  981. // removing file
  982. #if SPIFFS_CACHE_WR
  983. if (cur_fd->file_nbr && cur_fd->cache_page) {
  984. SPIFFS_CACHE_DBG("CACHE_DROP: file deleted, dropping cache page "_SPIPRIi", no writeback\n", cur_fd->cache_page->ix);
  985. spiffs_cache_fd_release(fs, cur_fd->cache_page);
  986. }
  987. #endif
  988. SPIFFS_DBG(" callback: release fd "_SPIPRIfd":"_SPIPRIid" span:"_SPIPRIsp" objix_pix to "_SPIPRIpg"\n", SPIFFS_FH_OFFS(fs, cur_fd->file_nbr), cur_fd->obj_id, spix, new_pix);
  989. cur_fd->file_nbr = 0;
  990. cur_fd->obj_id = SPIFFS_OBJ_ID_DELETED;
  991. }
  992. } // object index header update
  993. if (cur_fd->cursor_objix_spix == spix) {
  994. if (ev != SPIFFS_EV_IX_DEL) {
  995. SPIFFS_DBG(" callback: setting fd "_SPIPRIfd":"_SPIPRIid" span:"_SPIPRIsp" objix_pix to "_SPIPRIpg"\n", SPIFFS_FH_OFFS(fs, cur_fd->file_nbr), cur_fd->obj_id, spix, new_pix);
  996. cur_fd->cursor_objix_pix = new_pix;
  997. } else {
  998. cur_fd->cursor_objix_pix = 0;
  999. }
  1000. }
  1001. } // fd update loop
  1002. #if SPIFFS_IX_MAP
  1003. // update index maps
  1004. if (ev == SPIFFS_EV_IX_UPD || ev == SPIFFS_EV_IX_NEW) {
  1005. for (i = 0; i < fs->fd_count; i++) {
  1006. spiffs_fd *cur_fd = &fds[i];
  1007. // check fd opened, having ix map, match obj id
  1008. if (cur_fd->file_nbr == 0 ||
  1009. cur_fd->ix_map == 0 ||
  1010. (cur_fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) != obj_id) continue;
  1011. SPIFFS_DBG(" callback: map ix update fd "_SPIPRIfd":"_SPIPRIid" span:"_SPIPRIsp"\n", SPIFFS_FH_OFFS(fs, cur_fd->file_nbr), cur_fd->obj_id, spix);
  1012. spiffs_update_ix_map(fs, cur_fd, spix, objix);
  1013. }
  1014. }
  1015. #endif
  1016. // callback to user if object index header
  1017. if (fs->file_cb_f && spix == 0 && (obj_id_raw & SPIFFS_OBJ_ID_IX_FLAG)) {
  1018. spiffs_fileop_type op;
  1019. if (ev == SPIFFS_EV_IX_NEW) {
  1020. op = SPIFFS_CB_CREATED;
  1021. } else if (ev == SPIFFS_EV_IX_UPD ||
  1022. ev == SPIFFS_EV_IX_MOV ||
  1023. ev == SPIFFS_EV_IX_UPD_HDR) {
  1024. op = SPIFFS_CB_UPDATED;
  1025. } else if (ev == SPIFFS_EV_IX_DEL) {
  1026. op = SPIFFS_CB_DELETED;
  1027. } else {
  1028. SPIFFS_DBG(" callback: WARNING unknown callback event "_SPIPRIi"\n", ev);
  1029. return; // bail out
  1030. }
  1031. fs->file_cb_f(fs, op, obj_id, new_pix);
  1032. }
  1033. }
  1034. // Open object by id
  1035. s32_t spiffs_object_open_by_id(
  1036. spiffs *fs,
  1037. spiffs_obj_id obj_id,
  1038. spiffs_fd *fd,
  1039. spiffs_flags flags,
  1040. spiffs_mode mode) {
  1041. s32_t res = SPIFFS_OK;
  1042. spiffs_page_ix pix;
  1043. res = spiffs_obj_lu_find_id_and_span(fs, obj_id | SPIFFS_OBJ_ID_IX_FLAG, 0, 0, &pix);
  1044. SPIFFS_CHECK_RES(res);
  1045. res = spiffs_object_open_by_page(fs, pix, fd, flags, mode);
  1046. return res;
  1047. }
  1048. // Open object by page index
  1049. s32_t spiffs_object_open_by_page(
  1050. spiffs *fs,
  1051. spiffs_page_ix pix,
  1052. spiffs_fd *fd,
  1053. spiffs_flags flags,
  1054. spiffs_mode mode) {
  1055. (void)mode;
  1056. s32_t res = SPIFFS_OK;
  1057. spiffs_page_object_ix_header oix_hdr;
  1058. spiffs_obj_id obj_id;
  1059. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1060. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&oix_hdr);
  1061. SPIFFS_CHECK_RES(res);
  1062. spiffs_block_ix bix = SPIFFS_BLOCK_FOR_PAGE(fs, pix);
  1063. int entry = SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix);
  1064. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,
  1065. 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t *)&obj_id);
  1066. fd->fs = fs;
  1067. fd->objix_hdr_pix = pix;
  1068. fd->size = oix_hdr.size;
  1069. fd->offset = 0;
  1070. fd->cursor_objix_pix = pix;
  1071. fd->cursor_objix_spix = 0;
  1072. fd->obj_id = obj_id;
  1073. fd->flags = flags;
  1074. SPIFFS_VALIDATE_OBJIX(oix_hdr.p_hdr, fd->obj_id, 0);
  1075. SPIFFS_DBG("open: fd "_SPIPRIfd" is obj id "_SPIPRIid"\n", SPIFFS_FH_OFFS(fs, fd->file_nbr), fd->obj_id);
  1076. return res;
  1077. }
  1078. #if !SPIFFS_READ_ONLY
  1079. // Append to object
  1080. // keep current object index (header) page in fs->work buffer
  1081. s32_t spiffs_object_append(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) {
  1082. spiffs *fs = fd->fs;
  1083. s32_t res = SPIFFS_OK;
  1084. u32_t written = 0;
  1085. SPIFFS_DBG("append: "_SPIPRIi" bytes @ offs "_SPIPRIi" of size "_SPIPRIi"\n", len, offset, fd->size);
  1086. if (offset > fd->size) {
  1087. SPIFFS_DBG("append: offset reversed to size\n");
  1088. offset = fd->size;
  1089. }
  1090. res = spiffs_gc_check(fs, len + SPIFFS_DATA_PAGE_SIZE(fs)); // add an extra page of data worth for meta
  1091. if (res != SPIFFS_OK) {
  1092. SPIFFS_DBG("append: gc check fail "_SPIPRIi"\n", res);
  1093. }
  1094. SPIFFS_CHECK_RES(res);
  1095. spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  1096. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  1097. spiffs_page_header p_hdr;
  1098. spiffs_span_ix cur_objix_spix = 0;
  1099. spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;
  1100. spiffs_page_ix cur_objix_pix = fd->objix_hdr_pix;
  1101. spiffs_page_ix new_objix_hdr_page;
  1102. spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);
  1103. spiffs_page_ix data_page;
  1104. u32_t page_offs = offset % SPIFFS_DATA_PAGE_SIZE(fs);
  1105. // write all data
  1106. while (res == SPIFFS_OK && written < len) {
  1107. // calculate object index page span index
  1108. cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  1109. // handle storing and loading of object indices
  1110. if (cur_objix_spix != prev_objix_spix) {
  1111. // new object index page
  1112. // within this clause we return directly if something fails, object index mess-up
  1113. if (written > 0) {
  1114. // store previous object index page, unless first pass
  1115. SPIFFS_DBG("append: "_SPIPRIid" store objix "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", fd->obj_id,
  1116. cur_objix_pix, prev_objix_spix, written);
  1117. if (prev_objix_spix == 0) {
  1118. // this is an update to object index header page
  1119. objix_hdr->size = offset+written;
  1120. if (offset == 0) {
  1121. // was an empty object, update same page (size was 0xffffffff)
  1122. res = spiffs_page_index_check(fs, fd, cur_objix_pix, 0);
  1123. SPIFFS_CHECK_RES(res);
  1124. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  1125. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1126. SPIFFS_CHECK_RES(res);
  1127. } else {
  1128. // was a nonempty object, update to new page
  1129. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1130. fd->objix_hdr_pix, fs->work, 0, 0, offset+written, &new_objix_hdr_page);
  1131. SPIFFS_CHECK_RES(res);
  1132. SPIFFS_DBG("append: "_SPIPRIid" store new objix_hdr, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", fd->obj_id,
  1133. new_objix_hdr_page, 0, written);
  1134. }
  1135. } else {
  1136. // this is an update to an object index page
  1137. res = spiffs_page_index_check(fs, fd, cur_objix_pix, prev_objix_spix);
  1138. SPIFFS_CHECK_RES(res);
  1139. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  1140. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1141. SPIFFS_CHECK_RES(res);
  1142. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)fs->work,
  1143. SPIFFS_EV_IX_UPD,fd->obj_id, objix->p_hdr.span_ix, cur_objix_pix, 0);
  1144. // update length in object index header page
  1145. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1146. fd->objix_hdr_pix, 0, 0, 0, offset+written, &new_objix_hdr_page);
  1147. SPIFFS_CHECK_RES(res);
  1148. SPIFFS_DBG("append: "_SPIPRIid" store new size I "_SPIPRIi" in objix_hdr, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", fd->obj_id,
  1149. offset+written, new_objix_hdr_page, 0, written);
  1150. }
  1151. fd->size = offset+written;
  1152. fd->offset = offset+written;
  1153. }
  1154. // create or load new object index page
  1155. if (cur_objix_spix == 0) {
  1156. // load object index header page, must always exist
  1157. SPIFFS_DBG("append: "_SPIPRIid" load objixhdr page "_SPIPRIpg":"_SPIPRIsp"\n", fd->obj_id, cur_objix_pix, cur_objix_spix);
  1158. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1159. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1160. SPIFFS_CHECK_RES(res);
  1161. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  1162. } else {
  1163. spiffs_span_ix len_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, (fd->size-1)/SPIFFS_DATA_PAGE_SIZE(fs));
  1164. // on subsequent passes, create a new object index page
  1165. if (written > 0 || cur_objix_spix > len_objix_spix) {
  1166. p_hdr.obj_id = fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG;
  1167. p_hdr.span_ix = cur_objix_spix;
  1168. p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX);
  1169. res = spiffs_page_allocate_data(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG,
  1170. &p_hdr, 0, 0, 0, 1, &cur_objix_pix);
  1171. SPIFFS_CHECK_RES(res);
  1172. // quick "load" of new object index page
  1173. memset(fs->work, 0xff, SPIFFS_CFG_LOG_PAGE_SZ(fs));
  1174. _SPIFFS_MEMCPY(fs->work, &p_hdr, sizeof(spiffs_page_header));
  1175. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)fs->work,
  1176. SPIFFS_EV_IX_NEW, fd->obj_id, cur_objix_spix, cur_objix_pix, 0);
  1177. SPIFFS_DBG("append: "_SPIPRIid" create objix page, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", fd->obj_id
  1178. , cur_objix_pix, cur_objix_spix, written);
  1179. } else {
  1180. // on first pass, we load existing object index page
  1181. spiffs_page_ix pix;
  1182. SPIFFS_DBG("append: "_SPIPRIid" find objix span_ix:"_SPIPRIsp"\n", fd->obj_id, cur_objix_spix);
  1183. if (fd->cursor_objix_spix == cur_objix_spix) {
  1184. pix = fd->cursor_objix_pix;
  1185. } else {
  1186. res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &pix);
  1187. SPIFFS_CHECK_RES(res);
  1188. }
  1189. SPIFFS_DBG("append: "_SPIPRIid" found object index at page "_SPIPRIpg" [fd size "_SPIPRIi"]\n", fd->obj_id, pix, fd->size);
  1190. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1191. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1192. SPIFFS_CHECK_RES(res);
  1193. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  1194. cur_objix_pix = pix;
  1195. }
  1196. fd->cursor_objix_pix = cur_objix_pix;
  1197. fd->cursor_objix_spix = cur_objix_spix;
  1198. fd->offset = offset+written;
  1199. fd->size = offset+written;
  1200. }
  1201. prev_objix_spix = cur_objix_spix;
  1202. }
  1203. // write data
  1204. u32_t to_write = MIN(len-written, SPIFFS_DATA_PAGE_SIZE(fs) - page_offs);
  1205. if (page_offs == 0) {
  1206. // at beginning of a page, allocate and write a new page of data
  1207. p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
  1208. p_hdr.span_ix = data_spix;
  1209. p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL); // finalize immediately
  1210. res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,
  1211. &p_hdr, &data[written], to_write, page_offs, 1, &data_page);
  1212. SPIFFS_DBG("append: "_SPIPRIid" store new data page, "_SPIPRIpg":"_SPIPRIsp" offset:"_SPIPRIi", len "_SPIPRIi", written "_SPIPRIi"\n", fd->obj_id,
  1213. data_page, data_spix, page_offs, to_write, written);
  1214. } else {
  1215. // append to existing page, fill out free data in existing page
  1216. if (cur_objix_spix == 0) {
  1217. // get data page from object index header page
  1218. data_page = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];
  1219. } else {
  1220. // get data page from object index page
  1221. data_page = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];
  1222. }
  1223. res = spiffs_page_data_check(fs, fd, data_page, data_spix);
  1224. SPIFFS_CHECK_RES(res);
  1225. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  1226. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, data_page) + sizeof(spiffs_page_header) + page_offs, to_write, &data[written]);
  1227. SPIFFS_DBG("append: "_SPIPRIid" store to existing data page, "_SPIPRIpg":"_SPIPRIsp" offset:"_SPIPRIi", len "_SPIPRIi", written "_SPIPRIi"\n", fd->obj_id
  1228. , data_page, data_spix, page_offs, to_write, written);
  1229. }
  1230. if (res != SPIFFS_OK) break;
  1231. // update memory representation of object index page with new data page
  1232. if (cur_objix_spix == 0) {
  1233. // update object index header page
  1234. ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = data_page;
  1235. SPIFFS_DBG("append: "_SPIPRIid" wrote page "_SPIPRIpg" to objix_hdr entry "_SPIPRIsp" in mem\n", fd->obj_id
  1236. , data_page, data_spix);
  1237. objix_hdr->size = offset+written;
  1238. } else {
  1239. // update object index page
  1240. ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = data_page;
  1241. SPIFFS_DBG("append: "_SPIPRIid" wrote page "_SPIPRIpg" to objix entry "_SPIPRIsp" in mem\n", fd->obj_id
  1242. , data_page, (spiffs_span_ix)SPIFFS_OBJ_IX_ENTRY(fs, data_spix));
  1243. }
  1244. // update internals
  1245. page_offs = 0;
  1246. data_spix++;
  1247. written += to_write;
  1248. } // while all data
  1249. fd->size = offset+written;
  1250. fd->offset = offset+written;
  1251. fd->cursor_objix_pix = cur_objix_pix;
  1252. fd->cursor_objix_spix = cur_objix_spix;
  1253. // finalize updated object indices
  1254. s32_t res2 = SPIFFS_OK;
  1255. if (cur_objix_spix != 0) {
  1256. // wrote beyond object index header page
  1257. // write last modified object index page, unless object header index page
  1258. SPIFFS_DBG("append: "_SPIPRIid" store objix page, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", fd->obj_id,
  1259. cur_objix_pix, cur_objix_spix, written);
  1260. res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);
  1261. SPIFFS_CHECK_RES(res2);
  1262. res2 = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  1263. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1264. SPIFFS_CHECK_RES(res2);
  1265. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)fs->work,
  1266. SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, cur_objix_pix, 0);
  1267. // update size in object header index page
  1268. res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1269. fd->objix_hdr_pix, 0, 0, 0, offset+written, &new_objix_hdr_page);
  1270. SPIFFS_DBG("append: "_SPIPRIid" store new size II "_SPIPRIi" in objix_hdr, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi", res "_SPIPRIi"\n", fd->obj_id
  1271. , offset+written, new_objix_hdr_page, 0, written, res2);
  1272. SPIFFS_CHECK_RES(res2);
  1273. } else {
  1274. // wrote within object index header page
  1275. if (offset == 0) {
  1276. // wrote to empty object - simply update size and write whole page
  1277. objix_hdr->size = offset+written;
  1278. SPIFFS_DBG("append: "_SPIPRIid" store fresh objix_hdr page, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", fd->obj_id
  1279. , cur_objix_pix, cur_objix_spix, written);
  1280. res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);
  1281. SPIFFS_CHECK_RES(res2);
  1282. res2 = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  1283. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1284. SPIFFS_CHECK_RES(res2);
  1285. // callback on object index update
  1286. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)fs->work,
  1287. SPIFFS_EV_IX_UPD_HDR, fd->obj_id, objix_hdr->p_hdr.span_ix, cur_objix_pix, objix_hdr->size);
  1288. } else {
  1289. // modifying object index header page, update size and make new copy
  1290. res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1291. fd->objix_hdr_pix, fs->work, 0, 0, offset+written, &new_objix_hdr_page);
  1292. SPIFFS_DBG("append: "_SPIPRIid" store modified objix_hdr page, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", fd->obj_id
  1293. , new_objix_hdr_page, 0, written);
  1294. SPIFFS_CHECK_RES(res2);
  1295. }
  1296. }
  1297. return res;
  1298. } // spiffs_object_append
  1299. #endif // !SPIFFS_READ_ONLY
  1300. #if !SPIFFS_READ_ONLY
  1301. // Modify object
  1302. // keep current object index (header) page in fs->work buffer
  1303. s32_t spiffs_object_modify(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) {
  1304. spiffs *fs = fd->fs;
  1305. s32_t res = SPIFFS_OK;
  1306. u32_t written = 0;
  1307. res = spiffs_gc_check(fs, len + SPIFFS_DATA_PAGE_SIZE(fs));
  1308. SPIFFS_CHECK_RES(res);
  1309. spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  1310. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  1311. spiffs_page_header p_hdr;
  1312. spiffs_span_ix cur_objix_spix = 0;
  1313. spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;
  1314. spiffs_page_ix cur_objix_pix = fd->objix_hdr_pix;
  1315. spiffs_page_ix new_objix_hdr_pix;
  1316. spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);
  1317. spiffs_page_ix data_pix;
  1318. u32_t page_offs = offset % SPIFFS_DATA_PAGE_SIZE(fs);
  1319. // write all data
  1320. while (res == SPIFFS_OK && written < len) {
  1321. // calculate object index page span index
  1322. cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  1323. // handle storing and loading of object indices
  1324. if (cur_objix_spix != prev_objix_spix) {
  1325. // new object index page
  1326. // within this clause we return directly if something fails, object index mess-up
  1327. if (written > 0) {
  1328. // store previous object index (header) page, unless first pass
  1329. if (prev_objix_spix == 0) {
  1330. // store previous object index header page
  1331. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1332. fd->objix_hdr_pix, fs->work, 0, 0, 0, &new_objix_hdr_pix);
  1333. SPIFFS_DBG("modify: store modified objix_hdr page, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", new_objix_hdr_pix, 0, written);
  1334. SPIFFS_CHECK_RES(res);
  1335. } else {
  1336. // store new version of previous object index page
  1337. spiffs_page_ix new_objix_pix;
  1338. res = spiffs_page_index_check(fs, fd, cur_objix_pix, prev_objix_spix);
  1339. SPIFFS_CHECK_RES(res);
  1340. res = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix, fd->obj_id, 0, cur_objix_pix, &new_objix_pix);
  1341. SPIFFS_DBG("modify: store previous modified objix page, "_SPIPRIid":"_SPIPRIsp", written "_SPIPRIi"\n", new_objix_pix, objix->p_hdr.span_ix, written);
  1342. SPIFFS_CHECK_RES(res);
  1343. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)objix,
  1344. SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);
  1345. }
  1346. }
  1347. // load next object index page
  1348. if (cur_objix_spix == 0) {
  1349. // load object index header page, must exist
  1350. SPIFFS_DBG("modify: load objixhdr page "_SPIPRIpg":"_SPIPRIsp"\n", cur_objix_pix, cur_objix_spix);
  1351. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1352. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1353. SPIFFS_CHECK_RES(res);
  1354. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  1355. } else {
  1356. // load existing object index page on first pass
  1357. spiffs_page_ix pix;
  1358. SPIFFS_DBG("modify: find objix span_ix:"_SPIPRIsp"\n", cur_objix_spix);
  1359. if (fd->cursor_objix_spix == cur_objix_spix) {
  1360. pix = fd->cursor_objix_pix;
  1361. } else {
  1362. res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &pix);
  1363. SPIFFS_CHECK_RES(res);
  1364. }
  1365. SPIFFS_DBG("modify: found object index at page "_SPIPRIpg"\n", pix);
  1366. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1367. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1368. SPIFFS_CHECK_RES(res);
  1369. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  1370. cur_objix_pix = pix;
  1371. }
  1372. fd->cursor_objix_pix = cur_objix_pix;
  1373. fd->cursor_objix_spix = cur_objix_spix;
  1374. fd->offset = offset+written;
  1375. prev_objix_spix = cur_objix_spix;
  1376. }
  1377. // write partial data
  1378. u32_t to_write = MIN(len-written, SPIFFS_DATA_PAGE_SIZE(fs) - page_offs);
  1379. spiffs_page_ix orig_data_pix;
  1380. if (cur_objix_spix == 0) {
  1381. // get data page from object index header page
  1382. orig_data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];
  1383. } else {
  1384. // get data page from object index page
  1385. orig_data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];
  1386. }
  1387. p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
  1388. p_hdr.span_ix = data_spix;
  1389. p_hdr.flags = 0xff;
  1390. if (page_offs == 0 && to_write == SPIFFS_DATA_PAGE_SIZE(fs)) {
  1391. // a full page, allocate and write a new page of data
  1392. res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,
  1393. &p_hdr, &data[written], to_write, page_offs, 1, &data_pix);
  1394. SPIFFS_DBG("modify: store new data page, "_SPIPRIpg":"_SPIPRIsp" offset:"_SPIPRIi", len "_SPIPRIi", written "_SPIPRIi"\n", data_pix, data_spix, page_offs, to_write, written);
  1395. } else {
  1396. // write to existing page, allocate new and copy unmodified data
  1397. res = spiffs_page_data_check(fs, fd, orig_data_pix, data_spix);
  1398. SPIFFS_CHECK_RES(res);
  1399. res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,
  1400. &p_hdr, 0, 0, 0, 0, &data_pix);
  1401. if (res != SPIFFS_OK) break;
  1402. // copy unmodified data
  1403. if (page_offs > 0) {
  1404. // before modification
  1405. res = spiffs_phys_cpy(fs, fd->file_nbr,
  1406. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header),
  1407. SPIFFS_PAGE_TO_PADDR(fs, orig_data_pix) + sizeof(spiffs_page_header),
  1408. page_offs);
  1409. if (res != SPIFFS_OK) break;
  1410. }
  1411. if (page_offs + to_write < SPIFFS_DATA_PAGE_SIZE(fs)) {
  1412. // after modification
  1413. res = spiffs_phys_cpy(fs, fd->file_nbr,
  1414. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + page_offs + to_write,
  1415. SPIFFS_PAGE_TO_PADDR(fs, orig_data_pix) + sizeof(spiffs_page_header) + page_offs + to_write,
  1416. SPIFFS_DATA_PAGE_SIZE(fs) - (page_offs + to_write));
  1417. if (res != SPIFFS_OK) break;
  1418. }
  1419. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  1420. fd->file_nbr,
  1421. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + page_offs, to_write, &data[written]);
  1422. if (res != SPIFFS_OK) break;
  1423. p_hdr.flags &= ~SPIFFS_PH_FLAG_FINAL;
  1424. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  1425. fd->file_nbr,
  1426. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + offsetof(spiffs_page_header, flags),
  1427. sizeof(u8_t),
  1428. (u8_t *)&p_hdr.flags);
  1429. if (res != SPIFFS_OK) break;
  1430. SPIFFS_DBG("modify: store to existing data page, src:"_SPIPRIpg", dst:"_SPIPRIpg":"_SPIPRIsp" offset:"_SPIPRIi", len "_SPIPRIi", written "_SPIPRIi"\n", orig_data_pix, data_pix, data_spix, page_offs, to_write, written);
  1431. }
  1432. // delete original data page
  1433. res = spiffs_page_delete(fs, orig_data_pix);
  1434. if (res != SPIFFS_OK) break;
  1435. // update memory representation of object index page with new data page
  1436. if (cur_objix_spix == 0) {
  1437. // update object index header page
  1438. ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = data_pix;
  1439. SPIFFS_DBG("modify: wrote page "_SPIPRIpg" to objix_hdr entry "_SPIPRIsp" in mem\n", data_pix, data_spix);
  1440. } else {
  1441. // update object index page
  1442. ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = data_pix;
  1443. SPIFFS_DBG("modify: wrote page "_SPIPRIpg" to objix entry "_SPIPRIsp" in mem\n", data_pix, (spiffs_span_ix)SPIFFS_OBJ_IX_ENTRY(fs, data_spix));
  1444. }
  1445. // update internals
  1446. page_offs = 0;
  1447. data_spix++;
  1448. written += to_write;
  1449. } // while all data
  1450. fd->offset = offset+written;
  1451. fd->cursor_objix_pix = cur_objix_pix;
  1452. fd->cursor_objix_spix = cur_objix_spix;
  1453. // finalize updated object indices
  1454. s32_t res2 = SPIFFS_OK;
  1455. if (cur_objix_spix != 0) {
  1456. // wrote beyond object index header page
  1457. // write last modified object index page
  1458. // move and update page
  1459. spiffs_page_ix new_objix_pix;
  1460. res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);
  1461. SPIFFS_CHECK_RES(res2);
  1462. res2 = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix, fd->obj_id, 0, cur_objix_pix, &new_objix_pix);
  1463. SPIFFS_DBG("modify: store modified objix page, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", new_objix_pix, cur_objix_spix, written);
  1464. fd->cursor_objix_pix = new_objix_pix;
  1465. fd->cursor_objix_spix = cur_objix_spix;
  1466. SPIFFS_CHECK_RES(res2);
  1467. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)objix,
  1468. SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);
  1469. } else {
  1470. // wrote within object index header page
  1471. res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1472. fd->objix_hdr_pix, fs->work, 0, 0, 0, &new_objix_hdr_pix);
  1473. SPIFFS_DBG("modify: store modified objix_hdr page, "_SPIPRIpg":"_SPIPRIsp", written "_SPIPRIi"\n", new_objix_hdr_pix, 0, written);
  1474. SPIFFS_CHECK_RES(res2);
  1475. }
  1476. return res;
  1477. } // spiffs_object_modify
  1478. #endif // !SPIFFS_READ_ONLY
  1479. static s32_t spiffs_object_find_object_index_header_by_name_v(
  1480. spiffs *fs,
  1481. spiffs_obj_id obj_id,
  1482. spiffs_block_ix bix,
  1483. int ix_entry,
  1484. const void *user_const_p,
  1485. void *user_var_p) {
  1486. (void)user_var_p;
  1487. s32_t res;
  1488. spiffs_page_object_ix_header objix_hdr;
  1489. spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
  1490. if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||
  1491. (obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0) {
  1492. return SPIFFS_VIS_COUNTINUE;
  1493. }
  1494. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  1495. 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
  1496. SPIFFS_CHECK_RES(res);
  1497. if (objix_hdr.p_hdr.span_ix == 0 &&
  1498. (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
  1499. (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {
  1500. if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) {
  1501. return SPIFFS_OK;
  1502. }
  1503. }
  1504. return SPIFFS_VIS_COUNTINUE;
  1505. }
  1506. // Finds object index header page by name
  1507. s32_t spiffs_object_find_object_index_header_by_name(
  1508. spiffs *fs,
  1509. const u8_t name[SPIFFS_OBJ_NAME_LEN],
  1510. spiffs_page_ix *pix) {
  1511. s32_t res;
  1512. spiffs_block_ix bix;
  1513. int entry;
  1514. res = spiffs_obj_lu_find_entry_visitor(fs,
  1515. fs->cursor_block_ix,
  1516. fs->cursor_obj_lu_entry,
  1517. 0,
  1518. 0,
  1519. spiffs_object_find_object_index_header_by_name_v,
  1520. name,
  1521. 0,
  1522. &bix,
  1523. &entry);
  1524. if (res == SPIFFS_VIS_END) {
  1525. res = SPIFFS_ERR_NOT_FOUND;
  1526. }
  1527. SPIFFS_CHECK_RES(res);
  1528. if (pix) {
  1529. *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  1530. }
  1531. fs->cursor_block_ix = bix;
  1532. fs->cursor_obj_lu_entry = entry;
  1533. return res;
  1534. }
  1535. #if !SPIFFS_READ_ONLY
  1536. // Truncates object to new size. If new size is null, object may be removed totally
  1537. s32_t spiffs_object_truncate(
  1538. spiffs_fd *fd,
  1539. u32_t new_size,
  1540. u8_t remove_full) {
  1541. s32_t res = SPIFFS_OK;
  1542. spiffs *fs = fd->fs;
  1543. if ((fd->size == SPIFFS_UNDEFINED_LEN || fd->size == 0) && !remove_full) {
  1544. // no op
  1545. return res;
  1546. }
  1547. // need 2 pages if not removing: object index page + possibly chopped data page
  1548. if (remove_full == 0) {
  1549. res = spiffs_gc_check(fs, SPIFFS_DATA_PAGE_SIZE(fs) * 2);
  1550. SPIFFS_CHECK_RES(res);
  1551. }
  1552. spiffs_page_ix objix_pix = fd->objix_hdr_pix;
  1553. spiffs_span_ix data_spix = (fd->size > 0 ? fd->size-1 : 0) / SPIFFS_DATA_PAGE_SIZE(fs);
  1554. u32_t cur_size = fd->size == (u32_t)SPIFFS_UNDEFINED_LEN ? 0 : fd->size ;
  1555. spiffs_span_ix cur_objix_spix = 0;
  1556. spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;
  1557. spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  1558. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  1559. spiffs_page_ix data_pix;
  1560. spiffs_page_ix new_objix_hdr_pix;
  1561. // before truncating, check if object is to be fully removed and mark this
  1562. if (remove_full && new_size == 0) {
  1563. u8_t flags = ~( SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE);
  1564. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  1565. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, fd->objix_hdr_pix) + offsetof(spiffs_page_header, flags),
  1566. sizeof(u8_t),
  1567. (u8_t *)&flags);
  1568. SPIFFS_CHECK_RES(res);
  1569. }
  1570. // delete from end of object until desired len is reached
  1571. while (cur_size > new_size) {
  1572. cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  1573. // put object index for current data span index in work buffer
  1574. if (prev_objix_spix != cur_objix_spix) {
  1575. if (prev_objix_spix != (spiffs_span_ix)-1) {
  1576. // remove previous object index page
  1577. SPIFFS_DBG("truncate: delete objix page "_SPIPRIpg":"_SPIPRIsp"\n", objix_pix, prev_objix_spix);
  1578. res = spiffs_page_index_check(fs, fd, objix_pix, prev_objix_spix);
  1579. SPIFFS_CHECK_RES(res);
  1580. res = spiffs_page_delete(fs, objix_pix);
  1581. SPIFFS_CHECK_RES(res);
  1582. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)0,
  1583. SPIFFS_EV_IX_DEL, fd->obj_id, objix->p_hdr.span_ix, objix_pix, 0);
  1584. if (prev_objix_spix > 0) {
  1585. // Update object index header page, unless we totally want to remove the file.
  1586. // If fully removing, we're not keeping consistency as good as when storing the header between chunks,
  1587. // would we be aborted. But when removing full files, a crammed system may otherwise
  1588. // report ERR_FULL a la windows. We cannot have that.
  1589. // Hence, take the risk - if aborted, a file check would free the lost pages and mend things
  1590. // as the file is marked as fully deleted in the beginning.
  1591. if (remove_full == 0) {
  1592. SPIFFS_DBG("truncate: update objix hdr page "_SPIPRIpg":"_SPIPRIsp" to size "_SPIPRIi"\n", fd->objix_hdr_pix, prev_objix_spix, cur_size);
  1593. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1594. fd->objix_hdr_pix, 0, 0, 0, cur_size, &new_objix_hdr_pix);
  1595. SPIFFS_CHECK_RES(res);
  1596. }
  1597. fd->size = cur_size;
  1598. }
  1599. }
  1600. // load current object index (header) page
  1601. if (cur_objix_spix == 0) {
  1602. objix_pix = fd->objix_hdr_pix;
  1603. } else {
  1604. res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &objix_pix);
  1605. SPIFFS_CHECK_RES(res);
  1606. }
  1607. SPIFFS_DBG("truncate: load objix page "_SPIPRIpg":"_SPIPRIsp" for data spix:"_SPIPRIsp"\n", objix_pix, cur_objix_spix, data_spix);
  1608. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1609. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1610. SPIFFS_CHECK_RES(res);
  1611. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  1612. fd->cursor_objix_pix = objix_pix;
  1613. fd->cursor_objix_spix = cur_objix_spix;
  1614. fd->offset = cur_size;
  1615. prev_objix_spix = cur_objix_spix;
  1616. }
  1617. if (cur_objix_spix == 0) {
  1618. // get data page from object index header page
  1619. data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];
  1620. ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = SPIFFS_OBJ_ID_FREE;
  1621. } else {
  1622. // get data page from object index page
  1623. data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];
  1624. ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = SPIFFS_OBJ_ID_FREE;
  1625. }
  1626. SPIFFS_DBG("truncate: got data pix "_SPIPRIpg"\n", data_pix);
  1627. if (new_size == 0 || remove_full || cur_size - new_size >= SPIFFS_DATA_PAGE_SIZE(fs)) {
  1628. // delete full data page
  1629. res = spiffs_page_data_check(fs, fd, data_pix, data_spix);
  1630. if (res != SPIFFS_ERR_DELETED && res != SPIFFS_OK && res != SPIFFS_ERR_INDEX_REF_FREE) {
  1631. SPIFFS_DBG("truncate: err validating data pix "_SPIPRIi"\n", res);
  1632. break;
  1633. }
  1634. if (res == SPIFFS_OK) {
  1635. res = spiffs_page_delete(fs, data_pix);
  1636. if (res != SPIFFS_OK) {
  1637. SPIFFS_DBG("truncate: err deleting data pix "_SPIPRIi"\n", res);
  1638. break;
  1639. }
  1640. } else if (res == SPIFFS_ERR_DELETED || res == SPIFFS_ERR_INDEX_REF_FREE) {
  1641. res = SPIFFS_OK;
  1642. }
  1643. // update current size
  1644. if (cur_size % SPIFFS_DATA_PAGE_SIZE(fs) == 0) {
  1645. cur_size -= SPIFFS_DATA_PAGE_SIZE(fs);
  1646. } else {
  1647. cur_size -= cur_size % SPIFFS_DATA_PAGE_SIZE(fs);
  1648. }
  1649. fd->size = cur_size;
  1650. fd->offset = cur_size;
  1651. SPIFFS_DBG("truncate: delete data page "_SPIPRIpg" for data spix:"_SPIPRIsp", cur_size:"_SPIPRIi"\n", data_pix, data_spix, cur_size);
  1652. } else {
  1653. // delete last page, partially
  1654. spiffs_page_header p_hdr;
  1655. spiffs_page_ix new_data_pix;
  1656. u32_t bytes_to_remove = SPIFFS_DATA_PAGE_SIZE(fs) - (new_size % SPIFFS_DATA_PAGE_SIZE(fs));
  1657. SPIFFS_DBG("truncate: delete "_SPIPRIi" bytes from data page "_SPIPRIpg" for data spix:"_SPIPRIsp", cur_size:"_SPIPRIi"\n", bytes_to_remove, data_pix, data_spix, cur_size);
  1658. res = spiffs_page_data_check(fs, fd, data_pix, data_spix);
  1659. if (res != SPIFFS_OK) break;
  1660. p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
  1661. p_hdr.span_ix = data_spix;
  1662. p_hdr.flags = 0xff;
  1663. // allocate new page and copy unmodified data
  1664. res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,
  1665. &p_hdr, 0, 0, 0, 0, &new_data_pix);
  1666. if (res != SPIFFS_OK) break;
  1667. res = spiffs_phys_cpy(fs, 0,
  1668. SPIFFS_PAGE_TO_PADDR(fs, new_data_pix) + sizeof(spiffs_page_header),
  1669. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header),
  1670. SPIFFS_DATA_PAGE_SIZE(fs) - bytes_to_remove);
  1671. if (res != SPIFFS_OK) break;
  1672. // delete original data page
  1673. res = spiffs_page_delete(fs, data_pix);
  1674. if (res != SPIFFS_OK) break;
  1675. p_hdr.flags &= ~SPIFFS_PH_FLAG_FINAL;
  1676. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  1677. fd->file_nbr,
  1678. SPIFFS_PAGE_TO_PADDR(fs, new_data_pix) + offsetof(spiffs_page_header, flags),
  1679. sizeof(u8_t),
  1680. (u8_t *)&p_hdr.flags);
  1681. if (res != SPIFFS_OK) break;
  1682. // update memory representation of object index page with new data page
  1683. if (cur_objix_spix == 0) {
  1684. // update object index header page
  1685. ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = new_data_pix;
  1686. SPIFFS_DBG("truncate: wrote page "_SPIPRIpg" to objix_hdr entry "_SPIPRIsp" in mem\n", new_data_pix, (spiffs_span_ix)SPIFFS_OBJ_IX_ENTRY(fs, data_spix));
  1687. } else {
  1688. // update object index page
  1689. ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = new_data_pix;
  1690. SPIFFS_DBG("truncate: wrote page "_SPIPRIpg" to objix entry "_SPIPRIsp" in mem\n", new_data_pix, (spiffs_span_ix)SPIFFS_OBJ_IX_ENTRY(fs, data_spix));
  1691. }
  1692. cur_size = new_size;
  1693. fd->size = new_size;
  1694. fd->offset = cur_size;
  1695. break;
  1696. }
  1697. data_spix--;
  1698. } // while all data
  1699. // update object indices
  1700. if (cur_objix_spix == 0) {
  1701. // update object index header page
  1702. if (cur_size == 0) {
  1703. if (remove_full) {
  1704. // remove object altogether
  1705. SPIFFS_DBG("truncate: remove object index header page "_SPIPRIpg"\n", objix_pix);
  1706. res = spiffs_page_index_check(fs, fd, objix_pix, 0);
  1707. SPIFFS_CHECK_RES(res);
  1708. res = spiffs_page_delete(fs, objix_pix);
  1709. SPIFFS_CHECK_RES(res);
  1710. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)0,
  1711. SPIFFS_EV_IX_DEL, fd->obj_id, 0, objix_pix, 0);
  1712. } else {
  1713. // make uninitialized object
  1714. SPIFFS_DBG("truncate: reset objix_hdr page "_SPIPRIpg"\n", objix_pix);
  1715. memset(fs->work + sizeof(spiffs_page_object_ix_header), 0xff,
  1716. SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix_header));
  1717. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1718. objix_pix, fs->work, 0, 0, SPIFFS_UNDEFINED_LEN, &new_objix_hdr_pix);
  1719. SPIFFS_CHECK_RES(res);
  1720. }
  1721. } else {
  1722. // update object index header page
  1723. SPIFFS_DBG("truncate: update object index header page with indices and size\n");
  1724. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1725. objix_pix, fs->work, 0, 0, cur_size, &new_objix_hdr_pix);
  1726. SPIFFS_CHECK_RES(res);
  1727. }
  1728. } else {
  1729. // update both current object index page and object index header page
  1730. spiffs_page_ix new_objix_pix;
  1731. res = spiffs_page_index_check(fs, fd, objix_pix, cur_objix_spix);
  1732. SPIFFS_CHECK_RES(res);
  1733. // move and update object index page
  1734. res = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix_hdr, fd->obj_id, 0, objix_pix, &new_objix_pix);
  1735. SPIFFS_CHECK_RES(res);
  1736. spiffs_cb_object_event(fs, (spiffs_page_object_ix *)objix_hdr,
  1737. SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);
  1738. SPIFFS_DBG("truncate: store modified objix page, "_SPIPRIpg":"_SPIPRIsp"\n", new_objix_pix, cur_objix_spix);
  1739. fd->cursor_objix_pix = new_objix_pix;
  1740. fd->cursor_objix_spix = cur_objix_spix;
  1741. fd->offset = cur_size;
  1742. // update object index header page with new size
  1743. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1744. fd->objix_hdr_pix, 0, 0, 0, cur_size, &new_objix_hdr_pix);
  1745. SPIFFS_CHECK_RES(res);
  1746. }
  1747. fd->size = cur_size;
  1748. return res;
  1749. } // spiffs_object_truncate
  1750. #endif // !SPIFFS_READ_ONLY
  1751. s32_t spiffs_object_read(
  1752. spiffs_fd *fd,
  1753. u32_t offset,
  1754. u32_t len,
  1755. u8_t *dst) {
  1756. s32_t res = SPIFFS_OK;
  1757. spiffs *fs = fd->fs;
  1758. spiffs_page_ix objix_pix;
  1759. spiffs_page_ix data_pix;
  1760. spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);
  1761. u32_t cur_offset = offset;
  1762. spiffs_span_ix cur_objix_spix;
  1763. spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;
  1764. spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  1765. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  1766. while (cur_offset < offset + len) {
  1767. #if SPIFFS_IX_MAP
  1768. // check if we have a memory, index map and if so, if we're within index map's range
  1769. // and if so, if the entry is populated
  1770. if (fd->ix_map && data_spix >= fd->ix_map->start_spix && data_spix <= fd->ix_map->end_spix
  1771. && fd->ix_map->map_buf[data_spix - fd->ix_map->start_spix]) {
  1772. data_pix = fd->ix_map->map_buf[data_spix - fd->ix_map->start_spix];
  1773. } else {
  1774. #endif
  1775. cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  1776. if (prev_objix_spix != cur_objix_spix) {
  1777. // load current object index (header) page
  1778. if (cur_objix_spix == 0) {
  1779. objix_pix = fd->objix_hdr_pix;
  1780. } else {
  1781. SPIFFS_DBG("read: find objix "_SPIPRIid":"_SPIPRIsp"\n", fd->obj_id, cur_objix_spix);
  1782. if (fd->cursor_objix_spix == cur_objix_spix) {
  1783. objix_pix = fd->cursor_objix_pix;
  1784. } else {
  1785. res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &objix_pix);
  1786. SPIFFS_CHECK_RES(res);
  1787. }
  1788. }
  1789. SPIFFS_DBG("read: load objix page "_SPIPRIpg":"_SPIPRIsp" for data spix:"_SPIPRIsp"\n", objix_pix, cur_objix_spix, data_spix);
  1790. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1791. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1792. SPIFFS_CHECK_RES(res);
  1793. SPIFFS_VALIDATE_OBJIX(objix->p_hdr, fd->obj_id, cur_objix_spix);
  1794. fd->offset = cur_offset;
  1795. fd->cursor_objix_pix = objix_pix;
  1796. fd->cursor_objix_spix = cur_objix_spix;
  1797. prev_objix_spix = cur_objix_spix;
  1798. }
  1799. if (cur_objix_spix == 0) {
  1800. // get data page from object index header page
  1801. data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];
  1802. } else {
  1803. // get data page from object index page
  1804. data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];
  1805. }
  1806. #if SPIFFS_IX_MAP
  1807. }
  1808. #endif
  1809. // all remaining data
  1810. u32_t len_to_read = offset + len - cur_offset;
  1811. // remaining data in page
  1812. len_to_read = MIN(len_to_read, SPIFFS_DATA_PAGE_SIZE(fs) - (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)));
  1813. // remaining data in file
  1814. len_to_read = MIN(len_to_read, fd->size - cur_offset);
  1815. SPIFFS_DBG("read: offset:"_SPIPRIi" rd:"_SPIPRIi" data spix:"_SPIPRIsp" is data_pix:"_SPIPRIpg" addr:"_SPIPRIad"\n", cur_offset, len_to_read, data_spix, data_pix,
  1816. (u32_t)(SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs))));
  1817. if (len_to_read <= 0) {
  1818. res = SPIFFS_ERR_END_OF_OBJECT;
  1819. break;
  1820. }
  1821. res = spiffs_page_data_check(fs, fd, data_pix, data_spix);
  1822. SPIFFS_CHECK_RES(res);
  1823. res = _spiffs_rd(
  1824. fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,
  1825. fd->file_nbr,
  1826. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)),
  1827. len_to_read,
  1828. dst);
  1829. SPIFFS_CHECK_RES(res);
  1830. dst += len_to_read;
  1831. cur_offset += len_to_read;
  1832. fd->offset = cur_offset;
  1833. data_spix++;
  1834. }
  1835. return res;
  1836. }
  1837. #if !SPIFFS_READ_ONLY
  1838. typedef struct {
  1839. spiffs_obj_id min_obj_id;
  1840. spiffs_obj_id max_obj_id;
  1841. u32_t compaction;
  1842. const u8_t *conflicting_name;
  1843. } spiffs_free_obj_id_state;
  1844. static s32_t spiffs_obj_lu_find_free_obj_id_bitmap_v(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry,
  1845. const void *user_const_p, void *user_var_p) {
  1846. if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED) {
  1847. spiffs_obj_id min_obj_id = *((spiffs_obj_id*)user_var_p);
  1848. const u8_t *conflicting_name = (const u8_t*)user_const_p;
  1849. // if conflicting name parameter is given, also check if this name is found in object index hdrs
  1850. if (conflicting_name && (id & SPIFFS_OBJ_ID_IX_FLAG)) {
  1851. spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
  1852. int res;
  1853. spiffs_page_object_ix_header objix_hdr;
  1854. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  1855. 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
  1856. SPIFFS_CHECK_RES(res);
  1857. if (objix_hdr.p_hdr.span_ix == 0 &&
  1858. (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
  1859. (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {
  1860. if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) {
  1861. return SPIFFS_ERR_CONFLICTING_NAME;
  1862. }
  1863. }
  1864. }
  1865. id &= ~SPIFFS_OBJ_ID_IX_FLAG;
  1866. u32_t bit_ix = (id-min_obj_id) & 7;
  1867. int byte_ix = (id-min_obj_id) >> 3;
  1868. if (byte_ix >= 0 && (u32_t)byte_ix < SPIFFS_CFG_LOG_PAGE_SZ(fs)) {
  1869. fs->work[byte_ix] |= (1<<bit_ix);
  1870. }
  1871. }
  1872. return SPIFFS_VIS_COUNTINUE;
  1873. }
  1874. static s32_t spiffs_obj_lu_find_free_obj_id_compact_v(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry,
  1875. const void *user_const_p, void *user_var_p) {
  1876. (void)user_var_p;
  1877. if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED && (id & SPIFFS_OBJ_ID_IX_FLAG)) {
  1878. s32_t res;
  1879. const spiffs_free_obj_id_state *state = (const spiffs_free_obj_id_state*)user_const_p;
  1880. spiffs_page_object_ix_header objix_hdr;
  1881. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  1882. 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, ix_entry), sizeof(spiffs_page_object_ix_header), (u8_t*)&objix_hdr);
  1883. if (res == SPIFFS_OK && objix_hdr.p_hdr.span_ix == 0 &&
  1884. ((objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET)) ==
  1885. (SPIFFS_PH_FLAG_DELET))) {
  1886. // ok object look up entry
  1887. if (state->conflicting_name && strcmp((const char *)state->conflicting_name, (char *)objix_hdr.name) == 0) {
  1888. return SPIFFS_ERR_CONFLICTING_NAME;
  1889. }
  1890. id &= ~SPIFFS_OBJ_ID_IX_FLAG;
  1891. if (id >= state->min_obj_id && id <= state->max_obj_id) {
  1892. u8_t *map = (u8_t *)fs->work;
  1893. int ix = (id - state->min_obj_id) / state->compaction;
  1894. //SPIFFS_DBG("free_obj_id: add ix "_SPIPRIi" for id "_SPIPRIid" min"_SPIPRIid" max"_SPIPRIid" comp:"_SPIPRIi"\n", ix, id, state->min_obj_id, state->max_obj_id, state->compaction);
  1895. map[ix]++;
  1896. }
  1897. }
  1898. }
  1899. return SPIFFS_VIS_COUNTINUE;
  1900. }
  1901. // Scans thru all object lookup for object index header pages. If total possible number of
  1902. // object ids cannot fit into a work buffer, these are grouped. When a group containing free
  1903. // object ids is found, the object lu is again scanned for object ids within group and bitmasked.
  1904. // Finally, the bitmask is searched for a free id
  1905. s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, const u8_t *conflicting_name) {
  1906. s32_t res = SPIFFS_OK;
  1907. u32_t max_objects = (fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) / 2;
  1908. spiffs_free_obj_id_state state;
  1909. spiffs_obj_id free_obj_id = SPIFFS_OBJ_ID_FREE;
  1910. state.min_obj_id = 1;
  1911. state.max_obj_id = max_objects + 1;
  1912. if (state.max_obj_id & SPIFFS_OBJ_ID_IX_FLAG) {
  1913. state.max_obj_id = ((spiffs_obj_id)-1) & ~SPIFFS_OBJ_ID_IX_FLAG;
  1914. }
  1915. state.compaction = 0;
  1916. state.conflicting_name = conflicting_name;
  1917. while (res == SPIFFS_OK && free_obj_id == SPIFFS_OBJ_ID_FREE) {
  1918. if (state.max_obj_id - state.min_obj_id <= (spiffs_obj_id)SPIFFS_CFG_LOG_PAGE_SZ(fs)*8) {
  1919. // possible to represent in bitmap
  1920. u32_t i, j;
  1921. SPIFFS_DBG("free_obj_id: BITM min:"_SPIPRIid" max:"_SPIPRIid"\n", state.min_obj_id, state.max_obj_id);
  1922. memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));
  1923. res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_bitmap_v,
  1924. conflicting_name, &state.min_obj_id, 0, 0);
  1925. if (res == SPIFFS_VIS_END) res = SPIFFS_OK;
  1926. SPIFFS_CHECK_RES(res);
  1927. // traverse bitmask until found free obj_id
  1928. for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs); i++) {
  1929. u8_t mask = fs->work[i];
  1930. if (mask == 0xff) {
  1931. continue;
  1932. }
  1933. for (j = 0; j < 8; j++) {
  1934. if ((mask & (1<<j)) == 0) {
  1935. *obj_id = (i<<3)+j+state.min_obj_id;
  1936. return SPIFFS_OK;
  1937. }
  1938. }
  1939. }
  1940. return SPIFFS_ERR_FULL;
  1941. } else {
  1942. // not possible to represent all ids in range in a bitmap, compact and count
  1943. if (state.compaction != 0) {
  1944. // select element in compacted table, decrease range and recompact
  1945. u32_t i, min_i = 0;
  1946. u8_t *map = (u8_t *)fs->work;
  1947. u8_t min_count = 0xff;
  1948. for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs)/sizeof(u8_t); i++) {
  1949. if (map[i] < min_count) {
  1950. min_count = map[i];
  1951. min_i = i;
  1952. if (min_count == 0) {
  1953. break;
  1954. }
  1955. }
  1956. }
  1957. if (min_count == state.compaction) {
  1958. // there are no free objids!
  1959. SPIFFS_DBG("free_obj_id: compacted table is full\n");
  1960. return SPIFFS_ERR_FULL;
  1961. }
  1962. SPIFFS_DBG("free_obj_id: COMP select index:"_SPIPRIi" min_count:"_SPIPRIi" min:"_SPIPRIid" max:"_SPIPRIid" compact:"_SPIPRIi"\n", min_i, min_count, state.min_obj_id, state.max_obj_id, state.compaction);
  1963. if (min_count == 0) {
  1964. // no id in this range, skip compacting and use directly
  1965. *obj_id = min_i * state.compaction + state.min_obj_id;
  1966. return SPIFFS_OK;
  1967. } else {
  1968. SPIFFS_DBG("free_obj_id: COMP SEL chunk:"_SPIPRIi" min:"_SPIPRIid" -> "_SPIPRIid"\n", state.compaction, state.min_obj_id, state.min_obj_id + min_i * state.compaction);
  1969. state.min_obj_id += min_i * state.compaction;
  1970. state.max_obj_id = state.min_obj_id + state.compaction;
  1971. // decrease compaction
  1972. }
  1973. if ((state.max_obj_id - state.min_obj_id <= (spiffs_obj_id)SPIFFS_CFG_LOG_PAGE_SZ(fs)*8)) {
  1974. // no need for compacting, use bitmap
  1975. continue;
  1976. }
  1977. }
  1978. // in a work memory of log_page_size bytes, we may fit in log_page_size ids
  1979. // todo what if compaction is > 255 - then we cannot fit it in a byte
  1980. state.compaction = (state.max_obj_id-state.min_obj_id) / ((SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(u8_t)));
  1981. SPIFFS_DBG("free_obj_id: COMP min:"_SPIPRIid" max:"_SPIPRIid" compact:"_SPIPRIi"\n", state.min_obj_id, state.max_obj_id, state.compaction);
  1982. memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));
  1983. res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_compact_v, &state, 0, 0, 0);
  1984. if (res == SPIFFS_VIS_END) res = SPIFFS_OK;
  1985. SPIFFS_CHECK_RES(res);
  1986. state.conflicting_name = 0; // searched for conflicting name once, no need to do it again
  1987. }
  1988. }
  1989. return res;
  1990. }
  1991. #endif // !SPIFFS_READ_ONLY
  1992. #if SPIFFS_TEMPORAL_FD_CACHE
  1993. // djb2 hash
  1994. static u32_t spiffs_hash(spiffs *fs, const u8_t *name) {
  1995. (void)fs;
  1996. u32_t hash = 5381;
  1997. u8_t c;
  1998. int i = 0;
  1999. while ((c = name[i++]) && i < SPIFFS_OBJ_NAME_LEN) {
  2000. hash = (hash * 33) ^ c;
  2001. }
  2002. return hash;
  2003. }
  2004. #endif
  2005. s32_t spiffs_fd_find_new(spiffs *fs, spiffs_fd **fd, const char *name) {
  2006. #if SPIFFS_TEMPORAL_FD_CACHE
  2007. u32_t i;
  2008. u16_t min_score = 0xffff;
  2009. u32_t cand_ix = (u32_t)-1;
  2010. u32_t name_hash = name ? spiffs_hash(fs, (const u8_t *)name) : 0;
  2011. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  2012. if (name) {
  2013. // first, decrease score of all closed descriptors
  2014. for (i = 0; i < fs->fd_count; i++) {
  2015. spiffs_fd *cur_fd = &fds[i];
  2016. if (cur_fd->file_nbr == 0) {
  2017. if (cur_fd->score > 1) { // score == 0 indicates never used fd
  2018. cur_fd->score--;
  2019. }
  2020. }
  2021. }
  2022. }
  2023. // find the free fd with least score or name match
  2024. for (i = 0; i < fs->fd_count; i++) {
  2025. spiffs_fd *cur_fd = &fds[i];
  2026. if (cur_fd->file_nbr == 0) {
  2027. if (name && cur_fd->name_hash == name_hash) {
  2028. cand_ix = i;
  2029. break;
  2030. }
  2031. if (cur_fd->score < min_score) {
  2032. min_score = cur_fd->score;
  2033. cand_ix = i;
  2034. }
  2035. }
  2036. }
  2037. if (cand_ix != (u32_t)-1) {
  2038. spiffs_fd *cur_fd = &fds[cand_ix];
  2039. if (name) {
  2040. if (cur_fd->name_hash == name_hash && cur_fd->score > 0) {
  2041. // opened an fd with same name hash, assume same file
  2042. // set search point to saved obj index page and hope we have a correct match directly
  2043. // when start searching - if not, we will just keep searching until it is found
  2044. fs->cursor_block_ix = SPIFFS_BLOCK_FOR_PAGE(fs, cur_fd->objix_hdr_pix);
  2045. fs->cursor_obj_lu_entry = SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, cur_fd->objix_hdr_pix);
  2046. // update score
  2047. if (cur_fd->score < 0xffff-SPIFFS_TEMPORAL_CACHE_HIT_SCORE) {
  2048. cur_fd->score += SPIFFS_TEMPORAL_CACHE_HIT_SCORE;
  2049. } else {
  2050. cur_fd->score = 0xffff;
  2051. }
  2052. } else {
  2053. // no hash hit, restore this fd to initial state
  2054. cur_fd->score = SPIFFS_TEMPORAL_CACHE_HIT_SCORE;
  2055. cur_fd->name_hash = name_hash;
  2056. }
  2057. }
  2058. cur_fd->file_nbr = cand_ix+1;
  2059. *fd = cur_fd;
  2060. return SPIFFS_OK;
  2061. } else {
  2062. return SPIFFS_ERR_OUT_OF_FILE_DESCS;
  2063. }
  2064. #else
  2065. (void)name;
  2066. u32_t i;
  2067. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  2068. for (i = 0; i < fs->fd_count; i++) {
  2069. spiffs_fd *cur_fd = &fds[i];
  2070. if (cur_fd->file_nbr == 0) {
  2071. cur_fd->file_nbr = i+1;
  2072. *fd = cur_fd;
  2073. return SPIFFS_OK;
  2074. }
  2075. }
  2076. return SPIFFS_ERR_OUT_OF_FILE_DESCS;
  2077. #endif
  2078. }
  2079. s32_t spiffs_fd_return(spiffs *fs, spiffs_file f) {
  2080. if (f <= 0 || f > (s16_t)fs->fd_count) {
  2081. return SPIFFS_ERR_BAD_DESCRIPTOR;
  2082. }
  2083. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  2084. spiffs_fd *fd = &fds[f-1];
  2085. if (fd->file_nbr == 0) {
  2086. return SPIFFS_ERR_FILE_CLOSED;
  2087. }
  2088. fd->file_nbr = 0;
  2089. #if SPIFFS_IX_MAP
  2090. fd->ix_map = 0;
  2091. #endif
  2092. return SPIFFS_OK;
  2093. }
  2094. s32_t spiffs_fd_get(spiffs *fs, spiffs_file f, spiffs_fd **fd) {
  2095. if (f <= 0 || f > (s16_t)fs->fd_count) {
  2096. return SPIFFS_ERR_BAD_DESCRIPTOR;
  2097. }
  2098. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  2099. *fd = &fds[f-1];
  2100. if ((*fd)->file_nbr == 0) {
  2101. return SPIFFS_ERR_FILE_CLOSED;
  2102. }
  2103. return SPIFFS_OK;
  2104. }
  2105. #if SPIFFS_TEMPORAL_FD_CACHE
  2106. void spiffs_fd_temporal_cache_rehash(
  2107. spiffs *fs,
  2108. const char *old_path,
  2109. const char *new_path) {
  2110. u32_t i;
  2111. u32_t old_hash = spiffs_hash(fs, (const u8_t *)old_path);
  2112. u32_t new_hash = spiffs_hash(fs, (const u8_t *)new_path);
  2113. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  2114. for (i = 0; i < fs->fd_count; i++) {
  2115. spiffs_fd *cur_fd = &fds[i];
  2116. if (cur_fd->score > 0 && cur_fd->name_hash == old_hash) {
  2117. cur_fd->name_hash = new_hash;
  2118. }
  2119. }
  2120. }
  2121. #endif