[EXTRAS] Inotifytools testing
[gnome.gobject-introspection] / extras / inotifytools / example.js
diff --git a/extras/inotifytools/example.js b/extras/inotifytools/example.js
new file mode 100644 (file)
index 0000000..fce5471
--- /dev/null
@@ -0,0 +1,100 @@
+#!/usr/bin/seed
+
+//<script type="text/javascript">
+// silly test to see how libc works :)
+// borked - inotify sends a struct via a buffer -- need to use inotify tools!
+inotifytools = imports.gi.inotifytools;
+GLib = imports.gi.GLib;
+
+
+
+if (!inotifytools.initialize()) {
+    Seed.print("Failed to init");
+    Seed.quit();
+}
+Seed.print("watch Error?" + GLib.strerror(inotifytools.error()));
+var events = inotifytools.IN_ACCESS | 
+       inotifytools.IN_MODIFY | 
+        inotifytools.IN_ATTRIB | 
+        inotifytools.IN_CLOSE_WRITE | 
+        inotifytools.IN_CLOSE_NOWRITE | 
+       inotifytools.IN_OPEN | 
+       inotifytools.IN_MOVED_FROM | 
+       inotifytools.IN_MOVED_TO | 
+       inotifytools.IN_DELETE | 
+       inotifytools.IN_CREATE | 
+       inotifytools.IN_DELETE_SELF | 
+       inotifytools.IN_MOVE_SELF;
+print("EVENTS:"+ events);
+var dir = String(Seed.argv[2]) 
+Seed.print("MONITOR: " + dir +"?");
+var r = inotifytools.watch_recursively(  dir , events);
+
+Seed.print("GOT " +r );
+if (!r) {
+       Seed.print("watch Error?" + GLib.strerror(inotifytools.error()));
+    Seed.quit();
+}
+
+
+var num_watches = inotifytools.get_num_watches();
+Seed.print("Watch " + num_watches );
+var moved_from = '';
+while (true) {
+    
+    var event = inotifytools.next_event( 0 );
+    if ( !event ) {
+        if ( !inotifytools.error() ) {
+            Seed.print("Timeout?");
+            Seed.quit();
+        }
+        else if ( inotifytools.error() != 4 ) {
+                               Seed.print("Error?" + inotifytools.error());
+                               Seed.quit();
+        } else {
+            continue;
+                       
+               }
+    }
+    if ( moved_from.length && !(event.mask & inotifytools.IN_MOVED_TO) ) {
+        if ( !inotifytools.remove_watch_by_filename( moved_from ) ) {
+            Seed.print( "Error removing watch on " + moved_from );
+        }
+        moved_from = '';
+    }
+     
+    if ((event.mask & inotifytools.IN_CREATE) ||
+        (!moved_from.length && (event.mask & inotifytools.IN_MOVED_TO))) {
+        // New file - if it is a directory, watch it
+        
+
+        var new_file =  inotifytools.filename_from_wd( event.wd ) + event.name ;
+
+        if (  GLib.file_test(new_file, GLib.FileTest.IS_DIR) &&
+            !inotifytools.watch_recursively( new_file, events ) ) {
+            Seed.print( "Couldn't watch new directory " + new_file);
+        }
+        
+    } // IN_CREATE
+    else if (event.mask & inotifytools.IN_MOVED_FROM) {
+        moved_from =  inotifytools.filename_from_wd( event.wd ) + event.name ;
+        // if not watched...
+        if ( inotifytools.wd_from_filename(moved_from) == -1 ) {
+            moved_from = '';
+        }
+    } // IN_MOVED_FROM
+    else if (event.mask & inotifytools.IN_MOVED_TO) {
+        if ( moved_from.length ) {
+            var new_name = inotifytools_filename_from_wd( event. wd ) + 
+                       event.name;
+            inotifytools.replace_filename( moved_from, new_name );
+            
+            moved_from = '';
+        } // moved_from
+    }
+       Seed.print("CHANGE" + inotifytools.event_to_str(event.mask) + "\n" + 
+        inotifytools.filename_from_wd( event.wd ) + event.name + "\n");
+    
+}
\ No newline at end of file