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