From 15deac4e8c10b4d4dec4186b07f02729b8bfb05f Mon Sep 17 00:00:00 2001 From: davide Date: Sat, 28 Jul 2018 22:54:26 +0200 Subject: [PATCH 1/3] Added SPIFFS_truncate function --- src/spiffs.h | 8 ++++++++ src/spiffs_hydrogen.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/spiffs.h b/src/spiffs.h index 534c3df..4c9e101 100644 --- a/src/spiffs.h +++ b/src/spiffs.h @@ -496,6 +496,14 @@ s32_t SPIFFS_remove(spiffs *fs, const char *path); */ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh); +/** + * Removes a file by path + * @param fs the file system struct + * @param path the path of the file to remove + * @param length the new length of truncated file + */ +s32_t SPIFFS_truncate(spiffs *fs, const char *path, off_t length); + /** * Gets file status by path * @param fs the file system struct diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 235aaaa..54ad2e5 100644 --- a/src/spiffs_hydrogen.c +++ b/src/spiffs_hydrogen.c @@ -724,6 +724,49 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) { #endif // SPIFFS_READ_ONLY } +s32_t SPIFFS_truncate(spiffs *fs, const char *path, off_t length) { + SPIFFS_API_DBG("%s '%s'\n", __func__, path); +#if SPIFFS_READ_ONLY + (void)fs; (void)path; + return SPIFFS_ERR_RO_NOT_IMPL; +#else + SPIFFS_API_CHECK_CFG(fs); + SPIFFS_API_CHECK_MOUNT(fs); + if (strlen(path) > SPIFFS_OBJ_NAME_LEN - 1) { + SPIFFS_API_CHECK_RES(fs, SPIFFS_ERR_NAME_TOO_LONG); + } + SPIFFS_LOCK(fs); + + spiffs_fd *fd; + spiffs_page_ix pix; + s32_t res; + + res = spiffs_fd_find_new(fs, &fd, 0); + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + + 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); + } + 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_truncate(fd, length, 0); + if (res != SPIFFS_OK) { + spiffs_fd_return(fs, fd->file_nbr); + } + SPIFFS_API_CHECK_RES_UNLOCK(fs, res); + + SPIFFS_UNLOCK(fs); + return 0; +#endif // SPIFFS_READ_ONLY +} + static s32_t spiffs_stat_pix(spiffs *fs, spiffs_page_ix pix, spiffs_file fh, spiffs_stat *s) { (void)fh; spiffs_page_object_ix_header objix_hdr; From 28c366272cf4be9674eb4de02e7816a4b72de0af Mon Sep 17 00:00:00 2001 From: davctv Date: Mon, 30 Jul 2018 11:04:24 +0200 Subject: [PATCH 2/3] Modified type for length to u32_t --- src/spiffs.h | 2 +- src/spiffs_hydrogen.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/spiffs.h b/src/spiffs.h index 4c9e101..991e903 100644 --- a/src/spiffs.h +++ b/src/spiffs.h @@ -502,7 +502,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh); * @param path the path of the file to remove * @param length the new length of truncated file */ -s32_t SPIFFS_truncate(spiffs *fs, const char *path, off_t length); +s32_t SPIFFS_truncate(spiffs *fs, const char *path, u32_t length); /** * Gets file status by path diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 54ad2e5..40e1f7f 100644 --- a/src/spiffs_hydrogen.c +++ b/src/spiffs_hydrogen.c @@ -724,7 +724,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) { #endif // SPIFFS_READ_ONLY } -s32_t SPIFFS_truncate(spiffs *fs, const char *path, off_t length) { +s32_t SPIFFS_truncate(spiffs *fs, const char *path, u32_t length) { SPIFFS_API_DBG("%s '%s'\n", __func__, path); #if SPIFFS_READ_ONLY (void)fs; (void)path; From 9006bcc405dcf0ef09f4bde81fb0ec1059b54eeb Mon Sep 17 00:00:00 2001 From: davctv Date: Mon, 30 Jul 2018 11:54:55 +0200 Subject: [PATCH 3/3] Added length also for SPIFFS_READ_ONLY defined section --- src/spiffs_hydrogen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spiffs_hydrogen.c b/src/spiffs_hydrogen.c index 40e1f7f..f879e32 100644 --- a/src/spiffs_hydrogen.c +++ b/src/spiffs_hydrogen.c @@ -727,7 +727,7 @@ s32_t SPIFFS_fremove(spiffs *fs, spiffs_file fh) { s32_t SPIFFS_truncate(spiffs *fs, const char *path, u32_t length) { SPIFFS_API_DBG("%s '%s'\n", __func__, path); #if SPIFFS_READ_ONLY - (void)fs; (void)path; + (void)fs; (void)path; (void)length; return SPIFFS_ERR_RO_NOT_IMPL; #else SPIFFS_API_CHECK_CFG(fs);