| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | ###### DEPRECATED | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package Rex::Sudo::File; | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Rex; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Rex::Commands; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Rex::Commands::Fs; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Rex::Helper::Path; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub open { | ||||||||||||||||||||||||||
|
Subroutine
open: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 0 | my $that = shift; | |||||||||||||||||||||||||||
| 23 | 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
| |||||||||||||||||||||||||||||
| 24 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | $self->{mode} = shift; | |||||||||||||||||||||||||||
| 27 | 0 | $self->{file} = shift; | |||||||||||||||||||||||||||
| 28 | 0 | $self->{rndfile} = get_tmp_file; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if ( my $sftp = Rex::get_sftp() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ( $self->{mode} eq ">" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 32 | $self->{fh} = | ||||||||||||||||||||||||||||
| 33 | 0 | $sftp->open( $self->{rndfile}, O_WRONLY | O_CREAT | O_TRUNC ); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | elsif ( $self->{mode} eq ">>" ) { | ||||||||||||||||||||||||||||
| 36 | 0 | cp( $self->{file}, $self->{rndfile} ); | |||||||||||||||||||||||||||
| 37 | 0 | chmod( 666, $self->{rndfile} ); | |||||||||||||||||||||||||||
| 38 | 0 | $self->{fh} = $sftp->open( $self->{rndfile}, O_WRONLY | O_APPEND ); | |||||||||||||||||||||||||||
| 39 | 0 | my %stat = stat $self->{rndfile}; | |||||||||||||||||||||||||||
| 40 | 0 | $self->{fh}->seek( $stat{size} ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | else { | ||||||||||||||||||||||||||||
| 43 | 0 | cp( $self->{file}, $self->{rndfile} ); | |||||||||||||||||||||||||||
| 44 | 0 | chmod( 666, $self->{rndfile} ); | |||||||||||||||||||||||||||
| 45 | 0 | $self->{fh} = $sftp->open( $self->{rndfile}, O_RDONLY ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||
| 49 | 0 | $self->{fh} = IO::File->new; | |||||||||||||||||||||||||||
| 50 | 0 | $self->{fh}->open( $self->{mode} . " " . $self->{rndfile} ); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | bless( $self, $proto ); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | return $self; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 0 | my ( $self, $content ) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ( ref( $self->{fh} ) eq "Net::SSH2::File" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $self->{fh}->write($content); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||
| 65 | 0 | $self->{fh}->print($content); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub seek { | ||||||||||||||||||||||||||
|
Subroutine
seek: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 0 | my ( $self, $offset ) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ( ref( $self->{fh} ) eq "Net::SSH2::File" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $self->{fh}->seek($offset); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 0 | $self->{fh}->seek( $offset, 0 ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my ( $self, $len ) = @_; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | $len ||= 64; | ||||||||||||||||||||||||||
|
Condition0%0/2
$len ||= 64
MC/DC0%0/1
$len ||= 64
$len
Truth table0%0/2
A
A= $len
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | my $buf; | |||||||||||||||||||||||||||
| 85 | 0 | $self->{fh}->read( $buf, $len ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | return $buf; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 91 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | return unless $self->{fh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ( ref( $self->{fh} ) eq "Net::SSH2::File" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->{fh} = undef; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||
| 99 | 0 | $self->{fh}->close; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # use cat to not overwrite attributes/owner/group | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $self->{mode} eq ">" || $self->{mode} eq ">>" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"mode"} eq ">" or $self->{"mode"} eq ">>"
MC/DC0%0/2
$self->{"mode"} eq ">" or $self->{"mode"} eq ">>"
$self->{"mode"} eq ">"
$self->{"mode"} eq ">>"
Truth table0%0/3
A or B
A= $self->{"mode"} eq ">"B= $self->{"mode"} eq ">>"
| |||||||||||||||||||||||||||||
| 104 | 0 | i_run "cat " . $self->{rndfile} . " >" . $self->{file}; | |||||||||||||||||||||||||||
| 105 | 0 | rm( $self->{rndfile} ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 111 | 0 | $self->close; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | 1; | |||||||||||||||||||||||||||