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
|
/*
** Copyright 1998-2003 University of Illinois Board of Trustees
** Copyright 1998-2003 Mark D. Roth
** All rights reserved.
**
** block.c - libtar code to handle tar archive header blocks
**
** Mark D. Roth <roth@uiuc.edu>
** Campus Information Technologies and Educational Services
** University of Illinois at Urbana-Champaign
*/
#include <internal.h>
#include <errno.h>
#ifdef STDC_HEADERS
# include <string.h>
# include <stdlib.h>
#endif
#define BIT_ISSET(bitmask, bit) ((bitmask) & (bit))
/* read a header block */
int
th_read_internal(TAR *t)
{
int i;
int num_zero_blocks = 0;
#ifdef DEBUG
printf("==> th_read_internal(TAR=\"%s\")\n", t->pathname);
#endif
while ((i = tar_block_read(t, &(t->th_buf))) == T_BLOCKSIZE)
{
/* two all-zero blocks mark EOF */
if (t->th_buf.name[0] == '\0')
{
num_zero_blocks++;
if (!BIT_ISSET(t->options, TAR_IGNORE_EOT)
&& num_zero_blocks >= 2)
return 0; /* EOF */
else
continue;
}
/* verify magic and version */
if (BIT_ISSET(t->options, TAR_CHECK_MAGIC)
&& strncmp(t->th_buf.magic, TMAGIC, TMAGLEN - 1) != 0)
{
#ifdef DEBUG
puts("!!! unknown magic value in tar header");
#endif
return -2;
}
if (BIT_ISSET(t->options, TAR_CHECK_VERSION)
&& strncmp(t->th_buf.version, TVERSION, TVERSLEN) != 0)
{
#ifdef DEBUG
puts("!!! unknown version value in tar header");
#endif
return -2;
}
/* check chksum */
if (!BIT_ISSET(t->options, TAR_IGNORE_CRC)
&& !th_crc_ok(t))
{
#ifdef DEBUG
puts("!!! tar header checksum error");
#endif
return -2;
}
break;
}
#ifdef DEBUG
printf("<== th_read_internal(): returning %d\n", i);
#endif
return i;
}
/* wrapper function for th_read_internal() to handle GNU extensions */
int
th_read(TAR *t)
{
int i, j;
size_t sz;
char *ptr;
#ifdef DEBUG
printf("==> th_read(t=0x%lx)\n", t);
#endif
if (t->th_buf.gnu_longname != NULL)
free(t->th_buf.gnu_longname);
if (t->th_buf.gnu_longlink != NULL)
free(t->th_buf.gnu_longlink);
memset(&(t->th_buf), 0, sizeof(struct tar_header));
i = th_read_internal(t);
if (i == 0)
return 1;
else if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
/* check for GNU long link extention */
if (TH_ISLONGLINK(t))
{
sz = th_get_size(t);
j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0);
#ifdef DEBUG
printf(" th_read(): GNU long linkname detected "
"(%ld bytes, %d blocks)\n", sz, j);
#endif
t->th_buf.gnu_longlink = (char *)malloc(j * T_BLOCKSIZE);
if (t->th_buf.gnu_longlink == NULL)
return -1;
for (ptr = t->th_buf.gnu_longlink; j > 0;
j--, ptr += T_BLOCKSIZE)
{
#ifdef DEBUG
printf(" th_read(): reading long linkname "
"(%d blocks left, ptr == %ld)\n", j, ptr);
#endif
i = tar_block_read(t, ptr);
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
#ifdef DEBUG
printf(" th_read(): read block == \"%s\"\n", ptr);
#endif
}
#ifdef DEBUG
printf(" th_read(): t->th_buf.gnu_longlink == \"%s\"\n",
t->th_buf.gnu_longlink);
#endif
i = th_read_internal(t);
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
}
/* check for GNU long name extention */
if (TH_ISLONGNAME(t))
{
sz = th_get_size(t);
j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0);
#ifdef DEBUG
printf(" th_read(): GNU long filename detected "
"(%ld bytes, %d blocks)\n", sz, j);
#endif
t->th_buf.gnu_longname = (char *)malloc(j * T_BLOCKSIZE);
if (t->th_buf.gnu_longname == NULL)
return -1;
for (ptr = t->th_buf.gnu_longname; j > 0;
j--, ptr += T_BLOCKSIZE)
{
#ifdef DEBUG
printf(" th_read(): reading long filename "
"(%d blocks left, ptr == %ld)\n", j, ptr);
#endif
i = tar_block_read(t, ptr);
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
#ifdef DEBUG
printf(" th_read(): read block == \"%s\"\n", ptr);
#endif
}
#ifdef DEBUG
printf(" th_read(): t->th_buf.gnu_longname == \"%s\"\n",
t->th_buf.gnu_longname);
#endif
i = th_read_internal(t);
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
}
#if 0
/*
** work-around for old archive files with broken typeflag fields
** NOTE: I fixed this in the TH_IS*() macros instead
*/
/*
** (directories are signified with a trailing '/')
*/
if (t->th_buf.typeflag == AREGTYPE
&& t->th_buf.name[strlen(t->th_buf.name) - 1] == '/')
t->th_buf.typeflag = DIRTYPE;
/*
** fallback to using mode bits
*/
if (t->th_buf.typeflag == AREGTYPE)
{
mode = (mode_t)oct_to_int(t->th_buf.mode);
if (S_ISREG(mode))
t->th_buf.typeflag = REGTYPE;
else if (S_ISDIR(mode))
t->th_buf.typeflag = DIRTYPE;
else if (S_ISFIFO(mode))
t->th_buf.typeflag = FIFOTYPE;
else if (S_ISCHR(mode))
t->th_buf.typeflag = CHRTYPE;
else if (S_ISBLK(mode))
t->th_buf.typeflag = BLKTYPE;
else if (S_ISLNK(mode))
t->th_buf.typeflag = SYMTYPE;
}
#endif
return 0;
}
/* write a header block */
int
th_write(TAR *t)
{
int i, j;
char type2;
size_t sz, sz2;
char *ptr;
char buf[T_BLOCKSIZE];
#ifdef DEBUG
printf("==> th_write(TAR=\"%s\")\n", t->pathname);
th_print(t);
#endif
if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL)
{
#ifdef DEBUG
printf("th_write(): using gnu_longlink (\"%s\")\n",
t->th_buf.gnu_longlink);
#endif
/* save old size and type */
type2 = t->th_buf.typeflag;
sz2 = th_get_size(t);
/* write out initial header block with fake size and type */
t->th_buf.typeflag = GNU_LONGLINK_TYPE;
sz = strlen(t->th_buf.gnu_longlink);
th_set_size(t, sz);
th_finish(t);
i = tar_block_write(t, &(t->th_buf));
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
/* write out extra blocks containing long name */
for (j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0),
ptr = t->th_buf.gnu_longlink; j > 1;
j--, ptr += T_BLOCKSIZE)
{
i = tar_block_write(t, ptr);
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
}
memset(buf, 0, T_BLOCKSIZE);
strncpy(buf, ptr, T_BLOCKSIZE);
i = tar_block_write(t, &buf);
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
/* reset type and size to original values */
t->th_buf.typeflag = type2;
th_set_size(t, sz2);
}
if ((t->options & TAR_GNU) && t->th_buf.gnu_longname != NULL)
{
#ifdef DEBUG
printf("th_write(): using gnu_longname (\"%s\")\n",
t->th_buf.gnu_longname);
#endif
/* save old size and type */
type2 = t->th_buf.typeflag;
sz2 = th_get_size(t);
/* write out initial header block with fake size and type */
t->th_buf.typeflag = GNU_LONGNAME_TYPE;
sz = strlen(t->th_buf.gnu_longname);
th_set_size(t, sz);
th_finish(t);
i = tar_block_write(t, &(t->th_buf));
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
/* write out extra blocks containing long name */
for (j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0),
ptr = t->th_buf.gnu_longname; j > 1;
j--, ptr += T_BLOCKSIZE)
{
i = tar_block_write(t, ptr);
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
}
memset(buf, 0, T_BLOCKSIZE);
strncpy(buf, ptr, T_BLOCKSIZE);
i = tar_block_write(t, &buf);
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
/* reset type and size to original values */
t->th_buf.typeflag = type2;
th_set_size(t, sz2);
}
th_finish(t);
#ifdef DEBUG
/* print tar header */
th_print(t);
#endif
i = tar_block_write(t, &(t->th_buf));
if (i != T_BLOCKSIZE)
{
if (i != -1)
errno = EINVAL;
return -1;
}
#ifdef DEBUG
puts("th_write(): returning 0");
#endif
return 0;
}
|