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