1 /*
2 * Copyright (c) 2021 GOODIX.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <dirent.h>
16 #include <errno.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #define LOG_TAG "FS"
26 #include "log.h"
27 #include "ohos_init.h"
28 #include "cmsis_os2.h"
29
30 #define FS_TASK_STACK_SIZE 4096
31 #define FS_TASK_PRIO 25
32 #define MS_1000 1000
33 #define PRINTF_ERR (-1)
34
35 #define LOG_E(fmt, ...) HILOG_ERROR(HILOG_MODULE_APP, fmt, ##__VA_ARGS__)
36 #define LOG_I(fmt, ...) HILOG_INFO(HILOG_MODULE_APP, fmt, ##__VA_ARGS__)
37
dir_test(const char * path)38 static void dir_test(const char *path)
39 {
40 DIR *dir;
41 struct dirent *dp;
42 int ret;
43 struct stat st_buf = {0};
44 char realpath[128];
45
46 ret = 0;
47 if ((dir = opendir(path)) == NULL) {
48 LOG_E("opendir %s failed, %s\n", path, strerror(errno));
49 return;
50 }
51 while ((dp = readdir(dir)) != NULL) {
52 if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
53 continue;
54 }
55
56 ret = snprintf_s(realpath, sizeof(realpath), sizeof(realpath), "%s/%s", path, dp->d_name);
57 if (ret == PRINTF_ERR) {
58 printf("snprintf_s faiked, err = %d", ret);
59 return; // 返回失败
60 }
61
62 if (stat(realpath, &st_buf) != 0) {
63 LOG_E("can not access %s\n", realpath);
64 closedir(dir);
65 return;
66 }
67 if ((st_buf.st_mode & S_IFMT) == S_IFDIR) {
68 LOG_E("DIR %s\n", realpath);
69 } else {
70 LOG_I("FILE %s, %ld bytes\n", realpath, st_buf.st_size);
71 }
72 }
73 closedir(dir);
74 }
75
read_file(const char * file,bool print_str)76 static void read_file(const char *file, bool print_str)
77 {
78 int bytes = 0;
79 char buf[513];
80 int ret = 0;
81
82 int fd = open(file, O_RDONLY);
83 if (fd < 0) {
84 LOG_E("open file '%s' failed, %s\r\n", file, strerror(errno));
85 return;
86 }
87
88 while (1) {
89 ret = memset_s(buf, sizeof(buf), 0, sizeof(buf));
90 if (ret < 0) {
91 return;
92 }
93 int rc = read(fd, buf, sizeof(buf) - 1);
94 if (rc > 0) {
95 bytes += rc;
96 }
97
98 if (print_str) {
99 buf[rc] = '\0';
100 LOG_I("%s", buf);
101 }
102
103 if (rc < sizeof(buf) - 1) {
104 break;
105 }
106 }
107 close(fd);
108 LOG_I("read file '%s' total bytes: %d\r\n", file, bytes);
109 }
110
write_file(const char * file,const char * data)111 static void write_file(const char *file, const char *data)
112 {
113 int fd = open(file, O_RDWR | O_CREAT);
114 if (fd < 0) {
115 LOG_E("fopen file '%s' failed, %s\r\n", file, strerror(errno));
116 return;
117 }
118
119 int bytes = write(fd, data, strlen(data));
120 close(fd);
121 LOG_I("fwrite file '%s' total bytes: %d, %s\r\n", file, bytes, data);
122 }
123
fs_test(void)124 void fs_test(void)
125 {
126 dir_test("/data");
127 write_file("/data/test.txt", "fs write data test.\n");
128 read_file("/data/test.txt", true);
129 }
130
FsTestTask(const char * arg)131 static void *FsTestTask(const char *arg)
132 {
133 (void)arg;
134
135 printf("FS task.\r\n");
136
137 while (1) {
138 fs_test();
139 osDelay(MS_1000);
140 }
141 }
142
FsTaskEntry(void)143 void FsTaskEntry(void)
144 {
145 osThreadAttr_t attr;
146
147 attr.name = "FsTestTask";
148 attr.attr_bits = 0U;
149 attr.cb_mem = NULL;
150 attr.cb_size = 0U;
151 attr.stack_mem = NULL;
152 attr.stack_size = FS_TASK_STACK_SIZE;
153 attr.priority = FS_TASK_PRIO;
154
155 if (osThreadNew((osThreadFunc_t)FsTestTask, NULL, &attr) == NULL) {
156 printf("Failed to create FsTestTask!\n");
157 }
158 }
159
160 SYS_RUN(FsTaskEntry);
161
162