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