1 /*
2 * Copyright (c) 2021-2022 Talkweb Co., Ltd.
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 "fcntl.h"
17 #include "securec.h"
18 #include "sys/stat.h"
19 #include "utils_file.h"
20 #if (defined(LOSCFG_FS_LITTLEFS) && defined(LOSCFG_DRIVERS_HDF_PLATFORM_SPI))
21 #include "lfs.h"
22 #include "littlefs.h"
23 #define LITTLEFS_MAX_LFN_LEN 120
24 #define OFFSET_FD 3
25
HalFileGetPath(char * tmpPath,const char * path)26 static int HalFileGetPath(char *tmpPath, const char *path)
27 {
28 if (!path || !tmpPath) {
29 return -1;
30 }
31
32 int result;
33
34 for (; '.' == path[0]; path++);
35 for (; '/' == path[0]; path++);
36
37 result = snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/talkweb/%s", path);
38 if (result == -1) {
39 return -1;
40 }
41 return 0;
42 }
43
HalFileOpen(const char * path,int oflag,int mode)44 int HalFileOpen(const char *path, int oflag, int mode)
45 {
46 int fd, tflag = 0;
47 char tmpPath[LITTLEFS_MAX_LFN_LEN];
48 if (HalFileGetPath(tmpPath, path)) {
49 return -1;
50 }
51
52 if (O_CREAT_FS == (oflag & O_CREAT_FS)) {
53 tflag |= O_CREAT;
54 }
55
56 if (O_RDWR_FS == (oflag & O_RDWR_FS)) {
57 tflag |= O_RDWR;
58 }
59 #if O_WRONLY_FS != 0
60 else if (O_WRONLY_FS == (oflag & O_WRONLY_FS)) {
61 tflag |= O_WRONLY;
62 } else {
63 tflag |= O_RDONLY;
64 }
65 #else
66 else if (O_RDONLY_FS == (oflag & O_RDONLY_FS)) {
67 tflag |= O_RDONLY;
68 } else {
69 tflag |= O_WRONLY;
70 }
71
72 #endif
73 if (O_APPEND_FS == (oflag & O_APPEND_FS)) {
74 tflag |= O_APPEND;
75 }
76
77 if (O_EXCL_FS == (oflag & O_EXCL_FS)) {
78 tflag |= O_EXCL;
79 }
80
81 if (O_TRUNC_FS == (oflag & O_TRUNC_FS)) {
82 tflag |= O_TRUNC;
83 }
84
85 fd = _open(tmpPath, tflag);
86 if (fd < 0) {
87 return fd;
88 }
89
90 return fd + OFFSET_FD;
91 }
92
HalFileClose(int fd)93 int HalFileClose(int fd)
94 {
95 if (fd < OFFSET_FD) {
96 return -1;
97 }
98
99 return _close(fd - OFFSET_FD);
100 }
101
HalFileRead(int fd,char * buf,unsigned int len)102 int HalFileRead(int fd, char *buf, unsigned int len)
103 {
104 if (fd < OFFSET_FD) {
105 return -1;
106 }
107
108 return _read(fd - OFFSET_FD, buf, len);
109 }
110
HalFileWrite(int fd,const char * buf,unsigned int len)111 int HalFileWrite(int fd, const char *buf, unsigned int len)
112 {
113 if (fd < OFFSET_FD) {
114 return -1;
115 }
116
117 return _write(fd - OFFSET_FD, buf, len);
118 }
119
HalFileDelete(const char * path)120 int HalFileDelete(const char *path)
121 {
122 char tmpPath[LITTLEFS_MAX_LFN_LEN];
123 if (HalFileGetPath(tmpPath, path)) {
124 return -1;
125 }
126
127 return _unlink(tmpPath);
128 }
129
HalFileStat(const char * path,unsigned int * fileSize)130 int HalFileStat(const char *path, unsigned int *fileSize)
131 {
132 off_t len;
133 int fd;
134 char tmpPath[LITTLEFS_MAX_LFN_LEN];
135 if (HalFileGetPath(tmpPath, path)) {
136 return -1;
137 }
138
139 fd = _open(tmpPath, O_RDONLY);
140 if (fd < 0) {
141 return -1;
142 }
143
144 len = _lseek(fd, 0, SEEK_END);
145 _close(fd);
146 if (fileSize) {
147 *fileSize = len;
148 }
149
150 return 0;
151 }
152
HalFileSeek(int fd,int offset,unsigned int whence)153 int HalFileSeek(int fd, int offset, unsigned int whence)
154 {
155 if (fd < OFFSET_FD) {
156 return -1;
157 }
158
159 switch (whence) {
160 case SEEK_SET_FS:
161 whence = SEEK_SET;
162 break;
163 case SEEK_CUR_FS:
164 whence = SEEK_CUR;
165 break;
166 case SEEK_END_FS:
167 whence = SEEK_END;
168 break;
169 default:
170 return -1;
171 }
172 fd -= OFFSET_FD;
173 if ((SEEK_SET == whence) || (SEEK_CUR == whence)) {
174 off_t len, len2;
175 len = _lseek(fd, 0, SEEK_CUR);
176 len2 = _lseek(fd, 0, SEEK_END);
177 if (SEEK_CUR == whence) {
178 offset += len;
179 }
180 if (offset > len2) {
181 _lseek(fd, len, SEEK_SET);
182 return -1;
183 }
184 return _lseek(fd, (off_t)offset, SEEK_SET);
185 }
186 return _lseek(fd, (off_t)offset, whence);
187 }
188 #endif
189