Fix breakages that prevended -DPERL_POISON from compiling.
[p5sagit/p5-mst-13.2.git] / lib / File / Copy.pm
index fb57a9e..57670e1 100644 (file)
@@ -7,11 +7,11 @@
 
 package File::Copy;
 
-use 5.6.0;
+use 5.006;
 use strict;
 use warnings;
-use Carp;
 use File::Spec;
+use Config;
 our(@ISA, @EXPORT, @EXPORT_OK, $VERSION, $Too_Big, $Syscopy_is_copy);
 sub copy;
 sub syscopy;
@@ -23,7 +23,7 @@ sub mv;
 # 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.04';
+$VERSION = '2.08_01';
 
 require Exporter;
 @ISA = qw(Exporter);
@@ -32,6 +32,23 @@ require Exporter;
 
 $Too_Big = 1024 * 1024 * 2;
 
+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) {
@@ -65,6 +82,25 @@ sub copy {
                             || UNIVERSAL::isa($to, 'IO::Handle'))
                         : (ref(\$to) eq 'GLOB'));
 
+    if ($from eq $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);
     }
@@ -112,8 +148,7 @@ sub copy {
        $size = shift(@_) + 0;
        croak("Bad buffer size for copy: $size\n") unless ($size > 0);
     } else {
-       no warnings 'uninitialized';
-       $size = -s $from_h;
+       $size = tied(*$from_h) ? 0 : -s $from_h || 0;
        $size = 1024 if ($size < 512);
        $size = $Too_Big if ($size > $Too_Big);
     }
@@ -156,8 +191,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);
@@ -171,7 +209,6 @@ 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
@@ -180,7 +217,19 @@ sub move {
                 $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;
@@ -214,8 +263,7 @@ unless (defined &syscopy) {
            return 0 unless @_ == 2;
            return Win32::CopyFile(@_, 1);
        };
-    } elsif ($^O eq 'MacOS') {
-       require Mac::MoreFiles;
+    } elsif ($macfiles) {
        *syscopy = sub {
            my($from, $to) = @_;
            my($dir, $toname);
@@ -247,17 +295,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("/a/file","r");
-       cp($n,"x");'
+       cp($n,"x");
 
 =head1 DESCRIPTION
 
@@ -275,7 +322,8 @@ 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
@@ -316,9 +364,13 @@ 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. For Win32 systems, this calls C<Win32::CopyFile>.
+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 behaviour if C<syscopy> is defined (OS/2, VMS and Win32)