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