This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get t/uni/cache.t working under minitest
[perl5.git] / t / op / lfs.t
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
5 BEGIN {
6         chdir 't' if -d 't';
7         @INC = '../lib';
8         require './test.pl';
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
15 use strict;
16
17 our @s;
18
19 my $big0 = tempfile();
20 my $big1 = tempfile();
21 my $big2 = tempfile();
22
23 my $explained;
24
25 sub 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 #
42 EOM
43     }
44     if (@_) {
45         skip_all(@_);
46     }
47 }
48
49 $| = 1;
50
51 print "# checking whether we have sparse files...\n";
52
53 # Known have-nots.
54 if ($^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)
60 if ($^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
68 sub SEEK_SET () {0}
69 sub SEEK_CUR () {1}
70 sub 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
77 open(BIG, ">$big1") or
78     die "open $big1 failed: $!";
79 binmode(BIG) or
80     die "binmode $big1 failed: $!";
81 seek(BIG, 1_000_000, SEEK_SET) or
82     die "seek $big1 failed: $!";
83 print BIG "big" or
84     die "print $big1 failed: $!";
85 close(BIG) or
86     die "close $big1 failed: $!";
87
88 my @s1 = stat($big1);
89
90 print "# s1 = @s1\n";
91
92 open(BIG, ">$big2") or
93     die "open $big2 failed: $!";
94 binmode(BIG) or
95     die "binmode $big2 failed: $!";
96 seek(BIG, 2_000_000, SEEK_SET) or
97     die "seek $big2 failed: $!";
98 print BIG "big" or
99     die "print $big2 failed: $!";
100 close(BIG) or
101     die "close $big2 failed: $!";
102
103 my @s2 = stat($big2);
104
105 print "# s2 = @s2\n";
106
107 unless ($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
113 print "# 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
121 my $r = system '../perl', '-e', <<"EOF";
122 open my \$big, '>', q{$big0} or die qq{open $big0: $!};
123 seek \$big, 5_000_000_000, 0 or die qq{seek $big0: $!};
124 print \$big "big" or die qq{print $big0: $!};
125 close \$big or die qq{close $big0: $!};
126 exit 0;
127 EOF
128
129 open(BIG, ">$big0") or die "open failed: $!";
130 binmode BIG;
131 if ($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).
138 my $print = print BIG "big";
139 print "# print failed: $!\n" unless $print;
140 my $close = close BIG;
141 print "# close failed: $!\n" unless $close;
142 unless ($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
154 print "# @s\n";
155
156 unless ($s[7] == 5_000_000_003) {
157     explain("kernel/fs not configured to use large files?");
158 }
159
160 sub 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
184 plan(tests => 17);
185
186 is($s[7], 5_000_000_003, 'exercises pp_stat');
187 is(-s $big0, 5_000_000_003, 'exercises pp_ftsize');
188
189 is(-e $big0, 1);
190 is(-f $big0, 1);
191
192 open(BIG, $big0) or die "open failed: $!";
193 binmode BIG;
194
195 isnt(seek(BIG, 4_500_000_000, SEEK_SET), undef);
196
197 offset('tell(BIG)', 4_500_000_000);
198
199 isnt(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.
204 offset('tell(BIG)', 4_500_000_001);
205
206 isnt(seek(BIG, -1, SEEK_CUR), undef);
207
208 offset('tell(BIG)', 4_500_000_000);
209
210 isnt(seek(BIG, -3, SEEK_END), undef);
211
212 offset('tell(BIG)', 5_000_000_000);
213
214 my $big;
215
216 is(read(BIG, $big, 3), 3);
217
218 is($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.
223 isnt(seek(BIG, 705_032_704, SEEK_SET), undef);
224
225 my $zero;
226
227 is(read(BIG, $zero, 3), 3);
228
229 is($zero, "\0\0\0");
230
231 explain() unless $::Tests_Are_Passing;
232
233 END {
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