list.c 34 KB

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