1

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?

If so, are there any other classes that I can use for file integrity monitoring?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Quaxton Hale
  • 2,460
  • 5
  • 40
  • 71
  • Yes, it will only monitor when your program is running, but as @LaszloPapp states, you could get a hash of the file(s) and store them, then verify if they've changed when your program next starts up. Alternatively, create a service (Windows) or daemon (OSX / Linux) to run all the time and monitor the file(s). – TheDarkKnight May 20 '14 at 16:27

1 Answers1

1

You could run md5sum, etc. with QProcess initially and then for the changed signals and compare.

The alternative is to read all the file in or mmap and create your hash with QCryptoGraphicHash.

Either way, you would do this initially and then in the signal handlers, a.k.a. slots once the connection is properly made in your QObject subclass.

#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.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Missing MyClass scope for the checkIntegrity function ;O) – TheDarkKnight May 20 '14 at 16:25
  • @Merlin069: I usually write the example for the header inline because if I write the cpp, users tend to ask for issues with the header, so it is more self-contained. I was going to do add the header class boilerplate around, but I got called into a meeting. :P Will do that now. Thanks for the reminder that it is not complete. :) – László Papp May 20 '14 at 16:38