| 1 | package Text::Template::Simple::IO; | ||||||||||||||||||||||||||||
| 2 | 32 | $Text::Template::Simple::IO::VERSION = '0.91'; | |||||||||||||||||||||||||||
| 3 | 32 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 32 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 32 | ▶ | use constant MY_IO_LAYER => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 32 | ▶ | use constant MY_INCLUDE_PATHS => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 32 | ▶ | use constant MY_TAINT_MODE => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 32 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 32 | ▶ | use Text::Template::Simple::Constants qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 32 | ▶ | use Text::Template::Simple::Util qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | binary_mode | ||||||||||||||||||||||||||||
| 13 | fatal | ||||||||||||||||||||||||||||
| 14 | DEBUG | ||||||||||||||||||||||||||||
| 15 | LOG | ||||||||||||||||||||||||||||
| 16 | 32 | ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 45 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 45 | my $class = shift; | |||||||||||||||||||||||||||
| 20 | 45 | my $layer = shift; | |||||||||||||||||||||||||||
| 21 | 45 | my $paths = shift; | |||||||||||||||||||||||||||
| 22 | 45 | my $tmode = shift; | |||||||||||||||||||||||||||
| 23 | 45 | my $self = [ undef, undef, undef ]; | |||||||||||||||||||||||||||
| 24 | 45 | bless $self, $class; | |||||||||||||||||||||||||||
| 25 | 45 | ▶ | $self->[MY_IO_LAYER] = $layer if defined $layer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 45 | ▶ | $self->[MY_INCLUDE_PATHS] = [ @{ $paths } ] if $paths; # copy | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 45 | $self->[MY_TAINT_MODE] = $tmode; | |||||||||||||||||||||||||||
| 28 | 45 | return $self; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=7 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 2 | ▶ | my $type = shift || fatal('tts.io.validate.type'); | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || fatal('tts.io.validate.type')
MC/DC0%0/2
shift() || fatal('tts.io.validate.type')
shift()
fatal('tts.io.validate.type')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.io.validate.type')
| |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | my $path = shift || fatal('tts.io.validate.path'); | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || fatal('tts.io.validate.path')
MC/DC0%0/2
shift() || fatal('tts.io.validate.path')
shift()
fatal('tts.io.validate.path')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.io.validate.path')
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | if ( $type eq 'dir' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 2 | require File::Spec; | |||||||||||||||||||||||||||
| 38 | 2 | $path = File::Spec->canonpath( $path ); | |||||||||||||||||||||||||||
| 39 | 2 | my $wdir; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | if ( IS_WINDOWS ) { | |||||||||||||||||||||||||||
| 42 | $wdir = Win32::GetFullPathName( $path ); | ||||||||||||||||||||||||||||
| 43 | if( Win32::GetLastError() ) { | ||||||||||||||||||||||||||||
| 44 | LOG( FAIL => "Win32::GetFullPathName( $path ): $^E" ) if DEBUG; | ||||||||||||||||||||||||||||
| 45 | $wdir = EMPTY_STRING; # die "Win32::GetFullPathName: $^E"; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | my $ok = -e $wdir && -d _; | ||||||||||||||||||||||||||||
| 49 | $wdir = EMPTY_STRING if not $ok; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | $path = $wdir if $wdir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | my $ok = -e $path && -d _; | ||||||||||||||||||||||||||
|
Condition33%1/3
-e $path && -d _
MC/DC0%0/2
-e $path && -d _
-e $path
-d _
Truth table33%1/3
A && B
A= -e $pathB= -d _
| |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | return if not $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 2 | return $path; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | return fatal('tts.io.validate.file'); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 417 | ▶ | sub layer { | ||||||||||||||||||||||||||
|
Subroutine
layer: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 417 | return if ! UNICODE_PERL; | |||||||||||||||||||||||||||
| 64 | 417 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 417 | ▶ | my $fh = shift || fatal('tts.io.layer.fh'); | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || fatal('tts.io.layer.fh')
MC/DC0%0/2
shift() || fatal('tts.io.layer.fh')
shift()
fatal('tts.io.layer.fh')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.io.layer.fh')
| |||||||||||||||||||||||||||||
| 66 | 417 | my $layer = $self->[MY_IO_LAYER]; | |||||||||||||||||||||||||||
| 67 | 417 | ▶ | binary_mode( $fh, $layer ) if $layer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 417 | return; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 416 | ▶ | sub slurp { | ||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=11 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 416 | require IO::File; | |||||||||||||||||||||||||||
| 73 | 416 | require Fcntl; | |||||||||||||||||||||||||||
| 74 | 416 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 416 | my $file = shift; | |||||||||||||||||||||||||||
| 76 | 416 | my($fh, $seek); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 416 | ▶ | LOG(IO_SLURP => $file) if DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 416 | ▶ | if ( ref $file && fileno $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $file and fileno $file
MC/DC0%0/2
ref $file and fileno $file
ref $file
fileno $file
Truth table33%1/3
A and B
A= ref $fileB= fileno $file
| |||||||||||||||||||||||||||||
| 81 | 0 | $fh = $file; | |||||||||||||||||||||||||||
| 82 | 0 | $seek = 1; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | else { | ||||||||||||||||||||||||||||
| 85 | 416 | $fh = IO::File->new; | |||||||||||||||||||||||||||
| 86 | 416 | ▶ | $fh->open($file, 'r') or fatal('tts.io.slurp.open', $file, $!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 414 | flock $fh, Fcntl::LOCK_SH(); | |||||||||||||||||||||||||||
| 90 | 414 | ▶ | seek $fh, 0, Fcntl::SEEK_SET() if $seek; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 414 | ▶ | $self->layer( $fh ) if ! $seek; # apply the layer only if we opened this | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 414 | ▶ | if ( $self->_handle_looks_safe( $fh ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 414 | require IO::Handle; | |||||||||||||||||||||||||||
| 95 | 414 | my $rv = IO::Handle::untaint( $fh ); | |||||||||||||||||||||||||||
| 96 | 414 | ▶ | fatal('tts.io.slurp.taint') if $rv != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 414 | my $tmp = do { local $/; my $rv = <$fh>; $rv }; | |||||||||||||||||||||||||||
| 100 | 414 | flock $fh, Fcntl::LOCK_UN(); | |||||||||||||||||||||||||||
| 101 | 414 | ▶ | if ( ! $seek ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | # close only if we opened this | ||||||||||||||||||||||||||||
| 103 | 414 | ▶ | close $fh or die "Unable to close filehandle: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 414 | return $tmp; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 414 | ▶ | sub _handle_looks_safe { | ||||||||||||||||||||||||||
|
Subroutine
_handle_looks_safe: called CC=14 SCAR=31.7
| |||||||||||||||||||||||||||||
| 109 | # Cargo Culting: original taint checking code was taken from "The Camel" | ||||||||||||||||||||||||||||
| 110 | 414 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | 414 | my $fh = shift; | |||||||||||||||||||||||||||
| 112 | 414 | ▶ | fatal('tts.io.hls.invalid') if ! $fh || ! fileno $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fh and fileno $fh
MC/DC0%0/2
$fh and fileno $fh
$fh
fileno $fh
Truth table33%1/3
A and B
A= $fhB= fileno $fh
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 414 | require File::stat; | |||||||||||||||||||||||||||
| 115 | 414 | my $i = File::stat::stat( $fh ); | |||||||||||||||||||||||||||
| 116 | 414 | ▶ | return if ! $i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 414 | my $tmode = $self->[MY_TAINT_MODE]; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # ignore this check if the user is root | ||||||||||||||||||||||||||||
| 121 | # can happen with cpan clients | ||||||||||||||||||||||||||||
| 122 | 414 | ▶ | if ( $< != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | # owner neither superuser nor "me", whose | ||||||||||||||||||||||||||||
| 124 | # real uid is in the $< variable | ||||||||||||||||||||||||||||
| 125 | 414 | ▶ | return if $i->uid != 0 && $i->uid != $<; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$i->uid != 0 and $i->uid != $<
MC/DC0%0/2
$i->uid != 0 and $i->uid != $<
$i->uid != 0
$i->uid != $<
Truth table33%1/3
A and B
A= $i->uid != 0B= $i->uid != $<
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # Check whether group or other can write file. | ||||||||||||||||||||||||||||
| 129 | # Read check is disabled by default | ||||||||||||||||||||||||||||
| 130 | # Mode is always 0666 on Windows, so all tests below are disabled on Windows | ||||||||||||||||||||||||||||
| 131 | # unless you force them to run | ||||||||||||||||||||||||||||
| 132 | 414 | ▶ | LOG( FILE_MODE => sprintf '%04o', $i->mode & FTYPE_MASK) if DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 414 | my $bypass = IS_WINDOWS && ! ( $tmode & TAINT_CHECK_WINDOWS ) ? 1 : 0; | |||||||||||||||||||||||||||
| 135 | 414 | ▶ | my $go_write = $bypass ? 0 : $i->mode & FMODE_GO_WRITABLE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 414 | ▶ | my $go_read = ! $bypass && ( $tmode & TAINT_CHECK_FH_READ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $bypass and $tmode & 8
MC/DC0%0/2
not $bypass and $tmode & 8
not $bypass
$tmode & 8
Truth table33%1/3
not(A) and B
A= not $bypassB= $tmode & 8
| |||||||||||||||||||||||||||||
| 137 | ? $i->mode & FMODE_GO_READABLE | ||||||||||||||||||||||||||||
| 138 | : 0; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 414 | ▶ | LOG( TAINT => "tmode:$tmode; bypass:$bypass; " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | ."go_write:$go_write; go_read:$go_read") if DEBUG; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 414 | ▶ | return if $go_write || $go_read; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$go_write or $go_read
MC/DC0%0/2
$go_write or $go_read
$go_write
$go_read
Truth table33%1/3
A or B
A= $go_writeB= $go_read
| |||||||||||||||||||||||||||||
| 144 | 414 | return 1; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 671 | ▶ | sub is_file { | ||||||||||||||||||||||||||
|
Subroutine
is_file: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | # safer than a simple "-e" | ||||||||||||||||||||||||||||
| 149 | 671 | my $self = shift; | |||||||||||||||||||||||||||
| 150 | 671 | ▶ | my $file = shift || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 151 | 671 | ▶ | return $self->_looks_like_file( $file ) && ! -d $file; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->_looks_like_file($file) && !(-d $file)
MC/DC50%1/2
$self->_looks_like_file($file) && !(-d $file)
$self->_looks_like_file($file)
!(-d $file)
Truth table66%2/3
A && not(B)
A= $self->_looks_like_file($file)B= !(-d $file)
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 136 | ▶ | sub is_dir { | ||||||||||||||||||||||||||
|
Subroutine
is_dir: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | # safer than a simple "-d" | ||||||||||||||||||||||||||||
| 156 | 136 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | 136 | ▶ | my $file = shift || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 158 | 136 | ▶ | return $self->_looks_like_file( $file ) && -d $file; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->_looks_like_file($file) && -d $file
MC/DC0%0/2
$self->_looks_like_file($file) && -d $file
$self->_looks_like_file($file)
-d $file
Truth table66%2/3
A && B
A= $self->_looks_like_file($file)B= -d $file
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 665 | ▶ | sub file_exists { | ||||||||||||||||||||||||||
|
Subroutine
file_exists: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 665 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | 665 | my $file = shift; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 665 | ▶ | return $file if $self->is_file( $file ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 121 | foreach my $path ( @{ $self->[MY_INCLUDE_PATHS] } ) { | |||||||||||||||||||||||||||
| 168 | 6 | my $test = File::Spec->catfile( $path, $file ); | |||||||||||||||||||||||||||
| 169 | 6 | ▶ | return $test if $self->is_file( $test ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 117 | return; # fail! | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 807 | ▶ | sub _looks_like_file { | ||||||||||||||||||||||||||
|
Subroutine
_looks_like_file: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||
| 176 | 807 | my $self = shift; | |||||||||||||||||||||||||||
| 177 | 807 | ▶ | my $file = shift || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 178 | 807 | ▶ | return ref $file ? 0 | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 179 | : $file =~ RE_NONFILE ? 0 | ||||||||||||||||||||||||||||
| 180 | : length $file >= MAX_PATH_LENGTH ? 0 | ||||||||||||||||||||||||||||
| 181 | : -e $file ? 1 | ||||||||||||||||||||||||||||
| 182 | : 0 | ||||||||||||||||||||||||||||
| 183 | ; | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 187 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | LOG( DESTROY => ref $self ) if DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | return; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 32 | 1; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||