1 /*
2 * Copyright (c) 2020 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 "utils_file.h"
17 #include <securec.h>
18 #include "hal_file.h"
19 #include "ohos_errno.h"
20 #include "ohos_types.h"
21
22 #include <stdbool.h>
23
24 #define BUFFER_SIZE 128
25
UtilsFileOpen(const char * path,int oflag,int mode)26 int UtilsFileOpen(const char* path, int oflag, int mode)
27 {
28 return HalFileOpen(path, oflag, mode);
29 }
30
UtilsFileClose(int fd)31 int UtilsFileClose(int fd)
32 {
33 return HalFileClose(fd);
34 }
35
UtilsFileRead(int fd,char * buf,unsigned int len)36 int UtilsFileRead(int fd, char* buf, unsigned int len)
37 {
38 return HalFileRead(fd, buf, len);
39 }
40
UtilsFileWrite(int fd,const char * buf,unsigned int len)41 int UtilsFileWrite(int fd, const char* buf, unsigned int len)
42 {
43 return HalFileWrite(fd, buf, len);
44 }
45
UtilsFileDelete(const char * path)46 int UtilsFileDelete(const char* path)
47 {
48 return HalFileDelete(path);
49 }
50
UtilsFileStat(const char * path,unsigned int * fileSize)51 int UtilsFileStat(const char* path, unsigned int* fileSize)
52 {
53 return HalFileStat(path, fileSize);
54 }
55
UtilsFileSeek(int fd,int offset,unsigned int whence)56 int UtilsFileSeek(int fd, int offset, unsigned int whence)
57 {
58 return HalFileSeek(fd, offset, whence);
59 }
60
UtilsFileCopy(const char * src,const char * dest)61 int UtilsFileCopy(const char* src, const char* dest)
62 {
63 if ((src == NULL) || (dest == NULL)) {
64 return EC_FAILURE;
65 }
66 int fpSrc = UtilsFileOpen(src, O_RDONLY_FS, 0);
67 if (fpSrc < 0) {
68 return fpSrc;
69 }
70 int fpDest = UtilsFileOpen(dest, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0);
71 if (fpDest < 0) {
72 UtilsFileClose(fpSrc);
73 return fpDest;
74 }
75 bool copyFailed = true;
76 int nLen;
77 char* dataBuf = (char *)malloc(BUFFER_SIZE);
78 if (dataBuf == NULL) {
79 goto MALLOC_ERROR;
80 }
81 nLen = UtilsFileRead(fpSrc, dataBuf, BUFFER_SIZE);
82 while (nLen > 0) {
83 if (UtilsFileWrite(fpDest, dataBuf, nLen) != nLen) {
84 goto EXIT;
85 }
86 nLen = UtilsFileRead(fpSrc, dataBuf, BUFFER_SIZE);
87 }
88 copyFailed = (nLen < 0);
89
90 EXIT:
91 free(dataBuf);
92 MALLOC_ERROR:
93 UtilsFileClose(fpSrc);
94 UtilsFileClose(fpDest);
95 if (copyFailed) {
96 UtilsFileDelete(dest);
97 return EC_FAILURE;
98 }
99 return EC_SUCCESS;
100 }
101
UtilsFileMove(const char * src,const char * dest)102 int UtilsFileMove(const char* src, const char* dest)
103 {
104 int ret = UtilsFileCopy(src, dest);
105 if (ret == EC_SUCCESS) {
106 ret = UtilsFileDelete(src);
107 }
108 return ret;
109 }
110