Commit | Line | Data |
671637fe |
1 | #!./perl -w |
1a3850a5 |
2 | |
3 | BEGIN { |
96fe83cd |
4 | if( $ENV{PERL_CORE} ) { |
5 | chdir 't' if -d 't'; |
6 | @INC = '../lib'; |
7 | } |
1a3850a5 |
8 | } |
9 | |
81ec4fbc |
10 | use strict; |
11 | use warnings; |
12 | |
83519ebf |
13 | use Test::More; |
1a3850a5 |
14 | |
83519ebf |
15 | my $TB = Test::More->builder; |
16 | |
e55c0a82 |
17 | plan tests => 136; |
1ef59467 |
18 | |
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]) }; } |
22 | |
1a04d035 |
23 | |
1a3850a5 |
24 | use File::Copy; |
ac7b122d |
25 | use Config; |
1a3850a5 |
26 | |
754f2cd0 |
27 | |
28 | foreach my $code ("copy()", "copy('arg')", "copy('arg', 'arg', 'arg', 'arg')", |
29 | "move()", "move('arg')", "move('arg', 'arg', 'arg')" |
30 | ) |
31 | { |
32 | eval $code; |
96fe83cd |
33 | like $@, qr/^Usage: /, "'$code' is a usage error"; |
754f2cd0 |
34 | } |
35 | |
36 | |
1ef59467 |
37 | for my $cross_partition_test (0..1) { |
38 | { |
39 | # Simulate a cross-partition copy/move by forcing rename to |
40 | # fail. |
41 | no warnings 'redefine'; |
42 | *CORE::GLOBAL::rename = sub { 0 } if $cross_partition_test; |
43 | } |
1a04d035 |
44 | |
45 | # First we create a file |
671637fe |
46 | open(F, ">file-$$") or die $!; |
1a04d035 |
47 | binmode F; # for DOSISH platforms, because test 3 copies to stdout |
83519ebf |
48 | printf F "ok\n"; |
1a04d035 |
49 | close F; |
50 | |
51 | copy "file-$$", "copy-$$"; |
52 | |
671637fe |
53 | open(F, "copy-$$") or die $!; |
81ec4fbc |
54 | my $foo = <F>; |
1a04d035 |
55 | close(F); |
56 | |
96fe83cd |
57 | is -s "file-$$", -s "copy-$$", 'copy(fn, fn): files of the same size'; |
1a04d035 |
58 | |
96fe83cd |
59 | is $foo, "ok\n", 'copy(fn, fn): same contents'; |
1a04d035 |
60 | |
96fe83cd |
61 | print("# next test checks copying to STDOUT\n"); |
1a04d035 |
62 | binmode STDOUT unless $^O eq 'VMS'; # Copy::copy works in binary mode |
83519ebf |
63 | # This outputs "ok" so its a test. |
1a04d035 |
64 | copy "copy-$$", \*STDOUT; |
83519ebf |
65 | $TB->current_test($TB->current_test + 1); |
1a04d035 |
66 | unlink "copy-$$" or die "unlink: $!"; |
67 | |
68 | open(F,"file-$$"); |
69 | copy(*F, "copy-$$"); |
70 | open(R, "copy-$$") or die "open copy-$$: $!"; $foo = <R>; close(R); |
96fe83cd |
71 | is $foo, "ok\n", 'copy(*F, fn): same contents'; |
1a04d035 |
72 | unlink "copy-$$" or die "unlink: $!"; |
83519ebf |
73 | |
1a04d035 |
74 | open(F,"file-$$"); |
75 | copy(\*F, "copy-$$"); |
76 | close(F) or die "close: $!"; |
77 | open(R, "copy-$$") or die; $foo = <R>; close(R) or die "close: $!"; |
96fe83cd |
78 | is $foo, "ok\n", 'copy(\*F, fn): same contents'; |
1a04d035 |
79 | unlink "copy-$$" or die "unlink: $!"; |
80 | |
81 | require IO::File; |
81ec4fbc |
82 | my $fh = IO::File->new(">copy-$$") or die "Cannot open copy-$$:$!"; |
671637fe |
83 | binmode $fh or die $!; |
1a04d035 |
84 | copy("file-$$",$fh); |
85 | $fh->close or die "close: $!"; |
86 | open(R, "copy-$$") or die; $foo = <R>; close(R); |
96fe83cd |
87 | is $foo, "ok\n", 'copy(fn, io): same contents'; |
1a04d035 |
88 | unlink "copy-$$" or die "unlink: $!"; |
83519ebf |
89 | |
1a04d035 |
90 | require FileHandle; |
81ec4fbc |
91 | $fh = FileHandle->new(">copy-$$") or die "Cannot open copy-$$:$!"; |
671637fe |
92 | binmode $fh or die $!; |
1a04d035 |
93 | copy("file-$$",$fh); |
94 | $fh->close; |
671637fe |
95 | open(R, "copy-$$") or die $!; $foo = <R>; close(R); |
96fe83cd |
96 | is $foo, "ok\n", 'copy(fn, fh): same contents'; |
1a04d035 |
97 | unlink "file-$$" or die "unlink: $!"; |
98 | |
83519ebf |
99 | ok !move("file-$$", "copy-$$"), "move on missing file"; |
100 | ok -e "copy-$$", ' target still there'; |
1a04d035 |
101 | |
1ef59467 |
102 | # Doesn't really matter what time it is as long as its not now. |
103 | my $time = 1000000000; |
104 | utime( $time, $time, "copy-$$" ); |
105 | |
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]; |
110 | |
754f2cd0 |
111 | ok move("copy-$$", "file-$$"), 'move'; |
83519ebf |
112 | ok -e "file-$$", ' destination exists'; |
113 | ok !-e "copy-$$", ' source does not'; |
671637fe |
114 | open(R, "file-$$") or die $!; $foo = <R>; close(R); |
96fe83cd |
115 | is $foo, "ok\n", 'contents preserved'; |
83519ebf |
116 | |
e9e3be28 |
117 | TODO: { |
118 | local $TODO = 'mtime only preserved on ODS-5 with POSIX dates and DECC$EFS_FILE_TIMESTAMPS enabled' if $^O eq 'VMS'; |
119 | |
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" : ""); |
124 | } |
1ef59467 |
125 | |
96fe83cd |
126 | # trick: create lib/ if not exists - not needed in Perl core |
671637fe |
127 | unless (-d 'lib') { mkdir 'lib' or die $!; } |
83519ebf |
128 | copy "file-$$", "lib"; |
96fe83cd |
129 | open(R, "lib/file-$$") or die $!; $foo = <R>; close(R); |
130 | is $foo, "ok\n", 'copy(fn, dir): same contents'; |
83519ebf |
131 | unlink "lib/file-$$" or die "unlink: $!"; |
132 | |
133 | # Do it twice to ensure copying over the same file works. |
134 | copy "file-$$", "lib"; |
671637fe |
135 | open(R, "lib/file-$$") or die $!; $foo = <R>; close(R); |
96fe83cd |
136 | is $foo, "ok\n", 'copy over the same file works'; |
83519ebf |
137 | unlink "lib/file-$$" or die "unlink: $!"; |
138 | |
754f2cd0 |
139 | { |
140 | my $warnings = ''; |
141 | local $SIG{__WARN__} = sub { $warnings .= join '', @_ }; |
96fe83cd |
142 | ok copy("file-$$", "file-$$"), 'copy(fn, fn) succeeds'; |
754f2cd0 |
143 | |
96fe83cd |
144 | like $warnings, qr/are identical/, 'but warns'; |
145 | ok -s "file-$$", 'contents preserved'; |
754f2cd0 |
146 | } |
83519ebf |
147 | |
148 | move "file-$$", "lib"; |
149 | open(R, "lib/file-$$") or die "open lib/file-$$: $!"; $foo = <R>; close(R); |
96fe83cd |
150 | is $foo, "ok\n", 'move(fn, dir): same contents'; |
151 | ok !-e "file-$$", 'file moved indeed'; |
83519ebf |
152 | unlink "lib/file-$$" or die "unlink: $!"; |
153 | |
154 | SKIP: { |
754f2cd0 |
155 | skip "Testing symlinks", 3 unless $Config{d_symlink}; |
ac7b122d |
156 | |
ac7b122d |
157 | open(F, ">file-$$") or die $!; |
158 | print F "dummy content\n"; |
159 | close F; |
160 | symlink("file-$$", "symlink-$$") or die $!; |
754f2cd0 |
161 | |
162 | my $warnings = ''; |
163 | local $SIG{__WARN__} = sub { $warnings .= join '', @_ }; |
96fe83cd |
164 | ok !copy("file-$$", "symlink-$$"), 'copy to itself (via symlink) fails'; |
754f2cd0 |
165 | |
96fe83cd |
166 | like $warnings, qr/are identical/, 'emits a warning'; |
83519ebf |
167 | ok !-z "file-$$", |
168 | 'rt.perl.org 5196: copying to itself would truncate the file'; |
169 | |
671637fe |
170 | unlink "symlink-$$" or die $!; |
171 | unlink "file-$$" or die $!; |
6c254d95 |
172 | } |
ac7b122d |
173 | |
83519ebf |
174 | SKIP: { |
96fe83cd |
175 | skip "Testing hard links", 3 |
176 | if !$Config{d_link} or $^O eq 'MSWin32' or $^O eq 'cygwin'; |
83519ebf |
177 | |
178 | open(F, ">file-$$") or die $!; |
179 | print F "dummy content\n"; |
180 | close F; |
181 | link("file-$$", "hardlink-$$") or die $!; |
754f2cd0 |
182 | |
183 | my $warnings = ''; |
184 | local $SIG{__WARN__} = sub { $warnings .= join '', @_ }; |
96fe83cd |
185 | ok !copy("file-$$", "hardlink-$$"), 'copy to itself (via hardlink) fails'; |
754f2cd0 |
186 | |
96fe83cd |
187 | like $warnings, qr/are identical/, 'emits a warning'; |
83519ebf |
188 | ok ! -z "file-$$", |
189 | 'rt.perl.org 5196: copying to itself would truncate the file'; |
190 | |
671637fe |
191 | unlink "hardlink-$$" or die $!; |
192 | unlink "file-$$" or die $!; |
ac7b122d |
193 | } |
671637fe |
194 | |
195 | open(F, ">file-$$") or die $!; |
196 | binmode F; |
197 | print F "this is file\n"; |
198 | close F; |
199 | |
200 | my $copy_msg = "this is copy\n"; |
201 | open(F, ">copy-$$") or die $!; |
202 | binmode F; |
203 | print F $copy_msg; |
204 | close F; |
205 | |
206 | my @warnings; |
207 | local $SIG{__WARN__} = sub { push @warnings, join '', @_ }; |
208 | |
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; |
216 | } |
217 | |
218 | is -s "copy-$$", length $copy_msg, "but does not truncate the destination"; |
219 | open(F, "copy-$$") or die $!; |
220 | $foo = <F>; |
221 | close(F); |
222 | is $foo, $copy_msg, "nor change the destination's contents"; |
223 | |
224 | unlink "file-$$" or die $!; |
225 | unlink "copy-$$" or die $!; |
1a04d035 |
226 | } |
227 | |
441496b2 |
228 | |
32d68040 |
229 | SKIP: { |
230 | |
231 | skip "-- Copy preserves RMS defaults, not source file permissions.", 21 if $^O eq 'VMS'; |
7029ff37 |
232 | skip "Copy doesn't set file permissions correctly on Win32.", 21 if $^O eq "MSWin32"; |
32d68040 |
233 | |
81ec4fbc |
234 | # Just a sub to get better failure messages. |
235 | sub __ ($) { |
236 | join "" => map {(qw [--- --x -w- -wx r-- r-x rw- rwx]) [$_]} |
237 | split // => sprintf "%03o" => shift |
238 | } |
239 | # Testing permission bits. |
240 | my $src = "file-$$"; |
241 | my $copy1 = "copy1-$$"; |
242 | my $copy2 = "copy2-$$"; |
243 | my $copy3 = "copy3-$$"; |
244 | |
245 | open my $fh => ">", $src or die $!; |
246 | close $fh or die $!; |
247 | |
248 | open $fh => ">", $copy3 or die $!; |
249 | close $fh or die $!; |
250 | |
251 | my @tests = ( |
252 | [0000, 0777, 0777, 0777], |
253 | [0000, 0751, 0751, 0644], |
254 | [0022, 0777, 0755, 0206], |
255 | [0022, 0415, 0415, 0666], |
256 | [0077, 0777, 0700, 0333], |
257 | [0027, 0755, 0750, 0251], |
258 | [0777, 0751, 0000, 0215], |
259 | ); |
260 | my $old_mask = umask; |
261 | foreach my $test (@tests) { |
262 | my ($umask, $s_perm, $c_perm1, $c_perm3) = @$test; |
263 | # Make sure the copies doesn't exist. |
264 | ! -e $_ or unlink $_ or die $! for $copy1, $copy2; |
265 | |
266 | (umask $umask) // die $!; |
267 | chmod $s_perm => $src or die $!; |
268 | chmod $c_perm3 => $copy3 or die $!; |
269 | |
270 | open my $fh => "<", $src or die $!; |
271 | |
272 | copy ($src, $copy1); |
273 | copy ($fh, $copy2); |
274 | copy ($src, $copy3); |
275 | |
276 | my $perm1 = (stat $copy1) [2] & 0xFFF; |
277 | my $perm2 = (stat $copy2) [2] & 0xFFF; |
278 | my $perm3 = (stat $copy3) [2] & 0xFFF; |
279 | is (__$perm1, __$c_perm1, "Permission bits set correctly"); |
280 | is (__$perm2, __$c_perm1, "Permission bits set correctly"); |
c4e1003e |
281 | TODO: { |
282 | local $TODO = 'Permission bits inconsistent under cygwin' if $^O eq 'cygwin'; |
283 | is (__$perm3, __$c_perm3, "Permission bits not modified"); |
284 | } |
81ec4fbc |
285 | } |
286 | umask $old_mask or die $!; |
287 | |
288 | # Clean up. |
289 | ! -e $_ or unlink $_ or die $! for $src, $copy1, $copy2, $copy3; |
290 | } |
291 | |
e55c0a82 |
292 | { |
293 | package Crash; |
294 | # a package overloaded suspiciously like IO::Scalar |
295 | use overload '""' => sub { ${$_[0]} }; |
296 | use overload 'bool' => sub { 1 }; |
297 | sub new { |
298 | my ($class, $name) = @_; |
299 | bless \$name, $class; |
300 | } |
301 | |
302 | package Zowie; |
303 | # a different package overloaded suspiciously like IO::Scalar |
304 | use overload '""' => sub { ${$_[0]} }; |
305 | use overload 'bool' => sub { 1 }; |
306 | sub new { |
307 | my ($class, $name) = @_; |
308 | bless \$name, $class; |
309 | } |
310 | } |
311 | { |
312 | my $object = Crash->new('whack_eth'); |
313 | my %what = (plain => "$object", |
314 | object1 => $object, |
315 | object2 => Zowie->new('whack_eth'), |
316 | object2 => Zowie->new('whack_eth'), |
317 | ); |
318 | |
319 | my @warnings; |
320 | local $SIG{__WARN__} = sub { |
321 | push @warnings, @_; |
322 | }; |
323 | |
324 | foreach my $left (qw(plain object1 object2)) { |
325 | foreach my $right (qw(plain object1 object2)) { |
326 | @warnings = (); |
327 | $! = 0; |
328 | is eval {copy $what{$left}, $what{$right}}, 1, "copy $left $right"; |
329 | is $@, '', 'No croaking'; |
330 | is $!, '', 'No system call errors'; |
331 | is @warnings, 1, 'Exactly 1 warning'; |
332 | like $warnings[0], |
333 | qr/'$object' and '$object' are identical \(not copied\)/, |
334 | 'with the text we expect'; |
335 | } |
336 | } |
337 | } |
81ec4fbc |
338 | |
cfcb0b09 |
339 | END { |
340 | 1 while unlink "file-$$"; |
83519ebf |
341 | 1 while unlink "lib/file-$$"; |
cfcb0b09 |
342 | } |