Import upstream version 1.28
[debian/tar] / src / create.c
1 /* Create a tar archive.
2
3    Copyright 1985, 1992-1994, 1996-1997, 1999-2001, 2003-2007,
4    2009-2010, 2012-2014 Free Software Foundation, Inc.
5
6    This file is part of GNU tar.
7
8    GNU tar is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    GNU tar is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21    Written by John Gilmore, on 1985-08-25.  */
22
23 #include <system.h>
24
25 #include <quotearg.h>
26
27 #include "common.h"
28 #include <hash.h>
29
30 /* Error number to use when an impostor is discovered.
31    Pretend the impostor isn't there.  */
32 enum { IMPOSTOR_ERRNO = ENOENT };
33
34 struct link
35   {
36     dev_t dev;
37     ino_t ino;
38     nlink_t nlink;
39     char name[1];
40   };
41
42 struct exclusion_tag
43 {
44   const char *name;
45   size_t length;
46   enum exclusion_tag_type type;
47   bool (*predicate) (int fd);
48   struct exclusion_tag *next;
49 };
50
51 static struct exclusion_tag *exclusion_tags;
52
53 void
54 add_exclusion_tag (const char *name, enum exclusion_tag_type type,
55                    bool (*predicate) (int fd))
56 {
57   struct exclusion_tag *tag = xmalloc (sizeof tag[0]);
58   tag->next = exclusion_tags;
59   tag->name = name;
60   tag->type = type;
61   tag->predicate = predicate;
62   tag->length = strlen (name);
63   exclusion_tags = tag;
64 }
65
66 void
67 exclusion_tag_warning (const char *dirname, const char *tagname,
68                        const char *message)
69 {
70   if (verbose_option)
71     WARNOPT (WARN_CACHEDIR,
72              (0, 0,
73               _("%s: contains a cache directory tag %s; %s"),
74               quotearg_colon (dirname),
75               quotearg_n (1, tagname),
76               message));
77 }
78
79 enum exclusion_tag_type
80 check_exclusion_tags (struct tar_stat_info const *st, char const **tag_file_name)
81 {
82   struct exclusion_tag *tag;
83
84   for (tag = exclusion_tags; tag; tag = tag->next)
85     {
86       int tagfd = subfile_open (st, tag->name, open_read_flags);
87       if (0 <= tagfd)
88         {
89           bool satisfied = !tag->predicate || tag->predicate (tagfd);
90           close (tagfd);
91           if (satisfied)
92             {
93               if (tag_file_name)
94                 *tag_file_name = tag->name;
95               return tag->type;
96             }
97         }
98     }
99
100   return exclusion_tag_none;
101 }
102
103 /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
104    contains a valid header, as described at:
105         http://www.brynosaurus.com/cachedir
106    Applications can write this file into directories they create
107    for use as caches containing purely regenerable, non-precious data,
108    allowing us to avoid archiving them if --exclude-caches is specified. */
109
110 #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
111 #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
112
113 bool
114 cachedir_file_p (int fd)
115 {
116   char tagbuf[CACHEDIR_SIGNATURE_SIZE];
117
118   return
119     (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE) == CACHEDIR_SIGNATURE_SIZE
120      && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0);
121 }
122
123 \f
124 /* The maximum uintmax_t value that can be represented with DIGITS digits,
125    assuming that each digit is BITS_PER_DIGIT wide.  */
126 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
127    ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
128     ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
129     : (uintmax_t) -1)
130
131 /* The maximum uintmax_t value that can be represented with octal
132    digits and a trailing NUL in BUFFER.  */
133 #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
134
135 /* Convert VALUE to an octal representation suitable for tar headers.
136    Output to buffer WHERE with size SIZE.
137    The result is undefined if SIZE is 0 or if VALUE is too large to fit.  */
138
139 static void
140 to_octal (uintmax_t value, char *where, size_t size)
141 {
142   uintmax_t v = value;
143   size_t i = size;
144
145   do
146     {
147       where[--i] = '0' + (v & ((1 << LG_8) - 1));
148       v >>= LG_8;
149     }
150   while (i);
151 }
152
153 /* Copy at most LEN bytes from the string SRC to DST.  Terminate with
154    NUL unless SRC is LEN or more bytes long.  */
155
156 static void
157 tar_copy_str (char *dst, const char *src, size_t len)
158 {
159   size_t i;
160   for (i = 0; i < len; i++)
161     if (! (dst[i] = src[i]))
162       break;
163 }
164
165 /* Same as tar_copy_str, but always terminate with NUL if using
166    is OLDGNU format */
167
168 static void
169 tar_name_copy_str (char *dst, const char *src, size_t len)
170 {
171   tar_copy_str (dst, src, len);
172   if (archive_format == OLDGNU_FORMAT)
173     dst[len-1] = 0;
174 }
175
176 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
177    tar headers.  NEGATIVE is 1 if VALUE was negative before being cast
178    to uintmax_t, 0 otherwise.  Output to buffer WHERE with size SIZE.
179    The result is undefined if SIZE is 0 or if VALUE is too large to
180    fit.  */
181
182 static void
183 to_base256 (int negative, uintmax_t value, char *where, size_t size)
184 {
185   uintmax_t v = value;
186   uintmax_t propagated_sign_bits =
187     ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
188   size_t i = size;
189
190   do
191     {
192       where[--i] = v & ((1 << LG_256) - 1);
193       v = propagated_sign_bits | (v >> LG_256);
194     }
195   while (i);
196 }
197
198 #define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
199 #define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
200 #define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
201 #define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
202 #define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
203
204 #define UNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
205 #define GNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
206
207 static bool
208 to_chars (int negative, uintmax_t value, size_t valsize,
209           uintmax_t (*substitute) (int *),
210           char *where, size_t size, const char *type);
211
212 static bool
213 to_chars_subst (int negative, int gnu_format, uintmax_t value, size_t valsize,
214                 uintmax_t (*substitute) (int *),
215                 char *where, size_t size, const char *type)
216 {
217   uintmax_t maxval = (gnu_format
218                       ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
219                       : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
220   char valbuf[UINTMAX_STRSIZE_BOUND + 1];
221   char maxbuf[UINTMAX_STRSIZE_BOUND];
222   char minbuf[UINTMAX_STRSIZE_BOUND + 1];
223   char const *minval_string;
224   char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
225   char const *value_string;
226
227   if (gnu_format)
228     {
229       uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
230       char *p = STRINGIFY_BIGINT (m, minbuf + 1);
231       *--p = '-';
232       minval_string = p;
233     }
234   else
235     minval_string = "0";
236
237   if (negative)
238     {
239       char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
240       *--p = '-';
241       value_string = p;
242     }
243   else
244     value_string = STRINGIFY_BIGINT (value, valbuf);
245
246   if (substitute)
247     {
248       int negsub;
249       uintmax_t sub = substitute (&negsub) & maxval;
250       /* NOTE: This is one of the few places where GNU_FORMAT differs from
251          OLDGNU_FORMAT.  The actual differences are:
252
253          1. In OLDGNU_FORMAT all strings in a tar header end in \0
254          2. Incremental archives use oldgnu_header.
255
256          Apart from this they are completely identical. */
257       uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
258       char subbuf[UINTMAX_STRSIZE_BOUND + 1];
259       char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
260       if (negsub)
261         *--sub_string = '-';
262       WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
263              value_string, type, minval_string, maxval_string,
264              sub_string));
265       return to_chars (negsub, s, valsize, 0, where, size, type);
266     }
267   else
268     ERROR ((0, 0, _("value %s out of %s range %s..%s"),
269             value_string, type, minval_string, maxval_string));
270   return false;
271 }
272
273 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
274    external form, using SUBSTITUTE (...) if VALUE won't fit.  Output
275    to buffer WHERE with size SIZE.  NEGATIVE is 1 iff VALUE was
276    negative before being cast to uintmax_t; its original bitpattern
277    can be deduced from VALSIZE, its original size before casting.
278    TYPE is the kind of value being output (useful for diagnostics).
279    Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
280    digits), followed by '\0'.  If this won't work, and if GNU or
281    OLDGNU format is allowed, use '\200' followed by base-256, or (if
282    NEGATIVE is nonzero) '\377' followed by two's complement base-256.
283    If neither format works, use SUBSTITUTE (...)  instead.  Pass to
284    SUBSTITUTE the address of an 0-or-1 flag recording whether the
285    substitute value is negative.  */
286
287 static bool
288 to_chars (int negative, uintmax_t value, size_t valsize,
289           uintmax_t (*substitute) (int *),
290           char *where, size_t size, const char *type)
291 {
292   int gnu_format = (archive_format == GNU_FORMAT
293                     || archive_format == OLDGNU_FORMAT);
294
295   /* Generate the POSIX octal representation if the number fits.  */
296   if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
297     {
298       where[size - 1] = '\0';
299       to_octal (value, where, size - 1);
300       return true;
301     }
302   else if (gnu_format)
303     {
304       /* Try to cope with the number by using traditional GNU format
305          methods */
306
307       /* Generate the base-256 representation if the number fits.  */
308       if (((negative ? -1 - value : value)
309            <= MAX_VAL_WITH_DIGITS (size - 1, LG_256)))
310         {
311           where[0] = negative ? -1 : 1 << (LG_256 - 1);
312           to_base256 (negative, value, where + 1, size - 1);
313           return true;
314         }
315
316       /* Otherwise, if the number is negative, and if it would not cause
317          ambiguity on this host by confusing positive with negative
318          values, then generate the POSIX octal representation of the value
319          modulo 2**(field bits).  The resulting tar file is
320          machine-dependent, since it depends on the host word size.  Yuck!
321          But this is the traditional behavior.  */
322       else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
323         {
324           static int warned_once;
325           if (! warned_once)
326             {
327               warned_once = 1;
328               WARN ((0, 0, _("Generating negative octal headers")));
329             }
330           where[size - 1] = '\0';
331           to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
332                     where, size - 1);
333           return true;
334         }
335       /* Otherwise fall back to substitution, if possible: */
336     }
337   else
338     substitute = NULL; /* No substitution for formats, other than GNU */
339
340   return to_chars_subst (negative, gnu_format, value, valsize, substitute,
341                          where, size, type);
342 }
343
344 static uintmax_t
345 gid_substitute (int *negative)
346 {
347   gid_t r;
348 #ifdef GID_NOBODY
349   r = GID_NOBODY;
350 #else
351   static gid_t gid_nobody;
352   if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
353     gid_nobody = -2;
354   r = gid_nobody;
355 #endif
356   *negative = r < 0;
357   return r;
358 }
359
360 static bool
361 gid_to_chars (gid_t v, char *p, size_t s)
362 {
363   return to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
364 }
365
366 static bool
367 major_to_chars (major_t v, char *p, size_t s)
368 {
369   return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
370 }
371
372 static bool
373 minor_to_chars (minor_t v, char *p, size_t s)
374 {
375   return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
376 }
377
378 static bool
379 mode_to_chars (mode_t v, char *p, size_t s)
380 {
381   /* In the common case where the internal and external mode bits are the same,
382      and we are not using POSIX or GNU format,
383      propagate all unknown bits to the external mode.
384      This matches historical practice.
385      Otherwise, just copy the bits we know about.  */
386   int negative;
387   uintmax_t u;
388   if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
389       && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
390       && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
391       && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
392       && archive_format != POSIX_FORMAT
393       && archive_format != USTAR_FORMAT
394       && archive_format != GNU_FORMAT)
395     {
396       negative = v < 0;
397       u = v;
398     }
399   else
400     {
401       negative = 0;
402       u = ((v & S_ISUID ? TSUID : 0)
403            | (v & S_ISGID ? TSGID : 0)
404            | (v & S_ISVTX ? TSVTX : 0)
405            | (v & S_IRUSR ? TUREAD : 0)
406            | (v & S_IWUSR ? TUWRITE : 0)
407            | (v & S_IXUSR ? TUEXEC : 0)
408            | (v & S_IRGRP ? TGREAD : 0)
409            | (v & S_IWGRP ? TGWRITE : 0)
410            | (v & S_IXGRP ? TGEXEC : 0)
411            | (v & S_IROTH ? TOREAD : 0)
412            | (v & S_IWOTH ? TOWRITE : 0)
413            | (v & S_IXOTH ? TOEXEC : 0));
414     }
415   return to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
416 }
417
418 bool
419 off_to_chars (off_t v, char *p, size_t s)
420 {
421   return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
422 }
423
424 bool
425 time_to_chars (time_t v, char *p, size_t s)
426 {
427   return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
428 }
429
430 static uintmax_t
431 uid_substitute (int *negative)
432 {
433   uid_t r;
434 #ifdef UID_NOBODY
435   r = UID_NOBODY;
436 #else
437   static uid_t uid_nobody;
438   if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
439     uid_nobody = -2;
440   r = uid_nobody;
441 #endif
442   *negative = r < 0;
443   return r;
444 }
445
446 static bool
447 uid_to_chars (uid_t v, char *p, size_t s)
448 {
449   return to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
450 }
451
452 static bool
453 uintmax_to_chars (uintmax_t v, char *p, size_t s)
454 {
455   return to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
456 }
457
458 static void
459 string_to_chars (char const *str, char *p, size_t s)
460 {
461   tar_copy_str (p, str, s);
462   p[s - 1] = '\0';
463 }
464
465 \f
466 /* A directory is always considered dumpable.
467    Otherwise, only regular and contiguous files are considered dumpable.
468    Such a file is dumpable if it is sparse and both --sparse and --totals
469    are specified.
470    Otherwise, it is dumpable unless any of the following conditions occur:
471
472    a) it is empty *and* world-readable, or
473    b) current archive is /dev/null */
474
475 static bool
476 file_dumpable_p (struct stat const *st)
477 {
478   if (S_ISDIR (st->st_mode))
479     return true;
480   if (! (S_ISREG (st->st_mode) || S_ISCTG (st->st_mode)))
481     return false;
482   if (dev_null_output)
483     return totals_option && sparse_option && ST_IS_SPARSE (*st);
484   return ! (st->st_size == 0 && (st->st_mode & MODE_R) == MODE_R);
485 }
486
487 \f
488 /* Writing routines.  */
489
490 /* Write the EOT block(s).  Zero at least two blocks, through the end
491    of the record.  Old tar, as previous versions of GNU tar, writes
492    garbage after two zeroed blocks.  */
493 void
494 write_eot (void)
495 {
496   union block *pointer = find_next_block ();
497   memset (pointer->buffer, 0, BLOCKSIZE);
498   set_next_block_after (pointer);
499   pointer = find_next_block ();
500   memset (pointer->buffer, 0, available_space_after (pointer));
501   set_next_block_after (pointer);
502 }
503
504 /* Write a "private" header */
505 union block *
506 start_private_header (const char *name, size_t size, time_t t)
507 {
508   union block *header = find_next_block ();
509
510   memset (header->buffer, 0, sizeof (union block));
511
512   tar_name_copy_str (header->header.name, name, NAME_FIELD_SIZE);
513   OFF_TO_CHARS (size, header->header.size);
514
515   TIME_TO_CHARS (t < 0 ? 0 : min (t, MAX_OCTAL_VAL (header->header.mtime)),
516                  header->header.mtime);
517   MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode);
518   UID_TO_CHARS (0, header->header.uid);
519   GID_TO_CHARS (0, header->header.gid);
520   strncpy (header->header.magic, TMAGIC, TMAGLEN);
521   strncpy (header->header.version, TVERSION, TVERSLEN);
522   return header;
523 }
524
525 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
526    the file name */
527
528 static union block *
529 write_short_name (struct tar_stat_info *st)
530 {
531   union block *header = find_next_block ();
532   memset (header->buffer, 0, sizeof (union block));
533   tar_name_copy_str (header->header.name, st->file_name, NAME_FIELD_SIZE);
534   return header;
535 }
536
537 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block.  */
538 static void
539 write_gnu_long_link (struct tar_stat_info *st, const char *p, char type)
540 {
541   size_t size = strlen (p) + 1;
542   size_t bufsize;
543   union block *header;
544   char *tmpname;
545
546   header = start_private_header ("././@LongLink", size, 0);
547   uid_to_uname (0, &tmpname);
548   UNAME_TO_CHARS (tmpname, header->header.uname);
549   free (tmpname);
550   gid_to_gname (0, &tmpname);
551   GNAME_TO_CHARS (tmpname, header->header.gname);
552   free (tmpname);
553
554   strcpy (header->buffer + offsetof (struct posix_header, magic),
555           OLDGNU_MAGIC);
556   header->header.typeflag = type;
557   finish_header (st, header, -1);
558
559   header = find_next_block ();
560
561   bufsize = available_space_after (header);
562
563   while (bufsize < size)
564     {
565       memcpy (header->buffer, p, bufsize);
566       p += bufsize;
567       size -= bufsize;
568       set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
569       header = find_next_block ();
570       bufsize = available_space_after (header);
571     }
572   memcpy (header->buffer, p, size);
573   memset (header->buffer + size, 0, bufsize - size);
574   set_next_block_after (header + (size - 1) / BLOCKSIZE);
575 }
576
577 static size_t
578 split_long_name (const char *name, size_t length)
579 {
580   size_t i;
581
582   if (length > PREFIX_FIELD_SIZE + 1)
583     length = PREFIX_FIELD_SIZE + 1;
584   else if (ISSLASH (name[length - 1]))
585     length--;
586   for (i = length - 1; i > 0; i--)
587     if (ISSLASH (name[i]))
588       break;
589   return i;
590 }
591
592 static union block *
593 write_ustar_long_name (const char *name)
594 {
595   size_t length = strlen (name);
596   size_t i, nlen;
597   union block *header;
598
599   if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
600     {
601       ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
602               quotearg_colon (name),
603               PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
604       return NULL;
605     }
606
607   i = split_long_name (name, length);
608   if (i == 0 || (nlen = length - i - 1) > NAME_FIELD_SIZE || nlen == 0)
609     {
610       ERROR ((0, 0,
611               _("%s: file name is too long (cannot be split); not dumped"),
612               quotearg_colon (name)));
613       return NULL;
614     }
615
616   header = find_next_block ();
617   memset (header->buffer, 0, sizeof (header->buffer));
618   memcpy (header->header.prefix, name, i);
619   memcpy (header->header.name, name + i + 1, length - i - 1);
620
621   return header;
622 }
623
624 /* Write a long link name, depending on the current archive format */
625 static void
626 write_long_link (struct tar_stat_info *st)
627 {
628   switch (archive_format)
629     {
630     case POSIX_FORMAT:
631       xheader_store ("linkpath", st, NULL);
632       break;
633
634     case V7_FORMAT:                     /* old V7 tar format */
635     case USTAR_FORMAT:
636     case STAR_FORMAT:
637       ERROR ((0, 0,
638               _("%s: link name is too long; not dumped"),
639               quotearg_colon (st->link_name)));
640       break;
641
642     case OLDGNU_FORMAT:
643     case GNU_FORMAT:
644       write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK);
645       break;
646
647     default:
648       abort(); /*FIXME*/
649     }
650 }
651
652 static union block *
653 write_long_name (struct tar_stat_info *st)
654 {
655   switch (archive_format)
656     {
657     case POSIX_FORMAT:
658       xheader_store ("path", st, NULL);
659       break;
660
661     case V7_FORMAT:
662       if (strlen (st->file_name) > NAME_FIELD_SIZE-1)
663         {
664           ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
665                   quotearg_colon (st->file_name),
666                   NAME_FIELD_SIZE - 1));
667           return NULL;
668         }
669       break;
670
671     case USTAR_FORMAT:
672     case STAR_FORMAT:
673       return write_ustar_long_name (st->file_name);
674
675     case OLDGNU_FORMAT:
676     case GNU_FORMAT:
677       write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
678       break;
679
680     default:
681       abort(); /*FIXME*/
682     }
683   return write_short_name (st);
684 }
685
686 union block *
687 write_extended (bool global, struct tar_stat_info *st, union block *old_header)
688 {
689   union block *header, hp;
690   char *p;
691   int type;
692   time_t t;
693
694   if (st->xhdr.buffer || st->xhdr.stk == NULL)
695     return old_header;
696
697   xheader_finish (&st->xhdr);
698   memcpy (hp.buffer, old_header, sizeof (hp));
699   if (global)
700     {
701       type = XGLTYPE;
702       p = xheader_ghdr_name ();
703       t = start_time.tv_sec;
704     }
705   else
706     {
707       type = XHDTYPE;
708       p = xheader_xhdr_name (st);
709       t = st->stat.st_mtime;
710     }
711   xheader_write (type, p, t, &st->xhdr);
712   free (p);
713   header = find_next_block ();
714   memcpy (header, &hp.buffer, sizeof (hp.buffer));
715   return header;
716 }
717
718 static union block *
719 write_header_name (struct tar_stat_info *st)
720 {
721   if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
722     {
723       xheader_store ("path", st, NULL);
724       return write_short_name (st);
725     }
726   else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
727            < strlen (st->file_name))
728     return write_long_name (st);
729   else
730     return write_short_name (st);
731 }
732
733 \f
734 /* Header handling.  */
735
736 /* Make a header block for the file whose stat info is st,
737    and return its address.  */
738
739 union block *
740 start_header (struct tar_stat_info *st)
741 {
742   union block *header;
743
744   header = write_header_name (st);
745   if (!header)
746     return NULL;
747
748   /* Override some stat fields, if requested to do so.  */
749
750   if (owner_option != (uid_t) -1)
751     st->stat.st_uid = owner_option;
752   if (group_option != (gid_t) -1)
753     st->stat.st_gid = group_option;
754   if (mode_option)
755     st->stat.st_mode =
756       ((st->stat.st_mode & ~MODE_ALL)
757        | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0,
758                       initial_umask, mode_option, NULL));
759
760   /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
761      for a few tars and came up with the following interoperability
762      matrix:
763
764               WRITER
765         1 2 3 4 5 6 7 8 9   READER
766         . . . . . . . . .   1 = SunOS 4.2 tar
767         # . . # # . . # #   2 = NEC SVR4.0.2 tar
768         . . . # # . . # .   3 = Solaris 2.1 tar
769         . . . . . . . . .   4 = GNU tar 1.11.1
770         . . . . . . . . .   5 = HP-UX 8.07 tar
771         . . . . . . . . .   6 = Ultrix 4.1
772         . . . . . . . . .   7 = AIX 3.2
773         . . . . . . . . .   8 = Hitachi HI-UX 1.03
774         . . . . . . . . .   9 = Omron UNIOS-B 4.3BSD 1.60Beta
775
776              . = works
777              # = "impossible file type"
778
779      The following mask for old archive removes the '#'s in column 4
780      above, thus making GNU tar both a universal donor and a universal
781      acceptor for Paul's test.  */
782
783   if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
784     MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
785   else
786     MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
787
788   {
789     uid_t uid = st->stat.st_uid;
790     if (archive_format == POSIX_FORMAT
791         && MAX_OCTAL_VAL (header->header.uid) < uid)
792       {
793         xheader_store ("uid", st, NULL);
794         uid = 0;
795       }
796     if (!UID_TO_CHARS (uid, header->header.uid))
797       return NULL;
798   }
799
800   {
801     gid_t gid = st->stat.st_gid;
802     if (archive_format == POSIX_FORMAT
803         && MAX_OCTAL_VAL (header->header.gid) < gid)
804       {
805         xheader_store ("gid", st, NULL);
806         gid = 0;
807       }
808     if (!GID_TO_CHARS (gid, header->header.gid))
809       return NULL;
810   }
811
812   {
813     off_t size = st->stat.st_size;
814     if (archive_format == POSIX_FORMAT
815         && MAX_OCTAL_VAL (header->header.size) < size)
816       {
817         xheader_store ("size", st, NULL);
818         size = 0;
819       }
820     if (!OFF_TO_CHARS (size, header->header.size))
821       return NULL;
822   }
823
824   {
825     struct timespec mtime = set_mtime_option ? mtime_option : st->mtime;
826     if (archive_format == POSIX_FORMAT)
827       {
828         if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
829             || mtime.tv_nsec != 0)
830           xheader_store ("mtime", st, &mtime);
831         if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
832           mtime.tv_sec = 0;
833       }
834     if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime))
835       return NULL;
836   }
837
838   /* FIXME */
839   if (S_ISCHR (st->stat.st_mode)
840       || S_ISBLK (st->stat.st_mode))
841     {
842       major_t devmajor = major (st->stat.st_rdev);
843       minor_t devminor = minor (st->stat.st_rdev);
844
845       if (archive_format == POSIX_FORMAT
846           && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
847         {
848           xheader_store ("devmajor", st, NULL);
849           devmajor = 0;
850         }
851       if (!MAJOR_TO_CHARS (devmajor, header->header.devmajor))
852         return NULL;
853
854       if (archive_format == POSIX_FORMAT
855           && MAX_OCTAL_VAL (header->header.devminor) < devminor)
856         {
857           xheader_store ("devminor", st, NULL);
858           devminor = 0;
859         }
860       if (!MINOR_TO_CHARS (devminor, header->header.devminor))
861         return NULL;
862     }
863   else if (archive_format != GNU_FORMAT && archive_format != OLDGNU_FORMAT)
864     {
865       if (!(MAJOR_TO_CHARS (0, header->header.devmajor)
866             && MINOR_TO_CHARS (0, header->header.devminor)))
867         return NULL;
868     }
869
870   if (archive_format == POSIX_FORMAT)
871     {
872       xheader_store ("atime", st, NULL);
873       xheader_store ("ctime", st, NULL);
874     }
875   else if (incremental_option)
876     if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
877       {
878         TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
879         TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
880       }
881
882   header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
883
884   switch (archive_format)
885     {
886     case V7_FORMAT:
887       break;
888
889     case OLDGNU_FORMAT:
890     case GNU_FORMAT:   /*FIXME?*/
891       /* Overwrite header->header.magic and header.version in one blow.  */
892       strcpy (header->buffer + offsetof (struct posix_header, magic),
893               OLDGNU_MAGIC);
894       break;
895
896     case POSIX_FORMAT:
897     case USTAR_FORMAT:
898       strncpy (header->header.magic, TMAGIC, TMAGLEN);
899       strncpy (header->header.version, TVERSION, TVERSLEN);
900       break;
901
902     default:
903       abort ();
904     }
905
906   if (archive_format == V7_FORMAT || numeric_owner_option)
907     {
908       /* header->header.[ug]name are left as the empty string.  */
909     }
910   else
911     {
912       if (owner_name_option)
913         st->uname = xstrdup (owner_name_option);
914       else
915         uid_to_uname (st->stat.st_uid, &st->uname);
916
917       if (group_name_option)
918         st->gname = xstrdup (group_name_option);
919       else
920         gid_to_gname (st->stat.st_gid, &st->gname);
921
922       if (archive_format == POSIX_FORMAT
923           && (strlen (st->uname) > UNAME_FIELD_SIZE
924               || !string_ascii_p (st->uname)))
925         xheader_store ("uname", st, NULL);
926       UNAME_TO_CHARS (st->uname, header->header.uname);
927
928       if (archive_format == POSIX_FORMAT
929           && (strlen (st->gname) > GNAME_FIELD_SIZE
930               || !string_ascii_p (st->gname)))
931         xheader_store ("gname", st, NULL);
932       GNAME_TO_CHARS (st->gname, header->header.gname);
933     }
934
935   if (archive_format == POSIX_FORMAT)
936     {
937       if (acls_option > 0)
938         {
939           if (st->acls_a_ptr)
940             xheader_store ("SCHILY.acl.access", st, NULL);
941           if (st->acls_d_ptr)
942             xheader_store ("SCHILY.acl.default", st, NULL);
943         }
944       if ((selinux_context_option > 0) && st->cntx_name)
945         xheader_store ("RHT.security.selinux", st, NULL);
946       if (xattrs_option > 0)
947         {
948           size_t scan_xattr = 0;
949           struct xattr_array *xattr_map = st->xattr_map;
950
951           while (scan_xattr < st->xattr_map_size)
952             {
953               xheader_store (xattr_map[scan_xattr].xkey, st, &scan_xattr);
954               ++scan_xattr;
955             }
956         }
957     }
958
959   return header;
960 }
961
962 void
963 simple_finish_header (union block *header)
964 {
965   size_t i;
966   int sum;
967   char *p;
968
969   memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
970
971   sum = 0;
972   p = header->buffer;
973   for (i = sizeof *header; i-- != 0; )
974     /* We can't use unsigned char here because of old compilers, e.g. V7.  */
975     sum += 0xFF & *p++;
976
977   /* Fill in the checksum field.  It's formatted differently from the
978      other fields: it has [6] digits, a null, then a space -- rather than
979      digits, then a null.  We use to_chars.
980      The final space is already there, from
981      checksumming, and to_chars doesn't modify it.
982
983      This is a fast way to do:
984
985      sprintf(header->header.chksum, "%6o", sum);  */
986
987   uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
988
989   set_next_block_after (header);
990 }
991
992 /* Finish off a filled-in header block and write it out.  We also
993    print the file name and/or full info if verbose is on.  If BLOCK_ORDINAL
994    is not negative, is the block ordinal of the first record for this
995    file, which may be a preceding long name or long link record.  */
996 void
997 finish_header (struct tar_stat_info *st,
998                union block *header, off_t block_ordinal)
999 {
1000   /* Note: It is important to do this before the call to write_extended(),
1001      so that the actual ustar header is printed */
1002   if (verbose_option
1003       && header->header.typeflag != GNUTYPE_LONGLINK
1004       && header->header.typeflag != GNUTYPE_LONGNAME
1005       && header->header.typeflag != XHDTYPE
1006       && header->header.typeflag != XGLTYPE)
1007     {
1008       /* FIXME: This global is used in print_header, sigh.  */
1009       current_format = archive_format;
1010       print_header (st, header, block_ordinal);
1011     }
1012
1013   header = write_extended (false, st, header);
1014   simple_finish_header (header);
1015 }
1016 \f
1017
1018 void
1019 pad_archive (off_t size_left)
1020 {
1021   union block *blk;
1022   while (size_left > 0)
1023     {
1024       blk = find_next_block ();
1025       memset (blk->buffer, 0, BLOCKSIZE);
1026       set_next_block_after (blk);
1027       size_left -= BLOCKSIZE;
1028     }
1029 }
1030
1031 static enum dump_status
1032 dump_regular_file (int fd, struct tar_stat_info *st)
1033 {
1034   off_t size_left = st->stat.st_size;
1035   off_t block_ordinal;
1036   union block *blk;
1037
1038   block_ordinal = current_block_ordinal ();
1039   blk = start_header (st);
1040   if (!blk)
1041     return dump_status_fail;
1042
1043   /* Mark contiguous files, if we support them.  */
1044   if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
1045     blk->header.typeflag = CONTTYPE;
1046
1047   finish_header (st, blk, block_ordinal);
1048
1049   mv_begin_write (st->file_name, st->stat.st_size, st->stat.st_size);
1050   while (size_left > 0)
1051     {
1052       size_t bufsize, count;
1053
1054       blk = find_next_block ();
1055
1056       bufsize = available_space_after (blk);
1057
1058       if (size_left < bufsize)
1059         {
1060           /* Last read -- zero out area beyond.  */
1061           bufsize = size_left;
1062           count = bufsize % BLOCKSIZE;
1063           if (count)
1064             memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1065         }
1066
1067       count = (fd <= 0) ? bufsize : blocking_read (fd, blk->buffer, bufsize);
1068       if (count == SAFE_READ_ERROR)
1069         {
1070           read_diag_details (st->orig_file_name,
1071                              st->stat.st_size - size_left, bufsize);
1072           pad_archive (size_left);
1073           return dump_status_short;
1074         }
1075       size_left -= count;
1076       set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1077
1078       if (count != bufsize)
1079         {
1080           char buf[UINTMAX_STRSIZE_BOUND];
1081           memset (blk->buffer + count, 0, bufsize - count);
1082           WARNOPT (WARN_FILE_SHRANK,
1083                    (0, 0,
1084                     ngettext ("%s: File shrank by %s byte; padding with zeros",
1085                               "%s: File shrank by %s bytes; padding with zeros",
1086                               size_left),
1087                     quotearg_colon (st->orig_file_name),
1088                     STRINGIFY_BIGINT (size_left, buf)));
1089           if (! ignore_failed_read_option)
1090             set_exit_status (TAREXIT_DIFFERS);
1091           pad_archive (size_left - (bufsize - count));
1092           return dump_status_short;
1093         }
1094     }
1095   return dump_status_ok;
1096 }
1097
1098 \f
1099 /* Copy info from the directory identified by ST into the archive.
1100    DIRECTORY contains the directory's entries.  */
1101
1102 static void
1103 dump_dir0 (struct tar_stat_info *st, char const *directory)
1104 {
1105   bool top_level = ! st->parent;
1106   const char *tag_file_name;
1107   union block *blk = NULL;
1108   off_t block_ordinal = current_block_ordinal ();
1109
1110   st->stat.st_size = 0; /* force 0 size on dir */
1111
1112   blk = start_header (st);
1113   if (!blk)
1114     return;
1115
1116   info_attach_exclist (st);
1117   
1118   if (incremental_option && archive_format != POSIX_FORMAT)
1119     blk->header.typeflag = GNUTYPE_DUMPDIR;
1120   else /* if (standard_option) */
1121     blk->header.typeflag = DIRTYPE;
1122
1123   /* If we're gnudumping, we aren't done yet so don't close it.  */
1124
1125   if (!incremental_option)
1126     finish_header (st, blk, block_ordinal);
1127   else if (gnu_list_name->directory)
1128     {
1129       if (archive_format == POSIX_FORMAT)
1130         {
1131           xheader_store ("GNU.dumpdir", st,
1132                          safe_directory_contents (gnu_list_name->directory));
1133           finish_header (st, blk, block_ordinal);
1134         }
1135       else
1136         {
1137           off_t size_left;
1138           off_t totsize;
1139           size_t bufsize;
1140           ssize_t count;
1141           const char *buffer, *p_buffer;
1142
1143           block_ordinal = current_block_ordinal ();
1144           buffer = safe_directory_contents (gnu_list_name->directory);
1145           totsize = dumpdir_size (buffer);
1146           OFF_TO_CHARS (totsize, blk->header.size);
1147           finish_header (st, blk, block_ordinal);
1148           p_buffer = buffer;
1149           size_left = totsize;
1150
1151           mv_begin_write (st->file_name, totsize, totsize);
1152           while (size_left > 0)
1153             {
1154               blk = find_next_block ();
1155               bufsize = available_space_after (blk);
1156               if (size_left < bufsize)
1157                 {
1158                   bufsize = size_left;
1159                   count = bufsize % BLOCKSIZE;
1160                   if (count)
1161                     memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1162                 }
1163               memcpy (blk->buffer, p_buffer, bufsize);
1164               size_left -= bufsize;
1165               p_buffer += bufsize;
1166               set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1167             }
1168         }
1169       return;
1170     }
1171
1172   if (!recursion_option)
1173     return;
1174
1175   if (one_file_system_option
1176       && !top_level
1177       && st->parent->stat.st_dev != st->stat.st_dev)
1178     {
1179       if (verbose_option)
1180         WARNOPT (WARN_XDEV,
1181                  (0, 0,
1182                   _("%s: file is on a different filesystem; not dumped"),
1183                   quotearg_colon (st->orig_file_name)));
1184     }
1185   else
1186     {
1187       char *name_buf;
1188       size_t name_size;
1189
1190       switch (check_exclusion_tags (st, &tag_file_name))
1191         {
1192         case exclusion_tag_all:
1193           /* Handled in dump_file0 */
1194           break;
1195
1196         case exclusion_tag_none:
1197           {
1198             char const *entry;
1199             size_t entry_len;
1200             size_t name_len;
1201             
1202             name_buf = xstrdup (st->orig_file_name);
1203             name_size = name_len = strlen (name_buf);
1204
1205             /* Now output all the files in the directory.  */
1206             for (entry = directory; (entry_len = strlen (entry)) != 0;
1207                  entry += entry_len + 1)
1208               {
1209                 if (name_size < name_len + entry_len)
1210                   {
1211                     name_size = name_len + entry_len;
1212                     name_buf = xrealloc (name_buf, name_size + 1);
1213                   }
1214                 strcpy (name_buf + name_len, entry);
1215                 if (!excluded_name (name_buf, st))
1216                   dump_file (st, entry, name_buf);
1217               }
1218
1219             free (name_buf);
1220           }
1221           break;
1222
1223         case exclusion_tag_contents:
1224           exclusion_tag_warning (st->orig_file_name, tag_file_name,
1225                                  _("contents not dumped"));
1226           name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1;
1227           name_buf = xmalloc (name_size);
1228           strcpy (name_buf, st->orig_file_name);
1229           strcat (name_buf, tag_file_name);
1230           dump_file (st, tag_file_name, name_buf);
1231           free (name_buf);
1232           break;
1233
1234         case exclusion_tag_under:
1235           exclusion_tag_warning (st->orig_file_name, tag_file_name,
1236                                  _("contents not dumped"));
1237           break;
1238         }
1239     }
1240 }
1241
1242 /* Ensure exactly one trailing slash.  */
1243 static void
1244 ensure_slash (char **pstr)
1245 {
1246   size_t len = strlen (*pstr);
1247   while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
1248     len--;
1249   if (!ISSLASH ((*pstr)[len]))
1250     *pstr = xrealloc (*pstr, len + 2);
1251   (*pstr)[len++] = '/';
1252   (*pstr)[len] = '\0';
1253 }
1254
1255 /* If we just ran out of file descriptors, release a file descriptor
1256    in the directory chain somewhere leading from DIR->parent->parent
1257    up through the root.  Return true if successful, false (preserving
1258    errno == EMFILE) otherwise.
1259
1260    Do not release DIR's file descriptor, or DIR's parent, as other
1261    code assumes that they work.  On some operating systems, another
1262    process can claim file descriptor resources as we release them, and
1263    some calls or their emulations require multiple file descriptors,
1264    so callers should not give up if a single release doesn't work.  */
1265
1266 static bool
1267 open_failure_recover (struct tar_stat_info const *dir)
1268 {
1269   if (errno == EMFILE && dir && dir->parent)
1270     {
1271       struct tar_stat_info *p;
1272       for (p = dir->parent->parent; p; p = p->parent)
1273         if (0 < p->fd && (! p->parent || p->parent->fd <= 0))
1274           {
1275             tar_stat_close (p);
1276             return true;
1277           }
1278       errno = EMFILE;
1279     }
1280
1281   return false;
1282 }
1283
1284 /* Return the directory entries of ST, in a dynamically allocated buffer,
1285    each entry followed by '\0' and the last followed by an extra '\0'.
1286    Return null on failure, setting errno.  */
1287 char *
1288 get_directory_entries (struct tar_stat_info *st)
1289 {
1290   while (! (st->dirstream = fdopendir (st->fd)))
1291     if (! open_failure_recover (st))
1292       return 0;
1293   return streamsavedir (st->dirstream, savedir_sort_order);
1294 }
1295
1296 /* Dump the directory ST.  Return true if successful, false (emitting
1297    diagnostics) otherwise.  Get ST's entries, recurse through its
1298    subdirectories, and clean up file descriptors afterwards.  */
1299 static bool
1300 dump_dir (struct tar_stat_info *st)
1301 {
1302   char *directory = get_directory_entries (st);
1303   if (! directory)
1304     {
1305       savedir_diag (st->orig_file_name);
1306       return false;
1307     }
1308
1309   dump_dir0 (st, directory);
1310
1311   restore_parent_fd (st);
1312   free (directory);
1313   return true;
1314 }
1315
1316 \f
1317 /* Number of links a file can have without having to be entered into
1318    the link table.  Typically this is 1, but in trickier circumstances
1319    it is 0.  */
1320 static nlink_t trivial_link_count;
1321
1322 \f
1323 /* Main functions of this module.  */
1324
1325 void
1326 create_archive (void)
1327 {
1328   struct name const *p;
1329
1330   trivial_link_count = name_count <= 1 && ! dereference_option;
1331
1332   open_archive (ACCESS_WRITE);
1333   buffer_write_global_xheader ();
1334
1335   if (incremental_option)
1336     {
1337       size_t buffer_size = 1000;
1338       char *buffer = xmalloc (buffer_size);
1339       const char *q;
1340
1341       collect_and_sort_names ();
1342
1343       while ((p = name_from_list ()) != NULL)
1344         if (!excluded_name (p->name, NULL))
1345           dump_file (0, p->name, p->name);
1346
1347       blank_name_list ();
1348       while ((p = name_from_list ()) != NULL)
1349         if (!excluded_name (p->name, NULL))
1350           {
1351             struct tar_stat_info st;
1352             size_t plen = strlen (p->name);
1353             if (buffer_size <= plen)
1354               {
1355                 while ((buffer_size *= 2) <= plen)
1356                   continue;
1357                 buffer = xrealloc (buffer, buffer_size);
1358               }
1359             memcpy (buffer, p->name, plen);
1360             if (! ISSLASH (buffer[plen - 1]))
1361               buffer[plen++] = DIRECTORY_SEPARATOR;
1362             tar_stat_init (&st);
1363             q = directory_contents (p->directory);
1364             if (q)
1365               while (*q)
1366                 {
1367                   size_t qlen = strlen (q);
1368                   if (*q == 'Y')
1369                     {
1370                       if (! st.orig_file_name)
1371                         {
1372                           int fd = openat (chdir_fd, p->name,
1373                                            open_searchdir_flags);
1374                           if (fd < 0)
1375                             {
1376                               open_diag (p->name);
1377                               break;
1378                             }
1379                           st.fd = fd;
1380                           if (fstat (fd, &st.stat) != 0)
1381                             {
1382                               stat_diag (p->name);
1383                               break;
1384                             }
1385                           st.orig_file_name = xstrdup (p->name);
1386                         }
1387                       if (buffer_size < plen + qlen)
1388                         {
1389                           while ((buffer_size *=2 ) < plen + qlen)
1390                             continue;
1391                           buffer = xrealloc (buffer, buffer_size);
1392                         }
1393                       strcpy (buffer + plen, q + 1);
1394                       dump_file (&st, q + 1, buffer);
1395                     }
1396                   q += qlen + 1;
1397                 }
1398             tar_stat_destroy (&st);
1399           }
1400       free (buffer);
1401     }
1402   else
1403     {
1404       const char *name;
1405       while ((name = name_next (1)) != NULL)
1406         if (!excluded_name (name, NULL))
1407           dump_file (0, name, name);
1408     }
1409
1410   write_eot ();
1411   close_archive ();
1412   finish_deferred_unlinks ();
1413   if (listed_incremental_option)
1414     write_directory_file ();
1415 }
1416
1417
1418 /* Calculate the hash of a link.  */
1419 static size_t
1420 hash_link (void const *entry, size_t n_buckets)
1421 {
1422   struct link const *l = entry;
1423   uintmax_t num = l->dev ^ l->ino;
1424   return num % n_buckets;
1425 }
1426
1427 /* Compare two links for equality.  */
1428 static bool
1429 compare_links (void const *entry1, void const *entry2)
1430 {
1431   struct link const *link1 = entry1;
1432   struct link const *link2 = entry2;
1433   return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
1434 }
1435
1436 static void
1437 unknown_file_error (char const *p)
1438 {
1439   WARNOPT (WARN_FILE_IGNORED,
1440            (0, 0, _("%s: Unknown file type; file ignored"),
1441             quotearg_colon (p)));
1442   if (!ignore_failed_read_option)
1443     set_exit_status (TAREXIT_FAILURE);
1444 }
1445
1446 \f
1447 /* Handling of hard links */
1448
1449 /* Table of all non-directories that we've written so far.  Any time
1450    we see another, we check the table and avoid dumping the data
1451    again if we've done it once already.  */
1452 static Hash_table *link_table;
1453
1454 /* Try to dump stat as a hard link to another file in the archive.
1455    Return true if successful.  */
1456 static bool
1457 dump_hard_link (struct tar_stat_info *st)
1458 {
1459   if (link_table
1460       && (trivial_link_count < st->stat.st_nlink || remove_files_option))
1461     {
1462       struct link lp;
1463       struct link *duplicate;
1464       off_t block_ordinal;
1465       union block *blk;
1466
1467       lp.ino = st->stat.st_ino;
1468       lp.dev = st->stat.st_dev;
1469
1470       if ((duplicate = hash_lookup (link_table, &lp)))
1471         {
1472           /* We found a link.  */
1473           char const *link_name = safer_name_suffix (duplicate->name, true,
1474                                                      absolute_names_option);
1475
1476           duplicate->nlink--;
1477
1478           block_ordinal = current_block_ordinal ();
1479           assign_string (&st->link_name, link_name);
1480           if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
1481               < strlen (link_name))
1482             write_long_link (st);
1483
1484           st->stat.st_size = 0;
1485           blk = start_header (st);
1486           if (!blk)
1487             return false;
1488           tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
1489
1490           blk->header.typeflag = LNKTYPE;
1491           finish_header (st, blk, block_ordinal);
1492
1493           if (remove_files_option)
1494             queue_deferred_unlink (st->orig_file_name, false);
1495
1496           return true;
1497         }
1498     }
1499   return false;
1500 }
1501
1502 static void
1503 file_count_links (struct tar_stat_info *st)
1504 {
1505   if (hard_dereference_option)
1506     return;
1507   if (trivial_link_count < st->stat.st_nlink)
1508     {
1509       struct link *duplicate;
1510       char *linkname = NULL;
1511       struct link *lp;
1512
1513       assign_string (&linkname, st->orig_file_name);
1514       transform_name (&linkname, XFORM_LINK);
1515
1516       lp = xmalloc (offsetof (struct link, name)
1517                                  + strlen (linkname) + 1);
1518       lp->ino = st->stat.st_ino;
1519       lp->dev = st->stat.st_dev;
1520       lp->nlink = st->stat.st_nlink;
1521       strcpy (lp->name, linkname);
1522       free (linkname);
1523
1524       if (! ((link_table
1525               || (link_table = hash_initialize (0, 0, hash_link,
1526                                                 compare_links, 0)))
1527              && (duplicate = hash_insert (link_table, lp))))
1528         xalloc_die ();
1529
1530       if (duplicate != lp)
1531         abort ();
1532       lp->nlink--;
1533     }
1534 }
1535
1536 /* For each dumped file, check if all its links were dumped. Emit
1537    warnings if it is not so. */
1538 void
1539 check_links (void)
1540 {
1541   struct link *lp;
1542
1543   if (!link_table)
1544     return;
1545
1546   for (lp = hash_get_first (link_table); lp;
1547        lp = hash_get_next (link_table, lp))
1548     {
1549       if (lp->nlink)
1550         {
1551           WARN ((0, 0, _("Missing links to %s."), quote (lp->name)));
1552         }
1553     }
1554 }
1555
1556 /* Assuming DIR is the working directory, open FILE, using FLAGS to
1557    control the open.  A null DIR means to use ".".  If we are low on
1558    file descriptors, try to release one or more from DIR's parents to
1559    reuse it.  */
1560 int
1561 subfile_open (struct tar_stat_info const *dir, char const *file, int flags)
1562 {
1563   int fd;
1564
1565   static bool initialized;
1566   if (! initialized)
1567     {
1568       /* Initialize any tables that might be needed when file
1569          descriptors are exhausted, and whose initialization might
1570          require a file descriptor.  This includes the system message
1571          catalog and tar's message catalog.  */
1572       initialized = true;
1573       strerror (ENOENT);
1574       gettext ("");
1575     }
1576
1577   while ((fd = openat (dir ? dir->fd : chdir_fd, file, flags)) < 0
1578          && open_failure_recover (dir))
1579     continue;
1580   return fd;
1581 }
1582
1583 /* Restore the file descriptor for ST->parent, if it was temporarily
1584    closed to conserve file descriptors.  On failure, set the file
1585    descriptor to the negative of the corresponding errno value.  Call
1586    this every time a subdirectory is ascended from.  */
1587 void
1588 restore_parent_fd (struct tar_stat_info const *st)
1589 {
1590   struct tar_stat_info *parent = st->parent;
1591   if (parent && ! parent->fd)
1592     {
1593       int parentfd = openat (st->fd, "..", open_searchdir_flags);
1594       struct stat parentstat;
1595
1596       if (parentfd < 0)
1597         parentfd = - errno;
1598       else if (! (fstat (parentfd, &parentstat) == 0
1599                   && parent->stat.st_ino == parentstat.st_ino
1600                   && parent->stat.st_dev == parentstat.st_dev))
1601         {
1602           close (parentfd);
1603           parentfd = IMPOSTOR_ERRNO;
1604         }
1605
1606       if (parentfd < 0)
1607         {
1608           int origfd = openat (chdir_fd, parent->orig_file_name,
1609                                open_searchdir_flags);
1610           if (0 <= origfd)
1611             {
1612               if (fstat (parentfd, &parentstat) == 0
1613                   && parent->stat.st_ino == parentstat.st_ino
1614                   && parent->stat.st_dev == parentstat.st_dev)
1615                 parentfd = origfd;
1616               else
1617                 close (origfd);
1618             }
1619         }
1620
1621       parent->fd = parentfd;
1622     }
1623 }
1624
1625 /* Dump a single file, recursing on directories.  ST is the file's
1626    status info, NAME its name relative to the parent directory, and P
1627    its full name (which may be relative to the working directory).  */
1628
1629 /* FIXME: One should make sure that for *every* path leading to setting
1630    exit_status to failure, a clear diagnostic has been issued.  */
1631
1632 static void
1633 dump_file0 (struct tar_stat_info *st, char const *name, char const *p)
1634 {
1635   union block *header;
1636   char type;
1637   off_t original_size;
1638   struct timespec original_ctime;
1639   off_t block_ordinal = -1;
1640   int fd = 0;
1641   bool is_dir;
1642   struct tar_stat_info const *parent = st->parent;
1643   bool top_level = ! parent;
1644   int parentfd = top_level ? chdir_fd : parent->fd;
1645   void (*diag) (char const *) = 0;
1646
1647   if (interactive_option && !confirm ("add", p))
1648     return;
1649
1650   assign_string (&st->orig_file_name, p);
1651   assign_string (&st->file_name,
1652                  safer_name_suffix (p, false, absolute_names_option));
1653
1654   transform_name (&st->file_name, XFORM_REGFILE);
1655
1656   if (parentfd < 0 && ! top_level)
1657     {
1658       errno = - parentfd;
1659       diag = open_diag;
1660     }
1661   else if (fstatat (parentfd, name, &st->stat, fstatat_flags) != 0)
1662     diag = stat_diag;
1663   else if (file_dumpable_p (&st->stat))
1664     {
1665       fd = subfile_open (parent, name, open_read_flags);
1666       if (fd < 0)
1667         diag = open_diag;
1668       else
1669         {
1670           st->fd = fd;
1671           if (fstat (fd, &st->stat) != 0)
1672             diag = stat_diag;
1673         }
1674     }
1675   if (diag)
1676     {
1677       file_removed_diag (p, top_level, diag);
1678       return;
1679     }
1680
1681   st->archive_file_size = original_size = st->stat.st_size;
1682   st->atime = get_stat_atime (&st->stat);
1683   st->mtime = get_stat_mtime (&st->stat);
1684   st->ctime = original_ctime = get_stat_ctime (&st->stat);
1685
1686 #ifdef S_ISHIDDEN
1687   if (S_ISHIDDEN (st->stat.st_mode))
1688     {
1689       char *new = (char *) alloca (strlen (p) + 2);
1690       if (new)
1691         {
1692           strcpy (new, p);
1693           strcat (new, "@");
1694           p = new;
1695         }
1696     }
1697 #endif
1698
1699   /* See if we want only new files, and check if this one is too old to
1700      put in the archive.
1701
1702      This check is omitted if incremental_option is set *and* the
1703      requested file is not explicitly listed in the command line.  */
1704
1705   if (! (incremental_option && ! top_level)
1706       && !S_ISDIR (st->stat.st_mode)
1707       && OLDER_TAR_STAT_TIME (*st, m)
1708       && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
1709     {
1710       if (!incremental_option && verbose_option)
1711         WARNOPT (WARN_FILE_UNCHANGED,
1712                  (0, 0, _("%s: file is unchanged; not dumped"),
1713                   quotearg_colon (p)));
1714       return;
1715     }
1716
1717   /* See if we are trying to dump the archive.  */
1718   if (sys_file_is_archive (st))
1719     {
1720       WARNOPT (WARN_IGNORE_ARCHIVE,
1721                (0, 0, _("%s: file is the archive; not dumped"),
1722                 quotearg_colon (p)));
1723       return;
1724     }
1725
1726   is_dir = S_ISDIR (st->stat.st_mode) != 0;
1727
1728   if (!is_dir && dump_hard_link (st))
1729     return;
1730
1731   if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode))
1732     {
1733       bool ok;
1734       struct stat final_stat;
1735
1736       xattrs_acls_get (parentfd, name, st, 0, !is_dir);
1737       xattrs_selinux_get (parentfd, name, st, fd);
1738       xattrs_xattrs_get (parentfd, name, st, fd);
1739
1740       if (is_dir)
1741         {
1742           const char *tag_file_name;
1743           ensure_slash (&st->orig_file_name);
1744           ensure_slash (&st->file_name);
1745
1746           if (check_exclusion_tags (st, &tag_file_name) == exclusion_tag_all)
1747             {
1748               exclusion_tag_warning (st->orig_file_name, tag_file_name,
1749                                      _("directory not dumped"));
1750               return;
1751             }
1752
1753           ok = dump_dir (st);
1754
1755           fd = st->fd;
1756           parentfd = top_level ? chdir_fd : parent->fd;
1757         }
1758       else
1759         {
1760           enum dump_status status;
1761
1762           if (fd && sparse_option && ST_IS_SPARSE (st->stat))
1763             {
1764               status = sparse_dump_file (fd, st);
1765               if (status == dump_status_not_implemented)
1766                 status = dump_regular_file (fd, st);
1767             }
1768           else
1769             status = dump_regular_file (fd, st);
1770
1771           switch (status)
1772             {
1773             case dump_status_ok:
1774             case dump_status_short:
1775               file_count_links (st);
1776               break;
1777
1778             case dump_status_fail:
1779               break;
1780
1781             case dump_status_not_implemented:
1782               abort ();
1783             }
1784
1785           ok = status == dump_status_ok;
1786         }
1787
1788       if (ok)
1789         {
1790           if (fd < 0)
1791             {
1792               errno = - fd;
1793               ok = false;
1794             }
1795           else if (fd == 0)
1796             {
1797               if (parentfd < 0 && ! top_level)
1798                 {
1799                   errno = - parentfd;
1800                   ok = false;
1801                 }
1802               else
1803                 ok = fstatat (parentfd, name, &final_stat, fstatat_flags) == 0;
1804             }
1805           else
1806             ok = fstat (fd, &final_stat) == 0;
1807
1808           if (! ok)
1809             file_removed_diag (p, top_level, stat_diag);
1810         }
1811
1812       if (ok)
1813         {
1814           if ((timespec_cmp (get_stat_ctime (&final_stat), original_ctime) != 0
1815                /* Original ctime will change if the file is a directory and
1816                   --remove-files is given */
1817                && !(remove_files_option && is_dir))
1818               || original_size < final_stat.st_size)
1819             {
1820               WARNOPT (WARN_FILE_CHANGED,
1821                        (0, 0, _("%s: file changed as we read it"),
1822                         quotearg_colon (p)));
1823               set_exit_status (TAREXIT_DIFFERS);
1824             }
1825           else if (atime_preserve_option == replace_atime_preserve
1826                    && fd && (is_dir || original_size != 0)
1827                    && set_file_atime (fd, parentfd, name, st->atime) != 0)
1828             utime_error (p);
1829         }
1830
1831       ok &= tar_stat_close (st);
1832       if (ok && remove_files_option)
1833         queue_deferred_unlink (p, is_dir);
1834
1835       return;
1836     }
1837 #ifdef HAVE_READLINK
1838   else if (S_ISLNK (st->stat.st_mode))
1839     {
1840       char *buffer;
1841       int size;
1842       size_t linklen = st->stat.st_size;
1843       if (linklen != st->stat.st_size || linklen + 1 == 0)
1844         xalloc_die ();
1845       buffer = (char *) alloca (linklen + 1);
1846       size = readlinkat (parentfd, name, buffer, linklen + 1);
1847       if (size < 0)
1848         {
1849           file_removed_diag (p, top_level, readlink_diag);
1850           return;
1851         }
1852       buffer[size] = '\0';
1853       assign_string (&st->link_name, buffer);
1854       transform_name (&st->link_name, XFORM_SYMLINK);
1855       if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) < size)
1856         write_long_link (st);
1857
1858       xattrs_selinux_get (parentfd, name, st, 0);
1859       xattrs_xattrs_get (parentfd, name, st, 0);
1860
1861       block_ordinal = current_block_ordinal ();
1862       st->stat.st_size = 0;     /* force 0 size on symlink */
1863       header = start_header (st);
1864       if (!header)
1865         return;
1866       tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE);
1867       header->header.typeflag = SYMTYPE;
1868       finish_header (st, header, block_ordinal);
1869       /* nothing more to do to it */
1870
1871       if (remove_files_option)
1872         queue_deferred_unlink (p, false);
1873
1874       file_count_links (st);
1875       return;
1876     }
1877 #endif
1878   else if (S_ISCHR (st->stat.st_mode))
1879     {
1880       type = CHRTYPE;
1881       xattrs_acls_get (parentfd, name, st, 0, true);
1882       xattrs_selinux_get (parentfd, name, st, 0);
1883       xattrs_xattrs_get (parentfd, name, st, 0);
1884     }
1885   else if (S_ISBLK (st->stat.st_mode))
1886     {
1887       type = BLKTYPE;
1888       xattrs_acls_get (parentfd, name, st, 0, true);
1889       xattrs_selinux_get (parentfd, name, st, 0);
1890       xattrs_xattrs_get (parentfd, name, st, 0);
1891     }
1892   else if (S_ISFIFO (st->stat.st_mode))
1893     {
1894       type = FIFOTYPE;
1895       xattrs_acls_get (parentfd, name, st, 0, true);
1896       xattrs_selinux_get (parentfd, name, st, 0);
1897       xattrs_xattrs_get (parentfd, name, st, 0);
1898     }
1899   else if (S_ISSOCK (st->stat.st_mode))
1900     {
1901       WARNOPT (WARN_FILE_IGNORED,
1902                (0, 0, _("%s: socket ignored"), quotearg_colon (p)));
1903       return;
1904     }
1905   else if (S_ISDOOR (st->stat.st_mode))
1906     {
1907       WARNOPT (WARN_FILE_IGNORED,
1908                (0, 0, _("%s: door ignored"), quotearg_colon (p)));
1909       return;
1910     }
1911   else
1912     {
1913       unknown_file_error (p);
1914       return;
1915     }
1916
1917   if (archive_format == V7_FORMAT)
1918     {
1919       unknown_file_error (p);
1920       return;
1921     }
1922
1923   block_ordinal = current_block_ordinal ();
1924   st->stat.st_size = 0; /* force 0 size */
1925   header = start_header (st);
1926   if (!header)
1927     return;
1928   header->header.typeflag = type;
1929
1930   if (type != FIFOTYPE)
1931     {
1932       MAJOR_TO_CHARS (major (st->stat.st_rdev),
1933                       header->header.devmajor);
1934       MINOR_TO_CHARS (minor (st->stat.st_rdev),
1935                       header->header.devminor);
1936     }
1937
1938   finish_header (st, header, block_ordinal);
1939   if (remove_files_option)
1940     queue_deferred_unlink (p, false);
1941 }
1942
1943 /* Dump a file, recursively.  PARENT describes the file's parent
1944    directory, NAME is the file's name relative to PARENT, and FULLNAME
1945    its full name, possibly relative to the working directory.  NAME
1946    may contain slashes at the top level of invocation.  */
1947
1948 void
1949 dump_file (struct tar_stat_info *parent, char const *name,
1950            char const *fullname)
1951 {
1952   struct tar_stat_info st;
1953   tar_stat_init (&st);
1954   st.parent = parent;
1955   dump_file0 (&st, name, fullname);
1956   if (parent && listed_incremental_option)
1957     update_parent_directory (parent);
1958   tar_stat_destroy (&st);
1959 }