Qt源码解析-QFileSystemWatcher源码剖析(Linux).md

公众号:Qt那些事儿

Qt源码解析-QFileSystemWatcher源码剖析(Linux).md

简介

QFileSystemWatcher的作用是监视本地文件夹的变化以及文件的变化。

概述

QFileSystemWatcher的实现类是QFileSystemWatcherPrivate。 其中QFileSystemWatcherPrivate中的关键成员变量QFileSystemWatcherEngine用于监视目录以及文件的变化,发送信号给QFileystemWatcher。其中QFileSystemWatcherEngine派生了三个类。

class QFileSystemWatcherEngine : public QThread

其派生的子类三种类型分别为

// 这个用于监控Dir的变化
class QDnotifyFileSystemWatcherEngine : public QFileSystemWatcherEngine

// 这个外部没有暴露对应的变化接口,但是检测其它类型的目录变化时我们会用到
class QPollingFileSystemWatcherEngine : public QFileSystemWatcherEngine

// 这个用于检测文件类型的变化
class QInotifyFileSystemWatcherEngine : public QFileSystemWatcherEngine

QInotifyFileSystemWatcherEngine

QInotifyFileSystemWatcherEngine用于监视文件的变化。

// 太长可以忽略,这是详细实现

//media/zhangpf/workspace1/Qt4.8.7/qt-everywhere-opensource-src-4.8.7/src/corelib/io/qfilesystemwatcher_inotify_p.h

#include "qfilesystemwatcher_p.h"
#ifndef QT_NO_FILESYSTEMWATCHER
#include <qhash.h>
#include <qmutex.h>
QT_BEGIN_NAMESPACE
class QInotifyFileSystemWatcherEngine : public QFileSystemWatcherEngine
{
    Q_OBJECT
public:
    ~QInotifyFileSystemWatcherEngine();
    static QInotifyFileSystemWatcherEngine *create(); //单例模式
    void run();
    QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories);
    QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories);
    void stop();
private Q_SLOTS:
    void readFromInotify();
private:
    QInotifyFileSystemWatcherEngine(int fd);
    int inotifyFd;
    QMutex mutex;
    QHash<QString, int> pathToID;
    QHash<int, QString> idToPath;
};
QT_END_NAMESPACE
#endif // QT_NO_FILESYSTEMWATCHER
#endif // QFILESYSTEMWATCHER_INOTIFY_P_H

