• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "hc_file.h"
17 #include <dirent.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include "hc_log.h"
23 #include "hc_types.h"
24 #include "securec.h"
25 
26 #define MAX_FOLDER_NAME_SIZE 128
27 #define DEFAULT_FILE_PERMISSION 0600
28 
CreateDirectory(const char * filePath)29 static int32_t CreateDirectory(const char *filePath)
30 {
31     int32_t ret;
32     errno_t eno;
33     char *chPtr = NULL;
34     char dirCache[MAX_FOLDER_NAME_SIZE];
35 
36     chPtr = (char *)filePath;
37     while ((chPtr = strchr(chPtr, '/')) != NULL) {
38         unsigned long len = (unsigned long)((uintptr_t)chPtr - (uintptr_t)filePath);
39         if (len == 0ul) {
40             chPtr++;
41             continue;
42         }
43         eno = memcpy_s(dirCache, sizeof(dirCache), filePath, len);
44         if (eno != EOK) {
45             LOGE("memory copy failed");
46             return -1;
47         }
48         dirCache[len] = 0;
49         if (access(dirCache, F_OK) == 0) {
50             chPtr++;
51             continue;
52         }
53         DIR *dir = opendir(dirCache);
54         if (dir == NULL) {
55             ret = mkdir(dirCache, DEFAULT_FILE_PERMISSION);
56             if (ret != 0) {
57                 LOGE("make dir failed, err code %d, errno = %d", ret, errno);
58                 return -1;
59             }
60         } else {
61             closedir(dir);
62         }
63         chPtr++;
64     }
65     return 0;
66 }
67 
HcFileOpenRead(const char * path)68 static int HcFileOpenRead(const char *path)
69 {
70     return open(path, O_RDONLY);
71 }
72 
HcFileOpenWrite(const char * path)73 static int HcFileOpenWrite(const char *path)
74 {
75     if (access(path, F_OK) != 0) {
76         int32_t ret = CreateDirectory(path);
77         if (ret != 0) {
78             return -1;
79         }
80     }
81     return open(path, O_RDWR | O_CREAT | O_TRUNC);
82 }
83 
HcFileOpen(const char * path,int mode,FileHandle * file)84 int HcFileOpen(const char *path, int mode, FileHandle *file)
85 {
86     if (path == NULL || file == NULL) {
87         return -1;
88     }
89     if (mode == MODE_FILE_READ) {
90         file->fileHandle.fd = HcFileOpenRead(path);
91     } else {
92         file->fileHandle.fd = HcFileOpenWrite(path);
93     }
94     if (file->fileHandle.fd == -1) {
95         LOGE("[OS]: file open failed, errno = %d", errno);
96         return -1;
97     } else {
98         return 0;
99     }
100 }
101 
HcFileSize(FileHandle file)102 int HcFileSize(FileHandle file)
103 {
104     int fp = file.fileHandle.fd;
105     int size = lseek(fp, 0, SEEK_END);
106     (void)lseek(fp, 0, SEEK_SET);
107     return size;
108 }
109 
HcFileRead(FileHandle file,void * dst,int dstSize)110 int HcFileRead(FileHandle file, void *dst, int dstSize)
111 {
112     int fp = file.fileHandle.fd;
113     if (fp == -1 || dstSize < 0 || dst == NULL) {
114         return -1;
115     }
116 
117     char *dstBuffer = (char *)dst;
118     int total = 0;
119     while (total < dstSize) {
120         int readCount = read(fp, dstBuffer + total, dstSize - total);
121         if (readCount < 0 || readCount > (dstSize - total)) {
122             LOGE("read size error, errno = %d", errno);
123             return -1;
124         }
125         if (readCount == 0) {
126             LOGE("read size = 0, errno = %d", errno);
127             return total;
128         }
129         total += readCount;
130     }
131 
132     return total;
133 }
134 
HcFileWrite(FileHandle file,const void * src,int srcSize)135 int HcFileWrite(FileHandle file, const void *src, int srcSize)
136 {
137     int fp = file.fileHandle.fd;
138     if (fp == -1 || srcSize < 0 || src == NULL) {
139         return -1;
140     }
141 
142     const char *srcBuffer = (const char *)src;
143     int total = 0;
144     while (total < srcSize) {
145         int writeCount = write(fp, srcBuffer + total, srcSize - total);
146         if (writeCount < 0 || writeCount > (srcSize - total)) {
147             LOGE("write size error, errno = %d", errno);
148             return -1;
149         }
150         total += writeCount;
151     }
152     return total;
153 }
154 
HcFileClose(FileHandle file)155 void HcFileClose(FileHandle file)
156 {
157     int fp = file.fileHandle.fd;
158     if (fp == -1) {
159         return;
160     }
161 
162     int res = close(fp);
163     if (res != 0) {
164         LOGW("close file failed, res = %d", res);
165     }
166 }
167 
HcFileRemove(const char * path)168 void HcFileRemove(const char *path)
169 {
170     if (path == NULL) {
171         LOGE("Invalid file path");
172         return;
173     }
174     int res = unlink(path);
175     if (res != 0) {
176         LOGW("delete file failed, res = %d", res);
177     }
178 }
179 
HcFileGetSubFileName(const char * path,StringVector * nameVec)180 void HcFileGetSubFileName(const char *path, StringVector *nameVec)
181 {
182     DIR *dir = NULL;
183     struct dirent *entry = NULL;
184     if ((dir = opendir(path)) == NULL) {
185         LOGI("opendir failed!");
186         return;
187     }
188     while ((entry = readdir(dir)) != NULL) {
189         if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) {
190             continue;
191         }
192         HcString subFileName = CreateString();
193         if (!StringSetPointer(&subFileName, entry->d_name)) {
194             LOGE("Failed to copy subFileName!");
195             DeleteString(&subFileName);
196             continue;
197         }
198         if (nameVec->pushBackT(nameVec, subFileName) == NULL) {
199             LOGE("Failed to push path to pathVec!");
200             DeleteString(&subFileName);
201         }
202     }
203     if (closedir(dir) < 0) {
204         LOGE("Failed to close file");
205     }
206 }
207