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