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 using EndFileInfo = std::map<std::string, off_t>; 34 class BDir { 35 public: 36 /** 37 * @brief 读取指定目录下所有文件(非递归) 38 * 39 * @param 目录 40 * @return 错误码、文件名数组 41 */ 42 static std::tuple<ErrCode, std::vector<std::string>> GetDirFiles(const std::string &path); 43 44 /** 45 * @brief 从给定的includes和excludes目录及文件中获取所有有用大文件和其链接文件的集合 46 * 47 * @param includes 需要包含的文件及目录集合 48 * @param excludes 需要排除的文件及目录集合 49 * @return 错误码、大文件名集合 50 */ 51 static std::tuple<ErrCode, std::map<std::string, struct stat>, std::map<std::string, size_t>> GetBigFiles( 52 const std::vector<std::string> &includes, const std::vector<std::string> &excludes); 53 54 /** 55 * @brief Get the Dirs object 56 * 57 * @param paths 目录集合可带有通配符路径 58 * @return std::vector<std::string> 目录集合 59 */ 60 static std::vector<std::string> GetDirs(const std::vector<std::string_view> &paths); 61 62 /** 63 * @brief 从给定的includes和excludes目录中获取所有的大文件和小文件 64 * 65 * @param includes 需要包含的文件及目录集合 66 * @param excludes 需要排除的文件及目录集合 67 * @return 大文件和小文件的集合 68 */ 69 static std::tuple<std::vector<std::string>, std::vector<std::string>> GetBackupList( 70 const std::vector<std::string> &includes, const std::vector<std::string> &excludes); 71 72 /** 73 * @brief 获取bigfile和smaillfile的文件信息并生成清单 74 * 75 * @param bigFile 需要包含的文件及目录集合 76 * @param smallFile 需要排除的文件及目录集合 77 * @param allFiles 生成的所有文件信息清单 78 * @param smallFiles 生成的小文件信息清单 79 * @param bigFiles 生成的大文件信息清单 80 * @return 81 */ 82 static void GetUser0FileStat(std::vector<std::string> bigFile, 83 std::vector<std::string> smallFile, 84 std::vector<struct ReportFileInfo> &allFiles, 85 std::vector<struct ReportFileInfo> &smallFiles, 86 std::vector<struct ReportFileInfo> &bigFiles); 87 88 /** 89 * @brief 核实文件是否为异常无效路径 90 * 91 * @param filePath 待核实的路径 92 * @return 是否是异常无效路径 93 */ 94 static bool CheckFilePathInvalid(const std::string &filePath); 95 96 /** 97 * @brief 核实文件是否存在软链接并删除 98 * 99 * @param filePath 待核实的路径 100 * @return 是否存在软连接 101 */ 102 static bool CheckAndRmSoftLink(const std::string &filePath); 103 104 /** 105 * @brief 核实文件是否存在软链接并删除 106 * 107 * @param filePaths 待核实的路径集合 108 * @return 是否存在软连接 109 */ 110 static bool CheckAndRmSoftLink(const EndFileInfo &filePaths); 111 }; 112 } // namespace OHOS::FileManagement::Backup 113 114 #endif // OHOS_FILEMGMT_BACKUP_B_DIR_H