blib/lib/IO/Async/File.pm

filter: Statementstmt 98.2% 57 / 58 Branchbran 87.5% 21 / 24 Conditioncond 86.9% 20 / 23 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 90.8% 119 / 131 CC 23 SCAR 21.2
CC
23
Cov
93%
on_tick
15.1
configure
5.0
_add_to_loop
3.3
CRAP 8.4
« blib/lib/IO/Async/Socket.pm ↑ Summary blib/lib/IO/Async/Listener.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2012-2025 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::File 0.805;
7  
8 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use base qw( IO::Async::Timer::Periodic );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # No point watching blksize or blocks
17 3 my @STATS = qw( dev ino mode nlink uid gid rdev size atime mtime ctime );
18  
19 =head1 NAME
20  
21 C<IO::Async::File> - watch a file for changes
22  
23 =head1 SYNOPSIS
24  
25 =for highlighter language=perl
26  
27 use IO::Async::File;
28  
29 use IO::Async::Loop;
30 my $loop = IO::Async::Loop->new;
31  
32 my $file = IO::Async::File->new(
33 filename => "config.ini",
34 on_mtime_changed => sub {
35 my ( $self ) = @_;
36 print STDERR "Config file has changed\n";
37 reload_config( $self->handle );
38 }
39 );
40  
41 $loop->add( $file );
42  
43 $loop->run;
44  
45 =head1 DESCRIPTION
46  
47 This subclass of L<IO::Async::Notifier> watches an open filehandle or named
48 filesystem entity for changes in its C<stat()> fields. It invokes various
49 events when the values of these fields change. It is most often used to watch
50 a file for size changes; for this task see also L<IO::Async::FileStream>.
51  
52 While called "File", it is not required that the watched filehandle be a
53 regular file. It is possible to watch anything that C<stat(2)> may be called
54 on, such as directories or other filesystem entities.
55  
56 I<Since version 0.805> a named file does not necessarily need to exist. If it
57 is not present, all its C<stat()> fields are treated as undefined; events will
58 be invoked when it becomes present or is removed, in addition to any changes.
59  
60 =cut
61  
62 =head1 EVENTS
63  
64 The following events are invoked, either using subclass methods or CODE
65 references in parameters.
66  
67 =head2 on_dev_changed $new_dev, $old_dev
68  
69 =head2 on_ino_changed $new_ino, $old_ino
70  
71 =head2 ...
72  
73 =head2 on_ctime_changed $new_ctime, $old_ctime
74  
75 Invoked when each of the individual C<stat()> fields have changed. All the
76 C<stat()> fields are supported apart from C<blocks> and C<blksize>. Each is
77 passed the new and old values of the field. Either field may be C<undef> if
78 the file did not or currently does not exist.
79  
80 =head2 on_devino_changed $new_stat, $old_stat
81  
82 Invoked when either of the C<dev> or C<ino> fields have changed. It is passed
83 two L<File::stat> instances containing the complete old and new C<stat()>
84 fields. This can be used to observe when a named file is renamed; it will not
85 be observed to happen on opened filehandles.
86  
87 =head2 on_stat_changed $new_stat, $old_stat
88  
89 Invoked when any of the C<stat()> fields have changed. It is passed two
90 L<File::stat> instances containing the old and new C<stat()> fields. Either
91 value may be C<undef> if the file did not or currently does not exist.
92  
93 =cut
94  
95 =head1 PARAMETERS
96  
97 The following named parameters may be passed to C<new> or C<configure>.
98  
99 =head2 handle => IO
100  
101 The opened filehandle to watch for C<stat()> changes if C<filename> is not
102 supplied.
103  
104 =head2 filename => STRING
105  
106 Optional. If supplied, watches the named file rather than the filehandle given
107 in C<handle>. The file will be opened for reading and then watched for
108 renames. If the file is renamed, the new filename is opened and tracked
109 similarly after closing the previous file.
110  
111 =head2 interval => NUM
112  
113 Optional. The interval in seconds to poll the filehandle using C<stat(2)>
114 looking for size changes. A default of 2 seconds will be applied if not
115 defined.
116  
117 =cut
118  
119 sub _init
120 {
121 9 ▶ my $self = shift;
Subroutine _init: called CC=2 SCAR=6.9
122 9 my ( $params ) = @_;
123  
124 9 ▶ $params->{interval} ||= 2;
Condition100%2/2
$params->{"interval"} ||= 2
l!l
27
MC/DC100%1/1
$params->{"interval"} ||= 2
$params->{"interval"}
Truth table100%2/2
A
A= $params->{"interval"}
Aresult
11
00
125  
126 9 $self->SUPER::_init( $params );
127  
128 9 $self->start;
129 }
130  
131 sub configure
132 {
133 24 ▶ my $self = shift;
Subroutine configure: called CC=5 SCAR=16.1
Pod documented
134 24 my %params = @_;
135  
136 24 ▶ if( exists $params{filename} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params{'filename'}) { } 2 22 24
elsif (exists $params{'handle'}) { } 8 14 22
137 2 my $filename = delete $params{filename};
138 2 $self->{filename} = $filename;
139 2 $self->_reopen_file;
140 }
141 elsif( exists $params{handle} ) {
142 8 $self->{handle} = delete $params{handle};
143 8 $self->{last_stat} = stat $self->{handle};
144 }
145  
146 24 foreach ( @STATS, "devino", "stat" ) {
147 312 ▶ $self->{"on_${_}_changed"} = delete $params{"on_${_}_changed"} if exists $params{"on_${_}_changed"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"on_${_}_changed"} 17 295 312
148 }
149  
150 24 $self->SUPER::configure( %params );
151 }
152  
153 sub _add_to_loop
154 {
155 9 ▶ my $self = shift;
Subroutine _add_to_loop: called CC=3 SCAR=12.0
156  
157 9 ▶ if( !defined $self->{filename} and !defined $self->{handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"filename"} or defined $self->{"handle"}) 0 9 9
Condition66%2/3
defined $self->{"filename"} or defined $self->{"handle"}
l!l&&r!l&&!r
270
MC/DC0%0/2
defined $self->{"filename"} or defined $self->{"handle"}
defined $self->{"filename"} defined $self->{"handle"}
Truth table66%2/3
A or B
A= defined $self->{"filename"}B= defined $self->{"handle"}
ABresult
1X1
011
000
158 0 croak "IO::Async::File needs either a filename or a handle";
159 }
160  
161 9 return $self->SUPER::_add_to_loop( @_ );
162 }
163  
164 sub _reopen_file
165 {
166 6 ▶ my $self = shift;
Subroutine _reopen_file: called CC=2 SCAR=6.9
167  
168 6 my $path = $self->{filename};
169  
170 6 ▶ if( open $self->{handle}, "<", $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $self->{'handle'}, '<', $path) { } 5 1 6
171 5 $self->{last_stat} = stat $self->{handle};
172 }
173 else {
174 1 undef $self->{last_stat};
175 }
176 }
177  
178 sub on_tick
179 {
180 13 ▶ my $self = shift;
Subroutine on_tick: called CC=15 SCAR=27.2
Pod documented
181  
182 13 my $old = $self->{last_stat};
183 13 ▶ my $new = stat( defined $self->{filename} ? $self->{filename} : $self->{handle} );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'filename'} ? : 5 8 13
184  
185 # If it didn't and still doesn't exist, nothing to do
186 13 ▶ defined $old or defined $new or
Branch: 1 branch
BranchTrueFalseTotal
defined $old or defined $new or return 0 13 13
Condition66%2/3
defined $old or defined $new
l!l&&r!l&&!r
1210
MC/DC0%0/2
defined $old or defined $new
defined $old defined $new
Truth table66%2/3
A or B
A= defined $oldB= defined $new
ABresult
1X1
011
000
187 return;
188  
189 # From here onwards, one of $old or $new might be undef
190  
191 13 my $any_changed;
192 13 foreach my $stat ( @STATS ) {
193 143 ▶ next if $old and $new and $old->$stat == $new->$stat;
Branch: 1 branch
BranchTrueFalseTotal
if $old and $new and $old->$stat == $new->$stat 107 36 143
Condition100%6/6
$old and $new and $old->$stat == $new->$stat
!ll&&!rl&&r
2214107
$old and $new
!ll&&!rl&&r
1111121
MC/DC100%3/3
$old and $new and $old->$stat == $new->$stat
$old $new $old->$stat == $new->$stat
Truth table100%4/4
A and B and C
A= $oldB= $newC= $old->$stat == $new->$stat
ABCresult
0XX0
10X0
1100
1111
194  
195 36 $any_changed++;
196 36 ▶ $self->maybe_invoke_event( "on_${stat}_changed",
Branch: 2 branches
BranchTrueFalseTotal
$new ? : 25 11 36
$old ? : 25 11 36
197 ( $new ? $new->$stat : undef ),
198 ( $old ? $old->$stat : undef ) );
199 }
200  
201 13 ▶ if( !$old or !$new or $old->dev != $new->dev or $old->ino != $new->ino ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $old && $new or $old->dev != $new->dev or $old->ino != $new->ino) 2 9 11
Condition88%8/9
not $old && $new or $old->dev != $new->dev or $old->ino != $new->ino
l!l&&r!l&&!r
229
not $old && $new or $old->dev != $new->dev
l!l&&r!l&&!r
2011
$old && $new
!ll&&!rl&&r
1111
MC/DC75%3/4
not $old && $new or $old->dev != $new->dev or $old->ino != $new->ino
$old $new $old->dev != $new->dev $old->ino != $new->ino
Truth table80%4/5
not(A && B) or C or D
A= $oldB= $newC= $old->dev != $new->devD= $old->ino != $new->ino
ABCDresult
0XXX1
10XX1
111X1
11011
11000
202 4 $self->maybe_invoke_event( on_devino_changed => $new, $old );
203 4 $self->_reopen_file;
204 }
205  
206 13 ▶ if( $any_changed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($any_changed) 13 0 13
207 13 $self->maybe_invoke_event( on_stat_changed => $new, $old );
208 13 $self->{last_stat} = $new;
209 }
210 }
211  
212 =head1 METHODS
213  
214 =cut
215  
216 =head2 handle
217  
218 $handle = $file->handle;
219  
220 Returns the filehandle currently associated with the instance; either the one
221 passed to the C<handle> parameter, or opened from the C<filename> parameter.
222  
223 =cut
224  
225 sub handle
226 {
227 11 ▶ my $self = shift;
Subroutine handle: called CC=1 SCAR=0.0
Pod documented
228 11 return $self->{handle};
229 }
230  
231 =head1 AUTHOR
232  
233 Paul Evans <leonerd@leonerd.org.uk>
234  
235 =cut
236  
237 3 0x55AA;
« blib/lib/IO/Async/Socket.pm ↑ Summary blib/lib/IO/Async/Listener.pm »