• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SoftBusReadFullFile(const char * fileName,char * readBuf,uint32_t maxLen)83 int32_t SoftBusReadFullFile(const char *fileName, char *readBuf, uint32_t maxLen)
84 {
85     if (fileName == NULL || readBuf == NULL || maxLen == 0) {
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     return SOFTBUS_OK;
114 }
115 
SoftBusWriteFile(const char * fileName,const char * writeBuf,uint32_t len)116 int32_t SoftBusWriteFile(const char *fileName, const char *writeBuf, uint32_t len)
117 {
118     if (fileName == NULL || writeBuf == NULL || len == 0) {
119         return SOFTBUS_FILE_ERR;
120     }
121     if (access(fileName, F_OK) != 0 && SoftBusCreateFile(fileName) != SOFTBUS_OK) {
122         HILOG_ERROR(SOFTBUS_HILOG_ID, "create file fail");
123         return SOFTBUS_FILE_ERR;
124     }
125     int32_t fd = open(fileName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
126     if (fd < 0) {
127         HILOG_ERROR(SOFTBUS_HILOG_ID, "WriteFile open file fail");
128         return SOFTBUS_FILE_ERR;
129     }
130     int32_t ret = write(fd, writeBuf, len);
131     if (len > INT32_MAX || ret != (int32_t)len) {
132         HILOG_ERROR(SOFTBUS_HILOG_ID, "WriteFile write fail");
133         close(fd);
134         return SOFTBUS_FILE_ERR;
135     }
136     fsync(fd);
137     close(fd);
138     return SOFTBUS_OK;
139 }
140 
SoftBusOpenFile(const char * fileName,int32_t flags)141 int32_t SoftBusOpenFile(const char *fileName, int32_t flags)
142 {
143     if (fileName == NULL) {
144         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus open file [fileName is null]");
145         return SOFTBUS_INVALID_FD;
146     }
147     int32_t fd = open(fileName, flags);
148     if (fd < 0) {
149         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus open file [open fail], %s", strerror(errno));
150         return SOFTBUS_INVALID_FD;
151     }
152     return fd;
153 }
154 
SoftBusOpenFileWithPerms(const char * fileName,int32_t flags,int32_t perms)155 int32_t SoftBusOpenFileWithPerms(const char *fileName, int32_t flags, int32_t perms)
156 {
157     if (fileName == NULL) {
158         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus open with perms file [fileName is null]");
159         return SOFTBUS_INVALID_FD;
160     }
161     int32_t fd = open(fileName, flags, perms);
162     if (fd < 0) {
163         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus open with perms file [open fail], %s", strerror(errno));
164         return SOFTBUS_INVALID_FD;
165     }
166     return fd;
167 }
168 
SoftBusRemoveFile(const char * fileName)169 void SoftBusRemoveFile(const char *fileName)
170 {
171     if (fileName == NULL) {
172         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus remove file [fileName is null]");
173         return;
174     }
175     if (remove(fileName) != 0) {
176         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus remove file fail : %s", strerror(errno));
177         return;
178     }
179 }
180 
SoftBusCloseFile(int32_t fd)181 void SoftBusCloseFile(int32_t fd)
182 {
183     if (fd <= SOFTBUS_INVALID_FD) {
184         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus close file [fd is invalid]");
185         return;
186     }
187     if (close(fd) != 0) {
188         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus remove file fail : %s", strerror(errno));
189         return;
190     }
191 }
192 
SoftBusPreadFile(int32_t fd,void * buf,uint64_t readBytes,uint64_t offset)193 int64_t SoftBusPreadFile(int32_t fd, void *buf, uint64_t readBytes, uint64_t offset)
194 {
195     if (buf == NULL) {
196         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus pread file [buff is null]");
197         return SOFTBUS_ERR;
198     }
199     int64_t len = pread(fd, buf, readBytes, offset);
200     if (len < 0) {
201         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus pread file fail : %s", strerror(errno));
202     }
203     return len;
204 }
205 
SoftBusPwriteFile(int32_t fd,const void * buf,uint64_t writeBytes,uint64_t offset)206 int64_t SoftBusPwriteFile(int32_t fd, const void *buf, uint64_t writeBytes, uint64_t offset)
207 {
208     if (buf == NULL) {
209         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus pwrite file [buff is null]");
210         return SOFTBUS_ERR;
211     }
212     int64_t len = pwrite(fd, buf, writeBytes, offset);
213     if (len < 0) {
214         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus pwrite file fail : %s", strerror(errno));
215     }
216     return len;
217 }
218 
SoftBusAccessFile(const char * pathName,int32_t mode)219 int32_t SoftBusAccessFile(const char *pathName, int32_t mode)
220 {
221     if (pathName == NULL) {
222         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus access path [pathName is null]");
223         return SOFTBUS_ERR;
224     }
225 
226     int32_t ret = access(pathName, mode);
227     if (ret != 0) {
228         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus access path fail : %s", strerror(errno));
229         return SOFTBUS_ERR;
230     }
231     return SOFTBUS_OK;
232 }
233 
SoftBusMakeDir(const char * pathName,int32_t mode)234 int32_t SoftBusMakeDir(const char *pathName, int32_t mode)
235 {
236     if (pathName == NULL) {
237         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus mkdir file [pathName is null]");
238         return SOFTBUS_ERR;
239     }
240 
241     int32_t ret = mkdir(pathName, mode);
242     if (ret == 0) {
243         return SOFTBUS_ADAPTER_OK;
244     } else if ((ret == -1) && (errno == EEXIST)) {
245         return SOFTBUS_ADAPTER_FILE_EXIST;
246     } else {
247         return SOFTBUS_ADAPTER_ERR;
248     }
249 }
SoftBusGetFileSize(const char * fileName,uint64_t * fileSize)250 int32_t SoftBusGetFileSize(const char *fileName, uint64_t *fileSize)
251 {
252     if ((fileName == NULL) || (fileSize == NULL)) {
253         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus mkdir file [fileName or fileSize is null]");
254         return SOFTBUS_ERR;
255     }
256 
257     struct stat statbuff;
258     if (stat(fileName, &statbuff) < 0) {
259         HILOG_ERROR(SOFTBUS_HILOG_ID, "stat file fail");
260         return SOFTBUS_ERR;
261     } else {
262         *fileSize = statbuff.st_size;
263     }
264 
265     return SOFTBUS_OK;
266 }
267 
SoftBusRealPath(const char * path,char * absPath)268 char *SoftBusRealPath(const char *path, char *absPath)
269 {
270     if ((path == NULL) || (absPath == NULL)) {
271         HILOG_ERROR(SOFTBUS_HILOG_ID, "softbus realpath [path or absPath is null]");
272         return NULL;
273     }
274 
275     char *realPath = NULL;
276     if (realpath(path, absPath) == NULL) {
277         HILOG_ERROR(SOFTBUS_HILOG_ID, "realpath failed, err[%s]", strerror(errno));
278         return NULL;
279     } else {
280         realPath = absPath;
281     }
282     return realPath;
283 }
284