blib/lib/Logfile/Rotate.pm

filter: Statementstmt 96.9% 126 / 130 Branchbran 64.5% 62 / 96 Conditioncond 70.8% 17 / 24 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 80.1% 226 / 282 CC 58 SCAR 31.1
CC
58
Cov
82%
rotate
31.8
new
28.9
_gzip
7.4
CRAP 22.5
↑ Summary
1 #!/usr/bin/perl
2 ###############################################################################
3 #
4 # $Id: Rotate.pm,v 1.5 2000/08/29 03:57:23 paulg Exp $ vim:ts=4
5 #
6 # Copyright (c) 1997-99 Paul Gampe. All Rights Reserved.
7 #
8 # This program is free software; you can redistribute it and/or modify it
9 # under the same terms as Perl itself. See COPYRIGHT section below.
10 #
11 ###############################################################################
12  
13 ###############################################################################
14 ## L I B R A R I E S / M O D U L E S
15 ###############################################################################
16  
17 package Logfile::Rotate;
18  
19 9 ▶use Config; # do we have gzip
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 ###############################################################################
28 ## G L O B A L V A R I A B L E S
29 ###############################################################################
30  
31 9 ▶use vars qw($VERSION $COUNT $GZIP_FLAG);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 9 $VERSION = do { my @r=(q$Revision: 1.5 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
34 9 $COUNT =7; # default to keep 7 copies
35 9 $GZIP_FLAG='-qf'; # force writing over old logfiles
36  
37 ###############################################################################
38 ## E X P O R T S
39 ###############################################################################
40  
41 ###############################################################################
42 ## M A I N
43 ###############################################################################
44  
45 13 ▶sub new {
Subroutine new: called CC=25 SCAR=33.7
Pod documented
46 13 my ($class, %args) = @_;
47  
48 croak("usage: new( File => filename
49 [, Count => cnt ]
50 [, Gzip => lib or \"/path/to/gzip\" or no ]
51 [, Signal => \&sub_signal ]
52 [, Pre => \&sub_pre ]
53 [, Post => \&sub_post ]
54 [, Flock => yes or no ]
55 [, Persist => yes or no ]
56 [, Dir => \"dir/to/put/old/files/into\"] )")
57 13 ▶ unless defined($args{'File'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"File"} 0 13 13
58  
59 13 my $self = {};
60 13 $self->{'Fh'} = undef;
61 13 $self->{'File'} = $args{'File'};
62 13 ▶ $self->{'Count'} = ($args{'Count'} or 7);
Condition50%1/2
$args{'Count'} || 7
l!l
130
MC/DC0%0/1
$args{'Count'} || 7
$args{'Count'}
Truth table50%1/2
A
A= $args{'Count'}
Aresult
11
00
63 13 ▶ $self->{'Signal'} = ($args{'Signal'} or sub {1;});
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{'Signal'} || sub { 1; }
l!l&&r!l&&!r
1120
MC/DC0%0/2
$args{'Signal'} || sub { 1; }
$args{'Signal'} sub { 1; }
Truth table66%2/3
A || B
A= $args{'Signal'}B= sub { 1; }
ABresult
1X1
011
000
64 13 ▶ $self->{'Pre'} = ($args{'Pre'} or sub {1;});
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{'Pre'} || sub { 1; }
l!l&&r!l&&!r
2110
MC/DC0%0/2
$args{'Pre'} || sub { 1; }
$args{'Pre'} sub { 1; }
Truth table66%2/3
A || B
A= $args{'Pre'}B= sub { 1; }
ABresult
1X1
011
000
65 13 ▶ $self->{'Post'} = ($args{'Post'} or sub {1;});
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{'Post'} || sub { 1; }
l!l&&r!l&&!r
2110
MC/DC0%0/2
$args{'Post'} || sub { 1; }
$args{'Post'} sub { 1; }
Truth table66%2/3
A || B
A= $args{'Post'}B= sub { 1; }
ABresult
1X1
011
000
66 13 ▶ $self->{'Flock'} = ($args{'Flock'} or 'yes');
Condition100%2/2
$args{'Flock'} || 'yes'
l!l
211
MC/DC100%1/1
$args{'Flock'} || 'yes'
$args{'Flock'}
Truth table100%2/2
A
A= $args{'Flock'}
Aresult
11
00
67 13 ▶ $self->{'Persist'} = ($args{'Persist'} or 'yes');
Condition100%2/2
$args{'Persist'} || 'yes'
l!l
211
MC/DC100%1/1
$args{'Persist'} || 'yes'
$args{'Persist'}
Truth table100%2/2
A
A= $args{'Persist'}
Aresult
11
00
68  
69 # deprecated methods
70 13 ▶ carp "Signal is a deprecated argument, see Pre/Post" if $args{'Signal'};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"Signal"} 1 12 13
71  
72 # mutual excl
73 croak "Can not define both Signal and Post"
74 13 ▶ if ($args{Signal} and $args{Post});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"Signal"} and $args{"Post"} 0 13 13
Condition66%2/3
$args{"Signal"} and $args{"Post"}
!ll&&!rl&&r
1210
MC/DC0%0/2
$args{"Signal"} and $args{"Post"}
$args{"Signal"} $args{"Post"}
Truth table66%2/3
A and B
A= $args{"Signal"}B= $args{"Post"}
ABresult
0X0
100
111
75  
76 13 ▶ (ref($self->{'Signal'}) eq "CODE")
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"Signal"} eq "CODE" or croak("error: Signal is not a CODE reference.") 13 0 13
77 or croak "error: Signal is not a CODE reference.";
78  
79 13 ▶ (ref($self->{'Pre'}) eq "CODE")
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"Pre"} eq "CODE" or croak("error: Pre is not a CODE reference.") 13 0 13
80 or croak "error: Pre is not a CODE reference.";
81  
82 13 ▶ (ref($self->{'Post'}) eq "CODE")
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"Post"} eq "CODE" or croak("error: Post is not a CODE reference.") 13 0 13
83 or croak "error: Post is not a CODE reference.";
84  
85 # Process compression arg
86 13 ▶ unless ($args{Gzip}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'Gzip'}) { } 12 1 13
87 1 ▶ if (_have_compress_zlib()) {
Branch: 1 branch
BranchTrueFalseTotal
if (_have_compress_zlib()) { } 1 0 1
88 1 $self->{Gzip} = 'lib';
89 } else {
90 0 $self->{Gzip} = $Config{gzip};
91 }
92 } else {
93 12 ▶ if ($args{Gzip} eq 'no') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'Gzip'} eq 'no') { } 11 1 12
94 11 $self->{Gzip} = undef;
95 } else {
96 1 $self->{Gzip} = $args{Gzip};
97 }
98 }
99  
100  
101 # Process directory arg
102  
103 13 ▶ if (defined($args{'Dir'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'Dir'}) { } 1 12 13
104 1 $self->{'Dir'} = $args{'Dir'};
105 # don't know about creating directories ??
106 1 ▶ mkdir($self->{'Dir'},0750) unless (-d $self->{'Dir'});
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{"Dir"} 1 0 1
107 } else {
108 12 $self->{'Dir'} = undef;
109 }
110  
111 # confirm existence of dir
112  
113 13 ▶ if (defined $self->{'Dir'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Dir"}) 1 12 13
114 croak "error: $self->{'Dir'} not writable"
115 1 ▶ unless (-w $self->{'Dir'});
Branch: 1 branch
BranchTrueFalseTotal
unless -w $self->{"Dir"} 0 1 1
116 croak "error: $self->{'Dir'} not executable"
117 1 ▶ unless (-x $self->{'Dir'});
Branch: 1 branch
BranchTrueFalseTotal
unless -x $self->{"Dir"} 0 1 1
118 }
119  
120 # open and lock the file
121 13 ▶ if( $self->{'Flock'} eq 'yes'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Flock'} eq 'yes') { } 12 1 13
122 12 $self->{'Fh'} = new IO::File "$self->{'File'}", O_WRONLY|O_EXCL;
123 croak "error: can not lock open: ($self->{'File'})"
124 12 ▶ unless defined($self->{'Fh'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Fh"} 0 12 12
125 12 flock($self->{'Fh'},LOCK_EX);
126 }
127 else{
128 1 $self->{'Fh'} = new IO::File "$self->{'File'}";
129 croak "error: can not open: ($self->{'File'})"
130 1 ▶ unless defined($self->{'Fh'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Fh"} 0 1 1
131 }
132  
133 13 bless $self, $class;
134 }
135  
136 39 ▶sub rotate {
Subroutine rotate: called CC=26 SCAR=34.6
Pod documented
137 39 my ($self, %args) = @_;
138  
139 39 my ($prev,$next,$i,$j);
140  
141 # check we still have a filehandle
142 croak "error: lost file handle, may have called rotate twice ?"
143 39 ▶ unless defined($self->{'Fh'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Fh"} 0 39 39
144  
145 39 my $curr = $self->{'File'};
146 39 my $currn = $curr;
147 39 ▶ my $ext = $self->{'Gzip'} ? '.gz' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Gzip'} ? : 6 33 39
148  
149 # Execute and exit if Pre method fails
150 39 ▶ eval { &{$self->{'Pre'}}($curr); } if $self->{Pre};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Pre"} 39 0 39
151 39 ▶ croak "error: your supplied Pre function failed: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 39 39
152  
153 # TODO: what is this doing ??
154 39 ▶ my $dir = defined($self->{'Dir'}) ? "$self->{'Dir'}/" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'Dir'} ? : 3 36 39
155 39 ▶ $currn =~ s+.*/([^/]*)+$self->{'Dir'}/$1+ if defined($self->{'Dir'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Dir"} 3 36 39
156  
157 39 ▶ for($i = $self->{'Count'}; $i > 1; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 78 39 117
158 78 $j = $i - 1;
159 78 $next = "${currn}." . $i . $ext;
160 78 $prev = "${currn}." . $j . $ext;
161 78 ▶ if ( -r $prev && -f $prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $prev and -f $prev) 39 39 78
Condition66%2/3
-r $prev and -f $prev
!ll&&!rl&&r
39039
MC/DC50%1/2
-r $prev and -f $prev
-r $prev -f $prev
Truth table66%2/3
A and B
A= -r $prevB= -f $prev
ABresult
0X0
100
111
162 39 ▶ move($prev,$next) ## move will attempt rename for us
Branch: 1 branch
BranchTrueFalseTotal
move($prev, $next) or croak("error: move failed: ($prev,$next)") 39 0 39
163 or croak "error: move failed: ($prev,$next)";
164 }
165 }
166  
167 ## copy current to next incremental
168 39 $next = "${currn}.1";
169 39 copy ($curr, $next);
170  
171 ## preserve permissions and status
172 39 ▶ if ( $self->{'Persist'} eq 'yes' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Persist"} eq "yes") 36 3 39
173 36 my @stat = stat $curr;
174 36 ▶ chmod( $stat[2], $next ) or carp "error: chmod failed: ($next)";
Branch: 1 branch
BranchTrueFalseTotal
chmod $stat[2], $next or carp("error: chmod failed: ($next)") 36 0 36
175 36 ▶ utime( $stat[8], $stat[9], $next ) or carp "error: failed: ($next)";
Branch: 1 branch
BranchTrueFalseTotal
utime $stat[8], $stat[9], $next or carp("error: failed: ($next)") 36 0 36
176 36 ▶ chown( $stat[4], $stat[5], $next ) or carp "error: chown failed: ($next)";
Branch: 1 branch
BranchTrueFalseTotal
chown $stat[4], $stat[5], $next or carp("error: chown failed: ($next)") 36 0 36
177 }
178  
179 # now truncate the file
180 39 ▶ if( $self->{'Flock'} eq 'yes' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Flock'} eq 'yes') { } 36 3 39
181 {
182 36 ▶ truncate $curr,0 or croak "error: could not truncate $curr: $!"; }
Branch: 1 branch
BranchTrueFalseTotal
truncate $curr, 0 or croak("error: could not truncate ${curr}: $!") 36 0 36
183 else{
184 3 local(*IN);
185 3 ▶ open(IN, "+>$self->{'File'}")
Branch: 1 branch
BranchTrueFalseTotal
open IN, qq[+>$self->{"File"}] or croak("error: could not truncate ${curr}: $!") 3 0 3
186 or croak "error: could not truncate $curr: $!";
187 }
188  
189 39 ▶ if ($self->{'Gzip'} and $self->{'Gzip'} eq 'lib')
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'Gzip'} and $self->{'Gzip'} eq 'lib') { } 6 33 39
elsif ($self->{'Gzip'}) { } 0 33 33
Condition66%2/3
$self->{"Gzip"} and $self->{"Gzip"} eq "lib"
!ll&&!rl&&r
3306
MC/DC50%1/2
$self->{"Gzip"} and $self->{"Gzip"} eq "lib"
$self->{"Gzip"} $self->{"Gzip"} eq "lib"
Truth table66%2/3
A and B
A= $self->{"Gzip"}B= $self->{"Gzip"} eq "lib"
ABresult
0X0
100
111
190 {
191 6 _gzip($next, $next.$ext);
192 }
193 elsif ($self->{'Gzip'})
194 {
195 # WARNING: may not be safe system call
196 ( 0 == (system $self->{'Gzip'}, $GZIP_FLAG, $next) )
197 0 ▶ or croak "error: ", $self->{'Gzip'}, " failed";
Branch: 1 branch
BranchTrueFalseTotal
0 == system($self->{"Gzip"}, $Logfile::Rotate::GZIP_FLAG, $next) or croak("error: ", $self->{"Gzip"}, " failed") 0 0 0
198 }
199  
200 # TODO: deprecated: remove next release
201 39 ▶ eval { &{$self->{'Signal'}}($curr, $next); } if ($self->{Signal});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Signal"} 39 0 39
202 39 ▶ croak "error: your supplied Signal function failed: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 39 39
203  
204 # Execute and exit on post method
205 39 ▶ eval { &{$self->{'Post'}}($curr, $next); } if $self->{Post};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Post"} 39 0 39
206 39 ▶ croak "error: your supplied Post function failed: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 39 39
207  
208 # if we made it here we have succeeded
209 39 return 1;
210 }
211  
212 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
213 4 my ($self, %args) = @_;
214 4 ▶ return unless $self->{'Fh'}; # already gone
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Fh"} 0 4 4
215 4 flock($self->{'Fh'},LOCK_UN);
216 4 undef $self->{'Fh'}; # auto-close
217 }
218  
219 7 ▶sub _have_compress_zlib {
Subroutine _have_compress_zlib: called CC=2 SCAR=7.9
220 # try and load the compression library
221 7 eval { require Compress::Zlib; };
222 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
223 0 carp "warning: could not load Compress::Zlib, skipping compression" ;
224 0 return undef;
225 }
226 7 return 1;
227 }
228  
229 6 ▶sub _gzip {
Subroutine _gzip: called CC=7 SCAR=20.1
230 6 my $in = shift;
231 6 my $out = shift;
232  
233 # ASSERT
234 6 ▶ croak "error: _gzip called without mandatory argument" unless $in;
Branch: 1 branch
BranchTrueFalseTotal
unless $in 0 6 6
235  
236 6 ▶ return unless _have_compress_zlib();
Branch: 1 branch
BranchTrueFalseTotal
unless _have_compress_zlib() 0 6 6
237  
238 6 my($buffer,$fhw);
239 6 ▶ $fhw = new IO::File $in
Branch: 1 branch
BranchTrueFalseTotal
$fhw = "IO::File"->new($in) or croak("error: could not open ${in}: $!") 6 0 6
240 or croak "error: could not open $in: $!";
241 6 ▶ my $gz = Compress::Zlib::gzopen($out, "wb")
Branch: 1 branch
BranchTrueFalseTotal
my $gz = Compress::Zlib::gzopen($out, "wb") or croak("error: could not gzopen ${out}: $!") 6 0 6
242 or croak "error: could not gzopen $out: $!";
243 6 ▶ $gz->gzwrite($buffer)
Branch: 1 branch
BranchTrueFalseTotal
read($fhw, $buffer, 4096) > 0 and do { $gz->gzwrite($buffer) } 6 6 12
244 while read($fhw,$buffer,4096) > 0 ;
245 6 $gz->gzclose() ;
246 6 $fhw->close;
247  
248 6 ▶ unlink $in or croak "error: could not delete $in: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $in or croak("error: could not delete ${in}: $!") 6 0 6
249  
250 6 return 1;
251 }
252  
253 9 1;
254  
255  
256 __END__
↑ Summary