Skip to content

Commit

Permalink
bugfix: fix for fatfs "open("xx",O_CREAT|O_WRONLY,0666)" call failure
Browse files Browse the repository at this point in the history
fatfs 'open' with only O_CREAT flag fails to creat new file

Closes #1817
  • Loading branch information
RathiSonika committed Apr 18, 2023
1 parent 9cfc975 commit b31b68f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
24 changes: 24 additions & 0 deletions components/fatfs/test_apps/test_fatfs_common/test_fatfs_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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' */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion components/fatfs/vfs/vfs_fat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b31b68f

Please sign in to comment.