Skip to content

Commit

Permalink
Merge pull request RIOT-OS#14175 from sven-hm/fatfs_vfs_open_flag_tra…
Browse files Browse the repository at this point in the history
…nslation

pkg/fatfs/fatfs_vfs: fix flag translation in _open
  • Loading branch information
benpicco authored Jun 29, 2020
2 parents a92dac5 + 4469d8f commit 3af5efe
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/fatfs/fatfs_vfs/fatfs_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ static int _open(vfs_file_t *filp, const char *name, int flags, mode_t mode,
fatfs_flags |= FA_CREATE_ALWAYS;
}
if ((flags & O_CREAT) == O_CREAT) {
fatfs_flags |= FA_CREATE_NEW;
if ((flags & O_EXCL) == O_EXCL) {
fatfs_flags |= FA_CREATE_NEW;
}
else {
fatfs_flags |= FA_OPEN_ALWAYS;
}
}
else {
fatfs_flags |= FA_OPEN_EXISTING;
Expand Down
78 changes: 78 additions & 0 deletions tests/pkg_fatfs_vfs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,84 @@ static void test_create(void)
nw = vfs_write(fd, test_txt, sizeof(test_txt));
print_test_result("test_create__write_wo", nw == sizeof(test_txt));
print_test_result("test_create__close_wo", vfs_close(fd) == 0);

/* test create if file exists */
fd = vfs_open(FULL_FNAME1, O_WRONLY | O_CREAT, 0);
print_test_result("test_create__open_wo2", fd >= 0);

nw = vfs_write(fd, test_txt, sizeof(test_txt));
print_test_result("test_create__write_wo2", nw == sizeof(test_txt));
print_test_result("test_create__close_wo2", vfs_close(fd) == 0);

print_test_result("test_create__umount", vfs_umount(&_test_vfs_mount) == 0);
}

#ifdef MODULE_NEWLIB
static void test_newlib(void)
{
FILE* fl;
char buf[sizeof(test_txt) + sizeof(test_txt2)];
print_test_result("test_newlib__mount", vfs_mount(&_test_vfs_mount) == 0);

/* try to open file that doesn't exist */
fl = fopen(FULL_FNAME_NXIST, "r");
print_test_result("test_newlib__fopen", fl == NULL);
if (fl) {
fclose(fl);
}

/* create new file write and check content */
remove(FULL_FNAME2);
fl = fopen(FULL_FNAME2, "w+");
print_test_result("test_newlib__fopen_w", fl != NULL);
if (fl) {
print_test_result("test_newlib__fputs_w", fputs(test_txt, fl) >= 0);
rewind(fl);
print_test_result("test_newlib__fread_w",
fread(buf, sizeof(*buf), sizeof(buf), fl) > 0);
print_test_result("test_newlib__strcmp_w", strcmp(test_txt, buf) == 0);
print_test_result("test_newlib__fclose_w", fclose(fl) == 0);
}

/* cppcheck-suppress resourceLeak
* (reason: cppcheck <2.0 reports a false positive here) */
fl = fopen(FULL_FNAME2, "r"); /* open file RO */
print_test_result("test_newlib__fopen_r", fl != NULL);
if (fl) {
print_test_result("test_newlib__fclose_r", fclose(fl) == 0);
}

/* remove file */
print_test_result("test_newlib__remove", remove(FULL_FNAME2) == 0);

/* append to non existing file */
fl = fopen(FULL_FNAME2, "a");
print_test_result("test_newlib__fopen_a", fl != NULL);
if (fl) {
print_test_result("test_newlib__fputs_a", fputs(test_txt, fl) >= 0);
print_test_result("test_newlib__fclose_a", fclose(fl) == 0);
}

/* append to existing file and check content */
fl = fopen(FULL_FNAME2, "a+");
print_test_result("test_newlib__fopen_a2", fl != NULL);
if (fl) {
print_test_result("test_newlib__fputs_a2", fputs(test_txt2, fl) >= 0);
rewind(fl);
print_test_result("test_newlib__fread_a2",
fread(buf, sizeof(*buf), sizeof(buf), fl) > 0);
print_test_result("test_newlib__strcmp_a2",
strncmp(test_txt, buf, strlen(test_txt)) == 0);
print_test_result("test_newlib__strcmp_a2", strncmp(test_txt2,
&buf[strlen(test_txt)], strlen(test_txt2)) == 0);
print_test_result("test_newlib__fclose_a2", fclose(fl) == 0);
}
print_test_result("test_newlib__remove", remove(FULL_FNAME2) == 0);

print_test_result("test_newlib__umount", vfs_umount(&_test_vfs_mount) == 0);
}
#endif

int main(void)
{
#if MODULE_MTD_SDCARD
Expand Down Expand Up @@ -324,6 +399,9 @@ int main(void)
test_unlink();
test_mkrmdir();
test_create();
#ifdef MODULE_NEWLIB
test_newlib();
#endif

printf("Test end.\n");
return 0;
Expand Down

0 comments on commit 3af5efe

Please sign in to comment.