diff --git a/include/bundle_installer.h b/include/bundle_installer.h index ba5aad53818d8e3bb573a5a52c2213fc5c4b2800..21777a71d7f17b2d09e4cdbc9c44082e2952bd7d 100755 --- a/include/bundle_installer.h +++ b/include/bundle_installer.h @@ -43,6 +43,7 @@ private: bool isUpdate, uint8_t hapType); uint8_t UpdateBundleInfo(const char *appId, const BundleRes &bundleRes, BundleInfo *bundleInfo, bool isUpdate, uint8_t hapType); + uint8_t ReshapeAppId(const char *bundleName, std::string &appId); #ifndef __LINUX__ uint8_t CheckProvisionInfoIsValid(const SignatureInfo &signatureInfo, const Permissions &permissions, const char *bundleName); @@ -69,8 +70,7 @@ private: #define CHECK_PRO_RESULT(errcode, bundleInfo, permissions, abilityRes) \ do { \ if (errcode != ERR_OK) { \ - ClearBundleInfo(bundleInfo); \ - AdapterFree(bundleInfo); \ + BundleInfoUtils::FreeBundleInfo(bundleInfo); \ AdapterFree(permissions.permissionTrans); \ AdapterFree(abilityRes); \ return errcode; \ @@ -86,15 +86,27 @@ private: } \ } while (0) -#define CHECK_PRO_ROLLBACK(errcode, permissions, bundleInfo, abilityRes) \ - do { \ - if (errcode != ERR_OK && bundleInfo != nullptr) { \ - BundleUtil::DeleteJsonFile(bundleInfo->bundleName); \ - CLEAR_INSTALL_ENV(bundleInfo); \ - AdapterFree(permissions.permissionTrans); \ - AdapterFree(abilityRes); \ - return errcode; \ - } \ +#define CHECK_PRO_PART_ROLLBACK(errcode, path, permissions, bundleInfo, abilityRes) \ + do { \ + if (errcode != ERR_OK && bundleInfo != nullptr) { \ + BundleDaemonClient::GetInstance().RemoveFile(path.c_str()); \ + BundleInfoUtils::FreeBundleInfo(bundleInfo); \ + AdapterFree(permissions.permissionTrans); \ + AdapterFree(abilityRes); \ + return errcode; \ + } \ + } while (0) + +#define CHECK_PRO_ROLLBACK(errcode, permissions, bundleInfo, abilityRes) \ + do { \ + if (errcode != ERR_OK && bundleInfo != nullptr) { \ + AdapterFree(permissions.permissionTrans); \ + AdapterFree(abilityRes); \ + ManagerService::GetInstance().RemoveBundleInfo(bundleInfo->bundleName); \ + BundleUtil::DeleteJsonFile(bundleInfo->bundleName); \ + CLEAR_INSTALL_ENV(bundleInfo); \ + return errcode; \ + } \ } while (0) } // namespace OHOS #endif // OHOS_BUNDLE_INSTALLER_H diff --git a/src/bundle_installer.cpp b/src/bundle_installer.cpp index c0d3f7814a466016c8cab98baf421d87dfe26839..723780ce2fecd1ed2a7b96c5c036c71918375033 100755 --- a/src/bundle_installer.cpp +++ b/src/bundle_installer.cpp @@ -174,11 +174,8 @@ uint8_t BundleInstaller::ProcessBundleInstall(const std::string &path, InstallRe errorCode = CheckProvisionInfoIsValid(signatureInfo, permissions, bundleInfo->bundleName); CHECK_PRO_RESULT(errorCode, bundleInfo, permissions, bundleRes.abilityRes); // check version and signature when in update status - size_t index = signatureInfo.appId.find_first_of(DELIMITER); - errorCode = (index != std::string::npos) ? ERR_OK : ERR_APPEXECFWK_INSTALL_FAILED_INVALID_PROVISIONINFO; + errorCode = ReshapeAppId(bundleInfo->bundleName, signatureInfo.appId); CHECK_PRO_RESULT(errorCode, bundleInfo, permissions, bundleRes.abilityRes); - signatureInfo.appId = signatureInfo.appId.replace(signatureInfo.appId.begin(), signatureInfo.appId.begin() + - index, bundleInfo->bundleName); bundleInfo->appId = Utils::Strdup(signatureInfo.appId.c_str()); } else { bundleInfo->appId = Utils::Strdup(APPID); @@ -187,11 +184,8 @@ uint8_t BundleInstaller::ProcessBundleInstall(const std::string &path, InstallRe errorCode = CheckProvisionInfoIsValid(signatureInfo, permissions, bundleInfo->bundleName); CHECK_PRO_RESULT(errorCode, bundleInfo, permissions, bundleRes.abilityRes); // check version and signature when in update status - size_t index = signatureInfo.appId.find_first_of(DELIMITER); - errorCode = (index != std::string::npos) ? ERR_OK : ERR_APPEXECFWK_INSTALL_FAILED_INVALID_PROVISIONINFO; + errorCode = ReshapeAppId(bundleInfo->bundleName, signatureInfo.appId); CHECK_PRO_RESULT(errorCode, bundleInfo, permissions, bundleRes.abilityRes); - signatureInfo.appId = signatureInfo.appId.replace(signatureInfo.appId.begin(), signatureInfo.appId.begin() + - index, bundleInfo->bundleName); bundleInfo->appId = Utils::Strdup(signatureInfo.appId.c_str()); #endif errorCode = (bundleInfo->appId == nullptr) ? ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR : ERR_OK; @@ -210,7 +204,7 @@ uint8_t BundleInstaller::ProcessBundleInstall(const std::string &path, InstallRe std::string tmpCodePath = codePath + TMP_SUFFIX; errorCode = (BundleDaemonClient::GetInstance().ExtractHap(path.c_str(), tmpCodePath.c_str()) == EC_SUCCESS) ? ERR_OK : ERR_APPEXECFWK_INSTALL_FAILED_EXTRACT_HAP_ERROR; - CHECK_PRO_ROLLBACK(errorCode, permissions, bundleInfo, bundleRes.abilityRes); + CHECK_PRO_PART_ROLLBACK(errorCode, tmpCodePath, permissions, bundleInfo, bundleRes.abilityRes); // rename install path and record install infomation bool isUpdate = ManagerService::GetInstance().QueryBundleInfo(installRecord.bundleName) != nullptr; errorCode = HandleFileAndBackUpRecord(tmpCodePath.c_str(), codePath.c_str(), installRecord, isUpdate, hapType); @@ -230,6 +224,23 @@ uint8_t BundleInstaller::ProcessBundleInstall(const std::string &path, InstallRe return ERR_OK; } +uint8_t BundleInstaller::ReshapeAppId(const char *bundleName, std::string &appId) +{ + if (bundleName == nullptr) { + return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR; + } + size_t index = appId.find_first_of(DELIMITER); + if (index == std::string::npos) { + return ERR_APPEXECFWK_INSTALL_FAILED_INVALID_PROVISIONINFO; + } + appId = appId.replace(appId.begin(), appId.begin() + index, bundleName); + if (appId.empty()) { + HILOG_ERROR(HILOG_MODULE_APP, "appId is empty!"); + return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR; + } + return ERR_OK; +} + #ifndef __LINUX__ uint8_t BundleInstaller::CheckProvisionInfoIsValid(const SignatureInfo &signatureInfo, const Permissions &permissions, const char *bundleName) @@ -334,7 +345,7 @@ uint8_t BundleInstaller::HandleFileAndBackUpRecord(const char *tmpPath, const ch amsInterface->TerminateApp(record.bundleName); if (BundleDaemonClient::GetInstance().RenameFile(tmpPath, realPath) != EC_SUCCESS) { - ManagerService::GetInstance().RemoveBundleInfo(record.bundleName); + BundleDaemonClient::GetInstance().RemoveFile(tmpPath); return ERR_APPEXECFWK_INSTALL_FAILED_RENAME_DIR_ERROR; } @@ -356,6 +367,7 @@ uint8_t BundleInstaller::HandleFileAndBackUpRecord(const char *tmpPath, const ch } else { BundleInfo *bundleInfo = ManagerService::GetInstance().QueryBundleInfo(record.bundleName); if (bundleInfo == nullptr) { + HILOG_ERROR(HILOG_MODULE_APP, "bundleInfo is nullptr when query bundleInfo!"); return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR; } record.uid = bundleInfo->uid; @@ -364,7 +376,6 @@ uint8_t BundleInstaller::HandleFileAndBackUpRecord(const char *tmpPath, const ch std::string bundleTmpJsonPath = std::string(JSON_PATH) + PATH_SEPARATOR + record.bundleName + TMP_SUFFIX + JSON_SUFFIX; if (!BackUpInstallRecord(record, bundleTmpJsonPath.c_str())) { - ManagerService::GetInstance().RemoveBundleInfo(record.bundleName); return ERR_APPEXECFWK_INSTALL_FAILED_RECORD_INFO_ERROR; } return ERR_OK; @@ -667,18 +678,15 @@ uint8_t BundleInstaller::StorePermissions(const char *bundleName, PermissionTran if (bundleName == nullptr || permissions == nullptr) { HILOG_ERROR(HILOG_MODULE_APP, "Store permission fail when param is nullptr!"); - ManagerService::GetInstance().RemoveBundleInfo(bundleName); return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR; } if (BundleDaemonClient::GetInstance().CreatePermissionDir() != EC_SUCCESS) { - ManagerService::GetInstance().RemoveBundleInfo(bundleName); return ERR_APPEXECFWK_INSTALL_FAILED_CREATE_PERMISSIONS_DIR_ERROR; } if (SaveOrUpdatePermissions(const_cast(bundleName), permissions, permNum, static_cast(isUpdate)) != 0) { - ManagerService::GetInstance().RemoveBundleInfo(bundleName); return ERR_APPEXECFWK_INSTALL_FAILED_STORE_PERMISSIONS_ERROR; } return ERR_OK;