line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package File::ChangeNotify::Watcher; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
27389
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
87
|
|
4
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
218
|
|
5
|
3
|
|
|
3
|
|
21
|
use namespace::autoclean; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
19
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.30'; |
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
301
|
use Fcntl qw( S_IMODE ); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
181
|
|
10
|
3
|
|
|
3
|
|
836
|
use File::ChangeNotify::Event; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
127
|
|
11
|
3
|
|
|
3
|
|
24
|
use File::Find qw( find ); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
225
|
|
12
|
3
|
|
|
3
|
|
21
|
use File::Spec; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
66
|
|
13
|
3
|
|
|
3
|
|
25
|
use Module::Runtime qw( use_module ); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
28
|
|
14
|
|
|
|
|
|
|
use Types::Standard |
15
|
3
|
|
|
3
|
|
150
|
qw( ArrayRef Bool ClassName CodeRef HashRef Num RegexpRef Str ); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
21
|
|
16
|
3
|
|
|
3
|
|
4803
|
use Type::Utils -all; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
18
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Trying to import this just blows up on Win32, and checking |
19
|
|
|
|
|
|
|
# Time::HiRes::d_hires_stat() _also_ blows up on Win32. |
20
|
|
|
|
|
|
|
BEGIN { |
21
|
|
|
|
|
|
|
## no critic (ErrorHandling::RequireCheckingReturnValueOfEval) |
22
|
3
|
|
|
3
|
|
9596
|
eval { |
23
|
3
|
|
|
|
|
19
|
require Time::HiRes; |
24
|
3
|
|
|
|
|
25
|
Time::HiRes->import('stat'); |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
3
|
|
|
3
|
|
367
|
use Moo::Role; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
22
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has filter => ( |
31
|
|
|
|
|
|
|
is => 'ro', |
32
|
|
|
|
|
|
|
isa => RegexpRef, |
33
|
|
|
|
|
|
|
default => sub {qr/.*/}, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#<<< |
37
|
|
|
|
|
|
|
my $dir_t = subtype as Str, |
38
|
|
|
|
|
|
|
where { -d $_ }, |
39
|
|
|
|
|
|
|
message { "$_ is not a valid directory" }; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $array_of_dirs_t = subtype as ArrayRef[Str], |
42
|
|
|
|
|
|
|
where { |
43
|
|
|
|
|
|
|
map {-d} @{$_}; |
44
|
|
|
|
|
|
|
}, |
45
|
|
|
|
|
|
|
message {"@{$_} is not a list of valid directories"}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
coerce $array_of_dirs_t, |
48
|
|
|
|
|
|
|
from $dir_t, |
49
|
|
|
|
|
|
|
via { [$_] }; |
50
|
|
|
|
|
|
|
#>>> |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
has directories => ( |
53
|
|
|
|
|
|
|
is => 'ro', |
54
|
|
|
|
|
|
|
writer => '_set_directories', |
55
|
|
|
|
|
|
|
isa => $array_of_dirs_t, |
56
|
|
|
|
|
|
|
required => 1, |
57
|
|
|
|
|
|
|
coerce => 1, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
has follow_symlinks => ( |
61
|
|
|
|
|
|
|
is => 'ro', |
62
|
|
|
|
|
|
|
isa => Bool, |
63
|
|
|
|
|
|
|
default => 0, |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
has event_class => ( |
67
|
|
|
|
|
|
|
is => 'ro', |
68
|
|
|
|
|
|
|
isa => ClassName, |
69
|
|
|
|
|
|
|
default => 'File::ChangeNotify::Event', |
70
|
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
has sleep_interval => ( |
73
|
|
|
|
|
|
|
is => 'ro', |
74
|
|
|
|
|
|
|
isa => Num, |
75
|
|
|
|
|
|
|
default => 2, |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
my $files_or_regexps_or_code_t |
79
|
|
|
|
|
|
|
= subtype as ArrayRef [ Str | RegexpRef | CodeRef ]; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
has exclude => ( |
82
|
|
|
|
|
|
|
is => 'ro', |
83
|
|
|
|
|
|
|
isa => $files_or_regexps_or_code_t, |
84
|
|
|
|
|
|
|
default => sub { [] }, |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
has modify_includes_file_attributes => ( |
88
|
|
|
|
|
|
|
is => 'ro', |
89
|
|
|
|
|
|
|
isa => Bool | $files_or_regexps_or_code_t, |
90
|
|
|
|
|
|
|
default => 0, |
91
|
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
has modify_includes_content => ( |
94
|
|
|
|
|
|
|
is => 'ro', |
95
|
|
|
|
|
|
|
isa => Bool | $files_or_regexps_or_code_t, |
96
|
|
|
|
|
|
|
default => 0, |
97
|
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
has _map => ( |
100
|
|
|
|
|
|
|
is => 'ro', |
101
|
|
|
|
|
|
|
writer => '_set_map', |
102
|
|
|
|
|
|
|
isa => HashRef, |
103
|
|
|
|
|
|
|
predicate => '_has_map', |
104
|
|
|
|
|
|
|
); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub BUILD { |
107
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
0
|
use_module( $self->event_class ); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
## no critic ( Subroutines::ProhibitUnusedPrivateSubroutines) |
113
|
|
|
|
|
|
|
sub _current_map { |
114
|
54
|
|
|
54
|
|
93
|
my $self = shift; |
115
|
|
|
|
|
|
|
|
116
|
54
|
|
|
|
|
90
|
my %map; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
find( |
119
|
|
|
|
|
|
|
{ |
120
|
|
|
|
|
|
|
wanted => sub { |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# File::Find seems to use '/' as the path separator on Windows |
123
|
|
|
|
|
|
|
# for some odd reason. It really should be using File::Spec |
124
|
|
|
|
|
|
|
# internally everywhere but it doesn't. |
125
|
144
|
50
|
|
144
|
|
719
|
my $path |
126
|
|
|
|
|
|
|
= $^O eq 'MSWin32' |
127
|
|
|
|
|
|
|
? File::Spec->canonpath($File::Find::name) |
128
|
|
|
|
|
|
|
: $File::Find::name; |
129
|
|
|
|
|
|
|
|
130
|
144
|
100
|
|
|
|
388
|
if ( $self->_path_is_excluded($path) ) { |
131
|
4
|
|
|
|
|
11
|
$File::Find::prune = 1; |
132
|
4
|
|
|
|
|
40
|
return; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
140
|
100
|
|
|
|
301
|
my $entry = $self->_entry_for_map($path) or return; |
136
|
138
|
|
|
|
|
6599
|
$map{$path} = $entry; |
137
|
|
|
|
|
|
|
}, |
138
|
|
|
|
|
|
|
follow_fast => ( $self->follow_symlinks ? 1 : 0 ), |
139
|
|
|
|
|
|
|
no_chdir => 1, |
140
|
|
|
|
|
|
|
follow_skip => 2, |
141
|
|
|
|
|
|
|
}, |
142
|
54
|
100
|
|
|
|
483
|
@{ $self->directories }, |
|
54
|
|
|
|
|
3785
|
|
143
|
|
|
|
|
|
|
); |
144
|
|
|
|
|
|
|
|
145
|
54
|
|
|
|
|
1038
|
return \%map; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
## use critic |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub _path_is_excluded { |
150
|
150
|
|
|
150
|
|
251
|
my $self = shift; |
151
|
150
|
|
|
|
|
234
|
my $path = shift; |
152
|
|
|
|
|
|
|
|
153
|
150
|
|
|
|
|
430
|
return $self->_path_matches( $self->exclude, $path ); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _entry_for_map { |
157
|
140
|
|
|
140
|
|
213
|
my $self = shift; |
158
|
140
|
|
|
|
|
211
|
my $path = shift; |
159
|
|
|
|
|
|
|
|
160
|
140
|
100
|
|
|
|
1800
|
my $is_dir = -d $path ? 1 : 0; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# This should be free since the stat call was already done when checking |
163
|
|
|
|
|
|
|
# -d. |
164
|
140
|
|
|
|
|
2093
|
my @stat = stat; |
165
|
|
|
|
|
|
|
|
166
|
140
|
100
|
100
|
|
|
1653
|
return if -l $path && !$is_dir; |
167
|
|
|
|
|
|
|
|
168
|
138
|
100
|
|
|
|
392
|
unless ($is_dir) { |
169
|
56
|
|
|
|
|
177
|
my $filter = $self->filter; |
170
|
56
|
50
|
|
|
|
1010
|
return unless ( File::Spec->splitpath($path) )[2] =~ /$filter/; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
return { |
174
|
138
|
100
|
|
|
|
567
|
is_dir => $is_dir, |
|
|
100
|
|
|
|
|
|
175
|
|
|
|
|
|
|
size => ( $is_dir ? 0 : $stat[7] ), |
176
|
|
|
|
|
|
|
$self->_maybe_file_attributes( $path, \@stat ), |
177
|
|
|
|
|
|
|
( $is_dir ? () : $self->_maybe_content($path) ), |
178
|
|
|
|
|
|
|
}; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub _maybe_file_attributes { |
182
|
138
|
|
|
138
|
|
237
|
my $self = shift; |
183
|
138
|
|
|
|
|
217
|
my $path = shift; |
184
|
138
|
|
|
|
|
177
|
my $stat = shift; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# The Default watcher always requires the mtime, regardless of whether or |
187
|
|
|
|
|
|
|
# not we're including stat info in the modify events. |
188
|
138
|
50
|
|
|
|
414
|
unless ( $self->_always_requires_mtime ) { |
189
|
|
|
|
|
|
|
return |
190
|
0
|
0
|
|
|
|
0
|
unless $self->_path_matches( |
191
|
|
|
|
|
|
|
$self->modify_includes_file_attributes, |
192
|
|
|
|
|
|
|
$path, |
193
|
|
|
|
|
|
|
); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
138
|
|
|
|
|
312
|
return ( stat => $self->_stat( $path, $stat ) ); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub _stat { |
200
|
138
|
|
|
138
|
|
195
|
my $self = shift; |
201
|
138
|
|
|
|
|
190
|
my $path = shift; |
202
|
138
|
|
|
|
|
193
|
my $stat = shift; |
203
|
|
|
|
|
|
|
|
204
|
138
|
50
|
|
|
|
261
|
my @stat = $stat ? @{$stat} : stat $path; |
|
138
|
|
|
|
|
355
|
|
205
|
|
|
|
|
|
|
return { |
206
|
138
|
|
|
|
|
1372
|
attributes => { |
207
|
|
|
|
|
|
|
permissions => S_IMODE( $stat[2] ), |
208
|
|
|
|
|
|
|
uid => $stat[4], |
209
|
|
|
|
|
|
|
gid => $stat[5], |
210
|
|
|
|
|
|
|
}, |
211
|
|
|
|
|
|
|
mtime => $stat[9], |
212
|
|
|
|
|
|
|
}; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
0
|
|
|
0
|
|
0
|
sub _always_requires_mtime {0} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub _maybe_content { |
218
|
56
|
|
|
56
|
|
108
|
my $self = shift; |
219
|
56
|
|
|
|
|
75
|
my $path = shift; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
return |
222
|
56
|
100
|
|
|
|
144
|
unless $self->_path_matches( $self->modify_includes_content, $path ); |
223
|
|
|
|
|
|
|
|
224
|
8
|
50
|
|
|
|
284
|
open my $fh, '<', $path or die "Cannot open $path for reading: $!"; |
225
|
8
|
50
|
|
|
|
71
|
binmode $fh, ':bytes' or die qq{Cannot binmode $path as ':bytes': $!}; |
226
|
8
|
|
|
|
|
14
|
my $content = do { |
227
|
8
|
|
|
|
|
44
|
local $/ = undef; |
228
|
8
|
|
|
|
|
207
|
<$fh>; |
229
|
|
|
|
|
|
|
}; |
230
|
8
|
50
|
|
|
|
82
|
close $fh or die "Cannot close $path: $!"; |
231
|
|
|
|
|
|
|
|
232
|
8
|
|
|
|
|
76
|
return ( content => $content ); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
sub new_events { |
236
|
17
|
|
|
17
|
1
|
3278
|
my $self = shift; |
237
|
|
|
|
|
|
|
|
238
|
17
|
|
|
|
|
62
|
return $self->_interesting_events; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
## no critic ( Subroutines::ProhibitUnusedPrivateSubroutines) |
242
|
|
|
|
|
|
|
sub _modify_event_maybe_file_attribute_changes { |
243
|
47
|
|
|
47
|
|
70
|
my $self = shift; |
244
|
47
|
|
|
|
|
78
|
my $path = shift; |
245
|
47
|
|
|
|
|
66
|
my $old_map = shift; |
246
|
47
|
|
|
|
|
64
|
my $new_map = shift; |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
return |
249
|
47
|
100
|
|
|
|
125
|
unless $self->_path_matches( |
250
|
|
|
|
|
|
|
$self->modify_includes_file_attributes, |
251
|
|
|
|
|
|
|
$path, |
252
|
|
|
|
|
|
|
); |
253
|
|
|
|
|
|
|
|
254
|
8
|
|
|
|
|
22
|
my $old_attr = $old_map->{$path}{stat}{attributes}; |
255
|
8
|
|
|
|
|
16
|
my $new_attr = $new_map->{$path}{stat}{attributes}; |
256
|
|
|
|
|
|
|
|
257
|
8
|
|
|
|
|
13
|
for my $k ( keys %{$new_attr} ) { |
|
8
|
|
|
|
|
26
|
|
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
# Any possible info retrieved from stat will be numeric, so we can |
260
|
|
|
|
|
|
|
# always use numeric comparison safely. |
261
|
|
|
|
|
|
|
return ( attributes => [ $old_attr, $new_attr ] ) |
262
|
21
|
100
|
|
|
|
66
|
if $old_attr->{$k} != $new_attr->{$k}; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
4
|
|
|
|
|
16
|
return; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
sub _modify_event_maybe_content_changes { |
269
|
14
|
|
|
14
|
|
28
|
my $self = shift; |
270
|
14
|
|
|
|
|
24
|
my $path = shift; |
271
|
14
|
|
|
|
|
26
|
my $old_map = shift; |
272
|
14
|
|
|
|
|
20
|
my $new_map = shift; |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
return |
275
|
14
|
100
|
|
|
|
38
|
unless $self->_path_matches( $self->modify_includes_content, $path ); |
276
|
|
|
|
|
|
|
return ( |
277
|
4
|
|
|
|
|
127
|
content => [ $old_map->{$path}{content}, $new_map->{$path}{content} ] |
278
|
|
|
|
|
|
|
); |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
sub _path_matches { |
282
|
267
|
|
|
267
|
|
398
|
my $self = shift; |
283
|
267
|
|
|
|
|
339
|
my $matches = shift; |
284
|
267
|
|
|
|
|
361
|
my $path = shift; |
285
|
|
|
|
|
|
|
|
286
|
267
|
100
|
|
|
|
1095
|
return $matches if !ref $matches; |
287
|
|
|
|
|
|
|
|
288
|
172
|
|
|
|
|
242
|
foreach my $matcher ( @{$matches} ) { |
|
172
|
|
|
|
|
353
|
|
289
|
73
|
100
|
|
|
|
163
|
if ( my $ref = ref $matcher ) { |
290
|
54
|
100
|
|
|
|
107
|
if ( $ref eq 'Regexp' ) { |
|
|
50
|
|
|
|
|
|
291
|
51
|
100
|
|
|
|
387
|
return 1 if $path =~ /$matcher/; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
elsif ( $ref eq 'CODE' ) { |
294
|
3
|
|
|
|
|
7
|
local $_ = $path; |
295
|
3
|
50
|
|
|
|
7
|
return 1 if $matcher->($path); |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
else { |
299
|
19
|
100
|
|
|
|
38
|
return 1 if $path eq $matcher; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
|
303
|
155
|
|
|
|
|
490
|
return; |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
sub _remove_directory { |
307
|
2
|
|
|
2
|
|
7
|
my $self = shift; |
308
|
2
|
|
|
|
|
4
|
my $dir = shift; |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
$self->_set_directories( |
311
|
2
|
|
|
|
|
7
|
[ grep { $_ ne $dir } @{ $self->directories } ] ); |
|
2
|
|
|
|
|
62
|
|
|
2
|
|
|
|
|
9
|
|
312
|
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
|
## use critic |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
1; |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
# ABSTRACT: Role consumed by all watchers |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
__END__ |