Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/dfm-search/dfm-search-client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
#include <dfm-search/searchengine.h>
#include <dfm-search/searchfactory.h>
#include <dfm-search/searchquery.h>
#include <dfm-search/searchoptions.h>

Check warning on line 24 in src/dfm-search/dfm-search-client/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-search/searchoptions.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 24 in src/dfm-search/dfm-search-client/main.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-search/searchoptions.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dfm-search/filenamesearchapi.h>

Check warning on line 25 in src/dfm-search/dfm-search-client/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-search/filenamesearchapi.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 25 in src/dfm-search/dfm-search-client/main.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-search/filenamesearchapi.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dfm-search/contentsearchapi.h>

Check warning on line 26 in src/dfm-search/dfm-search-client/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-search/contentsearchapi.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 26 in src/dfm-search/dfm-search-client/main.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-search/contentsearchapi.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "../dfm-search-lib/utils/filenameblacklistmatcher.h"

using namespace dfmsearch;

Expand Down Expand Up @@ -522,6 +523,56 @@
*/
}

void doFileNameBlacklistMatchTest(const std::string &caseName,
const QString &inputPath,
const QStringList &blacklistEntries,
bool expected)
{
const bool actual = Global::BlacklistMatcher::isPathBlacklisted(inputPath, blacklistEntries);
std::cout << "文件名黑名单测试 [" << caseName << "]\t"
<< "输入路径: " << inputPath.toStdString() << "\t"
<< "黑名单: " << blacklistEntries.join(", ").toStdString() << "\t"
<< "预期结果: " << (expected ? "命中" : "不命中") << "\t"
<< "实际结果: " << (actual ? "命中" : "不命中") << "\t"
<< "状态: " << (actual == expected ? "通过" : "失败") << "\n"
<< "---------------------------------\n";
}

void testFileNameBlacklistMatcher()
{
std::cout << "====== 开始文件名黑名单规则测试 ======\n";

doFileNameBlacklistMatchTest("绝对路径-自身命中",
"/home/test/workspace",
{ "/home/test/workspace" },
true);

doFileNameBlacklistMatchTest("绝对路径-子路径命中",
"/home/test/workspace/a.txt",
{ "/home/test/workspace" },
true);

doFileNameBlacklistMatchTest("绝对路径-边界不误匹配",
"/home/test/workspace2",
{ "/home/test/workspace" },
false);

doFileNameBlacklistMatchTest("目录名-直接命中",
"/home/test/workspace",
{ "workspace" },
true);

doFileNameBlacklistMatchTest("目录名-深层命中",
"/home/test/aa/bb/workspace",
{ "workspace" },
true);

doFileNameBlacklistMatchTest("目录名-不命中相似名称",
"/home/test/aa/bb/myworkspace",
{ "workspace" },
false);
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Expand All @@ -531,6 +582,7 @@
testPinyin();
testPinyinAcronym();
testAnythingStatus();
testFileNameBlacklistMatcher();
#endif

QCommandLineParser parser;
Expand Down
59 changes: 59 additions & 0 deletions src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "filenameblacklistmatcher.h"

#include <QDir>

Check warning on line 6 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 6 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.

DFM_SEARCH_BEGIN_NS
namespace Global {
namespace BlacklistMatcher {

QString normalizePathForBlacklistMatch(const QString &path)
{
return QDir::cleanPath(path.trimmed());
}

static bool isAbsolutePathMatch(const QString &normalizedPath, const QString &blacklistAbsolutePath)
{
const QString normalizedRulePath = normalizePathForBlacklistMatch(blacklistAbsolutePath);
if (normalizedRulePath.isEmpty()) {
return false;
}

if (normalizedRulePath == "/") {
return normalizedPath.startsWith('/');
}

return normalizedPath == normalizedRulePath || normalizedPath.startsWith(normalizedRulePath + '/');
}

bool isPathBlacklisted(const QString &inputPath, const QStringList &blacklistEntries)
{
const QString normalizedPath = normalizePathForBlacklistMatch(inputPath);
const QStringList pathSegments = normalizedPath.split('/', Qt::SkipEmptyParts);

for (const QString &entry : blacklistEntries) {

Check warning on line 36 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Consider using std::any_of algorithm instead of a raw loop.

Check warning on line 36 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Consider using std::any_of algorithm instead of a raw loop.
const QString trimmedEntry = entry.trimmed();
if (trimmedEntry.isEmpty()) {
continue;
}

if (QDir::isAbsolutePath(trimmedEntry)) {
if (isAbsolutePathMatch(normalizedPath, trimmedEntry)) {
return true;
}
continue;
}

if (pathSegments.contains(trimmedEntry)) {
return true;
}
}

return false;
}

} // namespace BlacklistMatcher
} // namespace Global
DFM_SEARCH_END_NS
23 changes: 23 additions & 0 deletions src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef FILENAMEBLACKLISTMATCHER_H
#define FILENAMEBLACKLISTMATCHER_H

#include <QString>

Check warning on line 7 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QString> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 7 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.h

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QString> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QStringList>

Check warning on line 8 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QStringList> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 8 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.h

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QStringList> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <dfm-search/dsearch_global.h>

Check warning on line 10 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-search/dsearch_global.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 10 in src/dfm-search/dfm-search-lib/utils/filenameblacklistmatcher.h

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-search/dsearch_global.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

DFM_SEARCH_BEGIN_NS
namespace Global {
namespace BlacklistMatcher {

QString normalizePathForBlacklistMatch(const QString &path);
bool isPathBlacklisted(const QString &inputPath, const QStringList &blacklistEntries);

} // namespace BlacklistMatcher
} // namespace Global
DFM_SEARCH_END_NS

#endif // FILENAMEBLACKLISTMATCHER_H
4 changes: 4 additions & 0 deletions src/dfm-search/dfm-search-lib/utils/searchutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "searchutility.h"
#include "filenameblacklistmatcher.h"

#include <unistd.h>

Check warning on line 7 in src/dfm-search/dfm-search-lib/utils/searchutility.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 7 in src/dfm-search/dfm-search-lib/utils/searchutility.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <DConfig>

Expand Down Expand Up @@ -631,6 +632,9 @@
if (!isFileNameIndexDirectoryAvailable())
return false;

if (BlacklistMatcher::isPathBlacklisted(path, defaultBlacklistPaths()))
return false;

const QStringList &dirs = defaultIndexedDirectory();
return std::any_of(dirs.cbegin(), dirs.cend(),
[&path](const QString &dir) { return path.startsWith(dir); });
Expand Down
Loading