Merge branch 'bugfix/fatfs_open_O_CREAT_fails_v5.1' into 'release/v5.1'

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

See merge request espressif/esp-idf!23561
pull/11519/head
Ivan Grokhotkov 2023-05-05 18:13:51 +08:00
commit bba2581f1a
4 zmienionych plików z 37 dodań i 1 usunięć

Wyświetl plik

@ -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();

Wyświetl plik

@ -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' */

Wyświetl plik

@ -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);

Wyświetl plik

@ -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;