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 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include "cJSON.h"
20 #include "securec.h"
21 
22 #include "hnp_base.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
ParseLinksJsonToCfgInfo(cJSON * linksItem,HnpCfgInfo * hnpCfg)28 static int ParseLinksJsonToCfgInfo(cJSON *linksItem, HnpCfgInfo *hnpCfg)
29 {
30     NativeBinLink *linkArray = NULL;
31 
32     int linkArrayNum = cJSON_GetArraySize(linksItem);
33     if (linkArrayNum > 0) {
34         hnpCfg->linkNum = (size_t)linkArrayNum;
35         linkArray = (NativeBinLink*)malloc(sizeof(NativeBinLink) * linkArrayNum);
36         if (linkArray == NULL) {
37             HNP_LOGE("malloc unsuccess.");
38             return HNP_ERRNO_NOMEM;
39         }
40         for (int i = 0; i < linkArrayNum; i++) {
41             cJSON *link = cJSON_GetArrayItem(linksItem, i);
42             if (link == NULL) {
43                 free(linkArray);
44                 return HNP_ERRNO_BASE_GET_ARRAY_ITRM_FAILED;
45             }
46             cJSON *sourceItem = cJSON_GetObjectItem(link, "source");
47             if ((sourceItem == NULL) || (sourceItem->valuestring == NULL)) {
48                 HNP_LOGE("get source info in cfg unsuccess.");
49                 free(linkArray);
50                 return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND;
51             }
52             if (strcpy_s(linkArray[i].source, MAX_FILE_PATH_LEN, sourceItem->valuestring) != EOK) {
53                 HNP_LOGE("strcpy unsuccess.");
54                 free(linkArray);
55                 return HNP_ERRNO_BASE_COPY_FAILED;
56             }
57             linkArray[i].target[0] = '\0';  //允许target不填,软链接默认使用原二进制名称
58             cJSON *targetItem = cJSON_GetObjectItem(link, "target");
59             if ((targetItem != NULL) && (targetItem->valuestring != NULL) &&
60                 (strcpy_s(linkArray[i].target, MAX_FILE_PATH_LEN, targetItem->valuestring) != EOK)) {
61                 HNP_LOGE("strcpy unsuccess.");
62                 free(linkArray);
63                 return HNP_ERRNO_BASE_COPY_FAILED;
64             }
65         }
66         hnpCfg->links = linkArray;
67     } else {
68         hnpCfg->linkNum = 0;
69     }
70     return 0;
71 }
72 
ParseJsonStreamToHnpCfgInfo(cJSON * json,HnpCfgInfo * hnpCfg)73 static int ParseJsonStreamToHnpCfgInfo(cJSON *json, HnpCfgInfo *hnpCfg)
74 {
75     int ret;
76 
77     cJSON *typeItem = cJSON_GetObjectItem(json, "type");
78     if ((typeItem == NULL) || (typeItem->valuestring == NULL)) {
79         HNP_LOGE("get type info in cfg unsuccess.");
80         return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND;
81     }
82     if (strcmp(typeItem->valuestring, "hnp-config") != 0) {
83         HNP_LOGE("type info not match.type=%{public}s", typeItem->valuestring);
84         return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND;
85     }
86     cJSON *nameItem = cJSON_GetObjectItem(json, "name");
87     if ((nameItem == NULL) || (nameItem->valuestring == NULL)) {
88         HNP_LOGE("get name info in cfg unsuccess.");
89         return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND;
90     }
91     ret = strcpy_s(hnpCfg->name, MAX_FILE_PATH_LEN, nameItem->valuestring);
92     if (ret != EOK) {
93         HNP_LOGE("strcpy unsuccess.");
94         return HNP_ERRNO_BASE_COPY_FAILED;
95     }
96     cJSON *versionItem = cJSON_GetObjectItem(json, "version");
97     if ((versionItem == NULL) || (versionItem->valuestring == NULL)) {
98         HNP_LOGE("get version info in cfg unsuccess.");
99         return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND;
100     }
101     ret = strcpy_s(hnpCfg->version, HNP_VERSION_LEN, versionItem->valuestring);
102     if (ret != EOK) {
103         HNP_LOGE("strcpy unsuccess.");
104         return HNP_ERRNO_BASE_COPY_FAILED;
105     }
106     cJSON *installItem = cJSON_GetObjectItem(json, "install");
107     if (installItem == NULL) {
108         HNP_LOGE("get install info in cfg unsuccess.");
109         return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND;
110     }
111     cJSON *linksItem = cJSON_GetObjectItem(installItem, "links");
112     if (linksItem != NULL) {
113         ret = ParseLinksJsonToCfgInfo(linksItem, hnpCfg);
114         if (ret != 0) {
115             return ret;
116         }
117     } else {
118         hnpCfg->linkNum = 0;
119     }
120     return 0;
121 }
122 
ParseHnpCfgFile(const char * hnpCfgPath,HnpCfgInfo * hnpCfg)123 int ParseHnpCfgFile(const char *hnpCfgPath, HnpCfgInfo *hnpCfg)
124 {
125     int ret;
126     char *cfgStream = NULL;
127     cJSON *json;
128     int size;
129 
130     ret = ReadFileToStream(hnpCfgPath, &cfgStream, &size);
131     if (ret != 0) {
132         HNP_LOGE("read cfg file[%{public}s] unsuccess.", hnpCfgPath);
133         return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED;
134     }
135     json = cJSON_Parse(cfgStream);
136     free(cfgStream);
137     if (json == NULL) {
138         HNP_LOGE("parse json file[%{public}s] unsuccess.", hnpCfgPath);
139         return HNP_ERRNO_BASE_PARSE_JSON_FAILED;
140     }
141     ret = ParseJsonStreamToHnpCfgInfo(json, hnpCfg);
142     cJSON_Delete(json);
143 
144     return ret;
145 }
146 
HnpCfgGetFromSteam(char * cfgStream,HnpCfgInfo * hnpCfg)147 int HnpCfgGetFromSteam(char *cfgStream, HnpCfgInfo *hnpCfg)
148 {
149     cJSON *json;
150     int ret;
151 
152     if (cfgStream == NULL) {
153         HNP_LOGE("hnp cfg file not found.");
154         return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED;
155     }
156 
157     json = cJSON_Parse(cfgStream);
158     if (json == NULL) {
159         HNP_LOGE("parse json file unsuccess.");
160         return HNP_ERRNO_BASE_PARSE_JSON_FAILED;
161     }
162     ret = ParseJsonStreamToHnpCfgInfo(json, hnpCfg);
163     cJSON_Delete(json);
164 
165     return ret;
166 }
GetHnpJsonBuff(HnpCfgInfo * hnpCfg,char ** buff)167 int GetHnpJsonBuff(HnpCfgInfo *hnpCfg, char **buff)
168 {
169     cJSON *root = cJSON_CreateObject();
170 
171     cJSON_AddStringToObject(root, "type", "hnp-config");
172     cJSON_AddStringToObject(root, "name", hnpCfg->name);
173     cJSON_AddStringToObject(root, "version", hnpCfg->version);
174     cJSON_AddObjectToObject(root, "install");
175     char *str = cJSON_Print(root);
176     cJSON_Delete(root);
177 
178     *buff = str;
179     return 0;
180 }
181 
HnpInstallHapExistCheck(const char * hnpPackageName,cJSON * json,cJSON ** hapItemOut,int * hapIndex)182 static bool HnpInstallHapExistCheck(const char *hnpPackageName, cJSON *json, cJSON **hapItemOut, int *hapIndex)
183 {
184     cJSON *hapItem = NULL;
185     bool hapExist = false;
186     cJSON *hapJson = NULL;
187 
188     for (int i = 0; i < cJSON_GetArraySize(json); i++) {
189         hapItem = cJSON_GetArrayItem(json, i);
190         hapJson = cJSON_GetObjectItem(hapItem, "hap");
191         if ((hapJson != NULL) && (cJSON_IsString(hapJson)) && (strcmp(hapJson->valuestring, hnpPackageName) == 0)) {
192             hapExist = true;
193             *hapItemOut = hapItem;
194             *hapIndex = i;
195             break;
196         }
197     }
198 
199     return hapExist;
200 }
201 
HnpInstallHnpExistCheck(cJSON * hnpItemArr,const char * name,cJSON ** hnpItemOut,int * hnpIndex,const char * version)202 static bool HnpInstallHnpExistCheck(cJSON *hnpItemArr, const char *name, cJSON **hnpItemOut, int *hnpIndex,
203     const char *version)
204 {
205     if (hnpItemArr == NULL) {
206         return false;
207     }
208 
209     for (int i = 0; i < cJSON_GetArraySize(hnpItemArr); i++) {
210         cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, i);
211         cJSON *nameJson = cJSON_GetObjectItem(hnpItem, "name");
212         cJSON *versionJson = cJSON_GetObjectItem(hnpItem, "version");
213         if ((nameJson != NULL) && (strcmp(nameJson->valuestring, name) == 0)) {
214             *hnpItemOut = hnpItem;
215             *hnpIndex = i;
216             if (version == NULL) {
217                 return true;
218             }
219             if ((versionJson != NULL) && (strcmp(versionJson->valuestring, version) == 0)) {
220                 return true;
221             }
222         }
223     }
224 
225     return false;
226 }
227 
HnpPackageVersionUpdateAll(cJSON * json,const HnpCfgInfo * hnpCfg)228 static void HnpPackageVersionUpdateAll(cJSON *json, const HnpCfgInfo *hnpCfg)
229 {
230     for (int i = 0; i < cJSON_GetArraySize(json); i++) {
231         cJSON *hapItem = cJSON_GetArrayItem(json, i);
232         cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
233         for (int j = 0; j < cJSON_GetArraySize(hnpItemArr); j++) {
234             cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, j);
235             cJSON *nameItem = cJSON_GetObjectItem(hnpItem, "name");
236             if ((nameItem == NULL) ||
237                 ((cJSON_IsString(nameItem)) && (strcmp(nameItem->valuestring, hnpCfg->name) != 0))) {
238                 continue;
239             }
240             cJSON *version = cJSON_GetObjectItem(hnpItem, "version");
241             if (version == NULL) {
242                 break;
243             }
244             cJSON_SetValuestring(version, hnpCfg->version);
245             break;
246         }
247     }
248 
249     return;
250 }
251 
HnpHapJsonWrite(cJSON * json)252 static int HnpHapJsonWrite(cJSON *json)
253 {
254     FILE *fp = fopen(HNP_PACKAGE_INFO_JSON_FILE_PATH, "wb");
255     if (fp == NULL) {
256         HNP_LOGE("open file:%{public}s unsuccess!", HNP_PACKAGE_INFO_JSON_FILE_PATH);
257         return HNP_ERRNO_BASE_FILE_OPEN_FAILED;
258     }
259     char *jsonStr = cJSON_Print(json);
260     size_t jsonStrSize = strlen(jsonStr);
261     size_t writeLen = fwrite(jsonStr, sizeof(char), jsonStrSize, fp);
262     (void)fclose(fp);
263     free(jsonStr);
264     if (writeLen != jsonStrSize) {
265         HNP_LOGE("package info write file:%{public}s unsuccess!", HNP_PACKAGE_INFO_JSON_FILE_PATH);
266         return HNP_ERRNO_BASE_FILE_WRITE_FAILED;
267     }
268 
269     return 0;
270 }
271 
HnpHapJsonHnpAdd(bool hapExist,cJSON * json,cJSON * hapItem,const char * hnpPackageName,const HnpCfgInfo * hnpCfg)272 static int HnpHapJsonHnpAdd(bool hapExist, cJSON *json, cJSON *hapItem, const char *hnpPackageName,
273     const HnpCfgInfo *hnpCfg)
274 {
275     cJSON *hnpItemArr = NULL;
276     int ret;
277 
278     if (hapExist) {
279         hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
280         if (hnpItemArr == NULL) {
281             HNP_LOGE("hnp item array get unsuccess");
282             return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND;
283         }
284     } else {
285         hapItem = cJSON_CreateObject();
286         if (hapItem == NULL) {
287             HNP_LOGE("hnp json write create hap object unsuccess");
288             return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED;
289         }
290         cJSON_AddStringToObject(hapItem, "hap", hnpPackageName);
291         hnpItemArr = cJSON_CreateArray();
292         if (hnpItemArr == NULL) {
293             HNP_LOGE("hnp json write array create unsuccess");
294             cJSON_Delete(hapItem);
295             return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED;
296         }
297         cJSON_AddItemToObject(hapItem, "hnp", hnpItemArr);
298         cJSON_AddItemToArray(json, hapItem);
299     }
300 
301     cJSON *hnpItem = cJSON_CreateObject();
302     if (hnpItem == NULL) {
303         HNP_LOGE("hnp json write create hnp object unsuccess");
304         return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED;
305     }
306     cJSON_AddItemToObject(hnpItem, "name", cJSON_CreateString(hnpCfg->name));
307     cJSON_AddItemToObject(hnpItem, "version", cJSON_CreateString(hnpCfg->version));
308     cJSON_AddItemToArray(hnpItemArr, hnpItem);
309 
310     HnpPackageVersionUpdateAll(json, hnpCfg);
311 
312     ret = HnpHapJsonWrite(json);
313     return ret;
314 }
315 
HnpInstallInfoJsonWrite(const char * hapPackageName,const HnpCfgInfo * hnpCfg)316 int HnpInstallInfoJsonWrite(const char *hapPackageName, const HnpCfgInfo *hnpCfg)
317 {
318     bool hapExist = false;
319     int hapIndex = 0;
320     int hnpIndex = 0;
321     char *infoStream;
322     int size;
323     cJSON *hapItem = NULL;
324     cJSON *hnpItem = NULL;
325     cJSON *json = NULL;
326 
327     if ((hapPackageName == NULL) || (hnpCfg == NULL)) {
328         return HNP_ERRNO_BASE_PARAMS_INVALID;
329     }
330 
331     int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size);
332     if (ret != 0) {
333         if ((ret == HNP_ERRNO_BASE_FILE_OPEN_FAILED) || (ret == HNP_ERRNO_BASE_GET_FILE_LEN_NULL)) {
334             if ((json = cJSON_CreateArray()) == NULL) {
335                 HNP_LOGE("hnp json write array create unsuccess");
336                 return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED;
337             }
338         } else {
339             HNP_LOGE("hnp json write read hnp info file unsuccess");
340             return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED;
341         }
342     } else {
343         json = cJSON_Parse(infoStream);
344         free(infoStream);
345         if (json == NULL) {
346             HNP_LOGE("hnp json write parse json file unsuccess.");
347             return HNP_ERRNO_BASE_PARSE_JSON_FAILED;
348         }
349         hapExist = HnpInstallHapExistCheck(hapPackageName, json, &hapItem, &hapIndex);
350     }
351 
352     if (hapExist) {
353         cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
354         bool hnpExist = HnpInstallHnpExistCheck(hnpItemArr, hnpCfg->name, &hnpItem, &hnpIndex, NULL);
355         if (hnpExist) {
356             cJSON *versionJson = cJSON_GetObjectItem(hnpItem, "version");
357             if (versionJson != NULL) {
358                 cJSON_SetValuestring(versionJson, hnpCfg->version);
359                 HnpPackageVersionUpdateAll(json, hnpCfg);
360                 ret = HnpHapJsonWrite(json);
361                 cJSON_Delete(json);
362                 return ret;
363             }
364         }
365     }
366 
367     ret = HnpHapJsonHnpAdd(hapExist, json, hapItem, hapPackageName, hnpCfg);
368     cJSON_Delete(json);
369     return ret;
370 }
371 
HnpOtherPackageInstallCheck(const char * name,const char * version,int packageIndex,cJSON * json)372 static bool HnpOtherPackageInstallCheck(const char *name, const char *version, int packageIndex, cJSON *json)
373 {
374     bool hnpExist = false;
375     int hnpIndex = 0;
376 
377     for (int i = 0; i < cJSON_GetArraySize(json); i++) {
378         if (i == packageIndex) {
379             continue;
380         }
381         cJSON *hapItem = cJSON_GetArrayItem(json, i);
382         cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
383         cJSON *hnpItem = NULL;
384         hnpExist = HnpInstallHnpExistCheck(hnpItemArr, name, &hnpItem, &hnpIndex, version);
385         if (hnpExist) {
386             return true;
387         }
388     }
389 
390     return false;
391 }
392 
HnpPackageInfoGetOut(HnpPackageInfo * packageInfos,int sum,HnpPackageInfo ** packageInfoOut,int * count)393 static int HnpPackageInfoGetOut(HnpPackageInfo *packageInfos, int sum, HnpPackageInfo **packageInfoOut, int *count)
394 {
395     HnpPackageInfo *ptr;
396 
397     if (sum == 0) {
398         return 0;
399     }
400 
401     ptr = malloc(sizeof(HnpPackageInfo) * sum);
402     if (ptr == NULL) {
403         HNP_LOGE("malloc hnp info unsuccess.");
404         return HNP_ERRNO_NOMEM;
405     }
406 
407     if (memcpy_s(ptr, sizeof(HnpPackageInfo) * sum, packageInfos, sizeof(HnpPackageInfo) * sum) != 0) {
408         free(ptr);
409         HNP_LOGE("memcpy hnp info unsuccess.");
410         return HNP_ERRNO_BASE_MEMCPY_FAILED;
411     }
412 
413     *packageInfoOut = ptr;
414     *count = sum;
415     return 0;
416 }
417 
HnpPackageInfoGet(const char * packageName,HnpPackageInfo ** packageInfoOut,int * count)418 int HnpPackageInfoGet(const char *packageName, HnpPackageInfo **packageInfoOut, int *count)
419 {
420     char *infoStream;
421     int size;
422     bool hnpExist = false;
423     int hapIndex = 0;
424     HnpPackageInfo packageInfos[MAX_PACKAGE_HNP_NUM] = {0};
425     int sum = 0;
426 
427     int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size);
428     if (ret != 0) {
429         if (ret == HNP_ERRNO_BASE_FILE_OPEN_FAILED || ret == HNP_ERRNO_BASE_GET_FILE_LEN_NULL) {
430             return 0;
431         }
432         HNP_LOGE("package info get read hnp info file unsuccess");
433         return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED;
434     }
435 
436     cJSON *json = cJSON_Parse(infoStream);
437     free(infoStream);
438     if (json == NULL) {
439         HNP_LOGE("package info get parse json file unsuccess.");
440         return HNP_ERRNO_BASE_PARSE_JSON_FAILED;
441     }
442 
443     cJSON *hapItem = NULL;
444     if (HnpInstallHapExistCheck(packageName, json, &hapItem, &hapIndex) == false) {
445         cJSON_Delete(json);
446         return 0;
447     }
448 
449     cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
450     for (int j = 0; j < cJSON_GetArraySize(hnpItemArr); j++) {
451         cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, j);
452         cJSON *name = cJSON_GetObjectItem(hnpItem, "name");
453         cJSON *version = cJSON_GetObjectItem(hnpItem, "version");
454         if (name == NULL || version == NULL || !cJSON_IsString(name) || !cJSON_IsString(version)) {
455             continue;
456         }
457         hnpExist = HnpOtherPackageInstallCheck(name->valuestring, version->valuestring, hapIndex, json);
458         if (!hnpExist) {
459             if ((strcpy_s(packageInfos[sum].name, MAX_FILE_PATH_LEN, name->valuestring) != EOK) ||
460                 (strcpy_s(packageInfos[sum].version, HNP_VERSION_LEN, version->valuestring) != EOK)) {
461                 HNP_LOGE("strcpy hnp info name[%{public}s] version[%{public}s] unsuccess.", name->valuestring,
462                     version->valuestring);
463                 cJSON_Delete(json);
464                 return HNP_ERRNO_BASE_COPY_FAILED;
465             }
466             sum++;
467         }
468     }
469     cJSON_Delete(json);
470 
471     return HnpPackageInfoGetOut(packageInfos, sum, packageInfoOut, count);
472 }
473 
HnpPackageInfoHnpDelete(const char * packageName,const char * name,const char * version)474 int HnpPackageInfoHnpDelete(const char *packageName, const char *name, const char *version)
475 {
476     char *infoStream;
477     int size;
478     cJSON *hapItem = NULL;
479     cJSON *hnpItem = NULL;
480     int hapIndex = 0;
481     bool hapExist = false;
482     int hnpIndex = 0;
483     bool hnpExist = false;
484 
485     int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size);
486     if (ret != 0) {
487         if (ret == HNP_ERRNO_BASE_FILE_OPEN_FAILED || ret == HNP_ERRNO_BASE_GET_FILE_LEN_NULL) {
488             return 0;
489         } else {
490             HNP_LOGE("hnp delete read hnp info file unsuccess");
491             return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED;
492         }
493     }
494 
495     cJSON *json = cJSON_Parse(infoStream);
496     free(infoStream);
497     if (json == NULL) {
498         HNP_LOGE("hnp delete parse json file unsuccess.");
499         return HNP_ERRNO_BASE_PARSE_JSON_FAILED;
500     }
501 
502     hapExist = HnpInstallHapExistCheck(packageName, json, &hapItem, &hapIndex);
503     if (!hapExist) {
504         cJSON_Delete(json);
505         return 0;
506     }
507 
508     cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
509     hnpExist = HnpInstallHnpExistCheck(hnpItemArr, name, &hnpItem, &hnpIndex, version);
510     if (hnpExist) {
511         cJSON_DeleteItemFromArray(hnpItemArr, hnpIndex);
512     }
513 
514     ret = HnpHapJsonWrite(json);
515     cJSON_Delete(json);
516     return ret;
517 }
518 
HnpPackageInfoDelete(const char * packageName)519 int HnpPackageInfoDelete(const char *packageName)
520 {
521     char *infoStream;
522     int size;
523     cJSON *hapItem = NULL;
524     int hapIndex = 0;
525     bool hapExist = false;
526 
527     int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size);
528     if (ret != 0) {
529         if (ret == HNP_ERRNO_BASE_FILE_OPEN_FAILED || ret == HNP_ERRNO_BASE_GET_FILE_LEN_NULL) {
530             return 0;
531         }
532         HNP_LOGE("package info delete read hnp info file unsuccess");
533         return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED;
534     }
535 
536     cJSON *json = cJSON_Parse(infoStream);
537     free(infoStream);
538     if (json == NULL) {
539         HNP_LOGE("package info delete parse json file unsuccess.");
540         return HNP_ERRNO_BASE_PARSE_JSON_FAILED;
541     }
542 
543     hapExist = HnpInstallHapExistCheck(packageName, json, &hapItem, &hapIndex);
544     if (hapExist) {
545         cJSON_DeleteItemFromArray(json, hapIndex);
546     }
547 
548     ret = HnpHapJsonWrite(json);
549     cJSON_Delete(json);
550     return ret;
551 }
552 
HnpInstallHnpVersionGet(cJSON * hnpItemArr,const char * name)553 static char *HnpInstallHnpVersionGet(cJSON *hnpItemArr, const char *name)
554 {
555     if (hnpItemArr == NULL) {
556         return NULL;
557     }
558 
559     for (int i = 0; i < cJSON_GetArraySize(hnpItemArr); i++) {
560         cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, i);
561         cJSON *nameItem = cJSON_GetObjectItem(hnpItem, "name");
562         cJSON *versionItem = cJSON_GetObjectItem(hnpItem, "version");
563         if ((nameItem != NULL) && (versionItem != NULL) && (cJSON_IsString(nameItem)) &&
564             (cJSON_IsString(versionItem)) && (strcmp(nameItem->valuestring, name) == 0)) {
565             return versionItem->valuestring;
566         }
567     }
568 
569     return NULL;
570 }
571 
HnpPackgeHnpVersionGet(const char * packageName,const char * name)572 char *HnpPackgeHnpVersionGet(const char *packageName, const char *name)
573 {
574     char *infoStream;
575     int size;
576     cJSON *hapItem = NULL;
577     char *version = NULL;
578 
579     int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size);
580     if (ret != 0) {
581         return NULL;
582     }
583 
584     cJSON *json = cJSON_Parse(infoStream);
585     free(infoStream);
586     if (json == NULL) {
587         HNP_LOGE("hnp delete parse json file unsuccess.");
588         return NULL;
589     }
590 
591     for (int i = 0; i < cJSON_GetArraySize(json); i++) {
592         hapItem = cJSON_GetArrayItem(json, i);
593         cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
594         version = HnpInstallHnpVersionGet(hnpItemArr, name);
595         if (version != NULL) {
596             break;
597         }
598     }
599 
600     cJSON_Delete(json);
601     return version;
602 }
603 
604 #ifdef __cplusplus
605 }
606 #endif