4
0

list.c 41 KB

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