• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #ifndef __LITEOS_M__
16 
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <limits.h>
21 #include <securec.h>
22 #include "attest_utils_log.h"
23 #include "attest_utils.h"
24 #include "attest_utils_file.h"
25 
IsOverTemperatureLimit(void)26 bool IsOverTemperatureLimit(void)
27 {
28     return false;
29 }
30 
GetFileSize(const char * path,const char * fileName,uint32_t * result)31 int32_t GetFileSize(const char* path, const char* fileName, uint32_t* result)
32 {
33     if (path == NULL || fileName == NULL || result == NULL) {
34         ATTEST_LOG_ERROR("[GetFileSize] Invalid parameter");
35         return ATTEST_ERR;
36     }
37 
38     char* filePath = GenFilePath(path, fileName);
39     if (filePath == NULL) {
40         ATTEST_LOG_ERROR("[GetFileSize] Generate file path failed");
41         return ATTEST_ERR;
42     }
43 
44     char* formatPath = realpath(filePath, NULL);
45     ATTEST_MEM_FREE(filePath);
46     if (formatPath == NULL) {
47         ATTEST_LOG_ERROR("[GetFileSize] Invalid path of %s/%s", path, fileName);
48         return ATTEST_ERR;
49     }
50 
51     FILE* fp = fopen(formatPath, "r");
52     if (fp == NULL) {
53         ATTEST_LOG_ERROR("[GetFileSize] open file %s failed", formatPath);
54         free(formatPath);
55         return ATTEST_ERR;
56     }
57     if (fseek(fp, 0, SEEK_END) < 0) {
58         ATTEST_LOG_ERROR("[GetFileSize] seek file %s failed", formatPath);
59         free(formatPath);
60         (void)fclose(fp);
61         return ATTEST_ERR;
62     }
63     *result = ftell(fp);
64     free(formatPath);
65     (void)fclose(fp);
66     return ATTEST_OK;
67 }
68 
WriteFile(const char * path,const char * fileName,const char * data,uint32_t dataLen)69 int32_t WriteFile(const char* path, const char* fileName, const char* data, uint32_t dataLen)
70 {
71     if (path == NULL || fileName == NULL || data == NULL || dataLen == 0) {
72         ATTEST_LOG_ERROR("[WriteFile] Invalid parameter");
73         return ATTEST_ERR;
74     }
75     char* filePath = GenFilePath(path, fileName);
76     if (filePath == NULL) {
77         ATTEST_LOG_ERROR("[WriteFile] Generate file path failed");
78         return ATTEST_ERR;
79     }
80 
81     char* formatPath = realpath(filePath, NULL);
82     ATTEST_MEM_FREE(filePath);
83     if (formatPath == NULL) {
84         ATTEST_LOG_ERROR("[WriteFile] Invalid path of %s", path);
85         return ATTEST_ERR;
86     }
87 
88     FILE* fp = fopen(formatPath, "wb+");
89     if (fp == NULL) {
90         ATTEST_LOG_ERROR("[WriteFile] open file %s failed", formatPath);
91         free(formatPath);
92         return ATTEST_ERR;
93     }
94     int32_t ret = ATTEST_OK;
95     do {
96         if (chmod(formatPath, S_IRUSR | S_IWUSR) < 0) { // 文件权限改为600
97             ATTEST_LOG_ERROR("[WriteFile] chmod file failed");
98             ret = ATTEST_ERR;
99             break;
100         }
101         if (fwrite(data, dataLen, 1, fp) != 1) {
102             ATTEST_LOG_ERROR("[WriteFile] write file %s failed", formatPath);
103             ret = ATTEST_ERR;
104             break;
105         }
106         if (fflush(fp) != ATTEST_OK) {
107             ret = ATTEST_ERR;
108             break;
109         }
110         int fd = fileno(fp);
111         if (fsync(fd) != ATTEST_OK) {
112             ret = ATTEST_ERR;
113             break;
114         }
115     } while (0);
116     free(formatPath);
117     (void)fclose(fp);
118     return ret;
119 }
120 
ReadFile(const char * path,const char * fileName,char * buffer,uint32_t bufferLen)121 int32_t ReadFile(const char* path, const char* fileName, char* buffer, uint32_t bufferLen)
122 {
123     if (path == NULL || fileName == NULL || buffer == NULL || bufferLen == 0) {
124         ATTEST_LOG_ERROR("[ReadFile] Invalid parameter");
125         return ATTEST_ERR;
126     }
127 
128     uint32_t fileSize = 0;
129     if (GetFileSize(path, fileName, &fileSize) != 0 || fileSize > bufferLen) {
130         ATTEST_LOG_ERROR("[ReadFile] Invalid fileSize");
131         return ATTEST_ERR;
132     }
133 
134     char* filePath = GenFilePath(path, fileName);
135     if (filePath == NULL) {
136         ATTEST_LOG_ERROR("[ReadFile] Generate file path failed");
137         return ATTEST_ERR;
138     }
139 
140     char* formatPath = realpath(filePath, NULL);
141     ATTEST_MEM_FREE(filePath);
142     if (formatPath == NULL) {
143         ATTEST_LOG_ERROR("[ReadFile] Invalid path of %s or file %s not exist", path, fileName);
144         return ATTEST_ERR;
145     }
146 
147     FILE* fp = fopen(formatPath, "rb");
148     if (fp == NULL) {
149         ATTEST_LOG_ERROR("[ReadFile] open file %s failed", formatPath);
150         free(formatPath);
151         return ATTEST_ERR;
152     }
153     if (fread(buffer, fileSize, 1, fp) != 1) {
154         ATTEST_LOG_ERROR("[ReadFile] read file %s data from device failed", formatPath);
155         free(formatPath);
156         (void)fclose(fp);
157         return ATTEST_ERR;
158     }
159     free(formatPath);
160     (void)fclose(fp);
161     return ATTEST_OK;
162 }
163 
CreateFile(const char * path,const char * fileName)164 int32_t CreateFile(const char* path, const char* fileName)
165 {
166     if (path == NULL || fileName == NULL) {
167         return ATTEST_ERR;
168     }
169 
170     char* formatPath = realpath(path, NULL);
171     if (formatPath == NULL) {
172         ATTEST_LOG_ERROR("[CreateFile] Invalid path of %s or file %s not exist", path, fileName);
173         return ATTEST_ERR;
174     }
175     uint32_t realPathLen = strlen(formatPath) + 1 + strlen(fileName) + 1;
176     if (realPathLen > PATH_MAX) {
177         return ATTEST_ERR;
178     }
179     char* realPath = (char *)ATTEST_MEM_MALLOC(realPathLen);
180     if (realPath == NULL) {
181         free(formatPath);
182         return ATTEST_ERR;
183     }
184     if (sprintf_s(realPath, realPathLen, "%s%s%s", formatPath, "/", fileName) < 0) {
185         free(formatPath);
186         ATTEST_MEM_FREE(realPath);
187         return ATTEST_ERR;
188     }
189     free(formatPath);
190 
191     FILE* fp = fopen(realPath, "w");
192     if (fp == NULL) {
193         ATTEST_MEM_FREE(realPath);
194         return ATTEST_ERR;
195     }
196     ATTEST_MEM_FREE(realPath);
197     int32_t ret = ATTEST_OK;
198     do {
199         if (fflush(fp) != ATTEST_OK) {
200             ret = ATTEST_ERR;
201             break;
202         }
203         int fd = fileno(fp);
204         if (fsync(fd) != ATTEST_OK) {
205             ret = ATTEST_ERR;
206             break;
207         }
208     } while (0);
209     (void)fclose(fp);
210     return ret;
211 }
212 
IsFileExist(const char * path,const char * fileName)213 bool IsFileExist(const char* path, const char* fileName)
214 {
215     if (path == NULL || fileName == NULL) {
216         return false;
217     }
218     char* filePath = GenFilePath(path, fileName);
219     if (filePath == NULL) {
220         ATTEST_LOG_ERROR("[IsFileExist] Generate file path failed");
221         return false;
222     }
223     char* formatPath = realpath(filePath, NULL);
224     ATTEST_MEM_FREE(filePath);
225     if (formatPath == NULL) {
226         ATTEST_LOG_ERROR("[IsFileExist] Invalid path of %s or file %s not exist", path, fileName);
227         return false;
228     }
229 
230     FILE* fp = fopen(formatPath, "r");
231     if (fp == NULL) {
232         free(formatPath);
233         return false;
234     }
235     free(formatPath);
236     (void)fclose(fp);
237     return true;
238 }
239 
DeleteFile(const char * path,const char * fileName)240 bool DeleteFile(const char* path, const char* fileName)
241 {
242     if (path == NULL || fileName == NULL) {
243         return false;
244     }
245     if (strlen(path) == 0 || strlen(fileName) == 0) {
246         return false;
247     }
248     char* filePath = GenFilePath(path, fileName);
249     if (filePath == NULL) {
250         return false;
251     }
252     int32_t ret = remove(filePath);
253     ATTEST_MEM_FREE(filePath);
254     if (ret != 0) {
255         return false;
256     }
257     return true;
258 }
259 #endif
260