• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics 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 
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include "hal_file.h"
23 #include "utils_file.h"
24 #include "wm_type_def.h"
25 
26 #define FLASH_FILE_NAME_LEN      32
27 #define FLASH_FILE_MAX_NUM       64
28 
29 #define MAX_NUM_OF_OPENED_FILES 32
30 static int g_openFileNum = 0;
31 
_mode_convert(int flags)32 static int _mode_convert(int flags)
33 {
34     int mode, res = 0;
35 
36     mode = flags & O_ACCMODE;
37     if (mode == O_RDONLY_FS) {
38         res |= O_RDONLY;
39     } else if (mode == O_WRONLY_FS) {
40         res |= O_WRONLY;
41     } else if (mode == O_RDWR_FS) {
42         res |= O_RDWR;
43     }
44     if (flags & O_CREAT_FS) {
45         res |= O_CREAT;
46     }
47     if (flags & O_EXCL_FS) {
48         res |= O_EXCL;
49     }
50     if (flags & O_TRUNC_FS) {
51         res |= O_TRUNC;
52     }
53     if (flags & O_APPEND_FS) {
54         res |= O_CREAT | O_APPEND;
55     }
56     return res;
57 }
58 
59 /* Relative path convert */
_path_convert(const char * path)60 static char *_path_convert(const char *path)
61 {
62     int len;
63     char *target_path;
64 
65     len = strlen(path) + 8; // 8:byte alignment
66     target_path = (char *)malloc(len);
67     if (target_path == NULL) {
68         return NULL;
69     }
70     memset_s(target_path, len, 0, len);
71     int i, j = 0;
72     memcpy(target_path, "/data/", 6);
73     j += 6; // 6:byte alignment
74     for (i = 0; i < strlen(path); i++) {
75         if (path[i] != '/') {
76             target_path[j++] = path[i];
77         }
78     }
79     target_path[j] = '\0';
80 
81     return target_path;
82 }
83 
HalFileOpen(const char * path,int oflag,int mode)84 int HalFileOpen(const char* path, int oflag, int mode)
85 {
86     if ((path == NULL) || (strlen(path) >= FLASH_FILE_NAME_LEN)) {
87         return -1;
88     }
89     if (g_openFileNum >= MAX_NUM_OF_OPENED_FILES) {
90         printf("\r\n_file_open: the number of open files reached max (%d)", MAX_NUM_OF_OPENED_FILES);
91         return -1;
92     }
93 
94     char *target_path = _path_convert(path);
95     if (target_path == NULL) {
96         printf("\r\n_file_open: target_path is null");
97         return -1;
98     }
99     int newMode = _mode_convert(oflag);
100     int fd = open(target_path, newMode);
101     free(target_path);
102     target_path = NULL;
103     if (fd < 0) {
104         return -1;
105     }
106     g_openFileNum ++;
107     return fd+1;
108 }
109 
HalFileClose(int fd)110 int HalFileClose(int fd)
111 {
112     int ret = 0;
113     if (fd < 0) {
114         return -1;
115     }
116     ret = close(fd - 1);
117     if (ret) {
118         return -1;
119     }
120     g_openFileNum --;
121     return ret;
122 }
123 
HalFileRead(int fd,char * buf,unsigned int len)124 int HalFileRead(int fd, char* buf, unsigned int len)
125 {
126     int ret = 0;
127     if (fd < 0 || (buf == NULL) || (len == 0)) {
128         return -1;
129     }
130     ret = read(fd - 1, buf, len);
131     return ret;
132 }
133 
HalFileWrite(int fd,const char * buf,unsigned int len)134 int HalFileWrite(int fd, const char* buf, unsigned int len)
135 {
136     int ret = 0;
137     if (fd < 0 || (buf == NULL) || (len == 0)) {
138         return -1;
139     }
140     ret = write(fd-1, buf, len);
141     return ret;
142 }
143 
HalFileDelete(const char * path)144 int HalFileDelete(const char* path)
145 {
146     int ret = 0;
147     if ((path == NULL) || (strlen(path) >= FLASH_FILE_NAME_LEN)) {
148         printf("\r\nHalFileDelete: input invalid parameter");
149         return -1;
150     }
151 
152     char *target_path = _path_convert(path);
153     if (target_path == NULL) {
154         printf("\r\n_file_open: target_path is null");
155         return -1;
156     }
157     ret = unlink(target_path);
158     free(target_path);
159     return ret;
160 }
161 
HalFileStat(const char * path,unsigned int * fileSize)162 int HalFileStat(const char* path, unsigned int* fileSize)
163 {
164     if ((path == NULL) || (strlen(path) >= FLASH_FILE_NAME_LEN)) {
165         return -1;
166     }
167 
168     char *target_path = _path_convert(path);
169     if (target_path == NULL) {
170         printf("\r\n_file_open: target_path is null");
171         return -1;
172     }
173     struct stat info = {0};
174     if (stat(target_path, &info) != F_OK) {
175         free(target_path);
176         return -1;
177     }
178     free(target_path);
179     *fileSize = info.st_size;
180     return 0;
181 }
182 
HalFileSeek(int fd,int offset,unsigned int whence)183 int HalFileSeek(int fd, int offset, unsigned int whence)
184 {
185     int ret = 0;
186     if (fd < 0) {
187         return -1;
188     }
189     ret = lseek(fd-1, offset, whence);
190     if (ret < 0) {
191         return -1;
192     }
193     struct stat info = {0};
194     if (fstat(fd-1, &info) != F_OK) {
195         return -1;
196     }
197     if (ret > info.st_size) {
198         return -1;
199     }
200     return ret;
201 }
202