| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | defined($args{prefix} = delete $args0{prefix}) | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 278 | ▶ | or die "Please specify prefix"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 278 | ▶ | $args{suffix} = delete($args0{suffix}) // ""; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args0{"suffix"} // ""
MC/DC100%1/1
delete $args0{"suffix"} // ""
delete $args0{"suffix"}
Truth table100%2/2
A
A= delete $args0{"suffix"}
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 278 | ▶ | $args{size} = delete($args0{size}) // 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args0{"size"} // 0
MC/DC100%1/1
delete $args0{"size"} // 0
delete $args0{"size"}
Truth table100%2/2
A
A= delete $args0{"size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 278 | $args{period} = delete($args0{period}); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 278 | ▶ | if ($args{period}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 39 | ▶ | $args{period} =~ /\A(daily|day|month|monthly|year|yearly)\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 17 | $args{$_} = delete($args0{$_}); | |||||||||||||||||||||||||||||||||||||||||
| 47 | die "Invalid $_, please supply a coderef" | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 17 | ▶ | unless ref($args{$_}) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 278 | ▶ | if (!$args{period} && !$args{size}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"period"} or $args{"size"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 21 | $args{size} = 10 * 1024 * 1024; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 278 | ▶ | $args{histories} = delete($args0{histories}) // 10; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args0{"histories"} // 10
MC/DC100%1/1
delete $args0{"histories"} // 10
delete $args0{"histories"}
Truth table100%2/2
A
A= delete $args0{"histories"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
delete $args0{"lock_mode"} // "write"
delete $args0{"lock_mode"}
Truth table100%2/2
A
A= delete $args0{"lock_mode"}
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 278 | ▶ | $args{lock_mode} =~ /\A(none|write|exclusive)\z/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $args{rotate_probability} > 0 && $args{rotate_probability} < 1.0 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"rotate_probability"} > 0 and $args{"rotate_probability"} < 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | or die "Invalid rotate_probability, must be 0 < x < 1"; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 278 | ▶ | if (keys %args0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 110 | ▶ | if ($self->{period} =~ /year/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 801 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 588 | ▶ | return $self->{_weak_lock} if defined($self->{_weak_lock}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 463 | ▶ | my $cs; $cs = $1 if $e =~ s/(\.gz)\z//; # compress suffix | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 463 | ▶ | next unless $e =~ /\A\Q$self->{prefix}\E | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
$+{"period"} // ""
$cs // ""
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"}
Truth table100%2/2
A
A= $+{"period"}
Truth table100%2/2
A
A= $cs
| |||||||||||||||||||||||||||||||||||||||||||
| 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]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 22 | ▶ | say "DEBUG: Deleting old rotated file $dir/$orig$cs ..." | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | if $Debug; | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 22 | ▶ | if (unlink "$dir/$orig$cs") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not $delete_only and defined $rotating_period and $period eq $rotating_period
not $delete_only and defined $rotating_period
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
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 48 | my $new = $orig; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 48 | ▶ | if ($rs) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 48 | ▶ | say "DEBUG: Renaming rotated file $dir/$orig$cs -> ". | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | "$dir/$new$cs ..." if $Debug; | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 48 | ▶ | if (rename "$dir/$orig$cs", "$dir/$new$cs") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 298 | ▶ | if (defined $self->{binmode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 2 | ▶ | if ($self->{binmode} eq "1") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->{"rotate_probability"} and $self->{"_fh"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | last CASE if rand() > $self->{rotate_probability}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 493 | $fp = $self->_file_path; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 493 | ▶ | unless (-e $fp) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 219 | $self->_open; | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | # period has changed, rotate | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 443 | ▶ | if ($self->{_fp} ne $fp) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 28 | ▶ | say "DEBUG: Size of $self->{_fp} is $size, exceeds $self->{size}, rotating ..." | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $inode and $finode != $inode
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 493 | ▶ | $self->_open if $do_rotate || $do_open; # (re)open | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$do_rotate or $do_open
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 5 | ▶ | if (($self->{buffer_size} // 0) > @{ $self->{_buffer} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"buffer_size"} // 0
MC/DC0%0/1
$self->{"buffer_size"} // 0
$self->{"buffer_size"}
Truth table50%1/2
A
A= $self->{"buffer_size"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | 30 | ▶ | next if !$self->{period} && !$rs; # not old file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"period"} or $rs
MC/DC100%2/2
$self->{"period"} or $rs
$self->{"period"}
$rs
Truth table100%3/3
A or B
A= $self->{"period"}B= $rs
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | 25 | ▶ | next if $self->{period} && $period eq $latest_period; # not old file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"period"} and $period eq $latest_period
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
| |||||||||||||||||||||||||||||||||||||||||||
| 434 | 17 | push @tocompress, File::Spec->catfile($self->{dir}, $orig); | |||||||||||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 13 | ▶ | if (@tocompress) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 438 | 13 | for my $file (@tocompress) { | |||||||||||||||||||||||||||||||||||||||||
| 439 | gzip($file => "$file.gz") | ||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | or do { warn "gzip failed: $GzipError\n"; next }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||