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 'cygwin' || $^O eq 'amigaos' ||
31 $wd =~ m#$Config{afsroot}/# || $Is_MacOS
34 if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
35 if (Win32::FsType() eq 'NTFS') {
37 $accurate_timestamps = 1;
43 # Not needed on HPFS, but needed on HPFS386 ?!
46 $needs_fh_reopen = 1 if (defined &Win32::IsWin95 && Win32::IsWin95());
48 my $skip_mode_checks =
49 $^O eq 'cygwin' && $ENV{CYGWIN} !~ /ntsec/;
53 my $tmpdir = tempfile();
54 my $tmpdir1 = tempfile();
56 if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
57 `rmdir /s /q $tmpdir 2>nul`;
60 elsif ($^O eq 'VMS') {
61 `if f\$search("[.$tmpdir]*.*") .nes. "" then delete/nolog/noconfirm [.$tmpdir]*.*.*`;
62 `if f\$search("$tmpdir.dir") .nes. "" then set file/prot=o:rwed $tmpdir.dir;`;
63 `if f\$search("$tmpdir.dir") .nes. "" then delete/nolog/noconfirm $tmpdir.dir;`;
64 `create/directory [.$tmpdir]`;
67 rmdir "$tmpdir"; mkdir "$tmpdir";
70 `rm -f $tmpdir 2>/dev/null; mkdir $tmpdir 2>/dev/null`;
73 chdir catdir(curdir(), $tmpdir);
75 `/bin/rm -rf a b c x` if -x '/bin/rm';
80 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare') || ($^O eq 'epoc') || $Is_MacOS;
82 is((umask(0)&0777), 022, 'umask'),
85 open(FH,'>x') || die "Can't create x";
87 open(FH,'>a') || die "Can't create a";
90 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
91 $blksize,$blocks,$a_mode);
94 skip("no link", 4) unless $has_link;
96 ok(link('a','b'), "link a b");
97 ok(link('b','c'), "link b c");
99 $a_mode = (stat('a'))[2];
101 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
102 $blksize,$blocks) = stat('c');
105 skip "no nlink", 1 if $Config{dont_use_nlink};
107 is($nlink, 3, "link count of triply-linked file");
111 skip "hard links not that hard in $^O", 1 if $^O eq 'amigaos';
112 skip "no mode checks", 1 if $skip_mode_checks;
114 # if ($^O eq 'cygwin') { # new files on cygwin get rwx instead of rw-
115 # is($mode & 0777, 0777, "mode of triply-linked file");
117 is(sprintf("0%o", $mode & 0777),
118 sprintf("0%o", $a_mode & 0777),
119 "mode of triply-linked file");
124 $newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
126 is(chmod($newmode,'a'), 1, "chmod succeeding");
129 skip("no link", 7) unless $has_link;
131 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
132 $blksize,$blocks) = stat('c');
135 skip "no mode checks", 1 if $skip_mode_checks;
137 is($mode & 0777, $newmode, "chmod going through");
144 is(chmod($newmode,'c','x'), 2, "chmod two files");
146 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
147 $blksize,$blocks) = stat('c');
150 skip "no mode checks", 1 if $skip_mode_checks;
152 is($mode & 0777, $newmode, "chmod going through to c");
155 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
156 $blksize,$blocks) = stat('x');
159 skip "no mode checks", 1 if $skip_mode_checks;
161 is($mode & 0777, $newmode, "chmod going through to x");
164 is(unlink('b','x'), 2, "unlink two files");
166 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
167 $blksize,$blocks) = stat('b');
169 is($ino, undef, "ino of removed file b should be undef");
171 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
172 $blksize,$blocks) = stat('x');
174 is($ino, undef, "ino of removed file x should be undef");
178 skip "no fchmod", 5 unless ($Config{d_fchmod} || "") eq "define";
179 ok(open(my $fh, "<", "a"), "open a");
180 is(chmod(0, $fh), 1, "fchmod");
181 $mode = (stat "a")[2];
183 skip "no mode checks", 1 if $skip_mode_checks;
184 is($mode & 0777, 0, "perm reset");
186 is(chmod($newmode, "a"), 1, "fchmod");
187 $mode = (stat $fh)[2];
189 skip "no mode checks", 1 if $skip_mode_checks;
190 is($mode & 0777, $newmode, "perm restored");
195 skip "no fchown", 1 unless ($Config{d_fchown} || "") eq "define";
196 open(my $fh, "<", "a");
197 is(chown(-1, -1, $fh), 1, "fchown");
201 skip "has fchmod", 1 if ($Config{d_fchmod} || "") eq "define";
202 open(my $fh, "<", "a");
203 eval { chmod(0777, $fh); };
204 like($@, qr/^The fchmod function is unimplemented at/, "fchmod is unimplemented");
208 skip "has fchown", 1 if ($Config{d_fchown} || "") eq "define";
209 open(my $fh, "<", "a");
210 eval { chown(0, 0, $fh); };
211 like($@, qr/^The f?chown function is unimplemented at/, "fchown is unimplemented");
214 is(rename('a','b'), 1, "rename a b");
216 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
217 $blksize,$blocks) = stat('a');
219 is($ino, undef, "ino of renamed file a should be undef");
221 $delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
224 $foo = (utime 500000000,500000000 + $delta,'b');
225 is($foo, 1, "utime");
226 check_utime_result();
228 utime undef, undef, 'b';
229 ($atime,$mtime) = (stat 'b')[8,9];
230 print "# utime undef, undef --> $atime, $mtime\n";
231 isnt($atime, 500000000, 'atime');
232 isnt($mtime, 500000000 + $delta, 'mtime');
235 skip "no futimes", 4 unless ($Config{d_futimes} || "") eq "define";
236 open(my $fh, "<", 'b');
237 $foo = (utime 500000000,500000000 + $delta, $fh);
238 is($foo, 1, "futime");
239 check_utime_result();
243 sub check_utime_result {
244 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
245 $blksize,$blocks) = stat('b');
248 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
250 ok($ino, 'non-zero inode num');
254 skip "filesystem atime/mtime granularity too low", 2
255 unless $accurate_timestamps;
257 print "# atime - $atime mtime - $mtime delta - $delta\n";
258 if($atime == 500000000 && $mtime == 500000000 + $delta) {
263 if ($^O =~ /\blinux\b/i) {
264 print "# Maybe stat() cannot get the correct atime, ".
265 "as happens via NFS on linux?\n";
266 $foo = (utime 400000000,500000000 + 2*$delta,'b');
267 my ($new_atime, $new_mtime) = (stat('b'))[8,9];
268 print "# newatime - $new_atime nemtime - $new_mtime\n";
269 if ($new_atime == $atime && $new_mtime - $mtime == $delta) {
270 pass("atime - accounted for possible NFS/glibc2.2 bug on linux");
271 pass("mtime - accounted for possible NFS/glibc2.2 bug on linux");
274 fail("atime - $atime/$new_atime $mtime/$new_mtime");
275 fail("mtime - $atime/$new_atime $mtime/$new_mtime");
278 elsif ($^O eq 'VMS') {
279 # why is this 1 second off?
280 is( $atime, 500000001, 'atime' );
281 is( $mtime, 500000000 + $delta, 'mtime' );
283 elsif ($^O eq 'beos' || $^O eq 'haiku') {
285 skip "atime not updated", 1;
287 is($mtime, 500000001, 'mtime');
298 skip "has futimes", 1 if ($Config{d_futimes} || "") eq "define";
299 open(my $fh, "<", "b") || die;
300 eval { utime(undef, undef, $fh); };
301 like($@, qr/^The futimes function is unimplemented at/, "futimes is unimplemented");
304 is(unlink('b'), 1, "unlink b");
306 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
307 $blksize,$blocks) = stat('b');
308 is($ino, undef, "ino of unlinked file b should be undef");
311 chdir $wd || die "Can't cd back to $wd";
313 # Yet another way to look for links (perhaps those that cannot be
314 # created by perl?). Hopefully there is an ls utility in your
315 # %PATH%. N.B. that $^O is 'cygwin' on Cygwin.
318 skip "Win32/Netware specific test", 2
319 unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
320 skip "No symbolic links found to test with", 2
321 unless `ls -l perl 2>nul` =~ /^l.*->/;
323 system("cp TEST TEST$$");
324 # we have to copy because e.g. GNU grep gets huffy if we have
325 # a symlink forest to another disk (it complains about too many
326 # levels of symbolic links, even if we have only two)
327 is(symlink("TEST$$","c"), 1, "symlink");
328 $foo = `grep perl c 2>&1`;
329 ok($foo, "found perl in c");
334 my $tmpfile = tempfile();
335 open IOFSCOM, ">$tmpfile" or die "Could not write IOfs.tmp: $!";
336 print IOFSCOM 'helloworld';
339 # TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
343 # Check truncating a closed file.
344 eval { truncate $tmpfile, 5; };
346 skip("no truncate - $@", 8) if $@;
348 is(-s $tmpfile, 5, "truncation to five bytes");
350 truncate $tmpfile, 0;
352 ok(-z $tmpfile, "truncation to zero bytes");
354 #these steps are necessary to check if file is really truncated
355 #On Win95, FH is updated, but file properties aren't
356 open(FH, ">$tmpfile") or die "Can't create $tmpfile";
357 print FH "x\n" x 200;
360 # Check truncating an open file.
361 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
370 print FH "x\n" x 200;
371 ok(truncate(FH, 200), "fh resize to 200");
374 if ($needs_fh_reopen) {
375 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
380 skip ("# TODO - hit VOS bug posix-973 - cannot resize an open file below the current file pos.", 5);
383 is(-s $tmpfile, 200, "fh resize to 200 working (filename check)");
385 ok(truncate(FH, 0), "fh resize to zero");
387 if ($needs_fh_reopen) {
388 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
391 ok(-z $tmpfile, "fh resize to zero working (filename check)");
395 open(FH, ">>$tmpfile") or die "Can't open $tmpfile for appending";
404 print FH "x\n" x 200;
405 ok(truncate(*FH{IO}, 100), "fh resize by IO slot");
408 if ($needs_fh_reopen) {
409 close (FH); open (FH, ">>$tmpfile") or die "Can't reopen $tmpfile";
412 is(-s $tmpfile, 100, "fh resize by IO slot working");
418 # check if rename() can be used to just change case of filename
420 skip "Works in Cygwin only if check_case is set to relaxed", 1
421 if ($ENV{'CYGWIN'} && ($ENV{'CYGWIN'} =~ /check_case:(?:adjust|strict)/));
424 open(FH,'>x') || die "Can't create x";
428 # this works on win32 only, because fs isn't casesensitive
429 ok(-e 'X', "rename working");
432 chdir $wd || die "Can't cd back to $wd";
435 # check if rename() works on directories
437 # must have delete access to rename a directory
438 `set file $tmpdir.dir/protection=o:d`;
439 ok(rename("$tmpdir.dir", "$tmpdir1.dir"), "rename on directories") ||
440 print "# errno: $!\n";
443 ok(rename($tmpdir, $tmpdir1), "rename on directories");
446 ok(-d $tmpdir1, "rename on directories working");
449 # Change 26011: Re: A surprising segfault
450 # to make sure only that these obfuscated sentences will not crash.
452 map chmod(+()), ('')x68;
453 ok(1, "extend sp in pp_chmod");
455 map chown(+()), ('')x68;
456 ok(1, "extend sp in pp_chown");
459 # need to remove $tmpdir if rename() in test 28 failed!
460 END { rmdir $tmpdir1; rmdir $tmpdir; }