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