Perl/inotify.pm

filter: Statementstmt 97.7% 88 / 90 Branchbran 75.0% 12 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 19 / 19 Podpod 100% 4 / 4 totaltotal 92.5% 124 / 134 CC 13 SCAR 9.3
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 => {
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 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 3 use constant _read_only_event_num => (
54 UNMOUNT => 0x2000,
55 Q_OVERFLOW => 0x4000,
56 IGNORED => 0x8000,
57 ISDIR => 0x40000000,
58 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 use constant _shorthand_event_num => (
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 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 use constant _event_input_opts => {
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 ▶};
Subroutine BEGIN: called CC=2 SCAR=6.9
75  
76 3 use constant _event_opts => {
77 ONLYDIR => 0x01000000,
78 DONT_FOLLOW => 0x02000000,
79 EXCL_UNLINK => 0x04000000,
80 MASK_CREATE => 0x10000000,
81 MASK_ADD => 0x20000000,
82 ONESHOT => 0x80000000,
83 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
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 use constant EVENT_NUMBER => {
112 3 %{ _simple_event_num() },
113 _read_only_event_num(),
114 _shorthand_event_num(),
115 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
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 sub new {
133 2 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=3 SCAR=11.1
Pod documented
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 =cut
192  
193 sub add {
194 2 ▶ my ($self, %opts) = @_;
Subroutine add: called CC=3 SCAR=12.3
Pod documented
195  
196 2 my $path = $opts{'path'};
197 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
198 0 die 'Need path!';
199 }
200  
201 my $events_mask = Linux::Perl::EventFlags::events_flags_to_num(
202 2 $opts{'events'},
203 _event_input_opts(),
204 _event_opts(),
205 );
206  
207 2 return Linux::Perl::call(
208 $self->NR_inotify_add_watch(),
209 0 + $self->[0],
210 $path,
211 0 + $events_mask,
212 );
213 }
214  
215 #----------------------------------------------------------------------
216  
217 3 my ($inotify_keys_ar, $inotify_pack, $inotify_sizeof);
218 BEGIN {
219 3 ▶ ($inotify_keys_ar, $inotify_pack) = Linux::Perl::EasyPack::split_pack_list(
Subroutine BEGIN: called CC=1 SCAR=0.0
220 wd => 'i!', #int
221 mask => 'L', #uint32_t
222 cookie => 'L', #uint32_t
223 name => 'L/a', #uint32_t & char[]
224 );
225  
226 3 $inotify_sizeof = length pack $inotify_pack;
227 }
228  
229 =head2 @events = I<OBJ>->read()
230  
231 Reads events from the inotify instance. Each event is returned as
232 a hash reference with members C<wd>, C<mask>, C<cookie>, and C<name>.
233 See C<man 7 inotify> for details about what these mean. (Use the
234 members of C<EVENT_NUMBER()> above to parse C<mask>.)
235  
236 Note that if the underlying inotify object is not set C<NONBLOCK>
237 then this call will block until there is an inotify event to read.
238  
239 In scalar context this returns the number of events that happened.
240  
241 An empty return here indicates a read failure; C<$!> will contain the
242 usual information about the failure.
243  
244 =cut
245  
246 sub read {
247 4 ▶ my ($self) = @_;
Subroutine read: called CC=5 SCAR=16.1
Pod documented
248  
249 4 my @events;
250  
251 4 my $res = sysread $self->[1], my $buf, 65536;
252  
253 4 ▶ if (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) 2 2 4
254 2 ▶ while (my @els = unpack $inotify_pack, $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@els) = unpack($inotify_pack, $buf)) 16 0 16
255 16 my %evt;
256 16 @evt{ @$inotify_keys_ar } = @els;
257  
258 16 substr( $buf, 0, $inotify_sizeof + length $els[-1] ) = q<>;
259  
260 16 ▶ $evt{'name'} =~ tr<\0><>d if $evt{'name'};
Branch: 1 branch
BranchTrueFalseTotal
if $evt{"name"} 12 4 16
261  
262 16 push @events, \%evt;
263  
264 # Perl 5.16 and previous choke with:
265 #
266 # '/' must follow a numeric type in unpack
267 #
268 # » unless we avoid unpack() on an empty string.
269 16 ▶ last if !$buf;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf 2 14 16
270 }
271 }
272  
273 4 return @events;
274 }
275  
276 #----------------------------------------------------------------------
277  
278 =head2 I<OBJ>->remove( $WD )
279  
280 Analogous to C<man 2 inotify_rm_watch>.
281  
282 =cut
283  
284 sub remove {
285 2 ▶ my ($self, $wd) = @_;
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
286  
287 2 Linux::Perl::call(
288 $self->NR_inotify_rm_watch(),
289 0 + $self->[0],
290 0 + $wd,
291 );
292  
293 2 return $self;
294 }
295  
296 #----------------------------------------------------------------------
297  
298 package Linux::Perl::EventFlags;
299  
300 sub events_flags_to_num {
301 2 ▶ my ($input_ar, @names_to_nums) = @_;
Subroutine events_flags_to_num: called CC=4 SCAR=13.9
302  
303 2 my $mask = 0;
304  
305 EVENT:
306 2 for my $evt (@$input_ar) {
307 6 for my $name_to_num_hr (@names_to_nums) {
308 10 ▶ my $num = $name_to_num_hr->{$evt} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $num = $name_to_num_hr->{$evt} or next 4 6 10
309 6 $mask |= $num;
310 6 next EVENT;
311 }
312  
313 0 die "Unknown event or flag: $evt";
314 }
315  
316 2 return $mask;
317 }
318  
319 3 1;
« Perl/eventfd.pm ↑ Summary Perl/Constants.pm »