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