Node-inotify

Inotify bindings for google v8 javascript engine ( yes it is a nodejs addon)


Project maintained by c4milo Hosted on GitHub Pages — Theme by mattgraham

node-inotify - monitoring file system events in Gnu/Linux with NodeJS

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)

Installation

NodeJS versions 0.8.x are currently supported and tested.

Install from NPM (Easy way)

$ npm install inotify

Install from git

$ npm install node-gyp -g
$ git clone git://github.com/c4milo/node-inotify.git
$ cd node-inotify
$ node-gyp rebuild

API

Event object structure

    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.

Example of use

    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);

Inotify Events

Watch for:

Additional Flags:

The following bits may be set in the event.mask property returned in the callback

License

(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.