Skip to content

Instantly share code, notes, and snippets.

@nicokruger
Created March 13, 2012 18:57
Show Gist options
  • Save nicokruger/2030769 to your computer and use it in GitHub Desktop.
Save nicokruger/2030769 to your computer and use it in GitHub Desktop.
inotify-test.js
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.
console.log(JSON.stringify(event));
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) {
console.log(type + 'moved from');
} else if(mask & Inotify.IN_MOVED_TO) {
console.log(type + "moved to");
}
};
var home2_dir = { path: __dirname + '/incoming', // <--- change this for a valid directory in your machine
watch_for: Inotify.IN_ALL_EVENTS,
// Inotify.IN_CREATE | Inotify.CLOSE_WRITE
callback: callback
};
var home2_wd = inotify.addWatch(home2_dir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment