ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Linux-Inotify2/Inotify2.pm
Revision: 1.26
Committed: Fri May 4 19:38:06 2012 UTC (14 years, 4 months ago) by root
Branch: MAIN
Changes since 1.25: +1 -3 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Linux::Inotify2 - scalable directory/file change notification
4
5 =head1 SYNOPSIS
6
7 =head2 Callback Interface
8
9 use Linux::Inotify2;
10
11 # create a new object
12 my $inotify = new Linux::Inotify2
13 or die "unable to create new inotify object: $!";
14
15 # add watchers
16 $inotify->watch ("/etc/passwd", IN_ACCESS, sub {
17 my $e = shift;
18 my $name = $e->fullname;
19 print "$name was accessed\n" if $e->IN_ACCESS;
20 print "$name is no longer mounted\n" if $e->IN_UNMOUNT;
21 print "$name is gone\n" if $e->IN_IGNORED;
22 print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
23
24 # cancel this watcher: remove no further events
25 $e->w->cancel;
26 });
27
28 # integration into AnyEvent (works with EV, Glib, Tk, POE...)
29 my $inotify_w = AE::io $inotify->fileno, 0, sub { $inotify->poll };
30
31 # manual event loop
32 1 while $inotify->poll;
33
34 =head2 Streaming Interface
35
36 use Linux::Inotify2 ;
37
38 # create a new object
39 my $inotify = new Linux::Inotify2
40 or die "Unable to create new inotify object: $!" ;
41
42 # create watch
43 $inotify->watch ("/etc/passwd", IN_ACCESS)
44 or die "watch creation failed" ;
45
46 while () {
47 my @events = $inotify->read;
48 unless (@events > 0) {
49 print "read error: $!";
50 last ;
51 }
52 printf "mask\t%d\n", $_->mask foreach @events ;
53 }
54
55 =head1 DESCRIPTION
56
57 This module implements an interface to the Linux 2.6.13 and later Inotify
58 file/directory change notification system.
59
60 It has a number of advantages over the Linux::Inotify module:
61
62 - it is portable (Linux::Inotify only works on x86)
63 - the equivalent of fullname works correctly
64 - it is better documented
65 - it has callback-style interface, which is better suited for
66 integration.
67
68 =head2 The Linux::Inotify2 Class
69
70 =over 4
71
72 =cut
73
74 package Linux::Inotify2;
75
76 use Scalar::Util ();
77
78 use common::sense;
79
80 use Exporter qw(import);
81
82 BEGIN {
83 our $VERSION = '1.22';
84 our @EXPORT = qw(
85 IN_ACCESS IN_MODIFY IN_ATTRIB IN_CLOSE_WRITE
86 IN_CLOSE_NOWRITE IN_OPEN IN_MOVED_FROM IN_MOVED_TO
87 IN_CREATE IN_DELETE IN_DELETE_SELF IN_MOVE_SELF
88 IN_ALL_EVENTS
89 IN_UNMOUNT IN_Q_OVERFLOW IN_IGNORED
90 IN_CLOSE IN_MOVE
91 IN_ISDIR IN_ONESHOT IN_MASK_ADD IN_DONT_FOLLOW IN_ONLYDIR
92 );
93
94 require XSLoader;
95 XSLoader::load Linux::Inotify2, $VERSION;
96 }
97
98 =item my $inotify = new Linux::Inotify2
99
100 Create a new notify object and return it. A notify object is kind of a
101 container that stores watches on file system names and is responsible for
102 handling event data.
103
104 On error, C<undef> is returned and C<$!> will be set accordingly. The
105 following errors are documented:
106
107 ENFILE The system limit on the total number of file descriptors has been reached.
108 EMFILE The user limit on the total number of inotify instances has been reached.
109 ENOMEM Insufficient kernel memory is available.
110
111 Example:
112
113 my $inotify = new Linux::Inotify2
114 or die "Unable to create new inotify object: $!";
115
116 =cut
117
118 sub new {
119 my ($class) = @_;
120
121 my $fd = inotify_init;
122
123 return unless $fd >= 0;
124
125 bless { fd => $fd }, $class
126 }
127
128 =item $watch = $inotify->watch ($name, $mask[, $cb])
129
130 Add a new watcher to the given notifier. The watcher will create events
131 on the pathname C<$name> as given in C<$mask>, which can be any of the
132 following constants (all exported by default) ORed together.
133
134 "file" refers to any file system object in the watched object (always a
135 directory), that is files, directories, symlinks, device nodes etc., while
136 "object" refers to the object the watcher has been set on itself:
137
138 IN_ACCESS object was accessed
139 IN_MODIFY object was modified
140 IN_ATTRIB object metadata changed
141 IN_CLOSE_WRITE writable fd to file / to object was closed
142 IN_CLOSE_NOWRITE readonly fd to file / to object closed
143 IN_OPEN object was opened
144 IN_MOVED_FROM file was moved from this object (directory)
145 IN_MOVED_TO file was moved to this object (directory)
146 IN_CREATE file was created in this object (directory)
147 IN_DELETE file was deleted from this object (directory)
148 IN_DELETE_SELF object itself was deleted
149 IN_MOVE_SELF object itself was moved
150 IN_ALL_EVENTS all of the above events
151
152 IN_ONESHOT only send event once
153 IN_ONLYDIR only watch the path if it is a directory
154 IN_DONT_FOLLOW don't follow a sym link
155 IN_MASK_ADD not supported with the current version of this module
156
157 IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
158 IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO
159
160 C<$cb> is a perl code reference that, if given, is called for each
161 event. It receives a C<Linux::Inotify2::Event> object.
162
163 The returned C<$watch> object is of class C<Linux::Inotify2::Watch>.
164
165 On error, C<undef> is returned and C<$!> will be set accordingly. The
166 following errors are documented:
167
168 EBADF The given file descriptor is not valid.
169 EINVAL The given event mask contains no legal events.
170 ENOMEM Insufficient kernel memory was available.
171 ENOSPC The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource.
172 EACCESS Read access to the given file is not permitted.
173
174 Example, show when C</etc/passwd> gets accessed and/or modified once:
175
176 $inotify->watch ("/etc/passwd", IN_ACCESS | IN_MODIFY, sub {
177 my $e = shift;
178 print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS;
179 print "$e->{w}{name} was modified\n" if $e->IN_MODIFY;
180 print "$e->{w}{name} is no longer mounted\n" if $e->IN_UNMOUNT;
181 print "events for $e->{w}{name} have been lost\n" if $e->IN_Q_OVERFLOW;
182
183 $e->w->cancel;
184 });
185
186 =cut
187
188 sub watch {
189 my ($self, $name, $mask, $cb) = @_;
190
191 my $wd = inotify_add_watch $self->{fd}, $name, $mask;
192
193 return unless $wd >= 0;
194
195 my $w = $self->{w}{$wd} = bless {
196 inotify => $self,
197 wd => $wd,
198 name => $name,
199 mask => $mask,
200 cb => $cb,
201 }, "Linux::Inotify2::Watch";
202
203 Scalar::Util::weaken $w->{inotify};
204
205 $w
206 }
207
208 =item $inotify->fileno
209
210 Returns the file descriptor for this notify object. When in non-blocking
211 mode, you are responsible for calling the C<poll> method when this file
212 descriptor becomes ready for reading.
213
214 =cut
215
216 sub fileno {
217 $_[0]{fd}
218 }
219
220 =item $inotify->blocking ($blocking)
221
222 Clears ($blocking true) or sets ($blocking false) the C<O_NONBLOCK> flag on the file descriptor.
223
224 =cut
225
226 sub blocking {
227 my ($self, $blocking) = @_;
228
229 inotify_blocking $self->{fd}, $blocking;
230 }
231
232 =item $count = $inotify->poll
233
234 Reads events from the kernel and handles them. If the notify file
235 descriptor is blocking (the default), then this method waits for at least
236 one event (and thus returns true unless an error occurs). Otherwise it
237 returns immediately when no pending events could be read.
238
239 Returns the count of events that have been handled.
240
241 =cut
242
243 sub poll {
244 scalar &read
245 }
246
247 =item @events = $inotify->read
248
249 Reads events from the kernel. Blocks when the file descriptor is in
250 blocking mode (default) until any event arrives. Returns list of
251 C<Linux::Inotify2::Event> objects or empty list if none (non-blocking
252 mode) or error occurred ($! should be checked).
253
254 Normally you shouldn't use this function, but instead use watcher
255 callbacks and call C<< ->poll >>.
256
257 =cut
258
259 sub read {
260 my ($self) = @_;
261
262 my @ev = inotify_read $self->{fd};
263 my @res;
264
265 for (@ev) {
266 my $w = $_->{w} = $self->{w}{$_->{wd}}
267 or next; # no such watcher
268
269 exists $self->{ignore}{$_->{wd}}
270 and next; # watcher has been canceled
271
272 bless $_, "Linux::Inotify2::Event";
273
274 push @res, $_;
275
276 $w->{cb}->($_) if $w->{cb};
277 $w->cancel if $_->{mask} & (IN_IGNORED | IN_UNMOUNT | IN_ONESHOT | IN_DELETE_SELF);
278 }
279
280 delete $self->{ignore};
281
282 @res
283 }
284
285 sub DESTROY {
286 inotify_close $_[0]{fd}
287 }
288
289 =back
290
291 =head2 The Linux::Inotify2::Event Class
292
293 Objects of this class are handed as first argument to the watcher
294 callback. It has the following members and methods:
295
296 =over 4
297
298 =item $event->w
299
300 =item $event->{w}
301
302 The watcher object for this event.
303
304 =item $event->name
305
306 =item $event->{name}
307
308 The path of the file system object, relative to the watched name.
309
310 =item $event->fullname
311
312 Returns the "full" name of the relevant object, i.e. including the C<name>
313 member of the watcher (if the watch object is on a directory and a
314 directory entry is affected), or simply the C<name> member itself when the
315 object is the watch object itself.
316
317 =item $event->mask
318
319 =item $event->{mask}
320
321 The received event mask. In addition to the events described for C<<
322 $inotify->watch >>, the following flags (exported by default) can be set:
323
324 IN_ISDIR event object is a directory
325 IN_Q_OVERFLOW event queue overflowed
326
327 # when any of the following flags are set,
328 # then watchers for this event are automatically canceled
329 IN_UNMOUNT filesystem for watched object was unmounted
330 IN_IGNORED file was ignored/is gone (no more events are delivered)
331 IN_ONESHOT only one event was generated
332
333 =item $event->IN_xxx
334
335 Returns a boolean that returns true if the event mask contains any events
336 specified by the mask. All of the C<IN_xxx> constants can be used as
337 methods.
338
339 =item $event->cookie
340
341 =item $event->{cookie}
342
343 The event cookie to "synchronize two events". Normally zero, this value is
344 set when two events relating to the same file are generated. As far as I
345 know, this only happens for C<IN_MOVED_FROM> and C<IN_MOVED_TO> events, to
346 identify the old and new name of a file.
347
348 =back
349
350 =cut
351
352 package Linux::Inotify2::Event;
353
354 sub w { $_[0]{w} }
355 sub name { $_[0]{name} }
356 sub mask { $_[0]{mask} }
357 sub cookie { $_[0]{cookie} }
358
359 sub fullname {
360 length $_[0]{name}
361 ? "$_[0]{w}{name}/$_[0]{name}"
362 : $_[0]{w}{name};
363 }
364
365 for my $name (@Linux::Inotify2::EXPORT) {
366 my $mask = &{"Linux::Inotify2::$name"};
367
368 *$name = sub { $_[0]{mask} & $mask };
369 }
370
371 =head2 The Linux::Inotify2::Watch Class
372
373 Watcher objects are created by calling the C<watch> method of a notifier.
374
375 It has the following members and methods:
376
377 =over 4
378
379 =item $watch->name
380
381 =item $watch->{name}
382
383 The name as specified in the C<watch> call. For the object itself, this is
384 the empty string. For directory watches, this is the name of the entry
385 without leading path elements.
386
387 =item $watch->mask
388
389 =item $watch->{mask}
390
391 The mask as specified in the C<watch> call.
392
393 =item $watch->cb ([new callback])
394
395 =item $watch->{cb}
396
397 The callback as specified in the C<watch> call. Can optionally be changed.
398
399 =item $watch->cancel
400
401 Cancels/removes this watcher. Future events, even if already queued queued,
402 will not be handled and resources will be freed.
403
404 =back
405
406 =cut
407
408 package Linux::Inotify2::Watch;
409
410 sub name { $_[0]{name} }
411 sub mask { $_[0]{mask} }
412
413 sub cb {
414 $_[0]{cb} = $_[1] if @_ > 1;
415 $_[0]{cb}
416 }
417
418 sub cancel {
419 my ($self) = @_;
420
421 my $inotify = delete $self->{inotify}
422 or return 1; # already canceled
423
424 delete $inotify->{w}{$self->{wd}}; # we are no longer there
425 $inotify->{ignore}{$self->{wd}} = 1; # ignore further events for one poll
426
427 (Linux::Inotify2::inotify_rm_watch $inotify->{fd}, $self->{wd})
428 ? 1 : undef
429 }
430
431 =head1 SEE ALSO
432
433 L<AnyEvent>, L<Linux::Inotify>.
434
435 =head1 AUTHOR
436
437 Marc Lehmann <schmorp@schmorp.de>
438 http://home.schmorp.de/
439
440 =cut
441
442 1