lib/Rex/Interface/Fs/Base.pm

filter: Statementstmt 70.4% 88 / 125 Branchbran 35.7% 15 / 42 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 51.7% 15 / 29 Podpod 0.0% 0 / 21 totaltotal 51.4% 122 / 237 CC 27 SCAR 16.2
CC
27
Cov
60%
rmdir
12.0
chown
9.1
chgrp
9.1
CRAP 5.1
« lib/Rex/Commands/SimpleCheck.pm ↑ Summary lib/Rex/Cron/FreeBSD.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Interface::Fs::Base;
6  
7 52 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 52 our $VERSION = '1.16.1'; # VERSION
11  
12 52 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 52 ▶use Rex::Interface::Exec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 52 ▶use Rex::Helper::File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1260 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
17 1260 my $that = shift;
18 1260 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
012600
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
19 1260 my $self = {@_};
20  
21 1260 bless( $self, $proto );
22  
23 1260 return $self;
24 }
25  
26 0 ▶sub ls { die("Must be implemented by Interface Class"); }
Subroutine ls: not called CC=1 SCAR=6.9
Pod undocumented
27 0 ▶sub unlink { die("Must be implemented by Interface Class"); }
Subroutine unlink: not called CC=1 SCAR=6.9
Pod undocumented
28 0 ▶sub mkdir { die("Must be implemented by Interface Class"); }
Subroutine mkdir: not called CC=1 SCAR=6.9
Pod undocumented
29 0 ▶sub glob { die("Must be implemented by Interface Class"); }
Subroutine glob: not called CC=1 SCAR=6.9
Pod undocumented
30 0 ▶sub rename { die("Must be implemented by Interface Class"); }
Subroutine rename: not called CC=1 SCAR=6.9
Pod undocumented
31 0 ▶sub stat { die("Must be implemented by Interface Class"); }
Subroutine stat: not called CC=1 SCAR=6.9
Pod undocumented
32 0 ▶sub readlink { die("Must be implemented by Interface Class"); }
Subroutine readlink: not called CC=1 SCAR=6.9
Pod undocumented
33 0 ▶sub is_file { die("Must be implemented by Interface Class"); }
Subroutine is_file: not called CC=1 SCAR=6.9
Pod undocumented
34 0 ▶sub is_dir { die("Must be implemented by Interface Class"); }
Subroutine is_dir: not called CC=1 SCAR=6.9
Pod undocumented
35 0 ▶sub is_readable { die("Must be implemented by Interface Class"); }
Subroutine is_readable: not called CC=1 SCAR=6.9
Pod undocumented
36 0 ▶sub is_writable { die("Must be implemented by Interface Class"); }
Subroutine is_writable: not called CC=1 SCAR=6.9
Pod undocumented
37 0 ▶sub upload { die("Must be implemented by Interface Class"); }
Subroutine upload: not called CC=1 SCAR=6.9
Pod undocumented
38 0 ▶sub download { die("Must be implemented by Interface Class"); }
Subroutine download: not called CC=1 SCAR=6.9
Pod undocumented
39  
40 104 ▶sub is_symlink {
Subroutine is_symlink: called CC=2 SCAR=6.9
Pod undocumented
41 104 my ( $self, $path ) = @_;
42 104 ($path) = $self->_normalize_path($path);
43  
44 104 $self->_exec("/bin/sh -c '[ -L \"$path\" ]'");
45 104 my $ret = $?;
46  
47 104 ▶ if ( $ret == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($ret == 0) 47 57 104
48 }
49  
50 3 ▶sub ln {
Subroutine ln: called CC=3 SCAR=11.8
Pod undocumented
51 3 my ( $self, $from, $to ) = @_;
52  
53 3 Rex::Logger::debug("Symlinking files: $to -> $from");
54 3 ($from) = $self->_normalize_path($from);
55 3 ($to) = $self->_normalize_path($to);
56  
57 3 my $exec = Rex::Interface::Exec->create;
58 3 $exec->exec("ln -snf $from $to");
59  
60 3 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 3 0 3
61  
62 0 ▶ die "Error creating symlink. ($from -> $to)" if ( Rex::Config->get_autodie );
Branch: 1 branch
BranchTrueFalseTotal
if "Rex::Config"->get_autodie 0 0 0
63 }
64  
65 0 ▶sub rmdir {
Subroutine rmdir: not called CC=3 SCAR=24.8
Pod undocumented
66 0 my ( $self, @dirs ) = @_;
67  
68 0 @dirs = $self->_normalize_path(@dirs);
69  
70 0 Rex::Logger::debug( "Removing directories: " . join( ", ", @dirs ) );
71 0 my $exec = Rex::Interface::Exec->create;
72 0 $exec->exec( "/bin/rm -rf " . join( " ", @dirs ) );
73  
74 0 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 0 0 0
75  
76 0 ▶ die( "Error removing directory: " . join( ", ", @dirs ) )
Branch: 1 branch
BranchTrueFalseTotal
if "Rex::Config"->get_autodie 0 0 0
77 if ( Rex::Config->get_autodie );
78 }
79  
80 25 ▶sub chown {
Subroutine chown: called CC=6 SCAR=22.0
Pod undocumented
81 25 my ( $self, $user, $file, @opts ) = @_;
82 25 my $options = {@opts};
83 25 ($file) = $self->_normalize_path($file);
84  
85 25 my $recursive = "";
86 25 ▶ if ( exists $options->{"recursive"} && $options->{"recursive"} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"recursive"} and $options->{"recursive"} == 1) 0 25 25
Condition33%1/3
exists $options->{"recursive"} and $options->{"recursive"} == 1
!ll&&!rl&&r
2500
MC/DC0%0/2
exists $options->{"recursive"} and $options->{"recursive"} == 1
exists $options->{"recursive"} $options->{"recursive"} == 1
Truth table33%1/3
A and B
A= exists $options->{"recursive"}B= $options->{"recursive"} == 1
ABresult
0X0
100
111
87 0 $recursive = " -R ";
88 }
89  
90 25 my $exec = Rex::Interface::Exec->create;
91  
92 25 ▶ if ( $exec->can_run( ['chown'] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exec->can_run(['chown'])) { } 25 0 25
93 25 $exec->exec("chown $recursive $user $file");
94  
95 25 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 25 0 25
96  
97 0 ▶ die("Error running chown $recursive $user $file")
Branch: 1 branch
BranchTrueFalseTotal
if "Rex::Config"->get_autodie 0 0 0
98 if ( Rex::Config->get_autodie );
99 }
100 else {
101 0 Rex::Logger::debug("Can't find `chown`.");
102 0 return 1; # fake success for windows
103 }
104 }
105  
106 25 ▶sub chgrp {
Subroutine chgrp: called CC=6 SCAR=22.0
Pod undocumented
107 25 my ( $self, $group, $file, @opts ) = @_;
108 25 my $options = {@opts};
109 25 ($file) = $self->_normalize_path($file);
110  
111 25 my $recursive = "";
112 25 ▶ if ( exists $options->{"recursive"} && $options->{"recursive"} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"recursive"} and $options->{"recursive"} == 1) 0 25 25
Condition33%1/3
exists $options->{"recursive"} and $options->{"recursive"} == 1
!ll&&!rl&&r
2500
MC/DC0%0/2
exists $options->{"recursive"} and $options->{"recursive"} == 1
exists $options->{"recursive"} $options->{"recursive"} == 1
Truth table33%1/3
A and B
A= exists $options->{"recursive"}B= $options->{"recursive"} == 1
ABresult
0X0
100
111
113 0 $recursive = " -R ";
114 }
115  
116 25 my $exec = Rex::Interface::Exec->create;
117  
118 25 ▶ if ( $exec->can_run( ['chgrp'] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exec->can_run(['chgrp'])) { } 25 0 25
119 25 $exec->exec("chgrp $recursive $group $file");
120  
121 25 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 25 0 25
122  
123 0 ▶ die("Error running chgrp $recursive $group $file")
Branch: 1 branch
BranchTrueFalseTotal
if "Rex::Config"->get_autodie 0 0 0
124 if ( Rex::Config->get_autodie );
125 }
126 else {
127 0 Rex::Logger::debug("Can't find `chgrp`.");
128 0 return 1; # fake success for windows
129 }
130 }
131  
132 33 ▶sub chmod {
Subroutine chmod: called CC=6 SCAR=22.0
Pod undocumented
133 33 my ( $self, $mode, $file, @opts ) = @_;
134 33 my $options = {@opts};
135 33 ($file) = $self->_normalize_path($file);
136  
137 33 my $recursive = "";
138 33 ▶ if ( exists $options->{"recursive"} && $options->{"recursive"} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"recursive"} and $options->{"recursive"} == 1) 0 33 33
Condition33%1/3
exists $options->{"recursive"} and $options->{"recursive"} == 1
!ll&&!rl&&r
3300
MC/DC0%0/2
exists $options->{"recursive"} and $options->{"recursive"} == 1
exists $options->{"recursive"} $options->{"recursive"} == 1
Truth table33%1/3
A and B
A= exists $options->{"recursive"}B= $options->{"recursive"} == 1
ABresult
0X0
100
111
139 0 $recursive = " -R ";
140 }
141  
142 33 my $exec = Rex::Interface::Exec->create;
143  
144 33 ▶ if ( $exec->can_run( ['chmod'] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exec->can_run(['chmod'])) { } 33 0 33
145 33 $exec->exec("chmod $recursive $mode $file");
146  
147 33 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 33 0 33
148  
149 0 ▶ die("Error running chmod $recursive $mode $file")
Branch: 1 branch
BranchTrueFalseTotal
if "Rex::Config"->get_autodie 0 0 0
150 if ( Rex::Config->get_autodie );
151 }
152 else {
153 0 Rex::Logger::debug("Can't find `chmod`.");
154 0 return 1; # fake success for windows
155 }
156 }
157  
158 3 ▶sub cp {
Subroutine cp: called CC=4 SCAR=15.8
Pod undocumented
159 3 my ( $self, $source, $dest ) = @_;
160 3 ($source) = $self->_normalize_path($source);
161 3 ($dest) = $self->_normalize_path($dest);
162  
163 3 my $exec = Rex::Interface::Exec->create;
164  
165 3 ▶ if ( $OSNAME =~ m/^MSWin/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rex::Interface::Fs::Base::OSNAME =~ /^MSWin/msux) { } 0 3 3
166 0 $exec->exec("copy /v /y $source $dest");
167 }
168 else {
169 3 $exec->exec("cp -R $source $dest");
170 }
171  
172 3 ▶ if ( $? == 0 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) 3 0 3
173  
174 0 ▶ die("Error copying $source -> $dest") if ( Rex::Config->get_autodie );
Branch: 1 branch
BranchTrueFalseTotal
if "Rex::Config"->get_autodie 0 0 0
175 }
176  
177 278 ▶sub _normalize_path {
Subroutine _normalize_path: called CC=3 SCAR=11.1
178 278 my ( $self, @dirs ) = @_;
179  
180 278 my @ret;
181 278 for my $d (@dirs) {
182 278 my @t;
183 278 ▶ if (Rex::is_ssh) {
Branch: 1 branch
BranchTrueFalseTotal
if (Rex::is_ssh()) { } 0 278 278
184 0 @t = Rex::Helper::File::Spec->splitdir($d);
185 }
186 else {
187 278 @t = Rex::Helper::File::Spec->splitdir($d);
188 }
189 push( @ret,
190 278 Rex::Helper::File::Spec->catfile( map { $self->_quotepath($_) } @t ) );
191 }
192  
193 # for (@dirs) {
194 # s/ /\\ /g;
195 # }
196  
197 278 return @ret;
198 }
199  
200 1219 ▶sub _quotepath {
Subroutine _quotepath: called CC=1 SCAR=0.0
201 1219 my ( $self, $p ) = @_;
202 1219 $p =~ s/([\@\$\% ])/\\$1/g;
203  
204 1219 return $p;
205 }
206  
207 104 ▶sub _exec {
Subroutine _exec: called CC=1 SCAR=0.0
208 104 my ( $self, $cmd ) = @_;
209 104 my $exec = Rex::Interface::Exec->create;
210 104 return $exec->exec($cmd);
211 }
212  
213 52 1;
« lib/Rex/Commands/SimpleCheck.pm ↑ Summary lib/Rex/Cron/FreeBSD.pm »