// cpp
#include <sys/inotify.h>
#endif
QT_BEGIN_NAMESPACE
QInotifyFileSystemWatcherEngine *QInotifyFileSystemWatcherEngine::create()
{
    int fd = -1;
#ifdef IN_CLOEXEC
    fd = inotify_init1(IN_CLOEXEC);
#endif
    if (fd == -1) {
        fd = inotify_init();
        if (fd == -1)
            return 0;
        ::fcntl(fd, F_SETFD, FD_CLOEXEC);
    }
    return new QInotifyFileSystemWatcherEngine(fd);
}
QInotifyFileSystemWatcherEngine::QInotifyFileSystemWatcherEngine(int fd)
    : inotifyFd(fd)
{
    fcntl(inotifyFd, F_SETFD, FD_CLOEXEC);
    moveToThread(this);
}
QInotifyFileSystemWatcherEngine::~QInotifyFileSystemWatcherEngine()
{
    foreach (int id, pathToID)
        inotify_rm_watch(inotifyFd, id < 0 ? -id : id);
    ::close(inotifyFd);
}
void QInotifyFileSystemWatcherEngine::run()
{
    QSocketNotifier sn(inotifyFd, QSocketNotifier::Read, this);   //通过socket来监视文件的变化,替代thread一个很好的方式
    connect(&sn, SIGNAL(activated(int)), SLOT(readFromInotify()));
    (void) exec();
}
QStringList QInotifyFileSystemWatcherEngine::addPaths(const QStringList &paths,
                                                      QStringList *files,
                                                      QStringList *directories)
{
    QMutexLocker locker(&mutex);
    QStringList p = paths;
    QMutableListIterator<QString> it(p);
    while (it.hasNext()) {
        QString path = it.next();
        QFileInfo fi(path);
        bool isDir = fi.isDir();
        if (isDir) {
            if (directories->contains(path))
                continue;
        } else {
            if (files->contains(path))
                continue;
        }
        int wd = inotify_add_watch(inotifyFd,
                                   QFile::encodeName(path),
                                   (isDir
                                    ? (0
                                       | IN_ATTRIB
                                       | IN_MOVE
                                       | IN_CREATE
                                       | IN_DELETE
                                       | IN_DELETE_SELF
                                       )
                                    : (0
                                       | IN_ATTRIB
                                       | IN_MODIFY
                                       | IN_MOVE
                                       | IN_MOVE_SELF
                                       | IN_DELETE_SELF
                                       )));
        if (wd <= 0) {
            perror("QInotifyFileSystemWatcherEngine::addPaths: inotify_add_watch failed");
            continue;
        }
        it.remove();
        int id = isDir ? -wd : wd;
        if (id < 0) {
            directories->append(path);
        } else {
            files->append(path);
        }
        pathToID.insert(path, id);
        idToPath.insert(id, path);
    }
    start();
    return p;
}
QStringList QInotifyFileSystemWatcherEngine::removePaths(const QStringList &paths,
                                                         QStringList *files,
                                                         QStringList *directories)
{
    QMutexLocker locker(&mutex);
    QStringList p = paths;
    QMutableListIterator<QString> it(p);
    while (it.hasNext()) {
        QString path = it.next();
        int id = pathToID.take(path);
        QString x = idToPath.take(id);
        if (x.isEmpty() || x != path)
            continue;
        int wd = id < 0 ? -id : id;
        // qDebug() << "removing watch for path" << path << "wd" << wd;
        inotify_rm_watch(inotifyFd, wd);
        it.remove();
        if (id < 0) {
            directories->removeAll(path);
        } else {
            files->removeAll(path);
        }
    }
    return p;
}
void QInotifyFileSystemWatcherEngine::stop()
{
    quit();
}
void QInotifyFileSystemWatcherEngine::readFromInotify()
{
    //主要是通过unix库函数来获取文件对应的详细信息。再跟addpath实现中缓存下来的信息做对比,来检测文件的变化。
    QMutexLocker locker(&mutex);
    // qDebug() << "QInotifyFileSystemWatcherEngine::readFromInotify";
    int buffSize = 0;
    ioctl(inotifyFd, FIONREAD, (char *) &buffSize);
    QVarLengthArray<char, 4096> buffer(buffSize);
    buffSize = read(inotifyFd, buffer.data(), buffSize);
    char *at = buffer.data();
    char * const end = at + buffSize;
    QHash<int, inotify_event *> eventForId;
    while (at < end) {
        inotify_event *event = reinterpret_cast<inotify_event *>(at);
        if (eventForId.contains(event->wd))
            eventForId[event->wd]->mask |= event->mask;
        else
            eventForId.insert(event->wd, event);
        at += sizeof(inotify_event) + event->len;
    }
    QHash<int, inotify_event *>::const_iterator it = eventForId.constBegin();
    while (it != eventForId.constEnd()) {
        const inotify_event &event = **it;
        ++it;
        // qDebug() << "inotify event, wd" << event.wd << "mask" << hex << event.mask;
        int id = event.wd;
        QString path = idToPath.value(id);
        if (path.isEmpty()) {
            // perhaps a directory?
            id = -id;
            path = idToPath.value(id);
            if (path.isEmpty())
                continue;
        }
        // qDebug() << "event for path" << path;
        if ((event.mask & (IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT)) != 0) {
            pathToID.remove(path);
            idToPath.remove(id);
            inotify_rm_watch(inotifyFd, event.wd);
            if (id < 0)
                emit directoryChanged(path, true);
            else
                emit fileChanged(path, true);
        } else {
            if (id < 0)
                emit directoryChanged(path, false);
            else
                emit fileChanged(path, false);
        }
    }
}
QT_END_NAMESPACE
#endif // QT_NO_FILESYSTEMWATCHER

