From f06e9e3614a9d0cd7993c47e3f53dc8c981c67b8 Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Fri, 25 Dec 2020 20:06:20 +0800 Subject: [PATCH 1/2] refact traverse and inotify --- index/inotify.cpp | 151 +++++++++++++++++++++++++++++++++++++++++ index/inotify.h | 35 ++++++++++ index/traverse_bfs.cpp | 29 ++++++++ index/traverse_bfs.h | 20 ++++++ 4 files changed, 235 insertions(+) create mode 100644 index/inotify.cpp create mode 100644 index/inotify.h create mode 100644 index/traverse_bfs.cpp create mode 100644 index/traverse_bfs.h diff --git a/index/inotify.cpp b/index/inotify.cpp new file mode 100644 index 0000000..82d58c2 --- /dev/null +++ b/index/inotify.cpp @@ -0,0 +1,151 @@ +#include "inotify.h" + +InotifyManagerRefact::InotifyManagerRefact(const QString& path) : Traverse_BFS(path) +{ +// dirPath = new QMap(); + m_fd = inotify_init(); + qDebug() << "m_fd----------->" <mlm = new MessageListManager(); + return; + +} + +InotifyManagerRefact::~InotifyManagerRefact(){ + delete this->mlm; + this->mlm = nullptr; +// delete dirPath; +// dirPath = nullptr; +} + +void InotifyManagerRefact::DoSomething(const QFileInfo& fileInfo){ + this->mlm->AddMessage(QVector() << fileInfo.fileName() << fileInfo.absoluteFilePath() << QString(bool((fileInfo.isDir())))); + if(fileInfo.isDir()){ + this->AddWatch(fileInfo.absoluteFilePath()); + } +} + +bool InotifyManagerRefact::AddWatch(const QString &path){ + //m_fd = inotify_init(); +// qDebug() << "m_fd: " <::Iterator i = currentPath.begin(); i != currentPath.end();){ + if (i.value().length() > path.length()){ + if (i.value().mid(0, path.length()) == path){ + qDebug() << i.value(); + /*--------------------------------*/ + //在此调用删除索引 +// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(path)); + /*--------------------------------*/ + currentPath.erase(i++); + } + else{ + i++; + } + } + else{ + i++; + } + } + qDebug() << path; + //这个貌似不用删,先mark一下 + //currentPath.remove(currentPath.key(path)); + return true; +} + +void InotifyManagerRefact::run(){ + + char * p; + char buf[BUF_LEN] __attribute__((aligned(8))); + + ssize_t numRead; + + for (;;) { /* Read events forever */ + numRead = read(m_fd, buf, BUF_LEN); + if (numRead == 0) { + qDebug() << "read() from inotify fd returned 0!"; + } + if (numRead == -1) { + qDebug() << "read"; + } + qDebug() << "Read " << numRead << " bytes from inotify fd"; + + /* Process all of the events in buffer returned by read() */ + + for (p = buf; p < buf + numRead;) { + struct inotify_event * event = reinterpret_cast(p); + if(event->name[0] != '.'){ +// if(true){ + //这个位运算不要在意,只是懒得把文件夹、文件和事件排列组合了,只是看一下事件的类型 + qDebug() << "Read Event: " << num2string[(event->mask & 0x0000ffff)] << currentPath[event->wd] << QString(event->name) << event->cookie << event->wd; + //num2string[event->mask & 0x0000ffff] +// IndexGenerator::getInstance()->creatAllIndex(new QStringList(currentPath[event->wd] + event->name)); + + /*--------------------------------*/ + + //传创建或移动过来的文件路径 + if((event->mask & IN_CREATE) | (event->mask & IN_MOVED_TO)){ + //添加监视要先序遍历,先添加top节点 + if (event->mask & IN_ISDIR){ + AddWatch(currentPath[event->wd] + '/' + event->name); + this->setPath(currentPath[event->wd] + '/' + event->name); + Traverse(); + } +// else { + + //IndexGenerator::getInstance()->creatAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); + this->mlm->AddMessage(QVector() << event->name << (currentPath[event->wd] + '/' + event->name) << QString(bool((event->mask & IN_ISDIR)))); + this->mlm->SendMessage(); +// } + } + else if((event->mask & IN_DELETE) | (event->mask & IN_MOVED_FROM)){ + if (event->mask & IN_ISDIR){ + RemoveWatch(currentPath[event->wd] + '/' + event->name); + } +// else { + //这里调用删除索引 + this->mlm->AddMessage(QVector() << event->name << (currentPath[event->wd] + '/' + event->name) << QString(bool((event->mask & IN_ISDIR)))); + this->mlm->SendDeleteMessage(); +// IndexGenerator::getInstance()->deleteAllIndex(new QStringList(currentPath[event->wd] + '/' + event->name)); +// } + } + /*--------------------------------*/ + } + p += sizeof(struct inotify_event) + event->len; + } + } +} diff --git a/index/inotify.h b/index/inotify.h new file mode 100644 index 0000000..398ffdb --- /dev/null +++ b/index/inotify.h @@ -0,0 +1,35 @@ +#ifndef INOTIFY_H +#define INOTIFY_H + +#include +#include +#include +#include +#include "traverse_bfs.h" +#include "messagelist-manager.h" +#define BUF_LEN 1024 + +class InotifyManagerRefact : public QThread, public Traverse_BFS +{ + Q_OBJECT +public: + explicit InotifyManagerRefact(const QString&); + ~InotifyManagerRefact(); + + bool AddWatch(const QString&); + bool RemoveWatch(const QString&); + virtual void DoSomething(const QFileInfo &) final; +Q_SIGNALS: +protected: + void run() override; +private: + QString *m_watch_path; + int m_fd; + QMap currentPath; + QMap num2string; + MessageListManager* mlm; + + QMap* dirPath; +}; + +#endif // INOTIFY_H diff --git a/index/traverse_bfs.cpp b/index/traverse_bfs.cpp new file mode 100644 index 0000000..b554f2e --- /dev/null +++ b/index/traverse_bfs.cpp @@ -0,0 +1,29 @@ +#include "traverse_bfs.h" + +Traverse_BFS::Traverse_BFS(const QString& path) +{ + this->path = path; +} + +void Traverse_BFS::Traverse(){ + 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()){ + bfs.enqueue(i.absoluteFilePath()); + } + DoSomething(i); + } + } +} + +void Traverse_BFS::setPath(const QString& path){ + this->path = path; +} diff --git a/index/traverse_bfs.h b/index/traverse_bfs.h new file mode 100644 index 0000000..0ef67a4 --- /dev/null +++ b/index/traverse_bfs.h @@ -0,0 +1,20 @@ +#ifndef TRAVERSE_BFS_H +#define TRAVERSE_BFS_H + + +#include +#include +#include + +class Traverse_BFS +{ +public: + explicit Traverse_BFS(const QString&); + void Traverse(); + virtual void DoSomething(const QFileInfo&) = 0; + void setPath(const QString&); +private: + QString path = "/home"; +}; + +#endif // TRAVERSE_BFS_H From 0a48b42a84a1f117f7d745a08dddbf3046a210ab Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Fri, 25 Dec 2020 20:08:26 +0800 Subject: [PATCH 2/2] change message list data struct --- index/index-generator.cpp | 2 +- index/index.pri | 8 ++++++-- index/messagelist-manager.cpp | 4 ++-- index/messagelist-manager.h | 6 ++++-- index/test-Inotify-Manager.cpp | 14 +++++++++++++- 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/index/index-generator.cpp b/index/index-generator.cpp index b5d2f20..000fc4e 100644 --- a/index/index-generator.cpp +++ b/index/index-generator.cpp @@ -103,7 +103,7 @@ void IndexGenerator::HandlePathList(QStringList *pathlist) auto info = new QFileInfo(list->at(i)); //提取文件名并用空格分割,同时去除'.' QString filename = info->fileName(); - QString index_test = filename.replace(".","").replace(""," "); + QString index_test = filename.replace(".","").replace("+", "%2B").replace(""," "); QString pinyin_test = chineseCharactersToPinyin::find(filename.replace(".", "")).replace("", " "); diff --git a/index/index.pri b/index/index.pri index e388cf2..175a493 100644 --- a/index/index.pri +++ b/index/index.pri @@ -4,12 +4,16 @@ HEADERS += \ $$PWD/chinesecharacterstopinyin.h \ $$PWD/index-generator.h \ $$PWD/inotify-manager.h \ \ - $$PWD/messagelist-manager.h + $$PWD/inotify.h \ + $$PWD/messagelist-manager.h \ + $$PWD/traverse_bfs.h SOURCES += \ $$PWD/chinesecharacterstopinyin.cpp \ $$PWD/index-generator.cpp \ $$PWD/inotify-manager.cpp \ + $$PWD/inotify.cpp \ $$PWD/messagelist-manager.cpp \ - $$PWD/test-Inotify-Manager.cpp + $$PWD/test-Inotify-Manager.cpp \ + $$PWD/traverse_bfs.cpp diff --git a/index/messagelist-manager.cpp b/index/messagelist-manager.cpp index 39e6eb8..979cd38 100644 --- a/index/messagelist-manager.cpp +++ b/index/messagelist-manager.cpp @@ -27,8 +27,8 @@ MessageListManager::~MessageListManager(){ this->indexGeneratorThread = nullptr; } -void MessageListManager::AddMessage(const QString& path){ - this->messageList->append(path); +void MessageListManager::AddMessage(const QVector& pathVec){ + this->messageList->append(pathVec); if (static_cast(this->messageList->length()) >= this->length){ this->SendMessage(); } diff --git a/index/messagelist-manager.h b/index/messagelist-manager.h index e078bf5..909e496 100644 --- a/index/messagelist-manager.h +++ b/index/messagelist-manager.h @@ -11,13 +11,15 @@ public: explicit MessageListManager(); ~MessageListManager(); - void AddMessage(const QString&); + void AddMessage(const QVector&); bool SendMessage(); bool SendDeleteMessage(); void SetAutoSendMessageLength(const size_t&); private: - QStringList* messageList; +// QStringList* messageList; + QList>* messageList; + size_t length = 0; IndexGenerator* ig; QThread* indexGeneratorThread; diff --git a/index/test-Inotify-Manager.cpp b/index/test-Inotify-Manager.cpp index a6e2a5d..6f7cbc5 100644 --- a/index/test-Inotify-Manager.cpp +++ b/index/test-Inotify-Manager.cpp @@ -3,6 +3,7 @@ #include "src/mainwindow.h" #include "inotify-manager.h" #include "chinesecharacterstopinyin.h" +#include "inotify.h" void testTraverse(void){ /*-------------Inotify Test Start---------------*/ @@ -18,13 +19,24 @@ void testTraverse(void){ /*-------------PinyinSearch Test Start---------------*/ QTime t1 = QTime::currentTime(); - QString test("gongzuo"); + QString test("test"); qDebug() << IndexGenerator::IndexSearch(test); QTime t2 = QTime::currentTime(); qDebug() << t1; qDebug() << t2; /*-------------PinyinSearch Test End-----------------*/ + /*-------------InotyifyRefact Test Start---------------*/ +// QTime t1 = QTime::currentTime(); +// InotifyManagerRefact* imr = new InotifyManagerRefact("/home"); +// imr->AddWatch("/home"); +// imr->setPath("/home"); +// imr->Traverse(); +// QTime t2 = QTime::currentTime(); +// qDebug() << t1; +// qDebug() << t2; + /*-------------InotyifyRefact Test End-----------------*/ + exit(0);