1 /* Create a tar archive.
2 Copyright 1985,92,93,94,96,97,99,2000, 2001 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. */
53 /* The maximum uintmax_t value that can be represented with DIGITS digits,
54 assuming that each digit is BITS_PER_DIGIT wide. */
55 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
56 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
57 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
60 /* Convert VALUE to an octal representation suitable for tar headers.
61 Output to buffer WHERE with size SIZE.
62 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
65 to_octal (uintmax_t value
, char *where
, size_t size
)
72 where
[--i
] = '0' + (v
& ((1 << LG_8
) - 1));
78 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
79 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
80 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
81 The result is undefined if SIZE is 0 or if VALUE is too large to
85 to_base256 (int negative
, uintmax_t value
, char *where
, size_t size
)
88 uintmax_t propagated_sign_bits
=
89 ((uintmax_t) - negative
<< (CHAR_BIT
* sizeof v
- LG_256
));
94 where
[--i
] = v
& ((1 << LG_256
) - 1);
95 v
= propagated_sign_bits
| (v
>> LG_256
);
100 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
101 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
102 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
103 negative before being cast to uintmax_t; its original bitpattern
104 can be deduced from VALSIZE, its original size before casting.
105 TYPE is the kind of value being output (useful for diagnostics).
106 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
107 digits), followed by '\0'. If this won't work, and if GNU or
108 OLDGNU format is allowed, use '\200' followed by base-256, or (if
109 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
110 If neither format works, use SUBSTITUTE (...) instead. Pass to
111 SUBSTITUTE the address of an 0-or-1 flag recording whether the
112 substitute value is negative. */
115 to_chars (int negative
, uintmax_t value
, size_t valsize
,
116 uintmax_t (*substitute
) PARAMS ((int *)),
117 char *where
, size_t size
, const char *type
)
119 int base256_allowed
= (archive_format
== GNU_FORMAT
120 || archive_format
== OLDGNU_FORMAT
);
122 /* Generate the POSIX octal representation if the number fits. */
123 if (! negative
&& value
<= MAX_VAL_WITH_DIGITS (size
- 1, LG_8
))
125 where
[size
- 1] = '\0';
126 to_octal (value
, where
, size
- 1);
129 /* Otherwise, generate the base-256 representation if we are
130 generating an old or new GNU format and if the number fits. */
131 else if (((negative
? -1 - value
: value
)
132 <= MAX_VAL_WITH_DIGITS (size
- 1, LG_256
))
135 where
[0] = negative
? -1 : 1 << (LG_256
- 1);
136 to_base256 (negative
, value
, where
+ 1, size
- 1);
139 /* Otherwise, if the number is negative, and if it would not cause
140 ambiguity on this host by confusing positive with negative
141 values, then generate the POSIX octal representation of the value
142 modulo 2**(field bits). The resulting tar file is
143 machine-dependent, since it depends on the host word size. Yuck!
144 But this is the traditional behavior. */
145 else if (negative
&& valsize
* CHAR_BIT
<= (size
- 1) * LG_8
)
147 static int warned_once
;
151 WARN ((0, 0, _("Generating negative octal headers")));
153 where
[size
- 1] = '\0';
154 to_octal (value
& MAX_VAL_WITH_DIGITS (valsize
* CHAR_BIT
, 1),
158 /* Otherwise, output a substitute value if possible (with a
159 warning), and an error message if not. */
162 uintmax_t maxval
= (base256_allowed
163 ? MAX_VAL_WITH_DIGITS (size
- 1, LG_256
)
164 : MAX_VAL_WITH_DIGITS (size
- 1, LG_8
));
165 char valbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
166 char maxbuf
[UINTMAX_STRSIZE_BOUND
];
167 char minbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
168 char const *minval_string
;
169 char const *maxval_string
= STRINGIFY_BIGINT (maxval
, maxbuf
);
170 char const *value_string
;
174 uintmax_t m
= maxval
+ 1 ? maxval
+ 1 : maxval
/ 2 + 1;
175 char *p
= STRINGIFY_BIGINT (m
, minbuf
+ 1);
184 char *p
= STRINGIFY_BIGINT (- value
, valbuf
+ 1);
189 value_string
= STRINGIFY_BIGINT (value
, valbuf
);
194 uintmax_t sub
= substitute (&negsub
) & maxval
;
195 uintmax_t s
= (negsub
&= archive_format
== GNU_FORMAT
) ? - sub
: sub
;
196 char subbuf
[UINTMAX_STRSIZE_BOUND
+ 1];
197 char *sub_string
= STRINGIFY_BIGINT (s
, subbuf
+ 1);
200 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
201 value_string
, type
, minval_string
, maxval_string
,
203 to_chars (negsub
, s
, valsize
, 0, where
, size
, type
);
206 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
207 value_string
, type
, minval_string
, maxval_string
));
212 gid_substitute (int *negative
)
218 static gid_t gid_nobody
;
219 if (!gid_nobody
&& !gname_to_gid ("nobody", &gid_nobody
))
228 gid_to_chars (gid_t v
, char *p
, size_t s
)
230 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, gid_substitute
, p
, s
, "gid_t");
234 major_to_chars (major_t v
, char *p
, size_t s
)
236 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "major_t");
240 minor_to_chars (minor_t v
, char *p
, size_t s
)
242 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "minor_t");
246 mode_to_chars (mode_t v
, char *p
, size_t s
)
248 /* In the common case where the internal and external mode bits are the same,
249 and we are not using POSIX or GNU format,
250 propagate all unknown bits to the external mode.
251 This matches historical practice.
252 Otherwise, just copy the bits we know about. */
255 if (S_ISUID
== TSUID
&& S_ISGID
== TSGID
&& S_ISVTX
== TSVTX
256 && S_IRUSR
== TUREAD
&& S_IWUSR
== TUWRITE
&& S_IXUSR
== TUEXEC
257 && S_IRGRP
== TGREAD
&& S_IWGRP
== TGWRITE
&& S_IXGRP
== TGEXEC
258 && S_IROTH
== TOREAD
&& S_IWOTH
== TOWRITE
&& S_IXOTH
== TOEXEC
259 && archive_format
!= POSIX_FORMAT
260 && archive_format
!= GNU_FORMAT
)
268 u
= ((v
& S_ISUID
? TSUID
: 0)
269 | (v
& S_ISGID
? TSGID
: 0)
270 | (v
& S_ISVTX
? TSVTX
: 0)
271 | (v
& S_IRUSR
? TUREAD
: 0)
272 | (v
& S_IWUSR
? TUWRITE
: 0)
273 | (v
& S_IXUSR
? TUEXEC
: 0)
274 | (v
& S_IRGRP
? TGREAD
: 0)
275 | (v
& S_IWGRP
? TGWRITE
: 0)
276 | (v
& S_IXGRP
? TGEXEC
: 0)
277 | (v
& S_IROTH
? TOREAD
: 0)
278 | (v
& S_IWOTH
? TOWRITE
: 0)
279 | (v
& S_IXOTH
? TOEXEC
: 0));
281 to_chars (negative
, u
, sizeof v
, 0, p
, s
, "mode_t");
285 off_to_chars (off_t v
, char *p
, size_t s
)
287 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "off_t");
291 size_to_chars (size_t v
, char *p
, size_t s
)
293 to_chars (0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "size_t");
297 time_to_chars (time_t v
, char *p
, size_t s
)
299 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, 0, p
, s
, "time_t");
303 uid_substitute (int *negative
)
309 static uid_t uid_nobody
;
310 if (!uid_nobody
&& !uname_to_uid ("nobody", &uid_nobody
))
319 uid_to_chars (uid_t v
, char *p
, size_t s
)
321 to_chars (v
< 0, (uintmax_t) v
, sizeof v
, uid_substitute
, p
, s
, "uid_t");
325 uintmax_to_chars (uintmax_t v
, char *p
, size_t s
)
327 to_chars (0, v
, sizeof v
, 0, p
, s
, "uintmax_t");
330 /* Writing routines. */
332 /* Zero out the buffer so we don't confuse ourselves with leftover
335 clear_buffer (char *buffer
)
337 memset (buffer
, 0, BLOCKSIZE
);
340 /* Write the EOT block(s). Zero at least two blocks, through the end
341 of the record. Old tar, as previous versions of GNU tar, writes
342 garbage after two zeroed blocks. */
346 union block
*pointer
= find_next_block ();
347 memset (pointer
->buffer
, 0, BLOCKSIZE
);
348 set_next_block_after (pointer
);
349 pointer
= find_next_block ();
350 memset (pointer
->buffer
, 0, available_space_after (pointer
));
351 set_next_block_after (pointer
);
354 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
356 /* FIXME: Cross recursion between start_header and write_long! */
358 static union block
*start_header
PARAMS ((const char *, struct stat
*));
361 write_long (const char *p
, char type
)
363 size_t size
= strlen (p
) + 1;
368 memset (&foo
, 0, sizeof foo
);
371 header
= start_header ("././@LongLink", &foo
);
372 header
->header
.typeflag
= type
;
373 finish_header (header
);
375 header
= find_next_block ();
377 bufsize
= available_space_after (header
);
379 while (bufsize
< size
)
381 memcpy (header
->buffer
, p
, bufsize
);
384 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
385 header
= find_next_block ();
386 bufsize
= available_space_after (header
);
388 memcpy (header
->buffer
, p
, size
);
389 memset (header
->buffer
+ size
, 0, bufsize
- size
);
390 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
393 /* Return a suffix of the file NAME that is a relative file name.
394 Warn about `..' in file names. But return NAME if the user wants
395 absolute file names. */
397 relativize (char const *name
)
399 if (! absolute_names_option
)
402 static int warned_once
;
403 if (! warned_once
&& contains_dot_dot (name
))
406 WARN ((0, 0, _("Member names contain `..'")));
411 size_t prefix_len
= FILESYSTEM_PREFIX_LEN (name
);
413 while (ISSLASH (name
[prefix_len
]))
418 static int warned_once
;
422 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
423 (int) prefix_len
, name
));
433 /* Header handling. */
435 /* Make a header block for the file whose stat info is st,
436 and return its address. */
439 start_header (const char *name
, struct stat
*st
)
443 name
= relativize (name
);
445 if (sizeof header
->header
.name
<= strlen (name
))
446 write_long (name
, GNUTYPE_LONGNAME
);
447 header
= find_next_block ();
448 memset (header
->buffer
, 0, sizeof (union block
));
450 assign_string (¤t_file_name
, name
);
452 strncpy (header
->header
.name
, name
, NAME_FIELD_SIZE
);
453 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
455 /* Override some stat fields, if requested to do so. */
457 if (owner_option
!= (uid_t
) -1)
458 st
->st_uid
= owner_option
;
459 if (group_option
!= (gid_t
) -1)
460 st
->st_gid
= group_option
;
462 st
->st_mode
= ((st
->st_mode
& ~MODE_ALL
)
463 | mode_adjust (st
->st_mode
, mode_option
));
465 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
466 for a few tars and came up with the following interoperability
470 1 2 3 4 5 6 7 8 9 READER
471 . . . . . . . . . 1 = SunOS 4.2 tar
472 # . . # # . . # # 2 = NEC SVR4.0.2 tar
473 . . . # # . . # . 3 = Solaris 2.1 tar
474 . . . . . . . . . 4 = GNU tar 1.11.1
475 . . . . . . . . . 5 = HP-UX 8.07 tar
476 . . . . . . . . . 6 = Ultrix 4.1
477 . . . . . . . . . 7 = AIX 3.2
478 . . . . . . . . . 8 = Hitachi HI-UX 1.03
479 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
482 # = ``impossible file type''
484 The following mask for old archive removes the `#'s in column 4
485 above, thus making GNU tar both a universal donor and a universal
486 acceptor for Paul's test. */
488 if (archive_format
== V7_FORMAT
)
489 MODE_TO_CHARS (st
->st_mode
& MODE_ALL
, header
->header
.mode
);
491 MODE_TO_CHARS (st
->st_mode
, header
->header
.mode
);
493 UID_TO_CHARS (st
->st_uid
, header
->header
.uid
);
494 GID_TO_CHARS (st
->st_gid
, header
->header
.gid
);
495 OFF_TO_CHARS (st
->st_size
, header
->header
.size
);
496 TIME_TO_CHARS (st
->st_mtime
, header
->header
.mtime
);
498 if (incremental_option
)
499 if (archive_format
== OLDGNU_FORMAT
)
501 TIME_TO_CHARS (st
->st_atime
, header
->oldgnu_header
.atime
);
502 TIME_TO_CHARS (st
->st_ctime
, header
->oldgnu_header
.ctime
);
505 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
507 switch (archive_format
)
513 /* Overwrite header->header.magic and header.version in one blow. */
514 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
519 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
520 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
527 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
529 /* header->header.[ug]name are left as the empty string. */
533 uid_to_uname (st
->st_uid
, header
->header
.uname
);
534 gid_to_gname (st
->st_gid
, header
->header
.gname
);
540 /* Finish off a filled-in header block and write it out. We also
541 print the file name and/or full info if verbose is on. */
543 finish_header (union block
*header
)
549 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof header
->header
.chksum
);
553 for (i
= sizeof *header
; i
-- != 0; )
554 /* We can't use unsigned char here because of old compilers, e.g. V7. */
557 /* Fill in the checksum field. It's formatted differently from the
558 other fields: it has [6] digits, a null, then a space -- rather than
559 digits, then a null. We use to_chars.
560 The final space is already there, from
561 checksumming, and to_chars doesn't modify it.
563 This is a fast way to do:
565 sprintf(header->header.chksum, "%6o", sum); */
567 uintmax_to_chars ((uintmax_t) sum
, header
->header
.chksum
, 7);
570 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
571 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
)
573 /* These globals are parameters to print_header, sigh. */
575 current_header
= header
;
576 /* current_stat is already set up. */
577 current_format
= archive_format
;
581 set_next_block_after (header
);
584 /* Sparse file processing. */
586 /* Takes a blockful of data and basically cruises through it to see if
587 it's made *entirely* of zeros, returning a 0 the instant it finds
588 something that is a nonzero, i.e., useful data. */
590 zero_block_p (char *buffer
)
594 for (counter
= 0; counter
< BLOCKSIZE
; counter
++)
595 if (buffer
[counter
] != '\0')
601 init_sparsearray (void)
605 /* Make room for our scratch space -- initially is 10 elts long. */
607 sparsearray
= xmalloc (sp_array_size
* sizeof (struct sp_array
));
611 find_new_file_size (int sparses
)
615 for (i
= 0; i
< sparses
; i
++)
616 s
+= sparsearray
[i
].numbytes
;
620 /* Make one pass over the file NAME, studying where any non-zero data
621 is, that is, how far into the file each instance of data is, and
622 how many bytes are there. Save this information in the
623 sparsearray, which will later be translated into header
626 /* There is little point in trimming small amounts of null data at the head
627 and tail of blocks, only avoid dumping full null blocks. */
629 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
630 too kludgey for my taste... */
633 deal_with_sparse (char *name
, union block
*header
)
640 char buffer
[BLOCKSIZE
];
642 if (archive_format
== OLDGNU_FORMAT
)
643 header
->oldgnu_header
.isextended
= 0;
645 if (file
= open (name
, O_RDONLY
), file
< 0)
646 /* This problem will be caught later on, so just return. */
650 clear_buffer (buffer
);
654 /* Realloc the scratch area as necessary. FIXME: should reallocate
655 only at beginning of a new instance of non-zero data. */
657 if (sp_array_size
<= sparses
)
660 xrealloc (sparsearray
,
661 2 * sp_array_size
* sizeof (struct sp_array
));
665 count
= safe_read (file
, buffer
, sizeof buffer
);
669 /* Process one block. */
671 if (count
== sizeof buffer
)
673 if (zero_block_p (buffer
))
677 sparsearray
[sparses
++].numbytes
= numbytes
;
684 sparsearray
[sparses
].offset
= offset
;
690 /* Since count < sizeof buffer, we have the last bit of the file. */
692 if (!zero_block_p (buffer
))
695 sparsearray
[sparses
].offset
= offset
;
699 /* The next two lines are suggested by Andreas Degert, who says
700 they are required for trailing full blocks to be written to the
701 archive, when all zeroed. Yet, it seems to me that the case
702 does not apply. Further, at restore time, the file is not as
703 sparse as it should. So, some serious cleanup is *also* needed
704 in this area. Just one more... :-(. FIXME. */
708 /* Prepare for next block. */
711 /* FIXME: do not clear unless necessary. */
712 clear_buffer (buffer
);
716 sparsearray
[sparses
++].numbytes
= numbytes
;
719 sparsearray
[sparses
].offset
= offset
- 1;
720 sparsearray
[sparses
++].numbytes
= 1;
723 return close (file
) == 0 && 0 <= count
? sparses
: 0;
727 finish_sparse_file (int file
, off_t
*sizeleft
, off_t fullsize
, char *name
)
734 while (*sizeleft
> 0)
736 start
= find_next_block ();
737 memset (start
->buffer
, 0, BLOCKSIZE
);
738 bufsize
= sparsearray
[sparses
].numbytes
;
742 if (lseek (file
, sparsearray
[sparses
++].offset
, SEEK_SET
) < 0)
744 (ignore_failed_read_option
? seek_warn_details
: seek_error_details
)
745 (name
, sparsearray
[sparses
- 1].offset
);
749 /* If the number of bytes to be written here exceeds the size of
750 the temporary buffer, do it in steps. */
752 while (bufsize
> BLOCKSIZE
)
754 count
= safe_read (file
, start
->buffer
, BLOCKSIZE
);
757 (ignore_failed_read_option
759 : read_error_details
)
760 (name
, fullsize
- *sizeleft
, bufsize
);
765 set_next_block_after (start
);
766 start
= find_next_block ();
767 memset (start
->buffer
, 0, BLOCKSIZE
);
771 char buffer
[BLOCKSIZE
];
773 clear_buffer (buffer
);
774 count
= safe_read (file
, buffer
, bufsize
);
775 memcpy (start
->buffer
, buffer
, BLOCKSIZE
);
780 (ignore_failed_read_option
782 : read_error_details
)
783 (name
, fullsize
- *sizeleft
, bufsize
);
788 set_next_block_after (start
);
792 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
797 /* Main functions of this module. */
800 create_archive (void)
804 open_archive (ACCESS_WRITE
);
806 if (incremental_option
)
808 size_t buffer_size
= 1000;
809 char *buffer
= xmalloc (buffer_size
);
812 collect_and_sort_names ();
814 while (p
= name_from_list (), p
)
815 if (!excluded_name (p
))
816 dump_file (p
, -1, (dev_t
) 0);
819 while (p
= name_from_list (), p
)
820 if (!excluded_name (p
))
822 size_t plen
= strlen (p
);
823 if (buffer_size
<= plen
)
825 while ((buffer_size
*= 2) <= plen
)
827 buffer
= xrealloc (buffer
, buffer_size
);
829 memcpy (buffer
, p
, plen
);
830 if (! ISSLASH (buffer
[plen
- 1]))
831 buffer
[plen
++] = '/';
832 q
= gnu_list_name
->dir_contents
;
836 size_t qlen
= strlen (q
);
839 if (buffer_size
< plen
+ qlen
)
841 while ((buffer_size
*=2 ) < plen
+ qlen
)
843 buffer
= xrealloc (buffer
, buffer_size
);
845 strcpy (buffer
+ plen
, q
+ 1);
846 dump_file (buffer
, -1, (dev_t
) 0);
855 while (p
= name_next (1), p
)
856 if (!excluded_name (p
))
857 dump_file (p
, 1, (dev_t
) 0);
863 if (listed_incremental_option
)
864 write_directory_file ();
868 /* Calculate the hash of a link. */
870 hash_link (void const *entry
, unsigned n_buckets
)
872 struct link
const *link
= entry
;
873 return (uintmax_t) (link
->dev
^ link
->ino
) % n_buckets
;
876 /* Compare two links for equality. */
878 compare_links (void const *entry1
, void const *entry2
)
880 struct link
const *link1
= entry1
;
881 struct link
const *link2
= entry2
;
882 return ((link1
->dev
^ link2
->dev
) | (link1
->ino
^ link2
->ino
)) == 0;
885 /* Dump a single file, recursing on directories. P is the file name
886 to dump. TOP_LEVEL tells whether this is a top-level call; zero
887 means no, positive means yes, and negative means an incremental
888 dump. PARENT_DEVICE is the device of P's
889 parent directory; it is examined only if TOP_LEVEL is zero.
891 Set global CURRENT_STAT to stat output for this file. */
893 /* FIXME: One should make sure that for *every* path leading to setting
894 exit_status to failure, a clear diagnostic has been issued. */
897 dump_file (char *p
, int top_level
, dev_t parent_device
)
903 time_t original_ctime
;
904 struct utimbuf restore_times
;
906 /* FIXME: `header' might be used uninitialized in this
907 function. Reported by Bruno Haible. */
909 if (interactive_option
&& !confirm ("add", p
))
912 if (deref_stat (dereference_option
, p
, ¤t_stat
) != 0)
914 if (ignore_failed_read_option
)
921 original_ctime
= current_stat
.st_ctime
;
922 restore_times
.actime
= current_stat
.st_atime
;
923 restore_times
.modtime
= current_stat
.st_mtime
;
926 if (S_ISHIDDEN (current_stat
.st_mode
))
928 char *new = (char *) alloca (strlen (p
) + 2);
938 /* See if we want only new files, and check if this one is too old to
939 put in the archive. */
941 if ((0 < top_level
|| !incremental_option
)
942 && !S_ISDIR (current_stat
.st_mode
)
943 && current_stat
.st_mtime
< newer_mtime_option
944 && (!after_date_option
|| current_stat
.st_ctime
< newer_ctime_option
))
947 WARN ((0, 0, _("%s: file is unchanged; not dumped"),
948 quotearg_colon (p
)));
949 /* FIXME: recheck this return. */
954 /* See if we are trying to dump the archive. */
956 if (ar_dev
&& current_stat
.st_dev
== ar_dev
&& current_stat
.st_ino
== ar_ino
)
958 WARN ((0, 0, _("%s: file is the archive; not dumped"),
959 quotearg_colon (p
)));
964 if (S_ISDIR (current_stat
.st_mode
))
972 dev_t our_device
= current_stat
.st_dev
;
976 directory
= savedir (p
);
979 if (ignore_failed_read_option
)
986 /* Build new prototype name. Ensure exactly one trailing slash. */
989 buflen
= len
+ NAME_FIELD_SIZE
;
990 namebuf
= xmalloc (buflen
+ 1);
991 memcpy (namebuf
, p
, len
);
992 while (len
>= 1 && ISSLASH (namebuf
[len
- 1]))
994 namebuf
[len
++] = '/';
997 if (! is_avoided_name (namebuf
))
999 /* The condition above used to be "archive_format != V7_FORMAT".
1000 GNU tar was not writing directory blocks at all. Daniel Trinkle
1001 writes: ``All old versions of tar I have ever seen have
1002 correctly archived an empty directory. The really old ones I
1003 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1004 some subtle reason for the exclusion that I don't know, but the
1005 current behavior is broken.'' I do not know those subtle
1006 reasons either, so until these are reported (anew?), just allow
1007 directory blocks to be written even with old archives. */
1009 current_stat
.st_size
= 0; /* force 0 size on dir */
1011 /* FIXME: If people could really read standard archives, this
1015 = start_header (standard_option ? p : namebuf, ¤t_stat);
1017 but since they'd interpret DIRTYPE blocks as regular
1018 files, we'd better put the / on the name. */
1020 header
= start_header (namebuf
, ¤t_stat
);
1022 if (incremental_option
)
1023 header
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1024 else /* if (standard_option) */
1025 header
->header
.typeflag
= DIRTYPE
;
1027 /* If we're gnudumping, we aren't done yet so don't close it. */
1029 if (!incremental_option
)
1030 finish_header (header
); /* done with directory header */
1033 if (incremental_option
&& gnu_list_name
->dir_contents
)
1040 const char *buffer
, *p_buffer
;
1042 buffer
= gnu_list_name
->dir_contents
; /* FOO */
1044 for (p_buffer
= buffer
; p_buffer
&& *p_buffer
;)
1048 tmp
= strlen (p_buffer
) + 1;
1053 OFF_TO_CHARS (totsize
, header
->header
.size
);
1054 finish_header (header
);
1057 while (sizeleft
> 0)
1059 if (multi_volume_option
)
1061 assign_string (&save_name
, p
);
1062 save_sizeleft
= sizeleft
;
1063 save_totsize
= totsize
;
1065 start
= find_next_block ();
1066 bufsize
= available_space_after (start
);
1067 if (sizeleft
< bufsize
)
1070 count
= bufsize
% BLOCKSIZE
;
1072 memset (start
->buffer
+ sizeleft
, 0, BLOCKSIZE
- count
);
1074 memcpy (start
->buffer
, p_buffer
, bufsize
);
1075 sizeleft
-= bufsize
;
1076 p_buffer
+= bufsize
;
1077 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1079 if (multi_volume_option
)
1080 assign_string (&save_name
, 0);
1084 /* See if we are about to recurse into a directory, and avoid doing
1085 so if the user wants that we do not descend into directories. */
1087 if (! recursion_option
)
1090 /* See if we are crossing from one file system to another, and
1091 avoid doing so if the user only wants to dump one file system. */
1093 if (one_file_system_option
&& !top_level
1094 && parent_device
!= current_stat
.st_dev
)
1098 _("%s: file is on a different filesystem; not dumped"),
1099 quotearg_colon (p
)));
1103 /* Now output all the files in the directory. */
1105 /* FIXME: Should speed this up by cd-ing into the dir. */
1107 for (entry
= directory
;
1108 (entrylen
= strlen (entry
)) != 0;
1109 entry
+= entrylen
+ 1)
1111 if (buflen
<= len
+ entrylen
)
1113 buflen
= len
+ entrylen
;
1114 namebuf
= xrealloc (namebuf
, buflen
+ 1);
1116 strcpy (namebuf
+ len
, entry
);
1117 if (!excluded_name (namebuf
))
1118 dump_file (namebuf
, 0, our_device
);
1125 if (atime_preserve_option
)
1126 utime (p
, &restore_times
);
1129 else if (is_avoided_name (p
))
1133 /* Check for multiple links.
1135 We maintain a table of all such files that we've written so
1136 far. Any time we see another, we check the table and avoid
1137 dumping the data again if we've done it once already. */
1139 if (1 < current_stat
.st_nlink
)
1141 static Hash_table
*link_table
;
1142 struct link
*lp
= xmalloc (sizeof *lp
+ strlen (p
));
1144 lp
->ino
= current_stat
.st_ino
;
1145 lp
->dev
= current_stat
.st_dev
;
1146 strcpy (lp
->name
, p
);
1149 || (link_table
= hash_initialize (0, 0, hash_link
,
1151 && (dup
= hash_insert (link_table
, lp
))))
1156 /* We found a link. */
1157 char const *link_name
= dup
->name
;
1161 if (NAME_FIELD_SIZE
<= strlen (link_name
))
1162 write_long (link_name
, GNUTYPE_LONGLINK
);
1163 assign_string (¤t_link_name
, link_name
);
1165 current_stat
.st_size
= 0;
1166 header
= start_header (p
, ¤t_stat
);
1167 strncpy (header
->header
.linkname
, link_name
, NAME_FIELD_SIZE
);
1169 /* Force null termination. */
1170 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = 0;
1172 header
->header
.typeflag
= LNKTYPE
;
1173 finish_header (header
);
1175 /* FIXME: Maybe remove from table after all links found? */
1177 if (remove_files_option
&& unlink (p
) != 0)
1185 /* This is not a link to a previously dumped file, so dump it. */
1187 if (S_ISREG (current_stat
.st_mode
)
1188 || S_ISCTG (current_stat
.st_mode
))
1190 int f
; /* file descriptor */
1196 char isextended
= 0;
1203 /* Check the size of the file against the number of blocks
1204 allocated for it, counting both data and indirect blocks.
1205 If there is a smaller number of blocks that would be
1206 necessary to accommodate a file of this size, this is safe
1207 to say that we have a sparse file: at least one of those
1208 blocks in the file is just a useless hole. For sparse
1209 files not having more hole blocks than indirect blocks, the
1210 sparseness will go undetected. */
1212 /* Bruno Haible sent me these statistics for Linux. It seems
1213 that some filesystems count indirect blocks in st_blocks,
1214 while others do not seem to:
1216 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1217 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1218 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1219 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1221 Dick Streefland reports the previous numbers as misleading,
1222 because ext2fs use 12 direct blocks, while minix-fs uses only
1223 6 direct blocks. Dick gets:
1225 ext2 size=20480 ls listed blocks=21
1226 minix size=20480 ls listed blocks=21
1227 msdos size=20480 ls listed blocks=20
1229 It seems that indirect blocks *are* included in st_blocks.
1230 The minix filesystem does not account for phantom blocks in
1231 st_blocks, so `du' and `ls -s' give wrong results. So, the
1232 --sparse option would not work on a minix filesystem. */
1234 if (ST_NBLOCKS (current_stat
)
1235 < (current_stat
.st_size
/ ST_NBLOCKSIZE
1236 + (current_stat
.st_size
% ST_NBLOCKSIZE
!= 0)))
1240 header
= start_header (p
, ¤t_stat
);
1241 header
->header
.typeflag
= GNUTYPE_SPARSE
;
1244 /* Call the routine that figures out the layout of the
1245 sparse file in question. SPARSES is the index of the
1246 first unused element of the "sparsearray," i.e.,
1247 the number of elements it needed to describe the file. */
1249 sparses
= deal_with_sparse (p
, header
);
1251 /* See if we'll need an extended header later. */
1253 if (SPARSES_IN_OLDGNU_HEADER
< sparses
)
1254 header
->oldgnu_header
.isextended
= 1;
1256 /* We store the "real" file size so we can show that in
1257 case someone wants to list the archive, i.e., tar tvf
1258 <file>. It might be kind of disconcerting if the
1259 shrunken file size was the one that showed up. */
1261 OFF_TO_CHARS (current_stat
.st_size
,
1262 header
->oldgnu_header
.realsize
);
1264 /* This will be the new "size" of the file, i.e., the size
1265 of the file minus the blocks of holes that we're
1268 current_stat
.st_size
= find_new_file_size (sparses
);
1269 OFF_TO_CHARS (current_stat
.st_size
, header
->header
.size
);
1272 counter
< sparses
&& counter
< SPARSES_IN_OLDGNU_HEADER
;
1275 OFF_TO_CHARS (sparsearray
[counter
].offset
,
1276 header
->oldgnu_header
.sp
[counter
].offset
);
1277 SIZE_TO_CHARS (sparsearray
[counter
].numbytes
,
1278 header
->oldgnu_header
.sp
[counter
].numbytes
);
1283 sizeleft
= current_stat
.st_size
;
1285 /* Don't bother opening empty, world readable files. Also do not open
1286 files when archive is meant for /dev/null. */
1290 && MODE_R
== (MODE_R
& current_stat
.st_mode
)))
1294 f
= open (p
, O_RDONLY
| O_BINARY
);
1297 if (! top_level
&& errno
== ENOENT
)
1298 WARN ((0, 0, _("%s: File removed before we read it"),
1299 quotearg_colon (p
)));
1301 (ignore_failed_read_option
? open_warn
: open_error
) (p
);
1306 /* If the file is sparse, we've already taken care of this. */
1309 header
= start_header (p
, ¤t_stat
);
1311 /* Mark contiguous files, if we support them. */
1313 if (archive_format
!= V7_FORMAT
&& S_ISCTG (current_stat
.st_mode
))
1314 header
->header
.typeflag
= CONTTYPE
;
1316 isextended
= header
->oldgnu_header
.isextended
;
1317 save_typeflag
= header
->header
.typeflag
;
1318 finish_header (header
);
1321 int sparses_emitted
= SPARSES_IN_OLDGNU_HEADER
;
1326 exhdr
= find_next_block ();
1327 memset (exhdr
->buffer
, 0, BLOCKSIZE
);
1329 (i
< SPARSES_IN_SPARSE_HEADER
1330 && sparses_emitted
+ i
< sparses
);
1333 SIZE_TO_CHARS (sparsearray
[sparses_emitted
+ i
].numbytes
,
1334 exhdr
->sparse_header
.sp
[i
].numbytes
);
1335 OFF_TO_CHARS (sparsearray
[sparses_emitted
+ i
].offset
,
1336 exhdr
->sparse_header
.sp
[i
].offset
);
1338 set_next_block_after (exhdr
);
1339 sparses_emitted
+= i
;
1340 if (sparses
== sparses_emitted
)
1342 exhdr
->sparse_header
.isextended
= 1;
1345 if (save_typeflag
== GNUTYPE_SPARSE
)
1348 || finish_sparse_file (f
, &sizeleft
,
1349 current_stat
.st_size
, p
))
1353 while (sizeleft
> 0)
1355 if (multi_volume_option
)
1357 assign_string (&save_name
, p
);
1358 save_sizeleft
= sizeleft
;
1359 save_totsize
= current_stat
.st_size
;
1361 start
= find_next_block ();
1363 bufsize
= available_space_after (start
);
1365 if (sizeleft
< bufsize
)
1367 /* Last read -- zero out area beyond. */
1370 count
= bufsize
% BLOCKSIZE
;
1372 memset (start
->buffer
+ sizeleft
, 0, BLOCKSIZE
- count
);
1377 count
= safe_read (f
, start
->buffer
, bufsize
);
1380 (ignore_failed_read_option
1382 : read_error_details
)
1383 (p
, current_stat
.st_size
- sizeleft
, bufsize
);
1386 sizeleft
-= bufsize
;
1388 /* This is nonportable (the type of set_next_block_after's arg). */
1390 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1393 if (count
!= bufsize
)
1395 char buf
[UINTMAX_STRSIZE_BOUND
];
1396 memset (start
->buffer
+ count
, 0, bufsize
- count
);
1398 _("%s: File shrank by %s bytes; padding with zeros"),
1400 STRINGIFY_BIGINT (sizeleft
, buf
)));
1401 if (! ignore_failed_read_option
)
1402 exit_status
= TAREXIT_FAILURE
;
1403 goto padit
; /* short read */
1407 if (multi_volume_option
)
1408 assign_string (&save_name
, 0);
1412 struct stat final_stat
;
1413 if (fstat (f
, &final_stat
) != 0)
1415 if (ignore_failed_read_option
)
1420 else if (final_stat
.st_ctime
!= original_ctime
)
1422 char const *qp
= quotearg_colon (p
);
1423 WARN ((0, 0, _("%s: file changed as we read it"), qp
));
1424 if (! ignore_failed_read_option
)
1425 exit_status
= TAREXIT_FAILURE
;
1429 if (ignore_failed_read_option
)
1434 if (atime_preserve_option
)
1435 utime (p
, &restore_times
);
1437 if (remove_files_option
)
1439 if (unlink (p
) == -1)
1444 /* File shrunk or gave error, pad out tape to match the size we
1445 specified in the header. */
1448 while (sizeleft
> 0)
1450 save_sizeleft
= sizeleft
;
1451 start
= find_next_block ();
1452 memset (start
->buffer
, 0, BLOCKSIZE
);
1453 set_next_block_after (start
);
1454 sizeleft
-= BLOCKSIZE
;
1456 if (multi_volume_option
)
1457 assign_string (&save_name
, 0);
1461 if (atime_preserve_option
)
1462 utime (p
, &restore_times
);
1466 #ifdef HAVE_READLINK
1467 else if (S_ISLNK (current_stat
.st_mode
))
1471 size_t linklen
= current_stat
.st_size
;
1472 if (linklen
!= current_stat
.st_size
|| linklen
+ 1 == 0)
1474 buffer
= (char *) alloca (linklen
+ 1);
1475 size
= readlink (p
, buffer
, linklen
+ 1);
1478 if (ignore_failed_read_option
)
1484 buffer
[size
] = '\0';
1485 if (size
>= NAME_FIELD_SIZE
)
1486 write_long (buffer
, GNUTYPE_LONGLINK
);
1487 assign_string (¤t_link_name
, buffer
);
1489 current_stat
.st_size
= 0; /* force 0 size on symlink */
1490 header
= start_header (p
, ¤t_stat
);
1491 strncpy (header
->header
.linkname
, buffer
, NAME_FIELD_SIZE
);
1492 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = '\0';
1493 header
->header
.typeflag
= SYMTYPE
;
1494 finish_header (header
); /* nothing more to do to it */
1495 if (remove_files_option
)
1497 if (unlink (p
) == -1)
1503 else if (S_ISCHR (current_stat
.st_mode
))
1505 else if (S_ISBLK (current_stat
.st_mode
))
1507 else if (S_ISFIFO (current_stat
.st_mode
))
1509 else if (S_ISSOCK (current_stat
.st_mode
))
1511 WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p
)));
1514 else if (S_ISDOOR (current_stat
.st_mode
))
1516 WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p
)));
1523 if (archive_format
== V7_FORMAT
)
1526 current_stat
.st_size
= 0; /* force 0 size */
1527 header
= start_header (p
, ¤t_stat
);
1528 header
->header
.typeflag
= type
;
1530 if (type
!= FIFOTYPE
)
1532 MAJOR_TO_CHARS (major (current_stat
.st_rdev
), header
->header
.devmajor
);
1533 MINOR_TO_CHARS (minor (current_stat
.st_rdev
), header
->header
.devminor
);
1536 finish_header (header
);
1537 if (remove_files_option
)
1539 if (unlink (p
) == -1)
1545 WARN ((0, 0, _("%s: Unknown file type; file ignored"),
1546 quotearg_colon (p
)));
1547 if (! ignore_failed_read_option
)
1548 exit_status
= TAREXIT_FAILURE
;