1Inotify2(3) User Contributed Perl Documentation Inotify2(3)
2
3
4
6 Linux::Inotify2 - scalable directory/file change notification
7
9 Callback Interface
10 use Linux::Inotify2;
11
12 # create a new object
13 my $inotify = new Linux::Inotify2
14 or die "unable to create new inotify object: $!";
15
16 # add watchers
17 $inotify->watch ("/etc/passwd", IN_ACCESS, sub {
18 my $e = shift;
19 my $name = $e->fullname;
20 print "$name was accessed\n" if $e->IN_ACCESS;
21 print "$name is no longer mounted\n" if $e->IN_UNMOUNT;
22 print "$name is gone\n" if $e->IN_IGNORED;
23 print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
24
25 # cancel this watcher: remove no further events
26 $e->w->cancel;
27 });
28
29 # integration into AnyEvent (works with EV, Glib, Tk, POE...)
30 my $inotify_w = AnyEvent->io (
31 fh => $inofity->fileno, poll => 'r', cb => sub { $inotify->poll }
32 );
33
34 # manual event loop
35 1 while $inotify->poll;
36
37 Streaming Interface
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
58 This module implements an interface to the Linux 2.6.13 and later
59 Inotify file/directory change notification system.
60
61 It has a number of advantages over the Linux::Inotify module:
62
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 The Linux::Inotify2 Class
70 my $inotify = new Linux::Inotify2
71 Create a new notify object and return it. A notify object is kind
72 of a container that stores watches on file system names and is
73 responsible for handling event data.
74
75 On error, "undef" is returned and $! will be set accordingly. The
76 following errors are documented:
77
78 ENFILE The system limit on the total number of file descriptors has been reached.
79 EMFILE The user limit on the total number of inotify instances has been reached.
80 ENOMEM Insufficient kernel memory is available.
81
82 Example:
83
84 my $inotify = new Linux::Inotify2
85 or die "Unable to create new inotify object: $!";
86
87 $watch = $inotify->watch ($name, $mask[, $cb])
88 Add a new watcher to the given notifier. The watcher will create
89 events on the pathname $name as given in $mask, which can be any of
90 the following constants (all exported by default) ORed together.
91
92 "file" refers to any file system object in the watched object
93 (always a directory), that is files, directories, symlinks, device
94 nodes etc., while "object" refers to the object the watcher has
95 been set on itself:
96
97 IN_ACCESS object was accessed
98 IN_MODIFY object was modified
99 IN_ATTRIB object metadata changed
100 IN_CLOSE_WRITE writable fd to file / to object was closed
101 IN_CLOSE_NOWRITE readonly fd to file / to object closed
102 IN_OPEN object was opened
103 IN_MOVED_FROM file was moved from this object (directory)
104 IN_MOVED_TO file was moved to this object (directory)
105 IN_CREATE file was created in this object (directory)
106 IN_DELETE file was deleted from this object (directory)
107 IN_DELETE_SELF object itself was deleted
108 IN_MOVE_SELF object itself was moved
109 IN_ALL_EVENTS all of the above events
110
111 IN_ONESHOT only send event once
112 IN_ONLYDIR only watch the path if it is a directory
113 IN_DONT_FOLLOW don't follow a sym link
114 IN_MASK_ADD not supported with the current version of this module
115
116 IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE
117 IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO
118
119 $cb is a perl code reference that, if given, is called for each
120 event. It receives a "Linux::Inotify2::Event" object.
121
122 The returned $watch object is of class "Linux::Inotify2::Watch".
123
124 On error, "undef" is returned and $! will be set accordingly. The
125 following errors are documented:
126
127 EBADF The given file descriptor is not valid.
128 EINVAL The given event mask contains no legal events.
129 ENOMEM Insufficient kernel memory was available.
130 ENOSPC The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource.
131 EACCESS Read access to the given file is not permitted.
132
133 Example, show when "/etc/passwd" gets accessed and/or modified
134 once:
135
136 $inotify->watch ("/etc/passwd", IN_ACCESS | IN_MODIFY, sub {
137 my $e = shift;
138 print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS;
139 print "$e->{w}{name} was modified\n" if $e->IN_MODIFY;
140 print "$e->{w}{name} is no longer mounted\n" if $e->IN_UNMOUNT;
141 print "events for $e->{w}{name} have been lost\n" if $e->IN_Q_OVERFLOW;
142
143 $e->w->cancel;
144 });
145
146 $inotify->fileno
147 Returns the file descriptor for this notify object. When in non-
148 blocking mode, you are responsible for calling the "poll" method
149 when this file descriptor becomes ready for reading.
150
151 $inotify->blocking ($blocking)
152 Clears ($blocking true) or sets ($blocking false) the "O_NONBLOCK"
153 flag on the file descriptor.
154
155 $count = $inotify->poll
156 Reads events from the kernel and handles them. If the notify file
157 descriptor is blocking (the default), then this method waits for at
158 least one event (and thus returns true unless an error occurs).
159 Otherwise it returns immediately when no pending events could be
160 read.
161
162 Returns the count of events that have been handled.
163
164 @events = $inotify->read
165 Reads events from the kernel. Blocks when the file descriptor is in
166 blocking mode (default) until any event arrives. Returns list of
167 "Linux::Inotify2::Event" objects or empty list if none (non-
168 blocking mode) or error occurred ($! should be checked).
169
170 Normally you shouldn't use this function, but instead use watcher
171 callbacks and call "->poll".
172
173 The Linux::Inotify2::Event Class
174 Objects of this class are handed as first argument to the watcher
175 callback. It has the following members and methods:
176
177 $event->w
178 $event->{w}
179 The watcher object for this event.
180
181 $event->name
182 $event->{name}
183 The path of the file system object, relative to the watched name.
184
185 $event->fullname
186 Returns the "full" name of the relevant object, i.e. including the
187 "name" member of the watcher (if the watch object is on a directory
188 and a directory entry is affected), or simply the "name" member
189 itself when the object is the watch object itself.
190
191 $event->mask
192 $event->{mask}
193 The received event mask. In addition to the events described for
194 "$inotify->watch", the following flags (exported by default) can be
195 set:
196
197 IN_ISDIR event object is a directory
198 IN_Q_OVERFLOW event queue overflowed
199
200 # when any of the following flags are set,
201 # then watchers for this event are automatically canceled
202 IN_UNMOUNT filesystem for watched object was unmounted
203 IN_IGNORED file was ignored/is gone (no more events are delivered)
204 IN_ONESHOT only one event was generated
205
206 $event->IN_xxx
207 Returns a boolean that returns true if the event mask contains any
208 events specified by the mask. All of the "IN_xxx" constants can be
209 used as methods.
210
211 $event->cookie
212 $event->{cookie}
213 The event cookie to "synchronize two events". Normally zero, this
214 value is set when two events relating to the same file are
215 generated. As far as I know, this only happens for "IN_MOVED_FROM"
216 and "IN_MOVED_TO" events, to identify the old and new name of a
217 file.
218
219 The Linux::Inotify2::Watch Class
220 Watcher objects are created by calling the "watch" method of a
221 notifier.
222
223 It has the following members and methods:
224
225 $watch->name
226 $watch->{name}
227 The name as specified in the "watch" call. For the object itself,
228 this is the empty string. For directory watches, this is the name
229 of the entry without leading path elements.
230
231 $watch->mask
232 $watch->{mask}
233 The mask as specified in the "watch" call.
234
235 $watch->cb ([new callback])
236 $watch->{cb}
237 The callback as specified in the "watch" call. Can optionally be
238 changed.
239
240 $watch->cancel
241 Cancels/removes this watcher. Future events, even if already queued
242 queued, will not be handled and resources will be freed.
243
245 AnyEvent, Linux::Inotify.
246
248 Marc Lehmann <schmorp@schmorp.de>
249 http://home.schmorp.de/
250
251
252
253perl v5.28.0 2011-06-14 Inotify2(3)