From b31b68fc6865ad8a00d3a2e999e28e0f37509fe8 Mon Sep 17 00:00:00 2001 From: Sonika Rathi Date: Mon, 3 Apr 2023 14:55:37 +0530 Subject: [PATCH] bugfix: fix for fatfs "open("xx",O_CREAT|O_WRONLY,0666)" call failure fatfs 'open' with only O_CREAT flag fails to creat new file Closes https://github.com/espressif/esp-idf/issues/1817 --- .../flash_wl/main/test_fatfs_flash_wl.c | 8 +++++++ .../test_fatfs_common/test_fatfs_common.c | 24 +++++++++++++++++++ .../test_fatfs_common/test_fatfs_common.h | 4 ++++ components/fatfs/vfs/vfs_fat.c | 2 +- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c b/components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c index 64cf468a0c..a56b789507 100644 --- a/components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c +++ b/components/fatfs/test_apps/flash_wl/main/test_fatfs_flash_wl.c @@ -67,6 +67,14 @@ TEST_CASE("(WL) can create and write file", "[fatfs][wear_levelling]") test_teardown(); } +TEST_CASE("(WL) can create and open file with O_CREAT flag", "[fatfs][wear_levelling]") +{ + test_setup(); + test_fatfs_create_file_with_o_creat_flag("/spiflash/hello.txt"); + test_fatfs_open_file_with_o_creat_flag("/spiflash/hello.txt"); + test_teardown(); +} + TEST_CASE("(WL) can read file", "[fatfs][wear_levelling]") { test_setup(); diff --git a/components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c b/components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c index c9b578868e..1bc54c7aea 100644 --- a/components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c +++ b/components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c @@ -32,6 +32,30 @@ void test_fatfs_create_file_with_text(const char* name, const char* text) TEST_ASSERT_EQUAL(0, fclose(f)); } +void test_fatfs_create_file_with_o_creat_flag(const char* filename) +{ + const int fd = open(filename, O_CREAT|O_WRONLY); + TEST_ASSERT_NOT_EQUAL(-1, fd); + + const int r = pwrite(fd, fatfs_test_hello_str, strlen(fatfs_test_hello_str), 0); //offset=0 + TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str), r); + + TEST_ASSERT_EQUAL(0, close(fd)); +} + +void test_fatfs_open_file_with_o_creat_flag(const char* filename) +{ + char buf[32] = { 0 }; + const int fd = open(filename, O_CREAT|O_RDONLY); + TEST_ASSERT_NOT_EQUAL(-1, fd); + + int r = pread(fd, buf, sizeof(buf), 0); // it is a regular read() with offset==0 + TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str, buf)); + TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str), r); + + TEST_ASSERT_EQUAL(0, close(fd)); +} + void test_fatfs_overwrite_append(const char* filename) { /* Create new file with 'aaaa' */ diff --git a/components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h b/components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h index 8b2ed0ea32..dd28211dae 100644 --- a/components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h +++ b/components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.h @@ -27,6 +27,10 @@ extern const char* fatfs_test_hello_str_utf; void test_fatfs_create_file_with_text(const char* name, const char* text); +void test_fatfs_create_file_with_o_creat_flag(const char* filename); + +void test_fatfs_open_file_with_o_creat_flag(const char* filename); + void test_fatfs_overwrite_append(const char* filename); void test_fatfs_read_file(const char* filename); diff --git a/components/fatfs/vfs/vfs_fat.c b/components/fatfs/vfs/vfs_fat.c index 6cfd1145c0..eff73ec359 100644 --- a/components/fatfs/vfs/vfs_fat.c +++ b/components/fatfs/vfs/vfs_fat.c @@ -264,7 +264,7 @@ static int fat_mode_conv(int m) res |= FA_CREATE_NEW; } else if ((m & O_CREAT) && (m & O_TRUNC)) { res |= FA_CREATE_ALWAYS; - } else if (m & O_APPEND) { + } else if ((m & O_APPEND) || (m & O_CREAT)) { res |= FA_OPEN_ALWAYS; } else { res |= FA_OPEN_EXISTING;