1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include "hal_trace.h"
36 #include "utils_file.h"
37 #include "hal_file.h"
38 #include "prt_fs.h"
39
40 #define FS_DATA_ROOT_DIR "/data"
41 #define FS_DATA_ROOT_DIR_SIZE 5
42 #define PATH_NAME_LEN 64
FsRealPath(char * realPath,const char * path)43 static int FsRealPath(char *realPath, const char *path)
44 {
45 int len = strlen(path);
46 if ((len > FS_DATA_ROOT_DIR_SIZE) && (strncmp(FS_DATA_ROOT_DIR, path, FS_DATA_ROOT_DIR_SIZE) == 0)) {
47 realPath = (char *)path;
48 return 0;
49 }
50
51 if (snprintf_s(realPath, PATH_NAME_LEN, PATH_NAME_LEN - 1, "%s%s%s", FS_DATA_ROOT_DIR, "/", path) <= 0) {
52 return -1;
53 }
54 return 0;
55 }
56
CheckPathValid(const char * path)57 static int CheckPathValid(const char *path)
58 {
59 int len = strlen(path);
60 if (len > PATH_NAME_LEN) {
61 printf("path name is too long ,must less than %d", PATH_NAME_LEN);
62 return -1;
63 }
64
65 for (int i = 0; i < len; i++) {
66 if ((int)path[i] == 92) { /* 92: invalid key */
67 return -1;
68 }
69 }
70
71 return 0;
72 }
73
HalFileOpen(const char * path,int oflag,int mode)74 int HalFileOpen(const char *path, int oflag, int mode)
75 {
76 char realPath[PATH_NAME_LEN] = {0};
77 (void)mode;
78
79 if (CheckPathValid(path) < 0) {
80 return -1;
81 }
82
83 if (FsRealPath(realPath, path) < 0) {
84 return -1;
85 }
86
87 return open(realPath, oflag);
88 }
89
HalFileClose(int fd)90 int HalFileClose(int fd)
91 {
92 return close(fd);
93 }
94
HalFileRead(int fd,char * buf,unsigned int len)95 int HalFileRead(int fd, char *buf, unsigned int len)
96 {
97 if (fd > OS_LFS_MAX_OPEN_FILES) {
98 return -1;
99 }
100
101 return read(fd, buf, len);
102 }
103
HalFileWrite(int fd,const char * buf,unsigned int len)104 int HalFileWrite(int fd, const char *buf, unsigned int len)
105 {
106 if (fd > OS_LFS_MAX_OPEN_FILES) {
107 return -1;
108 }
109
110 return write(fd, buf, len);
111 }
112
HalFileDelete(const char * path)113 int HalFileDelete(const char *path)
114 {
115 char realPath[PATH_NAME_LEN] = {0};
116 if (FsRealPath(realPath, path) < 0) {
117 return -1;
118 }
119
120 return unlink(realPath);
121 }
122
HalFileStat(const char * path,unsigned int * fileSize)123 int HalFileStat(const char *path, unsigned int *fileSize)
124 {
125 char realPath[PATH_NAME_LEN] = {0};
126 struct stat st_buf = {0};
127
128 if (FsRealPath(realPath, path) < 0) {
129 return -1;
130 }
131
132 if (stat(realPath, &st_buf) != 0) {
133 return -1;
134 }
135
136 if (fileSize) {
137 *fileSize = st_buf.st_size;
138 }
139
140 return 0;
141 }
142
HalFileSeek(int fd,int offset,unsigned int whence)143 int HalFileSeek(int fd, int offset, unsigned int whence)
144 {
145 if (fd > OS_LFS_MAX_OPEN_FILES) {
146 return -1;
147 }
148
149 int len = (int)lseek(fd, (off_t)0, SEEK_END_FS);
150 if (offset > len) {
151 return -1;
152 }
153
154 return (int)lseek(fd, (off_t)offset, whence);
155 }
156