[EXTRAS] inotify extra details
[gnome.gobject-introspection] / extras / inotifytools / example.js
1 #!/usr/bin/seed
2
3 //<script type="text/javascript">
4 // silly test to see how libc works :)
5 // borked - inotify sends a struct via a buffer -- need to use inotify tools!
6 inotifytools = imports.gi.inotifytools;
7 GLib = imports.gi.GLib;
8
9
10
11  
12  
13 if (!inotifytools.initialize()) {
14     Seed.print("Failed to init");
15     Seed.quit();
16 }
17 Seed.print("watch Error?" + GLib.strerror(inotifytools.error()));
18 var events = inotifytools.IN_ACCESS | 
19         inotifytools.IN_MODIFY | 
20         inotifytools.IN_ATTRIB | 
21         inotifytools.IN_CLOSE_WRITE | 
22         inotifytools.IN_CLOSE_NOWRITE | 
23         inotifytools.IN_OPEN | 
24         inotifytools.IN_MOVED_FROM | 
25         inotifytools.IN_MOVED_TO | 
26         inotifytools.IN_DELETE | 
27         inotifytools.IN_CREATE | 
28         inotifytools.IN_DELETE_SELF | 
29         inotifytools.IN_MOVE_SELF;
30 print("EVENTS:"+ events);
31 var dir = String(Seed.argv[2]) 
32 Seed.print("MONITOR: " + dir +"?");
33 var r = inotifytools.watch_recursively(  dir , events);
34
35 Seed.print("GOT " +r );
36 if (!r) {
37         Seed.print("watch Error?" + GLib.strerror(inotifytools.error()));
38     Seed.quit();
39 }
40
41
42 var num_watches = inotifytools.get_num_watches();
43 Seed.print("Watch " + num_watches );
44 var moved_from = '';
45 while (true) {
46     
47     var event = inotifytools.next_event( 0 );
48     Seed.print(event.len);
49     inotifytools.printf(event, "in %w, file %f had event(s): %.e\n");
50     continue;
51     if ( !event ) {
52         if ( !inotifytools.error() ) {
53             Seed.print("Timeout?");
54             Seed.quit();
55         }
56         else if ( inotifytools.error() != 4 ) {
57                                 Seed.print("Error?" + inotifytools.error());
58                                 Seed.quit();
59         } else {
60             continue;
61                         
62         }
63     }
64     if ( moved_from.length && !(event.mask & inotifytools.IN_MOVED_TO) ) {
65         if ( !inotifytools.remove_watch_by_filename( moved_from ) ) {
66             Seed.print( "Error removing watch on " + moved_from );
67         }
68         moved_from = '';
69     }
70      
71     if ((event.mask & inotifytools.IN_CREATE) ||
72         (!moved_from.length && (event.mask & inotifytools.IN_MOVED_TO))) {
73         // New file - if it is a directory, watch it
74         
75
76         var new_file =  inotifytools.filename_from_wd( event.wd ) + event.name ;
77
78         if (  GLib.file_test(new_file, GLib.FileTest.IS_DIR) &&
79             !inotifytools.watch_recursively( new_file, events ) ) {
80             Seed.print( "Couldn't watch new directory " + new_file);
81         }
82         
83     } // IN_CREATE
84     else if (event.mask & inotifytools.IN_MOVED_FROM) {
85         moved_from =  inotifytools.filename_from_wd( event.wd ) + event.name ;
86         // if not watched...
87         if ( inotifytools.wd_from_filename(moved_from) == -1 ) {
88             moved_from = '';
89         }
90     } // IN_MOVED_FROM
91     else if (event.mask & inotifytools.IN_MOVED_TO) {
92         if ( moved_from.length ) {
93             var new_name = inotifytools_filename_from_wd( event. wd ) + 
94                        event.name;
95             inotifytools.replace_filename( moved_from, new_name );
96             
97             moved_from = '';
98         } // moved_from
99     }
100         Seed.print("CHANGE" + inotifytools.event_to_str(event.mask) + "\n" + 
101         inotifytools.filename_from_wd( event.wd ) + event.name + "\n");
102     
103 }