1 /* Create a tar archive.
3 Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
47 /* The maximum uintmax_t value that can be represented with DIGITS digits,
48 assuming that each digit is BITS_PER_DIGIT wide. */
49 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
50 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
51 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
54 /* Convert VALUE to an octal representation suitable for tar headers.
55 Output to buffer WHERE with size SIZE.
56 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
59 to_octal (uintmax_t value
, char *where
, size_t size
)
66 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
72 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
73 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
74 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
75 The result is undefined if SIZE is 0 or if VALUE is too large to
79 to_base256 (int negative
, uintmax_t value
, char *where
, size_t size
)
82 uintmax_t propagated_sign_bits
=
83 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
88 where
[--i
] = v
& ((1 << LG_256
) - 1);
89 v
= propagated_sign_bits
| (v
>> LG_256
);
94 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
95 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
96 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
97 negative before being cast to uintmax_t; its original bitpattern
98 can be deduced from VALSIZE, its original size before casting.
99 TYPE is the kind of value being output (useful for diagnostics).
100 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
101 digits), followed by '\0'. If this won't work, and if GNU or
102 OLDGNU format is allowed, use '\200' followed by base-256, or (if
103 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
104 If neither format works, use SUBSTITUTE (...) instead. Pass to
105 SUBSTITUTE the address of an 0-or-1 flag recording whether the
106 substitute value is negative. */
109 to_chars (int negative
, uintmax_t value
, size_t valsize
,
110 uintmax_t (*substitute
) (int *),
111 char *where
, size_t size
, const char *type
)
113 int base256_allowed
= (archive_format
== GNU_FORMAT
114 || archive_format
== OLDGNU_FORMAT
);
116 /* Generate the POSIX octal representation if the number fits. */
117 if (! negative
&& value
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
119 where
[size
- 1] = '\0';
120 to_octal (value
, where
, size
- 1);
123 /* Otherwise, generate the base-256 representation if we are
124 generating an old or new GNU format and if the number fits. */
125 else if (((negative
? -1 - value
: value
)
126 <= MAX_VAL_WITH_DIGITS (size
- 1, LG_256
))
129 where
[0] = negative
? -1 : 1 << (LG_256
- 1);
130 to_base256 (negative
, value
, where
+ 1, size
- 1);
133 /* Otherwise, if the number is negative, and if it would not cause
134 ambiguity on this host by confusing positive with negative
135 values, then generate the POSIX octal representation of the value
136 modulo 2**(field bits). The resulting tar file is
137 machine-dependent, since it depends on the host word size. Yuck!
138 But this is the traditional behavior. */
139 else if (negative
&& valsize
* CHAR_BIT
<= (size
- 1) * LG_8
)
141 static int warned_once
;
145 WARN ((0, 0, _("Generating negative octal headers")));
147 where
[size
- 1] = '\0';
148 to_octal (value
& MAX_VAL_WITH_DIGITS (valsize
* CHAR_BIT
, 1),
152 /* Otherwise, output a substitute value if possible (with a
153 warning), and an error message if not. */
156 uintmax_t maxval
= (base256_allowed
157 ? MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)
158 : MAX_VAL_WITH_DIGITS (size
- 1, LG_8
));
159 char valbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
160 char maxbuf
[UINTMAX_STRSIZE_BOUND
];
161 char minbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
162 char const *minval_string
;
163 char const *maxval_string
= STRINGIFY_BIGINT (maxval
, maxbuf
);
164 char const *value_string
;
168 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
169 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
178 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
183 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
188 uintmax_t sub
= substitute (&negsub
) & maxval
;
189 /* FIXME: This is the only place where GNU_FORMAT differs from
190 OLDGNU_FORMAT. Apart from this they are completely identical. */
191 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? - sub
: sub
;
192 char subbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
193 char *sub_string
= STRINGIFY_BIGINT (s
, subbuf
+ 1);
196 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
197 value_string
, type
, minval_string
, maxval_string
,
199 to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
202 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
203 value_string
, type
, minval_string
, maxval_string
));
208 gid_substitute (int *negative
)
214 static gid_t gid_nobody
;
215 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
224 gid_to_chars (gid_t v
, char *p
, size_t s
)
226 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
230 major_to_chars (major_t v
, char *p
, size_t s
)
232 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
236 minor_to_chars (minor_t v
, char *p
, size_t s
)
238 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
242 mode_to_chars (mode_t v
, char *p
, size_t s
)
244 /* In the common case where the internal and external mode bits are the same,
245 and we are not using POSIX or GNU format,
246 propagate all unknown bits to the external mode.
247 This matches historical practice.
248 Otherwise, just copy the bits we know about. */
251 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
252 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
253 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
254 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
255 && archive_format
!= POSIX_FORMAT
256 && archive_format
!= USTAR_FORMAT
257 && archive_format
!= GNU_FORMAT
)
265 u
= ((v
& S_ISUID
? TSUID
: 0)
266 | (v
& S_ISGID
? TSGID
: 0)
267 | (v
& S_ISVTX
? TSVTX
: 0)
268 | (v
& S_IRUSR
? TUREAD
: 0)
269 | (v
& S_IWUSR
? TUWRITE
: 0)
270 | (v
& S_IXUSR
? TUEXEC
: 0)
271 | (v
& S_IRGRP
? TGREAD
: 0)
272 | (v
& S_IWGRP
? TGWRITE
: 0)
273 | (v
& S_IXGRP
? TGEXEC
: 0)
274 | (v
& S_IROTH
? TOREAD
: 0)
275 | (v
& S_IWOTH
? TOWRITE
: 0)
276 | (v
& S_IXOTH
? TOEXEC
: 0));
278 to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
282 off_to_chars (off_t v
, char *p
, size_t s
)
284 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
288 size_to_chars (size_t v
, char *p
, size_t s
)
290 to_chars (0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "size_t");
294 time_to_chars (time_t v
, char *p
, size_t s
)
296 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
300 uid_substitute (int *negative
)
306 static uid_t uid_nobody
;
307 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
316 uid_to_chars (uid_t v
, char *p
, size_t s
)
318 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
322 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
324 to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
328 string_to_chars (char *str
, char *p
, size_t s
)
335 /* A file is not dumpable if
336 a) it is empty *and* world-readable, or
337 b) current archive is /dev/null */
340 file_dumpable_p (struct tar_stat_info
*stat
)
342 return !(dev_null_output
343 || (stat
->archive_file_size
== 0
344 && (stat
->stat
.st_mode
& MODE_R
) == MODE_R
));
348 /* Writing routines. */
350 /* Write the EOT block(s). Zero at least two blocks, through the end
351 of the record. Old tar, as previous versions of GNU tar, writes
352 garbage after two zeroed blocks. */
356 union block
*pointer
= find_next_block ();
357 memset (pointer
->buffer
, 0, BLOCKSIZE
);
358 set_next_block_after (pointer
);
359 pointer
= find_next_block ();
360 memset (pointer
->buffer
, 0, available_space_after (pointer
));
361 set_next_block_after (pointer
);
364 /* Copy at most LEN bytes from SRC to DST. Terminate with NUL unless
365 SRC is LEN characters long */
367 tar_copy_str (char *dst
, const char *src
, size_t len
)
370 strncpy (dst
, src
, len
);
373 /* Write a "private" header */
375 start_private_header (const char *name
, size_t size
)
378 union block
*header
= find_next_block ();
380 memset (header
->buffer
, 0, sizeof (union block
));
382 tar_copy_str (header
->header
.name
, name
, NAME_FIELD_SIZE
);
383 OFF_TO_CHARS (size
, header
->header
.size
);
386 TIME_TO_CHARS (t
, header
->header
.mtime
);
387 MODE_TO_CHARS (S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
, header
->header
.mode
);
388 UID_TO_CHARS (getuid (), header
->header
.uid
);
389 GID_TO_CHARS (getgid (), header
->header
.gid
);
390 MAJOR_TO_CHARS (0, header
->header
.devmajor
);
391 MAJOR_TO_CHARS (0, header
->header
.devminor
);
392 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
393 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
397 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
401 write_short_name (struct tar_stat_info
*st
)
403 union block
*header
= find_next_block ();
404 memset (header
->buffer
, 0, sizeof (union block
));
405 tar_copy_str (header
->header
.name
, st
->file_name
, NAME_FIELD_SIZE
);
409 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
411 write_gnu_long_link (struct tar_stat_info
*st
, const char *p
, char type
)
413 size_t size
= strlen (p
) + 1;
417 header
= start_private_header ("././@LongLink", size
);
418 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
419 header
->header
.typeflag
= type
;
420 finish_header (st
, header
, -1);
422 header
= find_next_block ();
424 bufsize
= available_space_after (header
);
426 while (bufsize
< size
)
428 memcpy (header
->buffer
, p
, bufsize
);
431 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
432 header
= find_next_block ();
433 bufsize
= available_space_after (header
);
435 memcpy (header
->buffer
, p
, size
);
436 memset (header
->buffer
+ size
, 0, bufsize
- size
);
437 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
441 split_long_name (const char *name
, size_t length
)
445 if (length
> PREFIX_FIELD_SIZE
)
446 length
= PREFIX_FIELD_SIZE
+2;
447 for (i
= length
- 1; i
> 0; i
--)
448 if (ISSLASH (name
[i
]))
454 write_ustar_long_name (const char *name
)
456 size_t length
= strlen (name
);
460 if (length
> PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1)
462 WARN ((0, 0, _("%s: file name is too long (max %d); not dumped"),
463 quotearg_colon (name
),
464 PREFIX_FIELD_SIZE
+ NAME_FIELD_SIZE
+ 1));
468 i
= split_long_name (name
, length
);
469 if (i
== 0 || length
- i
- 1 > NAME_FIELD_SIZE
)
472 _("%s: file name is too long (cannot be split); not dumped"),
473 quotearg_colon (name
)));
477 header
= find_next_block ();
478 memset (header
->buffer
, 0, sizeof (header
->buffer
));
479 memcpy (header
->header
.prefix
, name
, i
);
480 memcpy (header
->header
.name
, name
+ i
+ 1, length
- i
- 1);
485 /* Write a long link name, depending on the current archive format */
487 write_long_link (struct tar_stat_info
*st
)
489 switch (archive_format
)
492 xheader_store ("linkpath", st
, NULL
);
495 case V7_FORMAT
: /* old V7 tar format */
499 _("%s: link name is too long; not dumped"),
500 quotearg_colon (st
->link_name
)));
505 write_gnu_long_link (st
, st
->link_name
, GNUTYPE_LONGLINK
);
514 write_long_name (struct tar_stat_info
*st
)
516 switch (archive_format
)
519 xheader_store ("path", st
, NULL
);
525 return write_ustar_long_name (st
->file_name
);
529 write_gnu_long_link (st
, st
->file_name
, GNUTYPE_LONGNAME
);
535 return write_short_name (st
);
539 write_extended (struct tar_stat_info
*st
, union block
*old_header
)
541 union block
*header
, hp
;
545 if (extended_header
.buffer
|| extended_header
.stk
== NULL
)
548 xheader_finish (&extended_header
);
549 memcpy (hp
.buffer
, old_header
, sizeof (hp
));
550 p
= xheader_xhdr_name (st
);
551 xheader_write (XHDTYPE
, p
, &extended_header
);
553 header
= find_next_block ();
554 memcpy (header
, &hp
.buffer
, sizeof (hp
.buffer
));
559 write_header_name (struct tar_stat_info
*st
)
561 if (NAME_FIELD_SIZE
< strlen (st
->file_name
))
562 return write_long_name (st
);
564 return write_short_name (st
);
568 /* Header handling. */
570 /* Make a header block for the file whose stat info is st,
571 and return its address. */
574 start_header (struct tar_stat_info
*st
)
578 header
= write_header_name (st
);
582 /* Override some stat fields, if requested to do so. */
584 if (owner_option
!= (uid_t
) -1)
585 st
->stat
.st_uid
= owner_option
;
586 if (group_option
!= (gid_t
) -1)
587 st
->stat
.st_gid
= group_option
;
589 st
->stat
.st_mode
= ((st
->stat
.st_mode
& ~MODE_ALL
)
590 | mode_adjust (st
->stat
.st_mode
, mode_option
));
592 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
593 for a few tars and came up with the following interoperability
597 1 2 3 4 5 6 7 8 9 READER
598 . . . . . . . . . 1 = SunOS 4.2 tar
599 # . . # # . . # # 2 = NEC SVR4.0.2 tar
600 . . . # # . . # . 3 = Solaris 2.1 tar
601 . . . . . . . . . 4 = GNU tar 1.11.1
602 . . . . . . . . . 5 = HP-UX 8.07 tar
603 . . . . . . . . . 6 = Ultrix 4.1
604 . . . . . . . . . 7 = AIX 3.2
605 . . . . . . . . . 8 = Hitachi HI-UX 1.03
606 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
609 # = ``impossible file type''
611 The following mask for old archive removes the `#'s in column 4
612 above, thus making GNU tar both a universal donor and a universal
613 acceptor for Paul's test. */
615 if (archive_format
== V7_FORMAT
|| archive_format
== USTAR_FORMAT
)
616 MODE_TO_CHARS (st
->stat
.st_mode
& MODE_ALL
, header
->header
.mode
);
618 MODE_TO_CHARS (st
->stat
.st_mode
, header
->header
.mode
);
620 if (st
->stat
.st_uid
> MAXOCTAL7
&& archive_format
== POSIX_FORMAT
)
621 xheader_store ("uid", st
, NULL
);
623 UID_TO_CHARS (st
->stat
.st_uid
, header
->header
.uid
);
625 if (st
->stat
.st_gid
> MAXOCTAL7
&& archive_format
== POSIX_FORMAT
)
626 xheader_store ("gid", st
, NULL
);
628 GID_TO_CHARS (st
->stat
.st_gid
, header
->header
.gid
);
630 if (st
->stat
.st_size
> MAXOCTAL11
&& archive_format
== POSIX_FORMAT
)
631 xheader_store ("size", st
, NULL
);
633 OFF_TO_CHARS (st
->stat
.st_size
, header
->header
.size
);
635 TIME_TO_CHARS (st
->stat
.st_mtime
, header
->header
.mtime
);
638 if (S_ISCHR (st
->stat
.st_mode
)
639 || S_ISBLK (st
->stat
.st_mode
))
641 st
->devmajor
= major (st
->stat
.st_rdev
);
642 st
->devminor
= minor (st
->stat
.st_rdev
);
644 if (st
->devmajor
> MAXOCTAL7
&& archive_format
== POSIX_FORMAT
)
645 xheader_store ("devmajor", st
, NULL
);
647 MAJOR_TO_CHARS (st
->devmajor
, header
->header
.devmajor
);
649 if (st
->devminor
> MAXOCTAL7
&& archive_format
== POSIX_FORMAT
)
650 xheader_store ("devminor", st
, NULL
);
652 MAJOR_TO_CHARS (st
->devminor
, header
->header
.devminor
);
656 MAJOR_TO_CHARS (0, header
->header
.devmajor
);
657 MINOR_TO_CHARS (0, header
->header
.devminor
);
660 if (archive_format
== POSIX_FORMAT
)
662 xheader_store ("atime", st
, NULL
);
663 xheader_store ("ctime", st
, NULL
);
665 else if (incremental_option
)
666 if (archive_format
== OLDGNU_FORMAT
)
668 TIME_TO_CHARS (st
->stat
.st_atime
, header
->oldgnu_header
.atime
);
669 TIME_TO_CHARS (st
->stat
.st_ctime
, header
->oldgnu_header
.ctime
);
672 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
674 switch (archive_format
)
680 case GNU_FORMAT
: /*FIXME?*/
681 /* Overwrite header->header.magic and header.version in one blow. */
682 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
687 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
688 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
695 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
697 /* header->header.[ug]name are left as the empty string. */
701 uid_to_uname (st
->stat
.st_uid
, &st
->uname
);
702 gid_to_gname (st
->stat
.st_gid
, &st
->gname
);
704 if (archive_format
== POSIX_FORMAT
705 && strlen (st
->uname
) > UNAME_FIELD_SIZE
)
706 xheader_store ("uname", st
, NULL
);
708 UNAME_TO_CHARS (st
->uname
, header
->header
.uname
);
710 if (archive_format
== POSIX_FORMAT
711 && strlen (st
->gname
) > GNAME_FIELD_SIZE
)
712 xheader_store ("gname", st
, NULL
);
714 GNAME_TO_CHARS (st
->gname
, header
->header
.gname
);
721 simple_finish_header (union block
*header
)
727 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
731 for (i
= sizeof *header
; i
-- != 0; )
732 /* We can't use unsigned char here because of old compilers, e.g. V7. */
735 /* Fill in the checksum field. It's formatted differently from the
736 other fields: it has [6] digits, a null, then a space -- rather than
737 digits, then a null. We use to_chars.
738 The final space is already there, from
739 checksumming, and to_chars doesn't modify it.
741 This is a fast way to do:
743 sprintf(header->header.chksum, "%6o", sum); */
745 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
747 set_next_block_after (header
);
750 /* Finish off a filled-in header block and write it out. We also
751 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
752 is not negative, is the block ordinal of the first record for this
753 file, which may be a preceding long name or long link record. */
755 finish_header (struct tar_stat_info
*st
,
756 union block
*header
, off_t block_ordinal
)
762 /* Note: It is important to do this before the call to write_extended(),
763 so that the actual ustar header is printed */
765 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
766 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
767 && header
->header
.typeflag
!= XHDTYPE
768 && header
->header
.typeflag
!= XGLTYPE
)
770 /* These globals are parameters to print_header, sigh. */
772 current_header
= header
;
773 current_format
= archive_format
;
774 print_header (st
, block_ordinal
);
777 header
= write_extended (st
, header
);
778 simple_finish_header (header
);
783 pad_archive (off_t size_left
)
786 while (size_left
> 0)
788 save_sizeleft
= size_left
;
789 blk
= find_next_block ();
790 memset (blk
->buffer
, 0, BLOCKSIZE
);
791 set_next_block_after (blk
);
792 size_left
-= BLOCKSIZE
;
796 static enum dump_status
797 dump_regular_file (int fd
, struct tar_stat_info
*stat
)
799 off_t size_left
= stat
->stat
.st_size
;
803 block_ordinal
= current_block_ordinal ();
804 blk
= start_header (stat
);
806 return dump_status_fail
;
808 /* Mark contiguous files, if we support them. */
809 if (archive_format
!= V7_FORMAT
&& S_ISCTG (stat
->stat
.st_mode
))
810 blk
->header
.typeflag
= CONTTYPE
;
812 finish_header (stat
, blk
, block_ordinal
);
814 while (size_left
> 0)
816 size_t bufsize
, count
;
818 if (multi_volume_option
)
820 assign_string (&save_name
, stat
->file_name
);
821 save_sizeleft
= size_left
;
822 save_totsize
= stat
->stat
.st_size
;
824 blk
= find_next_block ();
826 bufsize
= available_space_after (blk
);
828 if (size_left
< bufsize
)
830 /* Last read -- zero out area beyond. */
832 count
= bufsize
% BLOCKSIZE
;
834 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
837 count
= (fd
< 0) ? bufsize
: safe_read (fd
, blk
->buffer
, bufsize
);
840 read_diag_details (stat
->orig_file_name
,
841 stat
->stat
.st_size
- size_left
, bufsize
);
842 pad_archive (size_left
);
843 return dump_status_short
;
847 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
849 if (count
!= bufsize
)
851 char buf
[UINTMAX_STRSIZE_BOUND
];
852 memset (blk
->buffer
+ count
, 0, bufsize
- count
);
854 ngettext ("%s: File shrank by %s byte; padding with zeros",
855 "%s: File shrank by %s bytes; padding with zeros",
857 quotearg_colon (stat
->orig_file_name
),
858 STRINGIFY_BIGINT (size_left
, buf
)));
859 if (! ignore_failed_read_option
)
860 exit_status
= TAREXIT_FAILURE
;
861 pad_archive (size_left
);
862 return dump_status_short
;
865 return dump_status_ok
;
869 dump_regular_finish (int fd
, struct tar_stat_info
*st
, time_t original_ctime
)
873 struct stat final_stat
;
874 if (fstat (fd
, &final_stat
) != 0)
876 stat_diag (st
->orig_file_name
);
878 else if (final_stat
.st_ctime
!= original_ctime
)
880 WARN ((0, 0, _("%s: file changed as we read it"),
881 quotearg_colon (st
->orig_file_name
)));
885 close_diag (st
->orig_file_name
);
888 if (remove_files_option
)
890 if (unlink (st
->orig_file_name
) == -1)
891 unlink_error (st
->orig_file_name
);
896 dump_dir0 (char *directory
,
897 struct tar_stat_info
*stat
, int top_level
, dev_t parent_device
)
899 dev_t our_device
= stat
->stat
.st_dev
;
901 if (!is_avoided_name (stat
->orig_file_name
))
903 union block
*blk
= NULL
;
904 off_t block_ordinal
= current_block_ordinal ();
905 stat
->stat
.st_size
= 0; /* force 0 size on dir */
907 blk
= start_header (stat
);
911 if (incremental_option
)
912 blk
->header
.typeflag
= GNUTYPE_DUMPDIR
;
913 else /* if (standard_option) */
914 blk
->header
.typeflag
= DIRTYPE
;
916 /* If we're gnudumping, we aren't done yet so don't close it. */
918 if (!incremental_option
)
919 finish_header (stat
, blk
, block_ordinal
);
920 else if (gnu_list_name
->dir_contents
)
926 const char *buffer
, *p_buffer
;
927 off_t block_ordinal
= current_block_ordinal ();
929 buffer
= gnu_list_name
->dir_contents
; /* FOO */
932 for (p_buffer
= buffer
; *p_buffer
; )
934 size_t size
= strlen (p_buffer
) + 1;
939 OFF_TO_CHARS (totsize
, blk
->header
.size
);
940 finish_header (stat
, blk
, block_ordinal
);
943 while (size_left
> 0)
945 if (multi_volume_option
)
947 assign_string (&save_name
, stat
->orig_file_name
);
948 save_sizeleft
= size_left
;
949 save_totsize
= totsize
;
951 blk
= find_next_block ();
952 bufsize
= available_space_after (blk
);
953 if (size_left
< bufsize
)
956 count
= bufsize
% BLOCKSIZE
;
958 memset (blk
->buffer
+ size_left
, 0, BLOCKSIZE
- count
);
960 memcpy (blk
->buffer
, p_buffer
, bufsize
);
961 size_left
-= bufsize
;
963 set_next_block_after (blk
+ (bufsize
- 1) / BLOCKSIZE
);
965 if (multi_volume_option
)
966 assign_string (&save_name
, 0);
971 if (!recursion_option
)
974 if (one_file_system_option
976 && parent_device
!= stat
->stat
.st_dev
)
980 _("%s: file is on a different filesystem; not dumped"),
981 quotearg_colon (stat
->orig_file_name
)));
988 char *name_buf
= strdup (stat
->orig_file_name
);
989 size_t name_size
= strlen (name_buf
);
990 size_t name_len
= name_size
;
992 /* Now output all the files in the directory. */
993 /* FIXME: Should speed this up by cd-ing into the dir. */
995 for (entry
= directory
; (entry_len
= strlen (entry
)) != 0;
996 entry
+= entry_len
+ 1)
998 if (name_size
< name_len
+ entry_len
)
1000 name_size
= name_len
+ entry_len
;
1001 name_buf
= xrealloc (name_buf
, name_size
+ 1);
1003 strcpy (name_buf
+ name_len
, entry
);
1004 if (!excluded_name (name_buf
))
1005 dump_file (name_buf
, 0, our_device
);
1012 /* Ensure exactly one trailing slash. */
1014 ensure_slash (char **pstr
)
1016 size_t len
= strlen (*pstr
);
1017 while (len
>= 1 && ISSLASH ((*pstr
)[len
- 1]))
1019 if (!ISSLASH ((*pstr
)[len
]))
1020 *pstr
= xrealloc (*pstr
, len
+ 2);
1021 (*pstr
)[len
++] = '/';
1022 (*pstr
)[len
] = '\0';
1026 dump_dir (struct tar_stat_info
*stat
, int top_level
, dev_t parent_device
)
1030 directory
= savedir (stat
->orig_file_name
);
1033 savedir_diag (stat
->orig_file_name
);
1037 ensure_slash (&stat
->orig_file_name
);
1038 ensure_slash (&stat
->file_name
);
1040 dump_dir0 (directory
, stat
, top_level
, parent_device
);
1047 /* Main functions of this module. */
1050 create_archive (void)
1054 open_archive (ACCESS_WRITE
);
1055 xheader_write_global ();
1057 if (incremental_option
)
1059 size_t buffer_size
= 1000;
1060 char *buffer
= xmalloc (buffer_size
);
1063 collect_and_sort_names ();
1065 while ((p
= name_from_list ()) != NULL
)
1066 if (!excluded_name (p
))
1067 dump_file (p
, -1, (dev_t
) 0);
1070 while ((p
= name_from_list ()) != NULL
)
1071 if (!excluded_name (p
))
1073 size_t plen
= strlen (p
);
1074 if (buffer_size
<= plen
)
1076 while ((buffer_size
*= 2) <= plen
)
1078 buffer
= xrealloc (buffer
, buffer_size
);
1080 memcpy (buffer
, p
, plen
);
1081 if (! ISSLASH (buffer
[plen
- 1]))
1082 buffer
[plen
++] = '/';
1083 q
= gnu_list_name
->dir_contents
;
1087 size_t qlen
= strlen (q
);
1090 if (buffer_size
< plen
+ qlen
)
1092 while ((buffer_size
*=2 ) < plen
+ qlen
)
1094 buffer
= xrealloc (buffer
, buffer_size
);
1096 strcpy (buffer
+ plen
, q
+ 1);
1097 dump_file (buffer
, -1, (dev_t
) 0);
1106 while ((p
= name_next (1)) != NULL
)
1107 if (!excluded_name (p
))
1108 dump_file (p
, 1, (dev_t
) 0);
1114 if (listed_incremental_option
)
1115 write_directory_file ();
1119 /* Calculate the hash of a link. */
1121 hash_link (void const *entry
, unsigned n_buckets
)
1123 struct link
const *link
= entry
;
1124 return (uintmax_t) (link
->dev
^ link
->ino
) % n_buckets
;
1127 /* Compare two links for equality. */
1129 compare_links (void const *entry1
, void const *entry2
)
1131 struct link
const *link1
= entry1
;
1132 struct link
const *link2
= entry2
;
1133 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
1137 unknown_file_error (char *p
)
1139 WARN ((0, 0, _("%s: Unknown file type; file ignored"),
1140 quotearg_colon (p
)));
1141 if (!ignore_failed_read_option
)
1142 exit_status
= TAREXIT_FAILURE
;
1146 /* Handling of hard links */
1148 /* Table of all non-directories that we've written so far. Any time
1149 we see another, we check the table and avoid dumping the data
1150 again if we've done it once already. */
1151 static Hash_table
*link_table
;
1153 /* Try to dump stat as a hard link to another file in the archive. If
1154 succeeded returns true */
1156 dump_hard_link (struct tar_stat_info
*stat
)
1158 if (link_table
&& stat
->stat
.st_nlink
> 1)
1162 off_t block_ordinal
;
1165 lp
.ino
= stat
->stat
.st_ino
;
1166 lp
.dev
= stat
->stat
.st_dev
;
1168 if ((dup
= hash_lookup (link_table
, &lp
)))
1170 /* We found a link. */
1171 char const *link_name
= safer_name_suffix (dup
->name
, true);
1175 block_ordinal
= current_block_ordinal ();
1176 assign_string (&stat
->link_name
, link_name
);
1177 if (NAME_FIELD_SIZE
< strlen (link_name
))
1178 write_long_link (stat
);
1180 stat
->stat
.st_size
= 0;
1181 blk
= start_header (stat
);
1184 tar_copy_str (blk
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1186 blk
->header
.typeflag
= LNKTYPE
;
1187 finish_header (stat
, blk
, block_ordinal
);
1189 if (remove_files_option
&& unlink (stat
->orig_file_name
) != 0)
1190 unlink_error (stat
->orig_file_name
);
1199 file_count_links (struct tar_stat_info
*stat
)
1201 if (stat
->stat
.st_nlink
> 1)
1204 struct link
*lp
= xmalloc (offsetof (struct link
, name
)
1205 + strlen (stat
->orig_file_name
) + 1);
1206 lp
->ino
= stat
->stat
.st_ino
;
1207 lp
->dev
= stat
->stat
.st_dev
;
1208 lp
->nlink
= stat
->stat
.st_nlink
;
1209 strcpy (lp
->name
, stat
->orig_file_name
);
1212 || (link_table
= hash_initialize (0, 0, hash_link
,
1214 && (dup
= hash_insert (link_table
, lp
))))
1223 /* For each dumped file, check if all its links were dumped. Emit
1224 warnings if it is not so. */
1233 for (lp
= hash_get_first (link_table
); lp
;
1234 lp
= hash_get_next (link_table
, lp
))
1238 WARN ((0, 0, _("Missing links to '%s'.\n"), lp
->name
));
1244 /* Dump a single file, recursing on directories. P is the file name
1245 to dump. TOP_LEVEL tells whether this is a top-level call; zero
1246 means no, positive means yes, and negative means the top level
1247 of an incremental dump. PARENT_DEVICE is the device of P's
1248 parent directory; it is examined only if TOP_LEVEL is zero. */
1250 /* FIXME: One should make sure that for *every* path leading to setting
1251 exit_status to failure, a clear diagnostic has been issued. */
1254 dump_file0 (struct tar_stat_info
*stat
, char *p
,
1255 int top_level
, dev_t parent_device
)
1257 union block
*header
;
1259 time_t original_ctime
;
1260 struct utimbuf restore_times
;
1261 off_t block_ordinal
= -1;
1263 if (interactive_option
&& !confirm ("add", p
))
1266 assign_string (&stat
->orig_file_name
, p
);
1267 assign_string (&stat
->file_name
, safer_name_suffix (p
, false));
1269 if (deref_stat (dereference_option
, p
, &stat
->stat
) != 0)
1274 stat
->archive_file_size
= stat
->stat
.st_size
;
1275 sys_stat_nanoseconds(stat
);
1276 original_ctime
= stat
->stat
.st_ctime
;
1277 restore_times
.actime
= stat
->stat
.st_atime
;
1278 restore_times
.modtime
= stat
->stat
.st_mtime
;
1281 if (S_ISHIDDEN (stat
->stat
.st_mode
))
1283 char *new = (char *) alloca (strlen (p
) + 2);
1293 /* See if we want only new files, and check if this one is too old to
1294 put in the archive. */
1296 if ((0 < top_level
|| !incremental_option
)
1297 && !S_ISDIR (stat
->stat
.st_mode
)
1298 && stat
->stat
.st_mtime
< newer_mtime_option
1299 && (!after_date_option
|| stat
->stat
.st_ctime
< newer_ctime_option
))
1302 WARN ((0, 0, _("%s: file is unchanged; not dumped"),
1303 quotearg_colon (p
)));
1304 /* FIXME: recheck this return. */
1308 /* See if we are trying to dump the archive. */
1309 if (sys_file_is_archive (stat
))
1311 WARN ((0, 0, _("%s: file is the archive; not dumped"),
1312 quotearg_colon (p
)));
1316 if (S_ISDIR (stat
->stat
.st_mode
))
1318 dump_dir (stat
, top_level
, parent_device
);
1319 if (atime_preserve_option
)
1320 utime (p
, &restore_times
);
1323 else if (is_avoided_name (p
))
1327 /* Check for multiple links. */
1328 if (dump_hard_link (stat
))
1331 /* This is not a link to a previously dumped file, so dump it. */
1333 if (S_ISREG (stat
->stat
.st_mode
)
1334 || S_ISCTG (stat
->stat
.st_mode
))
1337 enum dump_status status
;
1339 if (file_dumpable_p (stat
))
1341 fd
= open (stat
->orig_file_name
,
1342 O_RDONLY
| O_BINARY
);
1345 if (!top_level
&& errno
== ENOENT
)
1346 WARN ((0, 0, _("%s: File removed before we read it"),
1347 quotearg_colon (stat
->orig_file_name
)));
1349 open_diag (stat
->orig_file_name
);
1356 if (sparse_option
&& sparse_file_p (stat
))
1358 status
= sparse_dump_file (fd
, stat
);
1359 if (status
== dump_status_not_implemented
)
1360 status
= dump_regular_file (fd
, stat
);
1363 status
= dump_regular_file (fd
, stat
);
1367 case dump_status_ok
:
1368 if (multi_volume_option
)
1369 assign_string (&save_name
, 0);
1370 dump_regular_finish (fd
, stat
, original_ctime
);
1373 case dump_status_short
:
1374 if (multi_volume_option
)
1375 assign_string (&save_name
, 0);
1379 case dump_status_fail
:
1383 case dump_status_not_implemented
:
1387 if (atime_preserve_option
)
1388 utime (stat
->orig_file_name
, &restore_times
);
1389 file_count_links (stat
);
1392 #ifdef HAVE_READLINK
1393 else if (S_ISLNK (stat
->stat
.st_mode
))
1397 size_t linklen
= stat
->stat
.st_size
;
1398 if (linklen
!= stat
->stat
.st_size
|| linklen
+ 1 == 0)
1400 buffer
= (char *) alloca (linklen
+ 1);
1401 size
= readlink (p
, buffer
, linklen
+ 1);
1407 buffer
[size
] = '\0';
1408 assign_string (&stat
->link_name
, buffer
);
1409 if (size
> NAME_FIELD_SIZE
)
1410 write_long_link (stat
);
1412 block_ordinal
= current_block_ordinal ();
1413 stat
->stat
.st_size
= 0; /* force 0 size on symlink */
1414 header
= start_header (stat
);
1417 tar_copy_str (header
->header
.linkname
, buffer
, NAME_FIELD_SIZE
);
1418 header
->header
.typeflag
= SYMTYPE
;
1419 finish_header (stat
, header
, block_ordinal
);
1420 /* nothing more to do to it */
1422 if (remove_files_option
)
1424 if (unlink (p
) == -1)
1427 file_count_links (stat
);
1431 else if (S_ISCHR (stat
->stat
.st_mode
))
1433 else if (S_ISBLK (stat
->stat
.st_mode
))
1435 else if (S_ISFIFO (stat
->stat
.st_mode
))
1437 else if (S_ISSOCK (stat
->stat
.st_mode
))
1439 WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1442 else if (S_ISDOOR (stat
->stat
.st_mode
))
1444 WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1449 unknown_file_error (p
);
1454 if (archive_format
== V7_FORMAT
)
1456 unknown_file_error (p
);
1460 block_ordinal
= current_block_ordinal ();
1461 stat
->stat
.st_size
= 0; /* force 0 size */
1462 header
= start_header (stat
);
1465 header
->header
.typeflag
= type
;
1467 if (type
!= FIFOTYPE
)
1469 MAJOR_TO_CHARS (major (stat
->stat
.st_rdev
),
1470 header
->header
.devmajor
);
1471 MINOR_TO_CHARS (minor (stat
->stat
.st_rdev
),
1472 header
->header
.devminor
);
1475 finish_header (stat
, header
, block_ordinal
);
1476 if (remove_files_option
)
1478 if (unlink (p
) == -1)
1484 dump_file (char *p
, int top_level
, dev_t parent_device
)
1486 struct tar_stat_info stat
;
1487 tar_stat_init (&stat
);
1488 dump_file0 (&stat
, p
, top_level
, parent_device
);
1489 tar_stat_destroy (&stat
);