list.c 33 KB

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