list.c 33 KB

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