list.c 31 KB

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