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     FILE *fp = fopen(path, "wb+");
76     if (fp == NULL) {
77         LOGE("[OS]: fopen fail. [errno]: %d", errno);
78         return NULL;
79     }
80     int res = fchmod(fileno(fp), S_IRUSR | S_IWUSR | S_IRGRP);
81     if (res != 0) {
82         LOGW("[OS]: fchmod fail. [Res]: %d, [errno]: %d", res, errno);
83     }
84     return fp;
85 }
86 
HcFileOpen(const char * path,int mode,FileHandle * file)87 int HcFileOpen(const char *path, int mode, FileHandle *file)
88 {
89     if (path == NULL || file == NULL) {
90         return -1;
91     }
92     if (mode == MODE_FILE_READ) {
93         file->pfd = HcFileOpenRead(path);
94     } else {
95         file->pfd = HcFileOpenWrite(path);
96     }
97     if (file->pfd == NULL) {
98         return -1;
99     }
100     return 0;
101 }
102 
HcFileSize(FileHandle file)103 int HcFileSize(FileHandle file)
104 {
105     FILE *fp = (FILE *)file.pfd;
106     if (fp != NULL) {
107         if (fseek(fp, 0L, SEEK_END) != 0) {
108             return -1;
109         }
110         int size = ftell(fp);
111         if (fseek(fp, 0L, SEEK_SET) != 0) {
112             return -1;
113         }
114         return size;
115     } else {
116         return -1;
117     }
118 }
119 
HcFileRead(FileHandle file,void * dst,int dstSize)120 int HcFileRead(FileHandle file, void *dst, int dstSize)
121 {
122     FILE *fp = (FILE *)file.pfd;
123     if (fp == NULL || dstSize < 0 || dst == NULL) {
124         return -1;
125     }
126 
127     char *dstBuffer = (char *)dst;
128     int total = 0;
129     LOGI("[OS]: file read enter. [OriSize]: %d", dstSize);
130     while (total < dstSize) {
131         int readCount = (int)fread(dstBuffer + total, 1, dstSize - total, fp);
132         if (ferror(fp) != 0) {
133             LOGE("read file error!");
134         }
135         if (readCount == 0) {
136             return total;
137         }
138         total += readCount;
139     }
140     LOGI("[OS]: file read quit. [ReadSize]: %d", total);
141     return total;
142 }
143 
HcFileWrite(FileHandle file,const void * src,int srcSize)144 int HcFileWrite(FileHandle file, const void *src, int srcSize)
145 {
146     FILE *fp = (FILE *)file.pfd;
147     if (fp == NULL || srcSize < 0 || src == NULL) {
148         return -1;
149     }
150 
151     if (fseek(fp, 0L, SEEK_SET) != 0) {
152         LOGE("[OS]: fseek file error!");
153         return -1;
154     }
155     const char *srcBuffer = (const char *)src;
156     int total = 0;
157     LOGI("[OS]: file write enter. [OriSize]: %d", srcSize);
158     while (total < srcSize) {
159         int writeCount = (int)fwrite(srcBuffer + total, 1, srcSize - total, fp);
160         if (ferror(fp) != 0) {
161             LOGE("write file error!");
162         }
163         total += writeCount;
164     }
165     LOGI("[OS]: file write quit. [WriteSize]: %d", total);
166     if (fflush(fp) != 0) {
167         LOGE("[OS]: fflush fail. [errno]: %d", errno);
168     }
169     if (fsync(fileno(fp)) != 0) {
170         LOGE("[OS]: fsync fail. [errno]: %d", errno);
171     }
172     return total;
173 }
174 
HcFileClose(FileHandle file)175 void HcFileClose(FileHandle file)
176 {
177     FILE *fp = (FILE *)file.pfd;
178     if (fp == NULL) {
179         return;
180     }
181 
182     (void)fclose(fp);
183 }
184 
HcFileRemove(const char * path)185 void HcFileRemove(const char *path)
186 {
187     if (path == NULL) {
188         LOGE("Invalid file path");
189         return;
190     }
191     (void)remove(path);
192 }
193 
HcFileGetSubFileName(const char * path,StringVector * nameVec)194 void HcFileGetSubFileName(const char *path, StringVector *nameVec)
195 {
196     DIR *dir = NULL;
197     struct dirent *entry = NULL;
198     if ((dir = opendir(path)) == NULL) {
199         LOGI("opendir failed!");
200         return;
201     }
202     while ((entry = readdir(dir)) != NULL) {
203         if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) {
204             continue;
205         }
206         HcString subFileName = CreateString();
207         if (!StringSetPointer(&subFileName, entry->d_name)) {
208             LOGE("Failed to copy subFileName!");
209             DeleteString(&subFileName);
210             continue;
211         }
212         if (nameVec->pushBackT(nameVec, subFileName) == NULL) {
213             LOGE("Failed to push path to pathVec!");
214             DeleteString(&subFileName);
215         }
216     }
217     if (closedir(dir) < 0) {
218         LOGE("Failed to close file");
219     }
220 }
221 
222 #ifdef __cplusplus
223 }
224 #endif
225