This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Further VMS piping fixes from Charles Lane:
[perl5.git] / t / lib / 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 {
ea2b5ef6 6 chdir 't' if -d 't';
20822f61 7 @INC = '../lib';
9f8fdb7d
JH
8 require Config; import Config;
9 # Don't bother if there are no quad offsets.
10 if ($Config{lseeksize} < 8) {
6afb513c 11 print "1..0 # Skip: no 64-bit file offsets\n";
48ea9154 12 exit(0);
9f8fdb7d 13 }
ca6e1c26 14 require Fcntl; import Fcntl qw(/^O_/ /^SEEK_/);
ea2b5ef6
JH
15}
16
93c29725 17sub zap {
ea2b5ef6 18 close(BIG);
93c29725
JH
19 unlink("big");
20 unlink("big1");
21 unlink("big2");
22}
23
24sub bye {
25 zap();
ea2b5ef6
JH
26 exit(0);
27}
28
d731b481
JH
29my $explained;
30
fcbfa962 31sub explain {
d731b481
JH
32 unless ($explained++) {
33 print <<EOM;
fcbfa962 34#
d731b481
JH
35# If the lfs (large file support: large meaning larger than two
36# gigabytes) tests are skipped or fail, it may mean either that your
37# process (or process group) is not allowed to write large files
38# (resource limits) or that the file system (the network filesystem?)
39# you are running the tests on doesn't let your user/group have large
40# files (quota) or the filesystem simply doesn't support large files.
41# You may even need to reconfigure your kernel. (This is all very
42# operating system and site-dependent.)
fcbfa962
JH
43#
44# Perl may still be able to support large files, once you have
eed7fde4 45# such a process, enough quota, and such a (file) system.
d731b481 46# It is just that the test failed now.
fcbfa962
JH
47#
48EOM
d731b481
JH
49 }
50 print "1..0 # Skip: @_\n" if @_;
fcbfa962
JH
51}
52
e0a10278
JH
53print "# checking whether we have sparse files...\n";
54
05f8a9f5 55# Known have-nots.
86e48eb5
RS
56if ($^O eq 'MSWin32' || $^O eq 'VMS') {
57 print "1..0 # Skip: no sparse files in $^O\n";
ea2b5ef6
JH
58 bye();
59}
60
b18f8161
JH
61# Known haves that have problems running this test
62# (for example because they do not support sparse files, like UNICOS)
63if ($^O eq 'unicos') {
86e48eb5 64 print "1..0 # Skip: no sparse files in $^0, unable to test large files\n";
b18f8161
JH
65 bye();
66}
67
e0a10278 68# Then try heuristically to deduce whether we have sparse files.
05f8a9f5 69
ea2b5ef6 70# We'll start off by creating a one megabyte file which has
05f8a9f5
JH
71# only three "true" bytes. If we have sparseness, we should
72# consume less blocks than one megabyte (assuming nobody has
73# one megabyte blocks...)
ea2b5ef6 74
93c29725
JH
75sysopen(BIG, "big1", O_WRONLY|O_CREAT|O_TRUNC) or
76 do { warn "sysopen big1 failed: $!\n"; bye };
77sysseek(BIG, 1_000_000, SEEK_SET) or
78 do { warn "sysseek big1 failed: $!\n"; bye };
79syswrite(BIG, "big") or
80 do { warn "syswrite big1 failed; $!\n"; bye };
81close(BIG) or
82 do { warn "close big1 failed: $!\n"; bye };
ea2b5ef6 83
93c29725 84my @s1 = stat("big1");
ea2b5ef6 85
93c29725 86print "# s1 = @s1\n";
ea2b5ef6 87
93c29725
JH
88sysopen(BIG, "big2", O_WRONLY|O_CREAT|O_TRUNC) or
89 do { warn "sysopen big2 failed: $!\n"; bye };
90sysseek(BIG, 2_000_000, SEEK_SET) or
91 do { warn "sysseek big2 failed: $!\n"; bye };
92syswrite(BIG, "big") or
93 do { warn "syswrite big2 failed; $!\n"; bye };
94close(BIG) or
95 do { warn "close big2 failed: $!\n"; bye };
ea2b5ef6 96
93c29725 97my @s2 = stat("big2");
5cec1e3b 98
93c29725
JH
99print "# s2 = @s2\n";
100
101zap();
102
103unless ($s1[7] == 1_000_003 && $s2[7] == 2_000_003 &&
104 $s1[11] == $s2[11] && $s1[12] == $s2[12]) {
6afb513c 105 print "1..0 # Skip: no sparse files?\n";
93c29725 106 bye;
ea2b5ef6
JH
107}
108
e0a10278
JH
109print "# we seem to have sparse files...\n";
110
ea2b5ef6
JH
111# By now we better be sure that we do have sparse files:
112# if we are not, the following will hog 5 gigabytes of disk. Ooops.
6afb513c 113# This may fail by producing some signal; run in a subprocess first for safety
ea2b5ef6 114
eed7fde4
JH
115$ENV{LC_ALL} = "C";
116
6afb513c
MG
117my $r = system '../perl', '-I../lib', '-e', <<'EOF';
118use Fcntl qw(/^O_/ /^SEEK_/);
119sysopen(BIG, "big", O_WRONLY|O_CREAT|O_TRUNC) or die $!;
120my $sysseek = sysseek(BIG, 5_000_000_000, SEEK_SET);
121my $syswrite = syswrite(BIG, "big");
122exit 0;
123EOF
124
cc4466b7 125sysopen(BIG, "big", O_WRONLY|O_CREAT|O_TRUNC) or
e0a10278
JH
126 do { warn "sysopen 'big' failed: $!\n"; bye };
127my $sysseek = sysseek(BIG, 5_000_000_000, SEEK_SET);
6afb513c
MG
128unless (! $r && defined $sysseek && $sysseek == 5_000_000_000) {
129 $sysseek = 'undef' unless defined $sysseek;
d731b481
JH
130 explain("seeking past 2GB failed: ",
131 $r ? 'signal '.($r & 0x7f) : "$! (sysseek returned $sysseek)");
e0a10278
JH
132 bye();
133}
eed7fde4 134
fcbfa962 135# The syswrite will fail if there are are filesize limitations (process or fs).
e0a10278
JH
136my $syswrite = syswrite(BIG, "big");
137print "# syswrite failed: $! (syswrite returned ",
138 defined $syswrite ? $syswrite : 'undef', ")\n"
139 unless defined $syswrite && $syswrite == 3;
140my $close = close BIG;
141print "# close failed: $!\n" unless $close;
eed7fde4 142unless($syswrite && $close) {
b948423f 143 if ($! =~/too large/i) {
d731b481 144 explain("writing past 2GB failed: process limits?");
b948423f 145 } elsif ($! =~ /quota/i) {
d731b481
JH
146 explain("filesystem quota limits?");
147 } else {
148 explain("error: $!");
fcbfa962 149 }
eed7fde4 150 bye();
fcbfa962 151}
ea2b5ef6
JH
152
153@s = stat("big");
154
155print "# @s\n";
156
ae178db1 157unless ($s[7] == 5_000_000_003) {
d731b481 158 explain("kernel/fs not configured to use large files?");
ae178db1
JH
159 bye();
160}
161
05f8a9f5 162sub fail () {
64215065 163 print "not ";
05f8a9f5
JH
164 $fail++;
165}
166
77166d51 167print "1..17\n";
fcbfa962
JH
168
169my $fail = 0;
170
64215065 171fail unless $s[7] == 5_000_000_003; # exercizes pp_stat
ea2b5ef6
JH
172print "ok 1\n";
173
64215065 174fail unless -s "big" == 5_000_000_003; # exercizes pp_ftsize
ea2b5ef6
JH
175print "ok 2\n";
176
77166d51
JH
177fail unless -e "big";
178print "ok 3\n";
179
180fail unless -f "big";
181print "ok 4\n";
182
ea2b5ef6
JH
183sysopen(BIG, "big", O_RDONLY) or do { warn "sysopen failed: $!\n"; bye };
184
77166d51
JH
185fail unless sysseek(BIG, 4_500_000_000, SEEK_SET) == 4_500_000_000;
186print "ok 5\n";
ea2b5ef6 187
05f8a9f5 188fail unless sysseek(BIG, 0, SEEK_CUR) == 4_500_000_000;
77166d51 189print "ok 6\n";
ea2b5ef6 190
77166d51
JH
191fail unless sysseek(BIG, 1, SEEK_CUR) == 4_500_000_001;
192print "ok 7\n";
ea2b5ef6 193
05f8a9f5 194fail unless sysseek(BIG, 0, SEEK_CUR) == 4_500_000_001;
77166d51 195print "ok 8\n";
ea2b5ef6 196
77166d51
JH
197fail unless sysseek(BIG, -1, SEEK_CUR) == 4_500_000_000;
198print "ok 9\n";
ea2b5ef6 199
05f8a9f5 200fail unless sysseek(BIG, 0, SEEK_CUR) == 4_500_000_000;
77166d51 201print "ok 10\n";
ea2b5ef6 202
77166d51
JH
203fail unless sysseek(BIG, -3, SEEK_END) == 5_000_000_000;
204print "ok 11\n";
ea2b5ef6 205
05f8a9f5 206fail unless sysseek(BIG, 0, SEEK_CUR) == 5_000_000_000;
77166d51 207print "ok 12\n";
ea2b5ef6
JH
208
209my $big;
210
05f8a9f5 211fail unless sysread(BIG, $big, 3) == 3;
77166d51 212print "ok 13\n";
ea2b5ef6 213
05f8a9f5 214fail unless $big eq "big";
77166d51
JH
215print "ok 14\n";
216
217# 705_032_704 = (I32)5_000_000_000
a5853b9e 218fail unless sysseek(BIG, 705_032_704, SEEK_SET);
77166d51
JH
219print "ok 15\n";
220
221my $zero;
222
223fail unless read(BIG, $zero, 3) == 3;
224print "ok 16\n";
225
226fail unless $zero eq "\0\0\0";
227print "ok 17\n";
ea2b5ef6 228
d731b481 229explain() if $fail;
05f8a9f5 230
77166d51 231bye(); # does the necessary cleanup
e9a694fc 232
290be4b1
JH
233END {
234 unlink "big"; # be paranoid about leaving 5 gig files lying around
235}
236
ea2b5ef6 237# eof