• 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 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21 
22 #include "hks_file_operator.h"
23 
24 #include <dirent.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <sys/stat.h>
28 #include <sys/statfs.h>
29 #include <time.h>
30 #include <unistd.h>
31 
32 #include "hks_log.h"
33 #include "hks_mem.h"
34 #include "hks_template.h"
35 #include "securec.h"
36 #define REQUIRED_KEY_NOT_AVAILABLE 126
37 
38 #define UNKNOWN_TYPE (-1)
39 #define DE_TYPE 0
40 #define CE_TYPE 1
41 #define ECE_TYPE 2
42 
GetTimeString(const time_t * timer)43 static char *GetTimeString(const time_t *timer)
44 {
45     char *saveTime = NULL;
46     char *time = ctime(timer);
47     do {
48         HKS_IF_TRUE_BREAK(time == NULL);
49 
50         size_t timeLen = strlen(time);
51         HKS_IF_TRUE_BREAK(timeLen <= 0);
52 
53         saveTime = (char *)HksMalloc(timeLen);
54         HKS_IF_TRUE_BREAK(saveTime == NULL);
55 
56         if (memcpy_s(saveTime, timeLen, time, timeLen) != EOK) {
57             HKS_LOG_E("memcpy_s failed");
58             break;
59         }
60         saveTime[timeLen - 1] = '\0';
61     } while (0);
62     return saveTime;
63 }
64 
GetFileName(const char * path,const char * fileName,char * fullFileName,uint32_t fullFileNameLen)65 static int32_t GetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen)
66 {
67     if (path != NULL) {
68         if (strncpy_s(fullFileName, fullFileNameLen, path, strlen(path)) != EOK) {
69             return HKS_ERROR_INTERNAL_ERROR;
70         }
71 
72         if (path[strlen(path) - 1] != '/') {
73             if (strncat_s(fullFileName, fullFileNameLen, "/", strlen("/")) != EOK) {
74                 return HKS_ERROR_INTERNAL_ERROR;
75             }
76         }
77 
78         if (strncat_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) {
79             return HKS_ERROR_INTERNAL_ERROR;
80         }
81     } else {
82         HKS_IF_NOT_EOK_LOGE_RETURN(strncpy_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)),
83             HKS_ERROR_INTERNAL_ERROR, "strncpy_s fileName fail")
84     }
85 
86     return HKS_SUCCESS;
87 }
88 
GetFullFileName(const char * path,const char * fileName,char ** fullFileName)89 static int32_t GetFullFileName(const char *path, const char *fileName, char **fullFileName)
90 {
91     uint32_t nameLen = HKS_MAX_FILE_NAME_LEN;
92     char *tmpFileName = (char *)HksMalloc(nameLen);
93     HKS_IF_NULL_RETURN(tmpFileName, HKS_ERROR_MALLOC_FAIL)
94     (void)memset_s(tmpFileName, nameLen, 0, nameLen);
95 
96     int32_t ret = GetFileName(path, fileName, tmpFileName, nameLen);
97     if (ret != HKS_SUCCESS) {
98         HKS_LOG_E("get full fileName failed");
99         HKS_FREE(tmpFileName);
100         return ret;
101     }
102 
103     *fullFileName = tmpFileName;
104     return HKS_SUCCESS;
105 }
106 
IsValidPath(const char * path)107 static int32_t IsValidPath(const char *path)
108 {
109     if (path == NULL) {
110         HKS_LOG_E("path is NULL!");
111         return HKS_ERROR_NULL_POINTER;
112     }
113     HKS_IF_TRUE_LOGE_RETURN(strstr(path, "../") != NULL, HKS_ERROR_INVALID_ARGUMENT,
114         "dangerous filePath, ../ is not allowed to be included")
115 
116     return HKS_SUCCESS;
117 }
118 
GetFilePathType(const char * fileName)119 static int32_t GetFilePathType(const char *fileName)
120 {
121     if (strstr(fileName, "/data/service/el1") != NULL) {
122         return DE_TYPE;
123     }
124 #ifdef L2_STANDARD
125     if (strstr(fileName, HKS_CE_ROOT_PATH) != NULL) {
126         return CE_TYPE;
127     } else if (strstr(fileName, HKS_ECE_ROOT_PATH) != NULL) {
128         return ECE_TYPE;
129     }
130 #endif
131     return UNKNOWN_TYPE;
132 }
133 
IsFileExist(const char * fileName)134 static int32_t IsFileExist(const char *fileName)
135 {
136     if (access(fileName, F_OK) != 0) {
137         if (errno != ENOENT) {
138             HKS_LOG_FILE_OP_ERRNO("Check IsFileExist fail!", GetFilePathType(fileName));
139         }
140         return HKS_ERROR_NOT_EXIST;
141     }
142 
143     return HKS_SUCCESS;
144 }
145 
FileRead(const char * fileName,uint32_t offset,struct HksBlob * blob,uint32_t * size)146 static int32_t FileRead(const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size)
147 {
148     (void)offset;
149     HKS_IF_NOT_SUCC_RETURN(IsFileExist(fileName), HKS_ERROR_NOT_EXIST)
150     HKS_IF_TRUE_LOGE_RETURN(strstr(fileName, "../") != NULL, HKS_ERROR_INVALID_ARGUMENT,
151         "invalid filePath, ../ is included in file path")
152 
153     char filePath[PATH_MAX + 1] = {0};
154     if (realpath(fileName, filePath) == NULL) {
155         HKS_LOG_E("invalid filePath, realpath failed");
156         return HKS_ERROR_INVALID_ARGUMENT;
157     }
158 
159     FILE *fp = fopen(filePath, "rb");
160     if (fp ==  NULL) {
161         HKS_LOG_FILE_OP_ERRNO("open file fail,", GetFilePathType(filePath));
162         HKS_IF_TRUE_LOGE_RETURN(errno == REQUIRED_KEY_NOT_AVAILABLE, HKS_ERROR_NO_PERMISSION,
163             "Check Permission failed!")
164         return HKS_ERROR_OPEN_FILE_FAIL;
165     }
166 
167     // Print file time info
168     struct stat fileStat;
169     (void)memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
170     if (stat(fileName, &fileStat) != 0) {
171         HKS_LOG_FILE_OP_ERRNO("file stat fail,", GetFilePathType(fileName));
172         HKS_IF_TRUE_LOGE_RETURN(fclose(fp) < 0, HKS_ERROR_OPEN_FILE_FAIL,
173             "failed to close file, errno = 0x%" LOG_PUBLIC "x", errno)
174         return HKS_ERROR_OPEN_FILE_FAIL;
175     }
176 
177     char *cTime = GetTimeString(&fileStat.st_ctime);
178     char *mTime = GetTimeString(&fileStat.st_mtime);
179     if (cTime != NULL && mTime != NULL) {
180         HKS_LOG_I("File ctime: %" LOG_PUBLIC "s, mtime: %" LOG_PUBLIC "s", cTime, mTime);
181     }
182     HKS_FREE(cTime);
183     HKS_FREE(mTime);
184 
185     uint32_t len = fread(blob->data, 1, blob->size, fp);
186     if (fclose(fp) < 0) {
187         HKS_LOG_E("failed to close file, errno = 0x%" LOG_PUBLIC "x", errno);
188         return HKS_ERROR_CLOSE_FILE_FAIL;
189     }
190     *size = len;
191     return HKS_SUCCESS;
192 }
193 
FileSize(const char * fileName)194 static uint32_t FileSize(const char *fileName)
195 {
196     HKS_IF_NOT_SUCC_RETURN(IsFileExist(fileName), 0)
197 
198     struct stat fileStat;
199     (void)memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
200     if (stat(fileName, &fileStat) != 0) {
201         HKS_LOG_FILE_OP_ERRNO("file stat fail,", GetFilePathType(fileName));
202         return 0;
203     }
204 
205     return fileStat.st_size;
206 }
207 
GetRealPath(const char * fileName,char * filePath)208 static int32_t GetRealPath(const char *fileName, char *filePath)
209 {
210     if (memcpy_s(filePath, PATH_MAX, fileName, strlen(fileName)) != EOK) {
211         return HKS_ERROR_INSUFFICIENT_MEMORY;
212     }
213 
214     if (strstr(filePath, "../") != NULL) {
215         HKS_LOG_E("invalid filePath!");
216         return HKS_ERROR_INVALID_KEY_FILE;
217     }
218     return HKS_SUCCESS;
219 }
220 
FileWrite(const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)221 static int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
222 {
223     (void)offset;
224     char filePath[PATH_MAX + 1] = {0};
225     int32_t ret = GetRealPath(fileName, filePath);
226     HKS_IF_NOT_SUCC_LOGE(ret, "get real path faild")
227 
228     (void)realpath(fileName, filePath);
229 
230     /* caller function ensures that the folder exists */
231     FILE *fp = fopen(filePath, "wb+");
232     if (fp ==  NULL) {
233         HKS_LOG_FILE_OP_ERRNO("open file fail,", GetFilePathType(filePath));
234         HKS_IF_TRUE_LOGE_RETURN(errno == REQUIRED_KEY_NOT_AVAILABLE, HKS_ERROR_NO_PERMISSION,
235             "Check Permission failed!")
236         return HKS_ERROR_OPEN_FILE_FAIL;
237     }
238 
239     if (chmod(filePath, S_IRUSR | S_IWUSR) < 0) {
240         HKS_LOG_FILE_OP_ERRNO("chmod file fail,", GetFilePathType(filePath));
241         fclose(fp);
242         return HKS_ERROR_OPEN_FILE_FAIL;
243     }
244 
245     uint32_t size = fwrite(buf, 1, len, fp);
246     if (size != len) {
247         HKS_LOG_FILE_OP_ERRNO("write file size fail,", GetFilePathType(filePath));
248         fclose(fp);
249         return HKS_ERROR_WRITE_FILE_FAIL;
250     }
251 
252     if (fflush(fp) < 0) {
253         HKS_LOG_FILE_OP_ERRNO("fflush file fail,", GetFilePathType(filePath));
254         fclose(fp);
255         return HKS_ERROR_WRITE_FILE_FAIL;
256     }
257 
258     int fd = fileno(fp);
259     if (fd < 0) {
260         HKS_LOG_FILE_OP_ERRNO("fileno fail,", GetFilePathType(filePath));
261         fclose(fp);
262         return HKS_ERROR_WRITE_FILE_FAIL;
263     }
264 
265     if (fsync(fd) < 0) {
266         HKS_LOG_FILE_OP_ERRNO("sync file fail,", GetFilePathType(filePath));
267         fclose(fp);
268         return HKS_ERROR_WRITE_FILE_FAIL;
269     }
270 
271     if (fclose(fp) < 0) {
272         HKS_LOG_FILE_OP_ERRNO("failed to close file,", GetFilePathType(filePath));
273         return HKS_ERROR_CLOSE_FILE_FAIL;
274     }
275 
276     return HKS_SUCCESS;
277 }
278 
FileRemove(const char * fileName)279 static int32_t FileRemove(const char *fileName)
280 {
281     int32_t ret = IsFileExist(fileName);
282     HKS_IF_NOT_SUCC_RETURN(ret, HKS_SUCCESS) /* if file not exist, return ok */
283 
284     struct stat tmp;
285     if (stat(fileName, &tmp) != 0) {
286         HKS_LOG_FILE_OP_ERRNO("file stat fail,", GetFilePathType(fileName));
287         return HKS_ERROR_INTERNAL_ERROR;
288     }
289 
290     if (S_ISDIR(tmp.st_mode)) {
291         HKS_LOG_E("Is Dir!");
292         return HKS_ERROR_INVALID_ARGUMENT;
293     }
294 
295     if ((unlink(fileName) != 0) && (errno != ENOENT)) {
296         HKS_LOG_FILE_OP_ERRNO("unlink fail,", GetFilePathType(fileName));
297         return HKS_ERROR_REMOVE_FILE_FAIL;
298     }
299 
300     return HKS_SUCCESS;
301 }
302 
HksFileRemove(const char * path,const char * fileName)303 int32_t HksFileRemove(const char *path, const char *fileName)
304 {
305     HKS_IF_NULL_RETURN(fileName, HKS_ERROR_INVALID_ARGUMENT)
306 
307     char *fullFileName = NULL;
308     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
309     HKS_IF_NOT_SUCC_RETURN(ret, ret)
310     if (IsValidPath(fullFileName) != HKS_SUCCESS) {
311         HKS_FREE(fullFileName);
312         return HKS_ERROR_INVALID_ARGUMENT;
313     }
314 
315     ret = FileRemove(fullFileName);
316     HKS_FREE(fullFileName);
317     return ret;
318 }
319 
HksIsFileExist(const char * path,const char * fileName)320 int32_t HksIsFileExist(const char *path, const char *fileName)
321 {
322     HKS_IF_NULL_RETURN(fileName, HKS_ERROR_NULL_POINTER)
323 
324     char *fullFileName = NULL;
325     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
326     HKS_IF_NOT_SUCC_RETURN(ret, ret)
327     if (IsValidPath(fullFileName) != HKS_SUCCESS) {
328         HKS_FREE(fullFileName);
329         return HKS_ERROR_INVALID_ARGUMENT;
330     }
331 
332     ret = IsFileExist(fullFileName);
333     HKS_FREE(fullFileName);
334     return ret;
335 }
336 
HksIsDirExist(const char * path)337 int32_t HksIsDirExist(const char *path)
338 {
339     HKS_IF_NULL_RETURN(path, HKS_ERROR_NULL_POINTER)
340     HKS_IF_NOT_SUCC_RETURN(IsValidPath(path), HKS_ERROR_INVALID_ARGUMENT)
341     return IsFileExist(path);
342 }
343 
HksMakeDir(const char * path)344 int32_t HksMakeDir(const char *path)
345 {
346     if (IsValidPath(path) != HKS_SUCCESS) {
347         return HKS_ERROR_INVALID_ARGUMENT;
348     }
349     int result = mkdir(path, S_IRWXU);
350     if (result == 0) {
351         return HKS_SUCCESS;
352     } else {
353         switch (errno) {
354             case EEXIST:
355                 return HKS_ERROR_ALREADY_EXISTS;
356             default:
357                 HKS_LOG_FILE_OP_ERRNO("mkdir fail,", GetFilePathType(path));
358                 return HKS_ERROR_MAKE_DIR_FAIL;
359         }
360     }
361 }
362 
HksOpenDir(const char * path)363 void *HksOpenDir(const char *path)
364 {
365     HKS_IF_NOT_SUCC_RETURN(IsValidPath(path), NULL)
366     return (void *)opendir(path);
367 }
368 
HksCloseDir(void * dirp)369 int32_t HksCloseDir(void *dirp)
370 {
371     return closedir((DIR *)dirp);
372 }
373 
HksGetDirFile(void * dirp,struct HksFileDirentInfo * direntInfo)374 int32_t HksGetDirFile(void *dirp, struct HksFileDirentInfo *direntInfo)
375 {
376     DIR *dir = (DIR *)dirp;
377     struct dirent *dire = readdir(dir);
378 
379     while (dire != NULL) {
380         if (dire->d_type != DT_REG) { /* only care about files. */
381             dire = readdir(dir);
382             continue;
383         }
384 
385         uint32_t len = strlen(dire->d_name);
386         if (memcpy_s(direntInfo->fileName, sizeof(direntInfo->fileName) - 1, dire->d_name, len) != EOK) {
387             return HKS_ERROR_INSUFFICIENT_MEMORY;
388         }
389         direntInfo->fileName[len] = '\0';
390         return HKS_SUCCESS;
391     }
392 
393     return HKS_ERROR_NOT_EXIST;
394 }
395 
HksRemoveDir(const char * dirPath)396 int32_t HksRemoveDir(const char *dirPath)
397 {
398     HKS_IF_NOT_SUCC_RETURN(IsValidPath(dirPath), HKS_ERROR_INVALID_ARGUMENT)
399     struct stat fileStat;
400     int32_t ret = stat(dirPath, &fileStat);
401     if (ret != 0) {
402         HKS_LOG_FILE_OP_ERRNO("file stat failed", GetFilePathType(dirPath));
403         return HKS_FAILURE;
404     }
405 
406     if (!S_ISDIR(fileStat.st_mode)) {
407         HKS_LOG_E("path is not dir");
408         return HKS_FAILURE;
409     }
410 
411     DIR *dir = opendir(dirPath);
412     HKS_IF_NULL_LOGE_RETURN(dir, HKS_ERROR_OPEN_FILE_FAIL, "open dir failed")
413 
414     struct dirent *dire = readdir(dir);
415     while (dire != NULL) {
416         if (dire->d_type == DT_REG) { /* only care about files. */
417             ret = HksFileRemove(dirPath, dire->d_name);
418             HKS_IF_NOT_SUCC_LOGE(ret, "remove file failed when remove dir files, ret = %" LOG_PUBLIC "d.", ret)
419         }
420         dire = readdir(dir);
421     }
422 
423     closedir(dir);
424     return HKS_SUCCESS;
425 }
426 
HksDeletDirPartTwo(const char * path)427 static int32_t HksDeletDirPartTwo(const char *path)
428 {
429     int32_t ret;
430     char deletePath[HKS_MAX_FILE_NAME_LEN] = {0};
431     DIR *dir = opendir(path);
432     HKS_IF_NULL_LOGE_RETURN(dir, HKS_ERROR_OPEN_FILE_FAIL, "open dir failed")
433     struct dirent *dire = readdir(dir);
434     while (dire != NULL) {
435         if (strncpy_s(deletePath, sizeof(deletePath), path, strlen(path)) != EOK) {
436             closedir(dir);
437             return HKS_ERROR_INTERNAL_ERROR;
438         }
439 
440         if (deletePath[strlen(deletePath) - 1] != '/') {
441             if (strncat_s(deletePath, sizeof(deletePath), "/", strlen("/")) != EOK) {
442                 closedir(dir);
443                 return HKS_ERROR_INTERNAL_ERROR;
444             }
445         }
446 
447         if (strncat_s(deletePath, sizeof(deletePath), dire->d_name, strlen(dire->d_name)) != EOK) {
448             closedir(dir);
449             return HKS_ERROR_INTERNAL_ERROR;
450         }
451 
452         if ((strcmp("..", dire->d_name) != 0) && (strcmp(".", dire->d_name) != 0)) {
453             (void)remove(deletePath);
454         }
455         dire = readdir(dir);
456     }
457     closedir(dir);
458     ret = remove(path);
459     return ret;
460 }
461 
HksDeletDirPartOne(const char * path)462 static int32_t HksDeletDirPartOne(const char *path)
463 {
464     int32_t ret;
465     char deletePath[HKS_MAX_FILE_NAME_LEN] = {0};
466     DIR *dir = opendir(path);
467     HKS_IF_NULL_LOGE_RETURN(dir, HKS_ERROR_OPEN_FILE_FAIL, "open dir failed")
468     struct dirent *dire = readdir(dir);
469     while (dire != NULL) {
470         if (strncpy_s(deletePath, sizeof(deletePath), path, strlen(path)) != EOK) {
471             closedir(dir);
472             return HKS_ERROR_INTERNAL_ERROR;
473         }
474 
475         if (deletePath[strlen(deletePath) - 1] != '/') {
476             if (strncat_s(deletePath, sizeof(deletePath), "/", strlen("/")) != EOK) {
477                 closedir(dir);
478                 return HKS_ERROR_INTERNAL_ERROR;
479             }
480         }
481 
482         if (strncat_s(deletePath, sizeof(deletePath), dire->d_name, strlen(dire->d_name)) != EOK) {
483             closedir(dir);
484             return HKS_ERROR_INTERNAL_ERROR;
485         }
486 
487         if (dire->d_type == DT_DIR && (strcmp("..", dire->d_name) != 0) && (strcmp(".", dire->d_name) != 0)) {
488             HksDeletDirPartTwo(deletePath);
489         } else if (dire->d_type != DT_DIR) {
490             (void)remove(deletePath);
491         }
492         dire = readdir(dir);
493     }
494     closedir(dir);
495     ret = remove(path);
496     return ret;
497 }
498 
HksDeleteDir(const char * path)499 int32_t HksDeleteDir(const char *path)
500 {
501     HKS_IF_NOT_SUCC_RETURN(IsValidPath(path), HKS_ERROR_INVALID_ARGUMENT)
502     int32_t ret;
503     char deletePath[HKS_MAX_FILE_NAME_LEN] = { 0 };
504 
505     DIR *dir = opendir(path);
506     if (dir == NULL) {
507         HKS_LOG_FILE_OP_ERRNO("open dir failed", GetFilePathType(path));
508         return HKS_ERROR_OPEN_FILE_FAIL;
509     }
510     struct dirent *dire = readdir(dir);
511     while (dire != NULL) {
512         if (strncpy_s(deletePath, sizeof(deletePath), path, strlen(path)) != EOK) {
513             closedir(dir);
514             return HKS_ERROR_INTERNAL_ERROR;
515         }
516 
517         if (deletePath[strlen(deletePath) - 1] != '/') {
518             if (strncat_s(deletePath, sizeof(deletePath), "/", strlen("/")) != EOK) {
519                 closedir(dir);
520                 return HKS_ERROR_INTERNAL_ERROR;
521             }
522         }
523 
524         if (strncat_s(deletePath, sizeof(deletePath), dire->d_name, strlen(dire->d_name)) != EOK) {
525             closedir(dir);
526             return HKS_ERROR_INTERNAL_ERROR;
527         }
528 
529         if (dire->d_type == DT_DIR && (strcmp("..", dire->d_name) != 0) && (strcmp(".", dire->d_name) != 0)) {
530             HksDeletDirPartOne(deletePath);
531         } else if (dire->d_type != DT_DIR) {
532             (void)remove(deletePath);
533         }
534         dire = readdir(dir);
535     }
536     closedir(dir);
537     ret = remove(path);
538     return ret;
539 }
540 
HksFileRead(const char * path,const char * fileName,uint32_t offset,struct HksBlob * blob,uint32_t * size)541 int32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size)
542 {
543     if ((fileName == NULL) || (blob == NULL) || (blob->data == NULL) || (blob->size == 0) || (size == NULL)) {
544         return HKS_ERROR_INVALID_ARGUMENT;
545     }
546 
547     char *fullFileName = NULL;
548     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
549     HKS_IF_NOT_SUCC_RETURN(ret, ret)
550     if (IsValidPath(fullFileName) != HKS_SUCCESS) {
551         HKS_FREE(fullFileName);
552         return HKS_ERROR_INVALID_ARGUMENT;
553     }
554 
555     ret = FileRead(fullFileName, offset, blob, size);
556     HKS_FREE(fullFileName);
557     return ret;
558 }
559 
HksFileWrite(const char * path,const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)560 int32_t HksFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
561 {
562     if ((fileName == NULL) || (buf == NULL) || (len == 0)) {
563         return HKS_ERROR_INVALID_ARGUMENT;
564     }
565 
566     char *fullFileName = NULL;
567     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
568     HKS_IF_NOT_SUCC_RETURN(ret, ret)
569     if (IsValidPath(fullFileName) != HKS_SUCCESS) {
570         HKS_FREE(fullFileName);
571         return HKS_ERROR_INVALID_ARGUMENT;
572     }
573 
574     ret = FileWrite(fullFileName, offset, buf, len);
575     HKS_FREE(fullFileName);
576     return ret;
577 }
578 
HksFileSize(const char * path,const char * fileName)579 uint32_t HksFileSize(const char *path, const char *fileName)
580 {
581     HKS_IF_NULL_RETURN(fileName, 0)
582 
583     char *fullFileName = NULL;
584     int32_t ret = GetFullFileName(path, fileName, &fullFileName);
585     HKS_IF_NOT_SUCC_RETURN(ret, 0)
586     if (IsValidPath(fullFileName) != HKS_SUCCESS) {
587         HKS_FREE(fullFileName);
588         return 0;
589     }
590 
591     uint32_t size = FileSize(fullFileName);
592     HKS_FREE(fullFileName);
593     return size;
594 }
595 
HksGetFileName(const char * path,const char * fileName,char * fullFileName,uint32_t fullFileNameLen)596 int32_t HksGetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen)
597 {
598     return GetFileName(path, fileName, fullFileName, fullFileNameLen);
599 }
600 
GetDeviceValidSize(const char * partitionName)601 double GetDeviceValidSize(const char *partitionName)
602 {
603     struct statfs stat;
604     int err = statfs(partitionName, &stat);
605     if (err != 0) {
606         HKS_LOG_I("GetDeviceValidSize failed");
607         return 0;
608     }
609     return (double)(stat.f_bfree) * (double)(stat.f_bsize);
610 }