| 1 |
root |
1.1 |
NAME |
| 2 |
root |
1.2 |
Linux::Inotify2 - scalable directory/file change notification |
| 3 |
root |
1.1 |
|
| 4 |
|
|
SYNOPSIS |
| 5 |
root |
1.2 |
use Linux::Inotify2; |
| 6 |
root |
1.1 |
|
| 7 |
root |
1.3 |
# 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 |
root |
1.1 |
DESCRIPTION |
| 32 |
root |
1.2 |
The Linux::Inotify2 Class |
| 33 |
root |
1.3 |
This module implements an interface to the Linux 2.6.13 and later |
| 34 |
|
|
Inotify file/directory change notification sytem. |
| 35 |
root |
1.2 |
|
| 36 |
root |
1.3 |
It has a number of advantages over the Linux::Inotify module: |
| 37 |
root |
1.2 |
|
| 38 |
|
|
- it is portable (Linux::Inotify only works on x86) |
| 39 |
|
|
- the equivalent of fullname works correctly |
| 40 |
|
|
- it is better documented |
| 41 |
|
|
- it has callback-style interface, which is better suited for |
| 42 |
|
|
integration. |
| 43 |
|
|
|
| 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 |
root |
1.3 |
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 |
root |
1.2 |
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 |
root |
1.3 |
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_ALL_EVENTS all of the above events |
| 83 |
root |
1.2 |
|
| 84 |
|
|
IN_ONESHOT only send event once |
| 85 |
|
|
|
| 86 |
root |
1.3 |
IN_CLOSE same as IN_CLOSE_WRITE | IN_CLOSE_NOWRITE |
| 87 |
|
|
IN_MOVE same as IN_MOVED_FROM | IN_MOVED_TO |
| 88 |
root |
1.2 |
|
| 89 |
|
|
$cb is a perl code reference that is called for each event. It |
| 90 |
|
|
receives a "Linux::Inotify2::Event" object. |
| 91 |
|
|
|
| 92 |
|
|
The returned $watch object is of class "Linux::Inotify2::Watch". |
| 93 |
|
|
|
| 94 |
|
|
On error, "undef" is returned and $! will be set accordingly. The |
| 95 |
|
|
following errors are documented: |
| 96 |
|
|
|
| 97 |
|
|
EBADF The given file descriptor is not valid. |
| 98 |
|
|
EINVAL The given event mask contains no legal events. |
| 99 |
|
|
ENOMEM Insufficient kernel memory was available. |
| 100 |
|
|
ENOSPC The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource. |
| 101 |
|
|
EACCESS Read access to the given file is not permitted. |
| 102 |
|
|
|
| 103 |
|
|
Example, show when "/etc/passwd" gets accessed and/or modified once: |
| 104 |
|
|
|
| 105 |
|
|
$inotify->watch ("/etc/passwd", IN_ACCESS | IN_MODIFY, sub { |
| 106 |
|
|
my $e = shift; |
| 107 |
|
|
print "$e->{w}{name} was accessed\n" if $e->IN_ACCESS; |
| 108 |
|
|
print "$e->{w}{name} was modified\n" if $e->IN_MODIFY; |
| 109 |
|
|
print "$e->{w}{name} is no longer mounted\n" if $e->IN_UNMOUNT; |
| 110 |
|
|
print "events for $e->{w}{name} have been lost\n" if $e->IN_Q_OVERFLOW; |
| 111 |
|
|
|
| 112 |
|
|
$e->w->cancel; |
| 113 |
|
|
}); |
| 114 |
|
|
|
| 115 |
root |
1.3 |
$inotify->fileno |
| 116 |
root |
1.2 |
Returns the fileno for this notify object. You are responsible for |
| 117 |
|
|
calling the "poll" method when this fileno becomes ready for |
| 118 |
|
|
reading. |
| 119 |
|
|
|
| 120 |
root |
1.3 |
$count = $inotify->poll |
| 121 |
root |
1.2 |
Reads events from the kernel and handles them. If the notify fileno |
| 122 |
|
|
is blocking (the default), then this method waits for at least one |
| 123 |
root |
1.3 |
event (and thus returns true unless an error occurs). Otherwise it |
| 124 |
|
|
returns immediately when no pending events could be read. |
| 125 |
root |
1.2 |
|
| 126 |
|
|
Returns the count of events that have been handled. |
| 127 |
|
|
|
| 128 |
|
|
The Linux::Inotify2::Event Class |
| 129 |
|
|
Objects of this class are handed as first argument to the watch |
| 130 |
|
|
callback. It has the following members and methods: |
| 131 |
|
|
|
| 132 |
|
|
$event->w |
| 133 |
|
|
$event->{w} |
| 134 |
|
|
The watcher object for this event. |
| 135 |
|
|
|
| 136 |
|
|
$event->name |
| 137 |
|
|
$event->{name} |
| 138 |
|
|
The path of the filesystem object, relative to the watch name. |
| 139 |
|
|
|
| 140 |
|
|
$watch->fullname |
| 141 |
|
|
Returns the "full" name of the relevant object, i.e. including the |
| 142 |
|
|
"name" component of the watcher. |
| 143 |
|
|
|
| 144 |
|
|
$event->mask |
| 145 |
|
|
$event->{mask} |
| 146 |
|
|
The received event mask. In addition the the events described for |
| 147 |
|
|
"$inotify-"watch>, the following flags (exported by default) can be |
| 148 |
|
|
set: |
| 149 |
|
|
|
| 150 |
root |
1.3 |
IN_ISDIR event object is a directory |
| 151 |
|
|
|
| 152 |
|
|
IN_Q_OVERFLOW event queue overflowed |
| 153 |
root |
1.2 |
|
| 154 |
root |
1.3 |
# when the following flags are set, then watchers are canceled automatically |
| 155 |
|
|
IN_UNMOUNT filesystem for watch'ed object was unmounted |
| 156 |
|
|
IN_IGNORED file was ignored/is gone (no more events are delivered) |
| 157 |
root |
1.2 |
|
| 158 |
|
|
$event->IN_xxx |
| 159 |
|
|
Returns a boolean that returns true if the event mask matches the |
| 160 |
|
|
event. All of the "IN_xxx" constants can be used as methods. |
| 161 |
|
|
|
| 162 |
|
|
$event->cookie |
| 163 |
|
|
$event->{cookie} |
| 164 |
|
|
The event cookie, can be used to synchronize two related events. |
| 165 |
|
|
|
| 166 |
|
|
The Linux::Inotify2::Watch Class |
| 167 |
|
|
Watch objects are created by calling the "watch" method of a notifier. |
| 168 |
|
|
|
| 169 |
|
|
It has the following members and methods: |
| 170 |
|
|
|
| 171 |
|
|
$watch->name |
| 172 |
|
|
$watch->{name} |
| 173 |
|
|
The name as specified in the "watch" call. For the object itself, this |
| 174 |
|
|
is the empty string. For directory watches, this is the name of the |
| 175 |
|
|
entry without leading path elements. |
| 176 |
|
|
|
| 177 |
|
|
$watch->mask |
| 178 |
|
|
$watch->{mask} |
| 179 |
|
|
The mask as specified in the "watch" call. |
| 180 |
|
|
|
| 181 |
|
|
$watch->cb ([new callback]) |
| 182 |
|
|
$watch->{cb} |
| 183 |
|
|
The callback as specified in the "watch" call. Can optionally be |
| 184 |
|
|
changed. |
| 185 |
|
|
|
| 186 |
|
|
$watch->cancel |
| 187 |
|
|
Cancels/removes this watch. Future events, even if already queued |
| 188 |
|
|
queued, will not be handled and resources will be freed. |
| 189 |
root |
1.1 |
|
| 190 |
|
|
SEE ALSO |
| 191 |
root |
1.2 |
Linux::Inotify. |
| 192 |
root |
1.1 |
|
| 193 |
|
|
AUTHOR |
| 194 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 195 |
|
|
http://home.schmorp.de/ |
| 196 |
|
|
|