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