New option: --set-mtime-command
[tar.git] / src / create.c
blob29a747bd8bb4cfcd00a668b0b44608f690fc2fc5
1 /* Create a tar archive.
3 Copyright 1985-2023 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 Written by John Gilmore, on 1985-08-25. */
22 #include <system.h>
24 #include <areadlink.h>
25 #include <flexmember.h>
26 #include <quotearg.h>
28 #include "common.h"
29 #include <hash.h>
31 /* Error number to use when an impostor is discovered.
32 Pretend the impostor isn't there. */
33 enum { IMPOSTOR_ERRNO = ENOENT };
35 struct link
37 dev_t dev;
38 ino_t ino;
39 nlink_t nlink;
40 char name[FLEXIBLE_ARRAY_MEMBER];
43 struct exclusion_tag
45 const char *name;
46 size_t length;
47 enum exclusion_tag_type type;
48 bool (*predicate) (int fd);
49 struct exclusion_tag *next;
52 static struct exclusion_tag *exclusion_tags;
54 void
55 add_exclusion_tag (const char *name, enum exclusion_tag_type type,
56 bool (*predicate) (int fd))
58 struct exclusion_tag *tag = xmalloc (sizeof tag[0]);
59 tag->next = exclusion_tags;
60 tag->name = name;
61 tag->type = type;
62 tag->predicate = predicate;
63 tag->length = strlen (name);
64 exclusion_tags = tag;
67 void
68 exclusion_tag_warning (const char *dirname, const char *tagname,
69 const char *message)
71 if (verbose_option)
72 WARNOPT (WARN_CACHEDIR,
73 (0, 0,
74 _("%s: contains a cache directory tag %s; %s"),
75 quotearg_colon (dirname),
76 quotearg_n (1, tagname),
77 message));
80 enum exclusion_tag_type
81 check_exclusion_tags (struct tar_stat_info const *st, char const **tag_file_name)
83 struct exclusion_tag *tag;
85 for (tag = exclusion_tags; tag; tag = tag->next)
87 int tagfd = subfile_open (st, tag->name, open_read_flags);
88 if (0 <= tagfd)
90 bool satisfied = !tag->predicate || tag->predicate (tagfd);
91 close (tagfd);
92 if (satisfied)
94 if (tag_file_name)
95 *tag_file_name = tag->name;
96 return tag->type;
101 return exclusion_tag_none;
104 /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
105 contains a valid header, as described at:
106 http://www.brynosaurus.com/cachedir
107 Applications can write this file into directories they create
108 for use as caches containing purely regenerable, non-precious data,
109 allowing us to avoid archiving them if --exclude-caches is specified. */
111 #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
112 #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
114 bool
115 cachedir_file_p (int fd)
117 char tagbuf[CACHEDIR_SIGNATURE_SIZE];
119 return
120 (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE) == CACHEDIR_SIGNATURE_SIZE
121 && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0);
125 /* The maximum uintmax_t value that can be represented with DIGITS digits,
126 assuming that each digit is BITS_PER_DIGIT wide. */
127 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
128 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
129 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
130 : (uintmax_t) -1)
132 /* The maximum uintmax_t value that can be represented with octal
133 digits and a trailing NUL in BUFFER. */
134 #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
136 /* Convert VALUE to an octal representation suitable for tar headers.
137 Output to buffer WHERE with size SIZE.
138 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
140 static void
141 to_octal (uintmax_t value, char *where, size_t size)
143 uintmax_t v = value;
144 size_t i = size;
148 where[--i] = '0' + (v & ((1 << LG_8) - 1));
149 v >>= LG_8;
151 while (i);
154 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
155 NUL unless SRC is LEN or more bytes long. */
157 static void
158 tar_copy_str (char *dst, const char *src, size_t len)
160 size_t i;
161 for (i = 0; i < len; i++)
162 if (! (dst[i] = src[i]))
163 break;
166 /* Same as tar_copy_str, but always terminate with NUL if using
167 is OLDGNU format */
169 static void
170 tar_name_copy_str (char *dst, const char *src, size_t len)
172 tar_copy_str (dst, src, len);
173 if (archive_format == OLDGNU_FORMAT)
174 dst[len-1] = 0;
177 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
178 tar headers. NEGATIVE is true iff VALUE was negative before being
179 cast to uintmax_t. Output to buffer WHERE with size SIZE.
180 The result is undefined if SIZE is 0 or if VALUE is too large to
181 fit. */
183 static void
184 to_base256 (bool negative, uintmax_t value, char *where, size_t size)
186 uintmax_t v = value;
187 uintmax_t propagated_sign_bits =
188 ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
189 size_t i = size;
193 where[--i] = v & ((1 << LG_256) - 1);
194 v = propagated_sign_bits | (v >> LG_256);
196 while (i);
199 #define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
200 #define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
201 #define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
202 #define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
203 #define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
205 #define UNAME_TO_CHARS(name, buf) string_to_chars (name, buf, sizeof (buf))
206 #define GNAME_TO_CHARS(name, buf) string_to_chars (name, buf, sizeof (buf))
208 static bool
209 to_chars (bool negative, uintmax_t value, size_t valsize,
210 uintmax_t (*substitute) (bool *),
211 char *where, size_t size, const char *type);
213 static bool
214 to_chars_subst (bool negative, bool gnu_format, uintmax_t value, size_t valsize,
215 uintmax_t (*substitute) (bool *),
216 char *where, size_t size, const char *type)
218 uintmax_t maxval = (gnu_format
219 ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
220 : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
221 char valbuf[UINTMAX_STRSIZE_BOUND + 1];
222 char maxbuf[UINTMAX_STRSIZE_BOUND];
223 char minbuf[UINTMAX_STRSIZE_BOUND + 1];
224 char const *minval_string;
225 char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
226 char const *value_string;
228 if (gnu_format)
230 uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
231 char *p = STRINGIFY_BIGINT (m, minbuf + 1);
232 *--p = '-';
233 minval_string = p;
235 else
236 minval_string = "0";
238 if (negative)
240 char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
241 *--p = '-';
242 value_string = p;
244 else
245 value_string = STRINGIFY_BIGINT (value, valbuf);
247 if (substitute)
249 bool negsub;
250 uintmax_t sub = substitute (&negsub) & maxval;
251 /* NOTE: This is one of the few places where GNU_FORMAT differs from
252 OLDGNU_FORMAT. The actual differences are:
254 1. In OLDGNU_FORMAT all strings in a tar header end in \0
255 2. Incremental archives use oldgnu_header.
257 Apart from this they are completely identical. */
258 uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
259 char subbuf[UINTMAX_STRSIZE_BOUND + 1];
260 char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
261 if (negsub)
262 *--sub_string = '-';
263 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
264 value_string, type, minval_string, maxval_string,
265 sub_string));
266 return to_chars (negsub, s, valsize, 0, where, size, type);
268 else
269 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
270 value_string, type, minval_string, maxval_string));
271 return false;
274 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
275 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
276 to buffer WHERE with size SIZE. NEGATIVE is true iff VALUE was
277 negative before being cast to uintmax_t; its original bitpattern
278 can be deduced from VALSIZE, its original size before casting.
279 TYPE is the kind of value being output (useful for diagnostics).
280 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
281 digits), followed by '\0'. If this won't work, and if GNU or
282 OLDGNU format is allowed, use '\200' followed by base-256, or (if
283 NEGATIVE) '\377' followed by two's complement base-256.
284 If neither format works, use SUBSTITUTE (...) instead. Pass to
285 SUBSTITUTE the address of an 0-or-1 flag recording whether the
286 substitute value is negative. */
288 static bool
289 to_chars (bool negative, uintmax_t value, size_t valsize,
290 uintmax_t (*substitute) (bool *),
291 char *where, size_t size, const char *type)
293 bool gnu_format = (archive_format == GNU_FORMAT
294 || archive_format == OLDGNU_FORMAT);
296 /* Generate the POSIX octal representation if the number fits. */
297 if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
299 where[size - 1] = '\0';
300 to_octal (value, where, size - 1);
301 return true;
303 else if (gnu_format)
305 /* Try to cope with the number by using traditional GNU format
306 methods */
308 /* Generate the base-256 representation if the number fits. */
309 if (((negative ? -1 - value : value)
310 <= MAX_VAL_WITH_DIGITS (size - 1, LG_256)))
312 where[0] = (char) (negative ? -1 : 1 << (LG_256 - 1));
313 to_base256 (negative, value, where + 1, size - 1);
314 return true;
317 /* Otherwise, if the number is negative, and if it would not cause
318 ambiguity on this host by confusing positive with negative
319 values, then generate the POSIX octal representation of the value
320 modulo 2**(field bits). The resulting tar file is
321 machine-dependent, since it depends on the host word size. Yuck!
322 But this is the traditional behavior. */
323 else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
325 static bool warned_once;
326 if (! warned_once)
328 warned_once = true;
329 WARN ((0, 0, _("Generating negative octal headers")));
331 where[size - 1] = '\0';
332 to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
333 where, size - 1);
334 return true;
336 /* Otherwise fall back to substitution, if possible: */
338 else
339 substitute = NULL; /* No substitution for formats, other than GNU */
341 return to_chars_subst (negative, gnu_format, value, valsize, substitute,
342 where, size, type);
345 static uintmax_t
346 gid_substitute (bool *negative)
348 gid_t r;
349 #ifdef GID_NOBODY
350 r = GID_NOBODY;
351 #else
352 static gid_t gid_nobody;
353 if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
354 gid_nobody = -2;
355 r = gid_nobody;
356 #endif
357 *negative = r < 0;
358 return r;
361 static bool
362 gid_to_chars (gid_t v, char *p, size_t s)
364 return to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
367 static bool
368 major_to_chars (major_t v, char *p, size_t s)
370 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
373 static bool
374 minor_to_chars (minor_t v, char *p, size_t s)
376 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
379 static bool
380 mode_to_chars (mode_t v, char *p, size_t s)
382 /* In the common case where the internal and external mode bits are the same,
383 and we are not using POSIX or GNU format,
384 propagate all unknown bits to the external mode.
385 This matches historical practice.
386 Otherwise, just copy the bits we know about. */
387 bool negative;
388 uintmax_t u;
389 if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
390 && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
391 && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
392 && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
393 && archive_format != POSIX_FORMAT
394 && archive_format != USTAR_FORMAT
395 && archive_format != GNU_FORMAT)
397 negative = v < 0;
398 u = v;
400 else
402 negative = false;
403 u = ((v & S_ISUID ? TSUID : 0)
404 | (v & S_ISGID ? TSGID : 0)
405 | (v & S_ISVTX ? TSVTX : 0)
406 | (v & S_IRUSR ? TUREAD : 0)
407 | (v & S_IWUSR ? TUWRITE : 0)
408 | (v & S_IXUSR ? TUEXEC : 0)
409 | (v & S_IRGRP ? TGREAD : 0)
410 | (v & S_IWGRP ? TGWRITE : 0)
411 | (v & S_IXGRP ? TGEXEC : 0)
412 | (v & S_IROTH ? TOREAD : 0)
413 | (v & S_IWOTH ? TOWRITE : 0)
414 | (v & S_IXOTH ? TOEXEC : 0));
416 return to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
419 bool
420 off_to_chars (off_t v, char *p, size_t s)
422 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
425 bool
426 time_to_chars (time_t v, char *p, size_t s)
428 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
431 static uintmax_t
432 uid_substitute (bool *negative)
434 uid_t r;
435 #ifdef UID_NOBODY
436 r = UID_NOBODY;
437 #else
438 static uid_t uid_nobody;
439 if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
440 uid_nobody = -2;
441 r = uid_nobody;
442 #endif
443 *negative = r < 0;
444 return r;
447 static bool
448 uid_to_chars (uid_t v, char *p, size_t s)
450 return to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
453 static bool
454 uintmax_to_chars (uintmax_t v, char *p, size_t s)
456 return to_chars (false, v, sizeof v, 0, p, s, "uintmax_t");
459 static void
460 string_to_chars (char const *str, char *p, size_t s)
462 tar_copy_str (p, str, s);
463 p[s - 1] = '\0';
467 /* A directory is always considered dumpable.
468 Otherwise, only regular and contiguous files are considered dumpable.
469 Such a file is dumpable if it is sparse and both --sparse and --totals
470 are specified.
471 Otherwise, it is dumpable unless any of the following conditions occur:
473 a) it is empty *and* world-readable, or
474 b) current archive is /dev/null */
476 static bool
477 file_dumpable_p (struct stat const *st)
479 if (S_ISDIR (st->st_mode))
480 return true;
481 if (! (S_ISREG (st->st_mode) || S_ISCTG (st->st_mode)))
482 return false;
483 if (dev_null_output)
484 return totals_option && sparse_option && ST_IS_SPARSE (*st);
485 return ! (st->st_size == 0 && (st->st_mode & MODE_R) == MODE_R);
489 /* Writing routines. */
491 /* Write the EOT block(s). Zero at least two blocks, through the end
492 of the record. Old tar, as previous versions of GNU tar, writes
493 garbage after two zeroed blocks. */
494 void
495 write_eot (void)
497 union block *pointer = find_next_block ();
498 memset (pointer->buffer, 0, BLOCKSIZE);
499 set_next_block_after (pointer);
500 pointer = find_next_block ();
501 memset (pointer->buffer, 0, available_space_after (pointer));
502 set_next_block_after (pointer);
505 /* Write a "private" header */
506 union block *
507 start_private_header (const char *name, size_t size, time_t t)
509 union block *header = find_next_block ();
511 memset (header->buffer, 0, sizeof (union block));
513 tar_name_copy_str (header->header.name, name, NAME_FIELD_SIZE);
514 OFF_TO_CHARS (size, header->header.size);
516 TIME_TO_CHARS (t < 0 ? 0 : min (t, MAX_OCTAL_VAL (header->header.mtime)),
517 header->header.mtime);
518 MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode);
519 UID_TO_CHARS (0, header->header.uid);
520 GID_TO_CHARS (0, header->header.gid);
521 memcpy (header->header.magic, TMAGIC, TMAGLEN);
522 memcpy (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 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
539 static void
540 write_gnu_long_link (struct tar_stat_info *st, const char *p, char type)
542 size_t size = strlen (p) + 1;
543 size_t bufsize;
544 union block *header;
546 header = start_private_header ("././@LongLink", size, 0);
547 if (! numeric_owner_option)
549 static char *uname, *gname;
550 if (!uname)
552 uid_to_uname (0, &uname);
553 gid_to_gname (0, &gname);
555 UNAME_TO_CHARS (uname, header->header.uname);
556 GNAME_TO_CHARS (gname, header->header.gname);
559 strcpy (header->buffer + offsetof (struct posix_header, magic),
560 OLDGNU_MAGIC);
561 header->header.typeflag = type;
562 finish_header (st, header, -1);
564 header = find_next_block ();
566 bufsize = available_space_after (header);
568 while (bufsize < size)
570 memcpy (header->buffer, p, bufsize);
571 p += bufsize;
572 size -= bufsize;
573 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
574 header = find_next_block ();
575 bufsize = available_space_after (header);
577 memcpy (header->buffer, p, size);
578 memset (header->buffer + size, 0, bufsize - size);
579 set_next_block_after (header + (size - 1) / BLOCKSIZE);
582 static size_t
583 split_long_name (const char *name, size_t length)
585 size_t i;
587 if (length > PREFIX_FIELD_SIZE + 1)
588 length = PREFIX_FIELD_SIZE + 1;
589 else if (ISSLASH (name[length - 1]))
590 length--;
591 for (i = length - 1; i > 0; i--)
592 if (ISSLASH (name[i]))
593 break;
594 return i;
597 static union block *
598 write_ustar_long_name (const char *name)
600 size_t length = strlen (name);
601 size_t i, nlen;
602 union block *header;
604 if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
606 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
607 quotearg_colon (name),
608 PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
609 return NULL;
612 i = split_long_name (name, length);
613 if (i == 0 || (nlen = length - i - 1) > NAME_FIELD_SIZE || nlen == 0)
615 ERROR ((0, 0,
616 _("%s: file name is too long (cannot be split); not dumped"),
617 quotearg_colon (name)));
618 return NULL;
621 header = find_next_block ();
622 memset (header->buffer, 0, sizeof (header->buffer));
623 memcpy (header->header.prefix, name, i);
624 memcpy (header->header.name, name + i + 1, length - i - 1);
626 return header;
629 /* Write a long link name, depending on the current archive format */
630 static void
631 write_long_link (struct tar_stat_info *st)
633 switch (archive_format)
635 case POSIX_FORMAT:
636 xheader_store ("linkpath", st, NULL);
637 break;
639 case V7_FORMAT: /* old V7 tar format */
640 case USTAR_FORMAT:
641 case STAR_FORMAT:
642 ERROR ((0, 0,
643 _("%s: link name is too long; not dumped"),
644 quotearg_colon (st->link_name)));
645 break;
647 case OLDGNU_FORMAT:
648 case GNU_FORMAT:
649 write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK);
650 break;
652 default:
653 abort(); /*FIXME*/
657 static union block *
658 write_long_name (struct tar_stat_info *st)
660 switch (archive_format)
662 case POSIX_FORMAT:
663 xheader_store ("path", st, NULL);
664 break;
666 case V7_FORMAT:
667 if (strlen (st->file_name) > NAME_FIELD_SIZE-1)
669 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
670 quotearg_colon (st->file_name),
671 NAME_FIELD_SIZE - 1));
672 return NULL;
674 break;
676 case USTAR_FORMAT:
677 case STAR_FORMAT:
678 return write_ustar_long_name (st->file_name);
680 case OLDGNU_FORMAT:
681 case GNU_FORMAT:
682 write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
683 break;
685 default:
686 abort(); /*FIXME*/
688 return write_short_name (st);
691 union block *
692 write_extended (bool global, struct tar_stat_info *st, union block *old_header)
694 union block *header, hp;
695 char *p;
696 int type;
697 time_t t;
699 if (st->xhdr.buffer || st->xhdr.stk == NULL)
700 return old_header;
702 xheader_finish (&st->xhdr);
703 memcpy (hp.buffer, old_header, sizeof (hp));
704 if (global)
706 type = XGLTYPE;
707 p = xheader_ghdr_name ();
708 t = start_time.tv_sec;
710 else
712 type = XHDTYPE;
713 p = xheader_xhdr_name (st);
714 t = set_mtime_option ? mtime_option.tv_sec : st->stat.st_mtime;
716 xheader_write (type, p, t, &st->xhdr);
717 free (p);
718 header = find_next_block ();
719 memcpy (header, &hp.buffer, sizeof (hp.buffer));
720 return header;
723 static union block *
724 write_header_name (struct tar_stat_info *st)
726 if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
728 xheader_store ("path", st, NULL);
729 return write_short_name (st);
731 else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
732 < strlen (st->file_name))
733 return write_long_name (st);
734 else
735 return write_short_name (st);
739 /* Header handling. */
741 /* Make a header block for the file whose stat info is st,
742 and return its address. */
744 union block *
745 start_header (struct tar_stat_info *st)
747 union block *header;
748 char const *uname = NULL;
749 char const *gname = NULL;
751 header = write_header_name (st);
752 if (!header)
753 return NULL;
755 /* Override some stat fields, if requested to do so. */
756 owner_map_translate (st->stat.st_uid, &st->stat.st_uid, &uname);
757 group_map_translate (st->stat.st_gid, &st->stat.st_gid, &gname);
759 if (mode_option)
760 st->stat.st_mode =
761 ((st->stat.st_mode & ~MODE_ALL)
762 | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0,
763 initial_umask, mode_option, NULL));
765 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
766 for a few tars and came up with the following interoperability
767 matrix:
769 WRITER
770 1 2 3 4 5 6 7 8 9 READER
771 . . . . . . . . . 1 = SunOS 4.2 tar
772 # . . # # . . # # 2 = NEC SVR4.0.2 tar
773 . . . # # . . # . 3 = Solaris 2.1 tar
774 . . . . . . . . . 4 = GNU tar 1.11.1
775 . . . . . . . . . 5 = HP-UX 8.07 tar
776 . . . . . . . . . 6 = Ultrix 4.1
777 . . . . . . . . . 7 = AIX 3.2
778 . . . . . . . . . 8 = Hitachi HI-UX 1.03
779 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
781 . = works
782 # = "impossible file type"
784 The following mask for old archive removes the '#'s in column 4
785 above, thus making GNU tar both a universal donor and a universal
786 acceptor for Paul's test. */
788 if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
789 MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
790 else
791 MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
794 uid_t uid = st->stat.st_uid;
795 if (archive_format == POSIX_FORMAT
796 && MAX_OCTAL_VAL (header->header.uid) < uid)
798 xheader_store ("uid", st, NULL);
799 uid = 0;
801 if (!UID_TO_CHARS (uid, header->header.uid))
802 return NULL;
806 gid_t gid = st->stat.st_gid;
807 if (archive_format == POSIX_FORMAT
808 && MAX_OCTAL_VAL (header->header.gid) < gid)
810 xheader_store ("gid", st, NULL);
811 gid = 0;
813 if (!GID_TO_CHARS (gid, header->header.gid))
814 return NULL;
818 off_t size = st->stat.st_size;
819 if (archive_format == POSIX_FORMAT
820 && MAX_OCTAL_VAL (header->header.size) < size)
822 xheader_store ("size", st, NULL);
823 size = 0;
825 if (!OFF_TO_CHARS (size, header->header.size))
826 return NULL;
830 struct timespec mtime;
832 switch (set_mtime_option)
834 case USE_FILE_MTIME:
835 mtime = st->mtime;
836 break;
838 case FORCE_MTIME:
839 mtime = mtime_option;
840 break;
842 case CLAMP_MTIME:
843 mtime = timespec_cmp (st->mtime, mtime_option) > 0
844 ? mtime_option : st->mtime;
845 break;
847 case COMMAND_MTIME:
848 if (sys_exec_setmtime_script (set_mtime_command,
849 chdir_fd,
850 st->orig_file_name,
851 set_mtime_format,
852 &mtime))
853 mtime = st->mtime;
854 break;
857 if (archive_format == POSIX_FORMAT)
859 if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
860 || mtime.tv_nsec != 0)
861 xheader_store ("mtime", st, &mtime);
862 if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
863 mtime.tv_sec = 0;
865 if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime))
866 return NULL;
869 /* FIXME */
870 if (S_ISCHR (st->stat.st_mode)
871 || S_ISBLK (st->stat.st_mode))
873 major_t devmajor = major (st->stat.st_rdev);
874 minor_t devminor = minor (st->stat.st_rdev);
876 if (archive_format == POSIX_FORMAT
877 && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
879 xheader_store ("devmajor", st, NULL);
880 devmajor = 0;
882 if (!MAJOR_TO_CHARS (devmajor, header->header.devmajor))
883 return NULL;
885 if (archive_format == POSIX_FORMAT
886 && MAX_OCTAL_VAL (header->header.devminor) < devminor)
888 xheader_store ("devminor", st, NULL);
889 devminor = 0;
891 if (!MINOR_TO_CHARS (devminor, header->header.devminor))
892 return NULL;
895 if (archive_format == POSIX_FORMAT)
897 xheader_store ("atime", st, NULL);
898 xheader_store ("ctime", st, NULL);
900 else if (incremental_option)
901 if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
903 TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
904 TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
907 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
909 switch (archive_format)
911 case V7_FORMAT:
912 break;
914 case OLDGNU_FORMAT:
915 case GNU_FORMAT: /*FIXME?*/
916 /* Overwrite header->header.magic and header.version in one blow. */
917 strcpy (header->buffer + offsetof (struct posix_header, magic),
918 OLDGNU_MAGIC);
919 break;
921 case POSIX_FORMAT:
922 case USTAR_FORMAT:
923 memcpy (header->header.magic, TMAGIC, TMAGLEN);
924 memcpy (header->header.version, TVERSION, TVERSLEN);
925 break;
927 default:
928 abort ();
931 if (archive_format == V7_FORMAT || numeric_owner_option)
933 /* header->header.[ug]name are left as the empty string. */
935 else
937 if (uname)
938 st->uname = xstrdup (uname);
939 else
940 uid_to_uname (st->stat.st_uid, &st->uname);
942 if (gname)
943 st->gname = xstrdup (gname);
944 else
945 gid_to_gname (st->stat.st_gid, &st->gname);
947 if (archive_format == POSIX_FORMAT
948 && (strlen (st->uname) > UNAME_FIELD_SIZE
949 || !string_ascii_p (st->uname)))
950 xheader_store ("uname", st, NULL);
951 UNAME_TO_CHARS (st->uname, header->header.uname);
953 if (archive_format == POSIX_FORMAT
954 && (strlen (st->gname) > GNAME_FIELD_SIZE
955 || !string_ascii_p (st->gname)))
956 xheader_store ("gname", st, NULL);
957 GNAME_TO_CHARS (st->gname, header->header.gname);
960 if (archive_format == POSIX_FORMAT)
962 if (acls_option > 0)
964 if (st->acls_a_ptr)
965 xheader_store ("SCHILY.acl.access", st, NULL);
966 if (st->acls_d_ptr)
967 xheader_store ("SCHILY.acl.default", st, NULL);
969 if ((selinux_context_option > 0) && st->cntx_name)
970 xheader_store ("RHT.security.selinux", st, NULL);
971 if (xattrs_option > 0)
973 size_t i;
974 for (i = 0; i < st->xattr_map.xm_size; i++)
975 xheader_store (st->xattr_map.xm_map[i].xkey, st, &i);
979 return header;
982 void
983 simple_finish_header (union block *header)
985 size_t i;
986 int sum;
987 char *p;
989 memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
991 sum = 0;
992 p = header->buffer;
993 for (i = sizeof *header; i-- != 0; )
994 /* We can't use unsigned char here because of old compilers, e.g. V7. */
995 sum += 0xFF & *p++;
997 /* Fill in the checksum field. It's formatted differently from the
998 other fields: it has [6] digits, a null, then a space -- rather than
999 digits, then a null. We use to_chars.
1000 The final space is already there, from
1001 checksumming, and to_chars doesn't modify it.
1003 This is a fast way to do:
1005 sprintf(header->header.chksum, "%6o", sum); */
1007 uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
1009 set_next_block_after (header);
1012 /* Finish off a filled-in header block and write it out. We also
1013 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
1014 is not negative, is the block ordinal of the first record for this
1015 file, which may be a preceding long name or long link record. */
1016 void
1017 finish_header (struct tar_stat_info *st,
1018 union block *header, off_t block_ordinal)
1020 /* Note: It is important to do this before the call to write_extended(),
1021 so that the actual ustar header is printed */
1022 if (verbose_option
1023 && header->header.typeflag != GNUTYPE_LONGLINK
1024 && header->header.typeflag != GNUTYPE_LONGNAME
1025 && header->header.typeflag != XHDTYPE
1026 && header->header.typeflag != XGLTYPE)
1028 /* FIXME: This global is used in print_header, sigh. */
1029 current_format = archive_format;
1030 print_header (st, header, block_ordinal);
1033 header = write_extended (false, st, header);
1034 simple_finish_header (header);
1038 void
1039 pad_archive (off_t size_left)
1041 union block *blk;
1042 while (size_left > 0)
1044 blk = find_next_block ();
1045 memset (blk->buffer, 0, BLOCKSIZE);
1046 set_next_block_after (blk);
1047 size_left -= BLOCKSIZE;
1051 static enum dump_status
1052 dump_regular_file (int fd, struct tar_stat_info *st)
1054 off_t size_left = st->stat.st_size;
1055 off_t block_ordinal;
1056 union block *blk;
1058 block_ordinal = current_block_ordinal ();
1059 blk = start_header (st);
1060 if (!blk)
1061 return dump_status_fail;
1063 /* Mark contiguous files, if we support them. */
1064 if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
1065 blk->header.typeflag = CONTTYPE;
1067 finish_header (st, blk, block_ordinal);
1069 mv_begin_write (st->file_name, st->stat.st_size, st->stat.st_size);
1070 while (size_left > 0)
1072 size_t bufsize, count;
1074 blk = find_next_block ();
1076 bufsize = available_space_after (blk);
1078 if (size_left < bufsize)
1080 /* Last read -- zero out area beyond. */
1081 bufsize = size_left;
1082 count = bufsize % BLOCKSIZE;
1083 if (count)
1084 memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1087 count = (fd <= 0) ? bufsize : blocking_read (fd, blk->buffer, bufsize);
1088 if (count == SAFE_READ_ERROR)
1090 read_diag_details (st->orig_file_name,
1091 st->stat.st_size - size_left, bufsize);
1092 pad_archive (size_left);
1093 return dump_status_short;
1095 size_left -= count;
1096 set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1098 if (count != bufsize)
1100 char buf[UINTMAX_STRSIZE_BOUND];
1101 memset (blk->buffer + count, 0, bufsize - count);
1102 WARNOPT (WARN_FILE_SHRANK,
1103 (0, 0,
1104 ngettext ("%s: File shrank by %s byte; padding with zeros",
1105 "%s: File shrank by %s bytes; padding with zeros",
1106 size_left),
1107 quotearg_colon (st->orig_file_name),
1108 STRINGIFY_BIGINT (size_left, buf)));
1109 if (! ignore_failed_read_option)
1110 set_exit_status (TAREXIT_DIFFERS);
1111 pad_archive (size_left - (bufsize - count));
1112 return dump_status_short;
1115 return dump_status_ok;
1119 /* Copy info from the directory identified by ST into the archive.
1120 DIRECTORY contains the directory's entries. */
1122 static void
1123 dump_dir0 (struct tar_stat_info *st, char const *directory)
1125 bool top_level = ! st->parent;
1126 const char *tag_file_name;
1127 union block *blk = NULL;
1128 off_t block_ordinal = current_block_ordinal ();
1130 st->stat.st_size = 0; /* force 0 size on dir */
1132 blk = start_header (st);
1133 if (!blk)
1134 return;
1136 info_attach_exclist (st);
1138 if (incremental_option && archive_format != POSIX_FORMAT)
1139 blk->header.typeflag = GNUTYPE_DUMPDIR;
1140 else /* if (standard_option) */
1141 blk->header.typeflag = DIRTYPE;
1143 /* If we're gnudumping, we aren't done yet so don't close it. */
1145 if (!incremental_option)
1146 finish_header (st, blk, block_ordinal);
1147 else if (gnu_list_name->directory)
1149 if (archive_format == POSIX_FORMAT)
1151 xheader_store ("GNU.dumpdir", st,
1152 safe_directory_contents (gnu_list_name->directory));
1153 finish_header (st, blk, block_ordinal);
1155 else
1157 off_t size_left;
1158 off_t totsize;
1159 size_t bufsize;
1160 ssize_t count;
1161 const char *buffer, *p_buffer;
1163 block_ordinal = current_block_ordinal ();
1164 buffer = safe_directory_contents (gnu_list_name->directory);
1165 totsize = dumpdir_size (buffer);
1166 OFF_TO_CHARS (totsize, blk->header.size);
1167 finish_header (st, blk, block_ordinal);
1168 p_buffer = buffer;
1169 size_left = totsize;
1171 mv_begin_write (st->file_name, totsize, totsize);
1172 while (size_left > 0)
1174 blk = find_next_block ();
1175 bufsize = available_space_after (blk);
1176 if (size_left < bufsize)
1178 bufsize = size_left;
1179 count = bufsize % BLOCKSIZE;
1180 if (count)
1181 memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1183 memcpy (blk->buffer, p_buffer, bufsize);
1184 size_left -= bufsize;
1185 p_buffer += bufsize;
1186 set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1189 return;
1192 if (!recursion_option)
1193 return;
1195 if (one_file_system_option
1196 && !top_level
1197 && st->parent->stat.st_dev != st->stat.st_dev)
1199 if (verbose_option)
1200 WARNOPT (WARN_XDEV,
1201 (0, 0,
1202 _("%s: file is on a different filesystem; not dumped"),
1203 quotearg_colon (st->orig_file_name)));
1205 else
1207 char *name_buf;
1208 size_t name_size;
1210 switch (check_exclusion_tags (st, &tag_file_name))
1212 case exclusion_tag_all:
1213 /* Handled in dump_file0 */
1214 break;
1216 case exclusion_tag_none:
1218 char const *entry;
1219 size_t entry_len;
1220 size_t name_len;
1222 name_buf = xstrdup (st->orig_file_name);
1223 name_size = name_len = strlen (name_buf);
1225 /* Now output all the files in the directory. */
1226 for (entry = directory; (entry_len = strlen (entry)) != 0;
1227 entry += entry_len + 1)
1229 if (name_size < name_len + entry_len)
1231 name_size = name_len + entry_len;
1232 name_buf = xrealloc (name_buf, name_size + 1);
1234 strcpy (name_buf + name_len, entry);
1235 if (!excluded_name (name_buf, st))
1236 dump_file (st, entry, name_buf);
1239 free (name_buf);
1241 break;
1243 case exclusion_tag_contents:
1244 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1245 _("contents not dumped"));
1246 name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1;
1247 name_buf = xmalloc (name_size);
1248 strcpy (name_buf, st->orig_file_name);
1249 strcat (name_buf, tag_file_name);
1250 dump_file (st, tag_file_name, name_buf);
1251 free (name_buf);
1252 break;
1254 case exclusion_tag_under:
1255 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1256 _("contents not dumped"));
1257 break;
1262 /* Ensure exactly one trailing slash. */
1263 static void
1264 ensure_slash (char **pstr)
1266 size_t len = strlen (*pstr);
1267 while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
1268 len--;
1269 if (!ISSLASH ((*pstr)[len]))
1270 *pstr = xrealloc (*pstr, len + 2);
1271 (*pstr)[len++] = '/';
1272 (*pstr)[len] = '\0';
1275 /* If we just ran out of file descriptors, release a file descriptor
1276 in the directory chain somewhere leading from DIR->parent->parent
1277 up through the root. Return true if successful, false (preserving
1278 errno == EMFILE) otherwise.
1280 Do not release DIR's file descriptor, or DIR's parent, as other
1281 code assumes that they work. On some operating systems, another
1282 process can claim file descriptor resources as we release them, and
1283 some calls or their emulations require multiple file descriptors,
1284 so callers should not give up if a single release doesn't work. */
1286 static bool
1287 open_failure_recover (struct tar_stat_info const *dir)
1289 if (errno == EMFILE && dir && dir->parent)
1291 struct tar_stat_info *p;
1292 for (p = dir->parent->parent; p; p = p->parent)
1293 if (0 < p->fd && (! p->parent || p->parent->fd <= 0))
1295 tar_stat_close (p);
1296 return true;
1298 errno = EMFILE;
1301 return false;
1304 /* Return the directory entries of ST, in a dynamically allocated buffer,
1305 each entry followed by '\0' and the last followed by an extra '\0'.
1306 Return null on failure, setting errno. */
1307 char *
1308 get_directory_entries (struct tar_stat_info *st)
1310 while (! (st->dirstream = fdopendir (st->fd)))
1311 if (! open_failure_recover (st))
1312 return 0;
1313 return streamsavedir (st->dirstream, savedir_sort_order);
1316 /* Dump the directory ST. Return true if successful, false (emitting
1317 diagnostics) otherwise. Get ST's entries, recurse through its
1318 subdirectories, and clean up file descriptors afterwards. */
1319 static bool
1320 dump_dir (struct tar_stat_info *st)
1322 char *directory = get_directory_entries (st);
1323 if (! directory)
1325 savedir_diag (st->orig_file_name);
1326 return false;
1329 dump_dir0 (st, directory);
1331 restore_parent_fd (st);
1332 free (directory);
1333 return true;
1337 /* Number of links a file can have without having to be entered into
1338 the link table. Typically this is 1, but in trickier circumstances
1339 it is 0. */
1340 static nlink_t trivial_link_count;
1343 /* Main functions of this module. */
1345 void
1346 create_archive (void)
1348 struct name const *p;
1350 trivial_link_count = filename_args != FILES_MANY && ! dereference_option;
1352 open_archive (ACCESS_WRITE);
1353 buffer_write_global_xheader ();
1355 if (incremental_option)
1357 size_t buffer_size = 0;
1358 char *buffer = NULL;
1359 const char *q;
1361 collect_and_sort_names ();
1363 while ((p = name_from_list ()) != NULL)
1364 if (!excluded_name (p->name, NULL))
1365 dump_file (0, p->name, p->name);
1367 blank_name_list ();
1368 while ((p = name_from_list ()) != NULL)
1369 if (!excluded_name (p->name, NULL))
1371 struct tar_stat_info st;
1372 size_t plen = strlen (p->name);
1373 while (buffer_size <= plen)
1374 buffer = x2realloc (buffer, &buffer_size);
1375 memcpy (buffer, p->name, plen);
1376 if (! ISSLASH (buffer[plen - 1]))
1377 buffer[plen++] = DIRECTORY_SEPARATOR;
1378 tar_stat_init (&st);
1379 q = directory_contents (p->directory);
1380 if (q)
1381 while (*q)
1383 size_t qlen = strlen (q);
1384 if (*q == 'Y')
1386 if (! st.orig_file_name)
1388 int fd = openat (chdir_fd, p->name,
1389 open_searchdir_flags);
1390 if (fd < 0)
1392 file_removed_diag (p->name, !p->parent,
1393 open_diag);
1394 break;
1396 st.fd = fd;
1397 if (fstat (fd, &st.stat) != 0)
1399 file_removed_diag (p->name, !p->parent,
1400 stat_diag);
1401 break;
1403 st.orig_file_name = xstrdup (p->name);
1405 while (buffer_size < plen + qlen)
1406 buffer = x2realloc (buffer, &buffer_size);
1407 strcpy (buffer + plen, q + 1);
1408 dump_file (&st, q + 1, buffer);
1410 q += qlen + 1;
1412 tar_stat_destroy (&st);
1414 free (buffer);
1416 else
1418 const char *name;
1419 while ((name = name_next (1)) != NULL)
1420 if (!excluded_name (name, NULL))
1421 dump_file (0, name, name);
1424 write_eot ();
1425 close_archive ();
1426 finish_deferred_unlinks ();
1427 if (listed_incremental_option)
1428 write_directory_file ();
1432 /* Calculate the hash of a link. */
1433 static size_t
1434 hash_link (void const *entry, size_t n_buckets)
1436 struct link const *l = entry;
1437 uintmax_t num = l->dev ^ l->ino;
1438 return num % n_buckets;
1441 /* Compare two links for equality. */
1442 static bool
1443 compare_links (void const *entry1, void const *entry2)
1445 struct link const *link1 = entry1;
1446 struct link const *link2 = entry2;
1447 return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
1450 static void
1451 unknown_file_error (char const *p)
1453 WARNOPT (WARN_FILE_IGNORED,
1454 (0, 0, _("%s: Unknown file type; file ignored"),
1455 quotearg_colon (p)));
1456 if (!ignore_failed_read_option)
1457 set_exit_status (TAREXIT_FAILURE);
1461 /* Handling of hard links */
1463 /* Table of all non-directories that we've written so far. Any time
1464 we see another, we check the table and avoid dumping the data
1465 again if we've done it once already. */
1466 static Hash_table *link_table;
1468 /* Try to dump stat as a hard link to another file in the archive.
1469 Return true if successful. */
1470 static bool
1471 dump_hard_link (struct tar_stat_info *st)
1473 if (link_table
1474 && (trivial_link_count < st->stat.st_nlink || remove_files_option))
1476 struct link lp;
1477 struct link *duplicate;
1478 off_t block_ordinal;
1479 union block *blk;
1481 lp.ino = st->stat.st_ino;
1482 lp.dev = st->stat.st_dev;
1484 if ((duplicate = hash_lookup (link_table, &lp)))
1486 /* We found a link. */
1487 char const *link_name = safer_name_suffix (duplicate->name, true,
1488 absolute_names_option);
1489 if (duplicate->nlink)
1490 duplicate->nlink--;
1492 block_ordinal = current_block_ordinal ();
1493 assign_string (&st->link_name, link_name);
1494 if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
1495 < strlen (link_name))
1496 write_long_link (st);
1498 st->stat.st_size = 0;
1499 blk = start_header (st);
1500 if (!blk)
1501 return false;
1502 tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
1504 blk->header.typeflag = LNKTYPE;
1505 finish_header (st, blk, block_ordinal);
1507 if (remove_files_option)
1508 queue_deferred_unlink (st->orig_file_name, false);
1510 return true;
1513 return false;
1516 static void
1517 file_count_links (struct tar_stat_info *st)
1519 if (hard_dereference_option)
1520 return;
1521 if (trivial_link_count < st->stat.st_nlink)
1523 struct link *duplicate;
1524 char *linkname = NULL;
1525 struct link *lp;
1527 assign_string (&linkname, safer_name_suffix (st->orig_file_name, true,
1528 absolute_names_option));
1529 transform_name (&linkname, XFORM_LINK);
1531 lp = xmalloc (FLEXNSIZEOF (struct link, name, strlen (linkname) + 1));
1532 lp->ino = st->stat.st_ino;
1533 lp->dev = st->stat.st_dev;
1534 lp->nlink = st->stat.st_nlink;
1535 strcpy (lp->name, linkname);
1536 free (linkname);
1538 if (! ((link_table
1539 || (link_table = hash_initialize (0, 0, hash_link,
1540 compare_links, 0)))
1541 && (duplicate = hash_insert (link_table, lp))))
1542 xalloc_die ();
1544 if (duplicate != lp)
1545 abort ();
1546 lp->nlink--;
1550 /* For each dumped file, check if all its links were dumped. Emit
1551 warnings if it is not so. */
1552 void
1553 check_links (void)
1555 struct link *lp;
1557 if (!link_table)
1558 return;
1560 for (lp = hash_get_first (link_table); lp;
1561 lp = hash_get_next (link_table, lp))
1563 if (lp->nlink)
1565 WARN ((0, 0, _("Missing links to %s."), quote (lp->name)));
1570 /* Assuming DIR is the working directory, open FILE, using FLAGS to
1571 control the open. A null DIR means to use ".". If we are low on
1572 file descriptors, try to release one or more from DIR's parents to
1573 reuse it. */
1575 subfile_open (struct tar_stat_info const *dir, char const *file, int flags)
1577 int fd;
1579 static bool initialized;
1580 if (! initialized)
1582 /* Initialize any tables that might be needed when file
1583 descriptors are exhausted, and whose initialization might
1584 require a file descriptor. This includes the system message
1585 catalog and tar's message catalog. */
1586 initialized = true;
1587 strerror (ENOENT);
1588 gettext ("");
1591 while ((fd = openat (dir ? dir->fd : chdir_fd, file, flags)) < 0
1592 && open_failure_recover (dir))
1593 continue;
1594 return fd;
1597 /* Restore the file descriptor for ST->parent, if it was temporarily
1598 closed to conserve file descriptors. On failure, set the file
1599 descriptor to the negative of the corresponding errno value. Call
1600 this every time a subdirectory is ascended from. */
1601 void
1602 restore_parent_fd (struct tar_stat_info const *st)
1604 struct tar_stat_info *parent = st->parent;
1605 if (parent && ! parent->fd)
1607 int parentfd = openat (st->fd, "..", open_searchdir_flags);
1608 struct stat parentstat;
1610 if (parentfd < 0)
1611 parentfd = - errno;
1612 else if (! (fstat (parentfd, &parentstat) == 0
1613 && parent->stat.st_ino == parentstat.st_ino
1614 && parent->stat.st_dev == parentstat.st_dev))
1616 close (parentfd);
1617 parentfd = IMPOSTOR_ERRNO;
1620 if (parentfd < 0)
1622 int origfd = openat (chdir_fd, parent->orig_file_name,
1623 open_searchdir_flags);
1624 if (0 <= origfd)
1626 if (fstat (parentfd, &parentstat) == 0
1627 && parent->stat.st_ino == parentstat.st_ino
1628 && parent->stat.st_dev == parentstat.st_dev)
1629 parentfd = origfd;
1630 else
1631 close (origfd);
1635 parent->fd = parentfd;
1639 /* Dump a single file, recursing on directories. ST is the file's
1640 status info, NAME its name relative to the parent directory, and P
1641 its full name (which may be relative to the working directory). */
1643 /* FIXME: One should make sure that for *every* path leading to setting
1644 exit_status to failure, a clear diagnostic has been issued. */
1646 static void
1647 dump_file0 (struct tar_stat_info *st, char const *name, char const *p)
1649 union block *header;
1650 char type;
1651 off_t block_ordinal = -1;
1652 int fd = 0;
1653 bool is_dir;
1654 struct tar_stat_info const *parent = st->parent;
1655 bool top_level = ! parent;
1656 int parentfd = top_level ? chdir_fd : parent->fd;
1657 void (*diag) (char const *) = 0;
1659 if (interactive_option && !confirm ("add", p))
1660 return;
1662 assign_string (&st->orig_file_name, p);
1663 assign_string (&st->file_name,
1664 safer_name_suffix (p, false, absolute_names_option));
1666 transform_name (&st->file_name, XFORM_REGFILE);
1668 if (parentfd < 0 && ! top_level)
1670 errno = - parentfd;
1671 diag = open_diag;
1673 else if (fstatat (parentfd, name, &st->stat, fstatat_flags) != 0)
1674 diag = stat_diag;
1675 else if (file_dumpable_p (&st->stat))
1677 fd = subfile_open (parent, name, open_read_flags);
1678 if (fd < 0)
1679 diag = open_diag;
1680 else
1682 st->fd = fd;
1683 if (fstat (fd, &st->stat) != 0)
1684 diag = stat_diag;
1687 if (diag)
1689 file_removed_diag (p, top_level, diag);
1690 return;
1693 struct stat st1 = st->stat;
1694 st->archive_file_size = st->stat.st_size;
1695 st->atime = get_stat_atime (&st->stat);
1696 st->mtime = get_stat_mtime (&st->stat);
1697 st->ctime = get_stat_ctime (&st->stat);
1699 #ifdef S_ISHIDDEN
1700 if (S_ISHIDDEN (st->stat.st_mode))
1702 char *new = (char *) alloca (strlen (p) + 2);
1703 if (new)
1705 strcpy (new, p);
1706 strcat (new, "@");
1707 p = new;
1710 #endif
1712 /* See if we want only new files, and check if this one is too old to
1713 put in the archive.
1715 This check is omitted if incremental_option is set *and* the
1716 requested file is not explicitly listed in the command line. */
1718 if (! (incremental_option && ! top_level)
1719 && !S_ISDIR (st->stat.st_mode)
1720 && OLDER_TAR_STAT_TIME (*st, m)
1721 && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
1723 if (!incremental_option && verbose_option)
1724 WARNOPT (WARN_FILE_UNCHANGED,
1725 (0, 0, _("%s: file is unchanged; not dumped"),
1726 quotearg_colon (p)));
1727 return;
1730 /* See if we are trying to dump the archive. */
1731 if (sys_file_is_archive (st))
1733 WARNOPT (WARN_IGNORE_ARCHIVE,
1734 (0, 0, _("%s: archive cannot contain itself; not dumped"),
1735 quotearg_colon (p)));
1736 return;
1739 is_dir = S_ISDIR (st->stat.st_mode) != 0;
1741 if (!is_dir && dump_hard_link (st))
1742 return;
1744 if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode))
1746 bool ok;
1747 struct stat st2;
1749 xattrs_acls_get (parentfd, name, st, !is_dir);
1750 xattrs_selinux_get (parentfd, name, st, fd);
1751 xattrs_xattrs_get (parentfd, name, st, fd);
1753 if (is_dir)
1755 const char *tag_file_name;
1756 ensure_slash (&st->orig_file_name);
1757 ensure_slash (&st->file_name);
1759 if (check_exclusion_tags (st, &tag_file_name) == exclusion_tag_all)
1761 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1762 _("directory not dumped"));
1763 return;
1766 ok = dump_dir (st);
1768 fd = st->fd;
1769 parentfd = top_level ? chdir_fd : parent->fd;
1771 else
1773 enum dump_status status;
1775 if (fd && sparse_option && ST_IS_SPARSE (st->stat))
1777 status = sparse_dump_file (fd, st);
1778 if (status == dump_status_not_implemented)
1779 status = dump_regular_file (fd, st);
1781 else
1782 status = dump_regular_file (fd, st);
1784 switch (status)
1786 case dump_status_ok:
1787 case dump_status_short:
1788 file_count_links (st);
1789 break;
1791 case dump_status_fail:
1792 break;
1794 case dump_status_not_implemented:
1795 abort ();
1798 ok = status == dump_status_ok;
1801 if (ok)
1803 if (fd < 0)
1805 errno = - fd;
1806 ok = false;
1808 else if (fd == 0)
1810 if (parentfd < 0 && ! top_level)
1812 errno = - parentfd;
1813 ok = false;
1816 else
1817 ok = fstat (fd, &st2) == 0;
1819 if (! ok)
1820 file_removed_diag (p, top_level, stat_diag);
1823 if (ok && fd)
1825 /* Heuristically check whether the file is the same in all
1826 attributes that tar cares about and can easily check.
1827 Although the check is not perfect since it does not
1828 consult file contents, it is typically good enough.
1829 Do not check atime which is saved only to replace it later.
1830 Do not check ctime where changes might be benign (e.g.,
1831 another process creates a hard link to the file). */
1833 /* If the file's user ID, group ID or mode changed, tar may
1834 have output the wrong info for the file. */
1835 ok &= st1.st_uid == st2.st_uid;
1836 ok &= st1.st_gid == st2.st_gid;
1837 ok &= st1.st_mode == st2.st_mode;
1839 /* Likewise for the file's mtime, but skip this check if it
1840 is a directory possibly updated by --remove-files. */
1841 if (! (is_dir && remove_files_option))
1842 ok &= ! timespec_cmp (get_stat_mtime (&st1),
1843 get_stat_mtime (&st2));
1845 /* Likewise for the file's size, but skip this check if it
1846 is a directory as tar does not output directory sizes.
1847 Although dump_regular_file caught regular file shrinkage,
1848 it shouldn't hurt to check for shrinkage again now;
1849 plus, the file may have grown. */
1850 if (!is_dir)
1851 ok &= st1.st_size == st2.st_size;
1853 if (!ok)
1855 WARNOPT (WARN_FILE_CHANGED,
1856 (0, 0, _("%s: file changed as we read it"),
1857 quotearg_colon (p)));
1858 if (! ignore_failed_read_option)
1859 set_exit_status (TAREXIT_DIFFERS);
1861 else if (atime_preserve_option == replace_atime_preserve
1862 && timespec_cmp (st->atime, get_stat_atime (&st2)) != 0
1863 && set_file_atime (fd, parentfd, name, st->atime) != 0)
1864 utime_error (p);
1867 ok &= tar_stat_close (st);
1868 if (ok && remove_files_option)
1869 queue_deferred_unlink (p, is_dir);
1871 return;
1873 #ifdef HAVE_READLINK
1874 else if (S_ISLNK (st->stat.st_mode))
1876 st->link_name = areadlinkat_with_size (parentfd, name, st->stat.st_size);
1877 if (!st->link_name)
1879 if (errno == ENOMEM)
1880 xalloc_die ();
1881 file_removed_diag (p, top_level, readlink_diag);
1882 return;
1884 transform_name (&st->link_name, XFORM_SYMLINK);
1885 if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
1886 < strlen (st->link_name))
1887 write_long_link (st);
1889 xattrs_selinux_get (parentfd, name, st, 0);
1890 xattrs_xattrs_get (parentfd, name, st, 0);
1892 block_ordinal = current_block_ordinal ();
1893 st->stat.st_size = 0; /* force 0 size on symlink */
1894 header = start_header (st);
1895 if (!header)
1896 return;
1897 tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE);
1898 header->header.typeflag = SYMTYPE;
1899 finish_header (st, header, block_ordinal);
1900 /* nothing more to do to it */
1902 if (remove_files_option)
1903 queue_deferred_unlink (p, false);
1905 file_count_links (st);
1906 return;
1908 #endif
1909 else if (S_ISCHR (st->stat.st_mode))
1911 type = CHRTYPE;
1912 xattrs_acls_get (parentfd, name, st, true);
1913 xattrs_selinux_get (parentfd, name, st, 0);
1914 xattrs_xattrs_get (parentfd, name, st, 0);
1916 else if (S_ISBLK (st->stat.st_mode))
1918 type = BLKTYPE;
1919 xattrs_acls_get (parentfd, name, st, true);
1920 xattrs_selinux_get (parentfd, name, st, 0);
1921 xattrs_xattrs_get (parentfd, name, st, 0);
1923 else if (S_ISFIFO (st->stat.st_mode))
1925 type = FIFOTYPE;
1926 xattrs_acls_get (parentfd, name, st, true);
1927 xattrs_selinux_get (parentfd, name, st, 0);
1928 xattrs_xattrs_get (parentfd, name, st, 0);
1930 else if (S_ISSOCK (st->stat.st_mode))
1932 WARNOPT (WARN_FILE_IGNORED,
1933 (0, 0, _("%s: socket ignored"), quotearg_colon (p)));
1934 return;
1936 else if (S_ISDOOR (st->stat.st_mode))
1938 WARNOPT (WARN_FILE_IGNORED,
1939 (0, 0, _("%s: door ignored"), quotearg_colon (p)));
1940 return;
1942 else
1944 unknown_file_error (p);
1945 return;
1948 if (archive_format == V7_FORMAT)
1950 unknown_file_error (p);
1951 return;
1954 block_ordinal = current_block_ordinal ();
1955 st->stat.st_size = 0; /* force 0 size */
1956 header = start_header (st);
1957 if (!header)
1958 return;
1959 header->header.typeflag = type;
1960 finish_header (st, header, block_ordinal);
1961 if (remove_files_option)
1962 queue_deferred_unlink (p, false);
1965 /* Dump a file, recursively. PARENT describes the file's parent
1966 directory, NAME is the file's name relative to PARENT, and FULLNAME
1967 its full name, possibly relative to the working directory. NAME
1968 may contain slashes at the top level of invocation. */
1970 void
1971 dump_file (struct tar_stat_info *parent, char const *name,
1972 char const *fullname)
1974 struct tar_stat_info st;
1975 tar_stat_init (&st);
1976 st.parent = parent;
1977 dump_file0 (&st, name, fullname);
1978 if (parent && listed_incremental_option)
1979 update_parent_directory (parent);
1980 tar_stat_destroy (&st);