list.c 34 KB

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