inotify-java

Asked by vishalj

can i get notification recursively for directory within a directory

Question information

Language:
English Edit question
Status:
Answered
For:
inotify-java Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Nick Bargnesi (nbargnesi) said :
#1

Yes, you can use the trunk's Inotify class for that.

You can recursively monitor directories by reinvoking addWatch(...) as necessary.

Revision history for this message
Philipp C. Heckel (binwiederhier) said :
#2

Hey there,

Could you give an example?

I'm using this code, but no in-directory events (recursive) occur.
Now if I create a sub-folder of "AAA" called "BBB" (/home/myusername/Folder/AAA/BBB), nothing appears to happen.

Inotify i = new Inotify();
InotifyEventListener e = new InotifyEventListener() {
 @Override
 public void filesystemEventOccurred(InotifyEvent e) {
  System.out.println(e.toString());
 }

 @Override
 public void queueFull(EventQueueFull e) {
  System.out.println("...");
 }

};

int watchDesc = i.addWatch("/home/myusername/Folder/", Event.All);
i.addListener(watchDesc, e);

Thanks for the great project!!

Regards,
Philipp

Revision history for this message
Nick Bargnesi (nbargnesi) said :
#3

In your above example, you're asking to receive all events generated on the '/home/myusername/Folder/' path only.

For example,

 1 import static java.lang.System.*;
 2 import com.den_4.inotify_java.enums.*;
 3 import com.den_4.inotify_java.exceptions.*;
 4 import com.den_4.inotify_java.*;
 5
 6 public class RecursiveTest {
 7
 8 public static void main(String... args) {
 9 try {
10 final Inotify i = new Inotify();
11 InotifyEventListener e = new InotifyEventListener() {
12 @Override
13 public void filesystemEventOccurred(InotifyEvent e) {
14 out.println(e);
15 if (e.aboutDirectory() && e.isCreate()) {
16 try {
17 int wd = i.addWatch(e.getContextualName(), Event.All);
18 i.addListener(wd, this);
19 } catch (InotifyException ie) {
20 out.println(ie);
21 ie.printStackTrace();
22 }
23 }
24 }
25 @Override
26 public void queueFull(EventQueueFull e) {
27 System.out.println("...");
28 }
29 };
30
31 int wd = i.addWatch("/home/myusername/Folder/", Event.All);
32 i.addListener(wd, e);
33
34 while (i.isActive()) {
35 Thread.sleep(1000);
36 }
37
38 } catch (Exception e) {
39 e.printStackTrace();
40 }
41 }
42 }

In the above example, all events are being requested on the '/home/myusername/Folder/' path, line 31. The listener 'e', from line 11, receives inotify events for this directory. If a directory is created (line 15), another watch is added to monitor the subdirectory.

Re-invoking addWatch() is necessary to watch the newly created path. Don't expect to call addWatch("/") and get events on every path on the system!

Can you help with this problem?

Provide an answer of your own, or ask vishalj for more information if necessary.

To post a message you must log in.