ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-Inotify2/README
Revision: 1.7
Committed: Fri Sep 29 14:41:33 2006 UTC (19 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-1_1
Changes since 1.6: +11 -8 lines
Log Message:
*** empty log message ***

File Contents

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