hnp安装版本保留安装逻辑

Signed-off-by: 王达 <wangda20@huawei.com>
This commit is contained in:
王达 2024-07-27 17:21:46 +08:00
parent 64f091ba98
commit 49445f8997
3 changed files with 40 additions and 27 deletions

View File

@ -65,6 +65,7 @@ typedef struct NativeBinLinkStru {
typedef struct HnpCfgInfoStru {
char name[MAX_FILE_PATH_LEN];
char version[HNP_VERSION_LEN]; // Native软件包版本号
bool isInstall; // 是否已安装
unsigned int linkNum; // 软链接配置个数
NativeBinLink *links;
} HnpCfgInfo;
@ -260,7 +261,7 @@ int HnpPackageInfoHnpDelete(const char *packageName, const char *name, const cha
int HnpPackageInfoDelete(const char *packageName);
char *HnpPackgeHnpVersionGet(const char *packageName, const char *name);
char *HnpCurrentVersionUninstallCheck(const char *name);
int HnpFileCountGet(const char *path, int *count);

View File

@ -93,7 +93,7 @@ static int ParseJsonStreamToHnpCfgInfo(cJSON *json, HnpCfgInfo *hnpCfg)
HNP_LOGE("strcpy unsuccess.");
return HNP_ERRNO_BASE_COPY_FAILED;
}
cJSON *versionItem = cJSON_GetObjectItem(json, "version");
cJSON *versionItem = cJSON_GetObjectItem(json, "curretn_version");
if ((versionItem == NULL) || (versionItem->valuestring == NULL)) {
HNP_LOGE("get version info in cfg unsuccess.");
return HNP_ERRNO_BASE_PARSE_ITEM_NO_FOUND;
@ -170,7 +170,7 @@ int GetHnpJsonBuff(HnpCfgInfo *hnpCfg, char **buff)
cJSON_AddStringToObject(root, "type", "hnp-config");
cJSON_AddStringToObject(root, "name", hnpCfg->name);
cJSON_AddStringToObject(root, "version", hnpCfg->version);
cJSON_AddStringToObject(root, "current_version", hnpCfg->version);
cJSON_AddObjectToObject(root, "install");
char *str = cJSON_Print(root);
cJSON_Delete(root);
@ -208,7 +208,7 @@ static bool HnpInstallHnpExistCheck(cJSON *hnpItemArr, const char *name, cJSON *
for (int i = 0; i < cJSON_GetArraySize(hnpItemArr); i++) {
cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, i);
cJSON *nameJson = cJSON_GetObjectItem(hnpItem, "name");
cJSON *versionJson = cJSON_GetObjectItem(hnpItem, "version");
cJSON *versionJson = cJSON_GetObjectItem(hnpItem, "current_version");
if ((nameJson != NULL) && (strcmp(nameJson->valuestring, name) == 0)) {
*hnpItemOut = hnpItem;
*hnpIndex = i;
@ -235,7 +235,7 @@ static void HnpPackageVersionUpdateAll(cJSON *json, const HnpCfgInfo *hnpCfg)
(strcmp(cJSON_GetObjectItem(hnpItem, "name")->valuestring, hnpCfg->name) != 0)) {
continue;
}
cJSON *version = cJSON_GetObjectItem(hnpItem, "version");
cJSON *version = cJSON_GetObjectItem(hnpItem, "current_version");
if (version == NULL) {
break;
}
@ -299,7 +299,10 @@ static int HnpHapJsonHnpAdd(bool hapExist, cJSON *json, cJSON *hapItem, const ch
return HNP_ERRNO_BASE_JSON_ARRAY_CREATE_FAILED;
}
cJSON_AddItemToObject(hnpItem, "name", cJSON_CreateString(hnpCfg->name));
cJSON_AddItemToObject(hnpItem, "version", cJSON_CreateString(hnpCfg->version));
cJSON_AddItemToObject(hnpItem, "current_version", cJSON_CreateString(hnpCfg->version));
if (hnpCfg->isInstall) {
cJSON_AddItemToObject(hnpItem, "install_version", cJSON_CreateString(hnpCfg->version));
}
cJSON_AddItemToArray(hnpItemArr, hnpItem);
HnpPackageVersionUpdateAll(json, hnpCfg);
@ -348,8 +351,8 @@ int HnpInstallInfoJsonWrite(const char *hapPackageName, const HnpCfgInfo *hnpCfg
cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
bool hnpExist = HnpInstallHnpExistCheck(hnpItemArr, hnpCfg->name, &hnpItem, &hnpIndex, NULL);
if (hnpExist) {
cJSON *versionJson = cJSON_GetObjectItem(hnpItem, "version");
if (versionJson != NULL) {
cJSON *versionJson = cJSON_GetObjectItem(hnpItem, "current_version");
if (versionJson != NULL) { //版本存在即更新版本非新增无需更新install_version
cJSON_SetValuestring(versionJson, hnpCfg->version);
HnpPackageVersionUpdateAll(json, hnpCfg);
ret = HnpHapJsonWrite(json);
@ -445,7 +448,7 @@ int HnpPackageInfoGet(const char *packageName, HnpPackageInfo **packageInfoOut,
for (int j = 0; j < cJSON_GetArraySize(hnpItemArr); j++) {
cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, j);
cJSON *name = cJSON_GetObjectItem(hnpItem, "name");
cJSON *version = cJSON_GetObjectItem(hnpItem, "version");
cJSON *version = cJSON_GetObjectItem(hnpItem, "current_version");
if (name == NULL || version == NULL) {
continue;
}
@ -545,7 +548,7 @@ int HnpPackageInfoDelete(const char *packageName)
return ret;
}
static char *HnpInstallHnpVersionGet(cJSON *hnpItemArr, const char *name)
static char *HnpNeedUnInstallHnpVersionGet(cJSON *hnpItemArr, const char *name)
{
if (hnpItemArr == NULL) {
return NULL;
@ -554,16 +557,19 @@ static char *HnpInstallHnpVersionGet(cJSON *hnpItemArr, const char *name)
for (int i = 0; i < cJSON_GetArraySize(hnpItemArr); i++) {
cJSON *hnpItem = cJSON_GetArrayItem(hnpItemArr, i);
if ((cJSON_GetObjectItem(hnpItem, "name") != NULL) &&
(cJSON_GetObjectItem(hnpItem, "version") != NULL) &&
(strcmp(cJSON_GetObjectItem(hnpItem, "name")->valuestring, name) == 0)) {
return cJSON_GetObjectItem(hnpItem, "version")->valuestring;
(cJSON_GetObjectItem(hnpItem, "current_version") != NULL) &&
(cJSON_GetObjectItem(hnpItem, "install_version") != NULL) &&
(strcmp(cJSON_GetObjectItem(hnpItem, "name")->valuestring, name) == 0) &&
(strcmp(cJSON_GetObjectItem(hnpItem, "current_version")->valuestring,
cJSON_GetObjectItem(hnpItem, "install_version")->valuestring) == 0)) {
return cJSON_GetObjectItem(hnpItem, "current_version")->valuestring;
}
}
return NULL;
}
char *HnpPackgeHnpVersionGet(const char *packageName, const char *name)
char *HnpCurrentVersionUninstallCheck(const char *name)
{
char *infoStream;
int size;
@ -585,7 +591,7 @@ char *HnpPackgeHnpVersionGet(const char *packageName, const char *name)
for (int i = 0; i < cJSON_GetArraySize(json); i++) {
hapItem = cJSON_GetArrayItem(json, i);
cJSON *hnpItemArr = cJSON_GetObjectItem(hapItem, "hnp");
version = HnpInstallHnpVersionGet(hnpItemArr, name);
version = HnpNeedUnInstallHnpVersionGet(hnpItemArr, name);
if (version != NULL) {
break;
}

View File

@ -276,7 +276,6 @@ static int HnpUnInstall(int uid, const char *packageName)
static int HnpInstallForceCheck(HnpCfgInfo *hnpCfgInfo, HnpInstallInfo *hnpInfo)
{
int ret = 0;
char *version = NULL;
/* 判断安装目录是否存在,存在判断是否是强制安装,如果是则走卸载流程,否则返回错误 */
if (access(hnpInfo->hnpSoftwarePath, F_OK) == 0) {
@ -284,17 +283,10 @@ static int HnpInstallForceCheck(HnpCfgInfo *hnpCfgInfo, HnpInstallInfo *hnpInfo)
HNP_LOGE("hnp install path[%{public}s] exist, but force is false", hnpInfo->hnpSoftwarePath);
return HNP_ERRNO_INSTALLER_PATH_IS_EXIST;
}
if (hnpInfo->isPublic) {
version = HnpPackgeHnpVersionGet(hnpInfo->hapInstallInfo->hapPackageName, hnpCfgInfo->name);
if (version != NULL) {
ret = HnpUnInstallPublicHnp(hnpInfo->hapInstallInfo->hapPackageName, hnpCfgInfo->name, version,
hnpInfo->hapInstallInfo->uid);
if (hnpInfo->isPublic == false) {
if (HnpDeleteFolder(hnpInfo->hnpSoftwarePath) != 0) {
return ret;
}
} else {
ret = HnpDeleteFolder(hnpInfo->hnpSoftwarePath);
}
if (ret != 0) {
return ret;
}
}
@ -328,6 +320,19 @@ static int HnpInstallPathGet(HnpCfgInfo *hnpCfgInfo, HnpInstallInfo *hnpInfo)
return 0;
}
static int HnpPublicDealAfterInstall(HnpInstallInfo *hnpInfo, HnpCfgInfo *hnpCfg)
{
char *version = HnpCurrentVersionUninstallCheck(hnpCfgInfo->name);
if (version != NULL) {
HnpUnInstallPublicHnp(hnpInfo->hapInstallInfo->hapPackageName, hnpCfgInfo->name, version,
hnpInfo->hapInstallInfo->uid);
}
hnpCfg->isInstall = true;
return HnpInstallInfoJsonWrite(hnpInfo->hapInstallInfo->hapPackageName, hnpCfg);
}
static int HnpReadAndInstall(char *srcFile, HnpInstallInfo *hnpInfo, HnpSignMapInfo *hnpSignMapInfos, int *count)
{
int ret;
@ -378,8 +383,9 @@ static int HnpReadAndInstall(char *srcFile, HnpInstallInfo *hnpInfo, HnpSignMapI
hnpInfo->hapInstallInfo->uid);
return ret;
}
if (hnpInfo->isPublic) {
ret = HnpInstallInfoJsonWrite(hnpInfo->hapInstallInfo->hapPackageName, &hnpCfg);
ret = HnpPublicDealAfterInstall(hnpInfo, &hnpCfg);
if (ret != 0) {
HnpUnInstallPublicHnp(hnpInfo->hapInstallInfo->hapPackageName, hnpCfg.name, hnpCfg.version,
hnpInfo->hapInstallInfo->uid);