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 OHOS_FILEMGMT_BACKUP_B_DIR_H
17 #define OHOS_FILEMGMT_BACKUP_B_DIR_H
18 
19 #include <linux/stat.h>
20 #include <map>
21 #include <string>
22 #include <string_view>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <tuple>
26 #include <unistd.h>
27 #include <vector>
28 
29 #include "b_json/b_report_entity.h"
30 #include "errors.h"
31 
32 namespace OHOS::FileManagement::Backup {
33 class BDir {
34 public:
35     /**
36      * @brief 读取指定目录下所有文件(非递归)
37      *
38      * @param 目录
39      * @return 错误码、文件名数组
40      */
41     static std::tuple<ErrCode, std::vector<std::string>> GetDirFiles(const std::string &path);
42 
43     /**
44      * @brief 从给定的includes和excludes目录及文件中获取所有有用大文件和其链接文件的集合
45      *
46      * @param includes 需要包含的文件及目录集合
47      * @param excludes 需要排除的文件及目录集合
48      * @return 错误码、大文件名集合
49      */
50     static std::tuple<ErrCode, std::map<std::string, struct stat>, std::map<std::string, size_t>> GetBigFiles(
51         const std::vector<std::string> &includes, const std::vector<std::string> &excludes);
52 
53     /**
54      * @brief Get the Dirs object
55      *
56      * @param paths 目录集合可带有通配符路径
57      * @return std::vector<std::string> 目录集合
58      */
59     static std::vector<std::string> GetDirs(const std::vector<std::string_view> &paths);
60 
61     /**
62      * @brief 从给定的includes和excludes目录中获取所有的大文件和小文件
63      *
64      * @param includes 需要包含的文件及目录集合
65      * @param excludes 需要排除的文件及目录集合
66      * @return 大文件和小文件的集合
67      */
68     static std::tuple<std::vector<std::string>, std::vector<std::string>> GetBackupList(
69         const std::vector<std::string> &includes, const std::vector<std::string> &excludes);
70 
71     /**
72      * @brief 获取bigfile和smaillfile的文件信息并生成清单
73      *
74      * @param bigFile 需要包含的文件及目录集合
75      * @param smallFile 需要排除的文件及目录集合
76      * @param allFiles 生成的所有文件信息清单
77      * @param smallFiles 生成的小文件信息清单
78      * @param bigFiles 生成的大文件信息清单
79      * @return
80      */
81     static void GetUser0FileStat(std::vector<std::string> bigFile,
82                                  std::vector<std::string> smallFile,
83                                  std::vector<struct ReportFileInfo> &allFiles,
84                                  std::vector<struct ReportFileInfo> &smallFiles,
85                                  std::vector<struct ReportFileInfo> &bigFiles);
86 
87     /**
88      * @brief 核实文件是否为异常无效路径
89      *
90      * @param filePath 待核实的路径
91      * @return 是否是异常无效路径
92      */
93     static bool CheckFilePathInvalid(const std::string &filePath);
94 };
95 } // namespace OHOS::FileManagement::Backup
96 
97 #endif // OHOS_FILEMGMT_BACKUP_B_DIR_H