[EXTRA] inotifytools
[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);
49     if ( !event ) {
50         if ( !inotifytools.error() ) {
51             Seed.print("Timeout?");
52             Seed.quit();
53         }
54         else if ( inotifytools.error() != 4 ) {
55                                 Seed.print("Error?" + inotifytools.error());
56                                 Seed.quit();
57         } else {
58             continue;
59                         
60         }
61     }
62     if ( moved_from.length && !(event.mask & inotifytools.IN_MOVED_TO) ) {
63         if ( !inotifytools.remove_watch_by_filename( moved_from ) ) {
64             Seed.print( "Error removing watch on " + moved_from );
65         }
66         moved_from = '';
67     }
68      
69     if ((event.mask & inotifytools.IN_CREATE) ||
70         (!moved_from.length && (event.mask & inotifytools.IN_MOVED_TO))) {
71         // New file - if it is a directory, watch it
72         
73
74         var new_file =  inotifytools.filename_from_wd( event.wd ) + event.name ;
75
76         if (  GLib.file_test(new_file, GLib.FileTest.IS_DIR) &&
77             !inotifytools.watch_recursively( new_file, events ) ) {
78             Seed.print( "Couldn't watch new directory " + new_file);
79         }
80         
81     } // IN_CREATE
82     else if (event.mask & inotifytools.IN_MOVED_FROM) {
83         moved_from =  inotifytools.filename_from_wd( event.wd ) + event.name ;
84         // if not watched...
85         if ( inotifytools.wd_from_filename(moved_from) == -1 ) {
86             moved_from = '';
87         }
88     } // IN_MOVED_FROM
89     else if (event.mask & inotifytools.IN_MOVED_TO) {
90         if ( moved_from.length ) {
91             var new_name = inotifytools_filename_from_wd( event. wd ) + 
92                        event.name;
93             inotifytools.replace_filename( moved_from, new_name );
94             
95             moved_from = '';
96         } // moved_from
97     }
98         Seed.print("CHANGE" + inotifytools.event_to_str(event.mask) + "\n" + 
99         inotifytools.filename_from_wd( event.wd ) + event.name + "\n");
100     
101 }