| 1 |
NAME |
| 2 |
Linux::Inotify2 - scalable directory/file change notification |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
Callback interface |
| 6 |
use Linux::Inotify2; |
| 7 |
|
| 8 |
# create a new object |
| 9 |
my $inotify = new Linux::Inotify2 |
| 10 |
or die "Unable to create new inotify object: $!"; |
| 11 |
|
| 12 |
# for Event: |
| 13 |
Event->io (fd =>$inotify->fileno, poll => 'r', cb => sub { $inotify->poll }); |
| 14 |
# for Glib: |
| 15 |
add_watch Glib::IO $inotify->fileno, in => sub { $inotify->poll }; |
| 16 |
# manually: |
| 17 |
1 while $inotify->poll; |
| 18 |
|
| 19 |
# add watchers |
| 20 |
$inotify->watch ("/etc/passwd", IN_ACCESS, sub { |
| 21 |
my $e = shift; |
| 22 |
my $name = $e->fullname; |
| 23 |
print "$name was accessed\n" if $e->IN_ACCESS; |
| 24 |
print "$name is no longer mounted\n" if $e->IN_UNMOUNT; |
| 25 |
print "$name is gone\n" if $e->IN_IGNORED; |
| 26 |
print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW; |
| 27 |
|
| 28 |
# cancel this watcher: remove no further events |
| 29 |
$e->w->cancel; |
| 30 |
}); |
| 31 |
|
| 32 |
Streaming Interface |
| 33 |
use Linux::Inotify2 ; |
| 34 |
|
| 35 |
# create a new object |
| 36 |
my $inotify = new Linux::Inotify2 |
| 37 |
or die "Unable to create new inotify object: $!" ; |
| 38 |
|
| 39 |
# create watch |
| 40 |
$inotify->watch ("/etc/passwd", IN_ACCESS) |
| 41 |
or die "watch creation failed" ; |
| 42 |
|
| 43 |
while () { |
| 44 |
my @events = $inotify->read; |
| 45 |
unless (@events > 0) { |
| 46 |
print "read error: $!"; |
| 47 |
last ; |
| 48 |
} |
| 49 |
printf "mask\t%d\n", $_->mask foreach @events ; |
| 50 |
} |
| 51 |
|
| 52 |
DESCRIPTION |
| 53 |
This module implements an interface to the Linux 2.6.13 and later |
| 54 |
Inotify file/directory change notification sytem. |
| 55 |
|
| 56 |
It has a number of advantages over the Linux::Inotify module: |
| 57 |
|
| 58 |
- it is portable (Linux::Inotify only works on x86) |
| 59 |
- the equivalent of fullname works correctly |
| 60 |
- it is better documented |
| 61 |
- it has callback-style interface, which is better suited for |
| 62 |
integration. |
| 63 |
|
| 64 |
The Linux::Inotify2 Class |
| 65 |
my $inotify = new Linux::Inotify2 |
| 66 |
Create a new notify object and return it. A notify object is kind of |
| 67 |
a container that stores watches on filesystem names and is |
| 68 |
responsible for handling event data. |
| 69 |
|
| 70 |
On error, "undef" is returned and $! will be set accordingly. The |
| 71 |
followign errors are documented: |
| 72 |
|
| 73 |
ENFILE The system limit on the total number of file descriptors has been reached. |
| 74 |
EMFILE The user limit on the total number of inotify instances has been reached. |
| 75 |
ENOMEM Insufficient kernel memory is available. |
| 76 |
|
| 77 |
Example: |
| 78 |
|
| 79 |
my $inotify = new Linux::Inotify2 |
| 80 |
or die "Unable to create new inotify object: $!"; |
| 81 |
|
| 82 |
$watch = $inotify->watch ($name, $mask[, $cb]) |
| 83 |
Add a new watcher to the given notifier. The watcher will create |
| 84 |
events on the pathname $name as given in $mask, which can be any of |
| 85 |
the following constants (all exported by default) ORed together. |
| 86 |
|
| 87 |
"file" refers to any filesystem object in the watch'ed object |
| 88 |
(always a directory), that is files, directories, symlinks, device |
| 89 |
nodes etc., while "object" refers to the object the watch has been |
| 90 |
set on itself: |
| 91 |
|
| 92 |
IN_ACCESS object was accessed |
| 93 |
IN_MODIFY object was modified |
| 94 |
IN_ATTRIB object metadata changed |
| 95 |
IN_CLOSE_WRITE writable fd to file / to object was closed |
| 96 |
IN_CLOSE_NOWRITE readonly fd to file / to object closed |
| 97 |
IN_OPEN object was opened |
| 98 |
IN_MOVED_FROM file was moved from this object (directory) |
| 99 |
IN_MOVED_TO file was moved to this object (directory) |
| 100 |
IN_CREATE file was created in this object (directory) |
| 101 |
IN_DELETE file was deleted from this object (directory) |
| 102 |
IN_DELETE_SELF object itself was deleted |
| 103 |
IN_MOVE_SELF object itself was moved |
| 104 |
IN_ALL_EVENTS all of the above events |
| 105 |
|
| 106 |
IN_ONESHOT only send event once |
| 107 |
|
| 108 |
IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE |
| 109 |
IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO |
| 110 |
|
| 111 |
$cb is a perl code reference that, if given, is called for each |
| 112 |
event. It receives a "Linux::Inotify2::Event" object. |
| 113 |
|
| 114 |
The returned $watch object is of class "Linux::Inotify2::Watch". |
| 115 |
|
| 116 |
On error, "undef" is returned and $! will be set accordingly. The |
| 117 |
following errors are documented: |
| 118 |
|
| 119 |
EBADF The given file descriptor is not valid. |
| 120 |
EINVAL The given event mask contains no legal events. |
| 121 |
ENOMEM Insufficient kernel memory was available. |
| 122 |
ENOSPC The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource. |
| 123 |
EACCESS Read access to the given file is not permitted. |
| 124 |
|
| 125 |
Example, show when "/etc/passwd" gets accessed and/or modified once: |
| 126 |
|
| 127 |
$inotify->watch ("/etc/passwd", IN_ACCESS | IN_MODIFY, sub { |
| 128 |
my $e = shift; |
| 129 |
print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS; |
| 130 |
print "$e->{w}{name} was modified\n" if $e->IN_MODIFY; |
| 131 |
print "$e->{w}{name} is no longer mounted\n" if $e->IN_UNMOUNT; |
| 132 |
print "events for $e->{w}{name} have been lost\n" if $e->IN_Q_OVERFLOW; |
| 133 |
|
| 134 |
$e->w->cancel; |
| 135 |
}); |
| 136 |
|
| 137 |
$inotify->fileno |
| 138 |
Returns the fileno for this notify object. You are responsible for |
| 139 |
calling the "poll" method when this fileno becomes ready for |
| 140 |
reading. |
| 141 |
|
| 142 |
$inotify->blocking ($blocking) |
| 143 |
Clears ($blocking true) or sets ($blocking false) the "O_NONBLOCK" |
| 144 |
flag on the file descriptor. |
| 145 |
|
| 146 |
$count = $inotify->poll |
| 147 |
Reads events from the kernel and handles them. If the notify fileno |
| 148 |
is blocking (the default), then this method waits for at least one |
| 149 |
event (and thus returns true unless an error occurs). Otherwise it |
| 150 |
returns immediately when no pending events could be read. |
| 151 |
|
| 152 |
Returns the count of events that have been handled. |
| 153 |
|
| 154 |
$count = $inotify->read |
| 155 |
Reads events from the kernel. Blocks in blocking mode (default) |
| 156 |
until any event arrives. Returns list of "Linux::Inotify2::Event" |
| 157 |
objects or empty list if none (non-blocking mode) or error occured |
| 158 |
($! should be checked). |
| 159 |
|
| 160 |
The Linux::Inotify2::Event Class |
| 161 |
Objects of this class are handed as first argument to the watch |
| 162 |
callback. It has the following members and methods: |
| 163 |
|
| 164 |
$event->w |
| 165 |
$event->{w} |
| 166 |
The watcher object for this event. |
| 167 |
|
| 168 |
$event->name |
| 169 |
$event->{name} |
| 170 |
The path of the filesystem object, relative to the watch name. |
| 171 |
|
| 172 |
$watch->fullname |
| 173 |
Returns the "full" name of the relevant object, i.e. including the |
| 174 |
"name" member of the watcher (if the the watch is on a directory and |
| 175 |
a dir entry is affected), or simply the "name" member itself when |
| 176 |
the object is the watch object itself. |
| 177 |
|
| 178 |
$event->mask |
| 179 |
$event->{mask} |
| 180 |
The received event mask. In addition the the events described for |
| 181 |
"$inotify-"watch>, the following flags (exported by default) can be |
| 182 |
set: |
| 183 |
|
| 184 |
IN_ISDIR event object is a directory |
| 185 |
IN_Q_OVERFLOW event queue overflowed |
| 186 |
|
| 187 |
# when any of the following flags are set, |
| 188 |
# then watchers for this event are automatically canceled |
| 189 |
IN_UNMOUNT filesystem for watch'ed object was unmounted |
| 190 |
IN_IGNORED file was ignored/is gone (no more events are delivered) |
| 191 |
IN_ONESHOT only one event was generated |
| 192 |
|
| 193 |
$event->IN_xxx |
| 194 |
Returns a boolean that returns true if the event mask matches the |
| 195 |
event. All of the "IN_xxx" constants can be used as methods. |
| 196 |
|
| 197 |
$event->cookie |
| 198 |
$event->{cookie} |
| 199 |
The event cookie to "synchronize two events". Normally zero, this |
| 200 |
value is set when two events relating to the same file are |
| 201 |
generated. As far as I know, this only happens for "IN_MOVED_FROM" |
| 202 |
and "IN_MOVED_TO" events, to identify the old and new name of a |
| 203 |
file. |
| 204 |
|
| 205 |
The Linux::Inotify2::Watch Class |
| 206 |
Watch objects are created by calling the "watch" method of a notifier. |
| 207 |
|
| 208 |
It has the following members and methods: |
| 209 |
|
| 210 |
$watch->name |
| 211 |
$watch->{name} |
| 212 |
The name as specified in the "watch" call. For the object itself, this |
| 213 |
is the empty string. For directory watches, this is the name of the |
| 214 |
entry without leading path elements. |
| 215 |
|
| 216 |
$watch->mask |
| 217 |
$watch->{mask} |
| 218 |
The mask as specified in the "watch" call. |
| 219 |
|
| 220 |
$watch->cb ([new callback]) |
| 221 |
$watch->{cb} |
| 222 |
The callback as specified in the "watch" call. Can optionally be |
| 223 |
changed. |
| 224 |
|
| 225 |
$watch->cancel |
| 226 |
Cancels/removes this watch. Future events, even if already queued |
| 227 |
queued, will not be handled and resources will be freed. |
| 228 |
|
| 229 |
SEE ALSO |
| 230 |
Linux::Inotify. |
| 231 |
|
| 232 |
AUTHOR |
| 233 |
Marc Lehmann <schmorp@schmorp.de> |
| 234 |
http://home.schmorp.de/ |
| 235 |
|