use warnings;
use File::Spec;
use Config;
+use Fcntl qw [O_CREAT O_WRONLY O_TRUNC];
our(@ISA, @EXPORT, @EXPORT_OK, $VERSION, $Too_Big, $Syscopy_is_copy);
sub copy;
sub syscopy;
if ($from_a_handle) {
$from_h = $from;
} else {
- $from = _protect($from) if $from =~ /^\s/s;
- $from_h = \do { local *FH };
open $from_h, "<", $from or goto fail_open1;
binmode $from_h or die "($!,$^E)";
$closefrom = 1;
$to_h = $to;
} else {
$to = _protect($to) if $to =~ /^\s/s;
- $to_h = \do { local *FH };
- open $to_h, ">", $to or goto fail_open2;
+ my $perm = (stat $from_h) [2] & 0xFFF;
+ sysopen $to_h, $to, O_CREAT | O_TRUNC | O_WRONLY, $perm
+ or goto fail_open2;
binmode $to_h or die "($!,$^E)";
$closeto = 1;
}
*cp = \©
*mv = \&move;
-
-if ($^O eq 'MacOS') {
- *_protect = sub { MacPerl::MakeFSSpec($_[0]) };
-} else {
- *_protect = sub { "./$_[0]" };
-}
-
# &syscopy is an XSUB under OS/2
unless (defined &syscopy) {
if ($^O eq 'VMS') {
}
}
+use strict;
+use warnings;
+
use Test::More;
my $TB = Test::More->builder;
-plan tests => 70;
+plan tests => 91;
# We're going to override rename() later on but Perl has to see an override
# at compile time to honor it.
copy "file-$$", "copy-$$";
open(F, "copy-$$") or die $!;
- $foo = <F>;
+ my $foo = <F>;
close(F);
is -s "file-$$", -s "copy-$$", 'copy(fn, fn): files of the same size';
unlink "copy-$$" or die "unlink: $!";
require IO::File;
- $fh = IO::File->new(">copy-$$") or die "Cannot open copy-$$:$!";
+ my $fh = IO::File->new(">copy-$$") or die "Cannot open copy-$$:$!";
binmode $fh or die $!;
copy("file-$$",$fh);
$fh->close or die "close: $!";
unlink "copy-$$" or die "unlink: $!";
require FileHandle;
- my $fh = FileHandle->new(">copy-$$") or die "Cannot open copy-$$:$!";
+ $fh = FileHandle->new(">copy-$$") or die "Cannot open copy-$$:$!";
binmode $fh or die $!;
copy("file-$$",$fh);
$fh->close;
}
+{
+ # Just a sub to get better failure messages.
+ sub __ ($) {
+ join "" => map {(qw [--- --x -w- -wx r-- r-x rw- rwx]) [$_]}
+ split // => sprintf "%03o" => shift
+ }
+ # Testing permission bits.
+ my $src = "file-$$";
+ my $copy1 = "copy1-$$";
+ my $copy2 = "copy2-$$";
+ my $copy3 = "copy3-$$";
+
+ open my $fh => ">", $src or die $!;
+ close $fh or die $!;
+
+ open $fh => ">", $copy3 or die $!;
+ close $fh or die $!;
+
+ my @tests = (
+ [0000, 0777, 0777, 0777],
+ [0000, 0751, 0751, 0644],
+ [0022, 0777, 0755, 0206],
+ [0022, 0415, 0415, 0666],
+ [0077, 0777, 0700, 0333],
+ [0027, 0755, 0750, 0251],
+ [0777, 0751, 0000, 0215],
+ );
+ my $old_mask = umask;
+ foreach my $test (@tests) {
+ my ($umask, $s_perm, $c_perm1, $c_perm3) = @$test;
+ # Make sure the copies doesn't exist.
+ ! -e $_ or unlink $_ or die $! for $copy1, $copy2;
+
+ (umask $umask) // die $!;
+ chmod $s_perm => $src or die $!;
+ chmod $c_perm3 => $copy3 or die $!;
+
+ open my $fh => "<", $src or die $!;
+
+ copy ($src, $copy1);
+ copy ($fh, $copy2);
+ copy ($src, $copy3);
+
+ my $perm1 = (stat $copy1) [2] & 0xFFF;
+ my $perm2 = (stat $copy2) [2] & 0xFFF;
+ my $perm3 = (stat $copy3) [2] & 0xFFF;
+ is (__$perm1, __$c_perm1, "Permission bits set correctly");
+ is (__$perm2, __$c_perm1, "Permission bits set correctly");
+ is (__$perm3, __$c_perm3, "Permission bits not modified");
+ }
+ umask $old_mask or die $!;
+
+ # Clean up.
+ ! -e $_ or unlink $_ or die $! for $src, $copy1, $copy2, $copy3;
+}
+
+
END {
1 while unlink "file-$$";
1 while unlink "lib/file-$$";