1 /* 2 * Copyright (c) 2022-2024 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 OHOS_FILEMGMT_BACKUP_TOOLS_OP_H 17 #define OHOS_FILEMGMT_BACKUP_TOOLS_OP_H 18 19 #include <dirent.h> 20 #include <functional> 21 #include <map> 22 #include <string> 23 #include <string_view> 24 #include <vector> 25 26 namespace OHOS::FileManagement::Backup { 27 const int DEFAULT_ERR_NUMBER = -1; 28 class ToolsOp { 29 public: 30 using CRefVStrView = const std::vector<std::string_view> &; 31 struct CmdInfo { 32 std::string paramName; 33 bool repeatable = false; 34 }; 35 36 struct Descriptor { 37 // 命令名,必填 38 std::vector<std::string_view> opName; 39 // 参数,选填 40 std::vector<CmdInfo> argList; 41 // 命令帮助语句,选填 42 std::function<std::string()> funcGenHelpMsg; 43 // 命令执行主体,必填 44 std::function<int(std::map<std::string, std::vector<std::string>> &args)> funcExec; 45 }; 46 47 /** 48 * @brief 构造一个操作 49 * 50 * @param desc 操作具体信息 51 */ ToolsOp(Descriptor && desc)52 explicit ToolsOp(Descriptor &&desc) : desc_(std::move(desc)) {} 53 54 /** 55 * @brief 获取当前操作的名称。操作由多条字符串构成时,之间由空格隔开 56 * 57 * @return const std::string 当前操作的名称 58 */ 59 const std::string GetName() const; 60 61 /** 62 * @brief 获取当前操作的参数 63 * 64 * @return std::vector<CmdInfo> 当前参数的向量 65 */ GetParams()66 const std::vector<CmdInfo> GetParams() const 67 { 68 return desc_.argList; 69 } 70 71 /** 72 * @brief 获取当前操作的原始具体信息 73 * 74 * @return const Descriptor& 当前操作的原始具体信息 75 */ GetDescriptor()76 const Descriptor &GetDescriptor() const 77 { 78 return desc_; 79 } 80 81 /** 82 * @brief 获取所有操作 83 * 84 * @return const std::vector<ToolsOp>& 所有操作 85 */ GetAllOperations()86 static const std::vector<ToolsOp> &GetAllOperations() 87 { 88 return ToolsOp::opsAvailable_; 89 } 90 91 /** 92 * @brief 注册一个操作 93 * 94 * @param op 操作 95 * @return true 注册成功 96 * @return false 注册失败 97 */ 98 static bool Register(ToolsOp &&op); 99 100 /** 101 * @brief 将当前操作与主函数给定的操作相匹配(大小写敏感) 102 * 103 * @param op 给定操作 104 * @return true 匹配成功 105 * @return false 匹配失败 106 */ 107 bool TryMatch(CRefVStrView op) const; 108 109 /** 110 * @brief 使用主函数给定的参数表执行当前操作 111 * 112 * @param args 给定参数表 113 * @return int 错误码(0 表示成功,非零表示失败) 114 */ 115 int Execute(std::map<std::string, std::vector<std::string>> mapArg) const; 116 117 /** 118 * @brief 获取dir下文件个数,用于publishfile触发时机 119 * 120 * @return int 错误码(0 表示成功,非零表示失败) 121 */ 122 static int GetFIleNums(const std::string &bundleName, bool isWholeRestore = true); 123 private: 124 Descriptor desc_; 125 static inline std::vector<ToolsOp> opsAvailable_; 126 }; 127 } // namespace OHOS::FileManagement::Backup 128 129 #endif // OHOS_FILEMGMT_BACKUP_TOOLS_OP_H