lib/Rex/Interface/Fs/Sudo.pm

filter: Statementstmt 17.3% 26 / 150 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 27.5% 8 / 29 Podpod 0.0% 0 / 14 totaltotal 14.5% 34 / 233 CC 18 SCAR 17.9
CC
18
Cov
14%
upload
12.0
download
12.0
is_file
12.0
CRAP 6.0
« lib/Rex/User.pm ↑ Summary lib/Rex/User/OpenWrt.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Interface::Fs::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 require Rex::Commands;
13 1 ▶use Rex::Interface::Fs::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Rex::Helper::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Rex::Helper::Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use base qw(Rex::Interface::Fs::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Dumper;
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 ls {
Subroutine ls: not called CC=2 SCAR=17.9
Pod undocumented
31 0 my ( $self, $path ) = @_;
32  
33 0 my @ret;
34  
35 0 my @out = split(
36 /\n/,
37 $self->_exec(
38 "ls -a1 $path", undef, { env => { QUOTING_STYLE => "literal" } }
39 )
40 );
41  
42 # failed open directory, return undef
43 0 ▶ if ( $? != 0 ) { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
44  
45 0 @ret = grep { !m/^\.\.?$/ } @out;
46  
47 # return directory content
48 0 return @ret;
49 }
50  
51 0 ▶sub upload {
Subroutine upload: not called CC=3 SCAR=24.8
Pod undocumented
52 0 my ( $self, $source, $target ) = @_;
53  
54 0 my $rnd_file = get_tmp_file;
55  
56 0 ▶ if ( my $ssh = Rex::is_ssh() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ssh = Rex::is_ssh()) { } 0 0 0
57 0 ▶ if ( ref $ssh eq "Net::OpenSSH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ssh eq 'Net::OpenSSH') { } 0 0 0
58 0 $ssh->sftp->put( $source, $rnd_file );
59 }
60 else {
61 0 $ssh->scp_put( $source, $rnd_file );
62 }
63 0 $self->_exec("mv $rnd_file '$target'");
64 }
65 else {
66 0 $self->cp( $source, $target );
67 }
68  
69 }
70  
71 0 ▶sub download {
Subroutine download: not called CC=3 SCAR=24.8
Pod undocumented
72 0 my ( $self, $source, $target ) = @_;
73  
74 0 my $rnd_file = get_tmp_file;
75  
76 0 ▶ if ( my $ssh = Rex::is_ssh() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ssh = Rex::is_ssh()) { } 0 0 0
77 0 $self->_exec("cp '$source' $rnd_file");
78 0 $self->chmod( 444, $rnd_file );
79 0 ▶ if ( ref $ssh eq "Net::OpenSSH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ssh eq 'Net::OpenSSH') { } 0 0 0
80 0 $ssh->sftp->get( $rnd_file, $target );
81 }
82 else {
83 0 $ssh->scp_get( $rnd_file, $target );
84 }
85 Rex::get_current_connection_object()->run_sudo_unmodified(
86 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 $self->unlink($rnd_file);
88 }
89 0 );
90 }
91 else {
92 0 $self->cp( $source, $target );
93 }
94  
95 }
96  
97 0 ▶sub is_dir {
Subroutine is_dir: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my ( $self, $path ) = @_;
99  
100 0 ($path) = $self->_normalize_path($path);
101  
102 0 $self->_exec("test -d $path");
103 0 my $ret = $?;
104  
105 0 ▶ $ret == 0 ? return 1 : return undef;
Branch: 1 branch
BranchTrueFalseTotal
$ret == 0 ? : 0 0 0
106 }
107  
108 0 ▶sub is_file {
Subroutine is_file: not called CC=3 SCAR=24.8
Pod undocumented
109 0 my ( $self, $file ) = @_;
110  
111 0 ($file) = $self->_normalize_path($file);
112  
113 0 $self->_exec("test -e $file");
114 0 my $is_file = $?;
115  
116 0 $self->_exec("test -d $file");
117 0 my $is_dir = $?;
118  
119 0 ▶ ( $is_file == 0 && $is_dir != 0 ) ? return 1 : return undef;
Branch: 1 branch
BranchTrueFalseTotal
$is_file == 0 && $is_dir != 0 ? : 0 0 0
Condition0%0/3
$is_file == 0 and $is_dir != 0
!ll&&!rl&&r
000
MC/DC0%0/2
$is_file == 0 and $is_dir != 0
$is_file == 0 $is_dir != 0
Truth table0%0/3
A and B
A= $is_file == 0B= $is_dir != 0
ABresult
0X0
100
111
120 }
121  
122 0 ▶sub unlink {
Subroutine unlink: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my ( $self, @files ) = @_;
124 0 (@files) = $self->_normalize_path(@files);
125  
126 0 $self->_exec( "rm " . join( " ", @files ) );
127 0 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 0 0 0
128 }
129  
130 0 ▶sub mkdir {
Subroutine mkdir: not called CC=2 SCAR=17.9
Pod undocumented
131 0 my ( $self, $dir ) = @_;
132 0 ($dir) = $self->_normalize_path($dir);
133 0 $self->_exec("mkdir $dir >/dev/null 2>&1");
134 0 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 0 0 0
135 }
136  
137 0 ▶sub stat {
Subroutine stat: not called CC=2 SCAR=17.9
Pod undocumented
138 0 my ( $self, $file ) = @_;
139  
140 0 my $script = q|
141 unlink $0;
142  
143 if(my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
144 $atime, $mtime, $ctime, $blksize, $blocks) = stat($ARGV[0])) {
145  
146 my %ret;
147  
148 $ret{'mode'} = sprintf("%04o", $mode & 07777);
149 $ret{'size'} = $size;
150 $ret{'uid'} = $uid;
151 $ret{'gid'} = $gid;
152 $ret{'atime'} = $atime;
153 $ret{'mtime'} = $mtime;
154  
155 print to_json(\%ret);
156 }
157  
158 |;
159  
160 0 $script .= func_to_json();
161  
162 0 my $rnd_file = $self->_write_to_rnd_file($script);
163 0 ($file) = $self->_normalize_path($file);
164 0 my $out = $self->_exec("perl $rnd_file $file");
165  
166 Rex::get_current_connection_object()->run_sudo_unmodified(
167 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 $self->unlink($rnd_file);
169 }
170 0 );
171  
172 0 ▶ if ( !$out ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 0 0 0
173 0 return undef;
174 }
175  
176 0 my $tmp = decode_json($out);
177  
178 0 return %{$tmp};
179 }
180  
181 0 ▶sub is_readable {
Subroutine is_readable: not called CC=2 SCAR=17.9
Pod undocumented
182 0 my ( $self, $file ) = @_;
183  
184 0 ($file) = $self->_normalize_path($file);
185 0 $self->_exec("test -r $file");
186  
187 0 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 0 0 0
188 }
189  
190 0 ▶sub is_writable {
Subroutine is_writable: not called CC=2 SCAR=17.9
Pod undocumented
191 0 my ( $self, $file ) = @_;
192  
193 0 ($file) = $self->_normalize_path($file);
194 0 $self->_exec("test -w $file");
195  
196 0 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 0 0 0
197 }
198  
199 0 ▶sub readlink {
Subroutine readlink: not called CC=1 SCAR=6.9
Pod undocumented
200 0 my ( $self, $file ) = @_;
201 0 my $script = q|unlink $0; print readlink($ARGV[0]) . "\n"; |;
202 0 ($file) = $self->_normalize_path($file);
203  
204 0 my $rnd_file = $self->_write_to_rnd_file($script);
205 0 my $out = $self->_exec("perl $rnd_file $file");
206 0 my $ret = $?;
207 0 chomp $out;
208 Rex::get_current_connection_object()->run_sudo_unmodified(
209 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 $self->unlink($rnd_file);
211 }
212 0 );
213 0 $? = $ret;
214  
215 0 return $out;
216 }
217  
218 0 ▶sub rename {
Subroutine rename: not called CC=2 SCAR=17.9
Pod undocumented
219 0 my ( $self, $old, $new ) = @_;
220 0 ($old) = $self->_normalize_path($old);
221 0 ($new) = $self->_normalize_path($new);
222  
223 0 $self->_exec("mv $old $new");
224  
225 0 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 0 0 0
226 }
227  
228 0 ▶sub glob {
Subroutine glob: not called CC=1 SCAR=6.9
Pod undocumented
229 0 my ( $self, $glob ) = @_;
230  
231 0 my $script = q|
232 unlink $0;
233 print to_json([ glob("| . $glob . q|") ]);
234 |;
235  
236 0 $script .= func_to_json();
237  
238 0 my $rnd_file = $self->_write_to_rnd_file($script);
239 0 my $content = $self->_exec("perl $rnd_file");
240 0 my $ret = $?;
241 Rex::get_current_connection_object()->run_sudo_unmodified(
242 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
243 0 $self->unlink($rnd_file);
244 }
245 0 );
246 0 $? = $ret;
247  
248 0 my $tmp = decode_json($content);
249  
250 0 return @{$tmp};
251 }
252  
253 0 ▶sub _get_file_writer {
Subroutine _get_file_writer: not called CC=3 SCAR=24.8
254 0 my ($self) = @_;
255  
256 0 my $fh;
257 0 ▶ if ( my $o = Rex::is_ssh() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = Rex::is_ssh()) { } 0 0 0
258 0 ▶ if ( ref $o eq "Net::OpenSSH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $o eq 'Net::OpenSSH') { } 0 0 0
259 0 $fh = Rex::Interface::File->create("OpenSSH");
260 }
261 else {
262 0 $fh = Rex::Interface::File->create("SSH");
263 }
264 }
265 else {
266 0 $fh = Rex::Interface::File->create("Local");
267 }
268  
269 0 return $fh;
270 }
271  
272 0 ▶sub _write_to_rnd_file {
Subroutine _write_to_rnd_file: not called CC=1 SCAR=6.9
273 0 my ( $self, $content ) = @_;
274 0 my $fh = $self->_get_file_writer();
275 0 my $rnd_file = get_tmp_file;
276  
277 0 $fh->open( ">", $rnd_file );
278 0 $fh->write($content);
279 0 $fh->close;
280  
281 0 return $rnd_file;
282 }
283  
284 0 ▶sub _exec {
Subroutine _exec: not called CC=1 SCAR=6.9
285 0 my ( $self, $cmd, $path, $option ) = @_;
286 0 my $exec = Rex::Interface::Exec->create("Sudo");
287 0 return $exec->exec( $cmd, $path, $option );
288 }
289  
290 1 1;
« lib/Rex/User.pm ↑ Summary lib/Rex/User/OpenWrt.pm »