1 /*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /*
8 * @filesystem
9 * @brief test_filesystem
10 * Demonstrates the ZEPHYR File System APIs
11 */
12
13 #include "test_fat.h"
14 #include <stdio.h>
15
16 extern int test_file_write(void);
17 extern int test_file_close(void);
18 static int test_rmdir(const char *dir);
19
test_mkdir(const char * dir,const char * file)20 static int test_mkdir(const char *dir, const char *file)
21 {
22 int res;
23
24 TC_PRINT("mkdir tests:\n");
25
26 if (check_file_dir_exists(dir)) {
27 TC_PRINT("[%s] exists, delete it\n", dir);
28 if (test_rmdir(dir)) {
29 TC_PRINT("Error deleting dir %s\n", dir);
30 return TC_FAIL;
31 }
32 } else {
33 TC_PRINT("Creating new dir %s\n", dir);
34 }
35
36 /* Verify fs_mkdir() */
37 res = fs_mkdir(dir);
38 if (res) {
39 TC_PRINT("Error creating dir[%d]\n", res);
40 return res;
41 }
42
43 res = fs_open(&filep, file, FS_O_CREATE | FS_O_RDWR);
44 if (res) {
45 TC_PRINT("Failed opening file [%d]\n", res);
46 return res;
47 }
48
49 res = test_file_write();
50 if (res) {
51 return res;
52 }
53
54 res = fs_close(&filep);
55 if (res) {
56 TC_PRINT("Error closing file [%d]\n", res);
57 return res;
58 }
59
60 TC_PRINT("Created dir %s!\n", dir);
61
62 return res;
63 }
64
test_lsdir(const char * path)65 static int test_lsdir(const char *path)
66 {
67 int res;
68 struct fs_dir_t dirp;
69 struct fs_dirent entry;
70
71 TC_PRINT("lsdir tests:\n");
72
73 fs_dir_t_init(&dirp);
74
75 /* Verify fs_opendir() */
76 res = fs_opendir(&dirp, path);
77 if (res) {
78 TC_PRINT("Error opening dir %s [%d]\n", path, res);
79 return res;
80 }
81
82 TC_PRINT("Listing dir %s:\n", path);
83 for (;;) {
84 /* Verify fs_readdir() */
85 res = fs_readdir(&dirp, &entry);
86
87 /* entry.name[0] == 0 means end-of-dir */
88 if (res || entry.name[0] == 0) {
89 break;
90 }
91
92 if (entry.type == FS_DIR_ENTRY_DIR) {
93 TC_PRINT("[DIR ] %s\n", entry.name);
94 } else {
95 TC_PRINT("[FILE] %s (size = %zu)\n",
96 entry.name, entry.size);
97 }
98 }
99
100 /* Verify fs_closedir() */
101 fs_closedir(&dirp);
102
103 return res;
104 }
105
test_rmdir(const char * dir)106 static int test_rmdir(const char *dir)
107 {
108 int res;
109 struct fs_dir_t dirp;
110 static struct fs_dirent entry;
111 char file_path[80];
112
113 TC_PRINT("rmdir tests:\n");
114
115 fs_dir_t_init(&dirp);
116
117 if (!check_file_dir_exists(dir)) {
118 TC_PRINT("%s doesn't exist\n", dir);
119 return TC_FAIL;
120 }
121
122 res = fs_opendir(&dirp, dir);
123 if (res) {
124 TC_PRINT("Error opening dir[%d]\n", res);
125 return res;
126 }
127
128 TC_PRINT("Removing files and sub directories in %s\n", dir);
129 for (;;) {
130 res = fs_readdir(&dirp, &entry);
131
132 /* entry.name[0] == 0 means end-of-dir */
133 if (res || entry.name[0] == 0) {
134 break;
135 }
136
137 /* Delete file or sub directory */
138 sprintf(file_path, "%s/%s", dir, entry.name);
139 TC_PRINT("Removing %s\n", file_path);
140
141 res = fs_unlink(file_path);
142 if (res) {
143 TC_PRINT("Error deleting file/dir [%d]\n", res);
144 fs_closedir(&dirp);
145 return res;
146 }
147 }
148
149 fs_closedir(&dirp);
150
151 /* Verify fs_unlink() */
152 res = fs_unlink(dir);
153 if (res) {
154 TC_PRINT("Error removing dir [%d]\n", res);
155 return res;
156 }
157
158 TC_PRINT("Removed dir %s!\n", dir);
159
160 return res;
161 }
162
ZTEST(fat_fs_dual_drive,test_fat_dir)163 ZTEST(fat_fs_dual_drive, test_fat_dir)
164 {
165 TC_PRINT("\nTesting directory operations on %s\n", FATFS_MNTP);
166 zassert_true(test_mkdir(TEST_DIR, TEST_DIR_FILE) == TC_PASS);
167 zassert_true(test_lsdir(FATFS_MNTP) == TC_PASS);
168 zassert_true(test_lsdir(TEST_DIR) == TC_PASS);
169 zassert_true(test_rmdir(TEST_DIR) == TC_PASS);
170 zassert_true(test_lsdir(FATFS_MNTP) == TC_PASS);
171
172 TC_PRINT("\nTesting directory operations on %s\n", FATFS_MNTP1);
173 zassert_true(test_mkdir(TEST_DIR1, TEST_DIR_FILE1) == TC_PASS);
174 zassert_true(test_lsdir(FATFS_MNTP1) == TC_PASS);
175 zassert_true(test_lsdir(TEST_DIR1) == TC_PASS);
176 zassert_true(test_rmdir(TEST_DIR1) == TC_PASS);
177 zassert_true(test_lsdir(FATFS_MNTP1) == TC_PASS);
178 }
179