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. */
57 /* The maximum uintmax_t value that can be represented with DIGITS digits,
58 assuming that each digit is BITS_PER_DIGIT wide. */
59 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
60 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
61 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
64 /* Convert VALUE to an octal representation suitable for tar headers.
65 Output to buffer WHERE with size SIZE.
66 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
69 to_octal (uintmax_t value
, char *where
, size_t size
)
76 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
82 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
83 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
84 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
85 The result is undefined if SIZE is 0 or if VALUE is too large to
89 to_base256 (int negative
, uintmax_t value
, char *where
, size_t size
)
92 uintmax_t propagated_sign_bits
=
93 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
98 where
[--i
] = v
& ((1 << LG_256
) - 1);
99 v
= propagated_sign_bits
| (v
>> LG_256
);
104 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
105 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
106 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
107 negative before being cast to uintmax_t; its original bitpattern
108 can be deduced from VALSIZE, its original size before casting.
109 TYPE is the kind of value being output (useful for diagnostics).
110 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
111 digits), followed by '\0'. If this won't work, and if GNU or
112 OLDGNU format is allowed, use '\200' followed by base-256, or (if
113 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
114 If neither format works, use SUBSTITUTE (...) instead. Pass to
115 SUBSTITUTE the address of an 0-or-1 flag recording whether the
116 substitute value is negative. */
119 to_chars (int negative
, uintmax_t value
, size_t valsize
,
120 uintmax_t (*substitute
) (int *),
121 char *where
, size_t size
, const char *type
)
123 int base256_allowed
= (archive_format
== GNU_FORMAT
124 || archive_format
== OLDGNU_FORMAT
);
126 /* Generate the POSIX octal representation if the number fits. */
127 if (! negative
&& value
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
129 where
[size
- 1] = '\0';
130 to_octal (value
, where
, size
- 1);
133 /* Otherwise, generate the base-256 representation if we are
134 generating an old or new GNU format and if the number fits. */
135 else if (((negative
? -1 - value
: value
)
136 <= MAX_VAL_WITH_DIGITS (size
- 1, LG_256
))
139 where
[0] = negative
? -1 : 1 << (LG_256
- 1);
140 to_base256 (negative
, value
, where
+ 1, size
- 1);
143 /* Otherwise, if the number is negative, and if it would not cause
144 ambiguity on this host by confusing positive with negative
145 values, then generate the POSIX octal representation of the value
146 modulo 2**(field bits). The resulting tar file is
147 machine-dependent, since it depends on the host word size. Yuck!
148 But this is the traditional behavior. */
149 else if (negative
&& valsize
* CHAR_BIT
<= (size
- 1) * LG_8
)
151 static int warned_once
;
155 WARN ((0, 0, _("Generating negative octal headers")));
157 where
[size
- 1] = '\0';
158 to_octal (value
& MAX_VAL_WITH_DIGITS (valsize
* CHAR_BIT
, 1),
162 /* Otherwise, output a substitute value if possible (with a
163 warning), and an error message if not. */
166 uintmax_t maxval
= (base256_allowed
167 ? MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)
168 : MAX_VAL_WITH_DIGITS (size
- 1, LG_8
));
169 char valbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
170 char maxbuf
[UINTMAX_STRSIZE_BOUND
];
171 char minbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
172 char const *minval_string
;
173 char const *maxval_string
= STRINGIFY_BIGINT (maxval
, maxbuf
);
174 char const *value_string
;
178 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
179 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
188 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
193 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
198 uintmax_t sub
= substitute (&negsub
) & maxval
;
199 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? - sub
: sub
;
200 char subbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
201 char *sub_string
= STRINGIFY_BIGINT (s
, subbuf
+ 1);
204 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
205 value_string
, type
, minval_string
, maxval_string
,
207 to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
210 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
211 value_string
, type
, minval_string
, maxval_string
));
216 gid_substitute (int *negative
)
222 static gid_t gid_nobody
;
223 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
232 gid_to_chars (gid_t v
, char *p
, size_t s
)
234 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
238 major_to_chars (major_t v
, char *p
, size_t s
)
240 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
244 minor_to_chars (minor_t v
, char *p
, size_t s
)
246 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
250 mode_to_chars (mode_t v
, char *p
, size_t s
)
252 /* In the common case where the internal and external mode bits are the same,
253 and we are not using POSIX or GNU format,
254 propagate all unknown bits to the external mode.
255 This matches historical practice.
256 Otherwise, just copy the bits we know about. */
259 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
260 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
261 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
262 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
263 && archive_format
!= POSIX_FORMAT
264 && archive_format
!= GNU_FORMAT
)
272 u
= ((v
& S_ISUID
? TSUID
: 0)
273 | (v
& S_ISGID
? TSGID
: 0)
274 | (v
& S_ISVTX
? TSVTX
: 0)
275 | (v
& S_IRUSR
? TUREAD
: 0)
276 | (v
& S_IWUSR
? TUWRITE
: 0)
277 | (v
& S_IXUSR
? TUEXEC
: 0)
278 | (v
& S_IRGRP
? TGREAD
: 0)
279 | (v
& S_IWGRP
? TGWRITE
: 0)
280 | (v
& S_IXGRP
? TGEXEC
: 0)
281 | (v
& S_IROTH
? TOREAD
: 0)
282 | (v
& S_IWOTH
? TOWRITE
: 0)
283 | (v
& S_IXOTH
? TOEXEC
: 0));
285 to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
289 off_to_chars (off_t v
, char *p
, size_t s
)
291 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
295 size_to_chars (size_t v
, char *p
, size_t s
)
297 to_chars (0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "size_t");
301 time_to_chars (time_t v
, char *p
, size_t s
)
303 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
307 uid_substitute (int *negative
)
313 static uid_t uid_nobody
;
314 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
323 uid_to_chars (uid_t v
, char *p
, size_t s
)
325 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
329 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
331 to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
335 string_to_chars (char *str
, char *p
, size_t s
)
342 /* Writing routines. */
344 /* Zero out the buffer so we don't confuse ourselves with leftover
347 clear_buffer (char *buffer
)
349 memset (buffer
, 0, BLOCKSIZE
);
352 /* Write the EOT block(s). Zero at least two blocks, through the end
353 of the record. Old tar, as previous versions of GNU tar, writes
354 garbage after two zeroed blocks. */
358 union block
*pointer
= find_next_block ();
359 memset (pointer
->buffer
, 0, BLOCKSIZE
);
360 set_next_block_after (pointer
);
361 pointer
= find_next_block ();
362 memset (pointer
->buffer
, 0, available_space_after (pointer
));
363 set_next_block_after (pointer
);
366 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
368 /* FIXME: Cross recursion between start_header and write_long! */
370 static union block
*start_header (const char *, struct tar_stat_info
*);
373 write_long (const char *p
, char type
)
375 size_t size
= strlen (p
) + 1;
378 struct tar_stat_info foo
;
380 memset (&foo
, 0, sizeof foo
);
381 foo
.stat
.st_size
= size
;
383 header
= start_header ("././@LongLink", &foo
);
384 header
->header
.typeflag
= type
;
385 finish_header (header
, -1);
387 header
= find_next_block ();
389 bufsize
= available_space_after (header
);
391 while (bufsize
< size
)
393 memcpy (header
->buffer
, p
, bufsize
);
396 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
397 header
= find_next_block ();
398 bufsize
= available_space_after (header
);
400 memcpy (header
->buffer
, p
, size
);
401 memset (header
->buffer
+ size
, 0, bufsize
- size
);
402 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
405 /* Write a long link name, depending on the current archive format */
407 write_long_link (struct tar_stat_info
*st
)
409 if (archive_format
== POSIX_FORMAT
)
410 xheader_store ("linkpath", st
);
412 write_long (st
->link_name
, GNUTYPE_LONGNAME
);
415 /* NOTE: Cross recursion between start_header and write_extended */
418 write_extended (union block
*old_header
, char type
)
420 union block
*header
, hp
;
421 struct tar_stat_info foo
;
425 if (extended_header
.buffer
|| extended_header
.stk
== NULL
)
426 return old_header
; /* Prevent recursion */
428 xheader_finish (&extended_header
);
429 size
= extended_header
.size
;
430 memset (&foo
, 0, sizeof foo
);
431 foo
.stat
.st_mode
= S_IFREG
|S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
;
432 time (&foo
.stat
.st_ctime
);
433 foo
.stat
.st_atime
= foo
.stat
.st_ctime
;
434 foo
.stat
.st_mtime
= foo
.stat
.st_ctime
;
435 foo
.stat
.st_size
= size
;
437 memcpy (hp
.buffer
, old_header
, sizeof (hp
));
439 header
= start_header ("././@PaxHeader", &foo
);
440 header
->header
.typeflag
= type
;
442 finish_header (header
, -1);
444 p
= extended_header
.buffer
;
450 header
= find_next_block ();
454 memcpy (header
->buffer
, p
, len
);
456 memset (header
->buffer
+ len
, 0, BLOCKSIZE
- len
);
459 set_next_block_after (header
);
463 xheader_destroy (&extended_header
);
464 header
= find_next_block ();
465 memcpy (header
, &hp
.buffer
, sizeof (hp
.buffer
));
470 /* Header handling. */
472 /* Make a header block for the file whose stat info is st,
473 and return its address. */
476 start_header (const char *name
, struct tar_stat_info
*st
)
480 name
= safer_name_suffix (name
, 0);
481 assign_string (&st
->file_name
, name
);
483 if (sizeof header
->header
.name
<= strlen (name
))
485 if (archive_format
== POSIX_FORMAT
)
486 xheader_store ("path", st
);
488 write_long (name
, GNUTYPE_LONGNAME
);
491 header
= find_next_block ();
492 memset (header
->buffer
, 0, sizeof (union block
));
494 assign_string (¤t_stat_info
.file_name
, name
);
496 strncpy (header
->header
.name
, name
, NAME_FIELD_SIZE
);
497 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
499 /* Override some stat fields, if requested to do so. */
501 if (owner_option
!= (uid_t
) -1)
502 st
->stat
.st_uid
= owner_option
;
503 if (group_option
!= (gid_t
) -1)
504 st
->stat
.st_gid
= group_option
;
506 st
->stat
.st_mode
= ((st
->stat
.st_mode
& ~MODE_ALL
)
507 | mode_adjust (st
->stat
.st_mode
, mode_option
));
509 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
510 for a few tars and came up with the following interoperability
514 1 2 3 4 5 6 7 8 9 READER
515 . . . . . . . . . 1 = SunOS 4.2 tar
516 # . . # # . . # # 2 = NEC SVR4.0.2 tar
517 . . . # # . . # . 3 = Solaris 2.1 tar
518 . . . . . . . . . 4 = GNU tar 1.11.1
519 . . . . . . . . . 5 = HP-UX 8.07 tar
520 . . . . . . . . . 6 = Ultrix 4.1
521 . . . . . . . . . 7 = AIX 3.2
522 . . . . . . . . . 8 = Hitachi HI-UX 1.03
523 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
526 # = ``impossible file type''
528 The following mask for old archive removes the `#'s in column 4
529 above, thus making GNU tar both a universal donor and a universal
530 acceptor for Paul's test. */
532 if (archive_format
== V7_FORMAT
)
533 MODE_TO_CHARS (st
->stat
.st_mode
& MODE_ALL
, header
->header
.mode
);
535 MODE_TO_CHARS (st
->stat
.st_mode
, header
->header
.mode
);
537 if (st
->stat
.st_uid
> MAXOCTAL7
&& archive_format
== POSIX_FORMAT
)
538 xheader_store ("uid", st
);
540 UID_TO_CHARS (st
->stat
.st_uid
, header
->header
.uid
);
542 if (st
->stat
.st_gid
> MAXOCTAL7
&& archive_format
== POSIX_FORMAT
)
543 xheader_store ("gid", st
);
545 GID_TO_CHARS (st
->stat
.st_gid
, header
->header
.gid
);
547 if (st
->stat
.st_size
> MAXOCTAL11
&& archive_format
== POSIX_FORMAT
)
548 xheader_store ("size", st
);
550 OFF_TO_CHARS (st
->stat
.st_size
, header
->header
.size
);
552 TIME_TO_CHARS (st
->stat
.st_mtime
, header
->header
.mtime
);
555 if (S_ISCHR (st
->stat
.st_mode
)
556 || S_ISBLK (st
->stat
.st_mode
))
558 st
->devmajor
= major (st
->stat
.st_rdev
);
559 st
->devminor
= minor (st
->stat
.st_rdev
);
561 if (st
->devmajor
> MAXOCTAL7
&& archive_format
== POSIX_FORMAT
)
562 xheader_store ("devmajor", st
);
564 MAJOR_TO_CHARS (st
->devmajor
, header
->header
.devmajor
);
566 if (st
->devminor
> MAXOCTAL7
&& archive_format
== POSIX_FORMAT
)
567 xheader_store ("devminor", st
);
569 MAJOR_TO_CHARS (st
->devminor
, header
->header
.devminor
);
573 MAJOR_TO_CHARS (0, header
->header
.devmajor
);
574 MINOR_TO_CHARS (0, header
->header
.devminor
);
577 if (archive_format
== POSIX_FORMAT
)
579 xheader_store ("atime", st
);
580 xheader_store ("ctime", st
);
582 else if (incremental_option
)
583 if (archive_format
== OLDGNU_FORMAT
)
585 TIME_TO_CHARS (st
->stat
.st_atime
, header
->oldgnu_header
.atime
);
586 TIME_TO_CHARS (st
->stat
.st_ctime
, header
->oldgnu_header
.ctime
);
589 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
591 switch (archive_format
)
597 /* Overwrite header->header.magic and header.version in one blow. */
598 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
603 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
604 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
611 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
613 /* header->header.[ug]name are left as the empty string. */
617 uid_to_uname (st
->stat
.st_uid
, &st
->uname
);
618 gid_to_gname (st
->stat
.st_gid
, &st
->gname
);
620 if (archive_format
== POSIX_FORMAT
621 && strlen (st
->uname
) > UNAME_FIELD_SIZE
)
622 xheader_store ("uname", st
);
624 UNAME_TO_CHARS (st
->uname
, header
->header
.uname
);
626 if (archive_format
== POSIX_FORMAT
627 && strlen (st
->gname
) > GNAME_FIELD_SIZE
)
628 xheader_store ("gname", st
);
630 GNAME_TO_CHARS (st
->gname
, header
->header
.gname
);
636 /* Finish off a filled-in header block and write it out. We also
637 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
638 is not negative, is the block ordinal of the first record for this
639 file, which may be a preceding long name or long link record. */
641 finish_header (union block
*header
, off_t block_ordinal
)
647 /* Note: It is important to do this before the call to write_extended(),
648 so that the actual ustar header is printed */
650 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
651 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
652 && header
->header
.typeflag
!= XHDTYPE
653 && header
->header
.typeflag
!= XGLTYPE
)
655 /* These globals are parameters to print_header, sigh. */
657 current_header
= header
;
658 /* current_stat_info is already set up. */
659 current_format
= archive_format
;
660 print_header (block_ordinal
);
663 header
= write_extended (header
, XHDTYPE
);
665 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
669 for (i
= sizeof *header
; i
-- != 0; )
670 /* We can't use unsigned char here because of old compilers, e.g. V7. */
673 /* Fill in the checksum field. It's formatted differently from the
674 other fields: it has [6] digits, a null, then a space -- rather than
675 digits, then a null. We use to_chars.
676 The final space is already there, from
677 checksumming, and to_chars doesn't modify it.
679 This is a fast way to do:
681 sprintf(header->header.chksum, "%6o", sum); */
683 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
685 set_next_block_after (header
);
688 /* Sparse file processing. */
690 /* Takes a blockful of data and basically cruises through it to see if
691 it's made *entirely* of zeros, returning a 0 the instant it finds
692 something that is a nonzero, i.e., useful data. */
694 zero_block_p (char *buffer
)
698 for (counter
= 0; counter
< BLOCKSIZE
; counter
++)
699 if (buffer
[counter
] != '\0')
705 init_sparsearray (void)
708 sp_array_size
= SPARSES_IN_OLDGNU_HEADER
;
709 sparsearray
= xmalloc (sp_array_size
* sizeof *sparsearray
);
713 find_new_file_size (int sparses
)
717 for (i
= 0; i
< sparses
; i
++)
718 s
+= sparsearray
[i
].numbytes
;
722 /* Make one pass over the file NAME, studying where any non-zero data
723 is, that is, how far into the file each instance of data is, and
724 how many bytes are there. Save this information in the
725 sparsearray, which will later be translated into header
728 /* There is little point in trimming small amounts of null data at the head
729 and tail of blocks, only avoid dumping full null blocks. */
731 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
732 too kludgey for my taste... */
735 deal_with_sparse (char *name
, union block
*header
)
742 char buffer
[BLOCKSIZE
];
744 if (archive_format
== OLDGNU_FORMAT
)
745 header
->oldgnu_header
.isextended
= 0;
747 if (file
= open (name
, O_RDONLY
), file
< 0)
748 /* This problem will be caught later on, so just return. */
752 clear_buffer (buffer
);
756 /* Realloc the scratch area as necessary. FIXME: should reallocate
757 only at beginning of a new instance of non-zero data. */
759 if (sp_array_size
<= sparses
)
762 xrealloc (sparsearray
,
763 2 * sp_array_size
* sizeof (struct sp_array
));
767 count
= safe_read (file
, buffer
, sizeof buffer
);
771 /* Process one block. */
773 if (count
== sizeof buffer
)
775 if (zero_block_p (buffer
))
779 sparsearray
[sparses
++].numbytes
= numbytes
;
786 sparsearray
[sparses
].offset
= offset
;
792 /* Since count < sizeof buffer, we have the last bit of the file. */
794 if (!zero_block_p (buffer
))
797 sparsearray
[sparses
].offset
= offset
;
801 /* The next two lines are suggested by Andreas Degert, who says
802 they are required for trailing full blocks to be written to the
803 archive, when all zeroed. Yet, it seems to me that the case
804 does not apply. Further, at restore time, the file is not as
805 sparse as it should. So, some serious cleanup is *also* needed
806 in this area. Just one more... :-(. FIXME. */
810 /* Prepare for next block. */
813 /* FIXME: do not clear unless necessary. */
814 clear_buffer (buffer
);
818 sparsearray
[sparses
++].numbytes
= numbytes
;
821 sparsearray
[sparses
].offset
= offset
- 1;
822 sparsearray
[sparses
++].numbytes
= 1;
825 return close (file
) == 0 && 0 <= count
? sparses
: 0;
829 finish_sparse_file (int file
, off_t
*sizeleft
, off_t fullsize
, char *name
)
836 while (*sizeleft
> 0)
838 start
= find_next_block ();
839 memset (start
->buffer
, 0, BLOCKSIZE
);
840 bufsize
= sparsearray
[sparses
].numbytes
;
844 if (lseek (file
, sparsearray
[sparses
++].offset
, SEEK_SET
) < 0)
846 (ignore_failed_read_option
? seek_warn_details
: seek_error_details
)
847 (name
, sparsearray
[sparses
- 1].offset
);
851 /* If the number of bytes to be written here exceeds the size of
852 the temporary buffer, do it in steps. */
854 while (bufsize
> BLOCKSIZE
)
856 count
= safe_read (file
, start
->buffer
, BLOCKSIZE
);
859 (ignore_failed_read_option
861 : read_error_details
)
862 (name
, fullsize
- *sizeleft
, bufsize
);
867 set_next_block_after (start
);
868 start
= find_next_block ();
869 memset (start
->buffer
, 0, BLOCKSIZE
);
873 char buffer
[BLOCKSIZE
];
875 clear_buffer (buffer
);
876 count
= safe_read (file
, buffer
, bufsize
);
877 memcpy (start
->buffer
, buffer
, BLOCKSIZE
);
882 (ignore_failed_read_option
884 : read_error_details
)
885 (name
, fullsize
- *sizeleft
, bufsize
);
890 set_next_block_after (start
);
894 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
899 /* Main functions of this module. */
902 create_archive (void)
906 open_archive (ACCESS_WRITE
);
908 if (incremental_option
)
910 size_t buffer_size
= 1000;
911 char *buffer
= xmalloc (buffer_size
);
914 collect_and_sort_names ();
916 while (p
= name_from_list (), p
)
917 if (!excluded_name (p
))
918 dump_file (p
, -1, (dev_t
) 0);
921 while (p
= name_from_list (), p
)
922 if (!excluded_name (p
))
924 size_t plen
= strlen (p
);
925 if (buffer_size
<= plen
)
927 while ((buffer_size
*= 2) <= plen
)
929 buffer
= xrealloc (buffer
, buffer_size
);
931 memcpy (buffer
, p
, plen
);
932 if (! ISSLASH (buffer
[plen
- 1]))
933 buffer
[plen
++] = '/';
934 q
= gnu_list_name
->dir_contents
;
938 size_t qlen
= strlen (q
);
941 if (buffer_size
< plen
+ qlen
)
943 while ((buffer_size
*=2 ) < plen
+ qlen
)
945 buffer
= xrealloc (buffer
, buffer_size
);
947 strcpy (buffer
+ plen
, q
+ 1);
948 dump_file (buffer
, -1, (dev_t
) 0);
957 while (p
= name_next (1), p
)
958 if (!excluded_name (p
))
959 dump_file (p
, 1, (dev_t
) 0);
965 if (listed_incremental_option
)
966 write_directory_file ();
970 /* Calculate the hash of a link. */
972 hash_link (void const *entry
, unsigned n_buckets
)
974 struct link
const *link
= entry
;
975 return (uintmax_t) (link
->dev
^ link
->ino
) % n_buckets
;
978 /* Compare two links for equality. */
980 compare_links (void const *entry1
, void const *entry2
)
982 struct link
const *link1
= entry1
;
983 struct link
const *link2
= entry2
;
984 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
987 /* Table of all non-directories that we've written so far. Any time
988 we see another, we check the table and avoid dumping the data
989 again if we've done it once already. */
990 static Hash_table
*link_table
;
992 /* Dump a single file, recursing on directories. P is the file name
993 to dump. TOP_LEVEL tells whether this is a top-level call; zero
994 means no, positive means yes, and negative means the top level
995 of an incremental dump. PARENT_DEVICE is the device of P's
996 parent directory; it is examined only if TOP_LEVEL is zero.
998 Set global CURRENT_STAT_INFO to stat output for this file. */
1000 /* FIXME: One should make sure that for *every* path leading to setting
1001 exit_status to failure, a clear diagnostic has been issued. */
1004 dump_file (char *p
, int top_level
, dev_t parent_device
)
1006 union block
*header
;
1010 time_t original_ctime
;
1011 struct utimbuf restore_times
;
1012 off_t block_ordinal
= -1;
1014 /* FIXME: `header' might be used uninitialized in this
1015 function. Reported by Bruno Haible. */
1017 if (interactive_option
&& !confirm ("add", p
))
1020 if (deref_stat (dereference_option
, p
, ¤t_stat_info
.stat
) != 0)
1022 if (ignore_failed_read_option
)
1029 original_ctime
= current_stat_info
.stat
.st_ctime
;
1030 restore_times
.actime
= current_stat_info
.stat
.st_atime
;
1031 restore_times
.modtime
= current_stat_info
.stat
.st_mtime
;
1034 if (S_ISHIDDEN (current_stat_info
.stat
.st_mode
))
1036 char *new = (char *) alloca (strlen (p
) + 2);
1046 /* See if we want only new files, and check if this one is too old to
1047 put in the archive. */
1049 if ((0 < top_level
|| !incremental_option
)
1050 && !S_ISDIR (current_stat_info
.stat
.st_mode
)
1051 && current_stat_info
.stat
.st_mtime
< newer_mtime_option
1052 && (!after_date_option
|| current_stat_info
.stat
.st_ctime
< newer_ctime_option
))
1055 WARN ((0, 0, _("%s: file is unchanged; not dumped"),
1056 quotearg_colon (p
)));
1057 /* FIXME: recheck this return. */
1062 /* See if we are trying to dump the archive. */
1064 if (ar_dev
&& current_stat_info
.stat
.st_dev
== ar_dev
&& current_stat_info
.stat
.st_ino
== ar_ino
)
1066 WARN ((0, 0, _("%s: file is the archive; not dumped"),
1067 quotearg_colon (p
)));
1072 if (S_ISDIR (current_stat_info
.stat
.st_mode
))
1080 dev_t our_device
= current_stat_info
.stat
.st_dev
;
1084 directory
= savedir (p
);
1087 if (ignore_failed_read_option
)
1094 /* Build new prototype name. Ensure exactly one trailing slash. */
1097 buflen
= len
+ NAME_FIELD_SIZE
;
1098 namebuf
= xmalloc (buflen
+ 1);
1099 memcpy (namebuf
, p
, len
);
1100 while (len
>= 1 && ISSLASH (namebuf
[len
- 1]))
1102 namebuf
[len
++] = '/';
1103 namebuf
[len
] = '\0';
1105 if (! is_avoided_name (namebuf
))
1107 /* The condition above used to be "archive_format != V7_FORMAT".
1108 GNU tar was not writing directory blocks at all. Daniel Trinkle
1109 writes: ``All old versions of tar I have ever seen have
1110 correctly archived an empty directory. The really old ones I
1111 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1112 some subtle reason for the exclusion that I don't know, but the
1113 current behavior is broken.'' I do not know those subtle
1114 reasons either, so until these are reported (anew?), just allow
1115 directory blocks to be written even with old archives. */
1117 block_ordinal
= current_block_ordinal ();
1118 current_stat_info
.stat
.st_size
= 0; /* force 0 size on dir */
1120 /* FIXME: If people could really read standard archives, this
1124 = start_header (standard_option ? p : namebuf, ¤t_stat_info);
1126 but since they'd interpret DIRTYPE blocks as regular
1127 files, we'd better put the / on the name. */
1129 header
= start_header (namebuf
, ¤t_stat_info
);
1131 if (incremental_option
)
1132 header
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1133 else /* if (standard_option) */
1134 header
->header
.typeflag
= DIRTYPE
;
1136 /* If we're gnudumping, we aren't done yet so don't close it. */
1138 if (!incremental_option
)
1139 finish_header (header
, block_ordinal
);
1142 if (incremental_option
&& gnu_list_name
->dir_contents
)
1149 const char *buffer
, *p_buffer
;
1151 buffer
= gnu_list_name
->dir_contents
; /* FOO */
1154 for (p_buffer
= buffer
; *p_buffer
; )
1156 size_t size
= strlen (p_buffer
) + 1;
1161 OFF_TO_CHARS (totsize
, header
->header
.size
);
1162 finish_header (header
, block_ordinal
);
1165 while (sizeleft
> 0)
1167 if (multi_volume_option
)
1169 assign_string (&save_name
, p
);
1170 save_sizeleft
= sizeleft
;
1171 save_totsize
= totsize
;
1173 start
= find_next_block ();
1174 bufsize
= available_space_after (start
);
1175 if (sizeleft
< bufsize
)
1178 count
= bufsize
% BLOCKSIZE
;
1180 memset (start
->buffer
+ sizeleft
, 0, BLOCKSIZE
- count
);
1182 memcpy (start
->buffer
, p_buffer
, bufsize
);
1183 sizeleft
-= bufsize
;
1184 p_buffer
+= bufsize
;
1185 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1187 if (multi_volume_option
)
1188 assign_string (&save_name
, 0);
1192 /* See if we are about to recurse into a directory, and avoid doing
1193 so if the user wants that we do not descend into directories. */
1195 if (! recursion_option
)
1198 /* See if we are crossing from one file system to another, and
1199 avoid doing so if the user only wants to dump one file system. */
1201 if (one_file_system_option
&& !top_level
1202 && parent_device
!= current_stat_info
.stat
.st_dev
)
1206 _("%s: file is on a different filesystem; not dumped"),
1207 quotearg_colon (p
)));
1211 /* Now output all the files in the directory. */
1213 /* FIXME: Should speed this up by cd-ing into the dir. */
1215 for (entry
= directory
;
1216 (entrylen
= strlen (entry
)) != 0;
1217 entry
+= entrylen
+ 1)
1219 if (buflen
< len
+ entrylen
)
1221 buflen
= len
+ entrylen
;
1222 namebuf
= xrealloc (namebuf
, buflen
+ 1);
1224 strcpy (namebuf
+ len
, entry
);
1225 if (!excluded_name (namebuf
))
1226 dump_file (namebuf
, 0, our_device
);
1233 if (atime_preserve_option
)
1234 utime (p
, &restore_times
);
1237 else if (is_avoided_name (p
))
1241 /* Check for multiple links. */
1243 if (1 < current_stat_info
.stat
.st_nlink
&& link_table
)
1247 lp
.ino
= current_stat_info
.stat
.st_ino
;
1248 lp
.dev
= current_stat_info
.stat
.st_dev
;
1250 if ((dup
= hash_lookup (link_table
, &lp
)))
1252 /* We found a link. */
1253 char const *link_name
= safer_name_suffix (dup
->name
, 1);
1257 block_ordinal
= current_block_ordinal ();
1258 assign_string (¤t_stat_info
.link_name
, link_name
);
1259 if (NAME_FIELD_SIZE
<= strlen (link_name
))
1260 write_long_link (¤t_stat_info
);
1262 current_stat_info
.stat
.st_size
= 0;
1263 header
= start_header (p
, ¤t_stat_info
);
1264 strncpy (header
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1266 /* Force null termination. */
1267 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = 0;
1269 header
->header
.typeflag
= LNKTYPE
;
1270 finish_header (header
, block_ordinal
);
1272 /* FIXME: Maybe remove from table after all links found? */
1274 if (remove_files_option
&& unlink (p
) != 0)
1277 /* We dumped it, and we don't need to put it in the
1283 /* This is not a link to a previously dumped file, so dump it. */
1285 if (S_ISREG (current_stat_info
.stat
.st_mode
)
1286 || S_ISCTG (current_stat_info
.stat
.st_mode
))
1288 int f
; /* file descriptor */
1294 char isextended
= 0;
1301 /* Check the size of the file against the number of blocks
1302 allocated for it, counting both data and indirect blocks.
1303 If there is a smaller number of blocks than would be
1304 necessary to accommodate a file of this size, this is safe
1305 to say that we have a sparse file: at least one of those
1306 blocks in the file is just a useless hole. For sparse
1307 files not having more hole blocks than indirect blocks, the
1308 sparseness will go undetected. */
1310 /* Bruno Haible sent me these statistics for Linux. It seems
1311 that some filesystems count indirect blocks in st_blocks,
1312 while others do not seem to:
1314 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1315 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1316 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1317 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1319 Dick Streefland reports the previous numbers as misleading,
1320 because ext2fs use 12 direct blocks, while minix-fs uses only
1321 6 direct blocks. Dick gets:
1323 ext2 size=20480 ls listed blocks=21
1324 minix size=20480 ls listed blocks=21
1325 msdos size=20480 ls listed blocks=20
1327 It seems that indirect blocks *are* included in st_blocks.
1328 The minix filesystem does not account for phantom blocks in
1329 st_blocks, so `du' and `ls -s' give wrong results. So, the
1330 --sparse option would not work on a minix filesystem. */
1332 if (ST_NBLOCKS (current_stat_info
.stat
)
1333 < (current_stat_info
.stat
.st_size
/ ST_NBLOCKSIZE
1334 + (current_stat_info
.stat
.st_size
% ST_NBLOCKSIZE
!= 0)))
1338 block_ordinal
= current_block_ordinal ();
1339 header
= start_header (p
, ¤t_stat_info
);
1340 header
->header
.typeflag
= GNUTYPE_SPARSE
;
1343 /* Call the routine that figures out the layout of the
1344 sparse file in question. SPARSES is the index of the
1345 first unused element of the "sparsearray," i.e.,
1346 the number of elements it needed to describe the file. */
1348 sparses
= deal_with_sparse (p
, header
);
1350 /* See if we'll need an extended header later. */
1352 if (SPARSES_IN_OLDGNU_HEADER
< sparses
)
1353 header
->oldgnu_header
.isextended
= 1;
1355 /* We store the "real" file size so we can show that in
1356 case someone wants to list the archive, i.e., tar tvf
1357 <file>. It might be kind of disconcerting if the
1358 shrunken file size was the one that showed up. */
1360 OFF_TO_CHARS (current_stat_info
.stat
.st_size
,
1361 header
->oldgnu_header
.realsize
);
1363 /* This will be the new "size" of the file, i.e., the size
1364 of the file minus the blocks of holes that we're
1367 current_stat_info
.stat
.st_size
= find_new_file_size (sparses
);
1368 OFF_TO_CHARS (current_stat_info
.stat
.st_size
, header
->header
.size
);
1371 counter
< sparses
&& counter
< SPARSES_IN_OLDGNU_HEADER
;
1374 OFF_TO_CHARS (sparsearray
[counter
].offset
,
1375 header
->oldgnu_header
.sp
[counter
].offset
);
1376 SIZE_TO_CHARS (sparsearray
[counter
].numbytes
,
1377 header
->oldgnu_header
.sp
[counter
].numbytes
);
1382 sizeleft
= current_stat_info
.stat
.st_size
;
1384 /* Don't bother opening empty, world readable files. Also do not open
1385 files when archive is meant for /dev/null. */
1389 && MODE_R
== (MODE_R
& current_stat_info
.stat
.st_mode
)))
1393 f
= open (p
, O_RDONLY
| O_BINARY
);
1396 if (! top_level
&& errno
== ENOENT
)
1397 WARN ((0, 0, _("%s: File removed before we read it"),
1398 quotearg_colon (p
)));
1400 (ignore_failed_read_option
? open_warn
: open_error
) (p
);
1405 /* If the file is sparse, we've already taken care of this. */
1409 block_ordinal
= current_block_ordinal ();
1410 header
= start_header (p
, ¤t_stat_info
);
1413 /* Mark contiguous files, if we support them. */
1415 if (archive_format
!= V7_FORMAT
&& S_ISCTG (current_stat_info
.stat
.st_mode
))
1416 header
->header
.typeflag
= CONTTYPE
;
1418 isextended
= header
->oldgnu_header
.isextended
;
1419 save_typeflag
= header
->header
.typeflag
;
1420 finish_header (header
, block_ordinal
);
1423 int sparses_emitted
= SPARSES_IN_OLDGNU_HEADER
;
1428 exhdr
= find_next_block ();
1429 memset (exhdr
->buffer
, 0, BLOCKSIZE
);
1431 (i
< SPARSES_IN_SPARSE_HEADER
1432 && sparses_emitted
+ i
< sparses
);
1435 SIZE_TO_CHARS (sparsearray
[sparses_emitted
+ i
].numbytes
,
1436 exhdr
->sparse_header
.sp
[i
].numbytes
);
1437 OFF_TO_CHARS (sparsearray
[sparses_emitted
+ i
].offset
,
1438 exhdr
->sparse_header
.sp
[i
].offset
);
1440 set_next_block_after (exhdr
);
1441 sparses_emitted
+= i
;
1442 if (sparses
== sparses_emitted
)
1444 exhdr
->sparse_header
.isextended
= 1;
1447 if (save_typeflag
== GNUTYPE_SPARSE
)
1450 || finish_sparse_file (f
, &sizeleft
,
1451 current_stat_info
.stat
.st_size
, p
))
1455 while (sizeleft
> 0)
1457 if (multi_volume_option
)
1459 assign_string (&save_name
, p
);
1460 save_sizeleft
= sizeleft
;
1461 save_totsize
= current_stat_info
.stat
.st_size
;
1463 start
= find_next_block ();
1465 bufsize
= available_space_after (start
);
1467 if (sizeleft
< bufsize
)
1469 /* Last read -- zero out area beyond. */
1472 count
= bufsize
% BLOCKSIZE
;
1474 memset (start
->buffer
+ sizeleft
, 0, BLOCKSIZE
- count
);
1479 count
= safe_read (f
, start
->buffer
, bufsize
);
1482 (ignore_failed_read_option
1484 : read_error_details
)
1485 (p
, current_stat_info
.stat
.st_size
- sizeleft
, bufsize
);
1490 /* This is nonportable (the type of set_next_block_after's arg). */
1492 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1495 if (count
!= bufsize
)
1497 char buf
[UINTMAX_STRSIZE_BOUND
];
1498 memset (start
->buffer
+ count
, 0, bufsize
- count
);
1500 _("%s: File shrank by %s bytes; padding with zeros"),
1502 STRINGIFY_BIGINT (sizeleft
, buf
)));
1503 if (! ignore_failed_read_option
)
1504 exit_status
= TAREXIT_FAILURE
;
1505 goto padit
; /* short read */
1509 if (multi_volume_option
)
1510 assign_string (&save_name
, 0);
1514 struct stat final_stat
;
1515 if (fstat (f
, &final_stat
) != 0)
1517 if (ignore_failed_read_option
)
1522 else if (final_stat
.st_ctime
!= original_ctime
)
1524 char const *qp
= quotearg_colon (p
);
1525 WARN ((0, 0, _("%s: file changed as we read it"), qp
));
1529 if (ignore_failed_read_option
)
1534 if (atime_preserve_option
)
1535 utime (p
, &restore_times
);
1537 if (remove_files_option
)
1539 if (unlink (p
) == -1)
1542 goto file_was_dumped
;
1544 /* File shrunk or gave error, pad out tape to match the size we
1545 specified in the header. */
1548 while (sizeleft
> 0)
1550 save_sizeleft
= sizeleft
;
1551 start
= find_next_block ();
1552 memset (start
->buffer
, 0, BLOCKSIZE
);
1553 set_next_block_after (start
);
1554 sizeleft
-= BLOCKSIZE
;
1556 if (multi_volume_option
)
1557 assign_string (&save_name
, 0);
1561 if (atime_preserve_option
)
1562 utime (p
, &restore_times
);
1564 goto file_was_dumped
;
1566 #ifdef HAVE_READLINK
1567 else if (S_ISLNK (current_stat_info
.stat
.st_mode
))
1571 size_t linklen
= current_stat_info
.stat
.st_size
;
1572 if (linklen
!= current_stat_info
.stat
.st_size
|| linklen
+ 1 == 0)
1574 buffer
= (char *) alloca (linklen
+ 1);
1575 size
= readlink (p
, buffer
, linklen
+ 1);
1578 if (ignore_failed_read_option
)
1584 buffer
[size
] = '\0';
1585 assign_string (¤t_stat_info
.link_name
, buffer
);
1586 if (size
>= NAME_FIELD_SIZE
)
1587 write_long_link (¤t_stat_info
);
1589 block_ordinal
= current_block_ordinal ();
1590 current_stat_info
.stat
.st_size
= 0; /* force 0 size on symlink */
1591 header
= start_header (p
, ¤t_stat_info
);
1592 strncpy (header
->header
.linkname
, buffer
, NAME_FIELD_SIZE
);
1593 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = '\0';
1594 header
->header
.typeflag
= SYMTYPE
;
1595 finish_header (header
, block_ordinal
);
1596 /* nothing more to do to it */
1598 if (remove_files_option
)
1600 if (unlink (p
) == -1)
1603 goto file_was_dumped
;
1606 else if (S_ISCHR (current_stat_info
.stat
.st_mode
))
1608 else if (S_ISBLK (current_stat_info
.stat
.st_mode
))
1610 else if (S_ISFIFO (current_stat_info
.stat
.st_mode
))
1612 else if (S_ISSOCK (current_stat_info
.stat
.st_mode
))
1614 WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1617 else if (S_ISDOOR (current_stat_info
.stat
.st_mode
))
1619 WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1626 if (archive_format
== V7_FORMAT
)
1629 block_ordinal
= current_block_ordinal ();
1630 current_stat_info
.stat
.st_size
= 0; /* force 0 size */
1631 header
= start_header (p
, ¤t_stat_info
);
1632 header
->header
.typeflag
= type
;
1634 if (type
!= FIFOTYPE
)
1636 MAJOR_TO_CHARS (major (current_stat_info
.stat
.st_rdev
), header
->header
.devmajor
);
1637 MINOR_TO_CHARS (minor (current_stat_info
.stat
.st_rdev
), header
->header
.devminor
);
1640 finish_header (header
, block_ordinal
);
1641 if (remove_files_option
)
1643 if (unlink (p
) == -1)
1646 goto file_was_dumped
;
1649 WARN ((0, 0, _("%s: Unknown file type; file ignored"),
1650 quotearg_colon (p
)));
1651 if (! ignore_failed_read_option
)
1652 exit_status
= TAREXIT_FAILURE
;
1656 if (1 < current_stat_info
.stat
.st_nlink
)
1659 struct link
*lp
= xmalloc (offsetof (struct link
, name
)
1661 lp
->ino
= current_stat_info
.stat
.st_ino
;
1662 lp
->dev
= current_stat_info
.stat
.st_dev
;
1663 lp
->nlink
= current_stat_info
.stat
.st_nlink
;
1664 strcpy (lp
->name
, p
);
1667 || (link_table
= hash_initialize (0, 0, hash_link
,
1669 && (dup
= hash_insert (link_table
, lp
))))
1679 /* For each dumped file, check if all its links were dumped. Emit
1680 warnings if it is not so. */
1689 for (lp
= hash_get_first (link_table
); lp
;
1690 lp
= hash_get_next (link_table
, lp
))
1694 WARN ((0, 0, _("Missing links to '%s'.\n"), lp
->name
));