Merge branch 'bugfix/vfat_open_append' into 'master'
Fix fopen() in “a” (append) mode fopen() does not work when file is opened in “a” (append) mode * Merges github PR #302 https://github.com/espressif/esp-idf/pull/302 * Closes #328 https://github.com/espressif/esp-idf/issues/328 See merge request !503
This commit is contained in:
commit
7eaf1c76dc
2 changed files with 50 additions and 1 deletions
|
@ -152,7 +152,7 @@ static int fat_mode_conv(int m)
|
||||||
}
|
}
|
||||||
if ((m & O_CREAT) && (m & O_EXCL)) {
|
if ((m & O_CREAT) && (m & O_EXCL)) {
|
||||||
res |= FA_CREATE_NEW;
|
res |= FA_CREATE_NEW;
|
||||||
} else if (m & O_CREAT) {
|
} else if ((m & O_CREAT) && (m & O_TRUNC)) {
|
||||||
res |= FA_CREATE_ALWAYS;
|
res |= FA_CREATE_ALWAYS;
|
||||||
} else if (m & O_APPEND) {
|
} else if (m & O_APPEND) {
|
||||||
res |= FA_OPEN_ALWAYS;
|
res |= FA_OPEN_ALWAYS;
|
||||||
|
|
|
@ -69,6 +69,55 @@ TEST_CASE("can create and write file on sd card", "[fatfs][ignore]")
|
||||||
HEAP_SIZE_CHECK(0);
|
HEAP_SIZE_CHECK(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("overwrite and append file on sd card", "[fatfs][ignore]")
|
||||||
|
{
|
||||||
|
HEAP_SIZE_CAPTURE();
|
||||||
|
|
||||||
|
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
||||||
|
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
||||||
|
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||||
|
.format_if_mount_failed = true,
|
||||||
|
.max_files = 5
|
||||||
|
};
|
||||||
|
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL));
|
||||||
|
|
||||||
|
/* Create new file with 'aaaa' */
|
||||||
|
const char *NAME = "/sdcard/hello.txt";
|
||||||
|
create_file_with_text(NAME, "aaaa");
|
||||||
|
|
||||||
|
/* Append 'bbbb' to file */
|
||||||
|
FILE *f_a = fopen(NAME, "a");
|
||||||
|
TEST_ASSERT_NOT_NULL(f_a);
|
||||||
|
TEST_ASSERT_NOT_EQUAL(EOF, fputs("bbbb", f_a));
|
||||||
|
TEST_ASSERT_EQUAL(0, fclose(f_a));
|
||||||
|
|
||||||
|
/* Read back 8 bytes from file, verify it's 'aaaabbbb' */
|
||||||
|
char buf[10] = { 0 };
|
||||||
|
FILE *f_r = fopen(NAME, "r");
|
||||||
|
TEST_ASSERT_NOT_NULL(f_r);
|
||||||
|
TEST_ASSERT_EQUAL(8, fread(buf, 1, 8, f_r));
|
||||||
|
TEST_ASSERT_EQUAL_STRING_LEN("aaaabbbb", buf, 8);
|
||||||
|
|
||||||
|
/* Be sure we're at end of file */
|
||||||
|
TEST_ASSERT_EQUAL(0, fread(buf, 1, 8, f_r));
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(0, fclose(f_r));
|
||||||
|
|
||||||
|
/* Overwrite file with 'cccc' */
|
||||||
|
create_file_with_text(NAME, "cccc");
|
||||||
|
|
||||||
|
/* Verify file now only contains 'cccc' */
|
||||||
|
f_r = fopen(NAME, "r");
|
||||||
|
TEST_ASSERT_NOT_NULL(f_r);
|
||||||
|
bzero(buf, sizeof(buf));
|
||||||
|
TEST_ASSERT_EQUAL(4, fread(buf, 1, 8, f_r)); // trying to read 8 bytes, only expecting 4
|
||||||
|
TEST_ASSERT_EQUAL_STRING_LEN("cccc", buf, 4);
|
||||||
|
TEST_ASSERT_EQUAL(0, fclose(f_r));
|
||||||
|
|
||||||
|
TEST_ESP_OK(esp_vfs_fat_sdmmc_unmount());
|
||||||
|
HEAP_SIZE_CHECK(0);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("can read file on sd card", "[fatfs][ignore]")
|
TEST_CASE("can read file on sd card", "[fatfs][ignore]")
|
||||||
{
|
{
|
||||||
HEAP_SIZE_CAPTURE();
|
HEAP_SIZE_CAPTURE();
|
||||||
|
|
Loading…
Reference in a new issue