This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Partially reinstate "Perl 6" instead of Raku for mro.pm and feature.pm:
[perl5.git] / lib / File / Copy.t
CommitLineData
671637fe 1#!./perl -w
1a3850a5
GA
2
3BEGIN {
96fe83cd
AF
4 if( $ENV{PERL_CORE} ) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 }
1a3850a5
GA
8}
9
81ec4fbc
A
10use strict;
11use warnings;
12
83519ebf 13use Test::More;
1a3850a5 14
83519ebf
MS
15my $TB = Test::More->builder;
16
bd86609c 17plan tests => 466;
1ef59467 18
bd86609c 19# We are going to override rename() later on but Perl has to see an override
1ef59467
MS
20# at compile time to honor it.
21BEGIN { *CORE::GLOBAL::rename = sub { CORE::rename($_[0], $_[1]) }; }
22
1a04d035 23
e63b3379 24use File::Copy qw(copy move cp);
ac7b122d 25use Config;
1a3850a5 26
310d0155
AF
27# If we have Time::HiRes, File::Copy loaded it for us.
28BEGIN {
29 eval { Time::HiRes->import(qw( stat utime )) };
30 note "Testing Time::HiRes::utime support" unless $@;
31}
754f2cd0
MS
32
33foreach my $code ("copy()", "copy('arg')", "copy('arg', 'arg', 'arg', 'arg')",
34 "move()", "move('arg')", "move('arg', 'arg', 'arg')"
35 )
36{
37 eval $code;
96fe83cd 38 like $@, qr/^Usage: /, "'$code' is a usage error";
754f2cd0
MS
39}
40
41
1ef59467
MS
42for my $cross_partition_test (0..1) {
43 {
44 # Simulate a cross-partition copy/move by forcing rename to
45 # fail.
46 no warnings 'redefine';
47 *CORE::GLOBAL::rename = sub { 0 } if $cross_partition_test;
48 }
1a04d035
A
49
50 # First we create a file
1ae6ead9 51 open(F, ">", "file-$$") or die $!;
1a04d035 52 binmode F; # for DOSISH platforms, because test 3 copies to stdout
83519ebf 53 printf F "ok\n";
1a04d035
A
54 close F;
55
56 copy "file-$$", "copy-$$";
57
1ae6ead9 58 open(F, "<", "copy-$$") or die $!;
81ec4fbc 59 my $foo = <F>;
1a04d035
A
60 close(F);
61
96fe83cd 62 is -s "file-$$", -s "copy-$$", 'copy(fn, fn): files of the same size';
1a04d035 63
96fe83cd 64 is $foo, "ok\n", 'copy(fn, fn): same contents';
1a04d035 65
96fe83cd 66 print("# next test checks copying to STDOUT\n");
1a04d035 67 binmode STDOUT unless $^O eq 'VMS'; # Copy::copy works in binary mode
83519ebf 68 # This outputs "ok" so its a test.
1a04d035 69 copy "copy-$$", \*STDOUT;
83519ebf 70 $TB->current_test($TB->current_test + 1);
1a04d035
A
71 unlink "copy-$$" or die "unlink: $!";
72
1ae6ead9 73 open(F, "<", "file-$$");
e91a8fe5 74 binmode F;
1a04d035 75 copy(*F, "copy-$$");
e91a8fe5 76 open(R, "<:raw", "copy-$$") or die "open copy-$$: $!"; $foo = <R>; close(R);
96fe83cd 77 is $foo, "ok\n", 'copy(*F, fn): same contents';
1a04d035 78 unlink "copy-$$" or die "unlink: $!";
83519ebf 79
1ae6ead9 80 open(F, "<", "file-$$");
e91a8fe5 81 binmode F;
1a04d035
A
82 copy(\*F, "copy-$$");
83 close(F) or die "close: $!";
1ae6ead9 84 open(R, "<", "copy-$$") or die; $foo = <R>; close(R) or die "close: $!";
96fe83cd 85 is $foo, "ok\n", 'copy(\*F, fn): same contents';
1a04d035
A
86 unlink "copy-$$" or die "unlink: $!";
87
88 require IO::File;
81ec4fbc 89 my $fh = IO::File->new(">copy-$$") or die "Cannot open copy-$$:$!";
671637fe 90 binmode $fh or die $!;
1a04d035
A
91 copy("file-$$",$fh);
92 $fh->close or die "close: $!";
1ae6ead9 93 open(R, "<", "copy-$$") or die; $foo = <R>; close(R);
96fe83cd 94 is $foo, "ok\n", 'copy(fn, io): same contents';
1a04d035 95 unlink "copy-$$" or die "unlink: $!";
83519ebf 96
1a04d035 97 require FileHandle;
81ec4fbc 98 $fh = FileHandle->new(">copy-$$") or die "Cannot open copy-$$:$!";
671637fe 99 binmode $fh or die $!;
1a04d035
A
100 copy("file-$$",$fh);
101 $fh->close;
1ae6ead9 102 open(R, "<", "copy-$$") or die $!; $foo = <R>; close(R);
96fe83cd 103 is $foo, "ok\n", 'copy(fn, fh): same contents';
1a04d035
A
104 unlink "file-$$" or die "unlink: $!";
105
83519ebf
MS
106 ok !move("file-$$", "copy-$$"), "move on missing file";
107 ok -e "copy-$$", ' target still there';
1a04d035 108
1ef59467 109 # Doesn't really matter what time it is as long as its not now.
310d0155 110 my $time = 1000000000.12345;
1ef59467
MS
111 utime( $time, $time, "copy-$$" );
112
113 # Recheck the mtime rather than rely on utime in case we're on a
114 # system where utime doesn't work or there's no mtime at all.
115 # The destination file will reflect the same difficulties.
116 my $mtime = (stat("copy-$$"))[9];
117
754f2cd0 118 ok move("copy-$$", "file-$$"), 'move';
83519ebf
MS
119 ok -e "file-$$", ' destination exists';
120 ok !-e "copy-$$", ' source does not';
1ae6ead9 121 open(R, "<", "file-$$") or die $!; $foo = <R>; close(R);
96fe83cd 122 is $foo, "ok\n", 'contents preserved';
83519ebf 123
e9e3be28
CB
124 TODO: {
125 local $TODO = 'mtime only preserved on ODS-5 with POSIX dates and DECC$EFS_FILE_TIMESTAMPS enabled' if $^O eq 'VMS';
126
127 my $dest_mtime = (stat("file-$$"))[9];
128 is $dest_mtime, $mtime,
129 "mtime preserved by copy()".
130 ($cross_partition_test ? " while testing cross-partition" : "");
131 }
1ef59467 132
96fe83cd 133 # trick: create lib/ if not exists - not needed in Perl core
671637fe 134 unless (-d 'lib') { mkdir 'lib' or die $!; }
83519ebf 135 copy "file-$$", "lib";
1ae6ead9 136 open(R, "<", "lib/file-$$") or die $!; $foo = <R>; close(R);
96fe83cd 137 is $foo, "ok\n", 'copy(fn, dir): same contents';
83519ebf
MS
138 unlink "lib/file-$$" or die "unlink: $!";
139
140 # Do it twice to ensure copying over the same file works.
141 copy "file-$$", "lib";
1ae6ead9 142 open(R, "<", "lib/file-$$") or die $!; $foo = <R>; close(R);
96fe83cd 143 is $foo, "ok\n", 'copy over the same file works';
83519ebf
MS
144 unlink "lib/file-$$" or die "unlink: $!";
145
754f2cd0
MS
146 {
147 my $warnings = '';
148 local $SIG{__WARN__} = sub { $warnings .= join '', @_ };
39b80fd9 149 ok !copy("file-$$", "file-$$"), 'copy to itself fails';
754f2cd0 150
96fe83cd
AF
151 like $warnings, qr/are identical/, 'but warns';
152 ok -s "file-$$", 'contents preserved';
754f2cd0 153 }
83519ebf
MS
154
155 move "file-$$", "lib";
1ae6ead9 156 open(R, "<", "lib/file-$$") or die "open lib/file-$$: $!"; $foo = <R>; close(R);
96fe83cd
AF
157 is $foo, "ok\n", 'move(fn, dir): same contents';
158 ok !-e "file-$$", 'file moved indeed';
83519ebf
MS
159 unlink "lib/file-$$" or die "unlink: $!";
160
161 SKIP: {
754f2cd0 162 skip "Testing symlinks", 3 unless $Config{d_symlink};
ac7b122d 163
1ae6ead9 164 open(F, ">", "file-$$") or die $!;
ac7b122d
SR
165 print F "dummy content\n";
166 close F;
167 symlink("file-$$", "symlink-$$") or die $!;
754f2cd0
MS
168
169 my $warnings = '';
170 local $SIG{__WARN__} = sub { $warnings .= join '', @_ };
96fe83cd 171 ok !copy("file-$$", "symlink-$$"), 'copy to itself (via symlink) fails';
754f2cd0 172
96fe83cd 173 like $warnings, qr/are identical/, 'emits a warning';
83519ebf
MS
174 ok !-z "file-$$",
175 'rt.perl.org 5196: copying to itself would truncate the file';
176
671637fe
NC
177 unlink "symlink-$$" or die $!;
178 unlink "file-$$" or die $!;
6c254d95 179 }
ac7b122d 180
83519ebf 181 SKIP: {
96fe83cd
AF
182 skip "Testing hard links", 3
183 if !$Config{d_link} or $^O eq 'MSWin32' or $^O eq 'cygwin';
83519ebf 184
1ae6ead9 185 open(F, ">", "file-$$") or die $!;
83519ebf
MS
186 print F "dummy content\n";
187 close F;
188 link("file-$$", "hardlink-$$") or die $!;
754f2cd0
MS
189
190 my $warnings = '';
191 local $SIG{__WARN__} = sub { $warnings .= join '', @_ };
96fe83cd 192 ok !copy("file-$$", "hardlink-$$"), 'copy to itself (via hardlink) fails';
754f2cd0 193
96fe83cd 194 like $warnings, qr/are identical/, 'emits a warning';
83519ebf
MS
195 ok ! -z "file-$$",
196 'rt.perl.org 5196: copying to itself would truncate the file';
197
671637fe
NC
198 unlink "hardlink-$$" or die $!;
199 unlink "file-$$" or die $!;
ac7b122d 200 }
671637fe 201
1ae6ead9 202 open(F, ">", "file-$$") or die $!;
671637fe
NC
203 binmode F;
204 print F "this is file\n";
205 close F;
206
207 my $copy_msg = "this is copy\n";
1ae6ead9 208 open(F, ">", "copy-$$") or die $!;
671637fe
NC
209 binmode F;
210 print F $copy_msg;
211 close F;
212
213 my @warnings;
214 local $SIG{__WARN__} = sub { push @warnings, join '', @_ };
215
216 # pie-$$ so that we force a non-constant, else the numeric conversion (of 0)
bd86609c 217 # is cached and we do not get a warning the second time round
671637fe
NC
218 is eval { copy("file-$$", "copy-$$", "pie-$$"); 1 }, undef,
219 "a bad buffer size fails to copy";
220 like $@, qr/Bad buffer size for copy/, "with a helpful error message";
221 unless (is scalar @warnings, 1, "There is 1 warning") {
222 diag $_ foreach @warnings;
223 }
224
225 is -s "copy-$$", length $copy_msg, "but does not truncate the destination";
1ae6ead9 226 open(F, "<", "copy-$$") or die $!;
671637fe
NC
227 $foo = <F>;
228 close(F);
229 is $foo, $copy_msg, "nor change the destination's contents";
230
231 unlink "file-$$" or die $!;
232 unlink "copy-$$" or die $!;
fff5c6e2
DM
233
234 # RT #73714 copy to file with leading whitespace failed
235
69a90d4d
CB
236 TODO: {
237 local $TODO = 'spaces in filenames require DECC$EFS_CHARSET enabled' if $^O eq 'VMS';
1ae6ead9 238 open(F, ">", "file-$$") or die $!;
fff5c6e2
DM
239 close F;
240 copy "file-$$", " copy-$$";
fff5c6e2
DM
241 ok -e " copy-$$", "copy with leading whitespace";
242 unlink "file-$$" or die "unlink: $!";
243 unlink " copy-$$" or die "unlink: $!";
69a90d4d 244 }
1a04d035
A
245}
246
cae9400f
AB
247my $can_suidp = sub {
248 my $dir = "suid-$$";
249 my $ok = 1;
250 mkdir $dir or die "Can't mkdir($dir) for suid test";
251 $ok = 0 unless chmod 2000, $dir;
252 rmdir $dir;
253 return $ok;
254};
441496b2 255
32d68040 256SKIP: {
20513930
A
257 my @tests = (
258 [0000, 0777, 0777, 0777],
259 [0000, 0751, 0751, 0644],
260 [0022, 0777, 0755, 0206],
261 [0022, 0415, 0415, 0666],
262 [0077, 0777, 0700, 0333],
263 [0027, 0755, 0750, 0251],
264 [0777, 0751, 0000, 0215],
265 );
266
267 my $skips = @tests * 6 * 8;
32d68040 268
cae9400f
AB
269 my $can_suid = $can_suidp->();
270 skip "Can't suid on this $^O filesystem", $skips unless $can_suid;
20513930
A
271 skip "-- Copy preserves RMS defaults, not POSIX permissions.", $skips
272 if $^O eq 'VMS';
273 skip "Copy doesn't set file permissions correctly on Win32.", $skips
274 if $^O eq "MSWin32";
f6dacdca
PG
275 skip "Copy maps POSIX permissions to VOS permissions.", $skips
276 if $^O eq "vos";
33821f2f
CBW
277 skip "There be dragons here with DragonflyBSD.", $skips
278 if $^O eq 'dragonfly';
279
32d68040 280
81ec4fbc
A
281 # Just a sub to get better failure messages.
282 sub __ ($) {
20513930
A
283 my $perm = shift;
284 my $id = 07000 & $perm;
285 $id >>= 9;
286 $perm &= 0777;
287 my @chunks = map {(qw [--- --x -w- -wx r-- r-x rw- rwx]) [$_]}
288 split // => sprintf "%03o" => $perm;
289 if ($id & 4) {$chunks [0] =~ s/(.)$/$1 eq '-' ? 'S' : 's'/e;}
290 if ($id & 2) {$chunks [1] =~ s/(.)$/$1 eq '-' ? 'S' : 's'/e;}
291 if ($id & 1) {$chunks [2] =~ s/(.)$/$1 eq '-' ? 'T' : 't'/e;}
292 join "" => @chunks;
81ec4fbc
A
293 }
294 # Testing permission bits.
295 my $src = "file-$$";
296 my $copy1 = "copy1-$$";
297 my $copy2 = "copy2-$$";
298 my $copy3 = "copy3-$$";
e63b3379
CB
299 my $copy4 = "copy4-$$";
300 my $copy5 = "copy5-$$";
301 my $copy6 = "copy6-$$";
c1976a97 302 my $copyd = "copyd-$$";
81ec4fbc
A
303
304 open my $fh => ">", $src or die $!;
305 close $fh or die $!;
306
307 open $fh => ">", $copy3 or die $!;
308 close $fh or die $!;
309
e63b3379
CB
310 open $fh => ">", $copy6 or die $!;
311 close $fh or die $!;
312
81ec4fbc
A
313 my $old_mask = umask;
314 foreach my $test (@tests) {
20513930
A
315 foreach my $id (0 .. 7) {
316 my ($umask, $s_perm, $c_perm1, $c_perm3) = @$test;
bd86609c 317 # Make sure the copies do not exist.
20513930
A
318 ! -e $_ or unlink $_ or die $! for $copy1, $copy2, $copy4, $copy5;
319
320 $s_perm |= $id << 9;
321 $c_perm1 |= $id << 9;
b1144eba
JH
322 diag(sprintf "Src permission: %04o; umask %03o\n", $s_perm, $umask)
323 unless ($ENV{PERL_CORE});
76073986
NC
324
325 # Test that we can actually set a file to the correct permission.
326 # Slightly convoluted, because some operating systems will let us
327 # set a directory, but not a file. These should all work:
c1976a97
TC
328 mkdir $copyd or die "Can't mkdir $copyd: $!";
329 chmod $s_perm, $copyd
330 or die sprintf "Can't chmod %o $copyd: $!", $s_perm;
331 rmdir $copyd
332 or die sprintf "Can't rmdir $copyd: $!";
76073986
NC
333 open my $fh0, '>', $copy1 or die "Can't open $copy1: $!";
334 close $fh0 or die "Can't close $copy1: $!";
335 unless (chmod $s_perm, $copy1) {
336 $TB->skip(sprintf "Can't chmod $copy1 to %o: $!", $s_perm)
337 for 1..6;
338 next;
339 }
340 my $perm0 = (stat $copy1) [2] & 07777;
341 unless ($perm0 == $s_perm) {
342 $TB->skip(sprintf "chmod %o $copy1 lies - we actually get %o",
343 $s_perm, $perm0)
344 for 1..6;
345 next;
346 }
347 unlink $copy1 or die "Can't unlink $copy1: $!";
348
20513930
A
349 (umask $umask) // die $!;
350 chmod $s_perm => $src or die sprintf "$!: $src => %o", $s_perm;
351 chmod $c_perm3 => $copy3 or die $!;
352 chmod $c_perm3 => $copy6 or die $!;
353
354 open my $fh => "<", $src or die $!;
e91a8fe5 355 binmode $fh;
20513930
A
356
357 copy ($src, $copy1);
358 copy ($fh, $copy2);
359 copy ($src, $copy3);
360 cp ($src, $copy4);
361 cp ($fh, $copy5);
362 cp ($src, $copy6);
363
364 my $permdef = 0666 & ~$umask;
365 my $perm1 = (stat $copy1) [2] & 07777;
366 my $perm2 = (stat $copy2) [2] & 07777;
367 my $perm3 = (stat $copy3) [2] & 07777;
368 my $perm4 = (stat $copy4) [2] & 07777;
369 my $perm5 = (stat $copy5) [2] & 07777;
370 my $perm6 = (stat $copy6) [2] & 07777;
371 is (__$perm1, __$permdef, "Permission bits set correctly");
372 is (__$perm2, __$permdef, "Permission bits set correctly");
373 is (__$perm4, __$c_perm1, "Permission bits set correctly");
374 is (__$perm5, __$c_perm1, "Permission bits set correctly");
f095462e
TC
375 is (__$perm3, __$c_perm3, "Permission bits not modified");
376 is (__$perm6, __$c_perm3, "Permission bits not modified");
c4e1003e 377 }
81ec4fbc
A
378 }
379 umask $old_mask or die $!;
380
381 # Clean up.
20513930
A
382 ! -e $_ or unlink $_ or die $! for $src, $copy1, $copy2, $copy3,
383 $copy4, $copy5, $copy6;
81ec4fbc
A
384}
385
e55c0a82
PR
386{
387 package Crash;
388 # a package overloaded suspiciously like IO::Scalar
389 use overload '""' => sub { ${$_[0]} };
390 use overload 'bool' => sub { 1 };
391 sub new {
392 my ($class, $name) = @_;
393 bless \$name, $class;
394 }
395
396 package Zowie;
397 # a different package overloaded suspiciously like IO::Scalar
398 use overload '""' => sub { ${$_[0]} };
399 use overload 'bool' => sub { 1 };
400 sub new {
401 my ($class, $name) = @_;
402 bless \$name, $class;
403 }
404}
405{
406 my $object = Crash->new('whack_eth');
407 my %what = (plain => "$object",
408 object1 => $object,
409 object2 => Zowie->new('whack_eth'),
410 object2 => Zowie->new('whack_eth'),
411 );
412
413 my @warnings;
414 local $SIG{__WARN__} = sub {
415 push @warnings, @_;
416 };
417
418 foreach my $left (qw(plain object1 object2)) {
419 foreach my $right (qw(plain object1 object2)) {
420 @warnings = ();
421 $! = 0;
39b80fd9 422 is eval {copy $what{$left}, $what{$right}}, 0, "copy $left $right";
e55c0a82
PR
423 is $@, '', 'No croaking';
424 is $!, '', 'No system call errors';
425 is @warnings, 1, 'Exactly 1 warning';
426 like $warnings[0],
427 qr/'$object' and '$object' are identical \(not copied\)/,
428 'with the text we expect';
429 }
430 }
431}
81ec4fbc 432
079cb8cc
PF
433# On Unix systems, File::Copy always returns 0 to signal failure,
434# even when in list context! On Windows, it always returns "" to signal
435# failure.
436#
437# While returning a list containing a false value is arguably a bad
438# API design, at the very least we can make sure it always returns
439# the same false value.
440
441my $NO_SUCH_FILE = "this_file_had_better_not_exist";
442my $NO_SUCH_OTHER_FILE = "my_goodness_im_sick_of_airports";
443
444use constant EXPECTED_SCALAR => 0;
445use constant EXPECTED_LIST => [ EXPECTED_SCALAR ];
446
447my %subs = (
448 copy => \&File::Copy::copy,
449 cp => \&File::Copy::cp,
450 move => \&File::Copy::move,
451 mv => \&File::Copy::mv,
452);
453
454SKIP: {
455 skip( "Test can't run with $NO_SUCH_FILE existing", 2 * keys %subs)
456 if (-e $NO_SUCH_FILE);
457
458 foreach my $name (keys %subs) {
459
460 my $sub = $subs{$name};
461
462 my $scalar = $sub->( $NO_SUCH_FILE, $NO_SUCH_OTHER_FILE );
463 is( $scalar, EXPECTED_SCALAR, "$name in scalar context");
464
465 my @array = $sub->( $NO_SUCH_FILE, $NO_SUCH_OTHER_FILE );
466 is_deeply( \@array, EXPECTED_LIST, "$name in list context");
467 }
468}
469
16f708c9
NT
470SKIP: {
471 skip("fork required to test pipe copying", 2)
472 if (!$Config{'d_fork'});
473
474 open(my $IN, "-|") || exec $^X, '-e', 'print "Hello, world!\n"';
475 open(my $OUT, "|-") || exec $^X, '-ne', 'exit(/Hello/ ? 55 : 0)';
e91a8fe5
TC
476 binmode $IN;
477 binmode $OUT;
16f708c9
NT
478
479 ok(copy($IN, $OUT), "copy pipe to another");
480 close($OUT);
481 is($? >> 8, 55, "content copied through the pipes");
482 close($IN);
483}
484
b306ad7b
TC
485use File::Temp qw(tempdir);
486use File::Spec;
487
488SKIP: {
b306ad7b
TC
489 # RT #111126: File::Copy copy() zeros file when copying a file
490 # into the same directory it is stored in
491
492 my $temp_dir = tempdir( CLEANUP => 1 );
493 my $temp_file = File::Spec->catfile($temp_dir, "somefile");
494
495 open my $fh, ">", $temp_file
496 or skip "Cannot create $temp_file: $!", 2;
497 print $fh "Just some data";
498 close $fh
499 or skip "Cannot close $temp_file: $!", 2;
500
501 my $warn_message = "";
502 local $SIG{__WARN__} = sub { $warn_message .= "@_" };
503 ok(!copy($temp_file, $temp_dir),
504 "Copy of foo/file to foo/ should fail");
c83aeb21 505 like($warn_message, qr/^\Q'$temp_file' and '$temp_file'\E are identical.*Copy\.t/i,
b306ad7b
TC
506 "error message should describe the problem");
507 1 while unlink $temp_file;
508}
509
bd86609c
JK
510{
511 open(my $F, '>', "file-$$") or die $!;
512 binmode $F; # for DOSISH platforms
513 printf $F "ok\n";
514 close $F;
515
516 my $buffer = (1024 * 1024 * 2) + 1;
517 is eval {copy "file-$$", "copy-$$", $buffer}, 1,
518 "copy with buffer above normal size";
519}
520
521
cfcb0b09 522END {
bd86609c 523 1 while unlink "copy-$$";
cfcb0b09 524 1 while unlink "file-$$";
83519ebf 525 1 while unlink "lib/file-$$";
cfcb0b09 526}