1

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.

5
  • 1
    Why do you want to avoid the recursive solution using inotify, though? In fact implementing it would not be that much trouble for you. Commented 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. Commented 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. Commented 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. Commented Aug 30, 2014 at 12:31
  • Thanks Luis Colorado Commented Aug 30, 2014 at 12:34

3 Answers 3

2

To monitor recursively a directory you have to:

  1. Create an inotify(7) object with inotify_init(2) or inotify_init2(2).
  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).
  3. 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)

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any other less costly method to monitor directory recursively in Linux.
I think the one described in the inotify(7) interface is the by far less costly method you'll get. Just rethink if you need to monitor all the files and directories in that hierarchy. Mimic the behaviour of graphical file managers, as they only check the open viewports with an interface to the user, and switch each time the user changes the view.
1

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

Thanks for the reply. Is there any other method that will be less costly in terms of memory or any other subsystem like inotify that i can use to monitor directory changes recursively.
0

Probably you want to mix inotify and dnotify to achieve that.

1 Comment

dnotify is an older interface and is not recommended anymore. You can use it, but you'll not get further with it than with inotify.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.