Merge pull request #75 from MouseZhangZh/0110-dev

fix the bug of cannot delete files and add the handling of modify events
This commit is contained in:
iaom 2021-01-13 14:11:10 +08:00 committed by GitHub
commit d48aebd08b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 154 additions and 20 deletions

View File

@ -17,6 +17,7 @@
#define INDEX_DATABASE_STATE "index_database_state" #define INDEX_DATABASE_STATE "index_database_state"
#define CONTENT_INDEX_DATABASE_STATE "content_index_database_state" #define CONTENT_INDEX_DATABASE_STATE "content_index_database_state"
#define INDEX_GENERATOR_NORMAL_EXIT "index_generator_normal_exit" #define INDEX_GENERATOR_NORMAL_EXIT "index_generator_normal_exit"
#define INOTIFY_NORMAL_EXIT "inotify_normal_exit"
class LIBSEARCH_EXPORT GlobalSettings : public QObject class LIBSEARCH_EXPORT GlobalSettings : public QObject
{ {

View File

@ -5,9 +5,10 @@
void handler(int){ void handler(int){
qDebug() << "Recieved SIGTERM!"; qDebug() << "Recieved SIGTERM!";
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"2"); GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2");
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE,"2"); GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2");
GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"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(); 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 indexDataBaseStatus = GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString();
QString contentIndexDataBaseStatus = GlobalSettings::getInstance()->getValue(CONTENT_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() << "indexDataBaseStatus: " << indexDataBaseStatus;
qDebug() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus; qDebug() << "contentIndexDataBaseStatus: " << contentIndexDataBaseStatus;
qDebug() << "inotifyIndexStatus: " << inotifyIndexStatus;
if (indexDataBaseStatus == "" || contentIndexDataBaseStatus == ""){ if (indexDataBaseStatus == "" || contentIndexDataBaseStatus == ""){
@ -36,7 +39,7 @@ FirstIndex::FirstIndex(const QString& path) : Traverse_BFS(path)
else{ else{
this->bool_dataBaseExist = true; this->bool_dataBaseExist = true;
} }
if (indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2"){ if (indexDataBaseStatus != "2" || contentIndexDataBaseStatus != "2" || inotifyIndexStatus != "2"){
this->bool_dataBaseStatusOK = false; this->bool_dataBaseStatusOK = false;
} }
else{ else{
@ -79,8 +82,8 @@ void FirstIndex::run(){
//why??????????????????????????????????????????????????????????????? //why???????????????????????????????????????????????????????????????
//why not quit? //why not quit?
// this->quit(); // this->quit();
// exit(0);
return; return;
// return;
// this->wait(); // this->wait();
} }
else{ else{

View File

@ -127,9 +127,9 @@ IndexGenerator::~IndexGenerator()
delete m_datebase_path; delete m_datebase_path;
if(m_database_content) if(m_database_content)
delete m_database_content; delete m_database_content;
GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE,"2"); GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2");
GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE,"2"); GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2");
GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT,"2"); GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT, "2");
qDebug() << "QThread::currentThreadId()" << QThread::currentThreadId(); qDebug() << "QThread::currentThreadId()" << QThread::currentThreadId();
qDebug() << "~IndexGenerator end"; qDebug() << "~IndexGenerator end";

View File

@ -18,10 +18,14 @@ InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path)
this->AddWatch("/home"); this->AddWatch("/home");
this->Traverse(); this->Traverse();
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0");
} }
InotifyIndex::~InotifyIndex() InotifyIndex::~InotifyIndex()
{ {
GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2");
IndexGenerator::getInstance()->~IndexGenerator(); IndexGenerator::getInstance()->~IndexGenerator();
} }
@ -32,12 +36,13 @@ void InotifyIndex::DoSomething(const QFileInfo& fileInfo){
} }
bool InotifyIndex::AddWatch(const QString &path){ 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) { if (ret == -1) {
qDebug() << "AddWatch error:" << path; qDebug() << "AddWatch error:" << path;
return false; return false;
} }
Q_ASSERT(ret != -1); // Q_ASSERT(ret != -1);
assert(ret != -1);
currentPath[ret] = path; currentPath[ret] = path;
//qDebug() << "Watch:" << path; //qDebug() << "Watch:" << path;
return true; return true;
@ -49,15 +54,23 @@ bool InotifyIndex::RemoveWatch(const QString &path){
qDebug() << "remove path error"; qDebug() << "remove path error";
return false; return false;
} }
Q_ASSERT(ret != 0); // Q_ASSERT(ret == 0);
assert(ret == 0);
for (QMap<int, QString>::Iterator i = currentPath.begin(); i != currentPath.end();){ for (QMap<int, QString>::Iterator i = currentPath.begin(); i != currentPath.end();){
if (i.value().length() > path.length()){ if (i.value().length() > path.length()){
if (i.value().mid(0, path.length()) == path){ 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++); currentPath.erase(i++);
} }
@ -105,11 +118,12 @@ void InotifyIndex::run(){
for (p = buf; p < buf + numRead;) { for (p = buf; p < buf + numRead;) {
struct inotify_event * event = reinterpret_cast<inotify_event *>(p); struct inotify_event * event = reinterpret_cast<inotify_event *>(p);
qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask;
if(event->name[0] != '.'){ if(event->name[0] != '.'){
qDebug() << QString(currentPath[event->wd] + '/' + event->name);
//传创建或移动过来的文件路径 // switch (event->mask) {
if((event->mask & IN_CREATE) | (event->mask & IN_MOVED_TO)){ if (event->mask & IN_CREATE){
//添加监视要先序遍历先添加top节点
if (event->mask & IN_ISDIR){ if (event->mask & IN_ISDIR){
AddWatch(currentPath[event->wd] + '/' + event->name); AddWatch(currentPath[event->wd] + '/' + event->name);
this->setPath(currentPath[event->wd] + '/' + event->name); this->setPath(currentPath[event->wd] + '/' + event->name);
@ -118,7 +132,6 @@ void InotifyIndex::run(){
/*--------------------------------*/ /*--------------------------------*/
// IndexGenerator::getInstance()->creatAllIndex(QQueue<QVector<QString>>(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0"))); // IndexGenerator::getInstance()->creatAllIndex(QQueue<QVector<QString>>(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0")));
qDebug() << QString(currentPath[event->wd] + '/' + event->name);
indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0")); indexQueue->enqueue(QVector<QString>() << QString(event->name) << QString(currentPath[event->wd] + '/' + event->name) << QString((event->mask & IN_ISDIR) ? "1" : "0"));
IndexGenerator::getInstance()->creatAllIndex(indexQueue); IndexGenerator::getInstance()->creatAllIndex(indexQueue);
indexQueue->clear(); indexQueue->clear();
@ -127,18 +140,134 @@ void InotifyIndex::run(){
contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name)); contentIndexQueue->enqueue(QString(currentPath[event->wd] + '/' + event->name));
IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue); IndexGenerator::getInstance()->creatAllIndex(contentIndexQueue);
contentIndexQueue->clear(); 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){ if (event->mask & IN_ISDIR){
RemoveWatch(currentPath[event->wd] + '/' + event->name); 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>() << 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>() << 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>() << 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<QString>>(QVector<QString>() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0")));
// indexQueue->enqueue(QVector<QString>() << 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>() << 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; p += sizeof(struct inotify_event) + event->len;
} }
} }

View File

@ -7,6 +7,7 @@
#include "index-generator.h" #include "index-generator.h"
#include "traverse_bfs.h" #include "traverse_bfs.h"
#include "ukui-search-qdbus.h" #include "ukui-search-qdbus.h"
#include "global-settings.h"
#define BUF_LEN 1024 #define BUF_LEN 1024
class InotifyIndex; class InotifyIndex;