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. */
41 enum exclusion_tag_type type
;
42 bool (*predicate
) (int fd
);
43 struct exclusion_tag
*next
;
46 static struct exclusion_tag
*exclusion_tags
;
49 add_exclusion_tag (const char *name
, enum exclusion_tag_type type
,
50 bool (*predicate
) (int fd
))
52 struct exclusion_tag
*tag
= xmalloc (sizeof tag
[0]);
53 tag
->next
= exclusion_tags
;
56 tag
->predicate
= predicate
;
57 tag
->length
= strlen (name
);
62 exclusion_tag_warning (const char *dirname
, const char *tagname
,
66 WARNOPT (WARN_CACHEDIR
,
68 _("%s: contains a cache directory tag %s; %s"),
69 quotearg_colon (dirname
),
70 quotearg_n (1, tagname
),
74 enum exclusion_tag_type
75 check_exclusion_tags (int fd
, char const **tag_file_name
)
77 struct exclusion_tag
*tag
;
79 for (tag
= exclusion_tags
; tag
; tag
= tag
->next
)
81 int tagfd
= openat (fd
, tag
->name
, open_read_flags
);
84 bool satisfied
= !tag
->predicate
|| tag
->predicate (tagfd
);
89 *tag_file_name
= tag
->name
;
95 return exclusion_tag_none
;
98 /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
99 contains a valid header, as described at:
100 http://www.brynosaurus.com/cachedir
101 Applications can write this file into directories they create
102 for use as caches containing purely regenerable, non-precious data,
103 allowing us to avoid archiving them if --exclude-caches is specified. */
105 #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
106 #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
109 cachedir_file_p (int fd
)
111 char tagbuf
[CACHEDIR_SIGNATURE_SIZE
];
114 (read (fd
, tagbuf
, CACHEDIR_SIGNATURE_SIZE
) == CACHEDIR_SIGNATURE_SIZE
115 && memcmp (tagbuf
, CACHEDIR_SIGNATURE
, CACHEDIR_SIGNATURE_SIZE
) == 0);
119 /* The maximum uintmax_t value that can be represented with DIGITS digits,
120 assuming that each digit is BITS_PER_DIGIT wide. */
121 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
122 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
123 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
126 /* The maximum uintmax_t value that can be represented with octal
127 digits and a trailing NUL in BUFFER. */
128 #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
130 /* Convert VALUE to an octal representation suitable for tar headers.
131 Output to buffer WHERE with size SIZE.
132 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
135 to_octal (uintmax_t value
, char *where
, size_t size
)
142 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
148 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
149 NUL unless SRC is LEN or more bytes long. */
152 tar_copy_str (char *dst
, const char *src
, size_t len
)
155 for (i
= 0; i
< len
; i
++)
156 if (! (dst
[i
] = src
[i
]))
160 /* Same as tar_copy_str, but always terminate with NUL if using
164 tar_name_copy_str (char *dst
, const char *src
, size_t len
)
166 tar_copy_str (dst
, src
, len
);
167 if (archive_format
== OLDGNU_FORMAT
)
171 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
172 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
173 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
174 The result is undefined if SIZE is 0 or if VALUE is too large to
178 to_base256 (int negative
, uintmax_t value
, char *where
, size_t size
)
181 uintmax_t propagated_sign_bits
=
182 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
187 where
[--i
] = v
& ((1 << LG_256
) - 1);
188 v
= propagated_sign_bits
| (v
>> LG_256
);
193 #define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
194 #define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
195 #define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
196 #define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
197 #define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
199 #define UNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
200 #define GNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
203 to_chars (int negative
, uintmax_t value
, size_t valsize
,
204 uintmax_t (*substitute
) (int *),
205 char *where
, size_t size
, const char *type
);
208 to_chars_subst (int negative
, int gnu_format
, uintmax_t value
, size_t valsize
,
209 uintmax_t (*substitute
) (int *),
210 char *where
, size_t size
, const char *type
)
212 uintmax_t maxval
= (gnu_format
213 ? MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)
214 : MAX_VAL_WITH_DIGITS (size
- 1, LG_8
));
215 char valbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
216 char maxbuf
[UINTMAX_STRSIZE_BOUND
];
217 char minbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
218 char const *minval_string
;
219 char const *maxval_string
= STRINGIFY_BIGINT (maxval
, maxbuf
);
220 char const *value_string
;
224 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
225 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
234 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
239 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
244 uintmax_t sub
= substitute (&negsub
) & maxval
;
245 /* NOTE: This is one of the few places where GNU_FORMAT differs from
246 OLDGNU_FORMAT. The actual differences are:
248 1. In OLDGNU_FORMAT all strings in a tar header end in \0
249 2. Incremental archives use oldgnu_header.
251 Apart from this they are completely identical. */
252 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? - sub
: sub
;
253 char subbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
254 char *sub_string
= STRINGIFY_BIGINT (s
, subbuf
+ 1);
257 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
258 value_string
, type
, minval_string
, maxval_string
,
260 return to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
263 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
264 value_string
, type
, minval_string
, maxval_string
));
268 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
269 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
270 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
271 negative before being cast to uintmax_t; its original bitpattern
272 can be deduced from VALSIZE, its original size before casting.
273 TYPE is the kind of value being output (useful for diagnostics).
274 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
275 digits), followed by '\0'. If this won't work, and if GNU or
276 OLDGNU format is allowed, use '\200' followed by base-256, or (if
277 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
278 If neither format works, use SUBSTITUTE (...) instead. Pass to
279 SUBSTITUTE the address of an 0-or-1 flag recording whether the
280 substitute value is negative. */
283 to_chars (int negative
, uintmax_t value
, size_t valsize
,
284 uintmax_t (*substitute
) (int *),
285 char *where
, size_t size
, const char *type
)
287 int gnu_format
= (archive_format
== GNU_FORMAT
288 || archive_format
== OLDGNU_FORMAT
);
290 /* Generate the POSIX octal representation if the number fits. */
291 if (! negative
&& value
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
293 where
[size
- 1] = '\0';
294 to_octal (value
, where
, size
- 1);
299 /* Try to cope with the number by using traditional GNU format
302 /* Generate the base-256 representation if the number fits. */
303 if (((negative
? -1 - value
: value
)
304 <= MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)))
306 where
[0] = negative
? -1 : 1 << (LG_256
- 1);
307 to_base256 (negative
, value
, where
+ 1, size
- 1);
311 /* Otherwise, if the number is negative, and if it would not cause
312 ambiguity on this host by confusing positive with negative
313 values, then generate the POSIX octal representation of the value
314 modulo 2**(field bits). The resulting tar file is
315 machine-dependent, since it depends on the host word size. Yuck!
316 But this is the traditional behavior. */
317 else if (negative
&& valsize
* CHAR_BIT
<= (size
- 1) * LG_8
)
319 static int warned_once
;
323 WARN ((0, 0, _("Generating negative octal headers")));
325 where
[size
- 1] = '\0';
326 to_octal (value
& MAX_VAL_WITH_DIGITS (valsize
* CHAR_BIT
, 1),
330 /* Otherwise fall back to substitution, if possible: */
333 substitute
= NULL
; /* No substitution for formats, other than GNU */
335 return to_chars_subst (negative
, gnu_format
, value
, valsize
, substitute
,
340 gid_substitute (int *negative
)
346 static gid_t gid_nobody
;
347 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
356 gid_to_chars (gid_t v
, char *p
, size_t s
)
358 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
362 major_to_chars (major_t v
, char *p
, size_t s
)
364 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
368 minor_to_chars (minor_t v
, char *p
, size_t s
)
370 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
374 mode_to_chars (mode_t v
, char *p
, size_t s
)
376 /* In the common case where the internal and external mode bits are the same,
377 and we are not using POSIX or GNU format,
378 propagate all unknown bits to the external mode.
379 This matches historical practice.
380 Otherwise, just copy the bits we know about. */
383 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
384 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
385 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
386 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
387 && archive_format
!= POSIX_FORMAT
388 && archive_format
!= USTAR_FORMAT
389 && archive_format
!= GNU_FORMAT
)
397 u
= ((v
& S_ISUID
? TSUID
: 0)
398 | (v
& S_ISGID
? TSGID
: 0)
399 | (v
& S_ISVTX
? TSVTX
: 0)
400 | (v
& S_IRUSR
? TUREAD
: 0)
401 | (v
& S_IWUSR
? TUWRITE
: 0)
402 | (v
& S_IXUSR
? TUEXEC
: 0)
403 | (v
& S_IRGRP
? TGREAD
: 0)
404 | (v
& S_IWGRP
? TGWRITE
: 0)
405 | (v
& S_IXGRP
? TGEXEC
: 0)
406 | (v
& S_IROTH
? TOREAD
: 0)
407 | (v
& S_IWOTH
? TOWRITE
: 0)
408 | (v
& S_IXOTH
? TOEXEC
: 0));
410 return to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
414 off_to_chars (off_t v
, char *p
, size_t s
)
416 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
420 time_to_chars (time_t v
, char *p
, size_t s
)
422 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
426 uid_substitute (int *negative
)
432 static uid_t uid_nobody
;
433 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
442 uid_to_chars (uid_t v
, char *p
, size_t s
)
444 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
448 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
450 return to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
454 string_to_chars (char const *str
, char *p
, size_t s
)
456 tar_copy_str (p
, str
, s
);
461 /* A directory is always considered dumpable.
462 Otherwise, only regular and contiguous files are considered dumpable.
463 Such a file is dumpable if it is sparse and both --sparse and --totals
465 Otherwise, it is dumpable unless any of the following conditions occur:
467 a) it is empty *and* world-readable, or
468 b) current archive is /dev/null */
471 file_dumpable_p (struct stat
const *st
)
473 if (S_ISDIR (st
->st_mode
))
475 if (! (S_ISREG (st
->st_mode
) || S_ISCTG (st
->st_mode
)))
478 return totals_option
&& sparse_option
&& ST_IS_SPARSE (*st
);
479 return ! (st
->st_size
== 0 && (st
->st_mode
& MODE_R
) == MODE_R
);
483 /* Writing routines. */
485 /* Write the EOT block(s). Zero at least two blocks, through the end
486 of the record. Old tar, as previous versions of GNU tar, writes
487 garbage after two zeroed blocks. */
491 union block
*pointer
= find_next_block ();
492 memset (pointer
->buffer
, 0, BLOCKSIZE
);
493 set_next_block_after (pointer
);
494 pointer
= find_next_block ();
495 memset (pointer
->buffer
, 0, available_space_after (pointer
));
496 set_next_block_after (pointer
);
499 /* Write a "private" header */
501 start_private_header (const char *name
, size_t size
, time_t t
)
503 union block
*header
= find_next_block ();
505 memset (header
->buffer
, 0, sizeof (union block
));
507 tar_name_copy_str (header
->header
.name
, name
, NAME_FIELD_SIZE
);
508 OFF_TO_CHARS (size
, header
->header
.size
);
510 TIME_TO_CHARS (t
, header
->header
.mtime
);
511 MODE_TO_CHARS (S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, header
->header
.mode
);
512 UID_TO_CHARS (getuid (), header
->header
.uid
);
513 GID_TO_CHARS (getgid (), header
->header
.gid
);
514 MAJOR_TO_CHARS (0, header
->header
.devmajor
);
515 MINOR_TO_CHARS (0, header
->header
.devminor
);
516 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
517 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
521 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
525 write_short_name (struct tar_stat_info
*st
)
527 union block
*header
= find_next_block ();
528 memset (header
->buffer
, 0, sizeof (union block
));
529 tar_name_copy_str (header
->header
.name
, st
->file_name
, NAME_FIELD_SIZE
);
533 #define FILL(field,byte) do { \
534 memset(field, byte, sizeof(field)-1); \
535 (field)[sizeof(field)-1] = 0; \
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;
547 header
= start_private_header ("././@LongLink", size
, time (NULL
));
548 FILL (header
->header
.mtime
, '0');
549 FILL (header
->header
.mode
, '0');
550 FILL (header
->header
.uid
, '0');
551 FILL (header
->header
.gid
, '0');
552 FILL (header
->header
.devmajor
, 0);
553 FILL (header
->header
.devminor
, 0);
554 uid_to_uname (0, &tmpname
);
555 UNAME_TO_CHARS (tmpname
, header
->header
.uname
);
557 gid_to_gname (0, &tmpname
);
558 GNAME_TO_CHARS (tmpname
, header
->header
.gname
);
561 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
562 header
->header
.typeflag
= type
;
563 finish_header (st
, header
, -1);
565 header
= find_next_block ();
567 bufsize
= available_space_after (header
);
569 while (bufsize
< size
)
571 memcpy (header
->buffer
, p
, bufsize
);
574 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
575 header
= find_next_block ();
576 bufsize
= available_space_after (header
);
578 memcpy (header
->buffer
, p
, size
);
579 memset (header
->buffer
+ size
, 0, bufsize
- size
);
580 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
584 split_long_name (const char *name
, size_t length
)
588 if (length
> PREFIX_FIELD_SIZE
+ 1)
589 length
= PREFIX_FIELD_SIZE
+ 1;
590 else if (ISSLASH (name
[length
- 1]))
592 for (i
= length
- 1; i
> 0; i
--)
593 if (ISSLASH (name
[i
]))
599 write_ustar_long_name (const char *name
)
601 size_t length
= strlen (name
);
605 if (length
> PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1)
607 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
608 quotearg_colon (name
),
609 PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1));
613 i
= split_long_name (name
, length
);
614 if (i
== 0 || (nlen
= length
- i
- 1) > NAME_FIELD_SIZE
|| nlen
== 0)
617 _("%s: file name is too long (cannot be split); not dumped"),
618 quotearg_colon (name
)));
622 header
= find_next_block ();
623 memset (header
->buffer
, 0, sizeof (header
->buffer
));
624 memcpy (header
->header
.prefix
, name
, i
);
625 memcpy (header
->header
.name
, name
+ i
+ 1, length
- i
- 1);
630 /* Write a long link name, depending on the current archive format */
632 write_long_link (struct tar_stat_info
*st
)
634 switch (archive_format
)
637 xheader_store ("linkpath", st
, NULL
);
640 case V7_FORMAT
: /* old V7 tar format */
644 _("%s: link name is too long; not dumped"),
645 quotearg_colon (st
->link_name
)));
650 write_gnu_long_link (st
, st
->link_name
, GNUTYPE_LONGLINK
);
659 write_long_name (struct tar_stat_info
*st
)
661 switch (archive_format
)
664 xheader_store ("path", st
, NULL
);
668 if (strlen (st
->file_name
) > NAME_FIELD_SIZE
-1)
670 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
671 quotearg_colon (st
->file_name
),
672 NAME_FIELD_SIZE
- 1));
679 return write_ustar_long_name (st
->file_name
);
683 write_gnu_long_link (st
, st
->file_name
, GNUTYPE_LONGNAME
);
689 return write_short_name (st
);
693 write_extended (bool global
, struct tar_stat_info
*st
, union block
*old_header
)
695 union block
*header
, hp
;
700 if (st
->xhdr
.buffer
|| st
->xhdr
.stk
== NULL
)
703 xheader_finish (&st
->xhdr
);
704 memcpy (hp
.buffer
, old_header
, sizeof (hp
));
708 p
= xheader_ghdr_name ();
714 p
= xheader_xhdr_name (st
);
715 t
= st
->stat
.st_mtime
;
717 xheader_write (type
, p
, t
, &st
->xhdr
);
719 header
= find_next_block ();
720 memcpy (header
, &hp
.buffer
, sizeof (hp
.buffer
));
725 write_header_name (struct tar_stat_info
*st
)
727 if (archive_format
== POSIX_FORMAT
&& !string_ascii_p (st
->file_name
))
729 xheader_store ("path", st
, NULL
);
730 return write_short_name (st
);
732 else if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
733 < strlen (st
->file_name
))
734 return write_long_name (st
);
736 return write_short_name (st
);
740 /* Header handling. */
742 /* Make a header block for the file whose stat info is st,
743 and return its address. */
746 start_header (struct tar_stat_info
*st
)
750 header
= write_header_name (st
);
754 /* Override some stat fields, if requested to do so. */
756 if (owner_option
!= (uid_t
) -1)
757 st
->stat
.st_uid
= owner_option
;
758 if (group_option
!= (gid_t
) -1)
759 st
->stat
.st_gid
= group_option
;
762 ((st
->stat
.st_mode
& ~MODE_ALL
)
763 | mode_adjust (st
->stat
.st_mode
, S_ISDIR (st
->stat
.st_mode
) != 0,
764 initial_umask
, mode_option
, NULL
));
766 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
767 for a few tars and came up with the following interoperability
771 1 2 3 4 5 6 7 8 9 READER
772 . . . . . . . . . 1 = SunOS 4.2 tar
773 # . . # # . . # # 2 = NEC SVR4.0.2 tar
774 . . . # # . . # . 3 = Solaris 2.1 tar
775 . . . . . . . . . 4 = GNU tar 1.11.1
776 . . . . . . . . . 5 = HP-UX 8.07 tar
777 . . . . . . . . . 6 = Ultrix 4.1
778 . . . . . . . . . 7 = AIX 3.2
779 . . . . . . . . . 8 = Hitachi HI-UX 1.03
780 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
783 # = ``impossible file type''
785 The following mask for old archive removes the `#'s in column 4
786 above, thus making GNU tar both a universal donor and a universal
787 acceptor for Paul's test. */
789 if (archive_format
== V7_FORMAT
|| archive_format
== USTAR_FORMAT
)
790 MODE_TO_CHARS (st
->stat
.st_mode
& MODE_ALL
, header
->header
.mode
);
792 MODE_TO_CHARS (st
->stat
.st_mode
, header
->header
.mode
);
795 uid_t uid
= st
->stat
.st_uid
;
796 if (archive_format
== POSIX_FORMAT
797 && MAX_OCTAL_VAL (header
->header
.uid
) < uid
)
799 xheader_store ("uid", st
, NULL
);
802 if (!UID_TO_CHARS (uid
, header
->header
.uid
))
807 gid_t gid
= st
->stat
.st_gid
;
808 if (archive_format
== POSIX_FORMAT
809 && MAX_OCTAL_VAL (header
->header
.gid
) < gid
)
811 xheader_store ("gid", st
, NULL
);
814 if (!GID_TO_CHARS (gid
, header
->header
.gid
))
819 off_t size
= st
->stat
.st_size
;
820 if (archive_format
== POSIX_FORMAT
821 && MAX_OCTAL_VAL (header
->header
.size
) < size
)
823 xheader_store ("size", st
, NULL
);
826 if (!OFF_TO_CHARS (size
, header
->header
.size
))
831 struct timespec mtime
= set_mtime_option
? mtime_option
: st
->mtime
;
832 if (archive_format
== POSIX_FORMAT
)
834 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
835 || mtime
.tv_nsec
!= 0)
836 xheader_store ("mtime", st
, &mtime
);
837 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
)
840 if (!TIME_TO_CHARS (mtime
.tv_sec
, header
->header
.mtime
))
845 if (S_ISCHR (st
->stat
.st_mode
)
846 || S_ISBLK (st
->stat
.st_mode
))
848 major_t devmajor
= major (st
->stat
.st_rdev
);
849 minor_t devminor
= minor (st
->stat
.st_rdev
);
851 if (archive_format
== POSIX_FORMAT
852 && MAX_OCTAL_VAL (header
->header
.devmajor
) < devmajor
)
854 xheader_store ("devmajor", st
, NULL
);
857 if (!MAJOR_TO_CHARS (devmajor
, header
->header
.devmajor
))
860 if (archive_format
== POSIX_FORMAT
861 && MAX_OCTAL_VAL (header
->header
.devminor
) < devminor
)
863 xheader_store ("devminor", st
, NULL
);
866 if (!MINOR_TO_CHARS (devminor
, header
->header
.devminor
))
869 else if (archive_format
!= GNU_FORMAT
&& archive_format
!= OLDGNU_FORMAT
)
871 if (!(MAJOR_TO_CHARS (0, header
->header
.devmajor
)
872 && MINOR_TO_CHARS (0, header
->header
.devminor
)))
876 if (archive_format
== POSIX_FORMAT
)
878 xheader_store ("atime", st
, NULL
);
879 xheader_store ("ctime", st
, NULL
);
881 else if (incremental_option
)
882 if (archive_format
== OLDGNU_FORMAT
|| archive_format
== GNU_FORMAT
)
884 TIME_TO_CHARS (st
->atime
.tv_sec
, header
->oldgnu_header
.atime
);
885 TIME_TO_CHARS (st
->ctime
.tv_sec
, header
->oldgnu_header
.ctime
);
888 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
890 switch (archive_format
)
896 case GNU_FORMAT
: /*FIXME?*/
897 /* Overwrite header->header.magic and header.version in one blow. */
898 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
903 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
904 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
911 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
913 /* header->header.[ug]name are left as the empty string. */
917 uid_to_uname (st
->stat
.st_uid
, &st
->uname
);
918 gid_to_gname (st
->stat
.st_gid
, &st
->gname
);
920 if (archive_format
== POSIX_FORMAT
921 && (strlen (st
->uname
) > UNAME_FIELD_SIZE
922 || !string_ascii_p (st
->uname
)))
923 xheader_store ("uname", st
, NULL
);
924 UNAME_TO_CHARS (st
->uname
, header
->header
.uname
);
926 if (archive_format
== POSIX_FORMAT
927 && (strlen (st
->gname
) > GNAME_FIELD_SIZE
928 || !string_ascii_p (st
->gname
)))
929 xheader_store ("gname", st
, NULL
);
930 GNAME_TO_CHARS (st
->gname
, header
->header
.gname
);
937 simple_finish_header (union block
*header
)
943 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
947 for (i
= sizeof *header
; i
-- != 0; )
948 /* We can't use unsigned char here because of old compilers, e.g. V7. */
951 /* Fill in the checksum field. It's formatted differently from the
952 other fields: it has [6] digits, a null, then a space -- rather than
953 digits, then a null. We use to_chars.
954 The final space is already there, from
955 checksumming, and to_chars doesn't modify it.
957 This is a fast way to do:
959 sprintf(header->header.chksum, "%6o", sum); */
961 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
963 set_next_block_after (header
);
966 /* Finish off a filled-in header block and write it out. We also
967 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
968 is not negative, is the block ordinal of the first record for this
969 file, which may be a preceding long name or long link record. */
971 finish_header (struct tar_stat_info
*st
,
972 union block
*header
, off_t block_ordinal
)
974 /* Note: It is important to do this before the call to write_extended(),
975 so that the actual ustar header is printed */
977 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
978 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
979 && header
->header
.typeflag
!= XHDTYPE
980 && header
->header
.typeflag
!= XGLTYPE
)
982 /* FIXME: This global is used in print_header, sigh. */
983 current_format
= archive_format
;
984 print_header (st
, header
, block_ordinal
);
987 header
= write_extended (false, st
, header
);
988 simple_finish_header (header
);
993 pad_archive (off_t size_left
)
996 while (size_left
> 0)
998 blk
= find_next_block ();
999 memset (blk
->buffer
, 0, BLOCKSIZE
);
1000 set_next_block_after (blk
);
1001 size_left
-= BLOCKSIZE
;
1005 static enum dump_status
1006 dump_regular_file (int fd
, struct tar_stat_info
*st
)
1008 off_t size_left
= st
->stat
.st_size
;
1009 off_t block_ordinal
;
1012 block_ordinal
= current_block_ordinal ();
1013 blk
= start_header (st
);
1015 return dump_status_fail
;
1017 /* Mark contiguous files, if we support them. */
1018 if (archive_format
!= V7_FORMAT
&& S_ISCTG (st
->stat
.st_mode
))
1019 blk
->header
.typeflag
= CONTTYPE
;
1021 finish_header (st
, blk
, block_ordinal
);
1023 mv_begin_write (st
->file_name
, st
->stat
.st_size
, st
->stat
.st_size
);
1024 while (size_left
> 0)
1026 size_t bufsize
, count
;
1028 blk
= find_next_block ();
1030 bufsize
= available_space_after (blk
);
1032 if (size_left
< bufsize
)
1034 /* Last read -- zero out area beyond. */
1035 bufsize
= size_left
;
1036 count
= bufsize
% BLOCKSIZE
;
1038 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1041 count
= (fd
< 0) ? bufsize
: safe_read (fd
, blk
->buffer
, bufsize
);
1042 if (count
== SAFE_READ_ERROR
)
1044 read_diag_details (st
->orig_file_name
,
1045 st
->stat
.st_size
- size_left
, bufsize
);
1046 pad_archive (size_left
);
1047 return dump_status_short
;
1050 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1052 if (count
!= bufsize
)
1054 char buf
[UINTMAX_STRSIZE_BOUND
];
1055 memset (blk
->buffer
+ count
, 0, bufsize
- count
);
1056 WARNOPT (WARN_FILE_SHRANK
,
1058 ngettext ("%s: File shrank by %s byte; padding with zeros",
1059 "%s: File shrank by %s bytes; padding with zeros",
1061 quotearg_colon (st
->orig_file_name
),
1062 STRINGIFY_BIGINT (size_left
, buf
)));
1063 if (! ignore_failed_read_option
)
1064 set_exit_status (TAREXIT_DIFFERS
);
1065 pad_archive (size_left
- (bufsize
- count
));
1066 return dump_status_short
;
1069 return dump_status_ok
;
1073 /* Copy info from the directory identified by ST into the archive.
1074 DIRECTORY contains the directory's entries. */
1077 dump_dir0 (struct tar_stat_info
*st
, char const *directory
)
1079 bool top_level
= ! st
->parent
;
1080 const char *tag_file_name
;
1081 union block
*blk
= NULL
;
1082 off_t block_ordinal
= current_block_ordinal ();
1084 st
->stat
.st_size
= 0; /* force 0 size on dir */
1086 blk
= start_header (st
);
1090 if (incremental_option
&& archive_format
!= POSIX_FORMAT
)
1091 blk
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1092 else /* if (standard_option) */
1093 blk
->header
.typeflag
= DIRTYPE
;
1095 /* If we're gnudumping, we aren't done yet so don't close it. */
1097 if (!incremental_option
)
1098 finish_header (st
, blk
, block_ordinal
);
1099 else if (gnu_list_name
->directory
)
1101 if (archive_format
== POSIX_FORMAT
)
1103 xheader_store ("GNU.dumpdir", st
,
1104 safe_directory_contents (gnu_list_name
->directory
));
1105 finish_header (st
, blk
, block_ordinal
);
1113 const char *buffer
, *p_buffer
;
1115 block_ordinal
= current_block_ordinal ();
1116 buffer
= safe_directory_contents (gnu_list_name
->directory
);
1117 totsize
= dumpdir_size (buffer
);
1118 OFF_TO_CHARS (totsize
, blk
->header
.size
);
1119 finish_header (st
, blk
, block_ordinal
);
1121 size_left
= totsize
;
1123 mv_begin_write (st
->file_name
, totsize
, totsize
);
1124 while (size_left
> 0)
1126 blk
= find_next_block ();
1127 bufsize
= available_space_after (blk
);
1128 if (size_left
< bufsize
)
1130 bufsize
= size_left
;
1131 count
= bufsize
% BLOCKSIZE
;
1133 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1135 memcpy (blk
->buffer
, p_buffer
, bufsize
);
1136 size_left
-= bufsize
;
1137 p_buffer
+= bufsize
;
1138 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1144 if (!recursion_option
)
1147 if (one_file_system_option
1149 && st
->parent
->stat
.st_dev
!= st
->stat
.st_dev
)
1154 _("%s: file is on a different filesystem; not dumped"),
1155 quotearg_colon (st
->orig_file_name
)));
1162 switch (check_exclusion_tags (st
->fd
, &tag_file_name
))
1164 case exclusion_tag_all
:
1165 /* Handled in dump_file0 */
1168 case exclusion_tag_none
:
1174 name_buf
= xstrdup (st
->orig_file_name
);
1175 name_size
= name_len
= strlen (name_buf
);
1177 /* Now output all the files in the directory. */
1178 for (entry
= directory
; (entry_len
= strlen (entry
)) != 0;
1179 entry
+= entry_len
+ 1)
1181 if (name_size
< name_len
+ entry_len
)
1183 name_size
= name_len
+ entry_len
;
1184 name_buf
= xrealloc (name_buf
, name_size
+ 1);
1186 strcpy (name_buf
+ name_len
, entry
);
1187 if (!excluded_name (name_buf
))
1188 dump_file (st
, entry
, name_buf
);
1195 case exclusion_tag_contents
:
1196 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1197 _("contents not dumped"));
1198 name_size
= strlen (st
->orig_file_name
) + strlen (tag_file_name
) + 1;
1199 name_buf
= xmalloc (name_size
);
1200 strcpy (name_buf
, st
->orig_file_name
);
1201 strcat (name_buf
, tag_file_name
);
1202 dump_file (st
, tag_file_name
, name_buf
);
1206 case exclusion_tag_under
:
1207 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1208 _("contents not dumped"));
1214 /* Ensure exactly one trailing slash. */
1216 ensure_slash (char **pstr
)
1218 size_t len
= strlen (*pstr
);
1219 while (len
>= 1 && ISSLASH ((*pstr
)[len
- 1]))
1221 if (!ISSLASH ((*pstr
)[len
]))
1222 *pstr
= xrealloc (*pstr
, len
+ 2);
1223 (*pstr
)[len
++] = '/';
1224 (*pstr
)[len
] = '\0';
1228 dump_dir (struct tar_stat_info
*st
)
1230 char *directory
= 0;
1231 int dupfd
= dup (st
->fd
);
1234 directory
= fdsavedir (dupfd
);
1244 savedir_diag (st
->orig_file_name
);
1248 dump_dir0 (st
, directory
);
1255 /* Number of links a file can have without having to be entered into
1256 the link table. Typically this is 1, but in trickier circumstances
1258 static nlink_t trivial_link_count
;
1261 /* Main functions of this module. */
1264 create_archive (void)
1266 struct name
const *p
;
1268 trivial_link_count
= name_count
<= 1 && ! dereference_option
;
1270 open_archive (ACCESS_WRITE
);
1271 buffer_write_global_xheader ();
1273 if (incremental_option
)
1275 size_t buffer_size
= 1000;
1276 char *buffer
= xmalloc (buffer_size
);
1279 collect_and_sort_names ();
1281 while ((p
= name_from_list ()) != NULL
)
1282 if (!excluded_name (p
->name
))
1283 dump_file (0, p
->name
, p
->name
);
1286 while ((p
= name_from_list ()) != NULL
)
1287 if (!excluded_name (p
->name
))
1289 struct tar_stat_info st
;
1290 size_t plen
= strlen (p
->name
);
1291 if (buffer_size
<= plen
)
1293 while ((buffer_size
*= 2) <= plen
)
1295 buffer
= xrealloc (buffer
, buffer_size
);
1297 memcpy (buffer
, p
->name
, plen
);
1298 if (! ISSLASH (buffer
[plen
- 1]))
1299 buffer
[plen
++] = DIRECTORY_SEPARATOR
;
1300 tar_stat_init (&st
);
1301 q
= directory_contents (gnu_list_name
->directory
);
1305 size_t qlen
= strlen (q
);
1308 if (! st
.orig_file_name
)
1310 st
.orig_file_name
= xstrdup (p
->name
);
1311 st
.fd
= open (st
.orig_file_name
,
1312 ((open_read_flags
- O_RDONLY
1317 open_diag (p
->name
);
1320 if (fstat (st
.fd
, &st
.stat
) != 0)
1322 stat_diag (p
->name
);
1326 if (buffer_size
< plen
+ qlen
)
1328 while ((buffer_size
*=2 ) < plen
+ qlen
)
1330 buffer
= xrealloc (buffer
, buffer_size
);
1332 strcpy (buffer
+ plen
, q
+ 1);
1333 dump_file (&st
, q
+ 1, buffer
);
1337 tar_stat_destroy (&st
);
1344 while ((name
= name_next (1)) != NULL
)
1345 if (!excluded_name (name
))
1346 dump_file (0, name
, name
);
1351 finish_deferred_unlinks ();
1352 if (listed_incremental_option
)
1353 write_directory_file ();
1357 /* Calculate the hash of a link. */
1359 hash_link (void const *entry
, size_t n_buckets
)
1361 struct link
const *l
= entry
;
1362 uintmax_t num
= l
->dev
^ l
->ino
;
1363 return num
% n_buckets
;
1366 /* Compare two links for equality. */
1368 compare_links (void const *entry1
, void const *entry2
)
1370 struct link
const *link1
= entry1
;
1371 struct link
const *link2
= entry2
;
1372 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
1376 unknown_file_error (char const *p
)
1378 WARNOPT (WARN_FILE_IGNORED
,
1379 (0, 0, _("%s: Unknown file type; file ignored"),
1380 quotearg_colon (p
)));
1381 if (!ignore_failed_read_option
)
1382 set_exit_status (TAREXIT_FAILURE
);
1386 /* Handling of hard links */
1388 /* Table of all non-directories that we've written so far. Any time
1389 we see another, we check the table and avoid dumping the data
1390 again if we've done it once already. */
1391 static Hash_table
*link_table
;
1393 /* Try to dump stat as a hard link to another file in the archive.
1394 Return true if successful. */
1396 dump_hard_link (struct tar_stat_info
*st
)
1399 && (trivial_link_count
< st
->stat
.st_nlink
|| remove_files_option
))
1402 struct link
*duplicate
;
1403 off_t block_ordinal
;
1406 lp
.ino
= st
->stat
.st_ino
;
1407 lp
.dev
= st
->stat
.st_dev
;
1409 if ((duplicate
= hash_lookup (link_table
, &lp
)))
1411 /* We found a link. */
1412 char const *link_name
= safer_name_suffix (duplicate
->name
, true,
1413 absolute_names_option
);
1417 block_ordinal
= current_block_ordinal ();
1418 assign_string (&st
->link_name
, link_name
);
1419 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
1420 < strlen (link_name
))
1421 write_long_link (st
);
1423 st
->stat
.st_size
= 0;
1424 blk
= start_header (st
);
1427 tar_copy_str (blk
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1429 blk
->header
.typeflag
= LNKTYPE
;
1430 finish_header (st
, blk
, block_ordinal
);
1432 if (remove_files_option
)
1433 queue_deferred_unlink (st
->orig_file_name
, false);
1442 file_count_links (struct tar_stat_info
*st
)
1444 if (hard_dereference_option
)
1446 if (trivial_link_count
< st
->stat
.st_nlink
)
1448 struct link
*duplicate
;
1449 char *linkname
= NULL
;
1452 assign_string (&linkname
, st
->orig_file_name
);
1453 transform_name (&linkname
, XFORM_LINK
);
1455 lp
= xmalloc (offsetof (struct link
, name
)
1456 + strlen (linkname
) + 1);
1457 lp
->ino
= st
->stat
.st_ino
;
1458 lp
->dev
= st
->stat
.st_dev
;
1459 lp
->nlink
= st
->stat
.st_nlink
;
1460 strcpy (lp
->name
, linkname
);
1464 || (link_table
= hash_initialize (0, 0, hash_link
,
1466 && (duplicate
= hash_insert (link_table
, lp
))))
1469 if (duplicate
!= lp
)
1475 /* For each dumped file, check if all its links were dumped. Emit
1476 warnings if it is not so. */
1485 for (lp
= hash_get_first (link_table
); lp
;
1486 lp
= hash_get_next (link_table
, lp
))
1490 WARN ((0, 0, _("Missing links to %s."), quote (lp
->name
)));
1495 /* Dump a single file, recursing on directories. ST is the file's
1496 status info, NAME its name relative to the parent directory, and P
1497 its full name (which may be relative to the working directory). */
1499 /* FIXME: One should make sure that for *every* path leading to setting
1500 exit_status to failure, a clear diagnostic has been issued. */
1503 dump_file0 (struct tar_stat_info
*st
, char const *name
, char const *p
)
1505 union block
*header
;
1507 off_t original_size
;
1508 struct timespec original_ctime
;
1509 struct timespec restore_times
[2];
1510 off_t block_ordinal
= -1;
1513 bool top_level
= ! st
->parent
;
1514 int parentfd
= top_level
? AT_FDCWD
: st
->parent
->fd
;
1515 void (*diag
) (char const *) = 0;
1517 if (interactive_option
&& !confirm ("add", p
))
1520 assign_string (&st
->orig_file_name
, p
);
1521 assign_string (&st
->file_name
,
1522 safer_name_suffix (p
, false, absolute_names_option
));
1524 transform_name (&st
->file_name
, XFORM_REGFILE
);
1526 if (fstatat (parentfd
, name
, &st
->stat
, fstatat_flags
) != 0)
1528 else if (file_dumpable_p (&st
->stat
))
1530 fd
= st
->fd
= openat (parentfd
, name
, open_read_flags
);
1533 else if (fstat (fd
, &st
->stat
) != 0)
1538 file_removed_diag (p
, top_level
, diag
);
1542 st
->archive_file_size
= original_size
= st
->stat
.st_size
;
1543 st
->atime
= restore_times
[0] = get_stat_atime (&st
->stat
);
1544 st
->mtime
= restore_times
[1] = get_stat_mtime (&st
->stat
);
1545 st
->ctime
= original_ctime
= get_stat_ctime (&st
->stat
);
1548 if (S_ISHIDDEN (st
->stat
.st_mode
))
1550 char *new = (char *) alloca (strlen (p
) + 2);
1560 /* See if we want only new files, and check if this one is too old to
1563 This check is omitted if incremental_option is set *and* the
1564 requested file is not explicitely listed in the command line. */
1566 if (!(incremental_option
&& !is_individual_file (p
))
1567 && !S_ISDIR (st
->stat
.st_mode
)
1568 && OLDER_TAR_STAT_TIME (*st
, m
)
1569 && (!after_date_option
|| OLDER_TAR_STAT_TIME (*st
, c
)))
1571 if (!incremental_option
&& verbose_option
)
1572 WARNOPT (WARN_FILE_UNCHANGED
,
1573 (0, 0, _("%s: file is unchanged; not dumped"),
1574 quotearg_colon (p
)));
1578 /* See if we are trying to dump the archive. */
1579 if (sys_file_is_archive (st
))
1581 WARNOPT (WARN_IGNORE_ARCHIVE
,
1582 (0, 0, _("%s: file is the archive; not dumped"),
1583 quotearg_colon (p
)));
1587 is_dir
= S_ISDIR (st
->stat
.st_mode
) != 0;
1589 if (!is_dir
&& dump_hard_link (st
))
1592 if (is_dir
|| S_ISREG (st
->stat
.st_mode
) || S_ISCTG (st
->stat
.st_mode
))
1595 struct stat final_stat
;
1599 const char *tag_file_name
;
1600 ensure_slash (&st
->orig_file_name
);
1601 ensure_slash (&st
->file_name
);
1603 if (check_exclusion_tags (fd
, &tag_file_name
) == exclusion_tag_all
)
1605 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1606 _("directory not dumped"));
1614 enum dump_status status
;
1616 if (fd
!= -1 && sparse_option
&& ST_IS_SPARSE (st
->stat
))
1618 status
= sparse_dump_file (fd
, st
);
1619 if (status
== dump_status_not_implemented
)
1620 status
= dump_regular_file (fd
, st
);
1623 status
= dump_regular_file (fd
, st
);
1627 case dump_status_ok
:
1628 case dump_status_short
:
1629 file_count_links (st
);
1632 case dump_status_fail
:
1635 case dump_status_not_implemented
:
1639 ok
= status
== dump_status_ok
;
1645 ? fstatat (parentfd
, name
, &final_stat
, fstatat_flags
)
1646 : fstat (fd
, &final_stat
))
1649 file_removed_diag (p
, top_level
, stat_diag
);
1656 if ((timespec_cmp (get_stat_ctime (&final_stat
), original_ctime
) != 0
1657 /* Original ctime will change if the file is a directory and
1658 --remove-files is given */
1659 && !(remove_files_option
&& is_dir
))
1660 || original_size
< final_stat
.st_size
)
1662 WARNOPT (WARN_FILE_CHANGED
,
1663 (0, 0, _("%s: file changed as we read it"),
1664 quotearg_colon (p
)));
1665 set_exit_status (TAREXIT_DIFFERS
);
1667 else if (atime_preserve_option
== replace_atime_preserve
1668 && set_file_atime (fd
, p
, restore_times
) != 0)
1674 if (close (fd
) != 0)
1682 if (ok
&& remove_files_option
)
1683 queue_deferred_unlink (p
, is_dir
);
1687 #ifdef HAVE_READLINK
1688 else if (S_ISLNK (st
->stat
.st_mode
))
1692 size_t linklen
= st
->stat
.st_size
;
1693 if (linklen
!= st
->stat
.st_size
|| linklen
+ 1 == 0)
1695 buffer
= (char *) alloca (linklen
+ 1);
1696 size
= readlinkat (parentfd
, name
, buffer
, linklen
+ 1);
1699 file_removed_diag (p
, top_level
, readlink_diag
);
1702 buffer
[size
] = '\0';
1703 assign_string (&st
->link_name
, buffer
);
1704 transform_name (&st
->link_name
, XFORM_SYMLINK
);
1705 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
) < size
)
1706 write_long_link (st
);
1708 block_ordinal
= current_block_ordinal ();
1709 st
->stat
.st_size
= 0; /* force 0 size on symlink */
1710 header
= start_header (st
);
1713 tar_copy_str (header
->header
.linkname
, st
->link_name
, NAME_FIELD_SIZE
);
1714 header
->header
.typeflag
= SYMTYPE
;
1715 finish_header (st
, header
, block_ordinal
);
1716 /* nothing more to do to it */
1718 if (remove_files_option
)
1719 queue_deferred_unlink (p
, false);
1721 file_count_links (st
);
1725 else if (S_ISCHR (st
->stat
.st_mode
))
1727 else if (S_ISBLK (st
->stat
.st_mode
))
1729 else if (S_ISFIFO (st
->stat
.st_mode
))
1731 else if (S_ISSOCK (st
->stat
.st_mode
))
1733 WARNOPT (WARN_FILE_IGNORED
,
1734 (0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1737 else if (S_ISDOOR (st
->stat
.st_mode
))
1739 WARNOPT (WARN_FILE_IGNORED
,
1740 (0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1745 unknown_file_error (p
);
1749 if (archive_format
== V7_FORMAT
)
1751 unknown_file_error (p
);
1755 block_ordinal
= current_block_ordinal ();
1756 st
->stat
.st_size
= 0; /* force 0 size */
1757 header
= start_header (st
);
1760 header
->header
.typeflag
= type
;
1762 if (type
!= FIFOTYPE
)
1764 MAJOR_TO_CHARS (major (st
->stat
.st_rdev
),
1765 header
->header
.devmajor
);
1766 MINOR_TO_CHARS (minor (st
->stat
.st_rdev
),
1767 header
->header
.devminor
);
1770 finish_header (st
, header
, block_ordinal
);
1771 if (remove_files_option
)
1772 queue_deferred_unlink (p
, false);
1775 /* Dump a file, recursively. PARENT describes the file's parent
1776 directory, NAME is the file's name relative to PARENT, and FULLNAME
1777 its full name, possibly relative to the working directory. NAME
1778 may contain slashes at the top level of invocation. */
1781 dump_file (struct tar_stat_info
*parent
, char const *name
,
1782 char const *fullname
)
1784 struct tar_stat_info st
;
1785 tar_stat_init (&st
);
1787 dump_file0 (&st
, name
, fullname
);
1788 if (parent
&& listed_incremental_option
)
1789 update_parent_directory (parent
);
1790 tar_stat_destroy (&st
);