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
) (const char *name
);
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
) (const char *name
))
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 (const char *dirname
, const char **tag_file_name
)
78 static size_t tagsize
;
79 struct exclusion_tag
*tag
;
80 size_t dlen
= strlen (dirname
);
81 int addslash
= !ISSLASH (dirname
[dlen
-1]);
84 for (tag
= exclusion_tags
; tag
; tag
= tag
->next
)
86 size_t size
= dlen
+ addslash
+ tag
->length
+ 1;
90 tagname
= xrealloc (tagname
, tagsize
);
95 strcpy (tagname
, dirname
);
98 tagname
[noff
++] = '/';
100 strcpy (tagname
+ noff
, tag
->name
);
101 if (access (tagname
, F_OK
) == 0
102 && (!tag
->predicate
|| tag
->predicate (tagname
)))
105 *tag_file_name
= tag
->name
;
110 return exclusion_tag_none
;
113 /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
114 contains a valid header, as described at:
115 http://www.brynosaurus.com/cachedir
116 Applications can write this file into directories they create
117 for use as caches containing purely regenerable, non-precious data,
118 allowing us to avoid archiving them if --exclude-caches is specified. */
120 #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
121 #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
124 cachedir_file_p (const char *name
)
126 bool tag_present
= false;
127 int fd
= open (name
, O_RDONLY
);
130 static char tagbuf
[CACHEDIR_SIGNATURE_SIZE
];
132 if (read (fd
, tagbuf
, CACHEDIR_SIGNATURE_SIZE
)
133 == CACHEDIR_SIGNATURE_SIZE
134 && memcmp (tagbuf
, CACHEDIR_SIGNATURE
, CACHEDIR_SIGNATURE_SIZE
) == 0)
143 /* The maximum uintmax_t value that can be represented with DIGITS digits,
144 assuming that each digit is BITS_PER_DIGIT wide. */
145 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
146 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
147 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
150 /* The maximum uintmax_t value that can be represented with octal
151 digits and a trailing NUL in BUFFER. */
152 #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
154 /* Convert VALUE to an octal representation suitable for tar headers.
155 Output to buffer WHERE with size SIZE.
156 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
159 to_octal (uintmax_t value
, char *where
, size_t size
)
166 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
172 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
173 NUL unless SRC is LEN or more bytes long. */
176 tar_copy_str (char *dst
, const char *src
, size_t len
)
179 for (i
= 0; i
< len
; i
++)
180 if (! (dst
[i
] = src
[i
]))
184 /* Same as tar_copy_str, but always terminate with NUL if using
188 tar_name_copy_str (char *dst
, const char *src
, size_t len
)
190 tar_copy_str (dst
, src
, len
);
191 if (archive_format
== OLDGNU_FORMAT
)
195 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
196 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
197 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
198 The result is undefined if SIZE is 0 or if VALUE is too large to
202 to_base256 (int negative
, uintmax_t value
, char *where
, size_t size
)
205 uintmax_t propagated_sign_bits
=
206 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
211 where
[--i
] = v
& ((1 << LG_256
) - 1);
212 v
= propagated_sign_bits
| (v
>> LG_256
);
217 #define GID_TO_CHARS(val, where) gid_to_chars (val, where, sizeof (where))
218 #define MAJOR_TO_CHARS(val, where) major_to_chars (val, where, sizeof (where))
219 #define MINOR_TO_CHARS(val, where) minor_to_chars (val, where, sizeof (where))
220 #define MODE_TO_CHARS(val, where) mode_to_chars (val, where, sizeof (where))
221 #define UID_TO_CHARS(val, where) uid_to_chars (val, where, sizeof (where))
222 #define UINTMAX_TO_CHARS(val, where) uintmax_to_chars (val, where, sizeof (where))
223 #define UNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
224 #define GNAME_TO_CHARS(name,buf) string_to_chars (name, buf, sizeof(buf))
227 to_chars (int negative
, uintmax_t value
, size_t valsize
,
228 uintmax_t (*substitute
) (int *),
229 char *where
, size_t size
, const char *type
);
232 to_chars_subst (int negative
, int gnu_format
, uintmax_t value
, size_t valsize
,
233 uintmax_t (*substitute
) (int *),
234 char *where
, size_t size
, const char *type
)
236 uintmax_t maxval
= (gnu_format
237 ? MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)
238 : MAX_VAL_WITH_DIGITS (size
- 1, LG_8
));
239 char valbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
240 char maxbuf
[UINTMAX_STRSIZE_BOUND
];
241 char minbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
242 char const *minval_string
;
243 char const *maxval_string
= STRINGIFY_BIGINT (maxval
, maxbuf
);
244 char const *value_string
;
248 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
249 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
258 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
263 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
268 uintmax_t sub
= substitute (&negsub
) & maxval
;
269 /* NOTE: This is one of the few places where GNU_FORMAT differs from
270 OLDGNU_FORMAT. The actual differences are:
272 1. In OLDGNU_FORMAT all strings in a tar header end in \0
273 2. Incremental archives use oldgnu_header.
275 Apart from this they are completely identical. */
276 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? - sub
: sub
;
277 char subbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
278 char *sub_string
= STRINGIFY_BIGINT (s
, subbuf
+ 1);
281 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
282 value_string
, type
, minval_string
, maxval_string
,
284 return to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
287 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
288 value_string
, type
, minval_string
, maxval_string
));
292 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
293 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
294 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
295 negative before being cast to uintmax_t; its original bitpattern
296 can be deduced from VALSIZE, its original size before casting.
297 TYPE is the kind of value being output (useful for diagnostics).
298 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
299 digits), followed by '\0'. If this won't work, and if GNU or
300 OLDGNU format is allowed, use '\200' followed by base-256, or (if
301 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
302 If neither format works, use SUBSTITUTE (...) instead. Pass to
303 SUBSTITUTE the address of an 0-or-1 flag recording whether the
304 substitute value is negative. */
307 to_chars (int negative
, uintmax_t value
, size_t valsize
,
308 uintmax_t (*substitute
) (int *),
309 char *where
, size_t size
, const char *type
)
311 int gnu_format
= (archive_format
== GNU_FORMAT
312 || archive_format
== OLDGNU_FORMAT
);
314 /* Generate the POSIX octal representation if the number fits. */
315 if (! negative
&& value
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
317 where
[size
- 1] = '\0';
318 to_octal (value
, where
, size
- 1);
323 /* Try to cope with the number by using traditional GNU format
326 /* Generate the base-256 representation if the number fits. */
327 if (((negative
? -1 - value
: value
)
328 <= MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)))
330 where
[0] = negative
? -1 : 1 << (LG_256
- 1);
331 to_base256 (negative
, value
, where
+ 1, size
- 1);
335 /* Otherwise, if the number is negative, and if it would not cause
336 ambiguity on this host by confusing positive with negative
337 values, then generate the POSIX octal representation of the value
338 modulo 2**(field bits). The resulting tar file is
339 machine-dependent, since it depends on the host word size. Yuck!
340 But this is the traditional behavior. */
341 else if (negative
&& valsize
* CHAR_BIT
<= (size
- 1) * LG_8
)
343 static int warned_once
;
347 WARN ((0, 0, _("Generating negative octal headers")));
349 where
[size
- 1] = '\0';
350 to_octal (value
& MAX_VAL_WITH_DIGITS (valsize
* CHAR_BIT
, 1),
354 /* Otherwise fall back to substitution, if possible: */
357 substitute
= NULL
; /* No substitution for formats, other than GNU */
359 return to_chars_subst (negative
, gnu_format
, value
, valsize
, substitute
,
364 gid_substitute (int *negative
)
370 static gid_t gid_nobody
;
371 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
380 gid_to_chars (gid_t v
, char *p
, size_t s
)
382 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
386 major_to_chars (major_t v
, char *p
, size_t s
)
388 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
392 minor_to_chars (minor_t v
, char *p
, size_t s
)
394 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
398 mode_to_chars (mode_t v
, char *p
, size_t s
)
400 /* In the common case where the internal and external mode bits are the same,
401 and we are not using POSIX or GNU format,
402 propagate all unknown bits to the external mode.
403 This matches historical practice.
404 Otherwise, just copy the bits we know about. */
407 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
408 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
409 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
410 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
411 && archive_format
!= POSIX_FORMAT
412 && archive_format
!= USTAR_FORMAT
413 && archive_format
!= GNU_FORMAT
)
421 u
= ((v
& S_ISUID
? TSUID
: 0)
422 | (v
& S_ISGID
? TSGID
: 0)
423 | (v
& S_ISVTX
? TSVTX
: 0)
424 | (v
& S_IRUSR
? TUREAD
: 0)
425 | (v
& S_IWUSR
? TUWRITE
: 0)
426 | (v
& S_IXUSR
? TUEXEC
: 0)
427 | (v
& S_IRGRP
? TGREAD
: 0)
428 | (v
& S_IWGRP
? TGWRITE
: 0)
429 | (v
& S_IXGRP
? TGEXEC
: 0)
430 | (v
& S_IROTH
? TOREAD
: 0)
431 | (v
& S_IWOTH
? TOWRITE
: 0)
432 | (v
& S_IXOTH
? TOEXEC
: 0));
434 return to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
438 off_to_chars (off_t v
, char *p
, size_t s
)
440 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
444 size_to_chars (size_t v
, char *p
, size_t s
)
446 return to_chars (0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "size_t");
450 time_to_chars (time_t v
, char *p
, size_t s
)
452 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
456 uid_substitute (int *negative
)
462 static uid_t uid_nobody
;
463 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
472 uid_to_chars (uid_t v
, char *p
, size_t s
)
474 return to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
478 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
480 return to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
484 string_to_chars (char const *str
, char *p
, size_t s
)
486 tar_copy_str (p
, str
, s
);
491 /* A file is considered dumpable if it is sparse and both --sparse and --totals
493 Otherwise, it is dumpable unless any of the following conditions occur:
495 a) it is empty *and* world-readable, or
496 b) current archive is /dev/null */
499 file_dumpable_p (struct tar_stat_info
*st
)
502 return totals_option
&& sparse_option
&& ST_IS_SPARSE (st
->stat
);
503 return !(st
->archive_file_size
== 0
504 && (st
->stat
.st_mode
& MODE_R
) == MODE_R
);
508 /* Writing routines. */
510 /* Write the EOT block(s). Zero at least two blocks, through the end
511 of the record. Old tar, as previous versions of GNU tar, writes
512 garbage after two zeroed blocks. */
516 union block
*pointer
= find_next_block ();
517 memset (pointer
->buffer
, 0, BLOCKSIZE
);
518 set_next_block_after (pointer
);
519 pointer
= find_next_block ();
520 memset (pointer
->buffer
, 0, available_space_after (pointer
));
521 set_next_block_after (pointer
);
524 /* Write a "private" header */
526 start_private_header (const char *name
, size_t size
, time_t t
)
528 union block
*header
= find_next_block ();
530 memset (header
->buffer
, 0, sizeof (union block
));
532 tar_name_copy_str (header
->header
.name
, name
, NAME_FIELD_SIZE
);
533 OFF_TO_CHARS (size
, header
->header
.size
);
535 TIME_TO_CHARS (t
, header
->header
.mtime
);
536 MODE_TO_CHARS (S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, header
->header
.mode
);
537 UID_TO_CHARS (getuid (), header
->header
.uid
);
538 GID_TO_CHARS (getgid (), header
->header
.gid
);
539 MAJOR_TO_CHARS (0, header
->header
.devmajor
);
540 MINOR_TO_CHARS (0, header
->header
.devminor
);
541 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
542 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
546 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
550 write_short_name (struct tar_stat_info
*st
)
552 union block
*header
= find_next_block ();
553 memset (header
->buffer
, 0, sizeof (union block
));
554 tar_name_copy_str (header
->header
.name
, st
->file_name
, NAME_FIELD_SIZE
);
558 #define FILL(field,byte) do { \
559 memset(field, byte, sizeof(field)-1); \
560 (field)[sizeof(field)-1] = 0; \
563 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
565 write_gnu_long_link (struct tar_stat_info
*st
, const char *p
, char type
)
567 size_t size
= strlen (p
) + 1;
572 header
= start_private_header ("././@LongLink", size
, time (NULL
));
573 FILL (header
->header
.mtime
, '0');
574 FILL (header
->header
.mode
, '0');
575 FILL (header
->header
.uid
, '0');
576 FILL (header
->header
.gid
, '0');
577 FILL (header
->header
.devmajor
, 0);
578 FILL (header
->header
.devminor
, 0);
579 uid_to_uname (0, &tmpname
);
580 UNAME_TO_CHARS (tmpname
, header
->header
.uname
);
582 gid_to_gname (0, &tmpname
);
583 GNAME_TO_CHARS (tmpname
, header
->header
.gname
);
586 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
587 header
->header
.typeflag
= type
;
588 finish_header (st
, header
, -1);
590 header
= find_next_block ();
592 bufsize
= available_space_after (header
);
594 while (bufsize
< size
)
596 memcpy (header
->buffer
, p
, bufsize
);
599 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
600 header
= find_next_block ();
601 bufsize
= available_space_after (header
);
603 memcpy (header
->buffer
, p
, size
);
604 memset (header
->buffer
+ size
, 0, bufsize
- size
);
605 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
609 split_long_name (const char *name
, size_t length
)
613 if (length
> PREFIX_FIELD_SIZE
+ 1)
614 length
= PREFIX_FIELD_SIZE
+ 1;
615 else if (ISSLASH (name
[length
- 1]))
617 for (i
= length
- 1; i
> 0; i
--)
618 if (ISSLASH (name
[i
]))
624 write_ustar_long_name (const char *name
)
626 size_t length
= strlen (name
);
630 if (length
> PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1)
632 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
633 quotearg_colon (name
),
634 PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1));
638 i
= split_long_name (name
, length
);
639 if (i
== 0 || (nlen
= length
- i
- 1) > NAME_FIELD_SIZE
|| nlen
== 0)
642 _("%s: file name is too long (cannot be split); not dumped"),
643 quotearg_colon (name
)));
647 header
= find_next_block ();
648 memset (header
->buffer
, 0, sizeof (header
->buffer
));
649 memcpy (header
->header
.prefix
, name
, i
);
650 memcpy (header
->header
.name
, name
+ i
+ 1, length
- i
- 1);
655 /* Write a long link name, depending on the current archive format */
657 write_long_link (struct tar_stat_info
*st
)
659 switch (archive_format
)
662 xheader_store ("linkpath", st
, NULL
);
665 case V7_FORMAT
: /* old V7 tar format */
669 _("%s: link name is too long; not dumped"),
670 quotearg_colon (st
->link_name
)));
675 write_gnu_long_link (st
, st
->link_name
, GNUTYPE_LONGLINK
);
684 write_long_name (struct tar_stat_info
*st
)
686 switch (archive_format
)
689 xheader_store ("path", st
, NULL
);
693 if (strlen (st
->file_name
) > NAME_FIELD_SIZE
-1)
695 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
696 quotearg_colon (st
->file_name
),
697 NAME_FIELD_SIZE
- 1));
704 return write_ustar_long_name (st
->file_name
);
708 write_gnu_long_link (st
, st
->file_name
, GNUTYPE_LONGNAME
);
714 return write_short_name (st
);
718 write_extended (bool global
, struct tar_stat_info
*st
, union block
*old_header
)
720 union block
*header
, hp
;
725 if (st
->xhdr
.buffer
|| st
->xhdr
.stk
== NULL
)
728 xheader_finish (&st
->xhdr
);
729 memcpy (hp
.buffer
, old_header
, sizeof (hp
));
733 p
= xheader_ghdr_name ();
739 p
= xheader_xhdr_name (st
);
740 t
= st
->stat
.st_mtime
;
742 xheader_write (type
, p
, t
, &st
->xhdr
);
744 header
= find_next_block ();
745 memcpy (header
, &hp
.buffer
, sizeof (hp
.buffer
));
750 write_header_name (struct tar_stat_info
*st
)
752 if (archive_format
== POSIX_FORMAT
&& !string_ascii_p (st
->file_name
))
754 xheader_store ("path", st
, NULL
);
755 return write_short_name (st
);
757 else if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
758 < strlen (st
->file_name
))
759 return write_long_name (st
);
761 return write_short_name (st
);
765 /* Header handling. */
767 /* Make a header block for the file whose stat info is st,
768 and return its address. */
771 start_header (struct tar_stat_info
*st
)
775 header
= write_header_name (st
);
779 /* Override some stat fields, if requested to do so. */
781 if (owner_option
!= (uid_t
) -1)
782 st
->stat
.st_uid
= owner_option
;
783 if (group_option
!= (gid_t
) -1)
784 st
->stat
.st_gid
= group_option
;
787 ((st
->stat
.st_mode
& ~MODE_ALL
)
788 | mode_adjust (st
->stat
.st_mode
, S_ISDIR (st
->stat
.st_mode
) != 0,
789 initial_umask
, mode_option
, NULL
));
791 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
792 for a few tars and came up with the following interoperability
796 1 2 3 4 5 6 7 8 9 READER
797 . . . . . . . . . 1 = SunOS 4.2 tar
798 # . . # # . . # # 2 = NEC SVR4.0.2 tar
799 . . . # # . . # . 3 = Solaris 2.1 tar
800 . . . . . . . . . 4 = GNU tar 1.11.1
801 . . . . . . . . . 5 = HP-UX 8.07 tar
802 . . . . . . . . . 6 = Ultrix 4.1
803 . . . . . . . . . 7 = AIX 3.2
804 . . . . . . . . . 8 = Hitachi HI-UX 1.03
805 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
808 # = ``impossible file type''
810 The following mask for old archive removes the `#'s in column 4
811 above, thus making GNU tar both a universal donor and a universal
812 acceptor for Paul's test. */
814 if (archive_format
== V7_FORMAT
|| archive_format
== USTAR_FORMAT
)
815 MODE_TO_CHARS (st
->stat
.st_mode
& MODE_ALL
, header
->header
.mode
);
817 MODE_TO_CHARS (st
->stat
.st_mode
, header
->header
.mode
);
820 uid_t uid
= st
->stat
.st_uid
;
821 if (archive_format
== POSIX_FORMAT
822 && MAX_OCTAL_VAL (header
->header
.uid
) < uid
)
824 xheader_store ("uid", st
, NULL
);
827 if (!UID_TO_CHARS (uid
, header
->header
.uid
))
832 gid_t gid
= st
->stat
.st_gid
;
833 if (archive_format
== POSIX_FORMAT
834 && MAX_OCTAL_VAL (header
->header
.gid
) < gid
)
836 xheader_store ("gid", st
, NULL
);
839 if (!GID_TO_CHARS (gid
, header
->header
.gid
))
844 off_t size
= st
->stat
.st_size
;
845 if (archive_format
== POSIX_FORMAT
846 && MAX_OCTAL_VAL (header
->header
.size
) < size
)
848 xheader_store ("size", st
, NULL
);
851 if (!OFF_TO_CHARS (size
, header
->header
.size
))
856 struct timespec mtime
= set_mtime_option
? mtime_option
: st
->mtime
;
857 if (archive_format
== POSIX_FORMAT
)
859 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
860 || mtime
.tv_nsec
!= 0)
861 xheader_store ("mtime", st
, &mtime
);
862 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
)
865 if (!TIME_TO_CHARS (mtime
.tv_sec
, header
->header
.mtime
))
870 if (S_ISCHR (st
->stat
.st_mode
)
871 || S_ISBLK (st
->stat
.st_mode
))
873 major_t devmajor
= major (st
->stat
.st_rdev
);
874 minor_t devminor
= minor (st
->stat
.st_rdev
);
876 if (archive_format
== POSIX_FORMAT
877 && MAX_OCTAL_VAL (header
->header
.devmajor
) < devmajor
)
879 xheader_store ("devmajor", st
, NULL
);
882 if (!MAJOR_TO_CHARS (devmajor
, header
->header
.devmajor
))
885 if (archive_format
== POSIX_FORMAT
886 && MAX_OCTAL_VAL (header
->header
.devminor
) < devminor
)
888 xheader_store ("devminor", st
, NULL
);
891 if (!MINOR_TO_CHARS (devminor
, header
->header
.devminor
))
894 else if (archive_format
!= GNU_FORMAT
&& archive_format
!= OLDGNU_FORMAT
)
896 if (!(MAJOR_TO_CHARS (0, header
->header
.devmajor
)
897 && MINOR_TO_CHARS (0, header
->header
.devminor
)))
901 if (archive_format
== POSIX_FORMAT
)
903 xheader_store ("atime", st
, NULL
);
904 xheader_store ("ctime", st
, NULL
);
906 else if (incremental_option
)
907 if (archive_format
== OLDGNU_FORMAT
|| archive_format
== GNU_FORMAT
)
909 TIME_TO_CHARS (st
->atime
.tv_sec
, header
->oldgnu_header
.atime
);
910 TIME_TO_CHARS (st
->ctime
.tv_sec
, header
->oldgnu_header
.ctime
);
913 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
915 switch (archive_format
)
921 case GNU_FORMAT
: /*FIXME?*/
922 /* Overwrite header->header.magic and header.version in one blow. */
923 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
928 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
929 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
936 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
938 /* header->header.[ug]name are left as the empty string. */
942 uid_to_uname (st
->stat
.st_uid
, &st
->uname
);
943 gid_to_gname (st
->stat
.st_gid
, &st
->gname
);
945 if (archive_format
== POSIX_FORMAT
946 && (strlen (st
->uname
) > UNAME_FIELD_SIZE
947 || !string_ascii_p (st
->uname
)))
948 xheader_store ("uname", st
, NULL
);
949 UNAME_TO_CHARS (st
->uname
, header
->header
.uname
);
951 if (archive_format
== POSIX_FORMAT
952 && (strlen (st
->gname
) > GNAME_FIELD_SIZE
953 || !string_ascii_p (st
->gname
)))
954 xheader_store ("gname", st
, NULL
);
955 GNAME_TO_CHARS (st
->gname
, header
->header
.gname
);
962 simple_finish_header (union block
*header
)
968 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
972 for (i
= sizeof *header
; i
-- != 0; )
973 /* We can't use unsigned char here because of old compilers, e.g. V7. */
976 /* Fill in the checksum field. It's formatted differently from the
977 other fields: it has [6] digits, a null, then a space -- rather than
978 digits, then a null. We use to_chars.
979 The final space is already there, from
980 checksumming, and to_chars doesn't modify it.
982 This is a fast way to do:
984 sprintf(header->header.chksum, "%6o", sum); */
986 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
988 set_next_block_after (header
);
991 /* Finish off a filled-in header block and write it out. We also
992 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
993 is not negative, is the block ordinal of the first record for this
994 file, which may be a preceding long name or long link record. */
996 finish_header (struct tar_stat_info
*st
,
997 union block
*header
, off_t block_ordinal
)
999 /* Note: It is important to do this before the call to write_extended(),
1000 so that the actual ustar header is printed */
1002 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
1003 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
1004 && header
->header
.typeflag
!= XHDTYPE
1005 && header
->header
.typeflag
!= XGLTYPE
)
1007 /* FIXME: This global is used in print_header, sigh. */
1008 current_format
= archive_format
;
1009 print_header (st
, header
, block_ordinal
);
1012 header
= write_extended (false, st
, header
);
1013 simple_finish_header (header
);
1018 pad_archive (off_t size_left
)
1021 while (size_left
> 0)
1023 blk
= find_next_block ();
1024 memset (blk
->buffer
, 0, BLOCKSIZE
);
1025 set_next_block_after (blk
);
1026 size_left
-= BLOCKSIZE
;
1030 static enum dump_status
1031 dump_regular_file (int fd
, struct tar_stat_info
*st
)
1033 off_t size_left
= st
->stat
.st_size
;
1034 off_t block_ordinal
;
1037 block_ordinal
= current_block_ordinal ();
1038 blk
= start_header (st
);
1040 return dump_status_fail
;
1042 /* Mark contiguous files, if we support them. */
1043 if (archive_format
!= V7_FORMAT
&& S_ISCTG (st
->stat
.st_mode
))
1044 blk
->header
.typeflag
= CONTTYPE
;
1046 finish_header (st
, blk
, block_ordinal
);
1048 mv_begin_write (st
->file_name
, st
->stat
.st_size
, st
->stat
.st_size
);
1049 while (size_left
> 0)
1051 size_t bufsize
, count
;
1053 blk
= find_next_block ();
1055 bufsize
= available_space_after (blk
);
1057 if (size_left
< bufsize
)
1059 /* Last read -- zero out area beyond. */
1060 bufsize
= size_left
;
1061 count
= bufsize
% BLOCKSIZE
;
1063 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1066 count
= (fd
< 0) ? bufsize
: safe_read (fd
, blk
->buffer
, bufsize
);
1067 if (count
== SAFE_READ_ERROR
)
1069 read_diag_details (st
->orig_file_name
,
1070 st
->stat
.st_size
- size_left
, bufsize
);
1071 pad_archive (size_left
);
1072 return dump_status_short
;
1075 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1077 if (count
!= bufsize
)
1079 char buf
[UINTMAX_STRSIZE_BOUND
];
1080 memset (blk
->buffer
+ count
, 0, bufsize
- count
);
1081 WARNOPT (WARN_FILE_SHRANK
,
1083 ngettext ("%s: File shrank by %s byte; padding with zeros",
1084 "%s: File shrank by %s bytes; padding with zeros",
1086 quotearg_colon (st
->orig_file_name
),
1087 STRINGIFY_BIGINT (size_left
, buf
)));
1088 if (! ignore_failed_read_option
)
1089 set_exit_status (TAREXIT_DIFFERS
);
1090 pad_archive (size_left
- (bufsize
- count
));
1091 return dump_status_short
;
1094 return dump_status_ok
;
1099 dump_dir0 (char *directory
,
1100 struct tar_stat_info
*st
, bool top_level
, dev_t parent_device
)
1102 dev_t our_device
= st
->stat
.st_dev
;
1103 const char *tag_file_name
;
1104 union block
*blk
= NULL
;
1105 off_t block_ordinal
= current_block_ordinal ();
1107 st
->stat
.st_size
= 0; /* force 0 size on dir */
1109 blk
= start_header (st
);
1113 if (incremental_option
&& archive_format
!= POSIX_FORMAT
)
1114 blk
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1115 else /* if (standard_option) */
1116 blk
->header
.typeflag
= DIRTYPE
;
1118 /* If we're gnudumping, we aren't done yet so don't close it. */
1120 if (!incremental_option
)
1121 finish_header (st
, blk
, block_ordinal
);
1122 else if (gnu_list_name
->directory
)
1124 if (archive_format
== POSIX_FORMAT
)
1126 xheader_store ("GNU.dumpdir", st
,
1127 safe_directory_contents (gnu_list_name
->directory
));
1128 finish_header (st
, blk
, block_ordinal
);
1136 const char *buffer
, *p_buffer
;
1138 block_ordinal
= current_block_ordinal ();
1139 buffer
= safe_directory_contents (gnu_list_name
->directory
);
1140 totsize
= dumpdir_size (buffer
);
1141 OFF_TO_CHARS (totsize
, blk
->header
.size
);
1142 finish_header (st
, blk
, block_ordinal
);
1144 size_left
= totsize
;
1146 mv_begin_write (st
->file_name
, totsize
, totsize
);
1147 while (size_left
> 0)
1149 blk
= find_next_block ();
1150 bufsize
= available_space_after (blk
);
1151 if (size_left
< bufsize
)
1153 bufsize
= size_left
;
1154 count
= bufsize
% BLOCKSIZE
;
1156 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1158 memcpy (blk
->buffer
, p_buffer
, bufsize
);
1159 size_left
-= bufsize
;
1160 p_buffer
+= bufsize
;
1161 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1167 if (!recursion_option
)
1170 if (one_file_system_option
1172 && parent_device
!= st
->stat
.st_dev
)
1177 _("%s: file is on a different filesystem; not dumped"),
1178 quotearg_colon (st
->orig_file_name
)));
1185 switch (check_exclusion_tags (st
->orig_file_name
, &tag_file_name
))
1187 case exclusion_tag_all
:
1188 /* Handled in dump_file0 */
1191 case exclusion_tag_none
:
1197 name_buf
= xstrdup (st
->orig_file_name
);
1198 name_size
= name_len
= strlen (name_buf
);
1200 /* Now output all the files in the directory. */
1201 /* FIXME: Should speed this up by cd-ing into the dir. */
1202 for (entry
= directory
; (entry_len
= strlen (entry
)) != 0;
1203 entry
+= entry_len
+ 1)
1205 if (name_size
< name_len
+ entry_len
)
1207 name_size
= name_len
+ entry_len
;
1208 name_buf
= xrealloc (name_buf
, name_size
+ 1);
1210 strcpy (name_buf
+ name_len
, entry
);
1211 if (!excluded_name (name_buf
))
1212 dump_file (name_buf
, false, our_device
);
1219 case exclusion_tag_contents
:
1220 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1221 _("contents not dumped"));
1222 name_size
= strlen (st
->orig_file_name
) + strlen (tag_file_name
) + 1;
1223 name_buf
= xmalloc (name_size
);
1224 strcpy (name_buf
, st
->orig_file_name
);
1225 strcat (name_buf
, tag_file_name
);
1226 dump_file (name_buf
, false, our_device
);
1230 case exclusion_tag_under
:
1231 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1232 _("contents not dumped"));
1238 /* Ensure exactly one trailing slash. */
1240 ensure_slash (char **pstr
)
1242 size_t len
= strlen (*pstr
);
1243 while (len
>= 1 && ISSLASH ((*pstr
)[len
- 1]))
1245 if (!ISSLASH ((*pstr
)[len
]))
1246 *pstr
= xrealloc (*pstr
, len
+ 2);
1247 (*pstr
)[len
++] = '/';
1248 (*pstr
)[len
] = '\0';
1252 dump_dir (int fd
, struct tar_stat_info
*st
, bool top_level
,
1253 dev_t parent_device
)
1255 char *directory
= fdsavedir (fd
);
1258 savedir_diag (st
->orig_file_name
);
1262 dump_dir0 (directory
, st
, top_level
, parent_device
);
1269 /* Main functions of this module. */
1272 create_archive (void)
1274 struct name
const *p
;
1276 open_archive (ACCESS_WRITE
);
1277 buffer_write_global_xheader ();
1279 if (incremental_option
)
1281 size_t buffer_size
= 1000;
1282 char *buffer
= xmalloc (buffer_size
);
1285 collect_and_sort_names ();
1287 while ((p
= name_from_list ()) != NULL
)
1288 if (!excluded_name (p
->name
))
1289 dump_file (p
->name
, p
->cmdline
, (dev_t
) 0);
1292 while ((p
= name_from_list ()) != NULL
)
1293 if (!excluded_name (p
->name
))
1295 size_t plen
= strlen (p
->name
);
1296 if (buffer_size
<= plen
)
1298 while ((buffer_size
*= 2) <= plen
)
1300 buffer
= xrealloc (buffer
, buffer_size
);
1302 memcpy (buffer
, p
->name
, plen
);
1303 if (! ISSLASH (buffer
[plen
- 1]))
1304 buffer
[plen
++] = DIRECTORY_SEPARATOR
;
1305 q
= directory_contents (gnu_list_name
->directory
);
1309 size_t qlen
= strlen (q
);
1312 if (buffer_size
< plen
+ qlen
)
1314 while ((buffer_size
*=2 ) < plen
+ qlen
)
1316 buffer
= xrealloc (buffer
, buffer_size
);
1318 strcpy (buffer
+ plen
, q
+ 1);
1319 dump_file (buffer
, false, (dev_t
) 0);
1329 while ((name
= name_next (1)) != NULL
)
1330 if (!excluded_name (name
))
1331 dump_file (name
, true, (dev_t
) 0);
1336 finish_deferred_unlinks ();
1337 if (listed_incremental_option
)
1338 write_directory_file ();
1342 /* Calculate the hash of a link. */
1344 hash_link (void const *entry
, size_t n_buckets
)
1346 struct link
const *l
= entry
;
1347 uintmax_t num
= l
->dev
^ l
->ino
;
1348 return num
% n_buckets
;
1351 /* Compare two links for equality. */
1353 compare_links (void const *entry1
, void const *entry2
)
1355 struct link
const *link1
= entry1
;
1356 struct link
const *link2
= entry2
;
1357 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
1361 unknown_file_error (char const *p
)
1363 WARNOPT (WARN_FILE_IGNORED
,
1364 (0, 0, _("%s: Unknown file type; file ignored"),
1365 quotearg_colon (p
)));
1366 if (!ignore_failed_read_option
)
1367 set_exit_status (TAREXIT_FAILURE
);
1371 /* Handling of hard links */
1373 /* Table of all non-directories that we've written so far. Any time
1374 we see another, we check the table and avoid dumping the data
1375 again if we've done it once already. */
1376 static Hash_table
*link_table
;
1378 /* Try to dump stat as a hard link to another file in the archive.
1379 Return true if successful. */
1381 dump_hard_link (struct tar_stat_info
*st
)
1383 if (link_table
&& (st
->stat
.st_nlink
> 1 || remove_files_option
))
1386 struct link
*duplicate
;
1387 off_t block_ordinal
;
1390 lp
.ino
= st
->stat
.st_ino
;
1391 lp
.dev
= st
->stat
.st_dev
;
1393 if ((duplicate
= hash_lookup (link_table
, &lp
)))
1395 /* We found a link. */
1396 char const *link_name
= safer_name_suffix (duplicate
->name
, true,
1397 absolute_names_option
);
1401 block_ordinal
= current_block_ordinal ();
1402 assign_string (&st
->link_name
, link_name
);
1403 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
1404 < strlen (link_name
))
1405 write_long_link (st
);
1407 st
->stat
.st_size
= 0;
1408 blk
= start_header (st
);
1411 tar_copy_str (blk
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1413 blk
->header
.typeflag
= LNKTYPE
;
1414 finish_header (st
, blk
, block_ordinal
);
1416 if (remove_files_option
)
1417 queue_deferred_unlink (st
->orig_file_name
, false);
1426 file_count_links (struct tar_stat_info
*st
)
1428 if (hard_dereference_option
)
1430 if (st
->stat
.st_nlink
> 1)
1432 struct link
*duplicate
;
1433 char *linkname
= NULL
;
1436 assign_string (&linkname
, st
->orig_file_name
);
1437 transform_name (&linkname
, XFORM_LINK
);
1439 lp
= xmalloc (offsetof (struct link
, name
)
1440 + strlen (linkname
) + 1);
1441 lp
->ino
= st
->stat
.st_ino
;
1442 lp
->dev
= st
->stat
.st_dev
;
1443 lp
->nlink
= st
->stat
.st_nlink
;
1444 strcpy (lp
->name
, linkname
);
1448 || (link_table
= hash_initialize (0, 0, hash_link
,
1450 && (duplicate
= hash_insert (link_table
, lp
))))
1453 if (duplicate
!= lp
)
1459 /* For each dumped file, check if all its links were dumped. Emit
1460 warnings if it is not so. */
1469 for (lp
= hash_get_first (link_table
); lp
;
1470 lp
= hash_get_next (link_table
, lp
))
1474 WARN ((0, 0, _("Missing links to %s."), quote (lp
->name
)));
1479 /* Dump a single file, recursing on directories. P is the file name
1480 to dump. TOP_LEVEL tells whether this is a top-level call; zero
1481 means no, positive means yes, and negative means the top level
1482 of an incremental dump. PARENT_DEVICE is the device of P's
1483 parent directory; it is examined only if TOP_LEVEL is zero. */
1485 /* FIXME: One should make sure that for *every* path leading to setting
1486 exit_status to failure, a clear diagnostic has been issued. */
1489 dump_file0 (struct tar_stat_info
*st
, const char *p
,
1490 bool top_level
, dev_t parent_device
)
1492 union block
*header
;
1494 off_t original_size
;
1495 struct timespec original_ctime
;
1496 struct timespec restore_times
[2];
1497 off_t block_ordinal
= -1;
1500 if (interactive_option
&& !confirm ("add", p
))
1503 assign_string (&st
->orig_file_name
, p
);
1504 assign_string (&st
->file_name
,
1505 safer_name_suffix (p
, false, absolute_names_option
));
1507 transform_name (&st
->file_name
, XFORM_REGFILE
);
1509 if (deref_stat (dereference_option
, p
, &st
->stat
) != 0)
1511 file_removed_diag (p
, top_level
, stat_diag
);
1514 st
->archive_file_size
= original_size
= st
->stat
.st_size
;
1515 st
->atime
= restore_times
[0] = get_stat_atime (&st
->stat
);
1516 st
->mtime
= restore_times
[1] = get_stat_mtime (&st
->stat
);
1517 st
->ctime
= original_ctime
= get_stat_ctime (&st
->stat
);
1520 if (S_ISHIDDEN (st
->stat
.st_mode
))
1522 char *new = (char *) alloca (strlen (p
) + 2);
1532 /* See if we want only new files, and check if this one is too old to
1535 This check is omitted if incremental_option is set *and* the
1536 requested file is not explicitely listed in the command line. */
1538 if (!(incremental_option
&& !is_individual_file (p
))
1539 && !S_ISDIR (st
->stat
.st_mode
)
1540 && OLDER_TAR_STAT_TIME (*st
, m
)
1541 && (!after_date_option
|| OLDER_TAR_STAT_TIME (*st
, c
)))
1543 if (!incremental_option
&& verbose_option
)
1544 WARNOPT (WARN_FILE_UNCHANGED
,
1545 (0, 0, _("%s: file is unchanged; not dumped"),
1546 quotearg_colon (p
)));
1550 /* See if we are trying to dump the archive. */
1551 if (sys_file_is_archive (st
))
1553 WARNOPT (WARN_IGNORE_ARCHIVE
,
1554 (0, 0, _("%s: file is the archive; not dumped"),
1555 quotearg_colon (p
)));
1559 is_dir
= S_ISDIR (st
->stat
.st_mode
) != 0;
1561 if (!is_dir
&& dump_hard_link (st
))
1564 if (is_dir
|| S_ISREG (st
->stat
.st_mode
) || S_ISCTG (st
->stat
.st_mode
))
1568 struct stat final_stat
;
1570 if (is_dir
|| file_dumpable_p (st
))
1573 (O_RDONLY
| O_BINARY
1574 | (is_dir
? O_DIRECTORY
| O_NONBLOCK
: 0)
1575 | (atime_preserve_option
== system_atime_preserve
1580 file_removed_diag (p
, top_level
, open_diag
);
1587 const char *tag_file_name
;
1588 ensure_slash (&st
->orig_file_name
);
1589 ensure_slash (&st
->file_name
);
1591 if (check_exclusion_tags (st
->orig_file_name
, &tag_file_name
)
1592 == exclusion_tag_all
)
1594 exclusion_tag_warning (st
->orig_file_name
, tag_file_name
,
1595 _("directory not dumped"));
1601 ok
= dump_dir (fd
, st
, top_level
, parent_device
);
1603 /* dump_dir consumes FD if successful. */
1609 enum dump_status status
;
1611 if (fd
!= -1 && sparse_option
&& ST_IS_SPARSE (st
->stat
))
1613 status
= sparse_dump_file (fd
, st
);
1614 if (status
== dump_status_not_implemented
)
1615 status
= dump_regular_file (fd
, st
);
1618 status
= dump_regular_file (fd
, st
);
1622 case dump_status_ok
:
1623 case dump_status_short
:
1624 file_count_links (st
);
1627 case dump_status_fail
:
1630 case dump_status_not_implemented
:
1634 ok
= status
== dump_status_ok
;
1639 /* If possible, reopen a directory if we are preserving
1640 atimes, so that we can set just the atime on systems with
1642 if (fd
< 0 && is_dir
1643 && atime_preserve_option
== replace_atime_preserve
)
1644 fd
= open (p
, O_RDONLY
| O_BINARY
| O_DIRECTORY
| O_NONBLOCK
);
1647 ? deref_stat (dereference_option
, p
, &final_stat
)
1648 : fstat (fd
, &final_stat
))
1651 file_removed_diag (p
, top_level
, stat_diag
);
1658 if ((timespec_cmp (get_stat_ctime (&final_stat
), original_ctime
) != 0
1659 /* Original ctime will change if the file is a directory and
1660 --remove-files is given */
1661 && !(remove_files_option
&& is_dir
))
1662 || original_size
< final_stat
.st_size
)
1664 WARNOPT (WARN_FILE_CHANGED
,
1665 (0, 0, _("%s: file changed as we read it"),
1666 quotearg_colon (p
)));
1667 set_exit_status (TAREXIT_DIFFERS
);
1669 else if (atime_preserve_option
== replace_atime_preserve
1670 && set_file_atime (fd
, p
, restore_times
) != 0)
1674 if (0 <= fd
&& close (fd
) != 0)
1680 if (ok
&& remove_files_option
)
1681 queue_deferred_unlink (p
, is_dir
);
1685 #ifdef HAVE_READLINK
1686 else if (S_ISLNK (st
->stat
.st_mode
))
1690 size_t linklen
= st
->stat
.st_size
;
1691 if (linklen
!= st
->stat
.st_size
|| linklen
+ 1 == 0)
1693 buffer
= (char *) alloca (linklen
+ 1);
1694 size
= readlink (p
, buffer
, linklen
+ 1);
1697 file_removed_diag (p
, top_level
, readlink_diag
);
1700 buffer
[size
] = '\0';
1701 assign_string (&st
->link_name
, buffer
);
1702 transform_name (&st
->link_name
, XFORM_SYMLINK
);
1703 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
) < size
)
1704 write_long_link (st
);
1706 block_ordinal
= current_block_ordinal ();
1707 st
->stat
.st_size
= 0; /* force 0 size on symlink */
1708 header
= start_header (st
);
1711 tar_copy_str (header
->header
.linkname
, st
->link_name
, NAME_FIELD_SIZE
);
1712 header
->header
.typeflag
= SYMTYPE
;
1713 finish_header (st
, header
, block_ordinal
);
1714 /* nothing more to do to it */
1716 if (remove_files_option
)
1717 queue_deferred_unlink (p
, false);
1719 file_count_links (st
);
1723 else if (S_ISCHR (st
->stat
.st_mode
))
1725 else if (S_ISBLK (st
->stat
.st_mode
))
1727 else if (S_ISFIFO (st
->stat
.st_mode
))
1729 else if (S_ISSOCK (st
->stat
.st_mode
))
1731 WARNOPT (WARN_FILE_IGNORED
,
1732 (0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1735 else if (S_ISDOOR (st
->stat
.st_mode
))
1737 WARNOPT (WARN_FILE_IGNORED
,
1738 (0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1743 unknown_file_error (p
);
1747 if (archive_format
== V7_FORMAT
)
1749 unknown_file_error (p
);
1753 block_ordinal
= current_block_ordinal ();
1754 st
->stat
.st_size
= 0; /* force 0 size */
1755 header
= start_header (st
);
1758 header
->header
.typeflag
= type
;
1760 if (type
!= FIFOTYPE
)
1762 MAJOR_TO_CHARS (major (st
->stat
.st_rdev
),
1763 header
->header
.devmajor
);
1764 MINOR_TO_CHARS (minor (st
->stat
.st_rdev
),
1765 header
->header
.devminor
);
1768 finish_header (st
, header
, block_ordinal
);
1769 if (remove_files_option
)
1770 queue_deferred_unlink (p
, false);
1774 dump_file (const char *p
, bool top_level
, dev_t parent_device
)
1776 struct tar_stat_info st
;
1777 tar_stat_init (&st
);
1778 dump_file0 (&st
, p
, top_level
, parent_device
);
1779 if (listed_incremental_option
)
1780 update_parent_directory (p
);
1781 tar_stat_destroy (&st
);