list.c 33 KB

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