-
Notifications
You must be signed in to change notification settings - Fork 7
/
inotify.php
72 lines (56 loc) · 2.18 KB
/
inotify.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/*
The inotify extension exposes the inotify functions inotify_init(),
inotify_add_watch() and inotify_rm_watch(), which work just like the C
ones.
As the C inotify_init() function returns a file descriptor, PHP's
inotify_init() returns a stream resource, so you can use the standard
stream functions on it, like stream_select(), stream_set_blocking(),
and also fclose(), used to close an inotify instance and all its
watches.
It also exports two additional functions:
inotify_queue_len(resource $inotify_instance), which returns a number
upper than zero if their are waiting events
inotify_read(resource $inotify_instance), which replaces the C way
of reading inotify events. It returns arrays of events, in which keys
represents the members of the inotify_event structure.
*/
// 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);
// it may return something like this:
array(
array(
'wd' => 1, // $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)
),
);
print_r($events);
// If we do not want to block on inotify_read(), we may
// - Use stream_select() on $fd:
$read = array($fd);
$write = null;
$except = null;
stream_select($read,$write,$except,0);
// - Use stream_set_blocking() on $fd
stream_set_blocking($fd, 0);
inotify_read($fd); // Does no block, and return false if no events are pending
// - Use inotify_queue_len() to check if event queue is not empty
$queue_len = inotify_queue_len($fd); // If > 0, we can be assured that
// inotify_read() will not block
// We do not need anymore to watch events, closing them
// Stop watching __FILE__ for metadata changes
inotify_rm_watch($fd, $watch_descriptor);
// Close our inotify instance
// This may have closed all watches if we haven't already closed them
fclose($fd);
?>