1 /* 2 * Copyright (c) 2020 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 OHOS_GT_BUNDLE_INSTALLER_H 17 #define OHOS_GT_BUNDLE_INSTALLER_H 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 #ifdef __cplusplus 23 } 24 #endif 25 26 #include "bundle_common.h" 27 #include "bundle_info.h" 28 #include "bundle_info_utils.h" 29 #include "bundle_manager.h" 30 #include "bundle_manager_inner.h" 31 #include "stdint.h" 32 33 namespace OHOS { 34 class GtBundleInstaller { 35 public: 36 GtBundleInstaller() = default; 37 ~GtBundleInstaller() = default; 38 39 uint8_t Install(const char *path, InstallerCallback installerCallback); 40 uint8_t Uninstall(const char *bundleName); 41 private: 42 uint8_t PreCheckBundle(const char *path, int32_t &fp, SignatureInfo &signatureInfo, uint32_t &fileSize, 43 uint8_t bundleStyle); 44 uint8_t ProcessBundleInstall(const char *path, const char *randStr, InstallRecord &installRecord, 45 uint8_t bundleStyle, InstallerCallback installerCallback); 46 uint8_t HandleFileAndBackUpRecord(const InstallRecord &record, const char *tmpPath, const char *randStr, 47 const char *dataPath, bool isUpdate); 48 uint8_t UpdateBundleInfo(uint8_t bundleStyle, uint32_t labelId, uint32_t iconId, BundleInfo *bundleInfo, 49 bool isUpdate); 50 void RecordThirdSystemBundle(const char *bundleName, const char *path); 51 uint8_t StorePermissions(const char *bundleName, PermissionTrans *permissions, int32_t permNum, bool isUpdate); 52 uint8_t CheckVersionAndSignature(const char *bundleName, const char *appId, BundleInfo *bundleInfo); 53 bool RenameJsonFile(const char *fileName, const char *randStr); 54 bool CheckIsThirdSystemBundle(const char *bundleName); 55 bool BackUpInstallRecord(const InstallRecord &record, const char *jsonPath); 56 void InitThirdSystemBundleRecord(const char *bundleName, const char *path); 57 uint8_t VerifySignature(const char *path, SignatureInfo &signatureInfo, uint32_t &fileSize, uint8_t bundleStyle); 58 bool MatchBundleName(const char *bundleName, const char *matchedBundleName); 59 bool MatchPermissions(char ** restrictedPermissions, uint32_t restricNum, PermissionTrans *permissionsTrans, 60 int32_t permTransNum); 61 uint8_t CheckProvisionInfoIsValid(const SignatureInfo &signatureInfo, const Permissions &permissions, 62 const char *bundleName); 63 char *ReshapeAppId(const char *bundleName, char *appId); 64 uint8_t SwitchErrorCode(int32_t errorCode); 65 uint8_t AddBundleResList(const char *bundleName, uint32_t labelId, uint32_t iconId); 66 uint8_t MoveRawFileToDataPath(const BundleInfo *bundleInfo); 67 uint8_t TransformJsToBc(const char *codePath, InstallRecord &record); 68 }; 69 70 #define FREE_PRO_RESOURCE(fp, permissions, bundleInfo) \ 71 do { \ 72 if ((fp) >= 0) { \ 73 close(fp); \ 74 } \ 75 BundleInfoUtils::FreeBundleInfo(bundleInfo); \ 76 UI_Free((permissions).permissionTrans); \ 77 } while (0) 78 79 #define FREE_SIGNATUREINFO(signatureInfo) \ 80 do { \ 81 AdapterFree((signatureInfo).appId); \ 82 AdapterFree(signatureInfo.bundleName); \ 83 BundleUtil::FreeStrArrayMemory(signatureInfo.restricPermission, signatureInfo.restricNum); \ 84 } while (0) 85 86 #define CHECK_PRO_RESULT(errcode, fp, permissions, bundleInfo, signatureInfo) \ 87 do { \ 88 if ((errcode) != ERR_OK) { \ 89 FREE_PRO_RESOURCE(fp, permissions, bundleInfo); \ 90 FREE_SIGNATUREINFO(signatureInfo); \ 91 return errcode; \ 92 } \ 93 } while (0) 94 95 #define CLEAR_INSTALL_ENV(bundleInfo) \ 96 do { \ 97 if ((bundleInfo) != nullptr) { \ 98 BundleUtil::RemoveDir(bundleInfo->codePath); \ 99 BundleUtil::RemoveDir(bundleInfo->dataPath); \ 100 BundleInfoUtils::FreeBundleInfo(bundleInfo); \ 101 } \ 102 } while (0) 103 104 #define CHECK_PRO_PART_ROLLBACK(errcode, path, permissions, bundleInfo, signatureInfo) \ 105 do { \ 106 if ((errcode) != ERR_OK) { \ 107 BundleUtil::RemoveDir(path); \ 108 AdapterFree(path); \ 109 BundleInfoUtils::FreeBundleInfo(bundleInfo); \ 110 FREE_SIGNATUREINFO(signatureInfo); \ 111 UI_Free((permissions).permissionTrans); \ 112 return errcode; \ 113 } \ 114 } while (0) 115 116 #define CHECK_PRO_ROLLBACK(errcode, permissions, bundleInfo, signatureInfo, randStr) \ 117 do { \ 118 if ((errcode) != ERR_OK && (bundleInfo) != nullptr) { \ 119 FREE_SIGNATUREINFO(signatureInfo); \ 120 UI_Free((permissions).permissionTrans); \ 121 GtManagerService::GetInstance().RemoveBundleInfo(bundleInfo->bundleName); \ 122 BundleUtil::DeleteJsonFile(bundleInfo->bundleName, randStr); \ 123 CLEAR_INSTALL_ENV(bundleInfo); \ 124 return errcode; \ 125 } \ 126 } while (0) 127 } // namespace OHOS 128 #endif // OHOS_GT_BUNDLE_INSTALLER_H