ファイル及びフォルダ監視機能
https://www.npmjs.com/package/inotify
复制代码
git://www.github.com/c4milo/node-inotify.git Git Clone代码到本地:复制代码
git clone http://www.github.com/c4milo/node-inotify Subversion代码到本地:复制代码
$ svn co --depth empty http://www.github.com/c4milo/node-inotify Checked out revision 1. $ cd repo $ svn up trunk/linux中的节点inotify监控文件系统事件,带有 NodeJS。
inotify提供了一种监视文件系统事件的机制。 Inotify可以用来监控单个文件,或者监视目录。 监视目录时,inotify将返回目录本身的事件,以及目录中的文件。 ( 参考:gnu/linux手册)
1/x 版本 0.10. x, 0.12. x, 4. x. x,5. x. x 和 IO.js 1. x,. x, 3.x 当前支持并测试。
复制代码
$ npm install inotify复制代码
$ npm install node-gyp -g $ git clone git://github.com/c4milo/node-inotify.git $ cd node-inotify $ node-gyp rebuildvar inotify = new Inotify() :创建Inotify的新实例。 默认情况下它处于持久模式。 你可以指定 falsevar inotify = new Inotify(false) 使用非持久模式。
var wd = inotify.addWatch(arg) :为文件或者目录添加监视。 这将返回一个监视描述符。 参数是如下所示的对象
复制代码
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:Inotify.IN_ALL_EVENTS, // Callback function that will receive each event.callback:function (event) {} }你可以按需要多次调用这里函数,以便监视不同的路径。 对目录的监视为递归: 要监视目录下的子目录,必须创建额外的表。
inotify.removeWatch(watch_descriptor) :删除与watch_descriptor参数关联的表,如果操作成功或者在相反情况下返回 false,则返回 true。 删除表会导致为此表描述符生成一个 Inotify.IN_IGNORED 事件。
inotify.close(): 删除所有的表并关闭inotify描述符的文件。 如果操作成功,则返回 true,否则返回 false。
复制代码
varevent= { watch: Watch descriptor, mask: Mask of events, cookie: Cookie that permits to associate events, name: Optional name of the object being watched };只有在监视目录中的文件返回事件时,event.name 属性才存在;它标识相对于被监视目录的文件路径名。
复制代码
var Inotify =require('inotify').Inotify; var inotify =newInotify(); //persistent by default, new Inotify(false)//no persistentvar data = {}; //used to correlate two eventsvarcallback=function(event) { var mask =event.mask; var type = mask &Inotify.IN_ISDIR?'directory ':'file '; if (event.name) { type +=''+event.name+''; } else { type +=''; } // the purpose of this hell of 'if' statements is only illustrative.if (mask &Inotify.IN_ACCESS) { console.log(type +'was accessed '); } elseif (mask &Inotify.IN_MODIFY) { console.log(type +'was modified '); } elseif (mask &Inotify.IN_OPEN) { console.log(type +'was opened '); } elseif (mask &Inotify.IN_CLOSE_NOWRITE) { console.log(type +' opened for reading was closed '); } elseif (mask &Inotify.IN_CLOSE_WRITE) { console.log(type +' opened for writing was closed '); } elseif (mask &Inotify.IN_ATTRIB) { console.log(type +'metadata changed '); } elseif (mask &Inotify.IN_CREATE) { console.log(type +'created'); } elseif (mask &Inotify.IN_DELETE) { console.log(type +'deleted'); } elseif (mask &Inotify.IN_DELETE_SELF) { console.log(type +'watched deleted '); } elseif (mask &Inotify.IN_MOVE_SELF) { console.log(type +'watched moved'); } elseif (mask &Inotify.IN_IGNORED) { console.log(type +'watch was removed'); } elseif (mask &Inotify.IN_MOVED_FROM) { data =event; data.type= type; } elseif (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 = { // Change this for a valid directory in your machine. path:'/home/camilo', watch_for:Inotify.IN_OPEN|Inotify.IN_CLOSE, callback: callback }; var home_watch_descriptor =inotify.addWatch(home_dir); var home2_dir = { // Change this for a valid directory in your machine path:'/home/bob', watch_for:Inotify.IN_ALL_EVENTS, callback: callback }; var home2_wd =inotify.addWatch(home2_dir);http://www.quora.com/Inotify-monitoring-of-directories-is-not-recursive-Is-there-any-specific-reason-for-this-design-in-Linux-kernel
