diff --git a/include/fs/fs.h b/include/fs/fs.h index 7aef7c78d38..f717e8cd453 100644 --- a/include/fs/fs.h +++ b/include/fs/fs.h @@ -220,6 +220,20 @@ struct fs_statvfs { #define FS_FSTAB_DECLARE_ENTRY(node_id) \ extern struct fs_mount_t FS_FSTAB_ENTRY(node_id) +/** + * @brief Initialize fs_file_t object + * + * Initialized the fs_file_t object; the function needs to be invoked + * on object before first use with fs_open. + * + * @param zfp Pointer to file object + * + */ +static inline void fs_file_t_init(struct fs_file_t *zfp) +{ + *zfp = (struct fs_file_t){ 0 }; +} + /** * @brief Open or create file * diff --git a/include/fs/fs_interface.h b/include/fs/fs_interface.h index 889034ff708..d1a30da7460 100644 --- a/include/fs/fs_interface.h +++ b/include/fs/fs_interface.h @@ -45,6 +45,8 @@ struct fs_mount_t; /** * @brief File object representing an open file * + * The object needs to be initialized with function fs_file_t_init(). + * * @param Pointer to FATFS file object structure * @param mp Pointer to mount point structure */ diff --git a/tests/subsys/fs/common/test_fs_open_flags.c b/tests/subsys/fs/common/test_fs_open_flags.c index 3afc274ce35..9d17b0d6771 100644 --- a/tests/subsys/fs/common/test_fs_open_flags.c +++ b/tests/subsys/fs/common/test_fs_open_flags.c @@ -141,6 +141,8 @@ void test_fs_open_flags(void) }; int block = 1; + fs_file_t_init(&ts.file); + ZBEGIN("Attempt open non-existent"); ZOPEN(&ts, 0, -ENOENT); ZOPEN(&ts, FS_O_WRITE, -ENOENT); diff --git a/tests/subsys/fs/fs_api/src/main.c b/tests/subsys/fs/fs_api/src/main.c index 0527eb145aa..5bd1847c440 100644 --- a/tests/subsys/fs/fs_api/src/main.c +++ b/tests/subsys/fs/fs_api/src/main.c @@ -72,6 +72,7 @@ void test_main(void) ztest_unit_test_setup_teardown(test_mount, fs_setup, dummy_teardown), + ztest_unit_test(test_fs_file_t_init), ztest_unit_test(test_file_statvfs), ztest_unit_test(test_mkdir), ztest_unit_test(test_opendir), diff --git a/tests/subsys/fs/fs_api/src/test_fs.h b/tests/subsys/fs/fs_api/src/test_fs.h index f7ecb0aa902..2e423e4652a 100644 --- a/tests/subsys/fs/fs_api/src/test_fs.h +++ b/tests/subsys/fs/fs_api/src/test_fs.h @@ -32,6 +32,7 @@ struct test_fs_data { int reserve; }; +void test_fs_file_t_init(void); void test_fs_register(void); void test_mount(void); void test_file_statvfs(void); diff --git a/tests/subsys/fs/fs_api/src/test_fs_dir_file.c b/tests/subsys/fs/fs_api/src/test_fs_dir_file.c index bfdd38f3ccd..9231bdbb40f 100644 --- a/tests/subsys/fs/fs_api/src/test_fs_dir_file.c +++ b/tests/subsys/fs/fs_api/src/test_fs_dir_file.c @@ -78,6 +78,21 @@ static struct fs_file_t filep; static struct fs_file_t err_filep; static const char test_str[] = "hello world!"; +/** + * @brief Test fs_file_t_init initializer + */ +void test_fs_file_t_init(void) +{ + struct fs_file_t fst; + + memset(&fst, 0xff, sizeof(fst)); + + fs_file_t_init(&fst); + zassert_equal(fst.mp, NULL, "Expected to be initialized to NULL"); + zassert_equal(fst.filep, NULL, "Expected to be initialized to NULL"); + zassert_equal(fst.flags, 0, "Expected to be initialized to 0"); +} + /** * @brief Test mount interface of filesystem * @@ -396,6 +411,7 @@ void test_file_open(void) int ret; TC_PRINT("\nOpen tests:\n"); + fs_file_t_init(&filep); TC_PRINT("\nOpen a file without a path\n"); ret = fs_open(&filep, NULL, FS_O_READ); @@ -435,7 +451,7 @@ static int _test_file_write(void) TC_PRINT("\nWrite tests:\n"); TC_PRINT("Write to an unopened file\n"); - err_filep.mp = NULL; + fs_file_t_init(&err_filep); brw = fs_write(&err_filep, (char *)test_str, strlen(test_str)); if (brw >= 0) { return TC_FAIL; @@ -500,7 +516,7 @@ static int _test_file_sync(void) TC_PRINT("\nSync tests:\n"); TC_PRINT("sync an unopened file\n"); - err_filep.mp = NULL; + fs_file_t_init(&err_filep); ret = fs_sync(&err_filep); if (!ret) { return TC_FAIL; @@ -513,6 +529,7 @@ static int _test_file_sync(void) return TC_FAIL; } + fs_file_t_init(&filep); ret = fs_open(&filep, TEST_FILE, FS_O_RDWR); for (;;) { @@ -578,7 +595,7 @@ void test_file_read(void) TC_PRINT("\nRead tests:\n"); TC_PRINT("Read an unopened file\n"); - err_filep.mp = NULL; + fs_file_t_init(&err_filep); brw = fs_read(&err_filep, read_buff, sz); zassert_false(brw >= 0, "Can't read an unopened file"); @@ -637,7 +654,7 @@ static int _test_file_truncate(void) TC_PRINT("\nTruncate tests: max file size is 128byte\n"); TC_PRINT("\nTruncate, seek, tell an unopened file\n"); - err_filep.mp = NULL; + fs_file_t_init(&err_filep); ret = fs_truncate(&err_filep, 256); if (!ret) { return TC_FAIL; @@ -798,7 +815,7 @@ void test_file_close(void) TC_PRINT("\nClose tests:\n"); TC_PRINT("Close an unopened file\n"); - err_filep.mp = NULL; + fs_file_t_init(&err_filep); ret = fs_close(&err_filep); zassert_equal(ret, 0, "Should close an unopened file"); diff --git a/tests/subsys/fs/fs_api/src/test_fs_mount_flags.c b/tests/subsys/fs/fs_api/src/test_fs_mount_flags.c index 0139917c76e..92b1ed40090 100644 --- a/tests/subsys/fs/fs_api/src/test_fs_mount_flags.c +++ b/tests/subsys/fs/fs_api/src/test_fs_mount_flags.c @@ -18,7 +18,9 @@ static struct fs_mount_t mp = { void test_mount_flags(void) { int ret = 0; - struct fs_file_t fs = { 0 }; + struct fs_file_t fs; + + fs_file_t_init(&fs); /* Format volume and add some files/dirs to check read-only flag */ mp.flags = 0;