Skip to content

Commit

Permalink
Fix for
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arbor@al37al08.telecel.pt authored and jhi committed Jul 31, 2001
1 parent dd2de24 commit 96a91e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lib/File/Copy.pm
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<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
Expand Down
12 changes: 11 additions & 1 deletion lib/File/Copy.t
Expand Up @@ -9,7 +9,7 @@ BEGIN {
$| = 1;

my @pass = (0,1);
my $tests = $^O eq 'MacOS' ? 14 : 11;
my $tests = $^O eq 'MacOS' ? 15 : 12;
printf "1..%d\n", $tests * scalar(@pass);

use File::Copy;
Expand Down Expand Up @@ -116,6 +116,11 @@ for my $pass (@pass) {
print "not " unless $foo eq sprintf("ok %d\n", 3+$loopconst)
and not -e "file-$$";;
printf "ok %d\n", 14+$loopconst;

eval { copy("copy-$$", "copy-$$") };
printf "ok %d\n", 15+$loopconst
unless $@ =~ /are identical/ && -s "copy-$$";

unlink ":lib:file-$$" or die "unlink: $!";

} else {
Expand All @@ -131,6 +136,11 @@ for my $pass (@pass) {
print "not " unless $foo eq sprintf("ok %d\n", 3+$loopconst)
and not -e "file-$$";;
printf "ok %d\n", 11+$loopconst;

eval { copy("copy-$$", "copy-$$") };
printf "ok %d\n", 12+$loopconst
unless $@ =~ /are identical/ && -s "copy-$$";

unlink "lib/file-$$" or die "unlink: $!";

}
Expand Down

0 comments on commit 96a91e0

Please sign in to comment.