list.c 40 KB

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