Merge pull request #2654 from Antiklesys/master
[RRG-proxmark3.git] / armsrc / spiffs_hydrogen.c
blob93c7fbe89811979ba1c924e06936b8b770d609a3
1 //-----------------------------------------------------------------------------
2 // Borrowed initially from https://github.com/pellepl/spiffs
3 // Copyright (c) 2013-2017 Peter Andersson (pelleplutt1976 at gmail.com)
4 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // See LICENSE.txt for the text of the license.
17 //-----------------------------------------------------------------------------
19 #include "spiffs.h"
20 #include "spiffs_nucleus.h"
21 #include "printf.h"
23 #if SPIFFS_CACHE == 1
24 static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh);
25 #endif
27 #if SPIFFS_BUFFER_HELP
28 u32_t SPIFFS_buffer_bytes_for_filedescs(spiffs *fs, u32_t num_descs) {
29 return num_descs * sizeof(spiffs_fd);
31 #if SPIFFS_CACHE
32 u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages) {
33 return sizeof(spiffs_cache) + num_pages * (sizeof(spiffs_cache_page) + SPIFFS_CFG_LOG_PAGE_SZ(fs));
35 #endif
36 #endif
38 u8_t SPIFFS_mounted(spiffs *fs) {
39 return SPIFFS_CHECK_MOUNT(fs);
42 s32_t SPIFFS_format(spiffs *fs) {
43 #if SPIFFS_READ_ONLY
44 (void)fs;
45 return SPIFFS_ERR_RO_NOT_IMPL;
46 #else
47 SPIFFS_API_CHECK_CFG(fs);
48 if (SPIFFS_CHECK_MOUNT(fs)) {
49 fs->err_code = SPIFFS_ERR_MOUNTED;
50 return -1;
53 SPIFFS_LOCK(fs);
55 spiffs_block_ix bix = 0;
56 while (bix < fs->block_count) {
57 fs->max_erase_count = 0;
58 s32_t res = spiffs_erase_block(fs, bix);
59 if (res != SPIFFS_OK) {
60 res = SPIFFS_ERR_ERASE_FAIL;
62 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
63 bix++;
66 SPIFFS_UNLOCK(fs);
68 return 0;
69 #endif // SPIFFS_READ_ONLY
72 #if SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
74 s32_t SPIFFS_probe_fs(spiffs_config *config) {
75 SPIFFS_API_DBG("%s\n", __func__);
76 s32_t res = spiffs_probe(config);
77 return res;
80 #endif // SPIFFS_USE_MAGIC && SPIFFS_USE_MAGIC_LENGTH && SPIFFS_SINGLETON==0
82 s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work,
83 u8_t *fd_space, u32_t fd_space_size,
84 void *cache, u32_t cache_size,
85 spiffs_check_callback check_cb_f) {
86 SPIFFS_API_DBG("%s "
87 " sz:"_SPIPRIi " logpgsz:"_SPIPRIi " logblksz:"_SPIPRIi " perasz:"_SPIPRIi
88 " addr:"_SPIPRIad
89 " fdsz:"_SPIPRIi " cachesz:"_SPIPRIi
90 "\n",
91 __func__,
92 SPIFFS_CFG_PHYS_SZ(fs),
93 SPIFFS_CFG_LOG_PAGE_SZ(fs),
94 SPIFFS_CFG_LOG_BLOCK_SZ(fs),
95 SPIFFS_CFG_PHYS_ERASE_SZ(fs),
96 SPIFFS_CFG_PHYS_ADDR(fs),
97 fd_space_size, cache_size);
98 void *user_data;
99 SPIFFS_LOCK(fs);
100 user_data = fs->user_data;
101 memset(fs, 0, sizeof(spiffs));
102 _SPIFFS_MEMCPY(&fs->cfg, config, sizeof(spiffs_config));
103 fs->user_data = user_data;
104 fs->block_count = SPIFFS_CFG_PHYS_SZ(fs) / SPIFFS_CFG_LOG_BLOCK_SZ(fs);
105 fs->work = &work[0];
106 fs->lu_work = &work[SPIFFS_CFG_LOG_PAGE_SZ(fs)];
107 memset(fd_space, 0, fd_space_size);
108 // align fd_space pointer to pointer size byte boundary
109 u8_t ptr_size = sizeof(void *);
110 u8_t addr_lsb = ((u8_t)(intptr_t)fd_space) & (ptr_size - 1);
111 if (addr_lsb) {
112 fd_space += (ptr_size - addr_lsb);
113 fd_space_size -= (ptr_size - addr_lsb);
115 fs->fd_space = fd_space;
116 fs->fd_count = (fd_space_size / sizeof(spiffs_fd));
118 // align cache pointer to 4 byte boundary
119 addr_lsb = ((u8_t)(intptr_t)cache) & (ptr_size - 1);
120 if (addr_lsb) {
121 u8_t *cache_8 = (u8_t *)cache;
122 cache_8 += (ptr_size - addr_lsb);
123 cache = cache_8;
124 cache_size -= (ptr_size - addr_lsb);
126 if (cache_size & (ptr_size - 1)) {
127 cache_size -= (cache_size & (ptr_size - 1));
130 #if SPIFFS_CACHE
131 fs->cache = cache;
132 fs->cache_size = (cache_size > (SPIFFS_CFG_LOG_PAGE_SZ(fs) * 32)) ? SPIFFS_CFG_LOG_PAGE_SZ(fs) * 32 : cache_size;
133 spiffs_cache_init(fs);
134 #endif
136 s32_t res;
138 #if SPIFFS_USE_MAGIC
139 res = SPIFFS_CHECK_MAGIC_POSSIBLE(fs) ? SPIFFS_OK : SPIFFS_ERR_MAGIC_NOT_POSSIBLE;
140 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
141 #endif
143 fs->config_magic = SPIFFS_CONFIG_MAGIC;
145 res = spiffs_obj_lu_scan(fs);
146 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
148 SPIFFS_DBG("page index byte len: "_SPIPRIi"\n", (u32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs));
149 SPIFFS_DBG("object lookup pages: "_SPIPRIi"\n", (u32_t)SPIFFS_OBJ_LOOKUP_PAGES(fs));
150 SPIFFS_DBG("page pages per block: "_SPIPRIi"\n", (u32_t)SPIFFS_PAGES_PER_BLOCK(fs));
151 SPIFFS_DBG("page header length: "_SPIPRIi"\n", (u32_t)sizeof(spiffs_page_header));
152 SPIFFS_DBG("object header index entries: "_SPIPRIi"\n", (u32_t)SPIFFS_OBJ_HDR_IX_LEN(fs));
153 SPIFFS_DBG("object index entries: "_SPIPRIi"\n", (u32_t)SPIFFS_OBJ_IX_LEN(fs));
154 SPIFFS_DBG("available file descriptors: "_SPIPRIi"\n", (u32_t)fs->fd_count);
155 SPIFFS_DBG("free blocks: "_SPIPRIi"\n", (u32_t)fs->free_blocks);
157 fs->check_cb_f = check_cb_f;
159 fs->mounted = 1;
161 SPIFFS_UNLOCK(fs);
163 return 0;
166 void SPIFFS_unmount(spiffs *fs) {
167 SPIFFS_API_DBG("%s\n", __func__);
168 if (!SPIFFS_CHECK_CFG(fs) || !SPIFFS_CHECK_MOUNT(fs)) return;
169 SPIFFS_LOCK(fs);
170 u32_t i;
171 spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
172 for (i = 0; i < fs->fd_count; i++) {
173 spiffs_fd *cur_fd = &fds[i];
174 if (cur_fd->file_nbr != 0) {
175 #if SPIFFS_CACHE
176 (void)spiffs_fflush_cache(fs, cur_fd->file_nbr);
177 #endif
178 spiffs_fd_return(fs, cur_fd->file_nbr);
181 fs->mounted = 0;
183 SPIFFS_UNLOCK(fs);
186 s32_t SPIFFS_errno(spiffs *fs) {
187 return fs->err_code;
190 void SPIFFS_clearerr(spiffs *fs) {
191 SPIFFS_API_DBG("%s\n", __func__);
192 fs->err_code = SPIFFS_OK;
195 s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) {
196 SPIFFS_API_DBG("%s '%s'\n", __func__, path);
197 #if SPIFFS_READ_ONLY
198 (void)fs;
199 (void)path;
200 (void)mode;
201 return SPIFFS_ERR_RO_NOT_IMPL;
202 #else
203 (void)mode;
204 SPIFFS_API_CHECK_CFG(fs);
205 SPIFFS_API_CHECK_MOUNT(fs);
206 if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
207 SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
209 SPIFFS_LOCK(fs);
210 spiffs_obj_id obj_id;
211 s32_t res;
213 res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (const u8_t *)path);
214 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
215 res = spiffs_object_create(fs, obj_id, (const u8_t *)path, 0, SPIFFS_TYPE_FILE, 0);
216 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
217 SPIFFS_UNLOCK(fs);
218 return 0;
219 #endif // SPIFFS_READ_ONLY
222 spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode) {
223 SPIFFS_API_DBG("%s '%s' "_SPIPRIfl "\n", __func__, path, flags);
224 (void)mode;
225 SPIFFS_API_CHECK_CFG(fs);
226 SPIFFS_API_CHECK_MOUNT(fs);
227 if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
228 SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
230 SPIFFS_LOCK(fs);
232 spiffs_fd *fd;
233 spiffs_page_ix pix;
235 #if SPIFFS_READ_ONLY
236 // not valid flags in read only mode
237 flags &= ~(SPIFFS_WRONLY | SPIFFS_CREAT | SPIFFS_TRUNC);
238 #endif // SPIFFS_READ_ONLY
240 s32_t res = spiffs_fd_find_new(fs, &fd, path);
241 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
243 res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t *)path, &pix);
244 if ((flags & SPIFFS_O_CREAT) == 0) {
245 if (res < SPIFFS_OK) {
246 spiffs_fd_return(fs, fd->file_nbr);
248 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
251 if (res == SPIFFS_OK &&
252 (flags & (SPIFFS_O_CREAT | SPIFFS_O_EXCL)) == (SPIFFS_O_CREAT | SPIFFS_O_EXCL)) {
253 // creat and excl and file exists - fail
254 res = SPIFFS_ERR_FILE_EXISTS;
255 spiffs_fd_return(fs, fd->file_nbr);
256 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
259 if ((flags & SPIFFS_O_CREAT) && res == SPIFFS_ERR_NOT_FOUND) {
260 #if !SPIFFS_READ_ONLY
261 spiffs_obj_id obj_id;
262 // no need to enter conflicting name here, already looked for it above
263 res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, 0);
264 if (res < SPIFFS_OK) {
265 spiffs_fd_return(fs, fd->file_nbr);
267 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
268 res = spiffs_object_create(fs, obj_id, (const u8_t *)path, 0, SPIFFS_TYPE_FILE, &pix);
269 if (res < SPIFFS_OK) {
270 spiffs_fd_return(fs, fd->file_nbr);
272 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
273 flags &= ~SPIFFS_O_TRUNC;
274 #endif // !SPIFFS_READ_ONLY
275 } else {
276 if (res < SPIFFS_OK) {
277 spiffs_fd_return(fs, fd->file_nbr);
279 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
281 res = spiffs_object_open_by_page(fs, pix, fd, flags, mode);
282 if (res < SPIFFS_OK) {
283 spiffs_fd_return(fs, fd->file_nbr);
285 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
286 #if !SPIFFS_READ_ONLY
287 if (flags & SPIFFS_O_TRUNC) {
288 res = spiffs_object_truncate(fd, 0, 0);
289 if (res < SPIFFS_OK) {
290 spiffs_fd_return(fs, fd->file_nbr);
292 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
294 #endif // !SPIFFS_READ_ONLY
296 fd->fdoffset = 0;
298 SPIFFS_UNLOCK(fs);
300 return SPIFFS_FH_OFFS(fs, fd->file_nbr);
303 spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) {
304 SPIFFS_API_DBG("%s '%s':"_SPIPRIid " "_SPIPRIfl "\n", __func__, e->name, e->obj_id, flags);
305 SPIFFS_API_CHECK_CFG(fs);
306 SPIFFS_API_CHECK_MOUNT(fs);
307 SPIFFS_LOCK(fs);
309 spiffs_fd *fd;
311 s32_t res = spiffs_fd_find_new(fs, &fd, 0);
312 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
314 res = spiffs_object_open_by_page(fs, e->pix, fd, flags, mode);
315 if (res < SPIFFS_OK) {
316 spiffs_fd_return(fs, fd->file_nbr);
318 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
319 #if !SPIFFS_READ_ONLY
320 if (flags & SPIFFS_O_TRUNC) {
321 res = spiffs_object_truncate(fd, 0, 0);
322 if (res < SPIFFS_OK) {
323 spiffs_fd_return(fs, fd->file_nbr);
325 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
327 #endif // !SPIFFS_READ_ONLY
329 fd->fdoffset = 0;
331 SPIFFS_UNLOCK(fs);
333 return SPIFFS_FH_OFFS(fs, fd->file_nbr);
336 spiffs_file SPIFFS_open_by_page(spiffs *fs, spiffs_page_ix page_ix, spiffs_flags flags, spiffs_mode mode) {
337 SPIFFS_API_DBG("%s "_SPIPRIpg " "_SPIPRIfl "\n", __func__, page_ix, flags);
338 SPIFFS_API_CHECK_CFG(fs);
339 SPIFFS_API_CHECK_MOUNT(fs);
340 SPIFFS_LOCK(fs);
342 spiffs_fd *fd;
344 s32_t res = spiffs_fd_find_new(fs, &fd, 0);
345 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
347 if (SPIFFS_IS_LOOKUP_PAGE(fs, page_ix)) {
348 res = SPIFFS_ERR_NOT_A_FILE;
349 spiffs_fd_return(fs, fd->file_nbr);
350 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
353 res = spiffs_object_open_by_page(fs, page_ix, fd, flags, mode);
354 if (res == SPIFFS_ERR_IS_FREE ||
355 res == SPIFFS_ERR_DELETED ||
356 res == SPIFFS_ERR_NOT_FINALIZED ||
357 res == SPIFFS_ERR_NOT_INDEX ||
358 res == SPIFFS_ERR_INDEX_SPAN_MISMATCH) {
359 res = SPIFFS_ERR_NOT_A_FILE;
361 if (res < SPIFFS_OK) {
362 spiffs_fd_return(fs, fd->file_nbr);
364 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
366 #if !SPIFFS_READ_ONLY
367 if (flags & SPIFFS_O_TRUNC) {
368 res = spiffs_object_truncate(fd, 0, 0);
369 if (res < SPIFFS_OK) {
370 spiffs_fd_return(fs, fd->file_nbr);
372 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
374 #endif // !SPIFFS_READ_ONLY
376 fd->fdoffset = 0;
378 SPIFFS_UNLOCK(fs);
380 return SPIFFS_FH_OFFS(fs, fd->file_nbr);
383 static s32_t spiffs_hydro_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
384 SPIFFS_API_CHECK_CFG(fs);
385 SPIFFS_API_CHECK_MOUNT(fs);
386 SPIFFS_LOCK(fs);
388 spiffs_fd *fd;
389 s32_t res;
391 fh = SPIFFS_FH_UNOFFS(fs, fh);
392 res = spiffs_fd_get(fs, fh, &fd);
393 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
395 if ((fd->flags & SPIFFS_O_RDONLY) == 0) {
396 res = SPIFFS_ERR_NOT_READABLE;
397 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
400 if (fd->size == SPIFFS_UNDEFINED_LEN && len > 0) {
401 // special case for zero sized files
402 res = SPIFFS_ERR_END_OF_OBJECT;
403 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
406 #if SPIFFS_CACHE_WR
407 spiffs_fflush_cache(fs, fh);
408 #endif
410 if (fd->fdoffset + len >= fd->size) {
411 // reading beyond file size
412 s32_t avail = fd->size - fd->fdoffset;
413 if (avail <= 0) {
414 SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_END_OF_OBJECT);
416 res = spiffs_object_read(fd, fd->fdoffset, avail, (u8_t *)buf);
417 if (res == SPIFFS_ERR_END_OF_OBJECT) {
418 fd->fdoffset += avail;
419 SPIFFS_UNLOCK(fs);
420 return avail;
421 } else {
422 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
423 len = avail;
425 } else {
426 // reading within file size
427 res = spiffs_object_read(fd, fd->fdoffset, len, (u8_t *)buf);
428 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
430 fd->fdoffset += len;
432 SPIFFS_UNLOCK(fs);
434 return len;
437 s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
438 SPIFFS_API_DBG("%s "_SPIPRIfd " "_SPIPRIi "\n", __func__, fh, len);
439 s32_t res = spiffs_hydro_read(fs, fh, buf, len);
440 if (res == SPIFFS_ERR_END_OF_OBJECT) {
441 res = 0;
443 return res;
447 #if !SPIFFS_READ_ONLY
448 static s32_t spiffs_hydro_write(spiffs *fs, spiffs_fd *fd, void *buf, u32_t offset, s32_t len) {
449 (void)fs;
450 s32_t res = SPIFFS_OK;
451 s32_t remaining = len;
452 if (fd->size != SPIFFS_UNDEFINED_LEN && offset < fd->size) {
453 s32_t m_len = MIN((s32_t)(fd->size - offset), len);
454 res = spiffs_object_modify(fd, offset, (u8_t *)buf, m_len);
455 SPIFFS_CHECK_RES(res);
456 remaining -= m_len;
457 u8_t *buf_8 = (u8_t *)buf;
458 buf_8 += m_len;
459 buf = buf_8;
460 offset += m_len;
462 if (remaining > 0) {
463 res = spiffs_object_append(fd, offset, (u8_t *)buf, remaining);
464 SPIFFS_CHECK_RES(res);
466 return len;
469 #endif // !SPIFFS_READ_ONLY
471 s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
472 SPIFFS_API_DBG("%s "_SPIPRIfd " "_SPIPRIi "\n", __func__, fh, len);
473 #if SPIFFS_READ_ONLY
474 (void)fs;
475 (void)fh;
476 (void)buf;
477 (void)len;
478 return SPIFFS_ERR_RO_NOT_IMPL;
479 #else
480 SPIFFS_API_CHECK_CFG(fs);
481 SPIFFS_API_CHECK_MOUNT(fs);
482 SPIFFS_LOCK(fs);
484 spiffs_fd *fd;
485 s32_t res;
486 u32_t offset;
488 fh = SPIFFS_FH_UNOFFS(fs, fh);
489 res = spiffs_fd_get(fs, fh, &fd);
490 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
492 if ((fd->flags & SPIFFS_O_WRONLY) == 0) {
493 res = SPIFFS_ERR_NOT_WRITABLE;
494 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
497 if ((fd->flags & SPIFFS_O_APPEND)) {
498 fd->fdoffset = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size;
500 offset = fd->fdoffset;
502 #if SPIFFS_CACHE_WR
503 if (fd->cache_page == 0) {
504 // see if object id is associated with cache already
505 fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
507 #endif
508 if (fd->flags & SPIFFS_O_APPEND) {
509 if (fd->size == SPIFFS_UNDEFINED_LEN) {
510 offset = 0;
511 } else {
512 offset = fd->size;
514 #if SPIFFS_CACHE_WR
515 if (fd->cache_page) {
516 offset = MAX(offset, fd->cache_page->ucache.swrc.offset + fd->cache_page->ucache.swrc.size);
518 #endif
521 #if SPIFFS_CACHE_WR
522 if ((fd->flags & SPIFFS_O_DIRECT) == 0) {
523 if (len < (s32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs)) {
524 // small write, try to cache it
525 u8_t alloc_cpage = 1;
526 if (fd->cache_page) {
527 // have a cached page for this fd already, check cache page boundaries
528 if (offset < fd->cache_page->ucache.swrc.offset || // writing before cache
529 offset > fd->cache_page->ucache.swrc.offset + fd->cache_page->ucache.swrc.size || // writing after cache
530 offset + len > fd->cache_page->ucache.swrc.offset + SPIFFS_CFG_LOG_PAGE_SZ(fs)) { // writing beyond cache page
531 // boundary violation, write back cache first and allocate new
532 SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page "_SPIPRIi" for fd "_SPIPRIfd":"_SPIPRIid", boundary viol, offs:"_SPIPRIi" size:"_SPIPRIi"\n",
533 fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->ucache.swrc.offset, fd->cache_page->ucache.swrc.size);
534 res = spiffs_hydro_write(fs, fd,
535 spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
536 fd->cache_page->ucache.swrc.offset, fd->cache_page->ucache.swrc.size);
537 spiffs_cache_fd_release(fs, fd->cache_page);
538 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
539 } else {
540 // writing within cache
541 alloc_cpage = 0;
545 if (alloc_cpage) {
546 fd->cache_page = spiffs_cache_page_allocate_by_fd(fs, fd);
547 if (fd->cache_page) {
548 fd->cache_page->ucache.swrc.offset = offset;
549 fd->cache_page->ucache.swrc.size = 0;
550 SPIFFS_CACHE_DBG("CACHE_WR_ALLO: allocating cache page "_SPIPRIi" for fd "_SPIPRIfd":"_SPIPRIid"\n",
551 fd->cache_page->ix, fd->file_nbr, fd->obj_id);
555 if (fd->cache_page) {
556 u32_t offset_in_cpage = offset - fd->cache_page->ucache.swrc.offset;
557 SPIFFS_CACHE_DBG("CACHE_WR_WRITE: storing to cache page "_SPIPRIi" for fd "_SPIPRIfd":"_SPIPRIid", offs "_SPIPRIi":"_SPIPRIi" len "_SPIPRIi"\n",
558 fd->cache_page->ix, fd->file_nbr, fd->obj_id,
559 offset, offset_in_cpage, len);
560 spiffs_cache *cache = spiffs_get_cache(fs);
561 u8_t *cpage_data = spiffs_get_cache_page(fs, cache, fd->cache_page->ix);
562 #ifdef _SPIFFS_TEST
564 intptr_t __a1 = (u8_t *)&cpage_data[offset_in_cpage] - (u8_t *)cache;
565 intptr_t __a2 = (u8_t *)&cpage_data[offset_in_cpage] + len - (u8_t *)cache;
566 intptr_t __b = sizeof(spiffs_cache) + cache->cpage_count * (sizeof(spiffs_cache_page) + SPIFFS_CFG_LOG_PAGE_SZ(fs));
567 if (__a1 > __b || __a2 > __b) {
568 printf("FATAL OOB: CACHE_WR: memcpy to cache buffer ixs:%4ld..%4ld of %4ld\n", __a1, __a2, __b);
569 ERREXIT();
572 #endif
573 _SPIFFS_MEMCPY(&cpage_data[offset_in_cpage], buf, len);
574 fd->cache_page->ucache.swrc.size = MAX(fd->cache_page->ucache.swrc.size, offset_in_cpage + len);
575 fd->fdoffset += len;
576 SPIFFS_UNLOCK(fs);
577 return len;
578 } else {
579 res = spiffs_hydro_write(fs, fd, buf, offset, len);
580 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
581 fd->fdoffset += len;
582 SPIFFS_UNLOCK(fs);
583 return res;
585 } else {
586 // big write, no need to cache it - but first check if there is a cached write already
587 if (fd->cache_page) {
588 // write back cache first
589 SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page "_SPIPRIi" for fd "_SPIPRIfd":"_SPIPRIid", big write, offs:"_SPIPRIi" size:"_SPIPRIi"\n",
590 fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->ucache.swrc.offset, fd->cache_page->ucache.swrc.size);
591 res = spiffs_hydro_write(fs, fd,
592 spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
593 fd->cache_page->ucache.swrc.offset, fd->cache_page->ucache.swrc.size);
594 spiffs_cache_fd_release(fs, fd->cache_page);
595 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
596 // data written below
600 #endif
602 res = spiffs_hydro_write(fs, fd, buf, offset, len);
603 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
604 fd->fdoffset += len;
606 SPIFFS_UNLOCK(fs);
608 return res;
609 #endif // SPIFFS_READ_ONLY
612 s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) {
613 SPIFFS_API_DBG("%s "_SPIPRIfd " "_SPIPRIi " %s\n", __func__, fh, offs, (const char *[]) {"SET", "CUR", "END", "???"}[MIN(whence, 3)]);
614 SPIFFS_API_CHECK_CFG(fs);
615 SPIFFS_API_CHECK_MOUNT(fs);
616 SPIFFS_LOCK(fs);
618 spiffs_fd *fd;
619 s32_t res;
620 fh = SPIFFS_FH_UNOFFS(fs, fh);
621 res = spiffs_fd_get(fs, fh, &fd);
622 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
624 #if SPIFFS_CACHE_WR
625 spiffs_fflush_cache(fs, fh);
626 #endif
628 s32_t file_size = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size;
630 switch (whence) {
631 case SPIFFS_SEEK_CUR:
632 offs = fd->fdoffset + offs;
633 break;
634 case SPIFFS_SEEK_END:
635 offs = file_size + offs;
636 break;
638 if (offs < 0) {
639 SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_SEEK_BOUNDS);
641 if (offs > file_size) {
642 fd->fdoffset = file_size;
643 res = SPIFFS_ERR_END_OF_OBJECT;
645 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
647 spiffs_span_ix data_spix = (offs > 0 ? (offs - 1) : 0) / SPIFFS_DATA_PAGE_SIZE(fs);
648 spiffs_span_ix objix_spix = SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, data_spix);
649 if (fd->cursor_objix_spix != objix_spix) {
650 spiffs_page_ix pix;
651 res = spiffs_obj_lu_find_id_and_span(
652 fs, fd->obj_id | SPIFFS_OBJ_ID_IX_FLAG, objix_spix, 0, &pix);
653 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
654 fd->cursor_objix_spix = objix_spix;
655 fd->cursor_objix_pix = pix;
657 fd->fdoffset = offs;
659 SPIFFS_UNLOCK(fs);
661 return offs;
664 s32_t SPIFFS_remove(spiffs *fs, const char *path) {
665 SPIFFS_API_DBG("%s '%s'\n", __func__, path);
666 #if SPIFFS_READ_ONLY
667 (void)fs;
668 (void)path;
669 return SPIFFS_ERR_RO_NOT_IMPL;
670 #else
671 SPIFFS_API_CHECK_CFG(fs);
672 SPIFFS_API_CHECK_MOUNT(fs);
673 if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
674 SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
676 SPIFFS_LOCK(fs);
678 spiffs_fd *fd;
679 spiffs_page_ix pix;
680 s32_t res;
682 res = spiffs_fd_find_new(fs, &fd, 0);
683 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
685 res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t *)path, &pix);
686 if (res != SPIFFS_OK) {
687 spiffs_fd_return(fs, fd->file_nbr);
689 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
691 res = spiffs_object_open_by_page(fs, pix, fd, 0, 0);
692 if (res != SPIFFS_OK) {
693 spiffs_fd_return(fs, fd->file_nbr);
695 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
697 res = spiffs_object_truncate(fd, 0, 1);
698 if (res != SPIFFS_OK) {
699 spiffs_fd_return(fs, fd->file_nbr);
701 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
703 SPIFFS_UNLOCK(fs);
704 return 0;
705 #endif // SPIFFS_READ_ONLY
708 s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) {
709 SPIFFS_API_DBG("%s "_SPIPRIfd "\n", __func__, fh);
710 #if SPIFFS_READ_ONLY
711 (void)fs;
712 (void)fh;
713 return SPIFFS_ERR_RO_NOT_IMPL;
714 #else
715 SPIFFS_API_CHECK_CFG(fs);
716 SPIFFS_API_CHECK_MOUNT(fs);
717 SPIFFS_LOCK(fs);
719 spiffs_fd *fd;
720 s32_t res;
721 fh = SPIFFS_FH_UNOFFS(fs, fh);
722 res = spiffs_fd_get(fs, fh, &fd);
723 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
725 if ((fd->flags & SPIFFS_O_WRONLY) == 0) {
726 res = SPIFFS_ERR_NOT_WRITABLE;
727 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
730 #if SPIFFS_CACHE_WR
731 spiffs_cache_fd_release(fs, fd->cache_page);
732 #endif
734 res = spiffs_object_truncate(fd, 0, 1);
736 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
738 SPIFFS_UNLOCK(fs);
740 return 0;
741 #endif // SPIFFS_READ_ONLY
744 static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spiffs_stat *s) {
745 (void)fh;
746 spiffs_page_object_ix_header objix_hdr;
747 spiffs_obj_id obj_id;
748 s32_t res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_IX | SPIFFS_OP_C_READ, fh,
749 SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
750 SPIFFS_API_CHECK_RES(fs, res);
752 u32_t obj_id_addr = SPIFFS_BLOCK_TO_PADDR(fs, SPIFFS_BLOCK_FOR_PAGE(fs, pix)) +
753 SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, pix) * sizeof(spiffs_obj_id);
754 res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, fh,
755 obj_id_addr, sizeof(spiffs_obj_id), (u8_t *)&obj_id);
756 SPIFFS_API_CHECK_RES(fs, res);
758 s->obj_id = obj_id & ~SPIFFS_OBJ_ID_IX_FLAG;
759 s->type = objix_hdr.type;
760 s->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
761 s->pix = pix;
762 strncpy((char *)s->name, (char *)objix_hdr.name, SPIFFS_OBJ_NAME_LEN - 1);
763 #if SPIFFS_OBJ_META_LEN
764 _SPIFFS_MEMCPY(s->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);
765 #endif
767 return res;
770 s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s) {
771 SPIFFS_API_DBG("%s '%s'\n", __func__, path);
772 SPIFFS_API_CHECK_CFG(fs);
773 SPIFFS_API_CHECK_MOUNT(fs);
774 if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) {
775 SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
777 SPIFFS_LOCK(fs);
779 s32_t res;
780 spiffs_page_ix pix;
782 res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t *)path, &pix);
783 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
785 res = spiffs_stat_pix(fs, pix, 0, s);
787 SPIFFS_UNLOCK(fs);
789 return res;
792 s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s) {
793 SPIFFS_API_DBG("%s "_SPIPRIfd "\n", __func__, fh);
794 SPIFFS_API_CHECK_CFG(fs);
795 SPIFFS_API_CHECK_MOUNT(fs);
796 SPIFFS_LOCK(fs);
798 spiffs_fd *fd;
799 s32_t res;
801 fh = SPIFFS_FH_UNOFFS(fs, fh);
802 res = spiffs_fd_get(fs, fh, &fd);
803 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
805 #if SPIFFS_CACHE_WR
806 spiffs_fflush_cache(fs, fh);
807 #endif
809 res = spiffs_stat_pix(fs, fd->objix_hdr_pix, fh, s);
811 SPIFFS_UNLOCK(fs);
813 return res;
816 // Checks if there are any cached writes for the object id associated with
817 // given filehandle. If so, these writes are flushed.
818 #if SPIFFS_CACHE == 1
819 static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh) {
820 (void)fs;
821 (void)fh;
822 s32_t res = SPIFFS_OK;
823 #if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR
825 spiffs_fd *fd;
826 res = spiffs_fd_get(fs, fh, &fd);
827 SPIFFS_API_CHECK_RES(fs, res);
829 if ((fd->flags & SPIFFS_O_DIRECT) == 0) {
830 if (fd->cache_page == 0) {
831 // see if object id is associated with cache already
832 fd->cache_page = spiffs_cache_page_get_by_fd(fs, fd);
834 if (fd->cache_page) {
835 SPIFFS_CACHE_DBG("CACHE_WR_DUMP: dumping cache page "_SPIPRIi" for fd "_SPIPRIfd":"_SPIPRIid", flush, offs:"_SPIPRIi" size:"_SPIPRIi"\n",
836 fd->cache_page->ix, fd->file_nbr, fd->obj_id, fd->cache_page->ucache.swrc.offset, fd->cache_page->ucache.swrc.size);
837 res = spiffs_hydro_write(fs, fd,
838 spiffs_get_cache_page(fs, spiffs_get_cache(fs), fd->cache_page->ix),
839 fd->cache_page->ucache.swrc.offset, fd->cache_page->ucache.swrc.size);
840 if (res < SPIFFS_OK) {
841 fs->err_code = res;
843 spiffs_cache_fd_release(fs, fd->cache_page);
846 #endif
848 return res;
850 #endif
852 s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) {
853 SPIFFS_API_DBG("%s "_SPIPRIfd "\n", __func__, fh);
854 (void)fh;
855 SPIFFS_API_CHECK_CFG(fs);
856 SPIFFS_API_CHECK_MOUNT(fs);
857 s32_t res = SPIFFS_OK;
858 #if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR
859 SPIFFS_LOCK(fs);
860 fh = SPIFFS_FH_UNOFFS(fs, fh);
861 res = spiffs_fflush_cache(fs, fh);
862 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
863 SPIFFS_UNLOCK(fs);
864 #endif
866 return res;
869 s32_t SPIFFS_close(spiffs *fs, spiffs_file fh) {
870 SPIFFS_API_DBG("%s "_SPIPRIfd "\n", __func__, fh);
871 SPIFFS_API_CHECK_CFG(fs);
872 SPIFFS_API_CHECK_MOUNT(fs);
874 s32_t res = SPIFFS_OK;
875 SPIFFS_LOCK(fs);
877 fh = SPIFFS_FH_UNOFFS(fs, fh);
878 #if SPIFFS_CACHE
879 res = spiffs_fflush_cache(fs, fh);
880 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
881 #endif
882 res = spiffs_fd_return(fs, fh);
883 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
885 SPIFFS_UNLOCK(fs);
887 return res;
890 s32_t SPIFFS_rename(spiffs *fs, const char *old_path, const char *new_path) {
891 SPIFFS_API_DBG("%s %s %s\n", __func__, old_path, new_path);
892 #if SPIFFS_READ_ONLY
893 (void)fs;
894 (void)old_path;
895 (void)new_path;
896 return SPIFFS_ERR_RO_NOT_IMPL;
897 #else
898 SPIFFS_API_CHECK_CFG(fs);
899 SPIFFS_API_CHECK_MOUNT(fs);
900 if (strlen(new_path) > SPIFFS_OBJ_NAME_LEN - 1 ||
901 strlen(old_path) > SPIFFS_OBJ_NAME_LEN - 1) {
902 SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG);
904 SPIFFS_LOCK(fs);
906 spiffs_page_ix pix_old, pix_dummy;
907 spiffs_fd *fd;
909 s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t *)old_path, &pix_old);
910 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
912 res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t *)new_path, &pix_dummy);
913 if (res == SPIFFS_ERR_NOT_FOUND) {
914 res = SPIFFS_OK;
915 } else if (res == SPIFFS_OK) {
916 res = SPIFFS_ERR_CONFLICTING_NAME;
918 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
920 res = spiffs_fd_find_new(fs, &fd, 0);
921 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
923 res = spiffs_object_open_by_page(fs, pix_old, fd, 0, 0);
924 if (res != SPIFFS_OK) {
925 spiffs_fd_return(fs, fd->file_nbr);
927 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
929 res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (const u8_t *)new_path,
930 0, 0, &pix_dummy);
931 #if SPIFFS_TEMPORAL_FD_CACHE
932 if (res == SPIFFS_OK) {
933 spiffs_fd_temporal_cache_rehash(fs, old_path, new_path);
935 #endif
937 spiffs_fd_return(fs, fd->file_nbr);
939 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
941 SPIFFS_UNLOCK(fs);
943 return res;
944 #endif // SPIFFS_READ_ONLY
947 #if SPIFFS_OBJ_META_LEN
948 s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta) {
949 #if SPIFFS_READ_ONLY
950 (void)fs;
951 (void)name;
952 (void)meta;
953 return SPIFFS_ERR_RO_NOT_IMPL;
954 #else
955 SPIFFS_API_CHECK_CFG(fs);
956 SPIFFS_API_CHECK_MOUNT(fs);
957 SPIFFS_LOCK(fs);
959 spiffs_page_ix pix, pix_dummy;
960 spiffs_fd *fd;
962 s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t *)name, &pix);
963 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
965 res = spiffs_fd_find_new(fs, &fd, 0);
966 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
968 res = spiffs_object_open_by_page(fs, pix, fd, 0, 0);
969 if (res != SPIFFS_OK) {
970 spiffs_fd_return(fs, fd->file_nbr);
972 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
974 res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, meta,
975 0, &pix_dummy);
977 spiffs_fd_return(fs, fd->file_nbr);
979 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
981 SPIFFS_UNLOCK(fs);
983 return res;
984 #endif // SPIFFS_READ_ONLY
987 s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta) {
988 #if SPIFFS_READ_ONLY
989 (void)fs;
990 (void)fh;
991 (void)meta;
992 return SPIFFS_ERR_RO_NOT_IMPL;
993 #else
994 SPIFFS_API_CHECK_CFG(fs);
995 SPIFFS_API_CHECK_MOUNT(fs);
996 SPIFFS_LOCK(fs);
998 s32_t res;
999 spiffs_fd *fd;
1000 spiffs_page_ix pix_dummy;
1002 fh = SPIFFS_FH_UNOFFS(fs, fh);
1003 res = spiffs_fd_get(fs, fh, &fd);
1004 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1006 if ((fd->flags & SPIFFS_O_WRONLY) == 0) {
1007 res = SPIFFS_ERR_NOT_WRITABLE;
1008 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1011 res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, meta,
1012 0, &pix_dummy);
1014 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1016 SPIFFS_UNLOCK(fs);
1018 return res;
1019 #endif // SPIFFS_READ_ONLY
1021 #endif // SPIFFS_OBJ_META_LEN
1023 spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d) {
1024 SPIFFS_API_DBG("%s\n", __func__);
1025 (void)name;
1027 if (!SPIFFS_CHECK_CFG((fs))) {
1028 (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED;
1029 return 0;
1032 if (!SPIFFS_CHECK_MOUNT(fs)) {
1033 fs->err_code = SPIFFS_ERR_NOT_MOUNTED;
1034 return 0;
1037 d->fs = fs;
1038 d->block = 0;
1039 d->entry = 0;
1040 return d;
1043 static s32_t spiffs_read_dir_v(
1044 spiffs *fs,
1045 spiffs_obj_id obj_id,
1046 spiffs_block_ix bix,
1047 int ix_entry,
1048 const void *user_const_p,
1049 void *user_var_p) {
1050 (void)user_const_p;
1051 s32_t res;
1052 spiffs_page_object_ix_header objix_hdr;
1053 if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED ||
1054 (obj_id & SPIFFS_OBJ_ID_IX_FLAG) == 0) {
1055 return SPIFFS_VIS_COUNTINUE;
1058 spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry);
1059 res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ,
1060 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix_header), (u8_t *)&objix_hdr);
1061 if (res != SPIFFS_OK) return res;
1062 if ((obj_id & SPIFFS_OBJ_ID_IX_FLAG) &&
1063 objix_hdr.p_hdr.span_ix == 0 &&
1064 (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) ==
1065 (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) {
1066 struct spiffs_dirent *e = (struct spiffs_dirent *)user_var_p;
1067 e->obj_id = obj_id;
1068 strcpy((char *)e->name, (char *)objix_hdr.name);
1069 e->type = objix_hdr.type;
1070 e->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size;
1071 e->pix = pix;
1072 #if SPIFFS_OBJ_META_LEN
1073 _SPIFFS_MEMCPY(e->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN);
1074 #endif
1075 return SPIFFS_OK;
1077 return SPIFFS_VIS_COUNTINUE;
1080 struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) {
1081 SPIFFS_API_DBG("%s\n", __func__);
1082 if (!SPIFFS_CHECK_MOUNT(d->fs)) {
1083 d->fs->err_code = SPIFFS_ERR_NOT_MOUNTED;
1084 return 0;
1086 SPIFFS_LOCK(d->fs);
1088 spiffs_block_ix bix;
1089 int entry;
1090 s32_t res;
1091 struct spiffs_dirent *ret = 0;
1093 res = spiffs_obj_lu_find_entry_visitor(d->fs,
1094 d->block,
1095 d->entry,
1096 SPIFFS_VIS_NO_WRAP,
1098 spiffs_read_dir_v,
1101 &bix,
1102 &entry);
1103 if (res == SPIFFS_OK) {
1104 d->block = bix;
1105 d->entry = entry + 1;
1106 e->obj_id &= ~SPIFFS_OBJ_ID_IX_FLAG;
1107 ret = e;
1108 } else {
1109 d->fs->err_code = res;
1111 SPIFFS_UNLOCK(d->fs);
1112 return ret;
1115 s32_t SPIFFS_closedir(spiffs_DIR *d) {
1116 SPIFFS_API_DBG("%s\n", __func__);
1117 SPIFFS_API_CHECK_CFG(d->fs);
1118 SPIFFS_API_CHECK_MOUNT(d->fs);
1119 return 0;
1122 s32_t SPIFFS_check(spiffs *fs) {
1123 SPIFFS_API_DBG("%s\n", __func__);
1124 #if SPIFFS_READ_ONLY
1125 (void)fs;
1126 return SPIFFS_ERR_RO_NOT_IMPL;
1127 #else
1128 s32_t res;
1129 SPIFFS_API_CHECK_CFG(fs);
1130 SPIFFS_API_CHECK_MOUNT(fs);
1131 SPIFFS_LOCK(fs);
1133 res = spiffs_lookup_consistency_check(fs, 0);
1134 (void)res;
1135 res = spiffs_object_index_consistency_check(fs);
1136 (void)res;
1137 res = spiffs_page_consistency_check(fs);
1138 (void)res;
1139 res = spiffs_obj_lu_scan(fs);
1141 SPIFFS_UNLOCK(fs);
1142 return res;
1143 #endif // SPIFFS_READ_ONLY
1146 s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used) {
1147 SPIFFS_API_DBG("%s\n", __func__);
1148 s32_t res = SPIFFS_OK;
1149 SPIFFS_API_CHECK_CFG(fs);
1150 SPIFFS_API_CHECK_MOUNT(fs);
1151 SPIFFS_LOCK(fs);
1153 u32_t pages_per_block = SPIFFS_PAGES_PER_BLOCK(fs);
1154 u32_t blocks = fs->block_count;
1155 u32_t obj_lu_pages = SPIFFS_OBJ_LOOKUP_PAGES(fs);
1156 u32_t data_page_size = SPIFFS_DATA_PAGE_SIZE(fs);
1157 u32_t total_data_pages = (blocks - 2) * (pages_per_block - obj_lu_pages) + 1; // -2 for spare blocks, +1 for emergency page
1159 if (total) {
1160 *total = total_data_pages * data_page_size;
1163 if (used) {
1164 *used = fs->stats_p_allocated * data_page_size;
1167 SPIFFS_UNLOCK(fs);
1168 return res;
1171 s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages) {
1172 SPIFFS_API_DBG("%s "_SPIPRIi "\n", __func__, max_free_pages);
1173 #if SPIFFS_READ_ONLY
1174 (void)fs;
1175 (void)max_free_pages;
1176 return SPIFFS_ERR_RO_NOT_IMPL;
1177 #else
1178 s32_t res;
1179 SPIFFS_API_CHECK_CFG(fs);
1180 SPIFFS_API_CHECK_MOUNT(fs);
1181 SPIFFS_LOCK(fs);
1183 res = spiffs_gc_quick(fs, max_free_pages);
1185 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1186 SPIFFS_UNLOCK(fs);
1187 return 0;
1188 #endif // SPIFFS_READ_ONLY
1192 s32_t SPIFFS_gc(spiffs *fs, u32_t size) {
1193 SPIFFS_API_DBG("%s "_SPIPRIi "\n", __func__, size);
1194 #if SPIFFS_READ_ONLY
1195 (void)fs;
1196 (void)size;
1197 return SPIFFS_ERR_RO_NOT_IMPL;
1198 #else
1199 s32_t res;
1200 SPIFFS_API_CHECK_CFG(fs);
1201 SPIFFS_API_CHECK_MOUNT(fs);
1202 SPIFFS_LOCK(fs);
1204 res = spiffs_gc_check(fs, size);
1206 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1207 SPIFFS_UNLOCK(fs);
1208 return 0;
1209 #endif // SPIFFS_READ_ONLY
1212 s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) {
1213 SPIFFS_API_DBG("%s "_SPIPRIfd "\n", __func__, fh);
1214 s32_t res;
1215 SPIFFS_API_CHECK_CFG(fs);
1216 SPIFFS_API_CHECK_MOUNT(fs);
1217 SPIFFS_LOCK(fs);
1219 fh = SPIFFS_FH_UNOFFS(fs, fh);
1221 spiffs_fd *fd;
1222 res = spiffs_fd_get(fs, fh, &fd);
1223 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1225 #if SPIFFS_CACHE_WR
1226 res = spiffs_fflush_cache(fs, fh);
1227 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1228 #endif
1230 res = (fd->fdoffset >= (fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size));
1232 SPIFFS_UNLOCK(fs);
1233 return res;
1236 s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) {
1237 SPIFFS_API_DBG("%s "_SPIPRIfd "\n", __func__, fh);
1238 s32_t res;
1239 SPIFFS_API_CHECK_CFG(fs);
1240 SPIFFS_API_CHECK_MOUNT(fs);
1241 SPIFFS_LOCK(fs);
1243 fh = SPIFFS_FH_UNOFFS(fs, fh);
1245 spiffs_fd *fd;
1246 res = spiffs_fd_get(fs, fh, &fd);
1247 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1249 #if SPIFFS_CACHE_WR
1250 res = spiffs_fflush_cache(fs, fh);
1251 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1252 #endif
1254 res = fd->fdoffset;
1256 SPIFFS_UNLOCK(fs);
1257 return res;
1260 s32_t SPIFFS_set_file_callback_func(spiffs *fs, spiffs_file_callback cb_func) {
1261 SPIFFS_API_DBG("%s\n", __func__);
1262 SPIFFS_LOCK(fs);
1263 fs->file_cb_f = cb_func;
1264 SPIFFS_UNLOCK(fs);
1265 return 0;
1268 #if SPIFFS_IX_MAP
1270 s32_t SPIFFS_ix_map(spiffs *fs, spiffs_file fh, spiffs_ix_map *map,
1271 u32_t offset, u32_t len, spiffs_page_ix *map_buf) {
1272 SPIFFS_API_DBG("%s "_SPIPRIfd " "_SPIPRIi " "_SPIPRIi "\n", __func__, fh, offset, len);
1273 s32_t res;
1274 SPIFFS_API_CHECK_CFG(fs);
1275 SPIFFS_API_CHECK_MOUNT(fs);
1276 SPIFFS_LOCK(fs);
1278 fh = SPIFFS_FH_UNOFFS(fs, fh);
1280 spiffs_fd *fd;
1281 res = spiffs_fd_get(fs, fh, &fd);
1282 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1284 if (fd->ix_map) {
1285 SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_IX_MAP_MAPPED);
1288 map->map_buf = map_buf;
1289 map->offset = offset;
1290 // nb: spix range includes last
1291 map->start_spix = offset / SPIFFS_DATA_PAGE_SIZE(fs);
1292 map->end_spix = (offset + len) / SPIFFS_DATA_PAGE_SIZE(fs);
1293 memset(map_buf, 0, sizeof(spiffs_page_ix) * (map->end_spix - map->start_spix + 1));
1294 fd->ix_map = map;
1296 // scan for pixes
1297 res = spiffs_populate_ix_map(fs, fd, 0, map->end_spix - map->start_spix + 1);
1298 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1300 SPIFFS_UNLOCK(fs);
1301 return res;
1304 s32_t SPIFFS_ix_unmap(spiffs *fs, spiffs_file fh) {
1305 SPIFFS_API_DBG("%s "_SPIPRIfd "\n", __func__, fh);
1306 s32_t res;
1307 SPIFFS_API_CHECK_CFG(fs);
1308 SPIFFS_API_CHECK_MOUNT(fs);
1309 SPIFFS_LOCK(fs);
1311 fh = SPIFFS_FH_UNOFFS(fs, fh);
1313 spiffs_fd *fd;
1314 res = spiffs_fd_get(fs, fh, &fd);
1315 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1317 if (fd->ix_map == 0) {
1318 SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_IX_MAP_UNMAPPED);
1321 fd->ix_map = 0;
1323 SPIFFS_UNLOCK(fs);
1324 return res;
1327 s32_t SPIFFS_ix_remap(spiffs *fs, spiffs_file fh, u32_t offset) {
1328 SPIFFS_API_DBG("%s "_SPIPRIfd " "_SPIPRIi "\n", __func__, fh, offset);
1329 s32_t res = SPIFFS_OK;
1330 SPIFFS_API_CHECK_CFG(fs);
1331 SPIFFS_API_CHECK_MOUNT(fs);
1332 SPIFFS_LOCK(fs);
1334 fh = SPIFFS_FH_UNOFFS(fs, fh);
1336 spiffs_fd *fd;
1337 res = spiffs_fd_get(fs, fh, &fd);
1338 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1340 if (fd->ix_map == 0) {
1341 SPIFFS_API_CHECK_RES_UNLOCK(fs, SPIFFS_ERR_IX_MAP_UNMAPPED);
1344 spiffs_ix_map *map = fd->ix_map;
1346 s32_t spix_diff = offset / SPIFFS_DATA_PAGE_SIZE(fs) - map->start_spix;
1347 map->offset = offset;
1349 // move existing pixes if within map offs
1350 if (spix_diff != 0) {
1351 // move vector
1352 int i;
1353 const s32_t vec_len = map->end_spix - map->start_spix + 1; // spix range includes last
1354 map->start_spix += spix_diff;
1355 map->end_spix += spix_diff;
1356 if (spix_diff >= vec_len) {
1357 // moving beyond range
1358 memset(&map->map_buf, 0, vec_len * sizeof(spiffs_page_ix));
1359 // populate_ix_map is inclusive
1360 res = spiffs_populate_ix_map(fs, fd, 0, vec_len - 1);
1361 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1362 } else if (spix_diff > 0) {
1363 // diff positive
1364 for (i = 0; i < vec_len - spix_diff; i++) {
1365 map->map_buf[i] = map->map_buf[i + spix_diff];
1367 // memset is non-inclusive
1368 memset(&map->map_buf[vec_len - spix_diff], 0, spix_diff * sizeof(spiffs_page_ix));
1369 // populate_ix_map is inclusive
1370 res = spiffs_populate_ix_map(fs, fd, vec_len - spix_diff, vec_len - 1);
1371 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1372 } else {
1373 // diff negative
1374 for (i = vec_len - 1; i >= -spix_diff; i--) {
1375 map->map_buf[i] = map->map_buf[i + spix_diff];
1377 // memset is non-inclusive
1378 memset(&map->map_buf[0], 0, -spix_diff * sizeof(spiffs_page_ix));
1379 // populate_ix_map is inclusive
1380 res = spiffs_populate_ix_map(fs, fd, 0, -spix_diff - 1);
1381 SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
1386 SPIFFS_UNLOCK(fs);
1387 return res;
1390 s32_t SPIFFS_bytes_to_ix_map_entries(spiffs *fs, u32_t bytes) {
1391 SPIFFS_API_CHECK_CFG(fs);
1392 // always add one extra page, the offset might change to the middle of a page
1393 return (bytes + SPIFFS_DATA_PAGE_SIZE(fs)) / SPIFFS_DATA_PAGE_SIZE(fs);
1396 s32_t SPIFFS_ix_map_entries_to_bytes(spiffs *fs, u32_t map_page_ix_entries) {
1397 SPIFFS_API_CHECK_CFG(fs);
1398 return map_page_ix_entries * SPIFFS_DATA_PAGE_SIZE(fs);
1401 #endif // SPIFFS_IX_MAP
1403 #if SPIFFS_TEST_VISUALISATION
1404 s32_t SPIFFS_vis(spiffs *fs) {
1405 s32_t res = SPIFFS_OK;
1406 SPIFFS_API_CHECK_CFG(fs);
1407 SPIFFS_API_CHECK_MOUNT(fs);
1408 SPIFFS_LOCK(fs);
1410 int entries_per_page = (SPIFFS_CFG_LOG_PAGE_SZ(fs) / sizeof(spiffs_obj_id));
1411 spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work;
1412 spiffs_block_ix bix = 0;
1414 while (bix < fs->block_count) {
1415 // check each object lookup page
1416 int obj_lookup_page = 0;
1417 int cur_entry = 0;
1419 while (res == SPIFFS_OK && obj_lookup_page < (int)SPIFFS_OBJ_LOOKUP_PAGES(fs)) {
1420 int entry_offset = obj_lookup_page * entries_per_page;
1421 res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ,
1422 0, bix * SPIFFS_CFG_LOG_BLOCK_SZ(fs) + SPIFFS_PAGE_TO_PADDR(fs, obj_lookup_page), SPIFFS_CFG_LOG_PAGE_SZ(fs), fs->lu_work);
1423 // check each entry
1424 while (res == SPIFFS_OK &&
1425 cur_entry - entry_offset < entries_per_page && cur_entry < (int)(SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs))) {
1426 spiffs_obj_id obj_id = obj_lu_buf[cur_entry - entry_offset];
1427 if (cur_entry == 0) {
1428 spiffs_printf(_SPIPRIbl" ", bix);
1429 } else if ((cur_entry & 0x3f) == 0) {
1430 spiffs_printf(" ");
1432 if (obj_id == SPIFFS_OBJ_ID_FREE) {
1433 spiffs_printf(SPIFFS_TEST_VIS_FREE_STR);
1434 } else if (obj_id == SPIFFS_OBJ_ID_DELETED) {
1435 spiffs_printf(SPIFFS_TEST_VIS_DELE_STR);
1436 } else if (obj_id & SPIFFS_OBJ_ID_IX_FLAG) {
1437 spiffs_printf(SPIFFS_TEST_VIS_INDX_STR(obj_id));
1438 } else {
1439 spiffs_printf(SPIFFS_TEST_VIS_DATA_STR(obj_id));
1441 cur_entry++;
1442 if ((cur_entry & 0x3f) == 0) {
1443 spiffs_printf("\n");
1445 } // per entry
1446 obj_lookup_page++;
1447 } // per object lookup page
1449 spiffs_obj_id erase_count;
1450 res = _spiffs_rd(fs, SPIFFS_OP_C_READ | SPIFFS_OP_T_OBJ_LU2, 0,
1451 SPIFFS_ERASE_COUNT_PADDR(fs, bix),
1452 sizeof(spiffs_obj_id), (u8_t *)&erase_count);
1453 SPIFFS_CHECK_RES(res);
1455 if (erase_count != (spiffs_obj_id) - 1) {
1456 spiffs_printf("\tera_cnt: "_SPIPRIi"\n", erase_count);
1457 } else {
1458 spiffs_printf("\tera_cnt: n/a\n");
1461 bix++;
1462 } // per block
1464 spiffs_printf("era_cnt_max: "_SPIPRIi"\n", fs->max_erase_count);
1465 spiffs_printf("last_errno: "_SPIPRIi"\n", fs->err_code);
1466 spiffs_printf("blocks: "_SPIPRIi"\n", fs->block_count);
1467 spiffs_printf("free_blocks: "_SPIPRIi"\n", fs->free_blocks);
1468 spiffs_printf("page_alloc: "_SPIPRIi"\n", fs->stats_p_allocated);
1469 spiffs_printf("page_delet: "_SPIPRIi"\n", fs->stats_p_deleted);
1470 SPIFFS_UNLOCK(fs);
1471 u32_t total, used;
1472 SPIFFS_info(fs, &total, &used);
1473 spiffs_printf("used: "_SPIPRIi" of "_SPIPRIi"\n", used, total);
1474 return res;
1476 #endif