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