Implement window size passing between real tty and virtual console.
[dragonfly/vkernel-mp.git] / contrib / tar / src / create.c
blob76f79214806c9aee0ca7f89537419fa5b35495f6
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
8 version.
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. */
19 /* $FreeBSD: src/contrib/tar/src/create.c,v 1.2.2.1 2002/07/14 13:19:44 sobomax Exp $ */
20 /* $DragonFly: src/contrib/tar/src/create.c,v 1.2 2003/06/17 06:24:06 dillon Exp $ */
22 #include "system.h"
24 #if !MSDOS
25 # include <pwd.h>
26 # include <grp.h>
27 #endif
29 #if HAVE_UTIME_H
30 # include <utime.h>
31 #else
32 struct utimbuf
34 long actime;
35 long modtime;
37 #endif
39 #include <quotearg.h>
41 #include "common.h"
42 #include <hash.h>
44 #ifndef MSDOS
45 extern dev_t ar_dev;
46 extern ino_t ar_ino;
47 #endif
49 struct link
51 dev_t dev;
52 ino_t ino;
53 char name[1];
56 /* The maximum uintmax_t value that can be represented with DIGITS digits,
57 assuming that each digit is BITS_PER_DIGIT wide. */
58 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
59 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
60 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
61 : (uintmax_t) -1)
63 /* Convert VALUE to an octal representation suitable for tar headers.
64 Output to buffer WHERE with size SIZE.
65 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
67 static void
68 to_octal (uintmax_t value, char *where, size_t size)
70 uintmax_t v = value;
71 size_t i = size;
75 where[--i] = '0' + (v & ((1 << LG_8) - 1));
76 v >>= LG_8;
78 while (i);
81 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
82 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
83 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
84 The result is undefined if SIZE is 0 or if VALUE is too large to
85 fit. */
87 static void
88 to_base256 (int negative, uintmax_t value, char *where, size_t size)
90 uintmax_t v = value;
91 uintmax_t propagated_sign_bits =
92 ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
93 size_t i = size;
97 where[--i] = v & ((1 << LG_256) - 1);
98 v = propagated_sign_bits | (v >> LG_256);
100 while (i);
103 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
104 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
105 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
106 negative before being cast to uintmax_t; its original bitpattern
107 can be deduced from VALSIZE, its original size before casting.
108 TYPE is the kind of value being output (useful for diagnostics).
109 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
110 digits), followed by '\0'. If this won't work, and if GNU or
111 OLDGNU format is allowed, use '\200' followed by base-256, or (if
112 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
113 If neither format works, use SUBSTITUTE (...) instead. Pass to
114 SUBSTITUTE the address of an 0-or-1 flag recording whether the
115 substitute value is negative. */
117 static void
118 to_chars (int negative, uintmax_t value, size_t valsize,
119 uintmax_t (*substitute) PARAMS ((int *)),
120 char *where, size_t size, const char *type)
122 int base256_allowed = (archive_format == GNU_FORMAT
123 || archive_format == OLDGNU_FORMAT);
125 /* Generate the POSIX octal representation if the number fits. */
126 if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
128 where[size - 1] = '\0';
129 to_octal (value, where, size - 1);
132 /* Otherwise, generate the base-256 representation if we are
133 generating an old or new GNU format and if the number fits. */
134 else if (((negative ? -1 - value : value)
135 <= MAX_VAL_WITH_DIGITS (size - 1, LG_256))
136 && base256_allowed)
138 where[0] = negative ? -1 : 1 << (LG_256 - 1);
139 to_base256 (negative, value, where + 1, size - 1);
142 /* Otherwise, if the number is negative, and if it would not cause
143 ambiguity on this host by confusing positive with negative
144 values, then generate the POSIX octal representation of the value
145 modulo 2**(field bits). The resulting tar file is
146 machine-dependent, since it depends on the host word size. Yuck!
147 But this is the traditional behavior. */
148 else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
150 static int warned_once;
151 if (! warned_once)
153 warned_once = 1;
154 WARN ((0, 0, _("Generating negative octal headers")));
156 where[size - 1] = '\0';
157 to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
158 where, size - 1);
161 /* Otherwise, output a substitute value if possible (with a
162 warning), and an error message if not. */
163 else
165 uintmax_t maxval = (base256_allowed
166 ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
167 : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
168 char valbuf[UINTMAX_STRSIZE_BOUND + 1];
169 char maxbuf[UINTMAX_STRSIZE_BOUND];
170 char minbuf[UINTMAX_STRSIZE_BOUND + 1];
171 char const *minval_string;
172 char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
173 char const *value_string;
175 if (base256_allowed)
177 uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
178 char *p = STRINGIFY_BIGINT (m, minbuf + 1);
179 *--p = '-';
180 minval_string = p;
182 else
183 minval_string = "0";
185 if (negative)
187 char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
188 *--p = '-';
189 value_string = p;
191 else
192 value_string = STRINGIFY_BIGINT (value, valbuf);
194 if (substitute)
196 int negsub;
197 uintmax_t sub = substitute (&negsub) & maxval;
198 uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
199 char subbuf[UINTMAX_STRSIZE_BOUND + 1];
200 char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
201 if (negsub)
202 *--sub_string = '-';
203 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
204 value_string, type, minval_string, maxval_string,
205 sub_string));
206 to_chars (negsub, s, valsize, 0, where, size, type);
208 else
209 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
210 value_string, type, minval_string, maxval_string));
214 static uintmax_t
215 gid_substitute (int *negative)
217 gid_t r;
218 #ifdef GID_NOBODY
219 r = GID_NOBODY;
220 #else
221 static gid_t gid_nobody;
222 if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
223 gid_nobody = -2;
224 r = gid_nobody;
225 #endif
226 *negative = r < 0;
227 return r;
230 void
231 gid_to_chars (gid_t v, char *p, size_t s)
233 to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
236 void
237 major_to_chars (major_t v, char *p, size_t s)
239 to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
242 void
243 minor_to_chars (minor_t v, char *p, size_t s)
245 to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
248 void
249 mode_to_chars (mode_t v, char *p, size_t s)
251 /* In the common case where the internal and external mode bits are the same,
252 and we are not using POSIX or GNU format,
253 propagate all unknown bits to the external mode.
254 This matches historical practice.
255 Otherwise, just copy the bits we know about. */
256 int negative;
257 uintmax_t u;
258 if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
259 && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
260 && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
261 && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
262 && archive_format != POSIX_FORMAT
263 && archive_format != GNU_FORMAT)
265 negative = v < 0;
266 u = v;
268 else
270 negative = 0;
271 u = ((v & S_ISUID ? TSUID : 0)
272 | (v & S_ISGID ? TSGID : 0)
273 | (v & S_ISVTX ? TSVTX : 0)
274 | (v & S_IRUSR ? TUREAD : 0)
275 | (v & S_IWUSR ? TUWRITE : 0)
276 | (v & S_IXUSR ? TUEXEC : 0)
277 | (v & S_IRGRP ? TGREAD : 0)
278 | (v & S_IWGRP ? TGWRITE : 0)
279 | (v & S_IXGRP ? TGEXEC : 0)
280 | (v & S_IROTH ? TOREAD : 0)
281 | (v & S_IWOTH ? TOWRITE : 0)
282 | (v & S_IXOTH ? TOEXEC : 0));
284 to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
287 void
288 off_to_chars (off_t v, char *p, size_t s)
290 to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
293 void
294 size_to_chars (size_t v, char *p, size_t s)
296 to_chars (0, (uintmax_t) v, sizeof v, 0, p, s, "size_t");
299 void
300 time_to_chars (time_t v, char *p, size_t s)
302 to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
305 static uintmax_t
306 uid_substitute (int *negative)
308 uid_t r;
309 #ifdef UID_NOBODY
310 r = UID_NOBODY;
311 #else
312 static uid_t uid_nobody;
313 if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
314 uid_nobody = -2;
315 r = uid_nobody;
316 #endif
317 *negative = r < 0;
318 return r;
321 void
322 uid_to_chars (uid_t v, char *p, size_t s)
324 to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
327 void
328 uintmax_to_chars (uintmax_t v, char *p, size_t s)
330 to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
333 /* Writing routines. */
335 /* Zero out the buffer so we don't confuse ourselves with leftover
336 data. */
337 static void
338 clear_buffer (char *buffer)
340 memset (buffer, 0, BLOCKSIZE);
343 /* Write the EOT block(s). Zero at least two blocks, through the end
344 of the record. Old tar, as previous versions of GNU tar, writes
345 garbage after two zeroed blocks. */
346 void
347 write_eot (void)
349 union block *pointer = find_next_block ();
350 memset (pointer->buffer, 0, BLOCKSIZE);
351 set_next_block_after (pointer);
352 pointer = find_next_block ();
353 memset (pointer->buffer, 0, available_space_after (pointer));
354 set_next_block_after (pointer);
357 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
359 /* FIXME: Cross recursion between start_header and write_long! */
361 static union block *start_header PARAMS ((const char *, struct stat *));
363 static void
364 write_long (const char *p, char type)
366 size_t size = strlen (p) + 1;
367 size_t bufsize;
368 union block *header;
369 struct stat foo;
371 memset (&foo, 0, sizeof foo);
372 foo.st_size = size;
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);
385 p += bufsize;
386 size -= 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 /* Return a suffix of the file NAME that is a relative file name.
397 Warn about `..' in file names. But return NAME if the user wants
398 absolute file names. */
399 static char const *
400 relativize (char const *name)
402 if (! absolute_names_option)
405 static int warned_once;
406 if (! warned_once && contains_dot_dot (name))
408 warned_once = 1;
409 WARN ((0, 0, _("Member names contain `..'")));
414 size_t prefix_len = FILESYSTEM_PREFIX_LEN (name);
416 while (ISSLASH (name[prefix_len]))
417 prefix_len++;
419 if (prefix_len)
421 static int warned_once;
422 if (!warned_once)
424 warned_once = 1;
425 WARN ((0, 0, _("Removing leading `%.*s' from member names"),
426 (int) prefix_len, name));
428 name += prefix_len;
433 return name;
436 /* Header handling. */
438 /* Make a header block for the file whose stat info is st,
439 and return its address. */
441 static union block *
442 start_header (const char *name, struct stat *st)
444 union block *header;
446 name = relativize (name);
448 if (sizeof header->header.name <= strlen (name))
449 write_long (name, GNUTYPE_LONGNAME);
450 header = find_next_block ();
451 memset (header->buffer, 0, sizeof (union block));
453 assign_string (&current_file_name, name);
455 strncpy (header->header.name, name, NAME_FIELD_SIZE);
456 header->header.name[NAME_FIELD_SIZE - 1] = '\0';
458 /* Override some stat fields, if requested to do so. */
460 if (owner_option != (uid_t) -1)
461 st->st_uid = owner_option;
462 if (group_option != (gid_t) -1)
463 st->st_gid = group_option;
464 if (mode_option)
465 st->st_mode = ((st->st_mode & ~MODE_ALL)
466 | mode_adjust (st->st_mode, mode_option));
468 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
469 for a few tars and came up with the following interoperability
470 matrix:
472 WRITER
473 1 2 3 4 5 6 7 8 9 READER
474 . . . . . . . . . 1 = SunOS 4.2 tar
475 # . . # # . . # # 2 = NEC SVR4.0.2 tar
476 . . . # # . . # . 3 = Solaris 2.1 tar
477 . . . . . . . . . 4 = GNU tar 1.11.1
478 . . . . . . . . . 5 = HP-UX 8.07 tar
479 . . . . . . . . . 6 = Ultrix 4.1
480 . . . . . . . . . 7 = AIX 3.2
481 . . . . . . . . . 8 = Hitachi HI-UX 1.03
482 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
484 . = works
485 # = ``impossible file type''
487 The following mask for old archive removes the `#'s in column 4
488 above, thus making GNU tar both a universal donor and a universal
489 acceptor for Paul's test. */
491 if (archive_format == V7_FORMAT)
492 MODE_TO_CHARS (st->st_mode & MODE_ALL, header->header.mode);
493 else
494 MODE_TO_CHARS (st->st_mode, header->header.mode);
496 UID_TO_CHARS (st->st_uid, header->header.uid);
497 GID_TO_CHARS (st->st_gid, header->header.gid);
498 OFF_TO_CHARS (st->st_size, header->header.size);
499 TIME_TO_CHARS (st->st_mtime, header->header.mtime);
501 if (incremental_option)
502 if (archive_format == OLDGNU_FORMAT)
504 TIME_TO_CHARS (st->st_atime, header->oldgnu_header.atime);
505 TIME_TO_CHARS (st->st_ctime, header->oldgnu_header.ctime);
508 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
510 switch (archive_format)
512 case V7_FORMAT:
513 break;
515 case OLDGNU_FORMAT:
516 /* Overwrite header->header.magic and header.version in one blow. */
517 strcpy (header->header.magic, OLDGNU_MAGIC);
518 break;
520 case POSIX_FORMAT:
521 case GNU_FORMAT:
522 strncpy (header->header.magic, TMAGIC, TMAGLEN);
523 strncpy (header->header.version, TVERSION, TVERSLEN);
524 break;
526 default:
527 abort ();
530 if (archive_format == V7_FORMAT || numeric_owner_option)
532 /* header->header.[ug]name are left as the empty string. */
534 else
536 uid_to_uname (st->st_uid, header->header.uname);
537 gid_to_gname (st->st_gid, header->header.gname);
540 return header;
543 /* Finish off a filled-in header block and write it out. We also
544 print the file name and/or full info if verbose is on. */
545 void
546 finish_header (union block *header)
548 size_t i;
549 int sum;
550 char *p;
552 memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
554 sum = 0;
555 p = header->buffer;
556 for (i = sizeof *header; i-- != 0; )
557 /* We can't use unsigned char here because of old compilers, e.g. V7. */
558 sum += 0xFF & *p++;
560 /* Fill in the checksum field. It's formatted differently from the
561 other fields: it has [6] digits, a null, then a space -- rather than
562 digits, then a null. We use to_chars.
563 The final space is already there, from
564 checksumming, and to_chars doesn't modify it.
566 This is a fast way to do:
568 sprintf(header->header.chksum, "%6o", sum); */
570 uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
572 if (verbose_option
573 && header->header.typeflag != GNUTYPE_LONGLINK
574 && header->header.typeflag != GNUTYPE_LONGNAME)
576 /* These globals are parameters to print_header, sigh. */
578 current_header = header;
579 /* current_stat is already set up. */
580 current_format = archive_format;
581 print_header ();
584 set_next_block_after (header);
587 /* Sparse file processing. */
589 /* Takes a blockful of data and basically cruises through it to see if
590 it's made *entirely* of zeros, returning a 0 the instant it finds
591 something that is a nonzero, i.e., useful data. */
592 static int
593 zero_block_p (char *buffer)
595 int counter;
597 for (counter = 0; counter < BLOCKSIZE; counter++)
598 if (buffer[counter] != '\0')
599 return 0;
600 return 1;
603 static void
604 init_sparsearray (void)
606 sp_array_size = 10;
608 /* Make room for our scratch space -- initially is 10 elts long. */
610 sparsearray = xmalloc (sp_array_size * sizeof (struct sp_array));
613 static off_t
614 find_new_file_size (int sparses)
616 int i;
617 off_t s = 0;
618 for (i = 0; i < sparses; i++)
619 s += sparsearray[i].numbytes;
620 return s;
623 /* Make one pass over the file NAME, studying where any non-zero data
624 is, that is, how far into the file each instance of data is, and
625 how many bytes are there. Save this information in the
626 sparsearray, which will later be translated into header
627 information. */
629 /* There is little point in trimming small amounts of null data at the head
630 and tail of blocks, only avoid dumping full null blocks. */
632 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
633 too kludgey for my taste... */
635 static int
636 deal_with_sparse (char *name, union block *header)
638 size_t numbytes = 0;
639 off_t offset = 0;
640 int file;
641 int sparses = 0;
642 ssize_t count;
643 char buffer[BLOCKSIZE];
645 if (archive_format == OLDGNU_FORMAT)
646 header->oldgnu_header.isextended = 0;
648 if (file = open (name, O_RDONLY), file < 0)
649 /* This problem will be caught later on, so just return. */
650 return 0;
652 init_sparsearray ();
653 clear_buffer (buffer);
655 for (;;)
657 /* Realloc the scratch area as necessary. FIXME: should reallocate
658 only at beginning of a new instance of non-zero data. */
660 if (sp_array_size <= sparses)
662 sparsearray =
663 xrealloc (sparsearray,
664 2 * sp_array_size * sizeof (struct sp_array));
665 sp_array_size *= 2;
668 count = safe_read (file, buffer, sizeof buffer);
669 if (count <= 0)
670 break;
672 /* Process one block. */
674 if (count == sizeof buffer)
676 if (zero_block_p (buffer))
678 if (numbytes)
680 sparsearray[sparses++].numbytes = numbytes;
681 numbytes = 0;
684 else
686 if (!numbytes)
687 sparsearray[sparses].offset = offset;
688 numbytes += count;
691 else
693 /* Since count < sizeof buffer, we have the last bit of the file. */
695 if (!zero_block_p (buffer))
697 if (!numbytes)
698 sparsearray[sparses].offset = offset;
699 numbytes += count;
701 else
702 /* The next two lines are suggested by Andreas Degert, who says
703 they are required for trailing full blocks to be written to the
704 archive, when all zeroed. Yet, it seems to me that the case
705 does not apply. Further, at restore time, the file is not as
706 sparse as it should. So, some serious cleanup is *also* needed
707 in this area. Just one more... :-(. FIXME. */
708 if (numbytes)
709 numbytes += count;
711 /* Prepare for next block. */
713 offset += count;
714 /* FIXME: do not clear unless necessary. */
715 clear_buffer (buffer);
718 if (numbytes)
719 sparsearray[sparses++].numbytes = numbytes;
720 else
722 sparsearray[sparses].offset = offset - 1;
723 sparsearray[sparses++].numbytes = 1;
726 return close (file) == 0 && 0 <= count ? sparses : 0;
729 static int
730 finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
732 union block *start;
733 size_t bufsize;
734 int sparses = 0;
735 ssize_t count;
737 while (*sizeleft > 0)
739 start = find_next_block ();
740 memset (start->buffer, 0, BLOCKSIZE);
741 bufsize = sparsearray[sparses].numbytes;
742 if (! bufsize)
743 abort ();
745 if (lseek (file, sparsearray[sparses++].offset, SEEK_SET) < 0)
747 (ignore_failed_read_option ? seek_warn_details : seek_error_details)
748 (name, sparsearray[sparses - 1].offset);
749 break;
752 /* If the number of bytes to be written here exceeds the size of
753 the temporary buffer, do it in steps. */
755 while (bufsize > BLOCKSIZE)
757 count = safe_read (file, start->buffer, BLOCKSIZE);
758 if (count < 0)
760 (ignore_failed_read_option
761 ? read_warn_details
762 : read_error_details)
763 (name, fullsize - *sizeleft, bufsize);
764 return 1;
766 bufsize -= count;
767 *sizeleft -= count;
768 set_next_block_after (start);
769 start = find_next_block ();
770 memset (start->buffer, 0, BLOCKSIZE);
774 char buffer[BLOCKSIZE];
776 clear_buffer (buffer);
777 count = safe_read (file, buffer, bufsize);
778 memcpy (start->buffer, buffer, BLOCKSIZE);
781 if (count < 0)
783 (ignore_failed_read_option
784 ? read_warn_details
785 : read_error_details)
786 (name, fullsize - *sizeleft, bufsize);
787 return 1;
790 *sizeleft -= count;
791 set_next_block_after (start);
793 free (sparsearray);
794 #if 0
795 set_next_block_after (start + (count - 1) / BLOCKSIZE);
796 #endif
797 return 0;
800 /* Main functions of this module. */
802 void
803 create_archive (void)
805 char *p;
807 open_archive (ACCESS_WRITE);
809 if (incremental_option)
811 size_t buffer_size = 1000;
812 char *buffer = xmalloc (buffer_size);
813 const char *q;
815 collect_and_sort_names ();
817 while (p = name_from_list (), p)
818 if (!excluded_name (p))
819 dump_file (p, -1, (dev_t) 0);
821 blank_name_list ();
822 while (p = name_from_list (), p)
823 if (!excluded_name (p))
825 size_t plen = strlen (p);
826 if (buffer_size <= plen)
828 while ((buffer_size *= 2) <= plen)
829 continue;
830 buffer = xrealloc (buffer, buffer_size);
832 memcpy (buffer, p, plen);
833 if (! ISSLASH (buffer[plen - 1]))
834 buffer[plen++] = '/';
835 q = gnu_list_name->dir_contents;
836 if (q)
837 while (*q)
839 size_t qlen = strlen (q);
840 if (*q == 'Y')
842 if (buffer_size < plen + qlen)
844 while ((buffer_size *=2 ) < plen + qlen)
845 continue;
846 buffer = xrealloc (buffer, buffer_size);
848 strcpy (buffer + plen, q + 1);
849 dump_file (buffer, -1, (dev_t) 0);
851 q += qlen + 1;
854 free (buffer);
856 else
858 while (p = name_next (1), p)
859 if (!excluded_name (p))
860 dump_file (p, 1, (dev_t) 0);
863 write_eot ();
864 close_archive ();
866 if (listed_incremental_option)
867 write_directory_file ();
871 /* Calculate the hash of a link. */
872 static unsigned
873 hash_link (void const *entry, unsigned n_buckets)
875 struct link const *link = entry;
876 return (uintmax_t) (link->dev ^ link->ino) % n_buckets;
879 /* Compare two links for equality. */
880 static bool
881 compare_links (void const *entry1, void const *entry2)
883 struct link const *link1 = entry1;
884 struct link const *link2 = entry2;
885 return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
888 /* Dump a single file, recursing on directories. P is the file name
889 to dump. TOP_LEVEL tells whether this is a top-level call; zero
890 means no, positive means yes, and negative means an incremental
891 dump. PARENT_DEVICE is the device of P's
892 parent directory; it is examined only if TOP_LEVEL is zero.
894 Set global CURRENT_STAT to stat output for this file. */
896 /* FIXME: One should make sure that for *every* path leading to setting
897 exit_status to failure, a clear diagnostic has been issued. */
899 void
900 dump_file (char *p, int top_level, dev_t parent_device)
902 union block *header;
903 char type;
904 union block *exhdr;
905 char save_typeflag;
906 time_t original_ctime;
907 struct utimbuf restore_times;
909 /* FIXME: `header' might be used uninitialized in this
910 function. Reported by Bruno Haible. */
912 if (interactive_option && !confirm ("add", p))
913 return;
915 if (deref_stat (dereference_option, p, &current_stat) != 0)
917 if (ignore_failed_read_option)
918 stat_warn (p);
919 else
920 stat_error (p);
921 return;
924 original_ctime = current_stat.st_ctime;
925 restore_times.actime = current_stat.st_atime;
926 restore_times.modtime = current_stat.st_mtime;
928 #ifdef S_ISHIDDEN
929 if (S_ISHIDDEN (current_stat.st_mode))
931 char *new = (char *) alloca (strlen (p) + 2);
932 if (new)
934 strcpy (new, p);
935 strcat (new, "@");
936 p = new;
939 #endif
941 /* See if we want only new files, and check if this one is too old to
942 put in the archive. */
944 if ((0 < top_level || !incremental_option)
945 && !S_ISDIR (current_stat.st_mode)
946 && current_stat.st_mtime < newer_mtime_option
947 && (!after_date_option || current_stat.st_ctime < newer_ctime_option))
949 if (0 < top_level)
950 WARN ((0, 0, _("%s: file is unchanged; not dumped"),
951 quotearg_colon (p)));
952 /* FIXME: recheck this return. */
953 return;
956 #if !MSDOS
957 /* See if we are trying to dump the archive. */
959 if (ar_dev && current_stat.st_dev == ar_dev && current_stat.st_ino == ar_ino)
961 WARN ((0, 0, _("%s: file is the archive; not dumped"),
962 quotearg_colon (p)));
963 return;
965 #endif
967 if (S_ISDIR (current_stat.st_mode))
969 char *directory;
970 char const *entry;
971 size_t entrylen;
972 char *namebuf;
973 size_t buflen;
974 size_t len;
975 dev_t our_device = current_stat.st_dev;
977 errno = 0;
979 directory = savedir (p);
980 if (! directory)
982 if (ignore_failed_read_option)
983 savedir_warn (p);
984 else
985 savedir_error (p);
986 return;
989 /* Build new prototype name. Ensure exactly one trailing slash. */
991 len = strlen (p);
992 buflen = len + NAME_FIELD_SIZE;
993 namebuf = xmalloc (buflen + 1);
994 memcpy (namebuf, p, len);
995 while (len >= 1 && ISSLASH (namebuf[len - 1]))
996 len--;
997 namebuf[len++] = '/';
998 namebuf[len] = '\0';
1000 if (! is_avoided_name (namebuf))
1002 /* The condition above used to be "archive_format != V7_FORMAT".
1003 GNU tar was not writing directory blocks at all. Daniel Trinkle
1004 writes: ``All old versions of tar I have ever seen have
1005 correctly archived an empty directory. The really old ones I
1006 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1007 some subtle reason for the exclusion that I don't know, but the
1008 current behavior is broken.'' I do not know those subtle
1009 reasons either, so until these are reported (anew?), just allow
1010 directory blocks to be written even with old archives. */
1012 current_stat.st_size = 0; /* force 0 size on dir */
1014 /* FIXME: If people could really read standard archives, this
1015 should be:
1017 header
1018 = start_header (standard_option ? p : namebuf, &current_stat);
1020 but since they'd interpret DIRTYPE blocks as regular
1021 files, we'd better put the / on the name. */
1023 header = start_header (namebuf, &current_stat);
1025 if (incremental_option)
1026 header->header.typeflag = GNUTYPE_DUMPDIR;
1027 else /* if (standard_option) */
1028 header->header.typeflag = DIRTYPE;
1030 /* If we're gnudumping, we aren't done yet so don't close it. */
1032 if (!incremental_option)
1033 finish_header (header); /* done with directory header */
1036 if (incremental_option && gnu_list_name->dir_contents)
1038 off_t sizeleft;
1039 off_t totsize;
1040 size_t bufsize;
1041 union block *start;
1042 ssize_t count;
1043 const char *buffer, *p_buffer;
1045 buffer = gnu_list_name->dir_contents; /* FOO */
1046 totsize = 0;
1047 for (p_buffer = buffer; p_buffer && *p_buffer;)
1049 size_t tmp;
1051 tmp = strlen (p_buffer) + 1;
1052 totsize += tmp;
1053 p_buffer += tmp;
1055 totsize++;
1056 OFF_TO_CHARS (totsize, header->header.size);
1057 finish_header (header);
1058 p_buffer = buffer;
1059 sizeleft = totsize;
1060 while (sizeleft > 0)
1062 if (multi_volume_option)
1064 assign_string (&save_name, p);
1065 save_sizeleft = sizeleft;
1066 save_totsize = totsize;
1068 start = find_next_block ();
1069 bufsize = available_space_after (start);
1070 if (sizeleft < bufsize)
1072 bufsize = sizeleft;
1073 count = bufsize % BLOCKSIZE;
1074 if (count)
1075 memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
1077 memcpy (start->buffer, p_buffer, bufsize);
1078 sizeleft -= bufsize;
1079 p_buffer += bufsize;
1080 set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1082 if (multi_volume_option)
1083 assign_string (&save_name, 0);
1084 goto finish_dir;
1087 /* See if we are about to recurse into a directory, and avoid doing
1088 so if the user wants that we do not descend into directories. */
1090 if (! recursion_option)
1091 goto finish_dir;
1093 /* See if we are crossing from one file system to another, and
1094 avoid doing so if the user only wants to dump one file system. */
1096 if (one_file_system_option && !top_level
1097 && parent_device != current_stat.st_dev)
1099 if (verbose_option)
1100 WARN ((0, 0,
1101 _("%s: file is on a different filesystem; not dumped"),
1102 quotearg_colon (p)));
1103 goto finish_dir;
1106 /* Now output all the files in the directory. */
1108 /* FIXME: Should speed this up by cd-ing into the dir. */
1110 for (entry = directory;
1111 (entrylen = strlen (entry)) != 0;
1112 entry += entrylen + 1)
1114 if (buflen <= len + entrylen)
1116 buflen = len + entrylen;
1117 namebuf = xrealloc (namebuf, buflen + 1);
1119 strcpy (namebuf + len, entry);
1120 if (!excluded_name (namebuf))
1121 dump_file (namebuf, 0, our_device);
1124 finish_dir:
1126 free (directory);
1127 free (namebuf);
1128 if (atime_preserve_option)
1129 utime (p, &restore_times);
1130 return;
1132 else if (is_avoided_name (p))
1133 return;
1134 else
1136 /* Check for multiple links.
1138 We maintain a table of all such files that we've written so
1139 far. Any time we see another, we check the table and avoid
1140 dumping the data again if we've done it once already. */
1142 if (1 < current_stat.st_nlink)
1144 static Hash_table *link_table;
1145 struct link *lp = xmalloc (offsetof (struct link, name)
1146 + strlen (p) + 1);
1147 struct link *dup;
1148 lp->ino = current_stat.st_ino;
1149 lp->dev = current_stat.st_dev;
1150 strcpy (lp->name, p);
1152 if (! ((link_table
1153 || (link_table = hash_initialize (0, 0, hash_link,
1154 compare_links, 0)))
1155 && (dup = hash_insert (link_table, lp))))
1156 xalloc_die ();
1158 if (dup != lp)
1160 /* We found a link. */
1161 char const *link_name = relativize (dup->name);
1163 free (lp);
1165 if (NAME_FIELD_SIZE <= strlen (link_name))
1166 write_long (link_name, GNUTYPE_LONGLINK);
1167 assign_string (&current_link_name, link_name);
1169 current_stat.st_size = 0;
1170 header = start_header (p, &current_stat);
1171 strncpy (header->header.linkname, link_name, NAME_FIELD_SIZE);
1173 /* Force null termination. */
1174 header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
1176 header->header.typeflag = LNKTYPE;
1177 finish_header (header);
1179 /* FIXME: Maybe remove from table after all links found? */
1181 if (remove_files_option && unlink (p) != 0)
1182 unlink_error (p);
1184 /* We dumped it. */
1185 return;
1189 /* This is not a link to a previously dumped file, so dump it. */
1191 if (S_ISREG (current_stat.st_mode)
1192 || S_ISCTG (current_stat.st_mode))
1194 int f; /* file descriptor */
1195 size_t bufsize;
1196 ssize_t count;
1197 off_t sizeleft;
1198 union block *start;
1199 int header_moved;
1200 char isextended = 0;
1201 int sparses = 0;
1203 header_moved = 0;
1205 if (sparse_option)
1207 /* Check the size of the file against the number of blocks
1208 allocated for it, counting both data and indirect blocks.
1209 If there is a smaller number of blocks that would be
1210 necessary to accommodate a file of this size, this is safe
1211 to say that we have a sparse file: at least one of those
1212 blocks in the file is just a useless hole. For sparse
1213 files not having more hole blocks than indirect blocks, the
1214 sparseness will go undetected. */
1216 /* Bruno Haible sent me these statistics for Linux. It seems
1217 that some filesystems count indirect blocks in st_blocks,
1218 while others do not seem to:
1220 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1221 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1222 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1223 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1225 Dick Streefland reports the previous numbers as misleading,
1226 because ext2fs use 12 direct blocks, while minix-fs uses only
1227 6 direct blocks. Dick gets:
1229 ext2 size=20480 ls listed blocks=21
1230 minix size=20480 ls listed blocks=21
1231 msdos size=20480 ls listed blocks=20
1233 It seems that indirect blocks *are* included in st_blocks.
1234 The minix filesystem does not account for phantom blocks in
1235 st_blocks, so `du' and `ls -s' give wrong results. So, the
1236 --sparse option would not work on a minix filesystem. */
1238 if (ST_NBLOCKS (current_stat)
1239 < (current_stat.st_size / ST_NBLOCKSIZE
1240 + (current_stat.st_size % ST_NBLOCKSIZE != 0)))
1242 int counter;
1244 header = start_header (p, &current_stat);
1245 header->header.typeflag = GNUTYPE_SPARSE;
1246 header_moved = 1;
1248 /* Call the routine that figures out the layout of the
1249 sparse file in question. SPARSES is the index of the
1250 first unused element of the "sparsearray," i.e.,
1251 the number of elements it needed to describe the file. */
1253 sparses = deal_with_sparse (p, header);
1255 /* See if we'll need an extended header later. */
1257 if (SPARSES_IN_OLDGNU_HEADER < sparses)
1258 header->oldgnu_header.isextended = 1;
1260 /* We store the "real" file size so we can show that in
1261 case someone wants to list the archive, i.e., tar tvf
1262 <file>. It might be kind of disconcerting if the
1263 shrunken file size was the one that showed up. */
1265 OFF_TO_CHARS (current_stat.st_size,
1266 header->oldgnu_header.realsize);
1268 /* This will be the new "size" of the file, i.e., the size
1269 of the file minus the blocks of holes that we're
1270 skipping over. */
1272 current_stat.st_size = find_new_file_size (sparses);
1273 OFF_TO_CHARS (current_stat.st_size, header->header.size);
1275 for (counter = 0;
1276 counter < sparses && counter < SPARSES_IN_OLDGNU_HEADER;
1277 counter++)
1279 OFF_TO_CHARS (sparsearray[counter].offset,
1280 header->oldgnu_header.sp[counter].offset);
1281 SIZE_TO_CHARS (sparsearray[counter].numbytes,
1282 header->oldgnu_header.sp[counter].numbytes);
1287 sizeleft = current_stat.st_size;
1289 /* Don't bother opening empty, world readable files. Also do not open
1290 files when archive is meant for /dev/null. */
1292 if (dev_null_output
1293 || (sizeleft == 0
1294 && MODE_R == (MODE_R & current_stat.st_mode)))
1295 f = -1;
1296 else
1298 f = open (p, O_RDONLY | O_BINARY);
1299 if (f < 0)
1301 if (! top_level && errno == ENOENT)
1302 WARN ((0, 0, _("%s: File removed before we read it"),
1303 quotearg_colon (p)));
1304 else
1305 (ignore_failed_read_option ? open_warn : open_error) (p);
1306 return;
1310 /* If the file is sparse, we've already taken care of this. */
1312 if (!header_moved)
1313 header = start_header (p, &current_stat);
1315 /* Mark contiguous files, if we support them. */
1317 if (archive_format != V7_FORMAT && S_ISCTG (current_stat.st_mode))
1318 header->header.typeflag = CONTTYPE;
1320 isextended = header->oldgnu_header.isextended;
1321 save_typeflag = header->header.typeflag;
1322 finish_header (header);
1323 if (isextended)
1325 int sparses_emitted = SPARSES_IN_OLDGNU_HEADER;
1327 for (;;)
1329 int i;
1330 exhdr = find_next_block ();
1331 memset (exhdr->buffer, 0, BLOCKSIZE);
1332 for (i = 0;
1333 (i < SPARSES_IN_SPARSE_HEADER
1334 && sparses_emitted + i < sparses);
1335 i++)
1337 SIZE_TO_CHARS (sparsearray[sparses_emitted + i].numbytes,
1338 exhdr->sparse_header.sp[i].numbytes);
1339 OFF_TO_CHARS (sparsearray[sparses_emitted + i].offset,
1340 exhdr->sparse_header.sp[i].offset);
1342 set_next_block_after (exhdr);
1343 sparses_emitted += i;
1344 if (sparses == sparses_emitted)
1345 break;
1346 exhdr->sparse_header.isextended = 1;
1349 if (save_typeflag == GNUTYPE_SPARSE)
1351 if (f < 0
1352 || finish_sparse_file (f, &sizeleft,
1353 current_stat.st_size, p))
1354 goto padit;
1356 else
1357 while (sizeleft > 0)
1359 if (multi_volume_option)
1361 assign_string (&save_name, p);
1362 save_sizeleft = sizeleft;
1363 save_totsize = current_stat.st_size;
1365 start = find_next_block ();
1367 bufsize = available_space_after (start);
1369 if (sizeleft < bufsize)
1371 /* Last read -- zero out area beyond. */
1373 bufsize = sizeleft;
1374 count = bufsize % BLOCKSIZE;
1375 if (count)
1376 memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
1378 if (f < 0)
1379 count = bufsize;
1380 else
1381 count = safe_read (f, start->buffer, bufsize);
1382 if (count < 0)
1384 (ignore_failed_read_option
1385 ? read_warn_details
1386 : read_error_details)
1387 (p, current_stat.st_size - sizeleft, bufsize);
1388 goto padit;
1390 sizeleft -= bufsize;
1392 /* This is nonportable (the type of set_next_block_after's arg). */
1394 set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1397 if (count != bufsize)
1399 char buf[UINTMAX_STRSIZE_BOUND];
1400 memset (start->buffer + count, 0, bufsize - count);
1401 WARN ((0, 0,
1402 _("%s: File shrank by %s bytes; padding with zeros"),
1403 quotearg_colon (p),
1404 STRINGIFY_BIGINT (sizeleft, buf)));
1405 if (! ignore_failed_read_option)
1406 exit_status = TAREXIT_FAILURE;
1407 goto padit; /* short read */
1411 if (multi_volume_option)
1412 assign_string (&save_name, 0);
1414 if (f >= 0)
1416 struct stat final_stat;
1417 if (fstat (f, &final_stat) != 0)
1419 if (ignore_failed_read_option)
1420 stat_warn (p);
1421 else
1422 stat_error (p);
1424 else if (final_stat.st_ctime != original_ctime)
1426 char const *qp = quotearg_colon (p);
1427 WARN ((0, 0, _("%s: file changed as we read it"), qp));
1428 if (! ignore_failed_read_option)
1429 exit_status = TAREXIT_FAILURE;
1431 if (close (f) != 0)
1433 if (ignore_failed_read_option)
1434 close_warn (p);
1435 else
1436 close_error (p);
1438 if (atime_preserve_option)
1439 utime (p, &restore_times);
1441 if (remove_files_option)
1443 if (unlink (p) == -1)
1444 unlink_error (p);
1446 return;
1448 /* File shrunk or gave error, pad out tape to match the size we
1449 specified in the header. */
1451 padit:
1452 while (sizeleft > 0)
1454 save_sizeleft = sizeleft;
1455 start = find_next_block ();
1456 memset (start->buffer, 0, BLOCKSIZE);
1457 set_next_block_after (start);
1458 sizeleft -= BLOCKSIZE;
1460 if (multi_volume_option)
1461 assign_string (&save_name, 0);
1462 if (f >= 0)
1464 close (f);
1465 if (atime_preserve_option)
1466 utime (p, &restore_times);
1468 return;
1470 #ifdef HAVE_READLINK
1471 else if (S_ISLNK (current_stat.st_mode))
1473 char *buffer;
1474 int size;
1475 size_t linklen = current_stat.st_size;
1476 if (linklen != current_stat.st_size || linklen + 1 == 0)
1477 xalloc_die ();
1478 buffer = (char *) alloca (linklen + 1);
1479 size = readlink (p, buffer, linklen);
1480 if (size < 0)
1482 if (ignore_failed_read_option)
1483 readlink_warn (p);
1484 else
1485 readlink_error (p);
1486 return;
1488 buffer[size] = '\0';
1489 if (size >= NAME_FIELD_SIZE)
1490 write_long (buffer, GNUTYPE_LONGLINK);
1491 assign_string (&current_link_name, buffer);
1493 current_stat.st_size = 0; /* force 0 size on symlink */
1494 header = start_header (p, &current_stat);
1495 strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
1496 header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
1497 header->header.typeflag = SYMTYPE;
1498 finish_header (header); /* nothing more to do to it */
1499 if (remove_files_option)
1501 if (unlink (p) == -1)
1502 unlink_error (p);
1504 return;
1506 #endif
1507 else if (S_ISCHR (current_stat.st_mode))
1508 type = CHRTYPE;
1509 else if (S_ISBLK (current_stat.st_mode))
1510 type = BLKTYPE;
1511 else if (S_ISFIFO (current_stat.st_mode))
1512 type = FIFOTYPE;
1513 else if (S_ISSOCK (current_stat.st_mode))
1515 WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p)));
1516 return;
1518 else if (S_ISDOOR (current_stat.st_mode))
1520 WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p)));
1521 return;
1523 else
1524 goto unknown;
1527 if (archive_format == V7_FORMAT)
1528 goto unknown;
1530 current_stat.st_size = 0; /* force 0 size */
1531 header = start_header (p, &current_stat);
1532 header->header.typeflag = type;
1534 if (type != FIFOTYPE)
1536 MAJOR_TO_CHARS (major (current_stat.st_rdev), header->header.devmajor);
1537 MINOR_TO_CHARS (minor (current_stat.st_rdev), header->header.devminor);
1540 finish_header (header);
1541 if (remove_files_option)
1543 if (unlink (p) == -1)
1544 unlink_error (p);
1546 return;
1548 unknown:
1549 WARN ((0, 0, _("%s: Unknown file type; file ignored"),
1550 quotearg_colon (p)));
1551 if (! ignore_failed_read_option)
1552 exit_status = TAREXIT_FAILURE;