From 276f520888d8b965a7a2ad4f5d0916a896c42487 Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Wed, 13 Jan 2021 14:04:13 +0800 Subject: [PATCH] fix the bug of cannot delete files and add the handling of modify events --- libsearch/global-settings.h | 1 + libsearch/index/first-index.cpp | 13 ++- libsearch/index/index-generator.cpp | 6 +- libsearch/index/inotify-index.cpp | 153 +++++++++++++++++++++++++--- libsearch/index/inotify-index.h | 1 + 5 files changed, 154 insertions(+), 20 deletions(-) diff --git a/libsearch/global-settings.h b/libsearch/global-settings.h index a4d13d5..e9d2d1b 100644 --- a/libsearch/global-settings.h +++ b/libsearch/global-settings.h @@ -17,6 +17,7 @@ #define INDEX_DATABASE_STATE "index_database_state" #define CONTENT_INDEX_DATABASE_STATE "content_index_database_state" #define INDEX_GENERATOR_NORMAL_EXIT "index_generator_normal_exit" +#define INOTIFY_NORMAL_EXIT "inotify_normal_exit" class LIBSEARCH_EXPORT GlobalSettings : public QObject { diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index f2468ac..dc8ddac 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -5,9 +5,10 @@ void handler(int){ qDebug() << "Recieved SIGTERM!"; - GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"2"); - GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE,"2"); - GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"2"); + GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); + GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); + GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT, "2"); + GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); @@ -25,9 +26,11 @@ FirstIndex::FirstIndex(const QString& path) : Traverse_BFS(path) { QString indexDataBaseStatus = GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); QString contentIndexDataBaseStatus = GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString(); + QString inotifyIndexStatus = GlobalSettings::getInstance()->getValue(INOTIFY_NORMAL_EXIT).toString(); qDebug() << "indexDataBaseStatus: " << indexDataBaseStatus; qDebug() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus; + qDebug() << "inotifyIndexStatus: " << inotifyIndexStatus; if (indexDataBaseStatus == "" || contentIndexDataBaseStatus == ""){ @@ -36,7 +39,7 @@ FirstIndex::FirstIndex(const QString& path) : Traverse_BFS(path) else{ this->bool_dataBaseExist = true; } - if (indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2"){ + if (indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2" || inotifyIndexStatus != "2"){ this->bool_dataBaseStatusOK = false; } else{ @@ -79,8 +82,8 @@ void FirstIndex::run(){ //why??????????????????????????????????????????????????????????????? //why not quit? // this->quit(); +// exit(0); return; -// return; // this->wait(); } else{ diff --git a/libsearch/index/index-generator.cpp b/libsearch/index/index-generator.cpp index 2dfe68d..366f3b7 100644 --- a/libsearch/index/index-generator.cpp +++ b/libsearch/index/index-generator.cpp @@ -127,9 +127,9 @@ IndexGenerator::~IndexGenerator() delete m_datebase_path; if(m_database_content) delete m_database_content; - GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"2"); - GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE,"2"); - GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"2"); + GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2"); + GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2"); + GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT, "2"); qDebug() << "QThread::currentThreadId()" << QThread::currentThreadId(); qDebug() << "~IndexGenerator end"; diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 0448bc3..913380c 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -18,10 +18,14 @@ InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path) this->AddWatch("/home"); this->Traverse(); + + + GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0"); } InotifyIndex::~InotifyIndex() { + GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2"); IndexGenerator::getInstance()->~IndexGenerator(); } @@ -32,12 +36,13 @@ void InotifyIndex::DoSomething(const QFileInfo& fileInfo){ } bool InotifyIndex::AddWatch(const QString &path){ - int ret = inotify_add_watch(m_fd, path.toStdString().c_str(), (IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE | IN_DELETE)); + int ret = inotify_add_watch(m_fd, path.toStdString().c_str(), (IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE | IN_DELETE | IN_MODIFY)); if (ret == -1) { qDebug() << "AddWatch error:" << path; return false; } - Q_ASSERT(ret != -1); +// Q_ASSERT(ret != -1); + assert(ret != -1); currentPath[ret] = path; //qDebug() << "Watch:" << path; return true; @@ -49,15 +54,23 @@ bool InotifyIndex::RemoveWatch(const QString &path){ qDebug() << "remove path error"; return false; } - Q_ASSERT(ret != 0); +// Q_ASSERT(ret == 0); + assert(ret == 0); for (QMap::Iterator i = currentPath.begin(); i != currentPath.end();){ if (i.value().length() > path.length()){ if (i.value().mid(0, path.length()) == path){ -// qDebug() << i.value(); + qDebug() << "remove path: " << i.value(); + ret = inotify_rm_watch(m_fd, currentPath.key(path)); + if (ret){ + qDebug() << "remove path error"; +// return false; + } +// Q_ASSERT(ret == 0); +// assert(ret == 0); /*--------------------------------*/ //在此调用删除索引 - IndexGenerator::getInstance()->deleteAllIndex(new QStringList(path)); + IndexGenerator::getInstance()->deleteAllIndex(new QStringList(i.value())); /*--------------------------------*/ currentPath.erase(i++); } @@ -105,11 +118,12 @@ void InotifyIndex::run(){ for (p = buf; p < buf + numRead;) { struct inotify_event * event = reinterpret_cast(p); + qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask; if(event->name[0] != '.'){ + qDebug() << QString(currentPath[event->wd] + '/' + event->name); - //传创建或移动过来的文件路径 - if((event->mask & IN_CREATE) | (event->mask & IN_MOVED_TO)){ - //添加监视要先序遍历,先添加top节点 +// switch (event->mask) { + if (event->mask & IN_CREATE){ if (event->mask & IN_ISDIR){ AddWatch(currentPath[event->wd] + '/' + event->name); this->setPath(currentPath[event->wd] + '/' + event->name); @@ -118,7 +132,6 @@ void InotifyIndex::run(){ /*--------------------------------*/ // IndexGenerator::getInstance()->creatAllIndex(QQueue>(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0"))); - qDebug() << QString(currentPath[event->wd] + '/' + event->name); indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); IndexGenerator::getInstance()->creatAllIndex(indexQueue); indexQueue->clear(); @@ -127,18 +140,134 @@ void InotifyIndex::run(){ contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); contentIndexQueue->clear(); + break; } } - /*--------------------------------*/ + goto next; } - else if((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){ + + + if ((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){ if (event->mask & IN_ISDIR){ RemoveWatch(currentPath[event->wd] + '/' + event->name); } - IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); + //delete once more + IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); + goto next; } + + + if (event->mask & IN_MODIFY){ + if (!(event->mask & IN_ISDIR)){ + IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); + indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); + IndexGenerator::getInstance()->creatAllIndex(indexQueue); + indexQueue->clear(); + for (auto i : this->targetFileTypeVec){ + if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){ + contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); + IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); + contentIndexQueue->clear(); + break; + } + } + } + goto next; + } + + + if (event->mask & IN_MOVED_TO){ + if (event->mask & IN_ISDIR){ + RemoveWatch(currentPath[event->wd] + '/' + event->name); + IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); + AddWatch(currentPath[event->wd] + '/' + event->name); + this->setPath(currentPath[event->wd] + '/' + event->name); + Traverse(); + + // + + + + indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); + IndexGenerator::getInstance()->creatAllIndex(indexQueue); + indexQueue->clear(); + for (auto i : this->targetFileTypeVec){ + if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){ + contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); + IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); + contentIndexQueue->clear(); + break; + } + } + } + else { + IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); + indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); + IndexGenerator::getInstance()->creatAllIndex(indexQueue); + indexQueue->clear(); + for (auto i : this->targetFileTypeVec){ + if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){ + contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); + IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); + contentIndexQueue->clear(); + break; + } + } + } + goto next; + } + +// } + +// //传创建或移动过来的文件路径 +// if((event->mask & IN_CREATE)){ +// //添加监视要先序遍历,先添加top节点 +// if (event->mask & IN_ISDIR){ +// AddWatch(currentPath[event->wd] + '/' + event->name); +// this->setPath(currentPath[event->wd] + '/' + event->name); +// Traverse(); +// } + +// /*--------------------------------*/ +//// IndexGenerator::getInstance()->creatAllIndex(QQueue>(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0"))); +// indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); +// IndexGenerator::getInstance()->creatAllIndex(indexQueue); +// indexQueue->clear(); +// for (auto i : this->targetFileTypeVec){ +// if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){ +// contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); +// IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); +// contentIndexQueue->clear(); +// break; +// } +// } +// /*--------------------------------*/ +// } +// else if((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){ +// if (event->mask & IN_ISDIR){ +// RemoveWatch(currentPath[event->wd] + '/' + event->name); +// } +// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); +// } +// else if((event->mask & IN_MODIFY) | (event->mask & IN_MOVED_TO)){ +// if (!(event->mask & IN_ISDIR)){ +// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); +// indexQueue->enqueue(QVector() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); +// IndexGenerator::getInstance()->creatAllIndex(indexQueue); +// indexQueue->clear(); +// for (auto i : this->targetFileTypeVec){ +// if (QString(currentPath[event->wd] + '/' + event->name).endsWith(i)){ +// contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); +// IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); +// contentIndexQueue->clear(); +// break; +// } +// } +// } +// } /*--------------------------------*/ } +next: p += sizeof(struct inotify_event) + event->len; } } diff --git a/libsearch/index/inotify-index.h b/libsearch/index/inotify-index.h index 695ce76..32363aa 100644 --- a/libsearch/index/inotify-index.h +++ b/libsearch/index/inotify-index.h @@ -7,6 +7,7 @@ #include "index-generator.h" #include "traverse_bfs.h" #include "ukui-search-qdbus.h" +#include "global-settings.h" #define BUF_LEN 1024 class InotifyIndex;