10 use File::Spec::Functions;
12 my $Is_MacOS = ($^O eq 'MacOS');
13 my $Is_VMSish = ($^O eq 'VMS');
15 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
18 elsif ($^O eq 'VMS') {
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}/# ||
35 if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
36 if (Win32::FsType() eq 'NTFS') {
38 $accurate_timestamps = 1;
44 # Not needed on HPFS, but needed on HPFS386 ?!
47 $needs_fh_reopen = 1 if (defined &Win32::IsWin95 && Win32::IsWin95());
49 my $skip_mode_checks =
50 $^O eq 'cygwin' && $ENV{CYGWIN} !~ /ntsec/;
55 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
56 `rmdir /s /q tmp 2>nul`;
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]`;
66 rmdir "tmp"; mkdir "tmp";
69 `rm -f tmp 2>/dev/null; mkdir tmp 2>/dev/null`;
72 chdir catdir(curdir(), 'tmp');
74 `/bin/rm -rf a b c x` if -x '/bin/rm';
79 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare') || ($^O eq 'epoc') || $Is_MacOS;
81 is((umask(0)&0777), 022, 'umask'),
84 open(FH,'>x') || die "Can't create x";
86 open(FH,'>a') || die "Can't create a";
89 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
90 $blksize,$blocks,$a_mode);
93 skip("no link", 4) unless $has_link;
95 ok(link('a','b'), "link a b");
96 ok(link('b','c'), "link b c");
98 $a_mode = (stat('a'))[2];
100 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
101 $blksize,$blocks) = stat('c');
104 skip "no nlink", 1 if $Config{dont_use_nlink};
106 is($nlink, 3, "link count of triply-linked file");
110 skip "hard links not that hard in $^O", 1 if $^O eq 'amigaos';
111 skip "no mode checks", 1 if $skip_mode_checks;
113 # if ($^O eq 'cygwin') { # new files on cygwin get rwx instead of rw-
114 # is($mode & 0777, 0777, "mode of triply-linked file");
116 is(sprintf("0%o", $mode & 0777),
117 sprintf("0%o", $a_mode & 0777),
118 "mode of triply-linked file");
123 $newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
125 is(chmod($newmode,'a'), 1, "chmod succeeding");
128 skip("no link", 7) unless $has_link;
130 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
131 $blksize,$blocks) = stat('c');
134 skip "no mode checks", 1 if $skip_mode_checks;
136 is($mode & 0777, $newmode, "chmod going through");
143 is(chmod($newmode,'c','x'), 2, "chmod two files");
145 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
146 $blksize,$blocks) = stat('c');
149 skip "no mode checks", 1 if $skip_mode_checks;
151 is($mode & 0777, $newmode, "chmod going through to c");
154 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
155 $blksize,$blocks) = stat('x');
158 skip "no mode checks", 1 if $skip_mode_checks;
160 is($mode & 0777, $newmode, "chmod going through to x");
163 is(unlink('b','x'), 2, "unlink two files");
165 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
166 $blksize,$blocks) = stat('b');
168 is($ino, undef, "ino of removed file b should be undef");
170 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
171 $blksize,$blocks) = stat('x');
173 is($ino, undef, "ino of removed file x should be undef");
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];
182 skip "no mode checks", 1 if $skip_mode_checks;
183 is($mode & 0777, 0, "perm reset");
185 is(chmod($newmode, "a"), 1, "fchmod");
186 $mode = (stat $fh)[2];
188 skip "no mode checks", 1 if $skip_mode_checks;
189 is($mode & 0777, $newmode, "perm restored");
194 skip "no fchown", 1 unless ($Config{d_fchown} || "") eq "define";
195 open(my $fh, "<", "a");
196 is(chown(-1, -1, $fh), 1, "fchown");
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");
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");
213 is(rename('a','b'), 1, "rename a b");
215 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
216 $blksize,$blocks) = stat('a');
218 is($ino, undef, "ino of renamed file a should be undef");
220 $delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
223 $foo = (utime 500000000,500000000 + $delta,'b');
224 is($foo, 1, "utime");
225 check_utime_result();
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');
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();
242 sub check_utime_result {
243 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
244 $blksize,$blocks) = stat('b');
247 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
249 ok($ino, 'non-zero inode num');
253 skip "filesystem atime/mtime granularity too low", 2
254 unless $accurate_timestamps;
256 print "# atime - $atime mtime - $mtime delta - $delta\n";
257 if($atime == 500000000 && $mtime == 500000000 + $delta) {
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");
273 fail("atime - $atime/$new_atime $mtime/$new_mtime");
274 fail("mtime - $atime/$new_atime $mtime/$new_mtime");
277 elsif ($^O eq 'VMS') {
278 # why is this 1 second off?
279 is( $atime, 500000001, 'atime' );
280 is( $mtime, 500000000 + $delta, 'mtime' );
282 elsif ($^O eq 'beos') {
284 skip "atime not updated", 1;
286 is($mtime, 500000001, 'mtime');
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");
303 is(unlink('b'), 1, "unlink b");
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");
310 chdir $wd || die "Can't cd back to $wd";
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.
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.*->/;
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");
334 open IOFSCOM, ">Iofs.tmp" or die "Could not write IOfs.tmp: $!";
335 print IOFSCOM 'helloworld';
338 # TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
342 # Check truncating a closed file.
343 eval { truncate "Iofs.tmp", 5; };
345 skip("no truncate - $@", 8) if $@;
347 is(-s "Iofs.tmp", 5, "truncation to five bytes");
349 truncate "Iofs.tmp", 0;
351 ok(-z "Iofs.tmp", "truncation to zero bytes");
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;
359 # Check truncating an open file.
360 open(FH, ">>Iofs.tmp") or die "Can't open Iofs.tmp for appending";
369 print FH "x\n" x 200;
370 ok(truncate(FH, 200), "fh resize to 200");
373 if ($needs_fh_reopen) {
374 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
379 skip ("# TODO - hit VOS bug posix-973 - cannot resize an open file below the current file pos.", 5);
382 is(-s "Iofs.tmp", 200, "fh resize to 200 working (filename check)");
384 ok(truncate(FH, 0), "fh resize to zero");
386 if ($needs_fh_reopen) {
387 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
390 ok(-z "Iofs.tmp", "fh resize to zero working (filename check)");
394 open(FH, ">>Iofs.tmp") or die "Can't open Iofs.tmp for appending";
403 print FH "x\n" x 200;
404 ok(truncate(*FH{IO}, 100), "fh resize by IO slot");
407 if ($needs_fh_reopen) {
408 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
411 is(-s "Iofs.tmp", 100, "fh resize by IO slot working");
417 # check if rename() can be used to just change case of filename
419 skip "Works in Cygwin only if check_case is set to relaxed", 1
420 if ($ENV{'CYGWIN'} && ($ENV{'CYGWIN'} =~ /check_case:(?:adjust|strict)/));
423 open(FH,'>x') || die "Can't create x";
427 # this works on win32 only, because fs isn't casesensitive
428 ok(-e 'X', "rename working");
431 chdir $wd || die "Can't cd back to $wd";
434 # check if rename() works on directories
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";
442 ok(rename('tmp', 'tmp1'), "rename on directories");
445 ok(-d 'tmp1', "rename on directories working");
448 # Change 26011: Re: A surprising segfault
449 # to make sure only that these obfuscated sentences will not crash.
451 map chmod(+()), ('')x68;
452 ok(1, "extend sp in pp_chmod");
454 map chown(+()), ('')x68;
455 ok(1, "extend sp in pp_chown");
458 # need to remove 'tmp' if rename() in test 28 failed!
459 END { rmdir 'tmp1'; rmdir 'tmp'; 1 while unlink "Iofs.tmp"; }