diff --git a/components/fs/fatfs/fatfs.c b/components/fs/fatfs/fatfs.c index 9aab47af..05463c8e 100644 --- a/components/fs/fatfs/fatfs.c +++ b/components/fs/fatfs/fatfs.c @@ -387,7 +387,8 @@ int FatfsOpen(struct File *file, const char *path, int oflag) errno = ENOMEM; return (int)LOS_NOK; } - + (void)memset_s(fp, sizeof(FIL), 0, sizeof(FIL)); + ret = FsChangeDrive(path); if (ret != (int)LOS_OK) { PRINT_ERR("FAT open ChangeDrive err 0x%x!\r\n", ret); @@ -675,7 +676,8 @@ int FatfsOpendir(struct Dir *dir, const char *dirName) errno = ENOENT; return (int)LOS_NOK; } - + (void)memset_s(dp, sizeof(DIR), 0, sizeof(DIR)); + res = f_opendir(dp, dirName); if (res != FR_OK) { PRINT_ERR("FAT opendir err 0x%x!\r\n", res); diff --git a/testsuites/unittest/posix/src/fs/posix_fs_func_test.c b/testsuites/unittest/posix/src/fs/posix_fs_func_test.c index 69b9a470..07edf0c2 100644 --- a/testsuites/unittest/posix/src/fs/posix_fs_func_test.c +++ b/testsuites/unittest/posix/src/fs/posix_fs_func_test.c @@ -1722,7 +1722,125 @@ LITE_TEST_CASE(PosixFsFuncTestSuite, testFsOpen005, Function | MediumTest | Leve } /* * - * @tc.number SUB_KERNEL_FS_CLOSE_005 + * @tc.number SUB_KERNEL_FS_OPEN_006 + * @tc.name open ro + ro + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, testFsOpen006, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + int32_t fd1 = open(tmpFileName, O_RDONLY, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd1); + + int32_t fd2 = open(tmpFileName, O_RDONLY, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(fd2, POSIX_FS_IS_ERROR, fd2); + + int32_t fd3 = open(tmpFileName, O_RDONLY, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(fd3, POSIX_FS_IS_ERROR, fd3); + + ret = close(fd1); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + ret = close(fd2); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + ret = close(fd3); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_OPEN_007 + * @tc.name open rw + rw + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, testFsOpen007, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + int32_t fd1 = open(tmpFileName, O_RDWR, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd1); + + int32_t fd2 = open(tmpFileName, O_RDWR, TEST_MODE_NORMAL); + ICUNIT_ASSERT_EQUAL(fd2, POSIX_FS_IS_ERROR, fd2); + ICUNIT_ASSERT_EQUAL(errno, EBUSY, POSIX_FS_IS_ERROR); + + ret = close(fd1); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + +/* * + * @tc.number SUB_KERNEL_FS_OPEN_008 + * @tc.name open + * @tc.desc [C- SOFTWARE -0200] + */ +LITE_TEST_CASE(PosixFsFuncTestSuite, testFsOpen008, Function | MediumTest | Level1) +{ + int32_t ret; + const char tmpFileName[TEST_BUF_SIZE] = { FILE1 }; + + int32_t fd = open(tmpFileName, O_CREAT | O_RDWR, TEST_MODE_HIGH); + ICUNIT_ASSERT_NOT_EQUAL(fd, POSIX_FS_IS_ERROR, fd); + + ret = close(fd); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + int32_t fd1 = open(tmpFileName, O_RDONLY, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(fd1, POSIX_FS_IS_ERROR, fd1); + + int32_t fd2 = open(tmpFileName, O_RDONLY, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(fd2, POSIX_FS_IS_ERROR, fd2); + + ret = close(fd1); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = close(fd2); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + fd1 = open(tmpFileName, O_RDWR, TEST_MODE_NORMAL); + ICUNIT_ASSERT_NOT_EQUAL(fd1, POSIX_FS_IS_ERROR, fd1); + + fd2 = open(tmpFileName, O_RDWR, TEST_MODE_NORMAL); + ICUNIT_ASSERT_EQUAL(fd2, POSIX_FS_IS_ERROR, fd2); + + int32_t fd3 = open(tmpFileName, O_RDWR, TEST_MODE_NORMAL); + ICUNIT_ASSERT_EQUAL(fd3, POSIX_FS_IS_ERROR, fd3); + + ret = close(fd1); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + ret = unlink(tmpFileName); + ICUNIT_ASSERT_NOT_EQUAL(ret, POSIX_FS_IS_ERROR, ret); + + return POSIX_FS_NO_ERROR; +} + + + +/* * + * @tc.number SUB_KERNEL_FS_CLOSE_001 * @tc.name close * @tc.desc [C- SOFTWARE -0200] */ @@ -2044,6 +2162,11 @@ void PosixFsAPITest(void) RUN_ONE_TESTCASE(testFsOpen003); RUN_ONE_TESTCASE(testFsOpen004); RUN_ONE_TESTCASE(testFsOpen005); +#if (LOSCFG_SUPPORT_FATFS == 1) + RUN_ONE_TESTCASE(testFsOpen006); + RUN_ONE_TESTCASE(testFsOpen007); + RUN_ONE_TESTCASE(testFsOpen008); +#endif RUN_ONE_TESTCASE(testFsClose001); RUN_ONE_TESTCASE(testFsWrite001); RUN_ONE_TESTCASE(testFsWrite002);