Support for POSIX ACLs
[tar.git] / src / create.c
blob37a58089d6ffcfb6385e5d5c7384e1b9f9765f18
1 /* Create a tar archive.
3 Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2012
5 Free Software Foundation, Inc.
7 Written by John Gilmore, on 1985-08-25.
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3, or (at your option) any later
12 version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include <system.h>
25 #include <quotearg.h>
27 #include "common.h"
28 #include <hash.h>
30 /* Error number to use when an impostor is discovered.
31 Pretend the impostor isn't there. */
32 enum { IMPOSTOR_ERRNO = ENOENT };
34 struct link
36 dev_t dev;
37 ino_t ino;
38 nlink_t nlink;
39 char name[1];
42 struct exclusion_tag
44 const char *name;
45 size_t length;
46 enum exclusion_tag_type type;
47 bool (*predicate) (int fd);
48 struct exclusion_tag *next;
51 static struct exclusion_tag *exclusion_tags;
53 void
54 add_exclusion_tag (const char *name, enum exclusion_tag_type type,
55 bool (*predicate) (int fd))
57 struct exclusion_tag *tag = xmalloc (sizeof tag[0]);
58 tag->next = exclusion_tags;
59 tag->name = name;
60 tag->type = type;
61 tag->predicate = predicate;
62 tag->length = strlen (name);
63 exclusion_tags = tag;
66 void
67 exclusion_tag_warning (const char *dirname, const char *tagname,
68 const char *message)
70 if (verbose_option)
71 WARNOPT (WARN_CACHEDIR,
72 (0, 0,
73 _("%s: contains a cache directory tag %s; %s"),
74 quotearg_colon (dirname),
75 quotearg_n (1, tagname),
76 message));
79 enum exclusion_tag_type
80 check_exclusion_tags (struct tar_stat_info const *st, char const **tag_file_name)
82 struct exclusion_tag *tag;
84 for (tag = exclusion_tags; tag; tag = tag->next)
86 int tagfd = subfile_open (st, tag->name, open_read_flags);
87 if (0 <= tagfd)
89 bool satisfied = !tag->predicate || tag->predicate (tagfd);
90 close (tagfd);
91 if (satisfied)
93 if (tag_file_name)
94 *tag_file_name = tag->name;
95 return tag->type;
100 return exclusion_tag_none;
103 /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
104 contains a valid header, as described at:
105 http://www.brynosaurus.com/cachedir
106 Applications can write this file into directories they create
107 for use as caches containing purely regenerable, non-precious data,
108 allowing us to avoid archiving them if --exclude-caches is specified. */
110 #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
111 #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
113 bool
114 cachedir_file_p (int fd)
116 char tagbuf[CACHEDIR_SIGNATURE_SIZE];
118 return
119 (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE) == CACHEDIR_SIGNATURE_SIZE
120 && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0);
124 /* The maximum uintmax_t value that can be represented with DIGITS digits,
125 assuming that each digit is BITS_PER_DIGIT wide. */
126 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
127 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
128 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
129 : (uintmax_t) -1)
131 /* The maximum uintmax_t value that can be represented with octal
132 digits and a trailing NUL in BUFFER. */
133 #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
135 /* Convert VALUE to an octal representation suitable for tar headers.
136 Output to buffer WHERE with size SIZE.
137 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
139 static void
140 to_octal (uintmax_t value, char *where, size_t size)
142 uintmax_t v = value;
143 size_t i = size;
147 where[--i] = '0' + (v & ((1 << LG_8) - 1));
148 v >>= LG_8;
150 while (i);
153 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
154 NUL unless SRC is LEN or more bytes long. */
156 static void
157 tar_copy_str (char *dst, const char *src, size_t len)
159 size_t i;
160 for (i = 0; i < len; i++)
161 if (! (dst[i] = src[i]))
162 break;
165 /* Same as tar_copy_str, but always terminate with NUL if using
166 is OLDGNU format */
168 static void
169 tar_name_copy_str (char *dst, const char *src, size_t len)
171 tar_copy_str (dst, src, len);
172 if (archive_format == OLDGNU_FORMAT)
173 dst[len-1] = 0;
176 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
177 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
178 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
179 The result is undefined if SIZE is 0 or if VALUE is too large to
180 fit. */
182 static void
183 to_base256 (int negative, uintmax_t value, char *where, size_t size)
185 uintmax_t v = value;
186 uintmax_t propagated_sign_bits =
187 ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
188 size_t i = size;
192 where[--i] = v & ((1 << LG_256) - 1);
193 v = propagated_sign_bits | (v >> LG_256);
195 while (i);
198 #define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
199 #define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
200 #define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
201 #define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
202 #define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
204 #define UNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
205 #define GNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
207 static bool
208 to_chars (int negative, uintmax_t value, size_t valsize,
209 uintmax_t (*substitute) (int *),
210 char *where, size_t size, const char *type);
212 static bool
213 to_chars_subst (int negative, int gnu_format, uintmax_t value, size_t valsize,
214 uintmax_t (*substitute) (int *),
215 char *where, size_t size, const char *type)
217 uintmax_t maxval = (gnu_format
218 ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
219 : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
220 char valbuf[UINTMAX_STRSIZE_BOUND + 1];
221 char maxbuf[UINTMAX_STRSIZE_BOUND];
222 char minbuf[UINTMAX_STRSIZE_BOUND + 1];
223 char const *minval_string;
224 char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
225 char const *value_string;
227 if (gnu_format)
229 uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
230 char *p = STRINGIFY_BIGINT (m, minbuf + 1);
231 *--p = '-';
232 minval_string = p;
234 else
235 minval_string = "0";
237 if (negative)
239 char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
240 *--p = '-';
241 value_string = p;
243 else
244 value_string = STRINGIFY_BIGINT (value, valbuf);
246 if (substitute)
248 int negsub;
249 uintmax_t sub = substitute (&negsub) & maxval;
250 /* NOTE: This is one of the few places where GNU_FORMAT differs from
251 OLDGNU_FORMAT. The actual differences are:
253 1. In OLDGNU_FORMAT all strings in a tar header end in \0
254 2. Incremental archives use oldgnu_header.
256 Apart from this they are completely identical. */
257 uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
258 char subbuf[UINTMAX_STRSIZE_BOUND + 1];
259 char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
260 if (negsub)
261 *--sub_string = '-';
262 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
263 value_string, type, minval_string, maxval_string,
264 sub_string));
265 return to_chars (negsub, s, valsize, 0, where, size, type);
267 else
268 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
269 value_string, type, minval_string, maxval_string));
270 return false;
273 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
274 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
275 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
276 negative before being cast to uintmax_t; its original bitpattern
277 can be deduced from VALSIZE, its original size before casting.
278 TYPE is the kind of value being output (useful for diagnostics).
279 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
280 digits), followed by '\0'. If this won't work, and if GNU or
281 OLDGNU format is allowed, use '\200' followed by base-256, or (if
282 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
283 If neither format works, use SUBSTITUTE (...) instead. Pass to
284 SUBSTITUTE the address of an 0-or-1 flag recording whether the
285 substitute value is negative. */
287 static bool
288 to_chars (int negative, uintmax_t value, size_t valsize,
289 uintmax_t (*substitute) (int *),
290 char *where, size_t size, const char *type)
292 int gnu_format = (archive_format == GNU_FORMAT
293 || archive_format == OLDGNU_FORMAT);
295 /* Generate the POSIX octal representation if the number fits. */
296 if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
298 where[size - 1] = '\0';
299 to_octal (value, where, size - 1);
300 return true;
302 else if (gnu_format)
304 /* Try to cope with the number by using traditional GNU format
305 methods */
307 /* Generate the base-256 representation if the number fits. */
308 if (((negative ? -1 - value : value)
309 <= MAX_VAL_WITH_DIGITS (size - 1, LG_256)))
311 where[0] = negative ? -1 : 1 << (LG_256 - 1);
312 to_base256 (negative, value, where + 1, size - 1);
313 return true;
316 /* Otherwise, if the number is negative, and if it would not cause
317 ambiguity on this host by confusing positive with negative
318 values, then generate the POSIX octal representation of the value
319 modulo 2**(field bits). The resulting tar file is
320 machine-dependent, since it depends on the host word size. Yuck!
321 But this is the traditional behavior. */
322 else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
324 static int warned_once;
325 if (! warned_once)
327 warned_once = 1;
328 WARN ((0, 0, _("Generating negative octal headers")));
330 where[size - 1] = '\0';
331 to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
332 where, size - 1);
333 return true;
335 /* Otherwise fall back to substitution, if possible: */
337 else
338 substitute = NULL; /* No substitution for formats, other than GNU */
340 return to_chars_subst (negative, gnu_format, value, valsize, substitute,
341 where, size, type);
344 static uintmax_t
345 gid_substitute (int *negative)
347 gid_t r;
348 #ifdef GID_NOBODY
349 r = GID_NOBODY;
350 #else
351 static gid_t gid_nobody;
352 if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
353 gid_nobody = -2;
354 r = gid_nobody;
355 #endif
356 *negative = r < 0;
357 return r;
360 static bool
361 gid_to_chars (gid_t v, char *p, size_t s)
363 return to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
366 static bool
367 major_to_chars (major_t v, char *p, size_t s)
369 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
372 static bool
373 minor_to_chars (minor_t v, char *p, size_t s)
375 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
378 static bool
379 mode_to_chars (mode_t v, char *p, size_t s)
381 /* In the common case where the internal and external mode bits are the same,
382 and we are not using POSIX or GNU format,
383 propagate all unknown bits to the external mode.
384 This matches historical practice.
385 Otherwise, just copy the bits we know about. */
386 int negative;
387 uintmax_t u;
388 if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
389 && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
390 && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
391 && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
392 && archive_format != POSIX_FORMAT
393 && archive_format != USTAR_FORMAT
394 && archive_format != GNU_FORMAT)
396 negative = v < 0;
397 u = v;
399 else
401 negative = 0;
402 u = ((v & S_ISUID ? TSUID : 0)
403 | (v & S_ISGID ? TSGID : 0)
404 | (v & S_ISVTX ? TSVTX : 0)
405 | (v & S_IRUSR ? TUREAD : 0)
406 | (v & S_IWUSR ? TUWRITE : 0)
407 | (v & S_IXUSR ? TUEXEC : 0)
408 | (v & S_IRGRP ? TGREAD : 0)
409 | (v & S_IWGRP ? TGWRITE : 0)
410 | (v & S_IXGRP ? TGEXEC : 0)
411 | (v & S_IROTH ? TOREAD : 0)
412 | (v & S_IWOTH ? TOWRITE : 0)
413 | (v & S_IXOTH ? TOEXEC : 0));
415 return to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
418 bool
419 off_to_chars (off_t v, char *p, size_t s)
421 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
424 bool
425 time_to_chars (time_t v, char *p, size_t s)
427 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
430 static uintmax_t
431 uid_substitute (int *negative)
433 uid_t r;
434 #ifdef UID_NOBODY
435 r = UID_NOBODY;
436 #else
437 static uid_t uid_nobody;
438 if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
439 uid_nobody = -2;
440 r = uid_nobody;
441 #endif
442 *negative = r < 0;
443 return r;
446 static bool
447 uid_to_chars (uid_t v, char *p, size_t s)
449 return to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
452 static bool
453 uintmax_to_chars (uintmax_t v, char *p, size_t s)
455 return to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
458 static void
459 string_to_chars (char const *str, char *p, size_t s)
461 tar_copy_str (p, str, s);
462 p[s - 1] = '\0';
466 /* A directory is always considered dumpable.
467 Otherwise, only regular and contiguous files are considered dumpable.
468 Such a file is dumpable if it is sparse and both --sparse and --totals
469 are specified.
470 Otherwise, it is dumpable unless any of the following conditions occur:
472 a) it is empty *and* world-readable, or
473 b) current archive is /dev/null */
475 static bool
476 file_dumpable_p (struct stat const *st)
478 if (S_ISDIR (st->st_mode))
479 return true;
480 if (! (S_ISREG (st->st_mode) || S_ISCTG (st->st_mode)))
481 return false;
482 if (dev_null_output)
483 return totals_option && sparse_option && ST_IS_SPARSE (*st);
484 return ! (st->st_size == 0 && (st->st_mode & MODE_R) == MODE_R);
488 /* Writing routines. */
490 /* Write the EOT block(s). Zero at least two blocks, through the end
491 of the record. Old tar, as previous versions of GNU tar, writes
492 garbage after two zeroed blocks. */
493 void
494 write_eot (void)
496 union block *pointer = find_next_block ();
497 memset (pointer->buffer, 0, BLOCKSIZE);
498 set_next_block_after (pointer);
499 pointer = find_next_block ();
500 memset (pointer->buffer, 0, available_space_after (pointer));
501 set_next_block_after (pointer);
504 /* Write a "private" header */
505 union block *
506 start_private_header (const char *name, size_t size, time_t t)
508 union block *header = find_next_block ();
510 memset (header->buffer, 0, sizeof (union block));
512 tar_name_copy_str (header->header.name, name, NAME_FIELD_SIZE);
513 OFF_TO_CHARS (size, header->header.size);
515 TIME_TO_CHARS (t, header->header.mtime);
516 MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode);
517 UID_TO_CHARS (getuid (), header->header.uid);
518 GID_TO_CHARS (getgid (), header->header.gid);
519 MAJOR_TO_CHARS (0, header->header.devmajor);
520 MINOR_TO_CHARS (0, header->header.devminor);
521 strncpy (header->header.magic, TMAGIC, TMAGLEN);
522 strncpy (header->header.version, TVERSION, TVERSLEN);
523 return header;
526 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
527 the file name */
529 static union block *
530 write_short_name (struct tar_stat_info *st)
532 union block *header = find_next_block ();
533 memset (header->buffer, 0, sizeof (union block));
534 tar_name_copy_str (header->header.name, st->file_name, NAME_FIELD_SIZE);
535 return header;
538 #define FILL(field,byte) do { \
539 memset(field, byte, sizeof(field)-1); \
540 (field)[sizeof(field)-1] = 0; \
541 } while (0)
543 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
544 static void
545 write_gnu_long_link (struct tar_stat_info *st, const char *p, char type)
547 size_t size = strlen (p) + 1;
548 size_t bufsize;
549 union block *header;
550 char *tmpname;
552 header = start_private_header ("././@LongLink", size, time (NULL));
553 FILL (header->header.mtime, '0');
554 FILL (header->header.mode, '0');
555 FILL (header->header.uid, '0');
556 FILL (header->header.gid, '0');
557 FILL (header->header.devmajor, 0);
558 FILL (header->header.devminor, 0);
559 uid_to_uname (0, &tmpname);
560 UNAME_TO_CHARS (tmpname, header->header.uname);
561 free (tmpname);
562 gid_to_gname (0, &tmpname);
563 GNAME_TO_CHARS (tmpname, header->header.gname);
564 free (tmpname);
566 strcpy (header->buffer + offsetof (struct posix_header, magic),
567 OLDGNU_MAGIC);
568 header->header.typeflag = type;
569 finish_header (st, header, -1);
571 header = find_next_block ();
573 bufsize = available_space_after (header);
575 while (bufsize < size)
577 memcpy (header->buffer, p, bufsize);
578 p += bufsize;
579 size -= bufsize;
580 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
581 header = find_next_block ();
582 bufsize = available_space_after (header);
584 memcpy (header->buffer, p, size);
585 memset (header->buffer + size, 0, bufsize - size);
586 set_next_block_after (header + (size - 1) / BLOCKSIZE);
589 static size_t
590 split_long_name (const char *name, size_t length)
592 size_t i;
594 if (length > PREFIX_FIELD_SIZE + 1)
595 length = PREFIX_FIELD_SIZE + 1;
596 else if (ISSLASH (name[length - 1]))
597 length--;
598 for (i = length - 1; i > 0; i--)
599 if (ISSLASH (name[i]))
600 break;
601 return i;
604 static union block *
605 write_ustar_long_name (const char *name)
607 size_t length = strlen (name);
608 size_t i, nlen;
609 union block *header;
611 if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
613 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
614 quotearg_colon (name),
615 PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
616 return NULL;
619 i = split_long_name (name, length);
620 if (i == 0 || (nlen = length - i - 1) > NAME_FIELD_SIZE || nlen == 0)
622 ERROR ((0, 0,
623 _("%s: file name is too long (cannot be split); not dumped"),
624 quotearg_colon (name)));
625 return NULL;
628 header = find_next_block ();
629 memset (header->buffer, 0, sizeof (header->buffer));
630 memcpy (header->header.prefix, name, i);
631 memcpy (header->header.name, name + i + 1, length - i - 1);
633 return header;
636 /* Write a long link name, depending on the current archive format */
637 static void
638 write_long_link (struct tar_stat_info *st)
640 switch (archive_format)
642 case POSIX_FORMAT:
643 xheader_store ("linkpath", st, NULL);
644 break;
646 case V7_FORMAT: /* old V7 tar format */
647 case USTAR_FORMAT:
648 case STAR_FORMAT:
649 ERROR ((0, 0,
650 _("%s: link name is too long; not dumped"),
651 quotearg_colon (st->link_name)));
652 break;
654 case OLDGNU_FORMAT:
655 case GNU_FORMAT:
656 write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK);
657 break;
659 default:
660 abort(); /*FIXME*/
664 static union block *
665 write_long_name (struct tar_stat_info *st)
667 switch (archive_format)
669 case POSIX_FORMAT:
670 xheader_store ("path", st, NULL);
671 break;
673 case V7_FORMAT:
674 if (strlen (st->file_name) > NAME_FIELD_SIZE-1)
676 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
677 quotearg_colon (st->file_name),
678 NAME_FIELD_SIZE - 1));
679 return NULL;
681 break;
683 case USTAR_FORMAT:
684 case STAR_FORMAT:
685 return write_ustar_long_name (st->file_name);
687 case OLDGNU_FORMAT:
688 case GNU_FORMAT:
689 write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
690 break;
692 default:
693 abort(); /*FIXME*/
695 return write_short_name (st);
698 union block *
699 write_extended (bool global, struct tar_stat_info *st, union block *old_header)
701 union block *header, hp;
702 char *p;
703 int type;
704 time_t t;
706 if (st->xhdr.buffer || st->xhdr.stk == NULL)
707 return old_header;
709 xheader_finish (&st->xhdr);
710 memcpy (hp.buffer, old_header, sizeof (hp));
711 if (global)
713 type = XGLTYPE;
714 p = xheader_ghdr_name ();
715 time (&t);
717 else
719 type = XHDTYPE;
720 p = xheader_xhdr_name (st);
721 t = st->stat.st_mtime;
723 xheader_write (type, p, t, &st->xhdr);
724 free (p);
725 header = find_next_block ();
726 memcpy (header, &hp.buffer, sizeof (hp.buffer));
727 return header;
730 static union block *
731 write_header_name (struct tar_stat_info *st)
733 if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
735 xheader_store ("path", st, NULL);
736 return write_short_name (st);
738 else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
739 < strlen (st->file_name))
740 return write_long_name (st);
741 else
742 return write_short_name (st);
746 /* Header handling. */
748 /* Make a header block for the file whose stat info is st,
749 and return its address. */
751 union block *
752 start_header (struct tar_stat_info *st)
754 union block *header;
756 header = write_header_name (st);
757 if (!header)
758 return NULL;
760 /* Override some stat fields, if requested to do so. */
762 if (owner_option != (uid_t) -1)
763 st->stat.st_uid = owner_option;
764 if (group_option != (gid_t) -1)
765 st->stat.st_gid = group_option;
766 if (mode_option)
767 st->stat.st_mode =
768 ((st->stat.st_mode & ~MODE_ALL)
769 | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0,
770 initial_umask, mode_option, NULL));
772 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
773 for a few tars and came up with the following interoperability
774 matrix:
776 WRITER
777 1 2 3 4 5 6 7 8 9 READER
778 . . . . . . . . . 1 = SunOS 4.2 tar
779 # . . # # . . # # 2 = NEC SVR4.0.2 tar
780 . . . # # . . # . 3 = Solaris 2.1 tar
781 . . . . . . . . . 4 = GNU tar 1.11.1
782 . . . . . . . . . 5 = HP-UX 8.07 tar
783 . . . . . . . . . 6 = Ultrix 4.1
784 . . . . . . . . . 7 = AIX 3.2
785 . . . . . . . . . 8 = Hitachi HI-UX 1.03
786 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
788 . = works
789 # = "impossible file type"
791 The following mask for old archive removes the '#'s in column 4
792 above, thus making GNU tar both a universal donor and a universal
793 acceptor for Paul's test. */
795 if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
796 MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
797 else
798 MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
801 uid_t uid = st->stat.st_uid;
802 if (archive_format == POSIX_FORMAT
803 && MAX_OCTAL_VAL (header->header.uid) < uid)
805 xheader_store ("uid", st, NULL);
806 uid = 0;
808 if (!UID_TO_CHARS (uid, header->header.uid))
809 return NULL;
813 gid_t gid = st->stat.st_gid;
814 if (archive_format == POSIX_FORMAT
815 && MAX_OCTAL_VAL (header->header.gid) < gid)
817 xheader_store ("gid", st, NULL);
818 gid = 0;
820 if (!GID_TO_CHARS (gid, header->header.gid))
821 return NULL;
825 off_t size = st->stat.st_size;
826 if (archive_format == POSIX_FORMAT
827 && MAX_OCTAL_VAL (header->header.size) < size)
829 xheader_store ("size", st, NULL);
830 size = 0;
832 if (!OFF_TO_CHARS (size, header->header.size))
833 return NULL;
837 struct timespec mtime = set_mtime_option ? mtime_option : st->mtime;
838 if (archive_format == POSIX_FORMAT)
840 if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
841 || mtime.tv_nsec != 0)
842 xheader_store ("mtime", st, &mtime);
843 if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
844 mtime.tv_sec = 0;
846 if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime))
847 return NULL;
850 /* FIXME */
851 if (S_ISCHR (st->stat.st_mode)
852 || S_ISBLK (st->stat.st_mode))
854 major_t devmajor = major (st->stat.st_rdev);
855 minor_t devminor = minor (st->stat.st_rdev);
857 if (archive_format == POSIX_FORMAT
858 && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
860 xheader_store ("devmajor", st, NULL);
861 devmajor = 0;
863 if (!MAJOR_TO_CHARS (devmajor, header->header.devmajor))
864 return NULL;
866 if (archive_format == POSIX_FORMAT
867 && MAX_OCTAL_VAL (header->header.devminor) < devminor)
869 xheader_store ("devminor", st, NULL);
870 devminor = 0;
872 if (!MINOR_TO_CHARS (devminor, header->header.devminor))
873 return NULL;
875 else if (archive_format != GNU_FORMAT && archive_format != OLDGNU_FORMAT)
877 if (!(MAJOR_TO_CHARS (0, header->header.devmajor)
878 && MINOR_TO_CHARS (0, header->header.devminor)))
879 return NULL;
882 if (archive_format == POSIX_FORMAT)
884 xheader_store ("atime", st, NULL);
885 xheader_store ("ctime", st, NULL);
887 else if (incremental_option)
888 if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
890 TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
891 TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
894 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
896 switch (archive_format)
898 case V7_FORMAT:
899 break;
901 case OLDGNU_FORMAT:
902 case GNU_FORMAT: /*FIXME?*/
903 /* Overwrite header->header.magic and header.version in one blow. */
904 strcpy (header->buffer + offsetof (struct posix_header, magic),
905 OLDGNU_MAGIC);
906 break;
908 case POSIX_FORMAT:
909 case USTAR_FORMAT:
910 strncpy (header->header.magic, TMAGIC, TMAGLEN);
911 strncpy (header->header.version, TVERSION, TVERSLEN);
912 break;
914 default:
915 abort ();
918 if (archive_format == V7_FORMAT || numeric_owner_option)
920 /* header->header.[ug]name are left as the empty string. */
922 else
924 if (owner_name_option)
925 st->uname = xstrdup (owner_name_option);
926 else
927 uid_to_uname (st->stat.st_uid, &st->uname);
929 if (group_name_option)
930 st->gname = xstrdup (group_name_option);
931 else
932 gid_to_gname (st->stat.st_gid, &st->gname);
934 if (archive_format == POSIX_FORMAT
935 && (strlen (st->uname) > UNAME_FIELD_SIZE
936 || !string_ascii_p (st->uname)))
937 xheader_store ("uname", st, NULL);
938 UNAME_TO_CHARS (st->uname, header->header.uname);
940 if (archive_format == POSIX_FORMAT
941 && (strlen (st->gname) > GNAME_FIELD_SIZE
942 || !string_ascii_p (st->gname)))
943 xheader_store ("gname", st, NULL);
944 GNAME_TO_CHARS (st->gname, header->header.gname);
947 if (archive_format == POSIX_FORMAT)
949 if (acls_option > 0)
951 if (st->acls_a_ptr)
952 xheader_store ("SCHILY.acl.access", st, NULL);
953 if (st->acls_d_ptr)
954 xheader_store ("SCHILY.acl.default", st, NULL);
956 if (xattrs_option > 0)
958 size_t scan_xattr = 0;
959 struct xattr_array *xattr_map = st->xattr_map;
961 while (scan_xattr < st->xattr_map_size)
963 xheader_store (xattr_map[scan_xattr].xkey, st, &scan_xattr);
964 ++scan_xattr;
969 return header;
972 void
973 simple_finish_header (union block *header)
975 size_t i;
976 int sum;
977 char *p;
979 memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
981 sum = 0;
982 p = header->buffer;
983 for (i = sizeof *header; i-- != 0; )
984 /* We can't use unsigned char here because of old compilers, e.g. V7. */
985 sum += 0xFF & *p++;
987 /* Fill in the checksum field. It's formatted differently from the
988 other fields: it has [6] digits, a null, then a space -- rather than
989 digits, then a null. We use to_chars.
990 The final space is already there, from
991 checksumming, and to_chars doesn't modify it.
993 This is a fast way to do:
995 sprintf(header->header.chksum, "%6o", sum); */
997 uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
999 set_next_block_after (header);
1002 /* Finish off a filled-in header block and write it out. We also
1003 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
1004 is not negative, is the block ordinal of the first record for this
1005 file, which may be a preceding long name or long link record. */
1006 void
1007 finish_header (struct tar_stat_info *st,
1008 union block *header, off_t block_ordinal)
1010 /* Note: It is important to do this before the call to write_extended(),
1011 so that the actual ustar header is printed */
1012 if (verbose_option
1013 && header->header.typeflag != GNUTYPE_LONGLINK
1014 && header->header.typeflag != GNUTYPE_LONGNAME
1015 && header->header.typeflag != XHDTYPE
1016 && header->header.typeflag != XGLTYPE)
1018 /* FIXME: This global is used in print_header, sigh. */
1019 current_format = archive_format;
1020 print_header (st, header, block_ordinal);
1023 header = write_extended (false, st, header);
1024 simple_finish_header (header);
1028 void
1029 pad_archive (off_t size_left)
1031 union block *blk;
1032 while (size_left > 0)
1034 blk = find_next_block ();
1035 memset (blk->buffer, 0, BLOCKSIZE);
1036 set_next_block_after (blk);
1037 size_left -= BLOCKSIZE;
1041 static enum dump_status
1042 dump_regular_file (int fd, struct tar_stat_info *st)
1044 off_t size_left = st->stat.st_size;
1045 off_t block_ordinal;
1046 union block *blk;
1048 block_ordinal = current_block_ordinal ();
1049 blk = start_header (st);
1050 if (!blk)
1051 return dump_status_fail;
1053 /* Mark contiguous files, if we support them. */
1054 if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
1055 blk->header.typeflag = CONTTYPE;
1057 finish_header (st, blk, block_ordinal);
1059 mv_begin_write (st->file_name, st->stat.st_size, st->stat.st_size);
1060 while (size_left > 0)
1062 size_t bufsize, count;
1064 blk = find_next_block ();
1066 bufsize = available_space_after (blk);
1068 if (size_left < bufsize)
1070 /* Last read -- zero out area beyond. */
1071 bufsize = size_left;
1072 count = bufsize % BLOCKSIZE;
1073 if (count)
1074 memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1077 count = (fd <= 0) ? bufsize : blocking_read (fd, blk->buffer, bufsize);
1078 if (count == SAFE_READ_ERROR)
1080 read_diag_details (st->orig_file_name,
1081 st->stat.st_size - size_left, bufsize);
1082 pad_archive (size_left);
1083 return dump_status_short;
1085 size_left -= count;
1086 set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1088 if (count != bufsize)
1090 char buf[UINTMAX_STRSIZE_BOUND];
1091 memset (blk->buffer + count, 0, bufsize - count);
1092 WARNOPT (WARN_FILE_SHRANK,
1093 (0, 0,
1094 ngettext ("%s: File shrank by %s byte; padding with zeros",
1095 "%s: File shrank by %s bytes; padding with zeros",
1096 size_left),
1097 quotearg_colon (st->orig_file_name),
1098 STRINGIFY_BIGINT (size_left, buf)));
1099 if (! ignore_failed_read_option)
1100 set_exit_status (TAREXIT_DIFFERS);
1101 pad_archive (size_left - (bufsize - count));
1102 return dump_status_short;
1105 return dump_status_ok;
1109 /* Copy info from the directory identified by ST into the archive.
1110 DIRECTORY contains the directory's entries. */
1112 static void
1113 dump_dir0 (struct tar_stat_info *st, char const *directory)
1115 bool top_level = ! st->parent;
1116 const char *tag_file_name;
1117 union block *blk = NULL;
1118 off_t block_ordinal = current_block_ordinal ();
1120 st->stat.st_size = 0; /* force 0 size on dir */
1122 blk = start_header (st);
1123 if (!blk)
1124 return;
1126 if (incremental_option && archive_format != POSIX_FORMAT)
1127 blk->header.typeflag = GNUTYPE_DUMPDIR;
1128 else /* if (standard_option) */
1129 blk->header.typeflag = DIRTYPE;
1131 /* If we're gnudumping, we aren't done yet so don't close it. */
1133 if (!incremental_option)
1134 finish_header (st, blk, block_ordinal);
1135 else if (gnu_list_name->directory)
1137 if (archive_format == POSIX_FORMAT)
1139 xheader_store ("GNU.dumpdir", st,
1140 safe_directory_contents (gnu_list_name->directory));
1141 finish_header (st, blk, block_ordinal);
1143 else
1145 off_t size_left;
1146 off_t totsize;
1147 size_t bufsize;
1148 ssize_t count;
1149 const char *buffer, *p_buffer;
1151 block_ordinal = current_block_ordinal ();
1152 buffer = safe_directory_contents (gnu_list_name->directory);
1153 totsize = dumpdir_size (buffer);
1154 OFF_TO_CHARS (totsize, blk->header.size);
1155 finish_header (st, blk, block_ordinal);
1156 p_buffer = buffer;
1157 size_left = totsize;
1159 mv_begin_write (st->file_name, totsize, totsize);
1160 while (size_left > 0)
1162 blk = find_next_block ();
1163 bufsize = available_space_after (blk);
1164 if (size_left < bufsize)
1166 bufsize = size_left;
1167 count = bufsize % BLOCKSIZE;
1168 if (count)
1169 memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1171 memcpy (blk->buffer, p_buffer, bufsize);
1172 size_left -= bufsize;
1173 p_buffer += bufsize;
1174 set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1177 return;
1180 if (!recursion_option)
1181 return;
1183 if (one_file_system_option
1184 && !top_level
1185 && st->parent->stat.st_dev != st->stat.st_dev)
1187 if (verbose_option)
1188 WARNOPT (WARN_XDEV,
1189 (0, 0,
1190 _("%s: file is on a different filesystem; not dumped"),
1191 quotearg_colon (st->orig_file_name)));
1193 else
1195 char *name_buf;
1196 size_t name_size;
1198 switch (check_exclusion_tags (st, &tag_file_name))
1200 case exclusion_tag_all:
1201 /* Handled in dump_file0 */
1202 break;
1204 case exclusion_tag_none:
1206 char const *entry;
1207 size_t entry_len;
1208 size_t name_len;
1210 name_buf = xstrdup (st->orig_file_name);
1211 name_size = name_len = strlen (name_buf);
1213 /* Now output all the files in the directory. */
1214 for (entry = directory; (entry_len = strlen (entry)) != 0;
1215 entry += entry_len + 1)
1217 if (name_size < name_len + entry_len)
1219 name_size = name_len + entry_len;
1220 name_buf = xrealloc (name_buf, name_size + 1);
1222 strcpy (name_buf + name_len, entry);
1223 if (!excluded_name (name_buf))
1224 dump_file (st, entry, name_buf);
1227 free (name_buf);
1229 break;
1231 case exclusion_tag_contents:
1232 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1233 _("contents not dumped"));
1234 name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1;
1235 name_buf = xmalloc (name_size);
1236 strcpy (name_buf, st->orig_file_name);
1237 strcat (name_buf, tag_file_name);
1238 dump_file (st, tag_file_name, name_buf);
1239 free (name_buf);
1240 break;
1242 case exclusion_tag_under:
1243 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1244 _("contents not dumped"));
1245 break;
1250 /* Ensure exactly one trailing slash. */
1251 static void
1252 ensure_slash (char **pstr)
1254 size_t len = strlen (*pstr);
1255 while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
1256 len--;
1257 if (!ISSLASH ((*pstr)[len]))
1258 *pstr = xrealloc (*pstr, len + 2);
1259 (*pstr)[len++] = '/';
1260 (*pstr)[len] = '\0';
1263 /* If we just ran out of file descriptors, release a file descriptor
1264 in the directory chain somewhere leading from DIR->parent->parent
1265 up through the root. Return true if successful, false (preserving
1266 errno == EMFILE) otherwise.
1268 Do not release DIR's file descriptor, or DIR's parent, as other
1269 code assumes that they work. On some operating systems, another
1270 process can claim file descriptor resources as we release them, and
1271 some calls or their emulations require multiple file descriptors,
1272 so callers should not give up if a single release doesn't work. */
1274 static bool
1275 open_failure_recover (struct tar_stat_info const *dir)
1277 if (errno == EMFILE && dir && dir->parent)
1279 struct tar_stat_info *p;
1280 for (p = dir->parent->parent; p; p = p->parent)
1281 if (0 < p->fd && (! p->parent || p->parent->fd <= 0))
1283 tar_stat_close (p);
1284 return true;
1286 errno = EMFILE;
1289 return false;
1292 /* Return the directory entries of ST, in a dynamically allocated buffer,
1293 each entry followed by '\0' and the last followed by an extra '\0'.
1294 Return null on failure, setting errno. */
1295 char *
1296 get_directory_entries (struct tar_stat_info *st)
1298 while (! (st->dirstream = fdopendir (st->fd)))
1299 if (! open_failure_recover (st))
1300 return 0;
1301 return streamsavedir (st->dirstream);
1304 /* Dump the directory ST. Return true if successful, false (emitting
1305 diagnostics) otherwise. Get ST's entries, recurse through its
1306 subdirectories, and clean up file descriptors afterwards. */
1307 static bool
1308 dump_dir (struct tar_stat_info *st)
1310 char *directory = get_directory_entries (st);
1311 if (! directory)
1313 savedir_diag (st->orig_file_name);
1314 return false;
1317 dump_dir0 (st, directory);
1319 restore_parent_fd (st);
1320 free (directory);
1321 return true;
1325 /* Number of links a file can have without having to be entered into
1326 the link table. Typically this is 1, but in trickier circumstances
1327 it is 0. */
1328 static nlink_t trivial_link_count;
1331 /* Main functions of this module. */
1333 void
1334 create_archive (void)
1336 struct name const *p;
1338 trivial_link_count = name_count <= 1 && ! dereference_option;
1340 open_archive (ACCESS_WRITE);
1341 buffer_write_global_xheader ();
1343 if (incremental_option)
1345 size_t buffer_size = 1000;
1346 char *buffer = xmalloc (buffer_size);
1347 const char *q;
1349 collect_and_sort_names ();
1351 while ((p = name_from_list ()) != NULL)
1352 if (!excluded_name (p->name))
1353 dump_file (0, p->name, p->name);
1355 blank_name_list ();
1356 while ((p = name_from_list ()) != NULL)
1357 if (!excluded_name (p->name))
1359 struct tar_stat_info st;
1360 size_t plen = strlen (p->name);
1361 if (buffer_size <= plen)
1363 while ((buffer_size *= 2) <= plen)
1364 continue;
1365 buffer = xrealloc (buffer, buffer_size);
1367 memcpy (buffer, p->name, plen);
1368 if (! ISSLASH (buffer[plen - 1]))
1369 buffer[plen++] = DIRECTORY_SEPARATOR;
1370 tar_stat_init (&st);
1371 q = directory_contents (gnu_list_name->directory);
1372 if (q)
1373 while (*q)
1375 size_t qlen = strlen (q);
1376 if (*q == 'Y')
1378 if (! st.orig_file_name)
1380 int fd = openat (chdir_fd, p->name,
1381 open_searchdir_flags);
1382 if (fd < 0)
1384 open_diag (p->name);
1385 break;
1387 st.fd = fd;
1388 if (fstat (fd, &st.stat) != 0)
1390 stat_diag (p->name);
1391 break;
1393 st.orig_file_name = xstrdup (p->name);
1395 if (buffer_size < plen + qlen)
1397 while ((buffer_size *=2 ) < plen + qlen)
1398 continue;
1399 buffer = xrealloc (buffer, buffer_size);
1401 strcpy (buffer + plen, q + 1);
1402 dump_file (&st, q + 1, buffer);
1404 q += qlen + 1;
1406 tar_stat_destroy (&st);
1408 free (buffer);
1410 else
1412 const char *name;
1413 while ((name = name_next (1)) != NULL)
1414 if (!excluded_name (name))
1415 dump_file (0, name, name);
1418 write_eot ();
1419 close_archive ();
1420 finish_deferred_unlinks ();
1421 if (listed_incremental_option)
1422 write_directory_file ();
1426 /* Calculate the hash of a link. */
1427 static size_t
1428 hash_link (void const *entry, size_t n_buckets)
1430 struct link const *l = entry;
1431 uintmax_t num = l->dev ^ l->ino;
1432 return num % n_buckets;
1435 /* Compare two links for equality. */
1436 static bool
1437 compare_links (void const *entry1, void const *entry2)
1439 struct link const *link1 = entry1;
1440 struct link const *link2 = entry2;
1441 return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
1444 static void
1445 unknown_file_error (char const *p)
1447 WARNOPT (WARN_FILE_IGNORED,
1448 (0, 0, _("%s: Unknown file type; file ignored"),
1449 quotearg_colon (p)));
1450 if (!ignore_failed_read_option)
1451 set_exit_status (TAREXIT_FAILURE);
1455 /* Handling of hard links */
1457 /* Table of all non-directories that we've written so far. Any time
1458 we see another, we check the table and avoid dumping the data
1459 again if we've done it once already. */
1460 static Hash_table *link_table;
1462 /* Try to dump stat as a hard link to another file in the archive.
1463 Return true if successful. */
1464 static bool
1465 dump_hard_link (struct tar_stat_info *st)
1467 if (link_table
1468 && (trivial_link_count < st->stat.st_nlink || remove_files_option))
1470 struct link lp;
1471 struct link *duplicate;
1472 off_t block_ordinal;
1473 union block *blk;
1475 lp.ino = st->stat.st_ino;
1476 lp.dev = st->stat.st_dev;
1478 if ((duplicate = hash_lookup (link_table, &lp)))
1480 /* We found a link. */
1481 char const *link_name = safer_name_suffix (duplicate->name, true,
1482 absolute_names_option);
1484 duplicate->nlink--;
1486 block_ordinal = current_block_ordinal ();
1487 assign_string (&st->link_name, link_name);
1488 if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
1489 < strlen (link_name))
1490 write_long_link (st);
1492 st->stat.st_size = 0;
1493 blk = start_header (st);
1494 if (!blk)
1495 return false;
1496 tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
1498 blk->header.typeflag = LNKTYPE;
1499 finish_header (st, blk, block_ordinal);
1501 if (remove_files_option)
1502 queue_deferred_unlink (st->orig_file_name, false);
1504 return true;
1507 return false;
1510 static void
1511 file_count_links (struct tar_stat_info *st)
1513 if (hard_dereference_option)
1514 return;
1515 if (trivial_link_count < st->stat.st_nlink)
1517 struct link *duplicate;
1518 char *linkname = NULL;
1519 struct link *lp;
1521 assign_string (&linkname, st->orig_file_name);
1522 transform_name (&linkname, XFORM_LINK);
1524 lp = xmalloc (offsetof (struct link, name)
1525 + strlen (linkname) + 1);
1526 lp->ino = st->stat.st_ino;
1527 lp->dev = st->stat.st_dev;
1528 lp->nlink = st->stat.st_nlink;
1529 strcpy (lp->name, linkname);
1530 free (linkname);
1532 if (! ((link_table
1533 || (link_table = hash_initialize (0, 0, hash_link,
1534 compare_links, 0)))
1535 && (duplicate = hash_insert (link_table, lp))))
1536 xalloc_die ();
1538 if (duplicate != lp)
1539 abort ();
1540 lp->nlink--;
1544 /* For each dumped file, check if all its links were dumped. Emit
1545 warnings if it is not so. */
1546 void
1547 check_links (void)
1549 struct link *lp;
1551 if (!link_table)
1552 return;
1554 for (lp = hash_get_first (link_table); lp;
1555 lp = hash_get_next (link_table, lp))
1557 if (lp->nlink)
1559 WARN ((0, 0, _("Missing links to %s."), quote (lp->name)));
1564 /* Assuming DIR is the working directory, open FILE, using FLAGS to
1565 control the open. A null DIR means to use ".". If we are low on
1566 file descriptors, try to release one or more from DIR's parents to
1567 reuse it. */
1569 subfile_open (struct tar_stat_info const *dir, char const *file, int flags)
1571 int fd;
1573 static bool initialized;
1574 if (! initialized)
1576 /* Initialize any tables that might be needed when file
1577 descriptors are exhausted, and whose initialization might
1578 require a file descriptor. This includes the system message
1579 catalog and tar's message catalog. */
1580 initialized = true;
1581 strerror (ENOENT);
1582 gettext ("");
1585 while ((fd = openat (dir ? dir->fd : chdir_fd, file, flags)) < 0
1586 && open_failure_recover (dir))
1587 continue;
1588 return fd;
1591 /* Restore the file descriptor for ST->parent, if it was temporarily
1592 closed to conserve file descriptors. On failure, set the file
1593 descriptor to the negative of the corresponding errno value. Call
1594 this every time a subdirectory is ascended from. */
1595 void
1596 restore_parent_fd (struct tar_stat_info const *st)
1598 struct tar_stat_info *parent = st->parent;
1599 if (parent && ! parent->fd)
1601 int parentfd = openat (st->fd, "..", open_searchdir_flags);
1602 struct stat parentstat;
1604 if (parentfd < 0)
1605 parentfd = - errno;
1606 else if (! (fstat (parentfd, &parentstat) == 0
1607 && parent->stat.st_ino == parentstat.st_ino
1608 && parent->stat.st_dev == parentstat.st_dev))
1610 close (parentfd);
1611 parentfd = IMPOSTOR_ERRNO;
1614 if (parentfd < 0)
1616 int origfd = openat (chdir_fd, parent->orig_file_name,
1617 open_searchdir_flags);
1618 if (0 <= origfd)
1620 if (fstat (parentfd, &parentstat) == 0
1621 && parent->stat.st_ino == parentstat.st_ino
1622 && parent->stat.st_dev == parentstat.st_dev)
1623 parentfd = origfd;
1624 else
1625 close (origfd);
1629 parent->fd = parentfd;
1633 /* Dump a single file, recursing on directories. ST is the file's
1634 status info, NAME its name relative to the parent directory, and P
1635 its full name (which may be relative to the working directory). */
1637 /* FIXME: One should make sure that for *every* path leading to setting
1638 exit_status to failure, a clear diagnostic has been issued. */
1640 static void
1641 dump_file0 (struct tar_stat_info *st, char const *name, char const *p)
1643 union block *header;
1644 char type;
1645 off_t original_size;
1646 struct timespec original_ctime;
1647 off_t block_ordinal = -1;
1648 int fd = 0;
1649 bool is_dir;
1650 struct tar_stat_info const *parent = st->parent;
1651 bool top_level = ! parent;
1652 int parentfd = top_level ? chdir_fd : parent->fd;
1653 void (*diag) (char const *) = 0;
1655 if (interactive_option && !confirm ("add", p))
1656 return;
1658 assign_string (&st->orig_file_name, p);
1659 assign_string (&st->file_name,
1660 safer_name_suffix (p, false, absolute_names_option));
1662 transform_name (&st->file_name, XFORM_REGFILE);
1664 if (parentfd < 0 && ! top_level)
1666 errno = - parentfd;
1667 diag = open_diag;
1669 else if (fstatat (parentfd, name, &st->stat, fstatat_flags) != 0)
1670 diag = stat_diag;
1671 else if (file_dumpable_p (&st->stat))
1673 fd = subfile_open (parent, name, open_read_flags);
1674 if (fd < 0)
1675 diag = open_diag;
1676 else
1678 st->fd = fd;
1679 if (fstat (fd, &st->stat) != 0)
1680 diag = stat_diag;
1683 if (diag)
1685 file_removed_diag (p, top_level, diag);
1686 return;
1689 st->archive_file_size = original_size = st->stat.st_size;
1690 st->atime = get_stat_atime (&st->stat);
1691 st->mtime = get_stat_mtime (&st->stat);
1692 st->ctime = original_ctime = get_stat_ctime (&st->stat);
1694 #ifdef S_ISHIDDEN
1695 if (S_ISHIDDEN (st->stat.st_mode))
1697 char *new = (char *) alloca (strlen (p) + 2);
1698 if (new)
1700 strcpy (new, p);
1701 strcat (new, "@");
1702 p = new;
1705 #endif
1707 /* See if we want only new files, and check if this one is too old to
1708 put in the archive.
1710 This check is omitted if incremental_option is set *and* the
1711 requested file is not explicitly listed in the command line. */
1713 if (! (incremental_option && ! top_level)
1714 && !S_ISDIR (st->stat.st_mode)
1715 && OLDER_TAR_STAT_TIME (*st, m)
1716 && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
1718 if (!incremental_option && verbose_option)
1719 WARNOPT (WARN_FILE_UNCHANGED,
1720 (0, 0, _("%s: file is unchanged; not dumped"),
1721 quotearg_colon (p)));
1722 return;
1725 /* See if we are trying to dump the archive. */
1726 if (sys_file_is_archive (st))
1728 WARNOPT (WARN_IGNORE_ARCHIVE,
1729 (0, 0, _("%s: file is the archive; not dumped"),
1730 quotearg_colon (p)));
1731 return;
1734 is_dir = S_ISDIR (st->stat.st_mode) != 0;
1736 if (!is_dir && dump_hard_link (st))
1737 return;
1739 if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode))
1741 bool ok;
1742 struct stat final_stat;
1744 xattrs_acls_get (parentfd, name, st, 0, !is_dir);
1745 xattrs_xattrs_get (parentfd, name, st, fd);
1747 if (is_dir)
1749 const char *tag_file_name;
1750 ensure_slash (&st->orig_file_name);
1751 ensure_slash (&st->file_name);
1753 if (check_exclusion_tags (st, &tag_file_name) == exclusion_tag_all)
1755 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1756 _("directory not dumped"));
1757 return;
1760 ok = dump_dir (st);
1762 fd = st->fd;
1763 parentfd = top_level ? chdir_fd : parent->fd;
1765 else
1767 enum dump_status status;
1769 if (fd && sparse_option && ST_IS_SPARSE (st->stat))
1771 status = sparse_dump_file (fd, st);
1772 if (status == dump_status_not_implemented)
1773 status = dump_regular_file (fd, st);
1775 else
1776 status = dump_regular_file (fd, st);
1778 switch (status)
1780 case dump_status_ok:
1781 case dump_status_short:
1782 file_count_links (st);
1783 break;
1785 case dump_status_fail:
1786 break;
1788 case dump_status_not_implemented:
1789 abort ();
1792 ok = status == dump_status_ok;
1795 if (ok)
1797 if (fd < 0)
1799 errno = - fd;
1800 ok = false;
1802 else if (fd == 0)
1804 if (parentfd < 0 && ! top_level)
1806 errno = - parentfd;
1807 ok = false;
1809 else
1810 ok = fstatat (parentfd, name, &final_stat, fstatat_flags) == 0;
1812 else
1813 ok = fstat (fd, &final_stat) == 0;
1815 if (! ok)
1816 file_removed_diag (p, top_level, stat_diag);
1819 if (ok)
1821 if ((timespec_cmp (get_stat_ctime (&final_stat), original_ctime) != 0
1822 /* Original ctime will change if the file is a directory and
1823 --remove-files is given */
1824 && !(remove_files_option && is_dir))
1825 || original_size < final_stat.st_size)
1827 WARNOPT (WARN_FILE_CHANGED,
1828 (0, 0, _("%s: file changed as we read it"),
1829 quotearg_colon (p)));
1830 set_exit_status (TAREXIT_DIFFERS);
1832 else if (atime_preserve_option == replace_atime_preserve
1833 && fd && (is_dir || original_size != 0)
1834 && set_file_atime (fd, parentfd, name, st->atime) != 0)
1835 utime_error (p);
1838 ok &= tar_stat_close (st);
1839 if (ok && remove_files_option)
1840 queue_deferred_unlink (p, is_dir);
1842 return;
1844 #ifdef HAVE_READLINK
1845 else if (S_ISLNK (st->stat.st_mode))
1847 char *buffer;
1848 int size;
1849 size_t linklen = st->stat.st_size;
1850 if (linklen != st->stat.st_size || linklen + 1 == 0)
1851 xalloc_die ();
1852 buffer = (char *) alloca (linklen + 1);
1853 size = readlinkat (parentfd, name, buffer, linklen + 1);
1854 if (size < 0)
1856 file_removed_diag (p, top_level, readlink_diag);
1857 return;
1859 buffer[size] = '\0';
1860 assign_string (&st->link_name, buffer);
1861 transform_name (&st->link_name, XFORM_SYMLINK);
1862 if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) < size)
1863 write_long_link (st);
1865 xattrs_xattrs_get (parentfd, name, st, 0);
1867 block_ordinal = current_block_ordinal ();
1868 st->stat.st_size = 0; /* force 0 size on symlink */
1869 header = start_header (st);
1870 if (!header)
1871 return;
1872 tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE);
1873 header->header.typeflag = SYMTYPE;
1874 finish_header (st, header, block_ordinal);
1875 /* nothing more to do to it */
1877 if (remove_files_option)
1878 queue_deferred_unlink (p, false);
1880 file_count_links (st);
1881 return;
1883 #endif
1884 else if (S_ISCHR (st->stat.st_mode))
1886 type = CHRTYPE;
1887 xattrs_acls_get (parentfd, name, st, 0, true);
1888 xattrs_xattrs_get (parentfd, name, st, 0);
1890 else if (S_ISBLK (st->stat.st_mode))
1892 type = BLKTYPE;
1893 xattrs_acls_get (parentfd, name, st, 0, true);
1894 xattrs_xattrs_get (parentfd, name, st, 0);
1896 else if (S_ISFIFO (st->stat.st_mode))
1898 type = FIFOTYPE;
1899 xattrs_acls_get (parentfd, name, st, 0, true);
1900 xattrs_xattrs_get (parentfd, name, st, 0);
1902 else if (S_ISSOCK (st->stat.st_mode))
1904 WARNOPT (WARN_FILE_IGNORED,
1905 (0, 0, _("%s: socket ignored"), quotearg_colon (p)));
1906 return;
1908 else if (S_ISDOOR (st->stat.st_mode))
1910 WARNOPT (WARN_FILE_IGNORED,
1911 (0, 0, _("%s: door ignored"), quotearg_colon (p)));
1912 return;
1914 else
1916 unknown_file_error (p);
1917 return;
1920 if (archive_format == V7_FORMAT)
1922 unknown_file_error (p);
1923 return;
1926 block_ordinal = current_block_ordinal ();
1927 st->stat.st_size = 0; /* force 0 size */
1928 header = start_header (st);
1929 if (!header)
1930 return;
1931 header->header.typeflag = type;
1933 if (type != FIFOTYPE)
1935 MAJOR_TO_CHARS (major (st->stat.st_rdev),
1936 header->header.devmajor);
1937 MINOR_TO_CHARS (minor (st->stat.st_rdev),
1938 header->header.devminor);
1941 finish_header (st, header, block_ordinal);
1942 if (remove_files_option)
1943 queue_deferred_unlink (p, false);
1946 /* Dump a file, recursively. PARENT describes the file's parent
1947 directory, NAME is the file's name relative to PARENT, and FULLNAME
1948 its full name, possibly relative to the working directory. NAME
1949 may contain slashes at the top level of invocation. */
1951 void
1952 dump_file (struct tar_stat_info *parent, char const *name,
1953 char const *fullname)
1955 struct tar_stat_info st;
1956 tar_stat_init (&st);
1957 st.parent = parent;
1958 dump_file0 (&st, name, fullname);
1959 if (parent && listed_incremental_option)
1960 update_parent_directory (parent);
1961 tar_stat_destroy (&st);