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