From fc283322b7f4a4c08585dca4044bc51f55b1e4e8 Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Fri, 22 Jan 2021 17:15:43 +0800 Subject: [PATCH] Removed inotify on home and modified index status --- libsearch/file-utils.cpp | 2 +- libsearch/file-utils.h | 6 ++-- libsearch/index/first-index.cpp | 6 ++-- libsearch/index/index-generator.cpp | 6 ++++ libsearch/index/inotify-index.cpp | 54 ++++++++++++++--------------- src/main.cpp | 28 +++++++++++++-- src/settings-widget.cpp | 6 ++-- 7 files changed, 70 insertions(+), 38 deletions(-) diff --git a/libsearch/file-utils.cpp b/libsearch/file-utils.cpp index 557cc52..8f84e84 100644 --- a/libsearch/file-utils.cpp +++ b/libsearch/file-utils.cpp @@ -15,7 +15,7 @@ size_t FileUtils::_max_index_count = 0; size_t FileUtils::_current_index_count = 0; -unsigned short FileUtils::_index_status = INITIAL_STATE; +unsigned short FileUtils::_index_status = 0; QMap FileUtils::map_chinese2pinyin = QMap(); FileUtils::FileUtils() diff --git a/libsearch/file-utils.h b/libsearch/file-utils.h index 735b4c8..936835b 100644 --- a/libsearch/file-utils.h +++ b/libsearch/file-utils.h @@ -12,9 +12,9 @@ #include #include #include "libsearch_global.h" -#define INITIAL_STATE 0 -#define CREATING_INDEX 1 -#define FINISH_CREATING_INDEX 2 +//#define INITIAL_STATE 0 +//#define CREATING_INDEX 1 +//#define FINISH_CREATING_INDEX 2 #define UKUI_SEARCH_PIPE_PATH "/tmp/ukuisearch" diff --git a/libsearch/index/first-index.cpp b/libsearch/index/first-index.cpp index 52e341a..3d7b70a 100644 --- a/libsearch/index/first-index.cpp +++ b/libsearch/index/first-index.cpp @@ -106,13 +106,14 @@ void FirstIndex::run(){ // this->p_indexGenerator->creatAllIndex(this->q_content_index); + ++FileUtils::_index_status; + pid_t pid; pid = fork(); if(pid == 0) { prctl(PR_SET_PDEATHSIG, SIGKILL); prctl(PR_SET_NAME,"first-index"); - FileUtils::_index_status = CREATING_INDEX; QSemaphore sem(5); QMutex mutex1, mutex2, mutex3; mutex1.lock(); @@ -122,6 +123,7 @@ void FirstIndex::run(){ QtConcurrent::run([&](){ sem.acquire(1); mutex1.unlock(); + this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); this->Traverse(); FileUtils::_max_index_count = this->q_index->length(); sem.release(5); @@ -181,6 +183,7 @@ void FirstIndex::run(){ else { waitpid(pid,NULL,0); + --FileUtils::_index_status; } int retval = write(fifo_fd, buffer, strlen(buffer)); @@ -190,7 +193,6 @@ void FirstIndex::run(){ } printf("write data ok!\n"); - FileUtils::_index_status = FINISH_CREATING_INDEX; //quit() is shit!!! // return; diff --git a/libsearch/index/index-generator.cpp b/libsearch/index/index-generator.cpp index faad57d..734410f 100644 --- a/libsearch/index/index-generator.cpp +++ b/libsearch/index/index-generator.cpp @@ -42,6 +42,7 @@ bool IndexGenerator::setIndexdataPath() //文件名索引 bool IndexGenerator::creatAllIndex(QQueue > *messageList) { +// FileUtils::_index_status |= 0x1; HandlePathList(messageList); try { @@ -67,8 +68,10 @@ bool IndexGenerator::creatAllIndex(QQueue > *messageList) qWarning()<<"creatAllIndex fail!"<setValue(INDEX_DATABASE_STATE,"1"); +// FileUtils::_index_status &= ~0x1; assert(false); } +// FileUtils::_index_status &= ~0x1; _doc_list_path->clear(); delete _doc_list_path; _doc_list_path = nullptr; @@ -77,6 +80,7 @@ bool IndexGenerator::creatAllIndex(QQueue > *messageList) //文件内容索引 bool IndexGenerator::creatAllIndex(QQueue *messageList) { +// FileUtils::_index_status |= 0x2; HandlePathList(messageList); int size = _doc_list_content->size(); if(!size == 0) @@ -97,8 +101,10 @@ bool IndexGenerator::creatAllIndex(QQueue *messageList) { qWarning()<<"creat content Index fail!"<setValue(CONTENT_INDEX_DATABASE_STATE,"1"); +// FileUtils::_index_status &= ~0x2; assert(false); } +// FileUtils::_index_status &= ~0x2; _doc_list_content->clear(); delete _doc_list_content; _doc_list_content = nullptr; diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index 6e97091..71c845f 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -1,23 +1,5 @@ #include "inotify-index.h" -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(INOTIFY_NORMAL_EXIT, "2"); - - - qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); - qDebug() << "contentIndexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString(); - _exit(0); - -// InotifyIndex::getInstance("/home")->~InotifyIndex(); - - //wait linux kill this thread forcedly -// while (true); -} - InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path) { /*-------------ukuisearchdbus Test start-----------------*/ @@ -33,7 +15,8 @@ InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path) m_fd = inotify_init(); qDebug() << "m_fd----------->" <AddWatch("/home"); + this->AddWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); + this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); this->Traverse(); @@ -123,8 +106,6 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ // 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); - FileUtils::_index_status = CREATING_INDEX; - // switch (event->mask) { if (event->mask & IN_CREATE){ if (event->mask & IN_ISDIR){ @@ -265,7 +246,6 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ // } // } /*--------------------------------*/ - FileUtils::_index_status = FINISH_CREATING_INDEX; } next: p += sizeof(struct inotify_event) + event->len; @@ -308,18 +288,31 @@ void InotifyIndex::run(){ } unlink(UKUI_SEARCH_PIPE_PATH); - qDebug() << "sigset start!"; - sigset( SIGTERM, handler); - qDebug() << "sigset end!"; - char buf[BUF_LEN] __attribute__((aligned(8))); ssize_t numRead; for (;;) { /* Read events forever */ +read: numRead = read(m_fd, buf, BUF_LEN); + char * tmp = const_cast(buf); + + for (; tmp < buf + numRead;) { + struct inotify_event * event = reinterpret_cast(tmp); + // qDebug() << "Read Event: " << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd << event->mask; + if(event->name[0] != '.'){ + goto fork; + } + tmp += sizeof(struct inotify_event) + event->len; + + } + goto read; + +fork: + ++FileUtils::_index_status; + pid_t pid; pid = fork(); if(pid == 0) @@ -338,23 +331,28 @@ void InotifyIndex::run(){ liveTime->setInterval(60000); liveTime->start(); + //I don't know how to use QTimer, wish someone can fix it! + //MouseZhangZh + // connect(liveTime, &QTimer::timeout, [ = ](){ //// _exit(0); // *b_timeout = 1; // }); for (;;){ +// qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime(); numRead = read(m_fd, buf, BUF_LEN); - liveTime->stop(); this->eventProcess(buf, numRead); if (liveTime->remainingTime() < 1){ + qDebug() << "liveTime->remainingTime():" << liveTime->remainingTime(); _exit(0); } - liveTime->start(); } } else if (pid > 0){ memset(buf, 0x00, BUF_LEN); waitpid(pid, NULL, 0); + + --FileUtils::_index_status; } else{ assert(false); diff --git a/src/main.cpp b/src/main.cpp index 6878115..13aaa9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,6 +34,25 @@ #include "xatom-helper.h" +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(INOTIFY_NORMAL_EXIT, "2"); + + + qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString(); + qDebug() << "contentIndexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString(); + _exit(0); + +// InotifyIndex::getInstance("/home")->~InotifyIndex(); + + //wait linux kill this thread forcedly +// while (true); +} + + void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QByteArray localMsg = msg.toLocal8Bit(); @@ -126,9 +145,10 @@ int main(int argc, char *argv[]) }*/ + //here need to be modified /*-------------ukuisearchdbus Test start-----------------*/ -// UkuiSearchQDBus usQDBus; -// usQDBus.setInotifyMaxUserWatches(); + UkuiSearchQDBus usQDBus; + usQDBus.setInotifyMaxUserWatches(); /*-------------ukuisearchdbus Test End-----------------*/ @@ -206,6 +226,10 @@ int main(int argc, char *argv[]) // InotifyIndex ii("/home"); ii->start(); + qDebug() << "sigset start!"; + sigset( SIGTERM, handler); + qDebug() << "sigset end!"; + return app.exec(); } diff --git a/src/settings-widget.cpp b/src/settings-widget.cpp index d37a8b2..ce75d5d 100644 --- a/src/settings-widget.cpp +++ b/src/settings-widget.cpp @@ -209,7 +209,8 @@ void SettingsWidget::clearLayout(QLayout * layout) { */ void SettingsWidget::refreshIndexState() { - if (FileUtils::_index_status == CREATING_INDEX) { +// qDebug()<<"FileUtils::_index_status: "<setIndexState(true); } else { this->setIndexState(false); @@ -217,7 +218,8 @@ void SettingsWidget::refreshIndexState() m_indexNumLabel->setText(QString("%1/%2").arg(QString::number(FileSearcher::getCurrentIndexCount())).arg(QString::number(FileUtils::_max_index_count))); m_timer = new QTimer; connect(m_timer, &QTimer::timeout, this, [ = ]() { - if (FileUtils::_index_status == CREATING_INDEX) { + qDebug()<<"FileUtils::_index_status: "<setIndexState(true); } else { this->setIndexState(false);