spiffs_nucleus.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  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 %08x:%08x\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. 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 %i\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;
  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_READ_ONLY
  543. // Allocates a free defined page with given obj_id
  544. // Occupies object lookup entry and page
  545. // data may be NULL; where only page header is stored, len and page_offs is ignored
  546. s32_t spiffs_page_allocate_data(
  547. spiffs *fs,
  548. spiffs_obj_id obj_id,
  549. spiffs_page_header *ph,
  550. u8_t *data,
  551. u32_t len,
  552. u32_t page_offs,
  553. u8_t finalize,
  554. spiffs_page_ix *pix) {
  555. s32_t res = SPIFFS_OK;
  556. spiffs_block_ix bix;
  557. int entry;
  558. // find free entry
  559. res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);
  560. SPIFFS_CHECK_RES(res);
  561. // occupy page in object lookup
  562. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,
  563. 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t*)&obj_id);
  564. SPIFFS_CHECK_RES(res);
  565. fs->stats_p_allocated++;
  566. // write page header
  567. ph->flags &= ~SPIFFS_PH_FLAG_USED;
  568. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  569. 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry), sizeof(spiffs_page_header), (u8_t*)ph);
  570. SPIFFS_CHECK_RES(res);
  571. // write page data
  572. if (data) {
  573. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  574. 0,SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry) + sizeof(spiffs_page_header) + page_offs, len, data);
  575. SPIFFS_CHECK_RES(res);
  576. }
  577. // finalize header if necessary
  578. if (finalize && (ph->flags & SPIFFS_PH_FLAG_FINAL)) {
  579. ph->flags &= ~SPIFFS_PH_FLAG_FINAL;
  580. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  581. 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry) + offsetof(spiffs_page_header, flags),
  582. sizeof(u8_t),
  583. (u8_t *)&ph->flags);
  584. SPIFFS_CHECK_RES(res);
  585. }
  586. // return written page
  587. if (pix) {
  588. *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  589. }
  590. return res;
  591. }
  592. #endif // !SPIFFS_READ_ONLY
  593. #if !SPIFFS_READ_ONLY
  594. // Moves a page from src to a free page and finalizes it. Updates page index. Page data is given in param page.
  595. // If page data is null, provided header is used for metainfo and page data is physically copied.
  596. s32_t spiffs_page_move(
  597. spiffs *fs,
  598. spiffs_file fh,
  599. u8_t *page_data,
  600. spiffs_obj_id obj_id,
  601. spiffs_page_header *page_hdr,
  602. spiffs_page_ix src_pix,
  603. spiffs_page_ix *dst_pix) {
  604. s32_t res;
  605. u8_t was_final = 0;
  606. spiffs_page_header *p_hdr;
  607. spiffs_block_ix bix;
  608. int entry;
  609. spiffs_page_ix free_pix;
  610. // find free entry
  611. res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);
  612. SPIFFS_CHECK_RES(res);
  613. free_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  614. if (dst_pix) *dst_pix = free_pix;
  615. p_hdr = page_data ? (spiffs_page_header *)page_data : page_hdr;
  616. if (page_data) {
  617. // got page data
  618. was_final = (p_hdr->flags & SPIFFS_PH_FLAG_FINAL) == 0;
  619. // write unfinalized page
  620. p_hdr->flags |= SPIFFS_PH_FLAG_FINAL;
  621. p_hdr->flags &= ~SPIFFS_PH_FLAG_USED;
  622. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  623. 0, SPIFFS_PAGE_TO_PADDR(fs, free_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), page_data);
  624. } else {
  625. // copy page data
  626. 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));
  627. }
  628. SPIFFS_CHECK_RES(res);
  629. // mark entry in destination object lookup
  630. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,
  631. 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),
  632. sizeof(spiffs_obj_id),
  633. (u8_t *)&obj_id);
  634. SPIFFS_CHECK_RES(res);
  635. fs->stats_p_allocated++;
  636. if (was_final) {
  637. // mark finalized in destination page
  638. p_hdr->flags &= ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_USED);
  639. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  640. fh,
  641. SPIFFS_PAGE_TO_PADDR(fs, free_pix) + offsetof(spiffs_page_header, flags),
  642. sizeof(u8_t),
  643. (u8_t *)&p_hdr->flags);
  644. SPIFFS_CHECK_RES(res);
  645. }
  646. // mark source deleted
  647. res = spiffs_page_delete(fs, src_pix);
  648. return res;
  649. }
  650. #endif // !SPIFFS_READ_ONLY
  651. #if !SPIFFS_READ_ONLY
  652. // Deletes a page and removes it from object lookup.
  653. s32_t spiffs_page_delete(
  654. spiffs *fs,
  655. spiffs_page_ix pix) {
  656. s32_t res;
  657. spiffs_page_header hdr;
  658. hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED);
  659. // mark deleted entry in source object lookup
  660. spiffs_obj_id d_obj_id = SPIFFS_OBJ_ID_DELETED;
  661. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_DELE,
  662. 0,
  663. SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs, pix)) + SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix) * sizeof(spiffs_page_ix),
  664. sizeof(spiffs_obj_id),
  665. (u8_t *)&d_obj_id);
  666. SPIFFS_CHECK_RES(res);
  667. fs->stats_p_deleted++;
  668. fs->stats_p_allocated--;
  669. // mark deleted in source page
  670. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_DELE,
  671. 0,
  672. SPIFFS_PAGE_TO_PADDR(fs, pix) + offsetof(spiffs_page_header, flags),
  673. sizeof(u8_t),
  674. (u8_t *)&hdr.flags);
  675. return res;
  676. }
  677. #endif // !SPIFFS_READ_ONLY
  678. #if !SPIFFS_READ_ONLY
  679. // Create an object index header page with empty index and undefined length
  680. s32_t spiffs_object_create(
  681. spiffs *fs,
  682. spiffs_obj_id obj_id,
  683. const u8_t name[SPIFFS_OBJ_NAME_LEN],
  684. spiffs_obj_type type,
  685. spiffs_page_ix *objix_hdr_pix) {
  686. s32_t res = SPIFFS_OK;
  687. spiffs_block_ix bix;
  688. spiffs_page_object_ix_header oix_hdr;
  689. int entry;
  690. res = spiffs_gc_check(fs, SPIFFS_DATA_PAGE_SIZE(fs));
  691. SPIFFS_CHECK_RES(res);
  692. obj_id |= SPIFFS_OBJ_ID_IX_FLAG;
  693. // find free entry
  694. res = spiffs_obj_lu_find_free(fs, fs->free_cursor_block_ix, fs->free_cursor_obj_lu_entry, &bix, &entry);
  695. SPIFFS_CHECK_RES(res);
  696. SPIFFS_DBG("create: found free page @ %04x bix:%i entry:%i\n", SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry), bix, entry);
  697. // occupy page in object lookup
  698. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_UPDT,
  699. 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t*)&obj_id);
  700. SPIFFS_CHECK_RES(res);
  701. fs->stats_p_allocated++;
  702. // write empty object index page
  703. oix_hdr.p_hdr.obj_id = obj_id;
  704. oix_hdr.p_hdr.span_ix = 0;
  705. oix_hdr.p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_USED);
  706. oix_hdr.type = type;
  707. oix_hdr.size = SPIFFS_UNDEFINED_LEN; // keep ones so we can update later without wasting this page
  708. strncpy((char*)&oix_hdr.name, (const char*)name, SPIFFS_OBJ_NAME_LEN);
  709. // update page
  710. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  711. 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, entry), sizeof(spiffs_page_object_ix_header), (u8_t*)&oix_hdr);
  712. SPIFFS_CHECK_RES(res);
  713. spiffs_cb_object_event(fs, 0, SPIFFS_EV_IX_NEW, obj_id, 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry), SPIFFS_UNDEFINED_LEN);
  714. if (objix_hdr_pix) {
  715. *objix_hdr_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  716. }
  717. return res;
  718. }
  719. #endif // !SPIFFS_READ_ONLY
  720. #if !SPIFFS_READ_ONLY
  721. // update object index header with any combination of name/size/index
  722. // new_objix_hdr_data may be null, if so the object index header page is loaded
  723. // name may be null, if so name is not changed
  724. // size may be null, if so size is not changed
  725. s32_t spiffs_object_update_index_hdr(
  726. spiffs *fs,
  727. spiffs_fd *fd,
  728. spiffs_obj_id obj_id,
  729. spiffs_page_ix objix_hdr_pix,
  730. u8_t *new_objix_hdr_data,
  731. const u8_t name[SPIFFS_OBJ_NAME_LEN],
  732. u32_t size,
  733. spiffs_page_ix *new_pix) {
  734. s32_t res = SPIFFS_OK;
  735. spiffs_page_object_ix_header *objix_hdr;
  736. spiffs_page_ix new_objix_hdr_pix;
  737. obj_id |= SPIFFS_OBJ_ID_IX_FLAG;
  738. if (new_objix_hdr_data) {
  739. // object index header page already given to us, no need to load it
  740. objix_hdr = (spiffs_page_object_ix_header *)new_objix_hdr_data;
  741. } else {
  742. // read object index header page
  743. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  744. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_hdr_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  745. SPIFFS_CHECK_RES(res);
  746. objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  747. }
  748. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, obj_id, 0);
  749. // change name
  750. if (name) {
  751. strncpy((char*)objix_hdr->name, (const char*)name, SPIFFS_OBJ_NAME_LEN);
  752. }
  753. if (size) {
  754. objix_hdr->size = size;
  755. }
  756. // move and update page
  757. 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);
  758. if (res == SPIFFS_OK) {
  759. if (new_pix) {
  760. *new_pix = new_objix_hdr_pix;
  761. }
  762. // callback on object index update
  763. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, obj_id, objix_hdr->p_hdr.span_ix, new_objix_hdr_pix, objix_hdr->size);
  764. if (fd) fd->objix_hdr_pix = new_objix_hdr_pix; // if this is not in the registered cluster
  765. }
  766. return res;
  767. }
  768. #endif // !SPIFFS_READ_ONLY
  769. void spiffs_cb_object_event(
  770. spiffs *fs,
  771. spiffs_fd *fd,
  772. int ev,
  773. spiffs_obj_id obj_id_raw,
  774. spiffs_span_ix spix,
  775. spiffs_page_ix new_pix,
  776. u32_t new_size) {
  777. (void)fd;
  778. // update index caches in all file descriptors
  779. spiffs_obj_id obj_id = obj_id_raw & ~SPIFFS_OBJ_ID_IX_FLAG;
  780. u32_t i;
  781. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  782. for (i = 0; i < fs->fd_count; i++) {
  783. spiffs_fd *cur_fd = &fds[i];
  784. if (cur_fd->file_nbr == 0 || (cur_fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG) != obj_id) continue;
  785. if (spix == 0) {
  786. if (ev == SPIFFS_EV_IX_NEW || ev == SPIFFS_EV_IX_UPD) {
  787. SPIFFS_DBG(" callback: setting fd %i:%04x objix_hdr_pix to %04x, size:%i\n", cur_fd->file_nbr, cur_fd->obj_id, new_pix, new_size);
  788. cur_fd->objix_hdr_pix = new_pix;
  789. if (new_size != 0) {
  790. cur_fd->size = new_size;
  791. }
  792. } else if (ev == SPIFFS_EV_IX_DEL) {
  793. cur_fd->file_nbr = 0;
  794. cur_fd->obj_id = SPIFFS_OBJ_ID_DELETED;
  795. }
  796. }
  797. if (cur_fd->cursor_objix_spix == spix) {
  798. if (ev == SPIFFS_EV_IX_NEW || ev == SPIFFS_EV_IX_UPD) {
  799. SPIFFS_DBG(" callback: setting fd %i:%04x span:%04x objix_pix to %04x\n", cur_fd->file_nbr, cur_fd->obj_id, spix, new_pix);
  800. cur_fd->cursor_objix_pix = new_pix;
  801. } else {
  802. cur_fd->cursor_objix_pix = 0;
  803. }
  804. }
  805. }
  806. // callback to user if object index header
  807. if (fs->file_cb_f && spix == 0 && (obj_id_raw & SPIFFS_OBJ_ID_IX_FLAG)) {
  808. spiffs_fileop_type op;
  809. if (ev == SPIFFS_EV_IX_NEW) {
  810. op = SPIFFS_CB_CREATED;
  811. } else if (ev == SPIFFS_EV_IX_UPD) {
  812. op = SPIFFS_CB_UPDATED;
  813. } else if (ev == SPIFFS_EV_IX_DEL) {
  814. op = SPIFFS_CB_DELETED;
  815. } else {
  816. SPIFFS_DBG(" callback: WARNING unknown callback event %02x\n", ev);
  817. return; // bail out
  818. }
  819. fs->file_cb_f(fs, op, obj_id, new_pix);
  820. }
  821. }
  822. // Open object by id
  823. s32_t spiffs_object_open_by_id(
  824. spiffs *fs,
  825. spiffs_obj_id obj_id,
  826. spiffs_fd *fd,
  827. spiffs_flags flags,
  828. spiffs_mode mode) {
  829. s32_t res = SPIFFS_OK;
  830. spiffs_page_ix pix;
  831. res = spiffs_obj_lu_find_id_and_span(fs, obj_id | SPIFFS_OBJ_ID_IX_FLAG, 0, 0, &pix);
  832. SPIFFS_CHECK_RES(res);
  833. res = spiffs_object_open_by_page(fs, pix, fd, flags, mode);
  834. return res;
  835. }
  836. // Open object by page index
  837. s32_t spiffs_object_open_by_page(
  838. spiffs *fs,
  839. spiffs_page_ix pix,
  840. spiffs_fd *fd,
  841. spiffs_flags flags,
  842. spiffs_mode mode) {
  843. (void)mode;
  844. s32_t res = SPIFFS_OK;
  845. spiffs_page_object_ix_header oix_hdr;
  846. spiffs_obj_id obj_id;
  847. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  848. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&oix_hdr);
  849. SPIFFS_CHECK_RES(res);
  850. spiffs_block_ix bix = SPIFFS_BLOCK_FOR_PAGE(fs, pix);
  851. int entry = SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix);
  852. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,
  853. 0, SPIFFS_BLOCK_TO_PADDR(fs, bix) + entry * sizeof(spiffs_obj_id), sizeof(spiffs_obj_id), (u8_t *)&obj_id);
  854. fd->fs = fs;
  855. fd->objix_hdr_pix = pix;
  856. fd->size = oix_hdr.size;
  857. fd->offset = 0;
  858. fd->cursor_objix_pix = pix;
  859. fd->cursor_objix_spix = 0;
  860. fd->obj_id = obj_id;
  861. fd->flags = flags;
  862. SPIFFS_VALIDATE_OBJIX(oix_hdr.p_hdr, fd->obj_id, 0);
  863. SPIFFS_DBG("open: fd %i is obj id %04x\n", fd->file_nbr, fd->obj_id);
  864. return res;
  865. }
  866. #if !SPIFFS_READ_ONLY
  867. // Append to object
  868. // keep current object index (header) page in fs->work buffer
  869. s32_t spiffs_object_append(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) {
  870. spiffs *fs = fd->fs;
  871. s32_t res = SPIFFS_OK;
  872. u32_t written = 0;
  873. SPIFFS_DBG("append: %i bytes @ offs %i of size %i\n", len, offset, fd->size);
  874. if (offset > fd->size) {
  875. SPIFFS_DBG("append: offset reversed to size\n");
  876. offset = fd->size;
  877. }
  878. res = spiffs_gc_check(fs, len + SPIFFS_DATA_PAGE_SIZE(fs)); // add an extra page of data worth for meta
  879. if (res != SPIFFS_OK) {
  880. SPIFFS_DBG("append: gc check fail %i\n", res);
  881. }
  882. SPIFFS_CHECK_RES(res);
  883. spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  884. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  885. spiffs_page_header p_hdr;
  886. spiffs_span_ix cur_objix_spix = 0;
  887. spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;
  888. spiffs_page_ix cur_objix_pix = fd->objix_hdr_pix;
  889. spiffs_page_ix new_objix_hdr_page;
  890. spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);
  891. spiffs_page_ix data_page;
  892. u32_t page_offs = offset % SPIFFS_DATA_PAGE_SIZE(fs);
  893. // write all data
  894. while (res == SPIFFS_OK && written < len) {
  895. // calculate object index page span index
  896. cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  897. // handle storing and loading of object indices
  898. if (cur_objix_spix != prev_objix_spix) {
  899. // new object index page
  900. // within this clause we return directly if something fails, object index mess-up
  901. if (written > 0) {
  902. // store previous object index page, unless first pass
  903. SPIFFS_DBG("append: %04x store objix %04x:%04x, written %i\n", fd->obj_id,
  904. cur_objix_pix, prev_objix_spix, written);
  905. if (prev_objix_spix == 0) {
  906. // this is an update to object index header page
  907. objix_hdr->size = offset+written;
  908. if (offset == 0) {
  909. // was an empty object, update same page (size was 0xffffffff)
  910. res = spiffs_page_index_check(fs, fd, cur_objix_pix, 0);
  911. SPIFFS_CHECK_RES(res);
  912. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  913. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  914. SPIFFS_CHECK_RES(res);
  915. } else {
  916. // was a nonempty object, update to new page
  917. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  918. fd->objix_hdr_pix, fs->work, 0, offset+written, &new_objix_hdr_page);
  919. SPIFFS_CHECK_RES(res);
  920. SPIFFS_DBG("append: %04x store new objix_hdr, %04x:%04x, written %i\n", fd->obj_id,
  921. new_objix_hdr_page, 0, written);
  922. }
  923. } else {
  924. // this is an update to an object index page
  925. res = spiffs_page_index_check(fs, fd, cur_objix_pix, prev_objix_spix);
  926. SPIFFS_CHECK_RES(res);
  927. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  928. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  929. SPIFFS_CHECK_RES(res);
  930. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD,fd->obj_id, objix->p_hdr.span_ix, cur_objix_pix, 0);
  931. // update length in object index header page
  932. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  933. fd->objix_hdr_pix, 0, 0, offset+written, &new_objix_hdr_page);
  934. SPIFFS_CHECK_RES(res);
  935. SPIFFS_DBG("append: %04x store new size I %i in objix_hdr, %04x:%04x, written %i\n", fd->obj_id,
  936. offset+written, new_objix_hdr_page, 0, written);
  937. }
  938. fd->size = offset+written;
  939. fd->offset = offset+written;
  940. }
  941. // create or load new object index page
  942. if (cur_objix_spix == 0) {
  943. // load object index header page, must always exist
  944. SPIFFS_DBG("append: %04x load objixhdr page %04x:%04x\n", fd->obj_id, cur_objix_pix, cur_objix_spix);
  945. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  946. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  947. SPIFFS_CHECK_RES(res);
  948. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  949. } else {
  950. spiffs_span_ix len_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, (fd->size-1)/SPIFFS_DATA_PAGE_SIZE(fs));
  951. // on subsequent passes, create a new object index page
  952. if (written > 0 || cur_objix_spix > len_objix_spix) {
  953. p_hdr.obj_id = fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG;
  954. p_hdr.span_ix = cur_objix_spix;
  955. p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX);
  956. res = spiffs_page_allocate_data(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG,
  957. &p_hdr, 0, 0, 0, 1, &cur_objix_pix);
  958. SPIFFS_CHECK_RES(res);
  959. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_NEW, fd->obj_id, cur_objix_spix, cur_objix_pix, 0);
  960. // quick "load" of new object index page
  961. memset(fs->work, 0xff, SPIFFS_CFG_LOG_PAGE_SZ(fs));
  962. memcpy(fs->work, &p_hdr, sizeof(spiffs_page_header));
  963. SPIFFS_DBG("append: %04x create objix page, %04x:%04x, written %i\n", fd->obj_id
  964. , cur_objix_pix, cur_objix_spix, written);
  965. } else {
  966. // on first pass, we load existing object index page
  967. spiffs_page_ix pix;
  968. SPIFFS_DBG("append: %04x find objix span_ix:%04x\n", fd->obj_id, cur_objix_spix);
  969. if (fd->cursor_objix_spix == cur_objix_spix) {
  970. pix = fd->cursor_objix_pix;
  971. } else {
  972. res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &pix);
  973. SPIFFS_CHECK_RES(res);
  974. }
  975. SPIFFS_DBG("append: %04x found object index at page %04x [fd size %i]\n", fd->obj_id, pix, fd->size);
  976. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  977. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  978. SPIFFS_CHECK_RES(res);
  979. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  980. cur_objix_pix = pix;
  981. }
  982. fd->cursor_objix_pix = cur_objix_pix;
  983. fd->cursor_objix_spix = cur_objix_spix;
  984. fd->offset = offset+written;
  985. fd->size = offset+written;
  986. }
  987. prev_objix_spix = cur_objix_spix;
  988. }
  989. // write data
  990. u32_t to_write = MIN(len-written, SPIFFS_DATA_PAGE_SIZE(fs) - page_offs);
  991. if (page_offs == 0) {
  992. // at beginning of a page, allocate and write a new page of data
  993. p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
  994. p_hdr.span_ix = data_spix;
  995. p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL); // finalize immediately
  996. res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,
  997. &p_hdr, &data[written], to_write, page_offs, 1, &data_page);
  998. SPIFFS_DBG("append: %04x store new data page, %04x:%04x offset:%i, len %i, written %i\n", fd->obj_id,
  999. data_page, data_spix, page_offs, to_write, written);
  1000. } else {
  1001. // append to existing page, fill out free data in existing page
  1002. if (cur_objix_spix == 0) {
  1003. // get data page from object index header page
  1004. data_page = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];
  1005. } else {
  1006. // get data page from object index page
  1007. data_page = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];
  1008. }
  1009. res = spiffs_page_data_check(fs, fd, data_page, data_spix);
  1010. SPIFFS_CHECK_RES(res);
  1011. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  1012. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, data_page) + sizeof(spiffs_page_header) + page_offs, to_write, &data[written]);
  1013. SPIFFS_DBG("append: %04x store to existing data page, %04x:%04x offset:%i, len %i, written %i\n", fd->obj_id
  1014. , data_page, data_spix, page_offs, to_write, written);
  1015. }
  1016. if (res != SPIFFS_OK) break;
  1017. // update memory representation of object index page with new data page
  1018. if (cur_objix_spix == 0) {
  1019. // update object index header page
  1020. ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = data_page;
  1021. SPIFFS_DBG("append: %04x wrote page %04x to objix_hdr entry %02x in mem\n", fd->obj_id
  1022. , data_page, data_spix);
  1023. objix_hdr->size = offset+written;
  1024. } else {
  1025. // update object index page
  1026. ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = data_page;
  1027. SPIFFS_DBG("append: %04x wrote page %04x to objix entry %02x in mem\n", fd->obj_id
  1028. , data_page, SPIFFS_OBJ_IX_ENTRY(fs, data_spix));
  1029. }
  1030. // update internals
  1031. page_offs = 0;
  1032. data_spix++;
  1033. written += to_write;
  1034. } // while all data
  1035. fd->size = offset+written;
  1036. fd->offset = offset+written;
  1037. fd->cursor_objix_pix = cur_objix_pix;
  1038. fd->cursor_objix_spix = cur_objix_spix;
  1039. // finalize updated object indices
  1040. s32_t res2 = SPIFFS_OK;
  1041. if (cur_objix_spix != 0) {
  1042. // wrote beyond object index header page
  1043. // write last modified object index page, unless object header index page
  1044. SPIFFS_DBG("append: %04x store objix page, %04x:%04x, written %i\n", fd->obj_id,
  1045. cur_objix_pix, cur_objix_spix, written);
  1046. res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);
  1047. SPIFFS_CHECK_RES(res2);
  1048. res2 = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  1049. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1050. SPIFFS_CHECK_RES(res2);
  1051. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, cur_objix_pix, 0);
  1052. // update size in object header index page
  1053. res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1054. fd->objix_hdr_pix, 0, 0, offset+written, &new_objix_hdr_page);
  1055. SPIFFS_DBG("append: %04x store new size II %i in objix_hdr, %04x:%04x, written %i, res %i\n", fd->obj_id
  1056. , offset+written, new_objix_hdr_page, 0, written, res2);
  1057. SPIFFS_CHECK_RES(res2);
  1058. } else {
  1059. // wrote within object index header page
  1060. if (offset == 0) {
  1061. // wrote to empty object - simply update size and write whole page
  1062. objix_hdr->size = offset+written;
  1063. SPIFFS_DBG("append: %04x store fresh objix_hdr page, %04x:%04x, written %i\n", fd->obj_id
  1064. , cur_objix_pix, cur_objix_spix, written);
  1065. res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);
  1066. SPIFFS_CHECK_RES(res2);
  1067. res2 = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  1068. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1069. SPIFFS_CHECK_RES(res2);
  1070. // callback on object index update
  1071. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix_hdr->p_hdr.span_ix, cur_objix_pix, objix_hdr->size);
  1072. } else {
  1073. // modifying object index header page, update size and make new copy
  1074. res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1075. fd->objix_hdr_pix, fs->work, 0, offset+written, &new_objix_hdr_page);
  1076. SPIFFS_DBG("append: %04x store modified objix_hdr page, %04x:%04x, written %i\n", fd->obj_id
  1077. , new_objix_hdr_page, 0, written);
  1078. SPIFFS_CHECK_RES(res2);
  1079. }
  1080. }
  1081. return res;
  1082. } // spiffs_object_append
  1083. #endif // !SPIFFS_READ_ONLY
  1084. #if !SPIFFS_READ_ONLY
  1085. // Modify object
  1086. // keep current object index (header) page in fs->work buffer
  1087. s32_t spiffs_object_modify(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) {
  1088. spiffs *fs = fd->fs;
  1089. s32_t res = SPIFFS_OK;
  1090. u32_t written = 0;
  1091. res = spiffs_gc_check(fs, len + SPIFFS_DATA_PAGE_SIZE(fs));
  1092. SPIFFS_CHECK_RES(res);
  1093. spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  1094. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  1095. spiffs_page_header p_hdr;
  1096. spiffs_span_ix cur_objix_spix = 0;
  1097. spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;
  1098. spiffs_page_ix cur_objix_pix = fd->objix_hdr_pix;
  1099. spiffs_page_ix new_objix_hdr_pix;
  1100. spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);
  1101. spiffs_page_ix data_pix;
  1102. u32_t page_offs = offset % SPIFFS_DATA_PAGE_SIZE(fs);
  1103. // write all data
  1104. while (res == SPIFFS_OK && written < len) {
  1105. // calculate object index page span index
  1106. cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  1107. // handle storing and loading of object indices
  1108. if (cur_objix_spix != prev_objix_spix) {
  1109. // new object index page
  1110. // within this clause we return directly if something fails, object index mess-up
  1111. if (written > 0) {
  1112. // store previous object index (header) page, unless first pass
  1113. if (prev_objix_spix == 0) {
  1114. // store previous object index header page
  1115. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1116. fd->objix_hdr_pix, fs->work, 0, 0, &new_objix_hdr_pix);
  1117. SPIFFS_DBG("modify: store modified objix_hdr page, %04x:%04x, written %i\n", new_objix_hdr_pix, 0, written);
  1118. SPIFFS_CHECK_RES(res);
  1119. } else {
  1120. // store new version of previous object index page
  1121. spiffs_page_ix new_objix_pix;
  1122. res = spiffs_page_index_check(fs, fd, cur_objix_pix, prev_objix_spix);
  1123. SPIFFS_CHECK_RES(res);
  1124. res = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix, fd->obj_id, 0, cur_objix_pix, &new_objix_pix);
  1125. SPIFFS_DBG("modify: store previous modified objix page, %04x:%04x, written %i\n", new_objix_pix, objix->p_hdr.span_ix, written);
  1126. SPIFFS_CHECK_RES(res);
  1127. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);
  1128. }
  1129. }
  1130. // load next object index page
  1131. if (cur_objix_spix == 0) {
  1132. // load object index header page, must exist
  1133. SPIFFS_DBG("modify: load objixhdr page %04x:%04x\n", cur_objix_pix, cur_objix_spix);
  1134. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1135. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, cur_objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1136. SPIFFS_CHECK_RES(res);
  1137. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  1138. } else {
  1139. // load existing object index page on first pass
  1140. spiffs_page_ix pix;
  1141. SPIFFS_DBG("modify: find objix span_ix:%04x\n", cur_objix_spix);
  1142. if (fd->cursor_objix_spix == cur_objix_spix) {
  1143. pix = fd->cursor_objix_pix;
  1144. } else {
  1145. res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &pix);
  1146. SPIFFS_CHECK_RES(res);
  1147. }
  1148. SPIFFS_DBG("modify: found object index at page %04x\n", pix);
  1149. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1150. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1151. SPIFFS_CHECK_RES(res);
  1152. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  1153. cur_objix_pix = pix;
  1154. }
  1155. fd->cursor_objix_pix = cur_objix_pix;
  1156. fd->cursor_objix_spix = cur_objix_spix;
  1157. fd->offset = offset+written;
  1158. prev_objix_spix = cur_objix_spix;
  1159. }
  1160. // write partial data
  1161. u32_t to_write = MIN(len-written, SPIFFS_DATA_PAGE_SIZE(fs) - page_offs);
  1162. spiffs_page_ix orig_data_pix;
  1163. if (cur_objix_spix == 0) {
  1164. // get data page from object index header page
  1165. orig_data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];
  1166. } else {
  1167. // get data page from object index page
  1168. orig_data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];
  1169. }
  1170. p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
  1171. p_hdr.span_ix = data_spix;
  1172. p_hdr.flags = 0xff;
  1173. if (page_offs == 0 && to_write == SPIFFS_DATA_PAGE_SIZE(fs)) {
  1174. // a full page, allocate and write a new page of data
  1175. res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,
  1176. &p_hdr, &data[written], to_write, page_offs, 1, &data_pix);
  1177. SPIFFS_DBG("modify: store new data page, %04x:%04x offset:%i, len %i, written %i\n", data_pix, data_spix, page_offs, to_write, written);
  1178. } else {
  1179. // write to existing page, allocate new and copy unmodified data
  1180. res = spiffs_page_data_check(fs, fd, orig_data_pix, data_spix);
  1181. SPIFFS_CHECK_RES(res);
  1182. res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,
  1183. &p_hdr, 0, 0, 0, 0, &data_pix);
  1184. if (res != SPIFFS_OK) break;
  1185. // copy unmodified data
  1186. if (page_offs > 0) {
  1187. // before modification
  1188. res = spiffs_phys_cpy(fs, fd->file_nbr,
  1189. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header),
  1190. SPIFFS_PAGE_TO_PADDR(fs, orig_data_pix) + sizeof(spiffs_page_header),
  1191. page_offs);
  1192. if (res != SPIFFS_OK) break;
  1193. }
  1194. if (page_offs + to_write < SPIFFS_DATA_PAGE_SIZE(fs)) {
  1195. // after modification
  1196. res = spiffs_phys_cpy(fs, fd->file_nbr,
  1197. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + page_offs + to_write,
  1198. SPIFFS_PAGE_TO_PADDR(fs, orig_data_pix) + sizeof(spiffs_page_header) + page_offs + to_write,
  1199. SPIFFS_DATA_PAGE_SIZE(fs) - (page_offs + to_write));
  1200. if (res != SPIFFS_OK) break;
  1201. }
  1202. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  1203. fd->file_nbr,
  1204. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + page_offs, to_write, &data[written]);
  1205. if (res != SPIFFS_OK) break;
  1206. p_hdr.flags &= ~SPIFFS_PH_FLAG_FINAL;
  1207. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  1208. fd->file_nbr,
  1209. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + offsetof(spiffs_page_header, flags),
  1210. sizeof(u8_t),
  1211. (u8_t *)&p_hdr.flags);
  1212. if (res != SPIFFS_OK) break;
  1213. SPIFFS_DBG("modify: store to existing data page, src:%04x, dst:%04x:%04x offset:%i, len %i, written %i\n", orig_data_pix, data_pix, data_spix, page_offs, to_write, written);
  1214. }
  1215. // delete original data page
  1216. res = spiffs_page_delete(fs, orig_data_pix);
  1217. if (res != SPIFFS_OK) break;
  1218. // update memory representation of object index page with new data page
  1219. if (cur_objix_spix == 0) {
  1220. // update object index header page
  1221. ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = data_pix;
  1222. SPIFFS_DBG("modify: wrote page %04x to objix_hdr entry %02x in mem\n", data_pix, data_spix);
  1223. } else {
  1224. // update object index page
  1225. ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = data_pix;
  1226. SPIFFS_DBG("modify: wrote page %04x to objix entry %02x in mem\n", data_pix, SPIFFS_OBJ_IX_ENTRY(fs, data_spix));
  1227. }
  1228. // update internals
  1229. page_offs = 0;
  1230. data_spix++;
  1231. written += to_write;
  1232. } // while all data
  1233. fd->offset = offset+written;
  1234. fd->cursor_objix_pix = cur_objix_pix;
  1235. fd->cursor_objix_spix = cur_objix_spix;
  1236. // finalize updated object indices
  1237. s32_t res2 = SPIFFS_OK;
  1238. if (cur_objix_spix != 0) {
  1239. // wrote beyond object index header page
  1240. // write last modified object index page
  1241. // move and update page
  1242. spiffs_page_ix new_objix_pix;
  1243. res2 = spiffs_page_index_check(fs, fd, cur_objix_pix, cur_objix_spix);
  1244. SPIFFS_CHECK_RES(res2);
  1245. res2 = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix, fd->obj_id, 0, cur_objix_pix, &new_objix_pix);
  1246. SPIFFS_DBG("modify: store modified objix page, %04x:%04x, written %i\n", new_objix_pix, cur_objix_spix, written);
  1247. fd->cursor_objix_pix = new_objix_pix;
  1248. fd->cursor_objix_spix = cur_objix_spix;
  1249. SPIFFS_CHECK_RES(res2);
  1250. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);
  1251. } else {
  1252. // wrote within object index header page
  1253. res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1254. fd->objix_hdr_pix, fs->work, 0, 0, &new_objix_hdr_pix);
  1255. SPIFFS_DBG("modify: store modified objix_hdr page, %04x:%04x, written %i\n", new_objix_hdr_pix, 0, written);
  1256. SPIFFS_CHECK_RES(res2);
  1257. }
  1258. return res;
  1259. } // spiffs_object_modify
  1260. #endif // !SPIFFS_READ_ONLY
  1261. static s32_t spiffs_object_find_object_index_header_by_name_v(
  1262. spiffs *fs,
  1263. spiffs_obj_id obj_id,
  1264. spiffs_block_ix bix,
  1265. int ix_entry,
  1266. const void *user_const_p,
  1267. void *user_var_p) {
  1268. (void)user_var_p;
  1269. s32_t res;
  1270. spiffs_page_object_ix_header objix_hdr;
  1271. spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
  1272. if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||
  1273. (obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0) {
  1274. return SPIFFS_VIS_COUNTINUE;
  1275. }
  1276. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  1277. 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
  1278. SPIFFS_CHECK_RES(res);
  1279. if (objix_hdr.p_hdr.span_ix == 0 &&
  1280. (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
  1281. (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {
  1282. if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) {
  1283. return SPIFFS_OK;
  1284. }
  1285. }
  1286. return SPIFFS_VIS_COUNTINUE;
  1287. }
  1288. // Finds object index header page by name
  1289. s32_t spiffs_object_find_object_index_header_by_name(
  1290. spiffs *fs,
  1291. const u8_t name[SPIFFS_OBJ_NAME_LEN],
  1292. spiffs_page_ix *pix) {
  1293. s32_t res;
  1294. spiffs_block_ix bix;
  1295. int entry;
  1296. res = spiffs_obj_lu_find_entry_visitor(fs,
  1297. fs->cursor_block_ix,
  1298. fs->cursor_obj_lu_entry,
  1299. 0,
  1300. 0,
  1301. spiffs_object_find_object_index_header_by_name_v,
  1302. name,
  1303. 0,
  1304. &bix,
  1305. &entry);
  1306. if (res == SPIFFS_VIS_END) {
  1307. res = SPIFFS_ERR_NOT_FOUND;
  1308. }
  1309. SPIFFS_CHECK_RES(res);
  1310. if (pix) {
  1311. *pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, entry);
  1312. }
  1313. fs->cursor_block_ix = bix;
  1314. fs->cursor_obj_lu_entry = entry;
  1315. return res;
  1316. }
  1317. #if !SPIFFS_READ_ONLY
  1318. // Truncates object to new size. If new size is null, object may be removed totally
  1319. s32_t spiffs_object_truncate(
  1320. spiffs_fd *fd,
  1321. u32_t new_size,
  1322. u8_t remove) {
  1323. s32_t res = SPIFFS_OK;
  1324. spiffs *fs = fd->fs;
  1325. if ((fd->size == SPIFFS_UNDEFINED_LEN || fd->size == 0) && !remove) {
  1326. // no op
  1327. return res;
  1328. }
  1329. // need 2 pages if not removing: object index page + possibly chopped data page
  1330. if (remove == 0) {
  1331. res = spiffs_gc_check(fs, SPIFFS_DATA_PAGE_SIZE(fs) * 2);
  1332. SPIFFS_CHECK_RES(res);
  1333. }
  1334. spiffs_page_ix objix_pix = fd->objix_hdr_pix;
  1335. spiffs_span_ix data_spix = (fd->size > 0 ? fd->size-1 : 0) / SPIFFS_DATA_PAGE_SIZE(fs);
  1336. u32_t cur_size = fd->size == (u32_t)SPIFFS_UNDEFINED_LEN ? 0 : fd->size ;
  1337. spiffs_span_ix cur_objix_spix = 0;
  1338. spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;
  1339. spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  1340. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  1341. spiffs_page_ix data_pix;
  1342. spiffs_page_ix new_objix_hdr_pix;
  1343. // before truncating, check if object is to be fully removed and mark this
  1344. if (remove && new_size == 0) {
  1345. u8_t flags = ~( SPIFFS_PH_FLAG_USED | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE);
  1346. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_UPDT,
  1347. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, fd->objix_hdr_pix) + offsetof(spiffs_page_header, flags),
  1348. sizeof(u8_t),
  1349. (u8_t *)&flags);
  1350. SPIFFS_CHECK_RES(res);
  1351. }
  1352. // delete from end of object until desired len is reached
  1353. while (cur_size > new_size) {
  1354. cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  1355. // put object index for current data span index in work buffer
  1356. if (prev_objix_spix != cur_objix_spix) {
  1357. if (prev_objix_spix != (spiffs_span_ix)-1) {
  1358. // remove previous object index page
  1359. SPIFFS_DBG("truncate: delete objix page %04x:%04x\n", objix_pix, prev_objix_spix);
  1360. res = spiffs_page_index_check(fs, fd, objix_pix, prev_objix_spix);
  1361. SPIFFS_CHECK_RES(res);
  1362. res = spiffs_page_delete(fs, objix_pix);
  1363. SPIFFS_CHECK_RES(res);
  1364. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_DEL, fd->obj_id, objix->p_hdr.span_ix, objix_pix, 0);
  1365. if (prev_objix_spix > 0) {
  1366. // Update object index header page, unless we totally want to remove the file.
  1367. // If fully removing, we're not keeping consistency as good as when storing the header between chunks,
  1368. // would we be aborted. But when removing full files, a crammed system may otherwise
  1369. // report ERR_FULL a la windows. We cannot have that.
  1370. // Hence, take the risk - if aborted, a file check would free the lost pages and mend things
  1371. // as the file is marked as fully deleted in the beginning.
  1372. if (remove == 0) {
  1373. SPIFFS_DBG("truncate: update objix hdr page %04x:%04x to size %i\n", fd->objix_hdr_pix, prev_objix_spix, cur_size);
  1374. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1375. fd->objix_hdr_pix, 0, 0, cur_size, &new_objix_hdr_pix);
  1376. SPIFFS_CHECK_RES(res);
  1377. }
  1378. fd->size = cur_size;
  1379. }
  1380. }
  1381. // load current object index (header) page
  1382. if (cur_objix_spix == 0) {
  1383. objix_pix = fd->objix_hdr_pix;
  1384. } else {
  1385. res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &objix_pix);
  1386. SPIFFS_CHECK_RES(res);
  1387. }
  1388. SPIFFS_DBG("truncate: load objix page %04x:%04x for data spix:%04x\n", objix_pix, cur_objix_spix, data_spix);
  1389. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1390. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1391. SPIFFS_CHECK_RES(res);
  1392. SPIFFS_VALIDATE_OBJIX(objix_hdr->p_hdr, fd->obj_id, cur_objix_spix);
  1393. fd->cursor_objix_pix = objix_pix;
  1394. fd->cursor_objix_spix = cur_objix_spix;
  1395. fd->offset = cur_size;
  1396. prev_objix_spix = cur_objix_spix;
  1397. }
  1398. if (cur_objix_spix == 0) {
  1399. // get data page from object index header page
  1400. data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];
  1401. ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = SPIFFS_OBJ_ID_FREE;
  1402. } else {
  1403. // get data page from object index page
  1404. data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];
  1405. ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = SPIFFS_OBJ_ID_FREE;
  1406. }
  1407. SPIFFS_DBG("truncate: got data pix %04x\n", data_pix);
  1408. if (new_size == 0 || remove || cur_size - new_size >= SPIFFS_DATA_PAGE_SIZE(fs)) {
  1409. // delete full data page
  1410. res = spiffs_page_data_check(fs, fd, data_pix, data_spix);
  1411. if (res != SPIFFS_ERR_DELETED && res != SPIFFS_OK && res != SPIFFS_ERR_INDEX_REF_FREE) {
  1412. SPIFFS_DBG("truncate: err validating data pix %i\n", res);
  1413. break;
  1414. }
  1415. if (res == SPIFFS_OK) {
  1416. res = spiffs_page_delete(fs, data_pix);
  1417. if (res != SPIFFS_OK) {
  1418. SPIFFS_DBG("truncate: err deleting data pix %i\n", res);
  1419. break;
  1420. }
  1421. } else if (res == SPIFFS_ERR_DELETED || res == SPIFFS_ERR_INDEX_REF_FREE) {
  1422. res = SPIFFS_OK;
  1423. }
  1424. // update current size
  1425. if (cur_size % SPIFFS_DATA_PAGE_SIZE(fs) == 0) {
  1426. cur_size -= SPIFFS_DATA_PAGE_SIZE(fs);
  1427. } else {
  1428. cur_size -= cur_size % SPIFFS_DATA_PAGE_SIZE(fs);
  1429. }
  1430. fd->size = cur_size;
  1431. fd->offset = cur_size;
  1432. SPIFFS_DBG("truncate: delete data page %04x for data spix:%04x, cur_size:%i\n", data_pix, data_spix, cur_size);
  1433. } else {
  1434. // delete last page, partially
  1435. spiffs_page_header p_hdr;
  1436. spiffs_page_ix new_data_pix;
  1437. u32_t bytes_to_remove = SPIFFS_DATA_PAGE_SIZE(fs) - (new_size % SPIFFS_DATA_PAGE_SIZE(fs));
  1438. SPIFFS_DBG("truncate: delete %i bytes from data page %04x for data spix:%04x, cur_size:%i\n", bytes_to_remove, data_pix, data_spix, cur_size);
  1439. res = spiffs_page_data_check(fs, fd, data_pix, data_spix);
  1440. if (res != SPIFFS_OK) break;
  1441. p_hdr.obj_id = fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
  1442. p_hdr.span_ix = data_spix;
  1443. p_hdr.flags = 0xff;
  1444. // allocate new page and copy unmodified data
  1445. res = spiffs_page_allocate_data(fs, fd->obj_id & ~SPIFFS_OBJ_ID_IX_FLAG,
  1446. &p_hdr, 0, 0, 0, 0, &new_data_pix);
  1447. if (res != SPIFFS_OK) break;
  1448. res = spiffs_phys_cpy(fs, 0,
  1449. SPIFFS_PAGE_TO_PADDR(fs, new_data_pix) + sizeof(spiffs_page_header),
  1450. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header),
  1451. SPIFFS_DATA_PAGE_SIZE(fs) - bytes_to_remove);
  1452. if (res != SPIFFS_OK) break;
  1453. // delete original data page
  1454. res = spiffs_page_delete(fs, data_pix);
  1455. if (res != SPIFFS_OK) break;
  1456. p_hdr.flags &= ~SPIFFS_PH_FLAG_FINAL;
  1457. res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT,
  1458. fd->file_nbr,
  1459. SPIFFS_PAGE_TO_PADDR(fs, new_data_pix) + offsetof(spiffs_page_header, flags),
  1460. sizeof(u8_t),
  1461. (u8_t *)&p_hdr.flags);
  1462. if (res != SPIFFS_OK) break;
  1463. // update memory representation of object index page with new data page
  1464. if (cur_objix_spix == 0) {
  1465. // update object index header page
  1466. ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix] = new_data_pix;
  1467. SPIFFS_DBG("truncate: wrote page %04x to objix_hdr entry %02x in mem\n", new_data_pix, SPIFFS_OBJ_IX_ENTRY(fs, data_spix));
  1468. } else {
  1469. // update object index page
  1470. ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)] = new_data_pix;
  1471. SPIFFS_DBG("truncate: wrote page %04x to objix entry %02x in mem\n", new_data_pix, SPIFFS_OBJ_IX_ENTRY(fs, data_spix));
  1472. }
  1473. cur_size = new_size;
  1474. fd->size = new_size;
  1475. fd->offset = cur_size;
  1476. break;
  1477. }
  1478. data_spix--;
  1479. } // while all data
  1480. // update object indices
  1481. if (cur_objix_spix == 0) {
  1482. // update object index header page
  1483. if (cur_size == 0) {
  1484. if (remove) {
  1485. // remove object altogether
  1486. SPIFFS_DBG("truncate: remove object index header page %04x\n", objix_pix);
  1487. res = spiffs_page_index_check(fs, fd, objix_pix, 0);
  1488. SPIFFS_CHECK_RES(res);
  1489. res = spiffs_page_delete(fs, objix_pix);
  1490. SPIFFS_CHECK_RES(res);
  1491. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_DEL, fd->obj_id, 0, objix_pix, 0);
  1492. } else {
  1493. // make uninitialized object
  1494. SPIFFS_DBG("truncate: reset objix_hdr page %04x\n", objix_pix);
  1495. memset(fs->work + sizeof(spiffs_page_object_ix_header), 0xff,
  1496. SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix_header));
  1497. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1498. objix_pix, fs->work, 0, SPIFFS_UNDEFINED_LEN, &new_objix_hdr_pix);
  1499. SPIFFS_CHECK_RES(res);
  1500. }
  1501. } else {
  1502. // update object index header page
  1503. SPIFFS_DBG("truncate: update object index header page with indices and size\n");
  1504. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1505. objix_pix, fs->work, 0, cur_size, &new_objix_hdr_pix);
  1506. SPIFFS_CHECK_RES(res);
  1507. }
  1508. } else {
  1509. // update both current object index page and object index header page
  1510. spiffs_page_ix new_objix_pix;
  1511. res = spiffs_page_index_check(fs, fd, objix_pix, cur_objix_spix);
  1512. SPIFFS_CHECK_RES(res);
  1513. // move and update object index page
  1514. res = spiffs_page_move(fs, fd->file_nbr, (u8_t*)objix_hdr, fd->obj_id, 0, objix_pix, &new_objix_pix);
  1515. SPIFFS_CHECK_RES(res);
  1516. spiffs_cb_object_event(fs, fd, SPIFFS_EV_IX_UPD, fd->obj_id, objix->p_hdr.span_ix, new_objix_pix, 0);
  1517. SPIFFS_DBG("truncate: store modified objix page, %04x:%04x\n", new_objix_pix, cur_objix_spix);
  1518. fd->cursor_objix_pix = new_objix_pix;
  1519. fd->cursor_objix_spix = cur_objix_spix;
  1520. fd->offset = cur_size;
  1521. // update object index header page with new size
  1522. res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id,
  1523. fd->objix_hdr_pix, 0, 0, cur_size, &new_objix_hdr_pix);
  1524. SPIFFS_CHECK_RES(res);
  1525. }
  1526. fd->size = cur_size;
  1527. return res;
  1528. } // spiffs_object_truncate
  1529. #endif // !SPIFFS_READ_ONLY
  1530. s32_t spiffs_object_read(
  1531. spiffs_fd *fd,
  1532. u32_t offset,
  1533. u32_t len,
  1534. u8_t *dst) {
  1535. s32_t res = SPIFFS_OK;
  1536. spiffs *fs = fd->fs;
  1537. spiffs_page_ix objix_pix;
  1538. spiffs_page_ix data_pix;
  1539. spiffs_span_ix data_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);
  1540. u32_t cur_offset = offset;
  1541. spiffs_span_ix cur_objix_spix;
  1542. spiffs_span_ix prev_objix_spix = (spiffs_span_ix)-1;
  1543. spiffs_page_object_ix_header *objix_hdr = (spiffs_page_object_ix_header *)fs->work;
  1544. spiffs_page_object_ix *objix = (spiffs_page_object_ix *)fs->work;
  1545. while (cur_offset < offset + len) {
  1546. cur_objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
  1547. if (prev_objix_spix != cur_objix_spix) {
  1548. // load current object index (header) page
  1549. if (cur_objix_spix == 0) {
  1550. objix_pix = fd->objix_hdr_pix;
  1551. } else {
  1552. SPIFFS_DBG("read: find objix %04x:%04x\n", fd->obj_id, cur_objix_spix);
  1553. res = spiffs_obj_lu_find_id_and_span(fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, cur_objix_spix, 0, &objix_pix);
  1554. SPIFFS_CHECK_RES(res);
  1555. }
  1556. SPIFFS_DBG("read: load objix page %04x:%04x for data spix:%04x\n", objix_pix, cur_objix_spix, data_spix);
  1557. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ,
  1558. fd->file_nbr, SPIFFS_PAGE_TO_PADDR(fs, objix_pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->work);
  1559. SPIFFS_CHECK_RES(res);
  1560. SPIFFS_VALIDATE_OBJIX(objix->p_hdr, fd->obj_id, cur_objix_spix);
  1561. fd->offset = cur_offset;
  1562. fd->cursor_objix_pix = objix_pix;
  1563. fd->cursor_objix_spix = cur_objix_spix;
  1564. prev_objix_spix = cur_objix_spix;
  1565. }
  1566. if (cur_objix_spix == 0) {
  1567. // get data page from object index header page
  1568. data_pix = ((spiffs_page_ix*)((u8_t *)objix_hdr + sizeof(spiffs_page_object_ix_header)))[data_spix];
  1569. } else {
  1570. // get data page from object index page
  1571. data_pix = ((spiffs_page_ix*)((u8_t *)objix + sizeof(spiffs_page_object_ix)))[SPIFFS_OBJ_IX_ENTRY(fs, data_spix)];
  1572. }
  1573. // all remaining data
  1574. u32_t len_to_read = offset + len - cur_offset;
  1575. // remaining data in page
  1576. len_to_read = MIN(len_to_read, SPIFFS_DATA_PAGE_SIZE(fs) - (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)));
  1577. // remaining data in file
  1578. len_to_read = MIN(len_to_read, fd->size);
  1579. SPIFFS_DBG("read: offset:%i rd:%i data spix:%04x is data_pix:%04x addr:%08x\n", cur_offset, len_to_read, data_spix, data_pix,
  1580. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)));
  1581. if (len_to_read <= 0) {
  1582. res = SPIFFS_ERR_END_OF_OBJECT;
  1583. break;
  1584. }
  1585. res = spiffs_page_data_check(fs, fd, data_pix, data_spix);
  1586. SPIFFS_CHECK_RES(res);
  1587. res = _spiffs_rd(
  1588. fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_READ,
  1589. fd->file_nbr,
  1590. SPIFFS_PAGE_TO_PADDR(fs, data_pix) + sizeof(spiffs_page_header) + (cur_offset % SPIFFS_DATA_PAGE_SIZE(fs)),
  1591. len_to_read,
  1592. dst);
  1593. SPIFFS_CHECK_RES(res);
  1594. dst += len_to_read;
  1595. cur_offset += len_to_read;
  1596. fd->offset = cur_offset;
  1597. data_spix++;
  1598. }
  1599. return res;
  1600. }
  1601. #if !SPIFFS_READ_ONLY
  1602. typedef struct {
  1603. spiffs_obj_id min_obj_id;
  1604. spiffs_obj_id max_obj_id;
  1605. u32_t compaction;
  1606. const u8_t *conflicting_name;
  1607. } spiffs_free_obj_id_state;
  1608. 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,
  1609. const void *user_const_p, void *user_var_p) {
  1610. if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED) {
  1611. spiffs_obj_id min_obj_id = *((spiffs_obj_id*)user_var_p);
  1612. const u8_t *conflicting_name = (const u8_t*)user_const_p;
  1613. // if conflicting name parameter is given, also check if this name is found in object index hdrs
  1614. if (conflicting_name && (id & SPIFFS_OBJ_ID_IX_FLAG)) {
  1615. spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
  1616. int res;
  1617. spiffs_page_object_ix_header objix_hdr;
  1618. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  1619. 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
  1620. SPIFFS_CHECK_RES(res);
  1621. if (objix_hdr.p_hdr.span_ix == 0 &&
  1622. (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
  1623. (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {
  1624. if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) {
  1625. return SPIFFS_ERR_CONFLICTING_NAME;
  1626. }
  1627. }
  1628. }
  1629. id &= ~SPIFFS_OBJ_ID_IX_FLAG;
  1630. u32_t bit_ix = (id-min_obj_id) & 7;
  1631. int byte_ix = (id-min_obj_id) >> 3;
  1632. if (byte_ix >= 0 && (u32_t)byte_ix < SPIFFS_CFG_LOG_PAGE_SZ(fs)) {
  1633. fs->work[byte_ix] |= (1<<bit_ix);
  1634. }
  1635. }
  1636. return SPIFFS_VIS_COUNTINUE;
  1637. }
  1638. 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,
  1639. const void *user_const_p, void *user_var_p) {
  1640. (void)user_var_p;
  1641. if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED && (id & SPIFFS_OBJ_ID_IX_FLAG)) {
  1642. s32_t res;
  1643. const spiffs_free_obj_id_state *state = (const spiffs_free_obj_id_state*)user_const_p;
  1644. spiffs_page_object_ix_header objix_hdr;
  1645. res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
  1646. 0, SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, bix, ix_entry), sizeof(spiffs_page_object_ix_header), (u8_t*)&objix_hdr);
  1647. if (res == SPIFFS_OK && objix_hdr.p_hdr.span_ix == 0 &&
  1648. ((objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET)) ==
  1649. (SPIFFS_PH_FLAG_DELET))) {
  1650. // ok object look up entry
  1651. if (state->conflicting_name && strcmp((const char *)state->conflicting_name, (char *)objix_hdr.name) == 0) {
  1652. return SPIFFS_ERR_CONFLICTING_NAME;
  1653. }
  1654. id &= ~SPIFFS_OBJ_ID_IX_FLAG;
  1655. if (id >= state->min_obj_id && id <= state->max_obj_id) {
  1656. u8_t *map = (u8_t *)fs->work;
  1657. int ix = (id - state->min_obj_id) / state->compaction;
  1658. //SPIFFS_DBG("free_obj_id: add ix %i for id %04x min:%04x max%04x comp:%i\n", ix, id, state->min_obj_id, state->max_obj_id, state->compaction);
  1659. map[ix]++;
  1660. }
  1661. }
  1662. }
  1663. return SPIFFS_VIS_COUNTINUE;
  1664. }
  1665. // Scans thru all object lookup for object index header pages. If total possible number of
  1666. // object ids cannot fit into a work buffer, these are grouped. When a group containing free
  1667. // object ids is found, the object lu is again scanned for object ids within group and bitmasked.
  1668. // Finally, the bitmask is searched for a free id
  1669. s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, const u8_t *conflicting_name) {
  1670. s32_t res = SPIFFS_OK;
  1671. u32_t max_objects = (fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) / 2;
  1672. spiffs_free_obj_id_state state;
  1673. spiffs_obj_id free_obj_id = SPIFFS_OBJ_ID_FREE;
  1674. state.min_obj_id = 1;
  1675. state.max_obj_id = max_objects + 1;
  1676. if (state.max_obj_id & SPIFFS_OBJ_ID_IX_FLAG) {
  1677. state.max_obj_id = ((spiffs_obj_id)-1) & ~SPIFFS_OBJ_ID_IX_FLAG;
  1678. }
  1679. state.compaction = 0;
  1680. state.conflicting_name = conflicting_name;
  1681. while (res == SPIFFS_OK && free_obj_id == SPIFFS_OBJ_ID_FREE) {
  1682. if (state.max_obj_id - state.min_obj_id <= (spiffs_obj_id)SPIFFS_CFG_LOG_PAGE_SZ(fs)*8) {
  1683. // possible to represent in bitmap
  1684. u32_t i, j;
  1685. SPIFFS_DBG("free_obj_id: BITM min:%04x max:%04x\n", state.min_obj_id, state.max_obj_id);
  1686. memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));
  1687. res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_bitmap_v,
  1688. conflicting_name, &state.min_obj_id, 0, 0);
  1689. if (res == SPIFFS_VIS_END) res = SPIFFS_OK;
  1690. SPIFFS_CHECK_RES(res);
  1691. // traverse bitmask until found free obj_id
  1692. for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs); i++) {
  1693. u8_t mask = fs->work[i];
  1694. if (mask == 0xff) {
  1695. continue;
  1696. }
  1697. for (j = 0; j < 8; j++) {
  1698. if ((mask & (1<<j)) == 0) {
  1699. *obj_id = (i<<3)+j+state.min_obj_id;
  1700. return SPIFFS_OK;
  1701. }
  1702. }
  1703. }
  1704. return SPIFFS_ERR_FULL;
  1705. } else {
  1706. // not possible to represent all ids in range in a bitmap, compact and count
  1707. if (state.compaction != 0) {
  1708. // select element in compacted table, decrease range and recompact
  1709. u32_t i, min_i = 0;
  1710. u8_t *map = (u8_t *)fs->work;
  1711. u8_t min_count = 0xff;
  1712. for (i = 0; i < SPIFFS_CFG_LOG_PAGE_SZ(fs)/sizeof(u8_t); i++) {
  1713. if (map[i] < min_count) {
  1714. min_count = map[i];
  1715. min_i = i;
  1716. if (min_count == 0) {
  1717. break;
  1718. }
  1719. }
  1720. }
  1721. if (min_count == state.compaction) {
  1722. // there are no free objids!
  1723. SPIFFS_DBG("free_obj_id: compacted table is full\n");
  1724. return SPIFFS_ERR_FULL;
  1725. }
  1726. SPIFFS_DBG("free_obj_id: COMP select index:%i min_count:%i min:%04x max:%04x compact:%i\n", min_i, min_count, state.min_obj_id, state.max_obj_id, state.compaction);
  1727. if (min_count == 0) {
  1728. // no id in this range, skip compacting and use directly
  1729. *obj_id = min_i * state.compaction + state.min_obj_id;
  1730. return SPIFFS_OK;
  1731. } else {
  1732. SPIFFS_DBG("free_obj_id: COMP SEL chunk:%04x min:%04x -> %04x\n", state.compaction, state.min_obj_id, state.min_obj_id + min_i * state.compaction);
  1733. state.min_obj_id += min_i * state.compaction;
  1734. state.max_obj_id = state.min_obj_id + state.compaction;
  1735. // decrease compaction
  1736. }
  1737. if ((state.max_obj_id - state.min_obj_id <= (spiffs_obj_id)SPIFFS_CFG_LOG_PAGE_SZ(fs)*8)) {
  1738. // no need for compacting, use bitmap
  1739. continue;
  1740. }
  1741. }
  1742. // in a work memory of log_page_size bytes, we may fit in log_page_size ids
  1743. // todo what if compaction is > 255 - then we cannot fit it in a byte
  1744. state.compaction = (state.max_obj_id-state.min_obj_id) / ((SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(u8_t)));
  1745. SPIFFS_DBG("free_obj_id: COMP min:%04x max:%04x compact:%i\n", state.min_obj_id, state.max_obj_id, state.compaction);
  1746. memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs));
  1747. 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);
  1748. if (res == SPIFFS_VIS_END) res = SPIFFS_OK;
  1749. SPIFFS_CHECK_RES(res);
  1750. state.conflicting_name = 0; // searched for conflicting name once, no need to do it again
  1751. }
  1752. }
  1753. return res;
  1754. }
  1755. #endif // !SPIFFS_READ_ONLY
  1756. s32_t spiffs_fd_find_new(spiffs *fs, spiffs_fd **fd) {
  1757. u32_t i;
  1758. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  1759. for (i = 0; i < fs->fd_count; i++) {
  1760. spiffs_fd *cur_fd = &fds[i];
  1761. if (cur_fd->file_nbr == 0) {
  1762. cur_fd->file_nbr = i+1;
  1763. *fd = cur_fd;
  1764. return SPIFFS_OK;
  1765. }
  1766. }
  1767. return SPIFFS_ERR_OUT_OF_FILE_DESCS;
  1768. }
  1769. s32_t spiffs_fd_return(spiffs *fs, spiffs_file f) {
  1770. if (f <= 0 || f > (s16_t)fs->fd_count) {
  1771. return SPIFFS_ERR_BAD_DESCRIPTOR;
  1772. }
  1773. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  1774. spiffs_fd *fd = &fds[f-1];
  1775. if (fd->file_nbr == 0) {
  1776. return SPIFFS_ERR_FILE_CLOSED;
  1777. }
  1778. fd->file_nbr = 0;
  1779. return SPIFFS_OK;
  1780. }
  1781. s32_t spiffs_fd_get(spiffs *fs, spiffs_file f, spiffs_fd **fd) {
  1782. if (f <= 0 || f > (s16_t)fs->fd_count) {
  1783. return SPIFFS_ERR_BAD_DESCRIPTOR;
  1784. }
  1785. spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
  1786. *fd = &fds[f-1];
  1787. if ((*fd)->file_nbr == 0) {
  1788. return SPIFFS_ERR_FILE_CLOSED;
  1789. }
  1790. return SPIFFS_OK;
  1791. }