lib/Rex/Interface/File/Sudo.pm

filter: Statementstmt 32.9% 26 / 79 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 6 totaltotal 24.8% 34 / 137 CC 14 SCAR 30.1
CC
14
Cov
24%
open
42.0
close
42.0
write
12.0
CRAP 20.2
« blib/lib/Rex/Cloud/Amazon.pm ↑ Summary blib/lib/Rex/Notify.pm »
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
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (my $ssh = Rex::is_ssh()) { } 0 0 0
34 0 ▶ if ( ref $ssh eq "Net::OpenSSH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ssh eq 'Net::OpenSSH') { } 0 0 0
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
BranchTrueFalseTotal
if ($self->_fs->is_file($file)) 0 0 0
52  
53 # resolving symlinks
54 0 ▶ while ( my $link = $self->_fs->readlink($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $link = $self->_fs->readlink($file)) 0 0 0
55 0 ▶ if ( $link !~ m/^\// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $link =~ m[^/]u) { } 0 0 0
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
BranchTrueFalseTotal
if "Rex::Config"->get_write_utf8_files and utf8::is_utf8($buf) 0 0 0
Condition0%0/3
"Rex::Config"->get_write_utf8_files and utf8::is_utf8($buf)
!ll&&!rl&&r
000
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)
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $self->{"fh"} 0 0 0
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
BranchTrueFalseTotal
if (exists $self->{"mode"} and $self->{"mode"} eq ">" || $self->{"mode"} eq ">>") 0 0 0
Condition0%0/6
exists $self->{"mode"} and $self->{"mode"} eq ">" || $self->{"mode"} eq ">>"
!ll&&!rl&&r
000
$self->{"mode"} eq ">" or $self->{"mode"} eq ">>"
l!l&&r!l&&!r
000
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 ">>"
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
if ($self->{"file_stat"}) 0 0 0
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;
« blib/lib/Rex/Cloud/Amazon.pm ↑ Summary blib/lib/Rex/Notify.pm »