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