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 delete/nolog/noconfirm tmp.dir;`;
62 `create/directory [.tmp]`;
65 rmdir "tmp"; mkdir "tmp";
68 `rm -f tmp 2>/dev/null; mkdir tmp 2>/dev/null`;
71 chdir catdir(curdir(), 'tmp');
73 `/bin/rm -rf a b c x` if -x '/bin/rm';
78 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare') || ($^O eq 'epoc') || $Is_MacOS;
80 is((umask(0)&0777), 022, 'umask'),
83 open(FH,'>x') || die "Can't create x";
85 open(FH,'>a') || die "Can't create a";
88 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
92 skip("no link", 4) unless $has_link;
94 ok(link('a','b'), "link a b");
95 ok(link('b','c'), "link b c");
97 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
98 $blksize,$blocks) = stat('c');
101 skip "no nlink", 1 if $Config{dont_use_nlink};
103 is($nlink, 3, "link count of triply-linked file");
107 skip "hard links not that hard in $^O", 1 if $^O eq 'amigaos';
108 skip "no mode checks", 1 if $skip_mode_checks;
110 # if ($^O eq 'cygwin') { # new files on cygwin get rwx instead of rw-
111 # is($mode & 0777, 0777, "mode of triply-linked file");
113 is($mode & 0777, 0666, "mode of triply-linked file");
118 $newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
120 is(chmod($newmode,'a'), 1, "chmod succeeding");
123 skip("no link", 7) unless $has_link;
125 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
126 $blksize,$blocks) = stat('c');
129 skip "no mode checks", 1 if $skip_mode_checks;
131 is($mode & 0777, $newmode, "chmod going through");
138 is(chmod($newmode,'c','x'), 2, "chmod two files");
140 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
141 $blksize,$blocks) = stat('c');
144 skip "no mode checks", 1 if $skip_mode_checks;
146 is($mode & 0777, $newmode, "chmod going through to c");
149 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
150 $blksize,$blocks) = stat('x');
153 skip "no mode checks", 1 if $skip_mode_checks;
155 is($mode & 0777, $newmode, "chmod going through to x");
158 is(unlink('b','x'), 2, "unlink two files");
160 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
161 $blksize,$blocks) = stat('b');
163 is($ino, undef, "ino of removed file b should be undef");
165 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
166 $blksize,$blocks) = stat('x');
168 is($ino, undef, "ino of removed file x should be undef");
172 skip "no fchmod", 5 unless ($Config{d_fchmod} || "") eq "define";
173 ok(open(my $fh, "<", "a"), "open a");
174 is(chmod(0, $fh), 1, "fchmod");
175 $mode = (stat "a")[2];
177 skip "no mode checks", 1 if $skip_mode_checks;
178 is($mode & 0777, 0, "perm reset");
180 is(chmod($newmode, "a"), 1, "fchmod");
181 $mode = (stat $fh)[2];
183 skip "no mode checks", 1 if $skip_mode_checks;
184 is($mode & 0777, $newmode, "perm restored");
189 skip "no fchown", 1 unless ($Config{d_fchown} || "") eq "define";
190 open(my $fh, "<", "a");
191 is(chown(-1, -1, $fh), 1, "fchown");
195 skip "has fchmod", 1 if ($Config{d_fchmod} || "") eq "define";
196 open(my $fh, "<", "a");
197 eval { chmod(0777, $fh); };
198 like($@, qr/^The fchmod function is unimplemented at/, "fchmod is unimplemented");
202 skip "has fchown", 1 if ($Config{d_fchown} || "") eq "define";
203 open(my $fh, "<", "a");
204 eval { chown(0, 0, $fh); };
205 like($@, qr/^The fchown function is unimplemented at/, "fchown is unimplemented");
208 is(rename('a','b'), 1, "rename a b");
210 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
211 $blksize,$blocks) = stat('a');
213 is($ino, undef, "ino of renamed file a should be undef");
215 $delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
218 $foo = (utime 500000000,500000000 + $delta,'b');
219 is($foo, 1, "utime");
220 check_utime_result();
222 utime undef, undef, 'b';
223 ($atime,$mtime) = (stat 'b')[8,9];
224 print "# utime undef, undef --> $atime, $mtime\n";
225 isnt($atime, 500000000, 'atime');
226 isnt($mtime, 500000000 + $delta, 'mtime');
229 skip "no futimes", 4 unless ($Config{d_futimes} || "") eq "define";
230 open(my $fh, "<", 'b');
231 $foo = (utime 500000000,500000000 + $delta, $fh);
232 is($foo, 1, "futime");
233 check_utime_result();
237 sub check_utime_result {
238 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
239 $blksize,$blocks) = stat('b');
242 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
244 ok($ino, 'non-zero inode num');
248 skip "filesystem atime/mtime granularity too low", 2
249 unless $accurate_timestamps;
251 print "# atime - $atime mtime - $mtime delta - $delta\n";
252 if($atime == 500000000 && $mtime == 500000000 + $delta) {
257 if ($^O =~ /\blinux\b/i) {
258 print "# Maybe stat() cannot get the correct atime, ".
259 "as happens via NFS on linux?\n";
260 $foo = (utime 400000000,500000000 + 2*$delta,'b');
261 my ($new_atime, $new_mtime) = (stat('b'))[8,9];
262 print "# newatime - $new_atime nemtime - $new_mtime\n";
263 if ($new_atime == $atime && $new_mtime - $mtime == $delta) {
264 pass("atime - accounted for possible NFS/glibc2.2 bug on linux");
265 pass("mtime - accounted for possible NFS/glibc2.2 bug on linux");
268 fail("atime - $atime/$new_atime $mtime/$new_mtime");
269 fail("mtime - $atime/$new_atime $mtime/$new_mtime");
272 elsif ($^O eq 'VMS') {
273 # why is this 1 second off?
274 is( $atime, 500000001, 'atime' );
275 is( $mtime, 500000000 + $delta, 'mtime' );
277 elsif ($^O eq 'beos') {
279 skip "atime not updated", 1;
281 is($mtime, 500000001, 'mtime');
292 skip "has futimes", 1 if ($Config{d_futimes} || "") eq "define";
293 open(my $fh, "<", "b") || die;
294 eval { utime(undef, undef, $fh); };
295 like($@, qr/^The futimes function is unimplemented at/, "futimes is unimplemented");
298 is(unlink('b'), 1, "unlink b");
300 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
301 $blksize,$blocks) = stat('b');
302 is($ino, undef, "ino of unlinked file b should be undef");
305 chdir $wd || die "Can't cd back to $wd";
307 # Yet another way to look for links (perhaps those that cannot be
308 # created by perl?). Hopefully there is an ls utility in your
309 # %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
312 skip "Win32/Netware specific test", 2
313 unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
314 skip "No symbolic links found to test with", 2
315 unless `ls -l perl 2>nul` =~ /^l.*->/;
317 system("cp TEST TEST$$");
318 # we have to copy because e.g. GNU grep gets huffy if we have
319 # a symlink forest to another disk (it complains about too many
320 # levels of symbolic links, even if we have only two)
321 is(symlink("TEST$$","c"), 1, "symlink");
322 $foo = `grep perl c 2>&1`;
323 ok($foo, "found perl in c");
329 open IOFSCOM, ">Iofs.tmp" or die "Could not write IOfs.tmp: $!";
330 print IOFSCOM 'helloworld';
333 # TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
337 # Check truncating a closed file.
338 eval { truncate "Iofs.tmp", 5; };
340 skip("no truncate - $@", 8) if $@;
342 is(-s "Iofs.tmp", 5, "truncation to five bytes");
344 truncate "Iofs.tmp", 0;
346 ok(-z "Iofs.tmp", "truncation to zero bytes");
348 #these steps are necessary to check if file is really truncated
349 #On Win95, FH is updated, but file properties aren't
350 open(FH, ">Iofs.tmp") or die "Can't create Iofs.tmp";
351 print FH "x\n" x 200;
354 # Check truncating an open file.
355 open(FH, ">>Iofs.tmp") or die "Can't open Iofs.tmp for appending";
364 print FH "x\n" x 200;
365 ok(truncate(FH, 200), "fh resize to 200");
368 if ($needs_fh_reopen) {
369 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
374 skip ("# TODO - hit VOS bug posix-973 - cannot resize an open file below the current file pos.", 5);
377 is(-s "Iofs.tmp", 200, "fh resize to 200 working (filename check)");
379 ok(truncate(FH, 0), "fh resize to zero");
381 if ($needs_fh_reopen) {
382 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
385 ok(-z "Iofs.tmp", "fh resize to zero working (filename check)");
389 open(FH, ">>Iofs.tmp") or die "Can't open Iofs.tmp for appending";
398 print FH "x\n" x 200;
399 ok(truncate(*FH{IO}, 100), "fh resize by IO slot");
402 if ($needs_fh_reopen) {
403 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
406 is(-s "Iofs.tmp", 100, "fh resize by IO slot working");
412 # check if rename() can be used to just change case of filename
414 skip "Works in Cygwin only if check_case is set to relaxed", 1
418 open(FH,'>x') || die "Can't create x";
422 # this works on win32 only, because fs isn't casesensitive
423 ok(-e 'X', "rename working");
426 chdir $wd || die "Can't cd back to $wd";
429 # check if rename() works on directories
431 # must have delete access to rename a directory
432 `set file tmp.dir/protection=o:d`;
433 ok(rename('tmp.dir', 'tmp1.dir'), "rename on directories") ||
434 print "# errno: $!\n";
437 ok(rename('tmp', 'tmp1'), "rename on directories");
440 ok(-d 'tmp1', "rename on directories working");
443 # Change 26011: Re: A surprising segfault
444 # to make sure only that these obfuscated sentences will not crash.
446 map chmod(+()), ('')x68;
447 ok(1, "extend sp in pp_chmod");
449 map chown(+()), ('')x68;
450 ok(1, "extend sp in pp_chown");
453 # need to remove 'tmp' if rename() in test 28 failed!
454 END { rmdir 'tmp1'; rmdir 'tmp'; 1 while unlink "Iofs.tmp"; }