1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 #define _GNU_SOURCE
16 #include <dirent.h>
17 #include <errno.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #define LOG_TAG "FS"
27 #include "log.h"
28
29 #define LOG_E(fmt, ...) HILOG_ERROR(HILOG_MODULE_APP, fmt, ##__VA_ARGS__)
30 #define LOG_I(fmt, ...) HILOG_INFO(HILOG_MODULE_APP, fmt, ##__VA_ARGS__)
31
dir_test(const char * path)32 static void dir_test(const char *path)
33 {
34 DIR *dir;
35 struct dirent *dp;
36 if ((dir = opendir(path)) == NULL) {
37 LOG_E("opendir %s failed, %s\n", path, strerror(errno));
38 return;
39 }
40 while ((dp = readdir(dir)) != NULL) {
41 if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) {
42 continue;
43 }
44 struct stat st_buf = {0};
45 char realpath[260];
46 snprintf(realpath, sizeof(realpath), "%s/%s", path, dp->d_name);
47 if (stat(realpath, &st_buf) != 0) {
48 LOG_E("can not access %s\n", realpath);
49 closedir(dir);
50 return;
51 }
52 if ((st_buf.st_mode & S_IFMT) == S_IFDIR) {
53 LOG_I("DIR %s\n", realpath);
54 } else {
55 LOG_I("FILE %s, %ld bytes\n", realpath, st_buf.st_size);
56 }
57 }
58 closedir(dir);
59 }
60
read_test(const char * file,bool print_str)61 static void read_test(const char *file, bool print_str)
62 {
63 int fd = open(file, O_RDONLY);
64 if (fd < 0) {
65 LOG_E("open file '%s' failed, %s\r\n", file, strerror(errno));
66 return;
67 }
68 int bytes = 0;
69 char buf[513];
70 while (1) {
71 memset(buf, 0, sizeof(buf));
72 int rc = read(fd, buf, sizeof(buf) - 1);
73 if (rc > 0)
74 bytes += rc;
75
76 if (print_str) {
77 buf[rc] = '\0';
78 LOG_I("%s", buf);
79 }
80
81 if (rc < sizeof(buf) - 1)
82 break;
83 }
84 close(fd);
85 LOG_I("read file '%s' total bytes: %d\r\n", file, bytes);
86 }
87
fread_test(const char * file,bool print_str)88 static void fread_test(const char *file, bool print_str)
89 {
90 FILE *fp = fopen(file, "rb");
91 if (fp == NULL) {
92 LOG_E("fopen file '%s' failed, %s\r\n", file, strerror(errno));
93 return;
94 }
95 int bytes = 0;
96 char buf[513];
97 while (1) {
98 memset(buf, 0, sizeof(buf));
99 int rc = fread(buf, 1, sizeof(buf) - 1, fp);
100 if (rc > 0)
101 bytes += rc;
102
103 if (print_str) {
104 buf[rc] = '\0';
105 LOG_I("%s", buf);
106 }
107 if (rc < sizeof(buf) - 1)
108 break;
109 }
110 fclose(fp);
111 LOG_I("fread file '%s' total bytes: %d\r\n", file, bytes);
112 }
113
fwrite_test(const char * file,const char * data)114 static void fwrite_test(const char *file, const char *data)
115 {
116 FILE *fp = fopen(file, "w");
117 if (fp == NULL) {
118 LOG_E("fopen file '%s' failed, %s\r\n", file, strerror(errno));
119 return;
120 }
121 int bytes = fwrite(data, 1, strlen(data), fp);
122 fclose(fp);
123 LOG_I("fwrite file '%s' total bytes: %d, %s\r\n", file, bytes, data);
124 }
125
fstat_test(const char * path)126 static void fstat_test(const char *path)
127 {
128 int fd = open(path, O_RDONLY);
129 if (fd < 0) {
130 LOG_E("open file '%s' failed, %s\r\n", path, strerror(errno));
131 return;
132 }
133 struct stat st_buf = {0};
134 if (fstat(fd, &st_buf) != 0) {
135 LOG_E("can not access %s\n", path);
136 close(fd);
137 return;
138 }
139 close(fd);
140 if ((st_buf.st_mode & S_IFMT) == S_IFDIR) {
141 LOG_I("DIR %s\n", path);
142 } else {
143 LOG_I("FILE %s, %ld bytes\n", path, st_buf.st_size);
144 }
145 }
146
fseek_test(const char * path)147 static void fseek_test(const char *path)
148 {
149 FILE *fp = fopen(path, "rb");
150 if (fp == NULL) {
151 LOG_E("fopen file '%s' failed, %s\r\n", path, strerror(errno));
152 return;
153 }
154 fseek(fp, 0, SEEK_END);
155 uint32_t len = ftell(fp);
156 LOG_I("%s size %u bytes", path, len);
157 fclose(fp);
158 }
159
fs_test(void)160 void fs_test(void)
161 {
162 dir_test("/data");
163 read_test("/data/test.txt", true);
164 fwrite_test("/data/test.txt", "fwrite data test");
165 fread_test("/data/test.txt", true);
166 fstat_test("/data/font.ttf");
167 fseek_test("/data/font.ttf");
168 }
169