4 if( $ENV{PERL_CORE} ) {
15 my $TB = Test::More->builder;
19 # We're going to override rename() later on but Perl has to see an override
20 # at compile time to honor it.
21 BEGIN { *CORE::GLOBAL::rename = sub { CORE::rename($_[0], $_[1]) }; }
24 use File::Copy qw(copy move cp);
28 foreach my $code ("copy()", "copy('arg')", "copy('arg', 'arg', 'arg', 'arg')",
29 "move()", "move('arg')", "move('arg', 'arg', 'arg')"
33 like $@, qr/^Usage: /, "'$code' is a usage error";
37 for my $cross_partition_test (0..1) {
39 # Simulate a cross-partition copy/move by forcing rename to
41 no warnings 'redefine';
42 *CORE::GLOBAL::rename = sub { 0 } if $cross_partition_test;
45 # First we create a file
46 open(F, ">file-$$") or die $!;
47 binmode F; # for DOSISH platforms, because test 3 copies to stdout
51 copy "file-$$", "copy-$$";
53 open(F, "copy-$$") or die $!;
57 is -s "file-$$", -s "copy-$$", 'copy(fn, fn): files of the same size';
59 is $foo, "ok\n", 'copy(fn, fn): same contents';
61 print("# next test checks copying to STDOUT\n");
62 binmode STDOUT unless $^O eq 'VMS'; # Copy::copy works in binary mode
63 # This outputs "ok" so its a test.
64 copy "copy-$$", \*STDOUT;
65 $TB->current_test($TB->current_test + 1);
66 unlink "copy-$$" or die "unlink: $!";
70 open(R, "copy-$$") or die "open copy-$$: $!"; $foo = <R>; close(R);
71 is $foo, "ok\n", 'copy(*F, fn): same contents';
72 unlink "copy-$$" or die "unlink: $!";
76 close(F) or die "close: $!";
77 open(R, "copy-$$") or die; $foo = <R>; close(R) or die "close: $!";
78 is $foo, "ok\n", 'copy(\*F, fn): same contents';
79 unlink "copy-$$" or die "unlink: $!";
82 my $fh = IO::File->new(">copy-$$") or die "Cannot open copy-$$:$!";
83 binmode $fh or die $!;
85 $fh->close or die "close: $!";
86 open(R, "copy-$$") or die; $foo = <R>; close(R);
87 is $foo, "ok\n", 'copy(fn, io): same contents';
88 unlink "copy-$$" or die "unlink: $!";
91 $fh = FileHandle->new(">copy-$$") or die "Cannot open copy-$$:$!";
92 binmode $fh or die $!;
95 open(R, "copy-$$") or die $!; $foo = <R>; close(R);
96 is $foo, "ok\n", 'copy(fn, fh): same contents';
97 unlink "file-$$" or die "unlink: $!";
99 ok !move("file-$$", "copy-$$"), "move on missing file";
100 ok -e "copy-$$", ' target still there';
102 # Doesn't really matter what time it is as long as its not now.
103 my $time = 1000000000;
104 utime( $time, $time, "copy-$$" );
106 # Recheck the mtime rather than rely on utime in case we're on a
107 # system where utime doesn't work or there's no mtime at all.
108 # The destination file will reflect the same difficulties.
109 my $mtime = (stat("copy-$$"))[9];
111 ok move("copy-$$", "file-$$"), 'move';
112 ok -e "file-$$", ' destination exists';
113 ok !-e "copy-$$", ' source does not';
114 open(R, "file-$$") or die $!; $foo = <R>; close(R);
115 is $foo, "ok\n", 'contents preserved';
118 local $TODO = 'mtime only preserved on ODS-5 with POSIX dates and DECC$EFS_FILE_TIMESTAMPS enabled' if $^O eq 'VMS';
120 my $dest_mtime = (stat("file-$$"))[9];
121 is $dest_mtime, $mtime,
122 "mtime preserved by copy()".
123 ($cross_partition_test ? " while testing cross-partition" : "");
126 # trick: create lib/ if not exists - not needed in Perl core
127 unless (-d 'lib') { mkdir 'lib' or die $!; }
128 copy "file-$$", "lib";
129 open(R, "lib/file-$$") or die $!; $foo = <R>; close(R);
130 is $foo, "ok\n", 'copy(fn, dir): same contents';
131 unlink "lib/file-$$" or die "unlink: $!";
133 # Do it twice to ensure copying over the same file works.
134 copy "file-$$", "lib";
135 open(R, "lib/file-$$") or die $!; $foo = <R>; close(R);
136 is $foo, "ok\n", 'copy over the same file works';
137 unlink "lib/file-$$" or die "unlink: $!";
141 local $SIG{__WARN__} = sub { $warnings .= join '', @_ };
142 ok copy("file-$$", "file-$$"), 'copy(fn, fn) succeeds';
144 like $warnings, qr/are identical/, 'but warns';
145 ok -s "file-$$", 'contents preserved';
148 move "file-$$", "lib";
149 open(R, "lib/file-$$") or die "open lib/file-$$: $!"; $foo = <R>; close(R);
150 is $foo, "ok\n", 'move(fn, dir): same contents';
151 ok !-e "file-$$", 'file moved indeed';
152 unlink "lib/file-$$" or die "unlink: $!";
155 skip "Testing symlinks", 3 unless $Config{d_symlink};
157 open(F, ">file-$$") or die $!;
158 print F "dummy content\n";
160 symlink("file-$$", "symlink-$$") or die $!;
163 local $SIG{__WARN__} = sub { $warnings .= join '', @_ };
164 ok !copy("file-$$", "symlink-$$"), 'copy to itself (via symlink) fails';
166 like $warnings, qr/are identical/, 'emits a warning';
168 'rt.perl.org 5196: copying to itself would truncate the file';
170 unlink "symlink-$$" or die $!;
171 unlink "file-$$" or die $!;
175 skip "Testing hard links", 3
176 if !$Config{d_link} or $^O eq 'MSWin32' or $^O eq 'cygwin';
178 open(F, ">file-$$") or die $!;
179 print F "dummy content\n";
181 link("file-$$", "hardlink-$$") or die $!;
184 local $SIG{__WARN__} = sub { $warnings .= join '', @_ };
185 ok !copy("file-$$", "hardlink-$$"), 'copy to itself (via hardlink) fails';
187 like $warnings, qr/are identical/, 'emits a warning';
189 'rt.perl.org 5196: copying to itself would truncate the file';
191 unlink "hardlink-$$" or die $!;
192 unlink "file-$$" or die $!;
195 open(F, ">file-$$") or die $!;
197 print F "this is file\n";
200 my $copy_msg = "this is copy\n";
201 open(F, ">copy-$$") or die $!;
207 local $SIG{__WARN__} = sub { push @warnings, join '', @_ };
209 # pie-$$ so that we force a non-constant, else the numeric conversion (of 0)
210 # is cached and we don't get a warning the second time round
211 is eval { copy("file-$$", "copy-$$", "pie-$$"); 1 }, undef,
212 "a bad buffer size fails to copy";
213 like $@, qr/Bad buffer size for copy/, "with a helpful error message";
214 unless (is scalar @warnings, 1, "There is 1 warning") {
215 diag $_ foreach @warnings;
218 is -s "copy-$$", length $copy_msg, "but does not truncate the destination";
219 open(F, "copy-$$") or die $!;
222 is $foo, $copy_msg, "nor change the destination's contents";
224 unlink "file-$$" or die $!;
225 unlink "copy-$$" or die $!;
227 # RT #73714 copy to file with leading whitespace failed
229 open(F, ">file-$$") or die $!;
231 copy "file-$$", " copy-$$";
232 ok -e " copy-$$", "copy with leading whitespace";
233 unlink "file-$$" or die "unlink: $!";
234 unlink " copy-$$" or die "unlink: $!";
241 [0000, 0777, 0777, 0777],
242 [0000, 0751, 0751, 0644],
243 [0022, 0777, 0755, 0206],
244 [0022, 0415, 0415, 0666],
245 [0077, 0777, 0700, 0333],
246 [0027, 0755, 0750, 0251],
247 [0777, 0751, 0000, 0215],
250 my $skips = @tests * 6 * 8;
252 # TODO - make this skip fire if we're on a nosuid filesystem rather than guessing by OS
253 skip "OpenBSD filesystems default to nosuid breaking these tests", $skips
255 skip "-- Copy preserves RMS defaults, not POSIX permissions.", $skips
257 skip "Copy doesn't set file permissions correctly on Win32.", $skips
260 # Just a sub to get better failure messages.
263 my $id = 07000 & $perm;
266 my @chunks = map {(qw [--- --x -w- -wx r-- r-x rw- rwx]) [$_]}
267 split // => sprintf "%03o" => $perm;
268 if ($id & 4) {$chunks [0] =~ s/(.)$/$1 eq '-' ? 'S' : 's'/e;}
269 if ($id & 2) {$chunks [1] =~ s/(.)$/$1 eq '-' ? 'S' : 's'/e;}
270 if ($id & 1) {$chunks [2] =~ s/(.)$/$1 eq '-' ? 'T' : 't'/e;}
273 # Testing permission bits.
275 my $copy1 = "copy1-$$";
276 my $copy2 = "copy2-$$";
277 my $copy3 = "copy3-$$";
278 my $copy4 = "copy4-$$";
279 my $copy5 = "copy5-$$";
280 my $copy6 = "copy6-$$";
282 open my $fh => ">", $src or die $!;
285 open $fh => ">", $copy3 or die $!;
288 open $fh => ">", $copy6 or die $!;
291 my $old_mask = umask;
292 foreach my $test (@tests) {
293 foreach my $id (0 .. 7) {
294 my ($umask, $s_perm, $c_perm1, $c_perm3) = @$test;
295 # Make sure the copies doesn't exist.
296 ! -e $_ or unlink $_ or die $! for $copy1, $copy2, $copy4, $copy5;
299 $c_perm1 |= $id << 9;
300 diag(sprintf "Src permission: %04o; umask %03o\n", $s_perm, $umask)
301 unless ($ENV{PERL_CORE});
303 # Test that we can actually set a file to the correct permission.
304 # Slightly convoluted, because some operating systems will let us
305 # set a directory, but not a file. These should all work:
306 mkdir $copy1 or die "Can't mkdir $copy1: $!";
307 chmod $s_perm, $copy1
308 or die sprintf "Can't chmod %o $copy1: $!", $s_perm;
310 or die sprintf "Can't rmdir $copy1: $!";
311 open my $fh0, '>', $copy1 or die "Can't open $copy1: $!";
312 close $fh0 or die "Can't close $copy1: $!";
313 unless (chmod $s_perm, $copy1) {
314 $TB->skip(sprintf "Can't chmod $copy1 to %o: $!", $s_perm)
318 my $perm0 = (stat $copy1) [2] & 07777;
319 unless ($perm0 == $s_perm) {
320 $TB->skip(sprintf "chmod %o $copy1 lies - we actually get %o",
325 unlink $copy1 or die "Can't unlink $copy1: $!";
327 (umask $umask) // die $!;
328 chmod $s_perm => $src or die sprintf "$!: $src => %o", $s_perm;
329 chmod $c_perm3 => $copy3 or die $!;
330 chmod $c_perm3 => $copy6 or die $!;
332 open my $fh => "<", $src or die $!;
341 my $permdef = 0666 & ~$umask;
342 my $perm1 = (stat $copy1) [2] & 07777;
343 my $perm2 = (stat $copy2) [2] & 07777;
344 my $perm3 = (stat $copy3) [2] & 07777;
345 my $perm4 = (stat $copy4) [2] & 07777;
346 my $perm5 = (stat $copy5) [2] & 07777;
347 my $perm6 = (stat $copy6) [2] & 07777;
348 is (__$perm1, __$permdef, "Permission bits set correctly");
349 is (__$perm2, __$permdef, "Permission bits set correctly");
350 is (__$perm4, __$c_perm1, "Permission bits set correctly");
351 is (__$perm5, __$c_perm1, "Permission bits set correctly");
353 local $TODO = 'Permission bits inconsistent under cygwin'
355 is (__$perm3, __$c_perm3, "Permission bits not modified");
356 is (__$perm6, __$c_perm3, "Permission bits not modified");
360 umask $old_mask or die $!;
363 ! -e $_ or unlink $_ or die $! for $src, $copy1, $copy2, $copy3,
364 $copy4, $copy5, $copy6;
369 # a package overloaded suspiciously like IO::Scalar
370 use overload '""' => sub { ${$_[0]} };
371 use overload 'bool' => sub { 1 };
373 my ($class, $name) = @_;
374 bless \$name, $class;
378 # a different package overloaded suspiciously like IO::Scalar
379 use overload '""' => sub { ${$_[0]} };
380 use overload 'bool' => sub { 1 };
382 my ($class, $name) = @_;
383 bless \$name, $class;
387 my $object = Crash->new('whack_eth');
388 my %what = (plain => "$object",
390 object2 => Zowie->new('whack_eth'),
391 object2 => Zowie->new('whack_eth'),
395 local $SIG{__WARN__} = sub {
399 foreach my $left (qw(plain object1 object2)) {
400 foreach my $right (qw(plain object1 object2)) {
403 is eval {copy $what{$left}, $what{$right}}, 1, "copy $left $right";
404 is $@, '', 'No croaking';
405 is $!, '', 'No system call errors';
406 is @warnings, 1, 'Exactly 1 warning';
408 qr/'$object' and '$object' are identical \(not copied\)/,
409 'with the text we expect';
414 # On Unix systems, File::Copy always returns 0 to signal failure,
415 # even when in list context! On Windows, it always returns "" to signal
418 # While returning a list containing a false value is arguably a bad
419 # API design, at the very least we can make sure it always returns
420 # the same false value.
422 my $NO_SUCH_FILE = "this_file_had_better_not_exist";
423 my $NO_SUCH_OTHER_FILE = "my_goodness_im_sick_of_airports";
425 use constant EXPECTED_SCALAR => 0;
426 use constant EXPECTED_LIST => [ EXPECTED_SCALAR ];
429 copy => \&File::Copy::copy,
430 cp => \&File::Copy::cp,
431 move => \&File::Copy::move,
432 mv => \&File::Copy::mv,
436 skip( "Test can't run with $NO_SUCH_FILE existing", 2 * keys %subs)
437 if (-e $NO_SUCH_FILE);
439 foreach my $name (keys %subs) {
441 my $sub = $subs{$name};
443 my $scalar = $sub->( $NO_SUCH_FILE, $NO_SUCH_OTHER_FILE );
444 is( $scalar, EXPECTED_SCALAR, "$name in scalar context");
446 my @array = $sub->( $NO_SUCH_FILE, $NO_SUCH_OTHER_FILE );
447 is_deeply( \@array, EXPECTED_LIST, "$name in list context");
452 skip("fork required to test pipe copying", 2)
453 if (!$Config{'d_fork'});
455 open(my $IN, "-|") || exec $^X, '-e', 'print "Hello, world!\n"';
456 open(my $OUT, "|-") || exec $^X, '-ne', 'exit(/Hello/ ? 55 : 0)';
458 ok(copy($IN, $OUT), "copy pipe to another");
460 is($? >> 8, 55, "content copied through the pipes");
465 1 while unlink "file-$$";
466 1 while unlink "lib/file-$$";