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