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