QFileSystemWatcher: Modified files in a directory

Viewed 31

I am trying to keep track of files in a directory. Not only do I want to get notion of new files being created or existing files being deleted, but I also would like to watch for modifications done to existing files (i.e., when the file content is modified).

QFileSystemWatcher allows for watching directories and files. Given a large number of files in the directory it is not feasible to add every file to the watcher (QFileSystemWatcher::addFile()). In return, however, it would be totally sufficient to hinge the modification of a file on its modification time alone.

Now I've set up a little test like this:

#include <QtCore>
#include <QtWidgets>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QFileSystemWatcher *w = new QFileSystemWatcher;
    w->addPath("/tmp/xxx");
    QObject::connect(w, &QFileSystemWatcher::directoryChanged,
        []{ qDebug() << "change"; }
    );

    QFile f("/tmp/xxx/the.file");
    f.open(QFile::WriteOnly);
    f.write("blabla");
    f.setFileTime(QDateTime::fromSecsSinceEpoch(10000), QFile::FileModificationTime);
    f.close();

    QDialog d;
    d.show();
    app.exec();
}

Yet whatever I try within this program (writing to the file, setting the file times) no change is ever detected.

But what strikes me is that if I use touch on the file the change is detected. So what on earth does touch do differently?

Obviously I am running Linux.

0 Answers
Related