1 /* Create a tar archive.
3 Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-08-25.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 /* Error number to use when an impostor is discovered.
30 Pretend the impostor isn't there. */
31 enum { IMPOSTOR_ERRNO
= ENOENT
};
45 enum exclusion_tag_type type
;
46 bool (*predicate
) (int fd
);
47 struct exclusion_tag
*next
;
50 static struct exclusion_tag
*exclusion_tags
;
53 add_exclusion_tag (const char *name
, enum exclusion_tag_type type
,
54 bool (*predicate
) (int fd
))
56 struct exclusion_tag
*tag
= xmalloc (sizeof tag
[0]);
57 tag
->next
= exclusion_tags
;
60 tag
->predicate
= predicate
;
61 tag
->length
= strlen (name
);
66 exclusion_tag_warning (const char *dirname
, const char *tagname
,
70 WARNOPT (WARN_CACHEDIR
,
72 _("%s: contains a cache directory tag %s; %s"),
73 quotearg_colon (dirname
),
74 quotearg_n (1, tagname
),
78 enum exclusion_tag_type
79 check_exclusion_tags (struct tar_stat_info
const *st
, char const **tag_file_name
)
81 struct exclusion_tag
*tag
;
83 for (tag
= exclusion_tags
; tag
; tag
= tag
->next
)
85 int tagfd
= subfile_open (st
, tag
->name
, open_read_flags
);
88 bool satisfied
= !tag
->predicate
|| tag
->predicate (tagfd
);
93 *tag_file_name
= tag
->name
;
99 return exclusion_tag_none
;
102 /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
103 contains a valid header, as described at:
104 http://www.brynosaurus.com/cachedir
105 Applications can write this file into directories they create
106 for use as caches containing purely regenerable, non-precious data,
107 allowing us to avoid archiving them if --exclude-caches is specified. */
109 #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
110 #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
113 cachedir_file_p (int fd
)
115 char tagbuf
[CACHEDIR_SIGNATURE_SIZE
];
118 (read (fd
, tagbuf
, CACHEDIR_SIGNATURE_SIZE
) == CACHEDIR_SIGNATURE_SIZE
119 && memcmp (tagbuf
, CACHEDIR_SIGNATURE
, CACHEDIR_SIGNATURE_SIZE
) == 0);
123 /* The maximum uintmax_t value that can be represented with DIGITS digits,
124 assuming that each digit is BITS_PER_DIGIT wide. */
125 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
126 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
127 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
130 /* The maximum uintmax_t value that can be represented with octal
131 digits and a trailing NUL in BUFFER. */
132 #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
134 /* Convert VALUE to an octal representation suitable for tar headers.
135 Output to buffer WHERE with size SIZE.
136 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
139 to_octal (uintmax_t value
, char *where
, size_t size
)
146 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
152 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
153 NUL unless SRC is LEN or more bytes long. */
156 tar_copy_str (char *dst
, const char *src
, size_t len
)
159 for (i
= 0; i
< len
; i
++)
160 if (! (dst
[i
] = src
[i
]))
164 /* Same as tar_copy_str, but always terminate with NUL if using
168 tar_name_copy_str (char *dst
, const char *src
, size_t len
)
170 tar_copy_str (dst
, src
, len
);
171 if (archive_format
== OLDGNU_FORMAT
)
175 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
176 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
177 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
178 The result is undefined if SIZE is 0 or if VALUE is too large to
182 to_base256 (int negative
, uintmax_t value
, char *where
, size_t size
)
185 uintmax_t propagated_sign_bits
=
186 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
191 where
[--i
] = v
& ((1 << LG_256
) - 1);
192 v
= propagated_sign_bits
| (v
>> LG_256
);
197 #define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
198 #define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
199 #define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
200 #define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
201 #define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
203 #define UNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
204 #define GNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
207 to_chars (int negative
, uintmax_t value
, size_t valsize
,
208 uintmax_t (*substitute
) (int *),
209 char *where
, size_t size
, const char *type
);
212 to_chars_subst (int negative
, int gnu_format
, uintmax_t value
, size_t valsize
,
213 uintmax_t (*substitute
) (int *),
214 char *where
, size_t size
, const char *type
)
216 uintmax_t maxval
= (gnu_format
217 ? MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)
218 : MAX_VAL_WITH_DIGITS (size
- 1, LG_8
));
219 char valbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
220 char maxbuf
[UINTMAX_STRSIZE_BOUND
];
221 char minbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
222 char const *minval_string
;
223 char const *maxval_string
= STRINGIFY_BIGINT (maxval
, maxbuf
);
224 char const *value_string
;
228 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
229 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
238 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
243 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
248 uintmax_t sub
= substitute (&negsub
) & maxval
;
249 /* NOTE: This is one of the few places where GNU_FORMAT differs from
250 OLDGNU_FORMAT. The actual differences are:
252 1. In OLDGNU_FORMAT all strings in a tar header end in \0
253 2. Incremental archives use oldgnu_header.
255 Apart from this they are completely identical. */
256 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? - sub
: sub
;
257 char subbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
258 char *sub_string
= STRINGIFY_BIGINT (s
, subbuf
+ 1);
261 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
262 value_string
, type
, minval_string
, maxval_string
,
264 return to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
267 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
268 value_string
, type
, minval_string
, maxval_string
));
272 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
273 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
274 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
275 negative before being cast to uintmax_t; its original bitpattern
276 can be deduced from VALSIZE, its original size before casting.
277 TYPE is the kind of value being output (useful for diagnostics).
278 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
279 digits), followed by '\0'. If this won't work, and if GNU or
280 OLDGNU format is allowed, use '\200' followed by base-256, or (if
281 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
282 If neither format works, use SUBSTITUTE (...) instead. Pass to
283 SUBSTITUTE the address of an 0-or-1 flag recording whether the
284 substitute value is negative. */
287 to_chars (int negative
, uintmax_t value
, size_t valsize
,
288 uintmax_t (*substitute
) (int *),
289 char *where
, size_t size
, const char *type
)
291 int gnu_format
= (archive_format
== GNU_FORMAT
292 || archive_format
== OLDGNU_FORMAT
);
294 /* Generate the POSIX octal representation if the number fits. */
295 if (! negative
&& value
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
297 where
[size
- 1] = '\0';
298 to_octal (value
, where
, size
- 1);
303 /* Try to cope with the number by using traditional GNU format
306 /* Generate the base-256 representation if the number fits. */
307 if (((negative
? -1 - value
: value
)
308 <= MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)))
310 where
[0] = negative
? -1 : 1 << (LG_256
- 1);
311 to_base256 (negative
, value
, where
+ 1, size
- 1);
315 /* Otherwise, if the number is negative, and if it would not cause
316 ambiguity on this host by confusing positive with negative
317 values, then generate the POSIX octal representation of the value
318 modulo 2**(field bits). The resulting tar file is
319 machine-dependent, since it depends on the host word size. Yuck!
320 But this is the traditional behavior. */
321 else if (negative
&& valsize
* CHAR_BIT
<= (size
- 1) * LG_8
)
323 static int warned_once
;
327 WARN ((0, 0, _("Generating negative octal headers")));
329 where
[size
- 1] = '\0';
330 to_octal (value
& MAX_VAL_WITH_DIGITS (valsize
* CHAR_BIT
, 1),
334 /* Otherwise fall back to substitution, if possible: */
337 substitute
= NULL
; /* No substitution for formats, other than GNU */
339 return to_chars_subst (negative
, gnu_format
, value
, valsize
, substitute
,
344 gid_substitute (int *negative
)
350 static gid_t gid_nobody
;
351 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
360 gid_to_chars (gid_t v
, char *p
, size_t s
)
362 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
366 major_to_chars (major_t v
, char *p
, size_t s
)
368 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
372 minor_to_chars (minor_t v
, char *p
, size_t s
)
374 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
378 mode_to_chars (mode_t v
, char *p
, size_t s
)
380 /* In the common case where the internal and external mode bits are the same,
381 and we are not using POSIX or GNU format,
382 propagate all unknown bits to the external mode.
383 This matches historical practice.
384 Otherwise, just copy the bits we know about. */
387 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
388 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
389 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
390 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
391 && archive_format
!= POSIX_FORMAT
392 && archive_format
!= USTAR_FORMAT
393 && archive_format
!= GNU_FORMAT
)
401 u
= ((v
& S_ISUID
? TSUID
: 0)
402 | (v
& S_ISGID
? TSGID
: 0)
403 | (v
& S_ISVTX
? TSVTX
: 0)
404 | (v
& S_IRUSR
? TUREAD
: 0)
405 | (v
& S_IWUSR
? TUWRITE
: 0)
406 | (v
& S_IXUSR
? TUEXEC
: 0)
407 | (v
& S_IRGRP
? TGREAD
: 0)
408 | (v
& S_IWGRP
? TGWRITE
: 0)
409 | (v
& S_IXGRP
? TGEXEC
: 0)
410 | (v
& S_IROTH
? TOREAD
: 0)
411 | (v
& S_IWOTH
? TOWRITE
: 0)
412 | (v
& S_IXOTH
? TOEXEC
: 0));
414 return to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
418 off_to_chars (off_t v
, char *p
, size_t s
)
420 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
424 time_to_chars (time_t v
, char *p
, size_t s
)
426 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
430 uid_substitute (int *negative
)
436 static uid_t uid_nobody
;
437 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
446 uid_to_chars (uid_t v
, char *p
, size_t s
)
448 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
452 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
454 return to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
458 string_to_chars (char const *str
, char *p
, size_t s
)
460 tar_copy_str (p
, str
, s
);
465 /* A directory is always considered dumpable.
466 Otherwise, only regular and contiguous files are considered dumpable.
467 Such a file is dumpable if it is sparse and both --sparse and --totals
469 Otherwise, it is dumpable unless any of the following conditions occur:
471 a) it is empty *and* world-readable, or
472 b) current archive is /dev/null */
475 file_dumpable_p (struct stat
const *st
)
477 if (S_ISDIR (st
->st_mode
))
479 if (! (S_ISREG (st
->st_mode
) || S_ISCTG (st
->st_mode
)))
482 return totals_option
&& sparse_option
&& ST_IS_SPARSE (*st
);
483 return ! (st
->st_size
== 0 && (st
->st_mode
& MODE_R
) == MODE_R
);
487 /* Writing routines. */
489 /* Write the EOT block(s). Zero at least two blocks, through the end
490 of the record. Old tar, as previous versions of GNU tar, writes
491 garbage after two zeroed blocks. */
495 union block
*pointer
= find_next_block ();
496 memset (pointer
->buffer
, 0, BLOCKSIZE
);
497 set_next_block_after (pointer
);
498 pointer
= find_next_block ();
499 memset (pointer
->buffer
, 0, available_space_after (pointer
));
500 set_next_block_after (pointer
);
503 /* Write a "private" header */
505 start_private_header (const char *name
, size_t size
, time_t t
)
507 union block
*header
= find_next_block ();
509 memset (header
->buffer
, 0, sizeof (union block
));
511 tar_name_copy_str (header
->header
.name
, name
, NAME_FIELD_SIZE
);
512 OFF_TO_CHARS (size
, header
->header
.size
);
514 TIME_TO_CHARS (t
, header
->header
.mtime
);
515 MODE_TO_CHARS (S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, header
->header
.mode
);
516 UID_TO_CHARS (getuid (), header
->header
.uid
);
517 GID_TO_CHARS (getgid (), header
->header
.gid
);
518 MAJOR_TO_CHARS (0, header
->header
.devmajor
);
519 MINOR_TO_CHARS (0, header
->header
.devminor
);
520 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
521 strncpy (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 #define FILL(field,byte) do { \
538 memset(field, byte, sizeof(field)-1); \
539 (field)[sizeof(field)-1] = 0; \
542 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
544 write_gnu_long_link (struct tar_stat_info
*st
, const char *p
, char type
)
546 size_t size
= strlen (p
) + 1;
551 header
= start_private_header ("././@LongLink", size
, time (NULL
));
552 FILL (header
->header
.mtime
, '0');
553 FILL (header
->header
.mode
, '0');
554 FILL (header
->header
.uid
, '0');
555 FILL (header
->header
.gid
, '0');
556 FILL (header
->header
.devmajor
, 0);
557 FILL (header
->header
.devminor
, 0);
558 uid_to_uname (0, &tmpname
);
559 UNAME_TO_CHARS (tmpname
, header
->header
.uname
);
561 gid_to_gname (0, &tmpname
);
562 GNAME_TO_CHARS (tmpname
, header
->header
.gname
);
565 strcpy (header
->buffer
+ offsetof (struct posix_header
, magic
),
567 header
->header
.typeflag
= type
;
568 finish_header (st
, header
, -1);
570 header
= find_next_block ();
572 bufsize
= available_space_after (header
);
574 while (bufsize
< size
)
576 memcpy (header
->buffer
, p
, bufsize
);
579 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
580 header
= find_next_block ();
581 bufsize
= available_space_after (header
);
583 memcpy (header
->buffer
, p
, size
);
584 memset (header
->buffer
+ size
, 0, bufsize
- size
);
585 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
589 split_long_name (const char *name
, size_t length
)
593 if (length
> PREFIX_FIELD_SIZE
+ 1)
594 length
= PREFIX_FIELD_SIZE
+ 1;
595 else if (ISSLASH (name
[length
- 1]))
597 for (i
= length
- 1; i
> 0; i
--)
598 if (ISSLASH (name
[i
]))
604 write_ustar_long_name (const char *name
)
606 size_t length
= strlen (name
);
610 if (length
> PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1)
612 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
613 quotearg_colon (name
),
614 PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1));
618 i
= split_long_name (name
, length
);
619 if (i
== 0 || (nlen
= length
- i
- 1) > NAME_FIELD_SIZE
|| nlen
== 0)
622 _("%s: file name is too long (cannot be split); not dumped"),
623 quotearg_colon (name
)));
627 header
= find_next_block ();
628 memset (header
->buffer
, 0, sizeof (header
->buffer
));
629 memcpy (header
->header
.prefix
, name
, i
);
630 memcpy (header
->header
.name
, name
+ i
+ 1, length
- i
- 1);
635 /* Write a long link name, depending on the current archive format */
637 write_long_link (struct tar_stat_info
*st
)
639 switch (archive_format
)
642 xheader_store ("linkpath", st
, NULL
);
645 case V7_FORMAT
: /* old V7 tar format */
649 _("%s: link name is too long; not dumped"),
650 quotearg_colon (st
->link_name
)));
655 write_gnu_long_link (st
, st
->link_name
, GNUTYPE_LONGLINK
);
664 write_long_name (struct tar_stat_info
*st
)
666 switch (archive_format
)
669 xheader_store ("path", st
, NULL
);
673 if (strlen (st
->file_name
) > NAME_FIELD_SIZE
-1)
675 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
676 quotearg_colon (st
->file_name
),
677 NAME_FIELD_SIZE
- 1));
684 return write_ustar_long_name (st
->file_name
);
688 write_gnu_long_link (st
, st
->file_name
, GNUTYPE_LONGNAME
);
694 return write_short_name (st
);
698 write_extended (bool global
, struct tar_stat_info
*st
, union block
*old_header
)
700 union block
*header
, hp
;
705 if (st
->xhdr
.buffer
|| st
->xhdr
.stk
== NULL
)
708 xheader_finish (&st
->xhdr
);
709 memcpy (hp
.buffer
, old_header
, sizeof (hp
));
713 p
= xheader_ghdr_name ();
719 p
= xheader_xhdr_name (st
);
720 t
= st
->stat
.st_mtime
;
722 xheader_write (type
, p
, t
, &st
->xhdr
);
724 header
= find_next_block ();
725 memcpy (header
, &hp
.buffer
, sizeof (hp
.buffer
));
730 write_header_name (struct tar_stat_info
*st
)
732 if (archive_format
== POSIX_FORMAT
&& !string_ascii_p (st
->file_name
))
734 xheader_store ("path", st
, NULL
);
735 return write_short_name (st
);
737 else if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
738 < strlen (st
->file_name
))
739 return write_long_name (st
);
741 return write_short_name (st
);
745 /* Header handling. */
747 /* Make a header block for the file whose stat info is st,
748 and return its address. */
751 start_header (struct tar_stat_info
*st
)
755 header
= write_header_name (st
);
759 /* Override some stat fields, if requested to do so. */
761 if (owner_option
!= (uid_t
) -1)
762 st
->stat
.st_uid
= owner_option
;
763 if (group_option
!= (gid_t
) -1)
764 st
->stat
.st_gid
= group_option
;
767 ((st
->stat
.st_mode
& ~MODE_ALL
)
768 | mode_adjust (st
->stat
.st_mode
, S_ISDIR (st
->stat
.st_mode
) != 0,
769 initial_umask
, mode_option
, NULL
));
771 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
772 for a few tars and came up with the following interoperability
776 1 2 3 4 5 6 7 8 9 READER
777 . . . . . . . . . 1 = SunOS 4.2 tar
778 # . . # # . . # # 2 = NEC SVR4.0.2 tar
779 . . . # # . . # . 3 = Solaris 2.1 tar
780 . . . . . . . . . 4 = GNU tar 1.11.1
781 . . . . . . . . . 5 = HP-UX 8.07 tar
782 . . . . . . . . . 6 = Ultrix 4.1
783 . . . . . . . . . 7 = AIX 3.2
784 . . . . . . . . . 8 = Hitachi HI-UX 1.03
785 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
788 # = ``impossible file type''
790 The following mask for old archive removes the `#'s in column 4
791 above, thus making GNU tar both a universal donor and a universal
792 acceptor for Paul's test. */
794 if (archive_format
== V7_FORMAT
|| archive_format
== USTAR_FORMAT
)
795 MODE_TO_CHARS (st
->stat
.st_mode
& MODE_ALL
, header
->header
.mode
);
797 MODE_TO_CHARS (st
->stat
.st_mode
, header
->header
.mode
);
800 uid_t uid
= st
->stat
.st_uid
;
801 if (archive_format
== POSIX_FORMAT
802 && MAX_OCTAL_VAL (header
->header
.uid
) < uid
)
804 xheader_store ("uid", st
, NULL
);
807 if (!UID_TO_CHARS (uid
, header
->header
.uid
))
812 gid_t gid
= st
->stat
.st_gid
;
813 if (archive_format
== POSIX_FORMAT
814 && MAX_OCTAL_VAL (header
->header
.gid
) < gid
)
816 xheader_store ("gid", st
, NULL
);
819 if (!GID_TO_CHARS (gid
, header
->header
.gid
))
824 off_t size
= st
->stat
.st_size
;
825 if (archive_format
== POSIX_FORMAT
826 && MAX_OCTAL_VAL (header
->header
.size
) < size
)
828 xheader_store ("size", st
, NULL
);
831 if (!OFF_TO_CHARS (size
, header
->header
.size
))
836 struct timespec mtime
= set_mtime_option
? mtime_option
: st
->mtime
;
837 if (archive_format
== POSIX_FORMAT
)
839 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
840 || mtime
.tv_nsec
!= 0)
841 xheader_store ("mtime", st
, &mtime
);
842 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
)
845 if (!TIME_TO_CHARS (mtime
.tv_sec
, header
->header
.mtime
))
850 if (S_ISCHR (st
->stat
.st_mode
)
851 || S_ISBLK (st
->stat
.st_mode
))
853 major_t devmajor
= major (st
->stat
.st_rdev
);
854 minor_t devminor
= minor (st
->stat
.st_rdev
);
856 if (archive_format
== POSIX_FORMAT
857 && MAX_OCTAL_VAL (header
->header
.devmajor
) < devmajor
)
859 xheader_store ("devmajor", st
, NULL
);
862 if (!MAJOR_TO_CHARS (devmajor
, header
->header
.devmajor
))
865 if (archive_format
== POSIX_FORMAT
866 && MAX_OCTAL_VAL (header
->header
.devminor
) < devminor
)
868 xheader_store ("devminor", st
, NULL
);
871 if (!MINOR_TO_CHARS (devminor
, header
->header
.devminor
))
874 else if (archive_format
!= GNU_FORMAT
&& archive_format
!= OLDGNU_FORMAT
)
876 if (!(MAJOR_TO_CHARS (0, header
->header
.devmajor
)
877 && MINOR_TO_CHARS (0, header
->header
.devminor
)))
881 if (archive_format
== POSIX_FORMAT
)
883 xheader_store ("atime", st
, NULL
);
884 xheader_store ("ctime", st
, NULL
);
886 else if (incremental_option
)
887 if (archive_format
== OLDGNU_FORMAT
|| archive_format
== GNU_FORMAT
)
889 TIME_TO_CHARS (st
->atime
.tv_sec
, header
->oldgnu_header
.atime
);
890 TIME_TO_CHARS (st
->ctime
.tv_sec
, header
->oldgnu_header
.ctime
);
893 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
895 switch (archive_format
)
901 case GNU_FORMAT
: /*FIXME?*/
902 /* Overwrite header->header.magic and header.version in one blow. */
903 strcpy (header
->buffer
+ offsetof (struct posix_header
, magic
),
909 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
910 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
917 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
919 /* header->header.[ug]name are left as the empty string. */
923 uid_to_uname (st
->stat
.st_uid
, &st
->uname
);
924 gid_to_gname (st
->stat
.st_gid
, &st
->gname
);
926 if (archive_format
== POSIX_FORMAT
927 && (strlen (st
->uname
) > UNAME_FIELD_SIZE
928 || !string_ascii_p (st
->uname
)))
929 xheader_store ("uname", st
, NULL
);
930 UNAME_TO_CHARS (st
->uname
, header
->header
.uname
);
932 if (archive_format
== POSIX_FORMAT
933 && (strlen (st
->gname
) > GNAME_FIELD_SIZE
934 || !string_ascii_p (st
->gname
)))
935 xheader_store ("gname", st
, NULL
);
936 GNAME_TO_CHARS (st
->gname
, header
->header
.gname
);
943 simple_finish_header (union block
*header
)
949 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
953 for (i
= sizeof *header
; i
-- != 0; )
954 /* We can't use unsigned char here because of old compilers, e.g. V7. */
957 /* Fill in the checksum field. It's formatted differently from the
958 other fields: it has [6] digits, a null, then a space -- rather than
959 digits, then a null. We use to_chars.
960 The final space is already there, from
961 checksumming, and to_chars doesn't modify it.
963 This is a fast way to do:
965 sprintf(header->header.chksum, "%6o", sum); */
967 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
969 set_next_block_after (header
);
972 /* Finish off a filled-in header block and write it out. We also
973 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
974 is not negative, is the block ordinal of the first record for this
975 file, which may be a preceding long name or long link record. */
977 finish_header (struct tar_stat_info
*st
,
978 union block
*header
, off_t block_ordinal
)
980 /* Note: It is important to do this before the call to write_extended(),
981 so that the actual ustar header is printed */
983 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
984 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
985 && header
->header
.typeflag
!= XHDTYPE
986 && header
->header
.typeflag
!= XGLTYPE
)
988 /* FIXME: This global is used in print_header, sigh. */
989 current_format
= archive_format
;
990 print_header (st
, header
, block_ordinal
);
993 header
= write_extended (false, st
, header
);
994 simple_finish_header (header
);
999 pad_archive (off_t size_left
)
1002 while (size_left
> 0)
1004 blk
= find_next_block ();
1005 memset (blk
->buffer
, 0, BLOCKSIZE
);
1006 set_next_block_after (blk
);
1007 size_left
-= BLOCKSIZE
;
1011 static enum dump_status
1012 dump_regular_file (int fd
, struct tar_stat_info
*st
)
1014 off_t size_left
= st
->stat
.st_size
;
1015 off_t block_ordinal
;
1018 block_ordinal
= current_block_ordinal ();
1019 blk
= start_header (st
);
1021 return dump_status_fail
;
1023 /* Mark contiguous files, if we support them. */
1024 if (archive_format
!= V7_FORMAT
&& S_ISCTG (st
->stat
.st_mode
))
1025 blk
->header
.typeflag
= CONTTYPE
;
1027 finish_header (st
, blk
, block_ordinal
);
1029 mv_begin_write (st
->file_name
, st
->stat
.st_size
, st
->stat
.st_size
);
1030 while (size_left
> 0)
1032 size_t bufsize
, count
;
1034 blk
= find_next_block ();
1036 bufsize
= available_space_after (blk
);
1038 if (size_left
< bufsize
)
1040 /* Last read -- zero out area beyond. */
1041 bufsize
= size_left
;
1042 count
= bufsize
% BLOCKSIZE
;
1044 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1047 count
= (fd
<= 0) ? bufsize
: safe_read (fd
, blk
->buffer
, bufsize
);
1048 if (count
== SAFE_READ_ERROR
)
1050 read_diag_details (st
->orig_file_name
,
1051 st
->stat
.st_size
- size_left
, bufsize
);
1052 pad_archive (size_left
);
1053 return dump_status_short
;
1056 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1058 if (count
!= bufsize
)
1060 char buf
[UINTMAX_STRSIZE_BOUND
];
1061 memset (blk
->buffer
+ count
, 0, bufsize
- count
);
1062 WARNOPT (WARN_FILE_SHRANK
,
1064 ngettext ("%s: File shrank by %s byte; padding with zeros",
1065 "%s: File shrank by %s bytes; padding with zeros",
1067 quotearg_colon (st
->orig_file_name
),
1068 STRINGIFY_BIGINT (size_left
, buf
)));
1069 if (! ignore_failed_read_option
)
1070 set_exit_status (TAREXIT_DIFFERS
);
1071 pad_archive (size_left
- (bufsize
- count
));
1072 return dump_status_short
;
1075 return dump_status_ok
;
1079 /* Copy info from the directory identified by ST into the archive.
1080 DIRECTORY contains the directory's entries. */
1083 dump_dir0 (struct tar_stat_info
*st
, char const *directory
)
1085 bool top_level
= ! st
->parent
;
1086 const char *tag_file_name
;
1087 union block
*blk
= NULL
;
1088 off_t block_ordinal
= current_block_ordinal ();
1090 st
->stat
.st_size
= 0; /* force 0 size on dir */
1092 blk
= start_header (st
);
1096 if (incremental_option
&& archive_format
!= POSIX_FORMAT
)
1097 blk
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1098 else /* if (standard_option) */
1099 blk
->header
.typeflag
= DIRTYPE
;
1101 /* If we're gnudumping, we aren't done yet so don't close it. */
1103 if (!incremental_option
)
1104 finish_header (st
, blk
, block_ordinal
);
1105 else if (gnu_list_name
->directory
)
1107 if (archive_format
== POSIX_FORMAT
)
1109 xheader_store ("GNU.dumpdir", st
,
1110 safe_directory_contents (gnu_list_name
->directory
));
1111 finish_header (st
, blk
, block_ordinal
);
1119 const char *buffer
, *p_buffer
;
1121 block_ordinal
= current_block_ordinal ();
1122 buffer
= safe_directory_contents (gnu_list_name
->directory
);
1123 totsize
= dumpdir_size (buffer
);
1124 OFF_TO_CHARS (totsize
, blk
->header
.size
);
1125 finish_header (st
, blk
, block_ordinal
);
1127 size_left
= totsize
;
1129 mv_begin_write (st
->file_name
, totsize
, totsize
);
1130 while (size_left
> 0)
1132 blk
= find_next_block ();
1133 bufsize
= available_space_after (blk
);
1134 if (size_left
< bufsize
)
1136 bufsize
= size_left
;
1137 count
= bufsize
% BLOCKSIZE
;
1139 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1141 memcpy (blk
->buffer
, p_buffer
, bufsize
);
1142 size_left
-= bufsize
;
1143 p_buffer
+= bufsize
;
1144 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1150 if (!recursion_option
)
1153 if (one_file_system_option
1155 && st
->parent
->stat
.st_dev
!= st
->stat
.st_dev
)
1160 _("%s: file is on a different filesystem; not dumped"),
1161 quotearg_colon (st
->orig_file_name
)));
1168 switch (check_exclusion_tags (st
, &tag_file_name
))
1170 case exclusion_tag_all
:
1171 /* Handled in dump_file0 */
1174 case exclusion_tag_none
:
1180 name_buf
= xstrdup (st
->orig_file_name
);
1181 name_size
= name_len
= strlen (name_buf
);
1183 /* Now output all the files in the directory. */
1184 for (entry
= directory
; (entry_len
= strlen (entry
)) != 0;
1185 entry
+= entry_len
+ 1)
1187 if (name_size
< name_len
+ entry_len
)
1189 name_size
= name_len
+ entry_len
;
1190 name_buf
= xrealloc (name_buf
, name_size
+ 1);
1192 strcpy (name_buf
+ name_len
, entry
);
1193 if (!excluded_name (name_buf
))
1194 dump_file (st
, entry
, name_buf
);
1201 case exclusion_tag_contents
:
1202 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1203 _("contents not dumped"));
1204 name_size
= strlen (st
->orig_file_name
) + strlen (tag_file_name
) + 1;
1205 name_buf
= xmalloc (name_size
);
1206 strcpy (name_buf
, st
->orig_file_name
);
1207 strcat (name_buf
, tag_file_name
);
1208 dump_file (st
, tag_file_name
, name_buf
);
1212 case exclusion_tag_under
:
1213 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1214 _("contents not dumped"));
1220 /* Ensure exactly one trailing slash. */
1222 ensure_slash (char **pstr
)
1224 size_t len
= strlen (*pstr
);
1225 while (len
>= 1 && ISSLASH ((*pstr
)[len
- 1]))
1227 if (!ISSLASH ((*pstr
)[len
]))
1228 *pstr
= xrealloc (*pstr
, len
+ 2);
1229 (*pstr
)[len
++] = '/';
1230 (*pstr
)[len
] = '\0';
1233 /* If we just ran out of file descriptors, release a file descriptor
1234 in the directory chain somewhere leading from DIR->parent->parent
1235 up through the root. Return true if successful, false (preserving
1236 errno == EMFILE) otherwise.
1238 Do not release DIR's file descriptor, or DIR's parent, as other
1239 code assumes that they work. On some operating systems, another
1240 process can claim file descriptor resources as we release them, and
1241 some calls or their emulations require multiple file descriptors,
1242 so callers should not give up if a single release doesn't work. */
1245 open_failure_recover (struct tar_stat_info
const *dir
)
1247 if (errno
== EMFILE
&& dir
&& dir
->parent
)
1249 struct tar_stat_info
*p
;
1250 for (p
= dir
->parent
->parent
; p
; p
= p
->parent
)
1251 if (0 < p
->fd
&& (! p
->parent
|| p
->parent
->fd
<= 0))
1262 /* Return the directory entries of ST, in a dynamically allocated buffer,
1263 each entry followed by '\0' and the last followed by an extra '\0'.
1264 Return null on failure, setting errno. */
1266 get_directory_entries (struct tar_stat_info
*st
)
1268 while (! (st
->dirstream
= fdopendir (st
->fd
)))
1269 if (! open_failure_recover (st
))
1271 return streamsavedir (st
->dirstream
);
1274 /* Dump the directory ST. Return true if successful, false (emitting
1275 diagnostics) otherwise. Get ST's entries, recurse through its
1276 subdirectories, and clean up file descriptors afterwards. */
1278 dump_dir (struct tar_stat_info
*st
)
1280 char *directory
= get_directory_entries (st
);
1283 savedir_diag (st
->orig_file_name
);
1287 dump_dir0 (st
, directory
);
1289 restore_parent_fd (st
);
1295 /* Number of links a file can have without having to be entered into
1296 the link table. Typically this is 1, but in trickier circumstances
1298 static nlink_t trivial_link_count
;
1301 /* Main functions of this module. */
1304 create_archive (void)
1306 struct name
const *p
;
1308 trivial_link_count
= name_count
<= 1 && ! dereference_option
;
1310 open_archive (ACCESS_WRITE
);
1311 buffer_write_global_xheader ();
1313 if (incremental_option
)
1315 size_t buffer_size
= 1000;
1316 char *buffer
= xmalloc (buffer_size
);
1319 collect_and_sort_names ();
1321 while ((p
= name_from_list ()) != NULL
)
1322 if (!excluded_name (p
->name
))
1323 dump_file (0, p
->name
, p
->name
);
1326 while ((p
= name_from_list ()) != NULL
)
1327 if (!excluded_name (p
->name
))
1329 struct tar_stat_info st
;
1330 size_t plen
= strlen (p
->name
);
1331 if (buffer_size
<= plen
)
1333 while ((buffer_size
*= 2) <= plen
)
1335 buffer
= xrealloc (buffer
, buffer_size
);
1337 memcpy (buffer
, p
->name
, plen
);
1338 if (! ISSLASH (buffer
[plen
- 1]))
1339 buffer
[plen
++] = DIRECTORY_SEPARATOR
;
1340 tar_stat_init (&st
);
1341 q
= directory_contents (gnu_list_name
->directory
);
1345 size_t qlen
= strlen (q
);
1348 if (! st
.orig_file_name
)
1350 int fd
= openat (chdir_fd
, p
->name
,
1351 open_searchdir_flags
);
1354 open_diag (p
->name
);
1358 if (fstat (fd
, &st
.stat
) != 0)
1360 stat_diag (p
->name
);
1363 st
.orig_file_name
= xstrdup (p
->name
);
1365 if (buffer_size
< plen
+ qlen
)
1367 while ((buffer_size
*=2 ) < plen
+ qlen
)
1369 buffer
= xrealloc (buffer
, buffer_size
);
1371 strcpy (buffer
+ plen
, q
+ 1);
1372 dump_file (&st
, q
+ 1, buffer
);
1376 tar_stat_destroy (&st
);
1383 while ((name
= name_next (1)) != NULL
)
1384 if (!excluded_name (name
))
1385 dump_file (0, name
, name
);
1390 finish_deferred_unlinks ();
1391 if (listed_incremental_option
)
1392 write_directory_file ();
1396 /* Calculate the hash of a link. */
1398 hash_link (void const *entry
, size_t n_buckets
)
1400 struct link
const *l
= entry
;
1401 uintmax_t num
= l
->dev
^ l
->ino
;
1402 return num
% n_buckets
;
1405 /* Compare two links for equality. */
1407 compare_links (void const *entry1
, void const *entry2
)
1409 struct link
const *link1
= entry1
;
1410 struct link
const *link2
= entry2
;
1411 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
1415 unknown_file_error (char const *p
)
1417 WARNOPT (WARN_FILE_IGNORED
,
1418 (0, 0, _("%s: Unknown file type; file ignored"),
1419 quotearg_colon (p
)));
1420 if (!ignore_failed_read_option
)
1421 set_exit_status (TAREXIT_FAILURE
);
1425 /* Handling of hard links */
1427 /* Table of all non-directories that we've written so far. Any time
1428 we see another, we check the table and avoid dumping the data
1429 again if we've done it once already. */
1430 static Hash_table
*link_table
;
1432 /* Try to dump stat as a hard link to another file in the archive.
1433 Return true if successful. */
1435 dump_hard_link (struct tar_stat_info
*st
)
1438 && (trivial_link_count
< st
->stat
.st_nlink
|| remove_files_option
))
1441 struct link
*duplicate
;
1442 off_t block_ordinal
;
1445 lp
.ino
= st
->stat
.st_ino
;
1446 lp
.dev
= st
->stat
.st_dev
;
1448 if ((duplicate
= hash_lookup (link_table
, &lp
)))
1450 /* We found a link. */
1451 char const *link_name
= safer_name_suffix (duplicate
->name
, true,
1452 absolute_names_option
);
1456 block_ordinal
= current_block_ordinal ();
1457 assign_string (&st
->link_name
, link_name
);
1458 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
1459 < strlen (link_name
))
1460 write_long_link (st
);
1462 st
->stat
.st_size
= 0;
1463 blk
= start_header (st
);
1466 tar_copy_str (blk
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1468 blk
->header
.typeflag
= LNKTYPE
;
1469 finish_header (st
, blk
, block_ordinal
);
1471 if (remove_files_option
)
1472 queue_deferred_unlink (st
->orig_file_name
, false);
1481 file_count_links (struct tar_stat_info
*st
)
1483 if (hard_dereference_option
)
1485 if (trivial_link_count
< st
->stat
.st_nlink
)
1487 struct link
*duplicate
;
1488 char *linkname
= NULL
;
1491 assign_string (&linkname
, st
->orig_file_name
);
1492 transform_name (&linkname
, XFORM_LINK
);
1494 lp
= xmalloc (offsetof (struct link
, name
)
1495 + strlen (linkname
) + 1);
1496 lp
->ino
= st
->stat
.st_ino
;
1497 lp
->dev
= st
->stat
.st_dev
;
1498 lp
->nlink
= st
->stat
.st_nlink
;
1499 strcpy (lp
->name
, linkname
);
1503 || (link_table
= hash_initialize (0, 0, hash_link
,
1505 && (duplicate
= hash_insert (link_table
, lp
))))
1508 if (duplicate
!= lp
)
1514 /* For each dumped file, check if all its links were dumped. Emit
1515 warnings if it is not so. */
1524 for (lp
= hash_get_first (link_table
); lp
;
1525 lp
= hash_get_next (link_table
, lp
))
1529 WARN ((0, 0, _("Missing links to %s."), quote (lp
->name
)));
1534 /* Assuming DIR is the working directory, open FILE, using FLAGS to
1535 control the open. A null DIR means to use ".". If we are low on
1536 file descriptors, try to release one or more from DIR's parents to
1539 subfile_open (struct tar_stat_info
const *dir
, char const *file
, int flags
)
1543 static bool initialized
;
1546 /* Initialize any tables that might be needed when file
1547 descriptors are exhausted, and whose initialization might
1548 require a file descriptor. This includes the system message
1549 catalog and tar's message catalog. */
1555 while ((fd
= openat (dir
? dir
->fd
: chdir_fd
, file
, flags
)) < 0
1556 && open_failure_recover (dir
))
1561 /* Restore the file descriptor for ST->parent, if it was temporarily
1562 closed to conserve file descriptors. On failure, set the file
1563 descriptor to the negative of the corresponding errno value. Call
1564 this every time a subdirectory is ascended from. */
1566 restore_parent_fd (struct tar_stat_info
const *st
)
1568 struct tar_stat_info
*parent
= st
->parent
;
1569 if (parent
&& ! parent
->fd
)
1571 int parentfd
= openat (st
->fd
, "..", open_searchdir_flags
);
1572 struct stat parentstat
;
1576 else if (! (fstat (parentfd
, &parentstat
) == 0
1577 && parent
->stat
.st_ino
== parentstat
.st_ino
1578 && parent
->stat
.st_dev
== parentstat
.st_dev
))
1581 parentfd
= IMPOSTOR_ERRNO
;
1586 int origfd
= openat (chdir_fd
, parent
->orig_file_name
,
1587 open_searchdir_flags
);
1590 if (fstat (parentfd
, &parentstat
) == 0
1591 && parent
->stat
.st_ino
== parentstat
.st_ino
1592 && parent
->stat
.st_dev
== parentstat
.st_dev
)
1599 parent
->fd
= parentfd
;
1603 /* Dump a single file, recursing on directories. ST is the file's
1604 status info, NAME its name relative to the parent directory, and P
1605 its full name (which may be relative to the working directory). */
1607 /* FIXME: One should make sure that for *every* path leading to setting
1608 exit_status to failure, a clear diagnostic has been issued. */
1611 dump_file0 (struct tar_stat_info
*st
, char const *name
, char const *p
)
1613 union block
*header
;
1615 off_t original_size
;
1616 struct timespec original_ctime
;
1617 off_t block_ordinal
= -1;
1620 struct tar_stat_info
const *parent
= st
->parent
;
1621 bool top_level
= ! parent
;
1622 int parentfd
= top_level
? chdir_fd
: parent
->fd
;
1623 void (*diag
) (char const *) = 0;
1625 if (interactive_option
&& !confirm ("add", p
))
1628 assign_string (&st
->orig_file_name
, p
);
1629 assign_string (&st
->file_name
,
1630 safer_name_suffix (p
, false, absolute_names_option
));
1632 transform_name (&st
->file_name
, XFORM_REGFILE
);
1634 if (parentfd
< 0 && ! top_level
)
1639 else if (fstatat (parentfd
, name
, &st
->stat
, fstatat_flags
) != 0)
1641 else if (file_dumpable_p (&st
->stat
))
1643 fd
= subfile_open (parent
, name
, open_read_flags
);
1649 if (fstat (fd
, &st
->stat
) != 0)
1655 file_removed_diag (p
, top_level
, diag
);
1659 st
->archive_file_size
= original_size
= st
->stat
.st_size
;
1660 st
->atime
= get_stat_atime (&st
->stat
);
1661 st
->mtime
= get_stat_mtime (&st
->stat
);
1662 st
->ctime
= original_ctime
= get_stat_ctime (&st
->stat
);
1665 if (S_ISHIDDEN (st
->stat
.st_mode
))
1667 char *new = (char *) alloca (strlen (p
) + 2);
1677 /* See if we want only new files, and check if this one is too old to
1680 This check is omitted if incremental_option is set *and* the
1681 requested file is not explicitely listed in the command line. */
1683 if (!(incremental_option
&& !is_individual_file (p
))
1684 && !S_ISDIR (st
->stat
.st_mode
)
1685 && OLDER_TAR_STAT_TIME (*st
, m
)
1686 && (!after_date_option
|| OLDER_TAR_STAT_TIME (*st
, c
)))
1688 if (!incremental_option
&& verbose_option
)
1689 WARNOPT (WARN_FILE_UNCHANGED
,
1690 (0, 0, _("%s: file is unchanged; not dumped"),
1691 quotearg_colon (p
)));
1695 /* See if we are trying to dump the archive. */
1696 if (sys_file_is_archive (st
))
1698 WARNOPT (WARN_IGNORE_ARCHIVE
,
1699 (0, 0, _("%s: file is the archive; not dumped"),
1700 quotearg_colon (p
)));
1704 is_dir
= S_ISDIR (st
->stat
.st_mode
) != 0;
1706 if (!is_dir
&& dump_hard_link (st
))
1709 if (is_dir
|| S_ISREG (st
->stat
.st_mode
) || S_ISCTG (st
->stat
.st_mode
))
1712 struct stat final_stat
;
1716 const char *tag_file_name
;
1717 ensure_slash (&st
->orig_file_name
);
1718 ensure_slash (&st
->file_name
);
1720 if (check_exclusion_tags (st
, &tag_file_name
) == exclusion_tag_all
)
1722 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1723 _("directory not dumped"));
1730 parentfd
= top_level
? chdir_fd
: parent
->fd
;
1734 enum dump_status status
;
1736 if (fd
&& sparse_option
&& ST_IS_SPARSE (st
->stat
))
1738 status
= sparse_dump_file (fd
, st
);
1739 if (status
== dump_status_not_implemented
)
1740 status
= dump_regular_file (fd
, st
);
1743 status
= dump_regular_file (fd
, st
);
1747 case dump_status_ok
:
1748 case dump_status_short
:
1749 file_count_links (st
);
1752 case dump_status_fail
:
1755 case dump_status_not_implemented
:
1759 ok
= status
== dump_status_ok
;
1771 if (parentfd
< 0 && ! top_level
)
1777 ok
= fstatat (parentfd
, name
, &final_stat
, fstatat_flags
) == 0;
1780 ok
= fstat (fd
, &final_stat
) == 0;
1783 file_removed_diag (p
, top_level
, stat_diag
);
1788 if ((timespec_cmp (get_stat_ctime (&final_stat
), original_ctime
) != 0
1789 /* Original ctime will change if the file is a directory and
1790 --remove-files is given */
1791 && !(remove_files_option
&& is_dir
))
1792 || original_size
< final_stat
.st_size
)
1794 WARNOPT (WARN_FILE_CHANGED
,
1795 (0, 0, _("%s: file changed as we read it"),
1796 quotearg_colon (p
)));
1797 set_exit_status (TAREXIT_DIFFERS
);
1799 else if (atime_preserve_option
== replace_atime_preserve
1800 && set_file_atime (fd
, parentfd
, name
, st
->atime
) != 0)
1804 ok
&= tar_stat_close (st
);
1805 if (ok
&& remove_files_option
)
1806 queue_deferred_unlink (p
, is_dir
);
1810 #ifdef HAVE_READLINK
1811 else if (S_ISLNK (st
->stat
.st_mode
))
1815 size_t linklen
= st
->stat
.st_size
;
1816 if (linklen
!= st
->stat
.st_size
|| linklen
+ 1 == 0)
1818 buffer
= (char *) alloca (linklen
+ 1);
1819 size
= readlinkat (parentfd
, name
, buffer
, linklen
+ 1);
1822 file_removed_diag (p
, top_level
, readlink_diag
);
1825 buffer
[size
] = '\0';
1826 assign_string (&st
->link_name
, buffer
);
1827 transform_name (&st
->link_name
, XFORM_SYMLINK
);
1828 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
) < size
)
1829 write_long_link (st
);
1831 block_ordinal
= current_block_ordinal ();
1832 st
->stat
.st_size
= 0; /* force 0 size on symlink */
1833 header
= start_header (st
);
1836 tar_copy_str (header
->header
.linkname
, st
->link_name
, NAME_FIELD_SIZE
);
1837 header
->header
.typeflag
= SYMTYPE
;
1838 finish_header (st
, header
, block_ordinal
);
1839 /* nothing more to do to it */
1841 if (remove_files_option
)
1842 queue_deferred_unlink (p
, false);
1844 file_count_links (st
);
1848 else if (S_ISCHR (st
->stat
.st_mode
))
1850 else if (S_ISBLK (st
->stat
.st_mode
))
1852 else if (S_ISFIFO (st
->stat
.st_mode
))
1854 else if (S_ISSOCK (st
->stat
.st_mode
))
1856 WARNOPT (WARN_FILE_IGNORED
,
1857 (0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1860 else if (S_ISDOOR (st
->stat
.st_mode
))
1862 WARNOPT (WARN_FILE_IGNORED
,
1863 (0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1868 unknown_file_error (p
);
1872 if (archive_format
== V7_FORMAT
)
1874 unknown_file_error (p
);
1878 block_ordinal
= current_block_ordinal ();
1879 st
->stat
.st_size
= 0; /* force 0 size */
1880 header
= start_header (st
);
1883 header
->header
.typeflag
= type
;
1885 if (type
!= FIFOTYPE
)
1887 MAJOR_TO_CHARS (major (st
->stat
.st_rdev
),
1888 header
->header
.devmajor
);
1889 MINOR_TO_CHARS (minor (st
->stat
.st_rdev
),
1890 header
->header
.devminor
);
1893 finish_header (st
, header
, block_ordinal
);
1894 if (remove_files_option
)
1895 queue_deferred_unlink (p
, false);
1898 /* Dump a file, recursively. PARENT describes the file's parent
1899 directory, NAME is the file's name relative to PARENT, and FULLNAME
1900 its full name, possibly relative to the working directory. NAME
1901 may contain slashes at the top level of invocation. */
1904 dump_file (struct tar_stat_info
*parent
, char const *name
,
1905 char const *fullname
)
1907 struct tar_stat_info st
;
1908 tar_stat_init (&st
);
1910 dump_file0 (&st
, name
, fullname
);
1911 if (parent
&& listed_incremental_option
)
1912 update_parent_directory (parent
);
1913 tar_stat_destroy (&st
);