140f5d65dSopenharmony_ci/* 240f5d65dSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 340f5d65dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 440f5d65dSopenharmony_ci * you may not use this file except in compliance with the License. 540f5d65dSopenharmony_ci * You may obtain a copy of the License at 640f5d65dSopenharmony_ci * 740f5d65dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 840f5d65dSopenharmony_ci * 940f5d65dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1040f5d65dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1140f5d65dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1240f5d65dSopenharmony_ci * See the License for the specific language governing permissions and 1340f5d65dSopenharmony_ci * limitations under the License. 1440f5d65dSopenharmony_ci */ 1540f5d65dSopenharmony_ci 1640f5d65dSopenharmony_ci#ifndef BACKUP_TOOL_ENABLE 1740f5d65dSopenharmony_ciint main() 1840f5d65dSopenharmony_ci{ 1940f5d65dSopenharmony_ci return 0; 2040f5d65dSopenharmony_ci} 2140f5d65dSopenharmony_ci#else 2240f5d65dSopenharmony_ci 2340f5d65dSopenharmony_ci#include "errors.h" 2440f5d65dSopenharmony_ci#include "tools_op.h" 2540f5d65dSopenharmony_ci#include "tools_op_backup.h" 2640f5d65dSopenharmony_ci#include "tools_op_check_sa.h" 2740f5d65dSopenharmony_ci#include "tools_op_help.h" 2840f5d65dSopenharmony_ci#include "tools_op_restore.h" 2940f5d65dSopenharmony_ci#include "tools_op_restore_async.h" 3040f5d65dSopenharmony_ci#include "tools_op_incremental_backup.h" 3140f5d65dSopenharmony_ci#include "tools_op_incremental_restore.h" 3240f5d65dSopenharmony_ci#include "tools_op_incremental_restore_async.h" 3340f5d65dSopenharmony_ci 3440f5d65dSopenharmony_ci#include <algorithm> 3540f5d65dSopenharmony_ci#include <cstddef> 3640f5d65dSopenharmony_ci#include <cstdio> 3740f5d65dSopenharmony_ci#include <getopt.h> 3840f5d65dSopenharmony_ci#include <iostream> 3940f5d65dSopenharmony_ci#include <optional> 4040f5d65dSopenharmony_ci#include <sstream> 4140f5d65dSopenharmony_ci 4240f5d65dSopenharmony_cinamespace OHOS::FileManagement::Backup { 4340f5d65dSopenharmony_ciusing namespace std; 4440f5d65dSopenharmony_ci 4540f5d65dSopenharmony_cioptional<map<string, vector<string>>> GetArgsMap(int argc, char *const argv[], const vector<ToolsOp::CmdInfo> &argList) 4640f5d65dSopenharmony_ci{ 4740f5d65dSopenharmony_ci int i = 0; 4840f5d65dSopenharmony_ci map<int, string> mapOptToName; 4940f5d65dSopenharmony_ci vector<struct option> vecLongOptions; 5040f5d65dSopenharmony_ci for (auto &&arg : argList) { 5140f5d65dSopenharmony_ci mapOptToName[i] = arg.paramName; 5240f5d65dSopenharmony_ci vecLongOptions.emplace_back(option { 5340f5d65dSopenharmony_ci .name = arg.paramName.c_str(), 5440f5d65dSopenharmony_ci .has_arg = required_argument, 5540f5d65dSopenharmony_ci .flag = nullptr, 5640f5d65dSopenharmony_ci .val = i++, 5740f5d65dSopenharmony_ci }); 5840f5d65dSopenharmony_ci } 5940f5d65dSopenharmony_ci vecLongOptions.emplace_back(option {nullptr, 0, nullptr, 0}); 6040f5d65dSopenharmony_ci 6140f5d65dSopenharmony_ci int opt = 0; 6240f5d65dSopenharmony_ci int options_index = 0; 6340f5d65dSopenharmony_ci map<string, vector<string>> mapArgToVals; 6440f5d65dSopenharmony_ci while ((opt = getopt_long(argc, argv, "", vecLongOptions.data(), &options_index)) != -1) { 6540f5d65dSopenharmony_ci if (opt == '?') { 6640f5d65dSopenharmony_ci // "我们匹配到了一个奇怪的命令 返回 nullopt,getopt_long 在opterr 未被赋值0时 会自动打印未被定义参数到终端" 6740f5d65dSopenharmony_ci return nullopt; 6840f5d65dSopenharmony_ci } 6940f5d65dSopenharmony_ci string argName = mapOptToName[opt]; 7040f5d65dSopenharmony_ci if (mapArgToVals.find(argName) != mapArgToVals.end() && argList[opt].repeatable == true) { 7140f5d65dSopenharmony_ci mapArgToVals[argName].emplace_back(optarg); 7240f5d65dSopenharmony_ci } else if (mapArgToVals.find(argName) != mapArgToVals.end()) { 7340f5d65dSopenharmony_ci fprintf(stderr, "%s can only be entered once, but you repeat it.\n", argName.c_str()); 7440f5d65dSopenharmony_ci return nullopt; 7540f5d65dSopenharmony_ci } else { 7640f5d65dSopenharmony_ci mapArgToVals.emplace(argName, vector<string> {optarg}); 7740f5d65dSopenharmony_ci } 7840f5d65dSopenharmony_ci } 7940f5d65dSopenharmony_ci return mapArgToVals; 8040f5d65dSopenharmony_ci} 8140f5d65dSopenharmony_ci 8240f5d65dSopenharmony_civoid ToolRegister() 8340f5d65dSopenharmony_ci{ 8440f5d65dSopenharmony_ci OHOS::FileManagement::Backup::BackUpRegister(); 8540f5d65dSopenharmony_ci OHOS::FileManagement::Backup::HelpRegister(); 8640f5d65dSopenharmony_ci OHOS::FileManagement::Backup::CheckSaRegister(); 8740f5d65dSopenharmony_ci OHOS::FileManagement::Backup::RestoreRegister(); 8840f5d65dSopenharmony_ci OHOS::FileManagement::Backup::RestoreAsyncRegister(); 8940f5d65dSopenharmony_ci OHOS::FileManagement::Backup::IncrementalBackUpRegister(); 9040f5d65dSopenharmony_ci OHOS::FileManagement::Backup::IncrementalRestoreRegister(); 9140f5d65dSopenharmony_ci OHOS::FileManagement::Backup::IncrementalRestoreAsyncRegister(); 9240f5d65dSopenharmony_ci} 9340f5d65dSopenharmony_ci 9440f5d65dSopenharmony_ciint ParseOpAndExecute(const int argc, char *const argv[]) 9540f5d65dSopenharmony_ci{ 9640f5d65dSopenharmony_ci // 注册下命令 9740f5d65dSopenharmony_ci ToolRegister(); 9840f5d65dSopenharmony_ci int flag = -1; 9940f5d65dSopenharmony_ci for (int i = 1; i < argc; i++) { 10040f5d65dSopenharmony_ci // 暂存 {argv[1]...argv[i]}; 10140f5d65dSopenharmony_ci vector<string_view> curOp; 10240f5d65dSopenharmony_ci for (int j = 1; j <= i; ++j) { 10340f5d65dSopenharmony_ci curOp.emplace_back(argv[j]); 10440f5d65dSopenharmony_ci } 10540f5d65dSopenharmony_ci 10640f5d65dSopenharmony_ci // 尝试匹配当前命令,成功后执行 10740f5d65dSopenharmony_ci auto tryOpSucceed = [&curOp](const ToolsOp &op) { return op.TryMatch(curOp); }; 10840f5d65dSopenharmony_ci auto &&opeartions = ToolsOp::GetAllOperations(); 10940f5d65dSopenharmony_ci auto matchedOp = find_if(opeartions.begin(), opeartions.end(), tryOpSucceed); 11040f5d65dSopenharmony_ci if (matchedOp != opeartions.end()) { 11140f5d65dSopenharmony_ci vector<ToolsOp::CmdInfo> argList = matchedOp->GetParams(); 11240f5d65dSopenharmony_ci optional<map<string, vector<string>>> mapNameToArgs = GetArgsMap(argc, argv, argList); 11340f5d65dSopenharmony_ci if (mapNameToArgs.has_value()) { 11440f5d65dSopenharmony_ci flag = matchedOp->Execute(mapNameToArgs.value()); 11540f5d65dSopenharmony_ci } 11640f5d65dSopenharmony_ci } 11740f5d65dSopenharmony_ci } 11840f5d65dSopenharmony_ci if (flag != 0) { 11940f5d65dSopenharmony_ci printf("backup_tool: missing operand\nTry 'backup_tool help' for more information.\n"); 12040f5d65dSopenharmony_ci } 12140f5d65dSopenharmony_ci return flag; 12240f5d65dSopenharmony_ci} 12340f5d65dSopenharmony_ci} // namespace OHOS::FileManagement::Backup 12440f5d65dSopenharmony_ci 12540f5d65dSopenharmony_ciint main(int argc, char *const argv[]) 12640f5d65dSopenharmony_ci{ 12740f5d65dSopenharmony_ci return OHOS::FileManagement::Backup::ParseOpAndExecute(argc, argv); 12840f5d65dSopenharmony_ci} 12940f5d65dSopenharmony_ci 13040f5d65dSopenharmony_ci#endif