1 /* copyin.c - extract or list a cpio archive
2 Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 $FreeBSD: src/contrib/cpio/copyin.c,v 1.6.6.1 2002/03/12 19:10:14 phantom Exp $
22 #include <sys/types.h>
24 #ifdef HAVE_SYS_PARAM_H
25 #include <sys/param.h>
27 #if (defined(BSD) && (BSD >= 199306))
31 #include "filetypes.h"
41 #if defined(HAVE_STRFTIME) && defined(__FreeBSD__)
49 static void read_pattern_file ();
50 static void tape_skip_padding ();
51 static void defer_copyin ();
52 static void create_defered_links ();
53 static void create_final_defers ();
55 /* Return 16-bit integer I with the bytes swapped. */
56 #define swab_short(i) ((((i) << 8) & 0xff00) | (((i) >> 8) & 0x00ff))
58 /* Read the header, including the name of the file, from file
59 descriptor IN_DES into FILE_HDR. */
62 read_in_header (file_hdr, in_des)
63 struct new_cpio_header *file_hdr;
66 long bytes_skipped = 0; /* Bytes of junk found before magic number. */
68 /* Search for a valid magic number. */
70 if (archive_format == arf_unknown)
76 while (archive_format == arf_unknown)
78 peeked_bytes = tape_buffered_peek (tmpbuf, in_des, 512);
80 error (1, 0, "premature end of archive");
82 if (!strncmp (tmpbuf, "070701", 6))
83 archive_format = arf_newascii;
84 else if (!strncmp (tmpbuf, "070707", 6))
85 archive_format = arf_oldascii;
86 else if (!strncmp (tmpbuf, "070702", 6))
88 archive_format = arf_crcascii;
91 else if ((*((unsigned short *) tmpbuf) == 070707) ||
92 (*((unsigned short *) tmpbuf) == swab_short ((unsigned short) 070707)))
93 archive_format = arf_binary;
94 else if (peeked_bytes >= 512
95 && (check_tar = is_tar_header (tmpbuf)))
98 archive_format = arf_ustar;
100 archive_format = arf_tar;
104 tape_buffered_read ((char *) tmpbuf, in_des, 1L);
110 if (archive_format == arf_tar || archive_format == arf_ustar)
113 last_header_start = input_bytes - io_block_size +
114 (in_buff - input_buffer);
115 if (bytes_skipped > 0)
116 error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
117 read_in_tar_header (file_hdr, in_des);
121 file_hdr->c_tar_linkname = NULL;
123 tape_buffered_read ((char *) file_hdr, in_des, 6L);
127 last_header_start = input_bytes - io_block_size
128 + (in_buff - input_buffer) - 6;
129 if (archive_format == arf_newascii
130 && !strncmp ((char *) file_hdr, "070701", 6))
132 if (bytes_skipped > 0)
133 error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
134 read_in_new_ascii (file_hdr, in_des);
137 if (archive_format == arf_crcascii
138 && !strncmp ((char *) file_hdr, "070702", 6))
140 if (bytes_skipped > 0)
141 error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
142 read_in_new_ascii (file_hdr, in_des);
145 if ( (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
146 && !strncmp ((char *) file_hdr, "070707", 6))
148 if (bytes_skipped > 0)
149 error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
150 read_in_old_ascii (file_hdr, in_des);
153 if ( (archive_format == arf_binary || archive_format == arf_hpbinary)
154 && (file_hdr->c_magic == 070707
155 || file_hdr->c_magic == swab_short ((unsigned short) 070707)))
157 /* Having to skip 1 byte because of word alignment is normal. */
158 if (bytes_skipped > 0)
159 error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
160 read_in_binary (file_hdr, in_des);
164 bcopy ((char *) file_hdr + 1, (char *) file_hdr, 5);
165 tape_buffered_read ((char *) file_hdr + 5, in_des, 1L);
169 /* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
170 file descriptor IN_DES, except for the magic number, which is
171 already filled in. */
174 read_in_old_ascii (file_hdr, in_des)
175 struct new_cpio_header *file_hdr;
178 char ascii_header[78];
182 tape_buffered_read (ascii_header, in_des, 70L);
183 ascii_header[70] = '\0';
184 sscanf (ascii_header,
185 "%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
186 &dev, &file_hdr->c_ino,
187 &file_hdr->c_mode, &file_hdr->c_uid, &file_hdr->c_gid,
188 &file_hdr->c_nlink, &rdev, &file_hdr->c_mtime,
189 &file_hdr->c_namesize, &file_hdr->c_filesize);
190 file_hdr->c_dev_maj = major (dev);
191 file_hdr->c_dev_min = minor (dev);
192 file_hdr->c_rdev_maj = major (rdev);
193 file_hdr->c_rdev_min = minor (rdev);
195 /* Read file name from input. */
196 if (file_hdr->c_name != NULL)
197 free (file_hdr->c_name);
198 file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize + 1);
199 tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
201 /* HP/UX cpio creates archives that look just like ordinary archives,
202 but for devices it sets major = 0, minor = 1, and puts the
203 actual major/minor number in the filesize field. See if this
204 is an HP/UX cpio archive, and if so fix it. We have to do this
205 here because process_copy_in() assumes filesize is always 0
207 switch (file_hdr->c_mode & CP_IFMT)
217 if (file_hdr->c_filesize != 0
218 && file_hdr->c_rdev_maj == 0
219 && file_hdr->c_rdev_min == 1)
221 file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
222 file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
223 file_hdr->c_filesize = 0;
229 #endif /* __MSDOS__ */
232 /* Fill in FILE_HDR by reading a new-format ASCII format cpio header from
233 file descriptor IN_DES, except for the magic number, which is
234 already filled in. */
237 read_in_new_ascii (file_hdr, in_des)
238 struct new_cpio_header *file_hdr;
241 char ascii_header[112];
243 tape_buffered_read (ascii_header, in_des, 104L);
244 ascii_header[104] = '\0';
245 sscanf (ascii_header,
246 "%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx",
247 &file_hdr->c_ino, &file_hdr->c_mode, &file_hdr->c_uid,
248 &file_hdr->c_gid, &file_hdr->c_nlink, &file_hdr->c_mtime,
249 &file_hdr->c_filesize, &file_hdr->c_dev_maj, &file_hdr->c_dev_min,
250 &file_hdr->c_rdev_maj, &file_hdr->c_rdev_min, &file_hdr->c_namesize,
251 &file_hdr->c_chksum);
252 /* Read file name from input. */
253 if (file_hdr->c_name != NULL)
254 free (file_hdr->c_name);
255 file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
256 tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
258 /* In SVR4 ASCII format, the amount of space allocated for the header
259 is rounded up to the next long-word, so we might need to drop
261 tape_skip_padding (in_des, file_hdr->c_namesize + 110);
264 /* Fill in FILE_HDR by reading a binary format cpio header from
265 file descriptor IN_DES, except for the first 6 bytes (the magic
266 number, device, and inode number), which are already filled in. */
269 read_in_binary (file_hdr, in_des)
270 struct new_cpio_header *file_hdr;
273 struct old_cpio_header short_hdr;
275 /* Copy the data into the short header, then later transfer
276 it into the argument long header. */
277 short_hdr.c_dev = ((struct old_cpio_header *) file_hdr)->c_dev;
278 short_hdr.c_ino = ((struct old_cpio_header *) file_hdr)->c_ino;
279 tape_buffered_read (((char *) &short_hdr) + 6, in_des, 20L);
281 /* If the magic number is byte swapped, fix the header. */
282 if (file_hdr->c_magic == swab_short ((unsigned short) 070707))
284 static int warned = 0;
286 /* Alert the user that they might have to do byte swapping on
287 the file contents. */
290 error (0, 0, "warning: archive header has reverse byte-order");
293 swab_array ((char *) &short_hdr, 13);
296 file_hdr->c_dev_maj = major (short_hdr.c_dev);
297 file_hdr->c_dev_min = minor (short_hdr.c_dev);
298 file_hdr->c_ino = short_hdr.c_ino;
299 file_hdr->c_mode = short_hdr.c_mode;
300 file_hdr->c_uid = short_hdr.c_uid;
301 file_hdr->c_gid = short_hdr.c_gid;
302 file_hdr->c_nlink = short_hdr.c_nlink;
303 file_hdr->c_rdev_maj = major (short_hdr.c_rdev);
304 file_hdr->c_rdev_min = minor (short_hdr.c_rdev);
305 file_hdr->c_mtime = (unsigned long) short_hdr.c_mtimes[0] << 16
306 | short_hdr.c_mtimes[1];
308 file_hdr->c_namesize = short_hdr.c_namesize;
309 file_hdr->c_filesize = (unsigned long) short_hdr.c_filesizes[0] << 16
310 | short_hdr.c_filesizes[1];
312 /* Read file name from input. */
313 if (file_hdr->c_name != NULL)
314 free (file_hdr->c_name);
315 file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
316 tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
318 /* In binary mode, the amount of space allocated in the header for
319 the filename is `c_namesize' rounded up to the next short-word,
320 so we might need to drop a byte. */
321 if (file_hdr->c_namesize % 2)
322 tape_toss_input (in_des, 1L);
325 /* HP/UX cpio creates archives that look just like ordinary archives,
326 but for devices it sets major = 0, minor = 1, and puts the
327 actual major/minor number in the filesize field. See if this
328 is an HP/UX cpio archive, and if so fix it. We have to do this
329 here because process_copy_in() assumes filesize is always 0
331 switch (file_hdr->c_mode & CP_IFMT)
341 if (file_hdr->c_filesize != 0
342 && file_hdr->c_rdev_maj == 0
343 && file_hdr->c_rdev_min == 1)
345 file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
346 file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
347 file_hdr->c_filesize = 0;
353 #endif /* __MSDOS__ */
356 /* Exchange the bytes of each element of the array of COUNT shorts
360 swab_array (ptr, count)
376 /* Current time for verbose table. */
377 static time_t current_time;
379 /* Read the collection from standard input and create files
380 in the file system. */
385 char done = FALSE; /* True if trailer reached. */
386 int res; /* Result of various function calls. */
387 dynamic_string new_name; /* New file name for rename option. */
388 FILE *tty_in; /* Interactive file for rename option. */
389 FILE *tty_out; /* Interactive file for rename option. */
390 FILE *rename_in; /* Batch file for rename option. */
391 char *str_res; /* Result for string function. */
392 struct utimbuf times; /* For setting file times. */
393 struct stat file_stat; /* Output file stat record. */
394 struct new_cpio_header file_hdr; /* Output header information. */
395 int out_file_des; /* Output file descriptor. */
396 int in_file_des; /* Input file descriptor. */
397 char skip_file; /* Flag for use with patterns. */
398 int existing_dir; /* True if file is a dir & already exists. */
399 int i; /* Loop index variable. */
400 char *link_name = NULL; /* Name of hard and symbolic links. */
402 int cdf_flag; /* True if file is a CDF. */
403 int cdf_char; /* Index of `+' char indicating a CDF. */
406 /* Initialize the copy in. */
407 if (pattern_file_name)
408 read_pattern_file ();
409 file_hdr.c_name = NULL;
410 ds_init (&new_name, 128);
411 /* Initialize this in case it has members we don't know to set. */
412 bzero (×, sizeof (struct utimbuf));
414 if (rename_batch_file)
416 rename_in = fopen (rename_batch_file, "r");
417 if (rename_in == NULL)
418 error (2, errno, CONSOLE);
420 else if (rename_flag)
422 /* Open interactive file pair for rename operation. */
423 tty_in = fopen (CONSOLE, "r");
425 error (2, errno, CONSOLE);
426 tty_out = fopen (CONSOLE, "w");
428 error (2, errno, CONSOLE);
431 /* Get date and time if needed for processing the table option. */
432 if (table_flag && verbose_flag)
433 time (¤t_time);
436 setmode (archive_des, O_BINARY);
438 /* Check whether the input file might be a tape. */
439 in_file_des = archive_des;
440 if (_isrmt (in_file_des))
442 input_is_special = 1;
443 input_is_seekable = 0;
447 if (fstat (in_file_des, &file_stat))
448 error (1, errno, "standard input is closed");
451 S_ISBLK (file_stat.st_mode) ||
453 S_ISCHR (file_stat.st_mode);
454 input_is_seekable = S_ISREG (file_stat.st_mode);
456 output_is_seekable = TRUE;
458 /* While there is more input in the collection, process the input. */
462 swapping_halfwords = swapping_bytes = FALSE;
464 /* Start processing the next file by reading the header. */
465 read_in_header (&file_hdr, in_file_des);
470 struct new_cpio_header *h;
473 "magic = 0%o, ino = %d, mode = 0%o, uid = %d, gid = %d\n",
474 h->c_magic, h->c_ino, h->c_mode, h->c_uid, h->c_gid);
476 "nlink = %d, mtime = %d, filesize = %d, dev_maj = 0x%x\n",
477 h->c_nlink, h->c_mtime, h->c_filesize, h->c_dev_maj);
479 "dev_min = 0x%x, rdev_maj = 0x%x, rdev_min = 0x%x, namesize = %d\n",
480 h->c_dev_min, h->c_rdev_maj, h->c_rdev_min, h->c_namesize);
482 "chksum = %d, name = \"%s\", tar_linkname = \"%s\"\n",
483 h->c_chksum, h->c_name,
484 h->c_tar_linkname ? h->c_tar_linkname : "(null)" );
488 /* Is this the header for the TRAILER file? */
489 if (strcmp ("TRAILER!!!", file_hdr.c_name) == 0)
495 /* Do we have to ignore absolute paths, and if so, does the filename
496 have an absolute path? */
497 if (no_abs_paths_flag && file_hdr.c_name && file_hdr.c_name [0] == '/')
506 strcpy (file_hdr.c_name, ".");
512 non_abs_name = (char *) xmalloc (strlen (p) + 1);
513 strcpy (non_abs_name, p);
514 free (file_hdr.c_name);
515 file_hdr.c_name = non_abs_name;
519 /* Does the file name match one of the given patterns? */
520 if (num_patterns <= 0)
524 skip_file = copy_matching_files;
525 for (i = 0; i < num_patterns
526 && skip_file == copy_matching_files; i++)
528 if (fnmatch (save_patterns[i], file_hdr.c_name, 0) == 0)
529 skip_file = !copy_matching_files;
535 tape_toss_input (in_file_des, file_hdr.c_filesize);
536 tape_skip_padding (in_file_des, file_hdr.c_filesize);
543 if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
545 if (archive_format != arf_tar && archive_format != arf_ustar)
547 link_name = (char *) xmalloc ((unsigned int) file_hdr.c_filesize + 1);
548 link_name[file_hdr.c_filesize] = '\0';
549 tape_buffered_read (link_name, in_file_des, file_hdr.c_filesize);
550 long_format (&file_hdr, link_name);
552 tape_skip_padding (in_file_des, file_hdr.c_filesize);
557 long_format (&file_hdr, file_hdr.c_tar_linkname);
563 long_format (&file_hdr, (char *) 0);
566 printf ("%s\n", file_hdr.c_name);
569 tape_toss_input (in_file_des, file_hdr.c_filesize);
570 tape_skip_padding (in_file_des, file_hdr.c_filesize);
571 if (only_verify_crc_flag)
574 if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
575 continue; /* links don't have a checksum */
577 if (crc != file_hdr.c_chksum)
578 error (0, 0, "%s: checksum error (0x%x, should be 0x%x)",
579 file_hdr.c_name, crc, file_hdr.c_chksum);
582 else if (append_flag)
584 tape_toss_input (in_file_des, file_hdr.c_filesize);
585 tape_skip_padding (in_file_des, file_hdr.c_filesize);
587 else if (only_verify_crc_flag)
590 if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
592 if (archive_format != arf_tar && archive_format != arf_ustar)
594 tape_toss_input (in_file_des, file_hdr.c_filesize);
595 tape_skip_padding (in_file_des, file_hdr.c_filesize);
601 tape_toss_input (in_file_des, file_hdr.c_filesize);
602 tape_skip_padding (in_file_des, file_hdr.c_filesize);
603 if (crc != file_hdr.c_chksum)
604 error (0, 0, "%s: checksum error (0x%x, should be 0x%x)",
605 file_hdr.c_name, crc, file_hdr.c_chksum);
609 /* Copy the input file into the directory structure. */
611 /* Do we need to rename the file? */
612 if (rename_flag || rename_batch_file)
616 fprintf (tty_out, "rename %s -> ", file_hdr.c_name);
618 str_res = ds_fgets (tty_in, &new_name);
622 str_res = ds_fgetstr (rename_in, &new_name, '\n');
624 if (str_res == NULL || str_res[0] == 0)
626 tape_toss_input (in_file_des, file_hdr.c_filesize);
627 tape_skip_padding (in_file_des, file_hdr.c_filesize);
631 file_hdr.c_name = xstrdup (new_name.ds_string);
634 /* See if the file already exists. */
635 existing_dir = FALSE;
636 if (lstat (file_hdr.c_name, &file_stat) == 0)
638 if (S_ISDIR (file_stat.st_mode)
639 && ((file_hdr.c_mode & CP_IFMT) == CP_IFDIR))
641 /* If there is already a directory there that
642 we are trying to create, don't complain about
646 else if (!unconditional_flag
647 && file_hdr.c_mtime <= file_stat.st_mtime)
649 error (0, 0, "%s not created: newer or same age version exists",
651 tape_toss_input (in_file_des, file_hdr.c_filesize);
652 tape_skip_padding (in_file_des, file_hdr.c_filesize);
653 continue; /* Go to the next file. */
655 else if (S_ISDIR (file_stat.st_mode)
656 ? rmdir (file_hdr.c_name)
657 : unlink (file_hdr.c_name))
659 error (0, errno, "cannot remove current %s",
661 tape_toss_input (in_file_des, file_hdr.c_filesize);
662 tape_skip_padding (in_file_des, file_hdr.c_filesize);
663 continue; /* Go to the next file. */
667 /* Do the real copy or link. */
668 switch (file_hdr.c_mode & CP_IFMT)
672 /* Can the current file be linked to a previously copied file? */
673 if (file_hdr.c_nlink > 1 && (archive_format == arf_newascii
674 || archive_format == arf_crcascii) )
677 if (file_hdr.c_filesize == 0)
679 /* The newc and crc formats store multiply linked copies
680 of the same file in the archive only once. The
681 actual data is attached to the last link in the
682 archive, and the other links all have a filesize
683 of 0. Since this file has multiple links and a
684 filesize of 0, its data is probably attatched to
685 another file in the archive. Save the link, and
686 process it later when we get the actual data. We
687 can't just create it with length 0 and add the
688 data later, in case the file is readonly. We still
689 lose if its parent directory is readonly (and we aren't
690 running as root), but there's nothing we can do about
692 defer_copyin (&file_hdr);
693 tape_toss_input (in_file_des, file_hdr.c_filesize);
694 tape_skip_padding (in_file_des, file_hdr.c_filesize);
697 /* If the file has data (filesize != 0), then presumably
698 any other links have already been defer_copyin'ed(),
699 but GNU cpio version 2.0-2.2 didn't do that, so we
700 still have to check for links here (and also in case
701 the archive was created and later appeneded to). */
702 link_res = link_to_maj_min_ino (file_hdr.c_name,
703 file_hdr.c_dev_maj, file_hdr.c_dev_maj,
707 tape_toss_input (in_file_des, file_hdr.c_filesize);
708 tape_skip_padding (in_file_des, file_hdr.c_filesize);
712 else if (file_hdr.c_nlink > 1 && archive_format != arf_tar
713 && archive_format != arf_ustar)
716 link_res = link_to_maj_min_ino (file_hdr.c_name,
717 file_hdr.c_dev_maj, file_hdr.c_dev_maj,
721 tape_toss_input (in_file_des, file_hdr.c_filesize);
722 tape_skip_padding (in_file_des, file_hdr.c_filesize);
726 else if ((archive_format == arf_tar || archive_format == arf_ustar)
727 && file_hdr.c_tar_linkname &&
728 file_hdr.c_tar_linkname[0] != '\0')
731 link_res = link_to_name (file_hdr.c_name,
732 file_hdr.c_tar_linkname);
735 error (0, errno, "cannot link %s to %s",
736 file_hdr.c_tar_linkname, file_hdr.c_name);
742 /* If not linked, copy the contents of the file. */
743 if (link_name == NULL)
745 out_file_des = open (file_hdr.c_name,
746 O_CREAT | O_WRONLY | O_BINARY, 0600);
747 if (out_file_des < 0 && create_dir_flag)
749 create_all_directories (file_hdr.c_name);
750 out_file_des = open (file_hdr.c_name,
751 O_CREAT | O_WRONLY | O_BINARY,
754 if (out_file_des < 0)
756 error (0, errno, "%s", file_hdr.c_name);
757 tape_toss_input (in_file_des, file_hdr.c_filesize);
758 tape_skip_padding (in_file_des, file_hdr.c_filesize);
763 if (swap_halfwords_flag)
765 if ((file_hdr.c_filesize % 4) == 0)
766 swapping_halfwords = TRUE;
768 error (0, 0, "cannot swap halfwords of %s: odd number of halfwords",
773 if ((file_hdr.c_filesize % 2) == 0)
774 swapping_bytes = TRUE;
776 error (0, 0, "cannot swap bytes of %s: odd number of bytes",
779 copy_files_tape_to_disk (in_file_des, out_file_des, file_hdr.c_filesize);
780 disk_empty_output_buffer (out_file_des);
781 if (close (out_file_des) < 0)
782 error (0, errno, "%s", file_hdr.c_name);
784 if (archive_format == arf_crcascii)
786 if (crc != file_hdr.c_chksum)
787 error (0, 0, "%s: checksum error (0x%x, should be 0x%x)",
788 file_hdr.c_name, crc, file_hdr.c_chksum);
790 /* File is now copied; set attributes. */
792 if ((chown (file_hdr.c_name,
793 set_owner_flag ? set_owner : file_hdr.c_uid,
794 set_group_flag ? set_group : file_hdr.c_gid) < 0)
796 error (0, errno, "%s", file_hdr.c_name);
797 /* chown may have turned off some permissions we wanted. */
798 if (chmod (file_hdr.c_name, (int) file_hdr.c_mode) < 0)
799 error (0, errno, "%s", file_hdr.c_name);
800 if (retain_time_flag)
802 times.actime = times.modtime = file_hdr.c_mtime;
803 if (utime (file_hdr.c_name, ×) < 0)
804 error (0, errno, "%s", file_hdr.c_name);
806 tape_skip_padding (in_file_des, file_hdr.c_filesize);
807 if (file_hdr.c_nlink > 1 && (archive_format == arf_newascii
808 || archive_format == arf_crcascii) )
810 /* (see comment above for how the newc and crc formats
811 store multiple links). Now that we have the data
812 for this file, create any other links to it which
814 create_defered_links (&file_hdr);
820 /* Strip any trailing `/'s off the filename; tar puts
821 them on. We might as well do it here in case anybody
822 else does too, since they cause strange things to happen. */
823 strip_trailing_slashes (file_hdr.c_name);
825 /* Ignore the current directory. It must already exist,
826 and we don't want to change its permission, ownership
828 if (file_hdr.c_name[0] == '.' && file_hdr.c_name[1] == '\0')
838 /* If the directory name ends in a + and is SUID,
839 then it is a CDF. Strip the trailing + from
840 the name before creating it. */
841 cdf_char = strlen (file_hdr.c_name) - 1;
842 if ( (cdf_char > 0) &&
843 (file_hdr.c_mode & 04000) &&
844 (file_hdr.c_name [cdf_char] == '+') )
846 file_hdr.c_name [cdf_char] = '\0';
850 res = mkdir (file_hdr.c_name, file_hdr.c_mode);
854 if (res < 0 && create_dir_flag)
856 create_all_directories (file_hdr.c_name);
857 res = mkdir (file_hdr.c_name, file_hdr.c_mode);
861 /* In some odd cases where the file_hdr.c_name includes `.',
862 the directory may have actually been created by
863 create_all_directories(), so the mkdir will fail
864 because the directory exists. If that's the case,
865 don't complain about it. */
866 if ( (errno != EEXIST) ||
867 (lstat (file_hdr.c_name, &file_stat) != 0) ||
868 !(S_ISDIR (file_stat.st_mode) ) )
870 error (0, errno, "%s", file_hdr.c_name);
875 if ((chown (file_hdr.c_name,
876 set_owner_flag ? set_owner : file_hdr.c_uid,
877 set_group_flag ? set_group : file_hdr.c_gid) < 0)
879 error (0, errno, "%s", file_hdr.c_name);
880 /* chown may have turned off some permissions we wanted. */
881 if (chmod (file_hdr.c_name, (int) file_hdr.c_mode) < 0)
882 error (0, errno, "%s", file_hdr.c_name);
885 /* Once we "hide" the directory with the chmod(),
886 we have to refer to it using name+ instead of name. */
887 file_hdr.c_name [cdf_char] = '+';
889 if (retain_time_flag)
891 times.actime = times.modtime = file_hdr.c_mtime;
892 if (utime (file_hdr.c_name, ×) < 0)
893 error (0, errno, "%s", file_hdr.c_name);
906 if (file_hdr.c_nlink > 1 && archive_format != arf_tar
907 && archive_format != arf_ustar)
910 link_res = link_to_maj_min_ino (file_hdr.c_name,
911 file_hdr.c_dev_maj, file_hdr.c_dev_maj,
916 else if (archive_format == arf_ustar &&
917 file_hdr.c_tar_linkname &&
918 file_hdr.c_tar_linkname [0] != '\0')
921 link_res = link_to_name (file_hdr.c_name,
922 file_hdr.c_tar_linkname);
925 error (0, errno, "cannot link %s to %s",
926 file_hdr.c_tar_linkname, file_hdr.c_name);
927 /* Something must be wrong, because we couldn't
928 find the file to link to. But can we assume
929 that the device maj/min numbers are correct
930 and fall through to the mknod? It's probably
931 safer to just break, rather than possibly
932 creating a bogus device file. */
938 if ((file_hdr.c_mode & CP_IFMT) == CP_IFIFO)
939 res = mkfifo (file_hdr.c_name, file_hdr.c_mode);
942 res = mknod (file_hdr.c_name, file_hdr.c_mode,
943 makedev (file_hdr.c_rdev_maj, file_hdr.c_rdev_min));
944 if (res < 0 && create_dir_flag)
946 create_all_directories (file_hdr.c_name);
948 if ((file_hdr.c_mode & CP_IFMT) == CP_IFIFO)
949 res = mkfifo (file_hdr.c_name, file_hdr.c_mode);
952 res = mknod (file_hdr.c_name, file_hdr.c_mode,
953 makedev (file_hdr.c_rdev_maj,
954 file_hdr.c_rdev_min));
958 error (0, errno, "%s", file_hdr.c_name);
962 if ((chown (file_hdr.c_name,
963 set_owner_flag ? set_owner : file_hdr.c_uid,
964 set_group_flag ? set_group : file_hdr.c_gid) < 0)
966 error (0, errno, "%s", file_hdr.c_name);
967 /* chown may have turned off some permissions we wanted. */
968 if (chmod (file_hdr.c_name, file_hdr.c_mode) < 0)
969 error (0, errno, "%s", file_hdr.c_name);
970 if (retain_time_flag)
972 times.actime = times.modtime = file_hdr.c_mtime;
973 if (utime (file_hdr.c_name, ×) < 0)
974 error (0, errno, "%s", file_hdr.c_name);
982 if (archive_format != arf_tar && archive_format != arf_ustar)
984 link_name = (char *) xmalloc ((unsigned int) file_hdr.c_filesize + 1);
985 link_name[file_hdr.c_filesize] = '\0';
986 tape_buffered_read (link_name, in_file_des, file_hdr.c_filesize);
987 tape_skip_padding (in_file_des, file_hdr.c_filesize);
991 link_name = xstrdup (file_hdr.c_tar_linkname);
994 res = UMASKED_SYMLINK (link_name, file_hdr.c_name,
996 if (res < 0 && create_dir_flag)
998 create_all_directories (file_hdr.c_name);
999 res = UMASKED_SYMLINK (link_name, file_hdr.c_name,
1004 error (0, errno, "%s", file_hdr.c_name);
1010 if ((lchown (file_hdr.c_name,
1011 set_owner_flag ? set_owner : file_hdr.c_uid,
1012 set_group_flag ? set_group : file_hdr.c_gid) < 0)
1014 error (0, errno, "%s", file_hdr.c_name);
1022 error (0, 0, "%s: unknown file type", file_hdr.c_name);
1023 tape_toss_input (in_file_des, file_hdr.c_filesize);
1024 tape_skip_padding (in_file_des, file_hdr.c_filesize);
1028 fprintf (stderr, "%s\n", file_hdr.c_name);
1030 fputc ('.', stderr);
1035 fputc ('\n', stderr);
1040 if (archive_format == arf_newascii || archive_format == arf_crcascii)
1041 create_final_defers ();
1044 res = (input_bytes + io_block_size - 1) / io_block_size;
1046 fprintf (stderr, "1 block\n");
1048 fprintf (stderr, "%d blocks\n", res);
1052 /* Print the file described by FILE_HDR in long format.
1053 If LINK_NAME is nonzero, it is the name of the file that
1054 this file is a symbolic link to. */
1057 long_format (file_hdr, link_name)
1058 struct new_cpio_header *file_hdr;
1065 #ifdef HAVE_STRFTIME
1066 static int d_first = -1;
1069 mode_string (file_hdr->c_mode, mbuf);
1072 /* Get time values ready to print. */
1073 when = file_hdr->c_mtime;
1074 #ifdef HAVE_STRFTIME
1077 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
1081 if (current_time - when > 6L * 30L * 24L * 60L * 60L
1082 || current_time - when < 0L)
1083 ptbuf = d_first ? "%e %b %Y" : "%b %e %Y";
1085 ptbuf = d_first ? "%e %b %R" : "%b %e %R";
1086 strftime(tbuf, sizeof(tbuf), ptbuf, localtime(&when));
1089 strcpy (tbuf, ctime (&when));
1090 if (current_time - when > 6L * 30L * 24L * 60L * 60L
1091 || current_time - when < 0L)
1093 /* The file is older than 6 months, or in the future.
1094 Show the year instead of the time of day. */
1095 strcpy (tbuf + 11, tbuf + 19);
1101 printf ("%s %3lu ", mbuf, file_hdr->c_nlink);
1106 printf ("%-8u %-8u ", (unsigned int) file_hdr->c_uid,
1107 (unsigned int) file_hdr->c_gid);
1110 printf ("%-8.8s %-8.8s ", getuser (file_hdr->c_uid),
1111 getgroup (file_hdr->c_gid));
1113 if ((file_hdr->c_mode & CP_IFMT) == CP_IFCHR
1114 || (file_hdr->c_mode & CP_IFMT) == CP_IFBLK)
1115 printf ("%3lu, %3lu ", file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
1118 printf ("%8lu ", file_hdr->c_filesize);
1120 printf ("%s ", ptbuf);
1122 print_name_with_quoting (file_hdr->c_name);
1126 print_name_with_quoting (link_name);
1128 putc ('\n', stdout);
1132 print_name_with_quoting (p)
1135 register unsigned char c;
1137 while ( (c = *p++) )
1176 #if (defined(BSD) && (BSD >= 199306))
1181 c < 0377 && c != 0177
1189 printf ("\\%03o", (unsigned int) c);
1194 /* Read a pattern file (for the -E option). Put a list of
1195 `num_patterns' elements in `save_patterns'. Any patterns that were
1196 already in `save_patterns' (from the command line) are preserved. */
1199 read_pattern_file ()
1201 int max_new_patterns;
1202 char **new_save_patterns;
1203 int new_num_patterns;
1205 dynamic_string pattern_name;
1208 if (num_patterns < 0)
1210 max_new_patterns = 1 + num_patterns;
1211 new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
1212 new_num_patterns = num_patterns;
1213 ds_init (&pattern_name, 128);
1215 pattern_fp = fopen (pattern_file_name, "r");
1216 if (pattern_fp == NULL)
1217 error (1, errno, "%s", pattern_file_name);
1218 while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
1220 if (new_num_patterns >= max_new_patterns)
1222 max_new_patterns += 1;
1223 new_save_patterns = (char **)
1224 xrealloc ((char *) new_save_patterns,
1225 max_new_patterns * sizeof (char *));
1227 new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
1230 if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
1231 error (1, errno, "%s", pattern_file_name);
1233 for (i = 0; i < num_patterns; ++i)
1234 new_save_patterns[i] = save_patterns[i];
1236 save_patterns = new_save_patterns;
1237 num_patterns = new_num_patterns;
1240 /* Skip the padding on IN_FILE_DES after a header or file,
1241 up to the next header.
1242 The number of bytes skipped is based on OFFSET -- the current offset
1243 from the last start of a header (or file) -- and the current
1247 tape_skip_padding (in_file_des, offset)
1253 if (archive_format == arf_crcascii || archive_format == arf_newascii)
1254 pad = (4 - (offset % 4)) % 4;
1255 else if (archive_format == arf_binary || archive_format == arf_hpbinary)
1256 pad = (2 - (offset % 2)) % 2;
1257 else if (archive_format == arf_tar || archive_format == arf_ustar)
1258 pad = (512 - (offset % 512)) % 512;
1263 tape_toss_input (in_file_des, pad);
1267 /* The newc and crc formats store multiply linked copies of the same file
1268 in the archive only once. The actual data is attached to the last link
1269 in the archive, and the other links all have a filesize of 0. When a
1270 file in the archive has multiple links and a filesize of 0, its data is
1271 probably "attatched" to another file in the archive, so we can't create
1272 it right away. We have to "defer" creating it until we have created
1273 the file that has the data "attatched" to it. We keep a list of the
1274 "defered" links on deferments. */
1276 struct deferment *deferments = NULL;
1278 /* Add a file header to the deferments list. For now they all just
1279 go on one list, although we could optimize this if necessary. */
1282 defer_copyin (file_hdr)
1283 struct new_cpio_header *file_hdr;
1285 struct deferment *d;
1286 d = create_deferment (file_hdr);
1287 d->next = deferments;
1292 /* We just created a file that (probably) has some other links to it
1293 which have been defered. Go through all of the links on the deferments
1294 list and create any which are links to this file. */
1297 create_defered_links (file_hdr)
1298 struct new_cpio_header *file_hdr;
1300 struct deferment *d;
1301 struct deferment *d_prev;
1306 ino = file_hdr->c_ino;
1307 maj = file_hdr->c_dev_maj;
1308 min = file_hdr->c_dev_min;
1313 if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
1314 && (d->header.c_dev_min == min) )
1316 struct deferment *d_free;
1317 link_res = link_to_name (d->header.c_name, file_hdr->c_name);
1320 error (0, errno, "cannot link %s to %s",
1321 d->header.c_name, file_hdr->c_name);
1324 d_prev->next = d->next;
1326 deferments = d->next;
1329 free_deferment (d_free);
1339 /* If we had a multiply linked file that really was empty then we would
1340 have defered all of its links, since we never found any with data
1341 "attached", and they will still be on the deferment list even when
1342 we are done reading the whole archive. Write out all of these
1343 empty links that are still on the deferments list. */
1346 create_final_defers ()
1348 struct deferment *d;
1351 struct utimbuf times; /* For setting file times. */
1352 /* Initialize this in case it has members we don't know to set. */
1353 bzero (×, sizeof (struct utimbuf));
1355 for (d = deferments; d != NULL; d = d->next)
1357 link_res = link_to_maj_min_ino (d->header.c_name,
1358 d->header.c_dev_maj, d->header.c_dev_maj,
1364 out_file_des = open (d->header.c_name,
1365 O_CREAT | O_WRONLY | O_BINARY, 0600);
1366 if (out_file_des < 0 && create_dir_flag)
1368 create_all_directories (d->header.c_name);
1369 out_file_des = open (d->header.c_name,
1370 O_CREAT | O_WRONLY | O_BINARY,
1373 if (out_file_des < 0)
1375 error (0, errno, "%s", d->header.c_name);
1379 if (close (out_file_des) < 0)
1380 error (0, errno, "%s", d->header.c_name);
1382 /* File is now copied; set attributes. */
1384 if ((chown (d->header.c_name,
1385 set_owner_flag ? set_owner : d->header.c_uid,
1386 set_group_flag ? set_group : d->header.c_gid) < 0)
1388 error (0, errno, "%s", d->header.c_name);
1389 /* chown may have turned off some permissions we wanted. */
1390 if (chmod (d->header.c_name, (int) d->header.c_mode) < 0)
1391 error (0, errno, "%s", d->header.c_name);
1392 if (retain_time_flag)
1394 times.actime = times.modtime = d->header.c_mtime;
1395 if (utime (d->header.c_name, ×) < 0)
1396 error (0, errno, "%s", d->header.c_name);