简体   繁体   English

监视文件完整性

[英]Monitoring file integrity

I would like to write a simple application that tells me when a file has been modified. 我想编写一个简单的应用程序,告诉我文件何时被修改。

Does the <QFileSystemWatcher> class only monitor changes when the program is running? <QFileSystemWatcher>类仅在程序运行时才监视更改吗?

If so, are there any other classes that I can use for file integrity monitoring? 如果是这样,还有其他可用于文件完整性监视的类吗?

You could run md5sum, etc. with QProcess initially and then for the changed signals and compare. 您可以首先使用QProcess运行md5sum等,然后针对更改后的信号进行比较。

The alternative is to read all the file in or mmap and create your hash with QCryptoGraphicHash. 另一种方法是读取in或mmap中的所有文件,并使用QCryptoGraphicHash创建您的哈希。

Either way, you would do this initially and then in the signal handlers, aka slots once the connection is properly made in your QObject subclass. 无论哪种方式,一旦在QObject子类中正确建立连接,您都将首先执行此操作,然后在信号处理程序(也称为插槽)中执行此操作。

#include <QObject>
#include <QFileSystemWatcher>

class MyClass : public QObject
{
    Q_OBJECT
    public:
        explicit MyClass(QObject *parent = Q_NULLPTR)
            : QObject(parent)
        {
            // ...
            connect(m_fileSystemWatcher, SIGNAL(fileChanged(const QString&)), SLOT(checkIntegrity(const QString&)));
            // ...
        }

    public slots:
        void checkIntegrity(const QString &path)
        {
            // 1a. Use QProcess with an application like md5sum/sha1sum
            // OR
            // 1b. Use QFile with readAll() QCryptoGraphicsHash
            // 2. Compare with the previous
            // 3. Set the current to the new
        }

    private:
        QFileSystemWatcher m_fileSystemWatcher;
};

Disclaimer: This is obviously not in any way tested, whatsoever, but I hope it demonstrates the concept. 免责声明:显然,这没有经过任何测试,但我希望它能证明这一概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM