This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deprecate above \xFF in bitwise string ops
[perl5.git] / t / op / lfs.t
... / ...
CommitLineData
1# NOTE: this file tests how large files (>2GB) work with perlio (or stdio).
2# sysopen(), sysseek(), syswrite(), sysread() are tested in t/lib/syslfs.t.
3# If you modify/add tests here, remember to update also ext/Fcntl/t/syslfs.t.
4
5BEGIN {
6 chdir 't' if -d 't';
7 require './test.pl';
8 set_up_inc('../lib');
9 require Config;
10 # Don't bother if there are no quad offsets.
11 skip_all('no 64-bit file offsets')
12 if $Config::Config{lseeksize} < 8;
13}
14
15use strict;
16
17our @s;
18
19my $big0 = tempfile();
20my $big1 = tempfile();
21my $big2 = tempfile();
22
23my $explained;
24
25sub explain {
26 unless ($explained++) {
27 print <<EOM;
28#
29# If the lfs (large file support: large meaning larger than two
30# gigabytes) tests are skipped or fail, it may mean either that your
31# process (or process group) is not allowed to write large files
32# (resource limits) or that the file system (the network filesystem?)
33# you are running the tests on doesn't let your user/group have large
34# files (quota) or the filesystem simply doesn't support large files.
35# You may even need to reconfigure your kernel. (This is all very
36# operating system and site-dependent.)
37#
38# Perl may still be able to support large files, once you have
39# such a process, enough quota, and such a (file) system.
40# It is just that the test failed now.
41#
42EOM
43 }
44 if (@_) {
45 skip_all(@_);
46 }
47}
48
49$| = 1;
50
51print "# checking whether we have sparse files...\n";
52
53# Known have-nots.
54if ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') {
55 skip_all("no sparse files in $^O");
56}
57
58# Known haves that have problems running this test
59# (for example because they do not support sparse files, like UNICOS)
60if ($^O eq 'unicos') {
61 skip_all("no sparse files in $^O, unable to test large files");
62}
63
64# Then try heuristically to deduce whether we have sparse files.
65
66# Let's not depend on Fcntl or any other extension.
67
68sub SEEK_SET () {0}
69sub SEEK_CUR () {1}
70sub SEEK_END () {2}
71
72# We'll start off by creating a one megabyte file which has
73# only three "true" bytes. If we have sparseness, we should
74# consume less blocks than one megabyte (assuming nobody has
75# one megabyte blocks...)
76
77open(BIG, ">$big1") or
78 die "open $big1 failed: $!";
79binmode(BIG) or
80 die "binmode $big1 failed: $!";
81seek(BIG, 1_000_000, SEEK_SET) or
82 die "seek $big1 failed: $!";
83print BIG "big" or
84 die "print $big1 failed: $!";
85close(BIG) or
86 die "close $big1 failed: $!";
87
88my @s1 = stat($big1);
89
90print "# s1 = @s1\n";
91
92open(BIG, ">$big2") or
93 die "open $big2 failed: $!";
94binmode(BIG) or
95 die "binmode $big2 failed: $!";
96seek(BIG, 2_000_000, SEEK_SET) or
97 die "seek $big2 failed: $!";
98print BIG "big" or
99 die "print $big2 failed: $!";
100close(BIG) or
101 die "close $big2 failed: $!";
102
103my @s2 = stat($big2);
104
105print "# s2 = @s2\n";
106
107unless ($s1[7] == 1_000_003 && $s2[7] == 2_000_003 &&
108 $s1[11] == $s2[11] && $s1[12] == $s2[12] &&
109 $s1[12] > 0) {
110 skip_all("no sparse files?");
111}
112
113print "# we seem to have sparse files...\n";
114
115# By now we better be sure that we do have sparse files:
116# if we are not, the following will hog 5 gigabytes of disk. Ooops.
117# This may fail by producing some signal; run in a subprocess first for safety
118
119$ENV{LC_ALL} = "C";
120
121my $r = system '../perl', '-e', <<"EOF";
122open my \$big, '>', q{$big0} or die qq{open $big0: $!};
123seek \$big, 5_000_000_000, 0 or die qq{seek $big0: $!};
124print \$big "big" or die qq{print $big0: $!};
125close \$big or die qq{close $big0: $!};
126exit 0;
127EOF
128
129open(BIG, ">$big0") or die "open failed: $!";
130binmode BIG;
131if ($r or not seek(BIG, 5_000_000_000, SEEK_SET)) {
132 my $err = $r ? 'signal '.($r & 0x7f) : $!;
133 explain("seeking past 2GB failed: $err");
134}
135
136# Either the print or (more likely, thanks to buffering) the close will
137# fail if there are are filesize limitations (process or fs).
138my $print = print BIG "big";
139print "# print failed: $!\n" unless $print;
140my $close = close BIG;
141print "# close failed: $!\n" unless $close;
142unless ($print && $close) {
143 if ($! =~/too large/i) {
144 explain("writing past 2GB failed: process limits?");
145 } elsif ($! =~ /quota/i) {
146 explain("filesystem quota limits?");
147 } else {
148 explain("error: $!");
149 }
150}
151
152@s = stat($big0);
153
154print "# @s\n";
155
156unless ($s[7] == 5_000_000_003) {
157 explain("kernel/fs not configured to use large files?");
158}
159
160sub offset ($$) {
161 local $::Level = $::Level + 1;
162 my ($offset_will_be, $offset_want) = @_;
163 my $offset_is = eval $offset_will_be;
164 unless ($offset_is == $offset_want) {
165 print "# bad offset $offset_is, want $offset_want\n";
166 my ($offset_func) = ($offset_will_be =~ /^(\w+)/);
167 if (unpack("L", pack("L", $offset_want)) == $offset_is) {
168 print "# 32-bit wraparound suspected in $offset_func() since\n";
169 print "# $offset_want cast into 32 bits equals $offset_is.\n";
170 } elsif ($offset_want - unpack("L", pack("L", $offset_want)) - 1
171 == $offset_is) {
172 print "# 32-bit wraparound suspected in $offset_func() since\n";
173 printf "# %s - unpack('L', pack('L', %s)) - 1 equals %s.\n",
174 $offset_want,
175 $offset_want,
176 $offset_is;
177 }
178 fail($offset_will_be);
179 } else {
180 pass($offset_will_be);
181 }
182}
183
184plan(tests => 17);
185
186is($s[7], 5_000_000_003, 'exercises pp_stat');
187is(-s $big0, 5_000_000_003, 'exercises pp_ftsize');
188
189is(-e $big0, 1);
190is(-f $big0, 1);
191
192open(BIG, $big0) or die "open failed: $!";
193binmode BIG;
194
195isnt(seek(BIG, 4_500_000_000, SEEK_SET), undef);
196
197offset('tell(BIG)', 4_500_000_000);
198
199isnt(seek(BIG, 1, SEEK_CUR), undef);
200
201# If you get 205_032_705 from here it means that
202# your tell() is returning 32-bit values since (I32)4_500_000_001
203# is exactly 205_032_705.
204offset('tell(BIG)', 4_500_000_001);
205
206isnt(seek(BIG, -1, SEEK_CUR), undef);
207
208offset('tell(BIG)', 4_500_000_000);
209
210isnt(seek(BIG, -3, SEEK_END), undef);
211
212offset('tell(BIG)', 5_000_000_000);
213
214my $big;
215
216is(read(BIG, $big, 3), 3);
217
218is($big, "big");
219
220# 705_032_704 = (I32)5_000_000_000
221# See that we don't have "big" in the 705_... spot:
222# that would mean that we have a wraparound.
223isnt(seek(BIG, 705_032_704, SEEK_SET), undef);
224
225my $zero;
226
227is(read(BIG, $zero, 3), 3);
228
229is($zero, "\0\0\0");
230
231explain() unless $::Tests_Are_Passing;
232
233END {
234 # unlink may fail if applied directly to a large file
235 # be paranoid about leaving 5 gig files lying around
236 open(BIG, ">$big0"); # truncate
237 close(BIG);
238}
239
240# eof