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 
16 #include "hks_test_file_operator_c.h"
17 
18 
GetFileName(const char * path,const char * fileName,char * fullFileName,uint32_t fullFileNameLen)19 int32_t GetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen)
20 {
21     if (path != NULL) {
22         if (strncpy_s(fullFileName, fullFileNameLen, path, strlen(path)) != EOK) {
23             return HKS_ERROR_INTERNAL_ERROR;
24         }
25 
26         if (path[strlen(path) - 1] != '/') {
27             if (strncat_s(fullFileName, fullFileNameLen, "/", strlen("/")) != EOK) {
28                 return HKS_ERROR_INTERNAL_ERROR;
29             }
30         }
31 
32         if (strncat_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) {
33             return HKS_ERROR_INTERNAL_ERROR;
34         }
35     } else {
36         if (strncpy_s(fullFileName, fullFileNameLen, fileName, strlen(fileName)) != EOK) {
37             return HKS_ERROR_INTERNAL_ERROR;
38         }
39     }
40 
41     return HKS_SUCCESS;
42 }
43 
IsFileExist(const char * fileName)44 int32_t IsFileExist(const char *fileName)
45 {
46     if (access(fileName, F_OK) != 0) {
47         return HKS_ERROR_NOT_EXIST;
48     }
49 
50     return HKS_SUCCESS;
51 }
52 
FileRead(const char * fileName,uint32_t offset,struct HksBlob * blob,uint32_t * size)53 int32_t FileRead(const char *fileName, uint32_t offset, struct HksBlob *blob, uint32_t *size)
54 {
55     (void)offset;
56     if (IsFileExist(fileName) != HKS_SUCCESS) {
57         return HKS_ERROR_NOT_EXIST;
58     }
59 
60     char filePath[PATH_MAX + 1] = {0};
61     (void)realpath(fileName, filePath);
62     if (strstr(filePath, "../") != NULL) {
63         HKS_TEST_LOG_E("invalid filePath, path %s", filePath);
64         return HKS_ERROR_INVALID_ARGUMENT;
65     }
66 
67     FILE *fp = fopen(filePath, "rb");
68     if (fp ==  NULL) {
69         if (errno == REQUIRED_KEY_NOT_AVAILABLE) {
70             HKS_TEST_LOG_E("Check Permission failed!");
71             return HKS_ERROR_NO_PERMISSION;
72         }
73         HKS_TEST_LOG_E("open file fail, errno = 0x%x", errno);
74         return HKS_ERROR_OPEN_FILE_FAIL;
75     }
76 
77     uint32_t len = fread(blob->data, 1, blob->size, fp);
78     if (fclose(fp) < 0) {
79         HKS_TEST_LOG_E("failed to close file");
80         return HKS_ERROR_CLOSE_FILE_FAIL;
81     }
82     *size = len;
83     return HKS_SUCCESS;
84 }
85 
FileSize(const char * fileName)86 uint32_t FileSize(const char *fileName)
87 {
88     if (IsFileExist(fileName) != HKS_SUCCESS) {
89         return 0;
90     }
91 
92     struct stat fileStat;
93     (void)memset_s(&fileStat, sizeof(fileStat), 0, sizeof(fileStat));
94     if (stat(fileName, &fileStat) != 0) {
95         HKS_TEST_LOG_E("file stat fail.");
96         return 0;
97     }
98 
99     return fileStat.st_size;
100 }
101 
FileWrite(const char * fileName,uint32_t offset,const uint8_t * buf,uint32_t len)102 int32_t FileWrite(const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len)
103 {
104     (void)offset;
105     char filePath[PATH_MAX + 1] = {0};
106     if (memcpy_s(filePath, sizeof(filePath) - 1, fileName, strlen(fileName)) != EOK) {
107         return HKS_ERROR_BAD_STATE;
108     }
109     (void)realpath(fileName, filePath);
110     if (strstr(filePath, "../") != NULL) {
111         HKS_TEST_LOG_E("invalid filePath, path %s", filePath);
112         return HKS_ERROR_INVALID_KEY_FILE;
113     }
114 
115     /* caller function ensures that the folder exists */
116     FILE *fp = fopen(filePath, "wb+");
117     if (fp == NULL) {
118         HKS_TEST_LOG_E("open file fail");
119         return HKS_ERROR_OPEN_FILE_FAIL;
120     }
121 
122     if (chmod(filePath, S_IRUSR | S_IWUSR) < 0) {
123         HKS_TEST_LOG_E("chmod file fail.");
124         fclose(fp);
125         return HKS_ERROR_OPEN_FILE_FAIL;
126     }
127 
128     uint32_t size = fwrite(buf, 1, len, fp);
129     if (size != len) {
130         HKS_TEST_LOG_E("write file size fail.");
131         fclose(fp);
132         return HKS_ERROR_WRITE_FILE_FAIL;
133     }
134 
135     if (fclose(fp) < 0) {
136         HKS_TEST_LOG_E("failed to close file");
137         return HKS_ERROR_CLOSE_FILE_FAIL;
138     }
139 
140     return HKS_SUCCESS;
141 }