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