1 /* Create a tar archive.
3 Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005 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 2, 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. */
38 /* The maximum uintmax_t value that can be represented with DIGITS digits,
39 assuming that each digit is BITS_PER_DIGIT wide. */
40 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
41 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
42 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
45 /* The maximum uintmax_t value that can be represented with octal
46 digits and a trailing NUL in BUFFER. */
47 #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
49 /* Convert VALUE to an octal representation suitable for tar headers.
50 Output to buffer WHERE with size SIZE.
51 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
54 to_octal (uintmax_t value
, char *where
, size_t size
)
61 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
67 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
68 NUL unless SRC is LEN or more bytes long. */
71 tar_copy_str (char *dst
, const char *src
, size_t len
)
74 for (i
= 0; i
< len
; i
++)
75 if (! (dst
[i
] = src
[i
]))
79 /* Same as tar_copy_str, but always terminate with NUL if using
83 tar_name_copy_str (char *dst
, const char *src
, size_t len
)
85 tar_copy_str (dst
, src
, len
);
86 if (archive_format
== OLDGNU_FORMAT
)
90 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
91 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
92 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
93 The result is undefined if SIZE is 0 or if VALUE is too large to
97 to_base256 (int negative
, uintmax_t value
, char *where
, size_t size
)
100 uintmax_t propagated_sign_bits
=
101 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
106 where
[--i
] = v
& ((1 << LG_256
) - 1);
107 v
= propagated_sign_bits
| (v
>> LG_256
);
112 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
113 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
114 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
115 negative before being cast to uintmax_t; its original bitpattern
116 can be deduced from VALSIZE, its original size before casting.
117 TYPE is the kind of value being output (useful for diagnostics).
118 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
119 digits), followed by '\0'. If this won't work, and if GNU or
120 OLDGNU format is allowed, use '\200' followed by base-256, or (if
121 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
122 If neither format works, use SUBSTITUTE (...) instead. Pass to
123 SUBSTITUTE the address of an 0-or-1 flag recording whether the
124 substitute value is negative. */
127 to_chars (int negative
, uintmax_t value
, size_t valsize
,
128 uintmax_t (*substitute
) (int *),
129 char *where
, size_t size
, const char *type
)
131 int base256_allowed
= (archive_format
== GNU_FORMAT
132 || archive_format
== OLDGNU_FORMAT
);
134 /* Generate the POSIX octal representation if the number fits. */
135 if (! negative
&& value
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
137 where
[size
- 1] = '\0';
138 to_octal (value
, where
, size
- 1);
141 /* Otherwise, generate the base-256 representation if we are
142 generating an old or new GNU format and if the number fits. */
143 else if (((negative
? -1 - value
: value
)
144 <= MAX_VAL_WITH_DIGITS (size
- 1, LG_256
))
147 where
[0] = negative
? -1 : 1 << (LG_256
- 1);
148 to_base256 (negative
, value
, where
+ 1, size
- 1);
151 /* Otherwise, if the number is negative, and if it would not cause
152 ambiguity on this host by confusing positive with negative
153 values, then generate the POSIX octal representation of the value
154 modulo 2**(field bits). The resulting tar file is
155 machine-dependent, since it depends on the host word size. Yuck!
156 But this is the traditional behavior. */
157 else if (negative
&& valsize
* CHAR_BIT
<= (size
- 1) * LG_8
)
159 static int warned_once
;
163 WARN ((0, 0, _("Generating negative octal headers")));
165 where
[size
- 1] = '\0';
166 to_octal (value
& MAX_VAL_WITH_DIGITS (valsize
* CHAR_BIT
, 1),
170 /* Otherwise, output a substitute value if possible (with a
171 warning), and an error message if not. */
174 uintmax_t maxval
= (base256_allowed
175 ? MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)
176 : MAX_VAL_WITH_DIGITS (size
- 1, LG_8
));
177 char valbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
178 char maxbuf
[UINTMAX_STRSIZE_BOUND
];
179 char minbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
180 char const *minval_string
;
181 char const *maxval_string
= STRINGIFY_BIGINT (maxval
, maxbuf
);
182 char const *value_string
;
186 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
187 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
196 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
201 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
206 uintmax_t sub
= substitute (&negsub
) & maxval
;
207 /* FIXME: This is the only place where GNU_FORMAT differs from
208 OLDGNU_FORMAT. Apart from this they are completely identical. */
209 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? - sub
: sub
;
210 char subbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
211 char *sub_string
= STRINGIFY_BIGINT (s
, subbuf
+ 1);
214 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
215 value_string
, type
, minval_string
, maxval_string
,
217 to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
220 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
221 value_string
, type
, minval_string
, maxval_string
));
226 gid_substitute (int *negative
)
232 static gid_t gid_nobody
;
233 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
242 gid_to_chars (gid_t v
, char *p
, size_t s
)
244 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
248 major_to_chars (major_t v
, char *p
, size_t s
)
250 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
254 minor_to_chars (minor_t v
, char *p
, size_t s
)
256 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
260 mode_to_chars (mode_t v
, char *p
, size_t s
)
262 /* In the common case where the internal and external mode bits are the same,
263 and we are not using POSIX or GNU format,
264 propagate all unknown bits to the external mode.
265 This matches historical practice.
266 Otherwise, just copy the bits we know about. */
269 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
270 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
271 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
272 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
273 && archive_format
!= POSIX_FORMAT
274 && archive_format
!= USTAR_FORMAT
275 && archive_format
!= GNU_FORMAT
)
283 u
= ((v
& S_ISUID
? TSUID
: 0)
284 | (v
& S_ISGID
? TSGID
: 0)
285 | (v
& S_ISVTX
? TSVTX
: 0)
286 | (v
& S_IRUSR
? TUREAD
: 0)
287 | (v
& S_IWUSR
? TUWRITE
: 0)
288 | (v
& S_IXUSR
? TUEXEC
: 0)
289 | (v
& S_IRGRP
? TGREAD
: 0)
290 | (v
& S_IWGRP
? TGWRITE
: 0)
291 | (v
& S_IXGRP
? TGEXEC
: 0)
292 | (v
& S_IROTH
? TOREAD
: 0)
293 | (v
& S_IWOTH
? TOWRITE
: 0)
294 | (v
& S_IXOTH
? TOEXEC
: 0));
296 to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
300 off_to_chars (off_t v
, char *p
, size_t s
)
302 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
306 size_to_chars (size_t v
, char *p
, size_t s
)
308 to_chars (0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "size_t");
312 time_to_chars (time_t v
, char *p
, size_t s
)
314 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
318 uid_substitute (int *negative
)
324 static uid_t uid_nobody
;
325 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
334 uid_to_chars (uid_t v
, char *p
, size_t s
)
336 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
340 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
342 to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
346 string_to_chars (char const *str
, char *p
, size_t s
)
348 tar_copy_str (p
, str
, s
);
353 /* A file is considered dumpable if it is sparse and both --sparse and --totals
355 Otherwise, it is dumpable unless any of the following conditions occur:
357 a) it is empty *and* world-readable, or
358 b) current archive is /dev/null */
361 file_dumpable_p (struct tar_stat_info
*st
)
364 return totals_option
&& sparse_option
&& sparse_file_p (st
);
365 return !(st
->archive_file_size
== 0
366 && (st
->stat
.st_mode
& MODE_R
) == MODE_R
);
370 /* Writing routines. */
372 /* Write the EOT block(s). Zero at least two blocks, through the end
373 of the record. Old tar, as previous versions of GNU tar, writes
374 garbage after two zeroed blocks. */
378 union block
*pointer
= find_next_block ();
379 memset (pointer
->buffer
, 0, BLOCKSIZE
);
380 set_next_block_after (pointer
);
381 pointer
= find_next_block ();
382 memset (pointer
->buffer
, 0, available_space_after (pointer
));
383 set_next_block_after (pointer
);
386 /* Write a "private" header */
388 start_private_header (const char *name
, size_t size
)
391 union block
*header
= find_next_block ();
393 memset (header
->buffer
, 0, sizeof (union block
));
395 tar_name_copy_str (header
->header
.name
, name
, NAME_FIELD_SIZE
);
396 OFF_TO_CHARS (size
, header
->header
.size
);
399 TIME_TO_CHARS (t
, header
->header
.mtime
);
400 MODE_TO_CHARS (S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, header
->header
.mode
);
401 UID_TO_CHARS (getuid (), header
->header
.uid
);
402 GID_TO_CHARS (getgid (), header
->header
.gid
);
403 MAJOR_TO_CHARS (0, header
->header
.devmajor
);
404 MINOR_TO_CHARS (0, header
->header
.devminor
);
405 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
406 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
410 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
414 write_short_name (struct tar_stat_info
*st
)
416 union block
*header
= find_next_block ();
417 memset (header
->buffer
, 0, sizeof (union block
));
418 tar_name_copy_str (header
->header
.name
, st
->file_name
, NAME_FIELD_SIZE
);
422 #define FILL(field,byte) do { \
423 memset(field, byte, sizeof(field)-1); \
424 (field)[sizeof(field)-1] = 0; \
427 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
429 write_gnu_long_link (struct tar_stat_info
*st
, const char *p
, char type
)
431 size_t size
= strlen (p
) + 1;
436 header
= start_private_header ("././@LongLink", size
);
437 FILL(header
->header
.mtime
, '0');
438 FILL(header
->header
.mode
, '0');
439 FILL(header
->header
.uid
, '0');
440 FILL(header
->header
.gid
, '0');
441 FILL(header
->header
.devmajor
, 0);
442 FILL(header
->header
.devminor
, 0);
443 uid_to_uname (0, &tmpname
);
444 UNAME_TO_CHARS (tmpname
, header
->header
.uname
);
446 gid_to_gname (0, &tmpname
);
447 GNAME_TO_CHARS (tmpname
, header
->header
.gname
);
450 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
451 header
->header
.typeflag
= type
;
452 finish_header (st
, header
, -1);
454 header
= find_next_block ();
456 bufsize
= available_space_after (header
);
458 while (bufsize
< size
)
460 memcpy (header
->buffer
, p
, bufsize
);
463 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
464 header
= find_next_block ();
465 bufsize
= available_space_after (header
);
467 memcpy (header
->buffer
, p
, size
);
468 memset (header
->buffer
+ size
, 0, bufsize
- size
);
469 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
473 split_long_name (const char *name
, size_t length
)
477 if (length
> PREFIX_FIELD_SIZE
)
478 length
= PREFIX_FIELD_SIZE
+2;
479 for (i
= length
- 1; i
> 0; i
--)
480 if (ISSLASH (name
[i
]))
486 write_ustar_long_name (const char *name
)
488 size_t length
= strlen (name
);
492 if (length
> PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1)
494 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
495 quotearg_colon (name
),
496 PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1));
500 i
= split_long_name (name
, length
);
501 if (i
== 0 || length
- i
- 1 > NAME_FIELD_SIZE
)
504 _("%s: file name is too long (cannot be split); not dumped"),
505 quotearg_colon (name
)));
509 header
= find_next_block ();
510 memset (header
->buffer
, 0, sizeof (header
->buffer
));
511 memcpy (header
->header
.prefix
, name
, i
);
512 memcpy (header
->header
.name
, name
+ i
+ 1, length
- i
- 1);
517 /* Write a long link name, depending on the current archive format */
519 write_long_link (struct tar_stat_info
*st
)
521 switch (archive_format
)
524 xheader_store ("linkpath", st
, NULL
);
527 case V7_FORMAT
: /* old V7 tar format */
531 _("%s: link name is too long; not dumped"),
532 quotearg_colon (st
->link_name
)));
537 write_gnu_long_link (st
, st
->link_name
, GNUTYPE_LONGLINK
);
546 write_long_name (struct tar_stat_info
*st
)
548 switch (archive_format
)
551 xheader_store ("path", st
, NULL
);
555 if (strlen (st
->file_name
) > NAME_FIELD_SIZE
-1)
557 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
558 quotearg_colon (st
->file_name
),
559 NAME_FIELD_SIZE
- 1));
566 return write_ustar_long_name (st
->file_name
);
570 write_gnu_long_link (st
, st
->file_name
, GNUTYPE_LONGNAME
);
576 return write_short_name (st
);
580 write_extended (bool global
, struct tar_stat_info
*st
, union block
*old_header
)
582 union block
*header
, hp
;
586 if (extended_header
.buffer
|| extended_header
.stk
== NULL
)
589 xheader_finish (&extended_header
);
590 memcpy (hp
.buffer
, old_header
, sizeof (hp
));
594 p
= xheader_ghdr_name ();
599 p
= xheader_xhdr_name (st
);
601 xheader_write (type
, p
, &extended_header
);
603 header
= find_next_block ();
604 memcpy (header
, &hp
.buffer
, sizeof (hp
.buffer
));
609 write_header_name (struct tar_stat_info
*st
)
611 if (archive_format
== POSIX_FORMAT
&& !string_ascii_p (st
->file_name
))
613 xheader_store ("path", st
, NULL
);
614 return write_short_name (st
);
616 else if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
617 < strlen (st
->file_name
))
618 return write_long_name (st
);
620 return write_short_name (st
);
624 /* Header handling. */
626 /* Make a header block for the file whose stat info is st,
627 and return its address. */
630 start_header (struct tar_stat_info
*st
)
634 header
= write_header_name (st
);
638 /* Override some stat fields, if requested to do so. */
640 if (owner_option
!= (uid_t
) -1)
641 st
->stat
.st_uid
= owner_option
;
642 if (group_option
!= (gid_t
) -1)
643 st
->stat
.st_gid
= group_option
;
646 ((st
->stat
.st_mode
& ~MODE_ALL
)
647 | mode_adjust (st
->stat
.st_mode
, mode_option
, initial_umask
));
649 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
650 for a few tars and came up with the following interoperability
654 1 2 3 4 5 6 7 8 9 READER
655 . . . . . . . . . 1 = SunOS 4.2 tar
656 # . . # # . . # # 2 = NEC SVR4.0.2 tar
657 . . . # # . . # . 3 = Solaris 2.1 tar
658 . . . . . . . . . 4 = GNU tar 1.11.1
659 . . . . . . . . . 5 = HP-UX 8.07 tar
660 . . . . . . . . . 6 = Ultrix 4.1
661 . . . . . . . . . 7 = AIX 3.2
662 . . . . . . . . . 8 = Hitachi HI-UX 1.03
663 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
666 # = ``impossible file type''
668 The following mask for old archive removes the `#'s in column 4
669 above, thus making GNU tar both a universal donor and a universal
670 acceptor for Paul's test. */
672 if (archive_format
== V7_FORMAT
|| archive_format
== USTAR_FORMAT
)
673 MODE_TO_CHARS (st
->stat
.st_mode
& MODE_ALL
, header
->header
.mode
);
675 MODE_TO_CHARS (st
->stat
.st_mode
, header
->header
.mode
);
678 uid_t uid
= st
->stat
.st_uid
;
679 if (archive_format
== POSIX_FORMAT
680 && MAX_OCTAL_VAL (header
->header
.uid
) < uid
)
682 xheader_store ("uid", st
, NULL
);
685 UID_TO_CHARS (uid
, header
->header
.uid
);
689 gid_t gid
= st
->stat
.st_gid
;
690 if (archive_format
== POSIX_FORMAT
691 && MAX_OCTAL_VAL (header
->header
.gid
) < gid
)
693 xheader_store ("gid", st
, NULL
);
696 GID_TO_CHARS (gid
, header
->header
.gid
);
700 off_t size
= st
->stat
.st_size
;
701 if (archive_format
== POSIX_FORMAT
702 && MAX_OCTAL_VAL (header
->header
.size
) < size
)
704 xheader_store ("size", st
, NULL
);
707 OFF_TO_CHARS (size
, header
->header
.size
);
711 struct timespec mtime
= st
->mtime
;
712 if (archive_format
== POSIX_FORMAT
)
714 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
715 || mtime
.tv_nsec
!= 0)
716 xheader_store ("mtime", st
, NULL
);
717 if (MAX_OCTAL_VAL (header
->header
.mtime
) < mtime
.tv_sec
)
720 TIME_TO_CHARS (mtime
.tv_sec
, header
->header
.mtime
);
724 if (S_ISCHR (st
->stat
.st_mode
)
725 || S_ISBLK (st
->stat
.st_mode
))
727 major_t devmajor
= major (st
->stat
.st_rdev
);
728 minor_t devminor
= minor (st
->stat
.st_rdev
);
730 if (archive_format
== POSIX_FORMAT
731 && MAX_OCTAL_VAL (header
->header
.devmajor
) < devmajor
)
733 xheader_store ("devmajor", st
, NULL
);
736 MAJOR_TO_CHARS (devmajor
, header
->header
.devmajor
);
738 if (archive_format
== POSIX_FORMAT
739 && MAX_OCTAL_VAL (header
->header
.devminor
) < devminor
)
741 xheader_store ("devminor", st
, NULL
);
744 MINOR_TO_CHARS (devminor
, header
->header
.devminor
);
746 else if (archive_format
!= GNU_FORMAT
&& archive_format
!= OLDGNU_FORMAT
)
748 MAJOR_TO_CHARS (0, header
->header
.devmajor
);
749 MINOR_TO_CHARS (0, header
->header
.devminor
);
752 if (archive_format
== POSIX_FORMAT
)
754 xheader_store ("atime", st
, NULL
);
755 xheader_store ("ctime", st
, NULL
);
757 else if (incremental_option
)
758 if (archive_format
== OLDGNU_FORMAT
|| archive_format
== GNU_FORMAT
)
760 TIME_TO_CHARS (st
->atime
.tv_sec
, header
->oldgnu_header
.atime
);
761 TIME_TO_CHARS (st
->ctime
.tv_sec
, header
->oldgnu_header
.ctime
);
764 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
766 switch (archive_format
)
772 case GNU_FORMAT
: /*FIXME?*/
773 /* Overwrite header->header.magic and header.version in one blow. */
774 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
779 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
780 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
787 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
789 /* header->header.[ug]name are left as the empty string. */
793 uid_to_uname (st
->stat
.st_uid
, &st
->uname
);
794 gid_to_gname (st
->stat
.st_gid
, &st
->gname
);
796 if (archive_format
== POSIX_FORMAT
797 && (strlen (st
->uname
) > UNAME_FIELD_SIZE
798 || !string_ascii_p (st
->uname
)))
799 xheader_store ("uname", st
, NULL
);
800 UNAME_TO_CHARS (st
->uname
, header
->header
.uname
);
802 if (archive_format
== POSIX_FORMAT
803 && (strlen (st
->gname
) > GNAME_FIELD_SIZE
804 || !string_ascii_p (st
->gname
)))
805 xheader_store ("gname", st
, NULL
);
806 GNAME_TO_CHARS (st
->gname
, header
->header
.gname
);
813 simple_finish_header (union block
*header
)
819 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
823 for (i
= sizeof *header
; i
-- != 0; )
824 /* We can't use unsigned char here because of old compilers, e.g. V7. */
827 /* Fill in the checksum field. It's formatted differently from the
828 other fields: it has [6] digits, a null, then a space -- rather than
829 digits, then a null. We use to_chars.
830 The final space is already there, from
831 checksumming, and to_chars doesn't modify it.
833 This is a fast way to do:
835 sprintf(header->header.chksum, "%6o", sum); */
837 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
839 set_next_block_after (header
);
842 /* Finish off a filled-in header block and write it out. We also
843 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
844 is not negative, is the block ordinal of the first record for this
845 file, which may be a preceding long name or long link record. */
847 finish_header (struct tar_stat_info
*st
,
848 union block
*header
, off_t block_ordinal
)
850 /* Note: It is important to do this before the call to write_extended(),
851 so that the actual ustar header is printed */
853 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
854 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
855 && header
->header
.typeflag
!= XHDTYPE
856 && header
->header
.typeflag
!= XGLTYPE
)
858 /* These globals are parameters to print_header, sigh. */
860 current_header
= header
;
861 current_format
= archive_format
;
862 print_header (st
, block_ordinal
);
865 header
= write_extended (false, st
, header
);
866 simple_finish_header (header
);
871 pad_archive (off_t size_left
)
874 while (size_left
> 0)
876 mv_size_left (size_left
);
877 blk
= find_next_block ();
878 memset (blk
->buffer
, 0, BLOCKSIZE
);
879 set_next_block_after (blk
);
880 size_left
-= BLOCKSIZE
;
884 static enum dump_status
885 dump_regular_file (int fd
, struct tar_stat_info
*st
)
887 off_t size_left
= st
->stat
.st_size
;
891 block_ordinal
= current_block_ordinal ();
892 blk
= start_header (st
);
894 return dump_status_fail
;
896 /* Mark contiguous files, if we support them. */
897 if (archive_format
!= V7_FORMAT
&& S_ISCTG (st
->stat
.st_mode
))
898 blk
->header
.typeflag
= CONTTYPE
;
900 finish_header (st
, blk
, block_ordinal
);
903 while (size_left
> 0)
905 size_t bufsize
, count
;
907 mv_size_left (size_left
);
909 blk
= find_next_block ();
911 bufsize
= available_space_after (blk
);
913 if (size_left
< bufsize
)
915 /* Last read -- zero out area beyond. */
917 count
= bufsize
% BLOCKSIZE
;
919 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
922 count
= (fd
< 0) ? bufsize
: safe_read (fd
, blk
->buffer
, bufsize
);
923 if (count
== SAFE_READ_ERROR
)
925 read_diag_details (st
->orig_file_name
,
926 st
->stat
.st_size
- size_left
, bufsize
);
927 pad_archive (size_left
);
928 return dump_status_short
;
932 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
934 if (count
!= bufsize
)
936 char buf
[UINTMAX_STRSIZE_BOUND
];
937 memset (blk
->buffer
+ count
, 0, bufsize
- count
);
939 ngettext ("%s: File shrank by %s byte; padding with zeros",
940 "%s: File shrank by %s bytes; padding with zeros",
942 quotearg_colon (st
->orig_file_name
),
943 STRINGIFY_BIGINT (size_left
, buf
)));
944 if (! ignore_failed_read_option
)
945 exit_status
= TAREXIT_FAILURE
;
946 pad_archive (size_left
- (bufsize
-count
));
947 return dump_status_short
;
950 return dump_status_ok
;
954 dump_regular_finish (int fd
, struct tar_stat_info
*st
,
955 struct timespec original_ctime
)
959 struct stat final_stat
;
960 if (fstat (fd
, &final_stat
) != 0)
962 stat_diag (st
->orig_file_name
);
964 else if (timespec_cmp (get_stat_ctime (&final_stat
), original_ctime
)
967 WARN ((0, 0, _("%s: file changed as we read it"),
968 quotearg_colon (st
->orig_file_name
)));
972 close_diag (st
->orig_file_name
);
975 if (remove_files_option
)
977 if (unlink (st
->orig_file_name
) == -1)
978 unlink_error (st
->orig_file_name
);
982 /* Look in directory DIRNAME for a cache directory tag file
983 with the magic name "CACHEDIR.TAG" and a standard header,
985 http://www.brynosaurus.com/cachedir
986 Applications can write this file into directories they create
987 for use as caches containing purely regenerable, non-precious data,
988 allowing us to avoid archiving them if --exclude-caches is specified. */
990 #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
991 #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
994 check_cache_directory (char *dirname
)
996 static char tagname
[] = "CACHEDIR.TAG";
999 int tag_present
= false;
1001 tagpath
= xmalloc (strlen (dirname
) + strlen (tagname
) + 1);
1002 strcpy (tagpath
, dirname
);
1003 strcat (tagpath
, tagname
);
1005 fd
= open (tagpath
, O_RDONLY
);
1008 static char tagbuf
[CACHEDIR_SIGNATURE_SIZE
];
1010 if (read (fd
, tagbuf
, CACHEDIR_SIGNATURE_SIZE
)
1011 == CACHEDIR_SIGNATURE_SIZE
1012 && memcmp (tagbuf
, CACHEDIR_SIGNATURE
, CACHEDIR_SIGNATURE_SIZE
) == 0)
1024 dump_dir0 (char *directory
,
1025 struct tar_stat_info
*st
, int top_level
, dev_t parent_device
)
1027 dev_t our_device
= st
->stat
.st_dev
;
1029 if (!is_avoided_name (st
->orig_file_name
))
1031 union block
*blk
= NULL
;
1032 off_t block_ordinal
= current_block_ordinal ();
1033 st
->stat
.st_size
= 0; /* force 0 size on dir */
1035 blk
= start_header (st
);
1039 if (incremental_option
&& archive_format
!= POSIX_FORMAT
)
1040 blk
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1041 else /* if (standard_option) */
1042 blk
->header
.typeflag
= DIRTYPE
;
1044 /* If we're gnudumping, we aren't done yet so don't close it. */
1046 if (!incremental_option
)
1047 finish_header (st
, blk
, block_ordinal
);
1048 else if (gnu_list_name
->dir_contents
)
1050 if (archive_format
== POSIX_FORMAT
)
1052 xheader_store ("GNU.dumpdir", st
, gnu_list_name
->dir_contents
);
1053 finish_header (st
, blk
, block_ordinal
);
1061 const char *buffer
, *p_buffer
;
1063 block_ordinal
= current_block_ordinal ();
1064 buffer
= gnu_list_name
->dir_contents
;
1066 totsize
= dumpdir_size (buffer
);
1069 OFF_TO_CHARS (totsize
, blk
->header
.size
);
1070 finish_header (st
, blk
, block_ordinal
);
1072 size_left
= totsize
;
1075 mv_total_size (totsize
);
1076 while (size_left
> 0)
1078 mv_size_left (size_left
);
1079 blk
= find_next_block ();
1080 bufsize
= available_space_after (blk
);
1081 if (size_left
< bufsize
)
1083 bufsize
= size_left
;
1084 count
= bufsize
% BLOCKSIZE
;
1086 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
1088 memcpy (blk
->buffer
, p_buffer
, bufsize
);
1089 size_left
-= bufsize
;
1090 p_buffer
+= bufsize
;
1091 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
1099 if (!recursion_option
)
1102 if (one_file_system_option
1104 && parent_device
!= st
->stat
.st_dev
)
1108 _("%s: file is on a different filesystem; not dumped"),
1109 quotearg_colon (st
->orig_file_name
)));
1113 if (exclude_caches_option
1114 && check_cache_directory(st
->orig_file_name
))
1118 _("%s: contains a cache directory tag; not dumped"),
1119 quotearg_colon (st
->orig_file_name
)));
1126 char *name_buf
= xstrdup (st
->orig_file_name
);
1127 size_t name_size
= strlen (name_buf
);
1128 size_t name_len
= name_size
;
1130 /* Now output all the files in the directory. */
1131 /* FIXME: Should speed this up by cd-ing into the dir. */
1133 for (entry
= directory
; (entry_len
= strlen (entry
)) != 0;
1134 entry
+= entry_len
+ 1)
1136 if (name_size
< name_len
+ entry_len
)
1138 name_size
= name_len
+ entry_len
;
1139 name_buf
= xrealloc (name_buf
, name_size
+ 1);
1141 strcpy (name_buf
+ name_len
, entry
);
1142 if (!excluded_name (name_buf
))
1143 dump_file (name_buf
, 0, our_device
);
1150 /* Ensure exactly one trailing slash. */
1152 ensure_slash (char **pstr
)
1154 size_t len
= strlen (*pstr
);
1155 while (len
>= 1 && ISSLASH ((*pstr
)[len
- 1]))
1157 if (!ISSLASH ((*pstr
)[len
]))
1158 *pstr
= xrealloc (*pstr
, len
+ 2);
1159 (*pstr
)[len
++] = '/';
1160 (*pstr
)[len
] = '\0';
1164 dump_dir (struct tar_stat_info
*st
, int top_level
, dev_t parent_device
)
1168 directory
= savedir (st
->orig_file_name
);
1171 savedir_diag (st
->orig_file_name
);
1175 ensure_slash (&st
->orig_file_name
);
1176 ensure_slash (&st
->file_name
);
1178 dump_dir0 (directory
, st
, top_level
, parent_device
);
1185 /* Main functions of this module. */
1188 create_archive (void)
1192 open_archive (ACCESS_WRITE
);
1193 xheader_write_global ();
1195 if (incremental_option
)
1197 size_t buffer_size
= 1000;
1198 char *buffer
= xmalloc (buffer_size
);
1201 collect_and_sort_names ();
1203 while ((p
= name_from_list ()) != NULL
)
1204 if (!excluded_name (p
))
1205 dump_file (p
, -1, (dev_t
) 0);
1208 while ((p
= name_from_list ()) != NULL
)
1209 if (!excluded_name (p
))
1211 size_t plen
= strlen (p
);
1212 if (buffer_size
<= plen
)
1214 while ((buffer_size
*= 2) <= plen
)
1216 buffer
= xrealloc (buffer
, buffer_size
);
1218 memcpy (buffer
, p
, plen
);
1219 if (! ISSLASH (buffer
[plen
- 1]))
1220 buffer
[plen
++] = '/';
1221 q
= gnu_list_name
->dir_contents
;
1225 size_t qlen
= strlen (q
);
1228 if (buffer_size
< plen
+ qlen
)
1230 while ((buffer_size
*=2 ) < plen
+ qlen
)
1232 buffer
= xrealloc (buffer
, buffer_size
);
1234 strcpy (buffer
+ plen
, q
+ 1);
1235 dump_file (buffer
, -1, (dev_t
) 0);
1244 while ((p
= name_next (1)) != NULL
)
1245 if (!excluded_name (p
))
1246 dump_file (p
, 1, (dev_t
) 0);
1252 if (listed_incremental_option
)
1253 write_directory_file ();
1257 /* Calculate the hash of a link. */
1259 hash_link (void const *entry
, size_t n_buckets
)
1261 struct link
const *l
= entry
;
1262 uintmax_t num
= l
->dev
^ l
->ino
;
1263 return num
% n_buckets
;
1266 /* Compare two links for equality. */
1268 compare_links (void const *entry1
, void const *entry2
)
1270 struct link
const *link1
= entry1
;
1271 struct link
const *link2
= entry2
;
1272 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
1276 unknown_file_error (char *p
)
1278 WARN ((0, 0, _("%s: Unknown file type; file ignored"),
1279 quotearg_colon (p
)));
1280 if (!ignore_failed_read_option
)
1281 exit_status
= TAREXIT_FAILURE
;
1285 /* Handling of hard links */
1287 /* Table of all non-directories that we've written so far. Any time
1288 we see another, we check the table and avoid dumping the data
1289 again if we've done it once already. */
1290 static Hash_table
*link_table
;
1292 /* Try to dump stat as a hard link to another file in the archive. If
1293 succeeded returns true */
1295 dump_hard_link (struct tar_stat_info
*st
)
1297 if (link_table
&& st
->stat
.st_nlink
> 1)
1300 struct link
*duplicate
;
1301 off_t block_ordinal
;
1304 lp
.ino
= st
->stat
.st_ino
;
1305 lp
.dev
= st
->stat
.st_dev
;
1307 if ((duplicate
= hash_lookup (link_table
, &lp
)))
1309 /* We found a link. */
1310 char const *link_name
= safer_name_suffix (duplicate
->name
, true,
1311 absolute_names_option
);
1315 block_ordinal
= current_block_ordinal ();
1316 assign_string (&st
->link_name
, link_name
);
1317 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
)
1318 < strlen (link_name
))
1319 write_long_link (st
);
1321 st
->stat
.st_size
= 0;
1322 blk
= start_header (st
);
1325 tar_copy_str (blk
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1327 blk
->header
.typeflag
= LNKTYPE
;
1328 finish_header (st
, blk
, block_ordinal
);
1330 if (remove_files_option
&& unlink (st
->orig_file_name
) != 0)
1331 unlink_error (st
->orig_file_name
);
1340 file_count_links (struct tar_stat_info
*st
)
1342 if (st
->stat
.st_nlink
> 1)
1344 struct link
*duplicate
;
1345 struct link
*lp
= xmalloc (offsetof (struct link
, name
)
1346 + strlen (st
->orig_file_name
) + 1);
1347 lp
->ino
= st
->stat
.st_ino
;
1348 lp
->dev
= st
->stat
.st_dev
;
1349 lp
->nlink
= st
->stat
.st_nlink
;
1350 strcpy (lp
->name
, st
->orig_file_name
);
1353 || (link_table
= hash_initialize (0, 0, hash_link
,
1355 && (duplicate
= hash_insert (link_table
, lp
))))
1358 if (duplicate
!= lp
)
1364 /* For each dumped file, check if all its links were dumped. Emit
1365 warnings if it is not so. */
1374 for (lp
= hash_get_first (link_table
); lp
;
1375 lp
= hash_get_next (link_table
, lp
))
1379 WARN ((0, 0, _("Missing links to %s.\n"), quote (lp
->name
)));
1385 /* Dump a single file, recursing on directories. P is the file name
1386 to dump. TOP_LEVEL tells whether this is a top-level call; zero
1387 means no, positive means yes, and negative means the top level
1388 of an incremental dump. PARENT_DEVICE is the device of P's
1389 parent directory; it is examined only if TOP_LEVEL is zero. */
1391 /* FIXME: One should make sure that for *every* path leading to setting
1392 exit_status to failure, a clear diagnostic has been issued. */
1395 dump_file0 (struct tar_stat_info
*st
, char *p
,
1396 int top_level
, dev_t parent_device
)
1398 union block
*header
;
1400 struct timespec original_ctime
;
1401 struct timespec restore_times
[2];
1402 off_t block_ordinal
= -1;
1404 if (interactive_option
&& !confirm ("add", p
))
1407 assign_string (&st
->orig_file_name
, p
);
1408 assign_string (&st
->file_name
,
1409 safer_name_suffix (p
, false, absolute_names_option
));
1411 if (deref_stat (dereference_option
, p
, &st
->stat
) != 0)
1416 st
->archive_file_size
= st
->stat
.st_size
;
1417 st
->atime
= restore_times
[0] = get_stat_atime (&st
->stat
);
1418 st
->mtime
= restore_times
[1] = get_stat_mtime (&st
->stat
);
1419 st
->ctime
= original_ctime
= get_stat_ctime (&st
->stat
);
1422 if (S_ISHIDDEN (st
->stat
.st_mode
))
1424 char *new = (char *) alloca (strlen (p
) + 2);
1434 /* See if we want only new files, and check if this one is too old to
1437 This check is omitted if incremental_option is set *and* the
1438 requested file is not explicitely listed in the command line. */
1440 if (!(incremental_option
&& !is_individual_file (p
))
1441 && !S_ISDIR (st
->stat
.st_mode
)
1442 && OLDER_TAR_STAT_TIME (*st
, m
)
1443 && (!after_date_option
|| OLDER_TAR_STAT_TIME (*st
, c
)))
1445 if (!incremental_option
&& verbose_option
)
1446 WARN ((0, 0, _("%s: file is unchanged; not dumped"),
1447 quotearg_colon (p
)));
1451 /* See if we are trying to dump the archive. */
1452 if (sys_file_is_archive (st
))
1454 WARN ((0, 0, _("%s: file is the archive; not dumped"),
1455 quotearg_colon (p
)));
1459 if (is_avoided_name (p
))
1461 if (S_ISDIR (st
->stat
.st_mode
))
1463 dump_dir (st
, top_level
, parent_device
);
1464 if (atime_preserve_option
)
1465 utimens (p
, restore_times
);
1470 /* Check for multiple links. */
1471 if (dump_hard_link (st
))
1474 /* This is not a link to a previously dumped file, so dump it. */
1476 if (S_ISREG (st
->stat
.st_mode
)
1477 || S_ISCTG (st
->stat
.st_mode
))
1480 enum dump_status status
;
1482 if (file_dumpable_p (st
))
1484 fd
= open (st
->orig_file_name
,
1485 O_RDONLY
| O_BINARY
);
1488 if (!top_level
&& errno
== ENOENT
)
1489 WARN ((0, 0, _("%s: File removed before we read it"),
1490 quotearg_colon (st
->orig_file_name
)));
1492 open_diag (st
->orig_file_name
);
1499 if (fd
!= -1 && sparse_option
&& sparse_file_p (st
))
1501 status
= sparse_dump_file (fd
, st
);
1502 if (status
== dump_status_not_implemented
)
1503 status
= dump_regular_file (fd
, st
);
1506 status
= dump_regular_file (fd
, st
);
1510 case dump_status_ok
:
1512 dump_regular_finish (fd
, st
, original_ctime
);
1515 case dump_status_short
:
1520 case dump_status_fail
:
1524 case dump_status_not_implemented
:
1528 if (atime_preserve_option
)
1529 utimens (st
->orig_file_name
, restore_times
);
1530 file_count_links (st
);
1533 #ifdef HAVE_READLINK
1534 else if (S_ISLNK (st
->stat
.st_mode
))
1538 size_t linklen
= st
->stat
.st_size
;
1539 if (linklen
!= st
->stat
.st_size
|| linklen
+ 1 == 0)
1541 buffer
= (char *) alloca (linklen
+ 1);
1542 size
= readlink (p
, buffer
, linklen
+ 1);
1548 buffer
[size
] = '\0';
1549 assign_string (&st
->link_name
, buffer
);
1550 if (NAME_FIELD_SIZE
- (archive_format
== OLDGNU_FORMAT
) < size
)
1551 write_long_link (st
);
1553 block_ordinal
= current_block_ordinal ();
1554 st
->stat
.st_size
= 0; /* force 0 size on symlink */
1555 header
= start_header (st
);
1558 tar_copy_str (header
->header
.linkname
, buffer
, NAME_FIELD_SIZE
);
1559 header
->header
.typeflag
= SYMTYPE
;
1560 finish_header (st
, header
, block_ordinal
);
1561 /* nothing more to do to it */
1563 if (remove_files_option
)
1565 if (unlink (p
) == -1)
1568 file_count_links (st
);
1572 else if (S_ISCHR (st
->stat
.st_mode
))
1574 else if (S_ISBLK (st
->stat
.st_mode
))
1576 else if (S_ISFIFO (st
->stat
.st_mode
))
1578 else if (S_ISSOCK (st
->stat
.st_mode
))
1580 WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1583 else if (S_ISDOOR (st
->stat
.st_mode
))
1585 WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1590 unknown_file_error (p
);
1595 if (archive_format
== V7_FORMAT
)
1597 unknown_file_error (p
);
1601 block_ordinal
= current_block_ordinal ();
1602 st
->stat
.st_size
= 0; /* force 0 size */
1603 header
= start_header (st
);
1606 header
->header
.typeflag
= type
;
1608 if (type
!= FIFOTYPE
)
1610 MAJOR_TO_CHARS (major (st
->stat
.st_rdev
),
1611 header
->header
.devmajor
);
1612 MINOR_TO_CHARS (minor (st
->stat
.st_rdev
),
1613 header
->header
.devminor
);
1616 finish_header (st
, header
, block_ordinal
);
1617 if (remove_files_option
)
1619 if (unlink (p
) == -1)
1625 dump_file (char *p
, int top_level
, dev_t parent_device
)
1627 struct tar_stat_info st
;
1628 tar_stat_init (&st
);
1629 dump_file0 (&st
, p
, top_level
, parent_device
);
1630 if (listed_incremental_option
)
1631 update_parent_directory (p
);
1632 tar_stat_destroy (&st
);