File Coverage

lib/Rex/Interface/Fs/Base.pm
Criterion Covered Total %
statement 86 123 69.9
branch 15 42 35.7
condition 4 12 33.3
subroutine 15 29 51.7
pod 0 21 0.0
total 120 227 52.8


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Interface::Fs::Base;
6              
7 53     53   815 use v5.12.5;
  53         300  
8 53     53   411 use warnings;
  53         217  
  53         3213  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 53     53   460 use English qw(-no_match_vars);
  53         238  
  53         1176  
13 53     53   39608 use Rex::Interface::Exec;
  53         181  
  53         912  
14 53     53   1865 use Rex::Helper::File::Spec;
  53         180  
  53         1188  
15              
16             sub new {
17 1260     1260 0 4117 my $that = shift;
18 1260   33     11068 my $proto = ref($that) || $that;
19 1260         4053 my $self = {@_};
20              
21 1260         2850 bless( $self, $proto );
22              
23 1260         3626 return $self;
24             }
25              
26 0     0 0 0 sub ls { die("Must be implemented by Interface Class"); }
27 0     0 0 0 sub unlink { die("Must be implemented by Interface Class"); }
28 0     0 0 0 sub mkdir { die("Must be implemented by Interface Class"); }
29 0     0 0 0 sub glob { die("Must be implemented by Interface Class"); }
30 0     0 0 0 sub rename { die("Must be implemented by Interface Class"); }
31 0     0 0 0 sub stat { die("Must be implemented by Interface Class"); }
32 0     0 0 0 sub readlink { die("Must be implemented by Interface Class"); }
33 0     0 0 0 sub is_file { die("Must be implemented by Interface Class"); }
34 0     0 0 0 sub is_dir { die("Must be implemented by Interface Class"); }
35 0     0 0 0 sub is_readable { die("Must be implemented by Interface Class"); }
36 0     0 0 0 sub is_writable { die("Must be implemented by Interface Class"); }
37 0     0 0 0 sub upload { die("Must be implemented by Interface Class"); }
38 0     0 0 0 sub download { die("Must be implemented by Interface Class"); }
39              
40             sub is_symlink {
41 98     98 0 605 my ( $self, $path ) = @_;
42 98         961 ($path) = $self->_normalize_path($path);
43              
44 98         754 $self->_exec("/bin/sh -c '[ -L \"$path\" ]'");
45 98         1515 my $ret = $?;
46              
47 98 100       2278 if ( $ret == 0 ) { return 1; }
  47         1518  
48             }
49              
50             sub ln {
51 3     3 0 50 my ( $self, $from, $to ) = @_;
52              
53 3         103 Rex::Logger::debug("Symlinking files: $to -> $from");
54 3         37 ($from) = $self->_normalize_path($from);
55 3         27 ($to) = $self->_normalize_path($to);
56              
57 3         109 my $exec = Rex::Interface::Exec->create;
58 3         45 $exec->exec("ln -snf $from $to");
59              
60 3 50       60 if ( $? == 0 ) { return 1; }
  3         77  
61              
62 0 0       0 die "Error creating symlink. ($from -> $to)" if ( Rex::Config->get_autodie );
63             }
64              
65             sub rmdir {
66 0     0 0 0 my ( $self, @dirs ) = @_;
67              
68 0         0 @dirs = $self->_normalize_path(@dirs);
69              
70 0         0 Rex::Logger::debug( "Removing directories: " . join( ", ", @dirs ) );
71 0         0 my $exec = Rex::Interface::Exec->create;
72 0         0 $exec->exec( "/bin/rm -rf " . join( " ", @dirs ) );
73              
74 0 0       0 if ( $? == 0 ) { return 1; }
  0         0  
75              
76 0 0       0 die( "Error removing directory: " . join( ", ", @dirs ) )
77             if ( Rex::Config->get_autodie );
78             }
79              
80             sub chown {
81 25     25 0 319 my ( $self, $user, $file, @opts ) = @_;
82 25         126 my $options = {@opts};
83 25         279 ($file) = $self->_normalize_path($file);
84              
85 25         204 my $recursive = "";
86 25 50 33     183 if ( exists $options->{"recursive"} && $options->{"recursive"} == 1 ) {
87 0         0 $recursive = " -R ";
88             }
89              
90 25         671 my $exec = Rex::Interface::Exec->create;
91              
92 25 50       430 if ( $exec->can_run( ['chown'] ) ) {
93 25         663 $exec->exec("chown $recursive $user $file");
94              
95 25 50       600 if ( $? == 0 ) { return 1; }
  25         806  
96              
97 0 0       0 die("Error running chown $recursive $user $file")
98             if ( Rex::Config->get_autodie );
99             }
100             else {
101 0         0 Rex::Logger::debug("Can't find `chown`.");
102 0         0 return 1; # fake success for windows
103             }
104             }
105              
106             sub chgrp {
107 25     25 0 372 my ( $self, $group, $file, @opts ) = @_;
108 25         148 my $options = {@opts};
109 25         241 ($file) = $self->_normalize_path($file);
110              
111 25         278 my $recursive = "";
112 25 50 33     205 if ( exists $options->{"recursive"} && $options->{"recursive"} == 1 ) {
113 0         0 $recursive = " -R ";
114             }
115              
116 25         612 my $exec = Rex::Interface::Exec->create;
117              
118 25 50       374 if ( $exec->can_run( ['chgrp'] ) ) {
119 25         686 $exec->exec("chgrp $recursive $group $file");
120              
121 25 50       640 if ( $? == 0 ) { return 1; }
  25         768  
122              
123 0 0       0 die("Error running chgrp $recursive $group $file")
124             if ( Rex::Config->get_autodie );
125             }
126             else {
127 0         0 Rex::Logger::debug("Can't find `chgrp`.");
128 0         0 return 1; # fake success for windows
129             }
130             }
131              
132             sub chmod {
133 33     33 0 571 my ( $self, $mode, $file, @opts ) = @_;
134 33         177 my $options = {@opts};
135 33         474 ($file) = $self->_normalize_path($file);
136              
137 33         327 my $recursive = "";
138 33 50 33     317 if ( exists $options->{"recursive"} && $options->{"recursive"} == 1 ) {
139 0         0 $recursive = " -R ";
140             }
141              
142 33         826 my $exec = Rex::Interface::Exec->create;
143              
144 33 50       440 if ( $exec->can_run( ['chmod'] ) ) {
145 33         602 $exec->exec("chmod $recursive $mode $file");
146              
147 33 50       618 if ( $? == 0 ) { return 1; }
  33         1168  
148              
149 0 0       0 die("Error running chmod $recursive $mode $file")
150             if ( Rex::Config->get_autodie );
151             }
152             else {
153 0         0 Rex::Logger::debug("Can't find `chmod`.");
154 0         0 return 1; # fake success for windows
155             }
156             }
157              
158             sub cp {
159 3     3 0 24 my ( $self, $source, $dest ) = @_;
160 3         40 ($source) = $self->_normalize_path($source);
161 3         38 ($dest) = $self->_normalize_path($dest);
162              
163 3         50 my $exec = Rex::Interface::Exec->create;
164              
165 3 50       462 if ( $OSNAME =~ m/^MSWin/msx ) {
166 0         0 $exec->exec("copy /v /y $source $dest");
167             }
168             else {
169 3         37 $exec->exec("cp -R $source $dest");
170             }
171              
172 3 50       612 if ( $? == 0 ) { return 1; }
  3         123  
173              
174 0 0       0 die("Error copying $source -> $dest") if ( Rex::Config->get_autodie );
175             }
176              
177             sub _normalize_path {
178 272     272   1997 my ( $self, @dirs ) = @_;
179              
180 272         648 my @ret;
181 272         1341 for my $d (@dirs) {
182 272         548 my @t;
183 272 50       1447 if (Rex::is_ssh) {
184 0         0 @t = Rex::Helper::File::Spec->splitdir($d);
185             }
186             else {
187 272         7675 @t = Rex::Helper::File::Spec->splitdir($d);
188             }
189             push( @ret,
190 272         1485 Rex::Helper::File::Spec->catfile( map { $self->_quotepath($_) } @t ) );
  1114         2792  
191             }
192              
193             # for (@dirs) {
194             # s/ /\\ /g;
195             # }
196              
197 272         1121 return @ret;
198             }
199              
200             sub _quotepath {
201 1114     1114   2517 my ( $self, $p ) = @_;
202 1114         2765 $p =~ s/([\@\$\% ])/\\$1/g;
203              
204 1114         3963 return $p;
205             }
206              
207             sub _exec {
208 98     98   305 my ( $self, $cmd ) = @_;
209 98         1485 my $exec = Rex::Interface::Exec->create;
210 98         716 return $exec->exec($cmd);
211             }
212              
213             1;