10 use File::Spec::Functions;
12 my $Is_VMSish = ($^O eq 'VMS');
14 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
17 elsif ($^O eq 'VMS') {
25 my $has_link = $Config{d_link};
26 my $accurate_timestamps =
27 !($^O eq 'MSWin32' || $^O eq 'NetWare' ||
28 $^O eq 'dos' || $^O eq 'os2' ||
29 $^O eq 'cygwin' || $^O eq 'amigaos' ||
30 $wd =~ m#$Config{afsroot}/#
33 if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
34 if (Win32::FsType() eq 'NTFS') {
36 $accurate_timestamps = 1;
42 # Not needed on HPFS, but needed on HPFS386 ?!
45 $needs_fh_reopen = 1 if (defined &Win32::IsWin95 && Win32::IsWin95());
47 my $skip_mode_checks =
48 $^O eq 'cygwin' && $ENV{CYGWIN} !~ /ntsec/;
52 my $tmpdir = tempfile();
53 my $tmpdir1 = tempfile();
55 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
56 `rmdir /s /q $tmpdir 2>nul`;
59 elsif ($^O eq 'VMS') {
60 `if f\$search("[.$tmpdir]*.*") .nes. "" then delete/nolog/noconfirm [.$tmpdir]*.*.*`;
61 `if f\$search("$tmpdir.dir") .nes. "" then set file/prot=o:rwed $tmpdir.dir;`;
62 `if f\$search("$tmpdir.dir") .nes. "" then delete/nolog/noconfirm $tmpdir.dir;`;
63 `create/directory [.$tmpdir]`;
66 `rm -f $tmpdir 2>/dev/null; mkdir $tmpdir 2>/dev/null`;
69 chdir catdir(curdir(), $tmpdir);
71 `/bin/rm -rf a b c x` if -x '/bin/rm';
76 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare') || ($^O eq 'epoc');
78 is((umask(0)&0777), 022, 'umask'),
81 open(FH,'>x') || die "Can't create x";
83 open(FH,'>a') || die "Can't create a";
86 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
87 $blksize,$blocks,$a_mode);
90 skip("no link", 4) unless $has_link;
92 ok(link('a','b'), "link a b");
93 ok(link('b','c'), "link b c");
95 $a_mode = (stat('a'))[2];
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(sprintf("0%o", $mode & 0777),
114 sprintf("0%o", $a_mode & 0777),
115 "mode of triply-linked file");
120 $newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
122 is(chmod($newmode,'a'), 1, "chmod succeeding");
125 skip("no link", 7) unless $has_link;
127 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
128 $blksize,$blocks) = stat('c');
131 skip "no mode checks", 1 if $skip_mode_checks;
133 is($mode & 0777, $newmode, "chmod going through");
140 is(chmod($newmode,'c','x'), 2, "chmod two files");
142 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
143 $blksize,$blocks) = stat('c');
146 skip "no mode checks", 1 if $skip_mode_checks;
148 is($mode & 0777, $newmode, "chmod going through to c");
151 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
152 $blksize,$blocks) = stat('x');
155 skip "no mode checks", 1 if $skip_mode_checks;
157 is($mode & 0777, $newmode, "chmod going through to x");
160 is(unlink('b','x'), 2, "unlink two files");
162 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
163 $blksize,$blocks) = stat('b');
165 is($ino, undef, "ino of removed file b should be undef");
167 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
168 $blksize,$blocks) = stat('x');
170 is($ino, undef, "ino of removed file x should be undef");
174 skip "no fchmod", 5 unless ($Config{d_fchmod} || "") eq "define";
175 ok(open(my $fh, "<", "a"), "open a");
176 is(chmod(0, $fh), 1, "fchmod");
177 $mode = (stat "a")[2];
179 skip "no mode checks", 1 if $skip_mode_checks;
180 is($mode & 0777, 0, "perm reset");
182 is(chmod($newmode, "a"), 1, "fchmod");
183 $mode = (stat $fh)[2];
185 skip "no mode checks", 1 if $skip_mode_checks;
186 is($mode & 0777, $newmode, "perm restored");
191 skip "no fchown", 1 unless ($Config{d_fchown} || "") eq "define";
192 open(my $fh, "<", "a");
193 is(chown(-1, -1, $fh), 1, "fchown");
197 skip "has fchmod", 1 if ($Config{d_fchmod} || "") eq "define";
198 open(my $fh, "<", "a");
199 eval { chmod(0777, $fh); };
200 like($@, qr/^The fchmod function is unimplemented at/, "fchmod is unimplemented");
204 skip "has fchown", 1 if ($Config{d_fchown} || "") eq "define";
205 open(my $fh, "<", "a");
206 eval { chown(0, 0, $fh); };
207 like($@, qr/^The f?chown function is unimplemented at/, "fchown is unimplemented");
210 is(rename('a','b'), 1, "rename a b");
212 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
213 $blksize,$blocks) = stat('a');
215 is($ino, undef, "ino of renamed file a should be undef");
217 $delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
220 $foo = (utime 500000000,500000000 + $delta,'b');
221 is($foo, 1, "utime");
222 check_utime_result();
224 utime undef, undef, 'b';
225 ($atime,$mtime) = (stat 'b')[8,9];
226 print "# utime undef, undef --> $atime, $mtime\n";
227 isnt($atime, 500000000, 'atime');
228 isnt($mtime, 500000000 + $delta, 'mtime');
231 skip "no futimes", 4 unless ($Config{d_futimes} || "") eq "define";
232 open(my $fh, "<", 'b');
233 $foo = (utime 500000000,500000000 + $delta, $fh);
234 is($foo, 1, "futime");
235 check_utime_result();
239 sub check_utime_result {
240 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
241 $blksize,$blocks) = stat('b');
244 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
246 ok($ino, 'non-zero inode num');
250 skip "filesystem atime/mtime granularity too low", 2
251 unless $accurate_timestamps;
253 print "# atime - $atime mtime - $mtime delta - $delta\n";
254 if($atime == 500000000 && $mtime == 500000000 + $delta) {
259 if ($^O =~ /\blinux\b/i) {
260 print "# Maybe stat() cannot get the correct atime, ".
261 "as happens via NFS on linux?\n";
262 $foo = (utime 400000000,500000000 + 2*$delta,'b');
263 my ($new_atime, $new_mtime) = (stat('b'))[8,9];
264 print "# newatime - $new_atime nemtime - $new_mtime\n";
265 if ($new_atime == $atime && $new_mtime - $mtime == $delta) {
266 pass("atime - accounted for possible NFS/glibc2.2 bug on linux");
267 pass("mtime - accounted for possible NFS/glibc2.2 bug on linux");
270 fail("atime - $atime/$new_atime $mtime/$new_mtime");
271 fail("mtime - $atime/$new_atime $mtime/$new_mtime");
274 elsif ($^O eq 'VMS') {
275 # why is this 1 second off?
276 is( $atime, 500000001, 'atime' );
277 is( $mtime, 500000000 + $delta, 'mtime' );
279 elsif ($^O eq 'beos' || $^O eq 'haiku') {
281 skip "atime not updated", 1;
283 is($mtime, 500000001, 'mtime');
294 skip "has futimes", 1 if ($Config{d_futimes} || "") eq "define";
295 open(my $fh, "<", "b") || die;
296 eval { utime(undef, undef, $fh); };
297 like($@, qr/^The futimes function is unimplemented at/, "futimes is unimplemented");
300 is(unlink('b'), 1, "unlink b");
302 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
303 $blksize,$blocks) = stat('b');
304 is($ino, undef, "ino of unlinked file b should be undef");
307 chdir $wd || die "Can't cd back to $wd";
309 # Yet another way to look for links (perhaps those that cannot be
310 # created by perl?). Hopefully there is an ls utility in your
311 # %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
314 skip "Win32/Netware specific test", 2
315 unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
316 skip "No symbolic links found to test with", 2
317 unless `ls -l perl 2>nul` =~ /^l.*->/;
319 system("cp TEST TEST$$");
320 # we have to copy because e.g. GNU grep gets huffy if we have
321 # a symlink forest to another disk (it complains about too many
322 # levels of symbolic links, even if we have only two)
323 is(symlink("TEST$$","c"), 1, "symlink");
324 $foo = `grep perl c 2>&1`;
325 ok($foo, "found perl in c");
330 my $tmpfile = tempfile();
331 open IOFSCOM, ">$tmpfile" or die "Could not write IOfs.tmp: $!";
332 print IOFSCOM 'helloworld';
335 # TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
339 # Check truncating a closed file.
340 eval { truncate $tmpfile, 5; };
342 skip("no truncate - $@", 8) if $@;
344 is(-s $tmpfile, 5, "truncation to five bytes");
346 truncate $tmpfile, 0;
348 ok(-z $tmpfile, "truncation to zero bytes");
350 #these steps are necessary to check if file is really truncated
351 #On Win95, FH is updated, but file properties aren't
352 open(FH, ">$tmpfile") or die "Can't create $tmpfile";
353 print FH "x\n" x 200;
356 # Check truncating an open file.
357 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
366 print FH "x\n" x 200;
367 ok(truncate(FH, 200), "fh resize to 200");
370 if ($needs_fh_reopen) {
371 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
376 skip ("# TODO - hit VOS bug posix-973 - cannot resize an open file below the current file pos.", 5);
379 is(-s $tmpfile, 200, "fh resize to 200 working (filename check)");
381 ok(truncate(FH, 0), "fh resize to zero");
383 if ($needs_fh_reopen) {
384 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
387 ok(-z $tmpfile, "fh resize to zero working (filename check)");
391 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
400 print FH "x\n" x 200;
401 ok(truncate(*FH{IO}, 100), "fh resize by IO slot");
404 if ($needs_fh_reopen) {
405 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
408 is(-s $tmpfile, 100, "fh resize by IO slot working");
414 # check if rename() can be used to just change case of filename
416 skip "Works in Cygwin only if check_case is set to relaxed", 1
417 if ($ENV{'CYGWIN'} && ($ENV{'CYGWIN'} =~ /check_case:(?:adjust|strict)/));
420 open(FH,'>x') || die "Can't create x";
424 # this works on win32 only, because fs isn't casesensitive
425 ok(-e 'X', "rename working");
428 chdir $wd || die "Can't cd back to $wd";
431 # check if rename() works on directories
433 # must have delete access to rename a directory
434 `set file $tmpdir.dir/protection=o:d`;
435 ok(rename("$tmpdir.dir", "$tmpdir1.dir"), "rename on directories") ||
436 print "# errno: $!\n";
439 ok(rename($tmpdir, $tmpdir1), "rename on directories");
442 ok(-d $tmpdir1, "rename on directories working");
445 # Change 26011: Re: A surprising segfault
446 # to make sure only that these obfuscated sentences will not crash.
448 map chmod(+()), ('')x68;
449 ok(1, "extend sp in pp_chmod");
451 map chown(+()), ('')x68;
452 ok(1, "extend sp in pp_chown");
455 # need to remove $tmpdir if rename() in test 28 failed!
456 END { rmdir $tmpdir1; rmdir $tmpdir; }