inotify_add_watch

Add a watch to an initialized inotify instance

说明

int inotify_add_watch ( resource $inotify_instance , string $pathname , int $mask )

inotify_add_watch adds a new watch or modify an existing watch for the file or directory specified in pathname.

Using inotify_add_watch on a watched object replaces the existing watch. Using the IN_MASK_ADD constant adds (OR) events to the existing watch.

参数

inotify_instance
inotify_init返回的资源

pathname
File or directory to watch

mask
Events to watch for. See 预定义常量.

返回值

The return value is a unique (inotify instance wide) watch descriptor.

参见

  • inotify_init

inotify_init

Initialize an inotify instance

说明

resource inotify_init ( void )

Initialize an inotify instance for use with inotify_add_watch

返回值

A stream resource or FALSE on error.

范例

示例 #1 Example usage of inotify

<?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);

?>

以上例程的输出类似于:

array(
  array(
    'wd' => 1,     // Equals $watch_descriptor
    'mask' => 4,   // IN_ATTRIB bit is set
    'cookie' => 0, // unique id to connect related events (e.g. 
                   // IN_MOVE_FROM and IN_MOVE_TO events)
    'name' => '',  // the name of a file (e.g. if we monitored changes
                   // in a directory)
  ),
);

参见

  • inotify_add_watch
  • inotify_rm_watch
  • inotify_queue_len
  • inotify_read
  • fclose

inotify_queue_len

Return a number upper than zero if there are pending events

说明

int inotify_queue_len ( resource $inotify_instance )

This function allows to know if inotify_read will block or not. If a number upper than zero is returned, there are pending events and inotify_read will not block.

参数

inotify_instance
inotify_init返回的资源

返回值

Returns a number upper than zero if there are pending events.

参见

  • inotify_init
  • stream_select
  • stream_set_blocking

inotify_read

Read events from an inotify instance

说明

array inotify_read ( resource $inotify_instance )

Read inotify events from an inotify instance.

参数

inotify_instance
inotify_init返回的资源

返回值

An array of inotify events or FALSE if no events was pending and inotify_instance is non-blocking. Each event is an array with the following keys:

  • wd is a watch descriptor returned by inotify_add_watch
  • mask is a bit mask of events
  • cookie is a unique id to connect related events (e.g. IN_MOVE_FROM and IN_MOVE_TO)
  • name is the name of a file (e.g. if a file was modified in a watched directory)

参见

  • inotify_init
  • stream_select
  • stream_set_blocking
  • inotify_queue_len

inotify_rm_watch

Remove an existing watch from an inotify instance

说明

bool inotify_rm_watch ( resource $inotify_instance , int $watch_descriptor )

inotify_rm_watch removes the watch watch_descriptor from the inotify instance inotify_instance.

参数

inotify_instance
inotify_init返回的资源

watch_descriptor
Watch to remove from the instance

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • inotify_init

目录