169570cc8Sopenharmony_ci/* 269570cc8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License. 569570cc8Sopenharmony_ci * You may obtain a copy of the License at 669570cc8Sopenharmony_ci * 769570cc8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 869570cc8Sopenharmony_ci * 969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and 1369570cc8Sopenharmony_ci * limitations under the License. 1469570cc8Sopenharmony_ci */ 1569570cc8Sopenharmony_ci 1669570cc8Sopenharmony_ci#include <stdio.h> 1769570cc8Sopenharmony_ci#include <stdlib.h> 1869570cc8Sopenharmony_ci 1969570cc8Sopenharmony_ci#include "cJSON.h" 2069570cc8Sopenharmony_ci#include "securec.h" 2169570cc8Sopenharmony_ci 2269570cc8Sopenharmony_ci#include "hnp_base.h" 2369570cc8Sopenharmony_ci 2469570cc8Sopenharmony_ci#ifdef __cplusplus 2569570cc8Sopenharmony_ciextern "C" { 2669570cc8Sopenharmony_ci#endif 2769570cc8Sopenharmony_ci 2869570cc8Sopenharmony_cistatic int ParseLinksJsonToCfgInfo(cJSON *linksItem, HnpCfgInfo *hnpCfg) 2969570cc8Sopenharmony_ci{ 3069570cc8Sopenharmony_ci NativeBinLink *linkArray = NULL; 3169570cc8Sopenharmony_ci 3269570cc8Sopenharmony_ci int linkArrayNum = cJSON_GetArraySize(linksItem); 3369570cc8Sopenharmony_ci if (linkArrayNum > 0) { 3469570cc8Sopenharmony_ci hnpCfg->linkNum = (size_t)linkArrayNum; 3569570cc8Sopenharmony_ci linkArray = (NativeBinLink*)malloc(sizeof(NativeBinLink) * linkArrayNum); 3669570cc8Sopenharmony_ci if (linkArray == NULL) { 3769570cc8Sopenharmony_ci HNP_LOGE("malloc unsuccess."); 3869570cc8Sopenharmony_ci return HNP_ERRNO_NOMEM; 3969570cc8Sopenharmony_ci } 4069570cc8Sopenharmony_ci for (int i = 0; i < linkArrayNum; i++) { 4169570cc8Sopenharmony_ci cJSON *link = cJSON_GetArrayItem(linksItem, i); 4269570cc8Sopenharmony_ci if (link == NULL) { 4369570cc8Sopenharmony_ci free(linkArray); 4469570cc8Sopenharmony_ci return HNP_ERRNO_BASE_GET_ARRAY_ITRM_FAILED; 4569570cc8Sopenharmony_ci } 4669570cc8Sopenharmony_ci cJSON *sourceItem = cJSON_GetObjectItem(link, "source"); 4769570cc8Sopenharmony_ci if ((sourceItem == NULL) || (sourceItem->valuestring == NULL)) { 4869570cc8Sopenharmony_ci HNP_LOGE("get source info in cfg unsuccess."); 4969570cc8Sopenharmony_ci free(linkArray); 5069570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND; 5169570cc8Sopenharmony_ci } 5269570cc8Sopenharmony_ci if (strcpy_s(linkArray[i].source, MAX_FILE_PATH_LEN, sourceItem->valuestring) != EOK) { 5369570cc8Sopenharmony_ci HNP_LOGE("strcpy unsuccess."); 5469570cc8Sopenharmony_ci free(linkArray); 5569570cc8Sopenharmony_ci return HNP_ERRNO_BASE_COPY_FAILED; 5669570cc8Sopenharmony_ci } 5769570cc8Sopenharmony_ci linkArray[i].target[0] = '\0'; //允许target不填,软链接默认使用原二进制名称 5869570cc8Sopenharmony_ci cJSON *targetItem = cJSON_GetObjectItem(link, "target"); 5969570cc8Sopenharmony_ci if ((targetItem != NULL) && (targetItem->valuestring != NULL) && 6069570cc8Sopenharmony_ci (strcpy_s(linkArray[i].target, MAX_FILE_PATH_LEN, targetItem->valuestring) != EOK)) { 6169570cc8Sopenharmony_ci HNP_LOGE("strcpy unsuccess."); 6269570cc8Sopenharmony_ci free(linkArray); 6369570cc8Sopenharmony_ci return HNP_ERRNO_BASE_COPY_FAILED; 6469570cc8Sopenharmony_ci } 6569570cc8Sopenharmony_ci } 6669570cc8Sopenharmony_ci hnpCfg->links = linkArray; 6769570cc8Sopenharmony_ci } else { 6869570cc8Sopenharmony_ci hnpCfg->linkNum = 0; 6969570cc8Sopenharmony_ci } 7069570cc8Sopenharmony_ci return 0; 7169570cc8Sopenharmony_ci} 7269570cc8Sopenharmony_ci 7369570cc8Sopenharmony_cistatic int ParseJsonStreamToHnpCfgInfo(cJSON *json, HnpCfgInfo *hnpCfg) 7469570cc8Sopenharmony_ci{ 7569570cc8Sopenharmony_ci int ret; 7669570cc8Sopenharmony_ci 7769570cc8Sopenharmony_ci cJSON *typeItem = cJSON_GetObjectItem(json, "type"); 7869570cc8Sopenharmony_ci if ((typeItem == NULL) || (typeItem->valuestring == NULL)) { 7969570cc8Sopenharmony_ci HNP_LOGE("get type info in cfg unsuccess."); 8069570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND; 8169570cc8Sopenharmony_ci } 8269570cc8Sopenharmony_ci if (strcmp(typeItem->valuestring, "hnp-config") != 0) { 8369570cc8Sopenharmony_ci HNP_LOGE("type info not match.type=%{public}s", typeItem->valuestring); 8469570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND; 8569570cc8Sopenharmony_ci } 8669570cc8Sopenharmony_ci cJSON *nameItem = cJSON_GetObjectItem(json, "name"); 8769570cc8Sopenharmony_ci if ((nameItem == NULL) || (nameItem->valuestring == NULL)) { 8869570cc8Sopenharmony_ci HNP_LOGE("get name info in cfg unsuccess."); 8969570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND; 9069570cc8Sopenharmony_ci } 9169570cc8Sopenharmony_ci ret = strcpy_s(hnpCfg->name, MAX_FILE_PATH_LEN, nameItem->valuestring); 9269570cc8Sopenharmony_ci if (ret != EOK) { 9369570cc8Sopenharmony_ci HNP_LOGE("strcpy unsuccess."); 9469570cc8Sopenharmony_ci return HNP_ERRNO_BASE_COPY_FAILED; 9569570cc8Sopenharmony_ci } 9669570cc8Sopenharmony_ci cJSON *versionItem = cJSON_GetObjectItem(json, "version"); 9769570cc8Sopenharmony_ci if ((versionItem == NULL) || (versionItem->valuestring == NULL)) { 9869570cc8Sopenharmony_ci HNP_LOGE("get version info in cfg unsuccess."); 9969570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND; 10069570cc8Sopenharmony_ci } 10169570cc8Sopenharmony_ci ret = strcpy_s(hnpCfg->version, HNP_VERSION_LEN, versionItem->valuestring); 10269570cc8Sopenharmony_ci if (ret != EOK) { 10369570cc8Sopenharmony_ci HNP_LOGE("strcpy unsuccess."); 10469570cc8Sopenharmony_ci return HNP_ERRNO_BASE_COPY_FAILED; 10569570cc8Sopenharmony_ci } 10669570cc8Sopenharmony_ci cJSON *installItem = cJSON_GetObjectItem(json, "install"); 10769570cc8Sopenharmony_ci if (installItem == NULL) { 10869570cc8Sopenharmony_ci HNP_LOGE("get install info in cfg unsuccess."); 10969570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND; 11069570cc8Sopenharmony_ci } 11169570cc8Sopenharmony_ci cJSON *linksItem = cJSON_GetObjectItem(installItem, "links"); 11269570cc8Sopenharmony_ci if (linksItem != NULL) { 11369570cc8Sopenharmony_ci ret = ParseLinksJsonToCfgInfo(linksItem, hnpCfg); 11469570cc8Sopenharmony_ci if (ret != 0) { 11569570cc8Sopenharmony_ci return ret; 11669570cc8Sopenharmony_ci } 11769570cc8Sopenharmony_ci } else { 11869570cc8Sopenharmony_ci hnpCfg->linkNum = 0; 11969570cc8Sopenharmony_ci } 12069570cc8Sopenharmony_ci return 0; 12169570cc8Sopenharmony_ci} 12269570cc8Sopenharmony_ci 12369570cc8Sopenharmony_ciint ParseHnpCfgFile(const char *hnpCfgPath, HnpCfgInfo *hnpCfg) 12469570cc8Sopenharmony_ci{ 12569570cc8Sopenharmony_ci int ret; 12669570cc8Sopenharmony_ci char *cfgStream = NULL; 12769570cc8Sopenharmony_ci cJSON *json; 12869570cc8Sopenharmony_ci int size; 12969570cc8Sopenharmony_ci 13069570cc8Sopenharmony_ci ret = ReadFileToStream(hnpCfgPath, &cfgStream, &size); 13169570cc8Sopenharmony_ci if (ret != 0) { 13269570cc8Sopenharmony_ci HNP_LOGE("read cfg file[%{public}s] unsuccess.", hnpCfgPath); 13369570cc8Sopenharmony_ci return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED; 13469570cc8Sopenharmony_ci } 13569570cc8Sopenharmony_ci json = cJSON_Parse(cfgStream); 13669570cc8Sopenharmony_ci free(cfgStream); 13769570cc8Sopenharmony_ci if (json == NULL) { 13869570cc8Sopenharmony_ci HNP_LOGE("parse json file[%{public}s] unsuccess.", hnpCfgPath); 13969570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_JSON_FAILED; 14069570cc8Sopenharmony_ci } 14169570cc8Sopenharmony_ci ret = ParseJsonStreamToHnpCfgInfo(json, hnpCfg); 14269570cc8Sopenharmony_ci cJSON_Delete(json); 14369570cc8Sopenharmony_ci 14469570cc8Sopenharmony_ci return ret; 14569570cc8Sopenharmony_ci} 14669570cc8Sopenharmony_ci 14769570cc8Sopenharmony_ciint HnpCfgGetFromSteam(char *cfgStream, HnpCfgInfo *hnpCfg) 14869570cc8Sopenharmony_ci{ 14969570cc8Sopenharmony_ci cJSON *json; 15069570cc8Sopenharmony_ci int ret; 15169570cc8Sopenharmony_ci 15269570cc8Sopenharmony_ci if (cfgStream == NULL) { 15369570cc8Sopenharmony_ci HNP_LOGE("hnp cfg file not found."); 15469570cc8Sopenharmony_ci return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED; 15569570cc8Sopenharmony_ci } 15669570cc8Sopenharmony_ci 15769570cc8Sopenharmony_ci json = cJSON_Parse(cfgStream); 15869570cc8Sopenharmony_ci if (json == NULL) { 15969570cc8Sopenharmony_ci HNP_LOGE("parse json file unsuccess."); 16069570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_JSON_FAILED; 16169570cc8Sopenharmony_ci } 16269570cc8Sopenharmony_ci ret = ParseJsonStreamToHnpCfgInfo(json, hnpCfg); 16369570cc8Sopenharmony_ci cJSON_Delete(json); 16469570cc8Sopenharmony_ci 16569570cc8Sopenharmony_ci return ret; 16669570cc8Sopenharmony_ci} 16769570cc8Sopenharmony_ciint GetHnpJsonBuff(HnpCfgInfo *hnpCfg, char **buff) 16869570cc8Sopenharmony_ci{ 16969570cc8Sopenharmony_ci cJSON *root = cJSON_CreateObject(); 17069570cc8Sopenharmony_ci 17169570cc8Sopenharmony_ci cJSON_AddStringToObject(root, "type", "hnp-config"); 17269570cc8Sopenharmony_ci cJSON_AddStringToObject(root, "name", hnpCfg->name); 17369570cc8Sopenharmony_ci cJSON_AddStringToObject(root, "version", hnpCfg->version); 17469570cc8Sopenharmony_ci cJSON_AddObjectToObject(root, "install"); 17569570cc8Sopenharmony_ci char *str = cJSON_Print(root); 17669570cc8Sopenharmony_ci cJSON_Delete(root); 17769570cc8Sopenharmony_ci 17869570cc8Sopenharmony_ci *buff = str; 17969570cc8Sopenharmony_ci return 0; 18069570cc8Sopenharmony_ci} 18169570cc8Sopenharmony_ci 18269570cc8Sopenharmony_cistatic bool HnpInstallHapExistCheck(const char *hnpPackageName, cJSON *json, cJSON **hapItemOut, int *hapIndex) 18369570cc8Sopenharmony_ci{ 18469570cc8Sopenharmony_ci cJSON *hapItem = NULL; 18569570cc8Sopenharmony_ci bool hapExist = false; 18669570cc8Sopenharmony_ci cJSON *hapJson = NULL; 18769570cc8Sopenharmony_ci 18869570cc8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(json); i++) { 18969570cc8Sopenharmony_ci hapItem = cJSON_GetArrayItem(json, i); 19069570cc8Sopenharmony_ci hapJson = cJSON_GetObjectItem(hapItem, "hap"); 19169570cc8Sopenharmony_ci if ((hapJson != NULL) && (cJSON_IsString(hapJson)) && (strcmp(hapJson->valuestring, hnpPackageName) == 0)) { 19269570cc8Sopenharmony_ci hapExist = true; 19369570cc8Sopenharmony_ci *hapItemOut = hapItem; 19469570cc8Sopenharmony_ci *hapIndex = i; 19569570cc8Sopenharmony_ci break; 19669570cc8Sopenharmony_ci } 19769570cc8Sopenharmony_ci } 19869570cc8Sopenharmony_ci 19969570cc8Sopenharmony_ci return hapExist; 20069570cc8Sopenharmony_ci} 20169570cc8Sopenharmony_ci 20269570cc8Sopenharmony_cistatic bool HnpInstallHnpExistCheck(cJSON *hnpItemArr, const char *name, cJSON **hnpItemOut, int *hnpIndex, 20369570cc8Sopenharmony_ci const char *version) 20469570cc8Sopenharmony_ci{ 20569570cc8Sopenharmony_ci if (hnpItemArr == NULL) { 20669570cc8Sopenharmony_ci return false; 20769570cc8Sopenharmony_ci } 20869570cc8Sopenharmony_ci 20969570cc8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(hnpItemArr); i++) { 21069570cc8Sopenharmony_ci cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, i); 21169570cc8Sopenharmony_ci cJSON *nameJson = cJSON_GetObjectItem(hnpItem, "name"); 21269570cc8Sopenharmony_ci cJSON *versionJson = cJSON_GetObjectItem(hnpItem, "current_version"); 21369570cc8Sopenharmony_ci if ((nameJson != NULL) && (strcmp(nameJson->valuestring, name) == 0)) { 21469570cc8Sopenharmony_ci *hnpItemOut = hnpItem; 21569570cc8Sopenharmony_ci *hnpIndex = i; 21669570cc8Sopenharmony_ci if (version == NULL) { 21769570cc8Sopenharmony_ci return true; 21869570cc8Sopenharmony_ci } 21969570cc8Sopenharmony_ci if ((versionJson != NULL) && (strcmp(versionJson->valuestring, version) == 0)) { 22069570cc8Sopenharmony_ci return true; 22169570cc8Sopenharmony_ci } 22269570cc8Sopenharmony_ci } 22369570cc8Sopenharmony_ci } 22469570cc8Sopenharmony_ci 22569570cc8Sopenharmony_ci return false; 22669570cc8Sopenharmony_ci} 22769570cc8Sopenharmony_ci 22869570cc8Sopenharmony_cistatic void HnpPackageVersionUpdateAll(cJSON *json, const HnpCfgInfo *hnpCfg) 22969570cc8Sopenharmony_ci{ 23069570cc8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(json); i++) { 23169570cc8Sopenharmony_ci cJSON *hapItem = cJSON_GetArrayItem(json, i); 23269570cc8Sopenharmony_ci cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp"); 23369570cc8Sopenharmony_ci for (int j = 0; j < cJSON_GetArraySize(hnpItemArr); j++) { 23469570cc8Sopenharmony_ci cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, j); 23569570cc8Sopenharmony_ci cJSON *nameItem = cJSON_GetObjectItem(hnpItem, "name"); 23669570cc8Sopenharmony_ci if ((nameItem == NULL) || 23769570cc8Sopenharmony_ci ((cJSON_IsString(nameItem)) && (strcmp(nameItem->valuestring, hnpCfg->name) != 0))) { 23869570cc8Sopenharmony_ci continue; 23969570cc8Sopenharmony_ci } 24069570cc8Sopenharmony_ci cJSON *version = cJSON_GetObjectItem(hnpItem, "current_version"); 24169570cc8Sopenharmony_ci if (version == NULL) { 24269570cc8Sopenharmony_ci break; 24369570cc8Sopenharmony_ci } 24469570cc8Sopenharmony_ci cJSON_SetValuestring(version, hnpCfg->version); 24569570cc8Sopenharmony_ci break; 24669570cc8Sopenharmony_ci } 24769570cc8Sopenharmony_ci } 24869570cc8Sopenharmony_ci 24969570cc8Sopenharmony_ci return; 25069570cc8Sopenharmony_ci} 25169570cc8Sopenharmony_ci 25269570cc8Sopenharmony_cistatic int HnpHapJsonWrite(cJSON *json) 25369570cc8Sopenharmony_ci{ 25469570cc8Sopenharmony_ci FILE *fp = fopen(HNP_PACKAGE_INFO_JSON_FILE_PATH, "wb"); 25569570cc8Sopenharmony_ci if (fp == NULL) { 25669570cc8Sopenharmony_ci HNP_LOGE("open file:%{public}s unsuccess!", HNP_PACKAGE_INFO_JSON_FILE_PATH); 25769570cc8Sopenharmony_ci return HNP_ERRNO_BASE_FILE_OPEN_FAILED; 25869570cc8Sopenharmony_ci } 25969570cc8Sopenharmony_ci char *jsonStr = cJSON_Print(json); 26069570cc8Sopenharmony_ci size_t jsonStrSize = strlen(jsonStr); 26169570cc8Sopenharmony_ci size_t writeLen = fwrite(jsonStr, sizeof(char), jsonStrSize, fp); 26269570cc8Sopenharmony_ci (void)fclose(fp); 26369570cc8Sopenharmony_ci free(jsonStr); 26469570cc8Sopenharmony_ci if (writeLen != jsonStrSize) { 26569570cc8Sopenharmony_ci HNP_LOGE("package info write file:%{public}s unsuccess!", HNP_PACKAGE_INFO_JSON_FILE_PATH); 26669570cc8Sopenharmony_ci return HNP_ERRNO_BASE_FILE_WRITE_FAILED; 26769570cc8Sopenharmony_ci } 26869570cc8Sopenharmony_ci 26969570cc8Sopenharmony_ci return 0; 27069570cc8Sopenharmony_ci} 27169570cc8Sopenharmony_ci 27269570cc8Sopenharmony_cistatic int HnpHapJsonHnpAdd(bool hapExist, cJSON *json, cJSON *hapItem, const char *hnpPackageName, 27369570cc8Sopenharmony_ci const HnpCfgInfo *hnpCfg) 27469570cc8Sopenharmony_ci{ 27569570cc8Sopenharmony_ci cJSON *hnpItemArr = NULL; 27669570cc8Sopenharmony_ci int ret; 27769570cc8Sopenharmony_ci 27869570cc8Sopenharmony_ci if (hapExist) { 27969570cc8Sopenharmony_ci hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp"); 28069570cc8Sopenharmony_ci if (hnpItemArr == NULL) { 28169570cc8Sopenharmony_ci HNP_LOGE("hnp item array get unsuccess"); 28269570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND; 28369570cc8Sopenharmony_ci } 28469570cc8Sopenharmony_ci } else { 28569570cc8Sopenharmony_ci hapItem = cJSON_CreateObject(); 28669570cc8Sopenharmony_ci if (hapItem == NULL) { 28769570cc8Sopenharmony_ci HNP_LOGE("hnp json write create hap object unsuccess"); 28869570cc8Sopenharmony_ci return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED; 28969570cc8Sopenharmony_ci } 29069570cc8Sopenharmony_ci cJSON_AddStringToObject(hapItem, "hap", hnpPackageName); 29169570cc8Sopenharmony_ci hnpItemArr = cJSON_CreateArray(); 29269570cc8Sopenharmony_ci if (hnpItemArr == NULL) { 29369570cc8Sopenharmony_ci HNP_LOGE("hnp json write array create unsuccess"); 29469570cc8Sopenharmony_ci cJSON_Delete(hapItem); 29569570cc8Sopenharmony_ci return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED; 29669570cc8Sopenharmony_ci } 29769570cc8Sopenharmony_ci cJSON_AddItemToObject(hapItem, "hnp", hnpItemArr); 29869570cc8Sopenharmony_ci cJSON_AddItemToArray(json, hapItem); 29969570cc8Sopenharmony_ci } 30069570cc8Sopenharmony_ci 30169570cc8Sopenharmony_ci cJSON *hnpItem = cJSON_CreateObject(); 30269570cc8Sopenharmony_ci if (hnpItem == NULL) { 30369570cc8Sopenharmony_ci HNP_LOGE("hnp json write create hnp object unsuccess"); 30469570cc8Sopenharmony_ci return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED; 30569570cc8Sopenharmony_ci } 30669570cc8Sopenharmony_ci cJSON_AddItemToObject(hnpItem, "name", cJSON_CreateString(hnpCfg->name)); 30769570cc8Sopenharmony_ci cJSON_AddItemToObject(hnpItem, "current_version", cJSON_CreateString(hnpCfg->version)); 30869570cc8Sopenharmony_ci if (hnpCfg->isInstall) { 30969570cc8Sopenharmony_ci cJSON_AddItemToObject(hnpItem, "install_version", cJSON_CreateString(hnpCfg->version)); 31069570cc8Sopenharmony_ci } else { 31169570cc8Sopenharmony_ci cJSON_AddItemToObject(hnpItem, "install_version", cJSON_CreateString("none")); 31269570cc8Sopenharmony_ci } 31369570cc8Sopenharmony_ci cJSON_AddItemToArray(hnpItemArr, hnpItem); 31469570cc8Sopenharmony_ci 31569570cc8Sopenharmony_ci HnpPackageVersionUpdateAll(json, hnpCfg); 31669570cc8Sopenharmony_ci 31769570cc8Sopenharmony_ci ret = HnpHapJsonWrite(json); 31869570cc8Sopenharmony_ci return ret; 31969570cc8Sopenharmony_ci} 32069570cc8Sopenharmony_ci 32169570cc8Sopenharmony_ciint HnpInstallInfoJsonWrite(const char *hapPackageName, const HnpCfgInfo *hnpCfg) 32269570cc8Sopenharmony_ci{ 32369570cc8Sopenharmony_ci bool hapExist = false; 32469570cc8Sopenharmony_ci int hapIndex = 0; 32569570cc8Sopenharmony_ci int hnpIndex = 0; 32669570cc8Sopenharmony_ci char *infoStream; 32769570cc8Sopenharmony_ci int size; 32869570cc8Sopenharmony_ci cJSON *hapItem = NULL; 32969570cc8Sopenharmony_ci cJSON *hnpItem = NULL; 33069570cc8Sopenharmony_ci cJSON *json = NULL; 33169570cc8Sopenharmony_ci 33269570cc8Sopenharmony_ci if ((hapPackageName == NULL) || (hnpCfg == NULL)) { 33369570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARAMS_INVALID; 33469570cc8Sopenharmony_ci } 33569570cc8Sopenharmony_ci 33669570cc8Sopenharmony_ci int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size); 33769570cc8Sopenharmony_ci if (ret != 0) { 33869570cc8Sopenharmony_ci if ((ret == HNP_ERRNO_BASE_FILE_OPEN_FAILED) || (ret == HNP_ERRNO_BASE_GET_FILE_LEN_NULL)) { 33969570cc8Sopenharmony_ci if ((json = cJSON_CreateArray()) == NULL) { 34069570cc8Sopenharmony_ci HNP_LOGE("hnp json write array create unsuccess"); 34169570cc8Sopenharmony_ci return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED; 34269570cc8Sopenharmony_ci } 34369570cc8Sopenharmony_ci } else { 34469570cc8Sopenharmony_ci HNP_LOGE("hnp json write read hnp info file unsuccess"); 34569570cc8Sopenharmony_ci return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED; 34669570cc8Sopenharmony_ci } 34769570cc8Sopenharmony_ci } else { 34869570cc8Sopenharmony_ci json = cJSON_Parse(infoStream); 34969570cc8Sopenharmony_ci free(infoStream); 35069570cc8Sopenharmony_ci if (json == NULL) { 35169570cc8Sopenharmony_ci HNP_LOGE("hnp json write parse json file unsuccess."); 35269570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_JSON_FAILED; 35369570cc8Sopenharmony_ci } 35469570cc8Sopenharmony_ci hapExist = HnpInstallHapExistCheck(hapPackageName, json, &hapItem, &hapIndex); 35569570cc8Sopenharmony_ci } 35669570cc8Sopenharmony_ci 35769570cc8Sopenharmony_ci if (hapExist) { 35869570cc8Sopenharmony_ci cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp"); 35969570cc8Sopenharmony_ci bool hnpExist = HnpInstallHnpExistCheck(hnpItemArr, hnpCfg->name, &hnpItem, &hnpIndex, NULL); 36069570cc8Sopenharmony_ci if (hnpExist) { 36169570cc8Sopenharmony_ci cJSON *versionJson = cJSON_GetObjectItem(hnpItem, "current_version"); 36269570cc8Sopenharmony_ci if (versionJson != NULL) { // 当前版本存在,即非新增版本,仅更新current_version即可,无需更新install_version 36369570cc8Sopenharmony_ci cJSON_SetValuestring(versionJson, hnpCfg->version); 36469570cc8Sopenharmony_ci HnpPackageVersionUpdateAll(json, hnpCfg); 36569570cc8Sopenharmony_ci ret = HnpHapJsonWrite(json); 36669570cc8Sopenharmony_ci cJSON_Delete(json); 36769570cc8Sopenharmony_ci return ret; 36869570cc8Sopenharmony_ci } 36969570cc8Sopenharmony_ci } 37069570cc8Sopenharmony_ci } 37169570cc8Sopenharmony_ci 37269570cc8Sopenharmony_ci ret = HnpHapJsonHnpAdd(hapExist, json, hapItem, hapPackageName, hnpCfg); 37369570cc8Sopenharmony_ci cJSON_Delete(json); 37469570cc8Sopenharmony_ci return ret; 37569570cc8Sopenharmony_ci} 37669570cc8Sopenharmony_ci 37769570cc8Sopenharmony_cistatic bool HnpOtherPackageInstallCheck(const char *name, const char *version, int packageIndex, cJSON *json) 37869570cc8Sopenharmony_ci{ 37969570cc8Sopenharmony_ci bool hnpExist = false; 38069570cc8Sopenharmony_ci int hnpIndex = 0; 38169570cc8Sopenharmony_ci 38269570cc8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(json); i++) { 38369570cc8Sopenharmony_ci if (i == packageIndex) { 38469570cc8Sopenharmony_ci continue; 38569570cc8Sopenharmony_ci } 38669570cc8Sopenharmony_ci cJSON *hapItem = cJSON_GetArrayItem(json, i); 38769570cc8Sopenharmony_ci cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp"); 38869570cc8Sopenharmony_ci cJSON *hnpItem = NULL; 38969570cc8Sopenharmony_ci hnpExist = HnpInstallHnpExistCheck(hnpItemArr, name, &hnpItem, &hnpIndex, version); 39069570cc8Sopenharmony_ci if (hnpExist) { 39169570cc8Sopenharmony_ci return true; 39269570cc8Sopenharmony_ci } 39369570cc8Sopenharmony_ci } 39469570cc8Sopenharmony_ci 39569570cc8Sopenharmony_ci return false; 39669570cc8Sopenharmony_ci} 39769570cc8Sopenharmony_ci 39869570cc8Sopenharmony_cistatic int HnpPackageInfoGetOut(HnpPackageInfo *packageInfos, int sum, HnpPackageInfo **packageInfoOut, int *count) 39969570cc8Sopenharmony_ci{ 40069570cc8Sopenharmony_ci HnpPackageInfo *ptr; 40169570cc8Sopenharmony_ci 40269570cc8Sopenharmony_ci if (sum == 0) { 40369570cc8Sopenharmony_ci return 0; 40469570cc8Sopenharmony_ci } 40569570cc8Sopenharmony_ci 40669570cc8Sopenharmony_ci ptr = malloc(sizeof(HnpPackageInfo) * sum); 40769570cc8Sopenharmony_ci if (ptr == NULL) { 40869570cc8Sopenharmony_ci HNP_LOGE("malloc hnp info unsuccess."); 40969570cc8Sopenharmony_ci return HNP_ERRNO_NOMEM; 41069570cc8Sopenharmony_ci } 41169570cc8Sopenharmony_ci 41269570cc8Sopenharmony_ci if (memcpy_s(ptr, sizeof(HnpPackageInfo) * sum, packageInfos, sizeof(HnpPackageInfo) * sum) != 0) { 41369570cc8Sopenharmony_ci free(ptr); 41469570cc8Sopenharmony_ci HNP_LOGE("memcpy hnp info unsuccess."); 41569570cc8Sopenharmony_ci return HNP_ERRNO_BASE_MEMCPY_FAILED; 41669570cc8Sopenharmony_ci } 41769570cc8Sopenharmony_ci 41869570cc8Sopenharmony_ci *packageInfoOut = ptr; 41969570cc8Sopenharmony_ci *count = sum; 42069570cc8Sopenharmony_ci return 0; 42169570cc8Sopenharmony_ci} 42269570cc8Sopenharmony_ci 42369570cc8Sopenharmony_cistatic int HnpPackageJsonGet(cJSON **pJson) 42469570cc8Sopenharmony_ci{ 42569570cc8Sopenharmony_ci char *infoStream; 42669570cc8Sopenharmony_ci int size; 42769570cc8Sopenharmony_ci 42869570cc8Sopenharmony_ci int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size); 42969570cc8Sopenharmony_ci if (ret != 0) { 43069570cc8Sopenharmony_ci if (ret == HNP_ERRNO_BASE_FILE_OPEN_FAILED || ret == HNP_ERRNO_BASE_GET_FILE_LEN_NULL) { 43169570cc8Sopenharmony_ci return 0; 43269570cc8Sopenharmony_ci } 43369570cc8Sopenharmony_ci HNP_LOGE("package info get read hnp info file unsuccess"); 43469570cc8Sopenharmony_ci return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED; 43569570cc8Sopenharmony_ci } 43669570cc8Sopenharmony_ci 43769570cc8Sopenharmony_ci cJSON *json = cJSON_Parse(infoStream); 43869570cc8Sopenharmony_ci free(infoStream); 43969570cc8Sopenharmony_ci if (json == NULL) { 44069570cc8Sopenharmony_ci HNP_LOGE("package info get parse json file unsuccess."); 44169570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_JSON_FAILED; 44269570cc8Sopenharmony_ci } 44369570cc8Sopenharmony_ci 44469570cc8Sopenharmony_ci *pJson = json; 44569570cc8Sopenharmony_ci 44669570cc8Sopenharmony_ci return 0; 44769570cc8Sopenharmony_ci} 44869570cc8Sopenharmony_ci 44969570cc8Sopenharmony_ciint HnpPackageInfoGet(const char *packageName, HnpPackageInfo **packageInfoOut, int *count) 45069570cc8Sopenharmony_ci{ 45169570cc8Sopenharmony_ci bool hnpExist = false; 45269570cc8Sopenharmony_ci int hapIndex = 0; 45369570cc8Sopenharmony_ci HnpPackageInfo packageInfos[MAX_PACKAGE_HNP_NUM] = {0}; 45469570cc8Sopenharmony_ci int sum = 0; 45569570cc8Sopenharmony_ci cJSON *json = NULL; 45669570cc8Sopenharmony_ci 45769570cc8Sopenharmony_ci int ret = HnpPackageJsonGet(&json); 45869570cc8Sopenharmony_ci if (ret != 0 || json == NULL) { 45969570cc8Sopenharmony_ci return ret; 46069570cc8Sopenharmony_ci } 46169570cc8Sopenharmony_ci 46269570cc8Sopenharmony_ci cJSON *hapItem = NULL; 46369570cc8Sopenharmony_ci if (HnpInstallHapExistCheck(packageName, json, &hapItem, &hapIndex) == false) { 46469570cc8Sopenharmony_ci cJSON_Delete(json); 46569570cc8Sopenharmony_ci return 0; 46669570cc8Sopenharmony_ci } 46769570cc8Sopenharmony_ci 46869570cc8Sopenharmony_ci cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp"); 46969570cc8Sopenharmony_ci for (int j = 0; j < cJSON_GetArraySize(hnpItemArr); j++) { 47069570cc8Sopenharmony_ci cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, j); 47169570cc8Sopenharmony_ci cJSON *name = cJSON_GetObjectItem(hnpItem, "name"); 47269570cc8Sopenharmony_ci cJSON *version = cJSON_GetObjectItem(hnpItem, "current_version"); 47369570cc8Sopenharmony_ci cJSON *installVersion = cJSON_GetObjectItem(hnpItem, "install_version"); 47469570cc8Sopenharmony_ci if (name == NULL || version == NULL || installVersion == NULL || !cJSON_IsString(name) || 47569570cc8Sopenharmony_ci !cJSON_IsString(version) || !cJSON_IsString(installVersion)) { 47669570cc8Sopenharmony_ci continue; 47769570cc8Sopenharmony_ci } 47869570cc8Sopenharmony_ci hnpExist = HnpOtherPackageInstallCheck(name->valuestring, version->valuestring, hapIndex, json); 47969570cc8Sopenharmony_ci // 当卸载当前版本未被其他hap使用或者存在安装版本的时候,需要卸载对应的当前版本或者安装版本 48069570cc8Sopenharmony_ci if (!hnpExist || strcmp(installVersion->valuestring, "none") != 0) { 48169570cc8Sopenharmony_ci if ((strcpy_s(packageInfos[sum].name, MAX_FILE_PATH_LEN, name->valuestring) != EOK) || 48269570cc8Sopenharmony_ci (strcpy_s(packageInfos[sum].currentVersion, HNP_VERSION_LEN, version->valuestring) != EOK) || 48369570cc8Sopenharmony_ci (strcpy_s(packageInfos[sum].installVersion, HNP_VERSION_LEN, installVersion->valuestring) != EOK)) { 48469570cc8Sopenharmony_ci HNP_LOGE("strcpy hnp info name[%{public}s],version[%{public}s],install version[%{public}s] unsuccess.", 48569570cc8Sopenharmony_ci name->valuestring, version->valuestring, installVersion->valuestring); 48669570cc8Sopenharmony_ci cJSON_Delete(json); 48769570cc8Sopenharmony_ci return HNP_ERRNO_BASE_COPY_FAILED; 48869570cc8Sopenharmony_ci } 48969570cc8Sopenharmony_ci packageInfos[sum].hnpExist = hnpExist; 49069570cc8Sopenharmony_ci sum++; 49169570cc8Sopenharmony_ci } 49269570cc8Sopenharmony_ci } 49369570cc8Sopenharmony_ci cJSON_Delete(json); 49469570cc8Sopenharmony_ci 49569570cc8Sopenharmony_ci return HnpPackageInfoGetOut(packageInfos, sum, packageInfoOut, count); 49669570cc8Sopenharmony_ci} 49769570cc8Sopenharmony_ci 49869570cc8Sopenharmony_ciint HnpPackageInfoHnpDelete(const char *packageName, const char *name, const char *version) 49969570cc8Sopenharmony_ci{ 50069570cc8Sopenharmony_ci char *infoStream; 50169570cc8Sopenharmony_ci int size; 50269570cc8Sopenharmony_ci cJSON *hapItem = NULL; 50369570cc8Sopenharmony_ci cJSON *hnpItem = NULL; 50469570cc8Sopenharmony_ci int hapIndex = 0; 50569570cc8Sopenharmony_ci bool hapExist = false; 50669570cc8Sopenharmony_ci int hnpIndex = 0; 50769570cc8Sopenharmony_ci bool hnpExist = false; 50869570cc8Sopenharmony_ci 50969570cc8Sopenharmony_ci int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size); 51069570cc8Sopenharmony_ci if (ret != 0) { 51169570cc8Sopenharmony_ci if (ret == HNP_ERRNO_BASE_FILE_OPEN_FAILED || ret == HNP_ERRNO_BASE_GET_FILE_LEN_NULL) { 51269570cc8Sopenharmony_ci return 0; 51369570cc8Sopenharmony_ci } else { 51469570cc8Sopenharmony_ci HNP_LOGE("hnp delete read hnp info file unsuccess"); 51569570cc8Sopenharmony_ci return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED; 51669570cc8Sopenharmony_ci } 51769570cc8Sopenharmony_ci } 51869570cc8Sopenharmony_ci 51969570cc8Sopenharmony_ci cJSON *json = cJSON_Parse(infoStream); 52069570cc8Sopenharmony_ci free(infoStream); 52169570cc8Sopenharmony_ci if (json == NULL) { 52269570cc8Sopenharmony_ci HNP_LOGE("hnp delete parse json file unsuccess."); 52369570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_JSON_FAILED; 52469570cc8Sopenharmony_ci } 52569570cc8Sopenharmony_ci 52669570cc8Sopenharmony_ci hapExist = HnpInstallHapExistCheck(packageName, json, &hapItem, &hapIndex); 52769570cc8Sopenharmony_ci if (!hapExist) { 52869570cc8Sopenharmony_ci cJSON_Delete(json); 52969570cc8Sopenharmony_ci return 0; 53069570cc8Sopenharmony_ci } 53169570cc8Sopenharmony_ci 53269570cc8Sopenharmony_ci cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp"); 53369570cc8Sopenharmony_ci hnpExist = HnpInstallHnpExistCheck(hnpItemArr, name, &hnpItem, &hnpIndex, version); 53469570cc8Sopenharmony_ci if (hnpExist) { 53569570cc8Sopenharmony_ci cJSON_DeleteItemFromArray(hnpItemArr, hnpIndex); 53669570cc8Sopenharmony_ci } 53769570cc8Sopenharmony_ci 53869570cc8Sopenharmony_ci ret = HnpHapJsonWrite(json); 53969570cc8Sopenharmony_ci cJSON_Delete(json); 54069570cc8Sopenharmony_ci return ret; 54169570cc8Sopenharmony_ci} 54269570cc8Sopenharmony_ci 54369570cc8Sopenharmony_ciint HnpPackageInfoDelete(const char *packageName) 54469570cc8Sopenharmony_ci{ 54569570cc8Sopenharmony_ci char *infoStream; 54669570cc8Sopenharmony_ci int size; 54769570cc8Sopenharmony_ci cJSON *hapItem = NULL; 54869570cc8Sopenharmony_ci int hapIndex = 0; 54969570cc8Sopenharmony_ci bool hapExist = false; 55069570cc8Sopenharmony_ci 55169570cc8Sopenharmony_ci int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size); 55269570cc8Sopenharmony_ci if (ret != 0) { 55369570cc8Sopenharmony_ci if (ret == HNP_ERRNO_BASE_FILE_OPEN_FAILED || ret == HNP_ERRNO_BASE_GET_FILE_LEN_NULL) { 55469570cc8Sopenharmony_ci return 0; 55569570cc8Sopenharmony_ci } 55669570cc8Sopenharmony_ci HNP_LOGE("package info delete read hnp info file unsuccess"); 55769570cc8Sopenharmony_ci return HNP_ERRNO_BASE_READ_FILE_STREAM_FAILED; 55869570cc8Sopenharmony_ci } 55969570cc8Sopenharmony_ci 56069570cc8Sopenharmony_ci cJSON *json = cJSON_Parse(infoStream); 56169570cc8Sopenharmony_ci free(infoStream); 56269570cc8Sopenharmony_ci if (json == NULL) { 56369570cc8Sopenharmony_ci HNP_LOGE("package info delete parse json file unsuccess."); 56469570cc8Sopenharmony_ci return HNP_ERRNO_BASE_PARSE_JSON_FAILED; 56569570cc8Sopenharmony_ci } 56669570cc8Sopenharmony_ci 56769570cc8Sopenharmony_ci hapExist = HnpInstallHapExistCheck(packageName, json, &hapItem, &hapIndex); 56869570cc8Sopenharmony_ci if (hapExist) { 56969570cc8Sopenharmony_ci cJSON_DeleteItemFromArray(json, hapIndex); 57069570cc8Sopenharmony_ci } 57169570cc8Sopenharmony_ci 57269570cc8Sopenharmony_ci ret = HnpHapJsonWrite(json); 57369570cc8Sopenharmony_ci cJSON_Delete(json); 57469570cc8Sopenharmony_ci return ret; 57569570cc8Sopenharmony_ci} 57669570cc8Sopenharmony_ci 57769570cc8Sopenharmony_cistatic char *HnpNeedUnInstallHnpVersionGet(cJSON *hnpItemArr, const char *name) 57869570cc8Sopenharmony_ci{ 57969570cc8Sopenharmony_ci char *version = NULL; 58069570cc8Sopenharmony_ci if (hnpItemArr == NULL) { 58169570cc8Sopenharmony_ci return NULL; 58269570cc8Sopenharmony_ci } 58369570cc8Sopenharmony_ci 58469570cc8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(hnpItemArr); i++) { 58569570cc8Sopenharmony_ci cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, i); 58669570cc8Sopenharmony_ci cJSON *nameItem = cJSON_GetObjectItem(hnpItem, "name"); 58769570cc8Sopenharmony_ci cJSON *currentItem = cJSON_GetObjectItem(hnpItem, "current_version"); 58869570cc8Sopenharmony_ci cJSON *installItem = cJSON_GetObjectItem(hnpItem, "install_version"); 58969570cc8Sopenharmony_ci if ((nameItem != NULL) && (currentItem != NULL) && (installItem != NULL) && (cJSON_IsString(nameItem)) && 59069570cc8Sopenharmony_ci (cJSON_IsString(currentItem)) && (cJSON_IsString(installItem)) && 59169570cc8Sopenharmony_ci (strcmp(nameItem->valuestring, name) == 0) && 59269570cc8Sopenharmony_ci (strcmp(currentItem->valuestring, installItem->valuestring) == 0)) { 59369570cc8Sopenharmony_ci version = strdup(currentItem->valuestring); 59469570cc8Sopenharmony_ci return version; 59569570cc8Sopenharmony_ci } 59669570cc8Sopenharmony_ci } 59769570cc8Sopenharmony_ci 59869570cc8Sopenharmony_ci return NULL; 59969570cc8Sopenharmony_ci} 60069570cc8Sopenharmony_ci 60169570cc8Sopenharmony_cichar *HnpCurrentVersionGet(const char *name) 60269570cc8Sopenharmony_ci{ 60369570cc8Sopenharmony_ci char *infoStream; 60469570cc8Sopenharmony_ci int size; 60569570cc8Sopenharmony_ci cJSON *hapItem = NULL; 60669570cc8Sopenharmony_ci char *version = NULL; 60769570cc8Sopenharmony_ci 60869570cc8Sopenharmony_ci int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size); 60969570cc8Sopenharmony_ci if (ret != 0) { 61069570cc8Sopenharmony_ci return NULL; 61169570cc8Sopenharmony_ci } 61269570cc8Sopenharmony_ci 61369570cc8Sopenharmony_ci cJSON *json = cJSON_Parse(infoStream); 61469570cc8Sopenharmony_ci free(infoStream); 61569570cc8Sopenharmony_ci if (json == NULL) { 61669570cc8Sopenharmony_ci HNP_LOGE("hnp delete parse json file unsuccess."); 61769570cc8Sopenharmony_ci return NULL; 61869570cc8Sopenharmony_ci } 61969570cc8Sopenharmony_ci 62069570cc8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(json); i++) { 62169570cc8Sopenharmony_ci hapItem = cJSON_GetArrayItem(json, i); 62269570cc8Sopenharmony_ci cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp"); 62369570cc8Sopenharmony_ci if (hnpItemArr == NULL) { 62469570cc8Sopenharmony_ci cJSON_Delete(json); 62569570cc8Sopenharmony_ci return NULL; 62669570cc8Sopenharmony_ci } 62769570cc8Sopenharmony_ci 62869570cc8Sopenharmony_ci for (int j = 0; j < cJSON_GetArraySize(hnpItemArr); j++) { 62969570cc8Sopenharmony_ci cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, j); 63069570cc8Sopenharmony_ci cJSON *nameItem = cJSON_GetObjectItem(hnpItem, "name"); 63169570cc8Sopenharmony_ci cJSON *versionItem = cJSON_GetObjectItem(hnpItem, "current_version"); 63269570cc8Sopenharmony_ci if ((nameItem != NULL) && (versionItem != NULL) && (cJSON_IsString(nameItem)) && 63369570cc8Sopenharmony_ci (cJSON_IsString(versionItem)) && (strcmp(nameItem->valuestring, name) == 0)) { 63469570cc8Sopenharmony_ci version = strdup(versionItem->valuestring); 63569570cc8Sopenharmony_ci cJSON_Delete(json); 63669570cc8Sopenharmony_ci return version; 63769570cc8Sopenharmony_ci } 63869570cc8Sopenharmony_ci } 63969570cc8Sopenharmony_ci } 64069570cc8Sopenharmony_ci 64169570cc8Sopenharmony_ci cJSON_Delete(json); 64269570cc8Sopenharmony_ci return NULL; 64369570cc8Sopenharmony_ci} 64469570cc8Sopenharmony_ci 64569570cc8Sopenharmony_cichar *HnpCurrentVersionUninstallCheck(const char *name) 64669570cc8Sopenharmony_ci{ 64769570cc8Sopenharmony_ci char *infoStream; 64869570cc8Sopenharmony_ci int size; 64969570cc8Sopenharmony_ci cJSON *hapItem = NULL; 65069570cc8Sopenharmony_ci char *version = NULL; 65169570cc8Sopenharmony_ci 65269570cc8Sopenharmony_ci int ret = ReadFileToStream(HNP_PACKAGE_INFO_JSON_FILE_PATH, &infoStream, &size); 65369570cc8Sopenharmony_ci if (ret != 0) { 65469570cc8Sopenharmony_ci return NULL; 65569570cc8Sopenharmony_ci } 65669570cc8Sopenharmony_ci 65769570cc8Sopenharmony_ci cJSON *json = cJSON_Parse(infoStream); 65869570cc8Sopenharmony_ci free(infoStream); 65969570cc8Sopenharmony_ci if (json == NULL) { 66069570cc8Sopenharmony_ci HNP_LOGE("hnp delete parse json file unsuccess."); 66169570cc8Sopenharmony_ci return NULL; 66269570cc8Sopenharmony_ci } 66369570cc8Sopenharmony_ci 66469570cc8Sopenharmony_ci for (int i = 0; i < cJSON_GetArraySize(json); i++) { 66569570cc8Sopenharmony_ci hapItem = cJSON_GetArrayItem(json, i); 66669570cc8Sopenharmony_ci cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp"); 66769570cc8Sopenharmony_ci version = HnpNeedUnInstallHnpVersionGet(hnpItemArr, name); 66869570cc8Sopenharmony_ci if (version != NULL) { 66969570cc8Sopenharmony_ci break; 67069570cc8Sopenharmony_ci } 67169570cc8Sopenharmony_ci } 67269570cc8Sopenharmony_ci 67369570cc8Sopenharmony_ci cJSON_Delete(json); 67469570cc8Sopenharmony_ci return version; 67569570cc8Sopenharmony_ci} 67669570cc8Sopenharmony_ci 67769570cc8Sopenharmony_ci#ifdef __cplusplus 67869570cc8Sopenharmony_ci} 67969570cc8Sopenharmony_ci#endif