NodeJS
node-inotify - monitoring file system events in Gnu/Linux withThe 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.10.x, 0.12.x, 4.x.x, 5.x.x and IO.js 1.x, 2.x, 3.x are currently supported and tested.
Install from NPM
$ 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
-
var inotify = new Inotify()
: Creates a new instance of Inotify. By default it's in persistent mode. You can specifyfalse
invar inotify = new Inotify(false)
to use the non persistent mode. -
var wd = inotify.addWatch(arg)
: Adds a watch for files or directories. This will then return a watch descriptor. The argument is an object as follows
var arg = // Path to be monitored. path: '.' // An optional OR'ed set of events to watch for. // If they're not specified, it will use // Inotify.IN_ALL_EVENTS by default. watch_for: InotifyIN_ALL_EVENTS // Callback function that will receive each event. {}
You can call this function as many times as you want in order to monitor different paths. 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 returnstrue
if the action was successful orfalse
in the opposite case. Removing a watch causes anInotify.IN_IGNORED
event to be generated for this watch descriptor. -
inotify.close()
: Remove all the watches and close the inotify's file descriptor. Returnstrue
if the action was successful or false in the opposite case.
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 path name relative to the watched directory.
Example of use
var Inotify = Inotify; var inotify = ; //persistent by default, new Inotify(false) //no persistent var data = {}; //used to correlate two events var { var mask = eventmask; var type = mask & InotifyIN_ISDIR ? 'directory ' : 'file '; if eventname type += ' ' + eventname + ' '; else type += ' '; // the purpose of this hell of 'if' statements is only illustrative. if mask & InotifyIN_ACCESS console; else if mask & InotifyIN_MODIFY console; else if mask & InotifyIN_OPEN console; else if mask & InotifyIN_CLOSE_NOWRITE console; else if mask & InotifyIN_CLOSE_WRITE console; else if mask & InotifyIN_ATTRIB console; else if mask & InotifyIN_CREATE console; else if mask & InotifyIN_DELETE console; else if mask & InotifyIN_DELETE_SELF console; else if mask & InotifyIN_MOVE_SELF console; else if mask & InotifyIN_IGNORED console; else if mask & InotifyIN_MOVED_FROM data = event; datatype = type; else if mask & InotifyIN_MOVED_TO if Objectlength && datacookie === eventcookie console; data = {}; } var home_dir = // Change this for a valid directory in your machine. path: '/home/camilo' watch_for: InotifyIN_OPEN | InotifyIN_CLOSE callback: callback ; var home_watch_descriptor = inotify; var home2_dir = // Change this for a valid directory in your machine path: '/home/bob' watch_for: InotifyIN_ALL_EVENTS callback: callback ; var home2_wd = inotify;
Inotify Events
Watch for:
- Inotify.IN_ACCESS: File was accessed (read)
- Inotify.IN_ATTRIB: Metadata changed, e.g., permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID, GID, etc.
- Inotify.IN_CLOSE_WRITE: File opened for writing was closed
- Inotify.IN_CLOSE_NOWRITE: File not opened for writing was closed
- Inotify.IN_CREATE: File/directory created in the watched directory
- Inotify.IN_DELETE: File/directory deleted from the watched directory
- Inotify.IN_DELETE_SELF: Watched file/directory was deleted
- Inotify.IN_MODIFY: File was modified
- Inotify.IN_MOVE_SELF: Watched file/directory was moved
- Inotify.IN_MOVED_FROM: File moved out of the watched directory
- Inotify.IN_MOVED_TO: File moved into watched directory
- Inotify.IN_OPEN: File was opened
- Inotify.IN_ALL_EVENTS: Watch for all kind of events
- Inotify.IN_CLOSE: (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) Close
- Inotify.IN_MOVE: (IN_MOVED_FROM | IN_MOVED_TO) Moves
Additional Flags:
- Inotify.IN_ONLYDIR: Only watch the path if it is a directory.
- Inotify.IN_DONT_FOLLOW: Do not follow symbolics links
- Inotify.IN_ONESHOT: Only send events once
- Inotify.IN_MASK_ADD: Add (OR) events to watch mask for this pathname if it already exists (instead of replacing the mask).
event.mask
property returned in the callback
The following bits may be set in the - Inotify.IN_IGNORED: Watch was removed explicitly with inotify.removeWatch(watch_descriptor) or automatically (the file was deleted, or the file system was unmounted)
- Inotify.IN_ISDIR: Subject of this event is a directory
- Inotify.IN_Q_OVERFLOW: Event queue overflowed (wd is -1 for this event)
- Inotify.IN_UNMOUNT: File system containing the watched object was unmounted
FAQ
Why inotify does not watch directories recursively?
License
(The MIT License)
Copyright 2017 Node-Inotify AUTHORS. 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.