• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <errno.h>
16 #include <fcntl.h>
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include "utils_file.h"
25 #include "log.h"
26 #include "hal_file.h"
27 
28 #define LOG_E(fmt, ...)  HILOG_ERROR(HILOG_MODULE_APP, fmt, ##__VA_ARGS__)
29 #define LOG_I(fmt, ...)  HILOG_INFO(HILOG_MODULE_APP, fmt, ##__VA_ARGS__)
30 
31 #define ROOT_LEN         2
32 #define MAX_PATH_LEN     40
33 #define MaxOpenFile      33
34 #define ROOT_PATH        "/data"
35 
36 typedef struct _File_Context {
37     int fs_fd;
38     unsigned char fd;
39 } File_Context;
40 
41 static File_Context File[MaxOpenFile] = { 0 };
42 
Find_Free_Num(void)43 int Find_Free_Num(void)
44 {
45     int i = MaxOpenFile;
46     for (; i > 0; i--) {
47         if (File[i - 1].fd == 0) {
48             break;
49         }
50     }
51 
52     return i;
53 }
54 
55 
ReadModeChange(int oflag)56 int ReadModeChange(int oflag)
57 {
58     int ret = 0;
59     int buffer = 0;
60 
61     buffer = (oflag & 0x000f);
62     if (buffer == O_RDONLY_FS) {
63         ret = O_RDONLY;
64     } else if (buffer == O_WRONLY_FS) {
65         ret = O_WRONLY;
66     } else if (buffer == O_RDWR_FS) {
67         ret = O_RDWR;
68     }
69 
70     buffer = (oflag & 0x00f0);
71     if ((buffer & 0x0040) != 0) {
72         ret |= O_CREAT;
73     }
74 
75     if ((buffer & 0x0080) != 0) {
76         ret |= O_EXCL;
77     }
78 
79     buffer = (oflag & 0x0f00);
80     if ((buffer & 0x0200) != 0) {
81         ret |= O_TRUNC;
82     }
83 
84     if ((buffer & 0x0400) != 0) {
85         ret |= O_APPEND;
86     }
87 
88     return ret;
89 }
90 
HalFileOpen(const char * path,int oflag,int mode)91 int HalFileOpen(const char *path, int oflag, int mode)
92 {
93     char *file_path;
94     int fd;
95     uint16_t path_len;
96 
97     if (strlen(path) >= MAX_PATH_LEN) {
98         LOG_E("path name is too long!!!\n");
99         return -1;
100     }
101 
102     fd = Find_Free_Num();
103     if (fd == 0) {
104         LOG_E("NO enougn file Space!!!\n");
105         return -1;
106     }
107 
108     path_len = strlen(path) + strlen(ROOT_PATH) + ROOT_LEN;
109     file_path = (char *)malloc(path_len);
110     if (file_path == NULL) {
111         LOG_E("malloc path name buffer failed!\n");
112         return -1;
113     }
114     strcpy_s(file_path, path_len, ROOT_PATH);
115     if (strcat_s(file_path, path_len, "/") != 0) {
116         return -1;
117     }
118     if (strcat_s(file_path, path_len, path) != 0) {
119         return -1;
120     }
121 
122     int fs_fd = open(file_path, ReadModeChange(oflag));
123     if (fs_fd < 0) {
124         LOG_E("open file '%s' failed, %s\r\n", file_path, strerror(errno));
125         free(file_path);
126         return -1;
127     }
128 
129     File[fd - 1].fd = 1;
130     File[fd - 1].fs_fd = fs_fd;
131     free(file_path);
132 
133     return fd;
134 }
135 
HalFileClose(int fd)136 int HalFileClose(int fd)
137 {
138     int ret;
139 
140     if ((fd > MaxOpenFile) || (fd <= 0)) {
141         return -1;
142     }
143 
144     ret = close(File[fd - 1].fs_fd);
145     if (ret != 0) {
146         return -1;
147     }
148 
149     File[fd - 1].fd = 0;
150     File[fd - 1].fs_fd = -1;
151 
152     return ret;
153 }
154 
HalFileRead(int fd,char * buf,unsigned int len)155 int HalFileRead(int fd, char *buf, unsigned int len)
156 {
157     if ((fd > MaxOpenFile) || (fd <= 0)) {
158         return -1;
159     }
160 
161     return read(File[fd - 1].fs_fd, buf, len);
162 }
163 
HalFileWrite(int fd,const char * buf,unsigned int len)164 int HalFileWrite(int fd, const char *buf, unsigned int len)
165 {
166     if ((fd > MaxOpenFile) || (fd <= 0)) {
167         return -1;
168     }
169 
170     return write(File[fd - 1].fs_fd, buf, len);
171 }
172 
HalFileDelete(const char * path)173 int HalFileDelete(const char *path)
174 {
175     char *file_path;
176     uint16_t path_len;
177 
178     if (strlen(path) >= MAX_PATH_LEN) {
179         LOG_E("path name is too long!!!\n");
180         return -1;
181     }
182 
183     path_len = strlen(path) + strlen(ROOT_PATH) + ROOT_LEN;
184     file_path = (char *)malloc(path_len);
185     if (file_path == NULL) {
186         LOG_E("malloc path name buffer failed!\n");
187         return -1;
188     }
189 
190     strcpy_s(file_path, path_len, ROOT_PATH);
191     if (strcat_s(file_path, path_len, "/") != 0) {
192         return -1;
193     }
194     if (strcat_s(file_path, path_len, path) != 0) {
195         return -1;
196     }
197 
198     int ret = unlink(file_path);
199     free(file_path);
200 
201     return ret;
202 }
203 
HalFileStat(const char * path,unsigned int * fileSize)204 int HalFileStat(const char *path, unsigned int *fileSize)
205 {
206     char *file_path;
207     struct stat f_info;
208     uint16_t path_len;
209 
210     if (strlen(path) >= MAX_PATH_LEN) {
211         LOG_E("path name is too long!!!\n");
212         return -1;
213     }
214 
215     path_len = strlen(path) + strlen(ROOT_PATH) + ROOT_LEN;
216     file_path = (char *)malloc(path_len);
217     if (file_path == NULL) {
218         LOG_E("malloc path name buffer failed!\n");
219         return -1;
220     }
221     strcpy_s(file_path, path_len, ROOT_PATH);
222     if (strcat_s(file_path, path_len, "/") != 0) {
223         return -1;
224     }
225     if (strcat_s(file_path, path_len, path) != 0) {
226         return -1;
227     }
228 
229     int ret = stat(file_path, &f_info);
230     *fileSize = f_info.st_size;
231     free(file_path);
232 
233     return ((ret == 0) ? 0 : -1);
234 }
235 
HalFileSeek(int fd,int offset,unsigned int whence)236 int HalFileSeek(int fd, int offset, unsigned int whence)
237 {
238     int ret = 0;
239     struct stat f_info;
240 
241     if ((fd > MaxOpenFile) || (fd <= 0)) {
242         return -1;
243     }
244 
245     ret = fstat(File[fd - 1].fs_fd, &f_info);
246     if (ret != 0) {
247         return -1;
248     }
249 
250     if (whence == SEEK_SET_FS) {
251         if (offset > f_info.st_size) {
252             ret = -1;
253         }
254     }
255 
256     ret = lseek(File[fd - 1].fs_fd, offset, whence);
257     if ((ret >  f_info.st_size) || (ret < 0)) {
258         return -1;
259     }
260 
261     return ret;
262 }
263