| 1 | # ABSTRACT: Rex remote file handle via Net::LibSSH exec channels | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | package Rex::Interface::File::LibSSH; | ||||||||||||||||||||||||||||||||
| 4 | 2 | our $VERSION = '0.005'; | |||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Rex; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Rex::Config; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Rex::Interface::File::Base; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use base qw(Rex::Interface::File::Base); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 15 | 0 | my ( $that, %args ) = @_; | |||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||||||
| 17 | 0 | return bless {%args}, $proto; | |||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub open { | ||||||||||||||||||||||||||||||
|
Subroutine
open: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 21 | 0 | my ( $self, $mode, $path ) = @_; | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 0 | Rex::Logger::debug("LibSSH File::open $mode $path"); | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 0 | $self->{path} = $path; | |||||||||||||||||||||||||||||||
| 26 | 0 | $self->{mode} = $mode; | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my $ssh = Rex::is_ssh() | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | or die "LibSSH File: no active SSH connection"; | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 0 | my $qpath = _q($path); | |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ( $mode eq '>' || $mode eq '>>' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$mode eq ">" or $mode eq ">>"
MC/DC0%0/2
$mode eq ">" or $mode eq ">>"
$mode eq ">"
$mode eq ">>"
Truth table0%0/3
A or B
A= $mode eq ">"B= $mode eq ">>"
| |||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | my $cmd = $mode eq '>' ? "cat > $qpath" : "cat >> $qpath"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my $ch = $ssh->channel or die "LibSSH File: failed to open channel"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | unless ( $ch->exec($cmd) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 0 | $ch->close; | |||||||||||||||||||||||||||||||
| 38 | 0 | Rex::Logger::debug("LibSSH File::open: exec failed for $path"); | |||||||||||||||||||||||||||||||
| 39 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | 0 | $self->{ch} = $ch; | |||||||||||||||||||||||||||||||
| 42 | 0 | $self->{buf} = ''; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | elsif ( $mode eq '<' ) { | ||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $ch = $ssh->channel or die "LibSSH File: failed to open channel"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | unless ( $ch->exec("cat $qpath") ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | 0 | $ch->close; | |||||||||||||||||||||||||||||||
| 48 | 0 | Rex::Logger::debug("LibSSH File::open: exec failed for $path"); | |||||||||||||||||||||||||||||||
| 49 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | 0 | my $buf = $ch->read; | |||||||||||||||||||||||||||||||
| 52 | 0 | my $exit = $ch->exit_status; | |||||||||||||||||||||||||||||||
| 53 | 0 | $ch->close; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | # A missing or unreadable file must not read as an empty one: stock | ||||||||||||||||||||||||||||||||
| 56 | # Rex::Interface::File::SSH::open returns false there and callers test it. | ||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( $exit != 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 0 | Rex::Logger::debug("LibSSH File::open: cat $path exited $exit"); | |||||||||||||||||||||||||||||||
| 59 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | 0 | $self->{buf} = $buf; | |||||||||||||||||||||||||||||||
| 62 | 0 | $self->{pos} = 0; | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||
| 65 | 0 | die "LibSSH File: unsupported mode '$mode'"; | |||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||||||
|
Subroutine
read: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 72 | 0 | my ( $self, $len ) = @_; | |||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return undef unless defined $self->{buf}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 0 | my $chunk = substr( $self->{buf}, $self->{pos}, $len ); | |||||||||||||||||||||||||||||||
| 76 | 0 | $self->{pos} += length($chunk); | |||||||||||||||||||||||||||||||
| 77 | 0 | return $chunk; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||||||
|
Subroutine
write: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 81 | 0 | my ( $self, $buf ) = @_; | |||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | utf8::encode($buf) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
"Rex::Config"->get_write_utf8_files and utf8::is_utf8($buf)
MC/DC0%0/2
"Rex::Config"->get_write_utf8_files and utf8::is_utf8($buf)
"Rex::Config"->get_write_utf8_files
utf8::is_utf8($buf)
Truth table0%0/3
A and B
A= "Rex::Config"->get_write_utf8_filesB= utf8::is_utf8($buf)
| |||||||||||||||||||||||||||||||||
| 84 | if Rex::Config->get_write_utf8_files && utf8::is_utf8($buf); | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | my $ch = $self->{ch} | ||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | or die "LibSSH File: write on a handle that is not open for writing\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 0 | my $n = $ch->write($buf); | |||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | die "LibSSH File: write to $self->{path} failed\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $n or $n < 0
MC/DC0%0/2
not defined $n or $n < 0
not defined $n
$n < 0
Truth table0%0/3
not(A) or B
A= not defined $nB= $n < 0
| |||||||||||||||||||||||||||||||||
| 91 | if !defined $n || $n < 0; | ||||||||||||||||||||||||||||||||
| 92 | 0 | return $n; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub seek { | ||||||||||||||||||||||||||||||
|
Subroutine
seek: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 96 | 0 | my ( $self, $pos ) = @_; | |||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $self->{pos} = $pos if defined $self->{pos}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub close { | ||||||||||||||||||||||||||||||
|
Subroutine
close: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 101 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my $ch = delete $self->{ch} or return 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 0 | $ch->send_eof; | |||||||||||||||||||||||||||||||
| 105 | 0 | $ch->read; # drain | |||||||||||||||||||||||||||||||
| 106 | 0 | my $exit = $ch->exit_status; | |||||||||||||||||||||||||||||||
| 107 | 0 | $ch->close; | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | # cat exits non-zero on a full disk, a read-only filesystem or a missing | ||||||||||||||||||||||||||||||||
| 110 | # parent directory; Rex then renames the truncated temp file into place. | ||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | die "LibSSH File: writing $self->{path} failed (exit $exit)\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | if $exit != 0; | ||||||||||||||||||||||||||||||||
| 113 | 0 | return 1; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | # Shell-quote a single path component. | ||||||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||||||
| 118 | # Same hardening as Fs::LibSSH::_q: single-quote wrap with '\''-escaping | ||||||||||||||||||||||||||||||||
| 119 | # defends against spaces, $, `, \, newlines and the empty string; | ||||||||||||||||||||||||||||||||
| 120 | # NUL (\0) is rejected because it cannot be represented in argv. | ||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | sub _q { | ||||||||||||||||||||||||||||||
|
Subroutine
_q: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 122 | 3 | my ($path) = @_; | |||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | die "LibSSH File _q: path contains NUL byte\n" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $path and index($path, "\0") != -1
MC/DC50%1/2
defined $path and index($path, "\0") != -1
defined $path
index($path, "\0") != -1
Truth table66%2/3
A and B
A= defined $pathB= index($path, "\0") != -1
| |||||||||||||||||||||||||||||||||
| 124 | if defined $path && index( $path, "\0" ) >= 0; | ||||||||||||||||||||||||||||||||
| 125 | 2 | $path =~ s/'/'"'"'/g; | |||||||||||||||||||||||||||||||
| 126 | 2 | return "'$path'"; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 2 | 1; | |||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||