1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #14175 from sven-hm/fatfs_vfs_open_flag_translation

pkg/fatfs/fatfs_vfs: fix flag translation in _open
This commit is contained in:
benpicco 2020-06-29 23:40:55 +02:00 committed by GitHub
commit 3af5efe5cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 1 deletions

View File

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

View File

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