This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Typo fix, by John P. Linderman
[perl5.git] / lib / File / Copy.pm
index b1baa20..2726771 100644 (file)
@@ -7,18 +7,23 @@
 
 package File::Copy;
 
+use 5.006;
 use strict;
-use Carp;
-use UNIVERSAL qw(isa);
-use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $Too_Big
-           &copy &syscopy &cp &mv);
+use warnings;
+use File::Spec;
+use Config;
+our(@ISA, @EXPORT, @EXPORT_OK, $VERSION, $Too_Big, $Syscopy_is_copy);
+sub copy;
+sub syscopy;
+sub cp;
+sub mv;
 
 # Note that this module implements only *part* of the API defined by
 # the File/Copy.pm module of the File-Tools-2.0 package.  However, that
 # package has not yet been updated to work with Perl 5.004, and so it
 # would be a Bad Thing for the CPAN module to grab it and replace this
 # module.  Therefore, we set this module's version higher than 2.0.
-$VERSION = '2.02';
+$VERSION = '2.10';
 
 require Exporter;
 @ISA = qw(Exporter);
@@ -27,16 +32,44 @@ require Exporter;
 
 $Too_Big = 1024 * 1024 * 2;
 
-sub _catname { #  Will be replaced by File::Spec when it arrives
+sub croak {
+    require Carp;
+    goto &Carp::croak;
+}
+
+sub carp {
+    require Carp;
+    goto &Carp::carp;
+}
+
+my $macfiles;
+if ($^O eq 'MacOS') {
+       $macfiles = eval { require Mac::MoreFiles };
+       warn 'Mac::MoreFiles could not be loaded; using non-native syscopy'
+               if $@ && $^W;
+}
+
+sub _catname {
     my($from, $to) = @_;
     if (not defined &basename) {
        require File::Basename;
        import  File::Basename 'basename';
     }
-    if ($^O eq 'VMS')  { $to = VMS::Filespec::vmspath($to) . basename($from); }
-    elsif ($^O eq 'MacOS') { $to .= ':' . basename($from); }
-    elsif ($to =~ m|\\|)   { $to .= '\\' . basename($from); }
-    else                   { $to .= '/' . basename($from); }
+
+    if ($^O eq 'MacOS') {
+       # a partial dir name that's valid only in the cwd (e.g. 'tmp')
+       $to = ':' . $to if $to !~ /:/;
+    }
+
+    return File::Spec->catfile($to, basename($from));
+}
+
+# _eq($from, $to) tells whether $from and $to are identical
+# works for strings and references
+sub _eq {
+    return $_[0] == $_[1] if ref $_[0] && ref $_[1];
+    return $_[0] eq $_[1] if !ref $_[0] && !ref $_[1];
+    return "";
 }
 
 sub copy {
@@ -48,20 +81,46 @@ sub copy {
 
     my $from_a_handle = (ref($from)
                         ? (ref($from) eq 'GLOB'
-                           || isa($from, 'GLOB') || isa($from, 'IO::Handle'))
+                           || UNIVERSAL::isa($from, 'GLOB')
+                            || UNIVERSAL::isa($from, 'IO::Handle'))
                         : (ref(\$from) eq 'GLOB'));
     my $to_a_handle =   (ref($to)
                         ? (ref($to) eq 'GLOB'
-                           || isa($to, 'GLOB') || isa($to, 'IO::Handle'))
+                           || UNIVERSAL::isa($to, 'GLOB')
+                            || UNIVERSAL::isa($to, 'IO::Handle'))
                         : (ref(\$to) eq 'GLOB'));
 
+    if (_eq($from, $to)) { # works for references, too
+       carp("'$from' and '$to' are identical (not copied)");
+        # The "copy" was a success as the source and destination contain
+        # the same data.
+        return 1;
+    }
+
+    if ((($Config{d_symlink} && $Config{d_readlink}) || $Config{d_link}) &&
+       !($^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'vms')) {
+       my @fs = stat($from);
+       if (@fs) {
+           my @ts = stat($to);
+           if (@ts && $fs[0] == $ts[0] && $fs[1] == $ts[1]) {
+               carp("'$from' and '$to' are identical (not copied)");
+                return 0;
+           }
+       }
+    }
+
     if (!$from_a_handle && !$to_a_handle && -d $to && ! -d $from) {
        $to = _catname($from, $to);
     }
 
-    if (defined &syscopy && \&syscopy != \&copy
+    if (defined &syscopy && !$Syscopy_is_copy
        && !$to_a_handle
-       && !($from_a_handle && $^O eq 'os2'))   # OS/2 cannot handle handles
+       && !($from_a_handle && $^O eq 'os2' )   # OS/2 cannot handle handles
+       && !($from_a_handle && $^O eq 'mpeix')  # and neither can MPE/iX.
+       && !($from_a_handle && $^O eq 'MSWin32')
+       && !($from_a_handle && $^O eq 'MacOS')
+       && !($from_a_handle && $^O eq 'NetWare')
+       )
     {
        return syscopy($from, $to);
     }
@@ -69,32 +128,35 @@ sub copy {
     my $closefrom = 0;
     my $closeto = 0;
     my ($size, $status, $r, $buf);
-    local(*FROM, *TO);
     local($\) = '';
 
+    my $from_h;
     if ($from_a_handle) {
-       *FROM = *$from{FILEHANDLE};
+       $from_h = $from;
     } else {
-       $from = "./$from" if $from =~ /^\s/;
-       open(FROM, "< $from\0") or goto fail_open1;
-       binmode FROM or die "($!,$^E)";
+       $from = _protect($from) if $from =~ /^\s/s;
+       $from_h = \do { local *FH };
+       open($from_h, "< $from\0") or goto fail_open1;
+       binmode $from_h or die "($!,$^E)";
        $closefrom = 1;
-    } 
+    }
+
+    my $to_h;
     if ($to_a_handle) {
-       *TO = *$to{FILEHANDLE};
-    } else {        
-       $to = "./$to" if $to =~ /^\s/;
-       open(TO,"> $to\0") or goto fail_open2;
-       binmode TO or die "($!,$^E)";
+       $to_h = $to;
+    } else {
+       $to = _protect($to) if $to =~ /^\s/s;
+       $to_h = \do { local *FH };
+       open($to_h,"> $to\0") or goto fail_open2;
+       binmode $to_h or die "($!,$^E)";
        $closeto = 1;
-    }  
+    }
 
     if (@_) {
        $size = shift(@_) + 0;
        croak("Bad buffer size for copy: $size\n") unless ($size > 0);
     } else {
-       $size = -s FROM;
+       $size = tied(*$from_h) ? 0 : -s $from_h || 0;
        $size = 1024 if ($size < 512);
        $size = $Too_Big if ($size > $Too_Big);
     }