这是一个单例模式,里边的核心代码其实就是讲的是Inotify相关的函数。其中的关键的点,我已经打上备注。这个类中的主要实现是Linux下的Inotify的使用相关。

Inotify

Inotify简单的来讲是在Linux下监视文件与文件夹的相关机制,本来想自己写这一部分教程的,可是有一篇文章写的太好了,忍不住给大家分享了。
https://www.ibm.com/developerworks/cn/linux/l-inotify/ 看完这一篇文章之后我觉得你对Linux下如何监视文件应该有了解了,甚至可以自己封装一个类给大家用。

文件夹的检测变化实现类 QDnotifyFileSystemWatcherEngine

class QDnotifyFileSystemWatcherEngine : public QFileSystemWatcherEngine
{
    Q_OBJECT
public:
    virtual ~QDnotifyFileSystemWatcherEngine();
    static QDnotifyFileSystemWatcherEngine *create();
    void run();
    QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories);
    QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories);
    void stop();
private Q_SLOTS:
    void refresh(int);
private:
    //这个结构体比较关键
    struct Directory {
        Directory() : fd(0), parentFd(0), isMonitored(false) {}
        Directory(const Directory &o) : path(o.path),
                                        fd(o.fd),
                                        parentFd(o.parentFd),
                                        isMonitored(o.isMonitored),
                                        files(o.files) {}
        QString path;
        int fd;
        int parentFd;
        bool isMonitored;
        //这个结构体也比较关键
        struct File {
            File() : ownerId(0u), groupId(0u), permissions(0u) { }
            File(const File &o) : path(o.path),
                                  ownerId(o.ownerId),
                                  groupId(o.groupId),
                                  permissions(o.permissions),
                                  lastWrite(o.lastWrite) {}
            QString path;
            bool updateInfo();
            uint ownerId;
            uint groupId;
            QFile::Permissions permissions;
            QDateTime lastWrite;
        };
        QList<File> files;
    };
    QDnotifyFileSystemWatcherEngine();
    QMutex mutex;
    QHash<QString, int> pathToFD;
    QHash<int, Directory> fdToDirectory;
    QHash<int, int> parentToFD;
};

