diff --git a/frameworks/native/backup_ext/src/tar_file.cpp b/frameworks/native/backup_ext/src/tar_file.cpp index fb4a1e962470f675f56369a154f23e3474922933..a8aaa6977b5d6069afa3faa0973718ad62a281b3 100644 --- a/frameworks/native/backup_ext/src/tar_file.cpp +++ b/frameworks/native/backup_ext/src/tar_file.cpp @@ -49,7 +49,7 @@ TarFile &TarFile::GetInstance() bool TarFile::Packet(const vector &srcFiles, const string &tarFileName, const string &pkPath, TarMap &tarMap) { - if (srcFiles.empty() || tarFileName.empty() || pkPath.empty()) { + if (tarFileName.empty() || pkPath.empty()) { HILOGE("Invalid parameter"); return false; } diff --git a/utils/include/b_resources/b_constants.h b/utils/include/b_resources/b_constants.h index 9f4ae877b689f5fc4318fb3a35a792b5c087b3fd..da3ab5d0d7147f00f64da49fe73961b3c91880cb 100644 --- a/utils/include/b_resources/b_constants.h +++ b/utils/include/b_resources/b_constants.h @@ -108,7 +108,7 @@ static inline std::string GetSaBundleBackupToolDir(int32_t userId) } // 备份恢复配置文件暂存路径 -static inline std::string_view BACKUP_CONFIG_EXTENSION_PATH = "/data/storage/el2/base/temp/"; +static inline std::string_view BACKUP_CONFIG_EXTENSION_PATH = "/data/storage/el2/base/cache/"; // 应用备份恢复所需的索引文件 static inline std::string_view EXT_BACKUP_MANAGE = "manage.json"; diff --git a/utils/src/b_filesystem/b_dir.cpp b/utils/src/b_filesystem/b_dir.cpp index 1154b0727e4bea6d604da428431c2454f432e4e3..c8bee3f25b50d83fe94128c19808dc3caf587b8f 100644 --- a/utils/src/b_filesystem/b_dir.cpp +++ b/utils/src/b_filesystem/b_dir.cpp @@ -53,6 +53,25 @@ static bool IsEmptyDirectory(const string &path) return isEmpty; } +static tuple, vector> GetFile(const string &path, off_t size = -1) +{ + map files; + vector smallFiles; + struct stat sta = {}; + if (stat(path.data(), &sta) == -1) { + return {BError(errno).GetCode(), files, smallFiles}; + } + if (path == "/") { + return {BError(BError::Codes::OK).GetCode(), files, smallFiles}; + } + if (sta.st_size <= size) { + smallFiles.emplace_back(path); + } else { + files.try_emplace(path, sta); + } + return {BError(BError::Codes::OK).GetCode(), files, smallFiles}; +} + static tuple, vector> GetDirFilesDetail(const string &path, bool recursion, off_t size = -1) @@ -61,16 +80,19 @@ static tuple, vector> GetDirFilesDetai vector smallFiles; if (IsEmptyDirectory(path)) { - smallFiles.emplace_back(path); + string newPath = path; + if (path.at(path.size()-1) != '/') { + newPath += '/'; + } + smallFiles.emplace_back(newPath); return {ERR_OK, files, smallFiles}; } unique_ptr> dir = {opendir(path.c_str()), closedir}; if (!dir) { HILOGE("Invalid directory path: %{private}s", path.c_str()); - return {BError(errno).GetCode(), files, smallFiles}; + return GetFile(path, size); } - struct dirent *ptr = nullptr; while (!!(ptr = readdir(dir.get()))) { // current dir OR parent dir @@ -104,7 +126,6 @@ static tuple, vector> GetDirFilesDetai files.try_emplace(fileName, sta); } } - return {ERR_OK, files, smallFiles}; } @@ -132,7 +153,7 @@ tuple> BDir::GetDirFiles(const string &path) return {ERR_OK, files}; } -static set ExpandPathWildcard(const vector &vec) +static set ExpandPathWildcard(const vector &vec, bool onlyPath) { unique_ptr> gl {new glob_t, [](glob_t *ptr) { globfree(ptr); }}; *gl = {}; @@ -152,7 +173,7 @@ static set ExpandPathWildcard(const vector &vec) for (auto it = expandPath.begin(); it != expandPath.end(); ++it) { filteredPath.insert(*it); - if (*it->rbegin() != '/') { + if (onlyPath && *it->rbegin() != '/') { continue; } auto jt = it; @@ -168,18 +189,16 @@ static set ExpandPathWildcard(const vector &vec) tuple, vector> BDir::GetBigFiles(const vector &includes, const vector &excludes) { - set inc = ExpandPathWildcard(includes); + set inc = ExpandPathWildcard(includes, false); map incFiles; vector incSmallFiles; for (const auto &item : inc) { - auto [errCode, files, smallFiles] = - OHOS::FileManagement::Backup::GetDirFilesDetail(item, true, BConstants::BIG_FILE_BOUNDARY); + auto [errCode, files, smallFiles] = GetDirFilesDetail(item, true, BConstants::BIG_FILE_BOUNDARY); if (errCode == 0) { int32_t num = static_cast(files.size()); - HILOGI("found big files. total number is : %{public}d", num); incFiles.merge(move(files)); - HILOGI("found small files. total number is : %{public}d", static_cast(smallFiles.size())); + HILOGI("big files: %{public}d; small files: %{public}d", num, static_cast(smallFiles.size())); incSmallFiles.insert(incSmallFiles.end(), smallFiles.begin(), smallFiles.end()); } } @@ -189,14 +208,28 @@ tuple, vector> BDir::GetBigFiles(const return false; } for (const string &item : s) { - if (!item.empty() && (fnmatch(item.data(), str.data(), FNM_LEADING_DIR) == 0)) { - HILOGI("file %{public}s matchs exclude condition", str.c_str()); + if (item.empty()) { + continue; + } + string excludeItem = item; + if (excludeItem.at(item.size() - 1) == '/') { + excludeItem += "*"; + } + if (!item.empty() && fnmatch(excludeItem.data(), str.data(), FNM_LEADING_DIR) == 0) { return true; } } return false; }; + vector resSmallFiles; + for (const auto &item : incSmallFiles) { + if (!isMatch(excludes, item)) { + HILOGI("smallfile %{public}s matchs include condition and unmatchs exclude condition", item.c_str()); + resSmallFiles.emplace_back(item); + } + } + map bigFiles; for (const auto &item : incFiles) { if (!isMatch(excludes, item.first)) { @@ -205,14 +238,14 @@ tuple, vector> BDir::GetBigFiles(const } } HILOGI("total number of big files is %{public}d", static_cast(bigFiles.size())); - HILOGI("total number of small files is %{public}d", static_cast(incSmallFiles.size())); - return {ERR_OK, move(bigFiles), move(incSmallFiles)}; + HILOGI("total number of small files is %{public}d", static_cast(resSmallFiles.size())); + return {ERR_OK, move(bigFiles), move(resSmallFiles)}; } vector BDir::GetDirs(const vector &paths) { vector wildcardPath(paths.begin(), paths.end()); - set inc = ExpandPathWildcard(wildcardPath); + set inc = ExpandPathWildcard(wildcardPath, true); vector dirs(inc.begin(), inc.end()); return dirs; } diff --git a/utils/src/b_json/b_json_entity_extension_config.cpp b/utils/src/b_json/b_json_entity_extension_config.cpp index 4c9a845e8b2007e35e9d1413315eb6920ff284d3..d3f907ee54628a0e7b3864d3355d101d5fbdca61 100644 --- a/utils/src/b_json/b_json_entity_extension_config.cpp +++ b/utils/src/b_json/b_json_entity_extension_config.cpp @@ -47,7 +47,7 @@ vector BJsonEntityExtensionConfig::GetIncludes() const vector dirs; for (auto &&item : obj_["includes"]) { - if (!item.isString()) { + if (!item.isString() || item.empty()) { HILOGE("Each item of array 'includes' must be of the type string"); continue; } @@ -77,9 +77,9 @@ vector BJsonEntityExtensionConfig::GetExcludes() const vector dirs; for (auto &&item : obj_["excludes"]) { - if (!item.isString()) { + if (!item.isString() || item.empty()) { HILOGE("Each item of array 'excludes' must be of the type string"); - return {}; + continue; } dirs.push_back(item.asString()); }