From bda27e479b9be7bd0ab2de5ece06ac3c033b045e Mon Sep 17 00:00:00 2001 From: hunili Date: Tue, 5 Nov 2024 14:58:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=87=E6=BB=A4appdata(5.0.1release)=20issue?= =?UTF-8?q?:=20https://gitee.com/openharmony/filemanagement=5Fapp=5Ffile?= =?UTF-8?q?=5Fservice/issues/IB270D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hunili --- utils/include/b_resources/b_constants.h | 2 + utils/src/b_filesystem/b_dir.cpp | 62 ++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 447cde28b..0cfc16d25 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 32934a274..5fb354b9e 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) { -- Gitee