diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 447cde28b8b02f1a7eff64ca4b5d276661f5de9c..0cfc16d259be34afb43dd72ce5239425fcac044c 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -134,6 +134,8 @@ static inline std::string BACKUP_DIR_PRE = "/data/storage/"; static inline std::string CONTEXT_ELS[] = {"el1", "el2"}; static inline std::string BACKUP_DIR_END = "/base/.backup/"; static inline std::string BUNDLE_BASE_DIR = "/data/storage/el2/base"; +static inline std::string PATH_PUBLIC_HOME = "/storage/Users/currentUser/"; +static inline std::string PATH_APP_DATA = "appdata"; // 文管bundleName static inline std::string BUNDLE_FILE_MANAGER = "hmos.filemanager"; diff --git a/utils/src/b_filesystem/b_dir.cpp b/utils/src/b_filesystem/b_dir.cpp index dca6c15af37ad06ba620be93b4b906348f44bbbd..9a462c0984e57cd6669e70cc15b28d7691a865cc 100644 --- a/utils/src/b_filesystem/b_dir.cpp +++ b/utils/src/b_filesystem/b_dir.cpp @@ -37,6 +37,8 @@ namespace OHOS::FileManagement::Backup { using namespace std; const int32_t PATH_MAX_LEN = 4096; +const std::string APP_DATA_DIR = BConstants::PATH_PUBLIC_HOME + + BConstants::PATH_APP_DATA + BConstants::FILE_SEPARATOR_CHAR; static bool IsEmptyDirectory(const string &path) { @@ -169,6 +171,57 @@ tuple> BDir::GetDirFiles(const string &path) return {ERR_OK, files}; } +static std::set GetSubDir(const std::string &path) +{ + if (path.empty()) { + return {}; + } + std::set result; + unique_ptr> dir = {opendir(path.c_str()), closedir}; + if (!dir) { + HILOGE("Invalid directory path: %{private}s", path.c_str()); + return {}; + } + + struct dirent *ptr = nullptr; + while (!!(ptr = readdir(dir.get()))) { + // current dir OR parent dir + if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) { + continue; + } else if (ptr->d_type == DT_DIR) { + std::string tmpPath = IncludeTrailingPathDelimiter(path) + + string(ptr->d_name) + BConstants::FILE_SEPARATOR_CHAR; + if (tmpPath == APP_DATA_DIR) { + HILOGI("Filter appdata successfully"); + continue; + } + result.emplace(tmpPath); + } else { + result.emplace(IncludeTrailingPathDelimiter(path) + string(ptr->d_name)); + } + } + return result; +} + +static void RmForceExcludePath(set &expandPath) +{ + set addPaths; + for (auto it = expandPath.begin(); it != expandPath.end();) { + if (*it == BConstants::PATH_PUBLIC_HOME) { + addPaths = GetSubDir(*it); + } + if ((*it).find(APP_DATA_DIR) == 0) { + it = expandPath.erase(it); + continue; + } + ++it; + } + if (!addPaths.empty()) { + expandPath.erase(BConstants::PATH_PUBLIC_HOME); + expandPath.merge(addPaths); + } +} + static set ExpandPathWildcard(const vector &vec, bool onlyPath) { unique_ptr> gl {new glob_t, [](glob_t *ptr) { globfree(ptr); }}; @@ -184,9 +237,14 @@ static set ExpandPathWildcard(const vector &vec, bool onlyPath) set expandPath, filteredPath; for (size_t i = 0; i < gl->gl_pathc; ++i) { - expandPath.emplace(gl->gl_pathv[i]); + std::string tmpPath = gl->gl_pathv[i]; + auto pos = tmpPath.find(BConstants::FILE_SEPARATOR_CHAR); + if (pos != 0 && pos != std::string::npos) { + tmpPath = BConstants::FILE_SEPARATOR_CHAR + tmpPath; + } + expandPath.emplace(tmpPath); } - + RmForceExcludePath(expandPath); for (auto it = expandPath.begin(); it != expandPath.end(); ++it) { filteredPath.insert(*it); if (onlyPath && *it->rbegin() != BConstants::FILE_SEPARATOR_CHAR) {