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