• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 FuZhou Lockzhiner Electronic 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 <stdint.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 
21 #include "link.h"
22 #include "lz_hardware.h"
23 #include "lfs.h"
24 #include "los_config.h"
25 #include "lfs_api.h"
26 
27 /* 作为主目录文件夹 */
28 #define MOUNT_ROOT          "/data"
29 /* 文件名字字符串长度 */
30 #define FILE_NAME_MAXSIZE   32
31 
HalCheckPath(const char * src,char * dst,int max_len)32 static int HalCheckPath(const char *src, char *dst, int max_len)
33 {
34     if (src == NULL) {
35         return -1;
36     }
37     if (dst == NULL) {
38         return -1;
39     }
40     if (strlen(src) > max_len) {
41         return -1;
42     }
43     if (strlen(src) > max_len) {
44         return -1;
45     }
46 
47     memset_s(dst, sizeof(char) * max_len, 0, sizeof(char) * max_len);
48     if (src[0] == '/') {
49         if (memcpy_s(dst, sizeof(char) * max_len, src, strlen(src)) == NULL) {
50             return -1;
51         }
52     } else {
53         if (strlen(src) > max_len) {
54             return -1;
55         }
56 
57         if (snprintf_s(dst, max_len, max_len -1, "%s/%s", MOUNT_ROOT, src) < 0) {
58             return -1;
59         }
60     }
61 
62     return 0;
63 }
64 
HalFileOpen(const char * path,int oflag,int mode)65 int HalFileOpen(const char* path, int oflag, int mode)
66 {
67     int ret = 0;
68     char full_path[2 * LFS_NAME_MAX];
69 
70     ret = HalCheckPath(path, full_path, FILE_NAME_MAXSIZE);
71     if (ret != 0) {
72         return -1;
73     }
74 
75     ret = LfsOpen(full_path, oflag, mode);
76     if (ret < 0) {
77         printf("%s, %d: LfsOpen(%s) failed(%d)\n", __FILE__, __LINE__, full_path, ret);
78     }
79 
80     return ret;
81 }
82 
HalFileClose(int fd)83 int HalFileClose(int fd)
84 {
85     return LfsClose(fd);
86 }
87 
HalFileRead(int fd,char * buf,unsigned int len)88 int HalFileRead(int fd, char *buf, unsigned int len)
89 {
90     return LfsRead(fd, buf, len);
91 }
92 
HalFileWrite(int fd,const char * buf,unsigned int len)93 int HalFileWrite(int fd, const char *buf, unsigned int len)
94 {
95     return LfsWrite(fd, buf, len);
96 }
97 
HalFileSeek(int fd,int offset,unsigned int whence)98 int HalFileSeek(int fd, int offset, unsigned int whence)
99 {
100     return LfsSeek(fd, offset, whence);
101 }
102 
HalFileStat(const char * path,unsigned int * fileSize)103 int HalFileStat(const char* path, unsigned int* fileSize)
104 {
105     struct stat st_buf = { 0 };
106     int ret = 0;
107     char full_path[2 * LFS_NAME_MAX];
108 
109     if (path == NULL) {
110         return -1;
111     }
112     if (fileSize == NULL) {
113         return -1;
114     }
115 
116     ret = HalCheckPath(path, full_path, FILE_NAME_MAXSIZE);
117     if (ret != 0) {
118         return -1;
119     }
120 
121     if (LfsStat(full_path, &st_buf) != 0) {
122         return -1;
123     }
124 
125     *fileSize = st_buf.st_size;
126 
127     return 0;
128 }
129 
HalFileDelete(const char * path)130 int HalFileDelete(const char* path)
131 {
132     int ret = 0;
133     char full_path[LFS_NAME_MAX];
134 
135     if (path == NULL) {
136         return -1;
137     }
138 
139     ret = HalCheckPath(path, full_path, FILE_NAME_MAXSIZE);
140     if (ret != 0) {
141         return -1;
142     }
143 
144     return LfsUnlink(full_path);
145 }
146 
147