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