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 #ifndef HKS_FILE_OPERATOR_H 17 #define HKS_FILE_OPERATOR_H 18 19 #include "hks_type.h" 20 21 #ifdef HKS_CONFIG_FILE 22 #include HKS_CONFIG_FILE 23 #else 24 #include "hks_config.h" 25 #endif 26 27 #define HKS_MAX_FILE_NAME_LEN 512 28 29 #ifdef L2_STANDARD 30 #define HKS_KEY_STORE_PATH HKS_CONFIG_KEY_STORE_PATH "/maindata" 31 #define HKS_KEY_STORE_BAK_PATH HKS_CONFIG_KEY_STORE_PATH "/bakdata" 32 #else 33 #ifdef HKS_L1_SMALL 34 #define HKS_KEY_STORE_PATH "/storage/data/service/el1/public/huks_service/maindata" 35 #define HKS_KEY_STORE_BAK_PATH "/storage/data/service/el1/public/huks_service/bakdata" 36 #else 37 #ifdef _STORAGE_LITE_ 38 #define HKS_KEY_STORE_PATH HKS_CONFIG_KEY_STORE_PATH 39 #else 40 #ifdef HKS_ENABLE_LITE_HAP 41 #define HKS_KEY_STORE_LITE_HAP HKS_CONFIG_LITE_HAP_STORE_PATH 42 #endif 43 #ifdef HKS_CONFIG_KEY_STORE_PATH 44 #define HKS_KEY_STORE_PATH HKS_CONFIG_KEY_STORE_PATH "/maindata" 45 #define HKS_KEY_STORE_BAK_PATH HKS_CONFIG_KEY_STORE_PATH "/bakdata" 46 #else 47 #define HKS_KEY_STORE_PATH "/storage/maindata" 48 #define HKS_KEY_STORE_BAK_PATH "/storage/bakdata" 49 #endif 50 #endif 51 #endif 52 #endif 53 #define HKS_KEY_STORE_KEY_PATH "key" 54 #define HKS_KEY_STORE_CERTCHAIN_PATH "certchain" 55 #define HKS_KEY_STORE_ROOT_KEY_PATH "info" 56 57 #define HKS_PROCESS_INFO_LEN 128 58 #define HKS_MAX_DIRENT_FILE_LEN 128 59 struct HksFileDirentInfo { 60 char fileName[HKS_MAX_DIRENT_FILE_LEN]; /* point to dirent->d_name */ 61 }; 62 63 enum HksStoragePathType { 64 HKS_STORAGE_MAIN_PATH, 65 HKS_STORAGE_BACKUP_PATH, 66 #ifdef HKS_ENABLE_LITE_HAP 67 HKS_STORAGE_LITE_HAP_PATH, 68 #endif 69 }; 70 71 #ifdef __cplusplus 72 extern "C" { 73 #endif 74 75 uint32_t HksFileRead(const char *path, const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len); 76 77 int32_t HksFileWrite(const char *path, const char *fileName, uint32_t offset, const uint8_t *buf, uint32_t len); 78 79 int32_t HksFileRemove(const char *path, const char *fileName); 80 81 uint32_t HksFileSize(const char *path, const char *fileName); 82 83 int32_t HksIsFileExist(const char *path, const char *fileName); 84 85 int32_t HksIsDirExist(const char *path); 86 87 int32_t HksMakeDir(const char *path); 88 89 void *HksOpenDir(const char *path); 90 91 int32_t HksCloseDir(void *dirp); 92 93 int32_t HksGetDirFile(void *dirp, struct HksFileDirentInfo *direntInfo); 94 95 int32_t HksRemoveDir(const char *dirPath); 96 97 int32_t HksDeleteDir(const char *path); 98 99 /* return < 0 error; > 0 realFileSize; == 0 no data */ 100 int32_t HksOldVersionFileRead(const char *fileName, uint32_t offset, uint8_t *buf, uint32_t len); 101 102 int32_t HksOldVersionFileRemove(const char *fileName); 103 104 /* return < 0 error; >= 0 realFileSize */ 105 int32_t HksOldVersionFileSize(const char *fileName); 106 107 /* return true, exist; false not exist */ 108 bool HksOldVersionIsFileExist(const char *fileName); 109 110 int32_t HksGetStoragePath(enum HksStoragePathType pathType, char *path, uint32_t *len); 111 112 int32_t HksGetFileName(const char *path, const char *fileName, char *fullFileName, uint32_t fullFileNameLen); 113 114 #ifdef __cplusplus 115 } 116 #endif 117 118 #endif /* HKS_FILE_OPERATOR_H */ 119