inotify

ファイル及びフォルダ監視機能

https://www.npmjs.com/package/inotify

node-inotify, Inotify对 node.js的绑定

 

/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安装

复制代码

 $ npm install inotify

从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() :创建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);

Inotify事件

监视:

  • Inotify.IN_ACCESS: 文件被访问( 读取)
  • 收费的:元数据改变,比如,权限,时间戳,扩展属性,链接计数( 自 Linux 2.6.25以来),UID,GID等等。
  • 为写入打开的Inotify.IN_CLOSE_WRITE: 文件已经关闭
  • Inotify.IN_CLOSE_NOWRITE: 未打开写入的文件已经关闭
  • Inotify.IN_CREATE: watched目录中创建的文件/目录
  • Inotify.IN_DELETE: 文件/目录从被监视目录中删除
  • Inotify.IN_DELETE_SELF: 监视文件/目录被删除
  • Inotify.IN_MODIFY: 文件被修改
  • Inotify.IN_MOVE_SELF: 监视文件/目录被移动
  • Inotify.IN_MOVED_FROM: 文件已经移出监视目录
  • Inotify.IN_MOVED_TO: 文件被移动到监视目录中
  • Inotify.IN_OPEN: 文件已经打开
  • Inotify.IN_ALL_EVENTS: 监视所有类型的事件
  • Inotify.IN_CLOSE: ( IN_CLOSE_WRITE | IN_CLOSE_NOWRITE ) 关闭
  • Inotify.IN_MOVE: ( IN_MOVED_FROM | IN_MOVED_TO ) 移动

附加标志:

  • 如果路径是目录,Inotify.IN_ONLYDIR: 只监视路径。
  • Inotify.IN_DONT_FOLLOW: 不跟踪符号链接
  • Inotify.IN_ONESHOT: 只发送一次事件
  • 如果这里路径名已经存在,则为 Inotify.IN_MASK_ADD 添加( 或者或或者) 事件以监视该路径名。

可以在回调返回的event.mask 属性中设置以下位

  • 使用 inotify.removeWatch(watch_descriptor) 或者自动( 删除文件,或者卸载文件系统),Inotify.IN_IGNORED: 表被明确地删除了
  • Inotify.IN_ISDIR: 这里事件的主题为目录
  • Inotify.IN_Q_OVERFLOW: 事件队列溢出( 这里事件的wd为 -1 )
  • Inotify.IN_UNMOUNT: 包含监视对象的文件系统已经被卸载

常见问题解答

为什么inotify不递归监视目录?

http://www.quora.com/Inotify-monitoring-of-directories-is-not-recursive-Is-there-any-specific-reason-for-this-design-in-Linux-kernel

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值