-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvfs_file_demo.c
More file actions
243 lines (216 loc) · 8.31 KB
/
Copy pathvfs_file_demo.c
File metadata and controls
243 lines (216 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*****************************************************************/ /**
* @file vfs_file_demo.c
* @brief VFS file system demonstration
* @author larson.li@quectel.com
* @date 2023-06-07
*
* @copyright Copyright (c) 2023 Quectel Wireless Solution, Co., Ltd.
* All Rights Reserved. Quectel Wireless Solution Proprietary and Confidential.
*
* @par EDIT HISTORY FOR MODULE
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description"
* <tr><td>2023-06-07 <td>1.0 <td>Larson.Li <td> Initial version
* </table>
**********************************************************************/
#include "qosa_log.h"
#include "qosa_def.h"
#include "qosa_sys.h"
#include "qosa_virtual_file.h"
#include "unirtos_app_init_registry.h"
#define QOS_LOG_TAG LOG_TAG
#define UNIR_VFS_DEMO_TASK_STACK_SIZE 4096
/**
* @brief List all files and subdirectories in the specified directory and output detailed information.
*
* This function opens the directory at the specified path and iterates through each directory entry.
* For regular files, it outputs their name, size, and permission mode; for subdirectories,
* it outputs their name and total size.
*
* @param file_path Pointer to a null-terminated string representing the directory path to list contents from.
*/
static void unir_vfs_demo_dir_list(char *file_path)
{
QOSA_VFS_DIR *dir = QOSA_NULL; // Directory handle
struct qosa_vfs_dirent_t *entry = QOSA_NULL; // Directory entry information storage
struct qosa_vfs_stat_t st = {0}; // File status information storage
qosa_int64_t size = 0; // Directory total size storage
qosa_int32_t ret = 0; // Function return value
char child[QOSA_VFS_PATH_MAX] = {0}; // Subdirectory absolute path
// Open test directory "./testdir"
dir = qosa_vfs_opendir(file_path);
if (dir == QOSA_NULL)
{
QLOGE("dir open err=%d", qosa_get_errno());
return;
}
// Loop through each item in the directory
while ((entry = qosa_vfs_readdir(dir)) != QOSA_NULL)
{
// Output the name of the current directory entry
QLOGV("%s\n", entry->d_name);
// Check if it's a regular file
if (entry->d_type == QOSA_VFS_DT_REG)
{
char path[1024] = {0};
// Concatenate the complete file path
qosa_snprintf(path, sizeof(path), "./%s", entry->d_name);
// Get file status information
if (qosa_vfs_stat(path, &st) == -1)
{
QLOGV("stat");
}
else
{
QLOGV("File name: %s", entry->d_name);
QLOGV("File size: %ld bytes", (long)st.st_size);
QLOGV("File mode: %o", (int)st.st_mode);
}
}
// Check if it's a subdirectory
else if (entry->d_type == QOSA_VFS_DT_DIR)
{
// Complete subdirectory to absolute path
qosa_memset(child, 0, sizeof(child));
qosa_snprintf(child, sizeof(child), "%s/%s", file_path, entry->d_name);
// Get the total size of the subdirectory
size = qosa_vfs_dir_total_size(child);
QLOGV("Dir name: [%s],[%s]", entry->d_name, child);
qosa_int32_t high = 0;
qosa_int32_t low = 0;
high = ((size >> 32) & 0xFFFFFFFF);
low = (size & 0xFFFFFFFF);
// eigen doesn't support directly printing 64-bit types, so split into high and low 32-bit parts for printing
QLOGV("Dir size: %d,%d bytes", high, low);
}
}
ret = qosa_vfs_closedir(dir);
QLOGD("close ret=%d", ret);
}
/**
* @brief Test directory operation functions, including opening directories, reading directory contents,
* getting file information, and deleting directories.
*
* This function demonstrates how to use the VFS interface to traverse files and subdirectories
* in the specified directory ("./testdir"), and output each entry's name, type, size, and other information.
* Finally, it closes and deletes the test directory.
*
* @note This function is static and only visible within the current file.
* @note If directory opening fails, it returns directly.
* @note During traversal, it distinguishes between regular files and subdirectories and handles them separately.
* @note Finally, it attempts to close and remove the test directory.
*/
static void unir_vfs_demo_dir_test(void)
{
QOSA_VFS_DIR *dir = QOSA_NULL; // Directory handle
qosa_int32_t ret = 0; // Function return value
QLOGV("test dir");
// Open test directory "./testdir"
dir = qosa_vfs_opendir("./testdir");
if (dir == QOSA_NULL)
{
QLOGE("dir not exist");
// Create test directory
ret = qosa_vfs_mkdir("./testdir", 0);
if (ret != 0)
{
QLOGE("mkdir err=%d", qosa_get_errno());
return;
}
}
// Create test files and directories
ret = qosa_vfs_creat("./testdir/vfs_test1.txt", 0);
if (ret < 0)
{
QLOGE("creat file err=%d", qosa_get_errno());
}
ret = qosa_vfs_creat("./testdir/vfs_test2.txt", 0);
if (ret < 0)
{
QLOGE("creat file2 err=%d", qosa_get_errno());
}
ret = qosa_vfs_mkdir("./testdir/subdir", 0);
if (ret != 0)
{
QLOGE("subdir mkdir err=%d", qosa_get_errno());
}
// List all files and subdirectories in the test directory
unir_vfs_demo_dir_list("./testdir");
// Open test directory "./testdir"
dir = qosa_vfs_opendir("./testdir");
if (dir == QOSA_NULL)
{
QLOGE("dir open err=%d", qosa_get_errno());
return;
}
// Close directory and output result
ret = qosa_vfs_closedir(dir);
QLOGD("close dir ret=%d", ret);
// Delete test directory and output result, this API can only delete empty folders, here it should report an error
ret = qosa_vfs_rmdir("./testdir");
QLOGD("remove dir ret=%d", ret);
// Delete test directory and all files under the test directory
ret = qosa_vfs_rmdir_recursive("./testdir");
QLOGD("remove dir ret=%d", ret);
}
/**
* @brief VFS file operation test function
* @details This function demonstrates the complete operation flow of the VFS virtual file system,
* including file creation, writing, reading, status query, and other operations
* @param None
* @return None
*/
static void unir_vfs_demo_file_test(void)
{
int fd = 0;
int ret = 0;
char data[10 + 1] = {0};
struct qosa_vfs_stat_t stat = {0};
QLOGV("test file");
/* Open or create test file, supporting read-write mode */
fd = qosa_vfs_open("./vfs_test.txt", QOSA_VFS_O_CREAT | QOSA_VFS_O_RDWR);
if (fd < 0)
{
QLOGE("open dir error!!");
return;
}
/* Write test data to file */
qosa_snprintf(data, 10, "%s", "1234567890");
ret = qosa_vfs_write(fd, data, 10);
QLOGD("write ret=%d", ret);
/* Get file status information */
ret = qosa_vfs_fstat(fd, &stat);
if (ret == 0)
{
/* Print file size */
QLOGD("size=%d", stat.st_size);
}
/* Seek file pointer to offset 0 from beginning of file */
qosa_vfs_lseek(fd, 0, QOSA_VFS_SEEK_SET);
/* Read data from file and display */
qosa_memset(data, 0, sizeof(data));
ret = qosa_vfs_read(fd, data, 10);
QLOGD("read ret=%d,data=[%s]", ret, data);
/* Close file and delete test file */
qosa_vfs_close(fd);
qosa_vfs_unlink("./vfs_test.txt");
}
static void unir_vfs_task_handler(void *argv)
{
QOSA_UNUSED(argv);
qosa_task_sleep_sec(10);
unir_vfs_demo_file_test();
unir_vfs_demo_dir_test();
}
void unir_vfs_demo_init(void)
{
int err = 0;
qosa_task_t vfs_task = QOSA_NULL;
err = qosa_task_create(&vfs_task, UNIR_VFS_DEMO_TASK_STACK_SIZE, QOSA_PRIORITY_NORMAL, "vfs_demo", unir_vfs_task_handler, QOSA_NULL);
if (err != QOSA_OK)
{
QLOGE("task create error");
return;
}
}
UNIRTOS_APP_EXPORT(320, "vfs_demo", unir_vfs_demo_init);