1 /*
2 * Copyright (C) 2021-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 <errno.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include "hal_error.h"
23 #include "hc_log.h"
24 #include "securec.h"
25 #include "utils_file.h"
26
HcFileOpenRead(const char * path)27 static int HcFileOpenRead(const char *path)
28 {
29 int ret = UtilsFileOpen(path, O_RDONLY, 0);
30 LOGI("ret = %" LOG_PUB "d", ret);
31 return ret;
32 }
33
HcFileOpenWrite(const char * path)34 static int HcFileOpenWrite(const char *path)
35 {
36 int ret = UtilsFileOpen(path, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0);
37 LOGI("ret = %" LOG_PUB "d", ret);
38 return ret;
39 }
40
HcFileOpen(const char * path,int mode,FileHandle * file)41 int HcFileOpen(const char *path, int mode, FileHandle *file)
42 {
43 if (path == NULL || file == NULL) {
44 return -1;
45 }
46 if (strcpy_s(file->filePath, MAX_FILE_PATH_SIZE, path) != EOK) {
47 LOGE("Failed to copy filePath!");
48 return HAL_FAILED;
49 }
50 if (mode == MODE_FILE_READ) {
51 file->fileHandle.fd = HcFileOpenRead(path);
52 } else {
53 file->fileHandle.fd = HcFileOpenWrite(path);
54 }
55 if (file->fileHandle.fd == HAL_FAILED) {
56 return HAL_FAILED;
57 }
58 return HAL_SUCCESS;
59 }
60
HcFileSize(FileHandle file)61 int HcFileSize(FileHandle file)
62 {
63 int fileSize = 0;
64 int ret = UtilsFileStat(file.filePath, (unsigned int *)&fileSize);
65 LOGI("ret = %" LOG_PUB "d, fileSize = %" LOG_PUB "d\n", ret, fileSize);
66 if (ret == HAL_SUCCESS) {
67 return fileSize;
68 }
69 return HAL_FAILED;
70 }
71
HcFileRead(FileHandle file,void * dst,int dstSize)72 int HcFileRead(FileHandle file, void *dst, int dstSize)
73 {
74 int fp = file.fileHandle.fd;
75 if (fp == HAL_FAILED || dstSize < 0 || dst == NULL) {
76 return HAL_FAILED;
77 }
78 int ret = UtilsFileRead(fp, (char *)dst, dstSize);
79 LOGI("ret = %" LOG_PUB "d", ret);
80 return ret;
81 }
82
HcFileWrite(FileHandle file,const void * src,int srcSize)83 int HcFileWrite(FileHandle file, const void *src, int srcSize)
84 {
85 int fp = file.fileHandle.fd;
86 if (fp == HAL_FAILED || srcSize < 0 || src == NULL) {
87 return HAL_FAILED;
88 }
89 int ret = UtilsFileWrite(fp, src, srcSize);
90 LOGI("ret = %" LOG_PUB "d", ret);
91 return ret;
92 }
93
HcFileClose(FileHandle file)94 void HcFileClose(FileHandle file)
95 {
96 int fp = file.fileHandle.fd;
97 if (fp < 0) {
98 return;
99 }
100 int ret = UtilsFileClose(fp);
101 LOGI("ret = %" LOG_PUB "d", ret);
102 }
103
HcFileRemove(const char * path)104 void HcFileRemove(const char *path)
105 {
106 if (path == NULL) {
107 LOGE("Invalid file path");
108 return;
109 }
110 int ret = UtilsFileDelete(path);
111 LOGI("File delete result:%" LOG_PUB "d", ret);
112 }
113
HcFileGetSubFileName(const char * path,StringVector * nameVec)114 void HcFileGetSubFileName(const char *path, StringVector *nameVec)
115 {
116 /* Since the liteOS device does not support retrieving files in the directory, the default file is used. */
117 (void)path;
118 HcString subFileName = CreateString();
119 if (!StringSetPointer(&subFileName, "hcgroup.dat")) {
120 LOGE("Failed to copy subFileName!");
121 DeleteString(&subFileName);
122 return;
123 }
124 if (nameVec->pushBackT(nameVec, subFileName) == NULL) {
125 LOGE("Failed to push name!");
126 DeleteString(&subFileName);
127 }
128 }