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