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 #include "b_resources/b_constants.h"
17 #include "tools_op.h"
18 
19 #include <regex>
20 #include <sstream>
21 
22 namespace OHOS::FileManagement::Backup {
23 using namespace std;
24 
GetName() const25 const std::string ToolsOp::GetName() const
26 {
27     std::stringstream ss;
28 
29     auto &&allSubOps = desc_.opName;
30     for (size_t j = 0; j < allSubOps.size(); ++j) {
31         ss << allSubOps[j];
32         if (j != allSubOps.size() - 1) {
33             ss << ' ';
34         }
35     }
36 
37     return ss.str();
38 }
39 
Register(ToolsOp && op)40 bool ToolsOp::Register(ToolsOp &&op)
41 {
42     auto &&opName = op.GetDescriptor().opName;
43     auto isIncorrect = [&opName](const std::string_view &subOp) {
44         std::vector<std::string> patterns {
45             "\\W", // 匹配任意不是字母、数字、下划线的字符
46             "^$",  // 匹配空串
47         };
48 
49         for (auto subOp : opName) {
50             for (auto pattern : patterns) {
51                 std::regex re(pattern);
52                 if (std::regex_search(string(subOp), re)) {
53                     fprintf(stderr, "Sub-op '%s' failed to pass regex '%s'\n", subOp.data(), pattern.c_str());
54                     return true;
55                 }
56             }
57         }
58 
59         return false;
60     };
61     if (std::any_of(opName.begin(), opName.end(), isIncorrect)) {
62         fprintf(stderr, "Failed to register an illegal operation '%s'\n", op.GetName().c_str());
63         return false;
64     }
65 
66     ToolsOp::opsAvailable_.emplace_back(std::move(op));
67 
68     // sort with ascending order
69     std::sort(opsAvailable_.begin(), opsAvailable_.end(), [](const ToolsOp &lop, const ToolsOp &rop) {
70         return lop.desc_.opName < rop.desc_.opName;
71     });
72     return true;
73 }
74 
TryMatch(CRefVStrView op) const75 bool ToolsOp::TryMatch(CRefVStrView op) const
76 {
77     return op == desc_.opName;
78 }
79 
Execute(map<string,vector<string>> args) const80 int ToolsOp::Execute(map<string, vector<string>> args) const
81 {
82     if (!desc_.funcExec) {
83         fprintf(stderr, "Incomplete operation: executor is missing\n");
84         return -EPERM;
85     }
86     return desc_.funcExec(args);
87 }
88 
GetFIleNums(const std::string & bundleName,bool isWholeRestore)89 int ToolsOp::GetFIleNums(const std::string &bundleName, bool isWholeRestore)
90 {
91     std::string path = "";
92     path = string(isWholeRestore? BConstants::BACKUP_TOOL_RECEIVE_DIR :
93         BConstants::BACKUP_TOOL_INCREMENTAL_RECEIVE_DIR) + bundleName;
94     struct dirent *entry;
95     printf("bundle path =  %s\n", path.c_str());
96     DIR *dir = opendir(path.c_str());
97     if (dir == nullptr) {
98         fprintf(stderr, "Open path error %s\n", path.c_str());
99         return DEFAULT_ERR_NUMBER;
100     }
101     int num = 0;
102     while ((entry = readdir(dir)) != nullptr) {
103         if (entry->d_type != DT_DIR || (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)) {
104             ++num;
105         } else {
106             continue;
107         }
108     }
109     if (num == 0) {
110         fprintf(stderr, "The path dir is empty!\n");
111     }
112     closedir(dir);
113     return num;
114 }
115 } // namespace OHOS::FileManagement::Backup
116