ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Linux-Inotify2/Inotify2.pm
Revision: 1.11
Committed: Mon Dec 19 16:50:27 2005 UTC (18 years, 7 months ago) by root
Branch: MAIN
CVS Tags: rel-1_0
Changes since 1.10: +59 -7 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.11 =head2 Callback interface
8    
9 root 1.1 use Linux::Inotify2;
10    
11 root 1.4 # create a new object
12     my $inotify = new Linux::Inotify2
13     or die "Unable to create new inotify object: $!";
14    
15     # for Event:
16     Event->io (fd =>$inotify->fileno, poll => 'r', cb => sub { $inotify->poll });
17     # for Glib:
18     add_watch Glib::IO $inotify->fileno, in => sub { $inotify->poll };
19     # manually:
20     1 while $inotify->poll;
21    
22     # add watchers
23     $inotify->watch ("/etc/passwd", IN_ACCESS, sub {
24     my $e = shift;
25     my $name = $e->fullname;
26     print "$name was accessed\n" if $e->IN_ACCESS;
27     print "$name is no longer mounted\n" if $e->IN_UNMOUNT;
28     print "$name is gone\n" if $e->IN_IGNORED;
29     print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
30    
31 root 1.11 # cancel this watcher: remove no further events
32 root 1.4 $e->w->cancel;
33     });
34    
35 root 1.11 =head2 Streaming Interface
36    
37     use Linux::Inotify2 ;
38    
39     # create a new object
40     my $inotify = new Linux::Inotify2
41     or die "Unable to create new inotify object: $!" ;
42    
43     # create watch
44     $inotify->watch ("/etc/passwd", IN_ACCESS)
45     or die "watch creation failed" ;
46    
47     while () {
48     my @events = $inotify->read;
49     unless (@events > 0) {
50     print "read error: $!";
51     last ;
52     }
53     printf "mask\t%d\n", $_->mask foreach @events ;
54     }
55    
56 root 1.1 =head1 DESCRIPTION
57    
58 root 1.3 This module implements an interface to the Linux 2.6.13 and later Inotify
59     file/directory change notification sytem.
60 root 1.2
61 root 1.3 It has a number of advantages over the Linux::Inotify module:
62 root 1.2
63     - it is portable (Linux::Inotify only works on x86)
64     - the equivalent of fullname works correctly
65     - it is better documented
66     - it has callback-style interface, which is better suited for
67     integration.
68    
69 root 1.6 =head2 The Linux::Inotify2 Class
70    
71 root 1.1 =over 4
72    
73     =cut
74    
75     package Linux::Inotify2;
76    
77     use Carp ();
78 root 1.11 use Fcntl ();
79 root 1.1 use Scalar::Util ();
80    
81     use base 'Exporter';
82    
83     BEGIN {
84 root 1.11 $VERSION = '1.0';
85 root 1.1
86     @constants = qw(
87     IN_ACCESS IN_MODIFY IN_ATTRIB IN_CLOSE_WRITE
88     IN_CLOSE_NOWRITE IN_OPEN IN_MOVED_FROM IN_MOVED_TO
89 root 1.9 IN_CREATE IN_DELETE IN_DELETE_SELF IN_MOVE_SELF
90 root 1.1 IN_ALL_EVENTS
91     IN_UNMOUNT IN_Q_OVERFLOW IN_IGNORED
92     IN_CLOSE IN_MOVE
93     IN_ISDIR IN_ONESHOT
94     );
95    
96     @EXPORT = @constants;
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    
158 root 1.4 IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
159     IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO
160 root 1.1
161 root 1.11 C<$cb> is a perl code reference that, if given, is called for each
162     event. It receives a C<Linux::Inotify2::Event> object.
163 root 1.1
164     The returned C<$watch> object is of class C<Linux::Inotify2::Watch>.
165    
166     On error, C<undef> is returned and C<$!> will be set accordingly. The
167     following errors are documented:
168    
169     EBADF The given file descriptor is not valid.
170     EINVAL The given event mask contains no legal events.
171     ENOMEM Insufficient kernel memory was available.
172     ENOSPC The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource.
173     EACCESS Read access to the given file is not permitted.
174    
175     Example, show when C</etc/passwd> gets accessed and/or modified once:
176    
177     $inotify->watch ("/etc/passwd", IN_ACCESS | IN_MODIFY, sub {
178     my $e = shift;
179     print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS;
180     print "$e->{w}{name} was modified\n" if $e->IN_MODIFY;
181     print "$e->{w}{name} is no longer mounted\n" if $e->IN_UNMOUNT;
182     print "events for $e->{w}{name} have been lost\n" if $e->IN_Q_OVERFLOW;
183    
184     $e->w->cancel;
185     });
186    
187     =cut
188    
189     sub watch {
190     my ($self, $name, $mask, $cb) = @_;
191    
192     my $wd = inotify_add_watch $self->{fd}, $name, $mask;
193    
194     return unless $wd >= 0;
195    
196     my $w = $self->{w}{$wd} = bless {
197     inotify => $self,
198     wd => $wd,
199     name => $name,
200     mask => $mask,
201     cb => $cb,
202     }, Linux::Inotify2::Watch;
203    
204     Scalar::Util::weaken $w->{inotify};
205    
206     $w
207     }
208    
209 root 1.4 =item $inotify->fileno
210 root 1.1
211     Returns the fileno for this notify object. You are responsible for calling
212     the C<poll> method when this fileno becomes ready for reading.
213    
214     =cut
215    
216     sub fileno {
217     $_[0]{fd}
218     }
219    
220 root 1.11 =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 root 1.4 =item $count = $inotify->poll
233 root 1.1
234 root 1.4 Reads events from the kernel and handles them. If the notify fileno is
235     blocking (the default), then this method waits for at least one event
236     (and thus returns true unless an error occurs). Otherwise it returns
237     immediately when no pending events could be read.
238 root 1.1
239     Returns the count of events that have been handled.
240    
241     =cut
242    
243     sub poll {
244 root 1.11 scalar &read
245     }
246    
247     =item $count = $inotify->read
248    
249     Reads events from the kernel. Blocks in blocking mode (default) until any
250     event arrives. Returns list of C<Linux::Inotify2::Event> objects or empty
251     list if none (non-blocking mode) or error occured ($! should be checked).
252    
253     =cut
254    
255     sub read {
256 root 1.1 my ($self) = @_;
257    
258 root 1.4 my @ev = inotify_read $self->{fd};
259 root 1.11 my @res;
260 root 1.4
261     for (@ev) {
262     my $w = $_->{w} = $self->{w}{$_->{wd}}
263 root 1.1 or next; # no such watcher
264 root 1.4
265     exists $self->{ignore}{$_->{wd}}
266     and next; # watcher has been canceled
267    
268 root 1.11 push @res, $_;
269    
270     $w->{cb}->(bless $_, Linux::Inotify2::Event) if $w->{cb};
271 root 1.10 $w->cancel if $_->{mask} & (IN_IGNORED | IN_UNMOUNT | IN_ONESHOT);
272 root 1.11
273 root 1.1 }
274 root 1.4
275     delete $self->{ignore};
276    
277 root 1.11 @res
278 root 1.1 }
279    
280     sub DESTROY {
281     inotify_close $_[0]{fd}
282     }
283    
284     =back
285    
286     =head2 The Linux::Inotify2::Event Class
287    
288     Objects of this class are handed as first argument to the watch
289     callback. It has the following members and methods:
290    
291     =over 4
292    
293     =item $event->w
294    
295     =item $event->{w}
296    
297     The watcher object for this event.
298    
299     =item $event->name
300    
301     =item $event->{name}
302    
303     The path of the filesystem object, relative to the watch name.
304    
305     =item $watch->fullname
306    
307     Returns the "full" name of the relevant object, i.e. including the C<name>
308 root 1.7 member of the watcher (if the the watch is on a directory and a dir entry
309     is affected), or simply the C<name> member itself when the object is the
310     watch object itself.
311 root 1.1
312     =item $event->mask
313    
314     =item $event->{mask}
315    
316     The received event mask. In addition the the events described for
317     C<$inotify->watch>, the following flags (exported by default) can be set:
318    
319 root 1.4 IN_ISDIR event object is a directory
320     IN_Q_OVERFLOW event queue overflowed
321    
322 root 1.10 # when any of the following flags are set,
323     # then watchers for this event are automatically canceled
324 root 1.4 IN_UNMOUNT filesystem for watch'ed object was unmounted
325     IN_IGNORED file was ignored/is gone (no more events are delivered)
326 root 1.10 IN_ONESHOT only one event was generated
327 root 1.1
328     =item $event->IN_xxx
329    
330     Returns a boolean that returns true if the event mask matches the
331     event. All of the C<IN_xxx> constants can be used as methods.
332    
333     =item $event->cookie
334    
335     =item $event->{cookie}
336    
337 root 1.10 The event cookie to "synchronize two events". Normally zero, this value is
338     set when two events relating to the same file are generated. As far as I
339     know, this only happens for C<IN_MOVED_FROM> and C<IN_MOVED_TO> events, to
340     identify the old and new name of a file.
341 root 1.1
342     =back
343    
344     =cut
345    
346     package Linux::Inotify2::Event;
347    
348     sub w { $_[0]{w} }
349     sub name { $_[0]{name} }
350     sub mask { $_[0]{mask} }
351     sub cookie { $_[0]{cookie} }
352    
353     sub fullname {
354     length $_[0]{name}
355     ? "$_[0]{w}{name}/$_[0]{name}"
356     : $_[0]{w}{name};
357     }
358    
359     for my $name (@Linux::Inotify2::constants) {
360     my $mask = &{"Linux::Inotify2::$name"};
361    
362     *$name = sub { ($_[0]{mask} & $mask) == $mask };
363     }
364    
365     =head2 The Linux::Inotify2::Watch Class
366    
367     Watch objects are created by calling the C<watch> method of a notifier.
368    
369     It has the following members and methods:
370    
371     =item $watch->name
372    
373     =item $watch->{name}
374    
375     The name as specified in the C<watch> call. For the object itself, this is
376     the empty string. For directory watches, this is the name of the entry
377     without leading path elements.
378    
379     =item $watch->mask
380    
381     =item $watch->{mask}
382    
383     The mask as specified in the C<watch> call.
384    
385     =item $watch->cb ([new callback])
386    
387     =item $watch->{cb}
388    
389     The callback as specified in the C<watch> call. Can optionally be changed.
390    
391     =item $watch->cancel
392    
393     Cancels/removes this watch. Future events, even if already queued queued,
394     will not be handled and resources will be freed.
395    
396     =cut
397    
398     package Linux::Inotify2::Watch;
399    
400     sub name { $_[0]{name} }
401     sub mask { $_[0]{mask} }
402    
403     sub cb {
404     $_[0]{cb} = $_[1] if @_ > 1;
405     $_[0]{cb}
406     }
407    
408     sub cancel {
409     my ($self) = @_;
410    
411 root 1.4 my $inotify = delete $self->{inotify}
412     or return 1; # already canceled
413    
414     delete $inotify->{w}{$self->{wd}}; # we are no longer there
415     $inotify->{ignore}{$self->{wd}} = 1; # ignore further events for one poll
416    
417     (Linux::Inotify2::inotify_rm_watch $inotify->{fd}, $self->{wd})
418 root 1.1 ? 1 : undef
419     }
420    
421     =head1 SEE ALSO
422    
423     L<Linux::Inotify>.
424    
425     =head1 AUTHOR
426    
427     Marc Lehmann <schmorp@schmorp.de>
428     http://home.schmorp.de/
429    
430     =cut
431    
432     1