-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathOpenSSH.pm
More file actions
189 lines (135 loc) · 4.1 KB
/
OpenSSH.pm
File metadata and controls
189 lines (135 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
package Rex::Interface::Fs::OpenSSH;
use 5.010001;
use strict;
use warnings;
our $VERSION = '9999.99.99_99'; # VERSION
use Rex::Helper::File::Stat;
use Rex::Interface::Exec;
use Rex::Interface::Fs::SSH;
BEGIN {
use Rex::Require;
Net::SFTP::Foreign::Constants->use(qw(:flags));
}
use base qw(Rex::Interface::Fs::SSH);
require Rex::Commands;
sub new {
my $that = shift;
my $proto = ref($that) || $that;
my $self = $proto->SUPER::new(@_);
bless( $self, $proto );
return $self;
}
sub ls {
my ( $self, $path ) = @_;
my @ret;
Rex::Commands::profiler()->start("ls: $path");
eval {
my $sftp = Rex::get_sftp();
my $ls = $sftp->ls($path);
for my $entry ( @{$ls} ) {
push @ret, $entry->{'filename'};
}
};
Rex::Commands::profiler()->end("ls: $path");
# failed open directory, return undef
die "Error listing directory content ($path)"
if ( $@ && Rex::Config->get_autodie );
if ($@) { return; }
# return directory content
return @ret;
}
sub is_dir {
my ( $self, $path ) = @_;
Rex::Commands::profiler()->start("is_dir: $path");
my $sftp = Rex::get_sftp();
my $attr = $sftp->stat($path);
Rex::Commands::profiler()->end("is_dir: $path");
defined $attr
? return Rex::Helper::File::Stat->S_ISDIR( $attr->perm )
: return undef;
}
sub is_file {
my ( $self, $file ) = @_;
Rex::Commands::profiler()->start("is_file: $file");
my $sftp = Rex::get_sftp();
my $attr = $sftp->stat($file);
Rex::Commands::profiler()->end("is_file: $file");
defined $attr
? return ( Rex::Helper::File::Stat->S_ISREG( $attr->perm )
|| Rex::Helper::File::Stat->S_ISLNK( $attr->perm )
|| Rex::Helper::File::Stat->S_ISBLK( $attr->perm )
|| Rex::Helper::File::Stat->S_ISCHR( $attr->perm )
|| Rex::Helper::File::Stat->S_ISFIFO( $attr->perm )
|| Rex::Helper::File::Stat->S_ISSOCK( $attr->perm ) )
: return undef;
}
sub unlink {
my ( $self, @files ) = @_;
my $sftp = Rex::get_sftp();
for my $file (@files) {
Rex::Commands::profiler()->start("unlink: $file");
eval {
$sftp->remove($file);
1;
} or do {
die "Error unlinking file: $file." if ( Rex::Config->get_autodie );
};
Rex::Commands::profiler()->end("unlink: $file");
}
}
sub stat {
my ( $self, $file ) = @_;
Rex::Commands::profiler()->start("stat: $file");
my $sftp = Rex::get_sftp();
my $ret = $sftp->stat($file);
if ( !$ret ) { return undef; }
my %ret = (
mode => sprintf( "%04o", Rex::Helper::File::Stat->S_IMODE( $ret->perm ) ),
size => $ret->size,
uid => $ret->uid,
gid => $ret->gid,
atime => $ret->atime,
mtime => $ret->mtime,
);
Rex::Commands::profiler()->end("stat: $file");
return %ret;
}
=pod
=head3 OpenSSH->upload($source, $target)
Uploads an item from source to target.
If sudo is enabled for the connection will set permissions etc too.
Otherwise will only attempt to set permissions, and continue successfully
if not.
=cut
sub upload {
my ( $self, $source, $target ) = @_;
Rex::Commands::profiler()->start("upload: $source -> $target");
my $sftp = Rex::get_sftp();
# If we are not sudo, we need to fall back to best effort on
# file upload. The %best_effort hash will evaluate be expanded
# to best_effort => 1 if and and only if is_sudo returns false.
my %best_effort = ();
$best_effort{best_effort} = 1 unless Rex::is_sudo();
unless ( $sftp->put( $source, $target, %best_effort) ) {
Rex::Logger::debug("upload: $target is not writable");
Rex::Commands::profiler()->end("upload: $source -> $target");
die("upload: $target is not writable.");
}
Rex::Commands::profiler()->end("upload: $source -> $target");
}
sub download {
my ( $self, $source, $target ) = @_;
Rex::Commands::profiler()->start("download: $source -> $target");
my $sftp = Rex::get_sftp();
if ( !$sftp->get( $source, $target ) ) {
Rex::Commands::profiler()->end("download: $source -> $target");
die( $sftp->error );
}
Rex::Commands::profiler()->end("download: $source -> $target");
}
1;