From b1ce879b0b3ca7ee63e1e46c2b22b2e86b3a0ad2 Mon Sep 17 00:00:00 2001 From: hunili Date: Wed, 4 Dec 2024 11:25:06 +0800 Subject: [PATCH] =?UTF-8?q?excludes=20=E8=B7=AF=E5=BE=84=E6=9C=AA=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20/=20=E5=A4=B1=E6=95=88=20issue:=20https://gitee.com?= =?UTF-8?q?/openharmony/filemanagement=5Fapp=5Ffile=5Fservice/issues/IB8Y2?= =?UTF-8?q?6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hunili --- .../backup_utils/b_filesystem/b_dir_test.cpp | 38 ++++++++++++++ utils/src/b_filesystem/b_dir.cpp | 51 +++++++++++-------- 2 files changed, 68 insertions(+), 21 deletions(-) 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 793168564..333cc166a 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 baac25c55..dccdcf1f7 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; -- Gitee