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. */
55 static struct link
*linklist
; /* points to first link in list */
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
) PARAMS ((int *)),
121 char *where
, size_t size
, const char *type
)
123 int base256_allowed
= (archive_format
== GNU_FORMAT
124 || archive_format
== OLDGNU_FORMAT
);
125 uintmax_t v
= negative
? -value
: value
;
127 /* Generate the POSIX octal representation if the number fits. */
128 if (! negative
&& v
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
130 where
[size
- 1] = '\0';
131 to_octal (v
, where
, size
- 1);
134 /* Otherwise, generate the base-256 representation if we are
135 generating an old or new GNU format and if the number fits. */
136 else if (v
- negative
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)
139 where
[0] = negative
? -1 : 1 << (LG_256
- 1);
140 to_base256 (negative
, v
, 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\n")));
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 subbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
173 char *value_string
= STRINGIFY_BIGINT (v
, valbuf
+ 1);
174 char *maxval_string
= STRINGIFY_BIGINT (maxval
, maxbuf
);
175 char const *minval_string
;
178 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
179 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
186 *--value_string
= '-';
190 uintmax_t sub
= substitute (&negsub
) & maxval
;
191 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? -sub
: sub
;
192 char *sub_string
= STRINGIFY_BIGINT (s
, subbuf
+ 1);
195 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
196 value_string
, type
, minval_string
, maxval_string
,
198 to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
201 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
202 value_string
, type
, minval_string
, maxval_string
));
207 gid_substitute (int *negative
)
213 static gid_t gid_nobody
;
214 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
223 gid_to_chars (gid_t v
, char *p
, size_t s
)
225 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
229 major_to_chars (major_t v
, char *p
, size_t s
)
231 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
235 minor_to_chars (minor_t v
, char *p
, size_t s
)
237 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
241 mode_to_chars (mode_t v
, char *p
, size_t s
)
243 /* In the common case where the internal and external mode bits are the same,
244 and we are not using POSIX or GNU format,
245 propagate all unknown bits to the external mode.
246 This matches historical practice.
247 Otherwise, just copy the bits we know about. */
250 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
251 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
252 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
253 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
254 && archive_format
!= POSIX_FORMAT
255 && archive_format
!= GNU_FORMAT
)
263 u
= ((v
& S_ISUID
? TSUID
: 0)
264 | (v
& S_ISGID
? TSGID
: 0)
265 | (v
& S_ISVTX
? TSVTX
: 0)
266 | (v
& S_IRUSR
? TUREAD
: 0)
267 | (v
& S_IWUSR
? TUWRITE
: 0)
268 | (v
& S_IXUSR
? TUEXEC
: 0)
269 | (v
& S_IRGRP
? TGREAD
: 0)
270 | (v
& S_IWGRP
? TGWRITE
: 0)
271 | (v
& S_IXGRP
? TGEXEC
: 0)
272 | (v
& S_IROTH
? TOREAD
: 0)
273 | (v
& S_IWOTH
? TOWRITE
: 0)
274 | (v
& S_IXOTH
? TOEXEC
: 0));
276 to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
280 off_to_chars (off_t v
, char *p
, size_t s
)
282 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
286 size_to_chars (size_t v
, char *p
, size_t s
)
288 to_chars (0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "size_t");
292 time_to_chars (time_t v
, char *p
, size_t s
)
294 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
298 uid_substitute (int *negative
)
304 static uid_t uid_nobody
;
305 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
314 uid_to_chars (uid_t v
, char *p
, size_t s
)
316 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
320 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
322 to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
325 /* Writing routines. */
327 /*-----------------------------------------------------------------------.
328 | Just zeroes out the buffer so we don't confuse ourselves with leftover |
330 `-----------------------------------------------------------------------*/
333 clear_buffer (char *buffer
)
335 memset (buffer
, 0, BLOCKSIZE
);
338 /*-------------------------------------------------------------------------.
339 | Write the EOT block(s). We zero at least two blocks, through |
340 | the end of the record. Old tar, as previous versions of GNU tar, writes |
341 | garbage after two zeroed blocks. |
342 `-------------------------------------------------------------------------*/
347 union block
*pointer
= find_next_block ();
348 memset (pointer
->buffer
, 0, BLOCKSIZE
);
349 set_next_block_after (pointer
);
350 pointer
= find_next_block ();
351 memset (pointer
->buffer
, 0, available_space_after (pointer
));
352 set_next_block_after (pointer
);
355 /*-----------------------------------------------------.
356 | Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. |
357 `-----------------------------------------------------*/
359 /* FIXME: Cross recursion between start_header and write_long! */
361 static union block
*start_header
PARAMS ((const char *, struct stat
*));
364 write_long (const char *p
, char type
)
366 size_t size
= strlen (p
) + 1;
371 memset (&foo
, 0, sizeof foo
);
374 header
= start_header ("././@LongLink", &foo
);
375 header
->header
.typeflag
= type
;
376 finish_header (header
);
378 header
= find_next_block ();
380 bufsize
= available_space_after (header
);
382 while (bufsize
< size
)
384 memcpy (header
->buffer
, p
, bufsize
);
387 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
388 header
= find_next_block ();
389 bufsize
= available_space_after (header
);
391 memcpy (header
->buffer
, p
, size
);
392 memset (header
->buffer
+ size
, 0, bufsize
- size
);
393 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
396 /* Header handling. */
398 /* Make a header block for the file whose stat info is st,
399 and return its address. */
402 start_header (const char *name
, struct stat
*st
)
406 if (!absolute_names_option
)
408 size_t prefix_len
= FILESYSTEM_PREFIX_LEN (name
);
412 static int warned_once
;
416 WARN ((0, 0, _("Removing `%.*s' prefix from archive names"),
417 (int) prefix_len
, name
));
424 static int warned_once
;
428 WARN ((0, 0, _("Removing leading `/' from archive names")));
434 if (sizeof header
->header
.name
<= strlen (name
))
435 write_long (name
, GNUTYPE_LONGNAME
);
436 header
= find_next_block ();
437 memset (header
->buffer
, 0, sizeof (union block
));
439 assign_string (¤t_file_name
, name
);
441 strncpy (header
->header
.name
, name
, NAME_FIELD_SIZE
);
442 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
444 /* Override some stat fields, if requested to do so. */
446 if (owner_option
!= (uid_t
) -1)
447 st
->st_uid
= owner_option
;
448 if (group_option
!= (gid_t
) -1)
449 st
->st_gid
= group_option
;
451 st
->st_mode
= ((st
->st_mode
& ~MODE_ALL
)
452 | mode_adjust (st
->st_mode
, mode_option
));
454 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
455 for a few tars and came up with the following interoperability
459 1 2 3 4 5 6 7 8 9 READER
460 . . . . . . . . . 1 = SunOS 4.2 tar
461 # . . # # . . # # 2 = NEC SVR4.0.2 tar
462 . . . # # . . # . 3 = Solaris 2.1 tar
463 . . . . . . . . . 4 = GNU tar 1.11.1
464 . . . . . . . . . 5 = HP-UX 8.07 tar
465 . . . . . . . . . 6 = Ultrix 4.1
466 . . . . . . . . . 7 = AIX 3.2
467 . . . . . . . . . 8 = Hitachi HI-UX 1.03
468 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
471 # = ``impossible file type''
473 The following mask for old archive removes the `#'s in column 4
474 above, thus making GNU tar both a universal donor and a universal
475 acceptor for Paul's test. */
477 if (archive_format
== V7_FORMAT
)
478 MODE_TO_CHARS (st
->st_mode
& MODE_ALL
, header
->header
.mode
);
480 MODE_TO_CHARS (st
->st_mode
, header
->header
.mode
);
482 UID_TO_CHARS (st
->st_uid
, header
->header
.uid
);
483 GID_TO_CHARS (st
->st_gid
, header
->header
.gid
);
484 OFF_TO_CHARS (st
->st_size
, header
->header
.size
);
485 TIME_TO_CHARS (st
->st_mtime
, header
->header
.mtime
);
487 if (incremental_option
)
488 if (archive_format
== OLDGNU_FORMAT
)
490 TIME_TO_CHARS (st
->st_atime
, header
->oldgnu_header
.atime
);
491 TIME_TO_CHARS (st
->st_ctime
, header
->oldgnu_header
.ctime
);
494 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
496 switch (archive_format
)
502 /* Overwrite header->header.magic and header.version in one blow. */
503 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
508 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
509 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
516 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
518 /* header->header.[ug]name are left as the empty string. */
522 uid_to_uname (st
->st_uid
, header
->header
.uname
);
523 gid_to_gname (st
->st_gid
, header
->header
.gname
);
529 /*-------------------------------------------------------------------------.
530 | Finish off a filled-in header block and write it out. We also print the |
531 | file name and/or full info if verbose is on. |
532 `-------------------------------------------------------------------------*/
535 finish_header (union block
*header
)
541 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
545 for (i
= sizeof *header
; i
-- != 0; )
546 /* We can't use unsigned char here because of old compilers, e.g. V7. */
549 /* Fill in the checksum field. It's formatted differently from the
550 other fields: it has [6] digits, a null, then a space -- rather than
551 digits, then a null. We use to_chars.
552 The final space is already there, from
553 checksumming, and to_chars doesn't modify it.
555 This is a fast way to do:
557 sprintf(header->header.chksum, "%6o", sum); */
559 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
561 set_next_block_after (header
);
564 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
565 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
)
567 /* These globals are parameters to print_header, sigh. */
569 current_header
= header
;
570 /* current_stat is already set up. */
571 current_format
= archive_format
;
576 /* Sparse file processing. */
578 /*-------------------------------------------------------------------------.
579 | Takes a blockful of data and basically cruises through it to see if it's |
580 | made *entirely* of zeros, returning a 0 the instant it finds something |
581 | that is a nonzero, i.e., useful data. |
582 `-------------------------------------------------------------------------*/
585 zero_block_p (char *buffer
)
589 for (counter
= 0; counter
< BLOCKSIZE
; counter
++)
590 if (buffer
[counter
] != '\0')
600 init_sparsearray (void)
606 /* Make room for our scratch space -- initially is 10 elts long. */
608 sparsearray
= xmalloc (sp_array_size
* sizeof (struct sp_array
));
609 for (counter
= 0; counter
< sp_array_size
; counter
++)
611 sparsearray
[counter
].offset
= 0;
612 sparsearray
[counter
].numbytes
= 0;
621 find_new_file_size (off_t
*filesize
, int highest_index
)
627 sparsearray
[counter
].numbytes
&& counter
<= highest_index
;
629 *filesize
+= sparsearray
[counter
].numbytes
;
632 /*-----------------------------------------------------------------------.
633 | Make one pass over the file NAME, studying where any non-zero data is, |
634 | that is, how far into the file each instance of data is, and how many |
635 | bytes are there. Save this information in the sparsearray, which will |
636 | later be translated into header information. |
637 `-----------------------------------------------------------------------*/
639 /* There is little point in trimming small amounts of null data at the head
640 and tail of blocks, only avoid dumping full null blocks. */
642 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
643 too kludgey for my taste... */
646 deal_with_sparse (char *name
, union block
*header
)
651 int sparse_index
= 0;
653 char buffer
[BLOCKSIZE
];
655 if (archive_format
== OLDGNU_FORMAT
)
656 header
->oldgnu_header
.isextended
= 0;
658 if (file
= open (name
, O_RDONLY
), file
< 0)
659 /* This problem will be caught later on, so just return. */
663 clear_buffer (buffer
);
665 while (count
= safe_read (file
, buffer
, sizeof buffer
), count
!= 0)
667 /* Realloc the scratch area as necessary. FIXME: should reallocate
668 only at beginning of a new instance of non-zero data. */
670 if (sparse_index
> sp_array_size
- 1)
673 xrealloc (sparsearray
,
674 2 * sp_array_size
* sizeof (struct sp_array
));
678 /* Process one block. */
680 if (count
== sizeof buffer
)
682 if (zero_block_p (buffer
))
686 sparsearray
[sparse_index
++].numbytes
= numbytes
;
693 sparsearray
[sparse_index
].offset
= offset
;
699 /* Since count < sizeof buffer, we have the last bit of the file. */
701 if (!zero_block_p (buffer
))
704 sparsearray
[sparse_index
].offset
= offset
;
708 /* The next two lines are suggested by Andreas Degert, who says
709 they are required for trailing full blocks to be written to the
710 archive, when all zeroed. Yet, it seems to me that the case
711 does not apply. Further, at restore time, the file is not as
712 sparse as it should. So, some serious cleanup is *also* needed
713 in this area. Just one more... :-(. FIXME. */
717 /* Prepare for next block. */
720 /* FIXME: do not clear unless necessary. */
721 clear_buffer (buffer
);
725 sparsearray
[sparse_index
++].numbytes
= numbytes
;
728 sparsearray
[sparse_index
].offset
= offset
- 1;
729 sparsearray
[sparse_index
++].numbytes
= 1;
733 return sparse_index
- 1;
741 finish_sparse_file (int file
, off_t
*sizeleft
, off_t fullsize
, char *name
)
745 int sparse_index
= 0;
748 while (*sizeleft
> 0)
750 start
= find_next_block ();
751 memset (start
->buffer
, 0, BLOCKSIZE
);
752 bufsize
= sparsearray
[sparse_index
].numbytes
;
755 /* We blew it, maybe. */
756 char buf1
[UINTMAX_STRSIZE_BOUND
];
757 char buf2
[UINTMAX_STRSIZE_BOUND
];
759 ERROR ((0, 0, _("Wrote %s of %s bytes to file %s"),
760 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf1
),
761 STRINGIFY_BIGINT (fullsize
, buf2
),
766 if (lseek (file
, sparsearray
[sparse_index
++].offset
, SEEK_SET
) < 0)
768 char buf
[UINTMAX_STRSIZE_BOUND
];
769 ERROR ((0, errno
, _("lseek error at byte %s in file %s"),
770 STRINGIFY_BIGINT (sparsearray
[sparse_index
- 1].offset
, buf
),
775 /* If the number of bytes to be written here exceeds the size of
776 the temporary buffer, do it in steps. */
778 while (bufsize
> BLOCKSIZE
)
783 count
= safe_read (file
, start
->buffer
+ amount_read
,
784 BLOCKSIZE
- amount_read
);
785 bufsize
-= BLOCKSIZE
- amount_read
;
787 set_next_block_after (start
);
788 start
= find_next_block ();
789 memset (start
->buffer
, 0, BLOCKSIZE
);
792 /* Store the data. */
794 count
= safe_read (file
, start
->buffer
, BLOCKSIZE
);
797 char buf
[UINTMAX_STRSIZE_BOUND
];
799 _("Read error at byte %s, reading %lu bytes, in file %s"),
800 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
801 (unsigned long) bufsize
, name
));
806 set_next_block_after (start
);
807 start
= find_next_block ();
808 memset (start
->buffer
, 0, BLOCKSIZE
);
812 char buffer
[BLOCKSIZE
];
814 clear_buffer (buffer
);
815 count
= safe_read (file
, buffer
, bufsize
);
816 memcpy (start
->buffer
, buffer
, BLOCKSIZE
);
821 char buf
[UINTMAX_STRSIZE_BOUND
];
824 _("Read error at byte %s, reading %lu bytes, in file %s"),
825 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
826 (unsigned long) bufsize
, name
));
830 if (amount_read
>= BLOCKSIZE
)
833 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
834 if (count
!= bufsize
)
837 _("File %s shrunk, padding with zeros"),
841 start
= find_next_block ();
844 amount_read
+= bufsize
;
847 set_next_block_after (start
);
852 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
857 /* Main functions of this module. */
864 create_archive (void)
868 open_archive (ACCESS_WRITE
);
870 if (incremental_option
)
872 char *buffer
= xmalloc (PATH_MAX
);
876 collect_and_sort_names ();
878 while (p
= name_from_list (), p
)
879 if (!excluded_name (p
))
880 dump_file (p
, -1, (dev_t
) 0);
883 while (p
= name_from_list (), p
)
884 if (!excluded_name (p
))
887 if (p
[strlen (p
) - 1] != '/')
888 strcat (buffer
, "/");
889 bufp
= buffer
+ strlen (buffer
);
890 q
= gnu_list_name
->dir_contents
;
892 for (; *q
; q
+= strlen (q
) + 1)
895 strcpy (bufp
, q
+ 1);
896 dump_file (buffer
, -1, (dev_t
) 0);
903 while (p
= name_next (1), p
)
904 if (!excluded_name (p
))
905 dump_file (p
, 1, (dev_t
) 0);
911 if (listed_incremental_option
)
912 write_directory_file ();
915 /* Dump a single file, recursing on directories. P is the file name
916 to dump. TOP_LEVEL tells whether this is a top-level call; zero
917 means no, positive means yes, and negative means an incremental
918 dump where it's irrelevant. PARENT_DEVICE is the device of P's
919 parent directory; it is examined only if TOP_LEVEL is zero.
921 Set global CURRENT_STAT to stat output for this file. */
923 /* FIXME: One should make sure that for *every* path leading to setting
924 exit_status to failure, a clear diagnostic has been issued. */
927 dump_file (char *p
, int top_level
, dev_t parent_device
)
933 struct utimbuf restore_times
;
936 /* FIXME: `header' and `upperbound' might be used uninitialized in this
937 function. Reported by Bruno Haible. */
939 if (interactive_option
&& !confirm ("add", p
))
942 if (deref_stat (dereference_option
, p
, ¤t_stat
) != 0)
944 WARN ((0, errno
, _("Cannot add file %s"), p
));
945 if (!ignore_failed_read_option
)
946 exit_status
= TAREXIT_FAILURE
;
950 restore_times
.actime
= current_stat
.st_atime
;
951 restore_times
.modtime
= current_stat
.st_mtime
;
952 restore_size
= current_stat
.st_size
;
955 if (S_ISHIDDEN (current_stat
.st_mode
))
957 char *new = (char *) alloca (strlen (p
) + 2);
967 /* See if we want only new files, and check if this one is too old to
968 put in the archive. */
970 if ((0 < top_level
|| !incremental_option
)
971 && !S_ISDIR (current_stat
.st_mode
)
972 && current_stat
.st_mtime
< newer_mtime_option
973 && (!after_date_option
|| current_stat
.st_ctime
< newer_ctime_option
))
976 WARN ((0, 0, _("%s: is unchanged; not dumped"), p
));
977 /* FIXME: recheck this return. */
982 /* See if we are trying to dump the archive. */
984 if (ar_dev
&& current_stat
.st_dev
== ar_dev
&& current_stat
.st_ino
== ar_ino
)
986 WARN ((0, 0, _("%s is the archive; not dumped"), p
));
991 if (S_ISDIR (current_stat
.st_mode
))
994 struct dirent
*entry
;
998 dev_t our_device
= current_stat
.st_dev
;
1000 /* If this tar program is installed suid root, like for Amanda, the
1001 access might look like denied, while it is not really.
1003 FIXME: I have the feeling this test is done too early. Couldn't it
1004 just be bundled in later actions? I guess that the proper support
1005 of --ignore-failed-read is the key of the current writing. */
1007 if (access (p
, R_OK
) == -1 && geteuid () != 0)
1009 WARN ((0, errno
, _("Cannot add directory %s"), p
));
1010 if (!ignore_failed_read_option
)
1011 exit_status
= TAREXIT_FAILURE
;
1015 /* Build new prototype name. Ensure exactly one trailing slash. */
1018 buflen
= len
+ NAME_FIELD_SIZE
;
1019 namebuf
= xmalloc (buflen
+ 1);
1020 strncpy (namebuf
, p
, buflen
);
1021 while (len
>= 1 && namebuf
[len
- 1] == '/')
1023 namebuf
[len
++] = '/';
1024 namebuf
[len
] = '\0';
1026 if (! is_avoided_name (namebuf
))
1028 /* The condition above used to be "archive_format != V7_FORMAT".
1029 GNU tar was not writing directory blocks at all. Daniel Trinkle
1030 writes: ``All old versions of tar I have ever seen have
1031 correctly archived an empty directory. The really old ones I
1032 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1033 some subtle reason for the exclusion that I don't know, but the
1034 current behavior is broken.'' I do not know those subtle
1035 reasons either, so until these are reported (anew?), just allow
1036 directory blocks to be written even with old archives. */
1038 current_stat
.st_size
= 0; /* force 0 size on dir */
1040 /* FIXME: If people could really read standard archives, this
1044 = start_header (standard_option ? p : namebuf, ¤t_stat);
1046 but since they'd interpret DIRTYPE blocks as regular
1047 files, we'd better put the / on the name. */
1049 header
= start_header (namebuf
, ¤t_stat
);
1051 if (incremental_option
)
1052 header
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1053 else /* if (standard_option) */
1054 header
->header
.typeflag
= DIRTYPE
;
1056 /* If we're gnudumping, we aren't done yet so don't close it. */
1058 if (!incremental_option
)
1059 finish_header (header
); /* done with directory header */
1062 if (incremental_option
&& gnu_list_name
->dir_contents
)
1069 const char *buffer
, *p_buffer
;
1071 buffer
= gnu_list_name
->dir_contents
; /* FOO */
1073 for (p_buffer
= buffer
; p_buffer
&& *p_buffer
;)
1077 tmp
= strlen (p_buffer
) + 1;
1082 OFF_TO_CHARS (totsize
, header
->header
.size
);
1083 finish_header (header
);
1086 while (sizeleft
> 0)
1088 if (multi_volume_option
)
1090 assign_string (&save_name
, p
);
1091 save_sizeleft
= sizeleft
;
1092 save_totsize
= totsize
;
1094 start
= find_next_block ();
1095 bufsize
= available_space_after (start
);
1096 if (sizeleft
< bufsize
)
1099 count
= bufsize
% BLOCKSIZE
;
1101 memset (start
->buffer
+ sizeleft
, 0, BLOCKSIZE
- count
);
1103 memcpy (start
->buffer
, p_buffer
, bufsize
);
1104 sizeleft
-= bufsize
;
1105 p_buffer
+= bufsize
;
1106 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1108 if (multi_volume_option
)
1109 assign_string (&save_name
, 0);
1110 if (atime_preserve_option
)
1111 utime (p
, &restore_times
);
1115 /* See if we are about to recurse into a directory, and avoid doing
1116 so if the user wants that we do not descend into directories. */
1118 if (no_recurse_option
)
1121 /* See if we are crossing from one file system to another, and
1122 avoid doing so if the user only wants to dump one file system. */
1124 if (one_file_system_option
&& !top_level
1125 && parent_device
!= current_stat
.st_dev
)
1128 WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p
));
1132 /* Now output all the files in the directory. */
1134 errno
= 0; /* FIXME: errno should be read-only */
1136 directory
= opendir (p
);
1139 ERROR ((0, errno
, _("Cannot open directory %s"), p
));
1143 /* FIXME: Should speed this up by cd-ing into the dir. */
1145 while (entry
= readdir (directory
), entry
)
1147 /* Skip `.', `..', and excluded file names. */
1149 if (is_dot_or_dotdot (entry
->d_name
))
1152 if ((int) NAMLEN (entry
) + len
>= buflen
)
1154 buflen
= len
+ NAMLEN (entry
);
1155 namebuf
= xrealloc (namebuf
, buflen
+ 1);
1157 strcpy (namebuf
+ len
, entry
->d_name
);
1158 if (!excluded_name (namebuf
))
1159 dump_file (namebuf
, our_device
, 0);
1162 closedir (directory
);
1164 if (atime_preserve_option
)
1165 utime (p
, &restore_times
);
1168 else if (is_avoided_name (p
))
1172 /* Check for multiple links.
1174 We maintain a list of all such files that we've written so far. Any
1175 time we see another, we check the list and avoid dumping the data
1176 again if we've done it once already. */
1178 if (1 < current_stat
.st_nlink
)
1182 /* FIXME: First quick and dirty. Hashing, etc later. */
1184 for (lp
= linklist
; lp
; lp
= lp
->next
)
1185 if (lp
->ino
== current_stat
.st_ino
1186 && lp
->dev
== current_stat
.st_dev
)
1188 char *link_name
= lp
->name
;
1190 /* We found a link. */
1192 while (!absolute_names_option
&& *link_name
== '/')
1194 static int warned_once
;
1199 _("Removing leading `/' from link names")));
1203 if (strlen (link_name
) >= NAME_FIELD_SIZE
)
1204 write_long (link_name
, GNUTYPE_LONGLINK
);
1205 assign_string (¤t_link_name
, link_name
);
1207 current_stat
.st_size
= 0;
1208 header
= start_header (p
, ¤t_stat
);
1209 strncpy (header
->header
.linkname
,
1210 link_name
, NAME_FIELD_SIZE
);
1212 /* Force null truncated. */
1214 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = 0;
1216 header
->header
.typeflag
= LNKTYPE
;
1217 finish_header (header
);
1219 /* FIXME: Maybe remove from list after all links found? */
1221 if (remove_files_option
)
1222 if (unlink (p
) == -1)
1223 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1229 /* Not found. Add it to the list of possible links. */
1231 lp
= xmalloc (sizeof (struct link
) + strlen (p
));
1232 lp
->ino
= current_stat
.st_ino
;
1233 lp
->dev
= current_stat
.st_dev
;
1234 strcpy (lp
->name
, p
);
1235 lp
->next
= linklist
;
1239 /* This is not a link to a previously dumped file, so dump it. */
1241 if (S_ISREG (current_stat
.st_mode
)
1242 || S_ISCTG (current_stat
.st_mode
))
1244 int f
; /* file descriptor */
1250 char isextended
= 0;
1257 /* Check the size of the file against the number of blocks
1258 allocated for it, counting both data and indirect blocks.
1259 If there is a smaller number of blocks that would be
1260 necessary to accommodate a file of this size, this is safe
1261 to say that we have a sparse file: at least one of those
1262 blocks in the file is just a useless hole. For sparse
1263 files not having more hole blocks than indirect blocks, the
1264 sparseness will go undetected. */
1266 /* Bruno Haible sent me these statistics for Linux. It seems
1267 that some filesystems count indirect blocks in st_blocks,
1268 while others do not seem to:
1270 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1271 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1272 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1273 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1275 Dick Streefland reports the previous numbers as misleading,
1276 because ext2fs use 12 direct blocks, while minix-fs uses only
1277 6 direct blocks. Dick gets:
1279 ext2 size=20480 ls listed blocks=21
1280 minix size=20480 ls listed blocks=21
1281 msdos size=20480 ls listed blocks=20
1283 It seems that indirect blocks *are* included in st_blocks.
1284 The minix filesystem does not account for phantom blocks in
1285 st_blocks, so `du' and `ls -s' give wrong results. So, the
1286 --sparse option would not work on a minix filesystem. */
1288 if (ST_NBLOCKS (current_stat
)
1289 < (current_stat
.st_size
/ ST_NBLOCKSIZE
1290 + (current_stat
.st_size
% ST_NBLOCKSIZE
!= 0)))
1292 off_t filesize
= current_stat
.st_size
;
1295 header
= start_header (p
, ¤t_stat
);
1296 header
->header
.typeflag
= GNUTYPE_SPARSE
;
1299 /* Call the routine that figures out the layout of the
1300 sparse file in question. UPPERBOUND is the index of the
1301 last element of the "sparsearray," i.e., the number of
1302 elements it needed to describe the file. */
1304 upperbound
= deal_with_sparse (p
, header
);
1306 /* See if we'll need an extended header later. */
1308 if (upperbound
> SPARSES_IN_OLDGNU_HEADER
- 1)
1309 header
->oldgnu_header
.isextended
= 1;
1311 /* We store the "real" file size so we can show that in
1312 case someone wants to list the archive, i.e., tar tvf
1313 <file>. It might be kind of disconcerting if the
1314 shrunken file size was the one that showed up. */
1316 OFF_TO_CHARS (current_stat
.st_size
,
1317 header
->oldgnu_header
.realsize
);
1319 /* This will be the new "size" of the file, i.e., the size
1320 of the file minus the blocks of holes that we're
1323 find_new_file_size (&filesize
, upperbound
);
1324 current_stat
.st_size
= filesize
;
1325 OFF_TO_CHARS (filesize
, header
->header
.size
);
1327 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
1329 if (!sparsearray
[counter
].numbytes
)
1332 OFF_TO_CHARS (sparsearray
[counter
].offset
,
1333 header
->oldgnu_header
.sp
[counter
].offset
);
1334 SIZE_TO_CHARS (sparsearray
[counter
].numbytes
,
1335 header
->oldgnu_header
.sp
[counter
].numbytes
);
1341 upperbound
= SPARSES_IN_OLDGNU_HEADER
- 1;
1343 sizeleft
= current_stat
.st_size
;
1345 /* Don't bother opening empty, world readable files. Also do not open
1346 files when archive is meant for /dev/null. */
1350 && MODE_R
== (MODE_R
& current_stat
.st_mode
)))
1354 f
= open (p
, O_RDONLY
| O_BINARY
);
1357 WARN ((0, errno
, _("Cannot add file %s"), p
));
1358 if (!ignore_failed_read_option
)
1359 exit_status
= TAREXIT_FAILURE
;
1364 /* If the file is sparse, we've already taken care of this. */
1367 header
= start_header (p
, ¤t_stat
);
1369 /* Mark contiguous files, if we support them. */
1371 if (archive_format
!= V7_FORMAT
&& S_ISCTG (current_stat
.st_mode
))
1372 header
->header
.typeflag
= CONTTYPE
;
1374 isextended
= header
->oldgnu_header
.isextended
;
1375 save_typeflag
= header
->header
.typeflag
;
1376 finish_header (header
);
1380 /* static */ int index_offset
= SPARSES_IN_OLDGNU_HEADER
;
1383 exhdr
= find_next_block ();
1384 memset (exhdr
->buffer
, 0, BLOCKSIZE
);
1385 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
1387 if (counter
+ index_offset
> upperbound
)
1390 SIZE_TO_CHARS (sparsearray
[counter
+ index_offset
].numbytes
,
1391 exhdr
->sparse_header
.sp
[counter
].numbytes
);
1392 OFF_TO_CHARS (sparsearray
[counter
+ index_offset
].offset
,
1393 exhdr
->sparse_header
.sp
[counter
].offset
);
1395 set_next_block_after (exhdr
);
1396 if (index_offset
+ counter
<= upperbound
)
1398 index_offset
+= counter
;
1399 exhdr
->sparse_header
.isextended
= 1;
1404 if (save_typeflag
== GNUTYPE_SPARSE
)
1407 || finish_sparse_file (f
, &sizeleft
, current_stat
.st_size
, p
))
1411 while (sizeleft
> 0)
1413 if (multi_volume_option
)
1415 assign_string (&save_name
, p
);
1416 save_sizeleft
= sizeleft
;
1417 save_totsize
= current_stat
.st_size
;
1419 start
= find_next_block ();
1421 bufsize
= available_space_after (start
);
1423 if (sizeleft
< bufsize
)
1425 /* Last read -- zero out area beyond. */
1428 count
= bufsize
% BLOCKSIZE
;
1430 memset (start
->buffer
+ sizeleft
, 0, BLOCKSIZE
- count
);
1435 count
= safe_read (f
, start
->buffer
, bufsize
);
1438 char buf
[UINTMAX_STRSIZE_BOUND
];
1440 _("Read error at byte %s, reading %lu bytes, in file %s"),
1441 STRINGIFY_BIGINT (current_stat
.st_size
- sizeleft
,
1443 (unsigned long) bufsize
, p
));
1448 /* This is nonportable (the type of set_next_block_after's arg). */
1450 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
1452 if (count
== bufsize
)
1456 char buf
[UINTMAX_STRSIZE_BOUND
];
1458 _("File %s shrunk by %s bytes, padding with zeros"),
1459 p
, STRINGIFY_BIGINT (sizeleft
, buf
)));
1460 goto padit
; /* short read */
1464 if (multi_volume_option
)
1465 assign_string (&save_name
, 0);
1469 struct stat final_stat
;
1470 if (fstat (f
, &final_stat
) != 0)
1471 ERROR ((0, errno
, "%s: fstat", p
));
1472 else if (final_stat
.st_mtime
!= restore_times
.modtime
1473 || final_stat
.st_size
!= restore_size
)
1474 ERROR ((0, errno
, _("%s: file changed as we read it"), p
));
1476 ERROR ((0, errno
, _("%s: close"), p
));
1477 if (atime_preserve_option
)
1478 utime (p
, &restore_times
);
1480 if (remove_files_option
)
1482 if (unlink (p
) == -1)
1483 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1487 /* File shrunk or gave error, pad out tape to match the size we
1488 specified in the header. */
1491 while (sizeleft
> 0)
1493 save_sizeleft
= sizeleft
;
1494 start
= find_next_block ();
1495 memset (start
->buffer
, 0, BLOCKSIZE
);
1496 set_next_block_after (start
);
1497 sizeleft
-= BLOCKSIZE
;
1499 if (multi_volume_option
)
1500 assign_string (&save_name
, 0);
1504 if (atime_preserve_option
)
1505 utime (p
, &restore_times
);
1509 #ifdef HAVE_READLINK
1510 else if (S_ISLNK (current_stat
.st_mode
))
1513 char *buffer
= (char *) alloca (PATH_MAX
+ 1);
1515 size
= readlink (p
, buffer
, PATH_MAX
+ 1);
1518 WARN ((0, errno
, _("Cannot add file %s"), p
));
1519 if (!ignore_failed_read_option
)
1520 exit_status
= TAREXIT_FAILURE
;
1523 buffer
[size
] = '\0';
1524 if (size
>= NAME_FIELD_SIZE
)
1525 write_long (buffer
, GNUTYPE_LONGLINK
);
1526 assign_string (¤t_link_name
, buffer
);
1528 current_stat
.st_size
= 0; /* force 0 size on symlink */
1529 header
= start_header (p
, ¤t_stat
);
1530 strncpy (header
->header
.linkname
, buffer
, NAME_FIELD_SIZE
);
1531 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = '\0';
1532 header
->header
.typeflag
= SYMTYPE
;
1533 finish_header (header
); /* nothing more to do to it */
1534 if (remove_files_option
)
1536 if (unlink (p
) == -1)
1537 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1542 else if (S_ISCHR (current_stat
.st_mode
))
1544 else if (S_ISBLK (current_stat
.st_mode
))
1546 else if (S_ISFIFO (current_stat
.st_mode
)
1547 || S_ISSOCK (current_stat
.st_mode
))
1553 if (archive_format
== V7_FORMAT
)
1556 current_stat
.st_size
= 0; /* force 0 size */
1557 header
= start_header (p
, ¤t_stat
);
1558 header
->header
.typeflag
= type
;
1560 if (type
!= FIFOTYPE
)
1562 MAJOR_TO_CHARS (major (current_stat
.st_rdev
), header
->header
.devmajor
);
1563 MINOR_TO_CHARS (minor (current_stat
.st_rdev
), header
->header
.devminor
);
1566 finish_header (header
);
1567 if (remove_files_option
)
1569 if (unlink (p
) == -1)
1570 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1575 ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p
));