1 /* Create a tar archive.
3 Copyright 1985-2022 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. */
24 #include <areadlink.h>
30 /* Error number to use when an impostor is discovered.
31 Pretend the impostor isn't there. */
32 enum { IMPOSTOR_ERRNO
= ENOENT
};
46 enum exclusion_tag_type type
;
47 bool (*predicate
) (int fd
);
48 struct exclusion_tag
*next
;
51 static struct exclusion_tag
*exclusion_tags
;
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
;
61 tag
->predicate
= predicate
;
62 tag
->length
= strlen (name
);
67 exclusion_tag_warning (const char *dirname
, const char *tagname
,
71 WARNOPT (WARN_CACHEDIR
,
73 _("%s: contains a cache directory tag %s; %s"),
74 quotearg_colon (dirname
),
75 quotearg_n (1, tagname
),
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
);
89 bool satisfied
= !tag
->predicate
|| tag
->predicate (tagfd
);
94 *tag_file_name
= tag
->name
;
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)
114 cachedir_file_p (int fd
)
116 char tagbuf
[CACHEDIR_SIGNATURE_SIZE
];
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 \
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. */
140 to_octal (uintmax_t value
, char *where
, size_t size
)
147 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
153 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
154 NUL unless SRC is LEN or more bytes long. */
157 tar_copy_str (char *dst
, const char *src
, size_t len
)
160 for (i
= 0; i
< len
; i
++)
161 if (! (dst
[i
] = src
[i
]))
165 /* Same as tar_copy_str, but always terminate with NUL if using
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
)
176 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
177 tar headers. NEGATIVE is true iff VALUE was negative before being
178 cast to uintmax_t. Output to buffer WHERE with size SIZE.
179 The result is undefined if SIZE is 0 or if VALUE is too large to
183 to_base256 (bool negative
, uintmax_t value
, char *where
, size_t size
)
186 uintmax_t propagated_sign_bits
=
187 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
192 where
[--i
] = v
& ((1 << LG_256
) - 1);
193 v
= propagated_sign_bits
| (v
>> LG_256
);
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))
208 to_chars (bool negative
, uintmax_t value
, size_t valsize
,
209 uintmax_t (*substitute
) (bool *),
210 char *where
, size_t size
, const char *type
);
213 to_chars_subst (bool negative
, bool gnu_format
, uintmax_t value
, size_t valsize
,
214 uintmax_t (*substitute
) (bool *),
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
;
229 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
230 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
239 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
244 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
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);
262 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
263 value_string
, type
, minval_string
, maxval_string
,
265 return to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
268 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
269 value_string
, type
, minval_string
, maxval_string
));
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 true 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) '\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. */
288 to_chars (bool negative
, uintmax_t value
, size_t valsize
,
289 uintmax_t (*substitute
) (bool *),
290 char *where
, size_t size
, const char *type
)
292 bool 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);
304 /* Try to cope with the number by using traditional GNU format
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] = (char) (negative
? -1 : 1 << (LG_256
- 1));
312 to_base256 (negative
, value
, where
+ 1, size
- 1);
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 bool warned_once
;
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),
335 /* Otherwise fall back to substitution, if possible: */
338 substitute
= NULL
; /* No substitution for formats, other than GNU */
340 return to_chars_subst (negative
, gnu_format
, value
, valsize
, substitute
,
345 gid_substitute (bool *negative
)
351 static gid_t gid_nobody
;
352 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
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");
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");
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");
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. */
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
)
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");
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");
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");
431 uid_substitute (bool *negative
)
437 static uid_t uid_nobody
;
438 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
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");
453 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
455 return to_chars (false, v
, sizeof v
, 0, p
, s
, "uintmax_t");
459 string_to_chars (char const *str
, char *p
, size_t s
)
461 tar_copy_str (p
, str
, s
);
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
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 */
476 file_dumpable_p (struct stat
const *st
)
478 if (S_ISDIR (st
->st_mode
))
480 if (! (S_ISREG (st
->st_mode
) || S_ISCTG (st
->st_mode
)))
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. */
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 */
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
< 0 ? 0 : min (t
, MAX_OCTAL_VAL (header
->header
.mtime
)),
516 header
->header
.mtime
);
517 MODE_TO_CHARS (S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, header
->header
.mode
);
518 UID_TO_CHARS (0, header
->header
.uid
);
519 GID_TO_CHARS (0, header
->header
.gid
);
520 memcpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
521 memcpy (header
->header
.version
, TVERSION
, TVERSLEN
);
525 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
529 write_short_name (struct tar_stat_info
*st
)
531 union block
*header
= find_next_block ();
532 memset (header
->buffer
, 0, sizeof (union block
));
533 tar_name_copy_str (header
->header
.name
, st
->file_name
, NAME_FIELD_SIZE
);
537 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
539 write_gnu_long_link (struct tar_stat_info
*st
, const char *p
, char type
)
541 size_t size
= strlen (p
) + 1;
545 header
= start_private_header ("././@LongLink", size
, 0);
546 if (! numeric_owner_option
)
548 static char *uname
, *gname
;
551 uid_to_uname (0, &uname
);
552 gid_to_gname (0, &gname
);
554 UNAME_TO_CHARS (uname
, header
->header
.uname
);
555 GNAME_TO_CHARS (gname
, header
->header
.gname
);
558 strcpy (header
->buffer
+ offsetof (struct posix_header
, magic
),
560 header
->header
.typeflag
= type
;
561 finish_header (st
, header
, -1);
563 header
= find_next_block ();
565 bufsize
= available_space_after (header
);
567 while (bufsize
< size
)
569 memcpy (header
->buffer
, p
, bufsize
);
572 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
573 header
= find_next_block ();
574 bufsize
= available_space_after (header
);
576 memcpy (header
->buffer
, p
, size
);
577 memset (header
->buffer
+ size
, 0, bufsize
- size
);
578 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
582 split_long_name (const char *name
, size_t length
)
586 if (length
> PREFIX_FIELD_SIZE
+ 1)
587 length
= PREFIX_FIELD_SIZE
+ 1;
588 else if (ISSLASH (name
[length
- 1]))
590 for (i
= length
- 1; i
> 0; i
--)
591 if (ISSLASH (name
[i
]))
597 write_ustar_long_name (const char *name
)
599 size_t length
= strlen (name
);
603 if (length
> PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1)
605 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
606 quotearg_colon (name
),
607 PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1));
611 i
= split_long_name (name
, length
);
612 if (i
== 0 || (nlen
= length
- i
- 1) > NAME_FIELD_SIZE
|| nlen
== 0)
615 _("%s: file name is too long (cannot be split); not dumped"),
616 quotearg_colon (name
)));
620 header
= find_next_block ();
621 memset (header
->buffer
, 0, sizeof (header
->buffer
));
622 memcpy (header
->header
.prefix
, name
, i
);
623 memcpy (header
->header
.name
, name
+ i
+ 1, length
- i
- 1);
628 /* Write a long link name, depending on the current archive format */
630 write_long_link (struct tar_stat_info
*st
)
632 switch (archive_format
)
635 xheader_store ("linkpath", st
, NULL
);
638 case V7_FORMAT
: /* old V7 tar format */
642 _("%s: link name is too long; not dumped"),
643 quotearg_colon (st
->link_name
)));
648 write_gnu_long_link (st
, st
->link_name
, GNUTYPE_LONGLINK
);
657 write_long_name (struct tar_stat_info
*st
)
659 switch (archive_format
)
662 xheader_store ("path", st
, NULL
);
666 if (strlen (st
->file_name
) > NAME_FIELD_SIZE
-1)
668 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
669 quotearg_colon (st
->file_name
),
670 NAME_FIELD_SIZE
- 1));
677 return write_ustar_long_name (st
->file_name
);
681 write_gnu_long_link (st
, st
->file_name
, GNUTYPE_LONGNAME
);
687 return write_short_name (st
);
691 write_extended (bool global
, struct tar_stat_info
*st
, union block
*old_header
)
693 union block
*header
, hp
;
698 if (st
->xhdr
.buffer
|| st
->xhdr
.stk
== NULL
)
701 xheader_finish (&st
->xhdr
);
702 memcpy (hp
.buffer
, old_header
, sizeof (hp
));
706 p
= xheader_ghdr_name ();
707 t
= start_time
.tv_sec
;
712 p
= xheader_xhdr_name (st
);
713 t
= set_mtime_option
? mtime_option
.tv_sec
: st
->stat
.st_mtime
;
715 xheader_write (type
, p
, t
, &st
->xhdr
);
717 header
= find_next_block ();
718 memcpy (header
, &hp
.buffer
, sizeof (hp
.buffer
));
723 write_header_name (struct tar_stat_info
*st
)
725 if (archive_format
== POSIX_FORMAT
&& !string_ascii_p (st
->file_name
))
727 xheader_store ("path", st
, NULL
);
728 return write_short_name (st
);
730 else if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
731 < strlen (st
->file_name
))
732 return write_long_name (st
);
734 return write_short_name (st
);
738 /* Header handling. */
740 /* Make a header block for the file whose stat info is st,
741 and return its address. */
744 start_header (struct tar_stat_info
*st
)
747 char const *uname
= NULL
;
748 char const *gname
= NULL
;
750 header
= write_header_name (st
);
754 /* Override some stat fields, if requested to do so. */
755 owner_map_translate (st
->stat
.st_uid
, &st
->stat
.st_uid
, &uname
);
756 group_map_translate (st
->stat
.st_gid
, &st
->stat
.st_gid
, &gname
);
760 ((st
->stat
.st_mode
& ~MODE_ALL
)
761 | mode_adjust (st
->stat
.st_mode
, S_ISDIR (st
->stat
.st_mode
) != 0,
762 initial_umask
, mode_option
, NULL
));
764 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
765 for a few tars and came up with the following interoperability
769 1 2 3 4 5 6 7 8 9 READER
770 . . . . . . . . . 1 = SunOS 4.2 tar
771 # . . # # . . # # 2 = NEC SVR4.0.2 tar
772 . . . # # . . # . 3 = Solaris 2.1 tar
773 . . . . . . . . . 4 = GNU tar 1.11.1
774 . . . . . . . . . 5 = HP-UX 8.07 tar
775 . . . . . . . . . 6 = Ultrix 4.1
776 . . . . . . . . . 7 = AIX 3.2
777 . . . . . . . . . 8 = Hitachi HI-UX 1.03
778 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
781 # = "impossible file type"
783 The following mask for old archive removes the '#'s in column 4
784 above, thus making GNU tar both a universal donor and a universal
785 acceptor for Paul's test. */
787 if (archive_format
== V7_FORMAT
|| archive_format
== USTAR_FORMAT
)
788 MODE_TO_CHARS (st
->stat
.st_mode
& MODE_ALL
, header
->header
.mode
);
790 MODE_TO_CHARS (st
->stat
.st_mode
, header
->header
.mode
);
793 uid_t uid
= st
->stat
.st_uid
;
794 if (archive_format
== POSIX_FORMAT
795 && MAX_OCTAL_VAL (header
->header
.uid
) < uid
)
797 xheader_store ("uid", st
, NULL
);
800 if (!UID_TO_CHARS (uid
, header
->header
.uid
))
805 gid_t gid
= st
->stat
.st_gid
;
806 if (archive_format
== POSIX_FORMAT
807 && MAX_OCTAL_VAL (header
->header
.gid
) < gid
)
809 xheader_store ("gid", st
, NULL
);
812 if (!GID_TO_CHARS (gid
, header
->header
.gid
))
817 off_t size
= st
->stat
.st_size
;
818 if (archive_format
== POSIX_FORMAT
819 && MAX_OCTAL_VAL (header
->header
.size
) < size
)
821 xheader_store ("size", st
, NULL
);
824 if (!OFF_TO_CHARS (size
, header
->header
.size
))
829 struct timespec mtime
;
831 switch (set_mtime_option
)
838 mtime
= mtime_option
;
842 mtime
= timespec_cmp (st
->mtime
, mtime_option
) > 0
843 ? mtime_option
: st
->mtime
;
847 if (archive_format
== POSIX_FORMAT
)
849 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
850 || mtime
.tv_nsec
!= 0)
851 xheader_store ("mtime", st
, &mtime
);
852 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
)
855 if (!TIME_TO_CHARS (mtime
.tv_sec
, header
->header
.mtime
))
860 if (S_ISCHR (st
->stat
.st_mode
)
861 || S_ISBLK (st
->stat
.st_mode
))
863 major_t devmajor
= major (st
->stat
.st_rdev
);
864 minor_t devminor
= minor (st
->stat
.st_rdev
);
866 if (archive_format
== POSIX_FORMAT
867 && MAX_OCTAL_VAL (header
->header
.devmajor
) < devmajor
)
869 xheader_store ("devmajor", st
, NULL
);
872 if (!MAJOR_TO_CHARS (devmajor
, header
->header
.devmajor
))
875 if (archive_format
== POSIX_FORMAT
876 && MAX_OCTAL_VAL (header
->header
.devminor
) < devminor
)
878 xheader_store ("devminor", st
, NULL
);
881 if (!MINOR_TO_CHARS (devminor
, header
->header
.devminor
))
885 if (archive_format
== POSIX_FORMAT
)
887 xheader_store ("atime", st
, NULL
);
888 xheader_store ("ctime", st
, NULL
);
890 else if (incremental_option
)
891 if (archive_format
== OLDGNU_FORMAT
|| archive_format
== GNU_FORMAT
)
893 TIME_TO_CHARS (st
->atime
.tv_sec
, header
->oldgnu_header
.atime
);
894 TIME_TO_CHARS (st
->ctime
.tv_sec
, header
->oldgnu_header
.ctime
);
897 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
899 switch (archive_format
)
905 case GNU_FORMAT
: /*FIXME?*/
906 /* Overwrite header->header.magic and header.version in one blow. */
907 strcpy (header
->buffer
+ offsetof (struct posix_header
, magic
),
913 memcpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
914 memcpy (header
->header
.version
, TVERSION
, TVERSLEN
);
921 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
923 /* header->header.[ug]name are left as the empty string. */
928 st
->uname
= xstrdup (uname
);
930 uid_to_uname (st
->stat
.st_uid
, &st
->uname
);
933 st
->gname
= xstrdup (gname
);
935 gid_to_gname (st
->stat
.st_gid
, &st
->gname
);
937 if (archive_format
== POSIX_FORMAT
938 && (strlen (st
->uname
) > UNAME_FIELD_SIZE
939 || !string_ascii_p (st
->uname
)))
940 xheader_store ("uname", st
, NULL
);
941 UNAME_TO_CHARS (st
->uname
, header
->header
.uname
);
943 if (archive_format
== POSIX_FORMAT
944 && (strlen (st
->gname
) > GNAME_FIELD_SIZE
945 || !string_ascii_p (st
->gname
)))
946 xheader_store ("gname", st
, NULL
);
947 GNAME_TO_CHARS (st
->gname
, header
->header
.gname
);
950 if (archive_format
== POSIX_FORMAT
)
955 xheader_store ("SCHILY.acl.access", st
, NULL
);
957 xheader_store ("SCHILY.acl.default", st
, NULL
);
959 if ((selinux_context_option
> 0) && st
->cntx_name
)
960 xheader_store ("RHT.security.selinux", st
, NULL
);
961 if (xattrs_option
> 0)
964 for (i
= 0; i
< st
->xattr_map
.xm_size
; i
++)
965 xheader_store (st
->xattr_map
.xm_map
[i
].xkey
, st
, &i
);
973 simple_finish_header (union block
*header
)
979 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
983 for (i
= sizeof *header
; i
-- != 0; )
984 /* We can't use unsigned char here because of old compilers, e.g. V7. */
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. */
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 */
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
);
1029 pad_archive (off_t size_left
)
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
;
1048 block_ordinal
= current_block_ordinal ();
1049 blk
= start_header (st
);
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
;
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
;
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
,
1094 ngettext ("%s: File shrank by %s byte; padding with zeros",
1095 "%s: File shrank by %s bytes; padding with zeros",
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. */
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
);
1126 info_attach_exclist (st
);
1128 if (incremental_option
&& archive_format
!= POSIX_FORMAT
)
1129 blk
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1130 else /* if (standard_option) */
1131 blk
->header
.typeflag
= DIRTYPE
;
1133 /* If we're gnudumping, we aren't done yet so don't close it. */
1135 if (!incremental_option
)
1136 finish_header (st
, blk
, block_ordinal
);
1137 else if (gnu_list_name
->directory
)
1139 if (archive_format
== POSIX_FORMAT
)
1141 xheader_store ("GNU.dumpdir", st
,
1142 safe_directory_contents (gnu_list_name
->directory
));
1143 finish_header (st
, blk
, block_ordinal
);
1151 const char *buffer
, *p_buffer
;
1153 block_ordinal
= current_block_ordinal ();
1154 buffer
= safe_directory_contents (gnu_list_name
->directory
);
1155 totsize
= dumpdir_size (buffer
);
1156 OFF_TO_CHARS (totsize
, blk
->header
.size
);
1157 finish_header (st
, blk
, block_ordinal
);
1159 size_left
= totsize
;
1161 mv_begin_write (st
->file_name
, totsize
, totsize
);
1162 while (size_left
> 0)
1164 blk
= find_next_block ();
1165 bufsize
= available_space_after (blk
);
1166 if (size_left
< bufsize
)
1168 bufsize
= size_left
;
1169 count
= bufsize
% BLOCKSIZE
;
1171 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1173 memcpy (blk
->buffer
, p_buffer
, bufsize
);
1174 size_left
-= bufsize
;
1175 p_buffer
+= bufsize
;
1176 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1182 if (!recursion_option
)
1185 if (one_file_system_option
1187 && st
->parent
->stat
.st_dev
!= st
->stat
.st_dev
)
1192 _("%s: file is on a different filesystem; not dumped"),
1193 quotearg_colon (st
->orig_file_name
)));
1200 switch (check_exclusion_tags (st
, &tag_file_name
))
1202 case exclusion_tag_all
:
1203 /* Handled in dump_file0 */
1206 case exclusion_tag_none
:
1212 name_buf
= xstrdup (st
->orig_file_name
);
1213 name_size
= name_len
= strlen (name_buf
);
1215 /* Now output all the files in the directory. */
1216 for (entry
= directory
; (entry_len
= strlen (entry
)) != 0;
1217 entry
+= entry_len
+ 1)
1219 if (name_size
< name_len
+ entry_len
)
1221 name_size
= name_len
+ entry_len
;
1222 name_buf
= xrealloc (name_buf
, name_size
+ 1);
1224 strcpy (name_buf
+ name_len
, entry
);
1225 if (!excluded_name (name_buf
, st
))
1226 dump_file (st
, entry
, name_buf
);
1233 case exclusion_tag_contents
:
1234 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1235 _("contents not dumped"));
1236 name_size
= strlen (st
->orig_file_name
) + strlen (tag_file_name
) + 1;
1237 name_buf
= xmalloc (name_size
);
1238 strcpy (name_buf
, st
->orig_file_name
);
1239 strcat (name_buf
, tag_file_name
);
1240 dump_file (st
, tag_file_name
, name_buf
);
1244 case exclusion_tag_under
:
1245 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1246 _("contents not dumped"));
1252 /* Ensure exactly one trailing slash. */
1254 ensure_slash (char **pstr
)
1256 size_t len
= strlen (*pstr
);
1257 while (len
>= 1 && ISSLASH ((*pstr
)[len
- 1]))
1259 if (!ISSLASH ((*pstr
)[len
]))
1260 *pstr
= xrealloc (*pstr
, len
+ 2);
1261 (*pstr
)[len
++] = '/';
1262 (*pstr
)[len
] = '\0';
1265 /* If we just ran out of file descriptors, release a file descriptor
1266 in the directory chain somewhere leading from DIR->parent->parent
1267 up through the root. Return true if successful, false (preserving
1268 errno == EMFILE) otherwise.
1270 Do not release DIR's file descriptor, or DIR's parent, as other
1271 code assumes that they work. On some operating systems, another
1272 process can claim file descriptor resources as we release them, and
1273 some calls or their emulations require multiple file descriptors,
1274 so callers should not give up if a single release doesn't work. */
1277 open_failure_recover (struct tar_stat_info
const *dir
)
1279 if (errno
== EMFILE
&& dir
&& dir
->parent
)
1281 struct tar_stat_info
*p
;
1282 for (p
= dir
->parent
->parent
; p
; p
= p
->parent
)
1283 if (0 < p
->fd
&& (! p
->parent
|| p
->parent
->fd
<= 0))
1294 /* Return the directory entries of ST, in a dynamically allocated buffer,
1295 each entry followed by '\0' and the last followed by an extra '\0'.
1296 Return null on failure, setting errno. */
1298 get_directory_entries (struct tar_stat_info
*st
)
1300 while (! (st
->dirstream
= fdopendir (st
->fd
)))
1301 if (! open_failure_recover (st
))
1303 return streamsavedir (st
->dirstream
, savedir_sort_order
);
1306 /* Dump the directory ST. Return true if successful, false (emitting
1307 diagnostics) otherwise. Get ST's entries, recurse through its
1308 subdirectories, and clean up file descriptors afterwards. */
1310 dump_dir (struct tar_stat_info
*st
)
1312 char *directory
= get_directory_entries (st
);
1315 savedir_diag (st
->orig_file_name
);
1319 dump_dir0 (st
, directory
);
1321 restore_parent_fd (st
);
1327 /* Number of links a file can have without having to be entered into
1328 the link table. Typically this is 1, but in trickier circumstances
1330 static nlink_t trivial_link_count
;
1333 /* Main functions of this module. */
1336 create_archive (void)
1338 struct name
const *p
;
1340 trivial_link_count
= filename_args
!= FILES_MANY
&& ! dereference_option
;
1342 open_archive (ACCESS_WRITE
);
1343 buffer_write_global_xheader ();
1345 if (incremental_option
)
1347 size_t buffer_size
= 0;
1348 char *buffer
= NULL
;
1351 collect_and_sort_names ();
1353 while ((p
= name_from_list ()) != NULL
)
1354 if (!excluded_name (p
->name
, NULL
))
1355 dump_file (0, p
->name
, p
->name
);
1358 while ((p
= name_from_list ()) != NULL
)
1359 if (!excluded_name (p
->name
, NULL
))
1361 struct tar_stat_info st
;
1362 size_t plen
= strlen (p
->name
);
1363 while (buffer_size
<= plen
)
1364 buffer
= x2realloc (buffer
, &buffer_size
);
1365 memcpy (buffer
, p
->name
, plen
);
1366 if (! ISSLASH (buffer
[plen
- 1]))
1367 buffer
[plen
++] = DIRECTORY_SEPARATOR
;
1368 tar_stat_init (&st
);
1369 q
= directory_contents (p
->directory
);
1373 size_t qlen
= strlen (q
);
1376 if (! st
.orig_file_name
)
1378 int fd
= openat (chdir_fd
, p
->name
,
1379 open_searchdir_flags
);
1382 file_removed_diag (p
->name
, !p
->parent
,
1387 if (fstat (fd
, &st
.stat
) != 0)
1389 file_removed_diag (p
->name
, !p
->parent
,
1393 st
.orig_file_name
= xstrdup (p
->name
);
1395 while (buffer_size
< plen
+ qlen
)
1396 buffer
= x2realloc (buffer
, &buffer_size
);
1397 strcpy (buffer
+ plen
, q
+ 1);
1398 dump_file (&st
, q
+ 1, buffer
);
1402 tar_stat_destroy (&st
);
1409 while ((name
= name_next (1)) != NULL
)
1410 if (!excluded_name (name
, NULL
))
1411 dump_file (0, name
, name
);
1416 finish_deferred_unlinks ();
1417 if (listed_incremental_option
)
1418 write_directory_file ();
1422 /* Calculate the hash of a link. */
1424 hash_link (void const *entry
, size_t n_buckets
)
1426 struct link
const *l
= entry
;
1427 uintmax_t num
= l
->dev
^ l
->ino
;
1428 return num
% n_buckets
;
1431 /* Compare two links for equality. */
1433 compare_links (void const *entry1
, void const *entry2
)
1435 struct link
const *link1
= entry1
;
1436 struct link
const *link2
= entry2
;
1437 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
1441 unknown_file_error (char const *p
)
1443 WARNOPT (WARN_FILE_IGNORED
,
1444 (0, 0, _("%s: Unknown file type; file ignored"),
1445 quotearg_colon (p
)));
1446 if (!ignore_failed_read_option
)
1447 set_exit_status (TAREXIT_FAILURE
);
1451 /* Handling of hard links */
1453 /* Table of all non-directories that we've written so far. Any time
1454 we see another, we check the table and avoid dumping the data
1455 again if we've done it once already. */
1456 static Hash_table
*link_table
;
1458 /* Try to dump stat as a hard link to another file in the archive.
1459 Return true if successful. */
1461 dump_hard_link (struct tar_stat_info
*st
)
1464 && (trivial_link_count
< st
->stat
.st_nlink
|| remove_files_option
))
1467 struct link
*duplicate
;
1468 off_t block_ordinal
;
1471 lp
.ino
= st
->stat
.st_ino
;
1472 lp
.dev
= st
->stat
.st_dev
;
1474 if ((duplicate
= hash_lookup (link_table
, &lp
)))
1476 /* We found a link. */
1477 char const *link_name
= safer_name_suffix (duplicate
->name
, true,
1478 absolute_names_option
);
1479 if (duplicate
->nlink
)
1482 block_ordinal
= current_block_ordinal ();
1483 assign_string (&st
->link_name
, link_name
);
1484 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
1485 < strlen (link_name
))
1486 write_long_link (st
);
1488 st
->stat
.st_size
= 0;
1489 blk
= start_header (st
);
1492 tar_copy_str (blk
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1494 blk
->header
.typeflag
= LNKTYPE
;
1495 finish_header (st
, blk
, block_ordinal
);
1497 if (remove_files_option
)
1498 queue_deferred_unlink (st
->orig_file_name
, false);
1507 file_count_links (struct tar_stat_info
*st
)
1509 if (hard_dereference_option
)
1511 if (trivial_link_count
< st
->stat
.st_nlink
)
1513 struct link
*duplicate
;
1514 char *linkname
= NULL
;
1517 assign_string (&linkname
, safer_name_suffix (st
->orig_file_name
, true,
1518 absolute_names_option
));
1519 transform_name (&linkname
, XFORM_LINK
);
1521 lp
= xmalloc (offsetof (struct link
, name
)
1522 + strlen (linkname
) + 1);
1523 lp
->ino
= st
->stat
.st_ino
;
1524 lp
->dev
= st
->stat
.st_dev
;
1525 lp
->nlink
= st
->stat
.st_nlink
;
1526 strcpy (lp
->name
, linkname
);
1530 || (link_table
= hash_initialize (0, 0, hash_link
,
1532 && (duplicate
= hash_insert (link_table
, lp
))))
1535 if (duplicate
!= lp
)
1541 /* For each dumped file, check if all its links were dumped. Emit
1542 warnings if it is not so. */
1551 for (lp
= hash_get_first (link_table
); lp
;
1552 lp
= hash_get_next (link_table
, lp
))
1556 WARN ((0, 0, _("Missing links to %s."), quote (lp
->name
)));
1561 /* Assuming DIR is the working directory, open FILE, using FLAGS to
1562 control the open. A null DIR means to use ".". If we are low on
1563 file descriptors, try to release one or more from DIR's parents to
1566 subfile_open (struct tar_stat_info
const *dir
, char const *file
, int flags
)
1570 static bool initialized
;
1573 /* Initialize any tables that might be needed when file
1574 descriptors are exhausted, and whose initialization might
1575 require a file descriptor. This includes the system message
1576 catalog and tar's message catalog. */
1582 while ((fd
= openat (dir
? dir
->fd
: chdir_fd
, file
, flags
)) < 0
1583 && open_failure_recover (dir
))
1588 /* Restore the file descriptor for ST->parent, if it was temporarily
1589 closed to conserve file descriptors. On failure, set the file
1590 descriptor to the negative of the corresponding errno value. Call
1591 this every time a subdirectory is ascended from. */
1593 restore_parent_fd (struct tar_stat_info
const *st
)
1595 struct tar_stat_info
*parent
= st
->parent
;
1596 if (parent
&& ! parent
->fd
)
1598 int parentfd
= openat (st
->fd
, "..", open_searchdir_flags
);
1599 struct stat parentstat
;
1603 else if (! (fstat (parentfd
, &parentstat
) == 0
1604 && parent
->stat
.st_ino
== parentstat
.st_ino
1605 && parent
->stat
.st_dev
== parentstat
.st_dev
))
1608 parentfd
= IMPOSTOR_ERRNO
;
1613 int origfd
= openat (chdir_fd
, parent
->orig_file_name
,
1614 open_searchdir_flags
);
1617 if (fstat (parentfd
, &parentstat
) == 0
1618 && parent
->stat
.st_ino
== parentstat
.st_ino
1619 && parent
->stat
.st_dev
== parentstat
.st_dev
)
1626 parent
->fd
= parentfd
;
1630 /* Dump a single file, recursing on directories. ST is the file's
1631 status info, NAME its name relative to the parent directory, and P
1632 its full name (which may be relative to the working directory). */
1634 /* FIXME: One should make sure that for *every* path leading to setting
1635 exit_status to failure, a clear diagnostic has been issued. */
1638 dump_file0 (struct tar_stat_info
*st
, char const *name
, char const *p
)
1640 union block
*header
;
1642 off_t block_ordinal
= -1;
1645 struct tar_stat_info
const *parent
= st
->parent
;
1646 bool top_level
= ! parent
;
1647 int parentfd
= top_level
? chdir_fd
: parent
->fd
;
1648 void (*diag
) (char const *) = 0;
1650 if (interactive_option
&& !confirm ("add", p
))
1653 assign_string (&st
->orig_file_name
, p
);
1654 assign_string (&st
->file_name
,
1655 safer_name_suffix (p
, false, absolute_names_option
));
1657 transform_name (&st
->file_name
, XFORM_REGFILE
);
1659 if (parentfd
< 0 && ! top_level
)
1664 else if (fstatat (parentfd
, name
, &st
->stat
, fstatat_flags
) != 0)
1666 else if (file_dumpable_p (&st
->stat
))
1668 fd
= subfile_open (parent
, name
, open_read_flags
);
1674 if (fstat (fd
, &st
->stat
) != 0)
1680 file_removed_diag (p
, top_level
, diag
);
1684 struct stat st1
= st
->stat
;
1685 st
->archive_file_size
= st
->stat
.st_size
;
1686 st
->atime
= get_stat_atime (&st
->stat
);
1687 st
->mtime
= get_stat_mtime (&st
->stat
);
1688 st
->ctime
= get_stat_ctime (&st
->stat
);
1691 if (S_ISHIDDEN (st
->stat
.st_mode
))
1693 char *new = (char *) alloca (strlen (p
) + 2);
1703 /* See if we want only new files, and check if this one is too old to
1706 This check is omitted if incremental_option is set *and* the
1707 requested file is not explicitly listed in the command line. */
1709 if (! (incremental_option
&& ! top_level
)
1710 && !S_ISDIR (st
->stat
.st_mode
)
1711 && OLDER_TAR_STAT_TIME (*st
, m
)
1712 && (!after_date_option
|| OLDER_TAR_STAT_TIME (*st
, c
)))
1714 if (!incremental_option
&& verbose_option
)
1715 WARNOPT (WARN_FILE_UNCHANGED
,
1716 (0, 0, _("%s: file is unchanged; not dumped"),
1717 quotearg_colon (p
)));
1721 /* See if we are trying to dump the archive. */
1722 if (sys_file_is_archive (st
))
1724 WARNOPT (WARN_IGNORE_ARCHIVE
,
1725 (0, 0, _("%s: archive cannot contain itself; not dumped"),
1726 quotearg_colon (p
)));
1730 is_dir
= S_ISDIR (st
->stat
.st_mode
) != 0;
1732 if (!is_dir
&& dump_hard_link (st
))
1735 if (is_dir
|| S_ISREG (st
->stat
.st_mode
) || S_ISCTG (st
->stat
.st_mode
))
1740 xattrs_acls_get (parentfd
, name
, st
, 0, !is_dir
);
1741 xattrs_selinux_get (parentfd
, name
, st
, fd
);
1742 xattrs_xattrs_get (parentfd
, name
, st
, fd
);
1746 const char *tag_file_name
;
1747 ensure_slash (&st
->orig_file_name
);
1748 ensure_slash (&st
->file_name
);
1750 if (check_exclusion_tags (st
, &tag_file_name
) == exclusion_tag_all
)
1752 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1753 _("directory not dumped"));
1760 parentfd
= top_level
? chdir_fd
: parent
->fd
;
1764 enum dump_status status
;
1766 if (fd
&& sparse_option
&& ST_IS_SPARSE (st
->stat
))
1768 status
= sparse_dump_file (fd
, st
);
1769 if (status
== dump_status_not_implemented
)
1770 status
= dump_regular_file (fd
, st
);
1773 status
= dump_regular_file (fd
, st
);
1777 case dump_status_ok
:
1778 case dump_status_short
:
1779 file_count_links (st
);
1782 case dump_status_fail
:
1785 case dump_status_not_implemented
:
1789 ok
= status
== dump_status_ok
;
1801 if (parentfd
< 0 && ! top_level
)
1808 ok
= fstat (fd
, &st2
) == 0;
1811 file_removed_diag (p
, top_level
, stat_diag
);
1816 /* Heuristically check whether the file is the same in all
1817 attributes that tar cares about and can easily check.
1818 Although the check is not perfect since it does not
1819 consult file contents, it is typically good enough.
1820 Do not check atime which is saved only to replace it later.
1821 Do not check ctime where changes might be benign (e.g.,
1822 another process creates a hard link to the file). */
1824 /* If the file's user ID, group ID or mode changed, tar may
1825 have output the wrong info for the file. */
1826 ok
&= st1
.st_uid
== st2
.st_uid
;
1827 ok
&= st1
.st_gid
== st2
.st_gid
;
1828 ok
&= st1
.st_mode
== st2
.st_mode
;
1830 /* Likewise for the file's mtime, but skip this check if it
1831 is a directory possibly updated by --remove-files. */
1832 if (! (is_dir
&& remove_files_option
))
1833 ok
&= ! timespec_cmp (get_stat_mtime (&st1
),
1834 get_stat_mtime (&st2
));
1836 /* Likewise for the file's size, but skip this check if it
1837 is a directory as tar does not output directory sizes.
1838 Although dump_regular_file caught regular file shrinkage,
1839 it shouldn't hurt to check for shrinkage again now;
1840 plus, the file may have grown. */
1842 ok
&= st1
.st_size
== st2
.st_size
;
1846 WARNOPT (WARN_FILE_CHANGED
,
1847 (0, 0, _("%s: file changed as we read it"),
1848 quotearg_colon (p
)));
1849 if (! ignore_failed_read_option
)
1850 set_exit_status (TAREXIT_DIFFERS
);
1852 else if (atime_preserve_option
== replace_atime_preserve
1853 && timespec_cmp (st
->atime
, get_stat_atime (&st2
)) != 0
1854 && set_file_atime (fd
, parentfd
, name
, st
->atime
) != 0)
1858 ok
&= tar_stat_close (st
);
1859 if (ok
&& remove_files_option
)
1860 queue_deferred_unlink (p
, is_dir
);
1864 #ifdef HAVE_READLINK
1865 else if (S_ISLNK (st
->stat
.st_mode
))
1867 st
->link_name
= areadlinkat_with_size (parentfd
, name
, st
->stat
.st_size
);
1870 if (errno
== ENOMEM
)
1872 file_removed_diag (p
, top_level
, readlink_diag
);
1875 transform_name (&st
->link_name
, XFORM_SYMLINK
);
1876 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
1877 < strlen (st
->link_name
))
1878 write_long_link (st
);
1880 xattrs_selinux_get (parentfd
, name
, st
, 0);
1881 xattrs_xattrs_get (parentfd
, name
, st
, 0);
1883 block_ordinal
= current_block_ordinal ();
1884 st
->stat
.st_size
= 0; /* force 0 size on symlink */
1885 header
= start_header (st
);
1888 tar_copy_str (header
->header
.linkname
, st
->link_name
, NAME_FIELD_SIZE
);
1889 header
->header
.typeflag
= SYMTYPE
;
1890 finish_header (st
, header
, block_ordinal
);
1891 /* nothing more to do to it */
1893 if (remove_files_option
)
1894 queue_deferred_unlink (p
, false);
1896 file_count_links (st
);
1900 else if (S_ISCHR (st
->stat
.st_mode
))
1903 xattrs_acls_get (parentfd
, name
, st
, 0, true);
1904 xattrs_selinux_get (parentfd
, name
, st
, 0);
1905 xattrs_xattrs_get (parentfd
, name
, st
, 0);
1907 else if (S_ISBLK (st
->stat
.st_mode
))
1910 xattrs_acls_get (parentfd
, name
, st
, 0, true);
1911 xattrs_selinux_get (parentfd
, name
, st
, 0);
1912 xattrs_xattrs_get (parentfd
, name
, st
, 0);
1914 else if (S_ISFIFO (st
->stat
.st_mode
))
1917 xattrs_acls_get (parentfd
, name
, st
, 0, true);
1918 xattrs_selinux_get (parentfd
, name
, st
, 0);
1919 xattrs_xattrs_get (parentfd
, name
, st
, 0);
1921 else if (S_ISSOCK (st
->stat
.st_mode
))
1923 WARNOPT (WARN_FILE_IGNORED
,
1924 (0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1927 else if (S_ISDOOR (st
->stat
.st_mode
))
1929 WARNOPT (WARN_FILE_IGNORED
,
1930 (0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1935 unknown_file_error (p
);
1939 if (archive_format
== V7_FORMAT
)
1941 unknown_file_error (p
);
1945 block_ordinal
= current_block_ordinal ();
1946 st
->stat
.st_size
= 0; /* force 0 size */
1947 header
= start_header (st
);
1950 header
->header
.typeflag
= type
;
1951 finish_header (st
, header
, block_ordinal
);
1952 if (remove_files_option
)
1953 queue_deferred_unlink (p
, false);
1956 /* Dump a file, recursively. PARENT describes the file's parent
1957 directory, NAME is the file's name relative to PARENT, and FULLNAME
1958 its full name, possibly relative to the working directory. NAME
1959 may contain slashes at the top level of invocation. */
1962 dump_file (struct tar_stat_info
*parent
, char const *name
,
1963 char const *fullname
)
1965 struct tar_stat_info st
;
1966 tar_stat_init (&st
);
1968 dump_file0 (&st
, name
, fullname
);
1969 if (parent
&& listed_incremental_option
)
1970 update_parent_directory (parent
);
1971 tar_stat_destroy (&st
);