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 "file_operator.h"
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include "securec.h"
22 #include "adaptor_log.h"
23 #include "defines.h"
24
IsFileExist(const char * fileName)25 static bool IsFileExist(const char *fileName)
26 {
27 if (fileName == NULL) {
28 LOG_ERROR("get null file name");
29 return false;
30 }
31 FILE *fileOperator = fopen(fileName, "rb");
32 if (fileOperator == NULL) {
33 return false;
34 }
35 (void)fclose(fileOperator);
36 return true;
37 }
38
ReadFile(const char * fileName,uint8_t * buf,uint32_t len)39 static int32_t ReadFile(const char *fileName, uint8_t *buf, uint32_t len)
40 {
41 if ((fileName == NULL) || (buf == NULL) || (len == 0)) {
42 LOG_ERROR("get bad params");
43 return RESULT_BAD_PARAM;
44 }
45 FILE *fileOperator = fopen(fileName, "rb");
46 if (fileOperator == NULL) {
47 LOG_ERROR("open file failed");
48 return RESULT_BAD_PARAM;
49 }
50 size_t readLen = fread(buf, sizeof(uint8_t), len, fileOperator);
51 if (readLen != len) {
52 LOG_ERROR("read file failed");
53 (void)fclose(fileOperator);
54 (void)memset_s(buf, len, 0, len);
55 return RESULT_BAD_READ;
56 }
57 (void)fclose(fileOperator);
58 return RESULT_SUCCESS;
59 }
60
WriteFile(const char * fileName,const uint8_t * buf,uint32_t len)61 static int32_t WriteFile(const char *fileName, const uint8_t *buf, uint32_t len)
62 {
63 if ((fileName == NULL) || (buf == NULL) || (len == 0)) {
64 LOG_ERROR("get bad params");
65 return RESULT_BAD_PARAM;
66 }
67 FILE *fileOperator = fopen(fileName, "wb");
68 if (fileOperator == NULL) {
69 LOG_ERROR("open file failed");
70 return RESULT_BAD_PARAM;
71 }
72
73 /* Set the file permission to 600 */
74 if (chmod(fileName, S_IRUSR | S_IWUSR) != 0) {
75 LOG_ERROR("chmod file fail, and file name is : %{public}s", fileName);
76 (void)fclose(fileOperator);
77 return RESULT_GENERAL_ERROR;
78 }
79 size_t writeLen = fwrite(buf, sizeof(uint8_t), len, fileOperator);
80 if (writeLen != len) {
81 LOG_ERROR("write file failed");
82 (void)fclose(fileOperator);
83 return RESULT_BAD_WRITE;
84 }
85 if (fflush(fileOperator) == EOF) {
86 LOG_ERROR("fflush file fail");
87 }
88 (void)fsync(fileno(fileOperator));
89 (void)fclose(fileOperator);
90 return RESULT_SUCCESS;
91 }
92
GetFileLen(const char * fileName,uint32_t * len)93 static int32_t GetFileLen(const char *fileName, uint32_t *len)
94 {
95 if ((fileName == NULL) || (len == NULL)) {
96 LOG_ERROR("get bad params");
97 return RESULT_BAD_PARAM;
98 }
99 *len = 0;
100 FILE *fileOperator = fopen(fileName, "rb");
101 if (fileOperator == NULL) {
102 LOG_ERROR("fopen file failed");
103 return RESULT_BAD_PARAM;
104 }
105 if (fseek(fileOperator, 0L, SEEK_END) != 0) {
106 LOG_ERROR("seek file failed");
107 (void)fclose(fileOperator);
108 return RESULT_GENERAL_ERROR;
109 }
110 long fileLen = ftell(fileOperator);
111 if (fileLen < 0 || fileLen > UINT32_MAX) {
112 LOG_ERROR("tell file failed");
113 (void)fclose(fileOperator);
114 return RESULT_GENERAL_ERROR;
115 }
116 *len = fileLen;
117 (void)fclose(fileOperator);
118 return RESULT_SUCCESS;
119 }
120
DeleteFile(const char * fileName)121 static int32_t DeleteFile(const char *fileName)
122 {
123 if (fileName == NULL) {
124 LOG_ERROR("get bad params");
125 return RESULT_BAD_PARAM;
126 }
127 int ret = remove(fileName);
128 if (ret != 0) {
129 LOG_ERROR("delete file failed");
130 return RESULT_GENERAL_ERROR;
131 }
132 return RESULT_SUCCESS;
133 }
134
GetDefaultFileOperator(void)135 FileOperator *GetDefaultFileOperator(void)
136 {
137 static FileOperator fileOperator = {
138 .isFileExist = IsFileExist,
139 .getFileLen = GetFileLen,
140 .readFile = ReadFile,
141 .writeFile = WriteFile,
142 .deleteFile = DeleteFile,
143 };
144 return &fileOperator;
145 }
146