list.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /* List a tar archive, with support routines for reading a tar archive.
  2. Copyright 1988,92,93,94,96,97,98,1999 Free Software Foundation, Inc.
  3. Written by John Gilmore, on 1985-08-26.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Define to non-zero for forcing old ctime format instead of ISO format. */
  16. #undef USE_OLD_CTIME
  17. #include "system.h"
  18. #include <quotearg.h>
  19. #include <time.h>
  20. #ifndef time
  21. time_t time ();
  22. #endif
  23. #include "common.h"
  24. #define max(a, b) ((a) < (b) ? (b) : (a))
  25. union block *current_header; /* points to current archive header */
  26. struct stat current_stat; /* stat struct corresponding */
  27. enum archive_format current_format; /* recognized format */
  28. static uintmax_t from_header PARAMS ((const char *, size_t, const char *,
  29. uintmax_t, uintmax_t));
  30. /* Base 64 digits; see Internet RFC 2045 Table 1. */
  31. static char const base_64_digits[64] =
  32. {
  33. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  34. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  35. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  36. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  37. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
  38. };
  39. /* Table of base-64 digit values indexed by unsigned chars.
  40. The value is 64 for unsigned chars that are not base-64 digits. */
  41. static char base64_map[UCHAR_MAX + 1];
  42. static void
  43. base64_init (void)
  44. {
  45. int i;
  46. memset (base64_map, 64, sizeof base64_map);
  47. for (i = 0; i < 64; i++)
  48. base64_map[(int) base_64_digits[i]] = i;
  49. }
  50. /*-----------------------------------.
  51. | Main loop for reading an archive. |
  52. `-----------------------------------*/
  53. void
  54. read_and (void (*do_something) ())
  55. {
  56. enum read_header status = HEADER_STILL_UNREAD;
  57. enum read_header prev_status;
  58. base64_init ();
  59. name_gather ();
  60. open_archive (ACCESS_READ);
  61. while (1)
  62. {
  63. prev_status = status;
  64. status = read_header ();
  65. switch (status)
  66. {
  67. case HEADER_STILL_UNREAD:
  68. abort ();
  69. case HEADER_SUCCESS:
  70. /* Valid header. We should decode next field (mode) first.
  71. Ensure incoming names are null terminated. */
  72. if (! name_match (current_file_name)
  73. || (newer_mtime_option != TYPE_MINIMUM (time_t)
  74. /* FIXME: We get mtime now, and again later; this causes
  75. duplicate diagnostics if header.mtime is bogus. */
  76. && ((current_stat.st_mtime
  77. = TIME_FROM_HEADER (current_header->header.mtime))
  78. < newer_mtime_option))
  79. || excluded_name (current_file_name))
  80. {
  81. char save_typeflag = current_header->header.typeflag;
  82. switch (save_typeflag)
  83. {
  84. case GNUTYPE_VOLHDR:
  85. case GNUTYPE_MULTIVOL:
  86. case GNUTYPE_NAMES:
  87. break;
  88. case DIRTYPE:
  89. if (show_omitted_dirs_option)
  90. WARN ((0, 0, _("%s: Omitting"),
  91. quotearg_colon (current_file_name)));
  92. /* Fall through. */
  93. default:
  94. /* Skip past it in the archive. */
  95. set_next_block_after (current_header);
  96. if (current_header->oldgnu_header.isextended)
  97. skip_extended_headers ();
  98. /* Skip to the next header on the archive. */
  99. if (save_typeflag != DIRTYPE)
  100. skip_file (current_stat.st_size);
  101. continue;
  102. }
  103. }
  104. apply_nonancestor_delayed_set_stat (current_file_name);
  105. (*do_something) ();
  106. continue;
  107. case HEADER_ZERO_BLOCK:
  108. if (block_number_option)
  109. {
  110. char buf[UINTMAX_STRSIZE_BOUND];
  111. fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
  112. STRINGIFY_BIGINT (current_block_ordinal (), buf));
  113. }
  114. set_next_block_after (current_header);
  115. status = prev_status;
  116. if (ignore_zeros_option)
  117. continue;
  118. break;
  119. case HEADER_END_OF_FILE:
  120. if (block_number_option)
  121. {
  122. char buf[UINTMAX_STRSIZE_BOUND];
  123. fprintf (stdlis, _("block %s: ** End of File **\n"),
  124. STRINGIFY_BIGINT (current_block_ordinal (), buf));
  125. }
  126. break;
  127. case HEADER_FAILURE:
  128. /* If the previous header was good, tell them that we are
  129. skipping bad ones. */
  130. set_next_block_after (current_header);
  131. switch (prev_status)
  132. {
  133. case HEADER_STILL_UNREAD:
  134. ERROR ((0, 0, _("This does not look like a tar archive")));
  135. /* Fall through. */
  136. case HEADER_ZERO_BLOCK:
  137. case HEADER_SUCCESS:
  138. ERROR ((0, 0, _("Skipping to next header")));
  139. break;
  140. case HEADER_END_OF_FILE:
  141. case HEADER_FAILURE:
  142. /* We are in the middle of a cascade of errors. */
  143. break;
  144. }
  145. continue;
  146. }
  147. break;
  148. }
  149. apply_delayed_set_stat ();
  150. close_archive ();
  151. names_notfound (); /* print names not found */
  152. }
  153. /*---------------------------------------------.
  154. | Print a header block, based on tar options. |
  155. `---------------------------------------------*/
  156. void
  157. list_archive (void)
  158. {
  159. /* Print the header block. */
  160. if (verbose_option)
  161. {
  162. if (verbose_option > 1)
  163. decode_header (current_header, &current_stat, &current_format, 0);
  164. print_header ();
  165. }
  166. if (incremental_option && current_header->header.typeflag == GNUTYPE_DUMPDIR)
  167. {
  168. off_t size;
  169. size_t written, check;
  170. union block *data_block;
  171. set_next_block_after (current_header);
  172. if (multi_volume_option)
  173. {
  174. assign_string (&save_name, current_file_name);
  175. save_totsize = current_stat.st_size;
  176. }
  177. for (size = current_stat.st_size; size > 0; size -= written)
  178. {
  179. if (multi_volume_option)
  180. save_sizeleft = size;
  181. data_block = find_next_block ();
  182. if (!data_block)
  183. {
  184. ERROR ((0, 0, _("Unexpected EOF in archive")));
  185. break; /* FIXME: What happens, then? */
  186. }
  187. written = available_space_after (data_block);
  188. if (written > size)
  189. written = size;
  190. errno = 0;
  191. check = fwrite (data_block->buffer, sizeof (char), written, stdlis);
  192. set_next_block_after ((union block *)
  193. (data_block->buffer + written - 1));
  194. if (check != written)
  195. {
  196. write_error_details (current_file_name, check, written);
  197. skip_file (size - written);
  198. break;
  199. }
  200. }
  201. if (multi_volume_option)
  202. assign_string (&save_name, 0);
  203. fputc ('\n', stdlis);
  204. fflush (stdlis);
  205. return;
  206. }
  207. /* Skip past the header in the archive, and past any extended headers. */
  208. set_next_block_after (current_header);
  209. if (current_header->oldgnu_header.isextended)
  210. skip_extended_headers ();
  211. if (multi_volume_option)
  212. assign_string (&save_name, current_file_name);
  213. /* Skip to the next header on the archive. */
  214. skip_file (current_stat.st_size);
  215. if (multi_volume_option)
  216. assign_string (&save_name, 0);
  217. }
  218. /*-----------------------------------------------------------------------.
  219. | Read a block that's supposed to be a header block. Return its address |
  220. | in "current_header", and if it is good, the file's size in |
  221. | current_stat.st_size. |
  222. | |
  223. | Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a |
  224. | block full of zeros (EOF marker). |
  225. | |
  226. | You must always set_next_block_after(current_header) to skip past the |
  227. | header which this routine reads. |
  228. `-----------------------------------------------------------------------*/
  229. /* The standard BSD tar sources create the checksum by adding up the
  230. bytes in the header as type char. I think the type char was unsigned
  231. on the PDP-11, but it's signed on the Next and Sun. It looks like the
  232. sources to BSD tar were never changed to compute the checksum
  233. correctly, so both the Sun and Next add the bytes of the header as
  234. signed chars. This doesn't cause a problem until you get a file with
  235. a name containing characters with the high bit set. So read_header
  236. computes two checksums -- signed and unsigned. */
  237. enum read_header
  238. read_header (void)
  239. {
  240. size_t i;
  241. int unsigned_sum; /* the POSIX one :-) */
  242. int signed_sum; /* the Sun one :-( */
  243. int recorded_sum;
  244. uintmax_t parsed_sum;
  245. char *p;
  246. union block *header;
  247. char **longp;
  248. char *bp;
  249. union block *data_block;
  250. size_t size, written;
  251. static char *next_long_name, *next_long_link;
  252. while (1)
  253. {
  254. header = find_next_block ();
  255. current_header = header;
  256. if (!header)
  257. return HEADER_END_OF_FILE;
  258. unsigned_sum = 0;
  259. signed_sum = 0;
  260. p = header->buffer;
  261. for (i = sizeof *header; i-- != 0;)
  262. {
  263. unsigned_sum += (unsigned char) *p;
  264. signed_sum += signed_char (*p++);
  265. }
  266. if (unsigned_sum == 0)
  267. return HEADER_ZERO_BLOCK;
  268. /* Adjust checksum to count the "chksum" field as blanks. */
  269. for (i = sizeof header->header.chksum; i-- != 0;)
  270. {
  271. unsigned_sum -= (unsigned char) header->header.chksum[i];
  272. signed_sum -= signed_char (header->header.chksum[i]);
  273. }
  274. unsigned_sum += ' ' * sizeof header->header.chksum;
  275. signed_sum += ' ' * sizeof header->header.chksum;
  276. parsed_sum = from_header (header->header.chksum,
  277. sizeof header->header.chksum, 0,
  278. (uintmax_t) 0,
  279. (uintmax_t) TYPE_MAXIMUM (int));
  280. if (parsed_sum == (uintmax_t) -1)
  281. return HEADER_FAILURE;
  282. recorded_sum = parsed_sum;
  283. if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
  284. return HEADER_FAILURE;
  285. /* Good block. Decode file size and return. */
  286. if (header->header.typeflag == LNKTYPE)
  287. current_stat.st_size = 0; /* links 0 size on tape */
  288. else
  289. current_stat.st_size = OFF_FROM_HEADER (header->header.size);
  290. if (header->header.typeflag == GNUTYPE_LONGNAME
  291. || header->header.typeflag == GNUTYPE_LONGLINK)
  292. {
  293. longp = ((header->header.typeflag == GNUTYPE_LONGNAME)
  294. ? &next_long_name
  295. : &next_long_link);
  296. set_next_block_after (header);
  297. if (*longp)
  298. free (*longp);
  299. size = current_stat.st_size;
  300. if (size != current_stat.st_size)
  301. xalloc_die ();
  302. bp = *longp = xmalloc (size);
  303. for (; size > 0; size -= written)
  304. {
  305. data_block = find_next_block ();
  306. if (! data_block)
  307. {
  308. ERROR ((0, 0, _("Unexpected EOF in archive")));
  309. break;
  310. }
  311. written = available_space_after (data_block);
  312. if (written > size)
  313. written = size;
  314. memcpy (bp, data_block->buffer, written);
  315. bp += written;
  316. set_next_block_after ((union block *)
  317. (data_block->buffer + written - 1));
  318. }
  319. /* Loop! */
  320. }
  321. else
  322. {
  323. char *name;
  324. struct posix_header *h = &current_header->header;
  325. char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
  326. name = next_long_name;
  327. if (! name)
  328. {
  329. /* Accept file names as specified by POSIX.1-1996
  330. section 10.1.1. */
  331. int posix_header = strcmp (h->magic, TMAGIC) == 0;
  332. char *np = namebuf;
  333. if (posix_header && h->prefix[0])
  334. {
  335. memcpy (np, h->prefix, sizeof h->prefix);
  336. np[sizeof h->prefix] = '\0';
  337. np += strlen (np);
  338. *np++ = '/';
  339. }
  340. memcpy (np, h->name, sizeof h->name);
  341. np[sizeof h->name] = '\0';
  342. name = namebuf;
  343. }
  344. assign_string (&current_file_name, name);
  345. if (next_long_name)
  346. {
  347. free (next_long_name);
  348. next_long_name = 0;
  349. }
  350. name = next_long_link;
  351. if (! name)
  352. {
  353. memcpy (namebuf, h->linkname, sizeof h->linkname);
  354. namebuf[sizeof h->linkname] = '\0';
  355. name = namebuf;
  356. }
  357. assign_string (&current_link_name, name);
  358. if (next_long_link)
  359. {
  360. free (next_long_link);
  361. next_long_link = 0;
  362. }
  363. return HEADER_SUCCESS;
  364. }
  365. }
  366. }
  367. /*-------------------------------------------------------------------------.
  368. | Decode things from a file HEADER block into STAT_INFO, also setting |
  369. | *FORMAT_POINTER depending on the header block format. If DO_USER_GROUP, |
  370. | decode the user/group information (this is useful for extraction, but |
  371. | waste time when merely listing). |
  372. | |
  373. | read_header() has already decoded the checksum and length, so we don't. |
  374. | |
  375. | This routine should *not* be called twice for the same block, since the |
  376. | two calls might use different DO_USER_GROUP values and thus might end up |
  377. | with different uid/gid for the two calls. If anybody wants the uid/gid |
  378. | they should decode it first, and other callers should decode it without |
  379. | uid/gid before calling a routine, e.g. print_header, that assumes |
  380. | decoded data. |
  381. `-------------------------------------------------------------------------*/
  382. void
  383. decode_header (union block *header, struct stat *stat_info,
  384. enum archive_format *format_pointer, int do_user_group)
  385. {
  386. enum archive_format format;
  387. if (strcmp (header->header.magic, TMAGIC) == 0)
  388. format = POSIX_FORMAT;
  389. else if (strcmp (header->header.magic, OLDGNU_MAGIC) == 0)
  390. format = OLDGNU_FORMAT;
  391. else
  392. format = V7_FORMAT;
  393. *format_pointer = format;
  394. stat_info->st_mode = MODE_FROM_HEADER (header->header.mode);
  395. stat_info->st_mtime = TIME_FROM_HEADER (header->header.mtime);
  396. if (format == OLDGNU_FORMAT && incremental_option)
  397. {
  398. stat_info->st_atime = TIME_FROM_HEADER (header->oldgnu_header.atime);
  399. stat_info->st_ctime = TIME_FROM_HEADER (header->oldgnu_header.ctime);
  400. }
  401. if (format == V7_FORMAT)
  402. {
  403. stat_info->st_uid = UID_FROM_HEADER (header->header.uid);
  404. stat_info->st_gid = GID_FROM_HEADER (header->header.gid);
  405. stat_info->st_rdev = 0;
  406. }
  407. else
  408. {
  409. if (do_user_group)
  410. {
  411. /* FIXME: Decide if this should somewhat depend on -p. */
  412. if (numeric_owner_option
  413. || !*header->header.uname
  414. || !uname_to_uid (header->header.uname, &stat_info->st_uid))
  415. stat_info->st_uid = UID_FROM_HEADER (header->header.uid);
  416. if (numeric_owner_option
  417. || !*header->header.gname
  418. || !gname_to_gid (header->header.gname, &stat_info->st_gid))
  419. stat_info->st_gid = GID_FROM_HEADER (header->header.gid);
  420. }
  421. switch (header->header.typeflag)
  422. {
  423. case BLKTYPE:
  424. stat_info->st_rdev
  425. = makedev (MAJOR_FROM_HEADER (header->header.devmajor),
  426. MINOR_FROM_HEADER (header->header.devminor));
  427. break;
  428. case CHRTYPE:
  429. stat_info->st_rdev
  430. = makedev (MAJOR_FROM_HEADER (header->header.devmajor),
  431. MINOR_FROM_HEADER (header->header.devminor));
  432. break;
  433. default:
  434. stat_info->st_rdev = 0;
  435. }
  436. }
  437. }
  438. /*------------------------------------------------------------------------.
  439. | Convert buffer at WHERE0 of size DIGS from external format to uintmax_t.|
  440. | The data is of type TYPE. The buffer must represent a value in the |
  441. | range -MINUS_MINVAL through MAXVAL. DIGS must be positive. |
  442. `------------------------------------------------------------------------*/
  443. static uintmax_t
  444. from_header (char const *where0, size_t digs, char const *type,
  445. uintmax_t minus_minval, uintmax_t maxval)
  446. {
  447. uintmax_t value;
  448. char const *where = where0;
  449. char const *lim = where + digs;
  450. int negative = 0;
  451. /* Accommodate buggy tar of unknown vintage, which outputs leading
  452. NUL if the previous field overflows. */
  453. where += !*where;
  454. /* Accommodate older tars, which output leading spaces. */
  455. for (;;)
  456. {
  457. if (where == lim)
  458. {
  459. if (type)
  460. ERROR ((0, 0,
  461. _("Blanks in header where numeric %s value expected"),
  462. type));
  463. return -1;
  464. }
  465. if (!ISSPACE ((unsigned char) *where))
  466. break;
  467. where++;
  468. }
  469. value = 0;
  470. if (ISODIGIT (*where))
  471. {
  472. char const *where1 = where;
  473. uintmax_t overflow = 0;
  474. for (;;)
  475. {
  476. value += *where++ - '0';
  477. if (where == lim || ! ISODIGIT (*where))
  478. break;
  479. overflow |= value ^ (value << LG_8 >> LG_8);
  480. value <<= LG_8;
  481. }
  482. /* Parse the output of older, unportable tars, which generate
  483. negative values in two's complement octal. If the leading
  484. nonzero digit is 1, we can't recover the original value
  485. reliably; so do this only if the digit is 2 or more. This
  486. catches the common case of 32-bit negative time stamps. */
  487. if ((overflow || maxval < value) && '2' <= *where1)
  488. {
  489. /* Compute the negative of the input value, assuming two's
  490. complement. */
  491. int digit = (*where1 - '0') | 4;
  492. overflow = 0;
  493. value = 0;
  494. where = where1;
  495. for (;;)
  496. {
  497. value += 7 - digit;
  498. where++;
  499. if (where == lim || ! ISODIGIT (*where))
  500. break;
  501. digit = *where - '0';
  502. overflow |= value ^ (value << LG_8 >> LG_8);
  503. value <<= LG_8;
  504. }
  505. value++;
  506. overflow |= !value;
  507. if (!overflow && value <= minus_minval)
  508. {
  509. WARN ((0, 0,
  510. _("Archive octal value %.*s is out of %s range; assuming two's complement"),
  511. (int) (where - where1), where1, type));
  512. negative = 1;
  513. }
  514. }
  515. if (overflow)
  516. {
  517. ERROR ((0, 0,
  518. _("Archive octal value %.*s is out of %s range"),
  519. (int) (where - where1), where1, type));
  520. return -1;
  521. }
  522. }
  523. else if (type)
  524. {
  525. /* The following forms cannot appear as checksums, so we don't
  526. check for them if TYPE is null. */
  527. if (*where == '-' || *where == '+')
  528. {
  529. /* Parse base-64 output produced only by tar test versions
  530. 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
  531. Support for this will be withdrawn in future releases. */
  532. int dig;
  533. static int warned_once;
  534. if (! warned_once)
  535. {
  536. warned_once = 1;
  537. WARN ((0, 0,
  538. _("Archive contains obsolescent base-64 headers")));
  539. }
  540. negative = *where++ == '-';
  541. while (where != lim
  542. && (dig = base64_map[(unsigned char) *where]) < 64)
  543. {
  544. if (value << LG_64 >> LG_64 != value)
  545. {
  546. ERROR ((0, 0,
  547. _("Archive signed base-64 string `%.*s' is out of %s range"),
  548. (int) digs, where0, type));
  549. return -1;
  550. }
  551. value = (value << LG_64) | dig;
  552. where++;
  553. }
  554. }
  555. else if (*where == '\200' /* positive base-256 */
  556. || *where == '\377' /* negative base-256 */)
  557. {
  558. /* Parse base-256 output. A nonnegative number N is
  559. represented as (256**DIGS)/2 + N; a negative number -N is
  560. represented as (256**DIGS) - N, i.e. as two's complement.
  561. The representation guarantees that the leading bit is
  562. always on, so that we don't confuse this format with the
  563. others (assuming ASCII bytes of 8 bits or more). */
  564. int signbit = *where & (1 << (LG_256 - 2));
  565. uintmax_t topbits = (((uintmax_t) - signbit)
  566. << (CHAR_BIT * sizeof (uintmax_t)
  567. - LG_256 - (LG_256 - 2)));
  568. value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
  569. for (;;)
  570. {
  571. value = (value << LG_256) + (unsigned char) *where++;
  572. if (where == lim)
  573. break;
  574. if (((value << LG_256 >> LG_256) | topbits) != value)
  575. {
  576. ERROR ((0, 0,
  577. _("Archive base-256 value is out of %s range"),
  578. type));
  579. return -1;
  580. }
  581. }
  582. negative = signbit;
  583. if (negative)
  584. value = -value;
  585. }
  586. }
  587. if (where != lim && *where && !ISSPACE ((unsigned char) *where))
  588. {
  589. if (type)
  590. {
  591. char buf[1000]; /* Big enough to represent any header. */
  592. static struct quoting_options *o;
  593. if (!o)
  594. {
  595. o = clone_quoting_options (0);
  596. set_quoting_style (o, locale_quoting_style);
  597. }
  598. while (where0 != lim && ! lim[-1])
  599. lim--;
  600. quotearg_buffer (buf, sizeof buf, where0, lim - where, o);
  601. ERROR ((0, 0,
  602. _("Archive contains %.*s where numeric %s value expected"),
  603. (int) sizeof buf, buf, type));
  604. }
  605. return -1;
  606. }
  607. if (value <= (negative ? minus_minval : maxval))
  608. return negative ? -value : value;
  609. if (type)
  610. {
  611. char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
  612. char maxval_buf[UINTMAX_STRSIZE_BOUND];
  613. char value_buf[UINTMAX_STRSIZE_BOUND + 1];
  614. char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
  615. char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
  616. if (negative)
  617. *--value_string = '-';
  618. if (minus_minval)
  619. *--minval_string = '-';
  620. ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
  621. value_string, type,
  622. minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
  623. }
  624. return -1;
  625. }
  626. gid_t
  627. gid_from_header (const char *p, size_t s)
  628. {
  629. return from_header (p, s, "gid_t",
  630. - (uintmax_t) TYPE_MINIMUM (gid_t),
  631. (uintmax_t) TYPE_MAXIMUM (gid_t));
  632. }
  633. major_t
  634. major_from_header (const char *p, size_t s)
  635. {
  636. return from_header (p, s, "major_t",
  637. - (uintmax_t) TYPE_MINIMUM (major_t),
  638. (uintmax_t) TYPE_MAXIMUM (major_t));
  639. }
  640. minor_t
  641. minor_from_header (const char *p, size_t s)
  642. {
  643. return from_header (p, s, "minor_t",
  644. - (uintmax_t) TYPE_MINIMUM (minor_t),
  645. (uintmax_t) TYPE_MAXIMUM (minor_t));
  646. }
  647. mode_t
  648. mode_from_header (const char *p, size_t s)
  649. {
  650. /* Do not complain about unrecognized mode bits. */
  651. unsigned u = from_header (p, s, "mode_t",
  652. - (uintmax_t) TYPE_MINIMUM (mode_t),
  653. TYPE_MAXIMUM (uintmax_t));
  654. return ((u & TSUID ? S_ISUID : 0)
  655. | (u & TSGID ? S_ISGID : 0)
  656. | (u & TSVTX ? S_ISVTX : 0)
  657. | (u & TUREAD ? S_IRUSR : 0)
  658. | (u & TUWRITE ? S_IWUSR : 0)
  659. | (u & TUEXEC ? S_IXUSR : 0)
  660. | (u & TGREAD ? S_IRGRP : 0)
  661. | (u & TGWRITE ? S_IWGRP : 0)
  662. | (u & TGEXEC ? S_IXGRP : 0)
  663. | (u & TOREAD ? S_IROTH : 0)
  664. | (u & TOWRITE ? S_IWOTH : 0)
  665. | (u & TOEXEC ? S_IXOTH : 0));
  666. }
  667. off_t
  668. off_from_header (const char *p, size_t s)
  669. {
  670. /* Negative offsets are not allowed in tar files, so invoke
  671. from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
  672. return from_header (p, s, "off_t", (uintmax_t) 0,
  673. (uintmax_t) TYPE_MAXIMUM (off_t));
  674. }
  675. size_t
  676. size_from_header (const char *p, size_t s)
  677. {
  678. return from_header (p, s, "size_t", (uintmax_t) 0,
  679. (uintmax_t) TYPE_MAXIMUM (size_t));
  680. }
  681. time_t
  682. time_from_header (const char *p, size_t s)
  683. {
  684. time_t t = from_header (p, s, "time_t",
  685. - (uintmax_t) TYPE_MINIMUM (time_t),
  686. (uintmax_t) TYPE_MAXIMUM (time_t));
  687. if (start_time < t && time (0) < t)
  688. WARN ((0, 0, _("Archive contains future timestamp %s"), tartime (t)));
  689. return t;
  690. }
  691. uid_t
  692. uid_from_header (const char *p, size_t s)
  693. {
  694. return from_header (p, s, "uid_t",
  695. - (uintmax_t) TYPE_MINIMUM (uid_t),
  696. (uintmax_t) TYPE_MAXIMUM (uid_t));
  697. }
  698. uintmax_t
  699. uintmax_from_header (const char *p, size_t s)
  700. {
  701. return from_header (p, s, "uintmax_t", (uintmax_t) 0,
  702. TYPE_MAXIMUM (uintmax_t));
  703. }
  704. /*----------------------------------------------------------------------.
  705. | Format O as a null-terminated decimal string into BUF _backwards_; |
  706. | return pointer to start of result. |
  707. `----------------------------------------------------------------------*/
  708. char *
  709. stringify_uintmax_t_backwards (uintmax_t o, char *buf)
  710. {
  711. *--buf = '\0';
  712. do
  713. *--buf = '0' + (int) (o % 10);
  714. while ((o /= 10) != 0);
  715. return buf;
  716. }
  717. /* Return a printable representation of T. The result points to
  718. static storage that can be reused in the next call to this
  719. function, to ctime, or to asctime. */
  720. char const *
  721. tartime (time_t t)
  722. {
  723. static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
  724. INT_STRLEN_BOUND (int) + 16)];
  725. char *p;
  726. #if USE_OLD_CTIME
  727. p = ctime (&t);
  728. if (p)
  729. {
  730. char const *time_stamp = p + 4;
  731. for (p += 16; p[4] != '\n'; p++)
  732. p[0] = p[4];
  733. p[0] = '\0';
  734. return time_stamp;
  735. }
  736. #else
  737. /* Use ISO 8610 format. See:
  738. http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
  739. struct tm *tm = localtime (&t);
  740. if (tm)
  741. {
  742. sprintf (buffer, "%04d-%02d-%02d %02d:%02d:%02d",
  743. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  744. tm->tm_hour, tm->tm_min, tm->tm_sec);
  745. return buffer;
  746. }
  747. #endif
  748. /* The time stamp cannot be broken down, most likely because it
  749. is out of range. Convert it as an integer,
  750. right-adjusted in a field with the same width as the usual
  751. 19-byte 4-year ISO time format. */
  752. p = stringify_uintmax_t_backwards (t < 0 ? - (uintmax_t) t : (uintmax_t) t,
  753. buffer + sizeof buffer);
  754. if (t < 0)
  755. *--p = '-';
  756. while (buffer + sizeof buffer - 19 - 1 < p)
  757. *--p = ' ';
  758. return p;
  759. }
  760. /*-------------------------------------------------------------------------.
  761. | Actually print it. |
  762. | |
  763. | Plain and fancy file header block logging. Non-verbose just prints the |
  764. | name, e.g. for "tar t" or "tar x". This should just contain file names, |
  765. | so it can be fed back into tar with xargs or the "-T" option. The |
  766. | verbose option can give a bunch of info, one line per file. I doubt |
  767. | anybody tries to parse its format, or if they do, they shouldn't. Unix |
  768. | tar is pretty random here anyway. |
  769. `-------------------------------------------------------------------------*/
  770. /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
  771. HEAD_STANDARD, which must be set up in advance. Not very clean... */
  772. /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
  773. columns never shift during the listing. */
  774. #define UGSWIDTH 18
  775. static int ugswidth = UGSWIDTH; /* maximum width encountered so far */
  776. /* DATEWIDTH is the number of columns taken by the date and time fields. */
  777. #if USE_OLD_CDATE
  778. # define DATEWIDTH 19
  779. #else
  780. # define DATEWIDTH 18
  781. #endif
  782. void
  783. print_header (void)
  784. {
  785. char modes[11];
  786. char const *time_stamp;
  787. /* These hold formatted ints. */
  788. char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND];
  789. char *user, *group;
  790. char size[2 * UINTMAX_STRSIZE_BOUND];
  791. /* holds formatted size or major,minor */
  792. char uintbuf[UINTMAX_STRSIZE_BOUND];
  793. int pad;
  794. if (block_number_option)
  795. {
  796. char buf[UINTMAX_STRSIZE_BOUND];
  797. fprintf (stdlis, _("block %s: "),
  798. STRINGIFY_BIGINT (current_block_ordinal (), buf));
  799. }
  800. if (verbose_option <= 1)
  801. {
  802. /* Just the fax, mam. */
  803. fprintf (stdlis, "%s\n", quotearg (current_file_name));
  804. }
  805. else
  806. {
  807. /* File type and modes. */
  808. modes[0] = '?';
  809. switch (current_header->header.typeflag)
  810. {
  811. case GNUTYPE_VOLHDR:
  812. modes[0] = 'V';
  813. break;
  814. case GNUTYPE_MULTIVOL:
  815. modes[0] = 'M';
  816. break;
  817. case GNUTYPE_NAMES:
  818. modes[0] = 'N';
  819. break;
  820. case GNUTYPE_LONGNAME:
  821. case GNUTYPE_LONGLINK:
  822. ERROR ((0, 0, _("Visible longname error")));
  823. break;
  824. case GNUTYPE_SPARSE:
  825. case REGTYPE:
  826. case AREGTYPE:
  827. case LNKTYPE:
  828. modes[0] = '-';
  829. if (current_file_name[strlen (current_file_name) - 1] == '/')
  830. modes[0] = 'd';
  831. break;
  832. case GNUTYPE_DUMPDIR:
  833. modes[0] = 'd';
  834. break;
  835. case DIRTYPE:
  836. modes[0] = 'd';
  837. break;
  838. case SYMTYPE:
  839. modes[0] = 'l';
  840. break;
  841. case BLKTYPE:
  842. modes[0] = 'b';
  843. break;
  844. case CHRTYPE:
  845. modes[0] = 'c';
  846. break;
  847. case FIFOTYPE:
  848. modes[0] = 'p';
  849. break;
  850. case CONTTYPE:
  851. modes[0] = 'C';
  852. break;
  853. }
  854. decode_mode (current_stat.st_mode, modes + 1);
  855. /* Time stamp. */
  856. time_stamp = tartime (current_stat.st_mtime);
  857. /* User and group names. */
  858. if (*current_header->header.uname && current_format != V7_FORMAT
  859. && !numeric_owner_option)
  860. user = current_header->header.uname;
  861. else
  862. user = STRINGIFY_BIGINT (UINTMAX_FROM_HEADER
  863. (current_header->header.uid),
  864. uform);
  865. if (*current_header->header.gname && current_format != V7_FORMAT
  866. && !numeric_owner_option)
  867. group = current_header->header.gname;
  868. else
  869. group = STRINGIFY_BIGINT (UINTMAX_FROM_HEADER
  870. (current_header->header.gid),
  871. gform);
  872. /* Format the file size or major/minor device numbers. */
  873. switch (current_header->header.typeflag)
  874. {
  875. case CHRTYPE:
  876. case BLKTYPE:
  877. strcpy (size,
  878. STRINGIFY_BIGINT (major (current_stat.st_rdev), uintbuf));
  879. strcat (size, ",");
  880. strcat (size,
  881. STRINGIFY_BIGINT (minor (current_stat.st_rdev), uintbuf));
  882. break;
  883. case GNUTYPE_SPARSE:
  884. strcpy (size,
  885. STRINGIFY_BIGINT
  886. (UINTMAX_FROM_HEADER (current_header
  887. ->oldgnu_header.realsize),
  888. uintbuf));
  889. break;
  890. default:
  891. strcpy (size, STRINGIFY_BIGINT (current_stat.st_size, uintbuf));
  892. break;
  893. }
  894. /* Figure out padding and print the whole line. */
  895. pad = strlen (user) + strlen (group) + strlen (size) + 1;
  896. if (pad > ugswidth)
  897. ugswidth = pad;
  898. fprintf (stdlis, "%s %s/%s %*s%s %s",
  899. modes, user, group, ugswidth - pad, "", size, time_stamp);
  900. fprintf (stdlis, " %s", quotearg (current_file_name));
  901. switch (current_header->header.typeflag)
  902. {
  903. case SYMTYPE:
  904. fprintf (stdlis, " -> %s\n", quotearg (current_link_name));
  905. break;
  906. case LNKTYPE:
  907. fprintf (stdlis, _(" link to %s\n"), quotearg (current_link_name));
  908. break;
  909. default:
  910. fprintf (stdlis, _(" unknown file type `%c'\n"),
  911. current_header->header.typeflag);
  912. break;
  913. case AREGTYPE:
  914. case REGTYPE:
  915. case GNUTYPE_SPARSE:
  916. case CHRTYPE:
  917. case BLKTYPE:
  918. case DIRTYPE:
  919. case FIFOTYPE:
  920. case CONTTYPE:
  921. case GNUTYPE_DUMPDIR:
  922. putc ('\n', stdlis);
  923. break;
  924. case GNUTYPE_VOLHDR:
  925. fprintf (stdlis, _("--Volume Header--\n"));
  926. break;
  927. case GNUTYPE_MULTIVOL:
  928. strcpy (size,
  929. STRINGIFY_BIGINT
  930. (UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset),
  931. uintbuf));
  932. fprintf (stdlis, _("--Continued at byte %s--\n"), size);
  933. break;
  934. case GNUTYPE_NAMES:
  935. fprintf (stdlis, _("--Mangled file names--\n"));
  936. break;
  937. }
  938. }
  939. fflush (stdlis);
  940. }
  941. /*--------------------------------------------------------------.
  942. | Print a similar line when we make a directory automatically. |
  943. `--------------------------------------------------------------*/
  944. void
  945. print_for_mkdir (char *pathname, int length, mode_t mode)
  946. {
  947. char modes[11];
  948. if (verbose_option > 1)
  949. {
  950. /* File type and modes. */
  951. modes[0] = 'd';
  952. decode_mode (mode, modes + 1);
  953. if (block_number_option)
  954. {
  955. char buf[UINTMAX_STRSIZE_BOUND];
  956. fprintf (stdlis, _("block %s: "),
  957. STRINGIFY_BIGINT (current_block_ordinal (), buf));
  958. }
  959. fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
  960. _("Creating directory:"), length, quotearg (pathname));
  961. }
  962. }
  963. /*--------------------------------------------------------.
  964. | Skip over SIZE bytes of data in blocks in the archive. |
  965. `--------------------------------------------------------*/
  966. void
  967. skip_file (off_t size)
  968. {
  969. union block *x;
  970. if (multi_volume_option)
  971. {
  972. save_totsize = size;
  973. save_sizeleft = size;
  974. }
  975. while (size > 0)
  976. {
  977. x = find_next_block ();
  978. if (! x)
  979. FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
  980. set_next_block_after (x);
  981. size -= BLOCKSIZE;
  982. if (multi_volume_option)
  983. save_sizeleft -= BLOCKSIZE;
  984. }
  985. }
  986. /*---.
  987. | ? |
  988. `---*/
  989. void
  990. skip_extended_headers (void)
  991. {
  992. union block *exhdr;
  993. do
  994. {
  995. exhdr = find_next_block ();
  996. if (!exhdr)
  997. FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
  998. set_next_block_after (exhdr);
  999. }
  1000. while (exhdr->sparse_header.isextended);
  1001. }