blib/lib/File/Write/Rotate.pm

filter: Statementstmt 92.3% 218 / 236 Branchbran 75.0% 99 / 132 Conditioncond 80.4% 37 / 46 MC/DCmcdc 66.6% 18 / 27 Subroutinesub 95.2% 20 / 21 Podpod 100% 7 / 7 totaltotal 85.0% 399 / 469 CC 89 SCAR 25.9
CC
89
Cov
86%
new
23.3
_rotate_and_delete
18.6
_rotate_and_open
17.9
CRAP 13.3
↑ Summary
1 ## no critic: InputOutput::ProhibitOneArgSelect
2  
3 package File::Write::Rotate;
4  
5 4 our $DATE = '2019-06-27'; # DATE
6 4 our $VERSION = '0.321'; # VERSION
7  
8 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # we must not use Log::Any, looping if we are used as log output
13 #use Log::Any '$log';
14  
15 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use IO::Compress::Gzip qw(gzip $GzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 #use Taint::Runtime qw(untaint is_tainted);
19 4 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 our $Debug;
22  
23 278 ▶sub new {
Subroutine new: called CC=20 SCAR=31.5
Pod documented
24 278 my $class = shift;
25 278 my %args0 = @_;
26  
27 278 my %args;
28  
29 defined($args{dir} = delete $args0{dir})
30 278 ▶ or die "Please specify dir";
Branch: 1 branch
BranchTrueFalseTotal
defined($args{"dir"} = delete $args0{"dir"}) or die "Please specify dir" 278 0 278
31 defined($args{prefix} = delete $args0{prefix})
32 278 ▶ or die "Please specify prefix";
Branch: 1 branch
BranchTrueFalseTotal
defined($args{"prefix"} = delete $args0{"prefix"}) or die "Please specify prefix" 278 0 278
33 278 ▶ $args{suffix} = delete($args0{suffix}) // "";
Condition100%2/2
delete $args0{"suffix"} // ""
l!l
12266
MC/DC100%1/1
delete $args0{"suffix"} // ""
delete $args0{"suffix"}
Truth table100%2/2
A
A= delete $args0{"suffix"}
Aresult
11
00
34  
35 278 ▶ $args{size} = delete($args0{size}) // 0;
Condition100%2/2
delete $args0{"size"} // 0
l!l
23444
MC/DC100%1/1
delete $args0{"size"} // 0
delete $args0{"size"}
Truth table100%2/2
A
A= delete $args0{"size"}
Aresult
11
00
36  
37 278 $args{period} = delete($args0{period});
38 278 ▶ if ($args{period}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"period"}) 39 239 278
39 39 ▶ $args{period} =~ /\A(daily|day|month|monthly|year|yearly)\z/
Branch: 1 branch
BranchTrueFalseTotal
$args{"period"} =~ /\A(daily|day|month|monthly|year|yearly)\z/ or die "Invalid period, please use daily/monthly/yearly" 39 0 39
40 or die "Invalid period, please use daily/monthly/yearly";
41 }
42  
43 278 for (map {"hook_$_"} qw(before_rotate after_rotate after_create
44 before_write a)) {
45 1390 ▶ next unless $args0{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $args0{$_} 1373 17 1390
46 17 $args{$_} = delete($args0{$_});
47 die "Invalid $_, please supply a coderef"
48 17 ▶ unless ref($args{$_}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{$_} eq "CODE" 0 17 17
49 }
50  
51 278 ▶ if (!$args{period} && !$args{size}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"period"} or $args{"size"}) 21 257 278
Condition100%3/3
$args{"period"} or $args{"size"}
l!l&&r!l&&!r
3921821
MC/DC100%2/2
$args{"period"} or $args{"size"}
$args{"period"} $args{"size"}
Truth table100%3/3
A or B
A= $args{"period"}B= $args{"size"}
ABresult
1X1
011
000
52 21 $args{size} = 10 * 1024 * 1024;
53 }
54  
55 278 ▶ $args{histories} = delete($args0{histories}) // 10;
Condition100%2/2
delete $args0{"histories"} // 10
l!l
26252
MC/DC100%1/1
delete $args0{"histories"} // 10
delete $args0{"histories"}
Truth table100%2/2
A
A= delete $args0{"histories"}
Aresult
11
00
56  
57 278 $args{binmode} = delete($args0{binmode});
58  
59 278 $args{buffer_size} = delete($args0{buffer_size});
60  
61 278 ▶ $args{lock_mode} = delete($args0{lock_mode}) // 'write';
Condition100%2/2
delete $args0{"lock_mode"} // "write"
l!l
3275
MC/DC100%1/1
delete $args0{"lock_mode"} // "write"
delete $args0{"lock_mode"}
Truth table100%2/2
A
A= delete $args0{"lock_mode"}
Aresult
11
00
62 278 ▶ $args{lock_mode} =~ /\A(none|write|exclusive)\z/
Branch: 1 branch
BranchTrueFalseTotal
$args{"lock_mode"} =~ /\A(none|write|exclusive)\z/ or die "Invalid lock_mode, please use none/write/exclusive" 278 0 278
63 or die "Invalid lock_mode, please use none/write/exclusive";
64  
65 278 $args{rotate_probability} = delete($args0{rotate_probability});
66 278 ▶ if (defined $args{rotate_probability}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"rotate_probability"}) 0 278 278
67 0 ▶ $args{rotate_probability} > 0 && $args{rotate_probability} < 1.0
Branch: 1 branch
BranchTrueFalseTotal
$args{"rotate_probability"} > 0 and $args{"rotate_probability"} < 1 or die "Invalid rotate_probability, must be 0 < x < 1" 0 0 0
Condition0%0/3
$args{"rotate_probability"} > 0 and $args{"rotate_probability"} < 1
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"rotate_probability"} > 0 and $args{"rotate_probability"} < 1
$args{"rotate_probability"} > 0 $args{"rotate_probability"} < 1
Truth table0%0/3
A and B
A= $args{"rotate_probability"} > 0B= $args{"rotate_probability"} < 1
ABresult
0X0
100
111
68 or die "Invalid rotate_probability, must be 0 < x < 1";
69 }
70  
71 278 ▶ if (keys %args0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args0) 0 278 278
72 0 die "Unknown arguments to new(): ".join(", ", sort keys %args0);
73 }
74  
75 278 $args{_buffer} = [];
76  
77 278 my $self = bless \%args, $class;
78  
79 $self->{_exclusive_lock} = $self->_get_lock
80 278 ▶ if $self->{lock_mode} eq 'exclusive';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lock_mode"} eq "exclusive" 1 277 278
81  
82 278 $self;
83 }
84  
85 2 ▶sub buffer_size {
Subroutine buffer_size: called CC=2 SCAR=6.9
Pod documented
86 2 my $self = shift;
87 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 1 2
88 1 my $old = $self->{buffer_size};
89 1 $self->{buffer_size} = $_[0];
90 1 return $old;
91 } else {
92 1 return $self->{buffer_size};
93 }
94 }
95  
96 1 ▶sub handle {
Subroutine handle: called CC=1 SCAR=0.0
Pod documented
97 1 my $self = shift;
98 1 $self->{_fh};
99 }
100  
101 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
102 0 my $self = shift;
103 0 $self->{_fp};
104 }
105  
106 # file path, without the rotate suffix
107 801 ▶sub _file_path {
Subroutine _file_path: called CC=7 SCAR=19.5
108 801 my ($self) = @_;
109  
110 # _now is calculated every time we access this method
111 801 $self->{_now} = time();
112  
113 801 my @lt = localtime($self->{_now});
114 801 $lt[5] += 1900;
115 801 $lt[4]++;
116  
117 801 my $period;
118 801 ▶ if ($self->{period}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'period'}) { } 110 691 801
119 110 ▶ if ($self->{period} =~ /year/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'period'} =~ /year/i) { } 13 97 110
elsif ($self->{'period'} =~ /month/) { } 13 84 97
elsif ($self->{'period'} =~ /day|daily/) { } 84 0 84
120 13 $period = sprintf("%04d", $lt[5]);
121 } elsif ($self->{period} =~ /month/) {
122 13 $period = sprintf("%04d-%02d", $lt[5], $lt[4]);
123 } elsif ($self->{period} =~ /day|daily/) {
124 84 $period = sprintf("%04d-%02d-%02d", $lt[5], $lt[4], $lt[3]);
125 }
126 } else {
127 691 $period = "";
128 }
129  
130 my $path = join(
131 '',
132 $self->{dir}, '/',
133 $self->{prefix},
134 length($period) ? ".$period" : "",
135 $self->{suffix},
136 801 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
length $period ? : 110 691 801
137 801 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 308 493 801
138 308 return ($path, $period);
139 } else {
140 493 return $path;
141 }
142 }
143  
144 996 ▶sub lock_file_path {
Subroutine lock_file_path: called CC=1 SCAR=0.0
Pod documented
145 996 my ($self) = @_;
146 996 return File::Spec->catfile($self->{dir}, $self->{prefix} . '.lck');
147 }
148  
149 592 ▶sub _get_lock {
Subroutine _get_lock: called CC=3 SCAR=11.0
150 592 my ($self) = @_;
151 592 ▶ return undef if $self->{lock_mode} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lock_mode"} eq "none" 4 588 592
152 588 ▶ return $self->{_weak_lock} if defined($self->{_weak_lock});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_weak_lock"} 78 510 588
153  
154 510 require File::Flock::Retry;
155 510 my $lock = File::Flock::Retry->lock($self->lock_file_path);
156 510 $self->{_weak_lock} = $lock;
157 510 weaken $self->{_weak_lock};
158 510 return $lock;
159 }
160  
161 # will return \@files. each entry is [filename without compress suffix,
162 # rotate_suffix (for sorting), period (for sorting), compress suffix (for
163 # renaming back)]
164 98 ▶sub _get_files {
Subroutine _get_files: called CC=9 SCAR=22.2
165 98 my ($self) = @_;
166  
167 98 ▶ opendir my ($dh), $self->{dir} or do {
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->{"dir"} or do { warn qq[Can't opendir '$self->{"dir"}': $!]; return } 98 0 98
168 0 warn "Can't opendir '$self->{dir}': $!";
169 0 return;
170 };
171  
172 98 my @files;
173 98 ▶ while (my $e = readdir($dh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = readdir $dh)) 463 98 561
174 463 ▶ my $cs; $cs = $1 if $e =~ s/(\.gz)\z//; # compress suffix
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ s/(\.gz)\z// 1 462 463
175 463 ▶ next unless $e =~ /\A\Q$self->{prefix}\E
Branch: 1 branch
BranchTrueFalseTotal
unless $e =~ /\A\Q$self->{"prefix"}\E (?:\. (?<period>\d{4}(?:-\d\d(?:-\d\d)?)?) )? \Q$self->{"suffix"}\E (?:\. (?<rotate_suffix>\d+) )? \z /x 311 152 463
176 (?:\. (?<period>\d{4}(?:-\d\d(?:-\d\d)?)?) )?
177 \Q$self->{suffix}\E
178 (?:\. (?<rotate_suffix>\d+) )?
179 \z
180 /x;
181 push @files,
182 152 ▶ [ $e, $+{rotate_suffix} // 0, $+{period} // "", $cs // "" ];
Condition100%6/6
$+{"rotate_suffix"} // 0
l!l
52100
$+{"period"} // ""
l!l
11240
$cs // ""
l!l
1151
MC/DC100%1/1
$+{"rotate_suffix"} // 0
$+{"rotate_suffix"}
MC/DC100%1/1
$+{"period"} // ""
$+{"period"}
MC/DC100%1/1
$cs // ""
$cs
Truth table100%2/2
A
A= $+{"rotate_suffix"}
Aresult
11
00
Truth table100%2/2
A
A= $+{"period"}
Aresult
11
00
Truth table100%2/2
A
A= $cs
Aresult
11
00
183 }
184 98 closedir($dh);
185  
186 98 ▶ [ sort { $a->[2] cmp $b->[2] || $b->[1] <=> $a->[1] } @files ];
Condition66%2/3
$File::Write::Rotate::a->[2] cmp $File::Write::Rotate::b->[2] || $File::Write::Rotate::b->[1] <=> $File::Write::Rotate::a->[1]
l!l&&r!l&&!r
56650
MC/DC0%0/2
$File::Write::Rotate::a->[2] cmp $File::Write::Rotate::b->[2] || $File::Write::Rotate::b->[1] <=> $File::Write::Rotate::a->[1]
$File::Write::Rotate::a->[2] cmp $File::Write::Rotate::b->[2] $File::Write::Rotate::b->[1] <=> $File::Write::Rotate::a->[1]
Truth table66%2/3
A || B
A= $File::Write::Rotate::a->[2] cmp $File::Write::Rotate::b->[2]B= $File::Write::Rotate::b->[1] <=> $File::Write::Rotate::a->[1]
ABresult
1X1
011
000
187 }
188  
189 # rename (increase rotation suffix) and keep only n histories. note: failure in
190 # rotating should not be fatal, we just warn and return.
191 85 ▶sub _rotate_and_delete {
Subroutine _rotate_and_delete: called CC=17 SCAR=29.2
192 85 my ($self, %opts) = @_;
193  
194 85 my $delete_only = $opts{delete_only};
195 85 my $lock = $self->_get_lock;
196 CASE:
197 {
198 85 ▶ my $files = $self->_get_files or last CASE;
Branch: 1 branch
BranchTrueFalseTotal
my $files = $self->_get_files or last CASE 85 0 85
199  
200 # is there a compression process in progress? this is marked by the
201 # existence of <prefix>-compress.pid PID file.
202 #
203 # XXX check validity of PID file, otherwise a stale PID file will always
204 # prevent rotation to be done
205 85 ▶ if (-f "$self->{dir}/$self->{prefix}-compress.pid") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f qq[$self->{"dir"}/$self->{"prefix"}-compress.pid]) 0 85 85
206 0 warn "Compression is in progress, rotation is postponed";
207 0 last CASE;
208 }
209  
210 7 $self->{hook_before_rotate}->($self, [map {$_->[0]} @$files])
211 85 ▶ if $self->{hook_before_rotate};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hook_before_rotate"} 7 78 85
212  
213 85 my @deleted;
214 my @renamed;
215  
216 85 my $i;
217 85 my $dir = $self->{dir};
218 85 ▶ my $rotating_period = @$files ? $files->[-1][2] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$files ? : 53 32 85
219 85 for my $f (@$files) {
220 122 my ($orig, $rs, $period, $cs) = @$f;
221 122 $i++;
222  
223 #say "DEBUG: is_tainted \$dir? ".is_tainted($dir);
224 #say "DEBUG: is_tainted \$orig? ".is_tainted($orig);
225 #say "DEBUG: is_tainted \$cs? ".is_tainted($cs);
226  
227 # TODO actually, it's more proper to taint near the source (in this
228 # case, _get_files)
229 #untaint \$orig;
230 122 ($orig) = $orig =~ /(.*)/s; # we use this instead, no module needed
231  
232 122 ▶ if ($i <= @$files - $self->{histories}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= @$files - $self->{"histories"}) 22 100 122
233 22 ▶ say "DEBUG: Deleting old rotated file $dir/$orig$cs ..."
Branch: 1 branch
BranchTrueFalseTotal
if $File::Write::Rotate::Debug 0 22 22
234 if $Debug;
235 22 ▶ if (unlink "$dir/$orig$cs") {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink "$dir/$orig$cs") { } 22 0 22
236 22 push @deleted, "$orig$cs";
237 } else {
238 0 warn "Can't delete $dir/$orig$cs: $!";
239 }
240 22 next;
241 }
242 100 ▶ if (!$delete_only && defined($rotating_period) && $period eq $rotating_period) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $delete_only and defined $rotating_period and $period eq $rotating_period) 48 52 100
Condition83%5/6
not $delete_only and defined $rotating_period and $period eq $rotating_period
!ll&&!rl&&r
351748
not $delete_only and defined $rotating_period
!ll&&!rl&&r
35065
MC/DC66%2/3
not $delete_only and defined $rotating_period and $period eq $rotating_period
not $delete_only defined $rotating_period $period eq $rotating_period
Truth table75%3/4
not(A) and B and C
A= not $delete_onlyB= defined $rotating_periodC= $period eq $rotating_period
ABCresult
0XX0
10X0
1100
1111
243 48 my $new = $orig;
244 48 ▶ if ($rs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rs) { } 14 34 48
245 14 $new =~ s/\.(\d+)\z/"." . ($1+1)/e;
246 } else {
247 34 $new .= ".1";
248 }
249 48 ▶ if ($new ne $orig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new ne $orig) 48 0 48
250 48 ▶ say "DEBUG: Renaming rotated file $dir/$orig$cs -> ".
Branch: 1 branch
BranchTrueFalseTotal
if $File::Write::Rotate::Debug 0 48 48
251 "$dir/$new$cs ..." if $Debug;
252 48 ▶ if (rename "$dir/$orig$cs", "$dir/$new$cs") {
Branch: 1 branch
BranchTrueFalseTotal
if (rename "$dir/$orig$cs", "$dir/$new$cs") { } 48 0 48
253 48 push @renamed, "$new$cs";
254 } else {
255 0 warn "Can't rename '$dir/$orig$cs' -> '$dir/$new$cs': $!";
256 }
257 }
258 }
259 }
260  
261 $self->{hook_after_rotate}->($self, \@renamed, \@deleted)
262 85 ▶ if $self->{hook_after_rotate};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hook_after_rotate"} 7 78 85
263 } # CASE
264 }
265  
266 298 ▶sub _open {
Subroutine _open: called CC=6 SCAR=18.3
267 298 my $self = shift;
268  
269 298 my ($fp, $period) = $self->_file_path;
270 298 ▶ open $self->{_fh}, ">>", $fp or die "Can't open '$fp': $!";
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"_fh"}, ">>", $fp or die "Can't open '${fp}': $!" 298 0 298
271 298 ▶ if (defined $self->{binmode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"binmode"}) 2 296 298
272 2 ▶ if ($self->{binmode} eq "1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'binmode'} eq '1') { } 0 2 2
273 0 binmode $self->{_fh};
274 } else {
275 binmode $self->{_fh}, $self->{binmode}
276 2 ▶ or die "Can't set PerlIO layer on '$fp' ".
Branch: 1 branch
BranchTrueFalseTotal
binmode $self->{"_fh"}, $self->{"binmode"} or die "Can't set PerlIO layer on '${fp}' " . qq[to '$self->{"binmode"}': $!] 2 0 2
277 "to '$self->{binmode}': $!";
278 }
279 }
280 298 my $oldfh = select $self->{_fh};
281 298 $| = 1;
282 298 select $oldfh; # set autoflush
283 298 $self->{_fp} = $fp;
284 298 ▶ $self->{hook_after_create}->($self) if $self->{hook_after_create};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hook_after_create"} 7 291 298
285 }
286  
287 # (re)open file and optionally rotate if necessary
288 493 ▶sub _rotate_and_open {
Subroutine _rotate_and_open: called CC=16 SCAR=28.8
289  
290 493 my $self = shift;
291 493 my ($do_open, $do_rotate) = @_;
292 493 my $fp;
293 my %rotate_params;
294  
295 CASE:
296 {
297 # if instructed, only do rotate some of the time to shave overhead
298 493 ▶ if ($self->{rotate_probability} && $self->{_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rotate_probability"} and $self->{"_fh"}) 0 493 493
Condition33%1/3
$self->{"rotate_probability"} and $self->{"_fh"}
!ll&&!rl&&r
49300
MC/DC0%0/2
$self->{"rotate_probability"} and $self->{"_fh"}
$self->{"rotate_probability"} $self->{"_fh"}
Truth table33%1/3
A and B
A= $self->{"rotate_probability"}B= $self->{"_fh"}
ABresult
0X0
100
111
299 0 ▶ last CASE if rand() > $self->{rotate_probability};
Branch: 1 branch
BranchTrueFalseTotal
if rand() > $self->{"rotate_probability"} 0 0 0
300 }
301  
302 493 $fp = $self->_file_path;
303 493 ▶ unless (-e $fp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $fp) 50 443 493
304 50 $do_open++;
305 50 $do_rotate++;
306 50 $rotate_params{delete_only} = 1;
307 50 last CASE;
308 }
309  
310 # file is not opened yet, open
311 443 ▶ unless ($self->{_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_fh"}) 219 224 443
312 219 $self->_open;
313 }
314  
315 # period has changed, rotate
316 443 ▶ if ($self->{_fp} ne $fp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_fp"} ne $fp) 0 443 443
317 0 $do_rotate++;
318 0 $rotate_params{delete_only} = 1;
319 0 last CASE;
320 }
321  
322 # check whether size has been exceeded
323 443 my $inode;
324  
325 443 ▶ if ($self->{size} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"size"} > 0) 437 6 443
326  
327 437 my @st = stat($self->{_fh});
328 437 my $size = $st[7];
329 437 $inode = $st[1];
330  
331 437 ▶ if ($size >= $self->{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size >= $self->{'size'}) { } 28 409 437
332 28 ▶ say "DEBUG: Size of $self->{_fp} is $size, exceeds $self->{size}, rotating ..."
Branch: 1 branch
BranchTrueFalseTotal
if $File::Write::Rotate::Debug 0 28 28
333 if $Debug;
334 28 $do_rotate++;
335 28 last CASE;
336 } else {
337 # stat the current file (not our handle _fp)
338 409 my @st = stat($fp);
339 409 ▶ die "Can't stat '$fp': $!" unless @st;
Branch: 1 branch
BranchTrueFalseTotal
unless @st 0 409 409
340 409 my $finode = $st[1];
341  
342 # check whether other process has rename/rotate under us (for
343 # example, 'prefix' has been moved to 'prefix.1'), in which case
344 # we need to reopen
345 409 ▶ if (defined($inode) && $finode != $inode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inode and $finode != $inode) 1 408 409
Condition66%2/3
defined $inode and $finode != $inode
!ll&&!rl&&r
04081
MC/DC50%1/2
defined $inode and $finode != $inode
defined $inode $finode != $inode
Truth table66%2/3
A and B
A= defined $inodeB= $finode != $inode
ABresult
0X0
100
111
346 1 $do_open++;
347 }
348 }
349  
350 }
351 } # CASE
352  
353 493 ▶ $self->_rotate_and_delete(%rotate_params) if $do_rotate;
Branch: 1 branch
BranchTrueFalseTotal
if $do_rotate 78 415 493
354 493 ▶ $self->_open if $do_rotate || $do_open; # (re)open
Branch: 1 branch
BranchTrueFalseTotal
if $do_rotate or $do_open 79 414 493
Condition100%3/3
$do_rotate or $do_open
l!l&&r!l&&!r
781414
MC/DC100%2/2
$do_rotate or $do_open
$do_rotate $do_open
Truth table100%3/3
A or B
A= $do_rotateB= $do_open
ABresult
1X1
011
000
355 }
356  
357 493 ▶sub write {
Subroutine write: called CC=6 SCAR=17.9
Pod documented
358 493 my $self = shift;
359  
360 # the buffering implementation is currently pretty naive. it assume any
361 # die() as a write failure and store the message to buffer.
362  
363 # FYI: if privilege is dropped from superuser, the failure is usually at
364 # locking the lock file (permission denied).
365  
366 493 my @msg = (map( {@$_} @{ $self->{_buffer} } ), @_);
367  
368 493 eval {
369 493 my $lock = $self->_get_lock;
370  
371 493 $self->_rotate_and_open;
372  
373 $self->{hook_before_write}->($self, \@msg, $self->{_fh})
374 493 ▶ if $self->{hook_before_write};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hook_before_write"} 13 480 493
375  
376 488 print { $self->{_fh} } @msg;
377 488 $self->{_buffer} = [];
378  
379 };
380 493 my $err = $@;
381  
382 493 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 5 488 493
383 5 ▶ if (($self->{buffer_size} // 0) > @{ $self->{_buffer} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{'buffer_size'} // 0) > @{$self->{'_buffer'};}) { } 4 1 5
Condition50%1/2
$self->{"buffer_size"} // 0
l!l
50
MC/DC0%0/1
$self->{"buffer_size"} // 0
$self->{"buffer_size"}
Truth table50%1/2
A
A= $self->{"buffer_size"}
Aresult
11
00
384 # put message to buffer temporarily
385 4 push @{ $self->{_buffer} }, [@_];
386 } else {
387 # buffer is already full, let's dump the buffered + current message
388 # to the die message anyway.
389 die join(
390 "",
391 "Can't write",
392 (
393 1 @{ $self->{_buffer} }
394 ? " (buffer is full, "
395 1 ▶ . scalar(@{ $self->{_buffer} })
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'_buffer'};} ? : 1 0 1
396 . " message(s))"
397 : ""
398 ),
399 ": $err, message(s)=",
400 @msg
401 );
402 }
403 }
404 }
405  
406 13 ▶sub compress {
Subroutine compress: called CC=12 SCAR=25.4
Pod documented
407 13 my ($self) = shift;
408  
409 13 my $lock = $self->_get_lock;
410 13 my $files_ref = $self->_get_files;
411 13 my $done_compression = 0;
412  
413 13 ▶ if (@{$files_ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$files_ref;}) 13 0 13
414 13 require Proc::PID::File;
415  
416 my $pid = Proc::PID::File->new(
417 dir => $self->{dir},
418 13 name => "$self->{prefix}-compress",
419 verify => 1,
420 );
421 13 my $latest_period = $files_ref->[-1][2];
422  
423 13 ▶ if ($pid->alive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid->alive) { } 0 13 13
424 0 warn "Another compression is in progress";
425 } else {
426 13 my @tocompress;
427 #use DD; dd $self;
428 13 for my $file_ref (@{$files_ref}) {
429 30 my ($orig, $rs, $period, $cs) = @{ $file_ref };
430 #say "D:compress: orig=<$orig> rs=<$rs> period=<$period> cs=<$cs>";
431 30 ▶ next if $cs; # already compressed
Branch: 1 branch
BranchTrueFalseTotal
if $cs 0 30 30
432 30 ▶ next if !$self->{period} && !$rs; # not old file
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"period"} or $rs 5 25 30
Condition100%3/3
$self->{"period"} or $rs
l!l&&r!l&&!r
2055
MC/DC100%2/2
$self->{"period"} or $rs
$self->{"period"} $rs
Truth table100%3/3
A or B
A= $self->{"period"}B= $rs
ABresult
1X1
011
000
433 25 ▶ next if $self->{period} && $period eq $latest_period; # not old file
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"period"} and $period eq $latest_period 8 17 25
Condition100%3/3
$self->{"period"} and $period eq $latest_period
!ll&&!rl&&r
5128
MC/DC100%2/2
$self->{"period"} and $period eq $latest_period
$self->{"period"} $period eq $latest_period
Truth table100%3/3
A and B
A= $self->{"period"}B= $period eq $latest_period
ABresult
0X0
100
111
434 17 push @tocompress, File::Spec->catfile($self->{dir}, $orig);
435 }
436  
437 13 ▶ if (@tocompress) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tocompress) 13 0 13
438 13 for my $file (@tocompress) {
439 gzip($file => "$file.gz")
440 0 ▶ or do { warn "gzip failed: $GzipError\n"; next };
Branch: 1 branch
BranchTrueFalseTotal
gzip($file, "$file.gz") or do { warn "gzip failed: $File::Write::Rotate::GzipError\n"; next } 17 0 17
441 17 unlink $file;
442 }
443 13 $done_compression = 1;
444 }
445 }
446 }
447  
448 13 return $done_compression;
449  
450 }
451  
452 278 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
453 278 my ($self) = @_;
454  
455 # Proc::PID::File's DESTROY seem to create an empty PID file, remove it.
456 278 unlink "$self->{dir}/$self->{prefix}-compress.pid";
457 }
458  
459 4 1;
460  
461 # ABSTRACT: Write to files that archive/rotate themselves
462  
463 __END__
↑ Summary