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.
8 require Config; import Config;
9 # Don't bother if there are no quad offsets.
10 if ($Config{lseeksize} < 8) {
11 print "1..0 # Skip: no 64-bit file offsets\n";
14 require Fcntl; import Fcntl qw(/^O_/ /^SEEK_/);
39 unless ($explained++) {
42 # If the lfs (large file support: large meaning larger than two
43 # gigabytes) tests are skipped or fail, it may mean either that your
44 # process (or process group) is not allowed to write large files
45 # (resource limits) or that the file system (the network filesystem?)
46 # you are running the tests on doesn't let your user/group have large
47 # files (quota) or the filesystem simply doesn't support large files.
48 # You may even need to reconfigure your kernel. (This is all very
49 # operating system and site-dependent.)
51 # Perl may still be able to support large files, once you have
52 # such a process, enough quota, and such a (file) system.
53 # It is just that the test failed now.
57 print "1..0 # Skip: @_\n" if @_;
60 print "# checking whether we have sparse files...\n";
63 if ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') {
64 print "1..0 # Skip: no sparse files in $^O\n";
68 # Known haves that have problems running this test
69 # (for example because they do not support sparse files, like UNICOS)
70 if ($^O eq 'unicos') {
71 print "1..0 # Skip: no sparse files in $^0, unable to test large files\n";
75 # Then try heuristically to deduce whether we have sparse files.
77 # We'll start off by creating a one megabyte file which has
78 # only three "true" bytes. If we have sparseness, we should
79 # consume less blocks than one megabyte (assuming nobody has
80 # one megabyte blocks...)
82 sysopen(BIG, "big1", O_WRONLY|O_CREAT|O_TRUNC) or
83 do { warn "sysopen big1 failed: $!\n"; bye };
84 sysseek(BIG, 1_000_000, SEEK_SET) or
85 do { warn "sysseek big1 failed: $!\n"; bye };
86 syswrite(BIG, "big") or
87 do { warn "syswrite big1 failed; $!\n"; bye };
89 do { warn "close big1 failed: $!\n"; bye };
91 my @s1 = stat("big1");
95 sysopen(BIG, "big2", O_WRONLY|O_CREAT|O_TRUNC) or
96 do { warn "sysopen big2 failed: $!\n"; bye };
97 sysseek(BIG, 2_000_000, SEEK_SET) or
98 do { warn "sysseek big2 failed: $!\n"; bye };
99 syswrite(BIG, "big") or
100 do { warn "syswrite big2 failed; $!\n"; bye };
102 do { warn "close big2 failed: $!\n"; bye };
104 my @s2 = stat("big2");
106 print "# s2 = @s2\n";
110 unless ($s1[7] == 1_000_003 && $s2[7] == 2_000_003 &&
111 $s1[11] == $s2[11] && $s1[12] == $s2[12]) {
112 print "1..0 # Skip: no sparse files?\n";
116 print "# we seem to have sparse files...\n";
118 # By now we better be sure that we do have sparse files:
119 # if we are not, the following will hog 5 gigabytes of disk. Ooops.
120 # This may fail by producing some signal; run in a subprocess first for safety
124 my $r = system '../perl', '-I../lib', '-e', <<'EOF';
125 use Fcntl qw(/^O_/ /^SEEK_/);
126 sysopen(BIG, "big", O_WRONLY|O_CREAT|O_TRUNC) or die $!;
127 my $sysseek = sysseek(BIG, 5_000_000_000, SEEK_SET);
128 my $syswrite = syswrite(BIG, "big");
132 sysopen(BIG, "big", O_WRONLY|O_CREAT|O_TRUNC) or
133 do { warn "sysopen 'big' failed: $!\n"; bye };
134 my $sysseek = sysseek(BIG, 5_000_000_000, SEEK_SET);
135 unless (! $r && defined $sysseek && $sysseek == 5_000_000_000) {
136 $sysseek = 'undef' unless defined $sysseek;
137 explain("seeking past 2GB failed: ",
138 $r ? 'signal '.($r & 0x7f) : "$! (sysseek returned $sysseek)");
142 # The syswrite will fail if there are are filesize limitations (process or fs).
143 my $syswrite = syswrite(BIG, "big");
144 print "# syswrite failed: $! (syswrite returned ",
145 defined $syswrite ? $syswrite : 'undef', ")\n"
146 unless defined $syswrite && $syswrite == 3;
147 my $close = close BIG;
148 print "# close failed: $!\n" unless $close;
149 unless($syswrite && $close) {
150 if ($! =~/too large/i) {
151 explain("writing past 2GB failed: process limits?");
152 } elsif ($! =~ /quota/i) {
153 explain("filesystem quota limits?");
155 explain("error: $!");
164 unless ($s[7] == 5_000_000_003) {
165 explain("kernel/fs not configured to use large files?");
175 my ($offset_will_be, $offset_want) = @_;
176 my $offset_is = eval $offset_will_be;
177 unless ($offset_is == $offset_want) {
178 print "# bad offset $offset_is, want $offset_want\n";
179 my ($offset_func) = ($offset_will_be =~ /^(\w+)/);
180 if (unpack("L", pack("L", $offset_want)) == $offset_is) {
181 print "# 32-bit wraparound suspected in $offset_func() since\n";
182 print "# $offset_want cast into 32 bits equals $offset_is.\n";
183 } elsif ($offset_want - unpack("L", pack("L", $offset_want)) - 1
185 print "# 32-bit wraparound suspected in $offset_func() since\n";
186 printf "# %s - unpack('L', pack('L', %s)) - 1 equals %s.\n",
199 fail unless $s[7] == 5_000_000_003; # exercizes pp_stat
202 fail unless -s "big" == 5_000_000_003; # exercizes pp_ftsize
205 fail unless -e "big";
208 fail unless -f "big";
211 sysopen(BIG, "big", O_RDONLY) or do { warn "sysopen failed: $!\n"; bye };
213 offset('sysseek(BIG, 4_500_000_000, SEEK_SET)', 4_500_000_000);
216 offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_000);
219 offset('sysseek(BIG, 1, SEEK_CUR)', 4_500_000_001);
222 offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_001);
225 offset('sysseek(BIG, -1, SEEK_CUR)', 4_500_000_000);
228 offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_000);
231 offset('sysseek(BIG, -3, SEEK_END)', 5_000_000_000);
234 offset('sysseek(BIG, 0, SEEK_CUR)', 5_000_000_000);
239 fail unless sysread(BIG, $big, 3) == 3;
242 fail unless $big eq "big";
245 # 705_032_704 = (I32)5_000_000_000
246 # See that we don't have "big" in the 705_... spot:
247 # that would mean that we have a wraparound.
248 fail unless sysseek(BIG, 705_032_704, SEEK_SET);
253 fail unless read(BIG, $zero, 3) == 3;
256 fail unless $zero eq "\0\0\0";
261 bye(); # does the necessary cleanup
264 unlink "big"; # be paranoid about leaving 5 gig files lying around