1 /*
2  * Copyright (c) 2023 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  * Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS,
9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific lan
11  * guage governing permissions and
12  * limitations under the License.
13  */
14 
15 #include <cstring>
16 
17 #include "isys_installer_callback_func.h"
18 #include "isys_installer_callback.h"
19 #include "module_update_kits.h"
20 #include "module_error_code.h"
21 
22 using namespace OHOS;
23 using namespace OHOS::SysInstaller;
24 
25 static const int32_t MIN_PARAM_NUM = 2;
26 static const int32_t MAX_PARAM_NUM = 3;
27 
28 static const std::string HELP_MSG =
29     "usage: module_update_tool\n"
30     "example: ./module_update_tool install /data/tmp/xxx.hmp \n"
31     "command list:\n"
32     "  install         : upgrade some SA via hmp package\n"
33     "  uninstall       : degrade some SA via hmp name\n"
34     "  update          : upgrade SA via hmp package\n"
35     "  get_hmp_version : get hmp package version\n"
36     "  get_result      : get hmp upgrade result\n"
37     "  show hmpname    : show upgrade sa info, if hmp name is null, show all\n";
38 
39 static const std::string INSTALL_PARAM = "install";
40 static const std::string UNINSTALL_PARAM = "uninstall";
41 static const std::string SHOW_INFO = "show";
42 static const std::string UPDATE_PARAM = "update";
43 static const std::string GET_HMP_VERSION = "get_hmp_version";
44 static const std::string GET_RESULT = "get_result";
45 static const int32_t RET_FAILED = -1;
46 
CheckParam(int argc)47 static bool CheckParam(int argc)
48 {
49     if (argc < MIN_PARAM_NUM || argc > MAX_PARAM_NUM) {
50         printf("Invalid module update command\n");
51         printf("%s", HELP_MSG.c_str());
52         return false;
53     }
54     return true;
55 }
56 
GetFailReasonByErrCode(int32_t err)57 static std::string GetFailReasonByErrCode(int32_t err)
58 {
59     switch (err) {
60         case 0:
61             return "success";
62         case OHOS::SysInstaller::ERR_SERVICE_NOT_FOUND:
63             return "ERR_SERVICE_NOT_FOUND";
64         case OHOS::SysInstaller::ERR_INVALID_PATH:
65             return "ERR_INVALID_PATH";
66         case OHOS::SysInstaller::ERR_LOWER_VERSION:
67             return "ERR_LOWER_VERSION";
68         case OHOS::SysInstaller::ERR_VERIFY_SIGN_FAIL:
69             return "ERR_VERIFY_SIGN_FAIL";
70         case OHOS::SysInstaller::ERR_INSTALL_FAIL:
71             return "ERR_INSTALL_FAIL";
72         case OHOS::SysInstaller::ERR_UNINSTALL_FAIL:
73             return "ERR_UNINSTALL_FAIL";
74         case OHOS::SysInstaller::ERR_REPORT_STATUS_FAIL:
75             return "ERR_REPORT_STATUS_FAIL";
76         default:
77             return "Unknown Error";
78     }
79 }
80 
PrintErrMsg(const std::string & errMsg)81 static void PrintErrMsg(const std::string &errMsg)
82 {
83     printf("%s\n", errMsg.c_str());
84 }
85 
PrintUpgradeInfo(std::list<OHOS::SysInstaller::ModulePackageInfo> & modulePackageInfos)86 static void PrintUpgradeInfo(std::list<OHOS::SysInstaller::ModulePackageInfo> &modulePackageInfos)
87 {
88     std::list<OHOS::SysInstaller::ModulePackageInfo>::iterator it;
89     printf("Got %zu upgraded modules info\n", modulePackageInfos.size());
90     for (it = modulePackageInfos.begin(); it != modulePackageInfos.end(); it++) {
91         OHOS::SysInstaller::ModulePackageInfo moduleInfo = *it;
92         printf("%s\n", moduleInfo.hmpName.c_str());
93         std::list<OHOS::SysInstaller::SaInfo>::iterator saIt;
94         for (saIt = moduleInfo.saInfoList.begin(); saIt != moduleInfo.saInfoList.end(); saIt++) {
95             std::string verStr = (*saIt).version;
96             printf(" {saName:%s saId:%d version:%s}\n", (*saIt).saName.c_str(), (*saIt).saId, verStr.c_str());
97         }
98         printf(" \n");
99     }
100 }
101 
102 class ProcessCallback : public ISysInstallerCallbackFunc {
103 public:
104     ProcessCallback() = default;
105     ~ProcessCallback() = default;
OnUpgradeProgress(UpdateStatus updateStatus,int percent,const std::string & resultMsg)106     void OnUpgradeProgress(UpdateStatus updateStatus, int percent, const std::string &resultMsg) override
107     {
108         printf("ProgressCallback progress %d percent %d msg %s\n", updateStatus, percent, resultMsg.c_str());
109     }
110 };
111 
UpdateModulePackage(const std::string & path)112 static int UpdateModulePackage(const std::string &path)
113 {
114     printf("try to update an upgrade package\n");
115     sptr<ISysInstallerCallbackFunc> callback = new ProcessCallback;
116     if (callback == nullptr) {
117         printf("callback new failed\n");
118         return -1;
119     }
120 
121     int ret = ModuleUpdateKits::GetInstance().StartUpdateHmpPackage(path, callback);
122     PrintErrMsg(GetFailReasonByErrCode(ret));
123     return ret;
124 }
125 
GetHmpVersion()126 static int GetHmpVersion()
127 {
128     printf("try to get hmp version\n");
129 
130     std::vector<HmpVersionInfo> versioInfo = ModuleUpdateKits::GetInstance().GetHmpVersionInfo();
131     for (auto &info : versioInfo) {
132         printf("name:%s laneCode:%s compatibleVersion:%s version:%s\n",
133             info.name.c_str(), info.laneCode.c_str(), info.compatibleVersion.c_str(), info.version.c_str());
134     }
135     return 0;
136 }
137 
GetResult()138 static int GetResult()
139 {
140     printf("try to get hmp result\n");
141 
142     std::vector<HmpUpdateInfo> updateInfo = ModuleUpdateKits::GetInstance().GetHmpUpdateResult();
143     for (auto &info : updateInfo) {
144         printf("path:%s result:%d resultMsg:%s\n", info.path.c_str(), info.result, info.resultMsg.c_str());
145     }
146     return 0;
147 }
148 
ShowInfo(const std::string & hmpStr)149 static int ShowInfo(const std::string &hmpStr)
150 {
151     printf("try to show module update info\n");
152     std::list<OHOS::SysInstaller::ModulePackageInfo> modulePackageInfos;
153     int ret = OHOS::SysInstaller::ModuleUpdateKits::GetInstance().GetModulePackageInfo(hmpStr, modulePackageInfos);
154     PrintUpgradeInfo(modulePackageInfos);
155     PrintErrMsg(GetFailReasonByErrCode(ret));
156     return ret;
157 }
158 
main(int argc,char ** argv)159 int main(int argc, char **argv)
160 {
161     if (!CheckParam(argc)) {
162         return RET_FAILED;
163     }
164 
165     int ret = 0;
166     OHOS::SysInstaller::ModuleUpdateKits& moduleUpdateKits = OHOS::SysInstaller::ModuleUpdateKits::GetInstance();
167     ret = moduleUpdateKits.InitModuleUpdate();
168     if (ret != 0) {
169         PrintErrMsg(GetFailReasonByErrCode(ret));
170         return ret;
171     }
172 
173     if (INSTALL_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) {
174         printf("try to update a module\n");
175         ret = moduleUpdateKits.InstallModulePackage(argv[MIN_PARAM_NUM]);
176         PrintErrMsg(GetFailReasonByErrCode(ret));
177         return ret;
178     }
179     if (UNINSTALL_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) {
180         printf("try to uninstall an upgrade package\n");
181         ret = moduleUpdateKits.UninstallModulePackage(argv[MIN_PARAM_NUM]);
182         PrintErrMsg(GetFailReasonByErrCode(ret));
183         return ret;
184     }
185     if (UPDATE_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) {
186         return UpdateModulePackage(argv[MIN_PARAM_NUM]);
187     }
188     if (GET_HMP_VERSION.compare(argv[1]) == 0) {
189         return GetHmpVersion();
190     }
191     if (GET_RESULT.compare(argv[1]) == 0) {
192         return GetResult();
193     }
194     if (SHOW_INFO.compare(argv[1]) == 0) {
195         return ShowInfo((argc != MIN_PARAM_NUM) ? argv[MIN_PARAM_NUM] : "");
196     }
197 
198     printf("invalid command. \n");
199     printf("%s", HELP_MSG.c_str());
200     return ret;
201 }