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