3 /* copyin.c - extract or list a cpio archive
4 Copyright (C) 1990,1991,1992,2001,2002,2003,2004,
5 2005, 2006 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public
18 License along with this program; if not, write to the Free
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301 USA. */
25 #include <sys/types.h>
27 #include "filetypes.h"
40 # define lchown(f,u,g) 0
43 static void copyin_regular_file(struct cpio_file_stat* file_hdr,
47 warn_junk_bytes (long bytes_skipped)
49 error (0, 0, ngettext ("warning: skipped %ld byte of junk",
50 "warning: skipped %ld bytes of junk", bytes_skipped),
56 query_rename(struct cpio_file_stat* file_hdr, FILE *tty_in, FILE *tty_out,
59 char *str_res; /* Result for string function. */
60 static dynamic_string new_name; /* New file name for rename option. */
61 static int initialized_new_name = false;
62 if (!initialized_new_name)
64 ds_init (&new_name, 128);
65 initialized_new_name = true;
70 fprintf (tty_out, _("rename %s -> "), file_hdr->c_name);
72 str_res = ds_fgets (tty_in, &new_name);
76 str_res = ds_fgetstr (rename_in, &new_name, '\n');
78 if (str_res == NULL || str_res[0] == 0)
83 /* Debian hack: file_hrd.c_name is sometimes set to
84 point to static memory by code in tar.c. This
85 causes a segfault. This has been fixed and an
86 additional check to ensure that the file name
87 is not too long has been added. (Reported by
88 Horst Knobloch.) This bug has been reported to
89 "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
91 if (archive_format != arf_tar && archive_format != arf_ustar)
93 free (file_hdr->c_name);
94 file_hdr->c_name = xstrdup (new_name.ds_string);
98 if (is_tar_filename_too_long (new_name.ds_string))
99 error (0, 0, _("%s: file name too long"),
102 strcpy (file_hdr->c_name, new_name.ds_string);
108 /* Skip the padding on IN_FILE_DES after a header or file,
109 up to the next header.
110 The number of bytes skipped is based on OFFSET -- the current offset
111 from the last start of a header (or file) -- and the current
115 tape_skip_padding (int in_file_des, int offset)
119 if (archive_format == arf_crcascii || archive_format == arf_newascii)
120 pad = (4 - (offset % 4)) % 4;
121 else if (archive_format == arf_binary || archive_format == arf_hpbinary)
122 pad = (2 - (offset % 2)) % 2;
123 else if (archive_format == arf_tar || archive_format == arf_ustar)
124 pad = (512 - (offset % 512)) % 512;
129 tape_toss_input (in_file_des, pad);
134 list_file(struct cpio_file_stat* file_hdr, int in_file_des)
139 if ((file_hdr->c_mode & CP_IFMT) == CP_IFLNK)
141 if (archive_format != arf_tar && archive_format != arf_ustar)
143 char *link_name = NULL; /* Name of hard and symbolic links. */
145 link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
146 link_name[file_hdr->c_filesize] = '\0';
147 tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
148 long_format (file_hdr, link_name);
150 tape_skip_padding (in_file_des, file_hdr->c_filesize);
155 long_format (file_hdr, file_hdr->c_tar_linkname);
161 long_format (file_hdr, (char *) 0);
165 /* Debian hack: Modified to print a list of filenames
166 terminiated by a null character when the -t and -0
167 flags are used. This has been submitted as a
168 suggestion to "bug-gnu-utils@prep.ai.mit.edu". -BEM */
169 printf ("%s%c", file_hdr->c_name, name_end);
173 tape_toss_input (in_file_des, file_hdr->c_filesize);
174 tape_skip_padding (in_file_des, file_hdr->c_filesize);
175 if (only_verify_crc_flag)
178 if ((file_hdr->c_mode & CP_IFMT) == CP_IFLNK)
180 return; /* links don't have a checksum */
183 if (crc != file_hdr->c_chksum)
185 error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
186 file_hdr->c_name, crc, file_hdr->c_chksum);
192 try_existing_file (struct cpio_file_stat* file_hdr, int in_file_des,
195 struct stat file_stat;
197 *existing_dir = false;
198 if (lstat (file_hdr->c_name, &file_stat) == 0)
200 if (S_ISDIR (file_stat.st_mode)
201 && ((file_hdr->c_mode & CP_IFMT) == CP_IFDIR))
203 /* If there is already a directory there that
204 we are trying to create, don't complain about
206 *existing_dir = true;
209 else if (!unconditional_flag
210 && file_hdr->c_mtime <= file_stat.st_mtime)
212 error (0, 0, _("%s not created: newer or same age version exists"),
214 tape_toss_input (in_file_des, file_hdr->c_filesize);
215 tape_skip_padding (in_file_des, file_hdr->c_filesize);
216 return -1; /* Go to the next file. */
218 else if (S_ISDIR (file_stat.st_mode)
219 ? rmdir (file_hdr->c_name)
220 : unlink (file_hdr->c_name))
222 error (0, errno, _("cannot remove current %s"),
224 tape_toss_input (in_file_des, file_hdr->c_filesize);
225 tape_skip_padding (in_file_des, file_hdr->c_filesize);
226 return -1; /* Go to the next file. */
232 /* The newc and crc formats store multiply linked copies of the same file
233 in the archive only once. The actual data is attached to the last link
234 in the archive, and the other links all have a filesize of 0. When a
235 file in the archive has multiple links and a filesize of 0, its data is
236 probably "attatched" to another file in the archive, so we can't create
237 it right away. We have to "defer" creating it until we have created
238 the file that has the data "attatched" to it. We keep a list of the
239 "defered" links on deferments. */
241 struct deferment *deferments = NULL;
243 /* Add a file header to the deferments list. For now they all just
244 go on one list, although we could optimize this if necessary. */
247 defer_copyin (struct cpio_file_stat *file_hdr)
250 d = create_deferment (file_hdr);
251 d->next = deferments;
256 /* We just created a file that (probably) has some other links to it
257 which have been defered. Go through all of the links on the deferments
258 list and create any which are links to this file. */
261 create_defered_links (struct cpio_file_stat *file_hdr)
264 struct deferment *d_prev;
269 ino = file_hdr->c_ino;
270 maj = file_hdr->c_dev_maj;
271 min = file_hdr->c_dev_min;
276 if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
277 && (d->header.c_dev_min == min) )
279 struct deferment *d_free;
280 link_res = link_to_name (d->header.c_name, file_hdr->c_name);
283 error (0, errno, _("cannot link %s to %s"),
284 d->header.c_name, file_hdr->c_name);
287 d_prev->next = d->next;
289 deferments = d->next;
292 free_deferment (d_free);
302 /* We are skipping a file but there might be other links to it that we
303 did not skip, so we have to copy its data for the other links. Find
304 the first link that we didn't skip and try to create that. That will
305 then create the other deferred links. */
308 create_defered_links_to_skipped (struct cpio_file_stat *file_hdr,
312 struct deferment *d_prev;
316 if (file_hdr->c_filesize == 0)
318 /* The file doesn't have any data attached to it so we don't have
322 ino = file_hdr->c_ino;
323 maj = file_hdr->c_dev_maj;
324 min = file_hdr->c_dev_min;
329 if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
330 && (d->header.c_dev_min == min) )
333 d_prev->next = d->next;
335 deferments = d->next;
336 free (file_hdr->c_name);
337 file_hdr->c_name = xstrdup(d->header.c_name);
339 copyin_regular_file(file_hdr, in_file_des);
351 /* If we had a multiply linked file that really was empty then we would
352 have defered all of its links, since we never found any with data
353 "attached", and they will still be on the deferment list even when
354 we are done reading the whole archive. Write out all of these
355 empty links that are still on the deferments list. */
358 create_final_defers ()
364 for (d = deferments; d != NULL; d = d->next)
366 /* Debian hack: A line, which could cause an endless loop, was
367 removed (97/1/2). It was reported by Ronald F. Guilmette to
368 the upstream maintainers. -BEM */
369 /* Debian hack: This was reported by Horst Knobloch. This bug has
370 been reported to "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM
372 link_res = link_to_maj_min_ino (d->header.c_name,
373 d->header.c_dev_maj, d->header.c_dev_min,
379 out_file_des = open (d->header.c_name,
380 O_CREAT | O_WRONLY | O_BINARY, 0600);
381 if (out_file_des < 0 && create_dir_flag)
383 create_all_directories (d->header.c_name);
384 out_file_des = open (d->header.c_name,
385 O_CREAT | O_WRONLY | O_BINARY,
388 if (out_file_des < 0)
390 open_error (d->header.c_name);
394 set_perms (out_file_des, &d->header);
396 if (close (out_file_des) < 0)
397 close_error (d->header.c_name);
403 copyin_regular_file (struct cpio_file_stat* file_hdr, int in_file_des)
405 int out_file_des; /* Output file descriptor. */
407 if (to_stdout_option)
408 out_file_des = STDOUT_FILENO;
411 /* Can the current file be linked to a previously copied file? */
412 if (file_hdr->c_nlink > 1
413 && (archive_format == arf_newascii
414 || archive_format == arf_crcascii) )
417 if (file_hdr->c_filesize == 0)
419 /* The newc and crc formats store multiply linked copies
420 of the same file in the archive only once. The
421 actual data is attached to the last link in the
422 archive, and the other links all have a filesize
423 of 0. Since this file has multiple links and a
424 filesize of 0, its data is probably attatched to
425 another file in the archive. Save the link, and
426 process it later when we get the actual data. We
427 can't just create it with length 0 and add the
428 data later, in case the file is readonly. We still
429 lose if its parent directory is readonly (and we aren't
430 running as root), but there's nothing we can do about
432 defer_copyin (file_hdr);
433 tape_toss_input (in_file_des, file_hdr->c_filesize);
434 tape_skip_padding (in_file_des, file_hdr->c_filesize);
437 /* If the file has data (filesize != 0), then presumably
438 any other links have already been defer_copyin'ed(),
439 but GNU cpio version 2.0-2.2 didn't do that, so we
440 still have to check for links here (and also in case
441 the archive was created and later appeneded to). */
442 /* Debian hack: (97/1/2) This was reported by Ronald
443 F. Guilmette to the upstream maintainers. -BEM */
444 link_res = link_to_maj_min_ino (file_hdr->c_name,
445 file_hdr->c_dev_maj, file_hdr->c_dev_min,
449 tape_toss_input (in_file_des, file_hdr->c_filesize);
450 tape_skip_padding (in_file_des, file_hdr->c_filesize);
454 else if (file_hdr->c_nlink > 1
455 && archive_format != arf_tar
456 && archive_format != arf_ustar)
459 /* Debian hack: (97/1/2) This was reported by Ronald
460 F. Guilmette to the upstream maintainers. -BEM */
461 link_res = link_to_maj_min_ino (file_hdr->c_name,
467 tape_toss_input (in_file_des, file_hdr->c_filesize);
468 tape_skip_padding (in_file_des, file_hdr->c_filesize);
472 else if ((archive_format == arf_tar || archive_format == arf_ustar)
473 && file_hdr->c_tar_linkname
474 && file_hdr->c_tar_linkname[0] != '\0')
477 link_res = link_to_name (file_hdr->c_name, file_hdr->c_tar_linkname);
480 error (0, errno, _("cannot link %s to %s"),
481 file_hdr->c_tar_linkname, file_hdr->c_name);
486 /* If not linked, copy the contents of the file. */
487 out_file_des = open (file_hdr->c_name,
488 O_CREAT | O_WRONLY | O_BINARY, 0600);
490 if (out_file_des < 0 && create_dir_flag)
492 create_all_directories (file_hdr->c_name);
493 out_file_des = open (file_hdr->c_name,
494 O_CREAT | O_WRONLY | O_BINARY,
498 if (out_file_des < 0)
500 open_error (file_hdr->c_name);
501 tape_toss_input (in_file_des, file_hdr->c_filesize);
502 tape_skip_padding (in_file_des, file_hdr->c_filesize);
508 if (swap_halfwords_flag)
510 if ((file_hdr->c_filesize % 4) == 0)
511 swapping_halfwords = true;
513 error (0, 0, _("cannot swap halfwords of %s: odd number of halfwords"),
518 if ((file_hdr->c_filesize % 2) == 0)
519 swapping_bytes = true;
521 error (0, 0, _("cannot swap bytes of %s: odd number of bytes"),
524 copy_files_tape_to_disk (in_file_des, out_file_des, file_hdr->c_filesize);
525 disk_empty_output_buffer (out_file_des);
527 if (to_stdout_option)
529 if (archive_format == arf_crcascii)
531 if (crc != file_hdr->c_chksum)
532 error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
533 file_hdr->c_name, crc, file_hdr->c_chksum);
535 tape_skip_padding (in_file_des, file_hdr->c_filesize);
539 /* Debian hack to fix a bug in the --sparse option.
540 This bug has been reported to
541 "bug-gnu-utils@prep.ai.mit.edu". (96/7/10) -BEM */
542 if (delayed_seek_count > 0)
544 lseek (out_file_des, delayed_seek_count-1, SEEK_CUR);
545 write (out_file_des, "", 1);
546 delayed_seek_count = 0;
549 set_perms (out_file_des, file_hdr);
551 if (close (out_file_des) < 0)
552 close_error (file_hdr->c_name);
554 if (archive_format == arf_crcascii)
556 if (crc != file_hdr->c_chksum)
557 error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
558 file_hdr->c_name, crc, file_hdr->c_chksum);
561 tape_skip_padding (in_file_des, file_hdr->c_filesize);
562 if (file_hdr->c_nlink > 1
563 && (archive_format == arf_newascii || archive_format == arf_crcascii) )
565 /* (see comment above for how the newc and crc formats
566 store multiple links). Now that we have the data
567 for this file, create any other links to it which
569 create_defered_links (file_hdr);
574 copyin_directory (struct cpio_file_stat *file_hdr, int existing_dir)
576 int res; /* Result of various function calls. */
578 int cdf_flag; /* True if file is a CDF. */
579 int cdf_char; /* Index of `+' char indicating a CDF. */
582 if (to_stdout_option)
585 /* Strip any trailing `/'s off the filename; tar puts
586 them on. We might as well do it here in case anybody
587 else does too, since they cause strange things to happen. */
588 strip_trailing_slashes (file_hdr->c_name);
590 /* Ignore the current directory. It must already exist,
591 and we don't want to change its permission, ownership
593 if (file_hdr->c_name[0] == '.' && file_hdr->c_name[1] == '\0')
605 /* If the directory name ends in a + and is SUID,
606 then it is a CDF. Strip the trailing + from
607 the name before creating it. */
608 cdf_char = strlen (file_hdr->c_name) - 1;
609 if ( (cdf_char > 0) &&
610 (file_hdr->c_mode & 04000) &&
611 (file_hdr->c_name [cdf_char] == '+') )
613 file_hdr->c_name [cdf_char] = '\0';
617 res = mkdir (file_hdr->c_name, file_hdr->c_mode);
621 if (res < 0 && create_dir_flag)
623 create_all_directories (file_hdr->c_name);
624 res = mkdir (file_hdr->c_name, file_hdr->c_mode);
628 /* In some odd cases where the file_hdr->c_name includes `.',
629 the directory may have actually been created by
630 create_all_directories(), so the mkdir will fail
631 because the directory exists. If that's the case,
632 don't complain about it. */
633 struct stat file_stat;
636 mkdir_error (file_hdr->c_name);
639 if (lstat (file_hdr->c_name, &file_stat))
641 stat_error (file_hdr->c_name);
644 if (!(S_ISDIR (file_stat.st_mode)))
646 error (0, 0, _("%s is not a directory"),
647 quotearg_colon (file_hdr->c_name));
652 set_perms (-1, file_hdr);
656 copyin_device (struct cpio_file_stat* file_hdr)
658 int res; /* Result of various function calls. */
660 if (to_stdout_option)
663 if (file_hdr->c_nlink > 1 && archive_format != arf_tar
664 && archive_format != arf_ustar)
667 /* Debian hack: This was reported by Horst
668 Knobloch. This bug has been reported to
669 "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
670 link_res = link_to_maj_min_ino (file_hdr->c_name,
671 file_hdr->c_dev_maj, file_hdr->c_dev_min,
678 else if (archive_format == arf_ustar &&
679 file_hdr->c_tar_linkname &&
680 file_hdr->c_tar_linkname [0] != '\0')
683 link_res = link_to_name (file_hdr->c_name,
684 file_hdr->c_tar_linkname);
687 error (0, errno, _("cannot link %s to %s"),
688 file_hdr->c_tar_linkname, file_hdr->c_name);
689 /* Something must be wrong, because we couldn't
690 find the file to link to. But can we assume
691 that the device maj/min numbers are correct
692 and fall through to the mknod? It's probably
693 safer to just return, rather than possibly
694 creating a bogus device file. */
700 if ((file_hdr->c_mode & CP_IFMT) == CP_IFIFO)
701 res = mkfifo (file_hdr->c_name, file_hdr->c_mode);
704 res = mknod (file_hdr->c_name, file_hdr->c_mode,
705 makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
706 if (res < 0 && create_dir_flag)
708 create_all_directories (file_hdr->c_name);
710 if ((file_hdr->c_mode & CP_IFMT) == CP_IFIFO)
711 res = mkfifo (file_hdr->c_name, file_hdr->c_mode);
714 res = mknod (file_hdr->c_name, file_hdr->c_mode,
715 makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
719 mknod_error (file_hdr->c_name);
724 uid_t uid = set_owner_flag ? set_owner : file_hdr->c_uid;
725 gid_t gid = set_group_flag ? set_group : file_hdr->c_gid;
726 if ((chown (file_hdr->c_name, uid, gid) < 0)
728 chown_error_details (file_hdr->c_name, uid, gid);
730 /* chown may have turned off some permissions we wanted. */
731 if (chmod (file_hdr->c_name, file_hdr->c_mode) < 0)
732 chmod_error_details (file_hdr->c_name, file_hdr->c_mode);
733 if (retain_time_flag)
734 set_file_times (-1, file_hdr->c_name, file_hdr->c_mtime,
739 copyin_link(struct cpio_file_stat *file_hdr, int in_file_des)
741 char *link_name = NULL; /* Name of hard and symbolic links. */
742 int res; /* Result of various function calls. */
744 if (to_stdout_option)
747 if (archive_format != arf_tar && archive_format != arf_ustar)
749 link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
750 link_name[file_hdr->c_filesize] = '\0';
751 tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
752 tape_skip_padding (in_file_des, file_hdr->c_filesize);
756 link_name = xstrdup (file_hdr->c_tar_linkname);
759 res = UMASKED_SYMLINK (link_name, file_hdr->c_name,
761 if (res < 0 && create_dir_flag)
763 create_all_directories (file_hdr->c_name);
764 res = UMASKED_SYMLINK (link_name, file_hdr->c_name,
769 error (0, errno, _("%s: Cannot symlink to %s"),
770 quotearg_colon (link_name), quote_n (1, file_hdr->c_name));
776 uid_t uid = set_owner_flag ? set_owner : file_hdr->c_uid;
777 gid_t gid = set_group_flag ? set_group : file_hdr->c_gid;
778 if ((lchown (file_hdr->c_name, uid, gid) < 0)
780 chown_error_details (file_hdr->c_name, uid, gid);
786 copyin_file (struct cpio_file_stat* file_hdr, int in_file_des)
790 if (!to_stdout_option
791 && try_existing_file (file_hdr, in_file_des, &existing_dir) < 0)
794 /* Do the real copy or link. */
795 switch (file_hdr->c_mode & CP_IFMT)
798 copyin_regular_file (file_hdr, in_file_des);
802 copyin_directory (file_hdr, existing_dir);
813 copyin_device (file_hdr);
818 copyin_link (file_hdr, in_file_des);
823 error (0, 0, _("%s: unknown file type"), file_hdr->c_name);
824 tape_toss_input (in_file_des, file_hdr->c_filesize);
825 tape_skip_padding (in_file_des, file_hdr->c_filesize);
830 /* Current time for verbose table. */
831 static time_t current_time;
834 /* Print the file described by FILE_HDR in long format.
835 If LINK_NAME is nonzero, it is the name of the file that
836 this file is a symbolic link to. */
839 long_format (struct cpio_file_stat *file_hdr, char *link_name)
845 static int d_first = -1;
847 mode_string (file_hdr->c_mode, mbuf);
850 /* Get time values ready to print. */
851 when = file_hdr->c_mtime;
853 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
854 if (current_time - when > 6L * 30L * 24L * 60L * 60L
855 || current_time - when < 0L)
856 ptbuf = d_first ? "%e %b %Y" : "%b %e %Y";
858 ptbuf = d_first ? "%e %b %R" : "%b %e %R";
859 strftime(tbuf, sizeof(tbuf), ptbuf, localtime(&when));
862 printf ("%s %3lu ", mbuf, file_hdr->c_nlink);
865 printf ("%-8u %-8u ", (unsigned int) file_hdr->c_uid,
866 (unsigned int) file_hdr->c_gid);
868 printf ("%-8.8s %-8.8s ", getuser (file_hdr->c_uid),
869 getgroup (file_hdr->c_gid));
871 if ((file_hdr->c_mode & CP_IFMT) == CP_IFCHR
872 || (file_hdr->c_mode & CP_IFMT) == CP_IFBLK)
873 printf ("%3lu, %3lu ", file_hdr->c_rdev_maj,
874 file_hdr->c_rdev_min);
876 printf ("%8"PRIuMAX" ", (uintmax_t) file_hdr->c_filesize);
878 printf ("%s ", ptbuf);
880 print_name_with_quoting (file_hdr->c_name);
884 print_name_with_quoting (link_name);
890 print_name_with_quoting (register char *p)
892 register unsigned char c;
934 printf ("\\%03o", (unsigned int) c);
939 /* Read a pattern file (for the -E option). Put a list of
940 `num_patterns' elements in `save_patterns'. Any patterns that were
941 already in `save_patterns' (from the command line) are preserved. */
946 int max_new_patterns;
947 char **new_save_patterns;
948 int new_num_patterns;
950 dynamic_string pattern_name;
953 if (num_patterns < 0)
955 max_new_patterns = 1 + num_patterns;
956 new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
957 new_num_patterns = num_patterns;
958 ds_init (&pattern_name, 128);
960 pattern_fp = fopen (pattern_file_name, "r");
961 if (pattern_fp == NULL)
962 open_error (pattern_file_name);
963 while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
965 if (new_num_patterns >= max_new_patterns)
967 max_new_patterns += 1;
968 new_save_patterns = (char **)
969 xrealloc ((char *) new_save_patterns,
970 max_new_patterns * sizeof (char *));
972 new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
975 if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
976 close_error (pattern_file_name);
978 for (i = 0; i < num_patterns; ++i)
979 new_save_patterns[i] = save_patterns[i];
981 save_patterns = new_save_patterns;
982 num_patterns = new_num_patterns;
987 from_ascii (char const *where, size_t digs, unsigned logbase)
990 char const *buf = where;
991 char const *end = buf + digs;
993 static char codetab[] = "0123456789ABCDEF";
995 for (; *buf == ' '; buf++)
1001 if (buf == end || *buf == 0)
1007 char *p = strchr (codetab, toupper (*buf));
1010 error (0, 0, _("Malformed number %.*s"), digs, where);
1015 if ((d >> logbase) > 1)
1017 error (0, 0, _("Malformed number %.*s"), digs, where);
1021 if (++buf == end || *buf == 0)
1023 overflow |= value ^ (value << logbase >> logbase);
1027 error (0, 0, _("Archive value %.*s is out of range"),
1034 /* Return 16-bit integer I with the bytes swapped. */
1035 #define swab_short(i) ((((i) << 8) & 0xff00) | (((i) >> 8) & 0x00ff))
1037 /* Read the header, including the name of the file, from file
1038 descriptor IN_DES into FILE_HDR. */
1041 read_in_header (struct cpio_file_stat *file_hdr, int in_des)
1046 struct old_cpio_header old_header;
1048 long bytes_skipped = 0; /* Bytes of junk found before magic number. */
1050 /* Search for a valid magic number. */
1052 if (archive_format == arf_unknown)
1058 while (archive_format == arf_unknown)
1060 peeked_bytes = tape_buffered_peek (tmpbuf, in_des, 512);
1061 if (peeked_bytes < 6)
1062 error (1, 0, _("premature end of archive"));
1064 if (!strncmp (tmpbuf, "070701", 6))
1065 archive_format = arf_newascii;
1066 else if (!strncmp (tmpbuf, "070707", 6))
1067 archive_format = arf_oldascii;
1068 else if (!strncmp (tmpbuf, "070702", 6))
1070 archive_format = arf_crcascii;
1073 else if ((*((unsigned short *) tmpbuf) == 070707) ||
1074 (*((unsigned short *) tmpbuf) == swab_short ((unsigned short) 070707)))
1075 archive_format = arf_binary;
1076 else if (peeked_bytes >= 512
1077 && (check_tar = is_tar_header (tmpbuf)))
1080 archive_format = arf_ustar;
1082 archive_format = arf_tar;
1086 tape_buffered_read ((char *) tmpbuf, in_des, 1L);
1092 if (archive_format == arf_tar || archive_format == arf_ustar)
1095 last_header_start = input_bytes - io_block_size +
1096 (in_buff - input_buffer);
1097 if (bytes_skipped > 0)
1098 warn_junk_bytes (bytes_skipped);
1100 read_in_tar_header (file_hdr, in_des);
1104 file_hdr->c_tar_linkname = NULL;
1106 tape_buffered_read (magic.str, in_des, 6L);
1110 last_header_start = input_bytes - io_block_size
1111 + (in_buff - input_buffer) - 6;
1112 if (archive_format == arf_newascii
1113 && !strncmp (magic.str, "070701", 6))
1115 if (bytes_skipped > 0)
1116 warn_junk_bytes (bytes_skipped);
1117 file_hdr->c_magic = 070701;
1118 read_in_new_ascii (file_hdr, in_des);
1121 if (archive_format == arf_crcascii
1122 && !strncmp (magic.str, "070702", 6))
1124 if (bytes_skipped > 0)
1125 warn_junk_bytes (bytes_skipped);
1126 file_hdr->c_magic = 070702;
1127 read_in_new_ascii (file_hdr, in_des);
1130 if ( (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
1131 && !strncmp (magic.str, "070707", 6))
1133 if (bytes_skipped > 0)
1134 warn_junk_bytes (bytes_skipped);
1135 file_hdr->c_magic = 070707;
1136 read_in_old_ascii (file_hdr, in_des);
1139 if ( (archive_format == arf_binary || archive_format == arf_hpbinary)
1140 && (magic.num == 070707
1141 || magic.num == swab_short ((unsigned short) 070707)))
1143 /* Having to skip 1 byte because of word alignment is normal. */
1144 if (bytes_skipped > 0)
1145 warn_junk_bytes (bytes_skipped);
1146 file_hdr->c_magic = 070707;
1147 read_in_binary (file_hdr, &magic.old_header, in_des);
1151 memmove (magic.str, magic.str + 1, 5);
1152 tape_buffered_read (magic.str, in_des, 1L);
1156 /* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
1157 file descriptor IN_DES, except for the magic number, which is
1158 already filled in. */
1161 read_in_old_ascii (struct cpio_file_stat *file_hdr, int in_des)
1163 struct old_ascii_header ascii_header;
1166 tape_buffered_read (ascii_header.c_dev, in_des,
1167 sizeof ascii_header - sizeof ascii_header.c_magic);
1168 dev = FROM_OCTAL (ascii_header.c_dev);
1169 file_hdr->c_dev_maj = major (dev);
1170 file_hdr->c_dev_min = minor (dev);
1172 file_hdr->c_ino = FROM_OCTAL (ascii_header.c_ino);
1173 file_hdr->c_mode = FROM_OCTAL (ascii_header.c_mode);
1174 file_hdr->c_uid = FROM_OCTAL (ascii_header.c_uid);
1175 file_hdr->c_gid = FROM_OCTAL (ascii_header.c_gid);
1176 file_hdr->c_nlink = FROM_OCTAL (ascii_header.c_nlink);
1177 dev = FROM_OCTAL (ascii_header.c_rdev);
1178 file_hdr->c_rdev_maj = major (dev);
1179 file_hdr->c_rdev_min = minor (dev);
1181 file_hdr->c_mtime = FROM_OCTAL (ascii_header.c_mtime);
1182 file_hdr->c_namesize = FROM_OCTAL (ascii_header.c_namesize);
1183 file_hdr->c_filesize = FROM_OCTAL (ascii_header.c_filesize);
1185 /* Read file name from input. */
1186 if (file_hdr->c_name != NULL)
1187 free (file_hdr->c_name);
1188 file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize + 1);
1189 tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
1191 /* HP/UX cpio creates archives that look just like ordinary archives,
1192 but for devices it sets major = 0, minor = 1, and puts the
1193 actual major/minor number in the filesize field. See if this
1194 is an HP/UX cpio archive, and if so fix it. We have to do this
1195 here because process_copy_in() assumes filesize is always 0
1197 switch (file_hdr->c_mode & CP_IFMT)
1207 if (file_hdr->c_filesize != 0
1208 && file_hdr->c_rdev_maj == 0
1209 && file_hdr->c_rdev_min == 1)
1211 file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
1212 file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
1213 file_hdr->c_filesize = 0;
1221 /* Fill in FILE_HDR by reading a new-format ASCII format cpio header from
1222 file descriptor IN_DES, except for the magic number, which is
1223 already filled in. */
1226 read_in_new_ascii (struct cpio_file_stat *file_hdr, int in_des)
1228 struct new_ascii_header ascii_header;
1230 tape_buffered_read (ascii_header.c_ino, in_des,
1231 sizeof ascii_header - sizeof ascii_header.c_magic);
1233 file_hdr->c_ino = FROM_HEX (ascii_header.c_ino);
1234 file_hdr->c_mode = FROM_HEX (ascii_header.c_mode);
1235 file_hdr->c_uid = FROM_HEX (ascii_header.c_uid);
1236 file_hdr->c_gid = FROM_HEX (ascii_header.c_gid);
1237 file_hdr->c_nlink = FROM_HEX (ascii_header.c_nlink);
1238 file_hdr->c_mtime = FROM_HEX (ascii_header.c_mtime);
1239 file_hdr->c_filesize = FROM_HEX (ascii_header.c_filesize);
1240 file_hdr->c_dev_maj = FROM_HEX (ascii_header.c_dev_maj);
1241 file_hdr->c_dev_min = FROM_HEX (ascii_header.c_dev_min);
1242 file_hdr->c_rdev_maj = FROM_HEX (ascii_header.c_rdev_maj);
1243 file_hdr->c_rdev_min = FROM_HEX (ascii_header.c_rdev_min);
1244 file_hdr->c_namesize = FROM_HEX (ascii_header.c_namesize);
1245 file_hdr->c_chksum = FROM_HEX (ascii_header.c_chksum);
1247 /* Read file name from input. */
1248 if (file_hdr->c_name != NULL)
1249 free (file_hdr->c_name);
1250 file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
1251 tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
1253 /* In SVR4 ASCII format, the amount of space allocated for the header
1254 is rounded up to the next long-word, so we might need to drop
1256 tape_skip_padding (in_des, file_hdr->c_namesize + 110);
1259 /* Fill in FILE_HDR by reading a binary format cpio header from
1260 file descriptor IN_DES, except for the first 6 bytes (the magic
1261 number, device, and inode number), which are already filled in. */
1264 read_in_binary (struct cpio_file_stat *file_hdr,
1265 struct old_cpio_header *short_hdr,
1268 file_hdr->c_magic = short_hdr->c_magic;
1270 tape_buffered_read (((char *) short_hdr) + 6, in_des,
1271 sizeof *short_hdr - 6 /* = 20 */);
1273 /* If the magic number is byte swapped, fix the header. */
1274 if (file_hdr->c_magic == swab_short ((unsigned short) 070707))
1276 static int warned = 0;
1278 /* Alert the user that they might have to do byte swapping on
1279 the file contents. */
1282 error (0, 0, _("warning: archive header has reverse byte-order"));
1285 swab_array ((char *) &short_hdr, 13);
1288 file_hdr->c_dev_maj = major (short_hdr->c_dev);
1289 file_hdr->c_dev_min = minor (short_hdr->c_dev);
1290 file_hdr->c_ino = short_hdr->c_ino;
1291 file_hdr->c_mode = short_hdr->c_mode;
1292 file_hdr->c_uid = short_hdr->c_uid;
1293 file_hdr->c_gid = short_hdr->c_gid;
1294 file_hdr->c_nlink = short_hdr->c_nlink;
1295 file_hdr->c_rdev_maj = major (short_hdr->c_rdev);
1296 file_hdr->c_rdev_min = minor (short_hdr->c_rdev);
1297 file_hdr->c_mtime = (unsigned long) short_hdr->c_mtimes[0] << 16
1298 | short_hdr->c_mtimes[1];
1300 file_hdr->c_namesize = short_hdr->c_namesize;
1301 file_hdr->c_filesize = (unsigned long) short_hdr->c_filesizes[0] << 16
1302 | short_hdr->c_filesizes[1];
1304 /* Read file name from input. */
1305 if (file_hdr->c_name != NULL)
1306 free (file_hdr->c_name);
1307 file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
1308 tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
1310 /* In binary mode, the amount of space allocated in the header for
1311 the filename is `c_namesize' rounded up to the next short-word,
1312 so we might need to drop a byte. */
1313 if (file_hdr->c_namesize % 2)
1314 tape_toss_input (in_des, 1L);
1316 /* HP/UX cpio creates archives that look just like ordinary archives,
1317 but for devices it sets major = 0, minor = 1, and puts the
1318 actual major/minor number in the filesize field. See if this
1319 is an HP/UX cpio archive, and if so fix it. We have to do this
1320 here because process_copy_in() assumes filesize is always 0
1322 switch (file_hdr->c_mode & CP_IFMT)
1332 if (file_hdr->c_filesize != 0
1333 && file_hdr->c_rdev_maj == 0
1334 && file_hdr->c_rdev_min == 1)
1336 file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
1337 file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
1338 file_hdr->c_filesize = 0;
1346 /* Exchange the bytes of each element of the array of COUNT shorts
1350 swab_array (char *ptr, int count)
1364 #if 0 /* Now in util.c, but different */
1365 /* Return a safer suffix of FILE_NAME, or "." if it has no safer
1366 suffix. Check for fully specified file names and other atrocities. */
1369 safer_name_suffix (char const *file_name)
1373 /* Skip file system prefixes, leading file name components that contain
1374 "..", and leading slashes. */
1376 size_t prefix_len = FILE_SYSTEM_PREFIX_LEN (file_name);
1378 for (p = file_name + prefix_len; *p;)
1380 if (p[0] == '.' && p[1] == '.' && (ISSLASH (p[2]) || !p[2]))
1381 prefix_len = p + 2 - file_name;
1392 for (p = file_name + prefix_len; ISSLASH (*p); p++)
1394 prefix_len = p - file_name;
1398 char *prefix = alloca (prefix_len + 1);
1399 memcpy (prefix, file_name, prefix_len);
1400 prefix[prefix_len] = '\0';
1403 error (0, 0, _("Removing leading `%s' from member names"), prefix);
1413 /* Read the collection from standard input and create files
1414 in the file system. */
1419 char done = false; /* True if trailer reached. */
1420 FILE *tty_in = NULL; /* Interactive file for rename option. */
1421 FILE *tty_out = NULL; /* Interactive file for rename option. */
1422 FILE *rename_in = NULL; /* Batch file for rename option. */
1423 struct stat file_stat; /* Output file stat record. */
1424 struct cpio_file_stat file_hdr; /* Output header information. */
1425 int in_file_des; /* Input file descriptor. */
1426 char skip_file; /* Flag for use with patterns. */
1427 int i; /* Loop index variable. */
1429 umask (0); /* Reset umask to preserve modes of
1432 /* Initialize the copy in. */
1433 if (pattern_file_name)
1435 read_pattern_file ();
1437 file_hdr.c_name = NULL;
1439 if (rename_batch_file)
1441 rename_in = fopen (rename_batch_file, "r");
1442 if (rename_in == NULL)
1444 error (2, errno, TTY_NAME);
1447 else if (rename_flag)
1449 /* Open interactive file pair for rename operation. */
1450 tty_in = fopen (TTY_NAME, "r");
1453 error (2, errno, TTY_NAME);
1455 tty_out = fopen (TTY_NAME, "w");
1456 if (tty_out == NULL)
1458 error (2, errno, TTY_NAME);
1462 /* Get date and time if needed for processing the table option. */
1463 if (table_flag && verbose_flag)
1465 time (¤t_time);
1468 /* Check whether the input file might be a tape. */
1469 in_file_des = archive_des;
1470 if (_isrmt (in_file_des))
1472 input_is_special = 1;
1473 input_is_seekable = 0;
1477 if (fstat (in_file_des, &file_stat))
1478 error (1, errno, _("standard input is closed"));
1481 S_ISBLK (file_stat.st_mode) ||
1483 S_ISCHR (file_stat.st_mode);
1484 input_is_seekable = S_ISREG (file_stat.st_mode);
1486 output_is_seekable = true;
1488 /* While there is more input in the collection, process the input. */
1491 swapping_halfwords = swapping_bytes = false;
1493 /* Start processing the next file by reading the header. */
1494 read_in_header (&file_hdr, in_file_des);
1499 struct cpio_file_stat *h;
1502 "magic = 0%o, ino = %d, mode = 0%o, uid = %d, gid = %d\n",
1503 h->c_magic, h->c_ino, h->c_mode, h->c_uid, h->c_gid);
1505 "nlink = %d, mtime = %d, filesize = %d, dev_maj = 0x%x\n",
1506 h->c_nlink, h->c_mtime, h->c_filesize, h->c_dev_maj);
1508 "dev_min = 0x%x, rdev_maj = 0x%x, rdev_min = 0x%x, namesize = %d\n",
1509 h->c_dev_min, h->c_rdev_maj, h->c_rdev_min, h->c_namesize);
1511 "chksum = %d, name = \"%s\", tar_linkname = \"%s\"\n",
1512 h->c_chksum, h->c_name,
1513 h->c_tar_linkname ? h->c_tar_linkname : "(null)" );
1517 /* Is this the header for the TRAILER file? */
1518 if (strcmp (CPIO_TRAILER_NAME, file_hdr.c_name) == 0)
1524 cpio_safer_name_suffix (file_hdr.c_name, false, abs_paths_flag,
1527 /* Does the file name match one of the given patterns? */
1528 if (num_patterns <= 0)
1532 skip_file = copy_matching_files;
1533 for (i = 0; i < num_patterns
1534 && skip_file == copy_matching_files; i++)
1536 if (fnmatch (save_patterns[i], file_hdr.c_name, 0) == 0)
1537 skip_file = !copy_matching_files;
1543 /* If we're skipping a file with links, there might be other
1544 links that we didn't skip, and this file might have the
1545 data for the links. If it does, we'll copy in the data
1546 to the links, but not to this file. */
1547 if (file_hdr.c_nlink > 1 && (archive_format == arf_newascii
1548 || archive_format == arf_crcascii) )
1550 if (create_defered_links_to_skipped(&file_hdr, in_file_des) < 0)
1552 tape_toss_input (in_file_des, file_hdr.c_filesize);
1553 tape_skip_padding (in_file_des, file_hdr.c_filesize);
1558 tape_toss_input (in_file_des, file_hdr.c_filesize);
1559 tape_skip_padding (in_file_des, file_hdr.c_filesize);
1562 else if (table_flag)
1564 list_file(&file_hdr, in_file_des);
1566 else if (append_flag)
1568 tape_toss_input (in_file_des, file_hdr.c_filesize);
1569 tape_skip_padding (in_file_des, file_hdr.c_filesize);
1571 else if (only_verify_crc_flag)
1574 if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
1576 if (archive_format != arf_tar && archive_format != arf_ustar)
1578 tape_toss_input (in_file_des, file_hdr.c_filesize);
1579 tape_skip_padding (in_file_des, file_hdr.c_filesize);
1585 tape_toss_input (in_file_des, file_hdr.c_filesize);
1586 tape_skip_padding (in_file_des, file_hdr.c_filesize);
1587 if (crc != file_hdr.c_chksum)
1589 error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
1590 file_hdr.c_name, crc, file_hdr.c_chksum);
1592 /* Debian hack: -v and -V now work with --only-verify-crc.
1596 fprintf (stderr, "%s\n", file_hdr.c_name);
1600 fputc ('.', stderr);
1605 /* Copy the input file into the directory structure. */
1607 /* Do we need to rename the file? */
1608 if (rename_flag || rename_batch_file)
1610 if (query_rename(&file_hdr, tty_in, tty_out, rename_in) < 0)
1612 tape_toss_input (in_file_des, file_hdr.c_filesize);
1613 tape_skip_padding (in_file_des, file_hdr.c_filesize);
1618 copyin_file(&file_hdr, in_file_des);
1621 fprintf (stderr, "%s\n", file_hdr.c_name);
1623 fputc ('.', stderr);
1628 fputc ('\n', stderr);
1633 if (archive_format == arf_newascii || archive_format == arf_crcascii)
1635 create_final_defers ();
1640 blocks = (input_bytes + io_block_size - 1) / io_block_size;
1641 fprintf (stderr, ngettext ("%d block\n", "%d blocks\n", blocks), blocks);