• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., 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 #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      32
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     printf("[HalFileOpen] *********** path: %s\n", path);
98     if (strlen(path) >= MAX_PATH_LEN) {
99         LOG_E("path name is too long!!!\n");
100         return -1;
101     }
102 
103     fd = Find_Free_Num();
104     if (fd == 0) {
105         LOG_E("NO enougn file Space!!!\n");
106         return -1;
107     }
108 
109     path_len = strlen(path) + strlen(ROOT_PATH) + ROOT_LEN;
110     file_path = (char *)malloc(path_len);
111     if (file_path == NULL) {
112         LOG_E("malloc path name buffer failed!\n");
113         return -1;
114     }
115     printf("[HalFileOpen] *********** file_path: %s\n", file_path);
116     strcpy_s(file_path, path_len, ROOT_PATH);
117     if (strcat_s(file_path, path_len, "/") != 0) {
118         return -1;
119     }
120     if (strcat_s(file_path, path_len, path) != 0) {
121         return -1;
122     }
123 
124     printf("[HalFileOpen] *********** 111 file_path: %s\n", file_path);
125     int fs_fd = open(file_path, ReadModeChange(oflag));
126     if (fs_fd < 0) {
127         LOG_E("open file '%s' failed, %s\r\n", file_path, strerror(errno));
128         free(file_path);
129         return -1;
130     }
131 
132     File[fd - 1].fd = 1;
133     File[fd - 1].fs_fd = fs_fd;
134     free(file_path);
135 
136     return fd;
137 }
138 
HalFileClose(int fd)139 int HalFileClose(int fd)
140 {
141     int ret;
142 
143     if ((fd > MaxOpenFile) || (fd <= 0)) {
144         return -1;
145     }
146 
147     ret = close(File[fd - 1].fs_fd);
148     if (ret != 0) {
149         return -1;
150     }
151 
152     File[fd - 1].fd = 0;
153     File[fd - 1].fs_fd = -1;
154 
155     return ret;
156 }
157 
HalFileRead(int fd,char * buf,unsigned int len)158 int HalFileRead(int fd, char *buf, unsigned int len)
159 {
160     if ((fd > MaxOpenFile) || (fd <= 0)) {
161         return -1;
162     }
163 
164     return read(File[fd - 1].fs_fd, buf, len);
165 }
166 
HalFileWrite(int fd,const char * buf,unsigned int len)167 int HalFileWrite(int fd, const char *buf, unsigned int len)
168 {
169     if ((fd > MaxOpenFile) || (fd <= 0)) {
170         return -1;
171     }
172 
173     return write(File[fd - 1].fs_fd, buf, len);
174 }
175 
HalFileDelete(const char * path)176 int HalFileDelete(const char *path)
177 {
178     char *file_path;
179     uint16_t path_len;
180 
181     if (strlen(path) >= MAX_PATH_LEN) {
182         LOG_E("path name is too long!!!\n");
183         return -1;
184     }
185 
186     path_len = strlen(path) + strlen(ROOT_PATH) + ROOT_LEN;
187     file_path = (char *)malloc(path_len);
188     if (file_path == NULL) {
189         LOG_E("malloc path name buffer failed!\n");
190         return -1;
191     }
192 
193     strcpy_s(file_path, path_len, ROOT_PATH);
194     if (strcat_s(file_path, path_len, "/") != 0) {
195         return -1;
196     }
197     if (strcat_s(file_path, path_len, path) != 0) {
198         return -1;
199     }
200 
201     int ret = unlink(file_path);
202     free(file_path);
203 
204     return ret;
205 }
206 
HalFileStat(const char * path,unsigned int * fileSize)207 int HalFileStat(const char *path, unsigned int *fileSize)
208 {
209     char *file_path;
210     struct stat f_info;
211     uint16_t path_len;
212 
213     if (strlen(path) >= MAX_PATH_LEN) {
214         LOG_E("path name is too long!!!\n");
215         return -1;
216     }
217 
218     path_len = strlen(path) + strlen(ROOT_PATH) + ROOT_LEN;
219     file_path = (char *)malloc(path_len);
220     if (file_path == NULL) {
221         LOG_E("malloc path name buffer failed!\n");
222         return -1;
223     }
224     strcpy_s(file_path, path_len, ROOT_PATH);
225     if (strcat_s(file_path, path_len, "/") != 0) {
226         return -1;
227     }
228     if (strcat_s(file_path, path_len, path) != 0) {
229         return -1;
230     }
231 
232     int ret = stat(file_path, &f_info);
233     *fileSize = f_info.st_size;
234     free(file_path);
235 
236     return ((ret == 0) ? 0 : -1);
237 }
238 
HalFileSeek(int fd,int offset,unsigned int whence)239 int HalFileSeek(int fd, int offset, unsigned int whence)
240 {
241     int ret = 0;
242     struct stat f_info;
243 
244     if ((fd > MaxOpenFile) || (fd <= 0)) {
245         return -1;
246     }
247 
248     ret = fstat(File[fd - 1].fs_fd, &f_info);
249     if (ret != 0) {
250         return -1;
251     }
252 
253     if (whence == SEEK_SET_FS) {
254         if (offset > f_info.st_size) {
255             ret = -1;
256         }
257     }
258 
259     ret = lseek(File[fd - 1].fs_fd, offset, whence);
260     if ((ret >  f_info.st_size) || (ret < 0)) {
261         return -1;
262     }
263 
264     return ret;
265 }
266