| 1 | # | ||||||||||||||||||||||||||||||||||
| 2 | # $Id: DB.pm,v 1.9 2008/07/09 20:40:20 mprewitt Exp $ | ||||||||||||||||||||||||||||||||||
| 3 | # $Source: /usr/local/src/perllib/Tail/0.1/lib/File/SmartTail/RCS/DB.pm,v $ | ||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||
| 5 | # DMJA, Inc <smarttail@dmja.com> | ||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2003-2008 DMJA, Inc, File::SmartTail comes with | ||||||||||||||||||||||||||||||||||
| 8 | # ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to | ||||||||||||||||||||||||||||||||||
| 9 | # redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||
| 10 | # See the "The Artistic License" L<LICENSE> for more details. | ||||||||||||||||||||||||||||||||||
| 11 | package File::SmartTail::DB; | ||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use Fcntl qw(:DEFAULT :flock); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use constant MAX_RETRIES => 6; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | if ( ! -d "/var/tmp/filestatus" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | mkdir( "/var/tmp/filestatus", 01777 ) || | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 22 | die "Unable to make status directory /var/tmp/filestatus [$!].\n"; | ||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 1 | chmod( 01777, "/var/tmp/filestatus" ); | |||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||||||||
| 28 | 5 | my %cache; | |||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=34.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 30 | 1 | my $type = shift; | |||||||||||||||||||||||||||||||||
| 31 | 1 | my %h = @_; | |||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | my $statuskey = $h{statuskey} or | ||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | LOG()->logdie( "required param: statuskey" ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | my $tietype = $h{tietype} || 'DB_File'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$h{'tietype'} || 'DB_File'
MC/DC0%0/1
$h{'tietype'} || 'DB_File'
$h{'tietype'}
Truth table50%1/2
A
A= $h{'tietype'}
| |||||||||||||||||||||||||||||||||||
| 36 | 1 | my $cachekey = join "\0", $statuskey, $tietype; | |||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | $cache{$cachekey} and return $cache{$cachekey}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 1 | my $filename = "/var/tmp/filestatus/$statuskey"; | |||||||||||||||||||||||||||||||||
| 40 | 1 | my $saverr = ""; | |||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | my $fullname = $filename . (($tietype eq 'NDBM_File') ? ".pag" : ""); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | if (-f $fullname && open(FH, ">> $fullname ")) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $fullname and open FH, ">> $fullname "
MC/DC0%0/2
-f $fullname and open FH, ">> $fullname "
-f $fullname
open FH, ">> $fullname "
Truth table33%1/3
A and B
A= -f $fullnameB= open FH, ">> $fullname "
| |||||||||||||||||||||||||||||||||||
| 43 | 0 | my $count = 0; | |||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | while (++$count < MAX_RETRIES) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | last if flock(FH, LOCK_EX | LOCK_NB); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 46 | 0 | $saverr = $!; | |||||||||||||||||||||||||||||||||
| 47 | 0 | sleep (2 ** $count); | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | LOG()->logdie( "Could not lock $filename in @{[MAX_RETRIES()]} attemps [$saverr].\n" ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 50 | if ($count >= MAX_RETRIES); | ||||||||||||||||||||||||||||||||||
| 51 | 0 | flock(FH, LOCK_UN | LOCK_NB); | |||||||||||||||||||||||||||||||||
| 52 | 0 | close FH; | |||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||
| 55 | 1 | my %STATUS; | |||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | eval "use $tietype"; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | die "Unable to use $tietype [$@]" if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | my $STATFILE = tie( %STATUS, $tietype, $filename, | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
tie(%STATUS, $tietype, $filename, 66, 384) || LOG()->logdie("Tie of status for $statuskey failed [$!].\n")
MC/DC0%0/2
tie(%STATUS, $tietype, $filename, 66, 384) || LOG()->logdie("Tie of status for $statuskey failed [$!].\n")
tie(%STATUS, $tietype, $filename, 66, 384)
LOG()->logdie("Tie of status for $statuskey failed [$!].\n")
Truth table33%1/3
A || B
A= tie(%STATUS, $tietype, $filename, 66, 384)B= LOG()->logdie("Tie of status for $statuskey failed [$!].\n")
| |||||||||||||||||||||||||||||||||||
| 59 | O_RDWR | O_CREAT, 0600 ) || | ||||||||||||||||||||||||||||||||||
| 60 | LOG()->logdie( "Tie of status for $statuskey failed [$!].\n" ); | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | my $self = bless { | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $type || $type
MC/DC0%0/2
ref $type || $type
ref $type
$type
Truth table33%1/3
A || B
A= ref $typeB= $type
| |||||||||||||||||||||||||||||||||||
| 63 | STATUS => \%STATUS, | ||||||||||||||||||||||||||||||||||
| 64 | STATFILE => $STATFILE, | ||||||||||||||||||||||||||||||||||
| 65 | STATUSKEY => $statuskey, | ||||||||||||||||||||||||||||||||||
| 66 | TIETYPE => $tietype, | ||||||||||||||||||||||||||||||||||
| 67 | }, ref $type || $type; | ||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||||||||
| 70 | # keep our own reference, so the logging object is | ||||||||||||||||||||||||||||||||||
| 71 | # destroyed AFTER $self. | ||||||||||||||||||||||||||||||||||
| 72 | # | ||||||||||||||||||||||||||||||||||
| 73 | 1 | $self->{LOG} = LOG(); | |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | 1 | $self->sync; | |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||
| 77 | 1 | return $cache{$cachekey} = $self; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 5 | ▶ | sub sync { | ||||||||||||||||||||||||||||||||
|
Subroutine
sync: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 82 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | if ($self->{STATFILE} && $self->can('sync')) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"STATFILE"} and $self->can("sync")
MC/DC0%0/2
$self->{"STATFILE"} and $self->can("sync")
$self->{"STATFILE"}
$self->can("sync")
Truth table33%1/3
A and B
A= $self->{"STATFILE"}B= $self->can("sync")
| |||||||||||||||||||||||||||||||||||
| 85 | 1 | eval { | |||||||||||||||||||||||||||||||||
| 86 | $self->{STATFILE}->sync | ||||||||||||||||||||||||||||||||||
| 87 | 1 | }; | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $self = shift or return; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 0 | $self->sync; | |||||||||||||||||||||||||||||||||
| 95 | # LOG()->debug( sub { | ||||||||||||||||||||||||||||||||||
| 96 | # my $tt = $self->{TIETYPE} || ''; | ||||||||||||||||||||||||||||||||||
| 97 | # my $sf = $self->{STATFILE} || ''; | ||||||||||||||||||||||||||||||||||
| 98 | # my $ds = $self->DumpStatus || ''; | ||||||||||||||||||||||||||||||||||
| 99 | # "sub DESTROY: TIETYPE: $tt; STATFILE: $sf; $ds"; | ||||||||||||||||||||||||||||||||||
| 100 | # } ); | ||||||||||||||||||||||||||||||||||
| 101 | # delete $self->{STATFILE}; # undef necessary? | ||||||||||||||||||||||||||||||||||
| 102 | # untie %{ $self->{STATUS} }; # will this do the right thing? | ||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub DumpStatus { | ||||||||||||||||||||||||||||||||
|
Subroutine
DumpStatus: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my $self = shift or return; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 0 | my %h = @_; | |||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | 0 | my $indent = $h{indent}; | |||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | defined $indent or $indent = 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 0 | my $tab = "\t" x $indent; | |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my $sk = $self->{STATUSKEY} || ''; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'STATUSKEY'} || ''
MC/DC0%0/1
$self->{'STATUSKEY'} || ''
$self->{'STATUSKEY'}
Truth table0%0/2
A
A= $self->{'STATUSKEY'}
| |||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my @k = $self->{STATUS} ? sort keys %{ $self->{STATUS} } : (); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my @m = @k ? map "$_ =>\t$self->{STATUS}->{$_}", @k : (); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 117 | 0 | return join "\n$tab", "STATUSKEY file: $sk:", @m; | |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||||||||
| 121 | 5 | my $v; | |||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | sub LOG { | ||||||||||||||||||||||||||||||||
|
Subroutine
LOG: called CC=3 SCAR=15.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | $v ||= require File::SmartTail::Logger && File::SmartTail::Logger::LOG(); | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
$v ||= require File::SmartTail::Logger && File::SmartTail::Logger::LOG()
require File::SmartTail::Logger && File::SmartTail::Logger::LOG()
MC/DC0%0/3
$v ||= require File::SmartTail::Logger && File::SmartTail::Logger::LOG()
$v
require File::SmartTail::Logger
File::SmartTail::Logger::LOG()
Truth table0%0/4
A ||= B && C
A= $vB= require File::SmartTail::LoggerC= File::SmartTail::Logger::LOG()
| |||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | 5 | 1; | |||||||||||||||||||||||||||||||||