Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / tar / src / create.c
1 /* Create a tar archive.
2    Copyright 1985,92,93,94,96,97,99,2000, 2001 Free Software Foundation, Inc.
3    Written by John Gilmore, on 1985-08-25.
4
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by the
7    Free Software Foundation; either version 2, or (at your option) any later
8    version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
13    Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 /* $FreeBSD: src/contrib/tar/src/create.c,v 1.2.2.1 2002/07/14 13:19:44 sobomax Exp $ */
20
21 #include "system.h"
22
23 #if !MSDOS
24 # include <pwd.h>
25 # include <grp.h>
26 #endif
27
28 #if HAVE_UTIME_H
29 # include <utime.h>
30 #else
31 struct utimbuf
32   {
33     long actime;
34     long modtime;
35   };
36 #endif
37
38 #include <quotearg.h>
39
40 #include "common.h"
41 #include <hash.h>
42
43 #ifndef MSDOS
44 extern dev_t ar_dev;
45 extern ino_t ar_ino;
46 #endif
47
48 struct link
49   {
50     dev_t dev;
51     ino_t ino;
52     char name[1];
53   };
54 \f
55 /* The maximum uintmax_t value that can be represented with DIGITS digits,
56    assuming that each digit is BITS_PER_DIGIT wide.  */
57 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
58    ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
59     ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
60     : (uintmax_t) -1)
61
62 /* Convert VALUE to an octal representation suitable for tar headers.
63    Output to buffer WHERE with size SIZE.
64    The result is undefined if SIZE is 0 or if VALUE is too large to fit.  */
65
66 static void
67 to_octal (uintmax_t value, char *where, size_t size)
68 {
69   uintmax_t v = value;
70   size_t i = size;
71
72   do
73     {
74       where[--i] = '0' + (v & ((1 << LG_8) - 1));
75       v >>= LG_8;
76     }
77   while (i);
78 }
79
80 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
81    tar headers.  NEGATIVE is 1 if VALUE was negative before being cast
82    to uintmax_t, 0 otherwise.  Output to buffer WHERE with size SIZE.
83    The result is undefined if SIZE is 0 or if VALUE is too large to
84    fit.  */
85
86 static void
87 to_base256 (int negative, uintmax_t value, char *where, size_t size)
88 {
89   uintmax_t v = value;
90   uintmax_t propagated_sign_bits =
91     ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
92   size_t i = size;
93
94   do
95     {
96       where[--i] = v & ((1 << LG_256) - 1);
97       v = propagated_sign_bits | (v >> LG_256);
98     }
99   while (i);
100 }
101
102 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
103    external form, using SUBSTITUTE (...) if VALUE won't fit.  Output
104    to buffer WHERE with size SIZE.  NEGATIVE is 1 iff VALUE was
105    negative before being cast to uintmax_t; its original bitpattern
106    can be deduced from VALSIZE, its original size before casting.
107    TYPE is the kind of value being output (useful for diagnostics).
108    Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
109    digits), followed by '\0'.  If this won't work, and if GNU or
110    OLDGNU format is allowed, use '\200' followed by base-256, or (if
111    NEGATIVE is nonzero) '\377' followed by two's complement base-256.
112    If neither format works, use SUBSTITUTE (...)  instead.  Pass to
113    SUBSTITUTE the address of an 0-or-1 flag recording whether the
114    substitute value is negative.  */
115
116 static void
117 to_chars (int negative, uintmax_t value, size_t valsize,
118           uintmax_t (*substitute) PARAMS ((int *)),
119           char *where, size_t size, const char *type)
120 {
121   int base256_allowed = (archive_format == GNU_FORMAT
122                          || archive_format == OLDGNU_FORMAT);
123
124   /* Generate the POSIX octal representation if the number fits.  */
125   if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
126     {
127       where[size - 1] = '\0';
128       to_octal (value, where, size - 1);
129     }
130
131   /* Otherwise, generate the base-256 representation if we are
132      generating an old or new GNU format and if the number fits.  */
133   else if (((negative ? -1 - value : value)
134             <= MAX_VAL_WITH_DIGITS (size - 1, LG_256))
135            && base256_allowed)
136     {
137       where[0] = negative ? -1 : 1 << (LG_256 - 1);
138       to_base256 (negative, value, where + 1, size - 1);
139     }
140
141   /* Otherwise, if the number is negative, and if it would not cause
142      ambiguity on this host by confusing positive with negative
143      values, then generate the POSIX octal representation of the value
144      modulo 2**(field bits).  The resulting tar file is
145      machine-dependent, since it depends on the host word size.  Yuck!
146      But this is the traditional behavior.  */
147   else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
148     {
149       static int warned_once;
150       if (! warned_once)
151         {
152           warned_once = 1;
153           WARN ((0, 0, _("Generating negative octal headers")));
154         }
155       where[size - 1] = '\0';
156       to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
157                 where, size - 1);
158     }
159
160   /* Otherwise, output a substitute value if possible (with a
161      warning), and an error message if not.  */
162   else
163     {
164       uintmax_t maxval = (base256_allowed
165                           ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
166                           : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
167       char valbuf[UINTMAX_STRSIZE_BOUND + 1];
168       char maxbuf[UINTMAX_STRSIZE_BOUND];
169       char minbuf[UINTMAX_STRSIZE_BOUND + 1];
170       char const *minval_string;
171       char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
172       char const *value_string;
173
174       if (base256_allowed)
175         {
176           uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
177           char *p = STRINGIFY_BIGINT (m, minbuf + 1);
178           *--p = '-';
179           minval_string = p;
180         }
181       else
182         minval_string = "0";
183
184       if (negative)
185         {
186           char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
187           *--p = '-';
188           value_string = p;
189         }
190       else
191         value_string = STRINGIFY_BIGINT (value, valbuf);
192
193       if (substitute)
194         {
195           int negsub;
196           uintmax_t sub = substitute (&negsub) & maxval;
197           uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
198           char subbuf[UINTMAX_STRSIZE_BOUND + 1];
199           char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
200           if (negsub)
201             *--sub_string = '-';
202           WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
203                  value_string, type, minval_string, maxval_string,
204                  sub_string));
205           to_chars (negsub, s, valsize, 0, where, size, type);
206         }
207       else
208         ERROR ((0, 0, _("value %s out of %s range %s..%s"),
209                 value_string, type, minval_string, maxval_string));
210     }
211 }
212
213 static uintmax_t
214 gid_substitute (int *negative)
215 {
216   gid_t r;
217 #ifdef GID_NOBODY
218   r = GID_NOBODY;
219 #else
220   static gid_t gid_nobody;
221   if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
222     gid_nobody = -2;
223   r = gid_nobody;
224 #endif
225   *negative = r < 0;
226   return r;
227 }
228
229 void
230 gid_to_chars (gid_t v, char *p, size_t s)
231 {
232   to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
233 }
234
235 void
236 major_to_chars (major_t v, char *p, size_t s)
237 {
238   to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
239 }
240
241 void
242 minor_to_chars (minor_t v, char *p, size_t s)
243 {
244   to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
245 }
246
247 void
248 mode_to_chars (mode_t v, char *p, size_t s)
249 {
250   /* In the common case where the internal and external mode bits are the same,
251      and we are not using POSIX or GNU format,
252      propagate all unknown bits to the external mode.
253      This matches historical practice.
254      Otherwise, just copy the bits we know about.  */
255   int negative;
256   uintmax_t u;
257   if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
258       && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
259       && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
260       && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
261       && archive_format != POSIX_FORMAT
262       && archive_format != GNU_FORMAT)
263     {
264       negative = v < 0;
265       u = v;
266     }
267   else
268     {
269       negative = 0;
270       u = ((v & S_ISUID ? TSUID : 0)
271            | (v & S_ISGID ? TSGID : 0)
272            | (v & S_ISVTX ? TSVTX : 0)
273            | (v & S_IRUSR ? TUREAD : 0)
274            | (v & S_IWUSR ? TUWRITE : 0)
275            | (v & S_IXUSR ? TUEXEC : 0)
276            | (v & S_IRGRP ? TGREAD : 0)
277            | (v & S_IWGRP ? TGWRITE : 0)
278            | (v & S_IXGRP ? TGEXEC : 0)
279            | (v & S_IROTH ? TOREAD : 0)
280            | (v & S_IWOTH ? TOWRITE : 0)
281            | (v & S_IXOTH ? TOEXEC : 0));
282     }
283   to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
284 }
285
286 void
287 off_to_chars (off_t v, char *p, size_t s)
288 {
289   to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
290 }
291
292 void
293 size_to_chars (size_t v, char *p, size_t s)
294 {
295   to_chars (0, (uintmax_t) v, sizeof v, 0, p, s, "size_t");
296 }
297
298 void
299 time_to_chars (time_t v, char *p, size_t s)
300 {
301   to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
302 }
303
304 static uintmax_t
305 uid_substitute (int *negative)
306 {
307   uid_t r;
308 #ifdef UID_NOBODY
309   r = UID_NOBODY;
310 #else
311   static uid_t uid_nobody;
312   if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
313     uid_nobody = -2;
314   r = uid_nobody;
315 #endif
316   *negative = r < 0;
317   return r;
318 }
319
320 void
321 uid_to_chars (uid_t v, char *p, size_t s)
322 {
323   to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
324 }
325
326 void
327 uintmax_to_chars (uintmax_t v, char *p, size_t s)
328 {
329   to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
330 }
331 \f
332 /* Writing routines.  */
333
334 /* Zero out the buffer so we don't confuse ourselves with leftover
335    data.  */
336 static void
337 clear_buffer (char *buffer)
338 {
339   memset (buffer, 0, BLOCKSIZE);
340 }
341
342 /* Write the EOT block(s).  Zero at least two blocks, through the end
343    of the record.  Old tar, as previous versions of GNU tar, writes
344    garbage after two zeroed blocks.  */
345 void
346 write_eot (void)
347 {
348   union block *pointer = find_next_block ();
349   memset (pointer->buffer, 0, BLOCKSIZE);
350   set_next_block_after (pointer);
351   pointer = find_next_block ();
352   memset (pointer->buffer, 0, available_space_after (pointer));
353   set_next_block_after (pointer);
354 }
355
356 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block.  */
357
358 /* FIXME: Cross recursion between start_header and write_long!  */
359
360 static union block *start_header PARAMS ((const char *, struct stat *));
361
362 static void
363 write_long (const char *p, char type)
364 {
365   size_t size = strlen (p) + 1;
366   size_t bufsize;
367   union block *header;
368   struct stat foo;
369
370   memset (&foo, 0, sizeof foo);
371   foo.st_size = size;
372
373   header = start_header ("././@LongLink", &foo);
374   header->header.typeflag = type;
375   finish_header (header);
376
377   header = find_next_block ();
378
379   bufsize = available_space_after (header);
380
381   while (bufsize < size)
382     {
383       memcpy (header->buffer, p, bufsize);
384       p += bufsize;
385       size -= bufsize;
386       set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
387       header = find_next_block ();
388       bufsize = available_space_after (header);
389     }
390   memcpy (header->buffer, p, size);
391   memset (header->buffer + size, 0, bufsize - size);
392   set_next_block_after (header + (size - 1) / BLOCKSIZE);
393 }
394 \f
395 /* Return a suffix of the file NAME that is a relative file name.
396    Warn about `..' in file names.  But return NAME if the user wants
397    absolute file names.  */
398 static char const *
399 relativize (char const *name)
400 {
401   if (! absolute_names_option)
402     {
403       {
404         static int warned_once;
405         if (! warned_once && contains_dot_dot (name))
406           {
407             warned_once = 1;
408             WARN ((0, 0, _("Member names contain `..'")));
409           }
410       }
411
412       {
413         size_t prefix_len = FILESYSTEM_PREFIX_LEN (name);
414
415         while (ISSLASH (name[prefix_len]))
416           prefix_len++;
417
418         if (prefix_len)
419           {
420             static int warned_once;
421             if (!warned_once)
422               {
423                 warned_once = 1;
424                 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
425                        (int) prefix_len, name));
426               }
427             name += prefix_len;
428           }
429       }
430     }
431
432   return name;
433 }
434 \f
435 /* Header handling.  */
436
437 /* Make a header block for the file whose stat info is st,
438    and return its address.  */
439
440 static union block *
441 start_header (const char *name, struct stat *st)
442 {
443   union block *header;
444
445   name = relativize (name);
446
447   if (sizeof header->header.name <= strlen (name))
448     write_long (name, GNUTYPE_LONGNAME);
449   header = find_next_block ();
450   memset (header->buffer, 0, sizeof (union block));
451
452   assign_string (&current_file_name, name);
453
454   strncpy (header->header.name, name, NAME_FIELD_SIZE);
455   header->header.name[NAME_FIELD_SIZE - 1] = '\0';
456
457   /* Override some stat fields, if requested to do so.  */
458
459   if (owner_option != (uid_t) -1)
460     st->st_uid = owner_option;
461   if (group_option != (gid_t) -1)
462     st->st_gid = group_option;
463   if (mode_option)
464     st->st_mode = ((st->st_mode & ~MODE_ALL)
465                    | mode_adjust (st->st_mode, mode_option));
466
467   /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
468      for a few tars and came up with the following interoperability
469      matrix:
470
471               WRITER
472         1 2 3 4 5 6 7 8 9   READER
473         . . . . . . . . .   1 = SunOS 4.2 tar
474         # . . # # . . # #   2 = NEC SVR4.0.2 tar
475         . . . # # . . # .   3 = Solaris 2.1 tar
476         . . . . . . . . .   4 = GNU tar 1.11.1
477         . . . . . . . . .   5 = HP-UX 8.07 tar
478         . . . . . . . . .   6 = Ultrix 4.1
479         . . . . . . . . .   7 = AIX 3.2
480         . . . . . . . . .   8 = Hitachi HI-UX 1.03
481         . . . . . . . . .   9 = Omron UNIOS-B 4.3BSD 1.60Beta
482
483              . = works
484              # = ``impossible file type''
485
486      The following mask for old archive removes the `#'s in column 4
487      above, thus making GNU tar both a universal donor and a universal
488      acceptor for Paul's test.  */
489
490   if (archive_format == V7_FORMAT)
491     MODE_TO_CHARS (st->st_mode & MODE_ALL, header->header.mode);
492   else
493     MODE_TO_CHARS (st->st_mode, header->header.mode);
494
495   UID_TO_CHARS (st->st_uid, header->header.uid);
496   GID_TO_CHARS (st->st_gid, header->header.gid);
497   OFF_TO_CHARS (st->st_size, header->header.size);
498   TIME_TO_CHARS (st->st_mtime, header->header.mtime);
499
500   if (incremental_option)
501     if (archive_format == OLDGNU_FORMAT)
502       {
503         TIME_TO_CHARS (st->st_atime, header->oldgnu_header.atime);
504         TIME_TO_CHARS (st->st_ctime, header->oldgnu_header.ctime);
505       }
506
507   header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
508
509   switch (archive_format)
510     {
511     case V7_FORMAT:
512       break;
513
514     case OLDGNU_FORMAT:
515       /* Overwrite header->header.magic and header.version in one blow.  */
516       strcpy (header->header.magic, OLDGNU_MAGIC);
517       break;
518
519     case POSIX_FORMAT:
520     case GNU_FORMAT:
521       strncpy (header->header.magic, TMAGIC, TMAGLEN);
522       strncpy (header->header.version, TVERSION, TVERSLEN);
523       break;
524
525     default:
526       abort ();
527     }
528
529   if (archive_format == V7_FORMAT || numeric_owner_option)
530     {
531       /* header->header.[ug]name are left as the empty string.  */
532     }
533   else
534     {
535       uid_to_uname (st->st_uid, header->header.uname);
536       gid_to_gname (st->st_gid, header->header.gname);
537     }
538
539   return header;
540 }
541
542 /* Finish off a filled-in header block and write it out.  We also
543    print the file name and/or full info if verbose is on.  */
544 void
545 finish_header (union block *header)
546 {
547   size_t i;
548   int sum;
549   char *p;
550
551   memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
552
553   sum = 0;
554   p = header->buffer;
555   for (i = sizeof *header; i-- != 0; )
556     /* We can't use unsigned char here because of old compilers, e.g. V7.  */
557     sum += 0xFF & *p++;
558
559   /* Fill in the checksum field.  It's formatted differently from the
560      other fields: it has [6] digits, a null, then a space -- rather than
561      digits, then a null.  We use to_chars.
562      The final space is already there, from
563      checksumming, and to_chars doesn't modify it.
564
565      This is a fast way to do:
566
567      sprintf(header->header.chksum, "%6o", sum);  */
568
569   uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
570
571   if (verbose_option
572       && header->header.typeflag != GNUTYPE_LONGLINK
573       && header->header.typeflag != GNUTYPE_LONGNAME)
574     {
575       /* These globals are parameters to print_header, sigh.  */
576
577       current_header = header;
578       /* current_stat is already set up.  */
579       current_format = archive_format;
580       print_header ();
581     }
582
583   set_next_block_after (header);
584 }
585 \f
586 /* Sparse file processing.  */
587
588 /* Takes a blockful of data and basically cruises through it to see if
589    it's made *entirely* of zeros, returning a 0 the instant it finds
590    something that is a nonzero, i.e., useful data.  */
591 static int
592 zero_block_p (char *buffer)
593 {
594   int counter;
595
596   for (counter = 0; counter < BLOCKSIZE; counter++)
597     if (buffer[counter] != '\0')
598       return 0;
599   return 1;
600 }
601
602 static void
603 init_sparsearray (void)
604 {
605   sp_array_size = 10;
606
607   /* Make room for our scratch space -- initially is 10 elts long.  */
608
609   sparsearray = xmalloc (sp_array_size * sizeof (struct sp_array));
610 }
611
612 static off_t
613 find_new_file_size (int sparses)
614 {
615   int i;
616   off_t s = 0;
617   for (i = 0; i < sparses; i++)
618     s += sparsearray[i].numbytes;
619   return s;
620 }
621
622 /* Make one pass over the file NAME, studying where any non-zero data
623    is, that is, how far into the file each instance of data is, and
624    how many bytes are there.  Save this information in the
625    sparsearray, which will later be translated into header
626    information.  */
627
628 /* There is little point in trimming small amounts of null data at the head
629    and tail of blocks, only avoid dumping full null blocks.  */
630
631 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
632    too kludgey for my taste...  */
633
634 static int
635 deal_with_sparse (char *name, union block *header)
636 {
637   size_t numbytes = 0;
638   off_t offset = 0;
639   int file;
640   int sparses = 0;
641   ssize_t count;
642   char buffer[BLOCKSIZE];
643
644   if (archive_format == OLDGNU_FORMAT)
645     header->oldgnu_header.isextended = 0;
646
647   if (file = open (name, O_RDONLY), file < 0)
648     /* This problem will be caught later on, so just return.  */
649     return 0;
650
651   init_sparsearray ();
652   clear_buffer (buffer);
653
654   for (;;)
655     {
656       /* Realloc the scratch area as necessary.  FIXME: should reallocate
657          only at beginning of a new instance of non-zero data.  */
658
659       if (sp_array_size <= sparses)
660         {
661           sparsearray =
662             xrealloc (sparsearray,
663                       2 * sp_array_size * sizeof (struct sp_array));
664           sp_array_size *= 2;
665         }
666       
667       count = safe_read (file, buffer, sizeof buffer);
668       if (count <= 0)
669         break;
670
671       /* Process one block.  */
672
673       if (count == sizeof buffer)
674
675         if (zero_block_p (buffer))
676           {
677             if (numbytes)
678               {
679                 sparsearray[sparses++].numbytes = numbytes;
680                 numbytes = 0;
681               }
682           }
683         else
684           {
685             if (!numbytes)
686               sparsearray[sparses].offset = offset;
687             numbytes += count;
688           }
689
690       else
691
692         /* Since count < sizeof buffer, we have the last bit of the file.  */
693
694         if (!zero_block_p (buffer))
695           {
696             if (!numbytes)
697               sparsearray[sparses].offset = offset;
698             numbytes += count;
699           }
700         else
701           /* The next two lines are suggested by Andreas Degert, who says
702              they are required for trailing full blocks to be written to the
703              archive, when all zeroed.  Yet, it seems to me that the case
704              does not apply.  Further, at restore time, the file is not as
705              sparse as it should.  So, some serious cleanup is *also* needed
706              in this area.  Just one more... :-(.  FIXME.  */
707           if (numbytes)
708             numbytes += count;
709
710       /* Prepare for next block.  */
711
712       offset += count;
713       /* FIXME: do not clear unless necessary.  */
714       clear_buffer (buffer);
715     }
716
717   if (numbytes)
718     sparsearray[sparses++].numbytes = numbytes;
719   else
720     {
721       sparsearray[sparses].offset = offset - 1;
722       sparsearray[sparses++].numbytes = 1;
723     }
724
725   return close (file) == 0 && 0 <= count ? sparses : 0;
726 }
727
728 static int
729 finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
730 {
731   union block *start;
732   size_t bufsize;
733   int sparses = 0;
734   ssize_t count;
735
736   while (*sizeleft > 0)
737     {
738       start = find_next_block ();
739       memset (start->buffer, 0, BLOCKSIZE);
740       bufsize = sparsearray[sparses].numbytes;
741       if (! bufsize)
742         abort ();
743
744       if (lseek (file, sparsearray[sparses++].offset, SEEK_SET) < 0)
745         {
746           (ignore_failed_read_option ? seek_warn_details : seek_error_details)
747             (name, sparsearray[sparses - 1].offset);
748           break;
749         }
750
751       /* If the number of bytes to be written here exceeds the size of
752          the temporary buffer, do it in steps.  */
753
754       while (bufsize > BLOCKSIZE)
755         {
756           count = safe_read (file, start->buffer, BLOCKSIZE);
757           if (count < 0)
758             {
759               (ignore_failed_read_option
760                ? read_warn_details
761                : read_error_details)
762                 (name, fullsize - *sizeleft, bufsize);
763               return 1;
764             }
765           bufsize -= count;
766           *sizeleft -= count;
767           set_next_block_after (start);
768           start = find_next_block ();
769           memset (start->buffer, 0, BLOCKSIZE);
770         }
771
772       {
773         char buffer[BLOCKSIZE];
774
775         clear_buffer (buffer);
776         count = safe_read (file, buffer, bufsize);
777         memcpy (start->buffer, buffer, BLOCKSIZE);
778       }
779
780       if (count < 0)
781         {
782           (ignore_failed_read_option
783            ? read_warn_details
784            : read_error_details)
785             (name, fullsize - *sizeleft, bufsize);
786           return 1;
787         }
788
789       *sizeleft -= count;
790       set_next_block_after (start);
791     }
792   free (sparsearray);
793 #if 0
794   set_next_block_after (start + (count - 1) / BLOCKSIZE);
795 #endif
796   return 0;
797 }
798 \f
799 /* Main functions of this module.  */
800
801 void
802 create_archive (void)
803 {
804   char *p;
805
806   open_archive (ACCESS_WRITE);
807
808   if (incremental_option)
809     {
810       size_t buffer_size = 1000;
811       char *buffer = xmalloc (buffer_size);
812       const char *q;
813
814       collect_and_sort_names ();
815
816       while (p = name_from_list (), p)
817         if (!excluded_name (p))
818           dump_file (p, -1, (dev_t) 0);
819
820       blank_name_list ();
821       while (p = name_from_list (), p)
822         if (!excluded_name (p))
823           {
824             size_t plen = strlen (p);
825             if (buffer_size <= plen)
826               {
827                 while ((buffer_size *= 2) <= plen)
828                   continue;
829                 buffer = xrealloc (buffer, buffer_size);
830               }
831             memcpy (buffer, p, plen);
832             if (! ISSLASH (buffer[plen - 1]))
833               buffer[plen++] = '/';
834             q = gnu_list_name->dir_contents;
835             if (q)
836               while (*q)
837                 {
838                   size_t qlen = strlen (q);
839                   if (*q == 'Y')
840                     {
841                       if (buffer_size < plen + qlen)
842                         {
843                           while ((buffer_size *=2 ) < plen + qlen)
844                             continue;
845                           buffer = xrealloc (buffer, buffer_size);
846                         }
847                       strcpy (buffer + plen, q + 1);
848                       dump_file (buffer, -1, (dev_t) 0);
849                     }
850                   q += qlen + 1;
851                 }
852           }
853       free (buffer);
854     }
855   else
856     {
857       while (p = name_next (1), p)
858         if (!excluded_name (p))
859           dump_file (p, 1, (dev_t) 0);
860     }
861
862   write_eot ();
863   close_archive ();
864
865   if (listed_incremental_option)
866     write_directory_file ();
867 }
868
869
870 /* Calculate the hash of a link.  */
871 static unsigned
872 hash_link (void const *entry, unsigned n_buckets)
873 {
874   struct link const *link = entry;
875   return (uintmax_t) (link->dev ^ link->ino) % n_buckets;
876 }
877
878 /* Compare two links for equality.  */
879 static bool
880 compare_links (void const *entry1, void const *entry2)
881 {
882   struct link const *link1 = entry1;
883   struct link const *link2 = entry2;
884   return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
885 }
886
887 /* Dump a single file, recursing on directories.  P is the file name
888    to dump.  TOP_LEVEL tells whether this is a top-level call; zero
889    means no, positive means yes, and negative means an incremental
890    dump.  PARENT_DEVICE is the device of P's
891    parent directory; it is examined only if TOP_LEVEL is zero.
892
893    Set global CURRENT_STAT to stat output for this file.  */
894
895 /* FIXME: One should make sure that for *every* path leading to setting
896    exit_status to failure, a clear diagnostic has been issued.  */
897
898 void
899 dump_file (char *p, int top_level, dev_t parent_device)
900 {
901   union block *header;
902   char type;
903   union block *exhdr;
904   char save_typeflag;
905   time_t original_ctime;
906   struct utimbuf restore_times;
907
908   /* FIXME: `header' might be used uninitialized in this
909      function.  Reported by Bruno Haible.  */
910
911   if (interactive_option && !confirm ("add", p))
912     return;
913
914   if (deref_stat (dereference_option, p, &current_stat) != 0)
915     {
916       if (ignore_failed_read_option)
917         stat_warn (p);
918       else
919         stat_error (p);
920       return;
921     }
922
923   original_ctime = current_stat.st_ctime;
924   restore_times.actime = current_stat.st_atime;
925   restore_times.modtime = current_stat.st_mtime;
926
927 #ifdef S_ISHIDDEN
928   if (S_ISHIDDEN (current_stat.st_mode))
929     {
930       char *new = (char *) alloca (strlen (p) + 2);
931       if (new)
932         {
933           strcpy (new, p);
934           strcat (new, "@");
935           p = new;
936         }
937     }
938 #endif
939
940   /* See if we want only new files, and check if this one is too old to
941      put in the archive.  */
942
943   if ((0 < top_level || !incremental_option)
944       && !S_ISDIR (current_stat.st_mode)
945       && current_stat.st_mtime < newer_mtime_option
946       && (!after_date_option || current_stat.st_ctime < newer_ctime_option))
947     {
948       if (0 < top_level)
949         WARN ((0, 0, _("%s: file is unchanged; not dumped"),
950                quotearg_colon (p)));
951       /* FIXME: recheck this return.  */
952       return;
953     }
954
955 #if !MSDOS
956   /* See if we are trying to dump the archive.  */
957
958   if (ar_dev && current_stat.st_dev == ar_dev && current_stat.st_ino == ar_ino)
959     {
960       WARN ((0, 0, _("%s: file is the archive; not dumped"),
961              quotearg_colon (p)));
962       return;
963     }
964 #endif
965
966   if (S_ISDIR (current_stat.st_mode))
967     {
968       char *directory;
969       char const *entry;
970       size_t entrylen;
971       char *namebuf;
972       size_t buflen;
973       size_t len;
974       dev_t our_device = current_stat.st_dev;
975
976       errno = 0;
977
978       directory = savedir (p);
979       if (! directory)
980         {
981           if (ignore_failed_read_option)
982             savedir_warn (p);
983           else
984             savedir_error (p);
985           return;
986         }
987
988       /* Build new prototype name.  Ensure exactly one trailing slash.  */
989
990       len = strlen (p);
991       buflen = len + NAME_FIELD_SIZE;
992       namebuf = xmalloc (buflen + 1);
993       memcpy (namebuf, p, len);
994       while (len >= 1 && ISSLASH (namebuf[len - 1]))
995         len--;
996       namebuf[len++] = '/';
997       namebuf[len] = '\0';
998
999       if (! is_avoided_name (namebuf))
1000         {
1001           /* The condition above used to be "archive_format != V7_FORMAT".
1002              GNU tar was not writing directory blocks at all.  Daniel Trinkle
1003              writes: ``All old versions of tar I have ever seen have
1004              correctly archived an empty directory.  The really old ones I
1005              checked included HP-UX 7 and Mt. Xinu More/BSD.  There may be
1006              some subtle reason for the exclusion that I don't know, but the
1007              current behavior is broken.''  I do not know those subtle
1008              reasons either, so until these are reported (anew?), just allow
1009              directory blocks to be written even with old archives.  */
1010
1011           current_stat.st_size = 0;     /* force 0 size on dir */
1012
1013           /* FIXME: If people could really read standard archives, this
1014              should be:
1015
1016              header
1017                = start_header (standard_option ? p : namebuf, &current_stat);
1018
1019              but since they'd interpret DIRTYPE blocks as regular
1020              files, we'd better put the / on the name.  */
1021
1022           header = start_header (namebuf, &current_stat);
1023
1024           if (incremental_option)
1025             header->header.typeflag = GNUTYPE_DUMPDIR;
1026           else /* if (standard_option) */
1027             header->header.typeflag = DIRTYPE;
1028
1029           /* If we're gnudumping, we aren't done yet so don't close it.  */
1030
1031           if (!incremental_option)
1032             finish_header (header);     /* done with directory header */
1033         }
1034
1035       if (incremental_option && gnu_list_name->dir_contents)
1036         {
1037           off_t sizeleft;
1038           off_t totsize;
1039           size_t bufsize;
1040           union block *start;
1041           ssize_t count;
1042           const char *buffer, *p_buffer;
1043
1044           buffer = gnu_list_name->dir_contents; /* FOO */
1045           totsize = 0;
1046           for (p_buffer = buffer; p_buffer && *p_buffer;)
1047             {
1048               size_t tmp;
1049
1050               tmp = strlen (p_buffer) + 1;
1051               totsize += tmp;
1052               p_buffer += tmp;
1053             }
1054           totsize++;
1055           OFF_TO_CHARS (totsize, header->header.size);
1056           finish_header (header);
1057           p_buffer = buffer;
1058           sizeleft = totsize;
1059           while (sizeleft > 0)
1060             {
1061               if (multi_volume_option)
1062                 {
1063                   assign_string (&save_name, p);
1064                   save_sizeleft = sizeleft;
1065                   save_totsize = totsize;
1066                 }
1067               start = find_next_block ();
1068               bufsize = available_space_after (start);
1069               if (sizeleft < bufsize)
1070                 {
1071                   bufsize = sizeleft;
1072                   count = bufsize % BLOCKSIZE;
1073                   if (count)
1074                     memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
1075                 }
1076               memcpy (start->buffer, p_buffer, bufsize);
1077               sizeleft -= bufsize;
1078               p_buffer += bufsize;
1079               set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1080             }
1081           if (multi_volume_option)
1082             assign_string (&save_name, 0);
1083           goto finish_dir;
1084         }
1085
1086       /* See if we are about to recurse into a directory, and avoid doing
1087          so if the user wants that we do not descend into directories.  */
1088
1089       if (! recursion_option)
1090         goto finish_dir;
1091
1092       /* See if we are crossing from one file system to another, and
1093          avoid doing so if the user only wants to dump one file system.  */
1094
1095       if (one_file_system_option && !top_level
1096           && parent_device != current_stat.st_dev)
1097         {
1098           if (verbose_option)
1099             WARN ((0, 0,
1100                    _("%s: file is on a different filesystem; not dumped"),
1101                    quotearg_colon (p)));
1102           goto finish_dir;
1103         }
1104
1105       /* Now output all the files in the directory.  */
1106
1107       /* FIXME: Should speed this up by cd-ing into the dir.  */
1108
1109       for (entry = directory;
1110            (entrylen = strlen (entry)) != 0;
1111            entry += entrylen + 1)
1112         {
1113           if (buflen <= len + entrylen)
1114             {
1115               buflen = len + entrylen;
1116               namebuf = xrealloc (namebuf, buflen + 1);
1117             }
1118           strcpy (namebuf + len, entry);
1119           if (!excluded_name (namebuf))
1120             dump_file (namebuf, 0, our_device);
1121         }
1122
1123     finish_dir:
1124
1125       free (directory);
1126       free (namebuf);
1127       if (atime_preserve_option)
1128         utime (p, &restore_times);
1129       return;
1130     }
1131   else if (is_avoided_name (p))
1132     return;
1133   else
1134     {
1135       /* Check for multiple links.
1136
1137          We maintain a table of all such files that we've written so
1138          far.  Any time we see another, we check the table and avoid
1139          dumping the data again if we've done it once already.  */
1140
1141       if (1 < current_stat.st_nlink)
1142         {
1143           static Hash_table *link_table;
1144           struct link *lp = xmalloc (offsetof (struct link, name)
1145                                      + strlen (p) + 1);
1146           struct link *dup;
1147           lp->ino = current_stat.st_ino;
1148           lp->dev = current_stat.st_dev;
1149           strcpy (lp->name, p);
1150
1151           if (! ((link_table
1152                   || (link_table = hash_initialize (0, 0, hash_link,
1153                                                     compare_links, 0)))
1154                  && (dup = hash_insert (link_table, lp))))
1155             xalloc_die ();
1156
1157           if (dup != lp)
1158             {
1159               /* We found a link.  */
1160               char const *link_name = relativize (dup->name);
1161
1162               free (lp);
1163
1164               if (NAME_FIELD_SIZE <= strlen (link_name))
1165                 write_long (link_name, GNUTYPE_LONGLINK);
1166               assign_string (&current_link_name, link_name);
1167
1168               current_stat.st_size = 0;
1169               header = start_header (p, &current_stat);
1170               strncpy (header->header.linkname, link_name, NAME_FIELD_SIZE);
1171
1172               /* Force null termination.  */
1173               header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
1174
1175               header->header.typeflag = LNKTYPE;
1176               finish_header (header);
1177
1178               /* FIXME: Maybe remove from table after all links found?  */
1179
1180               if (remove_files_option && unlink (p) != 0)
1181                 unlink_error (p);
1182
1183               /* We dumped it.  */
1184               return;
1185             }
1186         }
1187
1188       /* This is not a link to a previously dumped file, so dump it.  */
1189
1190       if (S_ISREG (current_stat.st_mode)
1191           || S_ISCTG (current_stat.st_mode))
1192         {
1193           int f;                        /* file descriptor */
1194           size_t bufsize;
1195           ssize_t count;
1196           off_t sizeleft;
1197           union block *start;
1198           int header_moved;
1199           char isextended = 0;
1200           int sparses = 0;
1201
1202           header_moved = 0;
1203
1204           if (sparse_option)
1205             {
1206               /* Check the size of the file against the number of blocks
1207                  allocated for it, counting both data and indirect blocks.
1208                  If there is a smaller number of blocks that would be
1209                  necessary to accommodate a file of this size, this is safe
1210                  to say that we have a sparse file: at least one of those
1211                  blocks in the file is just a useless hole.  For sparse
1212                  files not having more hole blocks than indirect blocks, the
1213                  sparseness will go undetected.  */
1214
1215               /* Bruno Haible sent me these statistics for Linux.  It seems
1216                  that some filesystems count indirect blocks in st_blocks,
1217                  while others do not seem to:
1218
1219                  minix-fs   tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1220                  extfs      tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1221                  ext2fs     tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1222                  msdos-fs   tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1223
1224                  Dick Streefland reports the previous numbers as misleading,
1225                  because ext2fs use 12 direct blocks, while minix-fs uses only
1226                  6 direct blocks.  Dick gets:
1227
1228                  ext2   size=20480      ls listed blocks=21
1229                  minix  size=20480      ls listed blocks=21
1230                  msdos  size=20480      ls listed blocks=20
1231
1232                  It seems that indirect blocks *are* included in st_blocks.
1233                  The minix filesystem does not account for phantom blocks in
1234                  st_blocks, so `du' and `ls -s' give wrong results.  So, the
1235                  --sparse option would not work on a minix filesystem.  */
1236
1237               if (ST_NBLOCKS (current_stat)
1238                   < (current_stat.st_size / ST_NBLOCKSIZE
1239                      + (current_stat.st_size % ST_NBLOCKSIZE != 0)))
1240                 {
1241                   int counter;
1242
1243                   header = start_header (p, &current_stat);
1244                   header->header.typeflag = GNUTYPE_SPARSE;
1245                   header_moved = 1;
1246
1247                   /* Call the routine that figures out the layout of the
1248                      sparse file in question.  SPARSES is the index of the
1249                      first unused element of the "sparsearray," i.e.,
1250                      the number of elements it needed to describe the file.  */
1251
1252                   sparses = deal_with_sparse (p, header);
1253
1254                   /* See if we'll need an extended header later.  */
1255
1256                   if (SPARSES_IN_OLDGNU_HEADER < sparses)
1257                     header->oldgnu_header.isextended = 1;
1258
1259                   /* We store the "real" file size so we can show that in
1260                      case someone wants to list the archive, i.e., tar tvf
1261                      <file>.  It might be kind of disconcerting if the
1262                      shrunken file size was the one that showed up.  */
1263
1264                   OFF_TO_CHARS (current_stat.st_size,
1265                                 header->oldgnu_header.realsize);
1266
1267                   /* This will be the new "size" of the file, i.e., the size
1268                      of the file minus the blocks of holes that we're
1269                      skipping over.  */
1270
1271                   current_stat.st_size = find_new_file_size (sparses);
1272                   OFF_TO_CHARS (current_stat.st_size, header->header.size);
1273
1274                   for (counter = 0;
1275                        counter < sparses && counter < SPARSES_IN_OLDGNU_HEADER;
1276                        counter++)
1277                     {
1278                       OFF_TO_CHARS (sparsearray[counter].offset,
1279                                     header->oldgnu_header.sp[counter].offset);
1280                       SIZE_TO_CHARS (sparsearray[counter].numbytes,
1281                                      header->oldgnu_header.sp[counter].numbytes);
1282                     }
1283                 }
1284             }
1285
1286           sizeleft = current_stat.st_size;
1287
1288           /* Don't bother opening empty, world readable files.  Also do not open
1289              files when archive is meant for /dev/null.  */
1290
1291           if (dev_null_output
1292               || (sizeleft == 0
1293                   && MODE_R == (MODE_R & current_stat.st_mode)))
1294             f = -1;
1295           else
1296             {
1297               f = open (p, O_RDONLY | O_BINARY);
1298               if (f < 0)
1299                 {
1300                   if (! top_level && errno == ENOENT)
1301                     WARN ((0, 0, _("%s: File removed before we read it"),
1302                            quotearg_colon (p)));
1303                   else
1304                     (ignore_failed_read_option ? open_warn : open_error) (p);
1305                   return;
1306                 }
1307             }
1308
1309           /* If the file is sparse, we've already taken care of this.  */
1310
1311           if (!header_moved)
1312             header = start_header (p, &current_stat);
1313
1314           /* Mark contiguous files, if we support them.  */
1315
1316           if (archive_format != V7_FORMAT && S_ISCTG (current_stat.st_mode))
1317             header->header.typeflag = CONTTYPE;
1318
1319           isextended = header->oldgnu_header.isextended;
1320           save_typeflag = header->header.typeflag;
1321           finish_header (header);
1322           if (isextended)
1323             {
1324               int sparses_emitted = SPARSES_IN_OLDGNU_HEADER;
1325
1326               for (;;)
1327                 {
1328                   int i;
1329                   exhdr = find_next_block ();
1330                   memset (exhdr->buffer, 0, BLOCKSIZE);
1331                   for (i = 0;
1332                        (i < SPARSES_IN_SPARSE_HEADER
1333                         && sparses_emitted + i < sparses);
1334                        i++)
1335                     {
1336                       SIZE_TO_CHARS (sparsearray[sparses_emitted + i].numbytes,
1337                                      exhdr->sparse_header.sp[i].numbytes);
1338                       OFF_TO_CHARS (sparsearray[sparses_emitted + i].offset,
1339                                     exhdr->sparse_header.sp[i].offset);
1340                     }
1341                   set_next_block_after (exhdr);
1342                   sparses_emitted += i;
1343                   if (sparses == sparses_emitted)
1344                     break;
1345                   exhdr->sparse_header.isextended = 1;
1346                 }
1347             }
1348           if (save_typeflag == GNUTYPE_SPARSE)
1349             {
1350               if (f < 0
1351                   || finish_sparse_file (f, &sizeleft,
1352                                          current_stat.st_size, p))
1353                 goto padit;
1354             }
1355           else
1356             while (sizeleft > 0)
1357               {
1358                 if (multi_volume_option)
1359                   {
1360                     assign_string (&save_name, p);
1361                     save_sizeleft = sizeleft;
1362                     save_totsize = current_stat.st_size;
1363                   }
1364                 start = find_next_block ();
1365
1366                 bufsize = available_space_after (start);
1367
1368                 if (sizeleft < bufsize)
1369                   {
1370                     /* Last read -- zero out area beyond.  */
1371
1372                     bufsize = sizeleft;
1373                     count = bufsize % BLOCKSIZE;
1374                     if (count)
1375                       memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
1376                   }
1377                 if (f < 0)
1378                   count = bufsize;
1379                 else
1380                   count = safe_read (f, start->buffer, bufsize);
1381                 if (count < 0)
1382                   {
1383                     (ignore_failed_read_option
1384                      ? read_warn_details
1385                      : read_error_details)
1386                       (p, current_stat.st_size - sizeleft, bufsize);
1387                     goto padit;
1388                   }
1389                 sizeleft -= bufsize;
1390
1391                 /* This is nonportable (the type of set_next_block_after's arg).  */
1392
1393                 set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1394
1395
1396                 if (count != bufsize)
1397                   {
1398                     char buf[UINTMAX_STRSIZE_BOUND];
1399                     memset (start->buffer + count, 0, bufsize - count);
1400                     WARN ((0, 0,
1401                            _("%s: File shrank by %s bytes; padding with zeros"),
1402                            quotearg_colon (p),
1403                            STRINGIFY_BIGINT (sizeleft, buf)));
1404                     if (! ignore_failed_read_option)
1405                       exit_status = TAREXIT_FAILURE;
1406                     goto padit;         /* short read */
1407                   }
1408               }
1409
1410           if (multi_volume_option)
1411             assign_string (&save_name, 0);
1412
1413           if (f >= 0)
1414             {
1415               struct stat final_stat;
1416               if (fstat (f, &final_stat) != 0)
1417                 {
1418                   if (ignore_failed_read_option)
1419                     stat_warn (p);
1420                   else
1421                     stat_error (p);
1422                 }
1423               else if (final_stat.st_ctime != original_ctime)
1424                 {
1425                   char const *qp = quotearg_colon (p);
1426                   WARN ((0, 0, _("%s: file changed as we read it"), qp));
1427                   if (! ignore_failed_read_option)
1428                     exit_status = TAREXIT_FAILURE;
1429                 }
1430               if (close (f) != 0)
1431                 {
1432                   if (ignore_failed_read_option)
1433                     close_warn (p);
1434                   else
1435                     close_error (p);
1436                 }
1437               if (atime_preserve_option)
1438                 utime (p, &restore_times);
1439             }
1440           if (remove_files_option)
1441             {
1442               if (unlink (p) == -1)
1443                 unlink_error (p);
1444             }
1445           return;
1446
1447           /* File shrunk or gave error, pad out tape to match the size we
1448              specified in the header.  */
1449
1450         padit:
1451           while (sizeleft > 0)
1452             {
1453               save_sizeleft = sizeleft;
1454               start = find_next_block ();
1455               memset (start->buffer, 0, BLOCKSIZE);
1456               set_next_block_after (start);
1457               sizeleft -= BLOCKSIZE;
1458             }
1459           if (multi_volume_option)
1460             assign_string (&save_name, 0);
1461           if (f >= 0)
1462             {
1463               close (f);
1464               if (atime_preserve_option)
1465                 utime (p, &restore_times);
1466             }
1467           return;
1468         }
1469 #ifdef HAVE_READLINK
1470       else if (S_ISLNK (current_stat.st_mode))
1471         {
1472           char *buffer;
1473           int size;
1474           size_t linklen = current_stat.st_size;
1475           if (linklen != current_stat.st_size || linklen + 1 == 0)
1476             xalloc_die ();
1477           buffer = (char *) alloca (linklen + 1);
1478           size = readlink (p, buffer, linklen);
1479           if (size < 0)
1480             {
1481               if (ignore_failed_read_option)
1482                 readlink_warn (p);
1483               else
1484                 readlink_error (p);
1485               return;
1486             }
1487           buffer[size] = '\0';
1488           if (size >= NAME_FIELD_SIZE)
1489             write_long (buffer, GNUTYPE_LONGLINK);
1490           assign_string (&current_link_name, buffer);
1491
1492           current_stat.st_size = 0;     /* force 0 size on symlink */
1493           header = start_header (p, &current_stat);
1494           strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
1495           header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
1496           header->header.typeflag = SYMTYPE;
1497           finish_header (header);       /* nothing more to do to it */
1498           if (remove_files_option)
1499             {
1500               if (unlink (p) == -1)
1501                 unlink_error (p);
1502             }
1503           return;
1504         }
1505 #endif
1506       else if (S_ISCHR (current_stat.st_mode))
1507         type = CHRTYPE;
1508       else if (S_ISBLK (current_stat.st_mode))
1509         type = BLKTYPE;
1510       else if (S_ISFIFO (current_stat.st_mode))
1511         type = FIFOTYPE;
1512       else if (S_ISSOCK (current_stat.st_mode))
1513         {
1514           WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p)));
1515           return;
1516         }
1517       else if (S_ISDOOR (current_stat.st_mode))
1518         {
1519           WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p)));
1520           return;
1521         }
1522       else
1523         goto unknown;
1524     }
1525
1526   if (archive_format == V7_FORMAT)
1527     goto unknown;
1528
1529   current_stat.st_size = 0;     /* force 0 size */
1530   header = start_header (p, &current_stat);
1531   header->header.typeflag = type;
1532
1533   if (type != FIFOTYPE)
1534     {
1535       MAJOR_TO_CHARS (major (current_stat.st_rdev), header->header.devmajor);
1536       MINOR_TO_CHARS (minor (current_stat.st_rdev), header->header.devminor);
1537     }
1538
1539   finish_header (header);
1540   if (remove_files_option)
1541     {
1542       if (unlink (p) == -1)
1543         unlink_error (p);
1544     }
1545   return;
1546
1547 unknown:
1548   WARN ((0, 0, _("%s: Unknown file type; file ignored"),
1549          quotearg_colon (p)));
1550   if (! ignore_failed_read_option)
1551     exit_status = TAREXIT_FAILURE;
1552 }