Mr.Manhattan
Mr.Manhattan

Reputation: 5504

C++ / Qt: track filesystem changes

I'm currently trying to implement a c++ program which monitors a folder on the filesystem. On initalizing the application, it scans the Directory and saves some meta information about it.

when something is chenged while the program is active, i can read changes to the folder (for examlpe changing the name of a folder or a file). But i can't track changes to the Directory while the program isn't running. Upon startup i would get

Removed folder X
Added folder Y

instead of

Renamed folder X to Y

is it possible to identify a directory in another way than it's path/name? if yes, how would i gather that information in C++ / Qt ?

Upvotes: 0

Views: 804

Answers (3)

Mohamed Hamzaoui
Mohamed Hamzaoui

Reputation: 344

Like TheDarkNight said, you need to use QFileSystemWatcher to avoid portability and so other problem. But if you want to continue your approach:
In GNU/Linux land, you can check this with inode struct of directory (take care of symbolic link issue). inode struct have an index for example you can get it on shell with:

ls -id /path/to/your/folder

There is an API to access inode. You can google inode struct linux for it.

In Windows garden, you can get file id when accessing handle in the struct BY_HANDLE_FILE_INFORMATION:

nFileIndexHigh The high-order part of a unique identifier that is associated with a file. For more information, see nFileIndexLow. nFileIndexLow The low-order part of a unique identifier that is associated with a file.

Upvotes: 0

MSalters
MSalters

Reputation: 179991

This is filesystem-specific, but generally yes this is possible. FAT is the main exception, I think. But you won't find code for this in the C++ Standard Library or Qt. It's just too unusual, so you'll need OS-specific code if not filesystem-specific.

Upvotes: 0

TheDarkKnight
TheDarkKnight

Reputation: 27621

Rather than reinventing the wheel, you could just use the class QFileSystemWatcher which the Qt docs states: -

The QFileSystemWatcher class provides an interface for monitoring files and directories for modifications

If you want the program to run all the time, then you may want to look at creating a service (in Windows) or daemon (Linux / OSX).

Upvotes: 1

Related Questions