inotify-文件或目录监控

	从 Linux 2.6.13 内核开始,Linux 就推出了 inotify,允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。

	参考文章:
		http://blog.jiunile.com/php%E4%BD%BF%E7%94%A8inotify%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97%E5%A4%84%E7%90%86.html
		http://os.51cto.com/art/201011/232694_all.htm
		http://blog.csdn.net/myarrow/article/details/7096460

	简介:
		PHP的inotify扩展,暴露了inotify函数,inotify_init()、inotify_add_watch()、inotify_rm_watch()
		正如C语言的inotify_init()函数,返回一个文件描述符,PHP的inotify_init()返回一个stream流资源,使用标准的流函数,像:stream_select()、stream_set_blocking()和fclose()。PHP的inotify_read()函数替代了C语言读取inotify事件的方式。
	安装:
		未与PHP绑定,可通过:http://pecl.php.net/package/inotify 来下载编译安装

	函数:
		inotify_init() - 初始化一个inotify实例,用于 'inotify_add_watch()'
		inotify_add_watch() - 给inotify添加一个监控文件或目录,返回监控的唯一id
		inotify_rm_watch() - 移除inotify的一个监控
		inotify_read() - 从inotify中,读取inotify事件
		inotify_queue_len() - 如果有等待的事件,返回一个>0的数字。通过该函数,我们可以知道 'inotify_read()' 是否阻塞。如果返回一个>0的数字,有等待事件,inotify_read()将不会阻塞

	手册上示例:
	 0, inotify_read() will not block - >0,inotify_read() 将不会阻塞

		// Stop watching __FILE__ for metadata changes - 停止监听 '__FILE__' 元数据的改变
		inotify_rm_watch($fd, $watch_descriptor);

		// Close the inotify instance - 关闭 inotify 实例
		// This may have closed all watches if this was not already done
		fclose($fd);
	?>

	某人给的例子:
	$evdetails) {
		            // React on the event type
		            switch (true) {
		                // File was modified
		                case ($evdetails['mask'] & IN_MODIFY):
		                    // Stop watching $file for changes
		                    inotify_rm_watch($fd, $watch_descriptor);
		                    // Close the inotify instance
		                    fclose($fd);
		                    // open the file
		                    $fp = fopen($file,'r');
		                    if (!$fp) return false;
		                    // seek to the last EOF position
		                    fseek($fp,$pos);
		                    // read until EOF
		                    while (!feof($fp)) {
		                        $buf .= fread($fp,8192);
		                    }
		                    // save the new EOF to $pos
		                    $pos = ftell($fp); // (remember: $pos is called by reference)
		                    // close the file pointer
		                    fclose($fp);
		                    // return the new data and leave the function
		                    return $buf;
		                    // be a nice guy and program good code ;-)
		                    break;

		                    // File was moved or deleted
		                case ($evdetails['mask'] & IN_MOVE):
		                case ($evdetails['mask'] & IN_MOVE_SELF):
		                case ($evdetails['mask'] & IN_DELETE):
		                case ($evdetails['mask'] & IN_DELETE_SELF):
		                    // Stop watching $file for changes
		                    inotify_rm_watch($fd, $watch_descriptor);
		                    // Close the inotify instance
		                    fclose($fd);
		                    // Return a failure
		                    return false;
		                    break;
		            }
		        }
		    }
		}

		// Use it like that:
		$lastpos = 0;
		while (true) {
		    echo tail($file,$lastpos);
		}
	?>

你可能感兴趣的:(inotify-文件或目录监控)