ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Linux-Inotify2/Inotify2.pm
(Generate patch)

Comparing cvsroot/Linux-Inotify2/Inotify2.pm (file contents):
Revision 1.10 by root, Sun Nov 27 12:43:40 2005 UTC vs.
Revision 1.11 by root, Mon Dec 19 16:50:27 2005 UTC

1=head1 NAME 1=head1 NAME
2 2
3Linux::Inotify2 - scalable directory/file change notification 3Linux::Inotify2 - scalable directory/file change notification
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6
7=head2 Callback interface
6 8
7 use Linux::Inotify2; 9 use Linux::Inotify2;
8 10
9 # create a new object 11 # create a new object
10 my $inotify = new Linux::Inotify2 12 my $inotify = new Linux::Inotify2
24 print "$name was accessed\n" if $e->IN_ACCESS; 26 print "$name was accessed\n" if $e->IN_ACCESS;
25 print "$name is no longer mounted\n" if $e->IN_UNMOUNT; 27 print "$name is no longer mounted\n" if $e->IN_UNMOUNT;
26 print "$name is gone\n" if $e->IN_IGNORED; 28 print "$name is gone\n" if $e->IN_IGNORED;
27 print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW; 29 print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
28 30
29 # cancel this watcheR: remove no further events 31 # cancel this watcher: remove no further events
30 $e->w->cancel; 32 $e->w->cancel;
31 }); 33 });
34
35=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 }
32 55
33=head1 DESCRIPTION 56=head1 DESCRIPTION
34 57
35This module implements an interface to the Linux 2.6.13 and later Inotify 58This module implements an interface to the Linux 2.6.13 and later Inotify
36file/directory change notification sytem. 59file/directory change notification sytem.
50=cut 73=cut
51 74
52package Linux::Inotify2; 75package Linux::Inotify2;
53 76
54use Carp (); 77use Carp ();
78use Fcntl ();
55use Scalar::Util (); 79use Scalar::Util ();
56 80
57use base 'Exporter'; 81use base 'Exporter';
58 82
59BEGIN { 83BEGIN {
60 $VERSION = 0.8; 84 $VERSION = '1.0';
61 85
62 @constants = qw( 86 @constants = qw(
63 IN_ACCESS IN_MODIFY IN_ATTRIB IN_CLOSE_WRITE 87 IN_ACCESS IN_MODIFY IN_ATTRIB IN_CLOSE_WRITE
64 IN_CLOSE_NOWRITE IN_OPEN IN_MOVED_FROM IN_MOVED_TO 88 IN_CLOSE_NOWRITE IN_OPEN IN_MOVED_FROM IN_MOVED_TO
65 IN_CREATE IN_DELETE IN_DELETE_SELF IN_MOVE_SELF 89 IN_CREATE IN_DELETE IN_DELETE_SELF IN_MOVE_SELF
103 return unless $fd >= 0; 127 return unless $fd >= 0;
104 128
105 bless { fd => $fd }, $class 129 bless { fd => $fd }, $class
106} 130}
107 131
108=item $watch = $inotify->watch ($name, $mask, $cb) 132=item $watch = $inotify->watch ($name, $mask[, $cb])
109 133
110Add a new watcher to the given notifier. The watcher will create events 134Add a new watcher to the given notifier. The watcher will create events
111on the pathname C<$name> as given in C<$mask>, which can be any of the 135on the pathname C<$name> as given in C<$mask>, which can be any of the
112following constants (all exported by default) ORed together. 136following constants (all exported by default) ORed together.
113 137
132 IN_ONESHOT only send event once 156 IN_ONESHOT only send event once
133 157
134 IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE 158 IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
135 IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO 159 IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO
136 160
137C<$cb> is a perl code reference that is called for each event. It receives 161C<$cb> is a perl code reference that, if given, is called for each
138a C<Linux::Inotify2::Event> object. 162event. It receives a C<Linux::Inotify2::Event> object.
139 163
140The returned C<$watch> object is of class C<Linux::Inotify2::Watch>. 164The returned C<$watch> object is of class C<Linux::Inotify2::Watch>.
141 165
142On error, C<undef> is returned and C<$!> will be set accordingly. The 166On error, C<undef> is returned and C<$!> will be set accordingly. The
143following errors are documented: 167following errors are documented:
191 215
192sub fileno { 216sub fileno {
193 $_[0]{fd} 217 $_[0]{fd}
194} 218}
195 219
220=item $inotify->blocking ($blocking)
221
222Clears ($blocking true) or sets ($blocking false) the C<O_NONBLOCK> flag on the file descriptor.
223
224=cut
225
226sub blocking {
227 my ($self, $blocking) = @_;
228
229 inotify_blocking $self->{fd}, $blocking;
230}
231
196=item $count = $inotify->poll 232=item $count = $inotify->poll
197 233
198Reads events from the kernel and handles them. If the notify fileno is 234Reads events from the kernel and handles them. If the notify fileno is
199blocking (the default), then this method waits for at least one event 235blocking (the default), then this method waits for at least one event
200(and thus returns true unless an error occurs). Otherwise it returns 236(and thus returns true unless an error occurs). Otherwise it returns
203Returns the count of events that have been handled. 239Returns the count of events that have been handled.
204 240
205=cut 241=cut
206 242
207sub poll { 243sub poll {
244 scalar &read
245}
246
247=item $count = $inotify->read
248
249Reads events from the kernel. Blocks in blocking mode (default) until any
250event arrives. Returns list of C<Linux::Inotify2::Event> objects or empty
251list if none (non-blocking mode) or error occured ($! should be checked).
252
253=cut
254
255sub read {
208 my ($self) = @_; 256 my ($self) = @_;
209 257
210 my @ev = inotify_read $self->{fd}; 258 my @ev = inotify_read $self->{fd};
259 my @res;
211 260
212 for (@ev) { 261 for (@ev) {
213 my $w = $_->{w} = $self->{w}{$_->{wd}} 262 my $w = $_->{w} = $self->{w}{$_->{wd}}
214 or next; # no such watcher 263 or next; # no such watcher
215 264
216 exists $self->{ignore}{$_->{wd}} 265 exists $self->{ignore}{$_->{wd}}
217 and next; # watcher has been canceled 266 and next; # watcher has been canceled
218 267
268 push @res, $_;
269
219 $w->{cb}->(bless $_, Linux::Inotify2::Event); 270 $w->{cb}->(bless $_, Linux::Inotify2::Event) if $w->{cb};
220 $w->cancel if $_->{mask} & (IN_IGNORED | IN_UNMOUNT | IN_ONESHOT); 271 $w->cancel if $_->{mask} & (IN_IGNORED | IN_UNMOUNT | IN_ONESHOT);
272
221 } 273 }
222 274
223 delete $self->{ignore}; 275 delete $self->{ignore};
224 276
225 scalar @ev 277 @res
226} 278}
227 279
228sub DESTROY { 280sub DESTROY {
229 inotify_close $_[0]{fd} 281 inotify_close $_[0]{fd}
230} 282}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines