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