list.c 35 KB

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