本文整理汇总了PHP中inotify_read函数的典型用法代码示例。如果您正苦于以下问题:PHP inotify_read函数的具体用法?PHP inotify_read怎么用?PHP inotify_read使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了inotify_read函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: fire
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$watches = array();
$in = inotify_init();
// add watches starting from root directory
$root = Path::fromRelative('');
$this->addWatches($in, $root, $watches);
printf("\nReading for events\n");
while (true) {
$events = inotify_read($in);
foreach ($events as $event) {
$path = $watches[$event['wd']];
$expanded = $this->expandMask($event['mask']);
$eventName = trim(implode(', ', $expanded), ', ');
// if the event has a name attached, then index that
if ($event['name']) {
$newPathName = $path->getPathname() . '/' . $event['name'];
$newPath = new Path($newPathName);
Indexer::index($newPath, 1);
// this may be a new directory, so add a watch to it anyway
if ($newPath->exists() && $newPath->isDir()) {
try {
$wd = inotify_add_watch($in, $newPath->getPathname(), $this->computedMask);
$watches[$wd] = $newPath;
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
} else {
// event must apply to this directory, so index it, 1 level deep
Indexer::index($path, 1);
}
}
}
}
示例3: 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);
}
}
}
}
示例4: 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;
}
示例5: 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;
}
}
}
}
示例6: watch
/**
* Check the file system, triggered by timer
* @return void
*/
public function watch()
{
if ($this->inotify) {
$events = inotify_read($this->inotify);
if (!$events) {
return;
}
foreach ($events as $ev) {
$path = $this->descriptors[$ev['wd']];
if (!isset($this->files[$path])) {
continue;
}
$this->onFileChanged($path);
}
} else {
static $hash = [];
foreach ($this->files as $path => $v) {
if (!file_exists($path)) {
// file can be deleted
unset($this->files[$path]);
continue;
}
$mt = filemtime($path);
if (isset($hash[$path]) && $mt > $hash[$path]) {
$this->onFileChanged($path);
}
$hash[$path] = $mt;
}
}
}
示例7: tick
/**
* Tick handler
*/
public function tick()
{
$events = inotify_read($this->inotifyStreamDescriptor);
if (is_array($events)) {
$this->handleEvents($events);
}
}
示例8: evaluate
/**
* Evaluate Filesystem changes
*
* @return mixed
*/
public function evaluate()
{
$this->tracker->clearChangeSet();
$this->modified = array();
$inEvents = inotify_read($this->inotify);
$inEvents = is_array($inEvents) ? $inEvents : array();
foreach ($inEvents as $inEvent) {
$this->translateEvent($inEvent);
}
}
示例9: getModifiedFiles
/**
* 获取更改的文件
* @return array
*/
public static function getModifiedFiles()
{
// 读取监控事件
$events = inotify_read(self::$inotifyFd);
if (empty($events)) {
return false;
}
// 获得哪些文件被修改
$modify_files = array();
foreach ($events as $ev) {
$modify_files[$ev['wd']] = self::$inotifyWatchFds[$ev['wd']];
}
return $modify_files;
}
示例10: __invoke
/**
* Checks all new inotify events available
* and emits them via evenement
*/
public function __invoke()
{
if (false !== ($events = \inotify_read($this->inotifyHandler))) {
foreach ($events as $event) {
// make sure the watch descriptor assigned to this event is
// still valid. removing watch descriptors via 'remove()'
// implicitly sends a final event with mask IN_IGNORE set:
// http://php.net/manual/en/inotify.constants.php#constant.in-ignored
if (isset($this->watchDescriptors[$event['wd']])) {
$path = $this->watchDescriptors[$event['wd']]['path'];
$this->emit($event['mask'], array($path . $event['name']));
}
}
}
}
示例11: 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);
}
}
示例12: run
/**
* @return Result
*/
public function run()
{
/** @noinspection PhpUndefinedFunctionInspection */
$inotify = inotify_init();
$watches = $this->createInotifyWatches($inotify);
stream_set_blocking($inotify, 0);
while (true) {
/** @noinspection PhpUndefinedFunctionInspection */
$events = inotify_read($inotify);
if ($events) {
if ($this->clear) {
passthru('bash -c clear');
}
$this->runTasksForInotifyEvents($events);
}
sleep(1);
}
$this->shutdownInotify($inotify, $watches);
return $this->gasp->result()->setStatus(Result::SUCCESS)->setMessage('And now his watch is ended.');
}
示例13: start
public function start()
{
$this->init();
$this->fd = fopen($this->path, 'r');
stream_set_blocking($this->fd, 0);
$pos = filesize($this->path);
while (true) {
$events = inotify_read($this->ifd);
foreach ($events as $event) {
switch (true) {
case $event['mask'] & self::MASK:
fseek($this->fd, $pos);
$buf = '';
while (!feof($this->fd)) {
$buf .= fread($this->fd, 8192);
}
$this->handler->InModify($buf, $this->filename);
$pos = ftell($this->fd);
break;
}
}
}
}
示例14: __construct
/**
* @param $serverPid
* @throws NotFound
*/
function __construct($serverPid)
{
$this->pid = $serverPid;
if (posix_kill($serverPid, 0) === false) {
throw new NotFound("Process#{$serverPid} not found.");
}
$this->inotify = inotify_init();
$this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE;
swoole_event_add($this->inotify, function ($ifd) {
$events = inotify_read($this->inotify);
if (!$events) {
return;
}
var_dump($events);
foreach ($events as $ev) {
if ($ev['mask'] == IN_IGNORED) {
continue;
} else {
if ($ev['mask'] == IN_CREATE or $ev['mask'] == IN_DELETE or $ev['mask'] == IN_MODIFY or $ev['mask'] == IN_MOVED_TO or $ev['mask'] == IN_MOVED_FROM) {
$fileType = strstr($ev['name'], '.');
//非重启类型
if (!isset($this->reloadFileTypes[$fileType])) {
continue;
}
}
}
//正在reload,不再接受任何事件,冻结10秒
if (!$this->reloading) {
$this->putLog("after 10 seconds reload the server");
//有事件发生了,进行重启
swoole_timer_after($this->afterNSeconds * 1000, array($this, 'reload'));
$this->reloading = true;
}
}
});
}
示例15: readEvents
/**
* Returns all events happened since last event readout
*
* @return array
*/
protected function readEvents()
{
return inotify_read($this->inotify);
}