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