1 /* copyin.c - extract or list a cpio archive
2    Copyright (C) 1990-1992, 2001-2007, 2009-2010, 2014-2015, 2017 Free
3    Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3, or (at your option)
8    any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public
16    License along with this program; if not, write to the Free
17    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18    Boston, MA 02110-1301 USA.  */
19 
20 #include <system.h>
21 
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include "filetypes.h"
26 #include "cpiohdr.h"
27 #include "dstring.h"
28 #include "extern.h"
29 #include "defer.h"
30 #include <rmt.h>
31 #ifndef	FNM_PATHNAME
32 # include <fnmatch.h>
33 #endif
34 
35 #ifndef HAVE_LCHOWN
36 # define lchown(f,u,g) 0
37 #endif
38 
39 static void copyin_regular_file(struct cpio_file_stat* file_hdr,
40 				int in_file_des);
41 
42 void
warn_junk_bytes(long bytes_skipped)43 warn_junk_bytes (long bytes_skipped)
44 {
45   error (0, 0, ngettext ("warning: skipped %ld byte of junk",
46 			 "warning: skipped %ld bytes of junk", bytes_skipped),
47 	 bytes_skipped);
48 }
49 
50 
51 static int
query_rename(struct cpio_file_stat * file_hdr,FILE * tty_in,FILE * tty_out,FILE * rename_in)52 query_rename(struct cpio_file_stat* file_hdr, FILE *tty_in, FILE *tty_out,
53 	     FILE *rename_in)
54 {
55   char *str_res;		/* Result for string function.  */
56   static dynamic_string new_name;	/* New file name for rename option.  */
57   static int initialized_new_name = false;
58   if (!initialized_new_name)
59   {
60     ds_init (&new_name, 128);
61     initialized_new_name = true;
62   }
63 
64   if (rename_flag)
65     {
66       fprintf (tty_out, _("rename %s -> "), file_hdr->c_name);
67       fflush (tty_out);
68       str_res = ds_fgets (tty_in, &new_name);
69     }
70   else
71     {
72       str_res = ds_fgetstr (rename_in, &new_name, '\n');
73     }
74   if (str_res == NULL || str_res[0] == 0)
75     {
76       return -1;
77     }
78   else
79     cpio_set_c_name (file_hdr, new_name.ds_string);
80   return 0;
81 }
82 
83 /* Skip the padding on IN_FILE_DES after a header or file,
84    up to the next header.
85    The number of bytes skipped is based on OFFSET -- the current offset
86    from the last start of a header (or file) -- and the current
87    header type.  */
88 
89 static void
tape_skip_padding(int in_file_des,off_t offset)90 tape_skip_padding (int in_file_des, off_t offset)
91 {
92   off_t pad;
93 
94   if (archive_format == arf_crcascii || archive_format == arf_newascii)
95     pad = (4 - (offset % 4)) % 4;
96   else if (archive_format == arf_binary || archive_format == arf_hpbinary)
97     pad = (2 - (offset % 2)) % 2;
98   else if (archive_format == arf_tar || archive_format == arf_ustar)
99     pad = (512 - (offset % 512)) % 512;
100   else
101     pad = 0;
102 
103   if (pad != 0)
104     tape_toss_input (in_file_des, pad);
105 }
106 
107 static char *
get_link_name(struct cpio_file_stat * file_hdr,int in_file_des)108 get_link_name (struct cpio_file_stat *file_hdr, int in_file_des)
109 {
110   char *link_name;
111 
112   if (file_hdr->c_filesize < 0 || file_hdr->c_filesize > SIZE_MAX-1)
113     {
114       error (0, 0, _("%s: stored filename length is out of range"),
115 	     file_hdr->c_name);
116       link_name = NULL;
117     }
118   else
119     {
120       link_name = xmalloc (file_hdr->c_filesize + 1);
121       tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
122       link_name[file_hdr->c_filesize] = '\0';
123       tape_skip_padding (in_file_des, file_hdr->c_filesize);
124     }
125   return link_name;
126 }
127 
128 static void
list_file(struct cpio_file_stat * file_hdr,int in_file_des)129 list_file (struct cpio_file_stat* file_hdr, int in_file_des)
130 {
131   if (verbose_flag)
132     {
133 #ifdef CP_IFLNK
134       if ((file_hdr->c_mode & CP_IFMT) == CP_IFLNK)
135 	{
136 	  if (archive_format != arf_tar && archive_format != arf_ustar)
137 	    {
138 	      char *link_name = get_link_name (file_hdr, in_file_des);
139 	      if (link_name)
140 		{
141 		  long_format (file_hdr, link_name);
142 		  free (link_name);
143 		}
144 	    }
145 	  else
146 	    long_format (file_hdr, file_hdr->c_tar_linkname);
147 	  return;
148 	}
149       else
150 #endif
151 	long_format (file_hdr, (char *) 0);
152     }
153   else
154     {
155       /* Print out the name as it is.  The name_end delimiter is normally
156 	 '\n', but can be reset to '\0' by the -0 option. */
157       printf ("%s%c", file_hdr->c_name, name_end);
158     }
159 
160   crc = 0;
161   tape_toss_input (in_file_des, file_hdr->c_filesize);
162   tape_skip_padding (in_file_des, file_hdr->c_filesize);
163   if (only_verify_crc_flag)
164     {
165 #ifdef CP_IFLNK
166       if ((file_hdr->c_mode & CP_IFMT) == CP_IFLNK)
167 	{
168 	  return;   /* links don't have a checksum */
169 	}
170 #endif
171       if (crc != file_hdr->c_chksum)
172 	{
173 	  error (0, 0, _("%s: checksum error (0x%x, should be 0x%x)"),
174 		 file_hdr->c_name, crc, file_hdr->c_chksum);
175 	}
176     }
177 }
178 
179 static int
try_existing_file(struct cpio_file_stat * file_hdr,int in_file_des,bool * existing_dir)180 try_existing_file (struct cpio_file_stat* file_hdr, int in_file_des,
181 		   bool *existing_dir)
182 {
183   struct stat file_stat;
184 
185   *existing_dir = false;
186   if (lstat (file_hdr->c_name, &file_stat) == 0)
187     {
188       if (S_ISDIR (file_stat.st_mode)
189 	  && ((file_hdr->c_mode & CP_IFMT) == CP_IFDIR))
190 	{
191 	  /* If there is already a directory there that
192 	     we are trying to create, don't complain about
193 	     it.  */
194 	  *existing_dir = true;
195 	  return 0;
196 	}
197       else if (!unconditional_flag
198 	       && file_hdr->c_mtime <= file_stat.st_mtime)
199 	{
200 	  error (0, 0, _("%s not created: newer or same age version exists"),
201 		 file_hdr->c_name);
202 	  tape_toss_input (in_file_des, file_hdr->c_filesize);
203 	  tape_skip_padding (in_file_des, file_hdr->c_filesize);
204 	  return -1;	/* Go to the next file.  */
205 	}
206       else if (S_ISDIR (file_stat.st_mode)
207 		? rmdir (file_hdr->c_name)
208 		: unlink (file_hdr->c_name))
209 	{
210 	  error (0, errno, _("cannot remove current %s"),
211 		 file_hdr->c_name);
212 	  tape_toss_input (in_file_des, file_hdr->c_filesize);
213 	  tape_skip_padding (in_file_des, file_hdr->c_filesize);
214 	  return -1;	/* Go to the next file.  */
215 	}
216     }
217   return 0;
218 }
219 
220 /* The newc and crc formats store multiply linked copies of the same file
221    in the archive only once.  The actual data is attached to the last link
222    in the archive, and the other links all have a filesize of 0.  When a
223    file in the archive has multiple links and a filesize of 0, its data is
224    probably "attatched" to another file in the archive, so we can't create
225    it right away.  We have to "defer" creating it until we have created
226    the file that has the data "attatched" to it.  We keep a list of the
227    "defered" links on deferments.  */
228 
229 struct deferment *deferments = NULL;
230 
231 /* Add a file header to the deferments list.  For now they all just
232    go on one list, although we could optimize this if necessary.  */
233 
234 static void
defer_copyin(struct cpio_file_stat * file_hdr)235 defer_copyin (struct cpio_file_stat *file_hdr)
236 {
237   struct deferment *d;
238   d = create_deferment (file_hdr);
239   d->next = deferments;
240   deferments = d;
241   return;
242 }
243 
244 /* We just created a file that (probably) has some other links to it
245    which have been defered.  Go through all of the links on the deferments
246    list and create any which are links to this file.  */
247 
248 static void
create_defered_links(struct cpio_file_stat * file_hdr)249 create_defered_links (struct cpio_file_stat *file_hdr)
250 {
251   struct deferment *d;
252   struct deferment *d_prev;
253   ino_t	ino;
254   int 	maj;
255   int   min;
256   int 	link_res;
257   ino = file_hdr->c_ino;
258   maj = file_hdr->c_dev_maj;
259   min = file_hdr->c_dev_min;
260   d = deferments;
261   d_prev = NULL;
262   while (d != NULL)
263     {
264       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
265 	  && (d->header.c_dev_min == min) )
266 	{
267 	  struct deferment *d_free;
268 	  link_res = link_to_name (d->header.c_name, file_hdr->c_name);
269 	  if (link_res < 0)
270 	    {
271 	      error (0, errno, _("cannot link %s to %s"),
272 		     d->header.c_name, file_hdr->c_name);
273 	    }
274 	  if (d_prev != NULL)
275 	    d_prev->next = d->next;
276 	  else
277 	    deferments = d->next;
278 	  d_free = d;
279 	  d = d->next;
280 	  free_deferment (d_free);
281 	}
282       else
283 	{
284 	  d_prev = d;
285 	  d = d->next;
286 	}
287     }
288 }
289 
290 /* We are skipping a file but there might be other links to it that we
291    did not skip, so we have to copy its data for the other links.  Find
292    the first link that we didn't skip and try to create that.  That will
293    then create the other deferred links.  */
294 
295 static int
create_defered_links_to_skipped(struct cpio_file_stat * file_hdr,int in_file_des)296 create_defered_links_to_skipped (struct cpio_file_stat *file_hdr,
297 				 int in_file_des)
298 {
299   struct deferment *d;
300   struct deferment *d_prev;
301   ino_t	ino;
302   int 	maj;
303   int   min;
304   if (file_hdr->c_filesize == 0)
305     {
306       /* The file doesn't have any data attached to it so we don't have
307          to bother.  */
308       return -1;
309     }
310   ino = file_hdr->c_ino;
311   maj = file_hdr->c_dev_maj;
312   min = file_hdr->c_dev_min;
313   d = deferments;
314   d_prev = NULL;
315   while (d != NULL)
316     {
317       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
318 	  && (d->header.c_dev_min == min) )
319 	{
320 	  if (d_prev != NULL)
321 	    d_prev->next = d->next;
322 	  else
323 	    deferments = d->next;
324 	  cpio_set_c_name (file_hdr, d->header.c_name);
325 	  free_deferment (d);
326 	  copyin_regular_file(file_hdr, in_file_des);
327 	  return 0;
328 	}
329       else
330 	{
331 	  d_prev = d;
332 	  d = d->next;
333 	}
334     }
335   return -1;
336 }
337 
338 /* If we had a multiply linked file that really was empty then we would
339    have defered all of its links, since we never found any with data
340    "attached", and they will still be on the deferment list even when
341    we are done reading the whole archive.  Write out all of these
342    empty links that are still on the deferments list.  */
343 
344 static void
create_final_defers()345 create_final_defers ()
346 {
347   struct deferment *d;
348   int	link_res;
349   int	out_file_des;
350 
351   for (d = deferments; d != NULL; d = d->next)
352     {
353       /* Debian hack: A line, which could cause an endless loop, was
354          removed (97/1/2).  It was reported by Ronald F. Guilmette to
355          the upstream maintainers. -BEM */
356       /* Debian hack:  This was reported by Horst Knobloch. This bug has
357          been reported to "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM
358          */
359       link_res = link_to_maj_min_ino (d->header.c_name,
360 		    d->header.c_dev_maj, d->header.c_dev_min,
361 		    d->header.c_ino);
362       if (link_res == 0)
363 	{
364 	  continue;
365 	}
366       out_file_des = open (d->header.c_name,
367 			   O_CREAT | O_WRONLY | O_BINARY, 0600);
368       if (out_file_des < 0 && create_dir_flag)
369 	{
370 	  create_all_directories (d->header.c_name);
371 	  out_file_des = open (d->header.c_name,
372 			       O_CREAT | O_WRONLY | O_BINARY,
373 			       0600);
374 	}
375       if (out_file_des < 0)
376 	{
377 	  open_error (d->header.c_name);
378 	  continue;
379 	}
380 
381       set_perms (out_file_des, &d->header);
382 
383       if (close (out_file_des) < 0)
384 	close_error (d->header.c_name);
385 
386     }
387 }
388 
389 static void
copyin_regular_file(struct cpio_file_stat * file_hdr,int in_file_des)390 copyin_regular_file (struct cpio_file_stat* file_hdr, int in_file_des)
391 {
392   int out_file_des;		/* Output file descriptor.  */
393 
394   if (to_stdout_option)
395     out_file_des = STDOUT_FILENO;
396   else
397     {
398       /* Can the current file be linked to a previously copied file? */
399       if (file_hdr->c_nlink > 1
400 	  && (archive_format == arf_newascii
401 	      || archive_format == arf_crcascii) )
402 	{
403 	  int link_res;
404 	  if (file_hdr->c_filesize == 0)
405 	    {
406 	      /* The newc and crc formats store multiply linked copies
407 		 of the same file in the archive only once.  The
408 		 actual data is attached to the last link in the
409 		 archive, and the other links all have a filesize
410 		 of 0.  Since this file has multiple links and a
411 		 filesize of 0, its data is probably attatched to
412 		 another file in the archive.  Save the link, and
413 		 process it later when we get the actual data.  We
414 		 can't just create it with length 0 and add the
415 		 data later, in case the file is readonly.  We still
416 		 lose if its parent directory is readonly (and we aren't
417 		 running as root), but there's nothing we can do about
418 		 that.  */
419 	      defer_copyin (file_hdr);
420 	      tape_toss_input (in_file_des, file_hdr->c_filesize);
421 	      tape_skip_padding (in_file_des, file_hdr->c_filesize);
422 	      return;
423 	    }
424 	  /* If the file has data (filesize != 0), then presumably
425 	     any other links have already been defer_copyin'ed(),
426 	     but GNU cpio version 2.0-2.2 didn't do that, so we
427 	     still have to check for links here (and also in case
428 	     the archive was created and later appeneded to). */
429 	  /* Debian hack: (97/1/2) This was reported by Ronald
430 	     F. Guilmette to the upstream maintainers. -BEM */
431 	  link_res = link_to_maj_min_ino (file_hdr->c_name,
432 		    file_hdr->c_dev_maj, file_hdr->c_dev_min,
433 					  file_hdr->c_ino);
434 	  if (link_res == 0)
435 	    {
436 	      tape_toss_input (in_file_des, file_hdr->c_filesize);
437 	      tape_skip_padding (in_file_des, file_hdr->c_filesize);
438 	      return;
439 	    }
440 	}
441       else if (file_hdr->c_nlink > 1
442 	       && archive_format != arf_tar
443 	       && archive_format != arf_ustar)
444 	{
445 	  int link_res;
446 	  /* Debian hack: (97/1/2) This was reported by Ronald
447 	     F. Guilmette to the upstream maintainers. -BEM */
448 	  link_res = link_to_maj_min_ino (file_hdr->c_name,
449 					  file_hdr->c_dev_maj,
450 					  file_hdr->c_dev_min,
451 					  file_hdr->c_ino);
452 	  if (link_res == 0)
453 	    {
454 	      tape_toss_input (in_file_des, file_hdr->c_filesize);
455 	      tape_skip_padding (in_file_des, file_hdr->c_filesize);
456 	      return;
457 	    }
458 	}
459       else if ((archive_format == arf_tar || archive_format == arf_ustar)
460 	       && file_hdr->c_tar_linkname
461 	       && file_hdr->c_tar_linkname[0] != '\0')
462 	{
463 	  int	link_res;
464 	  link_res = link_to_name (file_hdr->c_name, file_hdr->c_tar_linkname);
465 	  if (link_res < 0)
466 	    {
467 	      error (0, errno, _("cannot link %s to %s"),
468 		     file_hdr->c_tar_linkname, file_hdr->c_name);
469 	    }
470 	  return;
471 	}
472 
473       /* If not linked, copy the contents of the file.  */
474       out_file_des = open (file_hdr->c_name,
475 			   O_CREAT | O_WRONLY | O_BINARY, 0600);
476 
477       if (out_file_des < 0 && create_dir_flag)
478 	{
479 	  create_all_directories (file_hdr->c_name);
480 	  out_file_des = open (file_hdr->c_name,
481 			       O_CREAT | O_WRONLY | O_BINARY,
482 			       0600);
483 	}
484 
485       if (out_file_des < 0)
486 	{
487 	  open_error (file_hdr->c_name);
488 	  tape_toss_input (in_file_des, file_hdr->c_filesize);
489 	  tape_skip_padding (in_file_des, file_hdr->c_filesize);
490 	  return;
491 	}
492     }
493 
494   crc = 0;
495   if (swap_halfwords_flag)
496     {
497       if ((file_hdr->c_filesize % 4) == 0)
498 	swapping_halfwords = true;
499       else
500 	error (0, 0, _("cannot swap halfwords of %s: odd number of halfwords"),
501 	       file_hdr->c_name);
502     }
503   if (swap_bytes_flag)
504     {
505       if ((file_hdr->c_filesize % 2) == 0)
506 	swapping_bytes = true;
507       else
508 	error (0, 0, _("cannot swap bytes of %s: odd number of bytes"),
509 	       file_hdr->c_name);
510     }
511   copy_files_tape_to_disk (in_file_des, out_file_des, file_hdr->c_filesize);
512   disk_empty_output_buffer (out_file_des, true);
513 
514   if (to_stdout_option)
515     {
516       if (archive_format == arf_crcascii)
517 	{
518 	  if (crc != file_hdr->c_chksum)
519 	    error (0, 0, _("%s: checksum error (0x%x, should be 0x%x)"),
520 		   file_hdr->c_name, crc, file_hdr->c_chksum);
521 	}
522       tape_skip_padding (in_file_des, file_hdr->c_filesize);
523       return;
524     }
525 
526   set_perms (out_file_des, file_hdr);
527 
528   if (close (out_file_des) < 0)
529     close_error (file_hdr->c_name);
530 
531   if (archive_format == arf_crcascii)
532     {
533       if (crc != file_hdr->c_chksum)
534 	error (0, 0, _("%s: checksum error (0x%x, should be 0x%x)"),
535 	       file_hdr->c_name, crc, file_hdr->c_chksum);
536     }
537 
538   tape_skip_padding (in_file_des, file_hdr->c_filesize);
539   if (file_hdr->c_nlink > 1
540       && (archive_format == arf_newascii || archive_format == arf_crcascii) )
541     {
542       /* (see comment above for how the newc and crc formats
543 	 store multiple links).  Now that we have the data
544 	 for this file, create any other links to it which
545 	 we defered.  */
546       create_defered_links (file_hdr);
547     }
548 }
549 
550 static void
copyin_device(struct cpio_file_stat * file_hdr)551 copyin_device (struct cpio_file_stat* file_hdr)
552 {
553   int res;			/* Result of various function calls.  */
554 
555   if (to_stdout_option)
556     return;
557 
558   if (file_hdr->c_nlink > 1 && archive_format != arf_tar
559       && archive_format != arf_ustar)
560     {
561       int link_res;
562       /* Debian hack:  This was reported by Horst
563 	 Knobloch. This bug has been reported to
564 	 "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
565       link_res = link_to_maj_min_ino (file_hdr->c_name,
566 		    file_hdr->c_dev_maj, file_hdr->c_dev_min,
567 		    file_hdr->c_ino);
568       if (link_res == 0)
569 	{
570 	  return;
571 	}
572     }
573   else if (archive_format == arf_ustar &&
574 	   file_hdr->c_tar_linkname &&
575 	   file_hdr->c_tar_linkname [0] != '\0')
576     {
577       int	link_res;
578       link_res = link_to_name (file_hdr->c_name,
579 			       file_hdr->c_tar_linkname);
580       if (link_res < 0)
581 	{
582 	  error (0, errno, _("cannot link %s to %s"),
583 		 file_hdr->c_tar_linkname, file_hdr->c_name);
584 	  /* Something must be wrong, because we couldn't
585 	     find the file to link to.  But can we assume
586 	     that the device maj/min numbers are correct
587 	     and fall through to the mknod?  It's probably
588 	     safer to just return, rather than possibly
589 	     creating a bogus device file.  */
590 	}
591       return;
592     }
593 
594   res = mknod (file_hdr->c_name, file_hdr->c_mode,
595 	    makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
596   if (res < 0 && create_dir_flag)
597     {
598       create_all_directories (file_hdr->c_name);
599       res = mknod (file_hdr->c_name, file_hdr->c_mode,
600 	    makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
601     }
602   if (res < 0)
603     {
604       mknod_error (file_hdr->c_name);
605       return;
606     }
607   if (!no_chown_flag)
608     {
609       uid_t uid = set_owner_flag ? set_owner : file_hdr->c_uid;
610       gid_t gid = set_group_flag ? set_group : file_hdr->c_gid;
611       if ((chown (file_hdr->c_name, uid, gid) < 0)
612 	  && errno != EPERM)
613         chown_error_details (file_hdr->c_name, uid, gid);
614     }
615   /* chown may have turned off some permissions we wanted. */
616   if (chmod (file_hdr->c_name, file_hdr->c_mode) < 0)
617     chmod_error_details (file_hdr->c_name, file_hdr->c_mode);
618   if (retain_time_flag)
619     set_file_times (-1, file_hdr->c_name, file_hdr->c_mtime,
620 		    file_hdr->c_mtime);
621 }
622 
623 static void
copyin_link(struct cpio_file_stat * file_hdr,int in_file_des)624 copyin_link (struct cpio_file_stat *file_hdr, int in_file_des)
625 {
626   char *link_name = NULL;	/* Name of hard and symbolic links.  */
627   int res;			/* Result of various function calls.  */
628 
629   if (archive_format != arf_tar && archive_format != arf_ustar)
630     {
631       if (to_stdout_option)
632         {
633           tape_toss_input (in_file_des, file_hdr->c_filesize);
634           tape_skip_padding (in_file_des, file_hdr->c_filesize);
635           return;
636         }
637       link_name = get_link_name (file_hdr, in_file_des);
638       if (!link_name)
639 	return;
640     }
641   else
642     {
643       if (to_stdout_option)
644         return;
645       link_name = xstrdup (file_hdr->c_tar_linkname);
646     }
647 
648   cpio_safer_name_suffix (link_name, true, !no_abs_paths_flag, false);
649 
650   res = UMASKED_SYMLINK (link_name, file_hdr->c_name,
651 			 file_hdr->c_mode);
652   if (res < 0 && create_dir_flag)
653     {
654       create_all_directories (file_hdr->c_name);
655       res = UMASKED_SYMLINK (link_name, file_hdr->c_name, file_hdr->c_mode);
656     }
657   if (res < 0)
658     {
659       error (0, errno, _("%s: Cannot symlink to %s"),
660 	     quotearg_colon (link_name), quote_n (1, file_hdr->c_name));
661       free (link_name);
662       return;
663     }
664   if (!no_chown_flag)
665     {
666       uid_t uid = set_owner_flag ? set_owner : file_hdr->c_uid;
667       gid_t gid = set_group_flag ? set_group : file_hdr->c_gid;
668       if ((lchown (file_hdr->c_name, uid, gid) < 0)
669   	  && errno != EPERM)
670 	chown_error_details (file_hdr->c_name, uid, gid);
671     }
672   free (link_name);
673 }
674 
675 static void
copyin_file(struct cpio_file_stat * file_hdr,int in_file_des)676 copyin_file (struct cpio_file_stat *file_hdr, int in_file_des)
677 {
678   bool existing_dir = false;
679 
680   if (!to_stdout_option
681       && try_existing_file (file_hdr, in_file_des, &existing_dir) < 0)
682     return;
683 
684   /* Do the real copy or link.  */
685   switch (file_hdr->c_mode & CP_IFMT)
686     {
687     case CP_IFREG:
688       copyin_regular_file (file_hdr, in_file_des);
689       break;
690 
691     case CP_IFDIR:
692       cpio_create_dir (file_hdr, existing_dir);
693       break;
694 
695     case CP_IFCHR:
696     case CP_IFBLK:
697 #ifdef CP_IFSOCK
698     case CP_IFSOCK:
699 #endif
700 #ifdef CP_IFIFO
701     case CP_IFIFO:
702 #endif
703       copyin_device (file_hdr);
704       break;
705 
706 #ifdef CP_IFLNK
707     case CP_IFLNK:
708       copyin_link (file_hdr, in_file_des);
709       break;
710 #endif
711 
712     default:
713       error (0, 0, _("%s: unknown file type"), file_hdr->c_name);
714       tape_toss_input (in_file_des, file_hdr->c_filesize);
715       tape_skip_padding (in_file_des, file_hdr->c_filesize);
716     }
717 }
718 
719 
720 /* Current time for verbose table.  */
721 static time_t current_time;
722 
723 
724 /* Print the file described by FILE_HDR in long format.
725    If LINK_NAME is nonzero, it is the name of the file that
726    this file is a symbolic link to.  */
727 
728 void
long_format(struct cpio_file_stat * file_hdr,char const * link_name)729 long_format (struct cpio_file_stat *file_hdr, char const *link_name)
730 {
731   char mbuf[11];
732   char tbuf[40];
733   time_t when;
734 
735   mode_string (file_hdr->c_mode, mbuf);
736   mbuf[10] = '\0';
737 
738   /* Get time values ready to print.  */
739   when = file_hdr->c_mtime;
740   strcpy (tbuf, ctime (&when));
741   if (current_time - when > 6L * 30L * 24L * 60L * 60L
742       || current_time - when < 0L)
743     {
744       /* The file is older than 6 months, or in the future.
745 	 Show the year instead of the time of day.  */
746       strcpy (tbuf + 11, tbuf + 19);
747     }
748   tbuf[16] = '\0';
749 
750   printf ("%s %3lu ", mbuf, (unsigned long) file_hdr->c_nlink);
751 
752   if (numeric_uid)
753     printf ("%-8u %-8u ", (unsigned int) file_hdr->c_uid,
754 	    (unsigned int) file_hdr->c_gid);
755   else
756     printf ("%-8.8s %-8.8s ", getuser (file_hdr->c_uid),
757 	    getgroup (file_hdr->c_gid));
758 
759   if ((file_hdr->c_mode & CP_IFMT) == CP_IFCHR
760       || (file_hdr->c_mode & CP_IFMT) == CP_IFBLK)
761     printf ("%3lu, %3lu ", file_hdr->c_rdev_maj,
762 	    file_hdr->c_rdev_min);
763   else
764     printf ("%8"PRIuMAX" ", (uintmax_t) file_hdr->c_filesize);
765 
766   printf ("%s ", tbuf + 4);
767 
768   printf ("%s", quotearg (file_hdr->c_name));
769   if (link_name)
770     {
771       printf (" -> ");
772       printf ("%s", quotearg (link_name));
773     }
774   putc ('\n', stdout);
775 }
776 
777 /* Read a pattern file (for the -E option).  Put a list of
778    `num_patterns' elements in `save_patterns'.  Any patterns that were
779    already in `save_patterns' (from the command line) are preserved.  */
780 
781 static void
read_pattern_file()782 read_pattern_file ()
783 {
784   int max_new_patterns;
785   char **new_save_patterns;
786   int new_num_patterns;
787   int i;
788   dynamic_string pattern_name;
789   FILE *pattern_fp;
790 
791   if (num_patterns < 0)
792     num_patterns = 0;
793   max_new_patterns = 1 + num_patterns;
794   new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
795   new_num_patterns = num_patterns;
796   ds_init (&pattern_name, 128);
797 
798   pattern_fp = fopen (pattern_file_name, "r");
799   if (pattern_fp == NULL)
800     open_fatal (pattern_file_name);
801   while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
802     {
803       if (new_num_patterns >= max_new_patterns)
804 	{
805 	  max_new_patterns += 1;
806 	  new_save_patterns = (char **)
807 	    xrealloc ((char *) new_save_patterns,
808 		      max_new_patterns * sizeof (char *));
809 	}
810       new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
811       ++new_num_patterns;
812     }
813   if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
814     close_error (pattern_file_name);
815 
816   for (i = 0; i < num_patterns; ++i)
817     new_save_patterns[i] = save_patterns[i];
818 
819   save_patterns = new_save_patterns;
820   num_patterns = new_num_patterns;
821 }
822 
823 
824 uintmax_t
from_ascii(char const * where,size_t digs,unsigned logbase)825 from_ascii (char const *where, size_t digs, unsigned logbase)
826 {
827   uintmax_t value = 0;
828   char const *buf = where;
829   char const *end = buf + digs;
830   int overflow = 0;
831   static char codetab[] = "0123456789ABCDEF";
832 
833   for (; *buf == ' '; buf++)
834     {
835       if (buf == end)
836 	return 0;
837     }
838 
839   if (buf == end || *buf == 0)
840     return 0;
841   while (1)
842     {
843       unsigned d;
844 
845       char *p = strchr (codetab, toupper (*buf));
846       if (!p)
847 	{
848 	  error (0, 0, _("Malformed number %.*s"), (int) digs, where);
849 	  break;
850 	}
851 
852       d = p - codetab;
853       if ((d >> logbase) > 1)
854 	{
855 	  error (0, 0, _("Malformed number %.*s"), (int) digs, where);
856 	  break;
857 	}
858       value += d;
859       if (++buf == end || *buf == 0)
860 	break;
861       overflow |= value ^ (value << logbase >> logbase);
862       value <<= logbase;
863     }
864   if (overflow)
865     error (0, 0, _("Archive value %.*s is out of range"),
866 	   (int) digs, where);
867   return value;
868 }
869 
870 
871 
872 /* Return 16-bit integer I with the bytes swapped.  */
873 #define swab_short(i) ((((i) << 8) & 0xff00) | (((i) >> 8) & 0x00ff))
874 
875 /* Read the header, including the name of the file, from file
876    descriptor IN_DES into FILE_HDR.  */
877 
878 void
read_in_header(struct cpio_file_stat * file_hdr,int in_des)879 read_in_header (struct cpio_file_stat *file_hdr, int in_des)
880 {
881   union {
882     char str[6];
883     unsigned short num;
884     struct old_cpio_header old_header;
885   } magic;
886   long bytes_skipped = 0;	/* Bytes of junk found before magic number.  */
887 
888   /* Search for a valid magic number.  */
889 
890   if (archive_format == arf_unknown)
891     {
892       union
893       {
894 	char s[512];
895 	unsigned short us;
896       }	tmpbuf;
897       int check_tar;
898       int peeked_bytes;
899 
900       while (archive_format == arf_unknown)
901 	{
902 	  peeked_bytes = tape_buffered_peek (tmpbuf.s, in_des, 512);
903 	  if (peeked_bytes < 6)
904 	    error (PAXEXIT_FAILURE, 0, _("premature end of archive"));
905 
906 	  if (!strncmp (tmpbuf.s, "070701", 6))
907 	    archive_format = arf_newascii;
908 	  else if (!strncmp (tmpbuf.s, "070707", 6))
909 	    archive_format = arf_oldascii;
910 	  else if (!strncmp (tmpbuf.s, "070702", 6))
911 	    {
912 	      archive_format = arf_crcascii;
913 	      crc_i_flag = true;
914 	    }
915 	  else if (tmpbuf.us == 070707
916 		   || tmpbuf.us == swab_short ((unsigned short) 070707))
917 	    archive_format = arf_binary;
918 	  else if (peeked_bytes >= 512
919 		   && (check_tar = is_tar_header (tmpbuf.s)))
920 	    {
921 	      if (check_tar == 2)
922 		archive_format = arf_ustar;
923 	      else
924 		archive_format = arf_tar;
925 	    }
926 	  else
927 	    {
928 	      tape_buffered_read (tmpbuf.s, in_des, 1L);
929 	      ++bytes_skipped;
930 	    }
931 	}
932     }
933 
934   if (archive_format == arf_tar || archive_format == arf_ustar)
935     {
936       if (append_flag)
937 	last_header_start = input_bytes - io_block_size +
938 	  (in_buff - input_buffer);
939       if (bytes_skipped > 0)
940 	warn_junk_bytes (bytes_skipped);
941 
942       read_in_tar_header (file_hdr, in_des);
943       return;
944     }
945 
946   file_hdr->c_tar_linkname = NULL;
947 
948   tape_buffered_read (magic.str, in_des, sizeof (magic.str));
949   while (1)
950     {
951       if (append_flag)
952 	last_header_start = input_bytes - io_block_size
953 	  + (in_buff - input_buffer) - 6;
954       if (archive_format == arf_newascii
955 	  && !strncmp (magic.str, "070701", 6))
956 	{
957 	  if (bytes_skipped > 0)
958 	    warn_junk_bytes (bytes_skipped);
959 	  file_hdr->c_magic = 070701;
960 	  read_in_new_ascii (file_hdr, in_des);
961 	  break;
962 	}
963       if (archive_format == arf_crcascii
964 	  && !strncmp (magic.str, "070702", 6))
965 	{
966 	  if (bytes_skipped > 0)
967 	    warn_junk_bytes (bytes_skipped);
968 	  file_hdr->c_magic = 070702;
969 	  read_in_new_ascii (file_hdr, in_des);
970 	  break;
971 	}
972       if ( (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
973 	  && !strncmp (magic.str, "070707", 6))
974 	{
975 	  if (bytes_skipped > 0)
976 	    warn_junk_bytes (bytes_skipped);
977 	  file_hdr->c_magic = 070707;
978 	  read_in_old_ascii (file_hdr, in_des);
979 	  break;
980 	}
981       if ( (archive_format == arf_binary || archive_format == arf_hpbinary)
982 	  && (magic.num == 070707
983 	      || magic.num == swab_short ((unsigned short) 070707)))
984 	{
985 	  /* Having to skip 1 byte because of word alignment is normal.  */
986 	  if (bytes_skipped > 0)
987 	    warn_junk_bytes (bytes_skipped);
988 	  file_hdr->c_magic = 070707;
989 	  read_in_binary (file_hdr, &magic.old_header, in_des);
990 	  break;
991 	}
992       bytes_skipped++;
993       memmove (magic.str, magic.str + 1, sizeof (magic.str) - 1);
994       tape_buffered_read (magic.str + sizeof (magic.str) - 1, in_des, 1L);
995     }
996 }
997 
998 static void
read_name_from_file(struct cpio_file_stat * file_hdr,int fd,uintmax_t len)999 read_name_from_file (struct cpio_file_stat *file_hdr, int fd, uintmax_t len)
1000 {
1001   cpio_realloc_c_name (file_hdr, len);
1002   tape_buffered_read (file_hdr->c_name, fd, len);
1003   file_hdr->c_namesize = len;
1004 }
1005 
1006 /* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
1007    file descriptor IN_DES, except for the magic number, which is
1008    already filled in.  */
1009 
1010 void
read_in_old_ascii(struct cpio_file_stat * file_hdr,int in_des)1011 read_in_old_ascii (struct cpio_file_stat *file_hdr, int in_des)
1012 {
1013   struct old_ascii_header ascii_header;
1014   unsigned long dev;
1015 
1016   tape_buffered_read (ascii_header.c_dev, in_des,
1017 		      sizeof ascii_header - sizeof ascii_header.c_magic);
1018   dev = FROM_OCTAL (ascii_header.c_dev);
1019   file_hdr->c_dev_maj = major (dev);
1020   file_hdr->c_dev_min = minor (dev);
1021 
1022   file_hdr->c_ino = FROM_OCTAL (ascii_header.c_ino);
1023   file_hdr->c_mode = FROM_OCTAL (ascii_header.c_mode);
1024   file_hdr->c_uid = FROM_OCTAL (ascii_header.c_uid);
1025   file_hdr->c_gid = FROM_OCTAL (ascii_header.c_gid);
1026   file_hdr->c_nlink = FROM_OCTAL (ascii_header.c_nlink);
1027   dev = FROM_OCTAL (ascii_header.c_rdev);
1028   file_hdr->c_rdev_maj = major (dev);
1029   file_hdr->c_rdev_min = minor (dev);
1030 
1031   file_hdr->c_mtime = FROM_OCTAL (ascii_header.c_mtime);
1032   file_hdr->c_filesize = FROM_OCTAL (ascii_header.c_filesize);
1033   read_name_from_file (file_hdr, in_des, FROM_OCTAL (ascii_header.c_namesize));
1034 
1035   /* HP/UX cpio creates archives that look just like ordinary archives,
1036      but for devices it sets major = 0, minor = 1, and puts the
1037      actual major/minor number in the filesize field.  See if this
1038      is an HP/UX cpio archive, and if so fix it.  We have to do this
1039      here because process_copy_in() assumes filesize is always 0
1040      for devices.  */
1041   switch (file_hdr->c_mode & CP_IFMT)
1042     {
1043       case CP_IFCHR:
1044       case CP_IFBLK:
1045 #ifdef CP_IFSOCK
1046       case CP_IFSOCK:
1047 #endif
1048 #ifdef CP_IFIFO
1049       case CP_IFIFO:
1050 #endif
1051 	if (file_hdr->c_filesize != 0
1052 	    && file_hdr->c_rdev_maj == 0
1053 	    && file_hdr->c_rdev_min == 1)
1054 	  {
1055 	    file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
1056 	    file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
1057 	    file_hdr->c_filesize = 0;
1058 	  }
1059 	break;
1060       default:
1061 	break;
1062     }
1063 }
1064 
1065 /* Fill in FILE_HDR by reading a new-format ASCII format cpio header from
1066    file descriptor IN_DES, except for the magic number, which is
1067    already filled in.  */
1068 
1069 void
read_in_new_ascii(struct cpio_file_stat * file_hdr,int in_des)1070 read_in_new_ascii (struct cpio_file_stat *file_hdr, int in_des)
1071 {
1072   struct new_ascii_header ascii_header;
1073 
1074   tape_buffered_read (ascii_header.c_ino, in_des,
1075 		      sizeof ascii_header - sizeof ascii_header.c_magic);
1076 
1077   file_hdr->c_ino = FROM_HEX (ascii_header.c_ino);
1078   file_hdr->c_mode = FROM_HEX (ascii_header.c_mode);
1079   file_hdr->c_uid = FROM_HEX (ascii_header.c_uid);
1080   file_hdr->c_gid = FROM_HEX (ascii_header.c_gid);
1081   file_hdr->c_nlink = FROM_HEX (ascii_header.c_nlink);
1082   file_hdr->c_mtime = FROM_HEX (ascii_header.c_mtime);
1083   file_hdr->c_filesize = FROM_HEX (ascii_header.c_filesize);
1084   file_hdr->c_dev_maj = FROM_HEX (ascii_header.c_dev_maj);
1085   file_hdr->c_dev_min = FROM_HEX (ascii_header.c_dev_min);
1086   file_hdr->c_rdev_maj = FROM_HEX (ascii_header.c_rdev_maj);
1087   file_hdr->c_rdev_min = FROM_HEX (ascii_header.c_rdev_min);
1088   file_hdr->c_chksum = FROM_HEX (ascii_header.c_chksum);
1089   read_name_from_file (file_hdr, in_des, FROM_HEX (ascii_header.c_namesize));
1090 
1091   /* In SVR4 ASCII format, the amount of space allocated for the header
1092      is rounded up to the next long-word, so we might need to drop
1093      1-3 bytes.  */
1094   tape_skip_padding (in_des, file_hdr->c_namesize + 110);
1095 }
1096 
1097 /* Fill in FILE_HDR by reading a binary format cpio header from
1098    file descriptor IN_DES, except for the first 6 bytes (the magic
1099    number, device, and inode number), which are already filled in.  */
1100 
1101 void
read_in_binary(struct cpio_file_stat * file_hdr,struct old_cpio_header * short_hdr,int in_des)1102 read_in_binary (struct cpio_file_stat *file_hdr,
1103 		struct old_cpio_header *short_hdr,
1104 		int in_des)
1105 {
1106   file_hdr->c_magic = short_hdr->c_magic;
1107 
1108   tape_buffered_read (((char *) short_hdr) + 6, in_des,
1109 		      sizeof *short_hdr - 6 /* = 20 */);
1110 
1111   /* If the magic number is byte swapped, fix the header.  */
1112   if (file_hdr->c_magic == swab_short ((unsigned short) 070707))
1113     {
1114       static int warned = 0;
1115 
1116       /* Alert the user that they might have to do byte swapping on
1117 	 the file contents.  */
1118       if (warned == 0)
1119 	{
1120 	  error (0, 0, _("warning: archive header has reverse byte-order"));
1121 	  warned = 1;
1122 	}
1123       swab_array ((char *) short_hdr, 13);
1124     }
1125 
1126   file_hdr->c_dev_maj = major (short_hdr->c_dev);
1127   file_hdr->c_dev_min = minor (short_hdr->c_dev);
1128   file_hdr->c_ino = short_hdr->c_ino;
1129   file_hdr->c_mode = short_hdr->c_mode;
1130   file_hdr->c_uid = short_hdr->c_uid;
1131   file_hdr->c_gid = short_hdr->c_gid;
1132   file_hdr->c_nlink = short_hdr->c_nlink;
1133   file_hdr->c_rdev_maj = major (short_hdr->c_rdev);
1134   file_hdr->c_rdev_min = minor (short_hdr->c_rdev);
1135   file_hdr->c_mtime = (unsigned long) short_hdr->c_mtimes[0] << 16
1136                       | short_hdr->c_mtimes[1];
1137   file_hdr->c_filesize = (unsigned long) short_hdr->c_filesizes[0] << 16
1138                       | short_hdr->c_filesizes[1];
1139   read_name_from_file (file_hdr, in_des, short_hdr->c_namesize);
1140 
1141   /* In binary mode, the amount of space allocated in the header for
1142      the filename is `c_namesize' rounded up to the next short-word,
1143      so we might need to drop a byte.  */
1144   if (file_hdr->c_namesize % 2)
1145     tape_toss_input (in_des, 1L);
1146 
1147   /* HP/UX cpio creates archives that look just like ordinary archives,
1148      but for devices it sets major = 0, minor = 1, and puts the
1149      actual major/minor number in the filesize field.  See if this
1150      is an HP/UX cpio archive, and if so fix it.  We have to do this
1151      here because process_copy_in() assumes filesize is always 0
1152      for devices.  */
1153   switch (file_hdr->c_mode & CP_IFMT)
1154     {
1155       case CP_IFCHR:
1156       case CP_IFBLK:
1157 #ifdef CP_IFSOCK
1158       case CP_IFSOCK:
1159 #endif
1160 #ifdef CP_IFIFO
1161       case CP_IFIFO:
1162 #endif
1163 	if (file_hdr->c_filesize != 0
1164 	    && file_hdr->c_rdev_maj == 0
1165 	    && file_hdr->c_rdev_min == 1)
1166 	  {
1167 	    file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
1168 	    file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
1169 	    file_hdr->c_filesize = 0;
1170 	  }
1171 	break;
1172       default:
1173 	break;
1174     }
1175 }
1176 
1177 /* Exchange the bytes of each element of the array of COUNT shorts
1178    starting at PTR.  */
1179 
1180 void
swab_array(char * ptr,int count)1181 swab_array (char *ptr, int count)
1182 {
1183   char tmp;
1184 
1185   while (count-- > 0)
1186     {
1187       tmp = *ptr;
1188       *ptr = *(ptr + 1);
1189       ++ptr;
1190       *ptr = tmp;
1191       ++ptr;
1192     }
1193 }
1194 
1195 /* Read the collection from standard input and create files
1196    in the file system.  */
1197 
1198 void
process_copy_in()1199 process_copy_in ()
1200 {
1201   char done = false;		/* True if trailer reached.  */
1202   FILE *tty_in = NULL;		/* Interactive file for rename option.  */
1203   FILE *tty_out = NULL;		/* Interactive file for rename option.  */
1204   FILE *rename_in = NULL;	/* Batch file for rename option.  */
1205   struct stat file_stat;	/* Output file stat record.  */
1206   struct cpio_file_stat file_hdr = CPIO_FILE_STAT_INITIALIZER;
1207                                 /* Output header information.  */
1208   int in_file_des;		/* Input file descriptor.  */
1209   char skip_file;		/* Flag for use with patterns.  */
1210   int i;			/* Loop index variable.  */
1211 
1212   newdir_umask = umask (0);     /* Reset umask to preserve modes of
1213 				   created files  */
1214 
1215   /* Initialize the copy in.  */
1216   if (pattern_file_name)
1217     {
1218       read_pattern_file ();
1219     }
1220 
1221   if (rename_batch_file)
1222     {
1223       rename_in = fopen (rename_batch_file, "r");
1224       if (rename_in == NULL)
1225 	{
1226 	  error (PAXEXIT_FAILURE, errno, TTY_NAME);
1227 	}
1228     }
1229   else if (rename_flag)
1230     {
1231       /* Open interactive file pair for rename operation.  */
1232       tty_in = fopen (TTY_NAME, "r");
1233       if (tty_in == NULL)
1234 	{
1235 	  error (PAXEXIT_FAILURE, errno, TTY_NAME);
1236 	}
1237       tty_out = fopen (TTY_NAME, "w");
1238       if (tty_out == NULL)
1239 	{
1240 	  error (PAXEXIT_FAILURE, errno, TTY_NAME);
1241 	}
1242     }
1243 
1244   /* Get date and time if needed for processing the table option.  */
1245   if (table_flag && verbose_flag)
1246     {
1247       time (&current_time);
1248     }
1249 
1250   /* Check whether the input file might be a tape.  */
1251   in_file_des = archive_des;
1252   if (_isrmt (in_file_des))
1253     {
1254       input_is_special = 1;
1255       input_is_seekable = 0;
1256     }
1257   else
1258     {
1259       if (fstat (in_file_des, &file_stat))
1260 	error (PAXEXIT_FAILURE, errno, _("standard input is closed"));
1261       input_is_special =
1262 #ifdef S_ISBLK
1263 	S_ISBLK (file_stat.st_mode) ||
1264 #endif
1265 	S_ISCHR (file_stat.st_mode);
1266       input_is_seekable = S_ISREG (file_stat.st_mode);
1267     }
1268   output_is_seekable = true;
1269 
1270   change_dir ();
1271 
1272   /* While there is more input in the collection, process the input.  */
1273   while (!done)
1274     {
1275       swapping_halfwords = swapping_bytes = false;
1276 
1277       /* Start processing the next file by reading the header.  */
1278       read_in_header (&file_hdr, in_file_des);
1279 
1280 #ifdef DEBUG_CPIO
1281       if (debug_flag)
1282 	{
1283 	  struct cpio_file_stat *h;
1284 	  h = &file_hdr;
1285 	  fprintf (stderr,
1286 		"magic = 0%o, ino = %ld, mode = 0%o, uid = %d, gid = %d\n",
1287 		h->c_magic, (long)h->c_ino, h->c_mode, h->c_uid, h->c_gid);
1288 	  fprintf (stderr,
1289 		"nlink = %d, mtime = %d, filesize = %d, dev_maj = 0x%x\n",
1290 		h->c_nlink, h->c_mtime, h->c_filesize, h->c_dev_maj);
1291 	  fprintf (stderr,
1292 	        "dev_min = 0x%x, rdev_maj = 0x%x, rdev_min = 0x%x, namesize = %d\n",
1293 		h->c_dev_min, h->c_rdev_maj, h->c_rdev_min, h->c_namesize);
1294 	  fprintf (stderr,
1295 		"chksum = %d, name = \"%s\", tar_linkname = \"%s\"\n",
1296 		h->c_chksum, h->c_name,
1297 		h->c_tar_linkname ? h->c_tar_linkname : "(null)" );
1298 
1299 	}
1300 #endif
1301       if (file_hdr.c_namesize == 0)
1302 	skip_file = true;
1303       else
1304 	{
1305 	  /* Is this the header for the TRAILER file?  */
1306 	  if (strcmp (CPIO_TRAILER_NAME, file_hdr.c_name) == 0)
1307 	    {
1308 	      done = true;
1309 	      break;
1310 	    }
1311 
1312 	  cpio_safer_name_suffix (file_hdr.c_name, false, !no_abs_paths_flag,
1313 				  false);
1314 
1315 	  /* Does the file name match one of the given patterns?  */
1316 	  if (num_patterns <= 0)
1317 	    skip_file = false;
1318 	  else
1319 	    {
1320 	      skip_file = copy_matching_files;
1321 	      for (i = 0; i < num_patterns
1322 		     && skip_file == copy_matching_files; i++)
1323 		{
1324 		  if (fnmatch (save_patterns[i], file_hdr.c_name, 0) == 0)
1325 		    skip_file = !copy_matching_files;
1326 		}
1327 	    }
1328 	}
1329 
1330       if (skip_file)
1331 	{
1332 	  /* If we're skipping a file with links, there might be other
1333 	     links that we didn't skip, and this file might have the
1334 	     data for the links.  If it does, we'll copy in the data
1335 	     to the links, but not to this file.  */
1336 	  if (file_hdr.c_nlink > 1 && (archive_format == arf_newascii
1337 	      || archive_format == arf_crcascii) )
1338 	    {
1339 	      if (create_defered_links_to_skipped(&file_hdr, in_file_des) < 0)
1340 	        {
1341 		  tape_toss_input (in_file_des, file_hdr.c_filesize);
1342 		  tape_skip_padding (in_file_des, file_hdr.c_filesize);
1343 		}
1344 	    }
1345 	  else
1346 	    {
1347 	      tape_toss_input (in_file_des, file_hdr.c_filesize);
1348 	      tape_skip_padding (in_file_des, file_hdr.c_filesize);
1349 	    }
1350 	}
1351       else if (table_flag)
1352 	{
1353 	  list_file (&file_hdr, in_file_des);
1354 	}
1355       else if (append_flag)
1356 	{
1357 	  tape_toss_input (in_file_des, file_hdr.c_filesize);
1358 	  tape_skip_padding (in_file_des, file_hdr.c_filesize);
1359 	}
1360       else if (only_verify_crc_flag)
1361 	{
1362 #ifdef CP_IFLNK
1363 	  if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
1364 	    {
1365 	      if (archive_format != arf_tar && archive_format != arf_ustar)
1366 		{
1367 		  tape_toss_input (in_file_des, file_hdr.c_filesize);
1368 		  tape_skip_padding (in_file_des, file_hdr.c_filesize);
1369 		  continue;
1370 		}
1371 	    }
1372 #endif
1373 	    crc = 0;
1374 	    tape_toss_input (in_file_des, file_hdr.c_filesize);
1375 	    tape_skip_padding (in_file_des, file_hdr.c_filesize);
1376 	    if (crc != file_hdr.c_chksum)
1377 	      {
1378 		error (0, 0, _("%s: checksum error (0x%x, should be 0x%x)"),
1379 		       file_hdr.c_name, crc, file_hdr.c_chksum);
1380 	      }
1381          /* Debian hack: -v and -V now work with --only-verify-crc.
1382             (99/11/10) -BEM */
1383 	    if (verbose_flag)
1384 	      {
1385 		fprintf (stderr, "%s\n", file_hdr.c_name);
1386 	      }
1387 	    if (dot_flag)
1388 	      {
1389 		fputc ('.', stderr);
1390 	      }
1391 	}
1392       else
1393 	{
1394 	  /* Copy the input file into the directory structure.  */
1395 
1396 	  /* Do we need to rename the file? */
1397 	  if (rename_flag || rename_batch_file)
1398 	    {
1399 	      if (query_rename(&file_hdr, tty_in, tty_out, rename_in) < 0)
1400 	        {
1401 		  tape_toss_input (in_file_des, file_hdr.c_filesize);
1402 		  tape_skip_padding (in_file_des, file_hdr.c_filesize);
1403 		  continue;
1404 		}
1405 	    }
1406 
1407 	  copyin_file(&file_hdr, in_file_des);
1408 
1409 	  if (verbose_flag)
1410 	    fprintf (stderr, "%s\n", file_hdr.c_name);
1411 	  if (dot_flag)
1412 	    fputc ('.', stderr);
1413 	}
1414     }
1415 
1416   if (dot_flag)
1417     fputc ('\n', stderr);
1418 
1419   apply_delayed_set_stat ();
1420 
1421   cpio_file_stat_free (&file_hdr);
1422 
1423   if (append_flag)
1424     return;
1425 
1426   if (archive_format == arf_newascii || archive_format == arf_crcascii)
1427     {
1428       create_final_defers ();
1429     }
1430   if (!quiet_flag)
1431     {
1432       size_t blocks;
1433       blocks = (input_bytes + io_block_size - 1) / io_block_size;
1434       fprintf (stderr,
1435 	       ngettext ("%lu block\n", "%lu blocks\n",
1436 			 (unsigned long) blocks),
1437 	       (unsigned long) blocks);
1438     }
1439 }
1440 
1441