From f06e9e3614a9d0cd7993c47e3f53dc8c981c67b8 Mon Sep 17 00:00:00 2001 From: zhangzihao Date: Fri, 25 Dec 2020 20:06:20 +0800 Subject: [PATCH] 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