This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor syslfs.t and lfs.t to call bye() directly from explain() when skipping.
[perl5.git] / ext / Fcntl / t / syslfs.t
CommitLineData
ea2b5ef6 1# NOTE: this file tests how large files (>2GB) work with raw system IO.
93c29725 2# stdio: open(), tell(), seek(), print(), read() is tested in t/op/lfs.t.
ea2b5ef6
JH
3# If you modify/add tests here, remember to update also t/op/lfs.t.
4
5BEGIN {
9f8fdb7d
JH
6 require Config; import Config;
7 # Don't bother if there are no quad offsets.
8 if ($Config{lseeksize} < 8) {
6afb513c 9 print "1..0 # Skip: no 64-bit file offsets\n";
48ea9154 10 exit(0);
9f8fdb7d 11 }
ca6e1c26 12 require Fcntl; import Fcntl qw(/^O_/ /^SEEK_/);
ea2b5ef6
JH
13}
14
326fd4b6 15use strict;
3d336648 16use File::Temp 'tempfile';
52ece81a 17
326fd4b6
A
18our @s;
19our $fail;
20
3d336648
NC
21(undef, my $big0) = tempfile(UNLINK => 1);
22(undef, my $big1) = tempfile(UNLINK => 1);
23(undef, my $big2) = tempfile(UNLINK => 1);
24
93c29725 25sub zap {
ea2b5ef6 26 close(BIG);
93c29725
JH
27}
28
29sub bye {
30 zap();
ea2b5ef6
JH
31 exit(0);
32}
33
d731b481
JH
34my $explained;
35
fcbfa962 36sub explain {
d731b481
JH
37 unless ($explained++) {
38 print <<EOM;
fcbfa962 39#
d731b481
JH
40# If the lfs (large file support: large meaning larger than two
41# gigabytes) tests are skipped or fail, it may mean either that your
42# process (or process group) is not allowed to write large files
43# (resource limits) or that the file system (the network filesystem?)
44# you are running the tests on doesn't let your user/group have large
45# files (quota) or the filesystem simply doesn't support large files.
46# You may even need to reconfigure your kernel. (This is all very
47# operating system and site-dependent.)
fcbfa962
JH
48#
49# Perl may still be able to support large files, once you have
eed7fde4 50# such a process, enough quota, and such a (file) system.
d731b481 51# It is just that the test failed now.
fcbfa962
JH
52#
53EOM
d731b481 54 }
80ed9481
NC
55 if (@_) {
56 print "1..0 # Skip: @_\n";
57 bye();
58 }
fcbfa962
JH
59}
60
6c1d9365
NC
61$| = 1;
62
e0a10278
JH
63print "# checking whether we have sparse files...\n";
64
05f8a9f5 65# Known have-nots.
2986a63f 66if ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') {
86e48eb5 67 print "1..0 # Skip: no sparse files in $^O\n";
ea2b5ef6
JH
68 bye();
69}
70
b18f8161
JH
71# Known haves that have problems running this test
72# (for example because they do not support sparse files, like UNICOS)
73if ($^O eq 'unicos') {
6c1d9365 74 print "1..0 # Skip: no sparse files in $^O, unable to test large files\n";
b18f8161
JH
75 bye();
76}
77
e0a10278 78# Then try heuristically to deduce whether we have sparse files.
05f8a9f5 79
ea2b5ef6 80# We'll start off by creating a one megabyte file which has
05f8a9f5
JH
81# only three "true" bytes. If we have sparseness, we should
82# consume less blocks than one megabyte (assuming nobody has
83# one megabyte blocks...)
ea2b5ef6 84
3d336648
NC
85sysopen(BIG, $big1, O_WRONLY|O_CREAT|O_TRUNC) or
86 do { warn "sysopen $big1 failed: $!\n"; bye };
93c29725 87sysseek(BIG, 1_000_000, SEEK_SET) or
3d336648 88 do { warn "sysseek $big1 failed: $!\n"; bye };
93c29725 89syswrite(BIG, "big") or
3d336648 90 do { warn "syswrite $big1 failed: $!\n"; bye };
93c29725 91close(BIG) or
3d336648 92 do { warn "close $big1 failed: $!\n"; bye };
ea2b5ef6 93
3d336648 94my @s1 = stat($big1);
ea2b5ef6 95
93c29725 96print "# s1 = @s1\n";
ea2b5ef6 97
3d336648
NC
98sysopen(BIG, $big2, O_WRONLY|O_CREAT|O_TRUNC) or
99 do { warn "sysopen $big2 failed: $!\n"; bye };
93c29725 100sysseek(BIG, 2_000_000, SEEK_SET) or
3d336648 101 do { warn "sysseek $big2 failed: $!\n"; bye };
93c29725 102syswrite(BIG, "big") or
3d336648 103 do { warn "syswrite $big2 failed: $!\n"; bye };
93c29725 104close(BIG) or
3d336648 105 do { warn "close $big2 failed: $!\n"; bye };
ea2b5ef6 106
3d336648 107my @s2 = stat($big2);
5cec1e3b 108
93c29725
JH
109print "# s2 = @s2\n";
110
111zap();
112
113unless ($s1[7] == 1_000_003 && $s2[7] == 2_000_003 &&
8c3c028a
LB
114 $s1[11] == $s2[11] && $s1[12] == $s2[12] &&
115 $s1[12] > 0) {
6afb513c 116 print "1..0 # Skip: no sparse files?\n";
93c29725 117 bye;
ea2b5ef6
JH
118}
119
e0a10278
JH
120print "# we seem to have sparse files...\n";
121
ea2b5ef6
JH
122# By now we better be sure that we do have sparse files:
123# if we are not, the following will hog 5 gigabytes of disk. Ooops.
6afb513c 124# This may fail by producing some signal; run in a subprocess first for safety
ea2b5ef6 125
eed7fde4
JH
126$ENV{LC_ALL} = "C";
127
3778d1b0
DM
128my $perl = '../../perl';
129unless (-x $perl) {
130 print "1..1\nnot ok 1 - can't find perl: expected $perl\n";
131 exit 0;
132}
3d336648 133my $r = system $perl, '-I../lib', '-e', <<"EOF";
6afb513c 134use Fcntl qw(/^O_/ /^SEEK_/);
3d336648
NC
135sysopen \$big, q{$big0}, O_WRONLY|O_CREAT|O_TRUNC or die qq{sysopen $big0 $!};
136sysseek \$big, 5_000_000_000, SEEK_SET or die qq{sysseek $big0 $!};
137syswrite \$big, "big" or die qq{syswrite $big0 $!};
138close \$big or die qq{close $big0: $!};
6afb513c
G
139exit 0;
140EOF
141
3778d1b0 142
3d336648
NC
143sysopen(BIG, $big0, O_WRONLY|O_CREAT|O_TRUNC) or
144 do { warn "sysopen $big0 failed: $!\n"; bye };
e0a10278 145my $sysseek = sysseek(BIG, 5_000_000_000, SEEK_SET);
6afb513c
G
146unless (! $r && defined $sysseek && $sysseek == 5_000_000_000) {
147 $sysseek = 'undef' unless defined $sysseek;
d731b481
JH
148 explain("seeking past 2GB failed: ",
149 $r ? 'signal '.($r & 0x7f) : "$! (sysseek returned $sysseek)");
e0a10278 150}
eed7fde4 151
fcbfa962 152# The syswrite will fail if there are are filesize limitations (process or fs).
e0a10278
JH
153my $syswrite = syswrite(BIG, "big");
154print "# syswrite failed: $! (syswrite returned ",
155 defined $syswrite ? $syswrite : 'undef', ")\n"
156 unless defined $syswrite && $syswrite == 3;
157my $close = close BIG;
158print "# close failed: $!\n" unless $close;
eed7fde4 159unless($syswrite && $close) {
b948423f 160 if ($! =~/too large/i) {
d731b481 161 explain("writing past 2GB failed: process limits?");
b948423f 162 } elsif ($! =~ /quota/i) {
d731b481
JH
163 explain("filesystem quota limits?");
164 } else {
165 explain("error: $!");
fcbfa962
JH
166 }
167}
ea2b5ef6 168
3d336648 169@s = stat($big0);
ea2b5ef6
JH
170
171print "# @s\n";
172
ae178db1 173unless ($s[7] == 5_000_000_003) {
d731b481 174 explain("kernel/fs not configured to use large files?");
ae178db1
JH
175}
176
6c1d9365 177sub fail {
64215065 178 print "not ";
05f8a9f5
JH
179 $fail++;
180}
181
326fd4b6
A
182sub offset ($$) {
183 my ($offset_will_be, $offset_want) = @_;
184 my $offset_is = eval $offset_will_be;
185 unless ($offset_is == $offset_want) {
186 print "# bad offset $offset_is, want $offset_want\n";
52ece81a 187 my ($offset_func) = ($offset_will_be =~ /^(\w+)/);
326fd4b6 188 if (unpack("L", pack("L", $offset_want)) == $offset_is) {
326fd4b6 189 print "# 32-bit wraparound suspected in $offset_func() since\n";
52ece81a 190 print "# $offset_want cast into 32 bits equals $offset_is.\n";
f06a04a3 191 } elsif ($offset_want - unpack("L", pack("L", $offset_want)) - 1
52ece81a 192 == $offset_is) {
f06a04a3
A
193 print "# 32-bit wraparound suspected in $offset_func() since\n";
194 printf "# %s - unpack('L', pack('L', %s)) - 1 equals %s.\n",
195 $offset_want,
196 $offset_want,
197 $offset_is;
198 }
326fd4b6
A
199 fail;
200 }
201}
202
77166d51 203print "1..17\n";
fcbfa962 204
52ece81a 205$fail = 0;
fcbfa962 206
64215065 207fail unless $s[7] == 5_000_000_003; # exercizes pp_stat
ea2b5ef6
JH
208print "ok 1\n";
209
3d336648 210fail unless -s $big0 == 5_000_000_003; # exercizes pp_ftsize
ea2b5ef6
JH
211print "ok 2\n";
212
3d336648 213fail unless -e $big0;
77166d51
JH
214print "ok 3\n";
215
3d336648 216fail unless -f $big0;
77166d51
JH
217print "ok 4\n";
218
3d336648 219sysopen(BIG, $big0, O_RDONLY) or do { warn "sysopen failed: $!\n"; bye };
ea2b5ef6 220
326fd4b6 221offset('sysseek(BIG, 4_500_000_000, SEEK_SET)', 4_500_000_000);
77166d51 222print "ok 5\n";
ea2b5ef6 223
326fd4b6 224offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_000);
77166d51 225print "ok 6\n";
ea2b5ef6 226
6c1d9365
NC
227# If you get 205_032_705 from here it means that
228# your tell() is returning 32-bit values since (I32)4_500_000_001
229# is exactly 205_032_705.
326fd4b6 230offset('sysseek(BIG, 1, SEEK_CUR)', 4_500_000_001);
77166d51 231print "ok 7\n";
ea2b5ef6 232
326fd4b6 233offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_001);
77166d51 234print "ok 8\n";
ea2b5ef6 235
326fd4b6 236offset('sysseek(BIG, -1, SEEK_CUR)', 4_500_000_000);
77166d51 237print "ok 9\n";
ea2b5ef6 238
326fd4b6 239offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_000);
77166d51 240print "ok 10\n";
ea2b5ef6 241
326fd4b6 242offset('sysseek(BIG, -3, SEEK_END)', 5_000_000_000);
77166d51 243print "ok 11\n";
ea2b5ef6 244
326fd4b6 245offset('sysseek(BIG, 0, SEEK_CUR)', 5_000_000_000);
77166d51 246print "ok 12\n";
ea2b5ef6
JH
247
248my $big;
249
05f8a9f5 250fail unless sysread(BIG, $big, 3) == 3;
77166d51 251print "ok 13\n";
ea2b5ef6 252
05f8a9f5 253fail unless $big eq "big";
77166d51
JH
254print "ok 14\n";
255
256# 705_032_704 = (I32)5_000_000_000
326fd4b6
A
257# See that we don't have "big" in the 705_... spot:
258# that would mean that we have a wraparound.
a5853b9e 259fail unless sysseek(BIG, 705_032_704, SEEK_SET);
77166d51
JH
260print "ok 15\n";
261
262my $zero;
263
264fail unless read(BIG, $zero, 3) == 3;
265print "ok 16\n";
266
267fail unless $zero eq "\0\0\0";
268print "ok 17\n";
ea2b5ef6 269
d731b481 270explain() if $fail;
05f8a9f5 271
77166d51 272bye(); # does the necessary cleanup
e9a694fc 273
290be4b1 274END {
b0d0c539 275 # unlink may fail if applied directly to a large file
6c8d78fb 276 # be paranoid about leaving 5 gig files lying around
3d336648 277 open(BIG, ">$big0"); # truncate
b0d0c539 278 close(BIG);
290be4b1
JH
279}
280
ea2b5ef6 281# eof