1N/A# NOTE: this file tests how large files (>2GB) work with perlio (
stdio/sfio).
1N/A chdir 't' if -d 't';
1N/A # Don't bother if there are no quad offsets.
1N/A require Config; import Config;
1N/A if ($Config{lseeksize} < 8) {
1N/A print "1..0 # Skip: no 64-bit file offsets\n";
1N/A unless ($explained++) {
1N/A# If the lfs (large file support: large meaning larger than two
1N/A# gigabytes) tests are skipped or fail, it may mean either that your
1N/A# process (or process group) is not allowed to write large files
1N/A# (resource limits) or that the file system (the network filesystem?)
1N/A# you are running the tests on doesn't let your
user/group have large
1N/A# files (quota) or the filesystem simply doesn't support large files.
1N/A# You may even need to reconfigure your kernel. (This is all very
1N/A# operating system and site-dependent.)
1N/A# Perl may still be able to support large files, once you have
1N/A# such a process, enough quota, and such a (file) system.
1N/A# It is just that the test failed now.
1N/A print "1..0 # Skip: @_\n" if @_;
1N/Aprint "# checking whether we have sparse files...\n";
1N/Aif ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') {
1N/A print "1..0 # Skip: no sparse files in $^O\n";
1N/A# Known haves that have problems running this test
1N/A# (for example because they do not support sparse files, like UNICOS)
1N/Aif ($^O eq 'unicos') {
1N/A print "1..0 # Skip: no sparse files in $^0, unable to test large files\n";
1N/A# Then try to heuristically deduce whether we have sparse files.
1N/A# Let's not depend on Fcntl or any other extension.
1N/Amy ($SEEK_SET, $SEEK_CUR, $SEEK_END) = (0, 1, 2);
1N/A# We'll start off by creating a one megabyte file which has
1N/A# only three "true" bytes. If we have sparseness, we should
1N/A# consume less blocks than one megabyte (assuming nobody has
1N/A# one megabyte blocks...)
1N/Aopen(BIG, ">big1") or
1N/A do { warn "open big1 failed: $!\n"; bye };
1N/A do { warn "binmode big1 failed: $!\n"; bye };
1N/Aseek(BIG, 1_000_000, $SEEK_SET) or
1N/A do { warn "seek big1 failed: $!\n"; bye };
1N/A do { warn "print big1 failed: $!\n"; bye };
1N/A do { warn "close big1 failed: $!\n"; bye };
1N/Amy @s1 = stat("big1");
1N/Aprint "# s1 = @s1\n";
1N/Aopen(BIG, ">big2") or
1N/A do { warn "open big2 failed: $!\n"; bye };
1N/A do { warn "binmode big2 failed: $!\n"; bye };
1N/Aseek(BIG, 2_000_000, $SEEK_SET) or
1N/A do { warn "seek big2 failed; $!\n"; bye };
1N/A do { warn "print big2 failed; $!\n"; bye };
1N/A do { warn "close big2 failed; $!\n"; bye };
1N/Amy @s2 = stat("big2");
1N/Aprint "# s2 = @s2\n";
1N/Aunless ($s1[7] == 1_000_003 && $s2[7] == 2_000_003 &&
1N/A $s1[11] == $s2[11] && $s1[12] == $s2[12]) {
1N/A print "1..0 # Skip: no sparse files?\n";
1N/Aprint "# we seem to have sparse files...\n";
1N/A# By now we better be sure that we do have sparse files:
1N/A# if we are not, the following will hog 5 gigabytes of disk. Ooops.
1N/A# This may fail by producing some signal; run in a subprocess first for safety
1N/Amy $r = system '../perl', '-e', <<'EOF';
1N/Aseek(BIG, 5_000_000_000, 0);
1N/Aopen(BIG, ">big") or do { warn "open failed: $!\n"; bye };
1N/Aif ($r or not seek(BIG, 5_000_000_000, $SEEK_SET)) {
1N/A my $err = $r ? 'signal '.($r & 0x7f) : $!;
1N/A explain("seeking past 2GB failed: $err");
1N/A# Either the print or (more likely, thanks to buffering) the close will
1N/A# fail if there are are filesize limitations (process or fs).
1N/Amy $print = print BIG "big";
1N/Aprint "# print failed: $!\n" unless $print;
1N/Amy $close = close BIG;
1N/Aprint "# close failed: $!\n" unless $close;
1N/Aunless ($print && $close) {
1N/A if ($! =~/too large/i) {
1N/A explain("writing past 2GB failed: process limits?");
1N/A } elsif ($! =~ /quota/i) {
1N/A explain("filesystem quota limits?");
1N/A explain("error: $!");
1N/Aunless ($s[7] == 5_000_000_003) {
1N/A my ($offset_will_be, $offset_want) = @_;
1N/A my $offset_is = eval $offset_will_be;
1N/A unless ($offset_is == $offset_want) {
1N/A print "# bad offset $offset_is, want $offset_want\n";
1N/A my ($offset_func) = ($offset_will_be =~ /^(\w+)/);
1N/A if (unpack("L", pack("L", $offset_want)) == $offset_is) {
1N/A print "# 32-bit wraparound suspected in $offset_func() since\n";
1N/A print "# $offset_want cast into 32 bits equals $offset_is.\n";
1N/A } elsif ($offset_want - unpack("L", pack("L", $offset_want)) - 1
1N/A print "# 32-bit wraparound suspected in $offset_func() since\n";
1N/A printf "# %s - unpack('L', pack('L', %s)) - 1 equals %s.\n",
1N/Afail unless $s[7] == 5_000_000_003; # exercizes pp_stat
1N/Afail unless -s "big" == 5_000_000_003; # exercizes pp_ftsize
1N/Afail unless -e "big";
1N/Afail unless -f "big";
1N/Aopen(BIG, "big") or do { warn "open failed: $!\n"; bye };
1N/Afail unless seek(BIG, 4_500_000_000, $SEEK_SET);
1N/Aoffset('tell(BIG)', 4_500_000_000);
1N/Afail unless seek(BIG, 1, $SEEK_CUR);
1N/A# If you get 205_032_705 from here it means that
1N/A# your tell() is returning 32-bit values since (I32)4_500_000_001
1N/A# is exactly 205_032_705.
1N/Aoffset('tell(BIG)', 4_500_000_001);
1N/Afail unless seek(BIG, -1, $SEEK_CUR);
1N/Aoffset('tell(BIG)', 4_500_000_000);
1N/Afail unless seek(BIG, -3, $SEEK_END);
1N/Aoffset('tell(BIG)', 5_000_000_000);
1N/Afail unless read(BIG, $big, 3) == 3;
1N/Afail unless $big eq "big";
1N/A# 705_032_704 = (I32)5_000_000_000
1N/A# See that we don't have "big" in the 705_... spot:
1N/A# that would mean that we have a wraparound.
1N/Afail unless seek(BIG, 705_032_704, $SEEK_SET);
1N/Afail unless read(BIG, $zero, 3) == 3;
1N/Afail unless $zero eq "\0\0\0";
1N/Abye(); # does the necessary cleanup
1N/A # unlink may fail if applied directly to a large file
1N/A # be paranoid about leaving 5 gig files lying around
1N/A open(BIG, ">big"); # truncate
1N/A 1 while unlink "big"; # standard portable idiom