@@ -102,34 +164,34 @@ sub copy {
     $! = 0;
     for (;;) {
        my ($r, $w, $t);
-       defined($r = sysread(FROM, $buf, $size))
+       defined($r = sysread($from_h, $buf, $size))
            or goto fail_inner;
        last unless $r;
        for ($w = 0; $w < $r; $w += $t) {
-           $t = syswrite(TO, $buf, $r - $w, $w)
+           $t = syswrite($to_h, $buf, $r - $w, $w)
                or goto fail_inner;
        }
     }
 
-    close(TO) || goto fail_open2 if $closeto;
-    close(FROM) || goto fail_open1 if $closefrom;
+    close($to_h) || goto fail_open2 if $closeto;
+    close($from_h) || goto fail_open1 if $closefrom;
 
     # Use this idiom to avoid uninitialized value warning.
     return 1;
-    
+
     # All of these contortions try to preserve error messages...
   fail_inner:
     if ($closeto) {
        $status = $!;
        $! = 0;
-       close TO;
+       close $to_h;
        $! = $status unless $!;
     }
   fail_open2:
     if ($closefrom) {
        $status = $!;
        $! = 0;
-       close FROM;
+       close $from_h;
        $! = $status unless $!;
     }
   fail_open1:
@@ -137,8 +199,11 @@ sub copy {
 }
 
 sub move {
+    croak("Usage: move(FROM, TO) ") unless @_ == 2;
+
     my($from,$to) = @_;
-    my($copied,$fromsz,$tosz1,$tomt1,$tosz2,$tomt2,$sts,$ossts);
+
+    my($fromsz,$tosz1,$tomt1,$tosz2,$tomt2,$sts,$ossts);
 
     if (-d $to && ! -d $from) {
        $to = _catname($from, $to);
@@ -152,17 +217,28 @@ sub move {
     }
     return 1 if rename $from, $to;
 
-    ($sts,$ossts) = ($! + 0, $^E + 0);
     # Did rename return an error even though it succeeded, because $to
     # is on a remote NFS file system, and NFS lost the server's ack?
     return 1 if defined($fromsz) && !-e $from &&           # $from disappeared
                 (($tosz2,$tomt2) = (stat($to))[7,9]) &&    # $to's there
                 ($tosz1 != $tosz2 or $tomt1 != $tomt2) &&  #   and changed
                 $tosz2 == $fromsz;                         # it's all there
+
     ($tosz1,$tomt1) = (stat($to))[7,9];  # just in case rename did something
-    return 1 if ($copied = copy($from,$to)) && unlink($from);
-  
+
+    {
+        local $@;
+        eval {
+            local $SIG{__DIE__};
+            copy($from,$to) or die;
+            my($atime, $mtime) = (stat($from))[8,9];
+            utime($atime, $mtime, $to);
+            unlink($from)   or die;
+        };
+        return 1 unless $@;
+    }
+    ($sts,$ossts) = ($! + 0, $^E + 0);
+
     ($tosz2,$tomt2) = ((stat($to))[7,9],0,0) if defined $tomt1;
     unlink($to) if !defined($tomt1) or $tomt1 != $tomt2 or $tosz1 != $tosz2;
     ($!,$^E) = ($sts,$ossts);
@@ -172,8 +248,51 @@ sub move {
 *cp = \&copy;
 *mv = \&move;
 
+
+if ($^O eq 'MacOS') {
+    *_protect = sub { MacPerl::MakeFSSpec($_[0]) };
+} else {
+    *_protect = sub { "./$_[0]" };
+}
+
 # &syscopy is an XSUB under OS/2
-*syscopy = ($^O eq 'VMS' ? \&rmscopy : \&copy) unless defined &syscopy;
+unless (defined &syscopy) {
+    if ($^O eq 'VMS') {
+       *syscopy = \&rmscopy;
+    } elsif ($^O eq 'mpeix') {
+       *syscopy = sub {
+           return 0 unless @_ == 2;
+           # Use the MPE cp program in order to
+           # preserve MPE file attributes.
+           return system('/bin/cp', '-f', $_[0], $_[1]) == 0;
+       };
+    } elsif ($^O eq 'MSWin32' && defined &DynaLoader::boot_DynaLoader) {
+       # Win32::CopyFile() fill only work if we can load Win32.xs
+       *syscopy = sub {
+           return 0 unless @_ == 2;
+           return Win32::CopyFile(@_, 1);
+       };
+    } elsif ($macfiles) {
+       *syscopy = sub {
+           my($from, $to) = @_;
+           my($dir, $toname);
+
+           return 0 unless -e $from;
+
+           if ($to =~ /(.*:)([^:]+):?$/) {
+               ($dir, $toname) = ($1, $2);
+           } else {
+               ($dir, $toname) = (":", $to);
+           }
+
+           unlink($to);
+           Mac::MoreFiles::FSpFileCopy($from, $dir, $toname, 1);
+       };
+    } else {
+       $Syscopy_is_copy = 1;
+       *syscopy = \&copy;
+    }
+}
 
 1;
 
@@ -185,17 +304,16 @@ File::Copy - Copy files or filehandles
 
 =head1 SYNOPSIS
 
-       use File::Copy;
+       use File::Copy;
 
-       copy("file1","file2");
-       copy("Copy.pm",\*STDOUT);'
+       copy("file1","file2") or die "Copy failed: $!";
+       copy("Copy.pm",\*STDOUT);
        move("/dev1/fileA","/dev2/fileB");
 
-       use POSIX;
-       use File::Copy cp;
+       use File::Copy "cp";
 
-       $n=FileHandle->new("/dev/null","r");
-       cp($n,"x");'
+       $n = FileHandle->new("/a/file","r");
+       cp($n,"x");
 
 =head1 DESCRIPTION
 
@@ -205,7 +323,8 @@ one place to another.
 
 =over 4
 
-=item *
+=item copy
+X<copy> X<cp>
 
 The C<copy> function takes two
 parameters: a file to copy from and a file to copy to. Either
@@ -213,18 +332,19 @@ argument may be a string, a FileHandle reference or a FileHandle
 glob. Obviously, if the first argument is a filehandle of some
 sort, it will be read from, and if it is a file I<name> it will
 be opened for reading. Likewise, the second argument will be
-written to (and created if need be).
+written to (and created if need be).  Trying to copy a file on top
+of itself is a fatal error.
 
 B<Note that passing in
 files as handles instead of names may lead to loss of information
 on some operating systems; it is recommended that you use file
 names whenever possible.>  Files are opened in binary mode where
-applicable.  To get a consistent behavour when copying from a
+applicable.  To get a consistent behaviour when copying from a
 filehandle to a file, use C<binmode> on the filehandle.
 
 An optional third parameter can be used to specify the buffer
 size used for copying. This is the number of bytes from the
-first file, that wil be held in memory at any given time, before
+first file, that will be held in memory at any given time, before
 being written to the second file. The default buffer size depends
 upon the file, but will generally be the whole file (up to 2Mb), or
 1k for filehandles that do not reference files (eg. sockets).
@@ -232,7 +352,8 @@ upon the file, but will generally be the whole file (up to 2Mb), or
 You may use the syntax C<use File::Copy "cp"> to get at the
 "cp" alias for this function. The syntax is I<exactly> the same.
 
-=item *
+=item move
+X<move> X<mv> X<rename>
 
 The C<move> function also takes two parameters: the current name
 and the intended name of the file to be moved.  If the destination
@@ -248,17 +369,22 @@ copy of the file under the destination name.
 You may use the "mv" alias for this function in the same way that
 you may use the "cp" alias for C<copy>.
 
-=back
+=item syscopy
+X<syscopy>
 
 File::Copy also provides the C<syscopy> routine, which copies the
 file specified in the first parameter to the file specified in the
 second parameter, preserving OS-specific attributes and file
 structure.  For Unix systems, this is equivalent to the simple
-C<copy> routine.  For VMS systems, this calls the C<rmscopy>
-routine (see below).  For OS/2 systems, this calls the C<syscopy>
-XSUB directly.
+C<copy> routine, which doesn't preserve OS-specific attributes.  For
+VMS systems, this calls the C<rmscopy> routine (see below).  For OS/2
+systems, this calls the C<syscopy> XSUB directly. For Win32 systems,
+this calls C<Win32::CopyFile>.
+
+On Mac OS (Classic), C<syscopy> calls C<Mac::MoreFiles::FSpFileCopy>,
+if available.
 
-=head2 Special behavior if C<syscopy> is defined (VMS and OS/2)
+B<Special behaviour if C<syscopy> is defined (OS/2, VMS and Win32)>
 
 If both arguments to C<copy> are not file handles,
 then C<copy> will perform a "system copy" of
@@ -273,9 +399,8 @@ The system copy routine may also be called directly under VMS and OS/2
 as C<File::Copy::syscopy> (or under VMS as C<File::Copy::rmscopy>, which
 is the routine that does the actual work for syscopy).
 
-=over 4
-
 =item rmscopy($from,$to[,$date_flag])
+X<rmscopy>
 
 The first and second arguments may be strings, typeglobs, typeglob
 references, or objects inheriting from IO::Handle;
@@ -317,10 +442,38 @@ it sets C<$!>, deletes the output file, and returns 0.
 All functions return 1 on success, 0 on failure.
 $! will be set if an error was encountered.
 
+=head1 NOTES
+
+=over 4
+
+=item *
+
+On Mac OS (Classic), the path separator is ':', not '/', and the 
+current directory is denoted as ':', not '.'. You should be careful 
+about specifying relative pathnames. While a full path always begins 
+with a volume name, a relative pathname should always begin with a 
+':'.  If specifying a volume name only, a trailing ':' is required.
+
+E.g.
+
+  copy("file1", "tmp");        # creates the file 'tmp' in the current directory
+  copy("file1", ":tmp:");      # creates :tmp:file1
+  copy("file1", ":tmp");       # same as above
+  copy("file1", "tmp");        # same as above, if 'tmp' is a directory (but don't do
+                               # that, since it may cause confusion, see example #1)
+  copy("file1", "tmp:file1");  # error, since 'tmp:' is not a volume
+  copy("file1", ":tmp:file1"); # ok, partial path
+  copy("file1", "DataHD:");    # creates DataHD:file1
+
+  move("MacintoshHD:fileA", "DataHD:fileB"); # moves (don't copies) files from one
+                                             # volume to another
+
+=back
+
 =head1 AUTHOR
 
 File::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995,
-and updated by Charles Bailey I<E<lt>bailey@genetics.upenn.eduE<gt>> in 1996.
+and updated by Charles Bailey I<E<lt>bailey@newman.upenn.eduE<gt>> in 1996.
 
 =cut