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