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