Subject: | PATCH: support FTP methods dir, mdtm, delete and rename |
Hi Ivan,
Nifty little module. Saved me quite a bit of time.
Please consider the enhanced version of SCP.pm . Basically, I had an
FTP-based script that worked pretty well and I needed to add a secure
option, so I added code for the FTP methods dir, mdtm, delete and
rename.
What I didn't add is an ASCII transfer, once I realised I'd need to
figure
out what the local and remote systems were and ran out of time.
However,
I'd say those would be a good feature.
(NOTE: patch is reversed, read/apply backwards)
Subject: | SCP-diff.txt |
--- Net/SCP.pm 2003-09-22 12:53:17.000000000 +1000
+++ /usr/lib/perl5/site_perl/5.8.0/Net/SCP.pm 2002-03-16 12:00:10.000000000 +1000
@@ -9,7 +9,6 @@
use IO::Handle;
use Net::SSH qw(sshopen3);
use IPC::Open3;
-use Time::Local;
@ISA = qw(Exporter);
@EXPORT_OK = qw( scp iscp );
@@ -92,7 +91,9 @@
my $pid = open3($writer, $reader, $error, @cmd );
waitpid $pid, 0;
if ( $? >> 8 ) {
- $self->{errstr} = join('', <$error>);
+ my $errstr = join('', <$error>);
+ #chomp(my $errstr = <$error>);
+ $self->{errstr} = $errstr;
0;
} else {
1;
@@ -194,8 +195,6 @@
Uses scp to transfer REMOTE_FILE from the remote host. If a local filename is
omitted, uses the basename of the remote file.
-Returns $local for compatibility with Net::FTP.
-
=cut
sub get {
@@ -204,7 +203,7 @@
$local ||= basename($remote);
my $source = $self->{'host'}. ":$remote";
$source = $self->{'user'}. '@'. $source if $self->{'user'};
- (scp($source,$local) ? $local : undef);
+ scp($source,$local);
}
=item mkdir DIRECTORY
@@ -237,44 +236,6 @@
1;
}
-=item dir [FILE]
-
-Returns the long-format file listing from the remote server, from which all the
-file attributes can be obtained.
-Returns an array of on error, and sets the B<errstr> attribute. In the case
-of an actual zero-length file on the remote server, the special value '0e0' is
-returned, which evaluates to zero when used as a number, but is true.
-
-(Implementation note: An ssh connection is established to the remote machine
-and 'ls -l' is used to get the list.)
-
-=cut
-
-sub dir {
- my($self, $file) = @_;
- $file = $self->{'cwd'}. "/$file" if $self->{'cwd'} && $file !~ /^\//;
- my $host = $self->{'host'};
- $host = $self->{'user'}. '@'. $host if $self->{'user'};
- my($reader, $writer, $error ) =
- ( new IO::Handle, new IO::Handle, new IO::Handle );
- $writer->autoflush(1);
- my $pid =
- sshopen3($host, $writer, $reader, $error, 'ls', '-l ', shell_quote($file) );
- waitpid $pid, 0;
- if ( $? >> 8 ) {
- chomp(my $errstr = <$error>);
- $self->{errstr} = $errstr || "ls exited with status ". $?>>8;
- 0;
- } else {
- my @list;
- for ( <$reader> ) {
- chomp;
- unshift @list, $_;
- }
- @list;
- }
-}
-
=item size FILE
Returns the size in bytes for the given file as stored on the remote server.
@@ -295,6 +256,7 @@
my($reader, $writer, $error ) =
( new IO::Handle, new IO::Handle, new IO::Handle );
$writer->autoflush(1);
+ #sshopen2($host, $reader, $writer, 'wc', '-c ', shell_quote($file) );
my $pid =
sshopen3($host, $writer, $reader, $error, 'wc', '-c ', shell_quote($file) );
waitpid $pid, 0;
@@ -313,109 +275,11 @@
}
}
-=item mdtm FILE
-
-Returns the modification date/time in seconds since the epoch of the given file
-as stored on the remote server.
-Returns 0 on error, and sets the B<errstr> attribute. In the case of an a file
-that was created at zero-seconds on the remote server (yeah, right), the special
-value '0e0' is returned, which evaluates to zero when used as a number, but is
-true.
-
-(Implementation note: calls the dir method and interprets its output.)
-
-=cut
-
-sub mdtm {
- my($self, $file) = @_;
- my $attr = ($self->dir($file))[0] || return undef;
- my %mon = ('Jan', 0, 'Feb', 1, 'Mar', 2, 'Apr', 3, 'May', 4, 'Jun', 5,
- 'Jul', 6, 'Aug', 7, 'Sep', 8, 'Oct', 9, 'Nov', 10, 'Dec', 11);
- # Based on the file age, the line format can be one of the following:
- # -rw-r--r-- 1 psoft dba 3051 Jul 24 14:35 Changes
- # -rw-r--r-- 1 psoft dba 3051 Jul 24 2003 Changes
- my ($mon, $mday, $year) = (split(/\s+/, $attr))[5, 6, 7];
- my ($hour, $min);
- if ( $year =~ /(\d\d):(\d\d)/ ) {
- $hour = $1;
- $min = $2;
- $year = (localtime)[5];
- } else {
- $hour = 0;
- $min = 0;
- }
- timelocal(0,$min,$hour,$mday,$mon{$mon},$year);
-}
-
-=item delete FILE
-
-Deletes the remote file.
-Returns 0 on error, and sets the B<errstr> attribute.
-
-(Implementation note: An ssh connection is established to the remote machine
-and 'rm -f' is used to delete the file.)
-
-=cut
-
-sub delete {
- my($self, $file) = @_;
- $file = $self->{'cwd'}. "/$file" if $self->{'cwd'} && $file !~ /^\//;
- my $host = $self->{'host'};
- $host = $self->{'user'}. '@'. $host if $self->{'user'};
- my($reader, $writer, $error ) =
- ( new IO::Handle, new IO::Handle, new IO::Handle );
- $writer->autoflush(1);
- my $pid =
- sshopen3($host, $writer, $reader, $error, 'rm', '-f ', shell_quote($file) );
- waitpid $pid, 0;
- if ( $? >> 8 ) {
- chomp(my $errstr = <$error>);
- $self->{errstr} = $errstr || "rm exited with status ". $?>>8;
- 0;
- } else {
- 1;
- }
-}
-
-=item rename FILE NEWNAME
-
-Renames the remote file to a new name (or moves it).
-Returns 0 on error, and sets the B<errstr> attribute.
-
-(Implementation note: An ssh connection is established to the remote machine
-and 'mv -f' is used to delete the file.)
-
-=cut
-
-sub rename {
- my($self, $file, $newname) = @_;
- $file = $self->{'cwd'}. "/$file" if $self->{'cwd'} && $file !~ /^\//;
- $newname = $self->{'cwd'}. "/$newname" if $self->{'cwd'} && $newname !~ /^\//;
- my $host = $self->{'host'};
- $host = $self->{'user'}. '@'. $host if $self->{'user'};
- my($reader, $writer, $error ) =
- ( new IO::Handle, new IO::Handle, new IO::Handle );
- $writer->autoflush(1);
- my $pid =
- sshopen3($host, $writer, $reader, $error, 'mv', '-f ', shell_quote($file),
- shell_quote($newname) );
- waitpid $pid, 0;
- if ( $? >> 8 ) {
- chomp(my $errstr = <$error>);
- $self->{errstr} = $errstr || "mv exited with status ". $?>>8;
- 0;
- } else {
- 1;
- }
-}
-
=item put LOCAL_FILE [, REMOTE_FILE]
Uses scp to trasnfer LOCAL_FILE to the remote host. If a remote filename is
omitted, uses the basename of the local file.
-Returns $remote for compatibility with Net::FTP.
-
=cut
sub put {
@@ -425,7 +289,7 @@
my $dest = $self->{'host'}. ":$remote";
$dest = $self->{'user'}. '@'. $dest if $self->{'user'};
warn "scp $local $dest\n";
- (scp($local, $dest) ? $remote : undef);
+ scp($local, $dest);
}
=item binary