list.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /* List a tar archive.
  2. Copyright (C) 1988, 1992 Free Software Foundation
  3. This file is part of GNU Tar.
  4. GNU Tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Tar; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /*
  16. * List a tar archive.
  17. *
  18. * Also includes support routines for reading a tar archive.
  19. *
  20. * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
  21. */
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <sys/types.h>
  25. #include <errno.h>
  26. #ifndef STDC_HEADERS
  27. extern int errno;
  28. #endif
  29. #include <time.h>
  30. #ifdef BSD42
  31. #include <sys/file.h>
  32. #else
  33. #ifndef V7
  34. #include <fcntl.h>
  35. #endif
  36. #endif
  37. #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
  38. #include "tar.h"
  39. #include "port.h"
  40. extern FILE *msg_file;
  41. long from_oct(); /* Decode octal number */
  42. void demode(); /* Print file mode */
  43. union record *head; /* Points to current archive header */
  44. struct stat hstat; /* Stat struct corresponding */
  45. int head_standard; /* Tape header is in ANSI format */
  46. int check_exclude();
  47. void close_archive();
  48. void decode_header();
  49. int findgid();
  50. int finduid();
  51. void name_gather();
  52. int name_match();
  53. void names_notfound();
  54. void open_archive();
  55. void print_header();
  56. int read_header();
  57. void saverec();
  58. void skip_file();
  59. void skip_extended_headers();
  60. extern char *quote_copy_string();
  61. /*
  62. * Main loop for reading an archive.
  63. */
  64. void
  65. read_and(do_something)
  66. void (*do_something)();
  67. {
  68. int status = 3; /* Initial status at start of archive */
  69. int prev_status;
  70. extern time_t new_time;
  71. char save_linkflag;
  72. name_gather(); /* Gather all the names */
  73. open_archive(1); /* Open for reading */
  74. for(;;) {
  75. prev_status = status;
  76. status = read_header();
  77. switch (status) {
  78. case 1: /* Valid header */
  79. /* We should decode next field (mode) first... */
  80. /* Ensure incoming names are null terminated. */
  81. head->header.name[NAMSIZ-1] = '\0';
  82. if ( !name_match(head->header.name)
  83. || (f_new_files && hstat.st_mtime<new_time)
  84. || (f_exclude && check_exclude(head->header.name))) {
  85. int isextended = 0;
  86. if( head->header.linkflag==LF_VOLHDR
  87. || head->header.linkflag==LF_MULTIVOL
  88. || head->header.linkflag==LF_NAMES) {
  89. (*do_something)();
  90. continue;
  91. }
  92. if (f_show_omitted_dirs
  93. && head->header.linkflag == LF_DIR)
  94. msg ("Omitting %s\n", head->header.name);
  95. /* Skip past it in the archive */
  96. if (head->header.isextended)
  97. isextended = 1;
  98. save_linkflag = head->header.linkflag;
  99. userec(head);
  100. if (isextended) {
  101. /* register union record *exhdr;
  102. for (;;) {
  103. exhdr = findrec();
  104. if (!exhdr->ext_hdr.isextended) {
  105. userec(exhdr);
  106. break;
  107. }
  108. }
  109. userec(exhdr);*/
  110. skip_extended_headers();
  111. }
  112. /* Skip to the next header on the archive */
  113. if(save_linkflag != LF_DIR)
  114. skip_file((long)hstat.st_size);
  115. continue;
  116. }
  117. (*do_something)();
  118. continue;
  119. /*
  120. * If the previous header was good, tell them
  121. * that we are skipping bad ones.
  122. */
  123. case 0: /* Invalid header */
  124. userec(head);
  125. switch (prev_status) {
  126. case 3: /* Error on first record */
  127. msg("Hmm, this doesn't look like a tar archive.");
  128. /* FALL THRU */
  129. case 2: /* Error after record of zeroes */
  130. case 1: /* Error after header rec */
  131. msg("Skipping to next file header...");
  132. case 0: /* Error after error */
  133. break;
  134. }
  135. continue;
  136. case 2: /* Record of zeroes */
  137. userec(head);
  138. status = prev_status; /* If error after 0's */
  139. if (f_ignorez)
  140. continue;
  141. /* FALL THRU */
  142. case EOF: /* End of archive */
  143. break;
  144. }
  145. break;
  146. };
  147. restore_saved_dir_info ();
  148. close_archive();
  149. names_notfound(); /* Print names not found */
  150. }
  151. /*
  152. * Print a header record, based on tar options.
  153. */
  154. void
  155. list_archive()
  156. {
  157. extern char *save_name;
  158. int isextended = 0; /* Flag to remember if head is extended */
  159. /* Save the record */
  160. saverec(&head);
  161. /* Print the header record */
  162. if (f_verbose) {
  163. if (f_verbose > 1)
  164. decode_header(head, &hstat, &head_standard, 0);
  165. print_header();
  166. }
  167. if(f_gnudump && head->header.linkflag==LF_DUMPDIR) {
  168. size_t size, written, check;
  169. char *data;
  170. extern long save_totsize;
  171. extern long save_sizeleft;
  172. userec(head);
  173. if(f_multivol) {
  174. save_name = head->header.name;
  175. save_totsize=hstat.st_size;
  176. }
  177. for(size = hstat.st_size;size>0;size-=written) {
  178. if(f_multivol)
  179. save_sizeleft=size;
  180. data = findrec()->charptr;
  181. if(data==NULL) {
  182. msg("EOF in archive file?");
  183. break;
  184. }
  185. written = endofrecs()->charptr - data;
  186. if(written>size)
  187. written=size;
  188. errno=0;
  189. check=fwrite(data,sizeof(char), written, msg_file);
  190. userec((union record *)(data+written - 1));
  191. if(check!=written) {
  192. msg_perror("only wrote %ld of %ld bytes to file %s",check, written,head->header.name);
  193. skip_file((long)(size)-written);
  194. break;
  195. }
  196. }
  197. if(f_multivol)
  198. save_name = 0;
  199. saverec((union record **) 0); /* Unsave it */
  200. fputc('\n',msg_file);
  201. fflush(msg_file);
  202. return;
  203. }
  204. saverec((union record **) 0); /* Unsave it */
  205. /* Check to see if we have an extended header to skip over also */
  206. if (head->header.isextended)
  207. isextended = 1;
  208. /* Skip past the header in the archive */
  209. userec(head);
  210. /*
  211. * If we needed to skip any extended headers, do so now, by
  212. * reading extended headers and skipping past them in the
  213. * archive.
  214. */
  215. if (isextended) {
  216. /* register union record *exhdr;
  217. for (;;) {
  218. exhdr = findrec();
  219. if (!exhdr->ext_hdr.isextended) {
  220. userec(exhdr);
  221. break;
  222. }
  223. userec(exhdr);
  224. }*/
  225. skip_extended_headers();
  226. }
  227. if(f_multivol)
  228. save_name=head->header.name;
  229. /* Skip to the next header on the archive */
  230. skip_file((long) hstat.st_size);
  231. if(f_multivol)
  232. save_name = 0;
  233. }
  234. /*
  235. * Read a record that's supposed to be a header record.
  236. * Return its address in "head", and if it is good, the file's
  237. * size in hstat.st_size.
  238. *
  239. * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  240. * 2 for a record full of zeros (EOF marker).
  241. *
  242. * You must always userec(head) to skip past the header which this
  243. * routine reads.
  244. */
  245. int
  246. read_header()
  247. {
  248. register int i;
  249. register long sum, recsum;
  250. register char *p;
  251. register union record *header;
  252. long from_oct();
  253. header = findrec();
  254. head = header; /* This is our current header */
  255. if (NULL == header)
  256. return EOF;
  257. recsum = from_oct(8, header->header.chksum);
  258. sum = 0;
  259. p = header->charptr;
  260. for (i = sizeof(*header); --i >= 0;) {
  261. /*
  262. * We can't use unsigned char here because of old compilers,
  263. * e.g. V7.
  264. */
  265. sum += 0xFF & *p++;
  266. }
  267. /* Adjust checksum to count the "chksum" field as blanks. */
  268. for (i = sizeof(header->header.chksum); --i >= 0;)
  269. sum -= 0xFF & header->header.chksum[i];
  270. sum += ' '* sizeof header->header.chksum;
  271. if (sum == recsum) {
  272. /*
  273. * Good record. Decode file size and return.
  274. */
  275. if (header->header.linkflag == LF_LINK)
  276. hstat.st_size = 0; /* Links 0 size on tape */
  277. else
  278. hstat.st_size = from_oct(1+12, header->header.size);
  279. return 1;
  280. }
  281. if (sum == 8*' ') {
  282. /*
  283. * This is a zeroed record...whole record is 0's except
  284. * for the 8 blanks we faked for the checksum field.
  285. */
  286. return 2;
  287. }
  288. return 0;
  289. }
  290. /*
  291. * Decode things from a file header record into a "struct stat".
  292. * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  293. * Standard" tar format or regular old tar format.
  294. *
  295. * read_header() has already decoded the checksum and length, so we don't.
  296. *
  297. * If wantug != 0, we want the uid/group info decoded from Unix Standard
  298. * tapes (for extraction). If == 0, we are just printing anyway, so save time.
  299. *
  300. * decode_header should NOT be called twice for the same record, since the
  301. * two calls might use different "wantug" values and thus might end up with
  302. * different uid/gid for the two calls. If anybody wants the uid/gid they
  303. * should decode it first, and other callers should decode it without uid/gid
  304. * before calling a routine, e.g. print_header, that assumes decoded data.
  305. */
  306. void
  307. decode_header(header, st, stdp, wantug)
  308. register union record *header;
  309. register struct stat *st;
  310. int *stdp;
  311. int wantug;
  312. {
  313. long from_oct();
  314. st->st_mode = from_oct(8, header->header.mode);
  315. st->st_mtime = from_oct(1+12, header->header.mtime);
  316. if(f_gnudump) {
  317. st->st_atime = from_oct(1+12, header->header.atime);
  318. st->st_ctime = from_oct(1+12, header->header.ctime);
  319. }
  320. if (0==strcmp(header->header.magic, TMAGIC)) {
  321. /* Unix Standard tar archive */
  322. *stdp = 1;
  323. if (wantug) {
  324. #ifdef NONAMES
  325. st->st_uid = from_oct(8, header->header.uid);
  326. st->st_gid = from_oct(8, header->header.gid);
  327. #else
  328. st->st_uid =
  329. (*header->header.uname
  330. ? finduid (header->header.uname)
  331. : from_oct (8, header->header.uid));
  332. st->st_gid =
  333. (*header->header.gname
  334. ? findgid (header->header.gname)
  335. : from_oct (8, header->header.gid));
  336. #endif
  337. }
  338. #if defined(S_IFBLK) || defined(S_IFCHR)
  339. switch (header->header.linkflag) {
  340. case LF_BLK: case LF_CHR:
  341. st->st_rdev = makedev(from_oct(8, header->header.devmajor),
  342. from_oct(8, header->header.devminor));
  343. }
  344. #endif
  345. } else {
  346. /* Old fashioned tar archive */
  347. *stdp = 0;
  348. st->st_uid = from_oct(8, header->header.uid);
  349. st->st_gid = from_oct(8, header->header.gid);
  350. st->st_rdev = 0;
  351. }
  352. }
  353. /*
  354. * Quick and dirty octal conversion.
  355. *
  356. * Result is -1 if the field is invalid (all blank, or nonoctal).
  357. */
  358. long
  359. from_oct(digs, where)
  360. register int digs;
  361. register char *where;
  362. {
  363. register long value;
  364. while (isspace(*where)) { /* Skip spaces */
  365. where++;
  366. if (--digs <= 0)
  367. return -1; /* All blank field */
  368. }
  369. value = 0;
  370. while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
  371. value = (value << 3) | (*where++ - '0');
  372. --digs;
  373. }
  374. if (digs > 0 && *where && !isspace(*where))
  375. return -1; /* Ended on non-space/nul */
  376. return value;
  377. }
  378. /*
  379. * Actually print it.
  380. *
  381. * Plain and fancy file header block logging.
  382. * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
  383. * This should just contain file names, so it can be fed back into tar
  384. * with xargs or the "-T" option. The verbose option can give a bunch
  385. * of info, one line per file. I doubt anybody tries to parse its
  386. * format, or if they do, they shouldn't. Unix tar is pretty random here
  387. * anyway.
  388. *
  389. * Note that print_header uses the globals <head>, <hstat>, and
  390. * <head_standard>, which must be set up in advance. This is not very clean
  391. * and should be cleaned up. FIXME.
  392. */
  393. #define UGSWIDTH 18 /* min width of User, group, size */
  394. /* UGSWIDTH of 18 means that with user and group names <= 8 chars the columns
  395. never shift during the listing. */
  396. #define DATEWIDTH 19 /* Last mod date */
  397. static int ugswidth = UGSWIDTH; /* Max width encountered so far */
  398. void
  399. print_header()
  400. {
  401. char modes[11];
  402. char *timestamp;
  403. char uform[11], gform[11]; /* These hold formatted ints */
  404. char *user, *group;
  405. char size[24]; /* Holds a formatted long or maj, min */
  406. time_t longie; /* To make ctime() call portable */
  407. int pad;
  408. char *name;
  409. extern long baserec;
  410. if(f_sayblock)
  411. fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  412. /* annofile(msg_file, (char *)NULL); */
  413. if (f_verbose <= 1) {
  414. /* Just the fax, mam. */
  415. char *name;
  416. name=quote_copy_string(head->header.name);
  417. if(name==0)
  418. name=head->header.name;
  419. fprintf(msg_file, "%s\n", name);
  420. if(name!=head->header.name)
  421. free(name);
  422. } else {
  423. /* File type and modes */
  424. modes[0] = '?';
  425. switch (head->header.linkflag) {
  426. case LF_VOLHDR:
  427. modes[0]='V';
  428. break;
  429. case LF_MULTIVOL:
  430. modes[0]='M';
  431. break;
  432. case LF_NAMES:
  433. modes[0]='N';
  434. break;
  435. case LF_SPARSE:
  436. case LF_NORMAL:
  437. case LF_OLDNORMAL:
  438. case LF_LINK:
  439. modes[0] = '-';
  440. if ('/' == head->header.name[strlen(head->header.name)-1])
  441. modes[0] = 'd';
  442. break;
  443. case LF_DUMPDIR:modes[0] = 'd'; break;
  444. case LF_DIR: modes[0] = 'd'; break;
  445. case LF_SYMLINK:modes[0] = 'l'; break;
  446. case LF_BLK: modes[0] = 'b'; break;
  447. case LF_CHR: modes[0] = 'c'; break;
  448. case LF_FIFO: modes[0] = 'p'; break;
  449. case LF_CONTIG: modes[0] = 'C'; break;
  450. }
  451. demode((unsigned)hstat.st_mode, modes+1);
  452. /* Timestamp */
  453. longie = hstat.st_mtime;
  454. timestamp = ctime(&longie);
  455. timestamp[16] = '\0';
  456. timestamp[24] = '\0';
  457. /* User and group names */
  458. if (*head->header.uname && head_standard) {
  459. user = head->header.uname;
  460. } else {
  461. user = uform;
  462. (void)sprintf(uform, "%d",
  463. from_oct (8, head->header.uid));
  464. }
  465. if (*head->header.gname && head_standard) {
  466. group = head->header.gname;
  467. } else {
  468. group = gform;
  469. (void)sprintf(gform, "%d",
  470. from_oct (8, head->header.gid));
  471. }
  472. /* Format the file size or major/minor device numbers */
  473. switch (head->header.linkflag) {
  474. #if defined(S_IFBLK) || defined(S_IFCHR)
  475. case LF_CHR:
  476. case LF_BLK:
  477. (void)sprintf(size, "%d,%d",
  478. major(hstat.st_rdev),
  479. minor(hstat.st_rdev));
  480. break;
  481. #endif
  482. case LF_SPARSE:
  483. (void)sprintf(size, "%ld",
  484. from_oct(1+12, head->header.realsize));
  485. break;
  486. default:
  487. (void)sprintf(size, "%ld", (long)hstat.st_size);
  488. }
  489. /* Figure out padding and print the whole line. */
  490. pad = strlen(user) + strlen(group) + strlen(size) + 1;
  491. if (pad > ugswidth) ugswidth = pad;
  492. name = quote_copy_string(head->header.name);
  493. if(!name)
  494. name=head->header.name;
  495. fprintf(msg_file, "%s %s/%s %*s%s %s %s %.*s",
  496. modes,
  497. user,
  498. group,
  499. ugswidth - pad,
  500. "",
  501. size,
  502. timestamp+4, timestamp+20,
  503. sizeof(head->header.name),
  504. name);
  505. if(name!=head->header.name)
  506. free(name);
  507. switch (head->header.linkflag) {
  508. case LF_SYMLINK:
  509. name=quote_copy_string(head->header.linkname);
  510. if(!name)
  511. name=head->header.linkname;
  512. fprintf(msg_file, " -> %s\n", name);
  513. if(name!=head->header.linkname)
  514. free(name);
  515. break;
  516. case LF_LINK:
  517. name=quote_copy_string(head->header.linkname);
  518. if(!name)
  519. name=head->header.linkname;
  520. fprintf(msg_file, " link to %s\n", head->header.linkname);
  521. if(name!=head->header.linkname)
  522. free(name);
  523. break;
  524. default:
  525. fprintf(msg_file, " unknown file type '%c'\n",
  526. head->header.linkflag);
  527. break;
  528. case LF_OLDNORMAL:
  529. case LF_NORMAL:
  530. case LF_SPARSE:
  531. case LF_CHR:
  532. case LF_BLK:
  533. case LF_DIR:
  534. case LF_FIFO:
  535. case LF_CONTIG:
  536. case LF_DUMPDIR:
  537. putc('\n', msg_file);
  538. break;
  539. case LF_VOLHDR:
  540. fprintf(msg_file, "--Volume Header--\n");
  541. break;
  542. case LF_MULTIVOL:
  543. fprintf(msg_file, "--Continued at byte %ld--\n",from_oct(1+12,head->header.offset));
  544. break;
  545. case LF_NAMES:
  546. fprintf(msg_file,"--Mangled file names--\n");
  547. break;
  548. }
  549. }
  550. fflush(msg_file);
  551. }
  552. /*
  553. * Print a similar line when we make a directory automatically.
  554. */
  555. void
  556. pr_mkdir(pathname, length, mode)
  557. char *pathname;
  558. int length;
  559. int mode;
  560. {
  561. char modes[11];
  562. char *name;
  563. extern long baserec;
  564. if (f_verbose > 1) {
  565. /* File type and modes */
  566. modes[0] = 'd';
  567. demode((unsigned)mode, modes+1);
  568. if(f_sayblock)
  569. fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  570. /* annofile(msg_file, (char *)NULL); */
  571. name=quote_copy_string(pathname);
  572. if(!name)
  573. name=pathname;
  574. fprintf(msg_file, "%s %*s %.*s\n",
  575. modes,
  576. ugswidth+DATEWIDTH,
  577. "Creating directory:",
  578. length,
  579. pathname);
  580. if(name!=pathname)
  581. free(name);
  582. }
  583. }
  584. /*
  585. * Skip over <size> bytes of data in records in the archive.
  586. */
  587. void
  588. skip_file(size)
  589. register long size;
  590. {
  591. union record *x;
  592. extern long save_totsize;
  593. extern long save_sizeleft;
  594. if(f_multivol) {
  595. save_totsize=size;
  596. save_sizeleft=size;
  597. }
  598. while (size > 0) {
  599. x = findrec();
  600. if (x == NULL) { /* Check it... */
  601. msg("Unexpected EOF on archive file");
  602. exit(EX_BADARCH);
  603. }
  604. userec(x);
  605. size -= RECORDSIZE;
  606. if(f_multivol)
  607. save_sizeleft-=RECORDSIZE;
  608. }
  609. }
  610. void
  611. skip_extended_headers()
  612. {
  613. register union record *exhdr;
  614. for (;;) {
  615. exhdr = findrec();
  616. if (!exhdr->ext_hdr.isextended) {
  617. userec(exhdr);
  618. break;
  619. }
  620. userec (exhdr);
  621. }
  622. }
  623. /*
  624. * Decode the mode string from a stat entry into a 9-char string and a null.
  625. */
  626. void
  627. demode(mode, string)
  628. register unsigned mode;
  629. register char *string;
  630. {
  631. register unsigned mask;
  632. register char *rwx = "rwxrwxrwx";
  633. for (mask = 0400; mask != 0; mask >>= 1) {
  634. if (mode & mask)
  635. *string++ = *rwx++;
  636. else {
  637. *string++ = '-';
  638. rwx++;
  639. }
  640. }
  641. if (mode & S_ISUID)
  642. if (string[-7] == 'x')
  643. string[-7] = 's';
  644. else
  645. string[-7] = 'S';
  646. if (mode & S_ISGID)
  647. if (string[-4] == 'x')
  648. string[-4] = 's';
  649. else
  650. string[-4] = 'S';
  651. if (mode & S_ISVTX)
  652. if (string[-1] == 'x')
  653. string[-1] = 't';
  654. else
  655. string[-1] = 'T';
  656. *string = '\0';
  657. }