list.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /* List a tar archive, with support routines for reading a tar archive.
  2. Copyright 1988,92,93,94,96,97,98,1999 Free Software Foundation, Inc.
  3. Written by John Gilmore, on 1985-08-26.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Define to non-zero for forcing old ctime() instead of isotime(). */
  16. #undef USE_OLD_CTIME
  17. #include "system.h"
  18. #include <ctype.h>
  19. #include <time.h>
  20. #define ISODIGIT(Char) \
  21. ((unsigned char) (Char) >= '0' && (unsigned char) (Char) <= '7')
  22. #define ISSPACE(Char) (ISASCII (Char) && isspace (Char))
  23. #ifndef FNM_LEADING_DIR
  24. # include <fnmatch.h>
  25. #endif
  26. #include "common.h"
  27. union block *current_header; /* points to current archive header */
  28. struct stat current_stat; /* stat struct corresponding */
  29. enum archive_format current_format; /* recognized format */
  30. /*-----------------------------------.
  31. | Main loop for reading an archive. |
  32. `-----------------------------------*/
  33. void
  34. read_and (void (*do_something) ())
  35. {
  36. enum read_header status = HEADER_STILL_UNREAD;
  37. enum read_header prev_status;
  38. char save_typeflag;
  39. name_gather ();
  40. open_archive (ACCESS_READ);
  41. while (1)
  42. {
  43. prev_status = status;
  44. status = read_header ();
  45. switch (status)
  46. {
  47. case HEADER_STILL_UNREAD:
  48. abort ();
  49. case HEADER_SUCCESS:
  50. /* Valid header. We should decode next field (mode) first.
  51. Ensure incoming names are null terminated. */
  52. if (ending_file_option &&
  53. fnmatch (ending_file_option, current_file_name,
  54. FNM_LEADING_DIR) == 0) {
  55. goto all_done;
  56. }
  57. /* FIXME: This is a quick kludge before 1.12 goes out. */
  58. current_stat.st_mtime = TIME_FROM_OCT (current_header->header.mtime);
  59. if (!name_match (current_file_name)
  60. || current_stat.st_mtime < newer_mtime_option
  61. || (exclude_option && check_exclude (current_file_name)))
  62. {
  63. int isextended = 0;
  64. if (current_header->header.typeflag == GNUTYPE_VOLHDR
  65. || current_header->header.typeflag == GNUTYPE_MULTIVOL
  66. || current_header->header.typeflag == GNUTYPE_NAMES)
  67. {
  68. (*do_something) ();
  69. continue;
  70. }
  71. if (show_omitted_dirs_option
  72. && current_header->header.typeflag == DIRTYPE)
  73. WARN ((0, 0, _("Omitting %s"), current_file_name));
  74. /* Skip past it in the archive. */
  75. if (current_header->oldgnu_header.isextended)
  76. isextended = 1;
  77. save_typeflag = current_header->header.typeflag;
  78. set_next_block_after (current_header);
  79. if (isextended)
  80. {
  81. #if 0
  82. union block *exhdr;
  83. while (1)
  84. {
  85. exhdr = find_next_block ();
  86. if (!exhdr->sparse_header.isextended)
  87. {
  88. set_next_block_after (exhdr);
  89. break;
  90. }
  91. }
  92. set_next_block_after (exhdr);
  93. #endif
  94. skip_extended_headers ();
  95. }
  96. /* Skip to the next header on the archive. */
  97. if (save_typeflag != DIRTYPE)
  98. skip_file (current_stat.st_size);
  99. continue;
  100. }
  101. (*do_something) ();
  102. continue;
  103. case HEADER_ZERO_BLOCK:
  104. if (block_number_option)
  105. {
  106. char buf[UINTMAX_STRSIZE_BOUND];
  107. fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
  108. STRINGIFY_BIGINT (current_block_ordinal (), buf));
  109. }
  110. set_next_block_after (current_header);
  111. status = prev_status;
  112. if (ignore_zeros_option)
  113. continue;
  114. break;
  115. case HEADER_END_OF_FILE:
  116. if (block_number_option)
  117. {
  118. char buf[UINTMAX_STRSIZE_BOUND];
  119. fprintf (stdlis, _("block %s: ** End of File **\n"),
  120. STRINGIFY_BIGINT (current_block_ordinal (), buf));
  121. }
  122. break;
  123. case HEADER_FAILURE:
  124. /* If the previous header was good, tell them that we are
  125. skipping bad ones. */
  126. set_next_block_after (current_header);
  127. switch (prev_status)
  128. {
  129. case HEADER_STILL_UNREAD:
  130. WARN ((0, 0, _("Hmm, this doesn't look like a tar archive")));
  131. /* Fall through. */
  132. case HEADER_ZERO_BLOCK:
  133. case HEADER_SUCCESS:
  134. WARN ((0, 0, _("Skipping to next file header")));
  135. break;
  136. case HEADER_END_OF_FILE:
  137. case HEADER_FAILURE:
  138. /* We are in the middle of a cascade of errors. */
  139. break;
  140. }
  141. continue;
  142. }
  143. break;
  144. }
  145. all_done: ;
  146. apply_delayed_set_stat ();
  147. close_archive ();
  148. names_notfound (); /* print names not found */
  149. }
  150. /*---------------------------------------------.
  151. | Print a header block, based on tar options. |
  152. `---------------------------------------------*/
  153. void
  154. list_archive (void)
  155. {
  156. int isextended = 0; /* to remember if current_header is extended */
  157. /* Print the header block. */
  158. if (verbose_option)
  159. {
  160. if (verbose_option > 1)
  161. decode_header (current_header, &current_stat, &current_format, 0);
  162. print_header ();
  163. }
  164. if (incremental_option && current_header->header.typeflag == GNUTYPE_DUMPDIR)
  165. {
  166. off_t size;
  167. size_t written, check;
  168. union block *data_block;
  169. set_next_block_after (current_header);
  170. if (multi_volume_option)
  171. {
  172. assign_string (&save_name, current_file_name);
  173. save_totsize = current_stat.st_size;
  174. }
  175. for (size = current_stat.st_size; size > 0; size -= written)
  176. {
  177. if (multi_volume_option)
  178. save_sizeleft = size;
  179. data_block = find_next_block ();
  180. if (!data_block)
  181. {
  182. ERROR ((0, 0, _("EOF in archive file")));
  183. break; /* FIXME: What happens, then? */
  184. }
  185. written = available_space_after (data_block);
  186. if (written > size)
  187. written = size;
  188. errno = 0; /* FIXME: errno should be read-only */
  189. check = fwrite (data_block->buffer, sizeof (char), written, stdlis);
  190. set_next_block_after ((union block *)
  191. (data_block->buffer + written - 1));
  192. if (check != written)
  193. {
  194. ERROR ((0, errno, _("Only wrote %lu of %lu bytes to file %s"),
  195. (unsigned long) check,
  196. (unsigned long) written, current_file_name));
  197. skip_file (size - written);
  198. break;
  199. }
  200. }
  201. if (multi_volume_option)
  202. assign_string (&save_name, NULL);
  203. fputc ('\n', stdlis);
  204. fflush (stdlis);
  205. return;
  206. }
  207. /* Check to see if we have an extended header to skip over also. */
  208. if (current_header->oldgnu_header.isextended)
  209. isextended = 1;
  210. /* Skip past the header in the archive. */
  211. set_next_block_after (current_header);
  212. /* If we needed to skip any extended headers, do so now, by reading
  213. extended headers and skipping past them in the archive. */
  214. if (isextended)
  215. {
  216. #if 0
  217. union block *exhdr;
  218. while (1)
  219. {
  220. exhdr = find_next_block ();
  221. if (!exhdr->sparse_header.isextended)
  222. {
  223. set_next_block_after (exhdr);
  224. break;
  225. }
  226. set_next_block_after (exhdr);
  227. }
  228. #endif
  229. skip_extended_headers ();
  230. }
  231. if (multi_volume_option)
  232. assign_string (&save_name, current_file_name);
  233. /* Skip to the next header on the archive. */
  234. skip_file (current_stat.st_size);
  235. if (multi_volume_option)
  236. assign_string (&save_name, NULL);
  237. }
  238. /*-----------------------------------------------------------------------.
  239. | Read a block that's supposed to be a header block. Return its address |
  240. | in "current_header", and if it is good, the file's size in |
  241. | current_stat.st_size. |
  242. | |
  243. | Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a |
  244. | block full of zeros (EOF marker). |
  245. | |
  246. | You must always set_next_block_after(current_header) to skip past the |
  247. | header which this routine reads. |
  248. `-----------------------------------------------------------------------*/
  249. /* The standard BSD tar sources create the checksum by adding up the
  250. bytes in the header as type char. I think the type char was unsigned
  251. on the PDP-11, but it's signed on the Next and Sun. It looks like the
  252. sources to BSD tar were never changed to compute the checksum
  253. currectly, so both the Sun and Next add the bytes of the header as
  254. signed chars. This doesn't cause a problem until you get a file with
  255. a name containing characters with the high bit set. So read_header
  256. computes two checksums -- signed and unsigned. */
  257. /* FIXME: The signed checksum computation is broken on machines where char's
  258. are unsigned. It's uneasy to handle all cases correctly... */
  259. enum read_header
  260. read_header (void)
  261. {
  262. size_t i;
  263. long unsigned_sum; /* the POSIX one :-) */
  264. long signed_sum; /* the Sun one :-( */
  265. long recorded_sum;
  266. char *p;
  267. union block *header;
  268. char **longp;
  269. char *bp;
  270. union block *data_block;
  271. size_t size, written;
  272. static char *next_long_name, *next_long_link;
  273. while (1)
  274. {
  275. header = find_next_block ();
  276. current_header = header;
  277. if (!header)
  278. return HEADER_END_OF_FILE;
  279. recorded_sum = UINTMAX_FROM_OCT (header->header.chksum);
  280. unsigned_sum = 0;
  281. signed_sum = 0;
  282. p = header->buffer;
  283. for (i = sizeof (*header); i-- != 0;)
  284. {
  285. /* We can't use unsigned char here because of old compilers,
  286. e.g. V7. */
  287. unsigned_sum += 0xFF & *p;
  288. signed_sum += *p++;
  289. }
  290. /* Adjust checksum to count the "chksum" field as blanks. */
  291. for (i = sizeof (header->header.chksum); i-- != 0;)
  292. {
  293. unsigned_sum -= 0xFF & header->header.chksum[i];
  294. signed_sum -= header->header.chksum[i];
  295. }
  296. unsigned_sum += ' ' * sizeof header->header.chksum;
  297. signed_sum += ' ' * sizeof header->header.chksum;
  298. if (unsigned_sum == sizeof header->header.chksum * ' ')
  299. {
  300. /* This is a zeroed block...whole block is 0's except for the
  301. blanks we faked for the checksum field. */
  302. return HEADER_ZERO_BLOCK;
  303. }
  304. if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
  305. return HEADER_FAILURE;
  306. /* Good block. Decode file size and return. */
  307. if (header->header.typeflag == LNKTYPE)
  308. current_stat.st_size = 0; /* links 0 size on tape */
  309. else
  310. current_stat.st_size = OFF_FROM_OCT (header->header.size);
  311. header->header.name[NAME_FIELD_SIZE - 1] = '\0';
  312. if (header->header.typeflag == GNUTYPE_LONGNAME
  313. || header->header.typeflag == GNUTYPE_LONGLINK)
  314. {
  315. longp = ((header->header.typeflag == GNUTYPE_LONGNAME)
  316. ? &next_long_name
  317. : &next_long_link);
  318. set_next_block_after (header);
  319. if (*longp)
  320. free (*longp);
  321. size = current_stat.st_size;
  322. if (size != current_stat.st_size)
  323. FATAL_ERROR ((0, 0, _("Memory exhausted")));
  324. bp = *longp = (char *) xmalloc (size);
  325. for (; size > 0; size -= written)
  326. {
  327. data_block = find_next_block ();
  328. if (data_block == NULL)
  329. {
  330. ERROR ((0, 0, _("Unexpected EOF on archive file")));
  331. break;
  332. }
  333. written = available_space_after (data_block);
  334. if (written > size)
  335. written = size;
  336. memcpy (bp, data_block->buffer, written);
  337. bp += written;
  338. set_next_block_after ((union block *)
  339. (data_block->buffer + written - 1));
  340. }
  341. /* Loop! */
  342. }
  343. else
  344. {
  345. char *name = next_long_name;
  346. struct posix_header *h = &current_header->header;
  347. char namebuf[sizeof h->prefix + 1 + sizeof h->name + 1];
  348. if (! name)
  349. {
  350. /* Accept file names as specified by POSIX.1-1996
  351. section 10.1.1. */
  352. char *np = namebuf;
  353. if (h->prefix[0])
  354. {
  355. memcpy (np, h->prefix, sizeof h->prefix);
  356. np[sizeof h->prefix] = '\0';
  357. np += strlen (np);
  358. *np++ = '/';
  359. }
  360. memcpy (np, h->name, sizeof h->name);
  361. np[sizeof h->name] = '\0';
  362. name = namebuf;
  363. }
  364. assign_string (&current_file_name, name);
  365. assign_string (&current_link_name,
  366. (next_long_link ? next_long_link
  367. : current_header->header.linkname));
  368. next_long_link = next_long_name = 0;
  369. return HEADER_SUCCESS;
  370. }
  371. }
  372. }
  373. /*-------------------------------------------------------------------------.
  374. | Decode things from a file HEADER block into STAT_INFO, also setting |
  375. | *FORMAT_POINTER depending on the header block format. If DO_USER_GROUP, |
  376. | decode the user/group information (this is useful for extraction, but |
  377. | waste time when merely listing). |
  378. | |
  379. | read_header() has already decoded the checksum and length, so we don't. |
  380. | |
  381. | This routine should *not* be called twice for the same block, since the |
  382. | two calls might use different DO_USER_GROUP values and thus might end up |
  383. | with different uid/gid for the two calls. If anybody wants the uid/gid |
  384. | they should decode it first, and other callers should decode it without |
  385. | uid/gid before calling a routine, e.g. print_header, that assumes |
  386. | decoded data. |
  387. `-------------------------------------------------------------------------*/
  388. void
  389. decode_header (union block *header, struct stat *stat_info,
  390. enum archive_format *format_pointer, int do_user_group)
  391. {
  392. enum archive_format format;
  393. if (strcmp (header->header.magic, TMAGIC) == 0)
  394. format = POSIX_FORMAT;
  395. else if (strcmp (header->header.magic, OLDGNU_MAGIC) == 0)
  396. format = OLDGNU_FORMAT;
  397. else
  398. format = V7_FORMAT;
  399. *format_pointer = format;
  400. stat_info->st_mode = MODE_FROM_OCT (header->header.mode);
  401. stat_info->st_mode &= 07777;
  402. stat_info->st_mtime = TIME_FROM_OCT (header->header.mtime);
  403. if (format == OLDGNU_FORMAT && incremental_option)
  404. {
  405. stat_info->st_atime = TIME_FROM_OCT (header->oldgnu_header.atime);
  406. stat_info->st_ctime = TIME_FROM_OCT (header->oldgnu_header.ctime);
  407. }
  408. if (format == V7_FORMAT)
  409. {
  410. stat_info->st_uid = UID_FROM_OCT (header->header.uid);
  411. stat_info->st_gid = GID_FROM_OCT (header->header.gid);
  412. stat_info->st_rdev = 0;
  413. }
  414. else
  415. {
  416. if (do_user_group)
  417. {
  418. /* FIXME: Decide if this should somewhat depend on -p. */
  419. if (numeric_owner_option
  420. || !*header->header.uname
  421. || !uname_to_uid (header->header.uname, &stat_info->st_uid))
  422. stat_info->st_uid = UID_FROM_OCT (header->header.uid);
  423. if (numeric_owner_option
  424. || !*header->header.gname
  425. || !gname_to_gid (header->header.gname, &stat_info->st_gid))
  426. stat_info->st_gid = GID_FROM_OCT (header->header.gid);
  427. }
  428. switch (header->header.typeflag)
  429. {
  430. #ifdef S_IFBLK
  431. case BLKTYPE:
  432. stat_info->st_rdev
  433. = makedev (MAJOR_FROM_OCT (header->header.devmajor),
  434. MINOR_FROM_OCT (header->header.devminor));
  435. break;
  436. #endif
  437. #ifdef S_IFCHR
  438. case CHRTYPE:
  439. stat_info->st_rdev
  440. = makedev (MAJOR_FROM_OCT (header->header.devmajor),
  441. MINOR_FROM_OCT (header->header.devminor));
  442. break;
  443. #endif
  444. default:
  445. stat_info->st_rdev = 0;
  446. }
  447. }
  448. }
  449. /*------------------------------------------------------------------------.
  450. | Quick and dirty octal conversion. Result is -1 if the field is invalid |
  451. | (all blank, or nonoctal). |
  452. `------------------------------------------------------------------------*/
  453. static uintmax_t
  454. from_oct (const char *where0, size_t digs0, const char *type, uintmax_t maxval)
  455. {
  456. uintmax_t value;
  457. const char *where = where0;
  458. size_t digs = digs0;
  459. for (;;)
  460. {
  461. if (digs == 0)
  462. {
  463. ERROR ((0, 0, _("Blanks in header where octal %s value expected"),
  464. type));
  465. return -1;
  466. }
  467. if (!ISSPACE (*where))
  468. break;
  469. where++;
  470. digs--;
  471. }
  472. value = 0;
  473. while (digs != 0 && ISODIGIT (*where))
  474. {
  475. /* Scan til nonoctal. */
  476. if (value << 3 >> 3 != value)
  477. goto out_of_range;
  478. value = (value << 3) | (*where++ - '0');
  479. --digs;
  480. }
  481. if (digs != 0 && *where && !ISSPACE (*where))
  482. {
  483. ERROR ((0, 0, _("Header contains `%.*s' where octal %s value expected"),
  484. (int) digs0, where0, type));
  485. return -1;
  486. }
  487. if (value <= maxval)
  488. return value;
  489. out_of_range:
  490. ERROR ((0, 0, _("Octal value `%.*s' is out of range for %s"),
  491. (int) digs0, where0, type));
  492. return -1;
  493. }
  494. gid_t
  495. gid_from_oct (const char *p, size_t s)
  496. {
  497. return from_oct (p, s, "gid_t", (uintmax_t) TYPE_MAXIMUM (gid_t));
  498. }
  499. major_t
  500. major_from_oct (const char *p, size_t s)
  501. {
  502. return from_oct (p, s, "major_t", (uintmax_t) TYPE_MAXIMUM (major_t));
  503. }
  504. minor_t
  505. minor_from_oct (const char *p, size_t s)
  506. {
  507. return from_oct (p, s, "minor_t", (uintmax_t) TYPE_MAXIMUM (minor_t));
  508. }
  509. mode_t
  510. mode_from_oct (const char *p, size_t s)
  511. {
  512. return from_oct (p, s, "mode_t", (uintmax_t) TYPE_MAXIMUM (mode_t));
  513. }
  514. off_t
  515. off_from_oct (const char *p, size_t s)
  516. {
  517. return from_oct (p, s, "off_t", (uintmax_t) TYPE_MAXIMUM (off_t));
  518. }
  519. size_t
  520. size_from_oct (const char *p, size_t s)
  521. {
  522. return from_oct (p, s, "size_t", (uintmax_t) TYPE_MAXIMUM (size_t));
  523. }
  524. time_t
  525. time_from_oct (const char *p, size_t s)
  526. {
  527. return from_oct (p, s, "time_t", (uintmax_t) TYPE_MAXIMUM (time_t));
  528. }
  529. uid_t
  530. uid_from_oct (const char *p, size_t s)
  531. {
  532. return from_oct (p, s, "uid_t", (uintmax_t) TYPE_MAXIMUM (uid_t));
  533. }
  534. uintmax_t
  535. uintmax_from_oct (const char *p, size_t s)
  536. {
  537. return from_oct (p, s, "uintmax_t", TYPE_MAXIMUM (uintmax_t));
  538. }
  539. /*----------------------------------------------------------------------.
  540. | Format O as a null-terminated decimal string into BUF _backwards_; |
  541. | return pointer to start of result. |
  542. `----------------------------------------------------------------------*/
  543. char *
  544. stringify_uintmax_t_backwards (uintmax_t o, char *buf)
  545. {
  546. *--buf = '\0';
  547. do
  548. *--buf = '0' + (int) (o % 10);
  549. while ((o /= 10) != 0);
  550. return buf;
  551. }
  552. #if !USE_OLD_CTIME
  553. /*-------------------------------------------.
  554. | Return the time formatted along ISO 8601. |
  555. `-------------------------------------------*/
  556. /* Also, see http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html. */
  557. static char *
  558. isotime (const time_t *time)
  559. {
  560. static char buffer[21];
  561. struct tm *tm;
  562. tm = localtime (time);
  563. sprintf (buffer, "%4d-%02d-%02d %02d:%02d:%02d\n",
  564. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  565. tm->tm_hour, tm->tm_min, tm->tm_sec);
  566. return buffer;
  567. }
  568. #endif /* not USE_OLD_CTIME */
  569. /*-------------------------------------------------------------------------.
  570. | Decode MODE from its binary form in a stat structure, and encode it into |
  571. | a 9 characters string STRING, terminated with a NUL. |
  572. `-------------------------------------------------------------------------*/
  573. static void
  574. decode_mode (mode_t mode, char *string)
  575. {
  576. mode_t mask;
  577. const char *rwx = "rwxrwxrwx";
  578. for (mask = 0400; mask != 0; mask >>= 1)
  579. if (mode & mask)
  580. *string++ = *rwx++;
  581. else
  582. {
  583. *string++ = '-';
  584. rwx++;
  585. }
  586. if (mode & S_ISUID)
  587. string[-7] = string[-7] == 'x' ? 's' : 'S';
  588. if (mode & S_ISGID)
  589. string[-4] = string[-4] == 'x' ? 's' : 'S';
  590. if (mode & S_ISVTX)
  591. string[-1] = string[-1] == 'x' ? 't' : 'T';
  592. *string = '\0';
  593. }
  594. /*-------------------------------------------------------------------------.
  595. | Actually print it. |
  596. | |
  597. | Plain and fancy file header block logging. Non-verbose just prints the |
  598. | name, e.g. for "tar t" or "tar x". This should just contain file names, |
  599. | so it can be fed back into tar with xargs or the "-T" option. The |
  600. | verbose option can give a bunch of info, one line per file. I doubt |
  601. | anybody tries to parse its format, or if they do, they shouldn't. Unix |
  602. | tar is pretty random here anyway. |
  603. `-------------------------------------------------------------------------*/
  604. /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
  605. HEAD_STANDARD, which must be set up in advance. Not very clean... */
  606. /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
  607. columns never shift during the listing. */
  608. #define UGSWIDTH 18
  609. static int ugswidth = UGSWIDTH; /* maximum width encountered so far */
  610. /* DATEWIDTH is the number of columns taken by the date and time fields. */
  611. #if USE_OLD_CDATE
  612. # define DATEWIDTH 19
  613. #else
  614. # define DATEWIDTH 18
  615. #endif
  616. void
  617. print_header (void)
  618. {
  619. char modes[11];
  620. char *timestamp;
  621. char uform[11], gform[11]; /* these hold formatted ints */
  622. char *user, *group;
  623. char size[2 * UINTMAX_STRSIZE_BOUND];
  624. /* holds formatted size or major,minor */
  625. char uintbuf[UINTMAX_STRSIZE_BOUND];
  626. time_t longie; /* to make ctime() call portable */
  627. int pad;
  628. char *name;
  629. if (block_number_option)
  630. {
  631. char buf[UINTMAX_STRSIZE_BOUND];
  632. fprintf (stdlis, _("block %s: "),
  633. STRINGIFY_BIGINT (current_block_ordinal (), buf));
  634. }
  635. if (verbose_option <= 1)
  636. {
  637. /* Just the fax, mam. */
  638. char *quoted_name = quote_copy_string (current_file_name);
  639. if (quoted_name)
  640. {
  641. fprintf (stdlis, "%s\n", quoted_name);
  642. free (quoted_name);
  643. }
  644. else
  645. fprintf (stdlis, "%s\n", current_file_name);
  646. }
  647. else
  648. {
  649. /* File type and modes. */
  650. modes[0] = '?';
  651. switch (current_header->header.typeflag)
  652. {
  653. case GNUTYPE_VOLHDR:
  654. modes[0] = 'V';
  655. break;
  656. case GNUTYPE_MULTIVOL:
  657. modes[0] = 'M';
  658. break;
  659. case GNUTYPE_NAMES:
  660. modes[0] = 'N';
  661. break;
  662. case GNUTYPE_LONGNAME:
  663. case GNUTYPE_LONGLINK:
  664. ERROR ((0, 0, _("Visible longname error")));
  665. break;
  666. case GNUTYPE_SPARSE:
  667. case REGTYPE:
  668. case AREGTYPE:
  669. case LNKTYPE:
  670. modes[0] = '-';
  671. if (current_file_name[strlen (current_file_name) - 1] == '/')
  672. modes[0] = 'd';
  673. break;
  674. case GNUTYPE_DUMPDIR:
  675. modes[0] = 'd';
  676. break;
  677. case DIRTYPE:
  678. modes[0] = 'd';
  679. break;
  680. case SYMTYPE:
  681. modes[0] = 'l';
  682. break;
  683. case BLKTYPE:
  684. modes[0] = 'b';
  685. break;
  686. case CHRTYPE:
  687. modes[0] = 'c';
  688. break;
  689. case FIFOTYPE:
  690. modes[0] = 'p';
  691. break;
  692. case CONTTYPE:
  693. modes[0] = 'C';
  694. break;
  695. }
  696. decode_mode (current_stat.st_mode, modes + 1);
  697. /* Timestamp. */
  698. longie = current_stat.st_mtime;
  699. #if USE_OLD_CTIME
  700. timestamp = ctime (&longie);
  701. timestamp[16] = '\0';
  702. timestamp[24] = '\0';
  703. #else
  704. timestamp = isotime (&longie);
  705. timestamp[16] = '\0';
  706. #endif
  707. /* User and group names. */
  708. if (*current_header->header.uname && current_format != V7_FORMAT)
  709. user = current_header->header.uname;
  710. else
  711. user = STRINGIFY_BIGINT (UINTMAX_FROM_OCT (current_header->header.uid),
  712. uform);
  713. if (*current_header->header.gname && current_format != V7_FORMAT)
  714. group = current_header->header.gname;
  715. else
  716. group = STRINGIFY_BIGINT (UINTMAX_FROM_OCT
  717. (current_header->header.gid),
  718. gform);
  719. /* Format the file size or major/minor device numbers. */
  720. switch (current_header->header.typeflag)
  721. {
  722. #if defined(S_IFBLK) || defined(S_IFCHR)
  723. case CHRTYPE:
  724. case BLKTYPE:
  725. sprintf (size, "%lu,%lu",
  726. (unsigned long) major (current_stat.st_rdev),
  727. (unsigned long) minor (current_stat.st_rdev));
  728. break;
  729. #endif
  730. case GNUTYPE_SPARSE:
  731. strcpy (size,
  732. STRINGIFY_BIGINT
  733. (UINTMAX_FROM_OCT (current_header->oldgnu_header.realsize),
  734. uintbuf));
  735. break;
  736. default:
  737. strcpy (size, STRINGIFY_BIGINT (current_stat.st_size, uintbuf));
  738. break;
  739. }
  740. /* Figure out padding and print the whole line. */
  741. pad = strlen (user) + strlen (group) + strlen (size) + 1;
  742. if (pad > ugswidth)
  743. ugswidth = pad;
  744. #if USE_OLD_CTIME
  745. fprintf (stdlis, "%s %s/%s %*s%s %s %s",
  746. modes, user, group, ugswidth - pad, "",
  747. size, timestamp + 4, timestamp + 20);
  748. #else
  749. fprintf (stdlis, "%s %s/%s %*s%s %s",
  750. modes, user, group, ugswidth - pad, "", size, timestamp);
  751. #endif
  752. name = quote_copy_string (current_file_name);
  753. if (name)
  754. {
  755. fprintf (stdlis, " %s", name);
  756. free (name);
  757. }
  758. else
  759. fprintf (stdlis, " %s", current_file_name);
  760. switch (current_header->header.typeflag)
  761. {
  762. case SYMTYPE:
  763. name = quote_copy_string (current_link_name);
  764. if (name)
  765. {
  766. fprintf (stdlis, " -> %s\n", name);
  767. free (name);
  768. }
  769. else
  770. fprintf (stdlis, " -> %s\n", current_link_name);
  771. break;
  772. case LNKTYPE:
  773. name = quote_copy_string (current_link_name);
  774. if (name)
  775. {
  776. fprintf (stdlis, _(" link to %s\n"), name);
  777. free (name);
  778. }
  779. else
  780. fprintf (stdlis, _(" link to %s\n"), current_link_name);
  781. break;
  782. default:
  783. fprintf (stdlis, _(" unknown file type `%c'\n"),
  784. current_header->header.typeflag);
  785. break;
  786. case AREGTYPE:
  787. case REGTYPE:
  788. case GNUTYPE_SPARSE:
  789. case CHRTYPE:
  790. case BLKTYPE:
  791. case DIRTYPE:
  792. case FIFOTYPE:
  793. case CONTTYPE:
  794. case GNUTYPE_DUMPDIR:
  795. putc ('\n', stdlis);
  796. break;
  797. case GNUTYPE_VOLHDR:
  798. fprintf (stdlis, _("--Volume Header--\n"));
  799. break;
  800. case GNUTYPE_MULTIVOL:
  801. strcpy (size,
  802. STRINGIFY_BIGINT
  803. (UINTMAX_FROM_OCT (current_header->oldgnu_header.offset),
  804. uintbuf));
  805. fprintf (stdlis, _("--Continued at byte %s--\n"), size);
  806. break;
  807. case GNUTYPE_NAMES:
  808. fprintf (stdlis, _("--Mangled file names--\n"));
  809. break;
  810. }
  811. }
  812. fflush (stdlis);
  813. }
  814. /*--------------------------------------------------------------.
  815. | Print a similar line when we make a directory automatically. |
  816. `--------------------------------------------------------------*/
  817. void
  818. print_for_mkdir (char *pathname, int length, mode_t mode)
  819. {
  820. char modes[11];
  821. char *name;
  822. if (verbose_option > 1)
  823. {
  824. /* File type and modes. */
  825. modes[0] = 'd';
  826. decode_mode (mode, modes + 1);
  827. if (block_number_option)
  828. {
  829. char buf[UINTMAX_STRSIZE_BOUND];
  830. fprintf (stdlis, _("block %s: "),
  831. STRINGIFY_BIGINT (current_block_ordinal (), buf));
  832. }
  833. name = quote_copy_string (pathname);
  834. if (name)
  835. {
  836. fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
  837. _("Creating directory:"), length, name);
  838. free (name);
  839. }
  840. else
  841. fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
  842. _("Creating directory:"), length, pathname);
  843. }
  844. }
  845. /*--------------------------------------------------------.
  846. | Skip over SIZE bytes of data in blocks in the archive. |
  847. `--------------------------------------------------------*/
  848. void
  849. skip_file (off_t size)
  850. {
  851. union block *x;
  852. if (multi_volume_option)
  853. {
  854. save_totsize = size;
  855. save_sizeleft = size;
  856. }
  857. while (size > 0)
  858. {
  859. x = find_next_block ();
  860. if (x == NULL)
  861. FATAL_ERROR ((0, 0, _("Unexpected EOF on archive file")));
  862. set_next_block_after (x);
  863. size -= BLOCKSIZE;
  864. if (multi_volume_option)
  865. save_sizeleft -= BLOCKSIZE;
  866. }
  867. }
  868. /*---.
  869. | ? |
  870. `---*/
  871. void
  872. skip_extended_headers (void)
  873. {
  874. union block *exhdr;
  875. while (1)
  876. {
  877. exhdr = find_next_block ();
  878. if (!exhdr->sparse_header.isextended)
  879. {
  880. set_next_block_after (exhdr);
  881. break;
  882. }
  883. set_next_block_after (exhdr);
  884. }
  885. }