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