本文整理汇总了PHP中inotify_add_watch函数的典型用法代码示例。如果您正苦于以下问题:PHP inotify_add_watch函数的具体用法?PHP inotify_add_watch怎么用?PHP inotify_add_watch使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了inotify_add_watch函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addPath
/**
* Add a path to the watcher
*
* @param string $path
* @param int $mode
* @param bool $recursive
*/
public function addPath($path, $mode = IN_MODIFY, $recursive = true)
{
TypeCheck::doCheck(DataType::STRING, DataType::INT, DataType::BOOLEAN);
if ($this->isExcluded($path)) {
return;
}
if (is_dir($path) && $recursive) {
$children = scandir($path);
foreach ($children as $child) {
if ($child != '.' && $child != '..' && $child != '.git' && is_dir($path . DIRECTORY_SEPARATOR . $child)) {
$this->addPath($path . DIRECTORY_SEPARATOR . $child, $mode, $recursive);
}
}
}
$watchDescriptor = inotify_add_watch($this->inotifyStreamDescriptor, $path, $mode);
$this->watchDescriptors[$watchDescriptor] = $path;
}
示例2: start
public function start()
{
$this->stop = false;
$fd = inotify_init();
$wd = inotify_add_watch($fd, $this->path, IN_ALL_EVENTS);
$read = [$fd];
$write = null;
$except = null;
stream_select($read, $write, $except, 0);
stream_set_blocking($fd, 0);
while (true) {
if ($this->stop) {
inotify_rm_watch($fd, $wd);
return fclose($fd);
}
if ($events = inotify_read($fd)) {
foreach ($events as $details) {
if ($details['name']) {
$target = rtrim($this->path, '/') . '/' . $details['name'];
} else {
$target = $this->path;
}
$file = new \SplFileInfo($target);
switch (true) {
case $details['mask'] & IN_MODIFY:
$this->modify($file);
break;
}
$this->all($file);
}
}
}
}
示例3: watchADir
function watchADir($path)
{
$this->dirs[$path] = $path;
$this->ifds[$path] = inotify_init();
$this->wds[$path] = inotify_add_watch($this->ifds[$path], $path, self::DIR_MASK);
$this->log("add dir {$path} " . print_r($this->dirs, 1) . print_r($this->wds, 1));
}
示例4: tail
function tail($file, &$pos)
{
// get the size of the file
if (!$pos) {
$pos = filesize($file);
}
// Open an inotify instance
$fd = inotify_init();
// Watch $file for changes.
$watch_descriptor = inotify_add_watch($fd, $file, IN_ALL_EVENTS);
// Loop forever (breaks are below)
while (true) {
// Read events (inotify_read is blocking!)
$events = inotify_read($fd);
// Loop though the events which occured
foreach ($events as $event => $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;
break;
// File was moved or deleted
// 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;
}
}
}
}
示例5: watch
/**
* @param array $directories
* @param \Closure|null $callback
* @return Watcher
* @throws \RuntimeException
*/
public function watch(array $directories, \Closure $callback = null)
{
foreach ($directories as $directory) {
if (!is_dir($directory)) {
$this->clearWatch();
throw new \RuntimeException("[{$directory}] is not a directory.");
}
$wd = inotify_add_watch($this->inotify, $directory, $this->events);
$this->watchDir[$directory] = $wd;
}
$this->callback = $callback;
// Listen modify.
\swoole_event_add($this->inotify, function () use($callback) {
$events = inotify_read($this->inotify);
if (!$events) {
return;
}
foreach ($events as $event) {
if (!empty($event['name'])) {
echo sprintf("[%s]\t" . sprintf('["%s"] modify', $event['name']) . PHP_EOL, date('Y-m-d H:i:s'));
}
}
if (is_callable($callback)) {
$callback($this);
}
});
return $this;
}
示例6: main_loop
/**
* Simple tail program using the inotify extension
* Usage: ./tail.php file
*/
function main_loop($file, $file_fd)
{
$inotify = inotify_init();
if ($inotify === false) {
fprintf(STDERR, "Failed to obtain an inotify instance\n");
return 1;
}
$watch = inotify_add_watch($inotify, $file, IN_MODIFY);
if ($watch === false) {
fprintf(STDERR, "Failed to watch file '%s'", $file);
return 1;
}
while (($events = inotify_read($inotify)) !== false) {
echo "Event received !\n";
foreach ($events as $event) {
if (!($event['mask'] & IN_MODIFY)) {
continue;
}
echo stream_get_contents($file_fd);
break;
}
}
// May not happen
inotify_rm_watch($inotify, $watch);
fclose($inotify);
fclose($file_fd);
return 0;
}
示例7: addWatch
/**
* Adds your subscription on object in FS
* @param string $path Path
* @param mixed $cb Callback
* @param integer $flags Look inotify_add_watch()
* @return true
*/
public function addWatch($path, $cb, $flags = null)
{
$path = realpath($path);
if (!isset($this->files[$path])) {
$this->files[$path] = [];
if ($this->inotify) {
$this->descriptors[inotify_add_watch($this->inotify, $path, $flags ?: IN_MODIFY)] = $path;
}
}
$this->files[$path][] = $cb;
Timer::setTimeout('fileWatcher');
return true;
}
示例8: add
/**
* Adds a path to the list of watched paths
*
* @param string $path Path to the watched file or directory
* @param integer $mask Bitmask of inotify constants
* @return integer unique watch identifier, can be used to remove() watch later
*/
public function add($path, $mask)
{
if ($this->inotifyHandler === false) {
// inotifyHandler not started yet => start a new one
$this->inotifyHandler = \inotify_init();
stream_set_blocking($this->inotifyHandler, 0);
// wait for any file events by reading from inotify handler asynchronously
$this->loop->addReadStream($this->inotifyHandler, $this);
}
$descriptor = \inotify_add_watch($this->inotifyHandler, $path, $mask);
$this->watchDescriptors[$descriptor] = array('path' => $path);
return $descriptor;
}
示例9: addWatch
public function addWatch($path, $subscriber, $flags = NULL)
{
$path = realpath($path);
if (!isset($this->files[$path])) {
$this->files[$path] = array();
if ($this->inotify) {
$this->descriptors[inotify_add_watch($this->inotify, $path, $flags ?: IN_MODIFY)] = $path;
}
}
$this->files[$path][] = $subscriber;
Daemon_TimedEvent::setTimeout('fileWatcherTimedEvent');
return true;
}
示例10: watch
public function watch(TrackedObject $tracked)
{
$path = realpath($tracked->getResource());
if ($tracked->getResource() instanceof FileResource) {
return;
}
if (!$path) {
return;
}
$id = inotify_add_watch($this->inotify, $path, $this->inotifyEventMask);
$this->inotifyMap[$id] = $tracked;
return parent::watch($tracked);
}
示例11: addWatches
protected function addWatches($in, Path $path, &$watches)
{
if (!$path->isDir()) {
// not in a directory, bail
return;
}
$wd = inotify_add_watch($in, $path->getPathname(), $this->computedMask);
$watches[$wd] = $path;
printf("\rAdding watches... %d", count($watches));
// recurse into this directory's children
$children = $path->getChildren();
foreach ($children as $child) {
if ($child->isDir()) {
$this->addWatches($in, $child, $watches);
}
}
}
示例12: watchPath
/**
* Adds the path to the list of files and folders to monitor for changes.
* If the path is a file, its modification time is stored for comparison.
* If a directory, the modification times for each sub-directory and file
* are recursively stored.
*
* @param string $path A valid path to a file or directory
*/
public function watchPath($path)
{
if (!$this->inotify) {
$this->paths[] = $path;
$this->addModifiedTimes($path);
return;
}
$mask = IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | IN_MOVE | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF;
$directoryIterator = new \RecursiveDirectoryIterator(realpath($path));
$subPaths = new \RecursiveIteratorIterator($directoryIterator);
// Iterate over instances of \SplFileObject
foreach ($subpaths as $subPath) {
if ($subPath->isDir()) {
$watchDescriptor = inotify_add_watch($this->inotify, $subPath->getRealPath(), $mask);
$this->paths[$watchDescriptor] = $subPath->getRealPath();
}
}
}
示例13: check_files_change
function check_files_change($inotify_fd)
{
global $monitor_files;
// 读取有哪些文件事件
$events = inotify_read($inotify_fd);
if ($events) {
// 检查哪些文件被更新了
foreach ($events as $ev) {
// 更新的文件
$file = $monitor_files[$ev['wd']];
echo $file . " update and reload\n";
unset($monitor_files[$ev['wd']]);
// 需要把文件重新加入监控
$wd = inotify_add_watch($inotify_fd, $file, IN_MODIFY);
$monitor_files[$wd] = $file;
}
// 给父进程也就是主进程发送reload信号
posix_kill(posix_getppid(), SIGUSR1);
}
}
示例14: addWatch
/**
* Watch resource
*
* @return int
*/
protected function addWatch()
{
return inotify_add_watch($this->getBag()->getInotify(), $this->getResource()->getResource(), $this->getInotifyEventMask());
}
示例15: inotify_init
<?php
// Open an inotify instance
$fd = inotify_init();
// Watch __FILE__ for metadata changes (e.g. mtime)
$watch_descriptor = inotify_add_watch($fd, __FILE__, IN_ATTRIB);
// generate an event
touch(__FILE__);
// Read events
$events = inotify_read($fd);
print_r($events);
// The following methods allows to use inotify functions without blocking on inotify_read():
// - Using stream_select() on $fd:
$read = array($fd);
$write = null;
$except = null;
stream_select($read, $write, $except, 0);
// - Using stream_set_blocking() on $fd
stream_set_blocking($fd, 0);
inotify_read($fd);
// Does no block, and return false if no events are pending
// - Using inotify_queue_len() to check if event queue is not empty
$queue_len = inotify_queue_len($fd);
// If > 0, inotify_read() will not block
// Stop watching __FILE__ for metadata changes
inotify_rm_watch($fd, $watch_descriptor);
// Close the inotify instance
// This may have closed all watches if this was not already done
fclose($fd);