Perl/inotify.pm

filter: Statementstmt 97.8% 89 / 91 Branchbran 75.0% 12 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 92.7% 127 / 137 CC 13 SCAR 9.1
CC
13
Cov
93%
read
5.0
events_flags_to_num
4.0
add
3.4
CRAP 2.5
« Perl/eventfd.pm ↑ Summary Perl/Constants.pm »
1 package Linux::Perl::inotify;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Linux::Perl::inotify
11  
12 =head1 SYNOPSIS
13  
14 my $inf = Linux::Perl::inotify->new();
15  
16 my $wd = $inf->add( path => $path, events => ['CREATE', 'ONLYDIR'] );
17  
18 my @events = $inf->read();
19  
20 $inf->remove($wd);
21  
22 =head1 DESCRIPTION
23  
24 This is an interface to Linux’s “inotify” feature.
25  
26 =cut
27  
28 3 ▶use Linux::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use Linux::Perl::Constants::Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use Linux::Perl::EasyPack;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use Linux::Perl::ParseFlags;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 ▶use parent 'Linux::Perl::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 *_flag_CLOEXEC = \*Linux::Perl::Constants::Fcntl::flag_CLOEXEC;
36 3 *_flag_NONBLOCK = \*Linux::Perl::Constants::Fcntl::flag_NONBLOCK;
37  
38 3 ▶use constant _simple_event_num => {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 ACCESS => 1,
40 MODIFY => 2,
41 ATTRIB => 4,
42 CLOSE_WRITE => 8,
43 CLOSE_NOWRITE => 16,
44 OPEN => 32,
45 MOVED_FROM => 64,
46 MOVED_TO => 128,
47 CREATE => 256,
48 DELETE => 512,
49 DELETE_SELF => 1024,
50 MOVE_SELF => 2048,
51 3 };
52  
53 3 ▶use constant _read_only_event_num => (
Subroutine BEGIN: called CC=1 SCAR=0.0
54 UNMOUNT => 0x2000,
55 Q_OVERFLOW => 0x4000,
56 IGNORED => 0x8000,
57 ISDIR => 0x40000000,
58 3 );
59  
60 3 ▶use constant _shorthand_event_num => (
Subroutine BEGIN: called CC=1 SCAR=0.0
61 CLOSE => _simple_event_num()->{'CLOSE_WRITE'} | _simple_event_num()->{'CLOSE_NOWRITE'},
62 3 MOVE => _simple_event_num()->{'MOVED_FROM'} | _simple_event_num()->{'MOVED_TO'},
63 3 );
64  
65 3 ▶use constant _event_input_opts => {
Subroutine BEGIN: called CC=2 SCAR=6.9
66 3 %{ _simple_event_num() },
67 _shorthand_event_num(),
68  
69 3 ALL_EVENTS => do {
70 3 my $num = 0;
71 3 $num |= $_ for values %{ _simple_event_num() };
72 3 $num;
73 },
74 3 };
75  
76 3 ▶use constant _event_opts => {
Subroutine BEGIN: called CC=1 SCAR=0.0
77 ONLYDIR => 0x01000000,
78 DONT_FOLLOW => 0x02000000,
79 EXCL_UNLINK => 0x04000000,
80 MASK_CREATE => 0x10000000,
81 MASK_ADD => 0x20000000,
82 ONESHOT => 0x80000000,
83 3 };
84  
85 =head1 METHODS
86  
87 =head2 I<CLASS>->EVENT_NUMBER()
88  
89 A hash reference of event names to numeric values. The member keys
90 are:
91  
92 =over
93  
94 =item * C<ACCESS>, C<MODIFY>, C<ATTRIB>
95  
96 =item * C<OPEN>, C<CLOSE>, C<CLOSE_WRITE>, C<CLOSE_NOWRITE>
97  
98 =item * C<MOVE>, C<MOVED_FROM>, C<MOVED_TO>, C<MOVE_SELF>
99  
100 =item * C<CREATE>, C<DELETE>, C<DELETE_SELF>
101  
102 =item * C<UNMOUNT>, C<Q_OVERFLOW>, C<IGNORED>, C<ISDIR>
103  
104 =back
105  
106 See C<man 7 inotify> for details of what these mean. This is
107 useful to parse the return from C<read()> (below).
108  
109 =cut
110  
111 3 ▶use constant EVENT_NUMBER => {
Subroutine BEGIN: called CC=1 SCAR=0.0
112 3 %{ _simple_event_num() },
113 _read_only_event_num(),
114 _shorthand_event_num(),
115 3 };
116  
117 =head2 I<CLASS>->new( %OPTS )
118  
119 Instantiates a new inotify instance.
120  
121 %OPTS is:
122  
123 =over
124  
125 =item * C<flags> - Optional, an array reference of either or both of
126 C<NONBLOCK> and/or C<CLOEXEC>.
127  
128 =back
129  
130 =cut
131  
132 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
133 2 my ($class, %opts) = @_;
134  
135 2 $class = $class->_get_arch_module();
136  
137 2 my $flags = Linux::Perl::ParseFlags::parse( $class, $opts{'flags'} );
138  
139 2 my $fn = 'NR_inotify_init';
140 2 ▶ $fn .= '1' if $flags;
Branch: 1 branch
BranchTrueFalseTotal
if $flags 2 0 2
141  
142 2 my $fd = Linux::Perl::call(
143 $class->$fn(),
144 $flags,
145 );
146  
147 2 ▶ local $^F = 1000 if $flags & _flag_CLOEXEC();
Branch: 1 branch
BranchTrueFalseTotal
if $flags & &_flag_CLOEXEC() 0 2 2
148  
149 2 open my $fh, '+<&=', $fd;
150  
151 2 return bless [$fd, $fh], $class;
152 }
153  
154 #----------------------------------------------------------------------
155  
156 =head2 $wd = I<OBJ>->add( %OPTS )
157  
158 Adds to an inotify instance and returns a watch descriptor.
159 See C<man 2 inotify_add_watch> for more information.
160  
161 %OPTS is:
162  
163 =over
164  
165 =item * C<path> - The filesystem path to monitor.
166  
167 =item * C<events> - An array reference of events to monitor for.
168 Recognized events are:
169  
170 =over
171  
172 =item * C<ACCESS>, C<MODIFY>, C<ATTRIB>
173  
174 =item * C<OPEN>, C<CLOSE>, C<CLOSE_WRITE>, C<CLOSE_NOWRITE>
175  
176 =item * C<MOVE>, C<MOVED_FROM>, C<MOVED_TO>, C<MOVE_SELF>
177  
178 =item * C<CREATE>, C<DELETE>, C<DELETE_SELF>
179  
180 =item * C<UNMOUNT>, C<Q_OVERFLOW>, C<IGNORED>, C<ISDIR>
181  
182 =item * C<ALL_EVENTS>
183  
184 =item * C<ONLYDIR>, C<DONT_FOLLOW>, C<EXCL_UNLINK>, C<MASK_CREATE>,
185 C<MASK_ADD>, C<ONESHOT>
186  
187 =back
188  
189 Note that your kernel may not recognize all of these.
190  
191 =back
192  
193 =cut
194  
195 2 ▶sub add {
Subroutine add: called CC=3 SCAR=12.3
Pod documented
196 2 my ($self, %opts) = @_;
197  
198 2 my $path = $opts{'path'};
199 2 ▶ if (!defined $path || !length $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path and length $path) 0 2 2
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
002
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
200 0 die 'Need path!';
201 }
202  
203 my $events_mask = Linux::Perl::EventFlags::events_flags_to_num(
204 2 $opts{'events'},
205 _event_input_opts(),
206 _event_opts(),
207 );
208  
209 2 return Linux::Perl::call(
210 $self->NR_inotify_add_watch(),
211 0 + $self->[0],
212 $path,
213 0 + $events_mask,
214 );
215 }
216  
217 #----------------------------------------------------------------------
218  
219 =head2 I<OBJ>->fileno()
220  
221 Returns the inotify instance’s file descriptor number.
222  
223 =cut
224  
225 2 ▶sub fileno { return $_[0][0] }
Subroutine fileno: called CC=1 SCAR=0.0
Pod documented
226  
227 #----------------------------------------------------------------------
228  
229 3 my ($inotify_keys_ar, $inotify_pack, $inotify_sizeof);
230 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
231 3 ($inotify_keys_ar, $inotify_pack) = Linux::Perl::EasyPack::split_pack_list(
232 wd => 'i!', #int
233 mask => 'L', #uint32_t
234 cookie => 'L', #uint32_t
235 name => 'L/a', #uint32_t & char[]
236 );
237  
238 3 $inotify_sizeof = length pack $inotify_pack;
239 }
240  
241 =head2 @events = I<OBJ>->read()
242  
243 Reads events from the inotify instance. Each event is returned as
244 a hash reference with members C<wd>, C<mask>, C<cookie>, and C<name>.
245 See C<man 7 inotify> for details about what these mean. (Use the
246 members of C<EVENT_NUMBER()> above to parse C<mask>.)
247  
248 Note that if the underlying inotify object is not set C<NONBLOCK>
249 then this call will block until there is an inotify event to read.
250  
251 In scalar context this returns the number of events that happened.
252  
253 An empty return here indicates a read failure; C<$!> will contain the
254 usual information about the failure.
255  
256 =cut
257  
258 4 ▶sub read {
Subroutine read: called CC=5 SCAR=16.1
Pod documented
259 4 my ($self) = @_;
260  
261 4 my @events;
262  
263 4 my $res = sysread $self->[1], my $buf, 65536;
264  
265 4 ▶ if (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) 2 2 4
266 2 ▶ while (my @els = unpack $inotify_pack, $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@els) = unpack($inotify_pack, $buf)) 16 0 16
267 16 my %evt;
268 16 @evt{ @$inotify_keys_ar } = @els;
269  
270 16 substr( $buf, 0, $inotify_sizeof + length $els[-1] ) = q<>;
271  
272 16 ▶ $evt{'name'} =~ tr<\0><>d if $evt{'name'};
Branch: 1 branch
BranchTrueFalseTotal
if $evt{"name"} 12 4 16
273  
274 16 push @events, \%evt;
275  
276 # Perl 5.16 and previous choke with:
277 #
278 # '/' must follow a numeric type in unpack
279 #
280 # » unless we avoid unpack() on an empty string.
281 16 ▶ last if !$buf;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf 2 14 16
282 }
283 }
284  
285 4 return @events;
286 }
287  
288 #----------------------------------------------------------------------
289  
290 =head2 I<OBJ>->remove( $WD )
291  
292 Analogous to C<man 2 inotify_rm_watch>.
293  
294 =cut
295  
296 2 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
297 2 my ($self, $wd) = @_;
298  
299 2 Linux::Perl::call(
300 $self->NR_inotify_rm_watch(),
301 0 + $self->[0],
302 0 + $wd,
303 );
304  
305 2 return $self;
306 }
307  
308 #----------------------------------------------------------------------
309  
310 package Linux::Perl::EventFlags;
311  
312 2 ▶sub events_flags_to_num {
Subroutine events_flags_to_num: called CC=4 SCAR=13.9
313 2 my ($input_ar, @names_to_nums) = @_;
314  
315 2 my $mask = 0;
316  
317 EVENT:
318 2 for my $evt (@$input_ar) {
319 6 for my $name_to_num_hr (@names_to_nums) {
320 10 ▶ my $num = $name_to_num_hr->{$evt} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $num = $name_to_num_hr->{$evt} or next 6 4 10
321 6 $mask |= $num;
322 6 next EVENT;
323 }
324  
325 0 die "Unknown event or flag: $evt";
326 }
327  
328 2 return $mask;
329 }
330  
331 3 1;
« Perl/eventfd.pm ↑ Summary Perl/Constants.pm »