blib/lib/Rex/Interface/Fs/SSH.pm untested

filter: Statementstmt 0.0% 0 / 159 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 292 CC 16 SCAR 19.1
CC
16
Cov
0%
ls
12.0
mkdir
12.0
rename
12.0
CRAP 6.8
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Interface::Fs::SSH;
6  
7 use v5.14.4;
8 use warnings;
9  
10 our $VERSION = '1.16.1'; # VERSION
11  
12 use Rex::Helper::File::Stat;
13 use Rex::Helper::Encode;
14 use Rex::Interface::Exec;
15 use Rex::Interface::Fs::Base;
16 use base qw(Rex::Interface::Fs::Base);
17  
18 require Rex::Commands;
19  
20 sub new {
21 my $that = shift;
22 my $proto = ref($that) || $that;
23 my $self = $proto->SUPER::new(@_);
24  
25 bless( $self, $proto );
26  
27 return $self;
28 }
29  
30 sub ls {
31 my ( $self, $path ) = @_;
32  
33 my @ret;
34  
35 Rex::Commands::profiler()->start("ls: $path");
36 eval {
37  
38 my $sftp = Rex::get_sftp();
39 my $dir = $sftp->opendir($path);
40 unless ($dir) {
41 die("$path is not a directory");
42 }
43  
44 while ( my $entry = $dir->read ) {
45 push @ret, $entry->{'name'};
46 }
47 };
48 Rex::Commands::profiler()->end("ls: $path");
49  
50 # failed open directory, return undef
51 if ($@) { return; }
52  
53 # return directory content
54 return @ret;
55 }
56  
57 sub is_dir {
58 my ( $self, $path ) = @_;
59  
60 Rex::Commands::profiler()->start("is_dir: $path");
61  
62 my $sftp = Rex::get_sftp();
63 my $stat = $sftp->stat($path);
64  
65 Rex::Commands::profiler()->end("is_dir: $path");
66  
67 defined $stat && defined $stat->{mode}
68 ? return Rex::Helper::File::Stat->S_ISDIR( $stat->{mode} )
69 : return undef;
70 }
71  
72 sub is_file {
73 my ( $self, $file ) = @_;
74  
75 Rex::Commands::profiler()->start("is_file: $file");
76  
77 my $sftp = Rex::get_sftp();
78 my $stat = $sftp->stat($file);
79 Rex::Commands::profiler()->end("is_file: $file");
80  
81 defined $stat && defined $stat->{mode}
82 ? return ( Rex::Helper::File::Stat->S_ISREG( $stat->{mode} )
83 || Rex::Helper::File::Stat->S_ISLNK( $stat->{mode} )
84 || Rex::Helper::File::Stat->S_ISBLK( $stat->{mode} )
85 || Rex::Helper::File::Stat->S_ISCHR( $stat->{mode} )
86 || Rex::Helper::File::Stat->S_ISFIFO( $stat->{mode} )
87 || Rex::Helper::File::Stat->S_ISSOCK( $stat->{mode} ) )
88 : return undef;
89 }
90  
91 sub unlink {
92 my ( $self, @files ) = @_;
93  
94 my $sftp = Rex::get_sftp();
95 for my $file (@files) {
96 Rex::Commands::profiler()->start("unlink: $file");
97 eval {
98 $sftp->unlink($file);
99 1;
100 } or do {
101 die "Error unlinking file: $file." if ( Rex::Config->get_autodie );
102 };
103 Rex::Commands::profiler()->end("unlink: $file");
104 }
105 }
106  
107 sub mkdir {
108 my ( $self, $dir ) = @_;
109  
110 my $ret;
111  
112 Rex::Commands::profiler()->start("mkdir: $dir");
113 my $sftp = Rex::get_sftp();
114  
115 $sftp->mkdir($dir);
116 if ( $self->is_dir($dir) ) {
117 $ret = 1;
118 }
119 else {
120 die "Error creating directory: $dir." if ( Rex::Config->get_autodie );
121 }
122  
123 Rex::Commands::profiler()->end("mkdir: $dir");
124  
125 return $ret;
126 }
127  
128 sub stat {
129 my ( $self, $file ) = @_;
130  
131 Rex::Commands::profiler()->start("stat: $file");
132  
133 my $sftp = Rex::get_sftp();
134 my %ret = $sftp->stat($file);
135  
136 if ( !%ret ) { return undef; }
137  
138 $ret{'mode'} =
139 sprintf( "%04o", Rex::Helper::File::Stat->S_IMODE( $ret{'mode'} ) );
140  
141 Rex::Commands::profiler()->end("stat: $file");
142  
143 return %ret;
144 }
145  
146 sub is_readable {
147 my ( $self, $file ) = @_;
148  
149 Rex::Commands::profiler()->start("is_readable: $file");
150 ($file) = $self->_normalize_path($file);
151  
152 my $exec = Rex::Interface::Exec->create;
153 $exec->exec("perl -le 'if(-r \"$file\") { exit 0; } exit 1'");
154  
155 Rex::Commands::profiler()->end("is_readable: $file");
156  
157 if ( $? == 0 ) { return 1; }
158 }
159  
160 sub is_writable {
161 my ( $self, $file ) = @_;
162  
163 Rex::Commands::profiler()->start("is_writable: $file");
164 ($file) = $self->_normalize_path($file);
165  
166 my $exec = Rex::Interface::Exec->create;
167 $exec->exec("perl -le 'if(-w \"$file\") { exit 0; } exit 1'");
168  
169 Rex::Commands::profiler()->end("is_writable: $file");
170  
171 if ( $? == 0 ) { return 1; }
172 }
173  
174 sub readlink {
175 my ( $self, $file ) = @_;
176  
177 my $ret;
178  
179 Rex::Commands::profiler()->start("readlink: $file");
180  
181 my $sftp = Rex::get_sftp();
182 $ret = $sftp->readlink($file);
183  
184 Rex::Commands::profiler()->end("readlink: $file");
185  
186 return $ret;
187 }
188  
189 sub rename {
190 my ( $self, $old, $new ) = @_;
191  
192 my $ret;
193 my $orig_path_old = $old;
194 my $orig_path_new = $new;
195  
196 Rex::Commands::profiler()->start("rename: $old -> $new");
197 ($old) = $self->_normalize_path($old);
198 ($new) = $self->_normalize_path($new);
199  
200 # don't use rename() doesn't work with different file systems / partitions
201 my $exec = Rex::Interface::Exec->create;
202 $exec->exec("/bin/mv $old $new");
203  
204 if ( ( !$self->is_file($orig_path_old) && !$self->is_dir($orig_path_old) )
205 && ( $self->is_file($orig_path_new) || $self->is_dir($orig_path_new) ) )
206 {
207 $ret = 1;
208 }
209 else {
210 die "Error renaming file or directory ($orig_path_old -> $orig_path_new)."
211 if ( Rex::Config->get_autodie );
212 }
213  
214 Rex::Commands::profiler()->end("rename: $orig_path_old -> $orig_path_new");
215  
216 return $ret;
217 }
218  
219 sub glob {
220 my ( $self, $glob ) = @_;
221  
222 Rex::Commands::profiler()->start("glob: $glob");
223  
224 my $ssh = Rex::is_ssh();
225 my $exec = Rex::Interface::Exec->create;
226 my $content = $exec->exec("perl -le'print join(\"*,*,*\", glob(\"$glob\"))'");
227 chomp $content;
228 my @files = split( /\*,\*,\*/, $content );
229  
230 Rex::Commands::profiler()->end("glob: $glob");
231  
232 return @files;
233 }
234  
235 sub upload {
236 my ( $self, $source, $target ) = @_;
237  
238 Rex::Commands::profiler()->start("upload: $source -> $target");
239  
240 my $ssh = Rex::is_ssh();
241 unless ( $ssh->scp_put( $source, $target ) ) {
242 Rex::Logger::debug("upload: $target is not writable");
243  
244 Rex::Commands::profiler()->end("upload: $source -> $target");
245  
246 die("upload: $target is not writable.");
247 }
248  
249 Rex::Commands::profiler()->end("upload: $source -> $target");
250 }
251  
252 sub download {
253 my ( $self, $source, $target ) = @_;
254  
255 Rex::Commands::profiler()->start("download: $source -> $target");
256  
257 if ( $^O =~ m/^MSWin/ ) {
258  
259 # fix for: #271
260 my $ssh = Rex::is_ssh();
261 my $sftp = $ssh->sftp();
262 eval {
263 my $fh = $sftp->open($source) or die($!);
264 open( my $out, ">", $target ) or die($!);
265 binmode $out;
266 print $out $_ while (<$fh>);
267 close $out;
268 close $fh;
269 1;
270 } or do {
271 Rex::Commands::profiler()->end("download: $source -> $target");
272 die( $ssh->error );
273 };
274 }
275 else {
276 my $ssh = Rex::is_ssh();
277 if ( !$ssh->scp_get( $source, $target ) ) {
278 Rex::Commands::profiler()->end("download: $source -> $target");
279 die( $ssh->error );
280 }
281 }
282  
283 Rex::Commands::profiler()->end("download: $source -> $target");
284 }
285  
286 1;