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