• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "scope_guard.h"
22 
23 using namespace OHOS;
24 using namespace Updater;
25 using namespace OHOS::SysInstaller;
26 
27 static const int32_t MIN_PARAM_NUM = 2;
28 static const int32_t MAX_PARAM_NUM = 3;
29 
30 static const std::string HELP_MSG =
31     "usage: module_update_tool\n"
32     "example: ./module_update_tool install /data/tmp/xxx.hmp \n"
33     "command list:\n"
34     "  install         : upgrade some SA via hmp package\n"
35     "  uninstall       : degrade some SA via hmp name\n"
36     "  update          : upgrade SA via hmp package\n"
37     "  get_hmp_version : get hmp package version\n"
38     "  get_result      : get hmp upgrade result\n"
39     "  show hmpname    : show upgrade sa info, if hmp name is null, show all\n";
40 
41 static const std::string INSTALL_PARAM = "install";
42 static const std::string UNINSTALL_PARAM = "uninstall";
43 static const std::string SHOW_INFO = "show";
44 static const std::string UPDATE_PARAM = "update";
45 static const std::string GET_HMP_VERSION = "get_hmp_version";
46 static const std::string GET_RESULT = "get_result";
47 static const int32_t RET_FAILED = -1;
48 
CheckParam(int argc)49 static bool CheckParam(int argc)
50 {
51     if (argc < MIN_PARAM_NUM || argc > MAX_PARAM_NUM) {
52         printf("Invalid module update command\n");
53         printf("%s", HELP_MSG.c_str());
54         return false;
55     }
56     return true;
57 }
58 
GetFailReasonByErrCode(int32_t err)59 static std::string GetFailReasonByErrCode(int32_t err)
60 {
61     switch (err) {
62         case 0:
63             return "success";
64         case OHOS::SysInstaller::ERR_SERVICE_NOT_FOUND:
65             return "ERR_SERVICE_NOT_FOUND";
66         case OHOS::SysInstaller::ERR_INVALID_PATH:
67             return "ERR_INVALID_PATH";
68         case OHOS::SysInstaller::ERR_LOWER_VERSION:
69             return "ERR_LOWER_VERSION";
70         case OHOS::SysInstaller::ERR_VERIFY_FAIL:
71             return "ERR_VERIFY_FAIL";
72         case OHOS::SysInstaller::ERR_INSTALL_FAIL:
73             return "ERR_INSTALL_FAIL";
74         case OHOS::SysInstaller::ERR_UNINSTALL_FAIL:
75             return "ERR_UNINSTALL_FAIL";
76         case OHOS::SysInstaller::ERR_REPORT_STATUS_FAIL:
77             return "ERR_REPORT_STATUS_FAIL";
78         default:
79             return "Unknown Error, number is " + std::to_string(err);
80     }
81 }
82 
PrintErrMsg(const std::string & errMsg)83 static void PrintErrMsg(const std::string &errMsg)
84 {
85     printf("%s\n", errMsg.c_str());
86 }
87 
PrintUpgradeInfo(std::list<OHOS::SysInstaller::ModulePackageInfo> & modulePackageInfos)88 static void PrintUpgradeInfo(std::list<OHOS::SysInstaller::ModulePackageInfo> &modulePackageInfos)
89 {
90     std::list<OHOS::SysInstaller::ModulePackageInfo>::iterator it;
91     printf("Got %zu upgraded modules info\n", modulePackageInfos.size());
92     for (it = modulePackageInfos.begin(); it != modulePackageInfos.end(); it++) {
93         OHOS::SysInstaller::ModulePackageInfo moduleInfo = *it;
94         printf("%s, %s, %s, %s, apiversion: %d\n", moduleInfo.hmpName.c_str(), moduleInfo.version.c_str(),
95             moduleInfo.saSdkVersion.c_str(), moduleInfo.type.c_str(), moduleInfo.apiVersion);
96         for (const auto &[key, value] : moduleInfo.moduleMap) {
97             printf("module: %s\n", key.c_str());
98             for (const auto& sa : value.saInfoList) {
99                 printf("SA: \n");
100                 printf(" {saName:%s saId:%d version:%s}\n", sa.saName.c_str(), sa.saId,
101                     std::string(sa.version).c_str());
102             }
103             for (const auto& bundle : value.bundleInfoList) {
104                 printf("Bundle: \n");
105                 printf("{bundleName:%s version:%s}\n", bundle.bundleName.c_str(), bundle.bundleVersion.c_str());
106             }
107         }
108         printf(" \n");
109     }
110 }
111 
112 class ProcessCallback : public ISysInstallerCallbackFunc {
113 public:
114     ProcessCallback() = default;
115     ~ProcessCallback() = default;
OnUpgradeProgress(UpdateStatus updateStatus,int percent,const std::string & resultMsg)116     void OnUpgradeProgress(UpdateStatus updateStatus, int percent, const std::string &resultMsg) override
117     {
118         printf("ProgressCallback progress %d percent %d msg %s\n", updateStatus, percent, resultMsg.c_str());
119     }
OnUpgradeDealLen(UpdateStatus updateStatus,int dealLen,const std::string & resultMsg)120     void OnUpgradeDealLen(UpdateStatus updateStatus, int dealLen, const std::string &resultMsg) override
121     {
122         printf("ProgressCallback progress %d dealLen %d msg %s\n", updateStatus, dealLen, resultMsg.c_str());
123     }
124 };
125 
UpdateModulePackage(const std::string & path)126 static int UpdateModulePackage(const std::string &path)
127 {
128     printf("try to update an upgrade package\n");
129     sptr<ISysInstallerCallbackFunc> callback = new ProcessCallback;
130     if (callback == nullptr) {
131         printf("callback new failed\n");
132         return -1;
133     }
134 
135     int ret = ModuleUpdateKits::GetInstance().StartUpdateHmpPackage(path, callback);
136     PrintErrMsg(GetFailReasonByErrCode(ret));
137     return ret;
138 }
139 
GetHmpVersion()140 static int GetHmpVersion()
141 {
142     printf("try to get hmp version\n");
143 
144     std::vector<HmpVersionInfo> versioInfo = ModuleUpdateKits::GetInstance().GetHmpVersionInfo();
145     for (auto &info : versioInfo) {
146         printf("name:%s laneCode:%s compatibleVersion:%s version:%s\n",
147             info.name.c_str(), info.laneCode.c_str(), info.compatibleVersion.c_str(), info.version.c_str());
148     }
149     return 0;
150 }
151 
GetResult()152 static int GetResult()
153 {
154     printf("try to get hmp result\n");
155 
156     std::vector<HmpUpdateInfo> updateInfo = ModuleUpdateKits::GetInstance().GetHmpUpdateResult();
157     for (auto &info : updateInfo) {
158         printf("path:%s result:%d resultMsg:%s\n", info.path.c_str(), info.result, info.resultMsg.c_str());
159     }
160     return 0;
161 }
162 
ShowInfo(const std::string & hmpStr)163 static int ShowInfo(const std::string &hmpStr)
164 {
165     printf("try to show module update info\n");
166     std::list<OHOS::SysInstaller::ModulePackageInfo> modulePackageInfos;
167     int ret = OHOS::SysInstaller::ModuleUpdateKits::GetInstance().GetModulePackageInfo(hmpStr, modulePackageInfos);
168     PrintUpgradeInfo(modulePackageInfos);
169     PrintErrMsg(GetFailReasonByErrCode(ret));
170     return ret;
171 }
172 
main(int argc,char ** argv)173 int main(int argc, char **argv)
174 {
175     if (!CheckParam(argc)) {
176         return RET_FAILED;
177     }
178 
179     int ret = 0;
180     OHOS::SysInstaller::ModuleUpdateKits& moduleUpdateKits = OHOS::SysInstaller::ModuleUpdateKits::GetInstance();
181     ret = moduleUpdateKits.InitModuleUpdate();
182 
183     ON_SCOPE_EXIT(exit) {
184         moduleUpdateKits.ExitModuleUpdate();
185     };
186 
187     if (ret != 0) {
188         PrintErrMsg(GetFailReasonByErrCode(ret));
189         return ret;
190     }
191 
192     if (INSTALL_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) {
193         printf("try to update a module\n");
194         ret = moduleUpdateKits.InstallModulePackage(argv[MIN_PARAM_NUM]);
195         PrintErrMsg(GetFailReasonByErrCode(ret));
196         return ret;
197     }
198     if (UNINSTALL_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) {
199         printf("try to uninstall an upgrade package\n");
200         ret = moduleUpdateKits.UninstallModulePackage(argv[MIN_PARAM_NUM]);
201         PrintErrMsg(GetFailReasonByErrCode(ret));
202         return ret;
203     }
204     if (UPDATE_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) {
205         return UpdateModulePackage(argv[MIN_PARAM_NUM]);
206     }
207     if (GET_HMP_VERSION.compare(argv[1]) == 0) {
208         return GetHmpVersion();
209     }
210     if (GET_RESULT.compare(argv[1]) == 0) {
211         return GetResult();
212     }
213     if (SHOW_INFO.compare(argv[1]) == 0) {
214         return ShowInfo((argc != MIN_PARAM_NUM) ? argv[MIN_PARAM_NUM] : "");
215     }
216 
217     printf("invalid command. \n");
218     printf("%s", HELP_MSG.c_str());
219     return ret;
220 }