1 /* 2 * Copyright (c) 2024 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 HNP_BASE_H 17 #define HNP_BASE_H 18 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <limits.h> 22 #include <stdbool.h> 23 24 #include "securec.h" 25 26 #ifndef HNP_CLI 27 28 #include "hilog/log.h" 29 #undef LOG_TAG 30 #define LOG_TAG "APPSPAWN_HNP" 31 #undef LOG_DOMAIN 32 #define LOG_DOMAIN (0xD002C00 + 0x11) 33 34 #endif 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 #ifndef MAX_FILE_PATH_LEN 41 #if PATH_MAX > 4096 42 #define MAX_FILE_PATH_LEN PATH_MAX 43 #else 44 #define MAX_FILE_PATH_LEN 4096 45 #endif 46 #endif 47 48 #define HNP_VERSION_LEN 32 49 #define BUFFER_SIZE 1024 50 #define MAX_PACKAGE_HNP_NUM 256 51 52 #define HNP_CFG_FILE_NAME "hnp.json" 53 #define HNP_PACKAGE_INFO_JSON_FILE_PATH "/data/service/el1/startup/hnp_info.json" 54 #define HNP_ELF_FILE_CHECK_HEAD_LEN 4 55 56 #ifdef _WIN32 57 #define DIR_SPLIT_SYMBOL '\\' 58 #else 59 #define DIR_SPLIT_SYMBOL '/' 60 #endif 61 62 #ifndef APPSPAWN_TEST 63 #define APPSPAWN_STATIC static 64 #else 65 #define APPSPAWN_STATIC 66 #endif 67 68 /* Native软件二进制软链接配置 */ 69 typedef struct NativeBinLinkStru { 70 char source[MAX_FILE_PATH_LEN]; 71 char target[MAX_FILE_PATH_LEN]; 72 } NativeBinLink; 73 74 /* hnp配置文件信息 */ 75 typedef struct HnpCfgInfoStru { 76 char name[MAX_FILE_PATH_LEN]; 77 char version[HNP_VERSION_LEN]; // Native软件包版本号 78 unsigned int linkNum; // 软链接配置个数 79 NativeBinLink *links; 80 } HnpCfgInfo; 81 82 /* hnp package文件信息 */ 83 typedef struct HnpPackageInfoStru { 84 char name[MAX_FILE_PATH_LEN]; 85 char version[HNP_VERSION_LEN]; // Native软件包版本号 86 } HnpPackageInfo; 87 88 /* 日志级别 */ 89 typedef enum { 90 HNP_LOG_INFO = 0, 91 HNP_LOG_WARN = 1, 92 HNP_LOG_ERROR = 2, 93 HNP_LOG_DEBUG = 3, 94 HNP_LOG_BUTT 95 } HNP_LOG_LEVEL_E; 96 97 /* 安装验签 */ 98 typedef struct HnpSignMapInfoStru { 99 char key[MAX_FILE_PATH_LEN]; 100 char value[MAX_FILE_PATH_LEN]; 101 } HnpSignMapInfo; 102 103 /* 数字索引 */ 104 enum { 105 HNP_INDEX_0 = 0, 106 HNP_INDEX_1, 107 HNP_INDEX_2, 108 HNP_INDEX_3, 109 HNP_INDEX_4, 110 HNP_INDEX_5, 111 HNP_INDEX_6, 112 HNP_INDEX_7 113 }; 114 115 // 错误码生成 116 #define HNP_ERRNO_HNP_MID 0x80 117 #define HNP_ERRNO_HIGH16_MAKE() (HNP_ERRNO_HNP_MID << 16) 118 #define HNP_ERRNO_LOW16_MAKE(Mid, Errno) (((Mid) << 8) + (Errno)) 119 #define HNP_ERRNO_COMMON(Mid, Errno) (HNP_ERRNO_HIGH16_MAKE() | HNP_ERRNO_LOW16_MAKE(Mid, Errno)) 120 121 #define HNP_ERRNO_PARAM_INVALID 0x22 122 #define HNP_ERRNO_NOMEM 0x23 123 124 enum { 125 HNP_MID_MAIN = 0x10, 126 HNP_MID_BASE = 0x11, 127 HNP_MID_PACK = 0x12, 128 HNP_MID_INSTALLER = 0x13 129 }; 130 131 /* hnp_main模块*/ 132 // 0x801001 操作类型非法 133 #define HNP_ERRNO_OPERATOR_TYPE_INVALID HNP_ERRNO_COMMON(HNP_MID_MAIN, 0x1) 134 135 // 0x801002 缺少必要的操作参数 136 #define HNP_ERRNO_OPERATOR_ARGV_MISS HNP_ERRNO_COMMON(HNP_MID_MAIN, 0x2) 137 138 /* hnp_base模块*/ 139 // 0x801101 打开文件失败 140 #define HNP_ERRNO_BASE_FILE_OPEN_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1) 141 142 // 0x801102 读取文件失败 143 #define HNP_ERRNO_BASE_FILE_READ_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x2) 144 145 // 0x801103 fseek设置失败 146 #define HNP_ERRNO_BASE_FILE_SEEK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x3) 147 148 // 0x801104 ftell设置失败 149 #define HNP_ERRNO_BASE_FILE_TELL_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x4) 150 151 // 0x801105 realpath失败 152 #define HNP_ERRNO_BASE_REALPATHL_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x5) 153 154 // 0x801106 获取文件大小为0 155 #define HNP_ERRNO_BASE_GET_FILE_LEN_NULL HNP_ERRNO_COMMON(HNP_MID_BASE, 0x6) 156 157 // 0x801107 字符串大小超出限制 158 #define HNP_ERRNO_BASE_STRING_LEN_OVER_LIMIT HNP_ERRNO_COMMON(HNP_MID_BASE, 0x7) 159 160 // 0x801108 目录打开失败 161 #define HNP_ERRNO_BASE_DIR_OPEN_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x8) 162 163 // 0x801109 sprintf拼装失败 164 #define HNP_ERRNO_BASE_SPRINTF_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x9) 165 166 // 0x80110a 生成压缩文件失败 167 #define HNP_ERRNO_BASE_CREATE_ZIP_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xa) 168 169 // 0x80110b 写文件失败 170 #define HNP_ERRNO_BASE_FILE_WRITE_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xb) 171 172 // 0x80110c 拷贝失败 173 #define HNP_ERRNO_BASE_COPY_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xc) 174 175 // 0x80110d 获取文件属性失败 176 #define HNP_ERRNO_GET_FILE_ATTR_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xd) 177 178 // 0x80110e 解压缩打开文件失败 179 #define HNP_ERRNO_BASE_UNZIP_OPEN_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xe) 180 181 // 0x80110f 解压缩获取文件信息失败 182 #define HNP_ERRNO_BASE_UNZIP_GET_INFO_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0xf) 183 184 // 0x801110 解压缩获取文件信息失败 185 #define HNP_ERRNO_BASE_UNZIP_READ_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x10) 186 187 // 0x801111 生成软链接失败 188 #define HNP_ERRNO_GENERATE_SOFT_LINK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x11) 189 190 // 0x801112 进程正在运行 191 #define HNP_ERRNO_PROCESS_RUNNING HNP_ERRNO_COMMON(HNP_MID_BASE, 0x12) 192 193 // 0x801113 入参失败 194 #define HNP_ERRNO_BASE_PARAMS_INVALID HNP_ERRNO_COMMON(HNP_MID_BASE, 0x13) 195 196 // 0x801114 strdup失败 197 #define HNP_ERRNO_BASE_STRDUP_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x14) 198 199 // 0x801115 设置权限失败 200 #define HNP_ERRNO_BASE_CHMOD_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x15) 201 202 // 0x801116 删除目录失败 203 #define HNP_ERRNO_BASE_UNLINK_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x16) 204 205 // 0x801117 对应进程不存在 206 #define HNP_ERRNO_BASE_PROCESS_NOT_FOUND HNP_ERRNO_COMMON(HNP_MID_BASE, 0x17) 207 208 // 0x801118 创建路径失败 209 #define HNP_ERRNO_BASE_MKDIR_PATH_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x18) 210 211 // 0x801119 读取配置文件流失败 212 #define HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x19) 213 214 // 0x80111a 解析json信息失败 215 #define HNP_ERRNO_BASE_PARSE_JSON_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1a) 216 217 // 0x80111b 未找到json项 218 #define HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1b) 219 220 // 0x80111c 解析json数组失败 221 #define HNP_ERRNO_BASE_GET_ARRAY_ITRM_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1c) 222 223 // 0x80111d 内存拷贝失败 224 #define HNP_ERRNO_BASE_MEMCPY_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1d) 225 226 // 0x80111e 创建json数组失败 227 #define HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1e) 228 229 // 0x80111f 获取文件属性失败 230 #define HNP_ERRNO_BASE_STAT_FAILED HNP_ERRNO_COMMON(HNP_MID_BASE, 0x1f) 231 232 // 0x801120 二进制文件过多 233 #define HNP_ERRNO_BASE_FILE_COUNT_OVER HNP_ERRNO_COMMON(HNP_MID_BASE, 0x20) 234 235 int GetFileSizeByHandle(FILE *file, int *size); 236 237 int ReadFileToStream(const char *filePath, char **stream, int *streamLen); 238 239 int GetRealPath(char *srcPath, char *realPath); 240 241 int HnpZip(const char *inputDir, const char *outputFile); 242 243 int HnpUnZip(const char *inputFile, const char *outputDir, const char *hnpSignKeyPrefix, 244 HnpSignMapInfo *hnpSignMapInfos, int *count); 245 246 int HnpAddFileToZip(char *zipfile, char *filename, char *buff, int size); 247 248 void HnpLogPrintf(int logLevel, char *module, const char *format, ...); 249 250 int HnpCfgGetFromZip(const char *inputFile, HnpCfgInfo *hnpCfg); 251 252 int HnpSymlink(const char *srcFile, const char *dstFile); 253 254 int HnpProcessRunCheck(const char *runPath); 255 256 int HnpDeleteFolder(const char *path); 257 258 int HnpCreateFolder(const char* path); 259 260 261 int ParseHnpCfgFile(const char *hnpCfgPath, HnpCfgInfo *hnpCfg); 262 263 int GetHnpJsonBuff(HnpCfgInfo *hnpCfg, char **buff); 264 265 int HnpCfgGetFromSteam(char *cfgStream, HnpCfgInfo *hnpCfg); 266 267 int HnpInstallInfoJsonWrite(const char *hapPackageName, const HnpCfgInfo *hnpCfg); 268 269 int HnpPackageInfoGet(const char *packageName, HnpPackageInfo **packageInfoOut, int *count); 270 271 int HnpPackageInfoHnpDelete(const char *packageName, const char *name, const char *version); 272 273 int HnpPackageInfoDelete(const char *packageName); 274 275 char *HnpPackgeHnpVersionGet(const char *packageName, const char *name); 276 277 int HnpFileCountGet(const char *path, int *count); 278 279 #ifdef HNP_CLI 280 #define HNP_LOGI(args, ...) \ 281 HnpLogPrintf(HNP_LOG_INFO, "HNP", "[%{public}s:%{public}d]" args, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__) 282 #else 283 #define HNP_LOGI(args, ...) \ 284 HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" args, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__); \ 285 HnpLogPrintf(HNP_LOG_INFO, "HNP", "[%{public}s:%{public}d]" args, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__) 286 #endif 287 288 #ifdef HNP_CLI 289 #define HNP_LOGE(args, ...) \ 290 HnpLogPrintf(HNP_LOG_ERROR, "HNP", "[%{public}s:%{public}d]" args, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__) 291 #else 292 #define HNP_LOGE(args, ...) \ 293 HILOG_ERROR(LOG_CORE, "[%{public}s:%{public}d]" args, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__); \ 294 HnpLogPrintf(HNP_LOG_ERROR, "HNP", "[%{public}s:%{public}d]" args, (__FILE_NAME__), (__LINE__), ##__VA_ARGS__) 295 #endif 296 297 #ifdef __cplusplus 298 } 299 #endif 300 301 #endif