]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - contrib/cpio/src/copyout.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / contrib / cpio / src / copyout.c
1 /* $FreeBSD$ */
2
3 /* copyout.c - create a cpio archive
4    Copyright (C) 1990, 1991, 1992, 2001, 2003, 2004,
5    2006 Free Software Foundation, Inc.
6
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)
10    any later version.
11
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.
16
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.  */
21
22 #include <system.h>
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include "filetypes.h"
28 #include "cpiohdr.h"
29 #include "dstring.h"
30 #include "extern.h"
31 #include "defer.h"
32 #include <rmt.h>
33 #include <paxlib.h>
34
35 static int check_rdev ();
36
37 /* Read FILE_SIZE bytes of FILE_NAME from IN_FILE_DES and
38    compute and return a checksum for them.  */
39
40 static unsigned int
41 read_for_checksum (int in_file_des, int file_size, char *file_name)
42 {
43   unsigned int crc;
44   char buf[BUFSIZ];
45   int bytes_left;
46   int bytes_read;
47   int i;
48
49   crc = 0;
50
51   for (bytes_left = file_size; bytes_left > 0; bytes_left -= bytes_read)
52     {
53       bytes_read = read (in_file_des, buf, BUFSIZ);
54       if (bytes_read < 0)
55         error (1, errno, _("cannot read checksum for %s"), file_name);
56       if (bytes_read == 0)
57         break;
58       if (bytes_left < bytes_read)
59         bytes_read = bytes_left;
60       for (i = 0; i < bytes_read; ++i)
61         crc += buf[i] & 0xff;
62     }
63   if (lseek (in_file_des, 0L, SEEK_SET))
64     error (1, errno, _("cannot read checksum for %s"), file_name);
65
66   return crc;
67 }
68
69 /* Write out NULs to fill out the rest of the current block on
70    OUT_FILE_DES.  */
71
72 static void
73 tape_clear_rest_of_block (int out_file_des)
74 {
75   write_nuls_to_file (io_block_size - output_size, out_file_des, 
76                       tape_buffered_write);
77 }
78
79 /* Write NULs on OUT_FILE_DES to move from OFFSET (the current location)
80    to the end of the header.  */
81
82 static void
83 tape_pad_output (int out_file_des, int offset)
84 {
85   size_t pad;
86
87   if (archive_format == arf_newascii || archive_format == arf_crcascii)
88     pad = (4 - (offset % 4)) % 4;
89   else if (archive_format == arf_tar || archive_format == arf_ustar)
90     pad = (512 - (offset % 512)) % 512;
91   else if (archive_format != arf_oldascii && archive_format != arf_hpoldascii)
92     pad = (2 - (offset % 2)) % 2;
93   else
94     pad = 0;
95
96   if (pad != 0)
97     write_nuls_to_file (pad, out_file_des, tape_buffered_write);
98 }
99
100
101 /* When creating newc and crc archives if a file has multiple (hard)
102    links, we don't put any of them into the archive until we have seen
103    all of them (or until we get to the end of the list of files that
104    are going into the archive and know that we have seen all of the links
105    to the file that we will see).  We keep these "defered" files on
106    this list.   */
107
108 struct deferment *deferouts = NULL;
109
110 /* Count the number of other (hard) links to this file that have
111    already been defered.  */
112
113 static int
114 count_defered_links_to_dev_ino (struct cpio_file_stat *file_hdr)
115 {
116   struct deferment *d;
117   int   ino;
118   int   maj;
119   int   min;
120   int   count;
121   ino = file_hdr->c_ino;
122   maj = file_hdr->c_dev_maj;
123   min = file_hdr->c_dev_min;
124   count = 0;
125   for (d = deferouts; d != NULL; d = d->next)
126     {
127       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
128           && (d->header.c_dev_min == min) )
129         ++count;
130     }
131   return count;
132 }
133
134 /* Is this file_hdr the last (hard) link to a file?  I.e., have
135    we already seen and defered all of the other links?  */
136
137 static int
138 last_link (struct cpio_file_stat *file_hdr)
139 {
140   int   other_files_sofar;
141
142   other_files_sofar = count_defered_links_to_dev_ino (file_hdr);
143   if (file_hdr->c_nlink == (other_files_sofar + 1) )
144     {
145       return 1;
146     }
147   return 0;
148 }
149
150
151 /* Add the file header for a link that is being defered to the deferouts
152    list.  */
153
154 static void
155 add_link_defer (struct cpio_file_stat *file_hdr)
156 {
157   struct deferment *d;
158   d = create_deferment (file_hdr);
159   d->next = deferouts;
160   deferouts = d;
161 }
162
163 /* We are about to put a file into a newc or crc archive that is
164    multiply linked.  We have already seen and deferred all of the
165    other links to the file but haven't written them into the archive.
166    Write the other links into the archive, and remove them from the
167    deferouts list.  */
168
169 static void
170 writeout_other_defers (struct cpio_file_stat *file_hdr, int out_des)
171 {
172   struct deferment *d;
173   struct deferment *d_prev;
174   int   ino;
175   int   maj;
176   int   min;
177   ino = file_hdr->c_ino;
178   maj = file_hdr->c_dev_maj;
179   min = file_hdr->c_dev_min;
180   d_prev = NULL;
181   d = deferouts;
182   while (d != NULL)
183     {
184       if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
185           && (d->header.c_dev_min == min) )
186         {
187           struct deferment *d_free;
188           d->header.c_filesize = 0;
189           write_out_header (&d->header, out_des);
190           if (d_prev != NULL)
191             d_prev->next = d->next;
192           else
193             deferouts = d->next;
194           d_free = d;
195           d = d->next;
196           free_deferment (d_free);
197         }
198       else
199         {
200           d_prev = d;
201           d = d->next;
202         }
203     }
204   return;
205 }
206
207 /* Write a file into the archive.  This code is the same as
208    the code in process_copy_out(), but we need it here too
209    for writeout_final_defers() to call.  */
210
211 static void
212 writeout_defered_file (struct cpio_file_stat *header, int out_file_des)
213 {
214   int in_file_des;
215   struct cpio_file_stat file_hdr;
216
217   file_hdr = *header;
218
219
220   in_file_des = open (header->c_name,
221                       O_RDONLY | O_BINARY, 0);
222   if (in_file_des < 0)
223     {
224       open_error (header->c_name);
225       return;
226     }
227
228   if (archive_format == arf_crcascii)
229     file_hdr.c_chksum = read_for_checksum (in_file_des,
230                                            file_hdr.c_filesize,
231                                            header->c_name);
232
233   if (write_out_header (&file_hdr, out_file_des))
234     return;
235   copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize,
236                            header->c_name);
237   warn_if_file_changed(header->c_name, file_hdr.c_filesize, file_hdr.c_mtime);
238
239   if (archive_format == arf_tar || archive_format == arf_ustar)
240     add_inode (file_hdr.c_ino, file_hdr.c_name, file_hdr.c_dev_maj,
241                file_hdr.c_dev_min);
242
243   tape_pad_output (out_file_des, file_hdr.c_filesize);
244
245   if (reset_time_flag)
246     set_file_times (in_file_des, file_hdr.c_name, file_hdr.c_mtime,
247                     file_hdr.c_mtime);
248   if (close (in_file_des) < 0)
249     close_error (header->c_name);
250 }
251
252 /* When writing newc and crc format archives we defer multiply linked
253    files until we have seen all of the links to the file.  If a file
254    has links to it that aren't going into the archive, then we will
255    never see the "last" link to the file, so at the end we just write 
256    all of the leftover defered files into the archive.  */
257
258 static void
259 writeout_final_defers (int out_des)
260 {
261   struct deferment *d;
262   int other_count;
263   while (deferouts != NULL)
264     {
265       d = deferouts;
266       other_count = count_defered_links_to_dev_ino (&d->header);
267       if (other_count == 1)
268         {
269           writeout_defered_file (&d->header, out_des);
270         }
271       else
272         {
273           struct cpio_file_stat file_hdr;
274           file_hdr = d->header;
275           file_hdr.c_filesize = 0;
276           write_out_header (&file_hdr, out_des);
277         }
278       deferouts = deferouts->next;
279     }
280 }
281
282 /* FIXME: to_ascii could be used instead of to_oct() and to_octal() from tar,
283    so it should be moved to paxutils too.
284    Allowed values for logbase are: 1 (binary), 2, 3 (octal), 4 (hex) */
285 int
286 to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase)
287 {
288   static char codetab[] = "0123456789ABCDEF";
289   int i = digits;
290   
291   do
292     {
293       where[--i] = codetab[(v & ((1 << logbase) - 1))];
294       v >>= logbase;
295     }
296   while (i);
297
298   return v != 0;
299 }
300
301 static void
302 field_width_error (const char *filename, const char *fieldname)
303 {
304   error (0, 0, _("%s: field width not sufficient for storing %s"),
305          filename, fieldname);
306 }
307
308 static void
309 field_width_warning (const char *filename, const char *fieldname)
310 {
311   if (warn_option & CPIO_WARN_TRUNCATE)
312     error (0, 0, _("%s: truncating %s"), filename, fieldname);
313 }
314
315 void
316 to_ascii_or_warn (char *where, uintmax_t n, size_t digits,
317                   unsigned logbase,
318                   const char *filename, const char *fieldname)
319 {
320   if (to_ascii (where, n, digits, logbase))
321     field_width_warning (filename, fieldname);
322 }    
323
324 int
325 to_ascii_or_error (char *where, uintmax_t n, size_t digits,
326                    unsigned logbase,
327                    const char *filename, const char *fieldname)
328 {
329   if (to_ascii (where, n, digits, logbase))
330     {
331       field_width_error (filename, fieldname);
332       return 1;
333     }
334   return 0;
335 }    
336
337 \f
338 int
339 write_out_new_ascii_header (const char *magic_string,
340                             struct cpio_file_stat *file_hdr, int out_des)
341 {
342   char ascii_header[110];
343   char *p;
344
345   p = stpcpy (ascii_header, magic_string);
346   to_ascii_or_warn (p, file_hdr->c_ino, 8, LG_16,
347                     file_hdr->c_name, _("inode number"));
348   p += 8;
349   to_ascii_or_warn (p, file_hdr->c_mode, 8, LG_16, file_hdr->c_name,
350                     _("file mode"));
351   p += 8;
352   to_ascii_or_warn (p, file_hdr->c_uid, 8, LG_16, file_hdr->c_name,
353                     _("uid"));
354   p += 8;
355   to_ascii_or_warn (p, file_hdr->c_gid, 8, LG_16, file_hdr->c_name,
356                     _("gid"));
357   p += 8;
358   to_ascii_or_warn (p, file_hdr->c_nlink, 8, LG_16, file_hdr->c_name,
359                     _("number of links"));
360   p += 8;
361   to_ascii_or_warn (p, file_hdr->c_mtime, 8, LG_16, file_hdr->c_name,
362                     _("modification time"));
363   p += 8;
364   if (to_ascii_or_error (p, file_hdr->c_filesize, 8, LG_16, file_hdr->c_name,
365                          _("file size")))
366     return 1;
367   p += 8;
368   if (to_ascii_or_error (p, file_hdr->c_dev_maj, 8, LG_16, file_hdr->c_name,
369                          _("device major number")))
370     return 1;
371   p += 8;
372   if (to_ascii_or_error (p, file_hdr->c_dev_min, 8, LG_16, file_hdr->c_name,
373                          _("device minor number")))
374     return 1;
375   p += 8;
376   if (to_ascii_or_error (p, file_hdr->c_rdev_maj, 8, LG_16, file_hdr->c_name,
377                          _("rdev major")))
378     return 1;
379   p += 8;
380   if (to_ascii_or_error (p, file_hdr->c_rdev_min, 8, LG_16, file_hdr->c_name,
381                          _("rdev minor")))
382     return 1;
383   p += 8;
384   if (to_ascii_or_error (p, file_hdr->c_namesize, 8, LG_16, file_hdr->c_name,
385                          _("name size")))
386     return 1;
387   p += 8;
388   to_ascii (p, file_hdr->c_chksum & 0xffffffff, 8, LG_16);
389
390   tape_buffered_write (ascii_header, out_des, sizeof ascii_header);
391
392   /* Write file name to output.  */
393   tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
394   tape_pad_output (out_des, file_hdr->c_namesize + sizeof ascii_header);
395   return 0;
396 }  
397
398 int
399 write_out_old_ascii_header (dev_t dev, dev_t rdev,
400                             struct cpio_file_stat *file_hdr, int out_des)
401 {
402   char ascii_header[76];
403   char *p = ascii_header;
404   
405   to_ascii (p, file_hdr->c_magic, 6, LG_8);
406   p += 6;
407   to_ascii_or_warn (p, dev, 6, LG_8, file_hdr->c_name, _("device number"));
408   p += 6;
409   to_ascii_or_warn (p, file_hdr->c_ino, 6, LG_8, file_hdr->c_name,
410                     _("inode number"));
411   p += 6;
412   to_ascii_or_warn (p, file_hdr->c_mode, 6, LG_8, file_hdr->c_name,
413                     _("file mode"));
414   p += 6;
415   to_ascii_or_warn (p, file_hdr->c_uid, 6, LG_8, file_hdr->c_name, _("uid"));
416   p += 6;
417   to_ascii_or_warn (p, file_hdr->c_gid, 6, LG_8, file_hdr->c_name, _("gid"));
418   p += 6;
419   to_ascii_or_warn (p, file_hdr->c_nlink, 6, LG_8, file_hdr->c_name,
420                     _("number of links"));
421   p += 6;
422   to_ascii_or_warn (p, rdev, 6, LG_8, file_hdr->c_name, _("rdev"));
423   p += 6;
424   to_ascii_or_warn (p, file_hdr->c_mtime, 11, LG_8, file_hdr->c_name,
425                     _("modification time"));
426   p += 11;
427   if (to_ascii_or_error (p, file_hdr->c_namesize, 6, LG_8, file_hdr->c_name,
428                          _("name size")))
429     return 1;
430   p += 6;
431   if (to_ascii_or_error (p, file_hdr->c_filesize, 11, LG_8, file_hdr->c_name,
432                          _("file size")))
433     return 1;
434
435   tape_buffered_write (ascii_header, out_des, sizeof ascii_header);
436
437   /* Write file name to output.  */
438   tape_buffered_write (file_hdr->c_name, out_des, file_hdr->c_namesize);
439   return 0;
440 }
441
442 void
443 hp_compute_dev (struct cpio_file_stat *file_hdr, dev_t *pdev, dev_t *prdev)
444 {
445   /* HP/UX cpio creates archives that look just like ordinary archives,
446      but for devices it sets major = 0, minor = 1, and puts the
447      actual major/minor number in the filesize field.  */
448   switch (file_hdr->c_mode & CP_IFMT)
449     {
450     case CP_IFCHR:
451     case CP_IFBLK:
452 #ifdef CP_IFSOCK
453     case CP_IFSOCK:
454 #endif
455 #ifdef CP_IFIFO
456     case CP_IFIFO:
457 #endif
458       file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj,
459                                       file_hdr->c_rdev_min);
460       *pdev = *prdev = makedev (0, 1);
461       break;
462
463     default:
464       *pdev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
465       *prdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
466       break;
467     }
468 }
469
470 int
471 write_out_binary_header (dev_t rdev,
472                          struct cpio_file_stat *file_hdr, int out_des)
473 {
474   struct old_cpio_header short_hdr;
475
476   short_hdr.c_magic = 070707;
477   short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
478
479   if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0)
480     error (0, 0, _("%s: truncating inode number"), file_hdr->c_name);
481
482   short_hdr.c_ino = file_hdr->c_ino & 0xFFFF;
483   if (short_hdr.c_ino != file_hdr->c_ino)
484     field_width_warning (file_hdr->c_name, _("inode number"));
485   
486   short_hdr.c_mode = file_hdr->c_mode & 0xFFFF;
487   if (short_hdr.c_mode != file_hdr->c_mode)
488     field_width_warning (file_hdr->c_name, _("file mode"));
489   
490   short_hdr.c_uid = file_hdr->c_uid & 0xFFFF;
491   if (short_hdr.c_uid != file_hdr->c_uid)
492     field_width_warning (file_hdr->c_name, _("uid"));
493   
494   short_hdr.c_gid = file_hdr->c_gid & 0xFFFF;
495   if (short_hdr.c_gid != file_hdr->c_gid)
496     field_width_warning (file_hdr->c_name, _("gid"));
497   
498   short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF;
499   if (short_hdr.c_nlink != file_hdr->c_nlink)
500     field_width_warning (file_hdr->c_name, _("number of links"));
501                       
502   short_hdr.c_rdev = rdev;
503   short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16;
504   short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF;
505
506   short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF;
507   if (short_hdr.c_namesize != file_hdr->c_namesize)
508     {
509       field_width_error (file_hdr->c_name, _("name size"));
510       return 1;
511     }
512                       
513   short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16;
514   short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF;
515
516   if (((off_t)short_hdr.c_filesizes[0] << 16) + short_hdr.c_filesizes[1]
517        != file_hdr->c_filesize)
518     {
519       field_width_error (file_hdr->c_name, _("file size"));
520       return 1;
521     }
522                       
523   /* Output the file header.  */
524   tape_buffered_write ((char *) &short_hdr, out_des, 26);
525
526   /* Write file name to output.  */
527   tape_buffered_write (file_hdr->c_name, out_des, file_hdr->c_namesize);
528
529   tape_pad_output (out_des, file_hdr->c_namesize + 26);
530   return 0;
531 }
532
533 \f
534 /* Write out header FILE_HDR, including the file name, to file
535    descriptor OUT_DES.  */
536
537 int 
538 write_out_header (struct cpio_file_stat *file_hdr, int out_des)
539 {
540   dev_t dev;
541   dev_t rdev;
542   
543   switch (archive_format)
544     {
545     case arf_newascii:
546       return write_out_new_ascii_header ("070701", file_hdr, out_des);
547       
548     case arf_crcascii:
549       return write_out_new_ascii_header ("070702", file_hdr, out_des);
550       
551     case arf_oldascii:
552       return write_out_old_ascii_header (makedev (file_hdr->c_dev_maj,
553                                                   file_hdr->c_dev_min),
554                                          makedev (file_hdr->c_rdev_maj,
555                                                   file_hdr->c_rdev_min),
556                                          file_hdr, out_des);
557       
558     case arf_hpoldascii:
559       hp_compute_dev (file_hdr, &dev, &rdev);
560       return write_out_old_ascii_header (dev, rdev, file_hdr, out_des);
561       
562     case arf_tar:
563     case arf_ustar:
564       if (is_tar_filename_too_long (file_hdr->c_name))
565         {
566           error (0, 0, _("%s: file name too long"), file_hdr->c_name);
567           return 1;
568         }
569       write_out_tar_header (file_hdr, out_des); /* FIXME: No error checking */
570       return 0;
571
572     case arf_binary:
573       return write_out_binary_header (makedev (file_hdr->c_rdev_maj,
574                                                file_hdr->c_rdev_min),
575                                       file_hdr, out_des);
576
577     case arf_hpbinary:
578       hp_compute_dev (file_hdr, &dev, &rdev);
579       /* FIXME: dev ignored. Should it be? */
580       return write_out_binary_header (rdev, file_hdr, out_des);
581
582     default:
583       abort ();
584     }
585 }
586
587 static void
588 assign_string (char **pvar, char *value)
589 {
590   char *p = xrealloc (*pvar, strlen (value) + 1);
591   strcpy (p, value);
592   *pvar = p;
593 }
594
595 /* Read a list of file names from the standard input
596    and write a cpio collection on the standard output.
597    The format of the header depends on the compatibility (-c) flag.  */
598
599 void
600 process_copy_out ()
601 {
602   int res;                      /* Result of functions.  */
603   dynamic_string input_name;    /* Name of file read from stdin.  */
604   struct stat file_stat;        /* Stat record for file.  */
605   struct cpio_file_stat file_hdr; /* Output header information.  */
606   int in_file_des;              /* Source file descriptor.  */
607   int out_file_des;             /* Output file descriptor.  */
608   char *orig_file_name = NULL;
609
610   /* Initialize the copy out.  */
611   ds_init (&input_name, 128);
612   file_hdr.c_magic = 070707;
613
614   /* Check whether the output file might be a tape.  */
615   out_file_des = archive_des;
616   if (_isrmt (out_file_des))
617     {
618       output_is_special = 1;
619       output_is_seekable = 0;
620     }
621   else
622     {
623       if (fstat (out_file_des, &file_stat))
624         error (1, errno, _("standard output is closed"));
625       output_is_special =
626 #ifdef S_ISBLK
627         S_ISBLK (file_stat.st_mode) ||
628 #endif
629         S_ISCHR (file_stat.st_mode);
630       output_is_seekable = S_ISREG (file_stat.st_mode);
631     }
632
633   if (append_flag)
634     {
635       process_copy_in ();
636       prepare_append (out_file_des);
637     }
638
639   /* Copy files with names read from stdin.  */
640   while (ds_fgetstr (stdin, &input_name, name_end) != NULL)
641     {
642       /* Check for blank line.  */
643       if (input_name.ds_string[0] == 0)
644         {
645           error (0, 0, _("blank line ignored"));
646           continue;
647         }
648
649       /* Process next file.  */
650       if ((*xstat) (input_name.ds_string, &file_stat) < 0)
651         stat_error (input_name.ds_string);
652       else
653         {
654           /* Set values in output header.  */
655           stat_to_cpio (&file_hdr, &file_stat);
656           
657           if (archive_format == arf_tar || archive_format == arf_ustar)
658             {
659               if (file_hdr.c_mode & CP_IFDIR)
660                 {
661                   int len = strlen (input_name.ds_string);
662                   /* Make sure the name ends with a slash */
663                   if (input_name.ds_string[len-1] != '/')
664                     {
665                       ds_resize (&input_name, len + 2);
666                       input_name.ds_string[len] = '/';
667                       input_name.ds_string[len+1] = 0;
668                     }
669                 }
670             }
671
672           switch (check_rdev (&file_hdr))
673             {
674               case 1:
675                 error (0, 0, "%s not dumped: major number would be truncated",
676                        file_hdr.c_name);
677                 continue;
678               case 2:
679                 error (0, 0, "%s not dumped: minor number would be truncated",
680                        file_hdr.c_name);
681                 continue;
682               case 4:
683                 error (0, 0, "%s not dumped: device number would be truncated",
684                        file_hdr.c_name);
685                 continue;
686             }
687
688           assign_string (&orig_file_name, input_name.ds_string);
689           cpio_safer_name_suffix (input_name.ds_string, false,
690                                   abs_paths_flag, true);
691 #ifndef HPUX_CDF
692           file_hdr.c_name = input_name.ds_string;
693           file_hdr.c_namesize = strlen (input_name.ds_string) + 1;
694 #else
695           if ( (archive_format != arf_tar) && (archive_format != arf_ustar) )
696             {
697               /* We mark CDF's in cpio files by adding a 2nd `/' after the
698                  "hidden" directory name.  We need to do this so we can
699                  properly recreate the directory as hidden (in case the
700                  files of a directory go into the archive before the
701                  directory itself (e.g from "find ... -depth ... | cpio")).  */
702               file_hdr.c_name = add_cdf_double_slashes (input_name.ds_string);
703               file_hdr.c_namesize = strlen (file_hdr.c_name) + 1;
704             }
705           else
706             {
707               /* We don't mark CDF's in tar files.  We assume the "hidden"
708                  directory will always go into the archive before any of
709                  its files.  */
710               file_hdr.c_name = input_name.ds_string;
711               file_hdr.c_namesize = strlen (input_name.ds_string) + 1;
712             }
713 #endif
714
715           /* Copy the named file to the output.  */
716           switch (file_hdr.c_mode & CP_IFMT)
717             {
718             case CP_IFREG:
719               if (archive_format == arf_tar || archive_format == arf_ustar)
720                 {
721                   char *otherfile;
722                   if ((otherfile = find_inode_file (file_hdr.c_ino,
723                                                     file_hdr.c_dev_maj,
724                                                     file_hdr.c_dev_min)))
725                     {
726                       file_hdr.c_tar_linkname = otherfile;
727                       if (write_out_header (&file_hdr, out_file_des))
728                         continue;
729                       break;
730                     }
731                 }
732               if ( (archive_format == arf_newascii || archive_format == arf_crcascii)
733                   && (file_hdr.c_nlink > 1) )
734                 {
735                   if (last_link (&file_hdr) )
736                     {
737                       writeout_other_defers (&file_hdr, out_file_des);
738                     }
739                   else
740                     {
741                       add_link_defer (&file_hdr);
742                       break;
743                     }
744                 }
745               in_file_des = open (orig_file_name,
746                                   O_RDONLY | O_BINARY, 0);
747               if (in_file_des < 0)
748                 {
749                   open_error (orig_file_name);
750                   continue;
751                 }
752
753               if (archive_format == arf_crcascii)
754                 file_hdr.c_chksum = read_for_checksum (in_file_des,
755                                                        file_hdr.c_filesize,
756                                                        orig_file_name);
757
758               if (write_out_header (&file_hdr, out_file_des))
759                 continue;
760               copy_files_disk_to_tape (in_file_des,
761                                        out_file_des, file_hdr.c_filesize,
762                                        orig_file_name);
763               warn_if_file_changed(orig_file_name, file_hdr.c_filesize,
764                                    file_hdr.c_mtime);
765
766               if (archive_format == arf_tar || archive_format == arf_ustar)
767                 add_inode (file_hdr.c_ino, orig_file_name, file_hdr.c_dev_maj,
768                            file_hdr.c_dev_min);
769
770               tape_pad_output (out_file_des, file_hdr.c_filesize);
771
772               if (reset_time_flag)
773                 set_file_times (in_file_des,
774                                 orig_file_name,
775                                 file_stat.st_atime, file_stat.st_mtime);
776               if (close (in_file_des) < 0)
777                 close_error (orig_file_name);
778               break;
779
780             case CP_IFDIR:
781               file_hdr.c_filesize = 0;
782               if (write_out_header (&file_hdr, out_file_des))
783                 continue;
784               break;
785
786             case CP_IFCHR:
787             case CP_IFBLK:
788 #ifdef CP_IFSOCK
789             case CP_IFSOCK:
790 #endif
791 #ifdef CP_IFIFO
792             case CP_IFIFO:
793 #endif
794               if (archive_format == arf_tar)
795                 {
796                   error (0, 0, _("%s not dumped: not a regular file"),
797                          orig_file_name);
798                   continue;
799                 }
800               else if (archive_format == arf_ustar)
801                 {
802                   char *otherfile;
803                   if ((otherfile = find_inode_file (file_hdr.c_ino,
804                                                     file_hdr.c_dev_maj,
805                                                     file_hdr.c_dev_min)))
806                     {
807                       /* This file is linked to another file already in the 
808                          archive, so write it out as a hard link. */
809                       file_hdr.c_mode = (file_stat.st_mode & 07777);
810                       file_hdr.c_mode |= CP_IFREG;
811                       file_hdr.c_tar_linkname = otherfile;
812                       if (write_out_header (&file_hdr, out_file_des))
813                         continue;
814                       break;
815                     }
816                   add_inode (file_hdr.c_ino, orig_file_name, 
817                              file_hdr.c_dev_maj, file_hdr.c_dev_min);
818                 }
819               file_hdr.c_filesize = 0;
820               if (write_out_header (&file_hdr, out_file_des))
821                 continue;
822               break;
823
824 #ifdef CP_IFLNK
825             case CP_IFLNK:
826               {
827                 char *link_name = (char *) xmalloc (file_stat.st_size + 1);
828                 int link_size;
829
830                 link_size = readlink (orig_file_name, link_name,
831                                       file_stat.st_size);
832                 if (link_size < 0)
833                   {
834                     readlink_warn (orig_file_name);
835                     free (link_name);
836                     continue;
837                   }
838                 link_name[link_size] = 0;
839                 cpio_safer_name_suffix (link_name, false,
840                                         abs_paths_flag, true);
841                 link_size = strlen (link_name);
842                 file_hdr.c_filesize = link_size;
843                 if (archive_format == arf_tar || archive_format == arf_ustar)
844                   {
845                     if (link_size + 1 > 100)
846                       {
847                         error (0, 0, _("%s: symbolic link too long"),
848                                file_hdr.c_name);
849                       }
850                     else
851                       {
852                         link_name[link_size] = '\0';
853                         file_hdr.c_tar_linkname = link_name;
854                         if (write_out_header (&file_hdr, out_file_des))
855                           continue;
856                       }
857                   }
858                 else
859                   {
860                     if (write_out_header (&file_hdr, out_file_des))
861                       continue;
862                     tape_buffered_write (link_name, out_file_des, link_size);
863                     tape_pad_output (out_file_des, link_size);
864                   }
865                 free (link_name);
866               }
867               break;
868 #endif
869
870             default:
871               error (0, 0, _("%s: unknown file type"), orig_file_name);
872             }
873           
874           if (verbose_flag)
875             fprintf (stderr, "%s\n", orig_file_name);
876           if (dot_flag)
877             fputc ('.', stderr);
878         }
879     }
880
881   free (orig_file_name);
882   
883   writeout_final_defers(out_file_des);
884   /* The collection is complete; append the trailer.  */
885   file_hdr.c_ino = 0;
886   file_hdr.c_mode = 0;
887   file_hdr.c_uid = 0;
888   file_hdr.c_gid = 0;
889   file_hdr.c_nlink = 1;         /* Must be 1 for crc format.  */
890   file_hdr.c_dev_maj = 0;
891   file_hdr.c_dev_min = 0;
892   file_hdr.c_rdev_maj = 0;
893   file_hdr.c_rdev_min = 0;
894   file_hdr.c_mtime = 0;
895   file_hdr.c_chksum = 0;
896
897   file_hdr.c_filesize = 0;
898   file_hdr.c_namesize = 11;
899   file_hdr.c_name = CPIO_TRAILER_NAME;
900   if (archive_format != arf_tar && archive_format != arf_ustar)
901     write_out_header (&file_hdr, out_file_des);
902   else
903     write_nuls_to_file (1024, out_file_des, tape_buffered_write);
904
905   /* Fill up the output block.  */
906   tape_clear_rest_of_block (out_file_des);
907   tape_empty_output_buffer (out_file_des);
908   if (dot_flag)
909     fputc ('\n', stderr);
910   if (!quiet_flag)
911     {
912       res = (output_bytes + io_block_size - 1) / io_block_size;
913       fprintf (stderr, ngettext ("%d block\n", "%d blocks\n", res), res);
914     }
915 }
916
917 static int
918 check_rdev (file_hdr)
919      struct cpio_file_stat *file_hdr;
920 {
921   if (archive_format == arf_newascii || archive_format == arf_crcascii)
922     {
923       if ((file_hdr->c_rdev_maj & 0xFFFFFFFF) != file_hdr->c_rdev_maj)
924         return 1;
925       if ((file_hdr->c_rdev_min & 0xFFFFFFFF) != file_hdr->c_rdev_min)
926         return 2;
927     }
928   else if (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
929     {
930 #ifndef __MSDOS__
931       dev_t rdev;
932
933       rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
934       if (archive_format == arf_oldascii)
935         {
936           if ((rdev & 0xFFFF) != rdev)
937             return 4;
938         }
939       else
940         {
941           switch (file_hdr->c_mode & CP_IFMT)
942             {
943               case CP_IFCHR:
944               case CP_IFBLK:
945 #ifdef CP_IFSOCK
946               case CP_IFSOCK:
947 #endif
948 #ifdef CP_IFIFO
949               case CP_IFIFO:
950 #endif
951                 /* We could handle one more bit if longs are >= 33 bits.  */
952                 if ((rdev & 037777777777) != rdev)
953                   return 4;
954                 break;
955               default:
956                 if ((rdev & 0xFFFF) != rdev)
957                   return 4;
958                 break;
959             }
960         }
961 #endif
962     }
963   else if (archive_format == arf_tar || archive_format == arf_ustar)
964     {
965       /* The major and minor formats are limited to 7 octal digits in ustar
966          format, and to_oct () adds a gratuitous trailing blank to further
967          limit the format to 6 octal digits.  */
968       if ((file_hdr->c_rdev_maj & 0777777) != file_hdr->c_rdev_maj)
969         return 1;
970       if ((file_hdr->c_rdev_min & 0777777) != file_hdr->c_rdev_min)
971         return 2;
972     }
973   else
974     {
975 #ifndef __MSDOS__
976       dev_t rdev;
977
978       rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
979       if (archive_format != arf_hpbinary)
980         {
981           if ((rdev & 0xFFFF) != rdev)
982         return 4;
983     }
984   else
985     {
986       switch (file_hdr->c_mode & CP_IFMT)
987         {
988           case CP_IFCHR:
989           case CP_IFBLK:
990 #ifdef CP_IFSOCK
991           case CP_IFSOCK:
992 #endif
993 #ifdef CP_IFIFO
994           case CP_IFIFO:
995 #endif
996             if ((rdev & 0xFFFFFFFF) != rdev)
997               return 4;
998             file_hdr->c_filesize = rdev;
999             rdev = makedev (0, 1);
1000             break;
1001           default:
1002             if ((rdev & 0xFFFF) != rdev)
1003               return 4;
1004             break;
1005         }
1006     }
1007 #endif
1008   }
1009   return 0;
1010 }