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