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.
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 //-----------------------------------------------------------------------------
20 #include "spiffs_nucleus.h"
24 static s32_t
spiffs_fflush_cache(spiffs
*fs
, spiffs_file fh
);
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
);
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
));
38 u8_t
SPIFFS_mounted(spiffs
*fs
) {
39 return SPIFFS_CHECK_MOUNT(fs
);
42 s32_t
SPIFFS_format(spiffs
*fs
) {
45 return SPIFFS_ERR_RO_NOT_IMPL
;
47 SPIFFS_API_CHECK_CFG(fs
);
48 if (SPIFFS_CHECK_MOUNT(fs
)) {
49 fs
->err_code
= SPIFFS_ERR_MOUNTED
;
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
);
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
);
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
) {
87 " sz:"_SPIPRIi
" logpgsz:"_SPIPRIi
" logblksz:"_SPIPRIi
" perasz:"_SPIPRIi
89 " fdsz:"_SPIPRIi
" cachesz:"_SPIPRIi
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
);
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
);
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);
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);
121 u8_t
*cache_8
= (u8_t
*)cache
;
122 cache_8
+= (ptr_size
- addr_lsb
);
124 cache_size
-= (ptr_size
- addr_lsb
);
126 if (cache_size
& (ptr_size
- 1)) {
127 cache_size
-= (cache_size
& (ptr_size
- 1));
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
);
139 res
= SPIFFS_CHECK_MAGIC_POSSIBLE(fs
) ? SPIFFS_OK
: SPIFFS_ERR_MAGIC_NOT_POSSIBLE
;
140 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
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
;
166 void SPIFFS_unmount(spiffs
*fs
) {
167 SPIFFS_API_DBG("%s\n", __func__
);
168 if (!SPIFFS_CHECK_CFG(fs
) || !SPIFFS_CHECK_MOUNT(fs
)) return;
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) {
176 (void)spiffs_fflush_cache(fs
, cur_fd
->file_nbr
);
178 spiffs_fd_return(fs
, cur_fd
->file_nbr
);
186 s32_t
SPIFFS_errno(spiffs
*fs
) {
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
);
201 return SPIFFS_ERR_RO_NOT_IMPL
;
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
);
210 spiffs_obj_id obj_id
;
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
);
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
);
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
);
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
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
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
);
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
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
);
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
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
);
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
);
407 spiffs_fflush_cache(fs
, fh
);
410 if (fd
->fdoffset
+ len
>= fd
->size
) {
411 // reading beyond file size
412 s32_t avail
= fd
->size
- fd
->fdoffset
;
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
;
422 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
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
);
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
) {
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
) {
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
);
457 u8_t
*buf_8
= (u8_t
*)buf
;
463 res
= spiffs_object_append(fd
, offset
, (u8_t
*)buf
, remaining
);
464 SPIFFS_CHECK_RES(res
);
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
);
478 return SPIFFS_ERR_RO_NOT_IMPL
;
480 SPIFFS_API_CHECK_CFG(fs
);
481 SPIFFS_API_CHECK_MOUNT(fs
);
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
;
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
);
508 if (fd
->flags
& SPIFFS_O_APPEND
) {
509 if (fd
->size
== SPIFFS_UNDEFINED_LEN
) {
515 if (fd
->cache_page
) {
516 offset
= MAX(offset
, fd
->cache_page
->ucache
.swrc
.offset
+ fd
->cache_page
->ucache
.swrc
.size
);
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
);
540 // writing within cache
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
);
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
);
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
);
579 res
= spiffs_hydro_write(fs
, fd
, buf
, offset
, len
);
580 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
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
602 res
= spiffs_hydro_write(fs
, fd
, buf
, offset
, len
);
603 SPIFFS_API_CHECK_RES_UNLOCK(fs
, 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
);
620 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
621 res
= spiffs_fd_get(fs
, fh
, &fd
);
622 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
625 spiffs_fflush_cache(fs
, fh
);
628 s32_t file_size
= fd
->size
== SPIFFS_UNDEFINED_LEN
? 0 : fd
->size
;
631 case SPIFFS_SEEK_CUR
:
632 offs
= fd
->fdoffset
+ offs
;
634 case SPIFFS_SEEK_END
:
635 offs
= file_size
+ offs
;
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
) {
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
;
664 s32_t
SPIFFS_remove(spiffs
*fs
, const char *path
) {
665 SPIFFS_API_DBG("%s '%s'\n", __func__
, path
);
669 return SPIFFS_ERR_RO_NOT_IMPL
;
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
);
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
);
705 #endif // SPIFFS_READ_ONLY
708 s32_t
SPIFFS_fremove(spiffs
*fs
, spiffs_file fh
) {
709 SPIFFS_API_DBG("%s "_SPIPRIfd
"\n", __func__
, fh
);
713 return SPIFFS_ERR_RO_NOT_IMPL
;
715 SPIFFS_API_CHECK_CFG(fs
);
716 SPIFFS_API_CHECK_MOUNT(fs
);
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
);
731 spiffs_cache_fd_release(fs
, fd
->cache_page
);
734 res
= spiffs_object_truncate(fd
, 0, 1);
736 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
741 #endif // SPIFFS_READ_ONLY
744 static s32_t
spiffs_stat_pix(spiffs
*fs
, spiffs_page_ix pix
, spiffs_file fh
, spiffs_stat
*s
) {
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
;
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
);
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
);
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
);
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
);
801 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
802 res
= spiffs_fd_get(fs
, fh
, &fd
);
803 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
806 spiffs_fflush_cache(fs
, fh
);
809 res
= spiffs_stat_pix(fs
, fd
->objix_hdr_pix
, fh
, s
);
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
) {
822 s32_t res
= SPIFFS_OK
;
823 #if !SPIFFS_READ_ONLY && SPIFFS_CACHE_WR
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
) {
843 spiffs_cache_fd_release(fs
, fd
->cache_page
);
852 s32_t
SPIFFS_fflush(spiffs
*fs
, spiffs_file fh
) {
853 SPIFFS_API_DBG("%s "_SPIPRIfd
"\n", __func__
, 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
860 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
861 res
= spiffs_fflush_cache(fs
, fh
);
862 SPIFFS_API_CHECK_RES_UNLOCK(fs
, 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
;
877 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
879 res
= spiffs_fflush_cache(fs
, fh
);
880 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
882 res
= spiffs_fd_return(fs
, fh
);
883 SPIFFS_API_CHECK_RES_UNLOCK(fs
, 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
);
896 return SPIFFS_ERR_RO_NOT_IMPL
;
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
);
906 spiffs_page_ix pix_old
, pix_dummy
;
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
) {
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
,
931 #if SPIFFS_TEMPORAL_FD_CACHE
932 if (res
== SPIFFS_OK
) {
933 spiffs_fd_temporal_cache_rehash(fs
, old_path
, new_path
);
937 spiffs_fd_return(fs
, fd
->file_nbr
);
939 SPIFFS_API_CHECK_RES_UNLOCK(fs
, 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
) {
953 return SPIFFS_ERR_RO_NOT_IMPL
;
955 SPIFFS_API_CHECK_CFG(fs
);
956 SPIFFS_API_CHECK_MOUNT(fs
);
959 spiffs_page_ix pix
, pix_dummy
;
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
,
977 spiffs_fd_return(fs
, fd
->file_nbr
);
979 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
984 #endif // SPIFFS_READ_ONLY
987 s32_t
SPIFFS_fupdate_meta(spiffs
*fs
, spiffs_file fh
, const void *meta
) {
992 return SPIFFS_ERR_RO_NOT_IMPL
;
994 SPIFFS_API_CHECK_CFG(fs
);
995 SPIFFS_API_CHECK_MOUNT(fs
);
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
,
1014 SPIFFS_API_CHECK_RES_UNLOCK(fs
, 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__
);
1027 if (!SPIFFS_CHECK_CFG((fs
))) {
1028 (fs
)->err_code
= SPIFFS_ERR_NOT_CONFIGURED
;
1032 if (!SPIFFS_CHECK_MOUNT(fs
)) {
1033 fs
->err_code
= SPIFFS_ERR_NOT_MOUNTED
;
1043 static s32_t
spiffs_read_dir_v(
1045 spiffs_obj_id obj_id
,
1046 spiffs_block_ix bix
,
1048 const void *user_const_p
,
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
;
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
;
1072 #if SPIFFS_OBJ_META_LEN
1073 _SPIFFS_MEMCPY(e
->meta
, objix_hdr
.meta
, SPIFFS_OBJ_META_LEN
);
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
;
1088 spiffs_block_ix bix
;
1091 struct spiffs_dirent
*ret
= 0;
1093 res
= spiffs_obj_lu_find_entry_visitor(d
->fs
,
1103 if (res
== SPIFFS_OK
) {
1105 d
->entry
= entry
+ 1;
1106 e
->obj_id
&= ~SPIFFS_OBJ_ID_IX_FLAG
;
1109 d
->fs
->err_code
= res
;
1111 SPIFFS_UNLOCK(d
->fs
);
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
);
1122 s32_t
SPIFFS_check(spiffs
*fs
) {
1123 SPIFFS_API_DBG("%s\n", __func__
);
1124 #if SPIFFS_READ_ONLY
1126 return SPIFFS_ERR_RO_NOT_IMPL
;
1129 SPIFFS_API_CHECK_CFG(fs
);
1130 SPIFFS_API_CHECK_MOUNT(fs
);
1133 res
= spiffs_lookup_consistency_check(fs
, 0);
1135 res
= spiffs_object_index_consistency_check(fs
);
1137 res
= spiffs_page_consistency_check(fs
);
1139 res
= spiffs_obj_lu_scan(fs
);
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
);
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
1160 *total
= total_data_pages
* data_page_size
;
1164 *used
= fs
->stats_p_allocated
* data_page_size
;
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
1175 (void)max_free_pages
;
1176 return SPIFFS_ERR_RO_NOT_IMPL
;
1179 SPIFFS_API_CHECK_CFG(fs
);
1180 SPIFFS_API_CHECK_MOUNT(fs
);
1183 res
= spiffs_gc_quick(fs
, max_free_pages
);
1185 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
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
1197 return SPIFFS_ERR_RO_NOT_IMPL
;
1200 SPIFFS_API_CHECK_CFG(fs
);
1201 SPIFFS_API_CHECK_MOUNT(fs
);
1204 res
= spiffs_gc_check(fs
, size
);
1206 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
1209 #endif // SPIFFS_READ_ONLY
1212 s32_t
SPIFFS_eof(spiffs
*fs
, spiffs_file fh
) {
1213 SPIFFS_API_DBG("%s "_SPIPRIfd
"\n", __func__
, fh
);
1215 SPIFFS_API_CHECK_CFG(fs
);
1216 SPIFFS_API_CHECK_MOUNT(fs
);
1219 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
1222 res
= spiffs_fd_get(fs
, fh
, &fd
);
1223 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
1226 res
= spiffs_fflush_cache(fs
, fh
);
1227 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
1230 res
= (fd
->fdoffset
>= (fd
->size
== SPIFFS_UNDEFINED_LEN
? 0 : fd
->size
));
1236 s32_t
SPIFFS_tell(spiffs
*fs
, spiffs_file fh
) {
1237 SPIFFS_API_DBG("%s "_SPIPRIfd
"\n", __func__
, fh
);
1239 SPIFFS_API_CHECK_CFG(fs
);
1240 SPIFFS_API_CHECK_MOUNT(fs
);
1243 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
1246 res
= spiffs_fd_get(fs
, fh
, &fd
);
1247 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
1250 res
= spiffs_fflush_cache(fs
, fh
);
1251 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
1260 s32_t
SPIFFS_set_file_callback_func(spiffs
*fs
, spiffs_file_callback cb_func
) {
1261 SPIFFS_API_DBG("%s\n", __func__
);
1263 fs
->file_cb_f
= cb_func
;
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
);
1274 SPIFFS_API_CHECK_CFG(fs
);
1275 SPIFFS_API_CHECK_MOUNT(fs
);
1278 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
1281 res
= spiffs_fd_get(fs
, fh
, &fd
);
1282 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
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));
1297 res
= spiffs_populate_ix_map(fs
, fd
, 0, map
->end_spix
- map
->start_spix
+ 1);
1298 SPIFFS_API_CHECK_RES_UNLOCK(fs
, res
);
1304 s32_t
SPIFFS_ix_unmap(spiffs
*fs
, spiffs_file fh
) {
1305 SPIFFS_API_DBG("%s "_SPIPRIfd
"\n", __func__
, fh
);
1307 SPIFFS_API_CHECK_CFG(fs
);
1308 SPIFFS_API_CHECK_MOUNT(fs
);
1311 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
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
);
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
);
1334 fh
= SPIFFS_FH_UNOFFS(fs
, fh
);
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) {
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) {
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
);
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
);
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
);
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;
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
);
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) {
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
));
1439 spiffs_printf(SPIFFS_TEST_VIS_DATA_STR(obj_id
));
1442 if ((cur_entry
& 0x3f) == 0) {
1443 spiffs_printf("\n");
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
);
1458 spiffs_printf("\tera_cnt: n/a\n");
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
);
1472 SPIFFS_info(fs
, &total
, &used
);
1473 spiffs_printf("used: "_SPIPRIi
" of "_SPIPRIi
"\n", used
, total
);