diff --git a/tests/unittests/backup_utils/b_filesystem/b_dir_test.cpp b/tests/unittests/backup_utils/b_filesystem/b_dir_test.cpp index 79316856459efe3b03ebb86b98e0713e6cb59581..333cc166a402e4351d610bec140cb27266cb50a9 100644 --- a/tests/unittests/backup_utils/b_filesystem/b_dir_test.cpp +++ b/tests/unittests/backup_utils/b_filesystem/b_dir_test.cpp @@ -527,4 +527,42 @@ HWTEST_F(BDirTest, b_dir_GetSubDir_0100, testing::ext::TestSize.Level1) } GTEST_LOG_(INFO) << "BDirTest-end b_dir_GetSubDir_0100"; } + +/** + * @tc.number: SUB_backup_b_dir_PreDealExcludes_0100 + * @tc.name: b_dir_PreDealExcludes_0100 + * @tc.desc: Test function of PreDealExcludes interface for SUCCESS + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6F3GV + */ +HWTEST_F(BDirTest, b_dir__PreDealExcludes_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "BDirTest-begin b_dir_PreDealExcludes_0100"; + try { + std::string firstEle = "test"; + std::string secEle = ""; + std::string thirdEle = "test/test1"; + std::string fourthEle = "/test/test1"; + std::string fifthEle = "/test/test1/"; + std::vector excludes = { + firstEle, + secEle, + thirdEle, + fourthEle, + fifthEle + }; + PreDealExcludes(excludes); + EXPECT_EQ(excludes.size(), 4); // 4: the size of excludes after preDeal + EXPECT_EQ(excludes[0], firstEle); // 0: first idx + EXPECT_EQ(excludes[1], fourthEle); // 1: second idx + EXPECT_EQ(excludes[2], fourthEle); // 2: third idx + EXPECT_EQ(excludes[3], fifthEle + "*"); // 3: firth idx + } catch (...) { + GTEST_LOG_(INFO) << "BDirTest-an PreDealExcludes exception occurred."; + EXPECT_TRUE(false); + } + GTEST_LOG_(INFO) << "BDirTest-end b_dir_PreDealExcludes_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/utils/src/b_filesystem/b_dir.cpp b/utils/src/b_filesystem/b_dir.cpp index baac25c55f185586a0fbfda3c2a43198876c4fc9..dccdcf1f76aeb3a0a5faa8ace6bf58fb8128823b 100644 --- a/utils/src/b_filesystem/b_dir.cpp +++ b/utils/src/b_filesystem/b_dir.cpp @@ -38,6 +38,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; const int32_t PATH_MAX_LEN = 4096; +const size_t TOP_ELE = 0; const std::string APP_DATA_DIR = BConstants::PATH_PUBLIC_HOME + BConstants::PATH_APP_DATA + BConstants::FILE_SEPARATOR_CHAR; @@ -149,6 +150,25 @@ static tuple, map> GetDirFiles return {ERR_OK, files, smallFiles}; } +static void PreDealExcludes(std::vector &excludes) +{ + size_t lenEx = excludes.size(); + int j = 0; + for (size_t i = 0; i < lenEx; ++i) { + if (!excludes[i].empty()) { + if (excludes[i].at(excludes[i].size() - 1) == BConstants::FILE_SEPARATOR_CHAR) { + excludes[i] += "*"; + } + if (excludes[i].find(BConstants::FILE_SEPARATOR_CHAR) != string::npos && + excludes[i].at(TOP_ELE) != BConstants::FILE_SEPARATOR_CHAR) { + excludes[i] = BConstants::FILE_SEPARATOR_CHAR + excludes[i]; + } + excludes[j++] = excludes[i]; + } + } + excludes.resize(j); +} + tuple> BDir::GetDirFiles(const string &path) { vector files; @@ -278,20 +298,14 @@ tuple, map> BDir::GetBigFiles( incSmallFiles.insert(smallFiles.begin(), smallFiles.end()); } } - + vector endExcludes = excludes; + PreDealExcludes(endExcludes); auto isMatch = [](const vector &s, const string &str) -> bool { if (str.empty()) { return false; } for (const string &item : s) { - if (item.empty()) { - continue; - } - string excludeItem = item; - if (excludeItem.at(item.size() - 1) == BConstants::FILE_SEPARATOR_CHAR) { - excludeItem += "*"; - } - if (fnmatch(excludeItem.data(), str.data(), FNM_LEADING_DIR) == 0) { + if (fnmatch(item.data(), str.data(), FNM_LEADING_DIR) == 0) { return true; } } @@ -300,14 +314,14 @@ tuple, map> BDir::GetBigFiles( map resSmallFiles; for (const auto &item : incSmallFiles) { - if (!isMatch(excludes, item.first)) { + if (!isMatch(endExcludes, item.first)) { resSmallFiles.insert(make_pair(item.first, item.second)); } } map bigFiles; for (const auto &item : incFiles) { - if (!isMatch(excludes, item.first)) { + if (!isMatch(endExcludes, item.first)) { bigFiles[item.first] = item.second; } } @@ -454,19 +468,14 @@ tuple, vector> BDir::GetBackupList(const vector & smallFiles.insert(smallFiles.end(), smallFile.begin(), smallFile.end()); } HILOGI("end bigfiles = %{public}zu, smallfiles = %{public}zu", bigFiles.size(), smallFiles.size()); + vector endExcludes = excludes; + PreDealExcludes(endExcludes); auto isMatch = [](const vector &s, const string &str) -> bool { if (str.empty()) { return false; } for (const string &item : s) { - if (item.empty()) { - continue; - } - string excludeItem = item; - if (excludeItem.at(item.size() - 1) == BConstants::FILE_SEPARATOR_CHAR) { - excludeItem += "*"; - } - if (fnmatch(excludeItem.data(), str.data(), FNM_LEADING_DIR) == 0) { + if (fnmatch(item.data(), str.data(), FNM_LEADING_DIR) == 0) { return true; } } @@ -474,14 +483,14 @@ tuple, vector> BDir::GetBackupList(const vector & }; for (auto item = bigFiles.begin(); item != bigFiles.end();) { - if (isMatch(excludes, *item)) { + if (isMatch(endExcludes, *item)) { item = bigFiles.erase(item); } else { ++item; } } for (auto item = smallFiles.begin(); item != smallFiles.end();) { - if (isMatch(excludes, *item)) { + if (isMatch(endExcludes, *item)) { item = smallFiles.erase(item); } else { ++item;