| 1 | # | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | package Rex::Interface::File::Sudo; | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Fcntl; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | require Rex::Commands; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Rex::Interface::Fs; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Rex::Interface::File::Base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Rex::Helper::Path; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use base qw(Rex::Interface::File::Base); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | my $that = shift; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | my $self = $proto->SUPER::new(@_); | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | bless( $self, $proto ); | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub open { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my ( $self, $mode, $file ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ( my $ssh = Rex::is_ssh() ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ( ref $ssh eq "Net::OpenSSH" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $self->{fh} = Rex::Interface::File->create("OpenSSH"); | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | $self->{fh} = Rex::Interface::File->create("SSH"); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $self->{fh} = Rex::Interface::File->create("Local"); | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # always use current logged in user for sudo fs operations | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | Rex::get_current_connection_object()->push_sudo_options( {} ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $self->{mode} = $mode; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $self->{file} = $file; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | $self->{rndfile} = get_tmp_file; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ( $self->_fs->is_file($file) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # resolving symlinks | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | while ( my $link = $self->_fs->readlink($file) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( $link !~ m/^\// ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $file = dirname($file) . "/" . $link; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $file = $link; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $link = $self->_fs->readlink($link); | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $self->{file_stat} = { $self->_fs->stat( $self->{file} ) }; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $self->_fs->cp( $file, $self->{rndfile} ); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $self->_fs->chmod( 600, $self->{rndfile} ); | |||||||||||||||||||||||||||||||||||||||||
| 67 | $self->_fs->chown( Rex::Commands::connection->get_auth_user, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $self->{rndfile} ); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $self->{fh}->open( $mode, $self->{rndfile} ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | Rex::get_current_connection_object()->pop_sudo_options(); | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return $self->{fh}; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my ( $self, $len ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | return $self->{fh}->read($len); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my ( $self, $buf ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 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)
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | if Rex::Config->get_write_utf8_files && utf8::is_utf8($buf); | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $self->{fh}->write($buf); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub seek { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
seek: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my ( $self, $pos ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $self->{fh}->seek($pos); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub close { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return unless $self->{fh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # always use current logged in user for sudo fs operations | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | Rex::get_current_connection_object()->push_sudo_options( {} ); | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $self->{fh}->close; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( exists $self->{mode} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
exists $self->{"mode"} and $self->{"mode"} eq ">" || $self->{"mode"} eq ">>"
$self->{"mode"} eq ">" or $self->{"mode"} eq ">>"
MC/DC0%0/3
exists $self->{"mode"} and $self->{"mode"} eq ">" || $self->{"mode"} eq ">>"
exists $self->{"mode"}
$self->{"mode"} eq ">"
$self->{"mode"} eq ">>"
Truth table0%0/4
A and B or C
A= exists $self->{"mode"}B= $self->{"mode"} eq ">"C= $self->{"mode"} eq ">>"
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | && ( $self->{mode} eq ">" || $self->{mode} eq ">>" ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $exec = Rex::Interface::Exec->create; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ( $self->{file_stat} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my %stat = $self->_fs->stat( $self->{file} ); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $self->_fs->chmod( $stat{mode}, $self->{rndfile} ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->_fs->chown( $stat{uid}, $self->{rndfile} ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->_fs->chgrp( $stat{gid}, $self->{rndfile} ); | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $self->_fs->rename( $self->{rndfile}, $self->{file} ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | #$exec->exec("cat " . $self->{rndfile} . " >'" . $self->{file} . "'"); | ||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $self->{fh} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $self->_fs->unlink( $self->{rndfile} ); | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | Rex::get_current_connection_object()->pop_sudo_options(); | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $self = undef; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub _fs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fs: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | return Rex::Interface::Fs->create("Sudo"); | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||