From da42024bf2bfc380d6c3b9b39dc6b167ed4cc8c3 Mon Sep 17 00:00:00 2001 From: Alexander Somov Date: Wed, 29 Nov 2023 18:05:25 +0300 Subject: [PATCH 1/2] feat(spiffs): add fsync() support to SPIFFS VFS driver Closes https://github.com/espressif/esp-idf/pull/12693 Signed-off-by: sonika.rathi --- components/spiffs/esp_spiffs.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/components/spiffs/esp_spiffs.c b/components/spiffs/esp_spiffs.c index 5731b322b7..45c7d8b1cb 100644 --- a/components/spiffs/esp_spiffs.c +++ b/components/spiffs/esp_spiffs.c @@ -58,6 +58,7 @@ static ssize_t vfs_spiffs_read(void* ctx, int fd, void * dst, size_t size); static int vfs_spiffs_close(void* ctx, int fd); static off_t vfs_spiffs_lseek(void* ctx, int fd, off_t offset, int mode); static int vfs_spiffs_fstat(void* ctx, int fd, struct stat * st); +static int vfs_spiffs_fsync(void* ctx, int fd); #ifdef CONFIG_VFS_SUPPORT_DIR static int vfs_spiffs_stat(void* ctx, const char * path, struct stat * st); static int vfs_spiffs_unlink(void* ctx, const char *path); @@ -426,6 +427,7 @@ esp_err_t esp_vfs_spiffs_register(const esp_vfs_spiffs_conf_t * conf) .open_p = &vfs_spiffs_open, .close_p = &vfs_spiffs_close, .fstat_p = &vfs_spiffs_fstat, + .fsync_p = &vfs_spiffs_fsync, #ifdef CONFIG_VFS_SUPPORT_DIR .stat_p = &vfs_spiffs_stat, .link_p = &vfs_spiffs_link, @@ -617,6 +619,18 @@ static int vfs_spiffs_fstat(void* ctx, int fd, struct stat * st) return res; } +static int vfs_spiffs_fsync(void* ctx, int fd) +{ + esp_spiffs_t * efs = (esp_spiffs_t *)ctx; + int res = SPIFFS_fflush(efs->fs, fd); + if (res < 0) { + errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); + SPIFFS_clearerr(efs->fs); + return -1; + } + return res; +} + #ifdef CONFIG_VFS_SUPPORT_DIR static int vfs_spiffs_stat(void* ctx, const char * path, struct stat * st) From c1adbb234b9299202d8b42ef31e646ceab6b75ea Mon Sep 17 00:00:00 2001 From: "sonika.rathi" Date: Fri, 26 Jul 2024 12:07:14 +0200 Subject: [PATCH 2/2] test(spiffs): add test case for fsync() call --- components/spiffs/esp_spiffs.c | 2 +- .../spiffs/test_apps/main/test_spiffs.c | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/components/spiffs/esp_spiffs.c b/components/spiffs/esp_spiffs.c index 45c7d8b1cb..df66be5079 100644 --- a/components/spiffs/esp_spiffs.c +++ b/components/spiffs/esp_spiffs.c @@ -628,7 +628,7 @@ static int vfs_spiffs_fsync(void* ctx, int fd) SPIFFS_clearerr(efs->fs); return -1; } - return res; + return ESP_OK; } #ifdef CONFIG_VFS_SUPPORT_DIR diff --git a/components/spiffs/test_apps/main/test_spiffs.c b/components/spiffs/test_apps/main/test_spiffs.c index ed8d85f864..4710de6a79 100644 --- a/components/spiffs/test_apps/main/test_spiffs.c +++ b/components/spiffs/test_apps/main/test_spiffs.c @@ -285,6 +285,37 @@ static void test_spiffs_ftruncate(const char *filename) TEST_ASSERT_EQUAL(0, close(fd)); } +static void test_spiffs_fsync(const char *filename) +{ + const char input[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + size_t expected_size = strlen(input); + + int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC); + TEST_ASSERT_NOT_EQUAL(-1, fd); + + ssize_t wr = write(fd, input, expected_size); + TEST_ASSERT_NOT_EQUAL(-1, wr); + + TEST_ASSERT_EQUAL(0, fsync(fd)); + struct stat st; + TEST_ASSERT_EQUAL(0, stat(filename, &st)); + TEST_ASSERT_EQUAL(wr, st.st_size); + + ssize_t wr2 = write(fd, input, expected_size); + TEST_ASSERT_NOT_EQUAL(-1, wr2); + + TEST_ASSERT_EQUAL(0, fsync(fd)); + TEST_ASSERT_EQUAL(0, stat(filename, &st)); + TEST_ASSERT_EQUAL(wr + wr2, st.st_size); + + TEST_ASSERT_EQUAL(0, ftruncate(fd, wr)); + TEST_ASSERT_EQUAL(0, fsync(fd)); + TEST_ASSERT_EQUAL(0, stat(filename, &st)); + TEST_ASSERT_EQUAL(wr, st.st_size); + + TEST_ASSERT_EQUAL(0, close(fd)); +} + static void test_spiffs_can_opendir(const char* path) { char name_dir_file[64]; @@ -710,6 +741,13 @@ TEST_CASE("ftruncate a file", "[spiffs]") test_teardown(); } +TEST_CASE("fsync works correctly", "[spiffs]") +{ + test_setup(); + test_spiffs_fsync("/spiffs/fsync.txt"); + test_teardown(); +} + TEST_CASE("can opendir root directory of FS", "[spiffs]") { test_setup();