فهرست منبع

Merge branch 'bugfix/fatfs_open_O_CREAT_fails' into 'master'

fatfs: fix "open("xx",O_CREAT|O_WRONLY,0666)" call failure

See merge request espressif/esp-idf!23045
Martin Vychodil 2 سال پیش
والد
کامیت
81d814b42f

+ 8 - 0
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();

+ 24 - 0
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' */

+ 4 - 0
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);

+ 1 - 1
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;