I want to make an C++ application that will monitor file changes in its directory or its sub-directories. I know inotify can monitor single level directory changes and i need to manually add a watch for each sub-directory to detect changes in the sub-directory. I need to know if there is any other way to recursively monitor changes in a directory in Linux other than inotify.
- 1Why do you want to avoid the recursive solution using inotify, though? In fact implementing it would not be that much trouble for you.Seyf– Seyf2014-08-27 10:45:46 +00:00Commented Aug 27, 2014 at 10:45
- One easy thing to forget is that as subdirectories are added and removed, you must keep adding and removing the inotifies on them.MSalters– MSalters2014-08-27 11:06:03 +00:00Commented Aug 27, 2014 at 11:06
- To implement that thing in kernel means when a dir/file is modified, to look up in the hierarchy to see if there are recursive watching points to be notified to perhaps not find any. That's a waste of kernel time and resources, and that's the reason it has not been implemented in kernel code.... But you are registering for that huge quantity of directories just once, and you have to recurse only once to register them all. Otherwise, you can modify your kernel to do things this way. You must think where are the resources being wasted also. The kernel also computes for timing resources.Luis Colorado– Luis Colorado2014-08-29 05:49:47 +00:00Commented Aug 29, 2014 at 5:49
- Thanks for the reply Seyfülislam Özdemir. I am not trying to avoid any recursive solution but using inotify recursively will be costly. I just trying to find the most cost efficient way to monitor a directory recursively.rex– rex2014-08-30 12:31:41 +00:00Commented Aug 30, 2014 at 12:31
- Thanks Luis Coloradorex– rex2014-08-30 12:34:58 +00:00Commented Aug 30, 2014 at 12:34
3 Answers
To monitor recursively a directory you have to:
- Create an inotify(7) object with inotify_init(2) or inotify_init2(2).
- Descend recursively on your directory, using inotify_add_watch(2) for all the nodes you want to be notified on (add the watch for the directory itself before scanning, or you'll lose events ---se below).
- Wait for events to come on the inotify descriptor you have received.
Take into account that directory creation forces you to possible rescan of directory contents, as you can get an event for a new subdirectory on a watched directory, but files can be created on it before you had a chance to add watches for the recently created directory, so you will not be informed of that files creation.
For this last reason, you'll need to consider putting all things in a subroutine and to call it each time a new directory gets created. This way perhaps you'll scan files for which you'll receive creation events, but the other side you can lost events.
Also, you must prepare yourshelf to do a complete tree rescan in case you lose some events (in a full queue overflow).
And believe me, this is by far more efficient than to do it the classic way. And you can bypass short lived files between rescans.
The reason there's not a recursive solution to this problem has been pointed above in one comment (You'll need the kernel to do the search for you, even if you are not interested on it)
2 Comments
Yes there is the old classic way: Simply check the directory (recursively) in intervals. Store a list of all files and directories and their modification dates. Then it's easy to see if you have a new file/directory, if one has been removed, or if otherwise modified.
It is time consuming though, and you need to store data about every file and directory, so if you try to do it on the root directory you will use a lot of memory or storage.
1 Comment
Probably you want to mix inotify and dnotify to achieve that.