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:
parent
96c7852318
commit
40572fde20
7 changed files with 45 additions and 6 deletions
|
@ -220,6 +220,20 @@ struct fs_statvfs {
|
||||||
#define FS_FSTAB_DECLARE_ENTRY(node_id) \
|
#define FS_FSTAB_DECLARE_ENTRY(node_id) \
|
||||||
extern struct fs_mount_t FS_FSTAB_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
|
* @brief Open or create file
|
||||||
*
|
*
|
||||||
|
|
|
@ -45,6 +45,8 @@ struct fs_mount_t;
|
||||||
/**
|
/**
|
||||||
* @brief File object representing an open file
|
* @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 Pointer to FATFS file object structure
|
||||||
* @param mp Pointer to mount point structure
|
* @param mp Pointer to mount point structure
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -141,6 +141,8 @@ void test_fs_open_flags(void)
|
||||||
};
|
};
|
||||||
int block = 1;
|
int block = 1;
|
||||||
|
|
||||||
|
fs_file_t_init(&ts.file);
|
||||||
|
|
||||||
ZBEGIN("Attempt open non-existent");
|
ZBEGIN("Attempt open non-existent");
|
||||||
ZOPEN(&ts, 0, -ENOENT);
|
ZOPEN(&ts, 0, -ENOENT);
|
||||||
ZOPEN(&ts, FS_O_WRITE, -ENOENT);
|
ZOPEN(&ts, FS_O_WRITE, -ENOENT);
|
||||||
|
|
|
@ -72,6 +72,7 @@ void test_main(void)
|
||||||
ztest_unit_test_setup_teardown(test_mount,
|
ztest_unit_test_setup_teardown(test_mount,
|
||||||
fs_setup,
|
fs_setup,
|
||||||
dummy_teardown),
|
dummy_teardown),
|
||||||
|
ztest_unit_test(test_fs_file_t_init),
|
||||||
ztest_unit_test(test_file_statvfs),
|
ztest_unit_test(test_file_statvfs),
|
||||||
ztest_unit_test(test_mkdir),
|
ztest_unit_test(test_mkdir),
|
||||||
ztest_unit_test(test_opendir),
|
ztest_unit_test(test_opendir),
|
||||||
|
|
|
@ -32,6 +32,7 @@ struct test_fs_data {
|
||||||
int reserve;
|
int reserve;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void test_fs_file_t_init(void);
|
||||||
void test_fs_register(void);
|
void test_fs_register(void);
|
||||||
void test_mount(void);
|
void test_mount(void);
|
||||||
void test_file_statvfs(void);
|
void test_file_statvfs(void);
|
||||||
|
|
|
@ -78,6 +78,21 @@ static struct fs_file_t filep;
|
||||||
static struct fs_file_t err_filep;
|
static struct fs_file_t err_filep;
|
||||||
static const char test_str[] = "hello world!";
|
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
|
* @brief Test mount interface of filesystem
|
||||||
*
|
*
|
||||||
|
@ -396,6 +411,7 @@ void test_file_open(void)
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
TC_PRINT("\nOpen tests:\n");
|
TC_PRINT("\nOpen tests:\n");
|
||||||
|
fs_file_t_init(&filep);
|
||||||
|
|
||||||
TC_PRINT("\nOpen a file without a path\n");
|
TC_PRINT("\nOpen a file without a path\n");
|
||||||
ret = fs_open(&filep, NULL, FS_O_READ);
|
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("\nWrite tests:\n");
|
||||||
|
|
||||||
TC_PRINT("Write to an unopened file\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));
|
brw = fs_write(&err_filep, (char *)test_str, strlen(test_str));
|
||||||
if (brw >= 0) {
|
if (brw >= 0) {
|
||||||
return TC_FAIL;
|
return TC_FAIL;
|
||||||
|
@ -500,7 +516,7 @@ static int _test_file_sync(void)
|
||||||
TC_PRINT("\nSync tests:\n");
|
TC_PRINT("\nSync tests:\n");
|
||||||
|
|
||||||
TC_PRINT("sync an unopened file\n");
|
TC_PRINT("sync an unopened file\n");
|
||||||
err_filep.mp = NULL;
|
fs_file_t_init(&err_filep);
|
||||||
ret = fs_sync(&err_filep);
|
ret = fs_sync(&err_filep);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
return TC_FAIL;
|
return TC_FAIL;
|
||||||
|
@ -513,6 +529,7 @@ static int _test_file_sync(void)
|
||||||
return TC_FAIL;
|
return TC_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fs_file_t_init(&filep);
|
||||||
ret = fs_open(&filep, TEST_FILE, FS_O_RDWR);
|
ret = fs_open(&filep, TEST_FILE, FS_O_RDWR);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
@ -578,7 +595,7 @@ void test_file_read(void)
|
||||||
TC_PRINT("\nRead tests:\n");
|
TC_PRINT("\nRead tests:\n");
|
||||||
|
|
||||||
TC_PRINT("Read an unopened file\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);
|
brw = fs_read(&err_filep, read_buff, sz);
|
||||||
zassert_false(brw >= 0, "Can't read an unopened file");
|
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 tests: max file size is 128byte\n");
|
||||||
|
|
||||||
TC_PRINT("\nTruncate, seek, tell an unopened file\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);
|
ret = fs_truncate(&err_filep, 256);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
return TC_FAIL;
|
return TC_FAIL;
|
||||||
|
@ -798,7 +815,7 @@ void test_file_close(void)
|
||||||
TC_PRINT("\nClose tests:\n");
|
TC_PRINT("\nClose tests:\n");
|
||||||
|
|
||||||
TC_PRINT("Close an unopened file\n");
|
TC_PRINT("Close an unopened file\n");
|
||||||
err_filep.mp = NULL;
|
fs_file_t_init(&err_filep);
|
||||||
ret = fs_close(&err_filep);
|
ret = fs_close(&err_filep);
|
||||||
zassert_equal(ret, 0, "Should close an unopened file");
|
zassert_equal(ret, 0, "Should close an unopened file");
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,9 @@ static struct fs_mount_t mp = {
|
||||||
void test_mount_flags(void)
|
void test_mount_flags(void)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
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 */
|
/* Format volume and add some files/dirs to check read-only flag */
|
||||||
mp.flags = 0;
|
mp.flags = 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue