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