]> 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, 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
301 recurse:
302
303 header = findrec();
304 head = header; /* This is our current header */
305 if (NULL == header)
306 return EOF;
307
308 recsum = from_oct(8, header->header.chksum);
309
310 sum = 0;
311 p = header->charptr;
312 for (i = sizeof(*header); --i >= 0;) {
313 /*
314 * We can't use unsigned char here because of old compilers,
315 * e.g. V7.
316 */
317 sum += 0xFF & *p++;
318 }
319
320 /* Adjust checksum to count the "chksum" field as blanks. */
321 for (i = sizeof(header->header.chksum); --i >= 0;)
322 sum -= 0xFF & header->header.chksum[i];
323 sum += ' '* sizeof header->header.chksum;
324
325 if (sum == 8*' ') {
326 /*
327 * This is a zeroed record...whole record is 0's except
328 * for the 8 blanks we faked for the checksum field.
329 */
330 return 2;
331 }
332
333 if (sum != recsum)
334 return 0;
335
336 /*
337 * Good record. Decode file size and return.
338 */
339 if (header->header.linkflag == LF_LINK)
340 hstat.st_size = 0; /* Links 0 size on tape */
341 else
342 hstat.st_size = from_oct(1+12, header->header.size);
343
344 header->header.arch_name[NAMSIZ-1] = '\0';
345 if (header->header.linkflag == LF_LONGNAME
346 || header->header.linkflag == LF_LONGLINK)
347 {
348 longp = ((header->header.linkflag == LF_LONGNAME)
349 ? &next_long_name
350 : &next_long_link);
351
352 userec (header);
353 if (*longp)
354 free (*longp);
355 bp = *longp = (char *) ck_malloc (hstat.st_size);
356
357 for (size = hstat.st_size;
358 size > 0;
359 size -= written)
360 {
361 data = findrec ()->charptr;
362 if (data == NULL)
363 {
364 msg ("Unexpected EOF on archive file");
365 break;
366 }
367 written = endofrecs () ->charptr - data;
368 if (written > size)
369 written = size;
370
371 bcopy (data, bp, written);
372 bp += written;
373 userec ((union record *) (data + written - 1));
374 }
375 goto recurse;
376 }
377 else
378 {
379 current_file_name = (next_long_name
380 ? next_long_name
381 : header->header.arch_name);
382 current_link_name = (next_long_link
383 ? next_long_link
384 : header->header.arch_linkname);
385 next_long_link = next_long_name = 0;
386 return 1;
387 }
388 }
389
390
391 /*
392 * Decode things from a file header record into a "struct stat".
393 * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
394 * Standard" tar format or regular old tar format.
395 *
396 * read_header() has already decoded the checksum and length, so we don't.
397 *
398 * If wantug != 0, we want the uid/group info decoded from Unix Standard
399 * tapes (for extraction). If == 0, we are just printing anyway, so save time.
400 *
401 * decode_header should NOT be called twice for the same record, since the
402 * two calls might use different "wantug" values and thus might end up with
403 * different uid/gid for the two calls. If anybody wants the uid/gid they
404 * should decode it first, and other callers should decode it without uid/gid
405 * before calling a routine, e.g. print_header, that assumes decoded data.
406 */
407 void
408 decode_header(header, st, stdp, wantug)
409 register union record *header;
410 register struct stat *st;
411 int *stdp;
412 int wantug;
413 {
414 long from_oct();
415 char **longp;
416 char *bp, *data;
417 int size, written;
418
419 st->st_mode = from_oct(8, header->header.mode);
420 st->st_mtime = from_oct(1+12, header->header.mtime);
421 if(f_gnudump) {
422 st->st_atime = from_oct(1+12, header->header.atime);
423 st->st_ctime = from_oct(1+12, header->header.ctime);
424 }
425
426 if (0==strcmp(header->header.magic, TMAGIC)) {
427 /* Unix Standard tar archive */
428 *stdp = 1;
429 if (wantug) {
430 #ifdef NONAMES
431 st->st_uid = from_oct(8, header->header.uid);
432 st->st_gid = from_oct(8, header->header.gid);
433 #else
434 st->st_uid =
435 (*header->header.uname
436 ? finduid (header->header.uname)
437 : from_oct (8, header->header.uid));
438 st->st_gid =
439 (*header->header.gname
440 ? findgid (header->header.gname)
441 : from_oct (8, header->header.gid));
442 #endif
443 }
444 #if defined(S_IFBLK) || defined(S_IFCHR)
445 switch (header->header.linkflag) {
446 case LF_BLK: case LF_CHR:
447 st->st_rdev = makedev(from_oct(8, header->header.devmajor),
448 from_oct(8, header->header.devminor));
449 }
450 #endif
451 } else {
452 /* Old fashioned tar archive */
453 *stdp = 0;
454 st->st_uid = from_oct(8, header->header.uid);
455 st->st_gid = from_oct(8, header->header.gid);
456 st->st_rdev = 0;
457 }
458 }
459
460
461 /*
462 * Quick and dirty octal conversion.
463 *
464 * Result is -1 if the field is invalid (all blank, or nonoctal).
465 */
466 long
467 from_oct(digs, where)
468 register int digs;
469 register char *where;
470 {
471 register long value;
472
473 while (isspace(*where)) { /* Skip spaces */
474 where++;
475 if (--digs <= 0)
476 return -1; /* All blank field */
477 }
478 value = 0;
479 while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
480 value = (value << 3) | (*where++ - '0');
481 --digs;
482 }
483
484 if (digs > 0 && *where && !isspace(*where))
485 return -1; /* Ended on non-space/nul */
486
487 return value;
488 }
489
490
491 /*
492 * Actually print it.
493 *
494 * Plain and fancy file header block logging.
495 * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
496 * This should just contain file names, so it can be fed back into tar
497 * with xargs or the "-T" option. The verbose option can give a bunch
498 * of info, one line per file. I doubt anybody tries to parse its
499 * format, or if they do, they shouldn't. Unix tar is pretty random here
500 * anyway.
501 *
502 * Note that print_header uses the globals <head>, <hstat>, and
503 * <head_standard>, which must be set up in advance. This is not very clean
504 * and should be cleaned up. FIXME.
505 */
506 #define UGSWIDTH 18 /* min width of User, group, size */
507 /* UGSWIDTH of 18 means that with user and group names <= 8 chars the columns
508 never shift during the listing. */
509 #define DATEWIDTH 19 /* Last mod date */
510 static int ugswidth = UGSWIDTH; /* Max width encountered so far */
511
512 void
513 print_header()
514 {
515 char modes[11];
516 char *timestamp;
517 char uform[11], gform[11]; /* These hold formatted ints */
518 char *user, *group;
519 char size[24]; /* Holds a formatted long or maj, min */
520 time_t longie; /* To make ctime() call portable */
521 int pad;
522 char *name;
523 extern long baserec;
524 static char *longname;
525 static char *longlink;
526 int bumplongs;
527
528 if(f_sayblock)
529 fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
530 /* annofile(msg_file, (char *)NULL); */
531
532 if (f_verbose <= 1) {
533 /* Just the fax, mam. */
534 char *name;
535
536 name=quote_copy_string(current_file_name);
537 if(name==0)
538 name=current_file_name;
539 fprintf(msg_file, "%s\n", name);
540 if(name!=current_file_name)
541 free(name);
542 } else {
543 /* File type and modes */
544 modes[0] = '?';
545 switch (head->header.linkflag) {
546 case LF_VOLHDR:
547 modes[0]='V';
548 break;
549
550 case LF_MULTIVOL:
551 modes[0]='M';
552 break;
553
554 case LF_NAMES:
555 modes[0]='N';
556 break;
557
558 case LF_LONGNAME:
559 case LF_LONGLINK:
560 msg ("Visible longname error\n");
561 break;
562
563 case LF_SPARSE:
564 case LF_NORMAL:
565 case LF_OLDNORMAL:
566 case LF_LINK:
567 modes[0] = '-';
568 if ('/' == current_file_name[strlen(current_file_name)-1])
569 modes[0] = 'd';
570 break;
571 case LF_DUMPDIR:modes[0] = 'd'; break;
572 case LF_DIR: modes[0] = 'd'; break;
573 case LF_SYMLINK:modes[0] = 'l'; break;
574 case LF_BLK: modes[0] = 'b'; break;
575 case LF_CHR: modes[0] = 'c'; break;
576 case LF_FIFO: modes[0] = 'p'; break;
577 case LF_CONTIG: modes[0] = 'C'; break;
578 }
579
580 demode((unsigned)hstat.st_mode, modes+1);
581
582 /* Timestamp */
583 longie = hstat.st_mtime;
584 timestamp = ctime(&longie);
585 timestamp[16] = '\0';
586 timestamp[24] = '\0';
587
588 /* User and group names */
589 if (*head->header.uname && head_standard) {
590 user = head->header.uname;
591 } else {
592 user = uform;
593 (void)sprintf(uform, "%d",
594 from_oct (8, head->header.uid));
595 }
596 if (*head->header.gname && head_standard) {
597 group = head->header.gname;
598 } else {
599 group = gform;
600 (void)sprintf(gform, "%d",
601 from_oct (8, head->header.gid));
602 }
603
604 /* Format the file size or major/minor device numbers */
605 switch (head->header.linkflag) {
606 #if defined(S_IFBLK) || defined(S_IFCHR)
607 case LF_CHR:
608 case LF_BLK:
609 (void)sprintf(size, "%d,%d",
610 major(hstat.st_rdev),
611 minor(hstat.st_rdev));
612 break;
613 #endif
614 case LF_SPARSE:
615 (void)sprintf(size, "%ld",
616 from_oct(1+12, head->header.realsize));
617 break;
618 default:
619 (void)sprintf(size, "%ld", (long)hstat.st_size);
620 }
621
622 /* Figure out padding and print the whole line. */
623 pad = strlen(user) + strlen(group) + strlen(size) + 1;
624 if (pad > ugswidth) ugswidth = pad;
625
626 name = quote_copy_string(current_file_name);
627 if(!name)
628 name=current_file_name;
629 fprintf(msg_file, "%s %s/%s %*s%s %s %s %s",
630 modes,
631 user,
632 group,
633 ugswidth - pad,
634 "",
635 size,
636 timestamp+4, timestamp+20,
637 name);
638
639 if(name!=current_file_name)
640 free(name);
641 switch (head->header.linkflag) {
642 case LF_SYMLINK:
643 name=quote_copy_string(current_link_name);
644 if(!name)
645 name=current_link_name;
646 fprintf(msg_file, " -> %s\n", name);
647 if(name!=current_link_name)
648 free(name);
649 break;
650
651 case LF_LINK:
652 name=quote_copy_string(current_link_name);
653 if(!name)
654 name=current_link_name;
655 fprintf(msg_file, " link to %s\n", current_link_name);
656 if(name!=current_link_name)
657 free(name);
658 break;
659
660 default:
661 fprintf(msg_file, " unknown file type '%c'\n",
662 head->header.linkflag);
663 break;
664
665 case LF_OLDNORMAL:
666 case LF_NORMAL:
667 case LF_SPARSE:
668 case LF_CHR:
669 case LF_BLK:
670 case LF_DIR:
671 case LF_FIFO:
672 case LF_CONTIG:
673 case LF_DUMPDIR:
674 putc('\n', msg_file);
675 break;
676
677 case LF_VOLHDR:
678 fprintf(msg_file, "--Volume Header--\n");
679 break;
680
681 case LF_MULTIVOL:
682 fprintf(msg_file, "--Continued at byte %ld--\n",from_oct(1+12,head->header.offset));
683 break;
684
685 case LF_NAMES:
686 fprintf(msg_file,"--Mangled file names--\n");
687 break;
688 }
689 }
690 fflush(msg_file);
691 }
692
693 /*
694 * Print a similar line when we make a directory automatically.
695 */
696 void
697 pr_mkdir(pathname, length, mode)
698 char *pathname;
699 int length;
700 int mode;
701 {
702 char modes[11];
703 char *name;
704 extern long baserec;
705
706 if (f_verbose > 1) {
707 /* File type and modes */
708 modes[0] = 'd';
709 demode((unsigned)mode, modes+1);
710
711 if(f_sayblock)
712 fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
713 /* annofile(msg_file, (char *)NULL); */
714 name=quote_copy_string(pathname);
715 if(!name)
716 name=pathname;
717 fprintf(msg_file, "%s %*s %.*s\n",
718 modes,
719 ugswidth+DATEWIDTH,
720 "Creating directory:",
721 length,
722 pathname);
723 if(name!=pathname)
724 free(name);
725 }
726 }
727
728
729 /*
730 * Skip over <size> bytes of data in records in the archive.
731 */
732 void
733 skip_file(size)
734 register long size;
735 {
736 union record *x;
737 extern long save_totsize;
738 extern long save_sizeleft;
739
740 if(f_multivol) {
741 save_totsize=size;
742 save_sizeleft=size;
743 }
744
745 while (size > 0) {
746 x = findrec();
747 if (x == NULL) { /* Check it... */
748 msg("Unexpected EOF on archive file");
749 exit(EX_BADARCH);
750 }
751 userec(x);
752 size -= RECORDSIZE;
753 if(f_multivol)
754 save_sizeleft-=RECORDSIZE;
755 }
756 }
757
758 void
759 skip_extended_headers()
760 {
761 register union record *exhdr;
762
763 for (;;) {
764 exhdr = findrec();
765 if (!exhdr->ext_hdr.isextended) {
766 userec(exhdr);
767 break;
768 }
769 userec (exhdr);
770 }
771 }
772
773 /*
774 * Decode the mode string from a stat entry into a 9-char string and a null.
775 */
776 void
777 demode(mode, string)
778 register unsigned mode;
779 register char *string;
780 {
781 register unsigned mask;
782 register char *rwx = "rwxrwxrwx";
783
784 for (mask = 0400; mask != 0; mask >>= 1) {
785 if (mode & mask)
786 *string++ = *rwx++;
787 else {
788 *string++ = '-';
789 rwx++;
790 }
791 }
792
793 if (mode & S_ISUID)
794 if (string[-7] == 'x')
795 string[-7] = 's';
796 else
797 string[-7] = 'S';
798 if (mode & S_ISGID)
799 if (string[-4] == 'x')
800 string[-4] = 's';
801 else
802 string[-4] = 'S';
803 if (mode & S_ISVTX)
804 if (string[-1] == 'x')
805 string[-1] = 't';
806 else
807 string[-1] = 'T';
808 *string = '\0';
809 }
This page took 0.070805 seconds and 5 git commands to generate.