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