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 5BEGIN { 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 15use strict; 16use File::Temp 'tempfile'; 17use Test::More; 18 19our @s; 20 21(undef, my $big0) = tempfile(UNLINK => 1); 22(undef, my $big1) = tempfile(UNLINK => 1); 23(undef, my $big2) = tempfile(UNLINK => 1); 24 25my $explained; 26 27sub explain { 28 unless ($explained++) { 29 print <<EOM; 30# 31# If the lfs (large file support: large meaning larger than two 32# gigabytes) tests are skipped or fail, it may mean either that your 33# process (or process group) is not allowed to write large files 34# (resource limits) or that the file system (the network filesystem?) 35# you are running the tests on doesn't let your user/group have large 36# files (quota) or the filesystem simply doesn't support large files. 37# You may even need to reconfigure your kernel. (This is all very 38# operating system and site-dependent.) 39# 40# Perl may still be able to support large files, once you have 41# such a process, enough quota, and such a (file) system. 42# It is just that the test failed now. 43# 44EOM 45 } 46 if (@_) { 47 plan(skip_all => "@_"); 48 } 49} 50 51$| = 1; 52 53print "# checking whether we have sparse files...\n"; 54 55# Known have-nots. 56if ($^O eq 'MSWin32' || $^O eq 'VMS') { 57 plan(skip_all => "no sparse files in $^O"); 58} 59 60# Known haves that have problems running this test 61# (for example because they do not support sparse files, like UNICOS) 62if ($^O eq 'unicos') { 63 plan(skip_all => "no sparse files in $^O, unable to test large files"); 64} 65 66# Then try heuristically to deduce whether we have sparse files. 67 68# We'll start off by creating a one megabyte file which has 69# only three "true" bytes. If we have sparseness, we should 70# consume less blocks than one megabyte (assuming nobody has 71# one megabyte blocks...) 72 73sysopen(BIG, $big1, O_WRONLY|O_CREAT|O_TRUNC) or 74 die "sysopen $big1 failed: $!"; 75binmode BIG; 76sysseek(BIG, 1_000_000, SEEK_SET) or 77 die "sysseek $big1 failed: $!"; 78syswrite(BIG, "big") or 79 die "syswrite $big1 failed: $!"; 80close(BIG) or 81 die "close $big1 failed: $!"; 82 83my @s1 = stat($big1); 84 85print "# s1 = @s1\n"; 86 87sysopen(BIG, $big2, O_WRONLY|O_CREAT|O_TRUNC) or 88 die "sysopen $big2 failed: $!"; 89binmode BIG; 90sysseek(BIG, 2_000_000, SEEK_SET) or 91 die "sysseek $big2 failed: $!"; 92syswrite(BIG, "big") or 93 die "syswrite $big2 failed: $!"; 94close(BIG) or 95 die "close $big2 failed: $!"; 96 97my @s2 = stat($big2); 98 99print "# s2 = @s2\n"; 100 101unless ($s1[7] == 1_000_003 && $s2[7] == 2_000_003 && 102 $s1[11] == $s2[11] && $s1[12] == $s2[12] && 103 $s1[12] > 0) { 104 plan(skip_all => "no sparse files?"); 105} 106 107print "# we seem to have sparse files...\n"; 108 109# By now we better be sure that we do have sparse files: 110# if we are not, the following will hog 5 gigabytes of disk. Ooops. 111# This may fail by producing some signal; run in a subprocess first for safety 112 113$ENV{LC_ALL} = "C"; 114 115my $perl = '../../perl'; 116unless (-x $perl) { 117 plan(tests => 1); 118 fail("can't find perl: expected $perl"); 119} 120my $r = system $perl, '-I../lib', '-e', <<"EOF"; 121use Fcntl qw(/^O_/ /^SEEK_/); 122sysopen \$big, q{$big0}, O_WRONLY|O_CREAT|O_TRUNC or die qq{sysopen $big0 $!}; 123sysseek \$big, 5_000_000_000, SEEK_SET or die qq{sysseek $big0 $!}; 124syswrite \$big, "big" or die qq{syswrite $big0 $!}; 125close \$big or die qq{close $big0: $!}; 126exit 0; 127EOF 128 129 130sysopen(BIG, $big0, O_WRONLY|O_CREAT|O_TRUNC) or 131 die "sysopen $big0 failed: $!"; 132binmode BIG; 133my $sysseek = sysseek(BIG, 5_000_000_000, SEEK_SET); 134unless (! $r && defined $sysseek && $sysseek == 5_000_000_000) { 135 $sysseek = 'undef' unless defined $sysseek; 136 explain("seeking past 2GB failed: ", 137 $r ? 'signal '.($r & 0x7f) : "$! (sysseek returned $sysseek)"); 138} 139 140# The syswrite will fail if there are are filesize limitations (process or fs). 141my $syswrite = syswrite(BIG, "big"); 142print "# syswrite failed: $! (syswrite returned ", 143 defined $syswrite ? $syswrite : 'undef', ")\n" 144 unless defined $syswrite && $syswrite == 3; 145my $close = close BIG; 146print "# close failed: $!\n" unless $close; 147unless($syswrite && $close) { 148 if ($! =~/too large/i) { 149 explain("writing past 2GB failed: process limits?"); 150 } elsif ($! =~ /quota/i) { 151 explain("filesystem quota limits?"); 152 } else { 153 explain("error: $!"); 154 } 155} 156 157@s = stat($big0); 158 159print "# @s\n"; 160 161unless ($s[7] == 5_000_000_003) { 162 explain("kernel/fs not configured to use large files?"); 163} 164 165sub offset ($$) { 166 local $Test::Builder::Level = $Test::Builder::Level + 1; 167 my ($offset_will_be, $offset_want) = @_; 168 my $offset_is = eval $offset_will_be; 169 unless ($offset_is == $offset_want) { 170 print "# bad offset $offset_is, want $offset_want\n"; 171 my ($offset_func) = ($offset_will_be =~ /^(\w+)/); 172 if (unpack("L", pack("L", $offset_want)) == $offset_is) { 173 print "# 32-bit wraparound suspected in $offset_func() since\n"; 174 print "# $offset_want cast into 32 bits equals $offset_is.\n"; 175 } elsif ($offset_want - unpack("L", pack("L", $offset_want)) - 1 176 == $offset_is) { 177 print "# 32-bit wraparound suspected in $offset_func() since\n"; 178 printf "# %s - unpack('L', pack('L', %s)) - 1 equals %s.\n", 179 $offset_want, 180 $offset_want, 181 $offset_is; 182 } 183 fail($offset_will_be); 184 } else { 185 pass($offset_will_be); 186 } 187} 188 189plan(tests => 17); 190 191is($s[7], 5_000_000_003, 'exercises pp_stat'); 192is(-s $big0, 5_000_000_003, 'exercises pp_ftsize'); 193 194is(-e $big0, 1); 195is(-f $big0, 1); 196 197sysopen(BIG, $big0, O_RDONLY) or die "sysopen failed: $!"; 198binmode BIG; 199offset('sysseek(BIG, 4_500_000_000, SEEK_SET)', 4_500_000_000); 200 201offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_000); 202 203# If you get 205_032_705 from here it means that 204# your tell() is returning 32-bit values since (I32)4_500_000_001 205# is exactly 205_032_705. 206offset('sysseek(BIG, 1, SEEK_CUR)', 4_500_000_001); 207 208offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_001); 209 210offset('sysseek(BIG, -1, SEEK_CUR)', 4_500_000_000); 211 212offset('sysseek(BIG, 0, SEEK_CUR)', 4_500_000_000); 213 214offset('sysseek(BIG, -3, SEEK_END)', 5_000_000_000); 215 216offset('sysseek(BIG, 0, SEEK_CUR)', 5_000_000_000); 217 218my $big; 219 220is(sysread(BIG, $big, 3), 3); 221 222is($big, "big"); 223 224# 705_032_704 = (I32)5_000_000_000 225# See that we don't have "big" in the 705_... spot: 226# that would mean that we have a wraparound. 227isnt(sysseek(BIG, 705_032_704, SEEK_SET), undef); 228 229my $zero; 230 231is(read(BIG, $zero, 3), 3); 232 233is($zero, "\0\0\0"); 234 235explain() unless Test::Builder->new()->is_passing(); 236 237END { 238 # unlink may fail if applied directly to a large file 239 # be paranoid about leaving 5 gig files lying around 240 open(BIG, '>', $big0); # truncate 241 close(BIG); 242} 243 244# eof 245