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