From 8b3d9996ede064206d4c4ca8cbbf8ef882a05842 Mon Sep 17 00:00:00 2001 From: philip Date: Thu, 29 Sep 2016 22:56:18 -0400 Subject: [PATCH 1/6] Fix the casting ... --- src/spiffs_gc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spiffs_gc.c b/src/spiffs_gc.c index 053db26..3b5f305 100644 --- a/src/spiffs_gc.c +++ b/src/spiffs_gc.c @@ -255,7 +255,7 @@ s32_t spiffs_gc_find_candidate( s32_t *cand_scores = (s32_t *)(fs->work + max_candidates * sizeof(spiffs_block_ix)); // align cand_scores on s32_t boundary - cand_scores = (s32_t*)(((ptrdiff_t)cand_scores + sizeof(ptrdiff_t) - 1) & ~(sizeof(ptrdiff_t) - 1)); + cand_scores = (s32_t*)(((intptr_t)cand_scores + sizeof(intptr_t) - 1) & ~(sizeof(intptr_t) - 1)); *block_candidates = cand_blocks; From 0dab43f171b4b1542991a06fc33d1f7bf9941d25 Mon Sep 17 00:00:00 2001 From: sensslen Date: Wed, 14 Dec 2016 16:44:16 +0100 Subject: [PATCH 2/6] set file offset when seeking over end The proposed change sets the file offset to the file's end when the file size is smaller than the expected seek index. This allows to use seek to put the file location to a certain position or the end. --- src/spiffs_hydrogen.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 1cf64ff..6810823 100644 --- a/src/spiffs_hydrogen.c +++ b/src/spiffs_hydrogen.c @@ -587,6 +587,7 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) { } if ((offs > (s32_t)fd->size) && (SPIFFS_UNDEFINED_LEN != fd->size)) { + fd->fdoffset = fd->size; res = SPIFFS_ERR_END_OF_OBJECT; } SPIFFS_API_CHECK_RES_UNLOCK(fs, res); From 19dd301cc63db96af8604829f64eea01d3f39fbd Mon Sep 17 00:00:00 2001 From: sensslen Date: Wed, 14 Dec 2016 17:05:08 +0100 Subject: [PATCH 3/6] Fix #120 - seek virgin file --- src/spiffs_hydrogen.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 1cf64ff..481f932 100644 --- a/src/spiffs_hydrogen.c +++ b/src/spiffs_hydrogen.c @@ -577,16 +577,18 @@ s32_t SPIFFS_lseek(spiffs *fs, spiffs_file fh, s32_t offs, int whence) { spiffs_fflush_cache(fs, fh); #endif + s32_t fileSize = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size; + switch (whence) { case SPIFFS_SEEK_CUR: offs = fd->fdoffset+offs; break; case SPIFFS_SEEK_END: - offs = (fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size) + offs; + offs = fileSize + offs; break; } - if ((offs > (s32_t)fd->size) && (SPIFFS_UNDEFINED_LEN != fd->size)) { + if ((offs > fileSize)) { res = SPIFFS_ERR_END_OF_OBJECT; } SPIFFS_API_CHECK_RES_UNLOCK(fs, res); From 8c5bacc45c7ae456d818107096fd1f5aafa191d8 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Thu, 19 Jan 2017 20:50:17 +0000 Subject: [PATCH 4/6] Add optional file metadata Metadata is stored in the file index structure and is opaque to SPIFFS. It allows user to associate additional information with a file. Fixes pellepl/spiffs#127 --- .travis.yml | 2 +- src/default/spiffs_config.h | 7 ++++ src/spiffs.h | 24 +++++++++++ src/spiffs_gc.c | 2 +- src/spiffs_hydrogen.c | 84 +++++++++++++++++++++++++++++++++++-- src/spiffs_nucleus.c | 41 ++++++++++++------ src/spiffs_nucleus.h | 6 +++ src/test/test_bugreports.c | 6 --- src/test/test_hydrogen.c | 76 +++++++++++++++++++++++++++++---- src/test/test_spiffs.c | 13 ++++-- 10 files changed, 228 insertions(+), 33 deletions(-) diff --git a/.travis.yml b/.travis.yml index fe7cb23..8c36dc5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,4 @@ compiler: before_script: -script: make all && make clean && make test && make build-all +script: make all && make clean && make test && make build-all && make clean test FLAGS=-DSPIFFS_OBJ_META_LEN=8 diff --git a/src/default/spiffs_config.h b/src/default/spiffs_config.h index 008a5d0..6b95a35 100644 --- a/src/default/spiffs_config.h +++ b/src/default/spiffs_config.h @@ -107,6 +107,13 @@ #define SPIFFS_OBJ_NAME_LEN (32) #endif +// Maximum length of the metadata associated with an object. +// Setting to non-zero value enables metadata-related API but also +// changes the on-disk format, so the change is not backward-compatible. +#ifndef SPIFFS_OBJ_META_LEN +#define SPIFFS_OBJ_META_LEN (0) +#endif + // Size of buffer allocated on stack used when copying data. // Lower value generates more read/writes. No meaning having it bigger // than logical page size. diff --git a/src/spiffs.h b/src/spiffs.h index bea90b3..b66dc32 100644 --- a/src/spiffs.h +++ b/src/spiffs.h @@ -297,6 +297,9 @@ typedef struct { spiffs_obj_type type; spiffs_page_ix pix; u8_t name[SPIFFS_OBJ_NAME_LEN]; +#if SPIFFS_OBJ_META_LEN + u8_t meta[SPIFFS_OBJ_META_LEN]; +#endif } spiffs_stat; struct spiffs_dirent { @@ -305,6 +308,9 @@ struct spiffs_dirent { spiffs_obj_type type; u32_t size; spiffs_page_ix pix; +#if SPIFFS_OBJ_META_LEN + u8_t meta[SPIFFS_OBJ_META_LEN]; +#endif }; typedef struct { @@ -525,6 +531,24 @@ s32_t SPIFFS_close(spiffs *fs, spiffs_file fh); */ s32_t SPIFFS_rename(spiffs *fs, const char *old, const char *newPath); +#if SPIFFS_OBJ_META_LEN +/** + * Updates file's metadata + * @param fs the file system struct + * @param path path to the file + * @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long. + */ +s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta); + +/** + * Updates file's metadata + * @param fs the file system struct + * @param fh file handle of the file + * @param meta new metadata. must be SPIFFS_OBJ_META_LEN bytes long. + */ +s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta); +#endif + /** * Returns last error of last file operation. * @param fs the file system struct diff --git a/src/spiffs_gc.c b/src/spiffs_gc.c index 3b5f305..b9656a1 100644 --- a/src/spiffs_gc.c +++ b/src/spiffs_gc.c @@ -575,7 +575,7 @@ s32_t spiffs_gc_clean(spiffs *fs, spiffs_block_ix bix) { cur_entry = gc.stored_scan_entry_index; // pop cursor if (gc.cur_objix_spix == 0) { // store object index header page - res = spiffs_object_update_index_hdr(fs, 0, gc.cur_obj_id | SPIFFS_OBJ_ID_IX_FLAG, gc.cur_objix_pix, fs->work, 0, 0, &new_objix_pix); + res = spiffs_object_update_index_hdr(fs, 0, gc.cur_obj_id | SPIFFS_OBJ_ID_IX_FLAG, gc.cur_objix_pix, fs->work, 0, 0, 0, &new_objix_pix); SPIFFS_GC_DBG("gc_clean: MOVE_DATA store modified objix_hdr page, %04x:%04x\n", new_objix_pix, 0); SPIFFS_CHECK_RES(res); } else { diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 2c454a4..456403c 100644 --- a/src/spiffs_hydrogen.c +++ b/src/spiffs_hydrogen.c @@ -191,7 +191,7 @@ s32_t SPIFFS_creat(spiffs *fs, const char *path, spiffs_mode mode) { 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, (const u8_t*)path, SPIFFS_TYPE_FILE, 0); + res = spiffs_object_create(fs, obj_id, (const u8_t*)path, 0, SPIFFS_TYPE_FILE, 0); SPIFFS_API_CHECK_RES_UNLOCK(fs, res); SPIFFS_UNLOCK(fs); return 0; @@ -243,7 +243,7 @@ spiffs_file SPIFFS_open(spiffs *fs, const char *path, spiffs_flags flags, spiffs spiffs_fd_return(fs, fd->file_nbr); } SPIFFS_API_CHECK_RES_UNLOCK(fs, res); - res = spiffs_object_create(fs, obj_id, (const u8_t*)path, SPIFFS_TYPE_FILE, &pix); + res = spiffs_object_create(fs, obj_id, (const u8_t*)path, 0, SPIFFS_TYPE_FILE, &pix); if (res < SPIFFS_OK) { spiffs_fd_return(fs, fd->file_nbr); } @@ -706,6 +706,9 @@ static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spi s->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size; s->pix = pix; strncpy((char *)s->name, (char *)objix_hdr.name, SPIFFS_OBJ_NAME_LEN); +#if SPIFFS_OBJ_META_LEN + memcpy(s->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN); +#endif return res; } @@ -863,7 +866,7 @@ s32_t SPIFFS_rename(spiffs *fs, const char *old_path, const char *new_path) { SPIFFS_API_CHECK_RES_UNLOCK(fs, res); res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, (const u8_t*)new_path, - 0, &pix_dummy); + 0, 0, &pix_dummy); #if SPIFFS_TEMPORAL_FD_CACHE if (res == SPIFFS_OK) { spiffs_fd_temporal_cache_rehash(fs, old_path, new_path); @@ -880,6 +883,78 @@ s32_t SPIFFS_rename(spiffs *fs, const char *old_path, const char *new_path) { #endif // SPIFFS_READ_ONLY } +#if SPIFFS_OBJ_META_LEN +s32_t SPIFFS_update_meta(spiffs *fs, const char *name, const void *meta) { +#if SPIFFS_READ_ONLY + (void)fs; (void)name; (void)meta; + return SPIFFS_ERR_RO_NOT_IMPL; +#else + SPIFFS_API_CHECK_CFG(fs); + SPIFFS_API_CHECK_MOUNT(fs); + SPIFFS_LOCK(fs); + + spiffs_page_ix pix, pix_dummy; + spiffs_fd *fd; + + s32_t res = spiffs_object_find_object_index_header_by_name(fs, (const u8_t*)name, &pix); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + + res = spiffs_fd_find_new(fs, &fd, 0); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + + res = spiffs_object_open_by_page(fs, pix, fd, 0, 0); + if (res != SPIFFS_OK) { + spiffs_fd_return(fs, fd->file_nbr); + } + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + + res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, meta, + 0, &pix_dummy); + + spiffs_fd_return(fs, fd->file_nbr); + + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + + SPIFFS_UNLOCK(fs); + + return res; +#endif // SPIFFS_READ_ONLY +} + +s32_t SPIFFS_fupdate_meta(spiffs *fs, spiffs_file fh, const void *meta) { +#if SPIFFS_READ_ONLY + (void)fs; (void)fh; (void)meta; + return SPIFFS_ERR_RO_NOT_IMPL; +#else + SPIFFS_API_CHECK_CFG(fs); + SPIFFS_API_CHECK_MOUNT(fs); + SPIFFS_LOCK(fs); + + s32_t res; + spiffs_fd *fd; + spiffs_page_ix pix_dummy; + + fh = SPIFFS_FH_UNOFFS(fs, fh); + res = spiffs_fd_get(fs, fh, &fd); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + + if ((fd->flags & SPIFFS_O_WRONLY) == 0) { + res = SPIFFS_ERR_NOT_WRITABLE; + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + } + + res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, fd->objix_hdr_pix, 0, 0, meta, + 0, &pix_dummy); + + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + + SPIFFS_UNLOCK(fs); + + return res; +#endif // SPIFFS_READ_ONLY +} +#endif // SPIFFS_OBJ_META_LEN + spiffs_DIR *SPIFFS_opendir(spiffs *fs, const char *name, spiffs_DIR *d) { (void)name; @@ -928,6 +1003,9 @@ static s32_t spiffs_read_dir_v( e->type = objix_hdr.type; e->size = objix_hdr.size == SPIFFS_UNDEFINED_LEN ? 0 : objix_hdr.size; e->pix = pix; +#if SPIFFS_OBJ_META_LEN + memcpy(e->meta, objix_hdr.meta, SPIFFS_OBJ_META_LEN); +#endif return SPIFFS_OK; } return SPIFFS_VIS_COUNTINUE; diff --git a/src/spiffs_nucleus.c b/src/spiffs_nucleus.c index 35fe0d4..1cfb833 100644 --- a/src/spiffs_nucleus.c +++ b/src/spiffs_nucleus.c @@ -908,6 +908,7 @@ s32_t spiffs_object_create( spiffs *fs, spiffs_obj_id obj_id, const u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t meta[SPIFFS_OBJ_META_LEN], spiffs_obj_type type, spiffs_page_ix *objix_hdr_pix) { s32_t res = SPIFFS_OK; @@ -938,8 +939,16 @@ 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, (const char*)name, SPIFFS_OBJ_NAME_LEN); - + strncpy((char*)oix_hdr.name, (const char*)name, SPIFFS_OBJ_NAME_LEN); +#if SPIFFS_OBJ_META_LEN + if (meta) { + memcpy(oix_hdr.meta, meta, SPIFFS_OBJ_META_LEN); + } else { + memset(oix_hdr.meta, 0xff, SPIFFS_OBJ_META_LEN); + } +#else + (void) meta; +#endif // update page res = _spiffs_wr(fs, SPIFFS_OP_T_OBJ_DA | SPIFFS_OP_C_UPDT, @@ -969,6 +978,7 @@ s32_t spiffs_object_update_index_hdr( spiffs_page_ix objix_hdr_pix, u8_t *new_objix_hdr_data, const u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t meta[SPIFFS_OBJ_META_LEN], u32_t size, spiffs_page_ix *new_pix) { s32_t res = SPIFFS_OK; @@ -994,6 +1004,13 @@ s32_t spiffs_object_update_index_hdr( if (name) { strncpy((char*)objix_hdr->name, (const char*)name, SPIFFS_OBJ_NAME_LEN); } +#if SPIFFS_OBJ_META_LEN + if (meta) { + memcpy(objix_hdr->meta, meta, SPIFFS_OBJ_META_LEN); + } +#else + (void) meta; +#endif if (size) { objix_hdr->size = size; } @@ -1212,7 +1229,7 @@ s32_t spiffs_object_append(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) { } else { // was a nonempty object, update to new page res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - fd->objix_hdr_pix, fs->work, 0, offset+written, &new_objix_hdr_page); + fd->objix_hdr_pix, fs->work, 0, 0, offset+written, &new_objix_hdr_page); SPIFFS_CHECK_RES(res); SPIFFS_DBG("append: %04x store new objix_hdr, %04x:%04x, written %i\n", fd->obj_id, new_objix_hdr_page, 0, written); @@ -1229,7 +1246,7 @@ s32_t spiffs_object_append(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) { SPIFFS_EV_IX_UPD,fd->obj_id, objix->p_hdr.span_ix, cur_objix_pix, 0); // update length in object index header page res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - fd->objix_hdr_pix, 0, 0, offset+written, &new_objix_hdr_page); + fd->objix_hdr_pix, 0, 0, 0, offset+written, &new_objix_hdr_page); SPIFFS_CHECK_RES(res); SPIFFS_DBG("append: %04x store new size I %i in objix_hdr, %04x:%04x, written %i\n", fd->obj_id, offset+written, new_objix_hdr_page, 0, written); @@ -1364,7 +1381,7 @@ s32_t spiffs_object_append(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) { // update size in object header index page res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - fd->objix_hdr_pix, 0, 0, offset+written, &new_objix_hdr_page); + fd->objix_hdr_pix, 0, 0, 0, offset+written, &new_objix_hdr_page); SPIFFS_DBG("append: %04x store new size II %i in objix_hdr, %04x:%04x, written %i, res %i\n", fd->obj_id , offset+written, new_objix_hdr_page, 0, written, res2); SPIFFS_CHECK_RES(res2); @@ -1388,7 +1405,7 @@ s32_t spiffs_object_append(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) { } else { // modifying object index header page, update size and make new copy res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - fd->objix_hdr_pix, fs->work, 0, offset+written, &new_objix_hdr_page); + fd->objix_hdr_pix, fs->work, 0, 0, offset+written, &new_objix_hdr_page); SPIFFS_DBG("append: %04x store modified objix_hdr page, %04x:%04x, written %i\n", fd->obj_id , new_objix_hdr_page, 0, written); SPIFFS_CHECK_RES(res2); @@ -1438,7 +1455,7 @@ s32_t spiffs_object_modify(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) { if (prev_objix_spix == 0) { // store previous object index header page res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - fd->objix_hdr_pix, fs->work, 0, 0, &new_objix_hdr_pix); + fd->objix_hdr_pix, fs->work, 0, 0, 0, &new_objix_hdr_pix); SPIFFS_DBG("modify: store modified objix_hdr page, %04x:%04x, written %i\n", new_objix_hdr_pix, 0, written); SPIFFS_CHECK_RES(res); } else { @@ -1595,7 +1612,7 @@ s32_t spiffs_object_modify(spiffs_fd *fd, u32_t offset, u8_t *data, u32_t len) { } else { // wrote within object index header page res2 = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - fd->objix_hdr_pix, fs->work, 0, 0, &new_objix_hdr_pix); + fd->objix_hdr_pix, fs->work, 0, 0, 0, &new_objix_hdr_pix); SPIFFS_DBG("modify: store modified objix_hdr page, %04x:%04x, written %i\n", new_objix_hdr_pix, 0, written); SPIFFS_CHECK_RES(res2); } @@ -1735,7 +1752,7 @@ s32_t spiffs_object_truncate( if (remove_full == 0) { SPIFFS_DBG("truncate: update objix hdr page %04x:%04x to size %i\n", fd->objix_hdr_pix, prev_objix_spix, cur_size); res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - fd->objix_hdr_pix, 0, 0, cur_size, &new_objix_hdr_pix); + fd->objix_hdr_pix, 0, 0, 0, cur_size, &new_objix_hdr_pix); SPIFFS_CHECK_RES(res); } fd->size = cur_size; @@ -1872,14 +1889,14 @@ s32_t spiffs_object_truncate( memset(fs->work + sizeof(spiffs_page_object_ix_header), 0xff, SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix_header)); res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - objix_pix, fs->work, 0, SPIFFS_UNDEFINED_LEN, &new_objix_hdr_pix); + objix_pix, fs->work, 0, 0, SPIFFS_UNDEFINED_LEN, &new_objix_hdr_pix); SPIFFS_CHECK_RES(res); } } else { // update object index header page SPIFFS_DBG("truncate: update object index header page with indices and size\n"); res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - objix_pix, fs->work, 0, cur_size, &new_objix_hdr_pix); + objix_pix, fs->work, 0, 0, cur_size, &new_objix_hdr_pix); SPIFFS_CHECK_RES(res); } } else { @@ -1900,7 +1917,7 @@ s32_t spiffs_object_truncate( fd->offset = cur_size; // update object index header page with new size res = spiffs_object_update_index_hdr(fs, fd, fd->obj_id, - fd->objix_hdr_pix, 0, 0, cur_size, &new_objix_hdr_pix); + fd->objix_hdr_pix, 0, 0, 0, cur_size, &new_objix_hdr_pix); SPIFFS_CHECK_RES(res); } fd->size = cur_size; diff --git a/src/spiffs_nucleus.h b/src/spiffs_nucleus.h index 0fa1cb3..f0e859d 100644 --- a/src/spiffs_nucleus.h +++ b/src/spiffs_nucleus.h @@ -480,6 +480,10 @@ typedef struct __attribute(( packed )) spiffs_obj_type type; // name of object u8_t name[SPIFFS_OBJ_NAME_LEN]; +#if SPIFFS_OBJ_META_LEN + // metadata. not interpreted by SPIFFS in any way. + u8_t meta[SPIFFS_OBJ_META_LEN]; +#endif } spiffs_page_object_ix_header; // object index page header @@ -635,6 +639,7 @@ s32_t spiffs_object_create( spiffs *fs, spiffs_obj_id obj_id, const u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t meta[SPIFFS_OBJ_META_LEN], spiffs_obj_type type, spiffs_page_ix *objix_hdr_pix); @@ -645,6 +650,7 @@ s32_t spiffs_object_update_index_hdr( spiffs_page_ix objix_hdr_pix, u8_t *new_objix_hdr_data, const u8_t name[SPIFFS_OBJ_NAME_LEN], + const u8_t meta[SPIFFS_OBJ_META_LEN], u32_t size, spiffs_page_ix *new_pix); diff --git a/src/test/test_bugreports.c b/src/test/test_bugreports.c index 8ea5f74..3cd0b18 100644 --- a/src/test/test_bugreports.c +++ b/src/test/test_bugreports.c @@ -194,7 +194,6 @@ TEST(nodemcu_309) { fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_DIRECT, 0); TEST_CHECK(fd > 0); int i; - spiffs_stat s; res = SPIFFS_OK; u8_t err = 0; for (i = 1; i <= 1280; i++) { @@ -247,7 +246,6 @@ TEST(robert) { sprintf(fname, "test.txt"); fd = SPIFFS_open(FS, fname, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0); TEST_CHECK(fd > 0); - int i; res = SPIFFS_OK; char buf[500]; memset(buf, 0xaa, 500); @@ -542,8 +540,6 @@ TEST(spiffs_dup_file_74) { TEST(temporal_fd_cache) { fs_reset_specific(0, 0, 1024*1024, 4096, 2*4096, 256); - spiffs_file fd; - int res; (FS)->fd_count = 4; char *fcss = "blaha.css"; @@ -574,8 +570,6 @@ TEST(temporal_fd_cache) { int run = 0; do { - u8_t buf[256]; - // open & read an html int dice = rand() % 100; int probability = 0; diff --git a/src/test/test_hydrogen.c b/src/test/test_hydrogen.c index 8244db4..05aadb0 100644 --- a/src/test/test_hydrogen.c +++ b/src/test/test_hydrogen.c @@ -189,6 +189,12 @@ TEST(bad_fd) res = SPIFFS_write(FS, fd, 0, 0); TEST_CHECK(res < 0); TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_BAD_DESCRIPTOR); +#if SPIFFS_OBJ_META_LEN + u8_t new_meta[SPIFFS_OBJ_META_LEN] = {0}; + res = SPIFFS_fupdate_meta(FS, fd, new_meta); + TEST_CHECK(res < 0); + TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_BAD_DESCRIPTOR); +#endif return TEST_RES_OK; } TEST_END @@ -218,6 +224,12 @@ TEST(closed_fd) res = SPIFFS_write(FS, fd, 0, 0); TEST_CHECK(res < 0); TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_FILE_CLOSED); +#if SPIFFS_OBJ_META_LEN + u8_t new_meta[SPIFFS_OBJ_META_LEN] = {0}; + res = SPIFFS_fupdate_meta(FS, fd, new_meta); + TEST_CHECK(res < 0); + TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_FILE_CLOSED); +#endif return TEST_RES_OK; } TEST_END @@ -351,8 +363,6 @@ TEST_END #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); @@ -417,6 +427,11 @@ TEST(list_dir) break; } } +#if SPIFFS_OBJ_META_LEN + for (i = 0; i < SPIFFS_OBJ_META_LEN; i++) { + TEST_CHECK_EQ(pe->meta[i], 0xff); + } +#endif } SPIFFS_closedir(&d); @@ -727,6 +742,53 @@ TEST(rename) { return TEST_RES_OK; } TEST_END +#if SPIFFS_OBJ_META_LEN +TEST(update_meta) { + s32_t i, res, fd; + spiffs_stat s; + u8_t new_meta[SPIFFS_OBJ_META_LEN], new_meta2[SPIFFS_OBJ_META_LEN]; + + res = test_create_file("foo"); + TEST_CHECK(res >= 0); + + for (i = 0; i < SPIFFS_OBJ_META_LEN; i++) { + new_meta[i] = 0xaa; + } + res = SPIFFS_update_meta(FS, "foo", new_meta); + TEST_CHECK(res >= 0); + + res = SPIFFS_stat(FS, "foo", &s); + TEST_CHECK(res >= 0); + TEST_CHECK_EQ(memcmp(s.meta, new_meta, SPIFFS_OBJ_META_LEN), 0); + + for (i = 0; i < SPIFFS_OBJ_META_LEN; i++) { + new_meta2[i] = 0xbb; + } + + fd = SPIFFS_open(FS, "foo", SPIFFS_RDONLY, 0); + TEST_CHECK(fd >= 0); + res = SPIFFS_fupdate_meta(FS, fd, new_meta2); + TEST_CHECK(res < 0); + TEST_CHECK(SPIFFS_errno(FS) == SPIFFS_ERR_NOT_WRITABLE); + SPIFFS_close(FS, fd); + + res = SPIFFS_stat(FS, "foo", &s); + TEST_CHECK(res >= 0); + TEST_CHECK_EQ(memcmp(s.meta, new_meta, SPIFFS_OBJ_META_LEN), 0); + + fd = SPIFFS_open(FS, "foo", SPIFFS_RDWR, 0); + TEST_CHECK(fd >= 0); + res = SPIFFS_fupdate_meta(FS, fd, new_meta2); + TEST_CHECK_EQ(res, 0); + SPIFFS_close(FS, fd); + + res = SPIFFS_stat(FS, "foo", &s); + TEST_CHECK(res >= 0); + TEST_CHECK_EQ(memcmp(s.meta, new_meta2, SPIFFS_OBJ_META_LEN), 0); + + return TEST_RES_OK; +} TEST_END +#endif TEST(remove_single_by_path) { @@ -1301,7 +1363,6 @@ TEST(lseek_simple_modification) { int res; spiffs_file fd; char *fname = "seekfile"; - int i; int len = 4096; fd = SPIFFS_open(FS, fname, SPIFFS_TRUNC | SPIFFS_CREAT | SPIFFS_RDWR, 0); TEST_CHECK(fd > 0); @@ -1341,7 +1402,6 @@ TEST(lseek_modification_append) { int res; spiffs_file fd; char *fname = "seekfile"; - int i; int len = 4096; fd = SPIFFS_open(FS, fname, SPIFFS_TRUNC | SPIFFS_CREAT | SPIFFS_RDWR, 0); TEST_CHECK(fd > 0); @@ -2022,7 +2082,7 @@ TEST(ix_map_remap) { // create a file, 10 data pages long s32_t res; - spiffs_file fd1, fd2; + spiffs_file fd1; fd1 = SPIFFS_open(FS, "1", SPIFFS_O_CREAT | SPIFFS_O_WRONLY, 0); TEST_CHECK_GT(fd1, 0); @@ -2128,7 +2188,7 @@ TEST(ix_map_partial) { // create a file, 10 data pages long s32_t res; - spiffs_file fd, fd2; + spiffs_file fd; fd = SPIFFS_open(FS, "1", SPIFFS_O_CREAT | SPIFFS_O_WRONLY, 0); TEST_CHECK_GT(fd, 0); @@ -2160,7 +2220,6 @@ TEST(ix_map_partial) const int entries = SPIFFS_bytes_to_ix_map_entries(FS, size/2); spiffs_ix_map map; spiffs_page_ix ixbuf[entries]; - spiffs_page_ix ixbuf_ref[entries]; printf("map 0-50%%\n"); res = SPIFFS_ix_map(FS, fd, &map, 0, size/2, ixbuf); @@ -2324,6 +2383,9 @@ SUITE_TESTS(hydrogen_tests) ADD_TEST(user_callback_gc) ADD_TEST(name_too_long) ADD_TEST(rename) +#if SPIFFS_OBJ_META_LEN + ADD_TEST(update_meta) +#endif ADD_TEST(remove_single_by_path) ADD_TEST(remove_single_by_fd) ADD_TEST(write_cache) diff --git a/src/test/test_spiffs.c b/src/test/test_spiffs.c index 2f138a2..ff7f952 100644 --- a/src/test/test_spiffs.c +++ b/src/test/test_spiffs.c @@ -342,7 +342,7 @@ void dump_flash_access_stats() { } -static u32_t old_perc = 999; +// static u32_t old_perc = 999; static void spiffs_check_cb_f(spiffs *fs, spiffs_check_type type, spiffs_check_report report, u32_t arg1, u32_t arg2) { /* if (report == SPIFFS_CHECK_PROGRESS && old_perc != arg1) { @@ -593,7 +593,6 @@ void real_assert(int c, const char *n, const char *file, int l) { } int read_and_verify(char *name) { - s32_t res; int fd = SPIFFS_open(&__fs, name, SPIFFS_RDONLY, 0); if (fd < 0) { printf(" read_and_verify: could not open file %s\n", name); @@ -693,6 +692,14 @@ int test_create_file(char *name) { CHECK_RES(res); CHECK(strcmp((char*)s.name, name) == 0); CHECK(s.size == 0); +#if SPIFFS_OBJ_META_LEN + { + int i; + for (i = 0; i < SPIFFS_OBJ_META_LEN; i++) { + CHECK(s.meta[i] == 0xff); + } + } +#endif SPIFFS_close(FS, fd); return 0; } @@ -707,7 +714,7 @@ int test_create_and_write_file(char *name, int size, int chunk_size) { } CHECK(res >= 0); fd = SPIFFS_open(FS, name, SPIFFS_APPEND | SPIFFS_RDWR, 0); - if (res < 0) { + if (fd < 0) { printf(" failed open, %i\n",res); } CHECK(fd >= 0); From a9030dfa8cb99a41d7fecfda51534fb7b06e0341 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Fri, 20 Jan 2017 20:58:38 +0000 Subject: [PATCH 5/6] Address review comment --- src/default/spiffs_config.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/default/spiffs_config.h b/src/default/spiffs_config.h index 6b95a35..d675f2b 100644 --- a/src/default/spiffs_config.h +++ b/src/default/spiffs_config.h @@ -110,6 +110,13 @@ // Maximum length of the metadata associated with an object. // Setting to non-zero value enables metadata-related API but also // changes the on-disk format, so the change is not backward-compatible. +// +// Do note: the meta length must never exceed +// logical_page_size - (SPIFFS_OBJ_NAME_LEN + 64) +// +// This is derived from following: +// logical_page_size - (SPIFFS_OBJ_NAME_LEN + sizeof(spiffs_page_header) + +// spiffs_object_ix_header fields + at least some LUT entries) #ifndef SPIFFS_OBJ_META_LEN #define SPIFFS_OBJ_META_LEN (0) #endif From bc87d0b44cd6e70147572173cd068a0b1cbc8b33 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Sat, 21 Jan 2017 07:01:59 +0000 Subject: [PATCH 6/6] Reset SPIFFS_OBJ_ID_IX_FLAG on obj_id returned by readdir Same as stat. --- src/spiffs_hydrogen.c | 1 + src/test/test_hydrogen.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 456403c..1c2cbd9 100644 --- a/src/spiffs_hydrogen.c +++ b/src/spiffs_hydrogen.c @@ -1036,6 +1036,7 @@ struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e) { if (res == SPIFFS_OK) { d->block = bix; d->entry = entry + 1; + e->obj_id &= ~SPIFFS_OBJ_ID_IX_FLAG; ret = e; } else { d->fs->err_code = res; diff --git a/src/test/test_hydrogen.c b/src/test/test_hydrogen.c index 05aadb0..273327a 100644 --- a/src/test/test_hydrogen.c +++ b/src/test/test_hydrogen.c @@ -427,11 +427,17 @@ TEST(list_dir) break; } } + { + spiffs_stat s; + TEST_CHECK_EQ(SPIFFS_stat(FS, pe->name, &s), 0); + TEST_CHECK_EQ(pe->obj_id, s.obj_id); + TEST_CHECK_EQ(pe->size, s.size); + TEST_CHECK_EQ(pe->type, s.type); + TEST_CHECK_EQ(pe->pix, s.pix); #if SPIFFS_OBJ_META_LEN - for (i = 0; i < SPIFFS_OBJ_META_LEN; i++) { - TEST_CHECK_EQ(pe->meta[i], 0xff); - } + TEST_CHECK_EQ(memcmp(pe->meta, s.meta, SPIFFS_OBJ_META_LEN), 0); #endif + } } SPIFFS_closedir(&d);