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