Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

pkg/fatfs/fatfs_vfs: fix flag translation in _open #14175

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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