This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: A surprising segfault
[perl5.git] / t / io / fs.t
CommitLineData
8d063cd8
LW
1#!./perl
2
ea368a7c
CS
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
0c5d4ba3 6 require "./test.pl";
ea368a7c
CS
7}
8
9use Config;
dc459aad 10use File::Spec::Functions;
ea368a7c 11
dc459aad 12my $Is_MacOS = ($^O eq 'MacOS');
6d738113 13my $Is_VMSish = ($^O eq 'VMS');
0c5d4ba3 14
20dd405c
MS
15if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
16 $wd = `cd`;
fc8d54b0
GA
17}
18elsif ($^O eq 'VMS') {
20dd405c 19 $wd = `show default`;
fc8d54b0
GA
20}
21else {
20dd405c
MS
22 $wd = `pwd`;
23}
24chomp($wd);
25
0c5d4ba3
JH
26my $has_link = $Config{d_link};
27my $accurate_timestamps =
28 !($^O eq 'MSWin32' || $^O eq 'NetWare' ||
29 $^O eq 'dos' || $^O eq 'os2' ||
20dd405c 30 $^O eq 'mint' || $^O eq 'cygwin' ||
dc459aad
JH
31 $^O eq 'amigaos' || $wd =~ m#$Config{afsroot}/# ||
32 $Is_MacOS
20dd405c 33 );
39e571d4 34
6b980173 35if (defined &Win32::IsWinNT && Win32::IsWinNT()) {
0c5d4ba3 36 if (Win32::FsType() eq 'NTFS') {
20dd405c
MS
37 $has_link = 1;
38 $accurate_timestamps = 1;
0c5d4ba3 39 }
6b980173
JD
40}
41
0c5d4ba3
JH
42my $needs_fh_reopen =
43 $^O eq 'dos'
44 # Not needed on HPFS, but needed on HPFS386 ?!
45 || $^O eq 'os2';
46
7a2cf369
NK
47$needs_fh_reopen = 1 if (defined &Win32::IsWin95 && Win32::IsWin95());
48
4e51f8e4
SR
49my $skip_mode_checks =
50 $^O eq 'cygwin' && $ENV{CYGWIN} !~ /ntsec/;
51
1937c63e 52plan tests => 51;
8d063cd8 53
378cc40b 54
6d738113
PP
55if (($^O eq 'MSWin32') || ($^O eq 'NetWare')) {
56 `rmdir /s /q tmp 2>nul`;
57 `mkdir tmp`;
dc459aad
JH
58}
59elsif ($^O eq 'VMS') {
6d738113
PP
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]`;
63}
dc459aad
JH
64elsif ($Is_MacOS) {
65 rmdir "tmp"; mkdir "tmp";
66}
6d738113
PP
67else {
68 `rm -f tmp 2>/dev/null; mkdir tmp 2>/dev/null`;
69}
0c5d4ba3 70
dc459aad 71chdir catdir(curdir(), 'tmp');
0c5d4ba3 72
b8440792 73`/bin/rm -rf a b c x` if -x '/bin/rm';
8d063cd8
LW
74
75umask(022);
76
20dd405c 77SKIP: {
dc459aad 78 skip "bogus umask", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare') || ($^O eq 'epoc') || $Is_MacOS;
20dd405c
MS
79
80 is((umask(0)&0777), 022, 'umask'),
0c5d4ba3
JH
81}
82
d5fc3e70
SP
83open(FH,'>x') || die "Can't create x";
84close(FH);
85open(FH,'>a') || die "Can't create a";
86close(FH);
8d063cd8 87
8268670f
JH
88my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
89 $blksize,$blocks);
90
3ed9f8f7 91SKIP: {
0c5d4ba3 92 skip("no link", 4) unless $has_link;
8d063cd8 93
0c5d4ba3
JH
94 ok(link('a','b'), "link a b");
95 ok(link('b','c'), "link b c");
8d063cd8 96
8268670f
JH
97 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
98 $blksize,$blocks) = stat('c');
8d063cd8 99
20dd405c
MS
100 SKIP: {
101 skip "no nlink", 1 if $Config{dont_use_nlink};
102
103 is($nlink, 3, "link count of triply-linked file");
0c5d4ba3 104 }
ea368a7c 105
20dd405c
MS
106 SKIP: {
107 skip "hard links not that hard in $^O", 1 if $^O eq 'amigaos';
4e51f8e4 108 skip "no mode checks", 1 if $skip_mode_checks;
20dd405c 109
bbf171ae
GH
110# if ($^O eq 'cygwin') { # new files on cygwin get rwx instead of rw-
111# is($mode & 0777, 0777, "mode of triply-linked file");
112# } else {
4e51f8e4 113 is($mode & 0777, 0666, "mode of triply-linked file");
bbf171ae 114# }
0c5d4ba3
JH
115 }
116}
8d063cd8 117
2986a63f 118$newmode = (($^O eq 'MSWin32') || ($^O eq 'NetWare')) ? 0444 : 0777;
8d063cd8 119
0c5d4ba3 120is(chmod($newmode,'a'), 1, "chmod succeeding");
8d063cd8 121
0c5d4ba3 122SKIP: {
2f3b333f 123 skip("no link", 7) unless $has_link;
0c5d4ba3 124
8268670f
JH
125 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
126 $blksize,$blocks) = stat('c');
0c5d4ba3 127
4e51f8e4
SR
128 SKIP: {
129 skip "no mode checks", 1 if $skip_mode_checks;
130
131 is($mode & 0777, $newmode, "chmod going through");
132 }
0c5d4ba3
JH
133
134 $newmode = 0700;
6b980173
JD
135 chmod 0444, 'x';
136 $newmode = 0666;
6b980173 137
0c5d4ba3 138 is(chmod($newmode,'c','x'), 2, "chmod two files");
3ed9f8f7 139
0c5d4ba3
JH
140 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
141 $blksize,$blocks) = stat('c');
8d063cd8 142
4e51f8e4
SR
143 SKIP: {
144 skip "no mode checks", 1 if $skip_mode_checks;
145
146 is($mode & 0777, $newmode, "chmod going through to c");
147 }
a245ea2d 148
0c5d4ba3
JH
149 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
150 $blksize,$blocks) = stat('x');
8d063cd8 151
4e51f8e4
SR
152 SKIP: {
153 skip "no mode checks", 1 if $skip_mode_checks;
154
155 is($mode & 0777, $newmode, "chmod going through to x");
156 }
0c5d4ba3
JH
157
158 is(unlink('b','x'), 2, "unlink two files");
159
160 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
161 $blksize,$blocks) = stat('b');
162
163 is($ino, undef, "ino of removed file b should be undef");
164
165 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
166 $blksize,$blocks) = stat('x');
167
168 is($ino, undef, "ino of removed file x should be undef");
8268670f 169}
0c5d4ba3 170
c4aca7d0
GA
171SKIP: {
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];
b0fdffbd
SP
176 SKIP: {
177 skip "no mode checks", 1 if $skip_mode_checks;
178 is($mode & 0777, 0, "perm reset");
179 }
c4aca7d0
GA
180 is(chmod($newmode, "a"), 1, "fchmod");
181 $mode = (stat $fh)[2];
b0fdffbd
SP
182 SKIP: {
183 skip "no mode checks", 1 if $skip_mode_checks;
184 is($mode & 0777, $newmode, "perm restored");
185 }
c4aca7d0
GA
186}
187
188SKIP: {
189 skip "no fchown", 1 unless ($Config{d_fchown} || "") eq "define";
190 open(my $fh, "<", "a");
191 is(chown(-1, -1, $fh), 1, "fchown");
192}
193
194SKIP: {
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");
199}
200
201SKIP: {
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");
206}
207
8268670f 208is(rename('a','b'), 1, "rename a b");
0c5d4ba3 209
8268670f
JH
210($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
211 $blksize,$blocks) = stat('a');
0c5d4ba3 212
8268670f 213is($ino, undef, "ino of renamed file a should be undef");
0c5d4ba3
JH
214
215$delta = $accurate_timestamps ? 1 : 2; # Granularity of time on the filesystem
1d825fcc 216chmod 0777, 'b';
0c5d4ba3 217
e96b369d 218$foo = (utime 500000000,500000000 + $delta,'b');
0c5d4ba3 219is($foo, 1, "utime");
e96b369d
GA
220check_utime_result();
221
222utime undef, undef, 'b';
223($atime,$mtime) = (stat 'b')[8,9];
224print "# utime undef, undef --> $atime, $mtime\n";
225isnt($atime, 500000000, 'atime');
226isnt($mtime, 500000000 + $delta, 'mtime');
227
228SKIP: {
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();
234}
0c5d4ba3 235
e96b369d
GA
236
237sub check_utime_result {
fc8d54b0
GA
238 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
239 $blksize,$blocks) = stat('b');
0c5d4ba3 240
fc8d54b0
GA
241 SKIP: {
242 skip "bogus inode num", 1 if ($^O eq 'MSWin32') || ($^O eq 'NetWare');
20dd405c 243
fc8d54b0
GA
244 ok($ino, 'non-zero inode num');
245 }
0c5d4ba3 246
fc8d54b0
GA
247 SKIP: {
248 skip "filesystem atime/mtime granularity too low", 2
249 unless $accurate_timestamps;
20dd405c 250
fc8d54b0
GA
251 print "# atime - $atime mtime - $mtime delta - $delta\n";
252 if($atime == 500000000 && $mtime == 500000000 + $delta) {
253 pass('atime');
254 pass('mtime');
255 }
256 else {
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");
266 }
267 else {
268 fail("atime - $atime/$new_atime $mtime/$new_mtime");
269 fail("mtime - $atime/$new_atime $mtime/$new_mtime");
270 }
271 }
272 elsif ($^O eq 'VMS') {
273 # why is this 1 second off?
274 is( $atime, 500000001, 'atime' );
275 is( $mtime, 500000000 + $delta, 'mtime' );
276 }
277 elsif ($^O eq 'beos') {
278 SKIP: {
279 skip "atime not updated", 1;
280 }
281 is($mtime, 500000001, 'mtime');
282 }
283 else {
284 fail("atime");
285 fail("mtime");
286 }
287 }
0c5d4ba3 288 }
6d738113 289}
e96b369d
GA
290
291SKIP: {
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");
296}
0c5d4ba3
JH
297
298is(unlink('b'), 1, "unlink b");
299
8d063cd8
LW
300($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
301 $blksize,$blocks) = stat('b');
0c5d4ba3 302is($ino, undef, "ino of unlinked file b should be undef");
378cc40b
LW
303unlink 'c';
304
305chdir $wd || die "Can't cd back to $wd";
306
0c5d4ba3
JH
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.
310
20dd405c
MS
311SKIP: {
312 skip "Win32/Netware specific test", 2
313 unless ($^O eq 'MSWin32') || ($^O eq 'NetWare');
3ed9f8f7 314 skip "No symbolic links found to test with", 2
20dd405c
MS
315 unless `ls -l perl 2>nul` =~ /^l.*->/;
316
4ba7095c
JH
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)
0c5d4ba3 321 is(symlink("TEST$$","c"), 1, "symlink");
4ba7095c 322 $foo = `grep perl c 2>&1`;
0c5d4ba3 323 ok($foo, "found perl in c");
44a8e56a 324 unlink 'c';
4ba7095c 325 unlink("TEST$$");
378cc40b 326}
f783569b 327
f783569b 328unlink "Iofs.tmp";
0c5d4ba3
JH
329open IOFSCOM, ">Iofs.tmp" or die "Could not write IOfs.tmp: $!";
330print IOFSCOM 'helloworld';
331close(IOFSCOM);
332
333# TODO: pp_truncate needs to be taught about F_CHSIZE and F_FREESP,
334# as per UNIX FAQ.
335
336SKIP: {
63720136 337# Check truncating a closed file.
0c5d4ba3 338 eval { truncate "Iofs.tmp", 5; };
90ddc76f 339
090bf15b 340 skip("no truncate - $@", 8) if $@;
0c5d4ba3
JH
341
342 is(-s "Iofs.tmp", 5, "truncation to five bytes");
343
344 truncate "Iofs.tmp", 0;
345
346 ok(-z "Iofs.tmp", "truncation to zero bytes");
347
7a2cf369
NK
348#these steps are necessary to check if file is really truncated
349#On Win95, FH is updated, but file properties aren't
0c5d4ba3 350 open(FH, ">Iofs.tmp") or die "Can't create Iofs.tmp";
7a2cf369
NK
351 print FH "x\n" x 200;
352 close FH;
353
63720136
PG
354# Check truncating an open file.
355 open(FH, ">>Iofs.tmp") or die "Can't open Iofs.tmp for appending";
0c5d4ba3
JH
356
357 binmode FH;
358 select FH;
359 $| = 1;
360 select STDOUT;
361
362 {
363 use strict;
364 print FH "x\n" x 200;
365 ok(truncate(FH, 200), "fh resize to 200");
62b86938 366 }
0c5d4ba3
JH
367
368 if ($needs_fh_reopen) {
369 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
370 }
90ddc76f 371
090bf15b
SR
372 SKIP: {
373 if ($^O eq 'vos') {
374 skip ("# TODO - hit VOS bug posix-973 - cannot resize an open file below the current file pos.", 5);
375 }
0c5d4ba3 376
090bf15b 377 is(-s "Iofs.tmp", 200, "fh resize to 200 working (filename check)");
63720136 378
090bf15b 379 ok(truncate(FH, 0), "fh resize to zero");
0c5d4ba3 380
090bf15b
SR
381 if ($needs_fh_reopen) {
382 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
383 }
0c5d4ba3 384
090bf15b 385 ok(-z "Iofs.tmp", "fh resize to zero working (filename check)");
7a2cf369 386
090bf15b
SR
387 close FH;
388
389 open(FH, ">>Iofs.tmp") or die "Can't open Iofs.tmp for appending";
390
391 binmode FH;
392 select FH;
393 $| = 1;
394 select STDOUT;
395
396 {
397 use strict;
398 print FH "x\n" x 200;
399 ok(truncate(*FH{IO}, 100), "fh resize by IO slot");
400 }
401
402 if ($needs_fh_reopen) {
403 close (FH); open (FH, ">>Iofs.tmp") or die "Can't reopen Iofs.tmp";
404 }
405
406 is(-s "Iofs.tmp", 100, "fh resize by IO slot working");
407
408 close FH;
409 }
f783569b 410}
80252599 411
65cb15a1 412# check if rename() can be used to just change case of filename
20dd405c
MS
413SKIP: {
414 skip "Works in Cygwin only if check_case is set to relaxed", 1
415 if $^O eq 'cygwin';
416
0c5d4ba3 417 chdir './tmp';
d5fc3e70
SP
418 open(FH,'>x') || die "Can't create x";
419 close(FH);
0c5d4ba3 420 rename('x', 'X');
3ed9f8f7 421
0c5d4ba3
JH
422 # this works on win32 only, because fs isn't casesensitive
423 ok(-e 'X', "rename working");
8268670f 424
20dd405c 425 1 while unlink 'X';
0c5d4ba3 426 chdir $wd || die "Can't cd back to $wd";
73077d53 427}
65cb15a1 428
80252599 429# check if rename() works on directories
0c5d4ba3 430if ($^O eq 'VMS') {
9df548ee
CB
431 # must have delete access to rename a directory
432 `set file tmp.dir/protection=o:d`;
20dd405c
MS
433 ok(rename('tmp.dir', 'tmp1.dir'), "rename on directories") ||
434 print "# errno: $!\n";
fc8d54b0
GA
435}
436else {
0c5d4ba3 437 ok(rename('tmp', 'tmp1'), "rename on directories");
6d738113 438}
0c5d4ba3
JH
439
440ok(-d 'tmp1', "rename on directories working");
80252599 441
1937c63e
TS
442{
443 # Change 26011: Re: A surprising segfault
444 # to make sure only that these obfuscated sentences will not crash.
445
446 map chmod(+()), ('')x68;
447 ok(1, "extend sp in pp_chmod");
448
449 map chown(+()), ('')x68;
450 ok(1, "extend sp in pp_chown");
451}
452
73077d53 453# need to remove 'tmp' if rename() in test 28 failed!
4e7ee149 454END { rmdir 'tmp1'; rmdir 'tmp'; 1 while unlink "Iofs.tmp"; }