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