list.c 37 KB

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