list.c 30 KB

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