1 /* Create a tar archive.
2 Copyright 1985, 92, 93, 94, 96, 97, 1999 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-08-25.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
43 extern struct name
*gnu_list_name
;
45 /* This module is the only one that cares about `struct link's. */
56 static struct link
*linklist
; /* points to first link in list */
58 /* Base 64 digits; see Internet RFC 2045 Table 1. */
59 char const base_64_digits
[64] =
61 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
62 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
63 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
64 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
65 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
67 #define base_8_digits (base_64_digits + 26 * 2)
69 /* The maximum uintmax_t value that can be represented with DIGITS digits,
70 assuming that each digit is BITS_PER_DIGIT wide. */
71 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
72 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
73 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
76 /* Convert VALUE to a representation suitable for tar headers,
77 using base 1 << BITS_PER_DIGIT.
78 Use the digits in DIGIT_CHAR[0] ... DIGIT_CHAR[base - 1].
79 Output to buffer WHERE with size SIZE.
80 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
83 to_base (uintmax_t value
, int bits_per_digit
, char const *digit_char
,
84 char *where
, size_t size
)
88 unsigned digit_mask
= (1 << bits_per_digit
) - 1;
92 where
[--i
] = digit_char
[v
& digit_mask
];
98 /* NEGATIVE is nonzero if VALUE was negative before being cast to
99 uintmax_t; its original bitpattern can be deduced from VALSIZE, its
100 original size before casting. Convert VALUE to external form,
101 using SUBSTITUTE (...) if VALUE won't fit. Output to buffer WHERE
102 with size SIZE. TYPE is the kind of value being output (useful for
103 diagnostics). Prefer the POSIX format of SIZE - 1 octal digits
104 (with leading zero digits), followed by '\0'. If this won't work,
105 and if GNU format is allowed, use '+' or '-' followed by SIZE - 1
106 base-64 digits. If neither format works, use SUBSTITUTE (...)
107 instead. Pass to SUBSTITUTE the address of an 0-or-1 flag
108 recording whether the substitute value is negative. */
111 to_chars (int negative
, uintmax_t value
, size_t valsize
,
112 uintmax_t (*substitute
) PARAMS ((int *)),
113 char *where
, size_t size
, const char *type
)
115 uintmax_t v
= negative
? -value
: value
;
117 /* Generate the POSIX octal representation if the number fits. */
118 if (! negative
&& v
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
120 where
[size
- 1] = '\0';
121 to_base (v
, LG_8
, base_8_digits
, where
, size
- 1);
124 /* Otherwise, generate the GNU base-64 representation if we are
125 generating an old or new GNU format and if the number fits. */
126 else if (v
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_64
)
127 && (archive_format
== GNU_FORMAT
128 || archive_format
== OLDGNU_FORMAT
))
130 where
[0] = negative
? '-' : '+';
131 to_base (v
, LG_64
, base_64_digits
, where
+ 1, size
- 1);
134 /* Otherwise, if the number is negative, and if it would not cause
135 ambiguity on this host by confusing positive with negative
136 values, then generate the POSIX octal representation of the value
137 modulo 2**(field bits). The resulting tar file is
138 machine-dependent, since it depends on the host word size. Yuck!
139 But this is the traditional behavior. */
140 else if (negative
&& valsize
* CHAR_BIT
<= (size
- 1) * LG_8
)
142 where
[size
- 1] = '\0';
143 to_base (value
& MAX_VAL_WITH_DIGITS (valsize
* CHAR_BIT
, 1),
144 LG_8
, base_8_digits
, where
, size
- 1);
147 /* Otherwise, output a substitute value if possible (with a
148 warning), and an error message if not. */
151 uintmax_t maxval
= (archive_format
== GNU_FORMAT
152 ? MAX_VAL_WITH_DIGITS (size
- 1, LG_64
)
153 : MAX_VAL_WITH_DIGITS (size
- 1, LG_8
));
154 char buf1
[UINTMAX_STRSIZE_BOUND
+ 1];
155 char buf2
[UINTMAX_STRSIZE_BOUND
+ 1];
156 char buf3
[UINTMAX_STRSIZE_BOUND
+ 1];
157 char *value_string
= STRINGIFY_BIGINT (v
, buf1
+ 1);
158 char *maxval_string
= STRINGIFY_BIGINT (maxval
, buf2
+ 1);
159 char const *minval_string
=
160 (archive_format
== GNU_FORMAT
162 : (maxval_string
[-1] = '-', maxval_string
- 1));
164 *--value_string
= '-';
168 uintmax_t sub
= substitute (&negsub
) & maxval
;
169 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? -sub
: sub
;
170 char *sub_string
= STRINGIFY_BIGINT (s
, buf3
+ 1);
173 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
174 value_string
, type
, minval_string
, maxval_string
,
176 to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
179 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
180 value_string
, type
, minval_string
, maxval_string
));
185 gid_substitute (int *negative
)
191 static gid_t gid_nobody
;
192 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
201 gid_to_chars (gid_t v
, char *p
, size_t s
)
203 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
207 major_to_chars (major_t v
, char *p
, size_t s
)
209 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
213 minor_to_chars (minor_t v
, char *p
, size_t s
)
215 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
219 mode_to_chars (mode_t v
, char *p
, size_t s
)
221 /* In the common case where the internal and external mode bits are the same,
222 propagate all unknown bits to the external mode.
223 This matches historical practice.
224 Otherwise, just copy the bits we know about. */
227 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
228 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
229 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
230 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
)
238 u
= ((v
& S_ISUID
? TSUID
: 0)
239 | (v
& S_ISGID
? TSGID
: 0)
240 | (v
& S_ISVTX
? TSVTX
: 0)
241 | (v
& S_IRUSR
? TUREAD
: 0)
242 | (v
& S_IWUSR
? TUWRITE
: 0)
243 | (v
& S_IXUSR
? TUEXEC
: 0)
244 | (v
& S_IRGRP
? TGREAD
: 0)
245 | (v
& S_IWGRP
? TGWRITE
: 0)
246 | (v
& S_IXGRP
? TGEXEC
: 0)
247 | (v
& S_IROTH
? TOREAD
: 0)
248 | (v
& S_IWOTH
? TOWRITE
: 0)
249 | (v
& S_IXOTH
? TOEXEC
: 0));
251 to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
255 off_to_chars (off_t v
, char *p
, size_t s
)
257 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
261 size_to_chars (size_t v
, char *p
, size_t s
)
263 to_chars (0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "size_t");
267 time_to_chars (time_t v
, char *p
, size_t s
)
269 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
273 uid_substitute (int *negative
)
279 static uid_t uid_nobody
;
280 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
289 uid_to_chars (uid_t v
, char *p
, size_t s
)
291 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
295 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
297 to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
300 /* Writing routines. */
302 /*-----------------------------------------------------------------------.
303 | Just zeroes out the buffer so we don't confuse ourselves with leftover |
305 `-----------------------------------------------------------------------*/
308 clear_buffer (char *buffer
)
310 memset (buffer
, 0, BLOCKSIZE
);
313 /*-------------------------------------------------------------------------.
314 | Write the EOT block(s). We zero at least two blocks, through |
315 | the end of the record. Old tar, as previous versions of GNU tar, writes |
316 | garbage after two zeroed blocks. |
317 `-------------------------------------------------------------------------*/
322 union block
*pointer
= find_next_block ();
323 memset (pointer
->buffer
, 0, BLOCKSIZE
);
324 set_next_block_after (pointer
);
325 pointer
= find_next_block ();
326 memset (pointer
->buffer
, 0, available_space_after (pointer
));
327 set_next_block_after (pointer
);
330 /*-----------------------------------------------------.
331 | Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. |
332 `-----------------------------------------------------*/
334 /* FIXME: Cross recursion between start_header and write_long! */
336 static union block
*start_header
PARAMS ((const char *, struct stat
*));
339 write_long (const char *p
, char type
)
341 size_t size
= strlen (p
) + 1;
346 memset (&foo
, 0, sizeof foo
);
349 header
= start_header ("././@LongLink", &foo
);
350 header
->header
.typeflag
= type
;
351 finish_header (header
);
353 header
= find_next_block ();
355 bufsize
= available_space_after (header
);
357 while (bufsize
< size
)
359 memcpy (header
->buffer
, p
, bufsize
);
362 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
363 header
= find_next_block ();
364 bufsize
= available_space_after (header
);
366 memcpy (header
->buffer
, p
, size
);
367 memset (header
->buffer
+ size
, 0, bufsize
- size
);
368 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
371 /* Header handling. */
373 /*---------------------------------------------------------------------.
374 | Make a header block for the file name whose stat info is st. Return |
375 | header pointer for success, zero if the name is too long. |
376 `---------------------------------------------------------------------*/
379 start_header (const char *name
, struct stat
*st
)
383 if (!absolute_names_option
)
385 size_t prefix_len
= FILESYSTEM_PREFIX_LEN (name
);
389 static int warned_once
;
393 WARN ((0, 0, _("Removing `%.*s' prefix from archive names"),
394 (int) prefix_len
, name
));
401 static int warned_once
;
405 WARN ((0, 0, _("Removing leading `/' from archive names")));
411 /* Check the file name and put it in the block. */
413 if (sizeof header
->header
.name
<= strlen (name
))
414 write_long (name
, GNUTYPE_LONGNAME
);
415 header
= find_next_block ();
416 memset (header
->buffer
, 0, sizeof (union block
));
418 assign_string (¤t_file_name
, name
);
420 strncpy (header
->header
.name
, name
, NAME_FIELD_SIZE
);
421 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
423 /* Override some stat fields, if requested to do so. */
425 if (owner_option
!= (uid_t
) -1)
426 st
->st_uid
= owner_option
;
427 if (group_option
!= (gid_t
) -1)
428 st
->st_gid
= group_option
;
430 st
->st_mode
= ((st
->st_mode
& ~MODE_ALL
)
431 | mode_adjust (st
->st_mode
, mode_option
));
433 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
434 for a few tars and came up with the following interoperability
438 1 2 3 4 5 6 7 8 9 READER
439 . . . . . . . . . 1 = SunOS 4.2 tar
440 # . . # # . . # # 2 = NEC SVR4.0.2 tar
441 . . . # # . . # . 3 = Solaris 2.1 tar
442 . . . . . . . . . 4 = GNU tar 1.11.1
443 . . . . . . . . . 5 = HP-UX 8.07 tar
444 . . . . . . . . . 6 = Ultrix 4.1
445 . . . . . . . . . 7 = AIX 3.2
446 . . . . . . . . . 8 = Hitachi HI-UX 1.03
447 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
450 # = ``impossible file type''
452 The following mask for old archive removes the `#'s in column 4
453 above, thus making GNU tar both a universal donor and a universal
454 acceptor for Paul's test. */
456 if (archive_format
== V7_FORMAT
)
457 MODE_TO_CHARS (st
->st_mode
& MODE_ALL
, header
->header
.mode
);
459 MODE_TO_CHARS (st
->st_mode
, header
->header
.mode
);
461 UID_TO_CHARS (st
->st_uid
, header
->header
.uid
);
462 GID_TO_CHARS (st
->st_gid
, header
->header
.gid
);
463 OFF_TO_CHARS (st
->st_size
, header
->header
.size
);
464 TIME_TO_CHARS (st
->st_mtime
, header
->header
.mtime
);
466 if (incremental_option
)
467 if (archive_format
== OLDGNU_FORMAT
)
469 TIME_TO_CHARS (st
->st_atime
, header
->oldgnu_header
.atime
);
470 TIME_TO_CHARS (st
->st_ctime
, header
->oldgnu_header
.ctime
);
473 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
475 switch (archive_format
)
481 /* Overwrite header->header.magic and header.version in one blow. */
482 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
487 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
488 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
495 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
497 /* header->header.[ug]name are left as the empty string. */
501 uid_to_uname (st
->st_uid
, header
->header
.uname
);
502 gid_to_gname (st
->st_gid
, header
->header
.gname
);
508 /*-------------------------------------------------------------------------.
509 | Finish off a filled-in header block and write it out. We also print the |
510 | file name and/or full info if verbose is on. |
511 `-------------------------------------------------------------------------*/
514 finish_header (union block
*header
)
520 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
524 for (i
= sizeof *header
; i
-- != 0; )
525 /* We can't use unsigned char here because of old compilers, e.g. V7. */
528 /* Fill in the checksum field. It's formatted differently from the
529 other fields: it has [6] digits, a null, then a space -- rather than
530 digits, then a null. We use to_chars.
531 The final space is already there, from
532 checksumming, and to_chars doesn't modify it.
534 This is a fast way to do:
536 sprintf(header->header.chksum, "%6o", sum); */
538 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
540 set_next_block_after (header
);
543 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
544 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
)
546 /* These globals are parameters to print_header, sigh. */
548 current_header
= header
;
549 /* current_stat is already set up. */
550 current_format
= archive_format
;
555 /* Sparse file processing. */
557 /*-------------------------------------------------------------------------.
558 | Takes a blockful of data and basically cruises through it to see if it's |
559 | made *entirely* of zeros, returning a 0 the instant it finds something |
560 | that is a nonzero, i.e., useful data. |
561 `-------------------------------------------------------------------------*/
564 zero_block_p (char *buffer
)
568 for (counter
= 0; counter
< BLOCKSIZE
; counter
++)
569 if (buffer
[counter
] != '\0')
579 init_sparsearray (void)
585 /* Make room for our scratch space -- initially is 10 elts long. */
587 sparsearray
= xmalloc (sp_array_size
* sizeof (struct sp_array
));
588 for (counter
= 0; counter
< sp_array_size
; counter
++)
590 sparsearray
[counter
].offset
= 0;
591 sparsearray
[counter
].numbytes
= 0;
600 find_new_file_size (off_t
*filesize
, int highest_index
)
606 sparsearray
[counter
].numbytes
&& counter
<= highest_index
;
608 *filesize
+= sparsearray
[counter
].numbytes
;
611 /*-----------------------------------------------------------------------.
612 | Make one pass over the file NAME, studying where any non-zero data is, |
613 | that is, how far into the file each instance of data is, and how many |
614 | bytes are there. Save this information in the sparsearray, which will |
615 | later be translated into header information. |
616 `-----------------------------------------------------------------------*/
618 /* There is little point in trimming small amounts of null data at the head
619 and tail of blocks, only avoid dumping full null blocks. */
621 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
622 too kludgey for my taste... */
625 deal_with_sparse (char *name
, union block
*header
)
630 int sparse_index
= 0;
632 char buffer
[BLOCKSIZE
];
634 if (archive_format
== OLDGNU_FORMAT
)
635 header
->oldgnu_header
.isextended
= 0;
637 if (file
= open (name
, O_RDONLY
), file
< 0)
638 /* This problem will be caught later on, so just return. */
642 clear_buffer (buffer
);
644 while (count
= safe_read (file
, buffer
, sizeof buffer
), count
!= 0)
646 /* Realloc the scratch area as necessary. FIXME: should reallocate
647 only at beginning of a new instance of non-zero data. */
649 if (sparse_index
> sp_array_size
- 1)
652 xrealloc (sparsearray
,
653 2 * sp_array_size
* sizeof (struct sp_array
));
657 /* Process one block. */
659 if (count
== sizeof buffer
)
661 if (zero_block_p (buffer
))
665 sparsearray
[sparse_index
++].numbytes
= numbytes
;
672 sparsearray
[sparse_index
].offset
= offset
;
678 /* Since count < sizeof buffer, we have the last bit of the file. */
680 if (!zero_block_p (buffer
))
683 sparsearray
[sparse_index
].offset
= offset
;
687 /* The next two lines are suggested by Andreas Degert, who says
688 they are required for trailing full blocks to be written to the
689 archive, when all zeroed. Yet, it seems to me that the case
690 does not apply. Further, at restore time, the file is not as
691 sparse as it should. So, some serious cleanup is *also* needed
692 in this area. Just one more... :-(. FIXME. */
696 /* Prepare for next block. */
699 /* FIXME: do not clear unless necessary. */
700 clear_buffer (buffer
);
704 sparsearray
[sparse_index
++].numbytes
= numbytes
;
707 sparsearray
[sparse_index
].offset
= offset
- 1;
708 sparsearray
[sparse_index
++].numbytes
= 1;
712 return sparse_index
- 1;
720 finish_sparse_file (int file
, off_t
*sizeleft
, off_t fullsize
, char *name
)
724 int sparse_index
= 0;
727 while (*sizeleft
> 0)
729 start
= find_next_block ();
730 memset (start
->buffer
, 0, BLOCKSIZE
);
731 bufsize
= sparsearray
[sparse_index
].numbytes
;
734 /* We blew it, maybe. */
735 char buf1
[UINTMAX_STRSIZE_BOUND
];
736 char buf2
[UINTMAX_STRSIZE_BOUND
];
738 ERROR ((0, 0, _("Wrote %s of %s bytes to file %s"),
739 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf1
),
740 STRINGIFY_BIGINT (fullsize
, buf2
),
745 if (lseek (file
, sparsearray
[sparse_index
++].offset
, SEEK_SET
) < 0)
747 char buf
[UINTMAX_STRSIZE_BOUND
];
748 ERROR ((0, errno
, _("lseek error at byte %s in file %s"),
749 STRINGIFY_BIGINT (sparsearray
[sparse_index
- 1].offset
, buf
),
754 /* If the number of bytes to be written here exceeds the size of
755 the temporary buffer, do it in steps. */
757 while (bufsize
> BLOCKSIZE
)
762 count
= safe_read (file
, start
->buffer
+ amount_read
,
763 BLOCKSIZE
- amount_read
);
764 bufsize
-= BLOCKSIZE
- amount_read
;
766 set_next_block_after (start
);
767 start
= find_next_block ();
768 memset (start
->buffer
, 0, BLOCKSIZE
);
771 /* Store the data. */
773 count
= safe_read (file
, start
->buffer
, BLOCKSIZE
);
776 char buf
[UINTMAX_STRSIZE_BOUND
];
778 _("Read error at byte %s, reading %lu bytes, in file %s"),
779 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
780 (unsigned long) bufsize
, name
));
785 set_next_block_after (start
);
786 start
= find_next_block ();
787 memset (start
->buffer
, 0, BLOCKSIZE
);
791 char buffer
[BLOCKSIZE
];
793 clear_buffer (buffer
);
794 count
= safe_read (file
, buffer
, bufsize
);
795 memcpy (start
->buffer
, buffer
, BLOCKSIZE
);
800 char buf
[UINTMAX_STRSIZE_BOUND
];
803 _("Read error at byte %s, reading %lu bytes, in file %s"),
804 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
805 (unsigned long) bufsize
, name
));
809 if (amount_read
>= BLOCKSIZE
)
812 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
813 if (count
!= bufsize
)
816 _("File %s shrunk, padding with zeros"),
820 start
= find_next_block ();
823 amount_read
+= bufsize
;
826 set_next_block_after (start
);
831 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
836 /* Main functions of this module. */
843 create_archive (void)
847 open_archive (ACCESS_WRITE
);
849 if (incremental_option
)
851 char *buffer
= xmalloc (PATH_MAX
);
855 collect_and_sort_names ();
857 while (p
= name_from_list (), p
)
858 if (!excluded_name (p
))
859 dump_file (p
, (dev_t
) -1, 1);
862 while (p
= name_from_list (), p
)
863 if (!excluded_name (p
))
866 if (p
[strlen (p
) - 1] != '/')
867 strcat (buffer
, "/");
868 bufp
= buffer
+ strlen (buffer
);
869 q
= gnu_list_name
->dir_contents
;
871 for (; *q
; q
+= strlen (q
) + 1)
874 strcpy (bufp
, q
+ 1);
875 dump_file (buffer
, (dev_t
) -1, 1);
882 while (p
= name_next (1), p
)
883 if (!excluded_name (p
))
884 dump_file (p
, (dev_t
) -1, 1);
890 if (listed_incremental_option
)
894 /*----------------------------------------------------------------------.
895 | Dump a single file. Recurse on directories. Result is nonzero for |
896 | success. P is file name to dump. PARENT_DEVICE is device our parent |
897 | directory was on. TOP_LEVEL tells wether we are a toplevel call. |
899 | Sets global CURRENT_STAT to stat output for this file. |
900 `----------------------------------------------------------------------*/
902 /* FIXME: One should make sure that for *every* path leading to setting
903 exit_status to failure, a clear diagnostic has been issued. */
906 dump_file (char *p
, dev_t parent_device
, int top_level
)
912 struct utimbuf restore_times
;
915 /* FIXME: `header' and `upperbound' might be used uninitialized in this
916 function. Reported by Bruno Haible. */
918 if (interactive_option
&& !confirm ("add", p
))
921 /* Use stat if following (rather than dumping) 4.2BSD's symbolic links.
922 Otherwise, use lstat (which falls back to stat if no symbolic links). */
924 if (dereference_option
!= 0
925 #if STX_HIDDEN && !_LARGE_FILES /* AIX */
926 ? statx (p
, ¤t_stat
, STATSIZE
, STX_HIDDEN
)
927 : statx (p
, ¤t_stat
, STATSIZE
, STX_HIDDEN
| STX_LINK
)
929 ? stat (p
, ¤t_stat
) : lstat (p
, ¤t_stat
)
933 WARN ((0, errno
, _("Cannot add file %s"), p
));
934 if (!ignore_failed_read_option
)
935 exit_status
= TAREXIT_FAILURE
;
939 restore_times
.actime
= current_stat
.st_atime
;
940 restore_times
.modtime
= current_stat
.st_mtime
;
941 restore_size
= current_stat
.st_size
;
944 if (S_ISHIDDEN (current_stat
.st_mode
))
946 char *new = alloca (strlen (p
) + 2);
956 /* See if we want only new files, and check if this one is too old to
957 put in the archive. */
959 if ((!incremental_option
|| listed_incremental_option
)
960 && !S_ISDIR (current_stat
.st_mode
)
961 && current_stat
.st_mtime
< newer_mtime_option
962 && (!after_date_option
|| current_stat
.st_ctime
< newer_ctime_option
))
964 if (!listed_incremental_option
&& parent_device
== (dev_t
) -1)
965 WARN ((0, 0, _("%s: is unchanged; not dumped"), p
));
966 /* FIXME: recheck this return. */
971 /* See if we are trying to dump the archive. */
973 if (ar_dev
&& current_stat
.st_dev
== ar_dev
&& current_stat
.st_ino
== ar_ino
)
975 WARN ((0, 0, _("%s is the archive; not dumped"), p
));
980 /* Check for multiple links.
982 We maintain a list of all such files that we've written so far. Any
983 time we see another, we check the list and avoid dumping the data
984 again if we've done it once already. */
986 if (current_stat
.st_nlink
> 1
987 && (S_ISREG (current_stat
.st_mode
)
988 || S_ISCTG (current_stat
.st_mode
)
989 || S_ISCHR (current_stat
.st_mode
)
990 || S_ISBLK (current_stat
.st_mode
)
991 || S_ISFIFO (current_stat
.st_mode
)))
995 /* FIXME: First quick and dirty. Hashing, etc later. */
997 for (lp
= linklist
; lp
; lp
= lp
->next
)
998 if (lp
->ino
== current_stat
.st_ino
&& lp
->dev
== current_stat
.st_dev
)
1000 char *link_name
= lp
->name
;
1002 /* We found a link. */
1004 while (!absolute_names_option
&& *link_name
== '/')
1006 static int warned_once
;
1010 WARN ((0, 0, _("Removing leading `/' from link names")));
1014 if (strlen (link_name
) >= NAME_FIELD_SIZE
)
1015 write_long (link_name
, GNUTYPE_LONGLINK
);
1016 assign_string (¤t_link_name
, link_name
);
1018 current_stat
.st_size
= 0;
1019 header
= start_header (p
, ¤t_stat
);
1022 exit_status
= TAREXIT_FAILURE
;
1025 strncpy (header
->header
.linkname
,
1026 link_name
, NAME_FIELD_SIZE
);
1028 /* Force null truncated. */
1030 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = 0;
1032 header
->header
.typeflag
= LNKTYPE
;
1033 finish_header (header
);
1035 /* FIXME: Maybe remove from list after all links found? */
1037 if (remove_files_option
)
1038 if (unlink (p
) == -1)
1039 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1045 /* Not found. Add it to the list of possible links. */
1047 lp
= xmalloc (sizeof (struct link
) + strlen (p
));
1048 lp
->ino
= current_stat
.st_ino
;
1049 lp
->dev
= current_stat
.st_dev
;
1050 strcpy (lp
->name
, p
);
1051 lp
->next
= linklist
;
1055 /* This is not a link to a previously dumped file, so dump it. */
1057 if (S_ISREG (current_stat
.st_mode
)
1058 || S_ISCTG (current_stat
.st_mode
))
1060 int f
; /* file descriptor */
1066 char isextended
= 0;
1073 /* Check the size of the file against the number of blocks
1074 allocated for it, counting both data and indirect blocks.
1075 If there is a smaller number of blocks that would be
1076 necessary to accommodate a file of this size, this is safe
1077 to say that we have a sparse file: at least one of those
1078 blocks in the file is just a useless hole. For sparse
1079 files not having more hole blocks than indirect blocks, the
1080 sparseness will go undetected. */
1082 /* Bruno Haible sent me these statistics for Linux. It seems
1083 that some filesystems count indirect blocks in st_blocks,
1084 while others do not seem to:
1086 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1087 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1088 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1089 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1091 Dick Streefland reports the previous numbers as misleading,
1092 because ext2fs use 12 direct blocks, while minix-fs uses only
1093 6 direct blocks. Dick gets:
1095 ext2 size=20480 ls listed blocks=21
1096 minix size=20480 ls listed blocks=21
1097 msdos size=20480 ls listed blocks=20
1099 It seems that indirect blocks *are* included in st_blocks.
1100 The minix filesystem does not account for phantom blocks in
1101 st_blocks, so `du' and `ls -s' give wrong results. So, the
1102 --sparse option would not work on a minix filesystem. */
1104 if (ST_NBLOCKS (current_stat
)
1105 < (current_stat
.st_size
/ ST_NBLOCKSIZE
1106 + (current_stat
.st_size
% ST_NBLOCKSIZE
!= 0)))
1108 off_t filesize
= current_stat
.st_size
;
1111 header
= start_header (p
, ¤t_stat
);
1114 exit_status
= TAREXIT_FAILURE
;
1117 header
->header
.typeflag
= GNUTYPE_SPARSE
;
1120 /* Call the routine that figures out the layout of the
1121 sparse file in question. UPPERBOUND is the index of the
1122 last element of the "sparsearray," i.e., the number of
1123 elements it needed to describe the file. */
1125 upperbound
= deal_with_sparse (p
, header
);
1127 /* See if we'll need an extended header later. */
1129 if (upperbound
> SPARSES_IN_OLDGNU_HEADER
- 1)
1130 header
->oldgnu_header
.isextended
= 1;
1132 /* We store the "real" file size so we can show that in
1133 case someone wants to list the archive, i.e., tar tvf
1134 <file>. It might be kind of disconcerting if the
1135 shrunken file size was the one that showed up. */
1137 OFF_TO_CHARS (current_stat
.st_size
,
1138 header
->oldgnu_header
.realsize
);
1140 /* This will be the new "size" of the file, i.e., the size
1141 of the file minus the blocks of holes that we're
1144 find_new_file_size (&filesize
, upperbound
);
1145 current_stat
.st_size
= filesize
;
1146 OFF_TO_CHARS (filesize
, header
->header
.size
);
1148 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
1150 if (!sparsearray
[counter
].numbytes
)
1153 OFF_TO_CHARS (sparsearray
[counter
].offset
,
1154 header
->oldgnu_header
.sp
[counter
].offset
);
1155 SIZE_TO_CHARS (sparsearray
[counter
].numbytes
,
1156 header
->oldgnu_header
.sp
[counter
].numbytes
);
1162 upperbound
= SPARSES_IN_OLDGNU_HEADER
- 1;
1164 sizeleft
= current_stat
.st_size
;
1166 /* Don't bother opening empty, world readable files. Also do not open
1167 files when archive is meant for /dev/null. */
1171 && MODE_R
== (MODE_R
& current_stat
.st_mode
)))
1175 f
= open (p
, O_RDONLY
| O_BINARY
);
1178 WARN ((0, errno
, _("Cannot add file %s"), p
));
1179 if (!ignore_failed_read_option
)
1180 exit_status
= TAREXIT_FAILURE
;
1185 /* If the file is sparse, we've already taken care of this. */
1189 header
= start_header (p
, ¤t_stat
);
1194 exit_status
= TAREXIT_FAILURE
;
1199 /* Mark contiguous files, if we support them. */
1201 if (archive_format
!= V7_FORMAT
&& S_ISCTG (current_stat
.st_mode
))
1202 header
->header
.typeflag
= CONTTYPE
;
1204 isextended
= header
->oldgnu_header
.isextended
;
1205 save_typeflag
= header
->header
.typeflag
;
1206 finish_header (header
);
1215 int arraybound
= SPARSES_IN_SPARSE_HEADER
;
1217 /* static */ int index_offset
= SPARSES_IN_OLDGNU_HEADER
;
1220 exhdr
= find_next_block ();
1223 exit_status
= TAREXIT_FAILURE
;
1226 memset (exhdr
->buffer
, 0, BLOCKSIZE
);
1227 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
1229 if (counter
+ index_offset
> upperbound
)
1232 SIZE_TO_CHARS (sparsearray
[counter
+ index_offset
].numbytes
,
1233 exhdr
->sparse_header
.sp
[counter
].numbytes
);
1234 OFF_TO_CHARS (sparsearray
[counter
+ index_offset
].offset
,
1235 exhdr
->sparse_header
.sp
[counter
].offset
);
1237 set_next_block_after (exhdr
);
1240 if (sum
< upperbound
)
1243 if (index_offset
+ counter
<= upperbound
)
1245 index_offset
+= counter
;
1246 exhdr
->sparse_header
.isextended
= 1;
1251 if (save_typeflag
== GNUTYPE_SPARSE
)
1254 || finish_sparse_file (f
, &sizeleft
, current_stat
.st_size
, p
))
1258 while (sizeleft
> 0)
1260 if (multi_volume_option
)
1262 assign_string (&save_name
, p
);
1263 save_sizeleft
= sizeleft
;
1264 save_totsize
= current_stat
.st_size
;
1266 start
= find_next_block ();
1268 bufsize
= available_space_after (start
);
1270 if (sizeleft
< bufsize
)
1272 /* Last read -- zero out area beyond. */
1275 count
= bufsize
% BLOCKSIZE
;
1277 memset (start
->buffer
+ sizeleft
, 0, BLOCKSIZE
- count
);
1282 count
= safe_read (f
, start
->buffer
, bufsize
);
1285 char buf
[UINTMAX_STRSIZE_BOUND
];
1287 _("Read error at byte %s, reading %lu bytes, in file %s"),
1288 STRINGIFY_BIGINT (current_stat
.st_size
- sizeleft
,
1290 (unsigned long) bufsize
, p
));
1295 /* This is nonportable (the type of set_next_block_after's arg). */
1297 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
1299 if (count
== bufsize
)
1303 char buf
[UINTMAX_STRSIZE_BOUND
];
1305 _("File %s shrunk by %s bytes, padding with zeros"),
1306 p
, STRINGIFY_BIGINT (sizeleft
, buf
)));
1307 goto padit
; /* short read */
1311 if (multi_volume_option
)
1312 assign_string (&save_name
, 0);
1316 struct stat final_stat
;
1317 if (fstat (f
, &final_stat
) != 0)
1318 ERROR ((0, errno
, "%s: fstat", p
));
1319 else if (final_stat
.st_mtime
!= restore_times
.modtime
1320 || final_stat
.st_size
!= restore_size
)
1321 ERROR ((0, errno
, _("%s: file changed as we read it"), p
));
1323 ERROR ((0, errno
, _("%s: close"), p
));
1324 if (atime_preserve_option
)
1325 utime (p
, &restore_times
);
1327 if (remove_files_option
)
1329 if (unlink (p
) == -1)
1330 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1334 /* File shrunk or gave error, pad out tape to match the size we
1335 specified in the header. */
1338 while (sizeleft
> 0)
1340 save_sizeleft
= sizeleft
;
1341 start
= find_next_block ();
1342 memset (start
->buffer
, 0, BLOCKSIZE
);
1343 set_next_block_after (start
);
1344 sizeleft
-= BLOCKSIZE
;
1346 if (multi_volume_option
)
1347 assign_string (&save_name
, 0);
1351 if (atime_preserve_option
)
1352 utime (p
, &restore_times
);
1357 #ifdef HAVE_READLINK
1358 else if (S_ISLNK (current_stat
.st_mode
))
1361 char *buffer
= alloca (PATH_MAX
+ 1);
1363 size
= readlink (p
, buffer
, PATH_MAX
+ 1);
1366 WARN ((0, errno
, _("Cannot add file %s"), p
));
1367 if (!ignore_failed_read_option
)
1368 exit_status
= TAREXIT_FAILURE
;
1371 buffer
[size
] = '\0';
1372 if (size
>= NAME_FIELD_SIZE
)
1373 write_long (buffer
, GNUTYPE_LONGLINK
);
1374 assign_string (¤t_link_name
, buffer
);
1376 current_stat
.st_size
= 0; /* force 0 size on symlink */
1377 header
= start_header (p
, ¤t_stat
);
1380 exit_status
= TAREXIT_FAILURE
;
1383 strncpy (header
->header
.linkname
, buffer
, NAME_FIELD_SIZE
);
1384 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = '\0';
1385 header
->header
.typeflag
= SYMTYPE
;
1386 finish_header (header
); /* nothing more to do to it */
1387 if (remove_files_option
)
1389 if (unlink (p
) == -1)
1390 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1396 else if (S_ISDIR (current_stat
.st_mode
))
1399 struct dirent
*entry
;
1403 dev_t our_device
= current_stat
.st_dev
;
1405 /* If this tar program is installed suid root, like for Amanda, the
1406 access might look like denied, while it is not really.
1408 FIXME: I have the feeling this test is done too early. Couldn't it
1409 just be bundled in later actions? I guess that the proper support
1410 of --ignore-failed-read is the key of the current writing. */
1412 if (access (p
, R_OK
) == -1 && geteuid () != 0)
1414 WARN ((0, errno
, _("Cannot add directory %s"), p
));
1415 if (!ignore_failed_read_option
)
1416 exit_status
= TAREXIT_FAILURE
;
1420 /* Build new prototype name. Ensure exactly one trailing slash. */
1423 buflen
= len
+ NAME_FIELD_SIZE
;
1424 namebuf
= xmalloc (buflen
+ 1);
1425 strncpy (namebuf
, p
, buflen
);
1426 while (len
>= 1 && namebuf
[len
- 1] == '/')
1428 namebuf
[len
++] = '/';
1429 namebuf
[len
] = '\0';
1433 /* The "1" above used to be "archive_format != V7_FORMAT", GNU tar
1434 was just not writing directory blocks at all. Daniel Trinkle
1435 writes: ``All old versions of tar I have ever seen have
1436 correctly archived an empty directory. The really old ones I
1437 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1438 some subtle reason for the exclusion that I don't know, but the
1439 current behavior is broken.'' I do not know those subtle
1440 reasons either, so until these are reported (anew?), just allow
1441 directory blocks to be written even with old archives. */
1443 current_stat
.st_size
= 0; /* force 0 size on dir */
1445 /* FIXME: If people could really read standard archives, this
1449 = start_header (standard_option ? p : namebuf, ¤t_stat);
1451 but since they'd interpret DIRTYPE blocks as regular
1452 files, we'd better put the / on the name. */
1454 header
= start_header (namebuf
, ¤t_stat
);
1457 exit_status
= TAREXIT_FAILURE
;
1458 return; /* eg name too long */
1461 if (incremental_option
)
1462 header
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1463 else /* if (standard_option) */
1464 header
->header
.typeflag
= DIRTYPE
;
1466 /* If we're gnudumping, we aren't done yet so don't close it. */
1468 if (!incremental_option
)
1469 finish_header (header
); /* done with directory header */
1472 if (incremental_option
&& gnu_list_name
->dir_contents
)
1479 const char *buffer
, *p_buffer
;
1481 buffer
= gnu_list_name
->dir_contents
; /* FOO */
1483 for (p_buffer
= buffer
; p_buffer
&& *p_buffer
;)
1487 tmp
= strlen (p_buffer
) + 1;
1492 OFF_TO_CHARS (totsize
, header
->header
.size
);
1493 finish_header (header
);
1496 while (sizeleft
> 0)
1498 if (multi_volume_option
)
1500 assign_string (&save_name
, p
);
1501 save_sizeleft
= sizeleft
;
1502 save_totsize
= totsize
;
1504 start
= find_next_block ();
1505 bufsize
= available_space_after (start
);
1506 if (sizeleft
< bufsize
)
1509 count
= bufsize
% BLOCKSIZE
;
1511 memset (start
->buffer
+ sizeleft
, 0, BLOCKSIZE
- count
);
1513 memcpy (start
->buffer
, p_buffer
, bufsize
);
1514 sizeleft
-= bufsize
;
1515 p_buffer
+= bufsize
;
1516 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1518 if (multi_volume_option
)
1519 assign_string (&save_name
, 0);
1520 if (atime_preserve_option
)
1521 utime (p
, &restore_times
);
1525 /* See if we are about to recurse into a directory, and avoid doing
1526 so if the user wants that we do not descend into directories. */
1528 if (no_recurse_option
)
1531 /* See if we are crossing from one file system to another, and
1532 avoid doing so if the user only wants to dump one file system. */
1534 if (one_file_system_option
&& !top_level
1535 && parent_device
!= current_stat
.st_dev
)
1538 WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p
));
1542 /* Now output all the files in the directory. */
1544 errno
= 0; /* FIXME: errno should be read-only */
1546 directory
= opendir (p
);
1549 ERROR ((0, errno
, _("Cannot open directory %s"), p
));
1553 /* FIXME: Should speed this up by cd-ing into the dir. */
1555 while (entry
= readdir (directory
), entry
)
1557 /* Skip `.', `..', and excluded file names. */
1559 if (is_dot_or_dotdot (entry
->d_name
))
1562 if ((int) NAMLEN (entry
) + len
>= buflen
)
1564 buflen
= len
+ NAMLEN (entry
);
1565 namebuf
= xrealloc (namebuf
, buflen
+ 1);
1567 namebuf
[len
] = '\0';
1568 ERROR ((0, 0, _("File name %s%s too long"),
1569 namebuf
, entry
->d_name
));
1573 strcpy (namebuf
+ len
, entry
->d_name
);
1574 if (!excluded_name (namebuf
))
1575 dump_file (namebuf
, our_device
, 0);
1578 closedir (directory
);
1580 if (atime_preserve_option
)
1581 utime (p
, &restore_times
);
1585 else if (S_ISCHR (current_stat
.st_mode
))
1587 else if (S_ISBLK (current_stat
.st_mode
))
1589 else if (S_ISFIFO (current_stat
.st_mode
)
1590 || S_ISSOCK (current_stat
.st_mode
))
1595 if (archive_format
== V7_FORMAT
)
1598 current_stat
.st_size
= 0; /* force 0 size */
1599 header
= start_header (p
, ¤t_stat
);
1602 exit_status
= TAREXIT_FAILURE
;
1603 return; /* eg name too long */
1606 header
->header
.typeflag
= type
;
1608 if (type
!= FIFOTYPE
)
1610 MAJOR_TO_CHARS (major (current_stat
.st_rdev
), header
->header
.devmajor
);
1611 MINOR_TO_CHARS (minor (current_stat
.st_rdev
), header
->header
.devminor
);
1614 finish_header (header
);
1615 if (remove_files_option
)
1617 if (unlink (p
) == -1)
1618 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1623 ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p
));