ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Linux-Inotify2/Inotify2.pm
Revision: 1.21
Committed: Thu Sep 24 02:58:36 2009 UTC (16 years, 11 months ago) by root
Branch: MAIN
Changes since 1.20: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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