This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Encode to CPAN version 2.78
[perl5.git] / t / io / fs.t
CommitLineData
8d063cd8
LW
1#!./perl
2
ea368a7c
CS
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
0c5d4ba3 6 require "./test.pl";
ea368a7c
CS
7}
8
9use Config;
10
6d738113 11my $Is_VMSish = ($^O eq 'VMS');
0c5d4ba3 12
f0c73bbb
TC
13if ($^O eq 'MSWin32') {
14 # under minitest, buildcustomize sets this to 1, which means
15 # nlinks isn't populated properly, allow our tests to pass
16 ${^WIN32_SLOPPY_STAT} = 0;
17}
18
20dd405c
MS
19if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
20 $wd = `cd`;
fc8d54b0
GA
21}
22elsif ($^O eq 'VMS') {
20dd405c 23 $wd = `show default`;
fc8d54b0 24}
97e175fe
BF
25elsif ( $^O =~ /android/ || $^O eq 'nto' ) {
26 # On Android and Blackberry 10, pwd is a shell builtin, so plain `pwd`
27 # won't cut it
f11cc189
BF
28 $wd = `sh -c pwd`;
29}
fc8d54b0 30else {
20dd405c
MS
31 $wd = `pwd`;
32}
33chomp($wd);
34
b844a53a
JR
35die "Can't get current working directory" if(!$wd);
36
0c5d4ba3
JH
37my $has_link = $Config{d_link};
38my $accurate_timestamps =
39 !($^O eq 'MSWin32' || $^O eq 'NetWare' ||
40 $^O eq 'dos' || $^O eq 'os2' ||
cd86ed9d 41 $^O eq 'cygwin' || $^O eq 'amigaos' ||
7b903762 42 $wd =~ m#$Config{afsroot}/#
20dd405c 43 );
39e571d4 44
6b980173 45if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
0c5d4ba3 46 if (Win32::FsType() eq 'NTFS') {
20dd405c
MS
47 $has_link = 1;
48 $accurate_timestamps = 1;
0c5d4ba3 49 }
58a5e53e
DD
50 else {
51 $has_link = 0;
52 }
6b980173
JD
53}
54
0c5d4ba3
JH
55my $needs_fh_reopen =
56 $^O eq 'dos'
57 # Not needed on HPFS, but needed on HPFS386 ?!
58 || $^O eq 'os2';
59
7a2cf369
NK
60$needs_fh_reopen = 1 if (defined &Win32::IsWin95 && Win32::IsWin95());
61
4e51f8e4
SR
62my $skip_mode_checks =
63 $^O eq 'cygwin' && $ENV{CYGWIN} !~ /ntsec/;
64
8334cae6 65plan tests => 61;
8d063cd8 66
62a28c97
NC
67my $tmpdir = tempfile();
68my $tmpdir1 = tempfile();
378cc40b 69
6d738113 70if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
62a28c97
NC
71 `rmdir /s /q $tmpdir 2>nul`;
72 `mkdir $tmpdir`;
dc459aad
JH
73}
74elsif ($^O eq 'VMS') {
62a28c97
NC
75 `if f\$search("[.$tmpdir]*.*") .nes. "" then delete/nolog/noconfirm [.$tmpdir]*.*.*`;
76 `if f\$search("$tmpdir.dir") .nes. "" then set file/prot=o:rwed $tmpdir.dir;`;
77 `if f\$search("$tmpdir.dir") .nes. "" then delete/nolog/noconfirm $tmpdir.dir;`;
78 `create/directory [.$tmpdir]`;
6d738113
PP
79}
80else {
62a28c97 81 `rm -f $tmpdir 2>/dev/null; mkdir $tmpdir 2>/dev/null`;
6d738113 82}
0c5d4ba3 83
519ecd2c 84chdir $tmpdir;
0c5d4ba3 85
b8440792 86`/bin/rm -rf a b c x` if -x '/bin/rm';
8d063cd8
LW
87
88umask(022);
89
20dd405c 90SKIP: {
739a0b84 91 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
20dd405c
MS
92
93 is((umask(0)&0777), 022, 'umask'),
0c5d4ba3
JH
94}
95
d5fc3e70
SP
96open(FH,'>x') || die "Can't create x";
97close(FH);
98open(FH,'>a') || die "Can't create a";
99close(FH);
8d063cd8 100
8268670f 101my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
dd568cb6 102 $blksize,$blocks,$a_mode);
8268670f 103
3ed9f8f7 104SKIP: {
0c5d4ba3 105 skip("no link", 4) unless $has_link;
8d063cd8 106
0c5d4ba3
JH
107 ok(link('a','b'), "link a b");
108 ok(link('b','c'), "link b c");
8d063cd8 109
dd568cb6
CB
110 $a_mode = (stat('a'))[2];
111
8268670f
JH
112 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
113 $blksize,$blocks) = stat('c');
8d063cd8 114
20dd405c
MS
115 SKIP: {
116 skip "no nlink", 1 if $Config{dont_use_nlink};
117
118 is($nlink, 3, "link count of triply-linked file");
0c5d4ba3 119 }
ea368a7c 120
20dd405c
MS
121 SKIP: {
122 skip "hard links not that hard in $^O", 1 if $^O eq 'amigaos';
4e51f8e4 123 skip "no mode checks", 1 if $skip_mode_checks;
20dd405c 124
bbf171ae
GH
125# if ($^O eq 'cygwin') { # new files on cygwin get rwx instead of rw-
126# is($mode & 0777, 0777, "mode of triply-linked file");
127# } else {
dd568cb6
CB
128 is(sprintf("0%o", $mode & 0777),
129 sprintf("0%o", $a_mode & 0777),
130 "mode of triply-linked file");
bbf171ae 131# }
0c5d4ba3
JH
132 }
133}
8d063cd8 134
2986a63f 135$newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
8d063cd8 136
0c5d4ba3 137is(chmod($newmode,'a'), 1, "chmod succeeding");
8d063cd8 138
0c5d4ba3 139SKIP: {
2f3b333f 140 skip("no link", 7) unless $has_link;
0c5d4ba3 141
8268670f
JH
142 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
143 $blksize,$blocks) = stat('c');
0c5d4ba3 144
4e51f8e4
SR
145 SKIP: {
146 skip "no mode checks", 1 if $skip_mode_checks;
147
148 is($mode & 0777, $newmode, "chmod going through");
149 }
0c5d4ba3
JH
150
151 $newmode = 0700;
6b980173
JD
152 chmod 0444, 'x';
153 $newmode = 0666;
6b980173 154
0c5d4ba3 155 is(chmod($newmode,'c','x'), 2, "chmod two files");
3ed9f8f7 156
0c5d4ba3
JH
157 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
158 $blksize,$blocks) = stat('c');
8d063cd8 159
4e51f8e4
SR
160 SKIP: {
161 skip "no mode checks", 1 if $skip_mode_checks;
162
163 is($mode & 0777, $newmode, "chmod going through to c");
164 }
a245ea2d 165
0c5d4ba3
JH
166 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
167 $blksize,$blocks) = stat('x');
8d063cd8 168
4e51f8e4
SR
169 SKIP: {
170 skip "no mode checks", 1 if $skip_mode_checks;
171
172 is($mode & 0777, $newmode, "chmod going through to x");
173 }
0c5d4ba3
JH
174
175 is(unlink('b','x'), 2, "unlink two files");
176
177 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
178 $blksize,$blocks) = stat('b');
179
180 is($ino, undef, "ino of removed file b should be undef");
181
182 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
183 $blksize,$blocks) = stat('x');
184
185 is($ino, undef, "ino of removed file x should be undef");
8268670f 186}
0c5d4ba3 187
c4aca7d0 188SKIP: {
8334cae6 189 skip "no fchmod", 7 unless ($Config{d_fchmod} || "") eq "define";
c4aca7d0
GA
190 ok(open(my $fh, "<", "a"), "open a");
191 is(chmod(0, $fh), 1, "fchmod");
192 $mode = (stat "a")[2];
b0fdffbd
SP
193 SKIP: {
194 skip "no mode checks", 1 if $skip_mode_checks;
195 is($mode & 0777, 0, "perm reset");
196 }
c4aca7d0
GA
197 is(chmod($newmode, "a"), 1, "fchmod");
198 $mode = (stat $fh)[2];
b0fdffbd
SP
199 SKIP: {
200 skip "no mode checks", 1 if $skip_mode_checks;
201 is($mode & 0777, $newmode, "perm restored");
202 }
8334cae6
TC
203
204 # [perl #122703]
205 close $fh;
206 $! = 0;
207 ok(!chmod(0666, $fh), "chmod through closed handle fails");
208 isnt($!+0, 0, "and errno was set");
c4aca7d0
GA
209}
210
211SKIP: {
8334cae6 212 skip "no fchown", 3 unless ($Config{d_fchown} || "") eq "define";
c4aca7d0
GA
213 open(my $fh, "<", "a");
214 is(chown(-1, -1, $fh), 1, "fchown");
8334cae6
TC
215
216 # [perl #122703]
217 # chown() behaved correctly, but there was no test for the chown()
218 # on closed handle case
219 close $fh;
220 $! = 0;
221 ok(!chown(-1, -1, $fh), "chown on closed handle fails");
222 isnt($!+0, 0, "and errno was set");
c4aca7d0
GA
223}
224
225SKIP: {
226 skip "has fchmod", 1 if ($Config{d_fchmod} || "") eq "define";
227 open(my $fh, "<", "a");
228 eval { chmod(0777, $fh); };
229 like($@, qr/^The fchmod function is unimplemented at/, "fchmod is unimplemented");
230}
231
232SKIP: {
233 skip "has fchown", 1 if ($Config{d_fchown} || "") eq "define";
234 open(my $fh, "<", "a");
235 eval { chown(0, 0, $fh); };
295d5f02 236 like($@, qr/^The f?chown function is unimplemented at/, "fchown is unimplemented");
c4aca7d0
GA
237}
238
8268670f 239is(rename('a','b'), 1, "rename a b");
0c5d4ba3 240
8268670f
JH
241($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
242 $blksize,$blocks) = stat('a');
0c5d4ba3 243
8268670f 244is($ino, undef, "ino of renamed file a should be undef");
0c5d4ba3
JH
245
246$delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
1d825fcc 247chmod 0777, 'b';
0c5d4ba3 248
e96b369d 249$foo = (utime 500000000,500000000 + $delta,'b');
0c5d4ba3 250is($foo, 1, "utime");
e96b369d
GA
251check_utime_result();
252
253utime undef, undef, 'b';
254($atime,$mtime) = (stat 'b')[8,9];
255print "# utime undef, undef --> $atime, $mtime\n";
256isnt($atime, 500000000, 'atime');
257isnt($mtime, 500000000 + $delta, 'mtime');
258
259SKIP: {
8334cae6 260 skip "no futimes", 6 unless ($Config{d_futimes} || "") eq "define";
e96b369d
GA
261 open(my $fh, "<", 'b');
262 $foo = (utime 500000000,500000000 + $delta, $fh);
263 is($foo, 1, "futime");
264 check_utime_result();
8334cae6
TC
265 # [perl #122703]
266 close $fh;
267 ok(!utime(500000000,500000000 + $delta, $fh),
268 "utime fails on a closed file handle");
269 isnt($!+0, 0, "and errno was set");
e96b369d 270}
0c5d4ba3 271
e96b369d
GA
272
273sub check_utime_result {
fc8d54b0
GA
274 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
275 $blksize,$blocks) = stat('b');
0c5d4ba3 276
fc8d54b0
GA
277 SKIP: {
278 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
20dd405c 279
fc8d54b0
GA
280 ok($ino, 'non-zero inode num');
281 }
0c5d4ba3 282
fc8d54b0
GA
283 SKIP: {
284 skip "filesystem atime/mtime granularity too low", 2
285 unless $accurate_timestamps;
20dd405c 286
701c441c
PG
287 if ($^O eq 'vos') {
288 skip ("# TODO - hit VOS bug posix-2055 - access time does not follow POSIX rules for an open file.", 2);
289 }
290
fc8d54b0
GA
291 print "# atime - $atime mtime - $mtime delta - $delta\n";
292 if($atime == 500000000 && $mtime == 500000000 + $delta) {
293 pass('atime');
294 pass('mtime');
295 }
296 else {
297 if ($^O =~ /\blinux\b/i) {
298 print "# Maybe stat() cannot get the correct atime, ".
299 "as happens via NFS on linux?\n";
300 $foo = (utime 400000000,500000000 + 2*$delta,'b');
301 my ($new_atime, $new_mtime) = (stat('b'))[8,9];
302 print "# newatime - $new_atime nemtime - $new_mtime\n";
303 if ($new_atime == $atime && $new_mtime - $mtime == $delta) {
304 pass("atime - accounted for possible NFS/glibc2.2 bug on linux");
305 pass("mtime - accounted for possible NFS/glibc2.2 bug on linux");
306 }
307 else {
308 fail("atime - $atime/$new_atime $mtime/$new_mtime");
309 fail("mtime - $atime/$new_atime $mtime/$new_mtime");
310 }
311 }
312 elsif ($^O eq 'VMS') {
313 # why is this 1 second off?
314 is( $atime, 500000001, 'atime' );
315 is( $mtime, 500000000 + $delta, 'mtime' );
316 }
b6c36746 317 elsif ($^O eq 'haiku') {
fc8d54b0
GA
318 SKIP: {
319 skip "atime not updated", 1;
320 }
321 is($mtime, 500000001, 'mtime');
322 }
323 else {
324 fail("atime");
325 fail("mtime");
326 }
327 }
0c5d4ba3 328 }
6d738113 329}
e96b369d
GA
330
331SKIP: {
332 skip "has futimes", 1 if ($Config{d_futimes} || "") eq "define";
333 open(my $fh, "<", "b") || die;
334 eval { utime(undef, undef, $fh); };
335 like($@, qr/^The futimes function is unimplemented at/, "futimes is unimplemented");
336}
0c5d4ba3
JH
337
338is(unlink('b'), 1, "unlink b");
339
8d063cd8
LW
340($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
341 $blksize,$blocks) = stat('b');
0c5d4ba3 342is($ino, undef, "ino of unlinked file b should be undef");
378cc40b
LW
343unlink 'c';
344
b844a53a 345chdir $wd || die "Can't cd back to '$wd' ($!)";
378cc40b 346
0c5d4ba3
JH
347# Yet another way to look for links (perhaps those that cannot be
348# created by perl?). Hopefully there is an ls utility in your
349# %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
350
20dd405c
MS
351SKIP: {
352 skip "Win32/Netware specific test", 2
353 unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
3ed9f8f7 354 skip "No symbolic links found to test with", 2
20dd405c
MS
355 unless `ls -l perl 2>nul` =~ /^l.*->/;
356
4ba7095c
JH
357 system("cp TEST TEST$$");
358 # we have to copy because e.g. GNU grep gets huffy if we have
359 # a symlink forest to another disk (it complains about too many
360 # levels of symbolic links, even if we have only two)
0c5d4ba3 361 is(symlink("TEST$$","c"), 1, "symlink");
4ba7095c 362 $foo = `grep perl c 2>&1`;
0c5d4ba3 363 ok($foo, "found perl in c");
44a8e56a 364 unlink 'c';
4ba7095c 365 unlink("TEST$$");
378cc40b 366}
f783569b 367
62a28c97
NC
368my $tmpfile = tempfile();
369open IOFSCOM, ">$tmpfile" or die "Could not write IOfs.tmp: $!";
0c5d4ba3
JH
370print IOFSCOM 'helloworld';
371close(IOFSCOM);
372
373# TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
374# as per UNIX FAQ.
375
376SKIP: {
63720136 377# Check truncating a closed file.
62a28c97 378 eval { truncate $tmpfile, 5; };
90ddc76f 379
090bf15b 380 skip("no truncate - $@", 8) if $@;
0c5d4ba3 381
62a28c97 382 is(-s $tmpfile, 5, "truncation to five bytes");
0c5d4ba3 383
62a28c97 384 truncate $tmpfile, 0;
0c5d4ba3 385
62a28c97 386 ok(-z $tmpfile, "truncation to zero bytes");
0c5d4ba3 387
7a2cf369
NK
388#these steps are necessary to check if file is really truncated
389#On Win95, FH is updated, but file properties aren't
62a28c97 390 open(FH, ">$tmpfile") or die "Can't create $tmpfile";
7a2cf369
NK
391 print FH "x\n" x 200;
392 close FH;
393
63720136 394# Check truncating an open file.
62a28c97 395 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
0c5d4ba3
JH
396
397 binmode FH;
398 select FH;
399 $| = 1;
400 select STDOUT;
401
402 {
403 use strict;
404 print FH "x\n" x 200;
405 ok(truncate(FH, 200), "fh resize to 200");
62b86938 406 }
0c5d4ba3
JH
407
408 if ($needs_fh_reopen) {
62a28c97 409 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
0c5d4ba3 410 }
90ddc76f 411
62a28c97 412 is(-s $tmpfile, 200, "fh resize to 200 working (filename check)");
63720136 413
090bf15b 414 ok(truncate(FH, 0), "fh resize to zero");
0c5d4ba3 415
090bf15b 416 if ($needs_fh_reopen) {
62a28c97 417 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
090bf15b 418 }
0c5d4ba3 419
62a28c97 420 ok(-z $tmpfile, "fh resize to zero working (filename check)");
7a2cf369 421
090bf15b
SR
422 close FH;
423
62a28c97 424 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
090bf15b
SR
425
426 binmode FH;
427 select FH;
428 $| = 1;
429 select STDOUT;
430
431 {
432 use strict;
433 print FH "x\n" x 200;
434 ok(truncate(*FH{IO}, 100), "fh resize by IO slot");
435 }
436
437 if ($needs_fh_reopen) {
62a28c97 438 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
090bf15b
SR
439 }
440
62a28c97 441 is(-s $tmpfile, 100, "fh resize by IO slot working");
090bf15b
SR
442
443 close FH;
42409c40
FC
444
445 my $n = "for_fs_dot_t$$";
446 open FH, ">$n" or die "open $n: $!";
447 print FH "bloh blah bla\n";
448 close FH or die "close $n: $!";
449 eval "truncate $n, 0; 1" or die;
450 ok !-z $n, 'truncate(word) does not fall back to file name';
451 unlink $n;
f783569b 452}
80252599 453
65cb15a1 454# check if rename() can be used to just change case of filename
20dd405c
MS
455SKIP: {
456 skip "Works in Cygwin only if check_case is set to relaxed", 1
1a36314c 457 if ($ENV{'CYGWIN'} && ($ENV{'CYGWIN'} =~ /check_case:(?:adjust|strict)/));
20dd405c 458
62a28c97 459 chdir "./$tmpdir";
d5fc3e70
SP
460 open(FH,'>x') || die "Can't create x";
461 close(FH);
0c5d4ba3 462 rename('x', 'X');
3ed9f8f7 463
0c5d4ba3
JH
464 # this works on win32 only, because fs isn't casesensitive
465 ok(-e 'X', "rename working");
8268670f 466
9bf0f160 467 unlink_all 'X';
0c5d4ba3 468 chdir $wd || die "Can't cd back to $wd";
73077d53 469}
65cb15a1 470
80252599 471# check if rename() works on directories
0c5d4ba3 472if ($^O eq 'VMS') {
9df548ee 473 # must have delete access to rename a directory
62a28c97 474 `set file $tmpdir.dir/protection=o:d`;
7aa55bb4 475 ok(rename("$tmpdir.dir", "$tmpdir1.dir"), "rename on directories") ||
20dd405c 476 print "# errno: $!\n";
fc8d54b0
GA
477}
478else {
62a28c97 479 ok(rename($tmpdir, $tmpdir1), "rename on directories");
6d738113 480}
0c5d4ba3 481
62a28c97 482ok(-d $tmpdir1, "rename on directories working");
80252599 483
1937c63e
TS
484{
485 # Change 26011: Re: A surprising segfault
486 # to make sure only that these obfuscated sentences will not crash.
487
488 map chmod(+()), ('')x68;
489 ok(1, "extend sp in pp_chmod");
490
491 map chown(+()), ('')x68;
492 ok(1, "extend sp in pp_chown");
493}
494
f0d85c30 495# Calling unlink on a directory without -U and privileges will always fail, but
1dcae8b8 496# it should set errno to EISDIR even though unlink(2) is never called.
af7058d0
FC
497SKIP: {
498 if (is_miniperl && !eval 'require Errno') {
499 skip "Errno not built yet", 3;
500 }
1dcae8b8
EZ
501 require Errno;
502
503 my $tmpdir = tempfile();
504 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
505 `mkdir $tmpdir`;
506 }
507 elsif ($^O eq 'VMS') {
508 `create/directory [.$tmpdir]`;
509 }
510 else {
511 `mkdir $tmpdir 2>/dev/null`;
512 }
513
514 # errno should be set even though unlink(2) is not called
515 local $!;
f0d85c30
CB
516 is(unlink($tmpdir), 0, "can't unlink directory without -U and privileges");
517 is(0+$!, Errno::EISDIR(), "unlink directory without -U sets errno");
1dcae8b8
EZ
518
519 rmdir $tmpdir;
520
521 # errno should be set by failed lstat(2) call
522 $! = 0;
523 unlink($tmpdir);
f0d85c30 524 is(0+$!, Errno::ENOENT(), "unlink non-existent directory without -U sets ENOENT");
1dcae8b8
EZ
525}
526
62a28c97
NC
527# need to remove $tmpdir if rename() in test 28 failed!
528END { rmdir $tmpdir1; rmdir $tmpdir; }