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