From 5333bdf8a6686d5763bb819c28e01fa474bc8411 Mon Sep 17 00:00:00 2001 From: Pascal Gollor Date: Sun, 18 Oct 2015 10:52:17 +0200 Subject: [PATCH] update SPIFFS to version 0.3.3 --- cores/esp8266/spiffs/spiffs.h | 38 ++++++--- cores/esp8266/spiffs/spiffs_check.c | 16 ++-- cores/esp8266/spiffs/spiffs_config.h | 33 ++++++-- cores/esp8266/spiffs/spiffs_gc.c | 4 +- cores/esp8266/spiffs/spiffs_hydrogen.c | 112 ++++++++++++++++++++----- cores/esp8266/spiffs/spiffs_nucleus.c | 89 ++++++++++---------- cores/esp8266/spiffs/spiffs_nucleus.h | 14 ++-- 7 files changed, 205 insertions(+), 101 deletions(-) diff --git a/cores/esp8266/spiffs/spiffs.h b/cores/esp8266/spiffs/spiffs.h index 7b59de9ed..0a8d0220f 100644 --- a/cores/esp8266/spiffs/spiffs.h +++ b/cores/esp8266/spiffs/spiffs.h @@ -5,8 +5,6 @@ * Author: petera */ - - #ifndef SPIFFS_H_ #define SPIFFS_H_ #if defined(__cplusplus) @@ -186,6 +184,11 @@ typedef struct { // logical size of a page, must be at least // log_block_size / 8 u32_t log_page_size; + +#endif +#if SPIFFS_FILEHDL_OFFSET + // an integer offset added to each file handle + u16_t fh_ix_offset; #endif } spiffs_config; @@ -310,7 +313,7 @@ void SPIFFS_unmount(spiffs *fs); * @param path the path of the new file * @param mode ignored, for posix compliance */ -s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode); +s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode); /** * Opens/creates a file. @@ -321,7 +324,7 @@ s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode); * SPIFFS_WR_ONLY, SPIFFS_RDWR, SPIFFS_DIRECT * @param mode ignored, for posix compliance */ -spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode mode); +spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode); /** @@ -360,13 +363,14 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len); s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len); /** - * Moves the read/write file offset + * Moves the read/write file offset. Resulting offset is returned or negative if error. + * lseek(fs, fd, 0, SPIFFS_SEEK_CUR) will thus return current offset. * @param fs the file system struct * @param fh the filehandle * @param offs how much/where to move the offset * @param whence if SPIFFS_SEEK_SET, the file offset shall be set to offset bytes * if SPIFFS_SEEK_CUR, the file offset shall be set to its current location plus offset - * if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offset + * if SPIFFS_SEEK_END, the file offset shall be set to the size of the file plus offse, which should be negative */ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence); @@ -375,7 +379,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence); * @param fs the file system struct * @param path the path of the file to remove */ -s32_t SPIFFS_remove(spiffs *fs, char *path); +s32_t SPIFFS_remove(spiffs *fs, const char *path); /** * Removes a file by filehandle @@ -390,7 +394,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh); * @param path the path of the file to stat * @param s the stat struct to populate */ -s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s); +s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s); /** * Gets file status by filehandle @@ -420,7 +424,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh); * @param old path of file to rename * @param newPath new path of file */ -s32_t SPIFFS_rename(spiffs *fs, char *old, char *newPath); +s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath); /** * Returns last error of last file operation. @@ -443,7 +447,7 @@ void SPIFFS_clearerr(spiffs *fs); * @param name the name of the directory * @param d pointer the directory stream to be populated */ -spiffs_DIR *SPIFFS_opendir(spiffs *fs, char *name, spiffs_DIR *d); +spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d); /** * Closes a directory stream @@ -544,6 +548,20 @@ s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages); */ s32_t SPIFFS_gc(spiffs *fs, u32_t size); +/** + * Check if EOF reached. + * @param fs the file system struct + * @param fh the filehandle of the file to check + */ +s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh); + +/** + * Get position in file. + * @param fs the file system struct + * @param fh the filehandle of the file to check + */ +s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh); + #if SPIFFS_TEST_VISUALISATION /** * Prints out a visualization of the filesystem. diff --git a/cores/esp8266/spiffs/spiffs_check.c b/cores/esp8266/spiffs/spiffs_check.c index 180ce345a..0576843e3 100644 --- a/cores/esp8266/spiffs/spiffs_check.c +++ b/cores/esp8266/spiffs/spiffs_check.c @@ -440,9 +440,9 @@ static s32_t spiffs_lookup_check_validate(spiffs *fs, spiffs_obj_id lu_obj_id, s } static s32_t spiffs_lookup_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block, int cur_entry, - u32_t user_data, void *user_p) { - (void)user_data; - (void)user_p; + const void *user_const_p, void *user_var_p) { + (void)user_const_p; + (void)user_var_p; s32_t res = SPIFFS_OK; spiffs_page_header p_hdr; spiffs_page_ix cur_pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, cur_block, cur_entry); @@ -873,11 +873,11 @@ static int spiffs_object_index_search(spiffs *fs, spiffs_obj_id obj_id) { } static s32_t spiffs_object_index_consistency_check_v(spiffs *fs, spiffs_obj_id obj_id, spiffs_block_ix cur_block, - int cur_entry, u32_t user_data, void *user_p) { - (void)user_data; + int cur_entry, const void *user_const_p, void *user_var_p) { + (void)user_const_p; s32_t res_c = SPIFFS_VIS_COUNTINUE; s32_t res = SPIFFS_OK; - u32_t *log_ix = (u32_t *)user_p; + u32_t *log_ix = (u32_t*)user_var_p; spiffs_obj_id *obj_table = (spiffs_obj_id *)fs->work; CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS, @@ -977,8 +977,8 @@ s32_t spiffs_object_index_consistency_check(spiffs *fs) { memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs)); u32_t obj_id_log_ix = 0; CHECK_CB(fs, SPIFFS_CHECK_INDEX, SPIFFS_CHECK_PROGRESS, 0, 0); - res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, 0, &obj_id_log_ix, - 0, 0); + res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_object_index_consistency_check_v, &obj_id_log_ix, + 0, 0, 0); if (res == SPIFFS_VIS_END) { res = SPIFFS_OK; } diff --git a/cores/esp8266/spiffs/spiffs_config.h b/cores/esp8266/spiffs/spiffs_config.h index 10497c819..2d83c261d 100644 --- a/cores/esp8266/spiffs/spiffs_config.h +++ b/cores/esp8266/spiffs/spiffs_config.h @@ -19,7 +19,7 @@ #define c_printf ets_printf #define c_memset memset -typedef signed short file_t; +typedef int16_t file_t; typedef int32_t s32_t; typedef uint32_t u32_t; typedef int16_t s16_t; @@ -46,20 +46,20 @@ typedef uint8_t u8_t; // compile time switches // Set generic spiffs debug output call. -#ifndef SPIFFS_DGB -#define SPIFFS_DBG(...) //c_printf(__VA_ARGS__) +#ifndef SPIFFS_DBG +#define SPIFFS_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for garbage collecting. -#ifndef SPIFFS_GC_DGB -#define SPIFFS_GC_DBG(...) //c_printf(__VA_ARGS__) +#ifndef SPIFFS_GC_DBG +#define SPIFFS_GC_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for caching. -#ifndef SPIFFS_CACHE_DGB -#define SPIFFS_CACHE_DBG(...) //c_printf(__VA_ARGS__) +#ifndef SPIFFS_CACHE_DBG +#define SPIFFS_CACHE_DBG(...) //printf(__VA_ARGS__) #endif // Set spiffs debug output call for system consistency checks. -#ifndef SPIFFS_CHECK_DGB -#define SPIFFS_CHECK_DBG(...) //c_printf(__VA_ARGS__) +#ifndef SPIFFS_CHECK_DBG +#define SPIFFS_CHECK_DBG(...) //printf(__VA_ARGS__) #endif // Enable/disable API functions to determine exact number of bytes @@ -189,6 +189,21 @@ typedef uint8_t u8_t; #define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 1 #endif +// Enable this if you want the HAL callbacks to be called with the spiffs struct +#ifndef SPIFFS_HAL_CALLBACK_EXTRA +#define SPIFFS_HAL_CALLBACK_EXTRA 0 +#endif + +// Enable this if you want to add an integer offset to all file handles +// (spiffs_file). This is useful if running multiple instances of spiffs on +// same target, in order to recognise to what spiffs instance a file handle +// belongs. +// NB: This adds config field fh_ix_offset in the configuration struct when +// mounting, which must be defined. +#ifndef SPIFFS_FILEHDL_OFFSET +#define SPIFFS_FILEHDL_OFFSET 0 +#endif + // Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function // in the api. This function will visualize all filesystem using given printf // function. diff --git a/cores/esp8266/spiffs/spiffs_gc.c b/cores/esp8266/spiffs/spiffs_gc.c index b60d7d3f2..91d7e54a1 100644 --- a/cores/esp8266/spiffs/spiffs_gc.c +++ b/cores/esp8266/spiffs/spiffs_gc.c @@ -36,7 +36,7 @@ s32_t spiffs_gc_quick( int cur_entry = 0; spiffs_obj_id *obj_lu_buf = (spiffs_obj_id *)fs->lu_work; - SPIFFS_GC_DBG("gc_quick: running\n", cur_block); + SPIFFS_GC_DBG("gc_quick: running\n"); #if SPIFFS_GC_STATS fs->stats_gc_runs++; #endif @@ -255,7 +255,7 @@ s32_t spiffs_gc_find_candidate( // align cand_scores on s32_t boundary #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpointer-to-int-cast" - cand_scores = (s32_t*)(((u32_t)cand_scores + sizeof(s32_t) - 1) & ~(sizeof(s32_t) - 1)); + cand_scores = (s32_t*)(((ptrdiff_t)cand_scores + sizeof(ptrdiff_t) - 1) & ~(sizeof(ptrdiff_t) - 1)); #pragma GCC diagnostic pop *block_candidates = cand_blocks; diff --git a/cores/esp8266/spiffs/spiffs_hydrogen.c b/cores/esp8266/spiffs/spiffs_hydrogen.c index 72b75d6ef..6c31da2de 100644 --- a/cores/esp8266/spiffs/spiffs_hydrogen.c +++ b/cores/esp8266/spiffs/spiffs_hydrogen.c @@ -8,6 +8,14 @@ #include "spiffs.h" #include "spiffs_nucleus.h" +#if SPIFFS_FILEHDL_OFFSET +#define SPIFFS_FH_OFFS(fs, fh) ((fh) != 0 ? ((fh) + (fs)->cfg.fh_ix_offset) : 0) +#define SPIFFS_FH_UNOFFS(fs, fh) ((fh) != 0 ? ((fh) - (fs)->cfg.fh_ix_offset) : 0) +#else +#define SPIFFS_FH_OFFS(fs, fh) (fh) +#define SPIFFS_FH_UNOFFS(fs, fh) (fh) +#endif + #if SPIFFS_CACHE == 1 static s32_t spiffs_fflush_cache(spiffs *fs, spiffs_file fh); #endif @@ -57,9 +65,12 @@ s32_t SPIFFS_mount(spiffs *fs, spiffs_config *config, u8_t *work, u8_t *fd_space, u32_t fd_space_size, void *cache, u32_t cache_size, spiffs_check_callback check_cb_f) { + void *user_data; SPIFFS_LOCK(fs); + user_data = fs->user_data; memset(fs, 0, sizeof(spiffs)); memcpy(&fs->cfg, config, sizeof(spiffs_config)); + fs->user_data = user_data; fs->block_count = SPIFFS_CFG_PHYS_SZ(fs) / SPIFFS_CFG_LOG_BLOCK_SZ(fs); fs->work = &work[0]; fs->lu_work = &work[SPIFFS_CFG_LOG_PAGE_SZ(fs)]; @@ -155,7 +166,7 @@ void SPIFFS_clearerr(spiffs *fs) { fs->err_code = SPIFFS_OK; } -s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode) { +s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) { (void)mode; SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); @@ -163,15 +174,15 @@ s32_t SPIFFS_creat(spiffs *fs, char *path, spiffs_mode mode) { spiffs_obj_id obj_id; s32_t res; - res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (u8_t *)path); + res = spiffs_obj_lu_find_free_obj_id(fs, &obj_id, (const u8_t*)path); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_create(fs, obj_id, (u8_t *)path, SPIFFS_TYPE_FILE, 0); + res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, 0); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); SPIFFS_UNLOCK(fs); return 0; } -spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode mode) { +spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs_mode mode) { (void)mode; SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); @@ -183,7 +194,7 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode s32_t res = spiffs_fd_find_new(fs, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix); + res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix); if ((flags & SPIFFS_CREAT) == 0) { if (res < SPIFFS_OK) { spiffs_fd_return(fs, fd->file_nbr); @@ -207,7 +218,7 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode spiffs_fd_return(fs, fd->file_nbr); } SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_create(fs, obj_id, (u8_t*)path, SPIFFS_TYPE_FILE, &pix); + res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, &pix); if (res < SPIFFS_OK) { spiffs_fd_return(fs, fd->file_nbr); } @@ -236,7 +247,7 @@ spiffs_file SPIFFS_open(spiffs *fs, char *path, spiffs_flags flags, spiffs_mode SPIFFS_UNLOCK(fs); - return fd->file_nbr; + return SPIFFS_FH_OFFS(fs, fd->file_nbr); } spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_flags flags, spiffs_mode mode) { @@ -266,7 +277,7 @@ spiffs_file SPIFFS_open_by_dirent(spiffs *fs, struct spiffs_dirent *e, spiffs_fl SPIFFS_UNLOCK(fs); - return fd->file_nbr; + return SPIFFS_FH_OFFS(fs, fd->file_nbr); } s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) { @@ -277,6 +288,7 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) { spiffs_fd *fd; s32_t res; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -285,6 +297,12 @@ s32_t SPIFFS_read(spiffs *fs, spiffs_file fh, void *buf, s32_t len) { SPIFFS_API_CHECK_RES_UNLOCK(fs, res); } + if (fd->size == SPIFFS_UNDEFINED_LEN && len > 0) { + // special case for zero sized files + res = SPIFFS_ERR_END_OF_OBJECT; + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + } + #if SPIFFS_CACHE_WR spiffs_fflush_cache(fs, fh); #endif @@ -347,6 +365,7 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) { s32_t res; u32_t offset; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -464,6 +483,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) { spiffs_fd *fd; s32_t res; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES(fs, res); @@ -502,7 +522,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) { return offs; } -s32_t SPIFFS_remove(spiffs *fs, char *path) { +s32_t SPIFFS_remove(spiffs *fs, const char *path) { SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -514,7 +534,7 @@ s32_t SPIFFS_remove(spiffs *fs, char *path) { res = spiffs_fd_find_new(fs, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_find_object_index_header_by_name(fs, (u8_t *)path, &pix); + res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix); if (res != SPIFFS_OK) { spiffs_fd_return(fs, fd->file_nbr); } @@ -543,6 +563,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) { spiffs_fd *fd; s32_t res; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -586,7 +607,7 @@ static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spi return res; } -s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s) { +s32_t SPIFFS_stat(spiffs *fs, const char *path, spiffs_stat *s) { SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -594,7 +615,7 @@ s32_t SPIFFS_stat(spiffs *fs, char *path, spiffs_stat *s) { s32_t res; spiffs_page_ix pix; - res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)path, &pix); + res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)path, &pix); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); res = spiffs_stat_pix(fs, pix, 0, s); @@ -612,6 +633,7 @@ s32_t SPIFFS_fstat(spiffs *fs, spiffs_file fh, spiffs_stat *s) { spiffs_fd *fd; s32_t res; + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fd_get(fs, fh, &fd); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -669,6 +691,7 @@ s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh) { s32_t res = SPIFFS_OK; #if SPIFFS_CACHE_WR SPIFFS_LOCK(fs); + fh = SPIFFS_FH_UNOFFS(fs, fh); res = spiffs_fflush_cache(fs, fh); SPIFFS_API_CHECK_RES_UNLOCK(fs,res); SPIFFS_UNLOCK(fs); @@ -684,6 +707,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh) { s32_t res = SPIFFS_OK; SPIFFS_LOCK(fs); + fh = SPIFFS_FH_UNOFFS(fs, fh); #if SPIFFS_CACHE res = spiffs_fflush_cache(fs, fh); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); @@ -696,7 +720,7 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh) { return res; } -s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { +s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *new) { SPIFFS_API_CHECK_CFG(fs); SPIFFS_API_CHECK_MOUNT(fs); SPIFFS_LOCK(fs); @@ -704,10 +728,10 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { spiffs_page_ix pix_old, pix_dummy; spiffs_fd *fd; - s32_t res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)old, &pix_old); + s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)old, &pix_old); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_find_object_index_header_by_name(fs, (u8_t*)new, &pix_dummy); + res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)new, &pix_dummy); if (res == SPIFFS_ERR_NOT_FOUND) { res = SPIFFS_OK; } else if (res == SPIFFS_OK) { @@ -724,7 +748,7 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { } SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (u8_t*)new, + res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (const u8_t*)new, 0, &pix_dummy); spiffs_fd_return(fs, fd->file_nbr); @@ -736,7 +760,7 @@ s32_t SPIFFS_rename(spiffs *fs, char *old, char *new) { return res; } -spiffs_DIR *SPIFFS_opendir(spiffs *fs, char *name, spiffs_DIR *d) { +spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d) { (void)name; if (!SPIFFS_CHECK_CFG((fs))) { @@ -760,9 +784,9 @@ static s32_t spiffs_read_dir_v( spiffs_obj_id obj_id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, - void *user_p) { - (void)user_data; + const void *user_const_p, + void *user_var_p) { + (void)user_const_p; s32_t res; spiffs_page_object_ix_header objix_hdr; if (obj_id == SPIFFS_OBJ_ID_FREE || obj_id == SPIFFS_OBJ_ID_DELETED || @@ -778,7 +802,7 @@ static s32_t spiffs_read_dir_v( objix_hdr.p_hdr.span_ix == 0 && (objix_hdr.p_hdr.flags& (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) == (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) { - struct spiffs_dirent *e = (struct spiffs_dirent *)user_p; + struct spiffs_dirent *e = (struct spiffs_dirent*)user_var_p; e->obj_id = obj_id; strcpy((char *)e->name, (char *)objix_hdr.name); e->type = objix_hdr.type; @@ -898,6 +922,52 @@ s32_t SPIFFS_gc(spiffs *fs, u32_t size) { return 0; } +s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) { + s32_t res; + SPIFFS_API_CHECK_CFG(fs); + SPIFFS_API_CHECK_MOUNT(fs); + SPIFFS_LOCK(fs); + + fh = SPIFFS_FH_UNOFFS(fs, fh); + + spiffs_fd *fd; + res = spiffs_fd_get(fs, fh, &fd); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + +#if SPIFFS_CACHE_WR + res = spiffs_fflush_cache(fs, fh); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); +#endif + + res = (fd->fdoffset == fd->size); + + SPIFFS_UNLOCK(fs); + return res; +} + +s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh) { + s32_t res; + SPIFFS_API_CHECK_CFG(fs); + SPIFFS_API_CHECK_MOUNT(fs); + SPIFFS_LOCK(fs); + + fh = SPIFFS_FH_UNOFFS(fs, fh); + + spiffs_fd *fd; + res = spiffs_fd_get(fs, fh, &fd); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + +#if SPIFFS_CACHE_WR + res = spiffs_fflush_cache(fs, fh); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); +#endif + + res = fd->fdoffset; + + SPIFFS_UNLOCK(fs); + return res; +} + #if SPIFFS_TEST_VISUALISATION s32_t SPIFFS_vis(spiffs *fs) { diff --git a/cores/esp8266/spiffs/spiffs_nucleus.c b/cores/esp8266/spiffs/spiffs_nucleus.c index 18712c6e1..ba55722bf 100644 --- a/cores/esp8266/spiffs/spiffs_nucleus.c +++ b/cores/esp8266/spiffs/spiffs_nucleus.c @@ -101,7 +101,7 @@ s32_t spiffs_phys_cpy( // Find object lookup entry containing given id with visitor. // Iterate over object lookup pages in each block until a given object id entry is found. -// When found, the visitor function is called with block index, entry index and user_data. +// When found, the visitor function is called with block index, entry index and user data. // If visitor returns SPIFFS_VIS_CONTINUE, the search goes on. Otherwise, the search will be // ended and visitor's return code is returned to caller. // If no visitor is given (0) the search returns on first entry with matching object id. @@ -113,8 +113,8 @@ s32_t spiffs_phys_cpy( // SPIFFS_VIS_NO_WRAP // @param obj_id argument object id // @param v visitor callback function -// @param user_data any data, passed to the callback visitor function -// @param user_p any pointer, passed to the callback visitor function +// @param user_const_p any const pointer, passed to the callback visitor function +// @param user_var_p any pointer, passed to the callback visitor function // @param block_ix reported block index where match was found // @param lu_entry reported look up index where match was found s32_t spiffs_obj_lu_find_entry_visitor( @@ -124,8 +124,8 @@ s32_t spiffs_obj_lu_find_entry_visitor( u8_t flags, spiffs_obj_id obj_id, spiffs_visitor_f v, - u32_t user_data, - void *user_p, + const void *user_const_p, + void *user_var_p, spiffs_block_ix *block_ix, int *lu_entry) { s32_t res = SPIFFS_OK; @@ -175,8 +175,8 @@ s32_t spiffs_obj_lu_find_entry_visitor( (flags & SPIFFS_VIS_CHECK_PH) ? obj_id : obj_lu_buf[cur_entry-entry_offset], cur_block, cur_entry, - user_data, - user_p); + user_const_p, + user_var_p); if (res == SPIFFS_VIS_COUNTINUE || res == SPIFFS_VIS_COUNTINUE_RELOAD) { if (res == SPIFFS_VIS_COUNTINUE_RELOAD) { res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU | SPIFFS_OP_C_READ, @@ -264,11 +264,11 @@ static s32_t spiffs_obj_lu_scan_v( spiffs_obj_id obj_id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, - void *user_p) { + const void *user_const_p, + void *user_var_p) { (void)bix; - (void)user_data; - (void)user_p; + (void)user_const_p; + (void)user_var_p; if (obj_id == SPIFFS_OBJ_ID_FREE) { if (ix_entry == 0) { fs->free_blocks++; @@ -409,11 +409,11 @@ s32_t spiffs_obj_lu_find_free( fs->free_blocks--; } } - if (res == SPIFFS_VIS_END) { + if (res == SPIFFS_ERR_FULL) { SPIFFS_DBG("fs full\n"); } - return res == SPIFFS_VIS_END ? SPIFFS_ERR_FULL : res; + return res; } // Find object lookup entry containing given id @@ -439,8 +439,8 @@ static s32_t spiffs_obj_lu_find_id_and_span_v( spiffs_obj_id obj_id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, - void *user_p) { + const void *user_const_p, + void *user_var_p) { s32_t res; spiffs_page_header ph; spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry); @@ -448,10 +448,10 @@ static s32_t spiffs_obj_lu_find_id_and_span_v( SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_header), (u8_t *)&ph); SPIFFS_CHECK_RES(res); if (ph.obj_id == obj_id && - ph.span_ix == (spiffs_span_ix)user_data && + ph.span_ix == *((spiffs_span_ix*)user_var_p) && (ph.flags & (SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_USED)) == SPIFFS_PH_FLAG_DELET && !((obj_id & SPIFFS_OBJ_ID_IX_FLAG) && (ph.flags & SPIFFS_PH_FLAG_IXDELE) == 0 && ph.span_ix == 0) && - (user_p == 0 || *((spiffs_page_ix *)user_p) != pix)) { + (user_const_p == 0 || *((const spiffs_page_ix*)user_const_p) != pix)) { return SPIFFS_OK; } else { return SPIFFS_VIS_COUNTINUE; @@ -476,8 +476,8 @@ s32_t spiffs_obj_lu_find_id_and_span( SPIFFS_VIS_CHECK_ID, obj_id, spiffs_obj_lu_find_id_and_span_v, - (u32_t)spix, exclusion_pix ? &exclusion_pix : 0, + &spix, &bix, &entry); @@ -515,8 +515,8 @@ s32_t spiffs_obj_lu_find_id_and_span_by_phdr( SPIFFS_VIS_CHECK_PH, obj_id, spiffs_obj_lu_find_id_and_span_v, - (u32_t)spix, exclusion_pix ? &exclusion_pix : 0, + &spix, &bix, &entry); @@ -690,7 +690,7 @@ s32_t spiffs_page_delete( s32_t spiffs_object_create( spiffs *fs, spiffs_obj_id obj_id, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], spiffs_obj_type type, spiffs_page_ix *objix_hdr_pix) { s32_t res = SPIFFS_OK; @@ -721,7 +721,7 @@ s32_t spiffs_object_create( oix_hdr.p_hdr.flags = 0xff & ~(SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_INDEX | SPIFFS_PH_FLAG_USED); oix_hdr.type = type; oix_hdr.size = SPIFFS_UNDEFINED_LEN; // keep ones so we can update later without wasting this page - strncpy((char *)&oix_hdr.name, (char *)name, SPIFFS_OBJ_NAME_LEN); + strncpy((char*)&oix_hdr.name, (const char*)name, SPIFFS_OBJ_NAME_LEN); // update page @@ -748,7 +748,7 @@ s32_t spiffs_object_update_index_hdr( spiffs_obj_id obj_id, spiffs_page_ix objix_hdr_pix, u8_t *new_objix_hdr_data, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], u32_t size, spiffs_page_ix *new_pix) { s32_t res = SPIFFS_OK; @@ -772,7 +772,7 @@ s32_t spiffs_object_update_index_hdr( // change name if (name) { - strncpy((char *)objix_hdr->name, (char *)name, SPIFFS_OBJ_NAME_LEN); + strncpy((char*)objix_hdr->name, (const char*)name, SPIFFS_OBJ_NAME_LEN); } if (size) { objix_hdr->size = size; @@ -1335,9 +1335,9 @@ static s32_t spiffs_object_find_object_index_header_by_name_v( spiffs_obj_id obj_id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, - void *user_p) { - (void)user_data; + const void *user_const_p, + void *user_var_p) { + (void)user_var_p; s32_t res; spiffs_page_object_ix_header objix_hdr; spiffs_page_ix pix = SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, bix, ix_entry); @@ -1351,7 +1351,7 @@ static s32_t spiffs_object_find_object_index_header_by_name_v( if (objix_hdr.p_hdr.span_ix == 0 && (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) == (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) { - if (strcmp((char *)user_p, (char *)objix_hdr.name) == 0) { + if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) { return SPIFFS_OK; } } @@ -1362,7 +1362,7 @@ static s32_t spiffs_object_find_object_index_header_by_name_v( // Finds object index header page by name s32_t spiffs_object_find_object_index_header_by_name( spiffs *fs, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], spiffs_page_ix *pix) { s32_t res; spiffs_block_ix bix; @@ -1374,8 +1374,8 @@ s32_t spiffs_object_find_object_index_header_by_name( 0, 0, spiffs_object_find_object_index_header_by_name_v, - 0, name, + 0, &bix, &entry); @@ -1402,7 +1402,8 @@ s32_t spiffs_object_truncate( s32_t res = SPIFFS_OK; spiffs *fs = fd->fs; - res = spiffs_gc_check(fs, remove ? 0 : SPIFFS_DATA_PAGE_SIZE(fs)); + // need 2 pages if not removing: object index page + possibly chopped data page + res = spiffs_gc_check(fs, remove ? 0 : SPIFFS_DATA_PAGE_SIZE(fs) * 2); SPIFFS_CHECK_RES(res); spiffs_page_ix objix_pix = fd->objix_hdr_pix; @@ -1482,7 +1483,7 @@ s32_t spiffs_object_truncate( SPIFFS_DBG("truncate: got data pix %04x\n", data_pix); - if (cur_size - SPIFFS_DATA_PAGE_SIZE(fs) >= new_size) { + if (new_size == 0 || remove || cur_size - new_size >= SPIFFS_DATA_PAGE_SIZE(fs)) { // delete full data page res = spiffs_page_data_check(fs, fd, data_pix, data_spix); if (res != SPIFFS_ERR_DELETED && res != SPIFFS_OK && res != SPIFFS_ERR_INDEX_REF_FREE) { @@ -1701,10 +1702,10 @@ typedef struct { } spiffs_free_obj_id_state; 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, - u32_t user_data, void *user_p) { + const void *user_const_p, void *user_var_p) { if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED) { - spiffs_obj_id min_obj_id = user_data; - u8_t *conflicting_name = (u8_t *)user_p; + spiffs_obj_id min_obj_id = *((spiffs_obj_id*)user_var_p); + const u8_t *conflicting_name = (const u8_t*)user_const_p; // if conflicting name parameter is given, also check if this name is found in object index hdrs if (conflicting_name && (id & SPIFFS_OBJ_ID_IX_FLAG)) { @@ -1717,7 +1718,7 @@ static s32_t spiffs_obj_lu_find_free_obj_id_bitmap_v(spiffs *fs, spiffs_obj_id i if (objix_hdr.p_hdr.span_ix == 0 && (objix_hdr.p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) == (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_IXDELE)) { - if (strcmp((char *)user_p, (char *)objix_hdr.name) == 0) { + if (strcmp((const char*)user_const_p, (char*)objix_hdr.name) == 0) { return SPIFFS_ERR_CONFLICTING_NAME; } } @@ -1734,11 +1735,11 @@ static s32_t spiffs_obj_lu_find_free_obj_id_bitmap_v(spiffs *fs, spiffs_obj_id i } 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, - u32_t user_data, void *user_p) { - (void)user_data; + const void *user_const_p, void *user_var_p) { + (void)user_var_p; if (id != SPIFFS_OBJ_ID_FREE && id != SPIFFS_OBJ_ID_DELETED && (id & SPIFFS_OBJ_ID_IX_FLAG)) { s32_t res; - spiffs_free_obj_id_state *state = (spiffs_free_obj_id_state *)user_p; + const spiffs_free_obj_id_state *state = (const spiffs_free_obj_id_state*)user_const_p; spiffs_page_object_ix_header objix_hdr; res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ, @@ -1766,10 +1767,10 @@ static s32_t spiffs_obj_lu_find_free_obj_id_compact_v(spiffs *fs, spiffs_obj_id // Scans thru all object lookup for object index header pages. If total possible number of // object ids cannot fit into a work buffer, these are grouped. When a group containing free // object ids is found, the object lu is again scanned for object ids within group and bitmasked. -// Finally, the bitmasked is searched for a free id -s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *conflicting_name) { +// Finally, the bitmask is searched for a free id +s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, const u8_t *conflicting_name) { s32_t res = SPIFFS_OK; - u32_t max_objects = (SPIFFS_CFG_PHYS_SZ(fs) / (u32_t)SPIFFS_CFG_LOG_PAGE_SZ(fs)) / 2; + u32_t max_objects = (fs->block_count * SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs)) / 2; spiffs_free_obj_id_state state; spiffs_obj_id free_obj_id = SPIFFS_OBJ_ID_FREE; state.min_obj_id = 1; @@ -1786,8 +1787,8 @@ s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *co SPIFFS_DBG("free_obj_id: BITM min:%04x max:%04x\n", state.min_obj_id, state.max_obj_id); memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs)); - res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_bitmap_v, state.min_obj_id, - conflicting_name, 0, 0); + res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_bitmap_v, + conflicting_name, &state.min_obj_id, 0, 0); if (res == SPIFFS_VIS_END) res = SPIFFS_OK; SPIFFS_CHECK_RES(res); // traverse bitmask until found free obj_id @@ -1851,7 +1852,7 @@ s32_t spiffs_obj_lu_find_free_obj_id(spiffs *fs, spiffs_obj_id *obj_id, u8_t *co SPIFFS_DBG("free_obj_id: COMP min:%04x max:%04x compact:%i\n", state.min_obj_id, state.max_obj_id, state.compaction); memset(fs->work, 0, SPIFFS_CFG_LOG_PAGE_SZ(fs)); - res = spiffs_obj_lu_find_entry_visitor(fs, 0, 0, 0, 0, spiffs_obj_lu_find_free_obj_id_compact_v, 0, &state, 0, 0); + 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); if (res == SPIFFS_VIS_END) res = SPIFFS_OK; SPIFFS_CHECK_RES(res); state.conflicting_name = 0; // searched for conflicting name once, no need to do it again diff --git a/cores/esp8266/spiffs/spiffs_nucleus.h b/cores/esp8266/spiffs/spiffs_nucleus.h index f19b24af8..b64d10cac 100644 --- a/cores/esp8266/spiffs/spiffs_nucleus.h +++ b/cores/esp8266/spiffs/spiffs_nucleus.h @@ -460,7 +460,7 @@ typedef struct __attribute(( packed )) { // callback func for object lookup visitor typedef s32_t (*spiffs_visitor_f)(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry, - u32_t user_data, void *user_p); + const void *user_const_p, void *user_var_p); #if SPIFFS_CACHE @@ -521,8 +521,8 @@ s32_t spiffs_obj_lu_find_entry_visitor( u8_t flags, spiffs_obj_id obj_id, spiffs_visitor_f v, - u32_t user_data, - void *user_p, + const void *user_const_p, + void *user_var_p, spiffs_block_ix *block_ix, int *lu_entry); @@ -538,7 +538,7 @@ s32_t spiffs_obj_lu_scan( s32_t spiffs_obj_lu_find_free_obj_id( spiffs *fs, spiffs_obj_id *obj_id, - u8_t *conflicting_name); + const u8_t *conflicting_name); s32_t spiffs_obj_lu_find_free( spiffs *fs, @@ -599,7 +599,7 @@ s32_t spiffs_page_delete( s32_t spiffs_object_create( spiffs *fs, spiffs_obj_id obj_id, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], spiffs_obj_type type, spiffs_page_ix *objix_hdr_pix); @@ -609,7 +609,7 @@ s32_t spiffs_object_update_index_hdr( spiffs_obj_id obj_id, spiffs_page_ix objix_hdr_pix, u8_t *new_objix_hdr_data, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], u32_t size, spiffs_page_ix *new_pix); @@ -661,7 +661,7 @@ s32_t spiffs_object_truncate( s32_t spiffs_object_find_object_index_header_by_name( spiffs *fs, - u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t name[SPIFFS_OBJ_NAME_LEN], spiffs_page_ix *pix); // ---------------