1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18
19 #include <securec.h>
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #include <hiview_log.h>
32
33 #include <hal_file.h>
34 #include <utils_file.h>
35
36 #define RD_WR_FIELD_MASK 0x000f
37 #define CREAT_EXCL_FIELD_MASK 0x00f0
38 #define TRUNC_FILED_MASK 0x0f00
39
40 #define ADDITIONAL_LEN 2
41 #define MAX_PATH_LEN 40
42 #define MAX_OPEN_FILE_NUM 32
43 #define ROOT_PATH "/data"
44 #define DIR_SEPARATOR "/"
45
46 #define SLOT_AVAILABLE -1
47
48 #define HAL_ERROR -1
49
50 static int FileHandlerArray[MAX_OPEN_FILE_NUM] = {
51 SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE,
52 SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE,
53 SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE,
54 SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE,
55 SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE, SLOT_AVAILABLE,
56 };
57
GetAvailableFileHandlerIndex(void)58 static int GetAvailableFileHandlerIndex(void)
59 {
60 int i = MAX_OPEN_FILE_NUM;
61
62 for (; i > 0; i--) {
63 if (FileHandlerArray[i - 1] == SLOT_AVAILABLE) {
64 break;
65 }
66 }
67
68 return i;
69 }
ConvertFlags(int oflag)70 static int ConvertFlags(int oflag)
71 {
72 int ret = 0;
73 int buffer = 0;
74
75 buffer = (oflag & RD_WR_FIELD_MASK);
76 if (buffer == O_RDONLY_FS) {
77 ret = O_RDONLY;
78 } else if (buffer == O_WRONLY_FS) {
79 ret = O_WRONLY;
80 } else if (buffer == O_RDWR_FS) {
81 ret = O_RDWR;
82 }
83
84 buffer = (oflag & CREAT_EXCL_FIELD_MASK);
85 if ((buffer & O_CREAT_FS) != 0) {
86 ret |= O_CREAT;
87 }
88
89 if ((buffer & O_EXCL_FS) != 0) {
90 ret |= O_EXCL;
91 }
92
93 buffer = (oflag & TRUNC_FILED_MASK);
94 if ((buffer & O_TRUNC_FS) != 0) {
95 ret |= O_TRUNC;
96 }
97
98 if ((buffer & O_APPEND_FS) != 0) {
99 ret |= O_APPEND;
100 }
101
102 return ret;
103 }
104
GetActualFilePath(const char * path)105 static char *GetActualFilePath(const char *path)
106 {
107 int len;
108 char *file_path = NULL;
109
110 len = strnlen(path, MAX_PATH_LEN);
111 if (len >= MAX_PATH_LEN) {
112 printf("path is too long!\r\n");
113 return NULL;
114 }
115
116 len += (strlen(ROOT_PATH) + ADDITIONAL_LEN);
117 file_path = (char *)malloc(len);
118 if (file_path == NULL) {
119 printf("malloc failed!\r\n");
120 return NULL;
121 }
122
123 strcpy_s(file_path, len, ROOT_PATH);
124 strcat_s(file_path, len, DIR_SEPARATOR);
125 strcat_s(file_path, len, path);
126
127 return file_path;
128 }
129
HalFileOpen(const char * path,int oflag,int mode)130 int HalFileOpen(const char *path, int oflag, int mode)
131 {
132 int index;
133 int fd;
134 char *file_path;
135
136 index = GetAvailableFileHandlerIndex();
137 if (index == 0) {
138 HILOG_ERROR(HILOG_MODULE_HIVIEW, "no space available!");
139 return HAL_ERROR;
140 }
141
142 file_path = GetActualFilePath(path);
143 if (file_path == NULL) {
144 return HAL_ERROR;
145 }
146
147 fd = open(file_path, ConvertFlags(oflag));
148 if (fd < 0) {
149 HILOG_ERROR(HILOG_MODULE_HIVIEW, "failed to open file : %d", errno);
150 free(file_path);
151 return HAL_ERROR;
152 }
153
154 FileHandlerArray[index - 1] = fd;
155 free(file_path);
156
157 return index;
158 }
159
HalFileClose(int fd)160 int HalFileClose(int fd)
161 {
162 int ret;
163
164 /* make sure fd is within the allowed range, which is 1 to MAX_OPEN_FILE_NUM */
165 if ((fd > MAX_OPEN_FILE_NUM) || (fd <= 0)) {
166 return HAL_ERROR;
167 }
168
169 ret = close(FileHandlerArray[fd - 1]);
170 if (ret != 0) {
171 return HAL_ERROR;
172 }
173
174 FileHandlerArray[fd - 1] = SLOT_AVAILABLE;
175
176 return ret;
177 }
178
HalFileRead(int fd,char * buf,unsigned int len)179 int HalFileRead(int fd, char *buf, unsigned int len)
180 {
181 /* make sure fd is within the allowed range, which is 1 to MAX_OPEN_FILE_NUM */
182 if ((fd > MAX_OPEN_FILE_NUM) || (fd <= 0)) {
183 return HAL_ERROR;
184 }
185
186 return read(FileHandlerArray[fd - 1], buf, len);
187 }
188
HalFileWrite(int fd,const char * buf,unsigned int len)189 int HalFileWrite(int fd, const char *buf, unsigned int len)
190 {
191 /* make sure fd is within the allowed range, which is 1 to MAX_OPEN_FILE_NUM */
192 if ((fd > MAX_OPEN_FILE_NUM) || (fd <= 0)) {
193 return HAL_ERROR;
194 }
195
196 return write(FileHandlerArray[fd - 1], buf, len);
197 }
198
HalFileDelete(const char * path)199 int HalFileDelete(const char *path)
200 {
201 char *file_path;
202 int ret;
203
204 file_path = GetActualFilePath(path);
205 if (file_path == NULL) {
206 return HAL_ERROR;
207 }
208
209 ret = unlink(file_path);
210 free(file_path);
211
212 return ret;
213 }
214
HalFileStat(const char * path,unsigned int * fileSize)215 int HalFileStat(const char *path, unsigned int *fileSize)
216 {
217 char *file_path;
218 struct stat f_info;
219 int ret;
220
221 file_path = GetActualFilePath(path);
222 if (file_path == NULL) {
223 return HAL_ERROR;
224 }
225
226 ret = stat(file_path, &f_info);
227 *fileSize = f_info.st_size;
228 free(file_path);
229
230 return ret;
231 }
232
HalFileSeek(int fd,int offset,unsigned int whence)233 int HalFileSeek(int fd, int offset, unsigned int whence)
234 {
235 int ret = 0;
236 struct stat f_info;
237
238 /* make sure fd is within the allowed range, which is 1 to MAX_OPEN_FILE_NUM */
239 if ((fd > MAX_OPEN_FILE_NUM) || (fd <= 0)) {
240 return HAL_ERROR;
241 }
242
243 ret = fstat(FileHandlerArray[fd - 1], &f_info);
244 if (ret != 0) {
245 return HAL_ERROR;
246 }
247
248 if (whence == SEEK_SET_FS) {
249 if (offset > f_info.st_size) {
250 ret = HAL_ERROR;
251 }
252 }
253
254 ret = lseek(FileHandlerArray[fd - 1], offset, whence);
255 if ((ret > f_info.st_size) || (ret < 0)) {
256 return HAL_ERROR;
257 }
258
259 return ret;
260 }
261