1 /*
2 * Copyright (c) 2022 Talkweb Co., Ltd.
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 <fcntl.h>
17 #include <stdbool.h>
18 #include <sys/mount.h>
19 #include <sys/stat.h>
20 #include "los_config.h"
21 #include "los_memory.h"
22 #include "los_task.h"
23 #include "ohos_run.h"
24
dir_test(const char * path)25 static void dir_test(const char *path)
26 {
27 DIR *dir;
28 int ret = 0;
29 struct dirent *dp;
30 if ((dir = opendir(path)) == NULL) {
31 printf("opendir %s failed \n", path);
32 return;
33 }
34 while ((dp = readdir(dir)) != NULL) {
35 if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
36 continue;
37 }
38 struct stat st_buf = {0};
39 char realpath[260];
40 ret = snprintf_s(realpath, sizeof(realpath), "%s/%s", path, dp->d_name);
41 if (ret == -1) {
42 printf("snprintf_s error\n");
43 closedir(dir);
44 return;
45 }
46 if (stat(realpath, &st_buf) != 0) {
47 printf("can not access %s\n", realpath);
48 closedir(dir);
49 return;
50 }
51 if ((st_buf.st_mode & S_IFMT) == S_IFDIR) {
52 printf("DIR %s\n", realpath);
53 } else {
54 printf("FILE %s, %ld bytes\n", realpath, st_buf.st_size);
55 }
56 }
57 closedir(dir);
58
59 ret = mkdir("/talkweb/fstestdir", S_IRUSR | S_IWUSR);
60 if (ret) {
61 printf("mkdir failed \n");
62 return;
63 }
64 ret = rmdir("/talkweb/fstestdir");
65 if (ret) {
66 printf("rmdir failed \n");
67 return;
68 }
69 printf("%s ok \n", __func__);
70 }
71
read_test(const char * file,bool print_str)72 static void read_test(const char *file, bool print_str)
73 {
74 int fd = _open(file, O_RDWR | O_CREAT);
75 if (fd < 0) {
76 printf("open file '%s' failed, \r\n", file);
77 return;
78 }
79 int bytes = 0;
80 char buf[513];
81 while (1) {
82 memset_s(buf, sizeof(buf), 0, sizeof(buf));
83 int rc = _read(fd, buf, sizeof(buf) - 1);
84 if (rc > 0) {
85 bytes += rc;
86 }
87
88 if (print_str) {
89 buf[rc] = '\0';
90 printf("%s\r\n", buf);
91 }
92
93 if (rc < sizeof(buf) - 1) {
94 break;
95 }
96 }
97 _close(fd);
98 printf("read file '%s' total bytes: %d\r\n", file, bytes);
99 printf("%s ok \n", __func__);
100 }
101
write_test(const char * file,const char * data)102 static void write_test(const char *file, const char *data)
103 {
104 int fd = _open(file, O_RDWR | O_CREAT);
105 if (fd < 0) {
106 printf("open file '%s' failed \r\n", file);
107 return;
108 }
109 int bytes = _write(fd, data, strlen(data));
110 _close(fd);
111 printf("write file '%s' total bytes: %d, %s\r\n", file, bytes, data);
112 printf("%s ok \n", __func__);
113 }
114
littlefs_test(void)115 void littlefs_test(void)
116 {
117 printf("%s\r\n", __func__);
118 dir_test("/talkweb");
119 read_test("/talkweb/wifi.cfg", true);
120 write_test("/talkweb/wifi.cfg", "ssid:talkweb password:123456");
121 read_test("/talkweb/wifi.cfg", true);
122 }
123 OHOS_APP_RUN(littlefs_test);