Skip to content

Commit 40572fd

Browse files
de-nordicnashif
authored andcommitted
fs: Add fs_file_t_init() function
The fs_file_t_init() function has been added that should be used for initialization of fs_file_t structures before passing them to fs_open and other functions. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 96c7852 commit 40572fd

File tree

7 files changed

+45
-6
lines changed

7 files changed

+45
-6
lines changed

include/fs/fs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,20 @@ struct fs_statvfs {
220220
#define FS_FSTAB_DECLARE_ENTRY(node_id) \
221221
extern struct fs_mount_t FS_FSTAB_ENTRY(node_id)
222222

223+
/**
224+
* @brief Initialize fs_file_t object
225+
*
226+
* Initialized the fs_file_t object; the function needs to be invoked
227+
* on object before first use with fs_open.
228+
*
229+
* @param zfp Pointer to file object
230+
*
231+
*/
232+
static inline void fs_file_t_init(struct fs_file_t *zfp)
233+
{
234+
*zfp = (struct fs_file_t){ 0 };
235+
}
236+
223237
/**
224238
* @brief Open or create file
225239
*

include/fs/fs_interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ struct fs_mount_t;
4545
/**
4646
* @brief File object representing an open file
4747
*
48+
* The object needs to be initialized with function fs_file_t_init().
49+
*
4850
* @param Pointer to FATFS file object structure
4951
* @param mp Pointer to mount point structure
5052
*/

tests/subsys/fs/common/test_fs_open_flags.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ void test_fs_open_flags(void)
141141
};
142142
int block = 1;
143143

144+
fs_file_t_init(&ts.file);
145+
144146
ZBEGIN("Attempt open non-existent");
145147
ZOPEN(&ts, 0, -ENOENT);
146148
ZOPEN(&ts, FS_O_WRITE, -ENOENT);

tests/subsys/fs/fs_api/src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void test_main(void)
7272
ztest_unit_test_setup_teardown(test_mount,
7373
fs_setup,
7474
dummy_teardown),
75+
ztest_unit_test(test_fs_file_t_init),
7576
ztest_unit_test(test_file_statvfs),
7677
ztest_unit_test(test_mkdir),
7778
ztest_unit_test(test_opendir),

tests/subsys/fs/fs_api/src/test_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct test_fs_data {
3232
int reserve;
3333
};
3434

35+
void test_fs_file_t_init(void);
3536
void test_fs_register(void);
3637
void test_mount(void);
3738
void test_file_statvfs(void);

tests/subsys/fs/fs_api/src/test_fs_dir_file.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ static struct fs_file_t filep;
7878
static struct fs_file_t err_filep;
7979
static const char test_str[] = "hello world!";
8080

81+
/**
82+
* @brief Test fs_file_t_init initializer
83+
*/
84+
void test_fs_file_t_init(void)
85+
{
86+
struct fs_file_t fst;
87+
88+
memset(&fst, 0xff, sizeof(fst));
89+
90+
fs_file_t_init(&fst);
91+
zassert_equal(fst.mp, NULL, "Expected to be initialized to NULL");
92+
zassert_equal(fst.filep, NULL, "Expected to be initialized to NULL");
93+
zassert_equal(fst.flags, 0, "Expected to be initialized to 0");
94+
}
95+
8196
/**
8297
* @brief Test mount interface of filesystem
8398
*
@@ -396,6 +411,7 @@ void test_file_open(void)
396411
int ret;
397412

398413
TC_PRINT("\nOpen tests:\n");
414+
fs_file_t_init(&filep);
399415

400416
TC_PRINT("\nOpen a file without a path\n");
401417
ret = fs_open(&filep, NULL, FS_O_READ);
@@ -435,7 +451,7 @@ static int _test_file_write(void)
435451
TC_PRINT("\nWrite tests:\n");
436452

437453
TC_PRINT("Write to an unopened file\n");
438-
err_filep.mp = NULL;
454+
fs_file_t_init(&err_filep);
439455
brw = fs_write(&err_filep, (char *)test_str, strlen(test_str));
440456
if (brw >= 0) {
441457
return TC_FAIL;
@@ -500,7 +516,7 @@ static int _test_file_sync(void)
500516
TC_PRINT("\nSync tests:\n");
501517

502518
TC_PRINT("sync an unopened file\n");
503-
err_filep.mp = NULL;
519+
fs_file_t_init(&err_filep);
504520
ret = fs_sync(&err_filep);
505521
if (!ret) {
506522
return TC_FAIL;
@@ -513,6 +529,7 @@ static int _test_file_sync(void)
513529
return TC_FAIL;
514530
}
515531

532+
fs_file_t_init(&filep);
516533
ret = fs_open(&filep, TEST_FILE, FS_O_RDWR);
517534

518535
for (;;) {
@@ -578,7 +595,7 @@ void test_file_read(void)
578595
TC_PRINT("\nRead tests:\n");
579596

580597
TC_PRINT("Read an unopened file\n");
581-
err_filep.mp = NULL;
598+
fs_file_t_init(&err_filep);
582599
brw = fs_read(&err_filep, read_buff, sz);
583600
zassert_false(brw >= 0, "Can't read an unopened file");
584601

@@ -637,7 +654,7 @@ static int _test_file_truncate(void)
637654
TC_PRINT("\nTruncate tests: max file size is 128byte\n");
638655

639656
TC_PRINT("\nTruncate, seek, tell an unopened file\n");
640-
err_filep.mp = NULL;
657+
fs_file_t_init(&err_filep);
641658
ret = fs_truncate(&err_filep, 256);
642659
if (!ret) {
643660
return TC_FAIL;
@@ -798,7 +815,7 @@ void test_file_close(void)
798815
TC_PRINT("\nClose tests:\n");
799816

800817
TC_PRINT("Close an unopened file\n");
801-
err_filep.mp = NULL;
818+
fs_file_t_init(&err_filep);
802819
ret = fs_close(&err_filep);
803820
zassert_equal(ret, 0, "Should close an unopened file");
804821

tests/subsys/fs/fs_api/src/test_fs_mount_flags.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ static struct fs_mount_t mp = {
1818
void test_mount_flags(void)
1919
{
2020
int ret = 0;
21-
struct fs_file_t fs = { 0 };
21+
struct fs_file_t fs;
22+
23+
fs_file_t_init(&fs);
2224

2325
/* Format volume and add some files/dirs to check read-only flag */
2426
mp.flags = 0;

0 commit comments

Comments
 (0)