mkdir patch from Ofer
[Net-SCP.git] / SCP.pm
diff --git a/SCP.pm b/SCP.pm
index 2b9d4d4..2b19b7e 100644 (file)
--- a/SCP.pm
+++ b/SCP.pm
@@ -1,7 +1,7 @@
 package Net::SCP;
 
 use strict;
-use vars qw($VERSION @ISA @EXPORT_OK $scp);
+use vars qw($VERSION @ISA @EXPORT_OK $scp $DEBUG);
 use Exporter;
 use Carp;
 use File::Basename;
@@ -12,10 +12,12 @@ use IPC::Open3;
 
 @ISA = qw(Exporter);
 @EXPORT_OK = qw( scp iscp );
-$VERSION = '0.04';
+$VERSION = '0.08';
 
 $scp = "scp";
 
+$DEBUG = 0;
+
 =head1 NAME
 
 Net::SCP - Perl extension for secure copy protocol
@@ -203,7 +205,37 @@ sub get {
   $local ||= basename($remote);
   my $source = $self->{'host'}. ":$remote";
   $source = $self->{'user'}. '@'. $source if $self->{'user'};
-  scp($source,$local);
+  $self->scp($source,$local);
+}
+
+=item mkdir DIRECTORY
+
+Makes a directory on the remote server.  Returns false and sets the B<errstr>
+attribute on errors.
+
+(Implementation note: An ssh connection is established to the remote machine
+and '/bin/mkdir B<-p>' is used to create the directory.)
+
+=cut
+
+sub mkdir {
+  my($self, $directory) = @_;
+  $directory = $self->{'cwd'}. "/$directory"
+    if $self->{'cwd'} && $directory !~ /^\//;
+  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,
+                      '/bin/mkdir', '-p ', shell_quote($directory) );
+  waitpid $pid, 0;
+  if ( $? >> 8 ) {
+    chomp(my $errstr = <$error> || '');
+    $self->{errstr} = $errstr || "mkdir exited with status ". ($?>>8);
+    return 0;
+  }
+  1;
 }
 
 =item size FILE
@@ -225,7 +257,7 @@ sub size {
   $host = $self->{'user'}. '@'. $host if $self->{'user'};
   my($reader, $writer, $error ) =
     ( new IO::Handle, new IO::Handle, new IO::Handle );
-  $writer->autoflush(1);#  $error->autoflush(1);
+  $writer->autoflush(1);
   #sshopen2($host, $reader, $writer, 'wc', '-c ', shell_quote($file) );
   my $pid =
     sshopen3($host, $writer, $reader, $error, 'wc', '-c ', shell_quote($file) );
@@ -258,8 +290,8 @@ sub put {
   $remote = $self->{'cwd'}. "/$remote" if $self->{'cwd'} && $remote !~ /^\//;
   my $dest = $self->{'host'}. ":$remote";
   $dest = $self->{'user'}. '@'. $dest if $self->{'user'};
-  warn "scp $local $dest\n";
-  scp($local, $dest);
+  warn "scp $local $dest\n" if $DEBUG;
+  $self->scp($local, $dest);
 }
 
 =item binary
@@ -272,13 +304,28 @@ sub binary { 1; }
 
 =back
 
+=head1 FREQUENTLY ASKED QUESTIONS
+
+Q: How do you supply a password to connect with ssh within a perl script
+using the Net::SSH module?
+
+A: You don't.  Use RSA or DSA keys.  See the ssh-keygen(1) manpage.
+
+Q: My script is "leaking" ssh processes.
+
+A: See L<perlfaq8/"How do I avoid zombies on a Unix system">, L<IPC::Open2>,
+L<IPC::Open3> and L<perlfunc/waitpid>.
+
 =head1 AUTHORS
 
 Ivan Kohler <ivan-netscp_pod@420.am>
-Anthony Deaver <bishop@projectmagnus.org>
+
+Major updates Anthony Deaver <bishop@projectmagnus.org>
 
 Thanks to Jon Gunnip <jon@soundbite.com> for fixing a bug with size().
 
+Patch for the mkdir method by Anthony Awtrey <tony@awtrey.com>
+
 =head1 COPYRIGHT
 
 Copyright (c) 2000 Ivan Kohler.