1 /*
2 * Copyright (C) 2022-2025 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 #include "string_util.h"
26
27 #define MAX_FOLDER_NAME_SIZE 128
28 #define DEFAULT_FILE_PERMISSION 0600
29
CreateDirectory(const char * filePath)30 static int32_t CreateDirectory(const char *filePath)
31 {
32 int32_t res;
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 if (memcpy_s(dirCache, sizeof(dirCache), filePath, len) != EOK) {
44 LOGE("memory copy failed");
45 return -1;
46 }
47 dirCache[len] = 0;
48 res = access(dirCache, F_OK);
49 if (res == 0) {
50 chPtr++;
51 continue;
52 }
53 LOGI("[OS]: CreateDirectory access fail. [Res]: %" LOG_PUB "d, [errno]: %" LOG_PUB "d", res, errno);
54 DIR *dir = opendir(dirCache);
55 if (dir == NULL) {
56 LOGI("[OS]: opendir fail. [errno]: %" LOG_PUB "d", errno);
57 res = mkdir(dirCache, DEFAULT_FILE_PERMISSION);
58 if (res != 0) {
59 LOGE("[OS]: mkdir fail. [Res]: %" LOG_PUB "d, [errno]: %" LOG_PUB "d", res, errno);
60 return -1;
61 }
62 } else {
63 closedir(dir);
64 }
65 chPtr++;
66 }
67 return 0;
68 }
69
HcFileOpenRead(const char * path)70 static int HcFileOpenRead(const char *path)
71 {
72 LOGI("[OS]: file open enter.");
73 int res = open(path, O_RDONLY);
74 LOGI("[OS]: file open quit.");
75 if (res == -1) {
76 LOGE("[OS]: file open fail. [Errno]: %" LOG_PUB "d", errno);
77 }
78 return res;
79 }
80
HcFileOpenWrite(const char * path)81 static int HcFileOpenWrite(const char *path)
82 {
83 if (access(path, F_OK) != 0) {
84 LOGI("[OS]: HcFileOpenWrite access fail. [errno]: %" LOG_PUB "d", errno);
85 if (CreateDirectory(path) != 0) {
86 return -1;
87 }
88 }
89 LOGI("[OS]: file open enter.");
90 int res = open(path, O_RDWR | O_CREAT | O_TRUNC, 0640);
91 LOGI("[OS]: file open quit.");
92 if (res == -1) {
93 LOGE("[OS]: file open fail. [Errno]: %" LOG_PUB "d", errno);
94 }
95 return res;
96 }
97
HcFileOpen(const char * path,int mode,FileHandle * file)98 int HcFileOpen(const char *path, int mode, FileHandle *file)
99 {
100 if (path == NULL || file == NULL) {
101 return -1;
102 }
103 if (mode == MODE_FILE_READ) {
104 file->fileHandle.fd = HcFileOpenRead(path);
105 } else {
106 file->fileHandle.fd = HcFileOpenWrite(path);
107 }
108 if (file->fileHandle.fd == -1) {
109 return -1;
110 } else {
111 return 0;
112 }
113 }
114
HcFileSize(FileHandle file)115 int HcFileSize(FileHandle file)
116 {
117 int fp = file.fileHandle.fd;
118 int size = lseek(fp, 0, SEEK_END);
119 (void)lseek(fp, 0, SEEK_SET);
120 return size;
121 }
122
HcFileRead(FileHandle file,void * dst,int dstSize)123 int HcFileRead(FileHandle file, void *dst, int dstSize)
124 {
125 int fp = file.fileHandle.fd;
126 if (fp == -1 || dstSize < 0 || dst == NULL) {
127 return -1;
128 }
129
130 char *dstBuffer = (char *)dst;
131 int total = 0;
132 LOGI("[OS]: file read enter. [OriSize]: %" LOG_PUB "d", dstSize);
133 while (total < dstSize) {
134 int readCount = read(fp, dstBuffer + total, dstSize - total);
135 if (readCount < 0 || readCount > (dstSize - total)) {
136 LOGE("[OS]: read size error. [Errno]: %" LOG_PUB "d", errno);
137 return -1;
138 }
139 if (readCount == 0) {
140 LOGE("read size = 0, errno = %" LOG_PUB "d", errno);
141 return total;
142 }
143 total += readCount;
144 }
145 LOGI("[OS]: file read quit. [ReadSize]: %" LOG_PUB "d", total);
146 return total;
147 }
148
HcFileWrite(FileHandle file,const void * src,int srcSize)149 int HcFileWrite(FileHandle file, const void *src, int srcSize)
150 {
151 int fp = file.fileHandle.fd;
152 if (fp == -1 || srcSize < 0 || src == NULL) {
153 return -1;
154 }
155
156 const char *srcBuffer = (const char *)src;
157 int total = 0;
158 LOGI("[OS]: file write enter. [OriSize]: %" LOG_PUB "d", srcSize);
159 while (total < srcSize) {
160 int writeCount = write(fp, srcBuffer + total, srcSize - total);
161 if (writeCount < 0 || writeCount > (srcSize - total)) {
162 LOGE("[OS]: write size error. [Errno]: %" LOG_PUB "d", errno);
163 return -1;
164 }
165 total += writeCount;
166 }
167 LOGI("[OS]: file write quit. [WriteSize]: %" LOG_PUB "d", total);
168 return total;
169 }
170
HcFileClose(FileHandle file)171 void HcFileClose(FileHandle file)
172 {
173 int fp = file.fileHandle.fd;
174 if (fp == -1) {
175 return;
176 }
177
178 int res = close(fp);
179 if (res != 0) {
180 LOGW("close file failed, res = %" LOG_PUB "d", res);
181 }
182 }
183
HcFileRemove(const char * path)184 void HcFileRemove(const char *path)
185 {
186 if (path == NULL) {
187 LOGE("Invalid file path");
188 return;
189 }
190 int res = unlink(path);
191 if (res != 0) {
192 LOGW("[OS]: delete file fail. [Res]: %" LOG_PUB "d", res);
193 }
194 }
195
HcFileGetSubFileName(const char * path,StringVector * nameVec)196 void HcFileGetSubFileName(const char *path, StringVector *nameVec)
197 {
198 DIR *dir = NULL;
199 struct dirent *entry = NULL;
200 if ((dir = opendir(path)) == NULL) {
201 LOGI("open dir failed!");
202 return;
203 }
204 while ((entry = readdir(dir)) != NULL) {
205 if (IsStrEqual(entry->d_name, ".") || IsStrEqual(entry->d_name, "..")) {
206 continue;
207 }
208 HcString subFileName = CreateString();
209 if (!StringSetPointer(&subFileName, entry->d_name)) {
210 LOGE("Failed to copy name to subFileName!");
211 DeleteString(&subFileName);
212 continue;
213 }
214 if (nameVec->pushBackT(nameVec, subFileName) == NULL) {
215 LOGE("Failed to push subFileName to nameVec!");
216 DeleteString(&subFileName);
217 }
218 }
219 if (closedir(dir) < 0) {
220 LOGE("Failed to close file");
221 }
222 }
223