11 my $Is_VMSish = ($^O eq 'VMS');
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;
19 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
22 elsif ($^O eq 'VMS') {
25 elsif ( $^O =~ /android/ || $^O eq 'nto' ) {
26 # On Android and Blackberry 10, pwd is a shell builtin, so plain `pwd`
35 die "Can't get current working directory" if(!$wd);
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}/#
45 if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
46 if (Win32::FsType() eq 'NTFS') {
48 $accurate_timestamps = 1;
57 # Not needed on HPFS, but needed on HPFS386 ?!
60 $needs_fh_reopen = 1 if (defined &Win32::IsWin95 && Win32::IsWin95());
62 my $skip_mode_checks =
63 $^O eq 'cygwin' && $ENV{CYGWIN} !~ /ntsec/;
67 my $tmpdir = tempfile();
68 my $tmpdir1 = tempfile();
70 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
71 `rmdir /s /q $tmpdir 2>nul`;
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]`;
81 `rm -f $tmpdir 2>/dev/null; mkdir $tmpdir 2>/dev/null`;
86 `/bin/rm -rf a b c x` if -x '/bin/rm';
91 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
93 is((umask(0)&0777), 022, 'umask'),
96 open(FH,'>x') || die "Can't create x";
98 open(FH,'>a') || die "Can't create a";
101 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
102 $blksize,$blocks,$a_mode);
105 skip("no link", 4) unless $has_link;
107 ok(link('a','b'), "link a b");
108 ok(link('b','c'), "link b c");
110 $a_mode = (stat('a'))[2];
112 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
113 $blksize,$blocks) = stat('c');
116 skip "no nlink", 1 if $Config{dont_use_nlink};
118 is($nlink, 3, "link count of triply-linked file");
122 skip "hard links not that hard in $^O", 1 if $^O eq 'amigaos';
123 skip "no mode checks", 1 if $skip_mode_checks;
125 is(sprintf("0%o", $mode & 0777),
126 sprintf("0%o", $a_mode & 0777),
127 "mode of triply-linked file");
131 $newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
133 is(chmod($newmode,'a'), 1, "chmod succeeding");
136 skip("no link", 7) unless $has_link;
138 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
139 $blksize,$blocks) = stat('c');
142 skip "no mode checks", 1 if $skip_mode_checks;
144 is($mode & 0777, $newmode, "chmod going through");
151 is(chmod($newmode,'c','x'), 2, "chmod two files");
153 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
154 $blksize,$blocks) = stat('c');
157 skip "no mode checks", 1 if $skip_mode_checks;
159 is($mode & 0777, $newmode, "chmod going through to c");
162 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
163 $blksize,$blocks) = stat('x');
166 skip "no mode checks", 1 if $skip_mode_checks;
168 is($mode & 0777, $newmode, "chmod going through to x");
171 is(unlink('b','x'), 2, "unlink two files");
173 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
174 $blksize,$blocks) = stat('b');
176 is($ino, undef, "ino of removed file b should be undef");
178 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
179 $blksize,$blocks) = stat('x');
181 is($ino, undef, "ino of removed file x should be undef");
185 skip "no fchmod", 7 unless ($Config{d_fchmod} || "") eq "define";
186 ok(open(my $fh, "<", "a"), "open a");
187 is(chmod(0, $fh), 1, "fchmod");
188 $mode = (stat "a")[2];
190 skip "no mode checks", 1 if $skip_mode_checks;
191 skip "chmod(0, FH) means assume user defaults on VMS", 1 if $^O eq 'VMS';
192 is($mode & 0777, 0, "perm reset");
194 is(chmod($newmode, "a"), 1, "fchmod");
195 $mode = (stat $fh)[2];
197 skip "no mode checks", 1 if $skip_mode_checks;
198 is($mode & 0777, $newmode, "perm restored");
204 ok(!chmod(0666, $fh), "chmod through closed handle fails");
205 isnt($!+0, 0, "and errno was set");
209 skip "no fchown", 3 unless ($Config{d_fchown} || "") eq "define";
210 open(my $fh, "<", "a");
211 is(chown(-1, -1, $fh), 1, "fchown");
214 # chown() behaved correctly, but there was no test for the chown()
215 # on closed handle case
218 ok(!chown(-1, -1, $fh), "chown on closed handle fails");
219 isnt($!+0, 0, "and errno was set");
223 skip "has fchmod", 1 if ($Config{d_fchmod} || "") eq "define";
224 open(my $fh, "<", "a");
225 eval { chmod(0777, $fh); };
226 like($@, qr/^The fchmod function is unimplemented at/, "fchmod is unimplemented");
230 skip "has fchown", 1 if ($Config{d_fchown} || "") eq "define";
231 open(my $fh, "<", "a");
232 eval { chown(0, 0, $fh); };
233 like($@, qr/^The f?chown function is unimplemented at/, "fchown is unimplemented");
236 is(rename('a','b'), 1, "rename a b");
238 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
239 $blksize,$blocks) = stat('a');
241 is($ino, undef, "ino of renamed file a should be undef");
243 $delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
248 note("basic check of atime and mtime");
249 $foo = (utime $ut,$ut + $delta,'b');
250 is($foo, 1, "utime");
251 check_utime_result();
253 utime undef, undef, 'b';
254 ($atime,$mtime) = (stat 'b')[8,9];
255 note("# utime undef, undef --> $atime, $mtime");
256 isnt($atime, $ut, 'atime: utime called with two undefs');
257 isnt($mtime, $ut + $delta, 'mtime: utime called with two undefs');
260 skip "no futimes", 6 unless ($Config{d_futimes} || "") eq "define";
261 note("check futimes");
262 open(my $fh, "<", 'b');
263 $foo = (utime $ut,$ut + $delta, $fh);
264 is($foo, 1, "futime");
265 check_utime_result();
268 ok(!utime($ut,$ut + $delta, $fh),
269 "utime fails on a closed file handle");
270 isnt($!+0, 0, "and errno was set");
274 sub check_utime_result {
275 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
276 $blksize,$blocks) = stat('b');
279 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
281 ok($ino, 'non-zero inode num');
285 skip "filesystem atime/mtime granularity too low", 2
286 unless $accurate_timestamps;
289 skip ("# TODO - hit VOS bug posix-2055 - access time does not follow POSIX rules for an open file.", 2);
292 note("# atime - $atime mtime - $mtime delta - $delta");
293 if($atime == $ut && $mtime == $ut + $delta) {
294 pass('atime: granularity test');
295 pass('mtime: granularity test');
298 if ($^O =~ /\blinux\b/i) {
299 note("# Maybe stat() cannot get the correct atime, ".
300 "as happens via NFS on linux?");
301 $foo = (utime 400000000,$ut + 2*$delta,'b');
302 my ($new_atime, $new_mtime) = (stat('b'))[8,9];
303 note("# newatime - $new_atime nemtime - $new_mtime");
304 if ($new_atime == $atime && $new_mtime - $mtime == $delta) {
305 pass("atime - accounted for possible NFS/glibc2.2 bug on linux");
306 pass("mtime - accounted for possible NFS/glibc2.2 bug on linux");
309 fail("atime - $atime/$new_atime $mtime/$new_mtime");
310 fail("mtime - $atime/$new_atime $mtime/$new_mtime");
313 elsif ($^O eq 'VMS') {
314 # why is this 1 second off?
315 is( $atime, $ut + 1, 'atime: VMS' );
316 is( $mtime, $ut + $delta, 'mtime: VMS' );
318 elsif ($^O eq 'haiku') {
320 skip "atime not updated", 1;
322 is($mtime, 500000001, 'mtime');
325 fail("atime: default case");
326 fail("mtime: default case");
333 skip "has futimes", 1 if ($Config{d_futimes} || "") eq "define";
334 open(my $fh, "<", "b") || die;
335 eval { utime(undef, undef, $fh); };
336 like($@, qr/^The futimes function is unimplemented at/, "futimes is unimplemented");
339 is(unlink('b'), 1, "unlink b");
341 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
342 $blksize,$blocks) = stat('b');
343 is($ino, undef, "ino of unlinked file b should be undef");
346 chdir $wd || die "Can't cd back to '$wd' ($!)";
348 # Yet another way to look for links (perhaps those that cannot be
349 # created by perl?). Hopefully there is an ls utility in your
350 # %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
353 skip "Win32/Netware specific test", 2
354 unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
355 skip "No symbolic links found to test with", 2
356 unless `ls -l perl 2>nul` =~ /^l.*->/;
358 system("cp TEST TEST$$");
359 # we have to copy because e.g. GNU grep gets huffy if we have
360 # a symlink forest to another disk (it complains about too many
361 # levels of symbolic links, even if we have only two)
362 is(symlink("TEST$$","c"), 1, "symlink");
363 $foo = `grep perl c 2>&1`;
364 ok($foo, "found perl in c");
369 my $tmpfile = tempfile();
370 open IOFSCOM, ">$tmpfile" or die "Could not write IOfs.tmp: $!";
371 print IOFSCOM 'helloworld';
374 # TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
378 # Check truncating a closed file.
379 eval { truncate $tmpfile, 5; };
381 skip("no truncate - $@", 8) if $@;
383 is(-s $tmpfile, 5, "truncation to five bytes");
385 truncate $tmpfile, 0;
387 ok(-z $tmpfile, "truncation to zero bytes");
389 #these steps are necessary to check if file is really truncated
390 #On Win95, FH is updated, but file properties aren't
391 open(FH, ">$tmpfile") or die "Can't create $tmpfile";
392 print FH "x\n" x 200;
395 # Check truncating an open file.
396 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
405 print FH "x\n" x 200;
406 ok(truncate(FH, 200), "fh resize to 200");
409 if ($needs_fh_reopen) {
410 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
413 is(-s $tmpfile, 200, "fh resize to 200 working (filename check)");
415 ok(truncate(FH, 0), "fh resize to zero");
417 if ($needs_fh_reopen) {
418 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
421 ok(-z $tmpfile, "fh resize to zero working (filename check)");
425 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
434 print FH "x\n" x 200;
435 ok(truncate(*FH{IO}, 100), "fh resize by IO slot");
438 if ($needs_fh_reopen) {
439 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
442 is(-s $tmpfile, 100, "fh resize by IO slot working");
446 my $n = "for_fs_dot_t$$";
447 open FH, ">$n" or die "open $n: $!";
448 print FH "bloh blah bla\n";
449 close FH or die "close $n: $!";
450 eval "truncate $n, 0; 1" or die;
451 ok !-z $n, 'truncate(word) does not fall back to file name';
455 # check if rename() can be used to just change case of filename
457 skip "Works in Cygwin only if check_case is set to relaxed", 1
458 if ($ENV{'CYGWIN'} && ($ENV{'CYGWIN'} =~ /check_case:(?:adjust|strict)/));
461 open(FH,'>x') || die "Can't create x";
465 # this works on win32 only, because fs isn't casesensitive
466 ok(-e 'X', "rename working");
469 chdir $wd || die "Can't cd back to $wd";
475 or skip "Cannot rename directories with link()", 2;
476 # check if rename() works on directories
478 # must have delete access to rename a directory
479 `set file $tmpdir.dir/protection=o:d`;
480 ok(rename("$tmpdir.dir", "$tmpdir1.dir"), "rename on directories") ||
481 print "# errno: $!\n";
484 ok(rename($tmpdir, $tmpdir1), "rename on directories");
487 ok(-d $tmpdir1, "rename on directories working");
491 # Change 26011: Re: A surprising segfault
492 # to make sure only that these obfuscated sentences will not crash.
494 map chmod(+()), ('')x68;
495 ok(1, "extend sp in pp_chmod");
497 map chown(+()), ('')x68;
498 ok(1, "extend sp in pp_chown");
501 # Calling unlink on a directory without -U and privileges will always fail, but
502 # it should set errno to EISDIR even though unlink(2) is never called.
504 if (is_miniperl && !eval 'require Errno') {
505 skip "Errno not built yet", 3;
509 my $tmpdir = tempfile();
510 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
513 elsif ($^O eq 'VMS') {
514 `create/directory [.$tmpdir]`;
517 `mkdir $tmpdir 2>/dev/null`;
520 # errno should be set even though unlink(2) is not called
522 is(unlink($tmpdir), 0, "can't unlink directory without -U and privileges");
523 is(0+$!, Errno::EISDIR(), "unlink directory without -U sets errno");
527 # errno should be set by failed lstat(2) call
530 is(0+$!, Errno::ENOENT(), "unlink non-existent directory without -U sets ENOENT");
533 # need to remove $tmpdir if rename() in test 28 failed!
534 END { rmdir $tmpdir1; rmdir $tmpdir; }