File: tar.cc

package info (click to toggle)
tardy 1.12-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 1,604 kB
  • ctags: 1,015
  • sloc: cpp: 9,530; sh: 4,241; makefile: 724; awk: 44
file content (437 lines) | stat: -rw-r--r-- 9,285 bytes parent folder | download | duplicates (3)
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
/*
 *	tardy - a tar post-processor
 *	Copyright (C) 1998, 1999, 2001-2004 Peter Miller;
 *	All rights reserved.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate tar input
 */

#include <ac/errno.h>
#include <ac/string.h>

#include <error.h>
#include <tar/format.h>
#include <tar/input/tar.h>


tar_input_tar::~tar_input_tar()
{
    delete fp;
}


tar_input_tar::tar_input_tar(file_input *arg) :
    fp(arg),
    pos(0),
    state(0)
{
}


static int
all_zero(char *buf, long len)
{
    while (len > 0)
    {
	if (*buf++)
	    return 0;
	--len;
    }
    return 1;
}


void
tar_input_tar::check_state(int a, int b)
{
    if (state != a && (b >= 0 && state != b))
	fatal("state %d: should be %d", state, a);
    if (state < 3 && (pos % TBLOCK))
	fatal("state %d: offset is %d", state, (int)(pos % TBLOCK));
}


int
tar_input_tar::read_data_strict(void *buffer, int maximum_length)
{
    if (fp < 0)
	::fatal("file not open (bug)");
    int result = 0;
    while (maximum_length > 0)
    {
	int nbytes = fp->read(buffer, maximum_length);
	if (nbytes == 0)
	{
    	    if (result == 0)
       		break;
    	    fatal
    	    (
       		"short read (asked %d, got %d)",
       		maximum_length + result,
       		result
    	    );
	}
	pos += nbytes;
	result += nbytes;
	maximum_length -= nbytes;
	buffer = (char *)buffer + nbytes;
    }
    return result;
}


int
tar_input_tar::read_data(void *buffer, int maximum_length)
{
    check_state(2, 3);
    state = 3;

    return read_data_strict(buffer, maximum_length);
}


void
tar_input_tar::read_data_padding()
{
    check_state(2, 3);
    state = 0;

    int n = pos % TBLOCK;
    if (n <= 0)
	return;
    char dummy[TBLOCK];
    n = TBLOCK - n;
    int nbytes = read_data_strict(dummy, n);
    if (nbytes < n)
	fatal("premature end of file (short padding read)");
}


int
tar_input_tar::read_header_longname(int size, tar_header &arg)
{
    //
    // Read the long name.
    //
    int size2 = (size + TBLOCK - 1) & -TBLOCK; 
    char *buffer = new char[size2];
    int nbytes = read_data_strict(buffer, size2);
    if (nbytes != size2)
	fatal("premature end of file (short name read)");
    while (size > 1 && buffer[size - 1] == 0)
	--size;
    rcstring long_file_name = rcstring(buffer, size);
    delete buffer;

    //
    // Read the next file header
    //
    state = 0;
    if (!read_header(arg))
	return 0;

    //
    // Replace the name with the long name we just read.
    //
    arg.name = long_file_name;

    //
    // Watch out for trailing slashes
    //
    while (arg.name[arg.name.length() - 1] == '/')
    {
	arg.name =
	    rcstring
	    (
		arg.name.to_c_string(),
		arg.name.length() - 1
	    );
	arg.type = tar_header::type_directory;
	arg.size = 0;
	arg.link_count = 2;
    }
    return 1;
}


int
tar_input_tar::read_header_longlink(int size, tar_header &arg)
{
    //
    // Read the long name.
    //
    int size2 = (size + TBLOCK - 1) & -TBLOCK; 
    char *buffer = new char[size2];
    int nbytes = read_data_strict(buffer, size2);
    if (nbytes != size2)
	fatal("premature end of file (short name read)");
    while (size > 1 && buffer[size - 1] == 0)
	--size;
    rcstring long_link_name = rcstring(buffer, size);
    delete buffer;

    //
    // Read the next file header
    //
    state = 0;
    if (!read_header(arg))
	return 0;

    //
    // Replace the name with the long name we just read.
    //
    arg.linkname = long_link_name;
    return 1;
}


int
tar_input_tar::read_header_gzipped(int size, tar_header &arg)
{
    //
    // Read the name of the uncompressed file.
    //
    int size2 = (size + TBLOCK - 1) & -TBLOCK; 
    char *buffer = new char[size2];
    int nbytes = read_data_strict(buffer, size2);
    if (nbytes != size2)
	fatal("premature end of file (short name read)");
    while (size > 1 && buffer[size - 1] == 0)
	--size;
    rcstring long_file_name = rcstring(buffer, size);
    delete buffer;

    //
    // Read the next file header
    //
    state = 0;
    if (!read_header(arg))
	return 0;

    //
    // Replace the name with the name of the uncompressed file.
    //
    if (long_file_name.length() != 0)
	arg.name = long_file_name;
    arg.type = tar_header::type_normal_gzipped;
    return 1;
}


