| 1 |
/* List a tar archive, with support routines for reading a tar archive. |
| 2 |
|
| 3 |
Copyright 1988-2023 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of GNU tar. |
| 6 |
|
| 7 |
GNU tar is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
GNU tar is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Written by John Gilmore, on 1985-08-26. */ |
| 21 |
|
| 22 |
#include <system.h> |
| 23 |
#include <inttostr.h> |
| 24 |
#include <quotearg.h> |
| 25 |
#include <time.h> |
| 26 |
#include "common.h" |
| 27 |
|
| 28 |
union block *current_header; /* points to current archive header */ |
| 29 |
enum archive_format current_format; /* recognized format */ |
| 30 |
union block *recent_long_name; /* recent long name header and contents */ |
| 31 |
union block *recent_long_link; /* likewise, for long link */ |
| 32 |
size_t recent_long_name_blocks; /* number of blocks in recent_long_name */ |
| 33 |
size_t recent_long_link_blocks; /* likewise, for long link */ |
| 34 |
static union block *recent_global_header; /* Recent global header block */ |
| 35 |
|
| 36 |
#define GID_FROM_HEADER(where) gid_from_header (where, sizeof (where)) |
| 37 |
#define MAJOR_FROM_HEADER(where) major_from_header (where, sizeof (where)) |
| 38 |
#define MINOR_FROM_HEADER(where) minor_from_header (where, sizeof (where)) |
| 39 |
#define MODE_FROM_HEADER(where, hbits) \ |
| 40 |
mode_from_header (where, sizeof (where), hbits) |
| 41 |
#define TIME_FROM_HEADER(where) time_from_header (where, sizeof (where)) |
| 42 |
#define UID_FROM_HEADER(where) uid_from_header (where, sizeof (where)) |
| 43 |
|
| 44 |
static gid_t gid_from_header (const char *buf, size_t size); |
| 45 |
static major_t major_from_header (const char *buf, size_t size); |
| 46 |
static minor_t minor_from_header (const char *buf, size_t size); |
| 47 |
static mode_t mode_from_header (const char *buf, size_t size, bool *hbits); |
| 48 |
static time_t time_from_header (const char *buf, size_t size); |
| 49 |
static uid_t uid_from_header (const char *buf, size_t size); |
| 50 |
static intmax_t from_header (const char *, size_t, const char *, |
| 51 |
intmax_t, uintmax_t, bool, bool); |
| 52 |
|
| 53 |
/* Base 64 digits; see Internet RFC 2045 Table 1. */ |
| 54 |
static char const base_64_digits[64] = |
| 55 |
{ |
| 56 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
| 57 |
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
| 58 |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
| 59 |
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
| 60 |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' |
| 61 |
}; |
| 62 |
|
| 63 |
/* Table of base-64 digit values indexed by unsigned chars. |
| 64 |
The value is 64 for unsigned chars that are not base-64 digits. */ |
| 65 |
static char base64_map[UCHAR_MAX + 1]; |
| 66 |
|
| 67 |
static void |
| 68 |
base64_init (void) |
| 69 |
{ |
| 70 |
int i; |
| 71 |
memset (base64_map, 64, sizeof base64_map); |
| 72 |
for (i = 0; i < 64; i++) |
| 73 |
base64_map[(int) base_64_digits[i]] = i; |
| 74 |
} |
| 75 |
|
| 76 |
static char * |
| 77 |
decode_xform (char *file_name, void *data) |
| 78 |
{ |
| 79 |
int type = *(int*)data; |
| 80 |
|
| 81 |
switch (type) |
| 82 |
{ |
| 83 |
case XFORM_SYMLINK: |
| 84 |
/* FIXME: It is not quite clear how and to which extent are the symbolic |
| 85 |
links subject to filename transformation. In the absence of another |
| 86 |
solution, symbolic links are exempt from component stripping and |
| 87 |
name suffix normalization, but subject to filename transformation |
| 88 |
proper. */ |
| 89 |
return file_name; |
| 90 |
|
| 91 |
case XFORM_LINK: |
| 92 |
file_name = safer_name_suffix (file_name, true, absolute_names_option); |
| 93 |
break; |
| 94 |
|
| 95 |
case XFORM_REGFILE: |
| 96 |
file_name = safer_name_suffix (file_name, false, absolute_names_option); |
| 97 |
break; |
| 98 |
} |
| 99 |
|
| 100 |
if (strip_name_components) |
| 101 |
{ |
| 102 |
size_t prefix_len = stripped_prefix_len (file_name, |
| 103 |
strip_name_components); |
| 104 |
if (prefix_len == (size_t) -1) |
| 105 |
prefix_len = strlen (file_name); |
| 106 |
file_name += prefix_len; |
| 107 |
} |
| 108 |
return file_name; |
| 109 |
} |
| 110 |
|
| 111 |
static bool |
| 112 |
transform_member_name (char **pinput, int type) |
| 113 |
{ |
| 114 |
return transform_name_fp (pinput, type, decode_xform, &type); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
enforce_one_top_level (char **pfile_name) |
| 119 |
{ |
| 120 |
char *file_name = *pfile_name; |
| 121 |
char *p; |
| 122 |
|
| 123 |
for (p = file_name; *p && (ISSLASH (*p) || *p == '.'); p++) |
| 124 |
; |
| 125 |
|
| 126 |
if (*p) |
| 127 |
{ |
| 128 |
int pos = strlen (one_top_level_dir); |
| 129 |
if (strncmp (p, one_top_level_dir, pos) == 0) |
| 130 |
{ |
| 131 |
if (ISSLASH (p[pos]) || p[pos] == 0) |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
*pfile_name = make_file_name (one_top_level_dir, file_name); |
| 136 |
normalize_filename_x (*pfile_name); |
| 137 |
} |
| 138 |
else |
| 139 |
*pfile_name = xstrdup (one_top_level_dir); |
| 140 |
free (file_name); |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
transform_stat_info (int typeflag, struct tar_stat_info *stat_info) |
| 145 |
{ |
| 146 |
if (typeflag == GNUTYPE_VOLHDR) |
| 147 |
/* Name transformations don't apply to volume headers. */ |
| 148 |
return; |
| 149 |
|
| 150 |
transform_member_name (&stat_info->file_name, XFORM_REGFILE); |
| 151 |
switch (typeflag) |
| 152 |
{ |
| 153 |
case SYMTYPE: |
| 154 |
transform_member_name (&stat_info->link_name, XFORM_SYMLINK); |
| 155 |
break; |
| 156 |
|
| 157 |
case LNKTYPE: |
| 158 |
transform_member_name (&stat_info->link_name, XFORM_LINK); |
| 159 |
} |
| 160 |
|
| 161 |
if (one_top_level_option) |
| 162 |
enforce_one_top_level (¤t_stat_info.file_name); |
| 163 |
} |
| 164 |
|
| 165 |
/* Main loop for reading an archive. */ |
| 166 |
void |
| 167 |
read_and (void (*do_something) (void)) |
| 168 |
{ |
| 169 |
enum read_header status = HEADER_STILL_UNREAD; |
| 170 |
enum read_header prev_status; |
| 171 |
struct timespec mtime; |
| 172 |
|
| 173 |
base64_init (); |
| 174 |
name_gather (); |
| 175 |
|
| 176 |
open_archive (ACCESS_READ); |
| 177 |
do |
| 178 |
{ |
| 179 |
prev_status = status; |
| 180 |
tar_stat_destroy (¤t_stat_info); |
| 181 |
|
| 182 |
status = read_header (¤t_header, ¤t_stat_info, |
| 183 |
read_header_auto); |
| 184 |
switch (status) |
| 185 |
{ |
| 186 |
case HEADER_STILL_UNREAD: |
| 187 |
case HEADER_SUCCESS_EXTENDED: |
| 188 |
abort (); |
| 189 |
|
| 190 |
case HEADER_SUCCESS: |
| 191 |
|
| 192 |
/* Valid header. We should decode next field (mode) first. |
| 193 |
Ensure incoming names are null terminated. */ |
| 194 |
decode_header (current_header, ¤t_stat_info, |
| 195 |
¤t_format, 1); |
| 196 |
if (! name_match (current_stat_info.file_name) |
| 197 |
|| (TIME_OPTION_INITIALIZED (newer_mtime_option) |
| 198 |
/* FIXME: We get mtime now, and again later; this causes |
| 199 |
duplicate diagnostics if header.mtime is bogus. */ |
| 200 |
&& ((mtime.tv_sec |
| 201 |
= TIME_FROM_HEADER (current_header->header.mtime)), |
| 202 |
/* FIXME: Grab fractional time stamps from |
| 203 |
extended header. */ |
| 204 |
mtime.tv_nsec = 0, |
| 205 |
current_stat_info.mtime = mtime, |
| 206 |
OLDER_TAR_STAT_TIME (current_stat_info, m))) |
| 207 |
|| excluded_name (current_stat_info.file_name, |
| 208 |
current_stat_info.parent)) |
| 209 |
{ |
| 210 |
switch (current_header->header.typeflag) |
| 211 |
{ |
| 212 |
case GNUTYPE_VOLHDR: |
| 213 |
case GNUTYPE_MULTIVOL: |
| 214 |
break; |
| 215 |
|
| 216 |
case DIRTYPE: |
| 217 |
if (show_omitted_dirs_option) |
| 218 |
WARN ((0, 0, _("%s: Omitting"), |
| 219 |
quotearg_colon (current_stat_info.file_name))); |
| 220 |
FALLTHROUGH; |
| 221 |
default: |
| 222 |
skip_member (); |
| 223 |
continue; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
transform_stat_info (current_header->header.typeflag, |
| 228 |
¤t_stat_info); |
| 229 |
(*do_something) (); |
| 230 |
continue; |
| 231 |
|
| 232 |
case HEADER_ZERO_BLOCK: |
| 233 |
if (block_number_option) |
| 234 |
{ |
| 235 |
char buf[UINTMAX_STRSIZE_BOUND]; |
| 236 |
fprintf (stdlis, _("block %s: ** Block of NULs **\n"), |
| 237 |
STRINGIFY_BIGINT (current_block_ordinal (), buf)); |
| 238 |
} |
| 239 |
|
| 240 |
set_next_block_after (current_header); |
| 241 |
|
| 242 |
if (!ignore_zeros_option) |
| 243 |
{ |
| 244 |
char buf[UINTMAX_STRSIZE_BOUND]; |
| 245 |
|
| 246 |
status = read_header (¤t_header, ¤t_stat_info, |
| 247 |
read_header_auto); |
| 248 |
if (status == HEADER_ZERO_BLOCK) |
| 249 |
break; |
| 250 |
WARNOPT (WARN_ALONE_ZERO_BLOCK, |
| 251 |
(0, 0, _("A lone zero block at %s"), |
| 252 |
STRINGIFY_BIGINT (current_block_ordinal (), buf))); |
| 253 |
break; |
| 254 |
} |
| 255 |
status = prev_status; |
| 256 |
continue; |
| 257 |
|
| 258 |
case HEADER_END_OF_FILE: |
| 259 |
if (!ignore_zeros_option) |
| 260 |
{ |
| 261 |
char buf[UINTMAX_STRSIZE_BOUND]; |
| 262 |
WARNOPT (WARN_MISSING_ZERO_BLOCKS, |
| 263 |
(0, 0, _("Terminating zero blocks missing at %s"), |
| 264 |
STRINGIFY_BIGINT (current_block_ordinal (), buf))); |
| 265 |
} |
| 266 |
if (block_number_option) |
| 267 |
{ |
| 268 |
char buf[UINTMAX_STRSIZE_BOUND]; |
| 269 |
fprintf (stdlis, _("block %s: ** End of File **\n"), |
| 270 |
STRINGIFY_BIGINT (current_block_ordinal (), buf)); |
| 271 |
} |
| 272 |
break; |
| 273 |
|
| 274 |
case HEADER_FAILURE: |
| 275 |
/* If the previous header was good, tell them that we are |
| 276 |
skipping bad ones. */ |
| 277 |
set_next_block_after (current_header); |
| 278 |
switch (prev_status) |
| 279 |
{ |
| 280 |
case HEADER_STILL_UNREAD: |
| 281 |
ERROR ((0, 0, _("This does not look like a tar archive"))); |
| 282 |
FALLTHROUGH; |
| 283 |
case HEADER_ZERO_BLOCK: |
| 284 |
case HEADER_SUCCESS: |
| 285 |
if (block_number_option) |
| 286 |
{ |
| 287 |
char buf[UINTMAX_STRSIZE_BOUND]; |
| 288 |
off_t block_ordinal = current_block_ordinal (); |
| 289 |
block_ordinal -= recent_long_name_blocks; |
| 290 |
block_ordinal -= recent_long_link_blocks; |
| 291 |
fprintf (stdlis, _("block %s: "), |
| 292 |
STRINGIFY_BIGINT (block_ordinal, buf)); |
| 293 |
} |
| 294 |
ERROR ((0, 0, _("Skipping to next header"))); |
| 295 |
break; |
| 296 |
|
| 297 |
case HEADER_END_OF_FILE: |
| 298 |
case HEADER_FAILURE: |
| 299 |
/* We are in the middle of a cascade of errors. */ |
| 300 |
break; |
| 301 |
|
| 302 |
case HEADER_SUCCESS_EXTENDED: |
| 303 |
abort (); |
| 304 |
} |
| 305 |
continue; |
| 306 |
} |
| 307 |
break; |
| 308 |
} |
| 309 |
while (!all_names_found (¤t_stat_info)); |
| 310 |
|
| 311 |
close_archive (); |
| 312 |
names_notfound (); /* print names not found */ |
| 313 |
} |
| 314 |
|
| 315 |
/* Print a header block, based on tar options. */ |
| 316 |
void |
| 317 |
list_archive (void) |
| 318 |
{ |
| 319 |
off_t block_ordinal = current_block_ordinal (); |
| 320 |
|
| 321 |
/* Print the header block. */ |
| 322 |
if (verbose_option) |
| 323 |
print_header (¤t_stat_info, current_header, block_ordinal); |
| 324 |
|
| 325 |
if (incremental_option) |
| 326 |
{ |
| 327 |
if (verbose_option > 2) |
| 328 |
{ |
| 329 |
if (is_dumpdir (¤t_stat_info)) |
| 330 |
list_dumpdir (current_stat_info.dumpdir, |
| 331 |
dumpdir_size (current_stat_info.dumpdir)); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
skip_member (); |
| 336 |
} |
| 337 |
|
| 338 |
/* Check header checksum */ |
| 339 |
/* The standard BSD tar sources create the checksum by adding up the |
| 340 |
bytes in the header as type char. I think the type char was unsigned |
| 341 |
on the PDP-11, but it's signed on the Next and Sun. It looks like the |
| 342 |
sources to BSD tar were never changed to compute the checksum |
| 343 |
correctly, so both the Sun and Next add the bytes of the header as |
| 344 |
signed chars. This doesn't cause a problem until you get a file with |
| 345 |
a name containing characters with the high bit set. So tar_checksum |
| 346 |
computes two checksums -- signed and unsigned. */ |
| 347 |
|
| 348 |
enum read_header |
| 349 |
tar_checksum (union block *header, bool silent) |
| 350 |
{ |
| 351 |
size_t i; |
| 352 |
int unsigned_sum = 0; /* the POSIX one :-) */ |
| 353 |
int signed_sum = 0; /* the Sun one :-( */ |
| 354 |
int recorded_sum; |
| 355 |
int parsed_sum; |
| 356 |
char *p; |
| 357 |
|
| 358 |
p = header->buffer; |
| 359 |
for (i = sizeof *header; i-- != 0;) |
| 360 |
{ |
| 361 |
unsigned_sum += (unsigned char) *p; |
| 362 |
signed_sum += (signed char) (*p++); |
| 363 |
} |
| 364 |
|
| 365 |
if (unsigned_sum == 0) |
| 366 |
return HEADER_ZERO_BLOCK; |
| 367 |
|
| 368 |
/* Adjust checksum to count the "chksum" field as blanks. */ |
| 369 |
|
| 370 |
for (i = sizeof header->header.chksum; i-- != 0;) |
| 371 |
{ |
| 372 |
unsigned_sum -= (unsigned char) header->header.chksum[i]; |
| 373 |
signed_sum -= (signed char) (header->header.chksum[i]); |
| 374 |
} |
| 375 |
unsigned_sum += ' ' * sizeof header->header.chksum; |
| 376 |
signed_sum += ' ' * sizeof header->header.chksum; |
| 377 |
|
| 378 |
parsed_sum = from_header (header->header.chksum, |
| 379 |
sizeof header->header.chksum, 0, |
| 380 |
0, INT_MAX, true, silent); |
| 381 |
if (parsed_sum < 0) |
| 382 |
return HEADER_FAILURE; |
| 383 |
|
| 384 |
recorded_sum = parsed_sum; |
| 385 |
|
| 386 |
if (unsigned_sum != recorded_sum && signed_sum != recorded_sum) |
| 387 |
return HEADER_FAILURE; |
| 388 |
|
| 389 |
return HEADER_SUCCESS; |
| 390 |
} |
| 391 |
|
| 392 |
/* Read a block that's supposed to be a header block. Return its |
| 393 |
address in *RETURN_BLOCK, and if it is good, the file's size |
| 394 |
and names (file name, link name) in *INFO. |
| 395 |
|
| 396 |
Return one of enum read_header describing the status of the |
| 397 |
operation. |
| 398 |
|
| 399 |
The MODE parameter instructs read_header what to do with special |
| 400 |
header blocks, i.e.: extended POSIX, GNU long name or long link, |
| 401 |
etc.: |
| 402 |
|
| 403 |
read_header_auto process them automatically, |
| 404 |
read_header_x_raw when a special header is read, return |
| 405 |
HEADER_SUCCESS_EXTENDED without actually |
| 406 |
processing the header, |
| 407 |
read_header_x_global when a POSIX global header is read, |
| 408 |
decode it and return HEADER_SUCCESS_EXTENDED. |
| 409 |
|
| 410 |
You must always set_next_block_after(*return_block) to skip past |
| 411 |
the header which this routine reads. */ |
| 412 |
|
| 413 |
enum read_header |
| 414 |
read_header (union block **return_block, struct tar_stat_info *info, |
| 415 |
enum read_header_mode mode) |
| 416 |
{ |
| 417 |
union block *header; |
| 418 |
char *bp; |
| 419 |
union block *data_block; |
| 420 |
size_t size, written; |
| 421 |
union block *next_long_name = NULL; |
| 422 |
union block *next_long_link = NULL; |
| 423 |
size_t next_long_name_blocks = 0; |
| 424 |
size_t next_long_link_blocks = 0; |
| 425 |
enum read_header status = HEADER_SUCCESS; |
| 426 |
|
| 427 |
while (1) |
| 428 |
{ |
| 429 |
header = find_next_block (); |
| 430 |
*return_block = header; |
| 431 |
if (!header) |
| 432 |
{ |
| 433 |
status = HEADER_END_OF_FILE; |
| 434 |
break; |
| 435 |
} |
| 436 |
|
| 437 |
if ((status = tar_checksum (header, false)) != HEADER_SUCCESS) |
| 438 |
break; |
| 439 |
|
| 440 |
/* Good block. Decode file size and return. */ |
| 441 |
|
| 442 |
if (header->header.typeflag == LNKTYPE) |
| 443 |
info->stat.st_size = 0; /* links 0 size on tape */ |
| 444 |
else |
| 445 |
{ |
| 446 |
info->stat.st_size = OFF_FROM_HEADER (header->header.size); |
| 447 |
if (info->stat.st_size < 0) |
| 448 |
{ |
| 449 |
status = HEADER_FAILURE; |
| 450 |
break; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
if (header->header.typeflag == GNUTYPE_LONGNAME |
| 455 |
|| header->header.typeflag == GNUTYPE_LONGLINK |
| 456 |
|| header->header.typeflag == XHDTYPE |
| 457 |
|| header->header.typeflag == XGLTYPE |
| 458 |
|| header->header.typeflag == SOLARIS_XHDTYPE) |
| 459 |
{ |
| 460 |
if (mode == read_header_x_raw) |
| 461 |
{ |
| 462 |
status = HEADER_SUCCESS_EXTENDED; |
| 463 |
break; |
| 464 |
} |
| 465 |
else if (header->header.typeflag == GNUTYPE_LONGNAME |
| 466 |
|| header->header.typeflag == GNUTYPE_LONGLINK) |
| 467 |
{ |
| 468 |
union block *header_copy; |
| 469 |
size_t name_size = info->stat.st_size; |
| 470 |
size_t n = name_size % BLOCKSIZE; |
| 471 |
size = name_size + BLOCKSIZE; |
| 472 |
if (n) |
| 473 |
size += BLOCKSIZE - n; |
| 474 |
|
| 475 |
if (name_size != info->stat.st_size || size < name_size) |
| 476 |
xalloc_die (); |
| 477 |
|
| 478 |
header_copy = xmalloc (size + 1); |
| 479 |
|
| 480 |
if (header->header.typeflag == GNUTYPE_LONGNAME) |
| 481 |
{ |
| 482 |
free (next_long_name); |
| 483 |
next_long_name = header_copy; |
| 484 |
next_long_name_blocks = size / BLOCKSIZE; |
| 485 |
} |
| 486 |
else |
| 487 |
{ |
| 488 |
free (next_long_link); |
| 489 |
next_long_link = header_copy; |
| 490 |
next_long_link_blocks = size / BLOCKSIZE; |
| 491 |
} |
| 492 |
|
| 493 |
set_next_block_after (header); |
| 494 |
*header_copy = *header; |
| 495 |
bp = header_copy->buffer + BLOCKSIZE; |
| 496 |
|
| 497 |
for (size -= BLOCKSIZE; size > 0; size -= written) |
| 498 |
{ |
| 499 |
data_block = find_next_block (); |
| 500 |
if (! data_block) |
| 501 |
{ |
| 502 |
ERROR ((0, 0, _("Unexpected EOF in archive"))); |
| 503 |
break; |
| 504 |
} |
| 505 |
written = available_space_after (data_block); |
| 506 |
if (written > size) |
| 507 |
written = size; |
| 508 |
|
| 509 |
memcpy (bp, data_block->buffer, written); |
| 510 |
bp += written; |
| 511 |
set_next_block_after ((union block *) |
| 512 |
(data_block->buffer + written - 1)); |
| 513 |
} |
| 514 |
|
| 515 |
*bp = '\0'; |
| 516 |
} |
| 517 |
else if (header->header.typeflag == XHDTYPE |
| 518 |
|| header->header.typeflag == SOLARIS_XHDTYPE) |
| 519 |
xheader_read (&info->xhdr, header, |
| 520 |
OFF_FROM_HEADER (header->header.size)); |
| 521 |
else if (header->header.typeflag == XGLTYPE) |
| 522 |
{ |
| 523 |
struct xheader xhdr; |
| 524 |
|
| 525 |
if (!recent_global_header) |
| 526 |
recent_global_header = xmalloc (sizeof *recent_global_header); |
| 527 |
memcpy (recent_global_header, header, |
| 528 |
sizeof *recent_global_header); |
| 529 |
memset (&xhdr, 0, sizeof xhdr); |
| 530 |
xheader_read (&xhdr, header, |
| 531 |
OFF_FROM_HEADER (header->header.size)); |
| 532 |
xheader_decode_global (&xhdr); |
| 533 |
xheader_destroy (&xhdr); |
| 534 |
if (mode == read_header_x_global) |
| 535 |
{ |
| 536 |
status = HEADER_SUCCESS_EXTENDED; |
| 537 |
break; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
/* Loop! */ |
| 542 |
|
| 543 |
} |
| 544 |
else |
| 545 |
{ |
| 546 |
char const *name; |
| 547 |
struct posix_header const *h = &header->header; |
| 548 |
char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1]; |
| 549 |
|
| 550 |
free (recent_long_name); |
| 551 |
|
| 552 |
if (next_long_name) |
| 553 |
{ |
| 554 |
name = next_long_name->buffer + BLOCKSIZE; |
| 555 |
recent_long_name = next_long_name; |
| 556 |
recent_long_name_blocks = next_long_name_blocks; |
| 557 |
next_long_name = NULL; |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
/* Accept file names as specified by POSIX.1-1996 |
| 562 |
section 10.1.1. */ |
| 563 |
char *np = namebuf; |
| 564 |
|
| 565 |
if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0) |
| 566 |
{ |
| 567 |
memcpy (np, h->prefix, sizeof h->prefix); |
| 568 |
np[sizeof h->prefix] = '\0'; |
| 569 |
np += strlen (np); |
| 570 |
*np++ = '/'; |
| 571 |
} |
| 572 |
memcpy (np, h->name, sizeof h->name); |
| 573 |
np[sizeof h->name] = '\0'; |
| 574 |
name = namebuf; |
| 575 |
recent_long_name = 0; |
| 576 |
recent_long_name_blocks = 0; |
| 577 |
} |
| 578 |
assign_string (&info->orig_file_name, name); |
| 579 |
assign_string (&info->file_name, name); |
| 580 |
info->had_trailing_slash = strip_trailing_slashes (info->file_name); |
| 581 |
|
| 582 |
free (recent_long_link); |
| 583 |
|
| 584 |
if (next_long_link) |
| 585 |
{ |
| 586 |
name = next_long_link->buffer + BLOCKSIZE; |
| 587 |
recent_long_link = next_long_link; |
| 588 |
recent_long_link_blocks = next_long_link_blocks; |
| 589 |
next_long_link = NULL; |
| 590 |
} |
| 591 |
else |
| 592 |
{ |
| 593 |
memcpy (namebuf, h->linkname, sizeof h->linkname); |
| 594 |
namebuf[sizeof h->linkname] = '\0'; |
| 595 |
name = namebuf; |
| 596 |
recent_long_link = 0; |
| 597 |
recent_long_link_blocks = 0; |
| 598 |
} |
| 599 |
assign_string (&info->link_name, name); |
| 600 |
|
| 601 |
break; |
| 602 |
} |
| 603 |
} |
| 604 |
free (next_long_name); |
| 605 |
free (next_long_link); |
| 606 |
return status; |
| 607 |
} |
| 608 |
|
| 609 |
#define ISOCTAL(c) ((c)>='0'&&(c)<='7') |
| 610 |
|
| 611 |
/* Decode things from a file HEADER block into STAT_INFO, also setting |
| 612 |
*FORMAT_POINTER depending on the header block format. If |
| 613 |
DO_USER_GROUP, decode the user/group information (this is useful |
| 614 |
for extraction, but waste time when merely listing). |
| 615 |
|
| 616 |
read_header() has already decoded the checksum and length, so we don't. |
| 617 |
|
| 618 |
This routine should *not* be called twice for the same block, since |
| 619 |
the two calls might use different DO_USER_GROUP values and thus |
| 620 |
might end up with different uid/gid for the two calls. If anybody |
| 621 |
wants the uid/gid they should decode it first, and other callers |
| 622 |
should decode it without uid/gid before calling a routine, |
| 623 |
e.g. print_header, that assumes decoded data. */ |
| 624 |
void |
| 625 |
decode_header (union block *header, struct tar_stat_info *stat_info, |
| 626 |
enum archive_format *format_pointer, int do_user_group) |
| 627 |
{ |
| 628 |
enum archive_format format; |
| 629 |
bool hbits; |
| 630 |
mode_t mode = MODE_FROM_HEADER (header->header.mode, &hbits); |
| 631 |
|
| 632 |
if (strcmp (header->header.magic, TMAGIC) == 0) |
| 633 |
{ |
| 634 |
if (header->star_header.prefix[130] == 0 |
| 635 |
&& ISOCTAL (header->star_header.atime[0]) |
| 636 |
&& header->star_header.atime[11] == ' ' |
| 637 |
&& ISOCTAL (header->star_header.ctime[0]) |
| 638 |
&& header->star_header.ctime[11] == ' ') |
| 639 |
format = STAR_FORMAT; |
| 640 |
else if (stat_info->xhdr.size) |
| 641 |
format = POSIX_FORMAT; |
| 642 |
else |
| 643 |
format = USTAR_FORMAT; |
| 644 |
} |
| 645 |
else if (strcmp (header->buffer + offsetof (struct posix_header, magic), |
| 646 |
OLDGNU_MAGIC) |
| 647 |
== 0) |
| 648 |
format = hbits ? OLDGNU_FORMAT : GNU_FORMAT; |
| 649 |
else |
| 650 |
format = V7_FORMAT; |
| 651 |
*format_pointer = format; |
| 652 |
|
| 653 |
stat_info->stat.st_mode = mode; |
| 654 |
stat_info->mtime.tv_sec = TIME_FROM_HEADER (header->header.mtime); |
| 655 |
stat_info->mtime.tv_nsec = 0; |
| 656 |
assign_string_n (&stat_info->uname, |
| 657 |
header->header.uname[0] ? header->header.uname : NULL, |
| 658 |
sizeof (header->header.uname)); |
| 659 |
assign_string_n (&stat_info->gname, |
| 660 |
header->header.gname[0] ? header->header.gname : NULL, |
| 661 |
sizeof (header->header.gname)); |
| 662 |
|
| 663 |
xheader_xattr_init (stat_info); |
| 664 |
|
| 665 |
if (format == OLDGNU_FORMAT && incremental_option) |
| 666 |
{ |
| 667 |
stat_info->atime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.atime); |
| 668 |
stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->oldgnu_header.ctime); |
| 669 |
stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0; |
| 670 |
} |
| 671 |
else if (format == STAR_FORMAT) |
| 672 |
{ |
| 673 |
stat_info->atime.tv_sec = TIME_FROM_HEADER (header->star_header.atime); |
| 674 |
stat_info->ctime.tv_sec = TIME_FROM_HEADER (header->star_header.ctime); |
| 675 |
stat_info->atime.tv_nsec = stat_info->ctime.tv_nsec = 0; |
| 676 |
} |
| 677 |
else |
| 678 |
stat_info->atime = stat_info->ctime = start_time; |
| 679 |
|
| 680 |
if (format == V7_FORMAT) |
| 681 |
{ |
| 682 |
stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid); |
| 683 |
stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid); |
| 684 |
stat_info->stat.st_rdev = 0; |
| 685 |
} |
| 686 |
else |
| 687 |
{ |
| 688 |
if (do_user_group) |
| 689 |
{ |
| 690 |
/* FIXME: Decide if this should somewhat depend on -p. */ |
| 691 |
|
| 692 |
if (numeric_owner_option |
| 693 |
|| !*header->header.uname |
| 694 |
|| !uname_to_uid (header->header.uname, &stat_info->stat.st_uid)) |
| 695 |
stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid); |
| 696 |
|
| 697 |
if (numeric_owner_option |
| 698 |
|| !*header->header.gname |
| 699 |
|| !gname_to_gid (header->header.gname, &stat_info->stat.st_gid)) |
| 700 |
stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid); |
| 701 |
} |
| 702 |
|
| 703 |
switch (header->header.typeflag) |
| 704 |
{ |
| 705 |
case BLKTYPE: |
| 706 |
case CHRTYPE: |
| 707 |
stat_info->stat.st_rdev = |
| 708 |
makedev (MAJOR_FROM_HEADER (header->header.devmajor), |
| 709 |
MINOR_FROM_HEADER (header->header.devminor)); |
| 710 |
break; |
| 711 |
|
| 712 |
default: |
| 713 |
stat_info->stat.st_rdev = 0; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
xheader_decode (stat_info); |
| 718 |
|
| 719 |
if (sparse_member_p (stat_info)) |
| 720 |
{ |
| 721 |
sparse_fixup_header (stat_info); |
| 722 |
stat_info->is_sparse = true; |
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
stat_info->is_sparse = false; |
| 727 |
if (((current_format == GNU_FORMAT |
| 728 |
|| current_format == OLDGNU_FORMAT) |
| 729 |
&& current_header->header.typeflag == GNUTYPE_DUMPDIR) |
| 730 |
|| stat_info->dumpdir) |
| 731 |
stat_info->is_dumpdir = true; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
/* Convert buffer at WHERE0 of size DIGS from external format to |
| 737 |
intmax_t. DIGS must be positive. If TYPE is nonnull, the data are |
| 738 |
of type TYPE. The buffer must represent a value in the range |
| 739 |
MINVAL through MAXVAL; if the mathematically correct result V would |
| 740 |
be greater than INTMAX_MAX, return a negative integer V such that |
| 741 |
(uintmax_t) V yields the correct result. If OCTAL_ONLY, allow only octal |
| 742 |
numbers instead of the other GNU extensions. Return -1 on error, |
| 743 |
diagnosing the error if TYPE is nonnull and if !SILENT. */ |
| 744 |
#if ! (INTMAX_MAX <= UINTMAX_MAX && - (INTMAX_MIN + 1) <= UINTMAX_MAX) |
| 745 |
# error "from_header internally represents intmax_t as uintmax_t + sign" |
| 746 |
#endif |
| 747 |
#if ! (UINTMAX_MAX / 2 <= INTMAX_MAX) |
| 748 |
# error "from_header returns intmax_t to represent uintmax_t" |
| 749 |
#endif |
| 750 |
static intmax_t |
| 751 |
from_header (char const *where0, size_t digs, char const *type, |
| 752 |
intmax_t minval, uintmax_t maxval, |
| 753 |
bool octal_only, bool silent) |
| 754 |
{ |
| 755 |
uintmax_t value; |
| 756 |
uintmax_t uminval = minval; |
| 757 |
uintmax_t minus_minval = - uminval; |
| 758 |
char const *where = where0; |
| 759 |
char const *lim = where + digs; |
| 760 |
bool negative = false; |
| 761 |
|
| 762 |
/* Accommodate buggy tar of unknown vintage, which outputs leading |
| 763 |
NUL if the previous field overflows. */ |
| 764 |
where += !*where; |
| 765 |
|
| 766 |
/* Accommodate older tars, which output leading spaces. */ |
| 767 |
for (;;) |
| 768 |
{ |
| 769 |
if (where == lim) |
| 770 |
{ |
| 771 |
if (type && !silent) |
| 772 |
ERROR ((0, 0, |
| 773 |
/* TRANSLATORS: %s is type of the value (gid_t, uid_t, |
| 774 |
etc.) */ |
| 775 |
_("Blanks in header where numeric %s value expected"), |
| 776 |
type)); |
| 777 |
return -1; |
| 778 |
} |
| 779 |
if (!isspace ((unsigned char) *where)) |
| 780 |
break; |
| 781 |
where++; |
| 782 |
} |
| 783 |
|
| 784 |
value = 0; |
| 785 |
if (ISODIGIT (*where)) |
| 786 |
{ |
| 787 |
char const *where1 = where; |
| 788 |
bool overflow = false; |
| 789 |
|
| 790 |
for (;;) |
| 791 |
{ |
| 792 |
value += *where++ - '0'; |
| 793 |
if (where == lim || ! ISODIGIT (*where)) |
| 794 |
break; |
| 795 |
overflow |= value != (value << LG_8 >> LG_8); |
| 796 |
value <<= LG_8; |
| 797 |
} |
| 798 |
|
| 799 |
/* Parse the output of older, unportable tars, which generate |
| 800 |
negative values in two's complement octal. If the leading |
| 801 |
nonzero digit is 1, we can't recover the original value |
| 802 |
reliably; so do this only if the digit is 2 or more. This |
| 803 |
catches the common case of 32-bit negative time stamps. */ |
| 804 |
if ((overflow || maxval < value) && '2' <= *where1 && type) |
| 805 |
{ |
| 806 |
/* Compute the negative of the input value, assuming two's |
| 807 |
complement. */ |
| 808 |
int digit = (*where1 - '0') | 4; |
| 809 |
overflow = 0; |
| 810 |
value = 0; |
| 811 |
where = where1; |
| 812 |
for (;;) |
| 813 |
{ |
| 814 |
value += 7 - digit; |
| 815 |
where++; |
| 816 |
if (where == lim || ! ISODIGIT (*where)) |
| 817 |
break; |
| 818 |
digit = *where - '0'; |
| 819 |
overflow |= value != (value << LG_8 >> LG_8); |
| 820 |
value <<= LG_8; |
| 821 |
} |
| 822 |
value++; |
| 823 |
overflow |= !value; |
| 824 |
|
| 825 |
if (!overflow && value <= minus_minval) |
| 826 |
{ |
| 827 |
if (!silent) |
| 828 |
WARN ((0, 0, |
| 829 |
/* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ |
| 830 |
_("Archive octal value %.*s is out of %s range; assuming two's complement"), |
| 831 |
(int) (where - where1), where1, type)); |
| 832 |
negative = true; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
if (overflow) |
| 837 |
{ |
| 838 |
if (type && !silent) |
| 839 |
ERROR ((0, 0, |
| 840 |
/* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ |
| 841 |
_("Archive octal value %.*s is out of %s range"), |
| 842 |
(int) (where - where1), where1, type)); |
| 843 |
return -1; |
| 844 |
} |
| 845 |
} |
| 846 |
else if (octal_only) |
| 847 |
{ |
| 848 |
/* Suppress the following extensions. */ |
| 849 |
} |
| 850 |
else if (*where == '-' || *where == '+') |
| 851 |
{ |
| 852 |
/* Parse base-64 output produced only by tar test versions |
| 853 |
1.13.6 (1999-08-11) through 1.13.11 (1999-08-23). |
| 854 |
Support for this will be withdrawn in future releases. */ |
| 855 |
int dig; |
| 856 |
if (!silent) |
| 857 |
{ |
| 858 |
static bool warned_once; |
| 859 |
if (! warned_once) |
| 860 |
{ |
| 861 |
warned_once = true; |
| 862 |
WARN ((0, 0, _("Archive contains obsolescent base-64 headers"))); |
| 863 |
} |
| 864 |
} |
| 865 |
negative = *where++ == '-'; |
| 866 |
while (where != lim |
| 867 |
&& (dig = base64_map[(unsigned char) *where]) < 64) |
| 868 |
{ |
| 869 |
if (value << LG_64 >> LG_64 != value) |
| 870 |
{ |
| 871 |
char *string = alloca (digs + 1); |
| 872 |
memcpy (string, where0, digs); |
| 873 |
string[digs] = '\0'; |
| 874 |
if (type && !silent) |
| 875 |
ERROR ((0, 0, |
| 876 |
_("Archive signed base-64 string %s is out of %s range"), |
| 877 |
quote (string), type)); |
| 878 |
return -1; |
| 879 |
} |
| 880 |
value = (value << LG_64) | dig; |
| 881 |
where++; |
| 882 |
} |
| 883 |
} |
| 884 |
else if (where <= lim - 2 |
| 885 |
&& (*where == '\200' /* positive base-256 */ |
| 886 |
|| *where == '\377' /* negative base-256 */)) |
| 887 |
{ |
| 888 |
/* Parse base-256 output. A nonnegative number N is |
| 889 |
represented as (256**DIGS)/2 + N; a negative number -N is |
| 890 |
represented as (256**DIGS) - N, i.e. as two's complement. |
| 891 |
The representation guarantees that the leading bit is |
| 892 |
always on, so that we don't confuse this format with the |
| 893 |
others (assuming ASCII bytes of 8 bits or more). */ |
| 894 |
int signbit = *where & (1 << (LG_256 - 2)); |
| 895 |
uintmax_t topbits = (((uintmax_t) - signbit) |
| 896 |
<< (CHAR_BIT * sizeof (uintmax_t) |
| 897 |
- LG_256 - (LG_256 - 2))); |
| 898 |
value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit; |
| 899 |
for (;;) |
| 900 |
{ |
| 901 |
value = (value << LG_256) + (unsigned char) *where++; |
| 902 |
if (where == lim) |
| 903 |
break; |
| 904 |
if (((value << LG_256 >> LG_256) | topbits) != value) |
| 905 |
{ |
| 906 |
if (type && !silent) |
| 907 |
ERROR ((0, 0, |
| 908 |
_("Archive base-256 value is out of %s range"), |
| 909 |
type)); |
| 910 |
return -1; |
| 911 |
} |
| 912 |
} |
| 913 |
negative = signbit != 0; |
| 914 |
if (negative) |
| 915 |
value = -value; |
| 916 |
} |
| 917 |
|
| 918 |
if (where != lim && *where && !isspace ((unsigned char) *where)) |
| 919 |
{ |
| 920 |
if (type) |
| 921 |
{ |
| 922 |
char buf[1000]; /* Big enough to represent any header. */ |
| 923 |
static struct quoting_options *o; |
| 924 |
|
| 925 |
if (!o) |
| 926 |
{ |
| 927 |
o = clone_quoting_options (0); |
| 928 |
set_quoting_style (o, locale_quoting_style); |
| 929 |
} |
| 930 |
|
| 931 |
while (where0 != lim && ! lim[-1]) |
| 932 |
lim--; |
| 933 |
quotearg_buffer (buf, sizeof buf, where0, lim - where0, o); |
| 934 |
if (!silent) |
| 935 |
ERROR ((0, 0, |
| 936 |
/* TRANSLATORS: Second %s is a type name (gid_t,uid_t,etc.) */ |
| 937 |
_("Archive contains %.*s where numeric %s value expected"), |
| 938 |
(int) sizeof buf, buf, type)); |
| 939 |
} |
| 940 |
|
| 941 |
return -1; |
| 942 |
} |
| 943 |
|
| 944 |
if (value <= (negative ? minus_minval : maxval)) |
| 945 |
return represent_uintmax (negative ? -value : value); |
| 946 |
|
| 947 |
if (type && !silent) |
| 948 |
{ |
| 949 |
char minval_buf[UINTMAX_STRSIZE_BOUND + 1]; |
| 950 |
char maxval_buf[UINTMAX_STRSIZE_BOUND]; |
| 951 |
char value_buf[UINTMAX_STRSIZE_BOUND + 1]; |
| 952 |
char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1); |
| 953 |
char *value_string = STRINGIFY_BIGINT (value, value_buf + 1); |
| 954 |
if (negative) |
| 955 |
*--value_string = '-'; |
| 956 |
if (minus_minval) |
| 957 |
*--minval_string = '-'; |
| 958 |
/* TRANSLATORS: Second %s is type name (gid_t,uid_t,etc.) */ |
| 959 |
ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"), |
| 960 |
value_string, type, |
| 961 |
minval_string, STRINGIFY_BIGINT (maxval, maxval_buf))); |
| 962 |
} |
| 963 |
|
| 964 |
return -1; |
| 965 |
} |
| 966 |
|
| 967 |
static gid_t |
| 968 |
gid_from_header (const char *p, size_t s) |
| 969 |
{ |
| 970 |
return from_header (p, s, "gid_t", |
| 971 |
TYPE_MINIMUM (gid_t), TYPE_MAXIMUM (gid_t), |
| 972 |
false, false); |
| 973 |
} |
| 974 |
|
| 975 |
static major_t |
| 976 |
major_from_header (const char *p, size_t s) |
| 977 |
{ |
| 978 |
return from_header (p, s, "major_t", |
| 979 |
TYPE_MINIMUM (major_t), TYPE_MAXIMUM (major_t), |
| 980 |
false, false); |
| 981 |
} |
| 982 |
|
| 983 |
static minor_t |
| 984 |
minor_from_header (const char *p, size_t s) |
| 985 |
{ |
| 986 |
return from_header (p, s, "minor_t", |
| 987 |
TYPE_MINIMUM (minor_t), TYPE_MAXIMUM (minor_t), |
| 988 |
false, false); |
| 989 |
} |
| 990 |
|
| 991 |
/* Convert P to the file mode, as understood by tar. |
| 992 |
Set *HBITS if there are any unrecognized bits. */ |
| 993 |
static mode_t |
| 994 |
mode_from_header (const char *p, size_t s, bool *hbits) |
| 995 |
{ |
| 996 |
intmax_t u = from_header (p, s, "mode_t", |
| 997 |
INTMAX_MIN, UINTMAX_MAX, |
| 998 |
false, false); |
| 999 |
mode_t mode = ((u & TSUID ? S_ISUID : 0) |
| 1000 |
| (u & TSGID ? S_ISGID : 0) |
| 1001 |
| (u & TSVTX ? S_ISVTX : 0) |
| 1002 |
| (u & TUREAD ? S_IRUSR : 0) |
| 1003 |
| (u & TUWRITE ? S_IWUSR : 0) |
| 1004 |
| (u & TUEXEC ? S_IXUSR : 0) |
| 1005 |
| (u & TGREAD ? S_IRGRP : 0) |
| 1006 |
| (u & TGWRITE ? S_IWGRP : 0) |
| 1007 |
| (u & TGEXEC ? S_IXGRP : 0) |
| 1008 |
| (u & TOREAD ? S_IROTH : 0) |
| 1009 |
| (u & TOWRITE ? S_IWOTH : 0) |
| 1010 |
| (u & TOEXEC ? S_IXOTH : 0)); |
| 1011 |
*hbits = (u & ~07777) != 0; |
| 1012 |
return mode; |
| 1013 |
} |
| 1014 |
|
| 1015 |
off_t |
| 1016 |
off_from_header (const char *p, size_t s) |
| 1017 |
{ |
| 1018 |
/* Negative offsets are not allowed in tar files, so invoke |
| 1019 |
from_header with minimum value 0, not TYPE_MINIMUM (off_t). */ |
| 1020 |
return from_header (p, s, "off_t", |
| 1021 |
0, TYPE_MAXIMUM (off_t), |
| 1022 |
false, false); |
| 1023 |
} |
| 1024 |
|
| 1025 |
static time_t |
| 1026 |
time_from_header (const char *p, size_t s) |
| 1027 |
{ |
| 1028 |
return from_header (p, s, "time_t", |
| 1029 |
TYPE_MINIMUM (time_t), TYPE_MAXIMUM (time_t), |
| 1030 |
false, false); |
| 1031 |
} |
| 1032 |
|
| 1033 |
static uid_t |
| 1034 |
uid_from_header (const char *p, size_t s) |
| 1035 |
{ |
| 1036 |
return from_header (p, s, "uid_t", |
| 1037 |
TYPE_MINIMUM (uid_t), TYPE_MAXIMUM (uid_t), |
| 1038 |
false, false); |
| 1039 |
} |
| 1040 |
|
| 1041 |
uintmax_t |
| 1042 |
uintmax_from_header (const char *p, size_t s) |
| 1043 |
{ |
| 1044 |
return from_header (p, s, "uintmax_t", 0, UINTMAX_MAX, false, false); |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
/* Return a printable representation of T. The result points to |
| 1049 |
static storage that can be reused in the next call to this |
| 1050 |
function, to ctime, or to asctime. If FULL_TIME, then output the |
| 1051 |
time stamp to its full resolution; otherwise, just output it to |
| 1052 |
1-minute resolution. */ |
| 1053 |
char const * |
| 1054 |
tartime (struct timespec t, bool full_time) |
| 1055 |
{ |
| 1056 |
enum { fraclen = sizeof ".FFFFFFFFF" - 1 }; |
| 1057 |
static char buffer[max (UINTMAX_STRSIZE_BOUND + 1, |
| 1058 |
INT_STRLEN_BOUND (int) + 16) |
| 1059 |
+ fraclen]; |
| 1060 |
struct tm *tm; |
| 1061 |
time_t s = t.tv_sec; |
| 1062 |
int ns = t.tv_nsec; |
| 1063 |
bool negative = s < 0; |
| 1064 |
char *p; |
| 1065 |
|
| 1066 |
if (negative && ns != 0) |
| 1067 |
{ |
| 1068 |
s++; |
| 1069 |
ns = 1000000000 - ns; |
| 1070 |
} |
| 1071 |
|
| 1072 |
tm = utc_option ? gmtime (&s) : localtime (&s); |
| 1073 |
if (tm) |
| 1074 |
{ |
| 1075 |
if (full_time) |
| 1076 |
{ |
| 1077 |
strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M:%S", tm); |
| 1078 |
code_ns_fraction (ns, buffer + strlen (buffer)); |
| 1079 |
} |
| 1080 |
else |
| 1081 |
strftime (buffer, sizeof buffer, "%Y-%m-%d %H:%M", tm); |
| 1082 |
return buffer; |
| 1083 |
} |
| 1084 |
|
| 1085 |
/* The time stamp cannot be broken down, most likely because it |
| 1086 |
is out of range. Convert it as an integer, |
| 1087 |
right-adjusted in a field with the same width as the usual |
| 1088 |
4-year ISO time format. */ |
| 1089 |
p = umaxtostr (negative ? - (uintmax_t) s : s, |
| 1090 |
buffer + sizeof buffer - UINTMAX_STRSIZE_BOUND - fraclen); |
| 1091 |
if (negative) |
| 1092 |
*--p = '-'; |
| 1093 |
while ((buffer + sizeof buffer - sizeof "YYYY-MM-DD HH:MM" |
| 1094 |
+ (full_time ? sizeof ":SS.FFFFFFFFF" - 1 : 0)) |
| 1095 |
< p) |
| 1096 |
*--p = ' '; |
| 1097 |
if (full_time) |
| 1098 |
code_ns_fraction (ns, buffer + sizeof buffer - 1 - fraclen); |
| 1099 |
return p; |
| 1100 |
} |
| 1101 |
|
| 1102 |
/* Actually print it. |
| 1103 |
|
| 1104 |
Plain and fancy file header block logging. Non-verbose just prints |
| 1105 |
the name, e.g. for "tar t" or "tar x". This should just contain |
| 1106 |
file names, so it can be fed back into tar with xargs or the "-T" |
| 1107 |
option. The verbose option can give a bunch of info, one line per |
| 1108 |
file. I doubt anybody tries to parse its format, or if they do, |
| 1109 |
they shouldn't. Unix tar is pretty random here anyway. */ |
| 1110 |
|
| 1111 |
|
| 1112 |
/* Width of "user/group size", with initial value chosen |
| 1113 |
heuristically. This grows as needed, though this may cause some |
| 1114 |
stairstepping in the output. Make it too small and the output will |
| 1115 |
almost always look ragged. Make it too large and the output will |
| 1116 |
be spaced out too far. */ |
| 1117 |
static int ugswidth = 19; |
| 1118 |
|
| 1119 |
/* Width of printed time stamps. It grows if longer time stamps are |
| 1120 |
found (typically, those with nanosecond resolution). Like |
| 1121 |
USGWIDTH, some stairstepping may occur. */ |
| 1122 |
static int datewidth = sizeof "YYYY-MM-DD HH:MM" - 1; |
| 1123 |
|
| 1124 |
static bool volume_label_printed = false; |
| 1125 |
|
| 1126 |
static void |
| 1127 |
simple_print_header (struct tar_stat_info *st, union block *blk, |
| 1128 |
off_t block_ordinal) |
| 1129 |
{ |
| 1130 |
char modes[12]; |
| 1131 |
char const *time_stamp; |
| 1132 |
int time_stamp_len; |
| 1133 |
char *temp_name; |
| 1134 |
|
| 1135 |
/* These hold formatted ints. */ |
| 1136 |
char uform[max (INT_BUFSIZE_BOUND (intmax_t), UINTMAX_STRSIZE_BOUND)]; |
| 1137 |
char gform[sizeof uform]; |
| 1138 |
char *user, *group; |
| 1139 |
char size[2 * UINTMAX_STRSIZE_BOUND]; |
| 1140 |
/* holds formatted size or major,minor */ |
| 1141 |
char uintbuf[UINTMAX_STRSIZE_BOUND]; |
| 1142 |
int pad; |
| 1143 |
int sizelen; |
| 1144 |
|
| 1145 |
if (show_transformed_names_option) |
| 1146 |
temp_name = st->file_name ? st->file_name : st->orig_file_name; |
| 1147 |
else |
| 1148 |
temp_name = st->orig_file_name ? st->orig_file_name : st->file_name; |
| 1149 |
|
| 1150 |
if (block_number_option) |
| 1151 |
{ |
| 1152 |
char buf[UINTMAX_STRSIZE_BOUND]; |
| 1153 |
if (block_ordinal < 0) |
| 1154 |
block_ordinal = current_block_ordinal (); |
| 1155 |
block_ordinal -= recent_long_name_blocks; |
| 1156 |
block_ordinal -= recent_long_link_blocks; |
| 1157 |
fprintf (stdlis, _("block %s: "), |
| 1158 |
STRINGIFY_BIGINT (block_ordinal, buf)); |
| 1159 |
} |
| 1160 |
|
| 1161 |
if (verbose_option <= 1) |
| 1162 |
{ |
| 1163 |
/* Just the fax, mam. */ |
| 1164 |
fputs (quotearg (temp_name), stdlis); |
| 1165 |
if (show_transformed_names_option && st->had_trailing_slash) |
| 1166 |
fputc ('/', stdlis); |
| 1167 |
fputc ('\n', stdlis); |
| 1168 |
} |
| 1169 |
else |
| 1170 |
{ |
| 1171 |
/* File type and modes. */ |
| 1172 |
|
| 1173 |
modes[0] = '?'; |
| 1174 |
switch (blk->header.typeflag) |
| 1175 |
{ |
| 1176 |
case GNUTYPE_VOLHDR: |
| 1177 |
volume_label_printed = true; |
| 1178 |
modes[0] = 'V'; |
| 1179 |
break; |
| 1180 |
|
| 1181 |
case GNUTYPE_MULTIVOL: |
| 1182 |
modes[0] = 'M'; |
| 1183 |
break; |
| 1184 |
|
| 1185 |
case GNUTYPE_LONGNAME: |
| 1186 |
case GNUTYPE_LONGLINK: |
| 1187 |
modes[0] = 'L'; |
| 1188 |
ERROR ((0, 0, _("Unexpected long name header"))); |
| 1189 |
break; |
| 1190 |
|
| 1191 |
case GNUTYPE_SPARSE: |
| 1192 |
case REGTYPE: |
| 1193 |
case AREGTYPE: |
| 1194 |
modes[0] = st->had_trailing_slash ? 'd' : '-'; |
| 1195 |
break; |
| 1196 |
case LNKTYPE: |
| 1197 |
modes[0] = 'h'; |
| 1198 |
break; |
| 1199 |
case GNUTYPE_DUMPDIR: |
| 1200 |
modes[0] = 'd'; |
| 1201 |
break; |
| 1202 |
case DIRTYPE: |
| 1203 |
modes[0] = 'd'; |
| 1204 |
break; |
| 1205 |
case SYMTYPE: |
| 1206 |
modes[0] = 'l'; |
| 1207 |
break; |
| 1208 |
case BLKTYPE: |
| 1209 |
modes[0] = 'b'; |
| 1210 |
break; |
| 1211 |
case CHRTYPE: |
| 1212 |
modes[0] = 'c'; |
| 1213 |
break; |
| 1214 |
case FIFOTYPE: |
| 1215 |
modes[0] = 'p'; |
| 1216 |
break; |
| 1217 |
case CONTTYPE: |
| 1218 |
modes[0] = 'C'; |
| 1219 |
break; |
| 1220 |
} |
| 1221 |
|
| 1222 |
pax_decode_mode (st->stat.st_mode, modes + 1); |
| 1223 |
|
| 1224 |
/* extended attributes: GNU `ls -l'-like preview */ |
| 1225 |
xattrs_print_char (st, modes + 10); |
| 1226 |
|
| 1227 |
/* Time stamp. */ |
| 1228 |
|
| 1229 |
time_stamp = tartime (st->mtime, full_time_option); |
| 1230 |
time_stamp_len = strlen (time_stamp); |
| 1231 |
if (datewidth < time_stamp_len) |
| 1232 |
datewidth = time_stamp_len; |
| 1233 |
|
| 1234 |
/* User and group names. */ |
| 1235 |
|
| 1236 |
if (st->uname |
| 1237 |
&& st->uname[0] |
| 1238 |
&& current_format != V7_FORMAT |
| 1239 |
&& !numeric_owner_option) |
| 1240 |
user = st->uname; |
| 1241 |
else |
| 1242 |
user = STRINGIFY_BIGINT (st->stat.st_uid, uform); |
| 1243 |
|
| 1244 |
if (st->gname |
| 1245 |
&& st->gname[0] |
| 1246 |
&& current_format != V7_FORMAT |
| 1247 |
&& !numeric_owner_option) |
| 1248 |
group = st->gname; |
| 1249 |
else |
| 1250 |
group = STRINGIFY_BIGINT (st->stat.st_gid, gform); |
| 1251 |
|
| 1252 |
/* Format the file size or major/minor device numbers. */ |
| 1253 |
|
| 1254 |
switch (blk->header.typeflag) |
| 1255 |
{ |
| 1256 |
case CHRTYPE: |
| 1257 |
case BLKTYPE: |
| 1258 |
strcpy (size, |
| 1259 |
STRINGIFY_BIGINT (major (st->stat.st_rdev), uintbuf)); |
| 1260 |
strcat (size, ","); |
| 1261 |
strcat (size, |
| 1262 |
STRINGIFY_BIGINT (minor (st->stat.st_rdev), uintbuf)); |
| 1263 |
break; |
| 1264 |
|
| 1265 |
default: |
| 1266 |
/* st->stat.st_size keeps stored file size */ |
| 1267 |
strcpy (size, STRINGIFY_BIGINT (st->stat.st_size, uintbuf)); |
| 1268 |
break; |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* Figure out padding and print the whole line. */ |
| 1272 |
|
| 1273 |
sizelen = strlen (size); |
| 1274 |
pad = strlen (user) + 1 + strlen (group) + 1 + sizelen; |
| 1275 |
if (pad > ugswidth) |
| 1276 |
ugswidth = pad; |
| 1277 |
|
| 1278 |
fprintf (stdlis, "%s %s/%s %*s %-*s", |
| 1279 |
modes, user, group, ugswidth - pad + sizelen, size, |
| 1280 |
datewidth, time_stamp); |
| 1281 |
|
| 1282 |
fprintf (stdlis, " %s", quotearg (temp_name)); |
| 1283 |
if (show_transformed_names_option && st->had_trailing_slash) |
| 1284 |
fputc ('/', stdlis); |
| 1285 |
|
| 1286 |
switch (blk->header.typeflag) |
| 1287 |
{ |
| 1288 |
case SYMTYPE: |
| 1289 |
fprintf (stdlis, " -> %s\n", quotearg (st->link_name)); |
| 1290 |
break; |
| 1291 |
|
| 1292 |
case LNKTYPE: |
| 1293 |
fprintf (stdlis, _(" link to %s\n"), quotearg (st->link_name)); |
| 1294 |
break; |
| 1295 |
|
| 1296 |
default: |
| 1297 |
{ |
| 1298 |
char type_string[2]; |
| 1299 |
type_string[0] = blk->header.typeflag; |
| 1300 |
type_string[1] = '\0'; |
| 1301 |
fprintf (stdlis, _(" unknown file type %s\n"), |
| 1302 |
quote (type_string)); |
| 1303 |
} |
| 1304 |
break; |
| 1305 |
|
| 1306 |
case AREGTYPE: |
| 1307 |
case REGTYPE: |
| 1308 |
case GNUTYPE_SPARSE: |
| 1309 |
case CHRTYPE: |
| 1310 |
case BLKTYPE: |
| 1311 |
case DIRTYPE: |
| 1312 |
case FIFOTYPE: |
| 1313 |
case CONTTYPE: |
| 1314 |
case GNUTYPE_DUMPDIR: |
| 1315 |
putc ('\n', stdlis); |
| 1316 |
break; |
| 1317 |
|
| 1318 |
case GNUTYPE_LONGLINK: |
| 1319 |
fprintf (stdlis, _("--Long Link--\n")); |
| 1320 |
break; |
| 1321 |
|
| 1322 |
case GNUTYPE_LONGNAME: |
| 1323 |
fprintf (stdlis, _("--Long Name--\n")); |
| 1324 |
break; |
| 1325 |
|
| 1326 |
case GNUTYPE_VOLHDR: |
| 1327 |
fprintf (stdlis, _("--Volume Header--\n")); |
| 1328 |
break; |
| 1329 |
|
| 1330 |
case GNUTYPE_MULTIVOL: |
| 1331 |
strcpy (size, |
| 1332 |
STRINGIFY_BIGINT |
| 1333 |
(UINTMAX_FROM_HEADER (blk->oldgnu_header.offset), |
| 1334 |
uintbuf)); |
| 1335 |
fprintf (stdlis, _("--Continued at byte %s--\n"), size); |
| 1336 |
break; |
| 1337 |
} |
| 1338 |
} |
| 1339 |
fflush (stdlis); |
| 1340 |
xattrs_print (st); |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
static void |
| 1345 |
print_volume_label (void) |
| 1346 |
{ |
| 1347 |
struct tar_stat_info vstat; |
| 1348 |
union block vblk; |
| 1349 |
enum archive_format dummy; |
| 1350 |
|
| 1351 |
memset (&vblk, 0, sizeof (vblk)); |
| 1352 |
vblk.header.typeflag = GNUTYPE_VOLHDR; |
| 1353 |
if (recent_global_header) |
| 1354 |
memcpy (vblk.header.mtime, recent_global_header->header.mtime, |
| 1355 |
sizeof vblk.header.mtime); |
| 1356 |
tar_stat_init (&vstat); |
| 1357 |
assign_string (&vstat.file_name, "."); |
| 1358 |
decode_header (&vblk, &vstat, &dummy, 0); |
| 1359 |
assign_string (&vstat.file_name, volume_label); |
| 1360 |
simple_print_header (&vstat, &vblk, 0); |
| 1361 |
tar_stat_destroy (&vstat); |
| 1362 |
} |
| 1363 |
|
| 1364 |
void |
| 1365 |
print_header (struct tar_stat_info *st, union block *blk, |
| 1366 |
off_t block_ordinal) |
| 1367 |
{ |
| 1368 |
if (current_format == POSIX_FORMAT && !volume_label_printed && volume_label) |
| 1369 |
{ |
| 1370 |
print_volume_label (); |
| 1371 |
volume_label_printed = true; |
| 1372 |
} |
| 1373 |
|
| 1374 |
simple_print_header (st, blk, block_ordinal); |
| 1375 |
} |
| 1376 |
|
| 1377 |
/* Print a similar line when we make a directory automatically. */ |
| 1378 |
void |
| 1379 |
print_for_mkdir (char *dirname, mode_t mode) |
| 1380 |
{ |
| 1381 |
char modes[11]; |
| 1382 |
|
| 1383 |
if (verbose_option > 1) |
| 1384 |
{ |
| 1385 |
/* File type and modes. */ |
| 1386 |
|
| 1387 |
modes[0] = 'd'; |
| 1388 |
pax_decode_mode (mode, modes + 1); |
| 1389 |
|
| 1390 |
if (block_number_option) |
| 1391 |
{ |
| 1392 |
char buf[UINTMAX_STRSIZE_BOUND]; |
| 1393 |
fprintf (stdlis, _("block %s: "), |
| 1394 |
STRINGIFY_BIGINT (current_block_ordinal (), buf)); |
| 1395 |
} |
| 1396 |
|
| 1397 |
fprintf (stdlis, "%s %*s %s\n", modes, ugswidth + 1 + datewidth, |
| 1398 |
_("Creating directory:"), quotearg (dirname)); |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
/* Skip over SIZE bytes of data in blocks in the archive. |
| 1403 |
This may involve copying the data. |
| 1404 |
If MUST_COPY, always copy instead of skipping. */ |
| 1405 |
void |
| 1406 |
skim_file (off_t size, bool must_copy) |
| 1407 |
{ |
| 1408 |
union block *x; |
| 1409 |
|
| 1410 |
/* FIXME: Make sure mv_begin_read is always called before it */ |
| 1411 |
|
| 1412 |
if (seekable_archive && !must_copy) |
| 1413 |
{ |
| 1414 |
off_t nblk = seek_archive (size); |
| 1415 |
if (nblk >= 0) |
| 1416 |
size -= nblk * BLOCKSIZE; |
| 1417 |
else |
| 1418 |
seekable_archive = false; |
| 1419 |
} |
| 1420 |
|
| 1421 |
mv_size_left (size); |
| 1422 |
|
| 1423 |
while (size > 0) |
| 1424 |
{ |
| 1425 |
x = find_next_block (); |
| 1426 |
if (! x) |
| 1427 |
FATAL_ERROR ((0, 0, _("Unexpected EOF in archive"))); |
| 1428 |
|
| 1429 |
set_next_block_after (x); |
| 1430 |
size -= BLOCKSIZE; |
| 1431 |
mv_size_left (size); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
/* Skip the current member in the archive. |
| 1436 |
NOTE: Current header must be decoded before calling this function. */ |
| 1437 |
void |
| 1438 |
skip_member (void) |
| 1439 |
{ |
| 1440 |
skim_member (false); |
| 1441 |
} |
| 1442 |
|
| 1443 |
/* Skip the current member in the archive. |
| 1444 |
If MUST_COPY, always copy instead of skipping. */ |
| 1445 |
void |
| 1446 |
skim_member (bool must_copy) |
| 1447 |
{ |
| 1448 |
if (!current_stat_info.skipped) |
| 1449 |
{ |
| 1450 |
char save_typeflag = current_header->header.typeflag; |
| 1451 |
set_next_block_after (current_header); |
| 1452 |
|
| 1453 |
mv_begin_read (¤t_stat_info); |
| 1454 |
|
| 1455 |
if (current_stat_info.is_sparse) |
| 1456 |
sparse_skim_file (¤t_stat_info, must_copy); |
| 1457 |
else if (save_typeflag != DIRTYPE) |
| 1458 |
skim_file (current_stat_info.stat.st_size, must_copy); |
| 1459 |
|
| 1460 |
mv_end (); |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
void |
| 1465 |
test_archive_label (void) |
| 1466 |
{ |
| 1467 |
base64_init (); |
| 1468 |
name_gather (); |
| 1469 |
|
| 1470 |
open_archive (ACCESS_READ); |
| 1471 |
if (read_header (¤t_header, ¤t_stat_info, read_header_auto) |
| 1472 |
== HEADER_SUCCESS) |
| 1473 |
{ |
| 1474 |
decode_header (current_header, |
| 1475 |
¤t_stat_info, ¤t_format, 0); |
| 1476 |
if (current_header->header.typeflag == GNUTYPE_VOLHDR) |
| 1477 |
ASSIGN_STRING_N (&volume_label, current_header->header.name); |
| 1478 |
|
| 1479 |
if (volume_label) |
| 1480 |
{ |
| 1481 |
if (verbose_option) |
| 1482 |
print_volume_label (); |
| 1483 |
if (!name_match (volume_label) && multi_volume_option) |
| 1484 |
{ |
| 1485 |
char *s = drop_volume_label_suffix (volume_label); |
| 1486 |
name_match (s); |
| 1487 |
free (s); |
| 1488 |
} |
| 1489 |
} |
| 1490 |
} |
| 1491 |
close_archive (); |
| 1492 |
label_notfound (); |
| 1493 |
} |