Inotify bindings for google v8 javascript engine ( yes it is a nodejs addon)
The inotify API provides a mechanism for monitoring file system events. Inotify can be used to monitor individual files, or to monitor directories. When a directory is monitored, inotify will return events for the directory itself, and for files inside the directory. (ref: GNU/Linux Manual)
NodeJS versions 0.8.x are currently supported and tested.
$ npm install inotify
$ npm install node-gyp -g
$ git clone git://github.com/c4milo/node-inotify.git
$ cd node-inotify
$ node-gyp rebuild
var inotify = new Inotify()
: Create a new instance of Inotify. By default it's in persistent mode.
You can specify false
in var inotify = new Inotify(false)
to use the non persistent mode.
var wd = inotify.addWatch(arg)
: Add a watch for files or directories. This will then return a watch descriptor. The argument is an object as follows
var arg = { path: 'path to be monitored',
watch_for: an optional OR'ed set of events to watch for.
If they're not specified, it will use
Inotify.IN_ALL_EVENTS by default,
callback: Callback function that will receive each event.
}
You can call this function as many times as you want to monitor different paths.
Inotify monitoring of directories is not recursive: to monitor subdirectories
under a directory, additional watches must be created.
inotify.removeWatch(watch_descriptor)
: Remove a watch associated with the watch_descriptor param and returns true
if the action was succesful or false
in the opposite case. Removing a watch cause an Inotify.IN_IGNORED
event to be generated for this watch descriptor.
inotify.close()
: Remove all the watches and close the inotify's file descriptor. Returns true
if the action was succesful or false in the opposite case.
var event = { watch: Watch descriptor,
mask: Mask of events,
cookie: Cookie that permits to associate events,
name: Optional name of the object being watched
};
The event.name
property is only present when an event is returned for a file inside a
watched directory; it identifies the file pathname relative to the watched
directory.
var Inotify = require('inotify').Inotify;
var inotify = new Inotify(); //persistent by default, new Inotify(false) //no persistent
var data = {}; //used to correlate two events
var callback = function(event) {
var mask = event.mask;
var type = mask & Inotify.IN_ISDIR ? 'directory ' : 'file ';
event.name ? type += ' ' + event.name + ' ': ' ';
//the porpuse of this hell of 'if'
//statements is only illustrative.
if(mask & Inotify.IN_ACCESS) {
console.log(type + 'was accessed ');
} else if(mask & Inotify.IN_MODIFY) {
console.log(type + 'was modified ');
} else if(mask & Inotify.IN_OPEN) {
console.log(type + 'was opened ');
} else if(mask & Inotify.IN_CLOSE_NOWRITE) {
console.log(type + ' opened for reading was closed ');
} else if(mask & Inotify.IN_CLOSE_WRITE) {
console.log(type + ' opened for writing was closed ');
} else if(mask & Inotify.IN_ATTRIB) {
console.log(type + 'metadata changed ');
} else if(mask & Inotify.IN_CREATE) {
console.log(type + 'created');
} else if(mask & Inotify.IN_DELETE) {
console.log(type + 'deleted');
} else if(mask & Inotify.IN_DELETE_SELF) {
console.log(type + 'watched deleted ');
} else if(mask & Inotify.IN_MOVE_SELF) {
console.log(type + 'watched moved');
} else if(mask & Inotify.IN_IGNORED) {
console.log(type + 'watch was removed');
} else if(mask & Inotify.IN_MOVED_FROM) {
data = event;
data.type = type;
} else if(mask & Inotify.IN_MOVED_TO) {
if( Object.keys(data).length &&
data.cookie === event.cookie) {
console.log(type + ' moved to ' + data.type);
data = {};
}
}
}
var home_dir = { path: '/home/camilo', // <--- change this for a valid directory in your machine.
watch_for: Inotify.IN_OPEN | Inotify.IN_CLOSE,
callback: callback
};
var home_watch_descriptor = inotify.addWatch(home_dir);
var home2_dir = { path: '/home/bob', // <--- change this for a valid directory in your machine
watch_for: Inotify.IN_ALL_EVENTS,
callback: callback
};
var home2_wd = inotify.addWatch(home2_dir);
event.mask
property returned in the callback(The MIT License)
Copyright 2012 Camilo Aguilar. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.