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