GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/* $OpenBSD: tar.c,v 1.59 2016/02/15 02:38:53 guenther Exp $ */ |
||
2 |
/* $NetBSD: tar.c,v 1.5 1995/03/21 09:07:49 cgd Exp $ */ |
||
3 |
|||
4 |
/*- |
||
5 |
* Copyright (c) 1992 Keith Muller. |
||
6 |
* Copyright (c) 1992, 1993 |
||
7 |
* The Regents of the University of California. All rights reserved. |
||
8 |
* |
||
9 |
* This code is derived from software contributed to Berkeley by |
||
10 |
* Keith Muller of the University of California, San Diego. |
||
11 |
* |
||
12 |
* Redistribution and use in source and binary forms, with or without |
||
13 |
* modification, are permitted provided that the following conditions |
||
14 |
* are met: |
||
15 |
* 1. Redistributions of source code must retain the above copyright |
||
16 |
* notice, this list of conditions and the following disclaimer. |
||
17 |
* 2. Redistributions in binary form must reproduce the above copyright |
||
18 |
* notice, this list of conditions and the following disclaimer in the |
||
19 |
* documentation and/or other materials provided with the distribution. |
||
20 |
* 3. Neither the name of the University nor the names of its contributors |
||
21 |
* may be used to endorse or promote products derived from this software |
||
22 |
* without specific prior written permission. |
||
23 |
* |
||
24 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
||
25 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
26 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
27 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
||
28 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
29 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
30 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
31 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
32 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
||
33 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
||
34 |
* SUCH DAMAGE. |
||
35 |
*/ |
||
36 |
|||
37 |
#include <sys/types.h> |
||
38 |
#include <sys/time.h> |
||
39 |
#include <sys/stat.h> |
||
40 |
#include <ctype.h> |
||
41 |
#include <errno.h> |
||
42 |
#include <limits.h> |
||
43 |
#include <string.h> |
||
44 |
#include <stdio.h> |
||
45 |
#include <unistd.h> |
||
46 |
#include <stdlib.h> |
||
47 |
#include "pax.h" |
||
48 |
#include "extern.h" |
||
49 |
#include "tar.h" |
||
50 |
|||
51 |
/* |
||
52 |
* Routines for reading, writing and header identify of various versions of tar |
||
53 |
*/ |
||
54 |
|||
55 |
static size_t expandname(char *, size_t, char **, const char *, size_t); |
||
56 |
static u_long tar_chksm(char *, int); |
||
57 |
static char *name_split(char *, int); |
||
58 |
static int ul_oct(u_long, char *, int, int); |
||
59 |
static int uqd_oct(u_quad_t, char *, int, int); |
||
60 |
#ifndef SMALL |
||
61 |
static int rd_xheader(ARCHD *arcn, int, off_t); |
||
62 |
#endif |
||
63 |
|||
64 |
static uid_t uid_nobody; |
||
65 |
static uid_t uid_warn; |
||
66 |
static gid_t gid_nobody; |
||
67 |
static gid_t gid_warn; |
||
68 |
|||
69 |
/* |
||
70 |
* Routines common to all versions of tar |
||
71 |
*/ |
||
72 |
|||
73 |
int tar_nodir; /* do not write dirs under old tar */ |
||
74 |
char *gnu_name_string; /* GNU ././@LongLink hackery name */ |
||
75 |
char *gnu_link_string; /* GNU ././@LongLink hackery link */ |
||
76 |
|||
77 |
/* |
||
78 |
* tar_endwr() |
||
79 |
* add the tar trailer of two null blocks |
||
80 |
* Return: |
||
81 |
* 0 if ok, -1 otherwise (what wr_skip returns) |
||
82 |
*/ |
||
83 |
|||
84 |
int |
||
85 |
tar_endwr(void) |
||
86 |
10 |
{ |
|
87 |
10 |
return(wr_skip((off_t)(NULLCNT*BLKMULT))); |
|
88 |
} |
||
89 |
|||
90 |
/* |
||
91 |
* tar_endrd() |
||
92 |
* no cleanup needed here, just return size of trailer (for append) |
||
93 |
* Return: |
||
94 |
* size of trailer (2 * BLKMULT) |
||
95 |
*/ |
||
96 |
|||
97 |
off_t |
||
98 |
tar_endrd(void) |
||
99 |
26 |
{ |
|
100 |
26 |
return((off_t)(NULLCNT*BLKMULT)); |
|
101 |
} |
||
102 |
|||
103 |
/* |
||
104 |
* tar_trail() |
||
105 |
* Called to determine if a header block is a valid trailer. We are passed |
||
106 |
* the block, the in_sync flag (which tells us we are in resync mode; |
||
107 |
* looking for a valid header), and cnt (which starts at zero) which is |
||
108 |
* used to count the number of empty blocks we have seen so far. |
||
109 |
* Return: |
||
110 |
* 0 if a valid trailer, -1 if not a valid trailer, or 1 if the block |
||
111 |
* could never contain a header. |
||
112 |
*/ |
||
113 |
|||
114 |
int |
||
115 |
tar_trail(ARCHD *ignore, char *buf, int in_resync, int *cnt) |
||
116 |
48 |
{ |
|
117 |
int i; |
||
118 |
|||
119 |
/* |
||
120 |
* look for all zero, trailer is two consecutive blocks of zero |
||
121 |
*/ |
||
122 |
✓✓ | 24624 |
for (i = 0; i < BLKMULT; ++i) { |
123 |
✗✓ | 24576 |
if (buf[i] != '\0') |
124 |
break; |
||
125 |
} |
||
126 |
|||
127 |
/* |
||
128 |
* if not all zero it is not a trailer, but MIGHT be a header. |
||
129 |
*/ |
||
130 |
✗✓ | 48 |
if (i != BLKMULT) |
131 |
return(-1); |
||
132 |
|||
133 |
/* |
||
134 |
* When given a zero block, we must be careful! |
||
135 |
* If we are not in resync mode, check for the trailer. Have to watch |
||
136 |
* out that we do not mis-identify file data as the trailer, so we do |
||
137 |
* NOT try to id a trailer during resync mode. During resync mode we |
||
138 |
* might as well throw this block out since a valid header can NEVER be |
||
139 |
* a block of all 0 (we must have a valid file name). |
||
140 |
*/ |
||
141 |
✓✗✓✓ |
48 |
if (!in_resync && (++*cnt >= NULLCNT)) |
142 |
24 |
return(0); |
|
143 |
24 |
return(1); |
|
144 |
} |
||
145 |
|||
146 |
/* |
||
147 |
* ul_oct() |
||
148 |
* convert an unsigned long to an octal string. many oddball field |
||
149 |
* termination characters are used by the various versions of tar in the |
||
150 |
* different fields. term selects which kind to use. str is '0' padded |
||
151 |
* at the front to len. we are unable to use only one format as many old |
||
152 |
* tar readers are very cranky about this. |
||
153 |
* Return: |
||
154 |
* 0 if the number fit into the string, -1 otherwise |
||
155 |
*/ |
||
156 |
|||
157 |
static int |
||
158 |
ul_oct(u_long val, char *str, int len, int term) |
||
159 |
80 |
{ |
|
160 |
char *pt; |
||
161 |
|||
162 |
/* |
||
163 |
* term selects the appropriate character(s) for the end of the string |
||
164 |
*/ |
||
165 |
80 |
pt = str + len - 1; |
|
166 |
✓✗✓✓ |
80 |
switch (term) { |
167 |
case 3: |
||
168 |
30 |
*pt-- = '\0'; |
|
169 |
30 |
break; |
|
170 |
case 2: |
||
171 |
*pt-- = ' '; |
||
172 |
*pt-- = '\0'; |
||
173 |
break; |
||
174 |
case 1: |
||
175 |
8 |
*pt-- = ' '; |
|
176 |
8 |
break; |
|
177 |
case 0: |
||
178 |
default: |
||
179 |
42 |
*pt-- = '\0'; |
|
180 |
42 |
*pt-- = ' '; |
|
181 |
break; |
||
182 |
} |
||
183 |
|||
184 |
/* |
||
185 |
* convert and blank pad if there is space |
||
186 |
*/ |
||
187 |
✓✗ | 252 |
while (pt >= str) { |
188 |
252 |
*pt-- = '0' + (char)(val & 0x7); |
|
189 |
✓✓ | 252 |
if ((val = val >> 3) == (u_long)0) |
190 |
80 |
break; |
|
191 |
} |
||
192 |
|||
193 |
✓✓ | 378 |
while (pt >= str) |
194 |
298 |
*pt-- = '0'; |
|
195 |
✗✓ | 80 |
if (val != (u_long)0) |
196 |
return(-1); |
||
197 |
80 |
return(0); |
|
198 |
} |
||
199 |
|||
200 |
/* |
||
201 |
* uqd_oct() |
||
202 |
* convert an u_quad_t to an octal string. one of many oddball field |
||
203 |
* termination characters are used by the various versions of tar in the |
||
204 |
* different fields. term selects which kind to use. str is '0' padded |
||
205 |
* at the front to len. we are unable to use only one format as many old |
||
206 |
* tar readers are very cranky about this. |
||
207 |
* Return: |
||
208 |
* 0 if the number fit into the string, -1 otherwise |
||
209 |
*/ |
||
210 |
|||
211 |
static int |
||
212 |
uqd_oct(u_quad_t val, char *str, int len, int term) |
||
213 |
28 |
{ |
|
214 |
char *pt; |
||
215 |
|||
216 |
/* |
||
217 |
* term selects the appropriate character(s) for the end of the string |
||
218 |
*/ |
||
219 |
28 |
pt = str + len - 1; |
|
220 |
✓✗✓✗ |
28 |
switch (term) { |
221 |
case 3: |
||
222 |
8 |
*pt-- = '\0'; |
|
223 |
8 |
break; |
|
224 |
case 2: |
||
225 |
*pt-- = ' '; |
||
226 |
*pt-- = '\0'; |
||
227 |
break; |
||
228 |
case 1: |
||
229 |
20 |
*pt-- = ' '; |
|
230 |
20 |
break; |
|
231 |
case 0: |
||
232 |
default: |
||
233 |
*pt-- = '\0'; |
||
234 |
*pt-- = ' '; |
||
235 |
break; |
||
236 |
} |
||
237 |
|||
238 |
/* |
||
239 |
* convert and blank pad if there is space |
||
240 |
*/ |
||
241 |
✓✗ | 208 |
while (pt >= str) { |
242 |
208 |
*pt-- = '0' + (char)(val & 0x7); |
|
243 |
✓✓ | 208 |
if ((val = val >> 3) == 0) |
244 |
28 |
break; |
|
245 |
} |
||
246 |
|||
247 |
✓✓ | 128 |
while (pt >= str) |
248 |
100 |
*pt-- = '0'; |
|
249 |
✗✓ | 28 |
if (val != (u_quad_t)0) |
250 |
return(-1); |
||
251 |
28 |
return(0); |
|
252 |
} |
||
253 |
|||
254 |
/* |
||
255 |
* tar_chksm() |
||
256 |
* calculate the checksum for a tar block counting the checksum field as |
||
257 |
* all blanks (BLNKSUM is that value pre-calculated, the sum of 8 blanks). |
||
258 |
* NOTE: we use len to short circuit summing 0's on write since we ALWAYS |
||
259 |
* pad headers with 0. |
||
260 |
* Return: |
||
261 |
* unsigned long checksum |
||
262 |
*/ |
||
263 |
|||
264 |
static u_long |
||
265 |
tar_chksm(char *blk, int len) |
||
266 |
164 |
{ |
|
267 |
char *stop; |
||
268 |
char *pt; |
||
269 |
164 |
u_long chksm = BLNKSUM; /* initial value is checksum field sum */ |
|
270 |
|||
271 |
/* |
||
272 |
* add the part of the block before the checksum field |
||
273 |
*/ |
||
274 |
164 |
pt = blk; |
|
275 |
164 |
stop = blk + CHK_OFFSET; |
|
276 |
✓✓ | 24600 |
while (pt < stop) |
277 |
24272 |
chksm += (u_long)(*pt++ & 0xff); |
|
278 |
/* |
||
279 |
* move past the checksum field and keep going, spec counts the |
||
280 |
* checksum field as the sum of 8 blanks (which is pre-computed as |
||
281 |
* BLNKSUM). |
||
282 |
* ASSUMED: len is greater than CHK_OFFSET. (len is where our 0 padding |
||
283 |
* starts, no point in summing zero's) |
||
284 |
*/ |
||
285 |
164 |
pt += CHK_LEN; |
|
286 |
164 |
stop = blk + len; |
|
287 |
✓✓ | 55094 |
while (pt < stop) |
288 |
54766 |
chksm += (u_long)(*pt++ & 0xff); |
|
289 |
164 |
return(chksm); |
|
290 |
} |
||
291 |
|||
292 |
/* |
||
293 |
* Routines for old BSD style tar (also made portable to sysV tar) |
||
294 |
*/ |
||
295 |
|||
296 |
/* |
||
297 |
* tar_id() |
||
298 |
* determine if a block given to us is a valid tar header (and not a USTAR |
||
299 |
* header). We have to be on the lookout for those pesky blocks of all |
||
300 |
* zero's. |
||
301 |
* Return: |
||
302 |
* 0 if a tar header, -1 otherwise |
||
303 |
*/ |
||
304 |
|||
305 |
int |
||
306 |
tar_id(char *blk, int size) |
||
307 |
40 |
{ |
|
308 |
HD_TAR *hd; |
||
309 |
HD_USTAR *uhd; |
||
310 |
|||
311 |
✗✓ | 40 |
if (size < BLKMULT) |
312 |
return(-1); |
||
313 |
40 |
hd = (HD_TAR *)blk; |
|
314 |
40 |
uhd = (HD_USTAR *)blk; |
|
315 |
|||
316 |
/* |
||
317 |
* check for block of zero's first, a simple and fast test, then make |
||
318 |
* sure this is not a ustar header by looking for the ustar magic |
||
319 |
* cookie. We should use TMAGLEN, but some USTAR archive programs are |
||
320 |
* wrong and create archives missing the \0. Last we check the |
||
321 |
* checksum. If this is ok we have to assume it is a valid header. |
||
322 |
*/ |
||
323 |
✓✓ | 40 |
if (hd->name[0] == '\0') |
324 |
8 |
return(-1); |
|
325 |
✗✓ | 32 |
if (strncmp(uhd->magic, TMAGIC, TMAGLEN - 1) == 0) |
326 |
return(-1); |
||
327 |
✗✓ | 32 |
if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT)) |
328 |
return(-1); |
||
329 |
32 |
force_one_volume = 1; |
|
330 |
32 |
return(0); |
|
331 |
} |
||
332 |
|||
333 |
/* |
||
334 |
* tar_opt() |
||
335 |
* handle tar format specific -o options |
||
336 |
* Return: |
||
337 |
* 0 if ok -1 otherwise |
||
338 |
*/ |
||
339 |
|||
340 |
int |
||
341 |
tar_opt(void) |
||
342 |
30 |
{ |
|
343 |
OPLIST *opt; |
||
344 |
|||
345 |
✗✓ | 60 |
while ((opt = opt_next()) != NULL) { |
346 |
if (strcmp(opt->name, TAR_OPTION) || |
||
347 |
strcmp(opt->value, TAR_NODIR)) { |
||
348 |
paxwarn(1, "Unknown tar format -o option/value pair %s=%s", |
||
349 |
opt->name, opt->value); |
||
350 |
paxwarn(1,"%s=%s is the only supported tar format option", |
||
351 |
TAR_OPTION, TAR_NODIR); |
||
352 |
return(-1); |
||
353 |
} |
||
354 |
|||
355 |
/* |
||
356 |
* we only support one option, and only when writing |
||
357 |
*/ |
||
358 |
if ((act != APPND) && (act != ARCHIVE)) { |
||
359 |
paxwarn(1, "%s=%s is only supported when writing.", |
||
360 |
opt->name, opt->value); |
||
361 |
return(-1); |
||
362 |
} |
||
363 |
tar_nodir = 1; |
||
364 |
} |
||
365 |
30 |
return(0); |
|
366 |
} |
||
367 |
|||
368 |
|||
369 |
/* |
||
370 |
* tar_rd() |
||
371 |
* extract the values out of block already determined to be a tar header. |
||
372 |
* store the values in the ARCHD parameter. |
||
373 |
* Return: |
||
374 |
* 0 |
||
375 |
*/ |
||
376 |
|||
377 |
int |
||
378 |
tar_rd(ARCHD *arcn, char *buf) |
||
379 |
36 |
{ |
|
380 |
HD_TAR *hd; |
||
381 |
u_quad_t val; |
||
382 |
char *pt; |
||
383 |
|||
384 |
/* |
||
385 |
* we only get proper sized buffers passed to us |
||
386 |
*/ |
||
387 |
✓✓ | 36 |
if (tar_id(buf, BLKMULT) < 0) |
388 |
8 |
return(-1); |
|
389 |
28 |
memset(arcn, 0, sizeof(*arcn)); |
|
390 |
28 |
arcn->org_name = arcn->name; |
|
391 |
28 |
arcn->sb.st_nlink = 1; |
|
392 |
|||
393 |
/* |
||
394 |
* copy out the name and values in the stat buffer |
||
395 |
*/ |
||
396 |
28 |
hd = (HD_TAR *)buf; |
|
397 |
✓✗ | 28 |
if (hd->linkflag != LONGLINKTYPE && hd->linkflag != LONGNAMETYPE) { |
398 |
28 |
arcn->nlen = expandname(arcn->name, sizeof(arcn->name), |
|
399 |
&gnu_name_string, hd->name, sizeof(hd->name)); |
||
400 |
28 |
arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name), |
|
401 |
&gnu_link_string, hd->linkname, sizeof(hd->linkname)); |
||
402 |
} |
||
403 |
28 |
arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode,sizeof(hd->mode),OCT) & |
|
404 |
0xfff); |
||
405 |
28 |
arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT); |
|
406 |
28 |
arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT); |
|
407 |
28 |
arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT); |
|
408 |
28 |
val = asc_uqd(hd->mtime, sizeof(hd->mtime), OCT); |
|
409 |
✗✓ | 28 |
if ((time_t)val < 0 || (time_t)val != val) |
410 |
arcn->sb.st_mtime = INT_MAX; /* XXX 2038 */ |
||
411 |
else |
||
412 |
28 |
arcn->sb.st_mtime = val; |
|
413 |
28 |
arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; |
|
414 |
|||
415 |
/* |
||
416 |
* have to look at the last character, it may be a '/' and that is used |
||
417 |
* to encode this as a directory |
||
418 |
*/ |
||
419 |
28 |
pt = &(arcn->name[arcn->nlen - 1]); |
|
420 |
28 |
arcn->pad = 0; |
|
421 |
28 |
arcn->skip = 0; |
|
422 |
✗✓✗✓ ✓ |
28 |
switch (hd->linkflag) { |
423 |
case SYMTYPE: |
||
424 |
/* |
||
425 |
* symbolic link, need to get the link name and set the type in |
||
426 |
* the st_mode so -v printing will look correct. |
||
427 |
*/ |
||
428 |
arcn->type = PAX_SLK; |
||
429 |
arcn->sb.st_mode |= S_IFLNK; |
||
430 |
break; |
||
431 |
case LNKTYPE: |
||
432 |
/* |
||
433 |
* hard link, need to get the link name, set the type in the |
||
434 |
* st_mode and st_nlink so -v printing will look better. |
||
435 |
*/ |
||
436 |
8 |
arcn->type = PAX_HLK; |
|
437 |
8 |
arcn->sb.st_nlink = 2; |
|
438 |
|||
439 |
/* |
||
440 |
* no idea of what type this thing really points at, but |
||
441 |
* we set something for printing only. |
||
442 |
*/ |
||
443 |
8 |
arcn->sb.st_mode |= S_IFREG; |
|
444 |
8 |
break; |
|
445 |
case LONGLINKTYPE: |
||
446 |
case LONGNAMETYPE: |
||
447 |
/* |
||
448 |
* GNU long link/file; we tag these here and let the |
||
449 |
* pax internals deal with it -- too ugly otherwise. |
||
450 |
*/ |
||
451 |
arcn->type = |
||
452 |
hd->linkflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF; |
||
453 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
||
454 |
arcn->skip = arcn->sb.st_size; |
||
455 |
break; |
||
456 |
case DIRTYPE: |
||
457 |
/* |
||
458 |
* It is a directory, set the mode for -v printing |
||
459 |
*/ |
||
460 |
4 |
arcn->type = PAX_DIR; |
|
461 |
4 |
arcn->sb.st_mode |= S_IFDIR; |
|
462 |
4 |
arcn->sb.st_nlink = 2; |
|
463 |
4 |
break; |
|
464 |
case AREGTYPE: |
||
465 |
case REGTYPE: |
||
466 |
default: |
||
467 |
/* |
||
468 |
* If we have a trailing / this is a directory and NOT a file. |
||
469 |
*/ |
||
470 |
16 |
arcn->ln_name[0] = '\0'; |
|
471 |
16 |
arcn->ln_nlen = 0; |
|
472 |
✓✓ | 16 |
if (*pt == '/') { |
473 |
/* |
||
474 |
* it is a directory, set the mode for -v printing |
||
475 |
*/ |
||
476 |
4 |
arcn->type = PAX_DIR; |
|
477 |
4 |
arcn->sb.st_mode |= S_IFDIR; |
|
478 |
4 |
arcn->sb.st_nlink = 2; |
|
479 |
} else { |
||
480 |
/* |
||
481 |
* have a file that will be followed by data. Set the |
||
482 |
* skip value to the size field and calculate the size |
||
483 |
* of the padding. |
||
484 |
*/ |
||
485 |
12 |
arcn->type = PAX_REG; |
|
486 |
12 |
arcn->sb.st_mode |= S_IFREG; |
|
487 |
12 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
|
488 |
12 |
arcn->skip = arcn->sb.st_size; |
|
489 |
} |
||
490 |
break; |
||
491 |
} |
||
492 |
|||
493 |
/* |
||
494 |
* strip off any trailing slash. |
||
495 |
*/ |
||
496 |
✓✓ | 28 |
if (*pt == '/') { |
497 |
8 |
*pt = '\0'; |
|
498 |
8 |
--arcn->nlen; |
|
499 |
} |
||
500 |
28 |
return(0); |
|
501 |
} |
||
502 |
|||
503 |
/* |
||
504 |
* tar_wr() |
||
505 |
* write a tar header for the file specified in the ARCHD to the archive. |
||
506 |
* Have to check for file types that cannot be stored and file names that |
||
507 |
* are too long. Be careful of the term (last arg) to ul_oct, each field |
||
508 |
* of tar has it own spec for the termination character(s). |
||
509 |
* ASSUMED: space after header in header block is zero filled |
||
510 |
* Return: |
||
511 |
* 0 if file has data to be written after the header, 1 if file has NO |
||
512 |
* data to write after the header, -1 if archive write failed |
||
513 |
*/ |
||
514 |
|||
515 |
int |
||
516 |
tar_wr(ARCHD *arcn) |
||
517 |
14 |
{ |
|
518 |
HD_TAR *hd; |
||
519 |
int len; |
||
520 |
char hdblk[sizeof(HD_TAR)]; |
||
521 |
|||
522 |
/* |
||
523 |
* check for those file system types which tar cannot store |
||
524 |
*/ |
||
525 |
✓✗✗✗ ✗✓✓ |
14 |
switch (arcn->type) { |
526 |
case PAX_DIR: |
||
527 |
/* |
||
528 |
* user asked that dirs not be written to the archive |
||
529 |
*/ |
||
530 |
✗✓ | 4 |
if (tar_nodir) |
531 |
return(1); |
||
532 |
break; |
||
533 |
case PAX_CHR: |
||
534 |
paxwarn(1, "Tar cannot archive a character device %s", |
||
535 |
arcn->org_name); |
||
536 |
return(1); |
||
537 |
case PAX_BLK: |
||
538 |
paxwarn(1, "Tar cannot archive a block device %s", arcn->org_name); |
||
539 |
return(1); |
||
540 |
case PAX_SCK: |
||
541 |
paxwarn(1, "Tar cannot archive a socket %s", arcn->org_name); |
||
542 |
return(1); |
||
543 |
case PAX_FIF: |
||
544 |
paxwarn(1, "Tar cannot archive a fifo %s", arcn->org_name); |
||
545 |
return(1); |
||
546 |
case PAX_SLK: |
||
547 |
case PAX_HLK: |
||
548 |
case PAX_HRG: |
||
549 |
✗✓ | 4 |
if (arcn->ln_nlen > sizeof(hd->linkname)) { |
550 |
paxwarn(1, "Link name too long for tar %s", |
||
551 |
arcn->ln_name); |
||
552 |
return(1); |
||
553 |
} |
||
554 |
break; |
||
555 |
case PAX_REG: |
||
556 |
case PAX_CTG: |
||
557 |
default: |
||
558 |
break; |
||
559 |
} |
||
560 |
|||
561 |
/* |
||
562 |
* check file name len, remember extra char for dirs (the / at the end) |
||
563 |
*/ |
||
564 |
14 |
len = arcn->nlen; |
|
565 |
✓✓ | 14 |
if (arcn->type == PAX_DIR) |
566 |
4 |
++len; |
|
567 |
✗✓ | 14 |
if (len > sizeof(hd->name)) { |
568 |
paxwarn(1, "File name too long for tar %s", arcn->name); |
||
569 |
return(1); |
||
570 |
} |
||
571 |
|||
572 |
/* |
||
573 |
* Copy the data out of the ARCHD into the tar header based on the type |
||
574 |
* of the file. Remember, many tar readers want all fields to be |
||
575 |
* padded with zero so we zero the header first. We then set the |
||
576 |
* linkflag field (type), the linkname, the size, and set the padding |
||
577 |
* (if any) to be added after the file data (0 for all other types, |
||
578 |
* as they only have a header). |
||
579 |
*/ |
||
580 |
14 |
memset(hdblk, 0, sizeof(hdblk)); |
|
581 |
14 |
hd = (HD_TAR *)hdblk; |
|
582 |
14 |
fieldcpy(hd->name, sizeof(hd->name), arcn->name, sizeof(arcn->name)); |
|
583 |
14 |
arcn->pad = 0; |
|
584 |
|||
585 |
✓✓ | 14 |
if (arcn->type == PAX_DIR) { |
586 |
/* |
||
587 |
* directories are the same as files, except have a filename |
||
588 |
* that ends with a /, we add the slash here. No data follows |
||
589 |
* dirs, so no pad. |
||
590 |
*/ |
||
591 |
4 |
hd->linkflag = AREGTYPE; |
|
592 |
4 |
hd->name[len-1] = '/'; |
|
593 |
✓✗ | 4 |
if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1)) |
594 |
goto out; |
||
595 |
✗✓ | 10 |
} else if (arcn->type == PAX_SLK) { |
596 |
/* |
||
597 |
* no data follows this file, so no pad |
||
598 |
*/ |
||
599 |
hd->linkflag = SYMTYPE; |
||
600 |
fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name, |
||
601 |
sizeof(arcn->ln_name)); |
||
602 |
if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1)) |
||
603 |
goto out; |
||
604 |
✓✓ | 10 |
} else if (PAX_IS_HARDLINK(arcn->type)) { |
605 |
/* |
||
606 |
* no data follows this file, so no pad |
||
607 |
*/ |
||
608 |
4 |
hd->linkflag = LNKTYPE; |
|
609 |
4 |
fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name, |
|
610 |
sizeof(arcn->ln_name)); |
||
611 |
✗✓ | 4 |
if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 1)) |
612 |
goto out; |
||
613 |
} else { |
||
614 |
/* |
||
615 |
* data follows this file, so set the pad |
||
616 |
*/ |
||
617 |
6 |
hd->linkflag = AREGTYPE; |
|
618 |
✗✓ | 6 |
if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size, |
619 |
sizeof(hd->size), 1)) { |
||
620 |
paxwarn(1,"File is too large for tar %s", arcn->org_name); |
||
621 |
return(1); |
||
622 |
} |
||
623 |
6 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
|
624 |
} |
||
625 |
|||
626 |
/* |
||
627 |
* copy those fields that are independent of the type |
||
628 |
*/ |
||
629 |
✓✗✓✗ ✓✗✓✗ |
14 |
if (ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 0) || |
630 |
uqd_oct(arcn->sb.st_mtime < 0 ? 0 : arcn->sb.st_mtime, hd->mtime, |
||
631 |
sizeof(hd->mtime), 1) || |
||
632 |
ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 0) || |
||
633 |
ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 0)) |
||
634 |
goto out; |
||
635 |
|||
636 |
/* |
||
637 |
* calculate and add the checksum, then write the header. A return of |
||
638 |
* 0 tells the caller to now write the file data, 1 says no data needs |
||
639 |
* to be written |
||
640 |
*/ |
||
641 |
✗✓ | 14 |
if (ul_oct(tar_chksm(hdblk, sizeof(HD_TAR)), hd->chksum, |
642 |
sizeof(hd->chksum), 3)) |
||
643 |
goto out; |
||
644 |
✗✓ | 14 |
if (wr_rdbuf(hdblk, sizeof(HD_TAR)) < 0) |
645 |
return(-1); |
||
646 |
✗✓ | 14 |
if (wr_skip((off_t)(BLKMULT - sizeof(HD_TAR))) < 0) |
647 |
return(-1); |
||
648 |
✓✓ | 14 |
if (PAX_IS_REG(arcn->type)) |
649 |
6 |
return(0); |
|
650 |
8 |
return(1); |
|
651 |
|||
652 |
out: |
||
653 |
/* |
||
654 |
* header field is out of range |
||
655 |
*/ |
||
656 |
paxwarn(1, "Tar header field is too small for %s", arcn->org_name); |
||
657 |
return(1); |
||
658 |
} |
||
659 |
|||
660 |
/* |
||
661 |
* Routines for POSIX ustar |
||
662 |
*/ |
||
663 |
|||
664 |
/* |
||
665 |
* ustar_strd() |
||
666 |
* initialization for ustar read |
||
667 |
* Return: |
||
668 |
* 0 if ok, -1 otherwise |
||
669 |
*/ |
||
670 |
|||
671 |
int |
||
672 |
ustar_strd(void) |
||
673 |
22 |
{ |
|
674 |
✓✗✗✓ |
22 |
if ((usrtb_start() < 0) || (grptb_start() < 0)) |
675 |
return(-1); |
||
676 |
22 |
return(0); |
|
677 |
} |
||
678 |
|||
679 |
/* |
||
680 |
* ustar_stwr() |
||
681 |
* initialization for ustar write |
||
682 |
* Return: |
||
683 |
* 0 if ok, -1 otherwise |
||
684 |
*/ |
||
685 |
|||
686 |
int |
||
687 |
ustar_stwr(void) |
||
688 |
6 |
{ |
|
689 |
✓✗✗✓ |
6 |
if ((uidtb_start() < 0) || (gidtb_start() < 0)) |
690 |
return(-1); |
||
691 |
6 |
return(0); |
|
692 |
} |
||
693 |
|||
694 |
/* |
||
695 |
* ustar_id() |
||
696 |
* determine if a block given to us is a valid ustar header. We have to |
||
697 |
* be on the lookout for those pesky blocks of all zero's |
||
698 |
* Return: |
||
699 |
* 0 if a ustar header, -1 otherwise |
||
700 |
*/ |
||
701 |
|||
702 |
int |
||
703 |
ustar_id(char *blk, int size) |
||
704 |
158 |
{ |
|
705 |
HD_USTAR *hd; |
||
706 |
|||
707 |
✗✓ | 158 |
if (size < BLKMULT) |
708 |
return(-1); |
||
709 |
158 |
hd = (HD_USTAR *)blk; |
|
710 |
|||
711 |
/* |
||
712 |
* check for block of zero's first, a simple and fast test then check |
||
713 |
* ustar magic cookie. We should use TMAGLEN, but some USTAR archive |
||
714 |
* programs are fouled up and create archives missing the \0. Last we |
||
715 |
* check the checksum. If ok we have to assume it is a valid header. |
||
716 |
*/ |
||
717 |
✓✓✓✓ |
158 |
if (hd->prefix[0] == '\0' && hd->name[0] == '\0') |
718 |
40 |
return(-1); |
|
719 |
✓✓ | 118 |
if (strncmp(hd->magic, TMAGIC, TMAGLEN - 1) != 0) |
720 |
4 |
return(-1); |
|
721 |
✗✓ | 114 |
if (asc_ul(hd->chksum,sizeof(hd->chksum),OCT) != tar_chksm(blk,BLKMULT)) |
722 |
return(-1); |
||
723 |
114 |
return(0); |
|
724 |
} |
||
725 |
|||
726 |
/* |
||
727 |
* ustar_rd() |
||
728 |
* extract the values out of block already determined to be a ustar header. |
||
729 |
* store the values in the ARCHD parameter. |
||
730 |
* Return: |
||
731 |
* 0 |
||
732 |
*/ |
||
733 |
|||
734 |
int |
||
735 |
ustar_rd(ARCHD *arcn, char *buf) |
||
736 |
132 |
{ |
|
737 |
132 |
HD_USTAR *hd = (HD_USTAR *)buf; |
|
738 |
char *dest; |
||
739 |
132 |
int cnt = 0; |
|
740 |
dev_t devmajor; |
||
741 |
dev_t devminor; |
||
742 |
u_quad_t val; |
||
743 |
|||
744 |
/* |
||
745 |
* we only get proper sized buffers |
||
746 |
*/ |
||
747 |
✓✓ | 132 |
if (ustar_id(buf, BLKMULT) < 0) |
748 |
40 |
return(-1); |
|
749 |
|||
750 |
#ifndef SMALL |
||
751 |
92 |
reset: |
|
752 |
#endif |
||
753 |
92 |
memset(arcn, 0, sizeof(*arcn)); |
|
754 |
92 |
arcn->org_name = arcn->name; |
|
755 |
92 |
arcn->sb.st_nlink = 1; |
|
756 |
|||
757 |
#ifndef SMALL |
||
758 |
/* Process Extended headers. */ |
||
759 |
✗✓ | 92 |
if (hd->typeflag == XHDRTYPE || hd->typeflag == GHDRTYPE) { |
760 |
if (rd_xheader(arcn, hd->typeflag == GHDRTYPE, |
||
761 |
(off_t)asc_ul(hd->size, sizeof(hd->size), OCT)) < 0) |
||
762 |
return (-1); |
||
763 |
|||
764 |
/* Update and check the ustar header. */ |
||
765 |
if (rd_wrbuf(buf, BLKMULT) != BLKMULT) |
||
766 |
return (-1); |
||
767 |
if (ustar_id(buf, BLKMULT) < 0) |
||
768 |
return(-1); |
||
769 |
|||
770 |
/* if the next block is another extension, reset the values */ |
||
771 |
if (hd->typeflag == XHDRTYPE || hd->typeflag == GHDRTYPE) |
||
772 |
goto reset; |
||
773 |
} |
||
774 |
#endif |
||
775 |
|||
776 |
✓✗ | 92 |
if (!arcn->nlen) { |
777 |
/* |
||
778 |
* See if the filename is split into two parts. if, so join |
||
779 |
* the parts. We copy the prefix first and add a / between |
||
780 |
* the prefix and name. |
||
781 |
*/ |
||
782 |
92 |
dest = arcn->name; |
|
783 |
✓✓ | 92 |
if (*(hd->prefix) != '\0') { |
784 |
60 |
cnt = fieldcpy(dest, sizeof(arcn->name) - 1, |
|
785 |
hd->prefix, sizeof(hd->prefix)); |
||
786 |
60 |
dest += cnt; |
|
787 |
60 |
*dest++ = '/'; |
|
788 |
60 |
cnt++; |
|
789 |
} else |
||
790 |
32 |
cnt = 0; |
|
791 |
|||
792 |
✓✗ | 92 |
if (hd->typeflag != LONGLINKTYPE && |
793 |
hd->typeflag != LONGNAMETYPE) { |
||
794 |
92 |
arcn->nlen = cnt + expandname(dest, |
|
795 |
sizeof(arcn->name) - cnt, &gnu_name_string, |
||
796 |
hd->name, sizeof(hd->name)); |
||
797 |
} |
||
798 |
} |
||
799 |
|||
800 |
✓✗✓✗ ✓✗ |
92 |
if (!arcn->ln_nlen && |
801 |
hd->typeflag != LONGLINKTYPE && hd->typeflag != LONGNAMETYPE) { |
||
802 |
92 |
arcn->ln_nlen = expandname(arcn->ln_name, sizeof(arcn->ln_name), |
|
803 |
&gnu_link_string, hd->linkname, sizeof(hd->linkname)); |
||
804 |
} |
||
805 |
|||
806 |
/* |
||
807 |
* follow the spec to the letter. we should only have mode bits, strip |
||
808 |
* off all other crud we may be passed. |
||
809 |
*/ |
||
810 |
92 |
arcn->sb.st_mode = (mode_t)(asc_ul(hd->mode, sizeof(hd->mode), OCT) & |
|
811 |
0xfff); |
||
812 |
92 |
arcn->sb.st_size = (off_t)asc_uqd(hd->size, sizeof(hd->size), OCT); |
|
813 |
92 |
val = asc_uqd(hd->mtime, sizeof(hd->mtime), OCT); |
|
814 |
✗✓ | 92 |
if ((time_t)val < 0 || (time_t)val != val) |
815 |
arcn->sb.st_mtime = INT_MAX; /* XXX 2038 */ |
||
816 |
else |
||
817 |
92 |
arcn->sb.st_mtime = val; |
|
818 |
92 |
arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime; |
|
819 |
|||
820 |
/* |
||
821 |
* If we can find the ascii names for gname and uname in the password |
||
822 |
* and group files we will use the uid's and gid they bind. Otherwise |
||
823 |
* we use the uid and gid values stored in the header. (This is what |
||
824 |
* the posix spec wants). |
||
825 |
*/ |
||
826 |
92 |
hd->gname[sizeof(hd->gname) - 1] = '\0'; |
|
827 |
✓✗✓✓ |
92 |
if (Nflag || gid_name(hd->gname, &(arcn->sb.st_gid)) < 0) |
828 |
72 |
arcn->sb.st_gid = (gid_t)asc_ul(hd->gid, sizeof(hd->gid), OCT); |
|
829 |
92 |
hd->uname[sizeof(hd->uname) - 1] = '\0'; |
|
830 |
✓✗✓✓ |
92 |
if (Nflag || uid_name(hd->uname, &(arcn->sb.st_uid)) < 0) |
831 |
72 |
arcn->sb.st_uid = (uid_t)asc_ul(hd->uid, sizeof(hd->uid), OCT); |
|
832 |
|||
833 |
/* |
||
834 |
* set the defaults, these may be changed depending on the file type |
||
835 |
*/ |
||
836 |
92 |
arcn->pad = 0; |
|
837 |
92 |
arcn->skip = 0; |
|
838 |
92 |
arcn->sb.st_rdev = (dev_t)0; |
|
839 |
|||
840 |
/* |
||
841 |
* set the mode and PAX type according to the typeflag in the header |
||
842 |
*/ |
||
843 |
✗✓✗✗ ✗✓ |
92 |
switch (hd->typeflag) { |
844 |
case FIFOTYPE: |
||
845 |
arcn->type = PAX_FIF; |
||
846 |
arcn->sb.st_mode |= S_IFIFO; |
||
847 |
break; |
||
848 |
case DIRTYPE: |
||
849 |
20 |
arcn->type = PAX_DIR; |
|
850 |
20 |
arcn->sb.st_mode |= S_IFDIR; |
|
851 |
20 |
arcn->sb.st_nlink = 2; |
|
852 |
|||
853 |
/* |
||
854 |
* Some programs that create ustar archives append a '/' |
||
855 |
* to the pathname for directories. This clearly violates |
||
856 |
* ustar specs, but we will silently strip it off anyway. |
||
857 |
*/ |
||
858 |
✓✓ | 20 |
if (arcn->name[arcn->nlen - 1] == '/') |
859 |
16 |
arcn->name[--arcn->nlen] = '\0'; |
|
860 |
break; |
||
861 |
case BLKTYPE: |
||
862 |
case CHRTYPE: |
||
863 |
/* |
||
864 |
* this type requires the rdev field to be set. |
||
865 |
*/ |
||
866 |
if (hd->typeflag == BLKTYPE) { |
||
867 |
arcn->type = PAX_BLK; |
||
868 |
arcn->sb.st_mode |= S_IFBLK; |
||
869 |
} else { |
||
870 |
arcn->type = PAX_CHR; |
||
871 |
arcn->sb.st_mode |= S_IFCHR; |
||
872 |
} |
||
873 |
devmajor = (dev_t)asc_ul(hd->devmajor,sizeof(hd->devmajor),OCT); |
||
874 |
devminor = (dev_t)asc_ul(hd->devminor,sizeof(hd->devminor),OCT); |
||
875 |
arcn->sb.st_rdev = TODEV(devmajor, devminor); |
||
876 |
break; |
||
877 |
case SYMTYPE: |
||
878 |
case LNKTYPE: |
||
879 |
if (hd->typeflag == SYMTYPE) { |
||
880 |
arcn->type = PAX_SLK; |
||
881 |
arcn->sb.st_mode |= S_IFLNK; |
||
882 |
} else { |
||
883 |
arcn->type = PAX_HLK; |
||
884 |
/* |
||
885 |
* so printing looks better |
||
886 |
*/ |
||
887 |
arcn->sb.st_mode |= S_IFREG; |
||
888 |
arcn->sb.st_nlink = 2; |
||
889 |
} |
||
890 |
break; |
||
891 |
case LONGLINKTYPE: |
||
892 |
case LONGNAMETYPE: |
||
893 |
/* |
||
894 |
* GNU long link/file; we tag these here and let the |
||
895 |
* pax internals deal with it -- too ugly otherwise. |
||
896 |
*/ |
||
897 |
arcn->type = |
||
898 |
hd->typeflag == LONGLINKTYPE ? PAX_GLL : PAX_GLF; |
||
899 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
||
900 |
arcn->skip = arcn->sb.st_size; |
||
901 |
break; |
||
902 |
case CONTTYPE: |
||
903 |
case AREGTYPE: |
||
904 |
case REGTYPE: |
||
905 |
default: |
||
906 |
/* |
||
907 |
* these types have file data that follows. Set the skip and |
||
908 |
* pad fields. |
||
909 |
*/ |
||
910 |
72 |
arcn->type = PAX_REG; |
|
911 |
72 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
|
912 |
72 |
arcn->skip = arcn->sb.st_size; |
|
913 |
72 |
arcn->sb.st_mode |= S_IFREG; |
|
914 |
break; |
||
915 |
} |
||
916 |
92 |
return(0); |
|
917 |
} |
||
918 |
|||
919 |
/* |
||
920 |
* ustar_wr() |
||
921 |
* write a ustar header for the file specified in the ARCHD to the archive |
||
922 |
* Have to check for file types that cannot be stored and file names that |
||
923 |
* are too long. Be careful of the term (last arg) to ul_oct, we only use |
||
924 |
* '\0' for the termination character (this is different than picky tar) |
||
925 |
* ASSUMED: space after header in header block is zero filled |
||
926 |
* Return: |
||
927 |
* 0 if file has data to be written after the header, 1 if file has NO |
||
928 |
* data to write after the header, -1 if archive write failed |
||
929 |
*/ |
||
930 |
|||
931 |
int |
||
932 |
ustar_wr(ARCHD *arcn) |
||
933 |
4 |
{ |
|
934 |
HD_USTAR *hd; |
||
935 |
char *pt; |
||
936 |
char hdblk[sizeof(HD_USTAR)]; |
||
937 |
|||
938 |
/* |
||
939 |
* check for those file system types ustar cannot store |
||
940 |
*/ |
||
941 |
✗✓ | 4 |
if (arcn->type == PAX_SCK) { |
942 |
paxwarn(1, "Ustar cannot archive a socket %s", arcn->org_name); |
||
943 |
return(1); |
||
944 |
} |
||
945 |
|||
946 |
/* |
||
947 |
* user asked that dirs not be written to the archive |
||
948 |
*/ |
||
949 |
✗✓✗✗ |
4 |
if (arcn->type == PAX_DIR && tar_nodir) |
950 |
return (1); |
||
951 |
|||
952 |
/* |
||
953 |
* check the length of the linkname |
||
954 |
*/ |
||
955 |
✗✓✗✗ |
4 |
if (PAX_IS_LINK(arcn->type) && (arcn->ln_nlen > sizeof(hd->linkname))) { |
956 |
paxwarn(1, "Link name too long for ustar %s", arcn->ln_name); |
||
957 |
return(1); |
||
958 |
} |
||
959 |
|||
960 |
/* |
||
961 |
* split the path name into prefix and name fields (if needed). if |
||
962 |
* pt != arcn->name, the name has to be split |
||
963 |
*/ |
||
964 |
✗✓ | 4 |
if ((pt = name_split(arcn->name, arcn->nlen)) == NULL) { |
965 |
paxwarn(1, "File name too long for ustar %s", arcn->name); |
||
966 |
return(1); |
||
967 |
} |
||
968 |
|||
969 |
/* |
||
970 |
* zero out the header so we don't have to worry about zero fill below |
||
971 |
*/ |
||
972 |
4 |
memset(hdblk, 0, sizeof(hdblk)); |
|
973 |
4 |
hd = (HD_USTAR *)hdblk; |
|
974 |
4 |
arcn->pad = 0L; |
|
975 |
|||
976 |
/* |
||
977 |
* split the name, or zero out the prefix |
||
978 |
*/ |
||
979 |
✗✓ | 4 |
if (pt != arcn->name) { |
980 |
/* |
||
981 |
* name was split, pt points at the / where the split is to |
||
982 |
* occur, we remove the / and copy the first part to the prefix |
||
983 |
*/ |
||
984 |
*pt = '\0'; |
||
985 |
fieldcpy(hd->prefix, sizeof(hd->prefix), arcn->name, |
||
986 |
sizeof(arcn->name)); |
||
987 |
*pt++ = '/'; |
||
988 |
} |
||
989 |
|||
990 |
/* |
||
991 |
* copy the name part. this may be the whole path or the part after |
||
992 |
* the prefix |
||
993 |
*/ |
||
994 |
4 |
fieldcpy(hd->name, sizeof(hd->name), pt, |
|
995 |
sizeof(arcn->name) - (pt - arcn->name)); |
||
996 |
|||
997 |
/* |
||
998 |
* set the fields in the header that are type dependent |
||
999 |
*/ |
||
1000 |
✗✗✗✗ ✓ |
4 |
switch (arcn->type) { |
1001 |
case PAX_DIR: |
||
1002 |
hd->typeflag = DIRTYPE; |
||
1003 |
if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3)) |
||
1004 |
goto out; |
||
1005 |
break; |
||
1006 |
case PAX_CHR: |
||
1007 |
case PAX_BLK: |
||
1008 |
if (arcn->type == PAX_CHR) |
||
1009 |
hd->typeflag = CHRTYPE; |
||
1010 |
else |
||
1011 |
hd->typeflag = BLKTYPE; |
||
1012 |
if (ul_oct((u_long)MAJOR(arcn->sb.st_rdev), hd->devmajor, |
||
1013 |
sizeof(hd->devmajor), 3) || |
||
1014 |
ul_oct((u_long)MINOR(arcn->sb.st_rdev), hd->devminor, |
||
1015 |
sizeof(hd->devminor), 3) || |
||
1016 |
ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3)) |
||
1017 |
goto out; |
||
1018 |
break; |
||
1019 |
case PAX_FIF: |
||
1020 |
hd->typeflag = FIFOTYPE; |
||
1021 |
if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3)) |
||
1022 |
goto out; |
||
1023 |
break; |
||
1024 |
case PAX_SLK: |
||
1025 |
case PAX_HLK: |
||
1026 |
case PAX_HRG: |
||
1027 |
if (arcn->type == PAX_SLK) |
||
1028 |
hd->typeflag = SYMTYPE; |
||
1029 |
else |
||
1030 |
hd->typeflag = LNKTYPE; |
||
1031 |
fieldcpy(hd->linkname, sizeof(hd->linkname), arcn->ln_name, |
||
1032 |
sizeof(arcn->ln_name)); |
||
1033 |
if (ul_oct((u_long)0L, hd->size, sizeof(hd->size), 3)) |
||
1034 |
goto out; |
||
1035 |
break; |
||
1036 |
case PAX_REG: |
||
1037 |
case PAX_CTG: |
||
1038 |
default: |
||
1039 |
/* |
||
1040 |
* file data with this type, set the padding |
||
1041 |
*/ |
||
1042 |
✗✓ | 4 |
if (arcn->type == PAX_CTG) |
1043 |
hd->typeflag = CONTTYPE; |
||
1044 |
else |
||
1045 |
4 |
hd->typeflag = REGTYPE; |
|
1046 |
4 |
arcn->pad = TAR_PAD(arcn->sb.st_size); |
|
1047 |
✗✓ | 4 |
if (uqd_oct((u_quad_t)arcn->sb.st_size, hd->size, |
1048 |
sizeof(hd->size), 3)) { |
||
1049 |
paxwarn(1,"File is too long for ustar %s",arcn->org_name); |
||
1050 |
return(1); |
||
1051 |
} |
||
1052 |
break; |
||
1053 |
} |
||
1054 |
|||
1055 |
4 |
strncpy(hd->magic, TMAGIC, TMAGLEN); |
|
1056 |
4 |
strncpy(hd->version, TVERSION, TVERSLEN); |
|
1057 |
|||
1058 |
/* |
||
1059 |
* set the remaining fields. Some versions want all 16 bits of mode |
||
1060 |
* we better humor them (they really do not meet spec though).... |
||
1061 |
*/ |
||
1062 |
✗✓ | 4 |
if (ul_oct((u_long)arcn->sb.st_uid, hd->uid, sizeof(hd->uid), 3)) { |
1063 |
if (uid_nobody == 0) { |
||
1064 |
if (uid_name("nobody", &uid_nobody) == -1) |
||
1065 |
goto out; |
||
1066 |
} |
||
1067 |
if (uid_warn != arcn->sb.st_uid) { |
||
1068 |
uid_warn = arcn->sb.st_uid; |
||
1069 |
paxwarn(1, |
||
1070 |
"Ustar header field is too small for uid %lu, " |
||
1071 |
"using nobody", (u_long)arcn->sb.st_uid); |
||
1072 |
} |
||
1073 |
if (ul_oct((u_long)uid_nobody, hd->uid, sizeof(hd->uid), 3)) |
||
1074 |
goto out; |
||
1075 |
} |
||
1076 |
✗✓ | 4 |
if (ul_oct((u_long)arcn->sb.st_gid, hd->gid, sizeof(hd->gid), 3)) { |
1077 |
if (gid_nobody == 0) { |
||
1078 |
if (gid_name("nobody", &gid_nobody) == -1) |
||
1079 |
goto out; |
||
1080 |
} |
||
1081 |
if (gid_warn != arcn->sb.st_gid) { |
||
1082 |
gid_warn = arcn->sb.st_gid; |
||
1083 |
paxwarn(1, |
||
1084 |
"Ustar header field is too small for gid %lu, " |
||
1085 |
"using nobody", (u_long)arcn->sb.st_gid); |
||
1086 |
} |
||
1087 |
if (ul_oct((u_long)gid_nobody, hd->gid, sizeof(hd->gid), 3)) |
||
1088 |
goto out; |
||
1089 |
} |
||
1090 |
✓✗✓✗ |
4 |
if (uqd_oct(arcn->sb.st_mtime < 0 ? 0 : arcn->sb.st_mtime, hd->mtime, |
1091 |
sizeof(hd->mtime), 3) || |
||
1092 |
ul_oct((u_long)arcn->sb.st_mode, hd->mode, sizeof(hd->mode), 3)) |
||
1093 |
goto out; |
||
1094 |
✓✗ | 4 |
if (!Nflag) { |
1095 |
4 |
strncpy(hd->uname, name_uid(arcn->sb.st_uid, 0), sizeof(hd->uname)); |
|
1096 |
4 |
strncpy(hd->gname, name_gid(arcn->sb.st_gid, 0), sizeof(hd->gname)); |
|
1097 |
} else { |
||
1098 |
strncpy(hd->uname, "", sizeof(hd->uname)); |
||
1099 |
strncpy(hd->gname, "", sizeof(hd->gname)); |
||
1100 |
} |
||
1101 |
|||
1102 |
/* |
||
1103 |
* calculate and store the checksum write the header to the archive |
||
1104 |
* return 0 tells the caller to now write the file data, 1 says no data |
||
1105 |
* needs to be written |
||
1106 |
*/ |
||
1107 |
✗✓ | 4 |
if (ul_oct(tar_chksm(hdblk, sizeof(HD_USTAR)), hd->chksum, |
1108 |
sizeof(hd->chksum), 3)) |
||
1109 |
goto out; |
||
1110 |
✗✓ | 4 |
if (wr_rdbuf(hdblk, sizeof(HD_USTAR)) < 0) |
1111 |
return(-1); |
||
1112 |
✗✓ | 4 |
if (wr_skip((off_t)(BLKMULT - sizeof(HD_USTAR))) < 0) |
1113 |
return(-1); |
||
1114 |
✓✗ | 4 |
if (PAX_IS_REG(arcn->type)) |
1115 |
4 |
return(0); |
|
1116 |
return(1); |
||
1117 |
|||
1118 |
out: |
||
1119 |
/* |
||
1120 |
* header field is out of range |
||
1121 |
*/ |
||
1122 |
paxwarn(1, "Ustar header field is too small for %s", arcn->org_name); |
||
1123 |
return(1); |
||
1124 |
} |
||
1125 |
|||
1126 |
/* |
||
1127 |
* name_split() |
||
1128 |
* see if the name has to be split for storage in a ustar header. We try |
||
1129 |
* to fit the entire name in the name field without splitting if we can. |
||
1130 |
* The split point is always at a / |
||
1131 |
* Return |
||
1132 |
* character pointer to split point (always the / that is to be removed |
||
1133 |
* if the split is not needed, the points is set to the start of the file |
||
1134 |
* name (it would violate the spec to split there). A NULL is returned if |
||
1135 |
* the file name is too long |
||
1136 |
*/ |
||
1137 |
|||
1138 |
static char * |
||
1139 |
name_split(char *name, int len) |
||
1140 |
4 |
{ |
|
1141 |
char *start; |
||
1142 |
|||
1143 |
/* |
||
1144 |
* check to see if the file name is small enough to fit in the name |
||
1145 |
* field. if so just return a pointer to the name. |
||
1146 |
* The strings can fill the complete name and prefix fields |
||
1147 |
* without a NUL terminator. |
||
1148 |
*/ |
||
1149 |
✓✗ | 4 |
if (len <= TNMSZ) |
1150 |
4 |
return(name); |
|
1151 |
if (len > (TPFSZ + TNMSZ + 1)) |
||
1152 |
return(NULL); |
||
1153 |
|||
1154 |
/* |
||
1155 |
* we start looking at the biggest sized piece that fits in the name |
||
1156 |
* field. We walk forward looking for a slash to split at. The idea is |
||
1157 |
* to find the biggest piece to fit in the name field (or the smallest |
||
1158 |
* prefix we can find) (the -1 is correct the biggest piece would |
||
1159 |
* include the slash between the two parts that gets thrown away) |
||
1160 |
*/ |
||
1161 |
start = name + len - TNMSZ - 1; |
||
1162 |
|||
1163 |
/* |
||
1164 |
* the prefix may not be empty, so skip the first character when |
||
1165 |
* trying to split a path of exactly TNMSZ+1 characters. |
||
1166 |
* NOTE: This means the ustar format can't store /str if |
||
1167 |
* str contains no slashes and the length of str == TNMSZ |
||
1168 |
*/ |
||
1169 |
if (start == name) |
||
1170 |
++start; |
||
1171 |
|||
1172 |
while ((*start != '\0') && (*start != '/')) |
||
1173 |
++start; |
||
1174 |
|||
1175 |
/* |
||
1176 |
* if we hit the end of the string, this name cannot be split, so we |
||
1177 |
* cannot store this file. |
||
1178 |
*/ |
||
1179 |
if (*start == '\0') |
||
1180 |
return(NULL); |
||
1181 |
|||
1182 |
/* |
||
1183 |
* the split point isn't valid if it results in a prefix |
||
1184 |
* longer than TPFSZ |
||
1185 |
*/ |
||
1186 |
if ((start - name) > TPFSZ) |
||
1187 |
return(NULL); |
||
1188 |
|||
1189 |
/* |
||
1190 |
* ok have a split point, return it to the caller |
||
1191 |
*/ |
||
1192 |
return(start); |
||
1193 |
} |
||
1194 |
|||
1195 |
static size_t |
||
1196 |
expandname(char *buf, size_t len, char **gnu_name, const char *name, |
||
1197 |
size_t limit) |
||
1198 |
240 |
{ |
|
1199 |
size_t nlen; |
||
1200 |
|||
1201 |
✗✓ | 240 |
if (*gnu_name) { |
1202 |
/* *gnu_name is NUL terminated */ |
||
1203 |
if ((nlen = strlcpy(buf, *gnu_name, len)) >= len) |
||
1204 |
nlen = len - 1; |
||
1205 |
free(*gnu_name); |
||
1206 |
*gnu_name = NULL; |
||
1207 |
} else |
||
1208 |
240 |
nlen = fieldcpy(buf, len, name, limit); |
|
1209 |
240 |
return(nlen); |
|
1210 |
} |
||
1211 |
|||
1212 |
#ifndef SMALL |
||
1213 |
|||
1214 |
/* shortest possible extended record: "5 a=\n" */ |
||
1215 |
#define MINXHDRSZ 5 |
||
1216 |
|||
1217 |
/* longest record we'll accept */ |
||
1218 |
#define MAXXHDRSZ BLKMULT |
||
1219 |
|||
1220 |
static int |
||
1221 |
rd_xheader(ARCHD *arcn, int global, off_t size) |
||
1222 |
{ |
||
1223 |
char buf[MAXXHDRSZ]; |
||
1224 |
unsigned long len; |
||
1225 |
char *delim, *keyword; |
||
1226 |
char *nextp, *p, *end; |
||
1227 |
int pad, ret = 0; |
||
1228 |
|||
1229 |
/* before we alter size, make note of how much we have to skip */ |
||
1230 |
pad = TAR_PAD((unsigned)size); |
||
1231 |
|||
1232 |
p = end = buf; |
||
1233 |
while (size > 0 || p < end) { |
||
1234 |
if (size > 0) { |
||
1235 |
int rdlen; |
||
1236 |
|||
1237 |
/* shift stuff down */ |
||
1238 |
if (p > buf) { |
||
1239 |
memmove(buf, p, end - p); |
||
1240 |
end -= p - buf; |
||
1241 |
p = buf; |
||
1242 |
} |
||
1243 |
|||
1244 |
/* fill starting at end */ |
||
1245 |
rdlen = MINIMUM(size, (buf + sizeof buf) - end); |
||
1246 |
if (rd_wrbuf(end, rdlen) != rdlen) { |
||
1247 |
ret = -1; |
||
1248 |
break; |
||
1249 |
} |
||
1250 |
size -= rdlen; |
||
1251 |
end += rdlen; |
||
1252 |
} |
||
1253 |
|||
1254 |
/* [p, end) is good */ |
||
1255 |
if (memchr(p, ' ', end - p) == NULL || |
||
1256 |
!isdigit((unsigned char)*p)) { |
||
1257 |
paxwarn(1, "Invalid extended header record"); |
||
1258 |
ret = -1; |
||
1259 |
break; |
||
1260 |
} |
||
1261 |
errno = 0; |
||
1262 |
len = strtoul(p, &delim, 10); |
||
1263 |
if (*delim != ' ' || (errno == ERANGE && len == ULONG_MAX) || |
||
1264 |
len < MINXHDRSZ) { |
||
1265 |
paxwarn(1, "Invalid extended header record length"); |
||
1266 |
ret = -1; |
||
1267 |
break; |
||
1268 |
} |
||
1269 |
if (len > end - p) { |
||
1270 |
paxwarn(1, "Extended header record length %lu is " |
||
1271 |
"out of range", len); |
||
1272 |
/* if we can just toss this record, do so */ |
||
1273 |
len -= end - p; |
||
1274 |
if (len <= size && rd_skip(len) == 0) { |
||
1275 |
size -= len; |
||
1276 |
p = end = buf; |
||
1277 |
continue; |
||
1278 |
} |
||
1279 |
ret = -1; |
||
1280 |
break; |
||
1281 |
} |
||
1282 |
nextp = p + len; |
||
1283 |
keyword = p = delim + 1; |
||
1284 |
p = memchr(p, '=', len); |
||
1285 |
if (!p || nextp[-1] != '\n') { |
||
1286 |
paxwarn(1, "Malformed extended header record"); |
||
1287 |
ret = -1; |
||
1288 |
break; |
||
1289 |
} |
||
1290 |
*p++ = nextp[-1] = '\0'; |
||
1291 |
if (!global) { |
||
1292 |
if (!strcmp(keyword, "path")) { |
||
1293 |
arcn->nlen = strlcpy(arcn->name, p, |
||
1294 |
sizeof(arcn->name)); |
||
1295 |
} else if (!strcmp(keyword, "linkpath")) { |
||
1296 |
arcn->ln_nlen = strlcpy(arcn->ln_name, p, |
||
1297 |
sizeof(arcn->ln_name)); |
||
1298 |
} |
||
1299 |
} |
||
1300 |
p = nextp; |
||
1301 |
} |
||
1302 |
|||
1303 |
if (rd_skip(size + pad) < 0) |
||
1304 |
return (-1); |
||
1305 |
return (ret); |
||
1306 |
} |
||
1307 |
#endif |
Generated by: GCOVR (Version 3.3) |