-
Notifications
You must be signed in to change notification settings - Fork 0
/
p-testwritefs2.cc
394 lines (268 loc) · 8.2 KB
/
p-testwritefs2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "u-lib.hh"
char bigbuf[4096];
size_t prepare_bigbuf() {
for (unsigned i = 0; i != 46; ++i) {
memcpy(&bigbuf[i * 88], "I should not talk so much about myself if there were any body else whom I knew as well.\n", 88);
}
return 46 * 88;
}
void process_main() {
printf("Starting testwritefs2 (assuming clean file system)...\n");
// read file
printf("%s:%d: read...\n", __FILE__, __LINE__);
int f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
char buf[200];
memset(buf, 0, sizeof(buf));
ssize_t n = sys_read(f, buf, 200);
assert_eq(n, 130);
assert_memeq(buf, "When piped a tiny voice hard by,\n"
"Gay and polite, a cheerful cry,\n"
"Chic-chicadeedee", 81);
sys_close(f);
// truncate file
printf("%s:%d: truncate...\n", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_WRITE | OF_TRUNC);
assert_gt(f, 2);
sys_close(f);
f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
n = sys_read(f, buf, 200);
assert_eq(n, 0);
sys_close(f);
// synchronize disk
printf("%s:%d: sync...\n", __FILE__, __LINE__);
int r = sys_sync(2);
assert_ge(r, 0);
// read again, this time from disk
printf("%s:%d: reread...\n", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
n = sys_read(f, buf, 200);
assert_eq(n, 0);
sys_close(f);
// write into truncated file
printf("%s:%d: write...\n", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_WRITE | OF_TRUNC);
assert_gt(f, 2);
n = sys_write(f, "CLARE ROJAS WAS HERE", 20);
assert_eq(n, 20);
sys_close(f);
f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 200);
assert_eq(n, 20);
assert_memeq(buf, "CLARE ROJAS WAS HERE", 20);
sys_close(f);
// synchronize disk
printf("%s:%d: sync...\n", __FILE__, __LINE__);
r = sys_sync(2);
assert_ge(r, 0);
// read again, this time from disk
printf("%s:%d: reread...\n", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 0, sizeof(buf));
n = sys_read(f, buf, 200);
assert_eq(n, 20);
assert_memeq(buf, "CLARE ROJAS WAS HERE", 20);
sys_close(f);
// seek within a file
printf("%s:%d: lseek...\n", __FILE__, __LINE__);
f = sys_open("thoreau.txt", OF_READ);
assert_gt(f, 2);
n = sys_read(f, buf, 4);
assert_eq(n, 4);
assert_memeq(buf, "The ", 4);
ssize_t sz = sys_lseek(f, 0, LSEEK_SIZE);
assert_eq(sz, 685);
n = sys_read(f, buf, 4);
assert_eq(n, 4);
assert_memeq(buf, "moon", 4);
sz = sys_lseek(f, 0, LSEEK_SET);
assert_eq(sz, 0);
n = sys_read(f, buf, 8);
assert_eq(n, 8);
assert_memeq(buf, "The moon", 8);
sz = sys_lseek(f, -4, LSEEK_CUR);
assert_eq(sz, 4);
n = sys_read(f, buf, 4);
assert_eq(n, 4);
assert_memeq(buf, "moon", 4);
sys_close(f);
// extend the file
printf("%s:%d: extend...\n", __FILE__, __LINE__);
int wf = sys_open("thoreau.txt", OF_WRITE | OF_TRUNC);
assert_gt(wf, 2);
for (unsigned i = 0; i != 100; ++i) {
dprintf(wf, "I should not talk so much about myself if there were any body else whom I knew as well.\n");
}
f = sys_open("thoreau.txt", OF_READ);
assert_gt(f, 2);
memset(buf, 'X', sizeof(buf));
n = sys_read(f, buf, 9);
assert_eq(n, 9);
assert_memeq(buf, "I should X", 10);
sz = sys_lseek(f, 4000, LSEEK_SET);
assert_eq(sz, 4000);
n = sys_read(f, buf, 9);
assert_eq(n, 9);
assert_memeq(buf, "f there wX", 10);
sz = sys_lseek(f, 4090, LSEEK_SET);
assert_eq(sz, 4090);
n = sys_read(f, buf, 40);
assert_eq(n, 40);
assert_memeq(buf, "there were any body else whom I knew as ", 40);
sys_close(f);
sz = sys_lseek(wf, 0, LSEEK_SIZE);
assert_eq(sz, 8800);
sys_close(wf);
// synchronize disk
printf("%s:%d: sync...\n", __FILE__, __LINE__);
r = sys_sync(2);
assert_ge(r, 0);
// read again
printf("%s:%d: reread...\n", __FILE__, __LINE__);
f = sys_open("thoreau.txt", OF_READ);
assert_gt(f, 2);
sz = sys_lseek(f, 0, LSEEK_SIZE);
assert_eq(sz, 8800);
memset(buf, 'X', sizeof(buf));
n = sys_read(f, buf, 9);
assert_eq(n, 9);
assert_memeq(buf, "I should X", 10);
sz = sys_lseek(f, 4000, LSEEK_SET);
assert_eq(sz, 4000);
n = sys_read(f, buf, 9);
assert_eq(n, 9);
assert_memeq(buf, "f there wX", 10);
sz = sys_lseek(f, 4090, LSEEK_SET);
assert_eq(sz, 4090);
n = sys_read(f, buf, 40);
assert_eq(n, 40);
assert_memeq(buf, "there were any body else whom I knew as ", 40);
sys_close(f);
// make a big file
printf("%s:%d: extend more", __FILE__, __LINE__);
size_t bbsz = prepare_bigbuf();
wf = sys_open("thoreau.txt", OF_WRITE);
assert_gt(f, 2);
for (int i = 0; i != 100; ++i) {
dprintf(wf, "Chick%ddee\n", i);
n = sys_write(wf, bigbuf, bbsz);
assert_eq(size_t(n), bbsz);
if (i % 10 == 0) {
printf(".");
}
}
printf("\n");
sz = sys_lseek(f, 0, LSEEK_SIZE);
assert_eq(sz, 405890);
sys_close(wf);
// compute crc32c
printf("%s:%d: check checksum", __FILE__, __LINE__);
f = sys_open("thoreau.txt", OF_READ);
uint32_t crc = 0;
sz = 0;
ssize_t printsz = 0;
while ((n = sys_read(f, bigbuf, sizeof(bigbuf))) > 0) {
crc = crc32c(crc, bigbuf, n);
sz += n;
while (sz > printsz + 40000) {
printf(".");
printsz += 40000;
}
}
printf("\n");
assert_eq(sz, 405890);
assert_eq(crc, 0x76954FBBU);
sys_close(f);
// synchronize disk
printf("%s:%d: sync...\n", __FILE__, __LINE__);
r = sys_sync(2);
assert_ge(r, 0);
// recompute crc32c
printf("%s:%d: recheck checksum", __FILE__, __LINE__);
f = sys_open("thoreau.txt", OF_READ);
crc = 0;
sz = 0;
printsz = 0;
while ((n = sys_read(f, bigbuf, sizeof(bigbuf))) > 0) {
crc = crc32c(crc, bigbuf, n);
sz += n;
while (sz > printsz + 40000) {
printf(".");
printsz += 40000;
}
}
printf("\n");
assert_eq(sz, 405890);
assert_eq(crc, 0x76954FBBU);
sys_close(f);
// extend two files through alternating writes, encouraging creation of
// indirect extents
printf("%s:%d: extend two files", __FILE__, __LINE__);
f = sys_open("emerson.txt", OF_WRITE);
int f2 = sys_open("thoreau.txt", OF_WRITE);
assert_gt(f, 2);
assert_gt(f2, 2);
assert_ne(f, f2);
r = sys_lseek(f, 0, LSEEK_END);
assert_eq(r, 20);
r = sys_lseek(f2, 0, LSEEK_END);
assert_eq(r, 405890);
bbsz = prepare_bigbuf();
for (int i = 0; i != 30; ++i) {
dprintf(f, "Chick%ddee\n", i);
n = sys_write(f, bigbuf, bbsz);
assert_eq(size_t(n), bbsz);
dprintf(f2, "Chick%ddee\n", i);
n = sys_write(f2, bigbuf, bbsz);
assert_eq(size_t(n), bbsz);
if (i % 5 == 0) {
printf(".");
}
}
printf("\n");
sys_close(f);
sys_close(f2);
// synchronize disk
printf("%s:%d: sync...\n", __FILE__, __LINE__);
r = sys_sync(2);
assert_ge(r, 0);
// recompute crc32c
printf("%s:%d: check checksums", __FILE__, __LINE__);
f = sys_open("thoreau.txt", OF_READ);
crc = 0;
sz = 0;
printsz = 0;
while ((n = sys_read(f, bigbuf, sizeof(bigbuf))) > 0) {
crc = crc32c(crc, bigbuf, n);
sz += n;
while (sz > printsz + 40000) {
printf(".");
printsz += 40000;
}
}
assert_eq(sz, 527650);
assert_eq(crc, 2111621591U);
sys_close(f);
f = sys_open("emerson.txt", OF_READ);
crc = 0;
sz = 0;
while ((n = sys_read(f, bigbuf, sizeof(bigbuf))) > 0) {
crc = crc32c(crc, bigbuf, n);
sz += n;
while (sz > printsz + 40000) {
printf(".");
printsz += 40000;
}
}
printf("\n");
assert_eq(sz, 121780);
assert_eq(crc, 1518875118U);
sys_close(f);
printf("testwritefs2 succeeded.\n");
sys_exit(0);
}