• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) 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 <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include "hal_trace.h"
21 #include "utils_file.h"
22 #include "hal_file.h"
23 
24 #define PATH_NAME_LEN 64
25 #define INVALID_KEY 92
check_path_valid(const char * path)26 static int check_path_valid(const char *path)
27 {
28     if (strlen(path) > PATH_NAME_LEN) {
29         TRACE(0, "path name is too long ,must less than %d", PATH_NAME_LEN);
30         return -1;
31     }
32 
33     for (int i = 0; i < strlen(path); i++) {
34         if ((int)path[i] == INVALID_KEY) {
35             return -1;
36         }
37     }
38 
39     return 0;
40 }
41 
HalFileOpen(const char * path,int oflag,int mode)42 int HalFileOpen(const char *path, int oflag, int mode)
43 {
44     (void)mode;
45 
46     if (check_path_valid(path) < 0) {
47         return -1;
48     }
49 
50     int ret = open(path, oflag);
51     return ret;
52 }
53 
HalFileClose(int fd)54 int HalFileClose(int fd)
55 {
56     return close(fd);
57 }
58 
HalFileRead(int fd,char * buf,unsigned int len)59 int HalFileRead(int fd, char *buf, unsigned int len)
60 {
61     if (fd > LOSCFG_LFS_MAX_OPEN_FILES) {
62         return -1;
63     }
64 
65     return read(fd, buf, len);
66 }
67 
HalFileWrite(int fd,const char * buf,unsigned int len)68 int HalFileWrite(int fd, const char *buf, unsigned int len)
69 {
70     if (fd > LOSCFG_LFS_MAX_OPEN_FILES) {
71         return -1;
72     }
73 
74     return write(fd, buf, len);
75 }
76 
HalFileDelete(const char * path)77 int HalFileDelete(const char *path)
78 {
79     return unlink(path);
80 }
81 
HalFileStat(const char * path,unsigned int * fileSize)82 int HalFileStat(const char *path, unsigned int *fileSize)
83 {
84     struct stat st_buf = {0};
85     if (stat(path, &st_buf) != 0) {
86         return -1;
87     }
88 
89     if (fileSize) {
90         *fileSize = st_buf.st_size;
91     }
92 
93     return 0;
94 }
95 
HalFileSeek(int fd,int offset,unsigned int whence)96 int HalFileSeek(int fd, int offset, unsigned int whence)
97 {
98     if (fd > LOSCFG_LFS_MAX_OPEN_FILES) {
99         return -1;
100     }
101 
102     int len = (int)lseek(fd, (off_t)0, SEEK_END_FS);
103     if (offset > len) {
104         return -1;
105     }
106 
107     return (int)lseek(fd, (off_t)offset, whence);
108 }
109