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