1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Linux::Inotify2 - scalable directory/file change notification |
3 | Linux::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 | |
35 | This module implements an interface to the Linux 2.6.13 and later Inotify |
58 | This module implements an interface to the Linux 2.6.13 and later Inotify |
36 | file/directory change notification sytem. |
59 | file/directory change notification sytem. |
… | |
… | |
50 | =cut |
73 | =cut |
51 | |
74 | |
52 | package Linux::Inotify2; |
75 | package Linux::Inotify2; |
53 | |
76 | |
54 | use Carp (); |
77 | use Carp (); |
|
|
78 | use Fcntl (); |
55 | use Scalar::Util (); |
79 | use Scalar::Util (); |
56 | |
80 | |
57 | use base 'Exporter'; |
81 | use base 'Exporter'; |
58 | |
82 | |
59 | BEGIN { |
83 | BEGIN { |
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 | |
110 | Add a new watcher to the given notifier. The watcher will create events |
134 | Add a new watcher to the given notifier. The watcher will create events |
111 | on the pathname C<$name> as given in C<$mask>, which can be any of the |
135 | on the pathname C<$name> as given in C<$mask>, which can be any of the |
112 | following constants (all exported by default) ORed together. |
136 | following 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 | |
137 | C<$cb> is a perl code reference that is called for each event. It receives |
161 | C<$cb> is a perl code reference that, if given, is called for each |
138 | a C<Linux::Inotify2::Event> object. |
162 | event. It receives a C<Linux::Inotify2::Event> object. |
139 | |
163 | |
140 | The returned C<$watch> object is of class C<Linux::Inotify2::Watch>. |
164 | The returned C<$watch> object is of class C<Linux::Inotify2::Watch>. |
141 | |
165 | |
142 | On error, C<undef> is returned and C<$!> will be set accordingly. The |
166 | On error, C<undef> is returned and C<$!> will be set accordingly. The |
143 | following errors are documented: |
167 | following errors are documented: |
… | |
… | |
191 | |
215 | |
192 | sub fileno { |
216 | sub fileno { |
193 | $_[0]{fd} |
217 | $_[0]{fd} |
194 | } |
218 | } |
195 | |
219 | |
|
|
220 | =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 | |
196 | =item $count = $inotify->poll |
232 | =item $count = $inotify->poll |
197 | |
233 | |
198 | Reads events from the kernel and handles them. If the notify fileno is |
234 | Reads events from the kernel and handles them. If the notify fileno is |
199 | blocking (the default), then this method waits for at least one event |
235 | blocking (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 |
… | |
… | |
203 | Returns the count of events that have been handled. |
239 | Returns the count of events that have been handled. |
204 | |
240 | |
205 | =cut |
241 | =cut |
206 | |
242 | |
207 | sub poll { |
243 | sub poll { |
|
|
244 | 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 { |
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 | |
228 | sub DESTROY { |
280 | sub DESTROY { |
229 | inotify_close $_[0]{fd} |
281 | inotify_close $_[0]{fd} |
230 | } |
282 | } |