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