int
tar_input_tar::read_header(tar_header &arg)
{
    check_state(0);
    state = 1;

    /*
     * read a block of input
     */
    char block[TBLOCK];
    int nbytes = read_data_strict(block, TBLOCK);
    if (!nbytes)
	return 0;
    if (nbytes != TBLOCK)
	fatal("premature end of file (short header read)");
    if (all_zero(block, TBLOCK))
	return 0;

    /*
     * Build a pointer to the on-tape header block structure.
     * Use this to asist decoding the header block.
     */
    header_ty *hp = (header_ty *)block;

    /*
     * chew over header fields
     */
    int hchksum = hp->chksum_get();
    if (hchksum < 0)
	fp->fatal("corrupted checksum field");
    int cs2 = hp->calculate_checksum();
    if (hchksum != cs2)
    {
	hp->dump();
	fp->fatal
	(
    	    "checksum does not match (calculated 0%o, file has 0%o)",
    	    cs2,
    	    hchksum
	);
    }

    arg = tar_header(); /* zero out everything */

    /*
     * Give all files a unique inode number.
     *
     * Let the writer figure out (from the linkname) if they should
     * be the same.  FIXME: use a symtab and get the inode_number
     * and link_count more correct.
     */
    static long ino;
    arg.inode_number = ++ino;

    arg.name = hp->name_get();

    int hsize = hp->size_get();
    if (hsize < 0)
	fp->fatal("%s: corrupted size field", arg.name.to_c_string());
    arg.size = hsize;

    switch (hp->linkflag_get())
    {
    case LF_OLDNORMAL:
    case LF_NORMAL:
	arg.type = tar_header::type_normal;
	/*
	 * Working out that it is a directory is
	 * interesting: you can't rely on the mode bits,
	 * and it shows as a normal file.  The clue is
	 * that the name will end with a slash.
	 */
	while (arg.name[arg.name.length() - 1] == '/')
	{
	    arg.name = rcstring(arg.name.to_c_string(), arg.name.length() - 1);
	    arg.type = tar_header::type_directory;
	    arg.size = 0;
	    arg.link_count = 2;
	}
	break;

    case LF_CONTIG:
	arg.type = tar_header::type_normal_contiguous;
	break;

    case LF_GZIPPED:
	return read_header_gzipped(hsize, arg);

    case LF_LINK:
	arg.linkname = hp->linkname_get();
	arg.type = tar_header::type_link_hard;
	arg.size = 0;
	arg.link_count = 2;
	break;

    case LF_SYMLINK:
	arg.linkname = hp->linkname_get();
	arg.type = tar_header::type_link_symbolic;
	arg.size = 0;
	break;

    case LF_CHR:
	arg.type = tar_header::type_device_character;
	arg.size = 0;
	break;

    case LF_BLK:
	arg.type = tar_header::type_device_block;
	arg.size = 0;
	break;

    case LF_DIR:
	while (arg.name[arg.name.length() - 1] == '/')
	    arg.name = rcstring(arg.name.to_c_string(), arg.name.length() - 1);
	arg.type = tar_header::type_directory;
	arg.size = 0;
	arg.link_count = 2;
	break;

    case LF_FIFO:
	arg.type = tar_header::type_fifo;
	arg.size = 0;
	break;

    case LF_LONGNAME:
	return read_header_longname(hsize, arg);

    case LF_LONGLINK:
	return read_header_longlink(hsize, arg);

    default:
	fp->fatal("file type ``%c'' unknown", hp->linkflag_get());
	break;
    }

    int hmode = hp->mode_get();
    if (hmode < 0)
	fatal("\"%s\" corrupted mode field", arg.name.to_c_string());
    arg.mode = hmode;

    int huid = hp->uid_get();
    if (huid < 0)
	fatal("\"%s\" corrupted uid field", arg.name.to_c_string());
    arg.user_id = huid;

    int hgid = hp->gid_get();
    if (hgid < 0)
	fatal("\"%s\" corrupted gid field", arg.name.to_c_string());
    arg.group_id = hgid;

    int hmtime = hp->mtime_get();
    if (hmtime < 0)
    {
	/*
	 * usually because of a file time
	 * before 1970 or after 2038
	 */
	if (errno != ERANGE)
	    fatal("\"%s\" corrupted mtime field", arg.name.to_c_string());
	warning("\"%s\" mtime field out of range", arg.name.to_c_string());

	time_t now;
	time(&now);
	hmtime = now;
    }
    arg.mtime = hmtime;

    if (0 == memcmp(hp->magic, TMAGIC, sizeof(hp->magic)))
    {
	arg.user_name = hp->uname_get();
	arg.group_name = hp->gname_get();
	arg.device_major = hp->devmajor_get();
	arg.device_minor = hp->devminor_get();
    }
    else
    {
	arg.user_name = "nobody";
	arg.group_name = "nogroup";
	arg.device_major = 0;
	arg.device_minor = 0;
    }

    /*
     * say that we found something
     */
    return 1;
}


void
tar_input_tar::read_header_padding()
{
    check_state(1);
    state = 2;
}


const char *
tar_input_tar::filename()
    const
{
    return fp->filename();
}