Recode the naughty binary bytes ([\x00-\x08\x0b-\x1f\x7f-\xff])
[p5sagit/p5-mst-13.2.git] / t / lib / 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         chdir 't' if -d 't';
7         @INC = '../lib';
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";
12                 exit(0);
13         }
14         require Fcntl; import Fcntl qw(/^O_/ /^SEEK_/);
15 }
16
17 sub zap {
18     close(BIG);
19     unlink("big");
20     unlink("big1");
21     unlink("big2");
22 }
23
24 sub bye {
25     zap(); 
26     exit(0);
27 }
28
29 my $explained;
30
31 sub explain {
32     unless ($explained++) {
33         print <<EOM;
34 #
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.)
43 #
44 # Perl may still be able to support large files, once you have
45 # such a process, enough quota, and such a (file) system.
46 # It is just that the test failed now.
47 #
48 EOM
49     }
50     print "1..0 # Skip: @_\n" if @_;
51 }
52
53 print "# checking whether we have sparse files...\n";
54
55 # Known have-nots.
56 if ($^O eq 'MSWin32' || $^O eq 'VMS') {
57     print "1..0 # Skip: no sparse files in $^O\n";
58     bye();
59 }
60
61 # Known haves that have problems running this test
62 # (for example because they do not support sparse files, like UNICOS)
63 if ($^O eq 'unicos') {
64     print "1..0 # Skip: no sparse files in $^0, unable to test large files\n";
65     bye();
66 }
67
68 # Then try heuristically to deduce whether we have sparse files.
69
70 # We'll start off by creating a one megabyte file which has
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...)
74
75 sysopen(BIG, "big1", O_WRONLY|O_CREAT|O_TRUNC) or
76     do { warn "sysopen big1 failed: $!\n"; bye };
77 sysseek(BIG, 1_000_000, SEEK_SET) or
78     do { warn "sysseek big1 failed: $!\n"; bye };
79 syswrite(BIG, "big") or
80     do { warn "syswrite big1 failed; $!\n"; bye };
81 close(BIG) or
82     do { warn "close big1 failed: $!\n"; bye };
83
84 my @s1 = stat("big1");
85
86 print "# s1 = @s1\n";
87
88 sysopen(BIG, "big2", O_WRONLY|O_CREAT|O_TRUNC) or
89     do { warn "sysopen big2 failed: $!\n"; bye };
90 sysseek(BIG, 2_000_000, SEEK_SET) or
91     do { warn "sysseek big2 failed: $!\n"; bye };
92 syswrite(BIG, "big") or
93     do { warn "syswrite big2 failed; $!\n"; bye };
94 close(BIG) or
95     do { warn "close big2 failed: $!\n"; bye };
96
97 my @s2 = stat("big2");
98
99 print "# s2 = @s2\n";
100
101 zap();
102
103 unless ($s1[7] == 1_000_003 && $s2[7] == 2_000_003 &&
104         $s1[11] == $s2[11] && $s1[12] == $s2[12]) {
105         print "1..0 # Skip: no sparse files?\n";
106         bye;
107 }
108
109 print "# we seem to have sparse files...\n";
110
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.
113 # This may fail by producing some signal; run in a subprocess first for safety
114
115 $ENV{LC_ALL} = "C";
116
117 my $r = system '../perl', '-I../lib', '-e', <<'EOF';
118 use Fcntl qw(/^O_/ /^SEEK_/);
119 sysopen(BIG, "big", O_WRONLY|O_CREAT|O_TRUNC) or die $!;
120 my $sysseek = sysseek(BIG, 5_000_000_000, SEEK_SET);
121 my $syswrite = syswrite(BIG, "big");
122 exit 0;
123 EOF
124
125 sysopen(BIG, "big", O_WRONLY|O_CREAT|O_TRUNC) or
126         do { warn "sysopen 'big' failed: $!\n"; bye };
127 my $sysseek = sysseek(BIG, 5_000_000_000, SEEK_SET);
128 unless (! $r && defined $sysseek && $sysseek == 5_000_000_000) {
129     $sysseek = 'undef' unless defined $sysseek;
130     explain("seeking past 2GB failed: ",
131             $r ? 'signal '.($r & 0x7f) : "$! (sysseek returned $sysseek)");
132     bye();
133 }
134
135 # The syswrite will fail if there are are filesize limitations (process or fs).
136 my $syswrite = syswrite(BIG, "big");
137 print "# syswrite failed: $! (syswrite returned ",
138       defined $syswrite ? $syswrite : 'undef', ")\n"
139     unless defined $syswrite && $syswrite == 3;
140 my $close     = close BIG;
141 print "# close failed: $!\n" unless $close;
142 unless($syswrite && $close) {
143     if ($! =~/too large/i) {
144         explain("writing past 2GB failed: process limits?");
145     } elsif ($! =~ /quota/i) {
146         explain("filesystem quota limits?");
147     } else {
148         explain("error: $!");
149     }
150     bye();
151 }
152
153 @s = stat("big");
154
155 print "# @s\n";
156
157 unless ($s[7] == 5_000_000_003) {
158     explain("kernel/fs not configured to use large files?");
159     bye();
160 }
161
162 sub fail () {
163     print "not ";
164     $fail++;
165 }
166
167 print "1..17\n";
168
169 my $fail = 0;
170
171 fail unless $s[7] == 5_000_000_003;     # exercizes pp_stat
172 print "ok 1\n";
173
174 fail unless -s "big" == 5_000_000_003;  # exercizes pp_ftsize
175 print "ok 2\n";
176
177 fail unless -e "big";
178 print "ok 3\n";
179
180 fail unless -f "big";
181 print "ok 4\n";
182
183 sysopen(BIG, "big", O_RDONLY) or do { warn "sysopen failed: $!\n"; bye };
184
185 fail unless sysseek(BIG, 4_500_000_000, SEEK_SET) == 4_500_000_000;
186 print "ok 5\n";
187
188 fail unless sysseek(BIG, 0, SEEK_CUR) == 4_500_000_000;
189 print "ok 6\n";
190
191 fail unless sysseek(BIG, 1, SEEK_CUR) == 4_500_000_001;
192 print "ok 7\n";
193
194 fail unless sysseek(BIG, 0, SEEK_CUR) == 4_500_000_001;
195 print "ok 8\n";
196
197 fail unless sysseek(BIG, -1, SEEK_CUR) == 4_500_000_000;
198 print "ok 9\n";
199
200 fail unless sysseek(BIG, 0, SEEK_CUR) == 4_500_000_000;
201 print "ok 10\n";
202
203 fail unless sysseek(BIG, -3, SEEK_END) == 5_000_000_000;
204 print "ok 11\n";
205
206 fail unless sysseek(BIG, 0, SEEK_CUR) == 5_000_000_000;
207 print "ok 12\n";
208
209 my $big;
210
211 fail unless sysread(BIG, $big, 3) == 3;
212 print "ok 13\n";
213
214 fail unless $big eq "big";
215 print "ok 14\n";
216
217 # 705_032_704 = (I32)5_000_000_000
218 fail unless sysseek(BIG, 705_032_704, SEEK_SET);
219 print "ok 15\n";
220
221 my $zero;
222
223 fail unless read(BIG, $zero, 3) == 3;
224 print "ok 16\n";
225
226 fail unless $zero eq "\0\0\0";
227 print "ok 17\n";
228
229 explain() if $fail;
230
231 bye(); # does the necessary cleanup
232
233 END {
234    unlink "big"; # be paranoid about leaving 5 gig files lying around
235 }
236
237 # eof