ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-Inotify2/Inotify2.pm
Revision: 1.19
Committed: Tue Oct 7 17:25:11 2008 UTC (15 years, 9 months ago) by root
Branch: MAIN
CVS Tags: rel-1_2
Changes since 1.18: +4 -4 lines
Log Message:
1.2

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 POE, Glib, Tk...)
29 my $inotify_w = AnyEvent->io
30 fh => $inofity, poll => 'r', cb => sub { $inotify->poll }
31 );
32
33 # manual event loop
34 1 while $inotify->poll;
35
36 =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 =head1 DESCRIPTION
58
59 This module implements an interface to the Linux 2.6.13 and later Inotify
60 file/directory change notification sytem.
61
62 It has a number of advantages over the Linux::Inotify module:
63
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 =head2 The Linux::Inotify2 Class
71
72 =over 4
73
74 =cut
75
76 package Linux::Inotify2;
77
78 use Carp ();
79 use Fcntl ();
80 use Scalar::Util ();
81
82 use base 'Exporter';
83
84 BEGIN {
85 $VERSION = '1.2';
86
87 @constants = qw(
88 IN_ACCESS IN_MODIFY IN_ATTRIB IN_CLOSE_WRITE
89 IN_CLOSE_NOWRITE IN_OPEN IN_MOVED_FROM IN_MOVED_TO
90 IN_CREATE IN_DELETE IN_DELETE_SELF IN_MOVE_SELF
91 IN_ALL_EVENTS
92 IN_UNMOUNT IN_Q_OVERFLOW IN_IGNORED
93 IN_CLOSE IN_MOVE
94 IN_ISDIR IN_ONESHOT IN_MASK_ADD IN_DONT_FOLLOW IN_ONLYDIR
95 );
96
97 @EXPORT = @constants;
98
99 require XSLoader;
100 XSLoader::load Linux::Inotify2, $VERSION;
101 }
102
103 =item my $inotify = new Linux::Inotify2
104
105 Create a new notify object and return it. A notify object is kind of a
106 container that stores watches on filesystem names and is responsible for
107 handling event data.
108
109 On error, C<undef> is returned and C<$!> will be set accordingly. The followign errors
110 are documented:
111
112 ENFILE The system limit on the total number of file descriptors has been reached.
113 EMFILE The user limit on the total number of inotify instances has been reached.
114 ENOMEM Insufficient kernel memory is available.
115
116 Example:
117
118 my $inotify = new Linux::Inotify2
119 or die "Unable to create new inotify object: $!";
120
121 =cut
122
123 sub new {
124 my ($class) = @_;
125
126 my $fd = inotify_init;
127
128 return unless $fd >= 0;
129
130 bless { fd => $fd }, $class
131 }
132
133 =item $watch = $inotify->watch ($name, $mask[, $cb])
134
135 Add a new watcher to the given notifier. The watcher will create events
136 on the pathname C<$name> as given in C<$mask>, which can be any of the
137 following constants (all exported by default) ORed together.
138
139 "file" refers to any filesystem object in the watch'ed object (always a
140 directory), that is files, directories, symlinks, device nodes etc., while
141 "object" refers to the object the watch has been set on itself:
142
143 IN_ACCESS object was accessed
144 IN_MODIFY object was modified
145 IN_ATTRIB object metadata changed
146 IN_CLOSE_WRITE writable fd to file / to object was closed
147 IN_CLOSE_NOWRITE readonly fd to file / to object closed
148 IN_OPEN object was opened
149 IN_MOVED_FROM file was moved from this object (directory)
150 IN_MOVED_TO file was moved to this object (directory)
151 IN_CREATE file was created in this object (directory)
152 IN_DELETE file was deleted from this object (directory)
153 IN_DELETE_SELF object itself was deleted
154 IN_MOVE_SELF object itself was moved
155 IN_ALL_EVENTS all of the above events
156
157 IN_ONESHOT only send event once
158 IN_ONLYDIR only watch the path if it is a directory
159 IN_DONT_FOLLOW don't follow a sym link
160 IN_MASK_ADD not supported with the current version of this module
161
162 IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
163 IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO
164
165 C<$cb> is a perl code reference that, if given, is called for each
166 event. It receives a C<Linux::Inotify2::Event> object.
167
168 The returned C<$watch> object is of class C<Linux::Inotify2::Watch>.
169
170 On error, C<undef> is returned and C<$!> will be set accordingly. The
171 following errors are documented:
172
173 EBADF The given file descriptor is not valid.
174 EINVAL The given event mask contains no legal events.
175 ENOMEM Insufficient kernel memory was available.
176 ENOSPC The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource.
177 EACCESS Read access to the given file is not permitted.
178
179 Example, show when C</etc/passwd> gets accessed and/or modified once:
180
181 $inotify->watch ("/etc/passwd", IN_ACCESS | IN_MODIFY, sub {
182 my $e = shift;
183 print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS;
184 print "$e->{w}{name} was modified\n" if $e->IN_MODIFY;
185 print "$e->{w}{name} is no longer mounted\n" if $e->IN_UNMOUNT;
186 print "events for $e->{w}{name} have been lost\n" if $e->IN_Q_OVERFLOW;
187
188 $e->w->cancel;
189 });
190
191 =cut
192
193 sub watch {
194 my ($self, $name, $mask, $cb) = @_;
195
196 my $wd = inotify_add_watch $self->{fd}, $name, $mask;
197
198 return unless $wd >= 0;
199
200 my $w = $self->{w}{$wd} = bless {
201 inotify => $self,
202 wd => $wd,
203 name => $name,
204 mask => $mask,
205 cb => $cb,
206 }, "Linux::Inotify2::Watch";
207
208 Scalar::Util::weaken $w->{inotify};
209
210 $w
211 }
212
213 =item $inotify->fileno
214
215 Returns the fileno for this notify object. You are responsible for calling
216 the C<poll> method when this fileno becomes ready for reading.
217
218 =cut
219
220 sub fileno {
221 $_[0]{fd}
222 }
223
224 =item $inotify->blocking ($blocking)
225
226 Clears ($blocking true) or sets ($blocking false) the C<O_NONBLOCK> flag on the file descriptor.
227
228 =cut
229
230 sub blocking {
231 my ($self, $blocking) = @_;
232
233 inotify_blocking $self->{fd}, $blocking;
234 }
235
236 =item $count = $inotify->poll
237
238 Reads events from the kernel and handles them. If the notify fileno is
239 blocking (the default), then this method waits for at least one event
240 (and thus returns true unless an error occurs). Otherwise it returns
241 immediately when no pending events could be read.
242
243 Returns the count of events that have been handled.
244
245 =cut
246
247 sub poll {
248 scalar &read
249 }
250
251 =item $count = $inotify->read
252
253 Reads events from the kernel. Blocks in blocking mode (default) until any
254 event arrives. Returns list of C<Linux::Inotify2::Event> objects or empty
255 list if none (non-blocking mode) or error occured ($! should be checked).
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 watch
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 filesystem object, relative to the watch name.
309
310 =item $watch->fullname
311
312 Returns the "full" name of the relevant object, i.e. including the C<name>
313 member of the watcher (if the the watch is on a directory and a dir entry
314 is affected), or simply the C<name> member itself when the object is the
315 watch object itself.
316
317 =item $event->mask
318
319 =item $event->{mask}
320
321 The received event mask. In addition the the events described for
322 C<$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 watch'ed 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 matches the
336 event. All of the C<IN_xxx> constants can be used as methods.
337
338 =item $event->cookie
339
340 =item $event->{cookie}
341
342 The event cookie to "synchronize two events". Normally zero, this value is
343 set when two events relating to the same file are generated. As far as I
344 know, this only happens for C<IN_MOVED_FROM> and C<IN_MOVED_TO> events, to
345 identify the old and new name of a file.
346
347 =back
348
349 =cut
350
351 package Linux::Inotify2::Event;
352
353 sub w { $_[0]{w} }
354 sub name { $_[0]{name} }
355 sub mask { $_[0]{mask} }
356 sub cookie { $_[0]{cookie} }
357
358 sub fullname {
359 length $_[0]{name}
360 ? "$_[0]{w}{name}/$_[0]{name}"
361 : $_[0]{w}{name};
362 }
363
364 for my $name (@Linux::Inotify2::constants) {
365 my $mask = &{"Linux::Inotify2::$name"};
366
367 *$name = sub { ($_[0]{mask} & $mask) == $mask };
368 }
369
370 =head2 The Linux::Inotify2::Watch Class
371
372 Watch objects are created by calling the C<watch> method of a notifier.
373
374 It has the following members and methods:
375
376 =over 4
377
378 =item $watch->name
379
380 =item $watch->{name}
381
382 The name as specified in the C<watch> call. For the object itself, this is
383 the empty string. For directory watches, this is the name of the entry
384 without leading path elements.
385
386 =item $watch->mask
387
388 =item $watch->{mask}
389
390 The mask as specified in the C<watch> call.
391
392 =item $watch->cb ([new callback])
393
394 =item $watch->{cb}
395
396 The callback as specified in the C<watch> call. Can optionally be changed.
397
398 =item $watch->cancel
399
400 Cancels/removes this watch. Future events, even if already queued queued,
401 will not be handled and resources will be freed.
402
403 =back
404
405 =cut
406
407 package Linux::Inotify2::Watch;
408
409 sub name { $_[0]{name} }
410 sub mask { $_[0]{mask} }
411
412 sub cb {
413 $_[0]{cb} = $_[1] if @_ > 1;
414 $_[0]{cb}
415 }
416
417 sub cancel {
418 my ($self) = @_;
419
420 my $inotify = delete $self->{inotify}
421 or return 1; # already canceled
422
423 delete $inotify->{w}{$self->{wd}}; # we are no longer there
424 $inotify->{ignore}{$self->{wd}} = 1; # ignore further events for one poll
425
426 (Linux::Inotify2::inotify_rm_watch $inotify->{fd}, $self->{wd})
427 ? 1 : undef
428 }
429
430 =head1 SEE ALSO
431
432 L<AnyEvent>, L<Linux::Inotify>.
433
434 =head1 AUTHOR
435
436 Marc Lehmann <schmorp@schmorp.de>
437 http://home.schmorp.de/
438
439 =cut
440
441 1