Perl/inotify.pm

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