| 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
| |||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$args{'Count'} || 7
$args{'Count'}
Truth table50%1/2
A
A= $args{'Count'}
| |||||||||||||||||||||||||||||||||
| 63 | 13 | ▶ | $self->{'Signal'} = ($args{'Signal'} or sub {1;}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{'Signal'} || sub {
1;
}
MC/DC0%0/2
$args{'Signal'} || sub {
1;
}
$args{'Signal'}
sub {
1;
}
Truth table66%2/3
A || B
A= $args{'Signal'}B= sub {
1;
}
| |||||||||||||||||||||||||||||||||
| 64 | 13 | ▶ | $self->{'Pre'} = ($args{'Pre'} or sub {1;}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{'Pre'} || sub {
1;
}
MC/DC0%0/2
$args{'Pre'} || sub {
1;
}
$args{'Pre'}
sub {
1;
}
Truth table66%2/3
A || B
A= $args{'Pre'}B= sub {
1;
}
| |||||||||||||||||||||||||||||||||
| 65 | 13 | ▶ | $self->{'Post'} = ($args{'Post'} or sub {1;}); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$args{'Post'} || sub {
1;
}
MC/DC0%0/2
$args{'Post'} || sub {
1;
}
$args{'Post'}
sub {
1;
}
Truth table66%2/3
A || B
A= $args{'Post'}B= sub {
1;
}
| |||||||||||||||||||||||||||||||||
| 66 | 13 | ▶ | $self->{'Flock'} = ($args{'Flock'} or 'yes'); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'Flock'} || 'yes'
MC/DC100%1/1
$args{'Flock'} || 'yes'
$args{'Flock'}
Truth table100%2/2
A
A= $args{'Flock'}
| |||||||||||||||||||||||||||||||||
| 67 | 13 | ▶ | $self->{'Persist'} = ($args{'Persist'} or 'yes'); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'Persist'} || 'yes'
MC/DC100%1/1
$args{'Persist'} || 'yes'
$args{'Persist'}
Truth table100%2/2
A
A= $args{'Persist'}
| |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | # deprecated methods | ||||||||||||||||||||||||||||||||
| 70 | 13 | ▶ | carp "Signal is a deprecated argument, see Pre/Post" if $args{'Signal'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | # mutual excl | ||||||||||||||||||||||||||||||||
| 73 | croak "Can not define both Signal and Post" | ||||||||||||||||||||||||||||||||
| 74 | 13 | ▶ | if ($args{Signal} and $args{Post}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"Signal"} and $args{"Post"}
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"}
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 13 | ▶ | (ref($self->{'Signal'}) eq "CODE") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | or croak "error: Signal is not a CODE reference."; | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 13 | ▶ | (ref($self->{'Pre'}) eq "CODE") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | or croak "error: Pre is not a CODE reference."; | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 13 | ▶ | (ref($self->{'Post'}) eq "CODE") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | or croak "error: Post is not a CODE reference."; | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | # Process compression arg | ||||||||||||||||||||||||||||||||
| 86 | 13 | ▶ | unless ($args{Gzip}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | 1 | ▶ | if (_have_compress_zlib()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||||||
| 108 | 12 | $self->{'Dir'} = undef; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | # confirm existence of dir | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 13 | ▶ | if (defined $self->{'Dir'} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | croak "error: $self->{'Dir'} not writable" | ||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | unless (-w $self->{'Dir'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | croak "error: $self->{'Dir'} not executable" | ||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | unless (-x $self->{'Dir'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | # open and lock the file | ||||||||||||||||||||||||||||||||
| 121 | 13 | ▶ | if( $self->{'Flock'} eq 'yes'){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 39 | my $curr = $self->{'File'}; | |||||||||||||||||||||||||||||||
| 146 | 39 | my $currn = $curr; | |||||||||||||||||||||||||||||||
| 147 | 39 | ▶ | my $ext = $self->{'Gzip'} ? '.gz' : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | # Execute and exit if Pre method fails | ||||||||||||||||||||||||||||||||
| 150 | 39 | ▶ | eval { &{$self->{'Pre'}}($curr); } if $self->{Pre}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | 39 | ▶ | croak "error: your supplied Pre function failed: $@" if ($@); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | # TODO: what is this doing ?? | ||||||||||||||||||||||||||||||||
| 154 | 39 | ▶ | my $dir = defined($self->{'Dir'}) ? "$self->{'Dir'}/" : ""; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 155 | 39 | ▶ | $currn =~ s+.*/([^/]*)+$self->{'Dir'}/$1+ if defined($self->{'Dir'}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 39 | ▶ | for($i = $self->{'Count'}; $i > 1; $i--) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
-r $prev and -f $prev
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
| |||||||||||||||||||||||||||||||||
| 162 | 39 | ▶ | move($prev,$next) ## move will attempt rename for us | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 173 | 36 | my @stat = stat $curr; | |||||||||||||||||||||||||||||||
| 174 | 36 | ▶ | chmod( $stat[2], $next ) or carp "error: chmod failed: ($next)"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | 36 | ▶ | utime( $stat[8], $stat[9], $next ) or carp "error: failed: ($next)"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 176 | 36 | ▶ | chown( $stat[4], $stat[5], $next ) or carp "error: chown failed: ($next)"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | # now truncate the file | ||||||||||||||||||||||||||||||||
| 180 | 39 | ▶ | if( $self->{'Flock'} eq 'yes' ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||||||
| 182 | 36 | ▶ | truncate $curr,0 or croak "error: could not truncate $curr: $!"; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | else{ | ||||||||||||||||||||||||||||||||
| 184 | 3 | local(*IN); | |||||||||||||||||||||||||||||||
| 185 | 3 | ▶ | open(IN, "+>$self->{'File'}") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 186 | or croak "error: could not truncate $curr: $!"; | ||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 39 | ▶ | if ($self->{'Gzip'} and $self->{'Gzip'} eq 'lib') | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$self->{"Gzip"} and $self->{"Gzip"} eq "lib"
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"
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | # TODO: deprecated: remove next release | ||||||||||||||||||||||||||||||||
| 201 | 39 | ▶ | eval { &{$self->{'Signal'}}($curr, $next); } if ($self->{Signal}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | 39 | ▶ | croak "error: your supplied Signal function failed: $@" if ($@); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | # Execute and exit on post method | ||||||||||||||||||||||||||||||||
| 205 | 39 | ▶ | eval { &{$self->{'Post'}}($curr, $next); } if $self->{Post}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 206 | 39 | ▶ | croak "error: your supplied Post function failed: $@" if ($@); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | 6 | ▶ | return unless _have_compress_zlib(); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 6 | my($buffer,$fhw); | |||||||||||||||||||||||||||||||
| 239 | 6 | ▶ | $fhw = new IO::File $in | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 240 | or croak "error: could not open $in: $!"; | ||||||||||||||||||||||||||||||||
| 241 | 6 | ▶ | my $gz = Compress::Zlib::gzopen($out, "wb") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 242 | or croak "error: could not gzopen $out: $!"; | ||||||||||||||||||||||||||||||||
| 243 | 6 | ▶ | $gz->gzwrite($buffer) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 6 | return 1; | |||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | 9 | 1; | |||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | __END__ | ||||||||||||||||||||||||||||||||