1 /* Create a tar archive.
3 Copyright 1985, 1992-1994, 1996-1997, 1999-2001, 2003-2007,
4 2009-2010, 2012-2014, 2016-2017 Free Software Foundation, Inc.
6 This file is part of GNU tar.
8 GNU tar is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GNU tar is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 Written by John Gilmore, on 1985-08-25. */
25 #include <areadlink.h>
31 /* Error number to use when an impostor is discovered.
32 Pretend the impostor isn't there. */
33 enum { IMPOSTOR_ERRNO
= ENOENT
};
47 enum exclusion_tag_type type
;
48 bool (*predicate
) (int fd
);
49 struct exclusion_tag
*next
;
52 static struct exclusion_tag
*exclusion_tags
;
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
;
62 tag
->predicate
= predicate
;
63 tag
->length
= strlen (name
);
68 exclusion_tag_warning (const char *dirname
, const char *tagname
,
72 WARNOPT (WARN_CACHEDIR
,
74 _("%s: contains a cache directory tag %s; %s"),
75 quotearg_colon (dirname
),
76 quotearg_n (1, tagname
),
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
);
90 bool satisfied
= !tag
->predicate
|| tag
->predicate (tagfd
);
95 *tag_file_name
= tag
->name
;
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)
115 cachedir_file_p (int fd
)
117 char tagbuf
[CACHEDIR_SIGNATURE_SIZE
];
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 \
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. */
141 to_octal (uintmax_t value
, char *where
, size_t size
)
148 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
154 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
155 NUL unless SRC is LEN or more bytes long. */
158 tar_copy_str (char *dst
, const char *src
, size_t len
)
161 for (i
= 0; i
< len
; i
++)
162 if (! (dst
[i
] = src
[i
]))
166 /* Same as tar_copy_str, but always terminate with NUL if using
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
)
177 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
178 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
179 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
180 The result is undefined if SIZE is 0 or if VALUE is too large to
184 to_base256 (int negative
, uintmax_t value
, char *where
, size_t size
)
187 uintmax_t propagated_sign_bits
=
188 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
193 where
[--i
] = v
& ((1 << LG_256
) - 1);
194 v
= propagated_sign_bits
| (v
>> LG_256
);
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))
209 to_chars (int negative
, uintmax_t value
, size_t valsize
,
210 uintmax_t (*substitute
) (int *),
211 char *where
, size_t size
, const char *type
);
214 to_chars_subst (int negative
, int gnu_format
, uintmax_t value
, size_t valsize
,
215 uintmax_t (*substitute
) (int *),
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
;
230 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
231 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
240 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
245 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
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);
263 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
264 value_string
, type
, minval_string
, maxval_string
,
266 return to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
269 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
270 value_string
, type
, minval_string
, maxval_string
));
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 1 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 is nonzero) '\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. */
289 to_chars (int negative
, uintmax_t value
, size_t valsize
,
290 uintmax_t (*substitute
) (int *),
291 char *where
, size_t size
, const char *type
)
293 int 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);
305 /* Try to cope with the number by using traditional GNU format
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] = negative
? -1 : 1 << (LG_256
- 1);
313 to_base256 (negative
, value
, where
+ 1, size
- 1);
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 int warned_once
;
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),
336 /* Otherwise fall back to substitution, if possible: */
339 substitute
= NULL
; /* No substitution for formats, other than GNU */
341 return to_chars_subst (negative
, gnu_format
, value
, valsize
, substitute
,
346 gid_substitute (int *negative
)
352 static gid_t gid_nobody
;
353 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
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");
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");
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");
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. */
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
)
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");
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");
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");
432 uid_substitute (int *negative
)
438 static uid_t uid_nobody
;
439 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
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");
454 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
456 return to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
460 string_to_chars (char const *str
, char *p
, size_t s
)
462 tar_copy_str (p
, str
, s
);
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
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 */
477 file_dumpable_p (struct stat
const *st
)
479 if (S_ISDIR (st
->st_mode
))
481 if (! (S_ISREG (st
->st_mode
) || S_ISCTG (st
->st_mode
)))
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. */
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 */
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
);
526 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
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
);
538 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
540 write_gnu_long_link (struct tar_stat_info
*st
, const char *p
, char type
)
542 size_t size
= strlen (p
) + 1;
546 header
= start_private_header ("././@LongLink", size
, 0);
547 if (! numeric_owner_option
)
549 static char *uname
, *gname
;
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
),
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
);
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
);
583 split_long_name (const char *name
, size_t length
)
587 if (length
> PREFIX_FIELD_SIZE
+ 1)
588 length
= PREFIX_FIELD_SIZE
+ 1;
589 else if (ISSLASH (name
[length
- 1]))
591 for (i
= length
- 1; i
> 0; i
--)
592 if (ISSLASH (name
[i
]))
598 write_ustar_long_name (const char *name
)
600 size_t length
= strlen (name
);
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));
612 i
= split_long_name (name
, length
);
613 if (i
== 0 || (nlen
= length
- i
- 1) > NAME_FIELD_SIZE
|| nlen
== 0)
616 _("%s: file name is too long (cannot be split); not dumped"),
617 quotearg_colon (name
)));
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);
629 /* Write a long link name, depending on the current archive format */
631 write_long_link (struct tar_stat_info
*st
)
633 switch (archive_format
)
636 xheader_store ("linkpath", st
, NULL
);
639 case V7_FORMAT
: /* old V7 tar format */
643 _("%s: link name is too long; not dumped"),
644 quotearg_colon (st
->link_name
)));
649 write_gnu_long_link (st
, st
->link_name
, GNUTYPE_LONGLINK
);
658 write_long_name (struct tar_stat_info
*st
)
660 switch (archive_format
)
663 xheader_store ("path", st
, NULL
);
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));
678 return write_ustar_long_name (st
->file_name
);
682 write_gnu_long_link (st
, st
->file_name
, GNUTYPE_LONGNAME
);
688 return write_short_name (st
);
692 write_extended (bool global
, struct tar_stat_info
*st
, union block
*old_header
)
694 union block
*header
, hp
;
699 if (st
->xhdr
.buffer
|| st
->xhdr
.stk
== NULL
)
702 xheader_finish (&st
->xhdr
);
703 memcpy (hp
.buffer
, old_header
, sizeof (hp
));
707 p
= xheader_ghdr_name ();
708 t
= start_time
.tv_sec
;
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
);
718 header
= find_next_block ();
719 memcpy (header
, &hp
.buffer
, sizeof (hp
.buffer
));
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
);
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. */
745 start_header (struct tar_stat_info
*st
)
748 char const *uname
= NULL
;
749 char const *gname
= NULL
;
751 header
= write_header_name (st
);
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
);
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
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
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
);
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
);
801 if (!UID_TO_CHARS (uid
, header
->header
.uid
))
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
);
813 if (!GID_TO_CHARS (gid
, header
->header
.gid
))
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
);
825 if (!OFF_TO_CHARS (size
, header
->header
.size
))
830 struct timespec mtime
;
832 switch (set_mtime_option
)
839 mtime
= mtime_option
;
843 mtime
= timespec_cmp (st
->mtime
, mtime_option
) > 0
844 ? mtime_option
: st
->mtime
;
848 if (archive_format
== POSIX_FORMAT
)
850 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
851 || mtime
.tv_nsec
!= 0)
852 xheader_store ("mtime", st
, &mtime
);
853 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
)
856 if (!TIME_TO_CHARS (mtime
.tv_sec
, header
->header
.mtime
))
861 if (S_ISCHR (st
->stat
.st_mode
)
862 || S_ISBLK (st
->stat
.st_mode
))
864 major_t devmajor
= major (st
->stat
.st_rdev
);
865 minor_t devminor
= minor (st
->stat
.st_rdev
);
867 if (archive_format
== POSIX_FORMAT
868 && MAX_OCTAL_VAL (header
->header
.devmajor
) < devmajor
)
870 xheader_store ("devmajor", st
, NULL
);
873 if (!MAJOR_TO_CHARS (devmajor
, header
->header
.devmajor
))
876 if (archive_format
== POSIX_FORMAT
877 && MAX_OCTAL_VAL (header
->header
.devminor
) < devminor
)
879 xheader_store ("devminor", st
, NULL
);
882 if (!MINOR_TO_CHARS (devminor
, header
->header
.devminor
))
885 else if (archive_format
!= GNU_FORMAT
&& archive_format
!= OLDGNU_FORMAT
)
887 if (!(MAJOR_TO_CHARS (0, header
->header
.devmajor
)
888 && MINOR_TO_CHARS (0, header
->header
.devminor
)))
892 if (archive_format
== POSIX_FORMAT
)
894 xheader_store ("atime", st
, NULL
);
895 xheader_store ("ctime", st
, NULL
);
897 else if (incremental_option
)
898 if (archive_format
== OLDGNU_FORMAT
|| archive_format
== GNU_FORMAT
)
900 TIME_TO_CHARS (st
->atime
.tv_sec
, header
->oldgnu_header
.atime
);
901 TIME_TO_CHARS (st
->ctime
.tv_sec
, header
->oldgnu_header
.ctime
);
904 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
906 switch (archive_format
)
912 case GNU_FORMAT
: /*FIXME?*/
913 /* Overwrite header->header.magic and header.version in one blow. */
914 strcpy (header
->buffer
+ offsetof (struct posix_header
, magic
),
920 memcpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
921 memcpy (header
->header
.version
, TVERSION
, TVERSLEN
);
928 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
930 /* header->header.[ug]name are left as the empty string. */
935 st
->uname
= xstrdup (uname
);
937 uid_to_uname (st
->stat
.st_uid
, &st
->uname
);
940 st
->gname
= xstrdup (gname
);
942 gid_to_gname (st
->stat
.st_gid
, &st
->gname
);
944 if (archive_format
== POSIX_FORMAT
945 && (strlen (st
->uname
) > UNAME_FIELD_SIZE
946 || !string_ascii_p (st
->uname
)))
947 xheader_store ("uname", st
, NULL
);
948 UNAME_TO_CHARS (st
->uname
, header
->header
.uname
);
950 if (archive_format
== POSIX_FORMAT
951 && (strlen (st
->gname
) > GNAME_FIELD_SIZE
952 || !string_ascii_p (st
->gname
)))
953 xheader_store ("gname", st
, NULL
);
954 GNAME_TO_CHARS (st
->gname
, header
->header
.gname
);
957 if (archive_format
== POSIX_FORMAT
)
962 xheader_store ("SCHILY.acl.access", st
, NULL
);
964 xheader_store ("SCHILY.acl.default", st
, NULL
);
966 if ((selinux_context_option
> 0) && st
->cntx_name
)
967 xheader_store ("RHT.security.selinux", st
, NULL
);
968 if (xattrs_option
> 0)
970 size_t scan_xattr
= 0;
971 struct xattr_array
*xattr_map
= st
->xattr_map
;
973 while (scan_xattr
< st
->xattr_map_size
)
975 xheader_store (xattr_map
[scan_xattr
].xkey
, st
, &scan_xattr
);
985 simple_finish_header (union block
*header
)
991 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
995 for (i
= sizeof *header
; i
-- != 0; )
996 /* We can't use unsigned char here because of old compilers, e.g. V7. */
999 /* Fill in the checksum field. It's formatted differently from the
1000 other fields: it has [6] digits, a null, then a space -- rather than
1001 digits, then a null. We use to_chars.
1002 The final space is already there, from
1003 checksumming, and to_chars doesn't modify it.
1005 This is a fast way to do:
1007 sprintf(header->header.chksum, "%6o", sum); */
1009 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
1011 set_next_block_after (header
);
1014 /* Finish off a filled-in header block and write it out. We also
1015 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
1016 is not negative, is the block ordinal of the first record for this
1017 file, which may be a preceding long name or long link record. */
1019 finish_header (struct tar_stat_info
*st
,
1020 union block
*header
, off_t block_ordinal
)
1022 /* Note: It is important to do this before the call to write_extended(),
1023 so that the actual ustar header is printed */
1025 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
1026 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
1027 && header
->header
.typeflag
!= XHDTYPE
1028 && header
->header
.typeflag
!= XGLTYPE
)
1030 /* FIXME: This global is used in print_header, sigh. */
1031 current_format
= archive_format
;
1032 print_header (st
, header
, block_ordinal
);
1035 header
= write_extended (false, st
, header
);
1036 simple_finish_header (header
);
1041 pad_archive (off_t size_left
)
1044 while (size_left
> 0)
1046 blk
= find_next_block ();
1047 memset (blk
->buffer
, 0, BLOCKSIZE
);
1048 set_next_block_after (blk
);
1049 size_left
-= BLOCKSIZE
;
1053 static enum dump_status
1054 dump_regular_file (int fd
, struct tar_stat_info
*st
)
1056 off_t size_left
= st
->stat
.st_size
;
1057 off_t block_ordinal
;
1060 block_ordinal
= current_block_ordinal ();
1061 blk
= start_header (st
);
1063 return dump_status_fail
;
1065 /* Mark contiguous files, if we support them. */
1066 if (archive_format
!= V7_FORMAT
&& S_ISCTG (st
->stat
.st_mode
))
1067 blk
->header
.typeflag
= CONTTYPE
;
1069 finish_header (st
, blk
, block_ordinal
);
1071 mv_begin_write (st
->file_name
, st
->stat
.st_size
, st
->stat
.st_size
);
1072 while (size_left
> 0)
1074 size_t bufsize
, count
;
1076 blk
= find_next_block ();
1078 bufsize
= available_space_after (blk
);
1080 if (size_left
< bufsize
)
1082 /* Last read -- zero out area beyond. */
1083 bufsize
= size_left
;
1084 count
= bufsize
% BLOCKSIZE
;
1086 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1089 count
= (fd
<= 0) ? bufsize
: blocking_read (fd
, blk
->buffer
, bufsize
);
1090 if (count
== SAFE_READ_ERROR
)
1092 read_diag_details (st
->orig_file_name
,
1093 st
->stat
.st_size
- size_left
, bufsize
);
1094 pad_archive (size_left
);
1095 return dump_status_short
;
1098 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1100 if (count
!= bufsize
)
1102 char buf
[UINTMAX_STRSIZE_BOUND
];
1103 memset (blk
->buffer
+ count
, 0, bufsize
- count
);
1104 WARNOPT (WARN_FILE_SHRANK
,
1106 ngettext ("%s: File shrank by %s byte; padding with zeros",
1107 "%s: File shrank by %s bytes; padding with zeros",
1109 quotearg_colon (st
->orig_file_name
),
1110 STRINGIFY_BIGINT (size_left
, buf
)));
1111 if (! ignore_failed_read_option
)
1112 set_exit_status (TAREXIT_DIFFERS
);
1113 pad_archive (size_left
- (bufsize
- count
));
1114 return dump_status_short
;
1117 return dump_status_ok
;
1121 /* Copy info from the directory identified by ST into the archive.
1122 DIRECTORY contains the directory's entries. */
1125 dump_dir0 (struct tar_stat_info
*st
, char const *directory
)
1127 bool top_level
= ! st
->parent
;
1128 const char *tag_file_name
;
1129 union block
*blk
= NULL
;
1130 off_t block_ordinal
= current_block_ordinal ();
1132 st
->stat
.st_size
= 0; /* force 0 size on dir */
1134 blk
= start_header (st
);
1138 info_attach_exclist (st
);
1140 if (incremental_option
&& archive_format
!= POSIX_FORMAT
)
1141 blk
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1142 else /* if (standard_option) */
1143 blk
->header
.typeflag
= DIRTYPE
;
1145 /* If we're gnudumping, we aren't done yet so don't close it. */
1147 if (!incremental_option
)
1148 finish_header (st
, blk
, block_ordinal
);
1149 else if (gnu_list_name
->directory
)
1151 if (archive_format
== POSIX_FORMAT
)
1153 xheader_store ("GNU.dumpdir", st
,
1154 safe_directory_contents (gnu_list_name
->directory
));
1155 finish_header (st
, blk
, block_ordinal
);
1163 const char *buffer
, *p_buffer
;
1165 block_ordinal
= current_block_ordinal ();
1166 buffer
= safe_directory_contents (gnu_list_name
->directory
);
1167 totsize
= dumpdir_size (buffer
);
1168 OFF_TO_CHARS (totsize
, blk
->header
.size
);
1169 finish_header (st
, blk
, block_ordinal
);
1171 size_left
= totsize
;
1173 mv_begin_write (st
->file_name
, totsize
, totsize
);
1174 while (size_left
> 0)
1176 blk
= find_next_block ();
1177 bufsize
= available_space_after (blk
);
1178 if (size_left
< bufsize
)
1180 bufsize
= size_left
;
1181 count
= bufsize
% BLOCKSIZE
;
1183 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1185 memcpy (blk
->buffer
, p_buffer
, bufsize
);
1186 size_left
-= bufsize
;
1187 p_buffer
+= bufsize
;
1188 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1194 if (!recursion_option
)
1197 if (one_file_system_option
1199 && st
->parent
->stat
.st_dev
!= st
->stat
.st_dev
)
1204 _("%s: file is on a different filesystem; not dumped"),
1205 quotearg_colon (st
->orig_file_name
)));
1212 switch (check_exclusion_tags (st
, &tag_file_name
))
1214 case exclusion_tag_all
:
1215 /* Handled in dump_file0 */
1218 case exclusion_tag_none
:
1224 name_buf
= xstrdup (st
->orig_file_name
);
1225 name_size
= name_len
= strlen (name_buf
);
1227 /* Now output all the files in the directory. */
1228 for (entry
= directory
; (entry_len
= strlen (entry
)) != 0;
1229 entry
+= entry_len
+ 1)
1231 if (name_size
< name_len
+ entry_len
)
1233 name_size
= name_len
+ entry_len
;
1234 name_buf
= xrealloc (name_buf
, name_size
+ 1);
1236 strcpy (name_buf
+ name_len
, entry
);
1237 if (!excluded_name (name_buf
, st
))
1238 dump_file (st
, entry
, name_buf
);
1245 case exclusion_tag_contents
:
1246 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1247 _("contents not dumped"));
1248 name_size
= strlen (st
->orig_file_name
) + strlen (tag_file_name
) + 1;
1249 name_buf
= xmalloc (name_size
);
1250 strcpy (name_buf
, st
->orig_file_name
);
1251 strcat (name_buf
, tag_file_name
);
1252 dump_file (st
, tag_file_name
, name_buf
);
1256 case exclusion_tag_under
:
1257 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1258 _("contents not dumped"));
1264 /* Ensure exactly one trailing slash. */
1266 ensure_slash (char **pstr
)
1268 size_t len
= strlen (*pstr
);
1269 while (len
>= 1 && ISSLASH ((*pstr
)[len
- 1]))
1271 if (!ISSLASH ((*pstr
)[len
]))
1272 *pstr
= xrealloc (*pstr
, len
+ 2);
1273 (*pstr
)[len
++] = '/';
1274 (*pstr
)[len
] = '\0';
1277 /* If we just ran out of file descriptors, release a file descriptor
1278 in the directory chain somewhere leading from DIR->parent->parent
1279 up through the root. Return true if successful, false (preserving
1280 errno == EMFILE) otherwise.
1282 Do not release DIR's file descriptor, or DIR's parent, as other
1283 code assumes that they work. On some operating systems, another
1284 process can claim file descriptor resources as we release them, and
1285 some calls or their emulations require multiple file descriptors,
1286 so callers should not give up if a single release doesn't work. */
1289 open_failure_recover (struct tar_stat_info
const *dir
)
1291 if (errno
== EMFILE
&& dir
&& dir
->parent
)
1293 struct tar_stat_info
*p
;
1294 for (p
= dir
->parent
->parent
; p
; p
= p
->parent
)
1295 if (0 < p
->fd
&& (! p
->parent
|| p
->parent
->fd
<= 0))
1306 /* Return the directory entries of ST, in a dynamically allocated buffer,
1307 each entry followed by '\0' and the last followed by an extra '\0'.
1308 Return null on failure, setting errno. */
1310 get_directory_entries (struct tar_stat_info
*st
)
1312 while (! (st
->dirstream
= fdopendir (st
->fd
)))
1313 if (! open_failure_recover (st
))
1315 return streamsavedir (st
->dirstream
, savedir_sort_order
);
1318 /* Dump the directory ST. Return true if successful, false (emitting
1319 diagnostics) otherwise. Get ST's entries, recurse through its
1320 subdirectories, and clean up file descriptors afterwards. */
1322 dump_dir (struct tar_stat_info
*st
)
1324 char *directory
= get_directory_entries (st
);
1327 savedir_diag (st
->orig_file_name
);
1331 dump_dir0 (st
, directory
);
1333 restore_parent_fd (st
);
1339 /* Number of links a file can have without having to be entered into
1340 the link table. Typically this is 1, but in trickier circumstances
1342 static nlink_t trivial_link_count
;
1345 /* Main functions of this module. */
1348 create_archive (void)
1350 struct name
const *p
;
1352 trivial_link_count
= name_count
<= 1 && ! dereference_option
;
1354 open_archive (ACCESS_WRITE
);
1355 buffer_write_global_xheader ();
1357 if (incremental_option
)
1359 size_t buffer_size
= 1000;
1360 char *buffer
= xmalloc (buffer_size
);
1363 collect_and_sort_names ();
1365 while ((p
= name_from_list ()) != NULL
)
1366 if (!excluded_name (p
->name
, NULL
))
1367 dump_file (0, p
->name
, p
->name
);
1370 while ((p
= name_from_list ()) != NULL
)
1371 if (!excluded_name (p
->name
, NULL
))
1373 struct tar_stat_info st
;
1374 size_t plen
= strlen (p
->name
);
1375 if (buffer_size
<= plen
)
1377 while ((buffer_size
*= 2) <= plen
)
1379 buffer
= xrealloc (buffer
, buffer_size
);
1381 memcpy (buffer
, p
->name
, plen
);
1382 if (! ISSLASH (buffer
[plen
- 1]))
1383 buffer
[plen
++] = DIRECTORY_SEPARATOR
;
1384 tar_stat_init (&st
);
1385 q
= directory_contents (p
->directory
);
1389 size_t qlen
= strlen (q
);
1392 if (! st
.orig_file_name
)
1394 int fd
= openat (chdir_fd
, p
->name
,
1395 open_searchdir_flags
);
1398 file_removed_diag (p
->name
, !p
->parent
,
1403 if (fstat (fd
, &st
.stat
) != 0)
1405 file_removed_diag (p
->name
, !p
->parent
,
1409 st
.orig_file_name
= xstrdup (p
->name
);
1411 if (buffer_size
< plen
+ qlen
)
1413 while ((buffer_size
*=2 ) < plen
+ qlen
)
1415 buffer
= xrealloc (buffer
, buffer_size
);
1417 strcpy (buffer
+ plen
, q
+ 1);
1418 dump_file (&st
, q
+ 1, buffer
);
1422 tar_stat_destroy (&st
);
1429 while ((name
= name_next (1)) != NULL
)
1430 if (!excluded_name (name
, NULL
))
1431 dump_file (0, name
, name
);
1436 finish_deferred_unlinks ();
1437 if (listed_incremental_option
)
1438 write_directory_file ();
1442 /* Calculate the hash of a link. */
1444 hash_link (void const *entry
, size_t n_buckets
)
1446 struct link
const *l
= entry
;
1447 uintmax_t num
= l
->dev
^ l
->ino
;
1448 return num
% n_buckets
;
1451 /* Compare two links for equality. */
1453 compare_links (void const *entry1
, void const *entry2
)
1455 struct link
const *link1
= entry1
;
1456 struct link
const *link2
= entry2
;
1457 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
1461 unknown_file_error (char const *p
)
1463 WARNOPT (WARN_FILE_IGNORED
,
1464 (0, 0, _("%s: Unknown file type; file ignored"),
1465 quotearg_colon (p
)));
1466 if (!ignore_failed_read_option
)
1467 set_exit_status (TAREXIT_FAILURE
);
1471 /* Handling of hard links */
1473 /* Table of all non-directories that we've written so far. Any time
1474 we see another, we check the table and avoid dumping the data
1475 again if we've done it once already. */
1476 static Hash_table
*link_table
;
1478 /* Try to dump stat as a hard link to another file in the archive.
1479 Return true if successful. */
1481 dump_hard_link (struct tar_stat_info
*st
)
1484 && (trivial_link_count
< st
->stat
.st_nlink
|| remove_files_option
))
1487 struct link
*duplicate
;
1488 off_t block_ordinal
;
1491 lp
.ino
= st
->stat
.st_ino
;
1492 lp
.dev
= st
->stat
.st_dev
;
1494 if ((duplicate
= hash_lookup (link_table
, &lp
)))
1496 /* We found a link. */
1497 char const *link_name
= safer_name_suffix (duplicate
->name
, true,
1498 absolute_names_option
);
1499 if (duplicate
->nlink
)
1502 block_ordinal
= current_block_ordinal ();
1503 assign_string (&st
->link_name
, link_name
);
1504 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
1505 < strlen (link_name
))
1506 write_long_link (st
);
1508 st
->stat
.st_size
= 0;
1509 blk
= start_header (st
);
1512 tar_copy_str (blk
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1514 blk
->header
.typeflag
= LNKTYPE
;
1515 finish_header (st
, blk
, block_ordinal
);
1517 if (remove_files_option
)
1518 queue_deferred_unlink (st
->orig_file_name
, false);
1527 file_count_links (struct tar_stat_info
*st
)
1529 if (hard_dereference_option
)
1531 if (trivial_link_count
< st
->stat
.st_nlink
)
1533 struct link
*duplicate
;
1534 char *linkname
= NULL
;
1537 assign_string (&linkname
, safer_name_suffix (st
->orig_file_name
, true,
1538 absolute_names_option
));
1539 transform_name (&linkname
, XFORM_LINK
);
1541 lp
= xmalloc (offsetof (struct link
, name
)
1542 + strlen (linkname
) + 1);
1543 lp
->ino
= st
->stat
.st_ino
;
1544 lp
->dev
= st
->stat
.st_dev
;
1545 lp
->nlink
= st
->stat
.st_nlink
;
1546 strcpy (lp
->name
, linkname
);
1550 || (link_table
= hash_initialize (0, 0, hash_link
,
1552 && (duplicate
= hash_insert (link_table
, lp
))))
1555 if (duplicate
!= lp
)
1561 /* For each dumped file, check if all its links were dumped. Emit
1562 warnings if it is not so. */
1571 for (lp
= hash_get_first (link_table
); lp
;
1572 lp
= hash_get_next (link_table
, lp
))
1576 WARN ((0, 0, _("Missing links to %s."), quote (lp
->name
)));
1581 /* Assuming DIR is the working directory, open FILE, using FLAGS to
1582 control the open. A null DIR means to use ".". If we are low on
1583 file descriptors, try to release one or more from DIR's parents to
1586 subfile_open (struct tar_stat_info
const *dir
, char const *file
, int flags
)
1590 static bool initialized
;
1593 /* Initialize any tables that might be needed when file
1594 descriptors are exhausted, and whose initialization might
1595 require a file descriptor. This includes the system message
1596 catalog and tar's message catalog. */
1602 while ((fd
= openat (dir
? dir
->fd
: chdir_fd
, file
, flags
)) < 0
1603 && open_failure_recover (dir
))
1608 /* Restore the file descriptor for ST->parent, if it was temporarily
1609 closed to conserve file descriptors. On failure, set the file
1610 descriptor to the negative of the corresponding errno value. Call
1611 this every time a subdirectory is ascended from. */
1613 restore_parent_fd (struct tar_stat_info
const *st
)
1615 struct tar_stat_info
*parent
= st
->parent
;
1616 if (parent
&& ! parent
->fd
)
1618 int parentfd
= openat (st
->fd
, "..", open_searchdir_flags
);
1619 struct stat parentstat
;
1623 else if (! (fstat (parentfd
, &parentstat
) == 0
1624 && parent
->stat
.st_ino
== parentstat
.st_ino
1625 && parent
->stat
.st_dev
== parentstat
.st_dev
))
1628 parentfd
= IMPOSTOR_ERRNO
;
1633 int origfd
= openat (chdir_fd
, parent
->orig_file_name
,
1634 open_searchdir_flags
);
1637 if (fstat (parentfd
, &parentstat
) == 0
1638 && parent
->stat
.st_ino
== parentstat
.st_ino
1639 && parent
->stat
.st_dev
== parentstat
.st_dev
)
1646 parent
->fd
= parentfd
;
1650 /* Dump a single file, recursing on directories. ST is the file's
1651 status info, NAME its name relative to the parent directory, and P
1652 its full name (which may be relative to the working directory). */
1654 /* FIXME: One should make sure that for *every* path leading to setting
1655 exit_status to failure, a clear diagnostic has been issued. */
1658 dump_file0 (struct tar_stat_info
*st
, char const *name
, char const *p
)
1660 union block
*header
;
1662 off_t original_size
;
1663 struct timespec original_ctime
;
1664 off_t block_ordinal
= -1;
1667 struct tar_stat_info
const *parent
= st
->parent
;
1668 bool top_level
= ! parent
;
1669 int parentfd
= top_level
? chdir_fd
: parent
->fd
;
1670 void (*diag
) (char const *) = 0;
1672 if (interactive_option
&& !confirm ("add", p
))
1675 assign_string (&st
->orig_file_name
, p
);
1676 assign_string (&st
->file_name
,
1677 safer_name_suffix (p
, false, absolute_names_option
));
1679 transform_name (&st
->file_name
, XFORM_REGFILE
);
1681 if (parentfd
< 0 && ! top_level
)
1686 else if (fstatat (parentfd
, name
, &st
->stat
, fstatat_flags
) != 0)
1688 else if (file_dumpable_p (&st
->stat
))
1690 fd
= subfile_open (parent
, name
, open_read_flags
);
1696 if (fstat (fd
, &st
->stat
) != 0)
1702 file_removed_diag (p
, top_level
, diag
);
1706 st
->archive_file_size
= original_size
= st
->stat
.st_size
;
1707 st
->atime
= get_stat_atime (&st
->stat
);
1708 st
->mtime
= get_stat_mtime (&st
->stat
);
1709 st
->ctime
= original_ctime
= get_stat_ctime (&st
->stat
);
1712 if (S_ISHIDDEN (st
->stat
.st_mode
))
1714 char *new = (char *) alloca (strlen (p
) + 2);
1724 /* See if we want only new files, and check if this one is too old to
1727 This check is omitted if incremental_option is set *and* the
1728 requested file is not explicitly listed in the command line. */
1730 if (! (incremental_option
&& ! top_level
)
1731 && !S_ISDIR (st
->stat
.st_mode
)
1732 && OLDER_TAR_STAT_TIME (*st
, m
)
1733 && (!after_date_option
|| OLDER_TAR_STAT_TIME (*st
, c
)))
1735 if (!incremental_option
&& verbose_option
)
1736 WARNOPT (WARN_FILE_UNCHANGED
,
1737 (0, 0, _("%s: file is unchanged; not dumped"),
1738 quotearg_colon (p
)));
1742 /* See if we are trying to dump the archive. */
1743 if (sys_file_is_archive (st
))
1745 WARNOPT (WARN_IGNORE_ARCHIVE
,
1746 (0, 0, _("%s: file is the archive; not dumped"),
1747 quotearg_colon (p
)));
1751 is_dir
= S_ISDIR (st
->stat
.st_mode
) != 0;
1753 if (!is_dir
&& dump_hard_link (st
))
1756 if (is_dir
|| S_ISREG (st
->stat
.st_mode
) || S_ISCTG (st
->stat
.st_mode
))
1759 struct stat final_stat
;
1761 xattrs_acls_get (parentfd
, name
, st
, 0, !is_dir
);
1762 xattrs_selinux_get (parentfd
, name
, st
, fd
);
1763 xattrs_xattrs_get (parentfd
, name
, st
, fd
);
1767 const char *tag_file_name
;
1768 ensure_slash (&st
->orig_file_name
);
1769 ensure_slash (&st
->file_name
);
1771 if (check_exclusion_tags (st
, &tag_file_name
) == exclusion_tag_all
)
1773 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1774 _("directory not dumped"));
1781 parentfd
= top_level
? chdir_fd
: parent
->fd
;
1785 enum dump_status status
;
1787 if (fd
&& sparse_option
&& ST_IS_SPARSE (st
->stat
))
1789 status
= sparse_dump_file (fd
, st
);
1790 if (status
== dump_status_not_implemented
)
1791 status
= dump_regular_file (fd
, st
);
1794 status
= dump_regular_file (fd
, st
);
1798 case dump_status_ok
:
1799 case dump_status_short
:
1800 file_count_links (st
);
1803 case dump_status_fail
:
1806 case dump_status_not_implemented
:
1810 ok
= status
== dump_status_ok
;
1822 if (parentfd
< 0 && ! top_level
)
1828 ok
= fstatat (parentfd
, name
, &final_stat
, fstatat_flags
) == 0;
1831 ok
= fstat (fd
, &final_stat
) == 0;
1834 file_removed_diag (p
, top_level
, stat_diag
);
1839 if ((timespec_cmp (get_stat_ctime (&final_stat
), original_ctime
) != 0
1840 /* Original ctime will change if the file is a directory and
1841 --remove-files is given */
1842 && !(remove_files_option
&& is_dir
))
1843 || original_size
< final_stat
.st_size
)
1845 WARNOPT (WARN_FILE_CHANGED
,
1846 (0, 0, _("%s: file changed as we read it"),
1847 quotearg_colon (p
)));
1848 set_exit_status (TAREXIT_DIFFERS
);
1850 else if (atime_preserve_option
== replace_atime_preserve
1851 && fd
&& (is_dir
|| original_size
!= 0)
1852 && set_file_atime (fd
, parentfd
, name
, st
->atime
) != 0)
1856 ok
&= tar_stat_close (st
);
1857 if (ok
&& remove_files_option
)
1858 queue_deferred_unlink (p
, is_dir
);
1862 #ifdef HAVE_READLINK
1863 else if (S_ISLNK (st
->stat
.st_mode
))
1865 st
->link_name
= areadlinkat_with_size (parentfd
, name
, st
->stat
.st_size
);
1868 if (errno
== ENOMEM
)
1870 file_removed_diag (p
, top_level
, readlink_diag
);
1873 transform_name (&st
->link_name
, XFORM_SYMLINK
);
1874 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
1875 < strlen (st
->link_name
))
1876 write_long_link (st
);
1878 xattrs_selinux_get (parentfd
, name
, st
, 0);
1879 xattrs_xattrs_get (parentfd
, name
, st
, 0);
1881 block_ordinal
= current_block_ordinal ();
1882 st
->stat
.st_size
= 0; /* force 0 size on symlink */
1883 header
= start_header (st
);
1886 tar_copy_str (header
->header
.linkname
, st
->link_name
, NAME_FIELD_SIZE
);
1887 header
->header
.typeflag
= SYMTYPE
;
1888 finish_header (st
, header
, block_ordinal
);
1889 /* nothing more to do to it */
1891 if (remove_files_option
)
1892 queue_deferred_unlink (p
, false);
1894 file_count_links (st
);
1898 else if (S_ISCHR (st
->stat
.st_mode
))
1901 xattrs_acls_get (parentfd
, name
, st
, 0, true);
1902 xattrs_selinux_get (parentfd
, name
, st
, 0);
1903 xattrs_xattrs_get (parentfd
, name
, st
, 0);
1905 else if (S_ISBLK (st
->stat
.st_mode
))
1908 xattrs_acls_get (parentfd
, name
, st
, 0, true);
1909 xattrs_selinux_get (parentfd
, name
, st
, 0);
1910 xattrs_xattrs_get (parentfd
, name
, st
, 0);
1912 else if (S_ISFIFO (st
->stat
.st_mode
))
1915 xattrs_acls_get (parentfd
, name
, st
, 0, true);
1916 xattrs_selinux_get (parentfd
, name
, st
, 0);
1917 xattrs_xattrs_get (parentfd
, name
, st
, 0);
1919 else if (S_ISSOCK (st
->stat
.st_mode
))
1921 WARNOPT (WARN_FILE_IGNORED
,
1922 (0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1925 else if (S_ISDOOR (st
->stat
.st_mode
))
1927 WARNOPT (WARN_FILE_IGNORED
,
1928 (0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1933 unknown_file_error (p
);
1937 if (archive_format
== V7_FORMAT
)
1939 unknown_file_error (p
);
1943 block_ordinal
= current_block_ordinal ();
1944 st
->stat
.st_size
= 0; /* force 0 size */
1945 header
= start_header (st
);
1948 header
->header
.typeflag
= type
;
1950 if (type
!= FIFOTYPE
)
1952 MAJOR_TO_CHARS (major (st
->stat
.st_rdev
),
1953 header
->header
.devmajor
);
1954 MINOR_TO_CHARS (minor (st
->stat
.st_rdev
),
1955 header
->header
.devminor
);
1958 finish_header (st
, header
, block_ordinal
);
1959 if (remove_files_option
)
1960 queue_deferred_unlink (p
, false);
1963 /* Dump a file, recursively. PARENT describes the file's parent
1964 directory, NAME is the file's name relative to PARENT, and FULLNAME
1965 its full name, possibly relative to the working directory. NAME
1966 may contain slashes at the top level of invocation. */
1969 dump_file (struct tar_stat_info
*parent
, char const *name
,
1970 char const *fullname
)
1972 struct tar_stat_info st
;
1973 tar_stat_init (&st
);
1975 dump_file0 (&st
, name
, fullname
);
1976 if (parent
&& listed_incremental_option
)
1977 update_parent_directory (parent
);
1978 tar_stat_destroy (&st
);