list.c 34 KB

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