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