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 "module_update_kits.h"
18 #include "module_error_code.h"
19
20 static const int32_t MIN_PARAM_NUM = 2;
21 static const int32_t MAX_PARAM_NUM = 3;
22
23 static const std::string HELP_MSG =
24 "usage: module_update_tool\n"
25 "example: ./module_update_tool install /data/tmp/xxx.hmp \n"
26 "command list:\n"
27 " install : upgrade some SA via hmp package\n"
28 " uninstall : degrade some SA via hmp name\n"
29 " show hmpname : show upgrade sa info, if hmp name is null, show all\n";
30
31 static const std::string INSTALL_PARAM = "install";
32 static const std::string UNINSTALL_PARAM = "uninstall";
33 static const std::string SHOW_INFO = "show";
34 static const int32_t RET_FAILED = -1;
35
CheckParam(int argc)36 static bool CheckParam(int argc)
37 {
38 if (argc < MIN_PARAM_NUM || argc > MAX_PARAM_NUM) {
39 printf("Invalid module update command\n");
40 printf("%s", HELP_MSG.c_str());
41 return false;
42 }
43 return true;
44 }
45
GetFailReasonByErrCode(int32_t err)46 static std::string GetFailReasonByErrCode(int32_t err)
47 {
48 switch (err) {
49 case 0:
50 return "success";
51 case OHOS::SysInstaller::ERR_SERVICE_NOT_FOUND:
52 return "ERR_SERVICE_NOT_FOUND";
53 case OHOS::SysInstaller::ERR_INVALID_PATH:
54 return "ERR_INVALID_PATH";
55 case OHOS::SysInstaller::ERR_LOWER_VERSION:
56 return "ERR_LOWER_VERSION";
57 case OHOS::SysInstaller::ERR_VERIFY_SIGN_FAIL:
58 return "ERR_VERIFY_SIGN_FAIL";
59 case OHOS::SysInstaller::ERR_INSTALL_FAIL:
60 return "ERR_INSTALL_FAIL";
61 case OHOS::SysInstaller::ERR_UNINSTALL_FAIL:
62 return "ERR_UNINSTALL_FAIL";
63 case OHOS::SysInstaller::ERR_REPORT_STATUS_FAIL:
64 return "ERR_REPORT_STATUS_FAIL";
65 default:
66 return "Unknown Error";
67 }
68 }
69
PrintErrMsg(const std::string & errMsg)70 static void PrintErrMsg(const std::string &errMsg)
71 {
72 printf("%s\n", errMsg.c_str());
73 }
74
PrintUpgradeInfo(std::list<OHOS::SysInstaller::ModulePackageInfo> & modulePackageInfos)75 static void PrintUpgradeInfo(std::list<OHOS::SysInstaller::ModulePackageInfo> &modulePackageInfos)
76 {
77 std::list<OHOS::SysInstaller::ModulePackageInfo>::iterator it;
78 printf("Got %zu upgraded modules info\n", modulePackageInfos.size());
79 for (it = modulePackageInfos.begin(); it != modulePackageInfos.end(); it++) {
80 OHOS::SysInstaller::ModulePackageInfo moduleInfo = *it;
81 printf("%s\n", moduleInfo.hmpName.c_str());
82 std::list<OHOS::SysInstaller::SaInfo>::iterator saIt;
83 for (saIt = moduleInfo.saInfoList.begin(); saIt != moduleInfo.saInfoList.end(); saIt++) {
84 std::string verStr = (*saIt).version;
85 printf(" {saName:%s saId:%d version:%s}\n", (*saIt).saName.c_str(), (*saIt).saId, verStr.c_str());
86 }
87 printf(" \n");
88 }
89 }
90
main(int argc,char ** argv)91 int main(int argc, char **argv)
92 {
93 if (!CheckParam(argc)) {
94 return RET_FAILED;
95 }
96
97 int ret = 0;
98 OHOS::SysInstaller::ModuleUpdateKits& moduleUpdateKits = OHOS::SysInstaller::ModuleUpdateKits::GetInstance();
99 ret = moduleUpdateKits.InitModuleUpdate();
100 if (ret != 0) {
101 PrintErrMsg(GetFailReasonByErrCode(ret));
102 return ret;
103 }
104
105 if (INSTALL_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) {
106 printf("try to update a module\n");
107 ret = moduleUpdateKits.InstallModulePackage(argv[MIN_PARAM_NUM]);
108 PrintErrMsg(GetFailReasonByErrCode(ret));
109 return ret;
110 }
111 if (UNINSTALL_PARAM.compare(argv[1]) == 0 && argc == MAX_PARAM_NUM) {
112 printf("try to uninstall an upgrade package\n");
113 ret = moduleUpdateKits.UninstallModulePackage(argv[MIN_PARAM_NUM]);
114 PrintErrMsg(GetFailReasonByErrCode(ret));
115 return ret;
116 }
117 if (SHOW_INFO.compare(argv[1]) == 0) {
118 printf("try to show module update info\n");
119 std::list<OHOS::SysInstaller::ModulePackageInfo> modulePackageInfos;
120 std::string hmpStr;
121 if (argc != MIN_PARAM_NUM) {
122 hmpStr = argv[MIN_PARAM_NUM];
123 } else {
124 hmpStr = "";
125 }
126 ret = moduleUpdateKits.GetModulePackageInfo(hmpStr, modulePackageInfos);
127 PrintUpgradeInfo(modulePackageInfos);
128 PrintErrMsg(GetFailReasonByErrCode(ret));
129 return ret;
130 }
131
132 printf("invalid command. \n");
133 printf("%s", HELP_MSG.c_str());
134 return ret;
135 }