//cpp
QDnotifySignalThread::QDnotifySignalThread()
: isExecing(false)
{
    moveToThread(this);
    qt_safe_pipe(qfswd_fileChanged_pipe, O_NONBLOCK);
    struct sigaction oldAction;
    struct sigaction action;
    memset(&action, 0, sizeof(action));
    action.sa_sigaction = qfswd_sigio_monitor;
    action.sa_flags = SA_SIGINFO;
    ::sigaction(SIGIO, &action, &oldAction);
    if (!(oldAction.sa_flags & SA_SIGINFO))
        qfswd_old_sigio_handler = oldAction.sa_handler;
    else
        qfswd_old_sigio_action = oldAction.sa_sigaction;
}
QDnotifySignalThread::~QDnotifySignalThread()
{
    if(isRunning()) {
        quit();
        QThread::wait();
    }
}
bool QDnotifySignalThread::event(QEvent *e)
{
    if(e->type() == QEvent::User) {
        QMutexLocker locker(&mutex);
        isExecing = true;
        wait.wakeAll();
        return true;
    } else {
        return QThread::event(e);
    }
}
void QDnotifySignalThread::startNotify()
{
    // Note: All this fancy waiting for the thread to enter its event
    // loop is to avoid nasty messages at app shutdown when the
    // QDnotifySignalThread singleton is deleted
    start();
    mutex.lock();
    while(!isExecing)
        wait.wait(&mutex);
    mutex.unlock();
}
void QDnotifySignalThread::run()
{
    QSocketNotifier sn(qfswd_fileChanged_pipe[0], QSocketNotifier::Read, this);
    connect(&sn, SIGNAL(activated(int)), SLOT(readFromDnotify()));
    QCoreApplication::instance()->postEvent(this, new QEvent(QEvent::User));
    (void) exec();
}
void QDnotifySignalThread::readFromDnotify()
{
    int fd;
    int readrv = qt_safe_read(qfswd_fileChanged_pipe[0], reinterpret_cast<char*>(&fd), sizeof(int));
    // Only expect EAGAIN or EINTR. Other errors are assumed to be impossible.
    if(readrv != -1) {
        Q_ASSERT(readrv == sizeof(int));
        Q_UNUSED(readrv);
        if(0 == fd)
            quit();
        else
            emit fdChanged(fd);
    }
}
QDnotifyFileSystemWatcherEngine::QDnotifyFileSystemWatcherEngine()
{
    QObject::connect(dnotifySignal(), SIGNAL(fdChanged(int)),
                     this, SLOT(refresh(int)), Qt::DirectConnection);
}
QDnotifyFileSystemWatcherEngine::~QDnotifyFileSystemWatcherEngine()
{
    QMutexLocker locker(&mutex);
    for(QHash<int, Directory>::ConstIterator iter = fdToDirectory.constBegin();
            iter != fdToDirectory.constEnd();
            ++iter) {
        qt_safe_close(iter->fd);
        if(iter->parentFd)
            qt_safe_close(iter->parentFd);
    }
}
QDnotifyFileSystemWatcherEngine *QDnotifyFileSystemWatcherEngine::create()
{
    return new QDnotifyFileSystemWatcherEngine();
}
void QDnotifyFileSystemWatcherEngine::run()
{
    qFatal("QDnotifyFileSystemWatcherEngine thread should not be run");
}
QStringList QDnotifyFileSystemWatcherEngine::addPaths(const QStringList &paths, QStringList *files, QStringList *directories)
{
    QMutexLocker locker(&mutex);
    QStringList p = paths;
    QMutableListIterator<QString> it(p);
    while (it.hasNext()) {
        QString path = it.next();
        QFileInfo fi(path);
        if(!fi.exists()) {
            continue;
        }
        bool isDir = fi.isDir();
        if (isDir && directories->contains(path)) {
            continue; // Skip monitored directories
        } else if(!isDir && files->contains(path)) {
            continue; // Skip monitored files
        }
        if(!isDir)
            path = fi.canonicalPath();
        // Locate the directory entry (creating if needed)
        int fd = pathToFD[path];
        if(fd == 0) {
            QT_DIR *d = QT_OPENDIR(path.toUtf8().constData());
            if(!d) continue; // Could not open directory
            QT_DIR *parent = 0;
            QDir parentDir(path);
            if(!parentDir.isRoot()) {
                parentDir.cdUp();
                parent = QT_OPENDIR(parentDir.path().toUtf8().constData());
                if(!parent) {
                    QT_CLOSEDIR(d);
                    continue;
                }
            }
            fd = qt_safe_dup(::dirfd(d));
            int parentFd = parent ? qt_safe_dup(::dirfd(parent)) : 0;
            QT_CLOSEDIR(d);
            if(parent) QT_CLOSEDIR(parent);
            Q_ASSERT(fd);
            if(::fcntl(fd, F_SETSIG, SIGIO) ||
               ::fcntl(fd, F_NOTIFY, DN_MODIFY | DN_CREATE | DN_DELETE |
                                     DN_RENAME | DN_ATTRIB | DN_MULTISHOT) ||
               (parent && ::fcntl(parentFd, F_SETSIG, SIGIO)) ||
               (parent && ::fcntl(parentFd, F_NOTIFY, DN_DELETE | DN_RENAME |
                                            DN_MULTISHOT))) {
                continue; // Could not set appropriate flags
            }
            Directory dir;
            dir.path = path;
            dir.fd = fd;
            dir.parentFd = parentFd;
            fdToDirectory.insert(fd, dir);
            pathToFD.insert(path, fd);
            if(parentFd)
                parentToFD.insert(parentFd, fd);
        }
        Directory &directory = fdToDirectory[fd];
        if(isDir) {
            directory.isMonitored = true;
        } else {
            Directory::File file;
            file.path = fi.filePath();
            file.lastWrite = fi.lastModified();
            directory.files.append(file);
            pathToFD.insert(fi.filePath(), fd);
        }
        it.remove();
        if(isDir) {
            directories->append(path);
        } else {
            files->append(fi.filePath());
        }
    }
    dnotifySignal()->startNotify();
    return p;
}
QStringList QDnotifyFileSystemWatcherEngine::removePaths(const QStringList &paths, QStringList *files, QStringList *directories)
{
    QMutexLocker locker(&mutex);
    QStringList p = paths;
    QMutableListIterator<QString> it(p);
    while (it.hasNext()) {
        QString path = it.next();
        int fd = pathToFD.take(path);
        if(!fd)
            continue;
        Directory &directory = fdToDirectory[fd];
        bool isDir = false;
        if(directory.path == path) {
            isDir = true;
            directory.isMonitored = false;
        } else {
            for(int ii = 0; ii < directory.files.count(); ++ii) {
                if(directory.files.at(ii).path == path) {
                    directory.files.removeAt(ii);
                    break;
                }
            }
        }
        if(!directory.isMonitored && directory.files.isEmpty()) {
            // No longer needed
            qt_safe_close(directory.fd);
            pathToFD.remove(directory.path);
            fdToDirectory.remove(fd);
        }
        if(isDir) {
            directories->removeAll(path);
        } else {
            files->removeAll(path);
        }
        it.remove();
    }
    return p;
}
void QDnotifyFileSystemWatcherEngine::refresh(int fd)
{
    QMutexLocker locker(&mutex);
    bool wasParent = false;
    QHash<int, Directory>::Iterator iter = fdToDirectory.find(fd);
    if(iter == fdToDirectory.end()) {
        QHash<int, int>::Iterator pIter = parentToFD.find(fd);
        if(pIter == parentToFD.end())
            return;
        iter = fdToDirectory.find(*pIter);
        if (iter == fdToDirectory.end())
            return;
        wasParent = true;
    }
    Directory &directory = *iter;
    if(!wasParent) {
        for(int ii = 0; ii < directory.files.count(); ++ii) {
            Directory::File &file = directory.files[ii];
            if(file.updateInfo()) {
                // Emit signal
                QString filePath = file.path;
                bool removed = !QFileInfo(filePath).exists();
                if(removed) {
                    directory.files.removeAt(ii);
                    --ii;
                }
                emit fileChanged(filePath, removed);
            }
        }
    }
    if(directory.isMonitored) {
        // Emit signal
        bool removed = !QFileInfo(directory.path).exists();
        QString path = directory.path;
        if(removed)
            directory.isMonitored = false;
        emit directoryChanged(path, removed);
    }
    if(!directory.isMonitored && directory.files.isEmpty()) {
        qt_safe_close(directory.fd);
        if(directory.parentFd) {
            qt_safe_close(directory.parentFd);
            parentToFD.remove(directory.parentFd);
        }
        fdToDirectory.erase(iter);
    }
}
void QDnotifyFileSystemWatcherEngine::stop()
{
}
bool QDnotifyFileSystemWatcherEngine::Directory::File::updateInfo()
{
    QFileInfo fi(path);
    QDateTime nLastWrite = fi.lastModified();
    uint nOwnerId = fi.ownerId();
    uint nGroupId = fi.groupId();
    QFile::Permissions nPermissions = fi.permissions();
    if(nLastWrite != lastWrite ||
       nOwnerId != ownerId ||
       nGroupId != groupId ||
       nPermissions != permissions) {
        ownerId = nOwnerId;
        groupId = nGroupId;
        permissions = nPermissions;
        lastWrite = nLastWrite;
        return true;
    } else {
        return false;
    }
}

