文件索引改为白名单形式.

This commit is contained in:
iaom 2022-04-14 11:16:26 +08:00
parent a7cec2da4f
commit 6e7ca5e35c
15 changed files with 130 additions and 65 deletions

View File

@ -39,7 +39,7 @@
#define MAIN_MARGINS 0, 0, 0, 0
#define TITLE_MARGINS 0,0,0,0
#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings"
#define SEARCH_METHOD_KEY "file-index-enable"
#define SEARCH_METHOD_KEY "fileIndexEnable"
#define WEB_ENGINE_KEY "webEngine"
#define WINDOW_WIDTH 700
#define WINDOW_HEIGHT 610

View File

@ -17,6 +17,8 @@ static const QString FILE_SEARCH_VALUE = QStringLiteral("0");
static const QString DIR_SEARCH_VALUE = QStringLiteral("1");
static const QString INDEX_SEM = QStringLiteral("ukui-search-index-sem");
static const int OCR_MIN_SIZE = 200;
static const QByteArray UKUI_SEARCH_SCHEMAS = QByteArrayLiteral("org.ukui.search.settings");
static const QString SEARCH_METHOD_KEY = QStringLiteral("fileIndexEnable");
static const QStringList allAppPath = {
{HOME_PATH + "/.local/share/applications/"},

View File

@ -29,16 +29,16 @@ QT_END_NAMESPACE
class DirWatcherAdaptor: public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.ukui.search.fileindex.service")
Q_CLASSINFO("D-Bus Interface", "org.ukui.search.fileindex")
Q_CLASSINFO("D-Bus Introspection", ""
" <interface name=\"org.ukui.search.fileindex\">\n"
" <method name=\"appendIndexableListItem\">\n"
" <arg direction=\"in\" type=\"s\" name=\"path\"/>\n"
" </method>\n"
" <method name=\"removeIndexableListItem\">\n"
" <arg direction=\"in\" type=\"s\" name=\"path\"/>\n"
" </method>\n"
" </interface>\n"
" <interface name=\"org.ukui.search.fileindex\">"
" <method name=\"appendIndexableListItem\">"
" <arg direction=\"in\" type=\"s\" name=\"path\"/>"
" </method>"
" <method name=\"removeIndexableListItem\">"
" <arg direction=\"in\" type=\"s\" name=\"path\"/>"
" </method>"
" </interface>"
"")
public:
DirWatcherAdaptor(QObject *parent);

View File

@ -43,6 +43,19 @@ DirWatcher *DirWatcher::getDirWatcher()
return global_intance;
}
void DirWatcher::initDbusService()
{
//注册服务
QDBusConnection sessionBus = QDBusConnection::sessionBus();
if (!sessionBus.registerService("com.ukui.search.fileindex.service")) {
qWarning() << "ukui-search-fileindex dbus register service failed reason:" << sessionBus.lastError();
}
if(!sessionBus.registerObject("/org/ukui/search/fileindex", this, QDBusConnection::ExportAdaptors)){
qWarning() << "ukui-search-fileindex dbus register object failed reason:" << sessionBus.lastError();
}
}
QStringList DirWatcher::currentindexableDir()
{
QMutexLocker locker(&s_mutex);

View File

@ -27,6 +27,7 @@ class DirWatcher : public QObject
public:
static DirWatcher *getDirWatcher();
void initDbusService();
QStringList currentindexableDir();
QStringList currentBlackListOfIndex();
void handleIndexItemAppend(const QString &path);

View File

@ -1,4 +1,6 @@
#include "file-index-manager.h"
#include "dir-watcher.h"
#include "common.h"
using namespace UkuiSearch;
static FileIndexManager* global_instance = nullptr;
FileIndexManager::FileIndexManager(QObject *parent) : QObject(parent), m_semaphore(INDEX_SEM, 1, QSystemSemaphore::AccessMode::Create)
@ -23,21 +25,6 @@ void FileIndexManager::searchMethod(FileUtils::SearchMethod sm) {
qWarning("enum class error!!!\n");
}
if(FileUtils::SearchMethod::INDEXSEARCH == sm && 0 == FileUtils::indexStatus) {
// // Create a fifo at ~/.config/org.ukui/ukui-search, the fifo is used to control the order of child processes' running.
// QDir fifoDir = QDir(QDir::homePath() + "/.config/org.ukui/ukui-search");
// if(!fifoDir.exists())
// qDebug() << "create fifo path" << fifoDir.mkpath(fifoDir.absolutePath());
// unlink(UKUI_SEARCH_PIPE_PATH);
// int retval = mkfifo(UKUI_SEARCH_PIPE_PATH, 0777);
// if(retval == -1) {
// qCritical() << "creat fifo error!!";
// syslog(LOG_ERR, "creat fifo error!!\n");
// assert(false);
// return;
// }
// qDebug() << "create fifo success\n";
qDebug() << "start first index";
m_semaphore.acquire();
m_fi->start();
@ -52,3 +39,32 @@ void FileIndexManager::searchMethod(FileUtils::SearchMethod sm) {
}
qWarning() << "searchMethod end: " << static_cast<int>(FileUtils::searchMethod);
}
void FileIndexManager::initIndexPathSetFunction()
{
const QByteArray id(UKUI_SEARCH_SCHEMAS);
if(QGSettings::isSchemaInstalled(id)) {
m_searchSettings = new QGSettings(id);
if(!m_searchSettings->keys().contains(SEARCH_METHOD_KEY)) {
qWarning() << "Can not find gsettings key:" << UKUI_SEARCH_SCHEMAS << SEARCH_METHOD_KEY;
return;
}
} else {
qWarning() << "Can not find gsettings:" << UKUI_SEARCH_SCHEMAS;
return;
}
connect(DirWatcher::getDirWatcher(), &DirWatcher::appendIndexItem, this, &FileIndexManager::handleIndexPathAppend, Qt::QueuedConnection);
DirWatcher::getDirWatcher()->initDbusService();
}
void FileIndexManager::handleIndexPathAppend(const QString path, const QStringList blockList)
{
if(!m_searchSettings->get(SEARCH_METHOD_KEY).toBool()) {
m_searchSettings->set(SEARCH_METHOD_KEY, true);
} else {
m_fi->addIndexPath(path, blockList);
m_iw->addIndexPath(path, blockList);
}
}

View File

@ -3,6 +3,7 @@
#include <QObject>
#include <QSystemSemaphore>
#include <QGSettings/QGSettings>
#include "first-index.h"
//#include "inotify-index.h"
#include "inotify-watch.h"
@ -12,12 +13,16 @@ class FileIndexManager : public QObject {
public:
static FileIndexManager *getInstance();
void searchMethod(FileUtils::SearchMethod sm);
void initIndexPathSetFunction();
private Q_SLOTS:
void handleIndexPathAppend(const QString path, const QStringList blockList);
private:
FileIndexManager(QObject *parent = nullptr);
FirstIndex *m_fi;
// InotifyIndex* m_ii;
InotifyWatch *m_iw = nullptr;
QSystemSemaphore m_semaphore;
QGSettings *m_searchSettings = nullptr;
};
}

