1 /*
2 * Copyright (C) 2021-2023 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 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #define MAX_FOLDER_NAME_SIZE 128
31
CreateDirectory(const char * filePath)32 static int32_t CreateDirectory(const char *filePath)
33 {
34 int32_t res;
35 char *chPtr = NULL;
36 char dirCache[MAX_FOLDER_NAME_SIZE];
37
38 chPtr = (char *)filePath;
39 while ((chPtr = strchr(chPtr, '/')) != NULL) {
40 unsigned long len = (unsigned long)((uintptr_t)chPtr - (uintptr_t)filePath);
41 if (len == 0ul) {
42 chPtr++;
43 continue;
44 }
45 if (memcpy_s(dirCache, sizeof(dirCache), filePath, len) != EOK) {
46 LOGE("memory copy failed");
47 return -1;
48 }
49 dirCache[len] = 0;
50 if (access(dirCache, F_OK) != 0) {
51 res = mkdir(dirCache, S_IRWXU);
52 if (res != 0) {
53 LOGE("[OS]: mkdir fail. [Res]: %d, [errno]: %d", res, errno);
54 return -1;
55 }
56 }
57 chPtr++;
58 }
59 return 0;
60 }
61
HcFileOpenRead(const char * path)62 static FILE *HcFileOpenRead(const char *path)
63 {
64 return fopen(path, "rb");
65 }
66
HcFileOpenWrite(const char * path)67 static FILE *HcFileOpenWrite(const char *path)
68 {
69 if (access(path, F_OK) != 0) {
70 int32_t ret = CreateDirectory(path);
71 if (ret != 0) {
72 return NULL;
73 }
74 }
75 return fopen(path, "wb+");
76 }
77
HcFileOpen(const char * path,int mode,FileHandle * file)78 int HcFileOpen(const char *path, int mode, FileHandle *file)
79 {
80 if (path == NULL || file == NULL) {
81 return -1;
82 }
83 if (mode == MODE_FILE_READ) {
84 file->pfd = HcFileOpenRead(path);
85 } else {
86 file->pfd = HcFileOpenWrite(path);
87 }
88 if (file->pfd == NULL) {
89 return -1;
90 }
91 return 0;
92 }
93
HcFileSize(FileHandle file)94 int HcFileSize(FileHandle file)
95 {
96 FILE *fp = (FILE *)file.pfd;
97 if (fp != NULL) {
98 if (fseek(fp, 0L, SEEK_END) != 0) {
99 return -1;
100 }
101 int size = ftell(fp);
102 if (fseek(fp, 0L, SEEK_SET) != 0) {
103 return -1;
104 }
105 return size;
106 } else {
107 return -1;
108 }
109 }
110
HcFileRead(FileHandle file,void * dst,int dstSize)111 int HcFileRead(FileHandle file, void *dst, int dstSize)
112 {
113 FILE *fp = (FILE *)file.pfd;
114 if (fp == NULL || dstSize < 0 || dst == NULL) {
115 return -1;
116 }
117
118 char *dstBuffer = (char *)dst;
119 int total = 0;
120 LOGI("[OS]: file read enter. [OriSize]: %d", dstSize);
121 while (total < dstSize) {
122 int readCount = fread(dstBuffer + total, 1, dstSize - total, fp);
123 if (ferror(fp) != 0) {
124 LOGE("read file error!");
125 }
126 if (readCount == 0) {
127 return total;
128 }
129 total += readCount;
130 }
131 LOGI("[OS]: file read quit. [ReadSize]: %d", total);
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 FILE *fp = (FILE *)file.pfd;
138 if (fp == NULL || srcSize < 0 || src == NULL) {
139 return -1;
140 }
141
142 if (fseek(fp, 0L, SEEK_SET) != 0) {
143 LOGE("[OS]: fseek file error!");
144 return -1;
145 }
146 const char *srcBuffer = (const char *)src;
147 int total = 0;
148 LOGI("[OS]: file write enter. [OriSize]: %d", srcSize);
149 while (total < srcSize) {
150 int writeCount = fwrite(srcBuffer + total, 1, srcSize - total, fp);
151 if (ferror(fp) != 0) {
152 LOGE("write file error!");
153 }
154 total += writeCount;
155 }
156 LOGI("[OS]: file write quit. [WriteSize]: %d", total);
157 if (fflush(fp) != 0) {
158 LOGE("[OS]: fflush fail. [errno]: %d", errno);
159 }
160 if (fsync(fileno(fp)) != 0) {
161 LOGE("[OS]: fsync fail. [errno]: %d", errno);
162 }
163 return total;
164 }
165
HcFileClose(FileHandle file)166 void HcFileClose(FileHandle file)
167 {
168 FILE *fp = (FILE *)file.pfd;
169 if (fp == NULL) {
170 return;
171 }
172
173 (void)fclose(fp);
174 }
175
HcFileRemove(const char * path)176 void HcFileRemove(const char *path)
177 {
178 if (path == NULL) {
179 LOGE("Invalid file path");
180 return;
181 }
182 (void)remove(path);
183 }
184
HcFileGetSubFileName(const char * path,StringVector * nameVec)185 void HcFileGetSubFileName(const char *path, StringVector *nameVec)
186 {
187 DIR *dir = NULL;
188 struct dirent *entry = NULL;
189 if ((dir = opendir(path)) == NULL) {
190 LOGI("opendir failed!");
191 return;
192 }
193 while ((entry = readdir(dir)) != NULL) {
194 if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) {
195 continue;
196 }
197 HcString subFileName = CreateString();
198 if (!StringSetPointer(&subFileName, entry->d_name)) {
199 LOGE("Failed to copy subFileName!");
200 DeleteString(&subFileName);
201 continue;
202 }
203 if (nameVec->pushBackT(nameVec, subFileName) == NULL) {
204 LOGE("Failed to push path to pathVec!");
205 DeleteString(&subFileName);
206 }
207 }
208 if (closedir(dir) < 0) {
209 LOGE("Failed to close file");
210 }
211 }
212
213 #ifdef __cplusplus
214 }
215 #endif
216