diff --git a/src/default/spiffs_config.h b/src/default/spiffs_config.h index 5a55921..65667fc 100644 --- a/src/default/spiffs_config.h +++ b/src/default/spiffs_config.h @@ -169,6 +169,15 @@ #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 diff --git a/src/spiffs.h b/src/spiffs.h index 4a0b147..2912f59 100644 --- a/src/spiffs.h +++ b/src/spiffs.h @@ -186,6 +186,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; diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 6763ac5..5daa28d 100644 --- a/src/spiffs_hydrogen.c +++ b/src/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 @@ -236,7 +244,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 +274,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 +285,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); @@ -353,6 +362,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); @@ -470,6 +480,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); @@ -549,6 +560,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); @@ -618,6 +630,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); @@ -675,6 +688,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); @@ -690,6 +704,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); diff --git a/src/test/params_test.h b/src/test/params_test.h index 95dfd36..bb5f52d 100644 --- a/src/test/params_test.h +++ b/src/test/params_test.h @@ -43,6 +43,11 @@ typedef unsigned char u8_t; #define SPIFFS_HAL_CALLBACK_EXTRA 1 #endif +#define SPIFFS_FILEHDL_OFFSET 1 +#if SPIFFS_FILEHDL_OFFSET +#define TEST_SPIFFS_FILEHDL_OFFSET 0x1000 +#endif + // Enable/disable void real_assert(int c, const char *n, const char *file, int l); diff --git a/src/test/test_bugreports.c b/src/test/test_bugreports.c index d24f25a..2c78f44 100644 --- a/src/test/test_bugreports.c +++ b/src/test/test_bugreports.c @@ -388,7 +388,12 @@ TEST(truncate_48) { TEST_CHECK_GE(fd, 0); spiffs_fd *desc; +#if SPIFFS_FILEHDL_OFFSET + res = spiffs_fd_get(FS, fd - TEST_SPIFFS_FILEHDL_OFFSET, &desc); +#else res = spiffs_fd_get(FS, fd, &desc); +#endif + TEST_CHECK_GE(res, 0); TEST_CHECK_EQ(desc->size, len); diff --git a/src/test/test_hydrogen.c b/src/test/test_hydrogen.c index 795e451..539e123 100644 --- a/src/test/test_hydrogen.c +++ b/src/test/test_hydrogen.c @@ -225,6 +225,42 @@ TEST(file_by_open_excl) } TEST_END(file_by_open_excl) +#if SPIFFS_FILEHDL_OFFSET +TEST(open_fh_offs) +{ + int res; + spiffs_stat s; + spiffs_file fd1, fd2, fd3; + fd1 = SPIFFS_open(FS, "1", SPIFFS_CREAT | SPIFFS_EXCL, 0); + fd2 = SPIFFS_open(FS, "2", SPIFFS_CREAT | SPIFFS_EXCL, 0); + fd3 = SPIFFS_open(FS, "3", SPIFFS_CREAT | SPIFFS_EXCL, 0); + TEST_CHECK(fd1 >= TEST_SPIFFS_FILEHDL_OFFSET); + TEST_CHECK(fd2 >= TEST_SPIFFS_FILEHDL_OFFSET); + TEST_CHECK(fd3 >= TEST_SPIFFS_FILEHDL_OFFSET); + SPIFFS_close(FS, fd1); + fd1 = SPIFFS_open(FS, "2", SPIFFS_RDONLY, 0); + TEST_CHECK(fd1 >= TEST_SPIFFS_FILEHDL_OFFSET); + SPIFFS_close(FS, fd2); + fd2 = SPIFFS_open(FS, "3", SPIFFS_RDONLY, 0); + TEST_CHECK(fd2 >= TEST_SPIFFS_FILEHDL_OFFSET); + SPIFFS_close(FS, fd3); + fd3 = SPIFFS_open(FS, "1", SPIFFS_RDONLY, 0); + TEST_CHECK(fd3 >= TEST_SPIFFS_FILEHDL_OFFSET); + SPIFFS_close(FS, fd1); + SPIFFS_close(FS, fd2); + SPIFFS_close(FS, fd3); + fd1 = SPIFFS_open(FS, "3", SPIFFS_RDONLY, 0); + TEST_CHECK(fd1 >= TEST_SPIFFS_FILEHDL_OFFSET); + SPIFFS_close(FS, fd1); + fd1 = SPIFFS_open(FS, "foo", SPIFFS_RDONLY, 0); + TEST_CHECK(fd1 < TEST_SPIFFS_FILEHDL_OFFSET); + + return TEST_RES_OK; +} +TEST_END(open_fh_offs) + +#endif //SPIFFS_FILEHDL_OFFSET + TEST(list_dir) { int res; diff --git a/src/test/test_spiffs.c b/src/test/test_spiffs.c index 726aa9e..07d46ee 100644 --- a/src/test/test_spiffs.c +++ b/src/test/test_spiffs.c @@ -332,7 +332,9 @@ s32_t fs_mount_specific(u32_t phys_addr, u32_t phys_size, c.phys_addr = phys_addr; c.phys_erase_block = phys_sector_size; c.phys_size = phys_size; - +#if SPIFFS_FILEHDL_OFFSET + c.fh_ix_offset = TEST_SPIFFS_FILEHDL_OFFSET; +#endif return SPIFFS_mount(&__fs, &c, _work, _fds, sizeof(_fds), _cache, sizeof(_cache), spiffs_check_cb_f); }