1 /*
2 * Copyright (c) 2021 Huawei Device 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 "softbus_adapter_file.h"
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <securec.h>
21 #include <signal.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include "softbus_adapter_errcode.h"
28 #include "softbus_adapter_log.h"
29 #include "softbus_def.h"
30 #include "softbus_errcode.h"
31
SoftBusCreateFile(const char * fileName)32 static int32_t SoftBusCreateFile(const char *fileName)
33 {
34 char dirPath[SOFTBUS_MAX_PATH_LEN] = {0};
35
36 if (fileName == NULL) {
37 return SOFTBUS_FILE_ERR;
38 }
39
40 char *dir = (char *)fileName;
41 while ((dir = strchr(dir, SOFTBUS_PATH_SEPRATOR)) != NULL) {
42 uint32_t len = (uint32_t)(dir - fileName);
43 if (len == 0) { // skip root
44 dir++;
45 continue;
46 }
47 if (memcpy_s(dirPath, sizeof(dirPath), fileName, len) != EOK) {
48 HILOG_ERROR(SOFTBUS_HILOG_ID, "memory copy dir name failed");
49 return SOFTBUS_ERR;
50 }
51 dirPath[len] = 0;
52 if (access(dirPath, F_OK) != 0) {
53 int32_t ret = mkdir(dirPath, S_IRWXU);
54 if (ret != 0) {
55 HILOG_ERROR(SOFTBUS_HILOG_ID, "make dir failed, err code %{public}d", ret);
56 return SOFTBUS_ERR;
57 }
58 }
59 dir++;
60 }
61 int32_t fd = open(fileName, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
62 if (fd < 0) {
63 HILOG_ERROR(SOFTBUS_HILOG_ID, "crate file failed, errno = %{public}d", errno);
64 return SOFTBUS_ERR;
65 }
66 close(fd);
67 return SOFTBUS_OK;
68 }
69
SoftBusReadFile(int32_t fd,void * readBuf,uint32_t maxLen)70 int32_t SoftBusReadFile(int32_t fd, void *readBuf, uint32_t maxLen)
71 {
72 if (readBuf == NULL) {
73 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus read file [buff is null]");
74 return SOFTBUS_INVALID_PARAM;
75 }
76 int64_t len = read(fd, readBuf, maxLen);
77 if (len < 0) {
78 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus read file fail : %s", strerror(errno));
79 }
80 return len;
81 }
82
ReadFullFile(const char * fileName,char * readBuf,uint32_t maxLen,int32_t * size)83 static int32_t ReadFullFile(const char *fileName, char *readBuf, uint32_t maxLen, int32_t *size)
84 {
85 if (fileName == NULL || readBuf == NULL || maxLen == 0 || size == NULL) {
86 return SOFTBUS_FILE_ERR;
87 }
88
89 int32_t fd = open(fileName, O_RDONLY, S_IRUSR | S_IWUSR);
90 if (fd < 0) {
91 HILOG_ERROR(SOFTBUS_HILOG_ID, "ReadFile open file fail");
92 return SOFTBUS_FILE_ERR;
93 }
94 int32_t fileLen = lseek(fd, 0, SEEK_END);
95 if (fileLen <= 0 || fileLen > (int32_t)maxLen) {
96 HILOG_ERROR(SOFTBUS_HILOG_ID, "ReadFile maxLen failed or over maxLen");
97 close(fd);
98 return SOFTBUS_FILE_ERR;
99 }
100 int32_t ret = lseek(fd, 0, SEEK_SET);
101 if (ret < 0) {
102 HILOG_ERROR(SOFTBUS_HILOG_ID, "ReadFile lseek file fail");
103 close(fd);
104 return SOFTBUS_FILE_ERR;
105 }
106 ret = read(fd, readBuf, fileLen);
107 if (ret < 0) {
108 HILOG_ERROR(SOFTBUS_HILOG_ID, "ReadFile read fail, ret=%{public}d", ret);
109 close(fd);
110 return SOFTBUS_FILE_ERR;
111 }
112 close(fd);
113 *size = fileLen;
114 return SOFTBUS_OK;
115 }
116
SoftBusReadFullFileAndSize(const char * fileName,char * readBuf,uint32_t maxLen,int32_t * size)117 int32_t SoftBusReadFullFileAndSize(const char *fileName, char *readBuf, uint32_t maxLen, int32_t *size)
118 {
119 return ReadFullFile(fileName, readBuf, maxLen, size);
120 }
121
SoftBusReadFullFile(const char * fileName,char * readBuf,uint32_t maxLen)122 int32_t SoftBusReadFullFile(const char *fileName, char *readBuf, uint32_t maxLen)
123 {
124 int32_t size = 0;
125 return ReadFullFile(fileName, readBuf, maxLen, &size);
126 }
127
SoftBusWriteFile(const char * fileName,const char * writeBuf,uint32_t len)128 int32_t SoftBusWriteFile(const char *fileName, const char *writeBuf, uint32_t len)
129 {
130 if (fileName == NULL || writeBuf == NULL || len == 0) {
131 return SOFTBUS_FILE_ERR;
132 }
133 if (access(fileName, F_OK) != 0 && SoftBusCreateFile(fileName) != SOFTBUS_OK) {
134 HILOG_ERROR(SOFTBUS_HILOG_ID, "create file fail");
135 return SOFTBUS_FILE_ERR;
136 }
137 int32_t fd = open(fileName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
138 if (fd < 0) {
139 HILOG_ERROR(SOFTBUS_HILOG_ID, "WriteFile open file fail");
140 return SOFTBUS_FILE_ERR;
141 }
142 int32_t ret = write(fd, writeBuf, len);
143 if (len > INT32_MAX || ret != (int32_t)len) {
144 HILOG_ERROR(SOFTBUS_HILOG_ID, "WriteFile write fail");
145 close(fd);
146 return SOFTBUS_FILE_ERR;
147 }
148 fsync(fd);
149 close(fd);
150 return SOFTBUS_OK;
151 }
152
SoftBusWriteFileFd(int32_t fd,const char * writeBuf,uint32_t len)153 int32_t SoftBusWriteFileFd(int32_t fd, const char *writeBuf, uint32_t len)
154 {
155 if (writeBuf == NULL || len == 0) {
156 return SOFTBUS_FILE_ERR;
157 }
158 int32_t ret = write(fd, writeBuf, len);
159 if (ret != (int32_t)len) {
160 HILOG_ERROR(SOFTBUS_HILOG_ID, "WriteFileFd write fail");
161 }
162 return ret;
163 }
164
SoftBusOpenFile(const char * fileName,int32_t flags)165 int32_t SoftBusOpenFile(const char *fileName, int32_t flags)
166 {
167 if (fileName == NULL) {
168 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus open file [fileName is null]");
169 return SOFTBUS_INVALID_FD;
170 }
171 int32_t fd = open(fileName, flags);
172 if (fd < 0) {
173 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus open file [open fail], %s", strerror(errno));
174 return SOFTBUS_INVALID_FD;
175 }
176 return fd;
177 }
178
SoftBusOpenFileWithPerms(const char * fileName,int32_t flags,int32_t perms)179 int32_t SoftBusOpenFileWithPerms(const char *fileName, int32_t flags, int32_t perms)
180 {
181 if (fileName == NULL) {
182 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus open with perms file [fileName is null]");
183 return SOFTBUS_INVALID_FD;
184 }
185 int32_t fd = open(fileName, flags, perms);
186 if (fd < 0) {
187 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus open with perms file [open fail], %s", strerror(errno));
188 return SOFTBUS_INVALID_FD;
189 }
190 return fd;
191 }
192
SoftBusRemoveFile(const char * fileName)193 void SoftBusRemoveFile(const char *fileName)
194 {
195 if (fileName == NULL) {
196 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus remove file [fileName is null]");
197 return;
198 }
199 if (remove(fileName) != 0) {
200 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus remove file fail : %s", strerror(errno));
201 return;
202 }
203 }
204
SoftBusCloseFile(int32_t fd)205 void SoftBusCloseFile(int32_t fd)
206 {
207 if (fd <= SOFTBUS_INVALID_FD) {
208 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus close file [fd is invalid]");
209 return;
210 }
211 if (close(fd) != 0) {
212 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus remove file fail : %s", strerror(errno));
213 return;
214 }
215 }
216
SoftBusPreadFile(int32_t fd,void * buf,uint64_t readBytes,uint64_t offset)217 int64_t SoftBusPreadFile(int32_t fd, void *buf, uint64_t readBytes, uint64_t offset)
218 {
219 if (buf == NULL) {
220 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus pread file [buff is null]");
221 return SOFTBUS_ERR;
222 }
223 int64_t len = pread(fd, buf, readBytes, offset);
224 if (len < 0) {
225 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus pread file fail : %s", strerror(errno));
226 }
227 return len;
228 }
229
SoftBusPwriteFile(int32_t fd,const void * buf,uint64_t writeBytes,uint64_t offset)230 int64_t SoftBusPwriteFile(int32_t fd, const void *buf, uint64_t writeBytes, uint64_t offset)
231 {
232 if (buf == NULL) {
233 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus pwrite file [buff is null]");
234 return SOFTBUS_ERR;
235 }
236 int64_t len = pwrite(fd, buf, writeBytes, offset);
237 if (len < 0) {
238 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus pwrite file fail : %s", strerror(errno));
239 }
240 return len;
241 }
242
SoftBusAccessFile(const char * pathName,int32_t mode)243 int32_t SoftBusAccessFile(const char *pathName, int32_t mode)
244 {
245 if (pathName == NULL) {
246 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus access path [pathName is null]");
247 return SOFTBUS_ERR;
248 }
249
250 int32_t ret = access(pathName, mode);
251 if (ret != 0) {
252 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus access path fail : %s", strerror(errno));
253 return SOFTBUS_ERR;
254 }
255 return SOFTBUS_OK;
256 }
257
SoftBusMakeDir(const char * pathName,int32_t mode)258 int32_t SoftBusMakeDir(const char *pathName, int32_t mode)
259 {
260 if (pathName == NULL) {
261 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus mkdir file [pathName is null]");
262 return SOFTBUS_ERR;
263 }
264
265 int32_t ret = mkdir(pathName, mode);
266 if (ret == 0) {
267 return SOFTBUS_ADAPTER_OK;
268 } else if ((ret == -1) && (errno == EEXIST)) {
269 return SOFTBUS_ADAPTER_FILE_EXIST;
270 } else {
271 return SOFTBUS_ADAPTER_ERR;
272 }
273 }
SoftBusGetFileSize(const char * fileName,uint64_t * fileSize)274 int32_t SoftBusGetFileSize(const char *fileName, uint64_t *fileSize)
275 {
276 if ((fileName == NULL) || (fileSize == NULL)) {
277 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus mkdir file [fileName or fileSize is null]");
278 return SOFTBUS_ERR;
279 }
280
281 struct stat statbuff;
282 if (stat(fileName, &statbuff) < 0) {
283 HILOG_ERROR(SOFTBUS_HILOG_ID, "stat file fail");
284 return SOFTBUS_ERR;
285 } else {
286 *fileSize = statbuff.st_size;
287 }
288
289 return SOFTBUS_OK;
290 }
291
SoftBusRealPath(const char * path,char * absPath)292 char *SoftBusRealPath(const char *path, char *absPath)
293 {
294 if ((path == NULL) || (absPath == NULL)) {
295 HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus realpath [path or absPath is null]");
296 return NULL;
297 }
298
299 char *realPath = NULL;
300 if (realpath(path, absPath) == NULL) {
301 HILOG_ERROR(SOFTBUS_HILOG_ID, "realpath failed, err[%s]", strerror(errno));
302 return NULL;
303 } else {
304 realPath = absPath;
305 }
306 return realPath;
307 }