diff --git a/libsearch/index/inotify-index.cpp b/libsearch/index/inotify-index.cpp index c5b7893..e17fb62 100644 --- a/libsearch/index/inotify-index.cpp +++ b/libsearch/index/inotify-index.cpp @@ -36,7 +36,8 @@ InotifyIndex::InotifyIndex(const QString& path) : Traverse_BFS(path) this->AddWatch(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); this->setPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); - this->Traverse(); +// this->Traverse(); + this->firstTraverse(); GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "0"); @@ -48,10 +49,44 @@ InotifyIndex::~InotifyIndex() IndexGenerator::getInstance()->~IndexGenerator(); } +void InotifyIndex::firstTraverse(){ + QQueue bfs; + bfs.enqueue(this->path); + QFileInfoList list; + QDir dir; + dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot); + dir.setSorting(QDir::DirsFirst); + while (!bfs.empty()) { + dir.setPath(bfs.dequeue()); + list = dir.entryInfoList(); + for (auto i : list){ + if (i.isDir()){ + this->AddWatch(i.absoluteFilePath()); + bfs.enqueue(i.absoluteFilePath()); + } + } + } +} + void InotifyIndex::DoSomething(const QFileInfo& fileInfo){ + qDebug() << fileInfo.fileName() << "-------" << fileInfo.absoluteFilePath(); if(fileInfo.isDir()){ this->AddWatch(fileInfo.absoluteFilePath()); } + QQueue >* tempFile = new QQueue >; + tempFile->enqueue(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(fileInfo.isDir() ? "1" : "0")); + IndexGenerator::getInstance()->creatAllIndex(tempFile); + if (!tempFile) + delete tempFile; + for (auto i : this->targetFileTypeVec){ + if (fileInfo.fileName().endsWith(i)){ + QQueue* tempContent = new QQueue; + tempContent->enqueue(fileInfo.absoluteFilePath()); + IndexGenerator::getInstance()->creatAllIndex(tempContent); + if (!tempContent) + delete tempContent; + } + } } bool InotifyIndex::AddWatch(const QString &path){ @@ -77,8 +112,11 @@ bool InotifyIndex::RemoveWatch(const QString &path){ assert(ret == 0); for (QMap::Iterator i = currentPath.begin(); i != currentPath.end();){ +// qDebug() << i.value(); if (i.value().length() > path.length()){ - if (i.value().mid(0, path.length()) == path){ +// if (i.value().mid(0, path.length()) == path){ +// if (path.startsWith(i.value())){ + if (i.value().startsWith(path)){ qDebug() << "remove path: " << i.value(); ret = inotify_rm_watch(m_fd, currentPath.key(path)); if (ret){ @@ -89,6 +127,7 @@ bool InotifyIndex::RemoveWatch(const QString &path){ // assert(ret == 0); /*--------------------------------*/ //在此调用删除索引 + qDebug() << i.value(); IndexGenerator::getInstance()->deleteAllIndex(new QStringList(i.value())); /*--------------------------------*/ currentPath.erase(i++); @@ -103,16 +142,11 @@ bool InotifyIndex::RemoveWatch(const QString &path){ } // qDebug() << path; //这个貌似不用删,先mark一下 - //currentPath.remove(currentPath.key(path)); + currentPath.remove(currentPath.key(path)); return true; } void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ - // qDebug() << "Read " << numRead << " bytes from inotify fd"; - - /* Process all of the events in buffer returned by read() */ - - QQueue>* indexQueue = new QQueue>(); QQueue* contentIndexQueue = new QQueue(); @@ -122,11 +156,12 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ for (; 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; + 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); // switch (event->mask) { if (event->mask & IN_CREATE){ + qDebug() << "IN_CREATE"; if (event->mask & IN_ISDIR){ AddWatch(currentPath[event->wd] + '/' + event->name); setPath(currentPath[event->wd] + '/' + event->name); @@ -151,6 +186,7 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ if ((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){ + qDebug() << "IN_DELETE or IN_MOVED_FROM"; if (event->mask & IN_ISDIR){ RemoveWatch(currentPath[event->wd] + '/' + event->name); } @@ -161,8 +197,9 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ if (event->mask & IN_MODIFY){ + qDebug() << "IN_MODIFY"; if (!(event->mask & IN_ISDIR)){ - IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); +// 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(); @@ -180,9 +217,10 @@ void InotifyIndex::eventProcess(const char* buf, ssize_t tmp){ if (event->mask & IN_MOVED_TO){ + qDebug() << "IN_MOVED_TO"; if (event->mask & IN_ISDIR){ RemoveWatch(currentPath[event->wd] + '/' + event->name); - IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); +// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); AddWatch(currentPath[event->wd] + '/' + event->name); setPath(currentPath[event->wd] + '/' + event->name); Traverse(); @@ -382,6 +420,7 @@ fork: fflush(stdout); assert(false); } + qDebug() << "Read " << numRead << " bytes from inotify fd"; this->eventProcess(buf, numRead); } } diff --git a/libsearch/index/inotify-index.h b/libsearch/index/inotify-index.h index cf74af9..ee381e3 100644 --- a/libsearch/index/inotify-index.h +++ b/libsearch/index/inotify-index.h @@ -52,6 +52,7 @@ public: virtual void DoSomething(const QFileInfo &) final; void eventProcess(const char*, ssize_t); + void firstTraverse(); protected: void run() override; private: diff --git a/libsearch/index/traverse_bfs.h b/libsearch/index/traverse_bfs.h index 21f1e72..b3c414c 100644 --- a/libsearch/index/traverse_bfs.h +++ b/libsearch/index/traverse_bfs.h @@ -34,10 +34,10 @@ public: void setPath(const QString&); protected: explicit Traverse_BFS(const QString&); + QString path = "/home"; private: Traverse_BFS(const Traverse_BFS&) = delete; void operator=(const Traverse_BFS&) = delete; - QString path = "/home"; }; #endif // TRAVERSE_BFS_H