| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | |||
| 5 | package Rex::Interface::Fs::OpenSSH; | ||
| 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::Interface::Exec; | ||
| 14 | use Rex::Interface::Fs::SSH; | ||
| 15 | |||
| 16 | BEGIN { | ||
| 17 | use Rex::Require; | ||
| 18 | Net::SFTP::Foreign::Constants->use(qw(:flags)); | ||
| 19 | } | ||
| 20 | |||
| 21 | use base qw(Rex::Interface::Fs::SSH); | ||
| 22 | |||
| 23 | require Rex::Commands; | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $that = shift; | ||
| 27 | my $proto = ref($that) || $that; | ||
| 28 | my $self = $proto->SUPER::new(@_); | ||
| 29 | |||
| 30 | bless( $self, $proto ); | ||
| 31 | |||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub ls { | ||
| 36 | my ( $self, $path ) = @_; | ||
| 37 | |||
| 38 | my @ret; | ||
| 39 | |||
| 40 | Rex::Commands::profiler()->start("ls: $path"); | ||
| 41 | eval { | ||
| 42 | |||
| 43 | my $sftp = Rex::get_sftp(); | ||
| 44 | my $ls = $sftp->ls($path); | ||
| 45 | |||
| 46 | for my $entry ( @{$ls} ) { | ||
| 47 | push @ret, $entry->{'filename'}; | ||
| 48 | } | ||
| 49 | }; | ||
| 50 | Rex::Commands::profiler()->end("ls: $path"); | ||
| 51 | |||
| 52 | # failed open directory, return undef | ||
| 53 | die "Error listing directory content ($path)" | ||
| 54 | if ( $@ && Rex::Config->get_autodie ); | ||
| 55 | if ($@) { return; } | ||
| 56 | |||
| 57 | # return directory content | ||
| 58 | return @ret; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub is_dir { | ||
| 62 | my ( $self, $path ) = @_; | ||
| 63 | |||
| 64 | Rex::Commands::profiler()->start("is_dir: $path"); | ||
| 65 | |||
| 66 | my $sftp = Rex::get_sftp(); | ||
| 67 | my $attr = $sftp->stat($path); | ||
| 68 | |||
| 69 | Rex::Commands::profiler()->end("is_dir: $path"); | ||
| 70 | |||
| 71 | defined $attr | ||
| 72 | ? return Rex::Helper::File::Stat->S_ISDIR( $attr->perm ) | ||
| 73 | : return undef; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub is_file { | ||
| 77 | my ( $self, $file ) = @_; | ||
| 78 | |||
| 79 | Rex::Commands::profiler()->start("is_file: $file"); | ||
| 80 | |||
| 81 | my $sftp = Rex::get_sftp(); | ||
| 82 | my $attr = $sftp->stat($file); | ||
| 83 | |||
| 84 | Rex::Commands::profiler()->end("is_file: $file"); | ||
| 85 | |||
| 86 | defined $attr | ||
| 87 | ? return ( Rex::Helper::File::Stat->S_ISREG( $attr->perm ) | ||
| 88 | || Rex::Helper::File::Stat->S_ISLNK( $attr->perm ) | ||
| 89 | || Rex::Helper::File::Stat->S_ISBLK( $attr->perm ) | ||
| 90 | || Rex::Helper::File::Stat->S_ISCHR( $attr->perm ) | ||
| 91 | || Rex::Helper::File::Stat->S_ISFIFO( $attr->perm ) | ||
| 92 | || Rex::Helper::File::Stat->S_ISSOCK( $attr->perm ) ) | ||
| 93 | : return undef; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub unlink { | ||
| 97 | my ( $self, @files ) = @_; | ||
| 98 | |||
| 99 | my $sftp = Rex::get_sftp(); | ||
| 100 | for my $file (@files) { | ||
| 101 | Rex::Commands::profiler()->start("unlink: $file"); | ||
| 102 | eval { | ||
| 103 | $sftp->remove($file); | ||
| 104 | 1; | ||
| 105 | } or do { | ||
| 106 | die "Error unlinking file: $file." if ( Rex::Config->get_autodie ); | ||
| 107 | }; | ||
| 108 | Rex::Commands::profiler()->end("unlink: $file"); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub stat { | ||
| 113 | my ( $self, $file ) = @_; | ||
| 114 | |||
| 115 | Rex::Commands::profiler()->start("stat: $file"); | ||
| 116 | |||
| 117 | my $sftp = Rex::get_sftp(); | ||
| 118 | my $ret = $sftp->stat($file); | ||
| 119 | |||
| 120 | if ( !$ret ) { return undef; } | ||
| 121 | |||
| 122 | my %ret = ( | ||
| 123 | mode => sprintf( "%04o", Rex::Helper::File::Stat->S_IMODE( $ret->perm ) ), | ||
| 124 | size => $ret->size, | ||
| 125 | uid => $ret->uid, | ||
| 126 | gid => $ret->gid, | ||
| 127 | atime => $ret->atime, | ||
| 128 | mtime => $ret->mtime, | ||
| 129 | ); | ||
| 130 | |||
| 131 | Rex::Commands::profiler()->end("stat: $file"); | ||
| 132 | |||
| 133 | return %ret; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub upload { | ||
| 137 | my ( $self, $source, $target ) = @_; | ||
| 138 | |||
| 139 | Rex::Commands::profiler()->start("upload: $source -> $target"); | ||
| 140 | |||
| 141 | my $sftp = Rex::get_sftp(); | ||
| 142 | unless ( $sftp->put( $source, $target ) ) { | ||
| 143 | Rex::Logger::debug("upload: $target is not writable"); | ||
| 144 | |||
| 145 | Rex::Commands::profiler()->end("upload: $source -> $target"); | ||
| 146 | |||
| 147 | die("upload: $target is not writable."); | ||
| 148 | } | ||
| 149 | |||
| 150 | Rex::Commands::profiler()->end("upload: $source -> $target"); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub download { | ||
| 154 | my ( $self, $source, $target ) = @_; | ||
| 155 | |||
| 156 | Rex::Commands::profiler()->start("download: $source -> $target"); | ||
| 157 | |||
| 158 | my $sftp = Rex::get_sftp(); | ||
| 159 | if ( !$sftp->get( $source, $target ) ) { | ||
| 160 | Rex::Commands::profiler()->end("download: $source -> $target"); | ||
| 161 | die( $sftp->error ); | ||
| 162 | } | ||
| 163 | |||
| 164 | Rex::Commands::profiler()->end("download: $source -> $target"); | ||
| 165 | } | ||
| 166 | |||
| 167 | 1; |