From: arbor@al37al08.telecel.pt Date: Tue, 16 Jan 2001 11:43:02 +0000 (+0000) Subject: Fix for X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=96a91e01636d3050d38ae3373a362c7d47a6647e;p=p5sagit%2Fp5-mst-13.2.git Fix for Subject: [ID 20010116.001] File::Copy truncates orig file Message-Id: <200101161143.AA11184@al37al08.telecel.pt> (copy($foo, $foo) would truncate $foo) p4raw-id: //depot/perl@11526 --- diff --git a/lib/File/Copy.pm b/lib/File/Copy.pm index fb57a9e..0a6ea8b 100644 --- a/lib/File/Copy.pm +++ b/lib/File/Copy.pm @@ -12,6 +12,7 @@ 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; @@ -65,6 +66,21 @@ sub copy { || UNIVERSAL::isa($to, 'IO::Handle')) : (ref(\$to) eq 'GLOB')); + if ($from eq $to) { # works for references, too + croak("'$from' and '$to' are identical (not copied)"); + } + + if ($Config{d_symlink} && $Config{d_readlink} && + !($^O eq 'Win32' || $^O eq 'os2' || $^O eq 'vms')) { + if (-l $from || -l $to) { + my @fs = stat($from); + my @ts = stat($to); + if ($fs[0] == $ts[0] && $fs[1] == $ts[1]) { + croak("'$from' and '$to' are identical (not copied)"); + } + } + } + if (!$from_a_handle && !$to_a_handle && -d $to && ! -d $from) { $to = _catname($from, $to); } @@ -275,7 +291,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 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