]>
Dogcows Code - chaz/tar/blob - src/list.c
2 Copyright (C) 1988, 1992, 1993 Free Software Foundation
4 This file is part of GNU Tar.
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)
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.
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. */
23 * Also includes support routines for reading a tar archive.
25 * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
30 #include <sys/types.h>
45 #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
50 extern FILE *msg_file
;
52 long from_oct (); /* Decode octal number */
53 void demode (); /* Print file mode */
54 void restore_saved_dir_info ();
57 union record
*head
; /* Points to current archive header */
58 struct stat hstat
; /* Stat struct corresponding */
59 int head_standard
; /* Tape header is in ANSI format */
62 void close_archive ();
63 void decode_header ();
68 void names_notfound ();
74 void skip_extended_headers ();
76 extern char *quote_copy_string ();
80 * Main loop for reading an archive.
83 read_and (do_something
)
84 void (*do_something
) ();
86 int status
= 3; /* Initial status at start of archive */
88 extern time_t new_time
;
91 name_gather (); /* Gather all the names */
92 open_archive (1); /* Open for reading */
97 status
= read_header ();
101 case 1: /* Valid header */
102 /* We should decode next field (mode) first... */
103 /* Ensure incoming names are null terminated. */
105 if (!name_match (current_file_name
)
106 || (f_new_files
&& hstat
.st_mtime
< new_time
)
107 || (f_exclude
&& check_exclude (current_file_name
)))
112 if (head
->header
.linkflag
== LF_VOLHDR
113 || head
->header
.linkflag
== LF_MULTIVOL
114 || head
->header
.linkflag
== LF_NAMES
)
119 if (f_show_omitted_dirs
120 && head
->header
.linkflag
== LF_DIR
)
121 msg ("Omitting %s\n", current_file_name
);
122 /* Skip past it in the archive */
123 if (head
->header
.isextended
)
125 save_linkflag
= head
->header
.linkflag
;
129 /* register union record *exhdr;
133 if (!exhdr->ext_hdr.isextended) {
139 skip_extended_headers ();
141 /* Skip to the next header on the archive */
142 if (save_linkflag
!= LF_DIR
)
143 skip_file ((long) hstat
.st_size
);
152 * If the previous header was good, tell them
153 * that we are skipping bad ones.
155 case 0: /* Invalid header */
159 case 3: /* Error on first record */
160 msg ("Hmm, this doesn't look like a tar archive.");
162 case 2: /* Error after record of zeroes */
163 case 1: /* Error after header rec */
164 msg ("Skipping to next file header...");
165 case 0: /* Error after error */
170 case 2: /* Record of zeroes */
172 status
= prev_status
; /* If error after 0's */
176 case EOF
: /* End of archive */
182 restore_saved_dir_info ();
184 names_notfound (); /* Print names not found */
189 * Print a header record, based on tar options.
194 extern char *save_name
;
195 int isextended
= 0; /* Flag to remember if head is extended */
197 /* Save the record */
200 /* Print the header record */
204 decode_header (head
, &hstat
, &head_standard
, 0);
208 if (f_gnudump
&& head
->header
.linkflag
== LF_DUMPDIR
)
210 size_t size
, written
, check
;
212 extern long save_totsize
;
213 extern long save_sizeleft
;
218 save_name
= current_file_name
;
219 save_totsize
= hstat
.st_size
;
221 for (size
= hstat
.st_size
; size
> 0; size
-= written
)
224 save_sizeleft
= size
;
225 data
= findrec ()->charptr
;
228 msg ("EOF in archive file?");
231 written
= endofrecs ()->charptr
- data
;
235 check
= fwrite (data
, sizeof (char), written
, msg_file
);
236 userec ((union record
*) (data
+ written
- 1));
237 if (check
!= written
)
239 msg_perror ("only wrote %ld of %ld bytes to file %s", check
, written
, current_file_name
);
240 skip_file ((long) (size
) - written
);
246 saverec ((union record
**) 0); /* Unsave it */
247 fputc ('\n', msg_file
);
252 saverec ((union record
**) 0);/* Unsave it */
253 /* Check to see if we have an extended header to skip over also */
254 if (head
->header
.isextended
)
257 /* Skip past the header in the archive */
261 * If we needed to skip any extended headers, do so now, by
262 * reading extended headers and skipping past them in the
267 /* register union record *exhdr;
272 if (!exhdr->ext_hdr.isextended) {
278 skip_extended_headers ();
282 save_name
= current_file_name
;
283 /* Skip to the next header on the archive */
285 skip_file ((long) hstat
.st_size
);
293 * Read a record that's supposed to be a header record.
294 * Return its address in "head", and if it is good, the file's
295 * size in hstat.st_size.
297 * Return 1 for success, 0 if the checksum is bad, EOF on eof,
298 * 2 for a record full of zeros (EOF marker).
300 * You must always userec(head) to skip past the header which this
307 register long sum
, signed_sum
, recsum
;
309 register union record
*header
;
314 static char *next_long_name
, *next_long_link
;
320 head
= header
; /* This is our current header */
324 recsum
= from_oct (8, header
->header
.chksum
);
328 for (i
= sizeof (*header
); --i
>= 0;)
331 * We can't use unsigned char here because of old compilers,
338 /* Adjust checksum to count the "chksum" field as blanks. */
339 for (i
= sizeof (header
->header
.chksum
); --i
>= 0;)
341 sum
-= 0xFF & header
->header
.chksum
[i
];
342 signed_sum
-= (char) header
->header
.chksum
[i
];
344 sum
+= ' ' * sizeof header
->header
.chksum
;
345 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
350 * This is a zeroed record...whole record is 0's except
351 * for the 8 blanks we faked for the checksum field.
356 if (sum
!= recsum
&& signed_sum
!= recsum
)
360 * Good record. Decode file size and return.
362 if (header
->header
.linkflag
== LF_LINK
)
363 hstat
.st_size
= 0; /* Links 0 size on tape */
365 hstat
.st_size
= from_oct (1 + 12, header
->header
.size
);
367 header
->header
.arch_name
[NAMSIZ
- 1] = '\0';
368 if (header
->header
.linkflag
== LF_LONGNAME
369 || header
->header
.linkflag
== LF_LONGLINK
)
371 longp
= ((header
->header
.linkflag
== LF_LONGNAME
)
378 bp
= *longp
= (char *) ck_malloc (hstat
.st_size
);
380 for (size
= hstat
.st_size
;
384 data
= findrec ()->charptr
;
387 msg ("Unexpected EOF on archive file");
390 written
= endofrecs ()->charptr
- data
;
394 bcopy (data
, bp
, written
);
396 userec ((union record
*) (data
+ written
- 1));
402 name
= (next_long_name
404 : head
->header
.arch_name
);
405 if (current_file_name
)
406 free (current_file_name
);
407 current_file_name
= ck_malloc (strlen (name
) + 1);
408 strcpy (current_file_name
, name
);
410 name
= (next_long_link
412 : head
->header
.arch_linkname
);
413 if (current_link_name
)
414 free (current_link_name
);
415 current_link_name
= ck_malloc (strlen (name
) + 1);
416 strcpy (current_link_name
, name
);
418 next_long_link
= next_long_name
= 0;
425 * Decode things from a file header record into a "struct stat".
426 * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
427 * Standard" tar format or regular old tar format.
429 * read_header() has already decoded the checksum and length, so we don't.
431 * If wantug != 0, we want the uid/group info decoded from Unix Standard
432 * tapes (for extraction). If == 0, we are just printing anyway, so save time.
434 * decode_header should NOT be called twice for the same record, since the
435 * two calls might use different "wantug" values and thus might end up with
436 * different uid/gid for the two calls. If anybody wants the uid/gid they
437 * should decode it first, and other callers should decode it without uid/gid
438 * before calling a routine, e.g. print_header, that assumes decoded data.
441 decode_header (header
, st
, stdp
, wantug
)
442 register union record
*header
;
443 register struct stat
*st
;
449 st
->st_mode
= from_oct (8, header
->header
.mode
);
450 st
->st_mtime
= from_oct (1 + 12, header
->header
.mtime
);
453 st
->st_atime
= from_oct (1 + 12, header
->header
.atime
);
454 st
->st_ctime
= from_oct (1 + 12, header
->header
.ctime
);
457 if (0 == strcmp (header
->header
.magic
, TMAGIC
))
459 /* Unix Standard tar archive */
464 st
->st_uid
= from_oct (8, header
->header
.uid
);
465 st
->st_gid
= from_oct (8, header
->header
.gid
);
468 (*header
->header
.uname
469 ? finduid (header
->header
.uname
)
470 : from_oct (8, header
->header
.uid
));
472 (*header
->header
.gname
473 ? findgid (header
->header
.gname
)
474 : from_oct (8, header
->header
.gid
));
477 #if defined(S_IFBLK) || defined(S_IFCHR)
478 switch (header
->header
.linkflag
)
482 st
->st_rdev
= makedev (from_oct (8, header
->header
.devmajor
),
483 from_oct (8, header
->header
.devminor
));
489 /* Old fashioned tar archive */
491 st
->st_uid
= from_oct (8, header
->header
.uid
);
492 st
->st_gid
= from_oct (8, header
->header
.gid
);
499 * Quick and dirty octal conversion.
501 * Result is -1 if the field is invalid (all blank, or nonoctal).
504 from_oct (digs
, where
)
506 register char *where
;
510 while (isspace (*where
))
514 return -1; /* All blank field */
517 while (digs
> 0 && isodigit (*where
))
518 { /* Scan til nonoctal */
519 value
= (value
<< 3) | (*where
++ - '0');
523 if (digs
> 0 && *where
&& !isspace (*where
))
524 return -1; /* Ended on non-space/nul */
533 * Plain and fancy file header block logging.
534 * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
535 * This should just contain file names, so it can be fed back into tar
536 * with xargs or the "-T" option. The verbose option can give a bunch
537 * of info, one line per file. I doubt anybody tries to parse its
538 * format, or if they do, they shouldn't. Unix tar is pretty random here
541 * Note that print_header uses the globals <head>, <hstat>, and
542 * <head_standard>, which must be set up in advance. This is not very clean
543 * and should be cleaned up. FIXME.
545 #define UGSWIDTH 18 /* min width of User, group, size */
546 /* UGSWIDTH of 18 means that with user and group names <= 8 chars the columns
547 never shift during the listing. */
548 #define DATEWIDTH 19 /* Last mod date */
549 static int ugswidth
= UGSWIDTH
; /* Max width encountered so far */
556 char uform
[11], gform
[11]; /* These hold formatted ints */
558 char size
[24]; /* Holds a formatted long or maj, min */
559 time_t longie
; /* To make ctime() call portable */
565 fprintf (msg_file
, "rec %10d: ", baserec
+ (ar_record
- ar_block
));
566 /* annofile(msg_file, (char *)NULL); */
570 /* Just the fax, mam. */
573 name
= quote_copy_string (current_file_name
);
575 name
= current_file_name
;
576 fprintf (msg_file
, "%s\n", name
);
577 if (name
!= current_file_name
)
582 /* File type and modes */
584 switch (head
->header
.linkflag
)
600 msg ("Visible longname error\n");
608 if ('/' == current_file_name
[strlen (current_file_name
) - 1])
634 demode ((unsigned) hstat
.st_mode
, modes
+ 1);
637 longie
= hstat
.st_mtime
;
638 timestamp
= ctime (&longie
);
639 timestamp
[16] = '\0';
640 timestamp
[24] = '\0';
642 /* User and group names */
643 if (*head
->header
.uname
&& head_standard
)
645 user
= head
->header
.uname
;
650 (void) sprintf (uform
, "%d",
651 from_oct (8, head
->header
.uid
));
653 if (*head
->header
.gname
&& head_standard
)
655 group
= head
->header
.gname
;
660 (void) sprintf (gform
, "%d",
661 from_oct (8, head
->header
.gid
));
664 /* Format the file size or major/minor device numbers */
665 switch (head
->header
.linkflag
)
667 #if defined(S_IFBLK) || defined(S_IFCHR)
670 (void) sprintf (size
, "%d,%d",
671 major (hstat
.st_rdev
),
672 minor (hstat
.st_rdev
));
676 (void) sprintf (size
, "%ld",
677 from_oct (1 + 12, head
->header
.realsize
));
680 (void) sprintf (size
, "%ld", (long) hstat
.st_size
);
683 /* Figure out padding and print the whole line. */
684 pad
= strlen (user
) + strlen (group
) + strlen (size
) + 1;
688 name
= quote_copy_string (current_file_name
);
690 name
= current_file_name
;
691 fprintf (msg_file
, "%s %s/%s %*s%s %s %s %s",
698 timestamp
+ 4, timestamp
+ 20,
701 if (name
!= current_file_name
)
703 switch (head
->header
.linkflag
)
706 name
= quote_copy_string (current_link_name
);
708 name
= current_link_name
;
709 fprintf (msg_file
, " -> %s\n", name
);
710 if (name
!= current_link_name
)
715 name
= quote_copy_string (current_link_name
);
717 name
= current_link_name
;
718 fprintf (msg_file
, " link to %s\n", current_link_name
);
719 if (name
!= current_link_name
)
724 fprintf (msg_file
, " unknown file type '%c'\n",
725 head
->header
.linkflag
);
737 putc ('\n', msg_file
);
741 fprintf (msg_file
, "--Volume Header--\n");
745 fprintf (msg_file
, "--Continued at byte %ld--\n", from_oct (1 + 12, head
->header
.offset
));
749 fprintf (msg_file
, "--Mangled file names--\n");
757 * Print a similar line when we make a directory automatically.
760 pr_mkdir (pathname
, length
, mode
)
771 /* File type and modes */
773 demode ((unsigned) mode
, modes
+ 1);
776 fprintf (msg_file
, "rec %10d: ", baserec
+ (ar_record
- ar_block
));
777 /* annofile(msg_file, (char *)NULL); */
778 name
= quote_copy_string (pathname
);
781 fprintf (msg_file
, "%s %*s %.*s\n",
783 ugswidth
+ DATEWIDTH
,
784 "Creating directory:",
787 if (name
!= pathname
)
794 * Skip over <size> bytes of data in records in the archive.
801 extern long save_totsize
;
802 extern long save_sizeleft
;
807 save_sizeleft
= size
;
815 msg ("Unexpected EOF on archive file");
821 save_sizeleft
-= RECORDSIZE
;
826 skip_extended_headers ()
828 register union record
*exhdr
;
833 if (!exhdr
->ext_hdr
.isextended
)
843 * Decode the mode string from a stat entry into a 9-char string and a null.
846 demode (mode
, string
)
847 register unsigned mode
;
848 register char *string
;
850 register unsigned mask
;
851 register char *rwx
= "rwxrwxrwx";
853 for (mask
= 0400; mask
!= 0; mask
>>= 1)
865 if (string
[-7] == 'x')
870 if (string
[-4] == 'x')
875 if (string
[-1] == 'x')
This page took 0.072081 seconds and 5 git commands to generate.