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/;
54 my $tmpdir = tempfile();
55 my $tmpdir1 = tempfile();
57 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
58 `rmdir /s /q $tmpdir 2>nul`;
61 elsif ($^O eq 'VMS') {
62 `if f\$search("[.$tmpdir]*.*") .nes. "" then delete/nolog/noconfirm [.$tmpdir]*.*.*`;
63 `if f\$search("$tmpdir.dir") .nes. "" then set file/prot=o:rwed $tmpdir.dir;`;
64 `if f\$search("$tmpdir.dir") .nes. "" then delete/nolog/noconfirm $tmpdir.dir;`;
65 `create/directory [.$tmpdir]`;
68 rmdir "$tmpdir"; mkdir "$tmpdir";
71 `rm -f $tmpdir 2>/dev/null; mkdir $tmpdir 2>/dev/null`;
74 chdir catdir(curdir(), $tmpdir);
76 `/bin/rm -rf a b c x` if -x '/bin/rm';
81 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare') || ($^O eq 'epoc') || $Is_MacOS;
83 is((umask(0)&0777), 022, 'umask'),
86 open(FH,'>x') || die "Can't create x";
88 open(FH,'>a') || die "Can't create a";
91 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
92 $blksize,$blocks,$a_mode);
95 skip("no link", 4) unless $has_link;
97 ok(link('a','b'), "link a b");
98 ok(link('b','c'), "link b c");
100 $a_mode = (stat('a'))[2];
102 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
103 $blksize,$blocks) = stat('c');
106 skip "no nlink", 1 if $Config{dont_use_nlink};
108 is($nlink, 3, "link count of triply-linked file");
112 skip "hard links not that hard in $^O", 1 if $^O eq 'amigaos';
113 skip "no mode checks", 1 if $skip_mode_checks;
115 # if ($^O eq 'cygwin') { # new files on cygwin get rwx instead of rw-
116 # is($mode & 0777, 0777, "mode of triply-linked file");
118 is(sprintf("0%o", $mode & 0777),
119 sprintf("0%o", $a_mode & 0777),
120 "mode of triply-linked file");
125 $newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
127 is(chmod($newmode,'a'), 1, "chmod succeeding");
130 skip("no link", 7) unless $has_link;
132 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
133 $blksize,$blocks) = stat('c');
136 skip "no mode checks", 1 if $skip_mode_checks;
138 is($mode & 0777, $newmode, "chmod going through");
145 is(chmod($newmode,'c','x'), 2, "chmod two files");
147 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
148 $blksize,$blocks) = stat('c');
151 skip "no mode checks", 1 if $skip_mode_checks;
153 is($mode & 0777, $newmode, "chmod going through to c");
156 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
157 $blksize,$blocks) = stat('x');
160 skip "no mode checks", 1 if $skip_mode_checks;
162 is($mode & 0777, $newmode, "chmod going through to x");
165 is(unlink('b','x'), 2, "unlink two files");
167 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
168 $blksize,$blocks) = stat('b');
170 is($ino, undef, "ino of removed file b should be undef");
172 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
173 $blksize,$blocks) = stat('x');
175 is($ino, undef, "ino of removed file x should be undef");
179 skip "no fchmod", 5 unless ($Config{d_fchmod} || "") eq "define";
180 ok(open(my $fh, "<", "a"), "open a");
181 is(chmod(0, $fh), 1, "fchmod");
182 $mode = (stat "a")[2];
184 skip "no mode checks", 1 if $skip_mode_checks;
185 is($mode & 0777, 0, "perm reset");
187 is(chmod($newmode, "a"), 1, "fchmod");
188 $mode = (stat $fh)[2];
190 skip "no mode checks", 1 if $skip_mode_checks;
191 is($mode & 0777, $newmode, "perm restored");
196 skip "no fchown", 1 unless ($Config{d_fchown} || "") eq "define";
197 open(my $fh, "<", "a");
198 is(chown(-1, -1, $fh), 1, "fchown");
202 skip "has fchmod", 1 if ($Config{d_fchmod} || "") eq "define";
203 open(my $fh, "<", "a");
204 eval { chmod(0777, $fh); };
205 like($@, qr/^The fchmod function is unimplemented at/, "fchmod is unimplemented");
209 skip "has fchown", 1 if ($Config{d_fchown} || "") eq "define";
210 open(my $fh, "<", "a");
211 eval { chown(0, 0, $fh); };
212 like($@, qr/^The f?chown function is unimplemented at/, "fchown is unimplemented");
215 is(rename('a','b'), 1, "rename a b");
217 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
218 $blksize,$blocks) = stat('a');
220 is($ino, undef, "ino of renamed file a should be undef");
222 $delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
225 $foo = (utime 500000000,500000000 + $delta,'b');
226 is($foo, 1, "utime");
227 check_utime_result();
229 utime undef, undef, 'b';
230 ($atime,$mtime) = (stat 'b')[8,9];
231 print "# utime undef, undef --> $atime, $mtime\n";
232 isnt($atime, 500000000, 'atime');
233 isnt($mtime, 500000000 + $delta, 'mtime');
236 skip "no futimes", 4 unless ($Config{d_futimes} || "") eq "define";
237 open(my $fh, "<", 'b');
238 $foo = (utime 500000000,500000000 + $delta, $fh);
239 is($foo, 1, "futime");
240 check_utime_result();
244 sub check_utime_result {
245 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
246 $blksize,$blocks) = stat('b');
249 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
251 ok($ino, 'non-zero inode num');
255 skip "filesystem atime/mtime granularity too low", 2
256 unless $accurate_timestamps;
258 print "# atime - $atime mtime - $mtime delta - $delta\n";
259 if($atime == 500000000 && $mtime == 500000000 + $delta) {
264 if ($^O =~ /\blinux\b/i) {
265 print "# Maybe stat() cannot get the correct atime, ".
266 "as happens via NFS on linux?\n";
267 $foo = (utime 400000000,500000000 + 2*$delta,'b');
268 my ($new_atime, $new_mtime) = (stat('b'))[8,9];
269 print "# newatime - $new_atime nemtime - $new_mtime\n";
270 if ($new_atime == $atime && $new_mtime - $mtime == $delta) {
271 pass("atime - accounted for possible NFS/glibc2.2 bug on linux");
272 pass("mtime - accounted for possible NFS/glibc2.2 bug on linux");
275 fail("atime - $atime/$new_atime $mtime/$new_mtime");
276 fail("mtime - $atime/$new_atime $mtime/$new_mtime");
279 elsif ($^O eq 'VMS') {
280 # why is this 1 second off?
281 is( $atime, 500000001, 'atime' );
282 is( $mtime, 500000000 + $delta, 'mtime' );
284 elsif ($^O eq 'beos' || $^O eq 'haiku') {
286 skip "atime not updated", 1;
288 is($mtime, 500000001, 'mtime');
299 skip "has futimes", 1 if ($Config{d_futimes} || "") eq "define";
300 open(my $fh, "<", "b") || die;
301 eval { utime(undef, undef, $fh); };
302 like($@, qr/^The futimes function is unimplemented at/, "futimes is unimplemented");
305 is(unlink('b'), 1, "unlink b");
307 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
308 $blksize,$blocks) = stat('b');
309 is($ino, undef, "ino of unlinked file b should be undef");
312 chdir $wd || die "Can't cd back to $wd";
314 # Yet another way to look for links (perhaps those that cannot be
315 # created by perl?). Hopefully there is an ls utility in your
316 # %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
319 skip "Win32/Netware specific test", 2
320 unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
321 skip "No symbolic links found to test with", 2
322 unless `ls -l perl 2>nul` =~ /^l.*->/;
324 system("cp TEST TEST$$");
325 # we have to copy because e.g. GNU grep gets huffy if we have
326 # a symlink forest to another disk (it complains about too many
327 # levels of symbolic links, even if we have only two)
328 is(symlink("TEST$$","c"), 1, "symlink");
329 $foo = `grep perl c 2>&1`;
330 ok($foo, "found perl in c");
335 my $tmpfile = tempfile();
336 open IOFSCOM, ">$tmpfile" or die "Could not write IOfs.tmp: $!";
337 print IOFSCOM 'helloworld';
340 # TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
344 # Check truncating a closed file.
345 eval { truncate $tmpfile, 5; };
347 skip("no truncate - $@", 8) if $@;
349 is(-s $tmpfile, 5, "truncation to five bytes");
351 truncate $tmpfile, 0;
353 ok(-z $tmpfile, "truncation to zero bytes");
355 #these steps are necessary to check if file is really truncated
356 #On Win95, FH is updated, but file properties aren't
357 open(FH, ">$tmpfile") or die "Can't create $tmpfile";
358 print FH "x\n" x 200;
361 # Check truncating an open file.
362 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
371 print FH "x\n" x 200;
372 ok(truncate(FH, 200), "fh resize to 200");
375 if ($needs_fh_reopen) {
376 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
381 skip ("# TODO - hit VOS bug posix-973 - cannot resize an open file below the current file pos.", 5);
384 is(-s $tmpfile, 200, "fh resize to 200 working (filename check)");
386 ok(truncate(FH, 0), "fh resize to zero");
388 if ($needs_fh_reopen) {
389 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
392 ok(-z $tmpfile, "fh resize to zero working (filename check)");
396 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
405 print FH "x\n" x 200;
406 ok(truncate(*FH{IO}, 100), "fh resize by IO slot");
409 if ($needs_fh_reopen) {
410 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
413 is(-s $tmpfile, 100, "fh resize by IO slot working");
419 # check if rename() can be used to just change case of filename
421 skip "Works in Cygwin only if check_case is set to relaxed", 1
422 if ($ENV{'CYGWIN'} && ($ENV{'CYGWIN'} =~ /check_case:(?:adjust|strict)/));
425 open(FH,'>x') || die "Can't create x";
429 # this works on win32 only, because fs isn't casesensitive
430 ok(-e 'X', "rename working");
433 chdir $wd || die "Can't cd back to $wd";
436 # check if rename() works on directories
438 # must have delete access to rename a directory
439 `set file $tmpdir.dir/protection=o:d`;
440 ok(rename("$tmpdir.dir", "$tmpdir1.dir"), "rename on directories") ||
441 print "# errno: $!\n";
444 ok(rename($tmpdir, $tmpdir1), "rename on directories");
447 ok(-d $tmpdir1, "rename on directories working");
450 # Change 26011: Re: A surprising segfault
451 # to make sure only that these obfuscated sentences will not crash.
453 map chmod(+()), ('')x68;
454 ok(1, "extend sp in pp_chmod");
456 map chown(+()), ('')x68;
457 ok(1, "extend sp in pp_chown");
460 # need to remove $tmpdir if rename() in test 28 failed!
461 END { rmdir $tmpdir1; rmdir $tmpdir; }