/
multitape_read.c
553 lines (470 loc) · 12 KB
/
multitape_read.c
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include "bsdtar_platform.h"
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "chunks.h"
#include "crypto.h"
#include "ctassert.h"
#include "multitape_internal.h"
#include "storage.h"
#include "sysendian.h"
#include "warnp.h"
#include "multitape.h"
/*
* The readtape_read API relies upon chunks being no more than SSIZE_MAX
* bytes in length.
*/
CTASSERT(MAXCHUNK <= SSIZE_MAX);
/* Stream parameters. */
struct stream {
struct stream * istr; /* Index stream. */
uint8_t * chunk; /* Buffer for holding a chunk. */
size_t chunklen; /* Length of current chunk. */
size_t chunkpos; /* Position within current chunk. */
off_t skiplen; /* Length to skip. */
struct chunkheader ch; /* Pending chunk header. */
int ch_valid; /* Non-zero if ch is valid. */
};
/* Cookie created by readtape_open and passed to other functions. */
struct multitape_read_internal {
struct stream h; /* Headers. */
struct stream hi; /* Headers index. */
struct stream c; /* Chunks. */
struct stream ci; /* Chunks index. */
struct stream cii; /* Chunks index index. */
struct stream t; /* Trailers. */
struct stream ti; /* Trailers index. */
off_t hlen; /* Queued length of header. */
off_t clen; /* Queued length of chunked data. */
off_t tlen; /* Queued length of trailer. */
struct tapemetaindex tmi; /* Metaindex. */
STORAGE_R * S; /* Storage layer cookie. */
CHUNKS_R * C; /* Chunk layer cookie. */
};
static int stream_get_chunkheader(struct stream *, CHUNKS_R *);
static int stream_get_chunk(struct stream *, const uint8_t **, size_t *,
CHUNKS_R *);
static ssize_t stream_read(struct stream *, uint8_t *, size_t, CHUNKS_R *);
/**
* stream_get_chunkheader(S, C):
* Fill ${S}->ch with the header for the next chunk, and set ch_valid to 1.
* On EOF of the parent stream, ch_valid will remain zero.
*/
static int
stream_get_chunkheader(struct stream * S, CHUNKS_R * C)
{
off_t len;
ssize_t readlen;
/* Loop until we have a chunk which we're not skipping. */
do {
if (S->ch_valid) {
len = le32dec(S->ch.len);
if (len <= S->skiplen) {
S->skiplen -= len;
S->ch_valid = 0;
} else {
/* We have a useful chunk. */
break;
}
}
/* Get a chunk header from the parent stream. */
readlen = stream_read(S->istr, (uint8_t *)&S->ch,
sizeof(struct chunkheader), C);
switch(readlen) {
case -1:
/* Error in stream_read. */
goto err0;
case 0:
/* No more chunks available. */
goto eof;
case sizeof(struct chunkheader):
/* Successful read of chunk header. */
S->ch_valid = 1;
break;
default:
/* Wrong length read. */
warnp("Premature EOF of archive index");
goto err0;
}
} while (1);
eof:
/* Success! */
return (0);
err0:
/* Failure! */
return (-1);
}
/**
* stream_get_chunk(S, buf, clen, C):
* Set ${buf} to point to the next available data, and set ${clen} to the
* length of data available; use the chunk read cookie ${C} if needed.
*/
static int
stream_get_chunk(struct stream * S, const uint8_t ** buf, size_t * clen,
CHUNKS_R * C)
{
size_t len, zlen;
off_t skip;
/* Skip part of the current chunk if appropriate. */
if (S->skiplen) {
skip = S->chunklen - S->chunkpos;
if (skip > S->skiplen)
skip = S->skiplen;
S->skiplen -= skip;
S->chunkpos += skip;
}
while ((S->chunklen == S->chunkpos) && (S->istr != NULL)) {
/* Get a chunk header. */
if (stream_get_chunkheader(S, C))
goto err0;
/* EOF? */
if (S->ch_valid == 0)
goto eof;
/* Decode chunk header. */
len = le32dec(S->ch.len);
zlen = le32dec(S->ch.zlen);
/* Read chunk. */
if (chunks_read_chunk(C, S->ch.hash, len, zlen, S->chunk, 0))
goto err0;
S->chunklen = len;
/* Set current position within buffer. */
S->chunkpos = S->skiplen;
S->skiplen = 0;
/* The chunk is no longer pending. */
S->ch_valid = 0;
}
/* We have some data. */
*buf = S->chunk + S->chunkpos;
*clen = S->chunklen - S->chunkpos;
/* Success! */
return (0);
eof:
*clen = 0;
*buf = NULL;
/* Success, but no data. */
return (0);
err0:
/* Failure! */
return (-1);
}
/**
* stream_read(S, buf, buflen, C):
* Fill ${buf} with ${buflen} bytes of data from the stream ${S}. Return the
* length written (which may be less than ${buflen} on EOF).
*/
static ssize_t
stream_read(struct stream * S, uint8_t * buf, size_t buflen, CHUNKS_R * C)
{
const uint8_t * readbuf;
size_t readlen;
size_t bufpos;
for (bufpos = 0; bufpos < buflen; bufpos += readlen) {
/* Read data. */
if (stream_get_chunk(S, &readbuf, &readlen, C))
goto err0;
/* Make sure we don't have too much data. */
if (readlen > buflen - bufpos)
readlen = buflen - bufpos;
/* Mark the data as consumed. */
S->chunkpos += readlen;
/* Copy into the correct position in our buffer. */
memcpy(buf + bufpos, readbuf, readlen);
/* Stop looping if we have no more data. */
if (readlen == 0)
break;
}
/* Success (or perhaps EOF). */
return (bufpos);
err0:
/* Failure! */
return (-1);
}
/**
* readtape_open(machinenum, tapename):
* Open the tape with the given name, and return a cookie which can be used
* for accessing it.
*/
TAPE_R *
readtape_open(uint64_t machinenum, const char * tapename)
{
struct multitape_read_internal * d;
struct tapemetadata tmd;
/* Allocate memory. */
if ((d = malloc(sizeof(struct multitape_read_internal))) == NULL)
goto err0;
memset(d, 0, sizeof(struct multitape_read_internal));
/* Obtain a storage layer read cookie. */
if ((d->S = storage_read_init(machinenum)) == NULL)
goto err1;
/* Obtain a chunk layer read cookie. */
if ((d->C = chunks_read_init(d->S, MAXCHUNK)) == NULL)
goto err2;
/* Allocate chunk buffers. */
d->h.chunk = malloc(MAXCHUNK);
d->c.chunk = malloc(MAXCHUNK);
d->ci.chunk = malloc(MAXCHUNK);
d->t.chunk = malloc(MAXCHUNK);
if ((d->h.chunk == NULL) || (d->c.chunk == NULL) ||
(d->ci.chunk == NULL) || (d->t.chunk == NULL))
goto err3;
/* Initialize streams. */
d->h.istr = &d->hi;
d->c.istr = &d->ci;
d->ci.istr = &d->cii;
d->t.istr = &d->ti;
/* Read the tape metadata. */
if (multitape_metadata_get_byname(d->S, NULL, &tmd, tapename, 0))
goto err3;
/* Read the tape metaindex. */
if (multitape_metaindex_get(d->S, NULL, &d->tmi, &tmd, 0))
goto err4;
/* Free parsed metadata. */
multitape_metadata_free(&tmd);
/* Initialize streams. */
d->hi.chunklen = d->tmi.hindexlen;
d->hi.chunk = d->tmi.hindex;
d->cii.chunklen = d->tmi.cindexlen;
d->cii.chunk = d->tmi.cindex;
d->ti.chunklen = d->tmi.tindexlen;
d->ti.chunk = d->tmi.tindex;
/* Success! */
return (d);
err4:
multitape_metadata_free(&tmd);
err3:
free(d->h.chunk);
free(d->c.chunk);
free(d->ci.chunk);
free(d->t.chunk);
chunks_read_free(d->C);
err2:
storage_read_free(d->S);
err1:
free(d);
err0:
/* Failure! */
return (NULL);
}
/**
* readtape_read(d, buffer):
* Read some data from the tape associated with ${d}, make *${buffer}
* point to the data, and return the number of bytes read.
*/
ssize_t
readtape_read(TAPE_R * d, const void ** buffer)
{
const uint8_t ** buf = (const uint8_t **)buffer;
struct entryheader eh;
ssize_t readlen;
struct stream * readstream;
off_t * readmaxlen;
size_t clen;
/* Loop until we read EOF or have some data to return. */
do {
if (d->hlen) {
/* We want some header data. */
readstream = &d->h;
readmaxlen = &d->hlen;
} else if (d->clen) {
/* We want some chunk data. */
readstream = &d->c;
readmaxlen = &d->clen;
} else if (d->tlen) {
/* We want some trailer data. */
readstream = &d->t;
readmaxlen = &d->tlen;
} else {
/* Read an archive entry header. */
readlen = stream_read(&d->h, (uint8_t *)&eh,
sizeof(struct entryheader), d->C);
switch(readlen) {
case -1:
/* Error in stream_read. */
goto err0;
case 0:
/* No more chunks available. */
goto eof;
case sizeof(struct entryheader):
/* Successful read of chunk header. */
break;
default:
/* Wrong length read. */
warnp("Premature EOF of archive index");
goto err0;
}
/* Decode entry header. */
d->hlen = le32dec(eh.hlen);
d->clen = le64dec(eh.clen);
d->tlen = le32dec(eh.tlen);
continue;
}
if (stream_get_chunk(readstream, buf, &clen, d->C))
goto err0;
if ((off_t)clen > *readmaxlen)
clen = *readmaxlen;
readstream->chunkpos += clen;
*readmaxlen -= clen;
/* Do we have data? */
if (clen)
break;
/* Premature EOF. */
warnp("Premature EOF reading archive");
goto err0;
} while (1);
/* Success! */
return (clen);
eof:
/* No more data. */
return (0);
err0:
/* Failure! */
return (-1);
}
/**
* readtape_readchunk(d, ch):
* Obtain a chunk header suitable for passing to writetape_writechunk.
* Return the length of the chunk, or 0 if no chunk is available (EOF or if
* the tape position isn't aligned at a chunk boundary).
*/
ssize_t
readtape_readchunk(TAPE_R * d, struct chunkheader ** ch)
{
size_t len;
/*
* We can only return a chunk if we're in the chunk portion of an
* archive entry.
*/
if (d->hlen != 0 || d->clen == 0)
goto nochunk;
/*
* Make sure we're not in the middle of a chunk (this should never
* happen, since this stream contains complete chunks from files!)
*/
if (d->c.chunkpos != d->c.chunklen) {
warn0("c.chunkpos != c.chunklen");
goto err0;
}
/* Get a chunk header. */
if (stream_get_chunkheader(&d->c, d->C))
goto err0;
/*
* EOF is an error, but we'll ignore it and let it be reported when
* readtape_read is next called.
*/
if (d->c.ch_valid == 0)
goto nochunk;
/* We need to be properly aligned on a chunk boundary. */
if (d->c.skiplen != 0)
goto nochunk;
/* We have a chunk! */
*ch = &d->c.ch;
len = le32dec(d->c.ch.len);
/* Return the chunk length. */
return (len);
nochunk:
/* We don't have a chunk available. */
return (0);
err0:
/* Failure! */
return (-1);
}
/**
* readtape_skip(d, request):
* Skip up to ${request} bytes from the tape associated with ${d},
* and return the length skipped.
*/
off_t
readtape_skip(TAPE_R * d, off_t request)
{
struct entryheader eh;
ssize_t readlen;
off_t skipped;
off_t skiplen;
/* Loop until we have skipped enough. */
for (skipped = 0; skipped < request;) {
if (d->hlen) {
/* We want to skip some header data. */
if (request - skipped < d->hlen)
skiplen = request - skipped;
else
skiplen = d->hlen;
d->hlen -= skiplen;
d->h.skiplen += skiplen;
skipped += skiplen;
} else if (d->clen) {
/* We want to skip some chunk data. */
if (request - skipped < d->clen)
skiplen = request - skipped;
else
skiplen = d->clen;
d->clen -= skiplen;
d->c.skiplen += skiplen;
skipped += skiplen;
} else if (d->tlen) {
/* We want to skip some trailer data. */
if (request - skipped < d->tlen)
skiplen = request - skipped;
else
skiplen = d->tlen;
d->tlen -= skiplen;
d->t.skiplen += skiplen;
skipped += skiplen;
} else {
/* Read an archive entry header. */
readlen = stream_read(&d->h, (uint8_t *)&eh,
sizeof(struct entryheader), d->C);
switch(readlen) {
case -1:
/* Error in stream_read. */
goto err0;
case 0:
/* No more chunks available. */
goto eof;
case sizeof(struct entryheader):
/* Successful read of chunk header. */
break;
default:
/* Wrong length read. */
warnp("Premature EOF of archive index");
goto err0;
}
/* Decode entry header. */
d->hlen = le32dec(eh.hlen);
d->clen = le64dec(eh.clen);
d->tlen = le32dec(eh.tlen);
}
}
/* Success! */
return (skipped);
eof:
/* No more data. */
return (skipped);
err0:
/* Failure! */
return (-1);
}
/**
* readtape_close(d):
* Close the tape associated with ${d}.
*/
int
readtape_close(TAPE_R * d)
{
/* Free metaindex buffers. */
multitape_metaindex_free(&d->tmi);
/* Free buffers. */
free(d->h.chunk);
free(d->c.chunk);
free(d->ci.chunk);
free(d->t.chunk);
/* Close the chunk layer read cookie. */
chunks_read_free(d->C);
/* Close the storage layer read cookie. */
storage_read_free(d->S);
/* Free the multitape layer read cookie. */
free(d);
/* Success! */
return (0);
}