• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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  *     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 BACKUP_TOOL_ENABLE
main()17 int main()
18 {
19     return 0;
20 }
21 #else
22 
23 #include "errors.h"
24 #include "tools_op.h"
25 #include "tools_op_backup.h"
26 #include "tools_op_check_sa.h"
27 #include "tools_op_help.h"
28 #include "tools_op_restore.h"
29 #include "tools_op_restore_async.h"
30 
31 #include <algorithm>
32 #include <cstddef>
33 #include <cstdio>
34 #include <getopt.h>
35 #include <iostream>
36 #include <optional>
37 #include <sstream>
38 
39 namespace OHOS::FileManagement::Backup {
40 using namespace std;
41 
GetArgsMap(int argc,char * const argv[],const vector<ToolsOp::CmdInfo> & argList)42 optional<map<string, vector<string>>> GetArgsMap(int argc, char *const argv[], const vector<ToolsOp::CmdInfo> &argList)
43 {
44     int i = 0;
45     map<int, string> mapOptToName;
46     vector<struct option> vecLongOptions;
47     for (auto &&arg : argList) {
48         mapOptToName[i] = arg.paramName;
49         vecLongOptions.emplace_back(option {
50             .name = arg.paramName.c_str(),
51             .has_arg = required_argument,
52             .flag = nullptr,
53             .val = i++,
54         });
55     }
56     vecLongOptions.emplace_back(option {nullptr, 0, nullptr, 0});
57 
58     int opt = 0;
59     int options_index = 0;
60     map<string, vector<string>> mapArgToVals;
61     while ((opt = getopt_long(argc, argv, "", vecLongOptions.data(), &options_index)) != -1) {
62         if (opt == '?') {
63             // "我们匹配到了一个奇怪的命令 返回 nullopt,getopt_long 在opterr 未被赋值0时 会自动打印未被定义参数到终端"
64             return nullopt;
65         }
66         string argName = mapOptToName[opt];
67         if (mapArgToVals.find(argName) != mapArgToVals.end() && argList[opt].repeatable == true) {
68             mapArgToVals[argName].emplace_back(optarg);
69         } else if (mapArgToVals.find(argName) != mapArgToVals.end()) {
70             fprintf(stderr, "%s can only be entered once, but you repeat it.\n", argName.c_str());
71             return nullopt;
72         } else {
73             mapArgToVals.emplace(argName, vector<string> {optarg});
74         }
75     }
76     return mapArgToVals;
77 }
78 
ToolRegister()79 void ToolRegister()
80 {
81     OHOS::FileManagement::Backup::BackUpRegister();
82     OHOS::FileManagement::Backup::HelpRegister();
83     OHOS::FileManagement::Backup::CheckSaRegister();
84     OHOS::FileManagement::Backup::RestoreRegister();
85     OHOS::FileManagement::Backup::RestoreAsyncRegister();
86 }
87 
ParseOpAndExecute(const int argc,char * const argv[])88 int ParseOpAndExecute(const int argc, char *const argv[])
89 {
90     // 注册下命令
91     ToolRegister();
92     int flag = -1;
93     for (int i = 1; i < argc; i++) {
94         // 暂存 {argv[1]...argv[i]};
95         vector<string_view> curOp;
96         for (int j = 1; j <= i; ++j) {
97             curOp.emplace_back(argv[j]);
98         }
99 
100         // 尝试匹配当前命令,成功后执行
101         auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); };
102         auto &&opeartions = ToolsOp::GetAllOperations();
103         auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed);
104         if (matchedOp != opeartions.end()) {
105             vector<ToolsOp::CmdInfo> argList = matchedOp->GetParams();
106             optional<map<string, vector<string>>> mapNameToArgs = GetArgsMap(argc, argv, argList);
107             if (mapNameToArgs.has_value()) {
108                 flag = matchedOp->Execute(mapNameToArgs.value());
109             }
110         }
111     }
112     if (flag != 0) {
113         printf("backup_tool: missing operand\nTry 'backup_tool help' for more information.\n");
114     }
115     return flag;
116 }
117 } // namespace OHOS::FileManagement::Backup
118 
main(int argc,char * const argv[])119 int main(int argc, char *const argv[])
120 {
121     return OHOS::FileManagement::Backup::ParseOpAndExecute(argc, argv);
122 }
123 
124 #endif