View File

@ -54,7 +54,7 @@ FirstIndex::~FirstIndex() {
qDebug() << "~FirstIndex end";
}
void FirstIndex::DoSomething(const QFileInfo& fileInfo) {
void FirstIndex::work(const QFileInfo& fileInfo) {
// qDebug() << "there are some shit here"<<fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0");
this->m_indexData->enqueue(QVector<QString>() << fileInfo.fileName()
<< fileInfo.absoluteFilePath()
@ -113,30 +113,47 @@ void FirstIndex::DoSomething(const QFileInfo& fileInfo) {
}
}
void FirstIndex::addIndexPath(const QString path, const QStringList blockList)
{
m_semaphore.acquire();
setPath(QStringList() << path);
setBlockPath(blockList);
this->wait();
this->start();
}
void FirstIndex::run() {
QTime t1 = QTime::currentTime();
// Create a fifo at ~/.config/org.ukui/ukui-search, the fifo is used to control the order of child processes' running.
QString indexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(INDEX_DATABASE_STATE).toString();
QString contentIndexDataBaseStatus = IndexStatusRecorder::getInstance()->getStatus(CONTENT_INDEX_DATABASE_STATE).toString();
// QString ocrIndexDatabaseStatus = IndexStatusRecorder::getInstance()->getStatus(OCR_DATABASE_STATE).toString();
// QString ocrIndexDatabaseStatus = IndexStatusRecorder::getInstance()->getStatus(OCR_DATABASE_STATE).toString();
QString inotifyIndexStatus = IndexStatusRecorder::getInstance()->getStatus(INOTIFY_NORMAL_EXIT).toString();
qInfo() << "indexDataBaseStatus: " << indexDataBaseStatus;
qInfo() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus;
// qInfo() << "ocrIndexDatabaseStatus: " << ocrIndexDatabaseStatus;
// qInfo() << "ocrIndexDatabaseStatus: " << ocrIndexDatabaseStatus;
qInfo() << "inotifyIndexStatus: " << inotifyIndexStatus;
m_allDatadaseStatus = inotifyIndexStatus == "2" ? true : false;
m_indexDatabaseStatus = indexDataBaseStatus == "2" ? true : false;
m_contentIndexDatabaseStatus = contentIndexDataBaseStatus == "2" ? true : false;
// m_ocrIndexDatabaseStatus = ocrIndexDatabaseStatus == "2" ? true : false;
// m_ocrIndexDatabaseStatus = ocrIndexDatabaseStatus == "2" ? true : false;
if(m_allDatadaseStatus && m_indexDatabaseStatus && m_contentIndexDatabaseStatus /*&& m_ocrIndexDatabaseStatus*/) {
m_semaphore.release(1);
return;
if(m_isFirstIndex) {
m_isFirstIndex = false;
m_semaphore.release(1);
return;
}
} else {
setPath(DirWatcher::getDirWatcher()->currentindexableDir());
setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex());
}
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "0");
this->m_indexData = new QQueue<QVector<QString>>();
this->m_contentIndexData = new QQueue<QPair<QString,qint64>>();
// this->m_ocrIndexData = new QQueue<QPair<QString,qint64>>();
@ -156,8 +173,6 @@ void FirstIndex::run() {
qInfo() << "index dir" << DirWatcher::getDirWatcher()->currentindexableDir();
qInfo() << "index block dir" << DirWatcher::getDirWatcher()->currentBlackListOfIndex();
setPath(DirWatcher::getDirWatcher()->currentindexableDir());
setBlockPath(DirWatcher::getDirWatcher()->currentBlackListOfIndex());
this->Traverse();
FileUtils::maxIndexCount = this->m_indexData->length();
@ -165,7 +180,7 @@ void FirstIndex::run() {
QtConcurrent::run(&m_pool, [&]() {
sem.acquire(2);
mutex1.unlock();
if(m_allDatadaseStatus && m_indexDatabaseStatus) {
if(m_isFirstIndex && m_allDatadaseStatus && m_indexDatabaseStatus) {
sem.release(2);
return;
}
@ -193,7 +208,7 @@ void FirstIndex::run() {
QtConcurrent::run(&m_pool,[&]() {
sem.acquire(2);
mutex2.unlock();
if(m_allDatadaseStatus && m_contentIndexDatabaseStatus) {
if(m_isFirstIndex && m_allDatadaseStatus && m_contentIndexDatabaseStatus) {
sem.release(2);
return;
}
@ -236,7 +251,7 @@ void FirstIndex::run() {
// mutex3.unlock();
// QQueue<QString>* tmpOcr = new QQueue<QString>();
// qDebug() << "m_ocr_index:" << m_ocr_index->size();
// if(m_allDatadaseStatus && m_contentIndexDatabaseStatus) {
// if(m_isFirstIndex && m_allDatadaseStatus && m_contentIndexDatabaseStatus) {
// sem.release(2);
// return;
// }
@ -296,8 +311,9 @@ void FirstIndex::run() {
--FileUtils::indexStatus;
}
m_semaphore.release(1);
m_isFirstIndex = false; //首次索引后置为false,后续start为添加索引目录时新建索引。
IndexStatusRecorder::getInstance()->setStatus(INOTIFY_NORMAL_EXIT, "2");
m_semaphore.release(1);
// int retval1 = write(fifo_fd, buffer, strlen(buffer));
// if(retval1 == -1) {
// qWarning("write error\n");

View File

@ -46,7 +46,8 @@ class FirstIndex : public QThread, public Traverse_BFS {
public:
static FirstIndex* getInstance();
~FirstIndex();
virtual void DoSomething(const QFileInfo &) final;
virtual void work(const QFileInfo &) final;
void addIndexPath(const QString path, const QStringList blockList);
protected:
void run() override;
@ -61,6 +62,7 @@ private:
bool m_contentIndexDatabaseStatus = false;
bool m_ocrIndexDatabaseStatus = false;
bool m_allDatadaseStatus = false;
bool m_isFirstIndex = true;
QThreadPool m_pool;
QQueue<QVector<QString>>* m_indexData = nullptr;

View File

@ -14,7 +14,7 @@ UkuiSearch::InotifyWatch *UkuiSearch::InotifyWatch::getInstance()
return global_instance_InotifyWatch;
}
UkuiSearch::InotifyWatch::InotifyWatch(): Traverse_BFS()
UkuiSearch::InotifyWatch::InotifyWatch(): Traverse_BFS(), m_semaphore(INDEX_SEM, 0, QSystemSemaphore::AccessMode::Open)
{
qDebug() << "setInotifyMaxUserWatches start";
UkuiSearchQDBus usQDBus;
@ -83,7 +83,7 @@ bool InotifyWatch::removeWatch(const QString &path, bool removeFromDatabase)
return true;
}
void InotifyWatch::DoSomething(const QFileInfo &info)
void InotifyWatch::work(const QFileInfo &info)
{
qDebug() << info.fileName() << "-------" << info.absoluteFilePath();
if(info.isDir() && (!info.isSymLink())) {
@ -96,24 +96,31 @@ void InotifyWatch::DoSomething(const QFileInfo &info)
PendingFileQueue::getInstance()->enqueue(f);
}
void InotifyWatch::firstTraverse()
void InotifyWatch::firstTraverse(QStringList pathList, QStringList blockList)
{
if(pathList.isEmpty()) {
pathList = m_pathList;
}
if(blockList.isEmpty()) {
blockList = m_blockList;
}
QQueue<QString> bfs;
for(QString blockPath : m_blockList) {
for(QString path : m_pathList) {
for(QString blockPath : blockList) {
for(QString path : pathList) {
if(FileUtils::isOrUnder(path, blockPath)) {
m_pathList.removeOne(path);
pathList.removeOne(path);
}
}
}
for(QString path : m_pathList) {
for(QString path : pathList) {
addWatch(path);
bfs.enqueue(path);
}
QFileInfoList list;
QDir dir;
QStringList tmpList = m_blockList;
QStringList tmpList = blockList;
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
dir.setSorting(QDir::DirsFirst);
while(!bfs.empty()) {
@ -139,6 +146,16 @@ void InotifyWatch::firstTraverse()
}
}
void InotifyWatch::addIndexPath(const QString path, const QStringList blockList)
{
this->firstTraverse(QStringList() << path, blockList);
}
void InotifyWatch::removeIndexPath(QString &path)
{
}
void InotifyWatch::stopWatch()
{
// if(this->isRunning()) {

View File

@ -6,6 +6,7 @@
#include <QSocketNotifier>
#include <QDataStream>
#include <QSharedMemory>
#include <QSystemSemaphore>
#include <sys/prctl.h>
#include <sys/wait.h>
@ -26,10 +27,12 @@ public:
static InotifyWatch* getInstance();
bool addWatch(const QString &path);
bool removeWatch(const QString &path, bool removeFromDatabase = true);
virtual void DoSomething(const QFileInfo &info) final;
virtual void work(const QFileInfo &info) final;
void firstTraverse();
void firstTraverse(QStringList pathList = {}, QStringList blockList = {});
void stopWatch();
void addIndexPath(const QString path, const QStringList blockList);
void removeIndexPath(QString &path);
protected:
void run() override;
@ -47,7 +50,7 @@ private:
QSharedMemory *m_sharedMemory = nullptr;
QMap<int, QString> m_pathMap;
QMutex m_mutex;
QSystemSemaphore m_semaphore;
};
}

View File

@ -35,6 +35,7 @@ void Traverse_BFS::Traverse() {
}
}
for(QString path : m_pathList) {
work(QFileInfo(path));
bfs.enqueue(path);
}
@ -62,7 +63,7 @@ void Traverse_BFS::Traverse() {
if(i.isDir() && (!(i.isSymLink()))) {
bfs.enqueue(i.absoluteFilePath());
}
DoSomething(i);
work(i);
}
}
}

View File

@ -30,7 +30,7 @@ public:
Traverse_BFS() = default;
void Traverse();
virtual ~Traverse_BFS() = default;
virtual void DoSomething(const QFileInfo&) = 0;
virtual void work(const QFileInfo&) = 0;
void setPath(const QStringList&);
void setBlockPath(const QStringList &pathList);
protected:

View File

@ -22,7 +22,7 @@
#include <ukcc/widgets/titlelabel.h>
#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings"
#define SEARCH_METHOD_KEY "file-index-enable"
#define SEARCH_METHOD_KEY "fileIndexEnable"
#define WEB_ENGINE_KEY "webEngine"
//TODO
#define CONFIG_FILE "/.config/org.ukui/ukui-search/ukui-search-block-dirs.conf"

View File

@ -2,11 +2,9 @@
#include "ukui-search-service.h"
#include "dir-watcher.h"
#include "dir-watcher-adaptor.h"
#include "common.h"
#include <QDBusConnection>
#define UKUI_SEARCH_SCHEMAS "org.ukui.search.settings"
#define SEARCH_METHOD_KEY "file-index-enable"
using namespace UkuiSearch;
UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &applicationName): QtSingleApplication (applicationName, argc, argv)
{
@ -19,17 +17,8 @@ UkuiSearchService::UkuiSearchService(int &argc, char *argv[], const QString &app
this->parseCmd(msg, true);
});
//注册服务
QDBusConnection sessionBus = QDBusConnection::sessionBus();
if (!sessionBus.registerService("com.ukui.search.fileindex.service")) {
qWarning() << "ukui-search-fileindex dbus register service failed reason:" << sessionBus.lastError();
}
if(!sessionBus.registerObject("/org/ukui/search/fileindex", DirWatcher::getDirWatcher(), QDBusConnection::ExportAdaptors)){
qWarning() << "ukui-search-fileindex dbus register object failed reason:" << sessionBus.lastError();
}
initGsettings();
FileIndexManager::getInstance()->initIndexPathSetFunction();
}
//parse cmd