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 struct link
*linklist
= NULL
; /* 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, _("%s value %s out of range %s..%s; substituting %s"),
174 type
, value_string
, minval_string
, maxval_string
,
176 to_chars (negsub
, s
, valsize
, NULL
, where
, size
, type
);
179 ERROR ((0, 0, _("%s value %s out of range %s..%s"),
180 type
, value_string
, 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
, NULL
, 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
, NULL
, 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
, NULL
, 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
, NULL
, 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
, NULL
, 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
, NULL
, 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
, NULL
, 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, NULL 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 (strlen (name
) >= (size_t) NAME_FIELD_SIZE
)
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
= (struct sp_array
*)
588 xmalloc (sp_array_size
* sizeof (struct sp_array
));
589 for (counter
= 0; counter
< sp_array_size
; counter
++)
591 sparsearray
[counter
].offset
= 0;
592 sparsearray
[counter
].numbytes
= 0;
601 find_new_file_size (off_t
*filesize
, int highest_index
)
607 sparsearray
[counter
].numbytes
&& counter
<= highest_index
;
609 *filesize
+= sparsearray
[counter
].numbytes
;
612 /*-----------------------------------------------------------------------.
613 | Make one pass over the file NAME, studying where any non-zero data is, |
614 | that is, how far into the file each instance of data is, and how many |
615 | bytes are there. Save this information in the sparsearray, which will |
616 | later be translated into header information. |
617 `-----------------------------------------------------------------------*/
619 /* There is little point in trimming small amounts of null data at the head
620 and tail of blocks, only avoid dumping full null blocks. */
622 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
623 too kludgey for my taste... */
626 deal_with_sparse (char *name
, union block
*header
)
631 int sparse_index
= 0;
633 char buffer
[BLOCKSIZE
];
635 if (archive_format
== OLDGNU_FORMAT
)
636 header
->oldgnu_header
.isextended
= 0;
638 if (file
= open (name
, O_RDONLY
), file
< 0)
639 /* This problem will be caught later on, so just return. */
643 clear_buffer (buffer
);
645 while (count
= safe_read (file
, buffer
, sizeof buffer
), count
!= 0)
647 /* Realloc the scratch area as necessary. FIXME: should reallocate
648 only at beginning of a new instance of non-zero data. */
650 if (sparse_index
> sp_array_size
- 1)
653 sparsearray
= (struct sp_array
*)
654 xrealloc (sparsearray
,
655 2 * sp_array_size
* sizeof (struct sp_array
));
659 /* Process one block. */
661 if (count
== sizeof buffer
)
663 if (zero_block_p (buffer
))
667 sparsearray
[sparse_index
++].numbytes
= numbytes
;
674 sparsearray
[sparse_index
].offset
= offset
;
680 /* Since count < sizeof buffer, we have the last bit of the file. */
682 if (!zero_block_p (buffer
))
685 sparsearray
[sparse_index
].offset
= offset
;
689 /* The next two lines are suggested by Andreas Degert, who says
690 they are required for trailing full blocks to be written to the
691 archive, when all zeroed. Yet, it seems to me that the case
692 does not apply. Further, at restore time, the file is not as
693 sparse as it should. So, some serious cleanup is *also* needed
694 in this area. Just one more... :-(. FIXME. */
698 /* Prepare for next block. */
701 /* FIXME: do not clear unless necessary. */
702 clear_buffer (buffer
);
706 sparsearray
[sparse_index
++].numbytes
= numbytes
;
709 sparsearray
[sparse_index
].offset
= offset
- 1;
710 sparsearray
[sparse_index
++].numbytes
= 1;
714 return sparse_index
- 1;
722 finish_sparse_file (int file
, off_t
*sizeleft
, off_t fullsize
, char *name
)
726 int sparse_index
= 0;
729 while (*sizeleft
> 0)
731 start
= find_next_block ();
732 memset (start
->buffer
, 0, BLOCKSIZE
);
733 bufsize
= sparsearray
[sparse_index
].numbytes
;
736 /* We blew it, maybe. */
737 char buf1
[UINTMAX_STRSIZE_BOUND
];
738 char buf2
[UINTMAX_STRSIZE_BOUND
];
740 ERROR ((0, 0, _("Wrote %s of %s bytes to file %s"),
741 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf1
),
742 STRINGIFY_BIGINT (fullsize
, buf2
),
747 if (lseek (file
, sparsearray
[sparse_index
++].offset
, SEEK_SET
) < 0)
749 char buf
[UINTMAX_STRSIZE_BOUND
];
750 ERROR ((0, errno
, _("lseek error at byte %s in file %s"),
751 STRINGIFY_BIGINT (sparsearray
[sparse_index
- 1].offset
, buf
),
756 /* If the number of bytes to be written here exceeds the size of
757 the temporary buffer, do it in steps. */
759 while (bufsize
> BLOCKSIZE
)
764 count
= safe_read (file
, start
->buffer
+ amount_read
,
765 BLOCKSIZE
- amount_read
);
766 bufsize
-= BLOCKSIZE
- amount_read
;
768 set_next_block_after (start
);
769 start
= find_next_block ();
770 memset (start
->buffer
, 0, BLOCKSIZE
);
773 /* Store the data. */
775 count
= safe_read (file
, start
->buffer
, BLOCKSIZE
);
778 char buf
[UINTMAX_STRSIZE_BOUND
];
780 _("Read error at byte %s, reading %lu bytes, in file %s"),
781 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
782 (unsigned long) bufsize
, name
));
787 set_next_block_after (start
);
788 start
= find_next_block ();
789 memset (start
->buffer
, 0, BLOCKSIZE
);
793 char buffer
[BLOCKSIZE
];
795 clear_buffer (buffer
);
796 count
= safe_read (file
, buffer
, bufsize
);
797 memcpy (start
->buffer
, buffer
, BLOCKSIZE
);
802 char buf
[UINTMAX_STRSIZE_BOUND
];
805 _("Read error at byte %s, reading %lu bytes, in file %s"),
806 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
807 (unsigned long) bufsize
, name
));
811 if (amount_read
>= BLOCKSIZE
)
814 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
815 if (count
!= bufsize
)
818 _("File %s shrunk, padding with zeros"),
822 start
= find_next_block ();
825 amount_read
+= bufsize
;
828 set_next_block_after (start
);
833 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
838 /* Main functions of this module. */
845 create_archive (void)
849 open_archive (ACCESS_WRITE
);
851 if (incremental_option
)
853 char *buffer
= xmalloc (PATH_MAX
);
857 collect_and_sort_names ();
859 while (p
= name_from_list (), p
)
860 if (!excluded_name (p
))
861 dump_file (p
, (dev_t
) -1, 1);
864 while (p
= name_from_list (), p
)
865 if (!excluded_name (p
))
868 if (p
[strlen (p
) - 1] != '/')
869 strcat (buffer
, "/");
870 bufp
= buffer
+ strlen (buffer
);
871 q
= gnu_list_name
->dir_contents
;
873 for (; *q
; q
+= strlen (q
) + 1)
876 strcpy (bufp
, q
+ 1);
877 dump_file (buffer
, (dev_t
) -1, 1);
884 while (p
= name_next (1), p
)
885 if (!excluded_name (p
))
886 dump_file (p
, (dev_t
) -1, 1);
892 if (listed_incremental_option
)
896 /*----------------------------------------------------------------------.
897 | Dump a single file. Recurse on directories. Result is nonzero for |
898 | success. P is file name to dump. PARENT_DEVICE is device our parent |
899 | directory was on. TOP_LEVEL tells wether we are a toplevel call. |
901 | Sets global CURRENT_STAT to stat output for this file. |
902 `----------------------------------------------------------------------*/
904 /* FIXME: One should make sure that for *every* path leading to setting
905 exit_status to failure, a clear diagnostic has been issued. */
908 dump_file (char *p
, dev_t parent_device
, int top_level
)
914 struct utimbuf restore_times
;
917 /* FIXME: `header' and `upperbound' might be used uninitialized in this
918 function. Reported by Bruno Haible. */
920 if (interactive_option
&& !confirm ("add", p
))
923 /* Use stat if following (rather than dumping) 4.2BSD's symbolic links.
924 Otherwise, use lstat (which falls back to stat if no symbolic links). */
926 if (dereference_option
!= 0
927 #if STX_HIDDEN && !_LARGE_FILES /* AIX */
928 ? statx (p
, ¤t_stat
, STATSIZE
, STX_HIDDEN
)
929 : statx (p
, ¤t_stat
, STATSIZE
, STX_HIDDEN
| STX_LINK
)
931 ? stat (p
, ¤t_stat
) : lstat (p
, ¤t_stat
)
935 WARN ((0, errno
, _("Cannot add file %s"), p
));
936 if (!ignore_failed_read_option
)
937 exit_status
= TAREXIT_FAILURE
;
941 restore_times
.actime
= current_stat
.st_atime
;
942 restore_times
.modtime
= current_stat
.st_mtime
;
943 restore_size
= current_stat
.st_size
;
946 if (S_ISHIDDEN (current_stat
.st_mode
))
948 char *new = (char *) alloca (strlen (p
) + 2);
958 /* See if we want only new files, and check if this one is too old to
959 put in the archive. */
961 if ((!incremental_option
|| listed_incremental_option
)
962 && !S_ISDIR (current_stat
.st_mode
)
963 && current_stat
.st_mtime
< newer_mtime_option
964 && (!after_date_option
|| current_stat
.st_ctime
< newer_ctime_option
))
966 if (!listed_incremental_option
&& parent_device
== (dev_t
) -1)
967 WARN ((0, 0, _("%s: is unchanged; not dumped"), p
));
968 /* FIXME: recheck this return. */
973 /* See if we are trying to dump the archive. */
975 if (ar_dev
&& current_stat
.st_dev
== ar_dev
&& current_stat
.st_ino
== ar_ino
)
977 WARN ((0, 0, _("%s is the archive; not dumped"), p
));
982 /* Check for multiple links.
984 We maintain a list of all such files that we've written so far. Any
985 time we see another, we check the list and avoid dumping the data
986 again if we've done it once already. */
988 if (current_stat
.st_nlink
> 1
989 && (S_ISREG (current_stat
.st_mode
)
990 || S_ISCTG (current_stat
.st_mode
)
991 || S_ISCHR (current_stat
.st_mode
)
992 || S_ISBLK (current_stat
.st_mode
)
993 || S_ISFIFO (current_stat
.st_mode
)))
997 /* FIXME: First quick and dirty. Hashing, etc later. */
999 for (lp
= linklist
; lp
; lp
= lp
->next
)
1000 if (lp
->ino
== current_stat
.st_ino
&& lp
->dev
== current_stat
.st_dev
)
1002 char *link_name
= lp
->name
;
1004 /* We found a link. */
1006 while (!absolute_names_option
&& *link_name
== '/')
1008 static int warned_once
;
1012 WARN ((0, 0, _("Removing leading `/' from link names")));
1016 if (strlen (link_name
) >= NAME_FIELD_SIZE
)
1017 write_long (link_name
, GNUTYPE_LONGLINK
);
1018 assign_string (¤t_link_name
, link_name
);
1020 current_stat
.st_size
= 0;
1021 header
= start_header (p
, ¤t_stat
);
1024 exit_status
= TAREXIT_FAILURE
;
1027 strncpy (header
->header
.linkname
,
1028 link_name
, NAME_FIELD_SIZE
);
1030 /* Force null truncated. */
1032 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = 0;
1034 header
->header
.typeflag
= LNKTYPE
;
1035 finish_header (header
);
1037 /* FIXME: Maybe remove from list after all links found? */
1039 if (remove_files_option
)
1040 if (unlink (p
) == -1)
1041 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1047 /* Not found. Add it to the list of possible links. */
1049 lp
= (struct link
*)
1050 xmalloc ((size_t) (sizeof (struct link
) + strlen (p
)));
1051 lp
->ino
= current_stat
.st_ino
;
1052 lp
->dev
= current_stat
.st_dev
;
1053 strcpy (lp
->name
, p
);
1054 lp
->next
= linklist
;
1058 /* This is not a link to a previously dumped file, so dump it. */
1060 if (S_ISREG (current_stat
.st_mode
)
1061 || S_ISCTG (current_stat
.st_mode
))
1063 int f
; /* file descriptor */
1069 char isextended
= 0;
1072 static int cried_once
= 0;
1079 /* Check the size of the file against the number of blocks
1080 allocated for it, counting both data and indirect blocks.
1081 If there is a smaller number of blocks that would be
1082 necessary to accommodate a file of this size, this is safe
1083 to say that we have a sparse file: at least one of those
1084 blocks in the file is just a useless hole. For sparse
1085 files not having more hole blocks than indirect blocks, the
1086 sparseness will go undetected. */
1088 /* Bruno Haible sent me these statistics for Linux. It seems
1089 that some filesystems count indirect blocks in st_blocks,
1090 while others do not seem to:
1092 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1093 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1094 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1095 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1097 Dick Streefland reports the previous numbers as misleading,
1098 because ext2fs use 12 direct blocks, while minix-fs uses only
1099 6 direct blocks. Dick gets:
1101 ext2 size=20480 ls listed blocks=21
1102 minix size=20480 ls listed blocks=21
1103 msdos size=20480 ls listed blocks=20
1105 It seems that indirect blocks *are* included in st_blocks.
1106 The minix filesystem does not account for phantom blocks in
1107 st_blocks, so `du' and `ls -s' give wrong results. So, the
1108 --sparse option would not work on a minix filesystem. */
1110 if (ST_NBLOCKS (current_stat
)
1111 < (current_stat
.st_size
/ ST_NBLOCKSIZE
1112 + (current_stat
.st_size
% ST_NBLOCKSIZE
!= 0)))
1114 off_t filesize
= current_stat
.st_size
;
1117 header
= start_header (p
, ¤t_stat
);
1120 exit_status
= TAREXIT_FAILURE
;
1123 header
->header
.typeflag
= GNUTYPE_SPARSE
;
1126 /* Call the routine that figures out the layout of the
1127 sparse file in question. UPPERBOUND is the index of the
1128 last element of the "sparsearray," i.e., the number of
1129 elements it needed to describe the file. */
1131 upperbound
= deal_with_sparse (p
, header
);
1133 /* See if we'll need an extended header later. */
1135 if (upperbound
> SPARSES_IN_OLDGNU_HEADER
- 1)
1136 header
->oldgnu_header
.isextended
= 1;
1138 /* We store the "real" file size so we can show that in
1139 case someone wants to list the archive, i.e., tar tvf
1140 <file>. It might be kind of disconcerting if the
1141 shrunken file size was the one that showed up. */
1143 OFF_TO_CHARS (current_stat
.st_size
,
1144 header
->oldgnu_header
.realsize
);
1146 /* This will be the new "size" of the file, i.e., the size
1147 of the file minus the blocks of holes that we're
1150 find_new_file_size (&filesize
, upperbound
);
1151 current_stat
.st_size
= filesize
;
1152 OFF_TO_CHARS (filesize
, header
->header
.size
);
1154 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
1156 if (!sparsearray
[counter
].numbytes
)
1159 OFF_TO_CHARS (sparsearray
[counter
].offset
,
1160 header
->oldgnu_header
.sp
[counter
].offset
);
1161 SIZE_TO_CHARS (sparsearray
[counter
].numbytes
,
1162 header
->oldgnu_header
.sp
[counter
].numbytes
);
1168 upperbound
= SPARSES_IN_OLDGNU_HEADER
- 1;
1170 sizeleft
= current_stat
.st_size
;
1172 /* Don't bother opening empty, world readable files. Also do not open
1173 files when archive is meant for /dev/null. */
1177 && MODE_R
== (MODE_R
& current_stat
.st_mode
)))
1181 f
= open (p
, O_RDONLY
| O_BINARY
);
1184 WARN ((0, errno
, _("Cannot add file %s"), p
));
1185 if (!ignore_failed_read_option
)
1186 exit_status
= TAREXIT_FAILURE
;
1191 /* If the file is sparse, we've already taken care of this. */
1195 header
= start_header (p
, ¤t_stat
);
1200 exit_status
= TAREXIT_FAILURE
;
1205 /* Mark contiguous files, if we support them. */
1207 if (archive_format
!= V7_FORMAT
&& S_ISCTG (current_stat
.st_mode
))
1208 header
->header
.typeflag
= CONTTYPE
;
1210 isextended
= header
->oldgnu_header
.isextended
;
1211 save_typeflag
= header
->header
.typeflag
;
1212 finish_header (header
);
1221 int arraybound
= SPARSES_IN_SPARSE_HEADER
;
1223 /* static */ int index_offset
= SPARSES_IN_OLDGNU_HEADER
;
1226 exhdr
= find_next_block ();
1230 exit_status
= TAREXIT_FAILURE
;
1233 memset (exhdr
->buffer
, 0, BLOCKSIZE
);
1234 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
1236 if (counter
+ index_offset
> upperbound
)
1239 SIZE_TO_CHARS (sparsearray
[counter
+ index_offset
].numbytes
,
1240 exhdr
->sparse_header
.sp
[counter
].numbytes
);
1241 OFF_TO_CHARS (sparsearray
[counter
+ index_offset
].offset
,
1242 exhdr
->sparse_header
.sp
[counter
].offset
);
1244 set_next_block_after (exhdr
);
1247 if (sum
< upperbound
)
1250 if (index_offset
+ counter
<= upperbound
)
1252 index_offset
+= counter
;
1253 exhdr
->sparse_header
.isextended
= 1;
1258 if (save_typeflag
== GNUTYPE_SPARSE
)
1261 || finish_sparse_file (f
, &sizeleft
, current_stat
.st_size
, p
))
1265 while (sizeleft
> 0)
1267 if (multi_volume_option
)
1269 assign_string (&save_name
, p
);
1270 save_sizeleft
= sizeleft
;
1271 save_totsize
= current_stat
.st_size
;
1273 start
= find_next_block ();
1275 bufsize
= available_space_after (start
);
1277 if (sizeleft
< bufsize
)
1279 /* Last read -- zero out area beyond. */
1282 count
= bufsize
% BLOCKSIZE
;
1284 memset (start
->buffer
+ sizeleft
, 0,
1285 (size_t) (BLOCKSIZE
- count
));
1290 count
= safe_read (f
, start
->buffer
, bufsize
);
1293 char buf
[UINTMAX_STRSIZE_BOUND
];
1295 _("Read error at byte %s, reading %lu bytes, in file %s"),
1296 STRINGIFY_BIGINT (current_stat
.st_size
- sizeleft
,
1298 (unsigned long) bufsize
, p
));
1303 /* This is nonportable (the type of set_next_block_after's arg). */
1305 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
1307 if (count
== bufsize
)
1311 char buf
[UINTMAX_STRSIZE_BOUND
];
1313 _("File %s shrunk by %s bytes, padding with zeros"),
1314 p
, STRINGIFY_BIGINT (sizeleft
, buf
)));
1315 goto padit
; /* short read */
1319 if (multi_volume_option
)
1320 assign_string (&save_name
, NULL
);
1324 struct stat final_stat
;
1325 if (fstat (f
, &final_stat
) != 0)
1326 ERROR ((0, errno
, "%s: fstat", p
));
1327 else if (final_stat
.st_mtime
!= restore_times
.modtime
1328 || final_stat
.st_size
!= restore_size
)
1329 ERROR ((0, errno
, _("%s: file changed as we read it"), p
));
1331 ERROR ((0, errno
, _("%s: close"), p
));
1332 if (atime_preserve_option
)
1333 utime (p
, &restore_times
);
1335 if (remove_files_option
)
1337 if (unlink (p
) == -1)
1338 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1342 /* File shrunk or gave error, pad out tape to match the size we
1343 specified in the header. */
1346 while (sizeleft
> 0)
1348 save_sizeleft
= sizeleft
;
1349 start
= find_next_block ();
1350 memset (start
->buffer
, 0, BLOCKSIZE
);
1351 set_next_block_after (start
);
1352 sizeleft
-= BLOCKSIZE
;
1354 if (multi_volume_option
)
1355 assign_string (&save_name
, NULL
);
1359 if (atime_preserve_option
)
1360 utime (p
, &restore_times
);
1365 #ifdef HAVE_READLINK
1366 else if (S_ISLNK (current_stat
.st_mode
))
1369 char *buffer
= (char *) alloca (PATH_MAX
+ 1);
1371 size
= readlink (p
, buffer
, PATH_MAX
+ 1);
1374 WARN ((0, errno
, _("Cannot add file %s"), p
));
1375 if (!ignore_failed_read_option
)
1376 exit_status
= TAREXIT_FAILURE
;
1379 buffer
[size
] = '\0';
1380 if (size
>= NAME_FIELD_SIZE
)
1381 write_long (buffer
, GNUTYPE_LONGLINK
);
1382 assign_string (¤t_link_name
, buffer
);
1384 current_stat
.st_size
= 0; /* force 0 size on symlink */
1385 header
= start_header (p
, ¤t_stat
);
1388 exit_status
= TAREXIT_FAILURE
;
1391 strncpy (header
->header
.linkname
, buffer
, NAME_FIELD_SIZE
);
1392 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = '\0';
1393 header
->header
.typeflag
= SYMTYPE
;
1394 finish_header (header
); /* nothing more to do to it */
1395 if (remove_files_option
)
1397 if (unlink (p
) == -1)
1398 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1404 else if (S_ISDIR (current_stat
.st_mode
))
1407 struct dirent
*entry
;
1411 dev_t our_device
= current_stat
.st_dev
;
1413 /* If this tar program is installed suid root, like for Amanda, the
1414 access might look like denied, while it is not really.
1416 FIXME: I have the feeling this test is done too early. Couldn't it
1417 just be bundled in later actions? I guess that the proper support
1418 of --ignore-failed-read is the key of the current writing. */
1420 if (access (p
, R_OK
) == -1 && geteuid () != 0)
1422 WARN ((0, errno
, _("Cannot add directory %s"), p
));
1423 if (!ignore_failed_read_option
)
1424 exit_status
= TAREXIT_FAILURE
;
1428 /* Build new prototype name. Ensure exactly one trailing slash. */
1431 buflen
= len
+ NAME_FIELD_SIZE
;
1432 namebuf
= xmalloc (buflen
+ 1);
1433 strncpy (namebuf
, p
, buflen
);
1434 while (len
>= 1 && namebuf
[len
- 1] == '/')
1436 namebuf
[len
++] = '/';
1437 namebuf
[len
] = '\0';
1441 /* The "1" above used to be "archive_format != V7_FORMAT", GNU tar
1442 was just not writing directory blocks at all. Daniel Trinkle
1443 writes: ``All old versions of tar I have ever seen have
1444 correctly archived an empty directory. The really old ones I
1445 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1446 some subtle reason for the exclusion that I don't know, but the
1447 current behavior is broken.'' I do not know those subtle
1448 reasons either, so until these are reported (anew?), just allow
1449 directory blocks to be written even with old archives. */
1451 current_stat
.st_size
= 0; /* force 0 size on dir */
1453 /* FIXME: If people could really read standard archives, this
1457 = start_header (standard_option ? p : namebuf, ¤t_stat);
1459 but since they'd interpret DIRTYPE blocks as regular
1460 files, we'd better put the / on the name. */
1462 header
= start_header (namebuf
, ¤t_stat
);
1465 exit_status
= TAREXIT_FAILURE
;
1466 return; /* eg name too long */
1469 if (incremental_option
)
1470 header
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1471 else /* if (standard_option) */
1472 header
->header
.typeflag
= DIRTYPE
;
1474 /* If we're gnudumping, we aren't done yet so don't close it. */
1476 if (!incremental_option
)
1477 finish_header (header
); /* done with directory header */
1480 if (incremental_option
&& gnu_list_name
->dir_contents
)
1487 const char *buffer
, *p_buffer
;
1489 buffer
= gnu_list_name
->dir_contents
; /* FOO */
1491 for (p_buffer
= buffer
; p_buffer
&& *p_buffer
;)
1495 tmp
= strlen (p_buffer
) + 1;
1500 OFF_TO_CHARS (totsize
, header
->header
.size
);
1501 finish_header (header
);
1504 while (sizeleft
> 0)
1506 if (multi_volume_option
)
1508 assign_string (&save_name
, p
);
1509 save_sizeleft
= sizeleft
;
1510 save_totsize
= totsize
;
1512 start
= find_next_block ();
1513 bufsize
= available_space_after (start
);
1514 if (sizeleft
< bufsize
)
1517 count
= bufsize
% BLOCKSIZE
;
1519 memset (start
->buffer
+ sizeleft
, 0,
1520 (size_t) (BLOCKSIZE
- count
));
1522 memcpy (start
->buffer
, p_buffer
, bufsize
);
1523 sizeleft
-= bufsize
;
1524 p_buffer
+= bufsize
;
1525 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1527 if (multi_volume_option
)
1528 assign_string (&save_name
, NULL
);
1529 if (atime_preserve_option
)
1530 utime (p
, &restore_times
);
1534 /* See if we are about to recurse into a directory, and avoid doing
1535 so if the user wants that we do not descend into directories. */
1537 if (no_recurse_option
)
1540 /* See if we are crossing from one file system to another, and
1541 avoid doing so if the user only wants to dump one file system. */
1543 if (one_file_system_option
&& !top_level
1544 && parent_device
!= current_stat
.st_dev
)
1547 WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p
));
1551 /* Now output all the files in the directory. */
1553 errno
= 0; /* FIXME: errno should be read-only */
1555 directory
= opendir (p
);
1558 ERROR ((0, errno
, _("Cannot open directory %s"), p
));
1562 /* FIXME: Should speed this up by cd-ing into the dir. */
1564 while (entry
= readdir (directory
), entry
)
1566 /* Skip `.', `..', and excluded file names. */
1568 if (is_dot_or_dotdot (entry
->d_name
))
1571 if ((int) NAMLEN (entry
) + len
>= buflen
)
1573 buflen
= len
+ NAMLEN (entry
);
1574 namebuf
= (char *) xrealloc (namebuf
, buflen
+ 1);
1576 namebuf
[len
] = '\0';
1577 ERROR ((0, 0, _("File name %s%s too long"),
1578 namebuf
, entry
->d_name
));
1582 strcpy (namebuf
+ len
, entry
->d_name
);
1583 if (!excluded_name (namebuf
))
1584 dump_file (namebuf
, our_device
, 0);
1587 closedir (directory
);
1589 if (atime_preserve_option
)
1590 utime (p
, &restore_times
);
1594 else if (S_ISCHR (current_stat
.st_mode
))
1596 else if (S_ISBLK (current_stat
.st_mode
))
1598 else if (S_ISFIFO (current_stat
.st_mode
)
1599 || S_ISSOCK (current_stat
.st_mode
))
1604 if (archive_format
== V7_FORMAT
)
1607 current_stat
.st_size
= 0; /* force 0 size */
1608 header
= start_header (p
, ¤t_stat
);
1611 exit_status
= TAREXIT_FAILURE
;
1612 return; /* eg name too long */
1615 header
->header
.typeflag
= type
;
1617 if (type
!= FIFOTYPE
)
1619 MAJOR_TO_CHARS (major (current_stat
.st_rdev
), header
->header
.devmajor
);
1620 MINOR_TO_CHARS (minor (current_stat
.st_rdev
), header
->header
.devminor
);
1623 finish_header (header
);
1624 if (remove_files_option
)
1626 if (unlink (p
) == -1)
1627 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1632 ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p
));