Dnotify

Dnotify同理,也是使用的Linux的系统函数 /usr/include/unistd.h 主要是这个头文件中的函数。有一些关于文件描述符相关的函数

里边主要监控的是其内部类的相关的信息

    struct Directory {
        Directory() : fd(0), parentFd(0), isMonitored(false) {}
        Directory(const Directory &o) : path(o.path),
                                        fd(o.fd),
                                        parentFd(o.parentFd),
                                        isMonitored(o.isMonitored),
                                        files(o.files) {}
        QString path;
        int fd;
        int parentFd;
        bool isMonitored;
        struct File {
            File() : ownerId(0u), groupId(0u), permissions(0u) { }
            File(const File &o) : path(o.path),
                                  ownerId(o.ownerId),
                                  groupId(o.groupId),
                                  permissions(o.permissions),
                                  lastWrite(o.lastWrite) {}
            QString path;
            bool updateInfo();
            uint ownerId;
            uint groupId;
            QFile::Permissions permissions;
            QDateTime lastWrite;

可以直接看这个结构D需要这四个描述信息

        QString path;  //路径
        int fd;  //文件的描述符
        int parentFd;    //父亲的描述符号
        bool isMonitored; //是否正在监控

其中这四个信息都是通过Linux的库函数与结构体来获取的。 其中遍历文件夹则是使用Qt的QFileInfo来遍历添加paths的信息,存储到其类的成员变量中。

// Directory iteration
#define QT_DIR DIR
#define QT_OPENDIR ::opendir
#define QT_CLOSEDIR ::closedir

Dir下的file需要这些信息

            QString path;
            uint ownerId;
            uint groupId;
            QFile::Permissions permissions;
            QDateTime lastWrite;

现在说一下关键代码

    ret = ::pipe(pipefd);
    if (ret == -1)
        return -1;
    ::fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
    ::fcntl(pipefd[1], F_SETFD, FD_CLOEXEC);
    // set non-block too?
    if (flags & O_NONBLOCK) {
        ::fcntl(pipefd[0], F_SETFL, ::fcntl(pipefd[0], F_GETFL) | O_NONBLOCK);
        ::fcntl(pipefd[1], F_SETFL, ::fcntl(pipefd[1], F_GETFL) | O_NONBLOCK);
    }

其中 pipefd[0]表示读,pipefd[1]表示写,实际上
关键代码在这里。

void QDnotifySignalThread::run()
{
    QSocketNotifier sn(qfswd_fileChanged_pipe[0], QSocketNotifier::Read, this);
    connect(&sn, SIGNAL(activated(int)), SLOT(readFromDnotify()));
    QCoreApplication::instance()->postEvent(this, new QEvent(QEvent::User));
    (void) exec();
}

这段代码实际上是使用QSocketNotifier实时检测出从pip管道中读取有关于文件信息的变化,加到了Qt在Linux下的事件循环中(还记上以前有个老哥写的那个u盘检测工具么?实际上原理跟这个一样,都是通过socket来读取文件描述符的状态来检测其变化)。然后等待其消息通知变化。这样来实时监控文件夹与文件的变化。其中QDnotify大量使用了Unix的库函数,建议有兴趣的可以多读读Unix高级环境编程这本书,可以当个字典来看。我也不一个个解释了。实际上这个类,我读起来也是有点吃力,因为大部分都是Linux的库函数,还是得补补课去看看《Unix环境高级编程》了

总结

这两个类的主要原理是先缓存当前addpath的文件or文件夹的信息,然后再通过socket来实时检测其变化。获取当前的信息与缓存的信息做对比,如果有变化,就发送对应的信号。这样我们就可以检测到文件or文件夹的变化了。

公众号:Qt那些事儿

Qt源码解析-QFileSystemWatcher源码剖析(Linux).md

Qt源码解析-QFileSystemWatcher源码剖析(Linux).md

上一篇:[Go] golang 执行 Linux 系统 command


下一篇:为何放弃Eclipse,选择IntelliJ IDEA,看完终于明白了