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 <dominik.ermel@nordicsemi.no>
This commit is contained in:
Dominik Ermel 2021-01-27 09:19:25 +00:00 committed by Anas Nashif
commit 40572fde20
7 changed files with 45 additions and 6 deletions

View file

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

View file

@ -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
*/

View file

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

View file

@ -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),

View file

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

View file

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

View file

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