]> Dogcows Code - chaz/tar/blob - src/create.c
Fix handling of files removed during incremental dumps.
[chaz/tar] / src / create.c
1 /* Create a tar archive.
2
3 Copyright (C) 1985, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
4 2003, 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5
6 Written by John Gilmore, on 1985-08-25.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21
22 #include <system.h>
23
24 #include <quotearg.h>
25
26 #include "common.h"
27 #include <hash.h>
28
29 struct link
30 {
31 dev_t dev;
32 ino_t ino;
33 size_t nlink;
34 char name[1];
35 };
36
37 struct exclusion_tag
38 {
39 const char *name;
40 size_t length;
41 enum exclusion_tag_type type;
42 bool (*predicate) (const char *name);
43 struct exclusion_tag *next;
44 };
45
46 static struct exclusion_tag *exclusion_tags;
47
48 void
49 add_exclusion_tag (const char *name, enum exclusion_tag_type type,
50 bool (*predicate) (const char *name))
51 {
52 struct exclusion_tag *tag = xmalloc (sizeof tag[0]);
53 tag->next = exclusion_tags;
54 tag->name = name;
55 tag->type = type;
56 tag->predicate = predicate;
57 tag->length = strlen (name);
58 exclusion_tags = tag;
59 }
60
61 void
62 exclusion_tag_warning (const char *dirname, const char *tagname,
63 const char *message)
64 {
65 if (verbose_option)
66 WARNOPT (WARN_CACHEDIR,
67 (0, 0,
68 _("%s: contains a cache directory tag %s; %s"),
69 quotearg_colon (dirname),
70 quotearg_n (1, tagname),
71 message));
72 }
73
74 enum exclusion_tag_type
75 check_exclusion_tags (const char *dirname, const char **tag_file_name)
76 {
77 static char *tagname;
78 static size_t tagsize;
79 struct exclusion_tag *tag;
80 size_t dlen = strlen (dirname);
81 int addslash = !ISSLASH (dirname[dlen-1]);
82 char *nptr = NULL;
83
84 for (tag = exclusion_tags; tag; tag = tag->next)
85 {
86 size_t size = dlen + addslash + tag->length + 1;
87 if (size > tagsize)
88 {
89 tagsize = size;
90 tagname = xrealloc (tagname, tagsize);
91 }
92
93 if (!nptr)
94 {
95 strcpy (tagname, dirname);
96 nptr = tagname + dlen;
97 if (addslash)
98 *nptr++ = '/';
99 }
100 strcpy (nptr, tag->name);
101 if (access (tagname, F_OK) == 0
102 && (!tag->predicate || tag->predicate (tagname)))
103 {
104 if (tag_file_name)
105 *tag_file_name = tag->name;
106 return tag->type;
107 }
108 }
109
110 return exclusion_tag_none;
111 }
112
113 /* Exclusion predicate to test if the named file (usually "CACHEDIR.TAG")
114 contains a valid header, as described at:
115 http://www.brynosaurus.com/cachedir
116 Applications can write this file into directories they create
117 for use as caches containing purely regenerable, non-precious data,
118 allowing us to avoid archiving them if --exclude-caches is specified. */
119
120 #define CACHEDIR_SIGNATURE "Signature: 8a477f597d28d172789f06886806bc55"
121 #define CACHEDIR_SIGNATURE_SIZE (sizeof CACHEDIR_SIGNATURE - 1)
122
123 bool
124 cachedir_file_p (const char *name)
125 {
126 bool tag_present = false;
127 int fd = open (name, O_RDONLY);
128 if (fd >= 0)
129 {
130 static char tagbuf[CACHEDIR_SIGNATURE_SIZE];
131
132 if (read (fd, tagbuf, CACHEDIR_SIGNATURE_SIZE)
133 == CACHEDIR_SIGNATURE_SIZE
134 && memcmp (tagbuf, CACHEDIR_SIGNATURE, CACHEDIR_SIGNATURE_SIZE) == 0)
135 tag_present = true;
136
137 close (fd);
138 }
139 return tag_present;
140 }
141
142 \f
143 /* The maximum uintmax_t value that can be represented with DIGITS digits,
144 assuming that each digit is BITS_PER_DIGIT wide. */
145 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
146 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
147 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
148 : (uintmax_t) -1)
149
150 /* The maximum uintmax_t value that can be represented with octal
151 digits and a trailing NUL in BUFFER. */
152 #define MAX_OCTAL_VAL(buffer) MAX_VAL_WITH_DIGITS (sizeof (buffer) - 1, LG_8)
153
154 /* Convert VALUE to an octal representation suitable for tar headers.
155 Output to buffer WHERE with size SIZE.
156 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
157
158 static void
159 to_octal (uintmax_t value, char *where, size_t size)
160 {
161 uintmax_t v = value;
162 size_t i = size;
163
164 do
165 {
166 where[--i] = '0' + (v & ((1 << LG_8) - 1));
167 v >>= LG_8;
168 }
169 while (i);
170 }
171
172 /* Copy at most LEN bytes from the string SRC to DST. Terminate with
173 NUL unless SRC is LEN or more bytes long. */
174
175 static void
176 tar_copy_str (char *dst, const char *src, size_t len)
177 {
178 size_t i;
179 for (i = 0; i < len; i++)
180 if (! (dst[i] = src[i]))
181 break;
182 }
183
184 /* Same as tar_copy_str, but always terminate with NUL if using
185 is OLDGNU format */
186
187 static void
188 tar_name_copy_str (char *dst, const char *src, size_t len)
189 {
190 tar_copy_str (dst, src, len);
191 if (archive_format == OLDGNU_FORMAT)
192 dst[len-1] = 0;
193 }
194
195 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
196 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
197 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
198 The result is undefined if SIZE is 0 or if VALUE is too large to
199 fit. */
200
201 static void
202 to_base256 (int negative, uintmax_t value, char *where, size_t size)
203 {
204 uintmax_t v = value;
205 uintmax_t propagated_sign_bits =
206 ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
207 size_t i = size;
208
209 do
210 {
211 where[--i] = v & ((1 << LG_256) - 1);
212 v = propagated_sign_bits | (v >> LG_256);
213 }
214 while (i);
215 }
216
217
218 static bool
219 to_chars (int negative, uintmax_t value, size_t valsize,
220 uintmax_t (*substitute) (int *),
221 char *where, size_t size, const char *type);
222
223 static bool
224 to_chars_subst (int negative, int gnu_format, uintmax_t value, size_t valsize,
225 uintmax_t (*substitute) (int *),
226 char *where, size_t size, const char *type)
227 {
228 uintmax_t maxval = (gnu_format
229 ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
230 : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
231 char valbuf[UINTMAX_STRSIZE_BOUND + 1];
232 char maxbuf[UINTMAX_STRSIZE_BOUND];
233 char minbuf[UINTMAX_STRSIZE_BOUND + 1];
234 char const *minval_string;
235 char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
236 char const *value_string;
237
238 if (gnu_format)
239 {
240 uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
241 char *p = STRINGIFY_BIGINT (m, minbuf + 1);
242 *--p = '-';
243 minval_string = p;
244 }
245 else
246 minval_string = "0";
247
248 if (negative)
249 {
250 char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
251 *--p = '-';
252 value_string = p;
253 }
254 else
255 value_string = STRINGIFY_BIGINT (value, valbuf);
256
257 if (substitute)
258 {
259 int negsub;
260 uintmax_t sub = substitute (&negsub) & maxval;
261 /* NOTE: This is one of the few places where GNU_FORMAT differs from
262 OLDGNU_FORMAT. The actual differences are:
263
264 1. In OLDGNU_FORMAT all strings in a tar header end in \0
265 2. Incremental archives use oldgnu_header.
266
267 Apart from this they are completely identical. */
268 uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
269 char subbuf[UINTMAX_STRSIZE_BOUND + 1];
270 char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
271 if (negsub)
272 *--sub_string = '-';
273 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
274 value_string, type, minval_string, maxval_string,
275 sub_string));
276 return to_chars (negsub, s, valsize, 0, where, size, type);
277 }
278 else
279 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
280 value_string, type, minval_string, maxval_string));
281 return false;
282 }
283
284 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
285 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
286 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
287 negative before being cast to uintmax_t; its original bitpattern
288 can be deduced from VALSIZE, its original size before casting.
289 TYPE is the kind of value being output (useful for diagnostics).
290 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
291 digits), followed by '\0'. If this won't work, and if GNU or
292 OLDGNU format is allowed, use '\200' followed by base-256, or (if
293 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
294 If neither format works, use SUBSTITUTE (...) instead. Pass to
295 SUBSTITUTE the address of an 0-or-1 flag recording whether the
296 substitute value is negative. */
297
298 static bool
299 to_chars (int negative, uintmax_t value, size_t valsize,
300 uintmax_t (*substitute) (int *),
301 char *where, size_t size, const char *type)
302 {
303 int gnu_format = (archive_format == GNU_FORMAT
304 || archive_format == OLDGNU_FORMAT);
305
306 /* Generate the POSIX octal representation if the number fits. */
307 if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
308 {
309 where[size - 1] = '\0';
310 to_octal (value, where, size - 1);
311 return true;
312 }
313 else if (gnu_format)
314 {
315 /* Try to cope with the number by using traditional GNU format
316 methods */
317
318 /* Generate the base-256 representation if the number fits. */
319 if (((negative ? -1 - value : value)
320 <= MAX_VAL_WITH_DIGITS (size - 1, LG_256)))
321 {
322 where[0] = negative ? -1 : 1 << (LG_256 - 1);
323 to_base256 (negative, value, where + 1, size - 1);
324 return true;
325 }
326
327 /* Otherwise, if the number is negative, and if it would not cause
328 ambiguity on this host by confusing positive with negative
329 values, then generate the POSIX octal representation of the value
330 modulo 2**(field bits). The resulting tar file is
331 machine-dependent, since it depends on the host word size. Yuck!
332 But this is the traditional behavior. */
333 else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
334 {
335 static int warned_once;
336 if (! warned_once)
337 {
338 warned_once = 1;
339 WARN ((0, 0, _("Generating negative octal headers")));
340 }
341 where[size - 1] = '\0';
342 to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
343 where, size - 1);
344 return true;
345 }
346 /* Otherwise fall back to substitution, if possible: */
347 }
348 else
349 substitute = NULL; /* No substitution for formats, other than GNU */
350
351 return to_chars_subst (negative, gnu_format, value, valsize, substitute,
352 where, size, type);
353 }
354
355 static uintmax_t
356 gid_substitute (int *negative)
357 {
358 gid_t r;
359 #ifdef GID_NOBODY
360 r = GID_NOBODY;
361 #else
362 static gid_t gid_nobody;
363 if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
364 gid_nobody = -2;
365 r = gid_nobody;
366 #endif
367 *negative = r < 0;
368 return r;
369 }
370
371 bool
372 gid_to_chars (gid_t v, char *p, size_t s)
373 {
374 return to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
375 }
376
377 bool
378 major_to_chars (major_t v, char *p, size_t s)
379 {
380 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
381 }
382
383 bool
384 minor_to_chars (minor_t v, char *p, size_t s)
385 {
386 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
387 }
388
389 bool
390 mode_to_chars (mode_t v, char *p, size_t s)
391 {
392 /* In the common case where the internal and external mode bits are the same,
393 and we are not using POSIX or GNU format,
394 propagate all unknown bits to the external mode.
395 This matches historical practice.
396 Otherwise, just copy the bits we know about. */
397 int negative;
398 uintmax_t u;
399 if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
400 && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
401 && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
402 && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
403 && archive_format != POSIX_FORMAT
404 && archive_format != USTAR_FORMAT
405 && archive_format != GNU_FORMAT
406 && archive_format != OLDGNU_FORMAT)
407 {
408 negative = v < 0;
409 u = v;
410 }
411 else
412 {
413 negative = 0;
414 u = ((v & S_ISUID ? TSUID : 0)
415 | (v & S_ISGID ? TSGID : 0)
416 | (v & S_ISVTX ? TSVTX : 0)
417 | (v & S_IRUSR ? TUREAD : 0)
418 | (v & S_IWUSR ? TUWRITE : 0)
419 | (v & S_IXUSR ? TUEXEC : 0)
420 | (v & S_IRGRP ? TGREAD : 0)
421 | (v & S_IWGRP ? TGWRITE : 0)
422 | (v & S_IXGRP ? TGEXEC : 0)
423 | (v & S_IROTH ? TOREAD : 0)
424 | (v & S_IWOTH ? TOWRITE : 0)
425 | (v & S_IXOTH ? TOEXEC : 0));
426 }
427 return to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
428 }
429
430 bool
431 off_to_chars (off_t v, char *p, size_t s)
432 {
433 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
434 }
435
436 bool
437 size_to_chars (size_t v, char *p, size_t s)
438 {
439 return to_chars (0, (uintmax_t) v, sizeof v, 0, p, s, "size_t");
440 }
441
442 bool
443 time_to_chars (time_t v, char *p, size_t s)
444 {
445 return to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
446 }
447
448 static uintmax_t
449 uid_substitute (int *negative)
450 {
451 uid_t r;
452 #ifdef UID_NOBODY
453 r = UID_NOBODY;
454 #else
455 static uid_t uid_nobody;
456 if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
457 uid_nobody = -2;
458 r = uid_nobody;
459 #endif
460 *negative = r < 0;
461 return r;
462 }
463
464 bool
465 uid_to_chars (uid_t v, char *p, size_t s)
466 {
467 return to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
468 }
469
470 bool
471 uintmax_to_chars (uintmax_t v, char *p, size_t s)
472 {
473 return to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
474 }
475
476 void
477 string_to_chars (char const *str, char *p, size_t s)
478 {
479 tar_copy_str (p, str, s);
480 p[s - 1] = '\0';
481 }
482
483 \f
484 /* A file is considered dumpable if it is sparse and both --sparse and --totals
485 are specified.
486 Otherwise, it is dumpable unless any of the following conditions occur:
487
488 a) it is empty *and* world-readable, or
489 b) current archive is /dev/null */
490
491 bool
492 file_dumpable_p (struct tar_stat_info *st)
493 {
494 if (dev_null_output)
495 return totals_option && sparse_option && ST_IS_SPARSE (st->stat);
496 return !(st->archive_file_size == 0
497 && (st->stat.st_mode & MODE_R) == MODE_R);
498 }
499
500 \f
501 /* Writing routines. */
502
503 /* Write the EOT block(s). Zero at least two blocks, through the end
504 of the record. Old tar, as previous versions of GNU tar, writes
505 garbage after two zeroed blocks. */
506 void
507 write_eot (void)
508 {
509 union block *pointer = find_next_block ();
510 memset (pointer->buffer, 0, BLOCKSIZE);
511 set_next_block_after (pointer);
512 pointer = find_next_block ();
513 memset (pointer->buffer, 0, available_space_after (pointer));
514 set_next_block_after (pointer);
515 }
516
517 /* Write a "private" header */
518 union block *
519 start_private_header (const char *name, size_t size)
520 {
521 time_t t;
522 union block *header = find_next_block ();
523
524 memset (header->buffer, 0, sizeof (union block));
525
526 tar_name_copy_str (header->header.name, name, NAME_FIELD_SIZE);
527 OFF_TO_CHARS (size, header->header.size);
528
529 time (&t);
530 TIME_TO_CHARS (t, header->header.mtime);
531 MODE_TO_CHARS (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, header->header.mode);
532 UID_TO_CHARS (getuid (), header->header.uid);
533 GID_TO_CHARS (getgid (), header->header.gid);
534 MAJOR_TO_CHARS (0, header->header.devmajor);
535 MINOR_TO_CHARS (0, header->header.devminor);
536 strncpy (header->header.magic, TMAGIC, TMAGLEN);
537 strncpy (header->header.version, TVERSION, TVERSLEN);
538 return header;
539 }
540
541 /* Create a new header and store there at most NAME_FIELD_SIZE bytes of
542 the file name */
543
544 static union block *
545 write_short_name (struct tar_stat_info *st)
546 {
547 union block *header = find_next_block ();
548 memset (header->buffer, 0, sizeof (union block));
549 tar_name_copy_str (header->header.name, st->file_name, NAME_FIELD_SIZE);
550 return header;
551 }
552
553 #define FILL(field,byte) do { \
554 memset(field, byte, sizeof(field)-1); \
555 (field)[sizeof(field)-1] = 0; \
556 } while (0)
557
558 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
559 static void
560 write_gnu_long_link (struct tar_stat_info *st, const char *p, char type)
561 {
562 size_t size = strlen (p) + 1;
563 size_t bufsize;
564 union block *header;
565 char *tmpname;
566
567 header = start_private_header ("././@LongLink", size);
568 FILL(header->header.mtime, '0');
569 FILL(header->header.mode, '0');
570 FILL(header->header.uid, '0');
571 FILL(header->header.gid, '0');
572 FILL(header->header.devmajor, 0);
573 FILL(header->header.devminor, 0);
574 uid_to_uname (0, &tmpname);
575 UNAME_TO_CHARS (tmpname, header->header.uname);
576 free (tmpname);
577 gid_to_gname (0, &tmpname);
578 GNAME_TO_CHARS (tmpname, header->header.gname);
579 free (tmpname);
580
581 strcpy (header->header.magic, OLDGNU_MAGIC);
582 header->header.typeflag = type;
583 finish_header (st, header, -1);
584
585 header = find_next_block ();
586
587 bufsize = available_space_after (header);
588
589 while (bufsize < size)
590 {
591 memcpy (header->buffer, p, bufsize);
592 p += bufsize;
593 size -= bufsize;
594 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
595 header = find_next_block ();
596 bufsize = available_space_after (header);
597 }
598 memcpy (header->buffer, p, size);
599 memset (header->buffer + size, 0, bufsize - size);
600 set_next_block_after (header + (size - 1) / BLOCKSIZE);
601 }
602
603 static size_t
604 split_long_name (const char *name, size_t length)
605 {
606 size_t i;
607
608 if (length > PREFIX_FIELD_SIZE)
609 length = PREFIX_FIELD_SIZE + 1;
610 for (i = length - 1; i > 0; i--)
611 if (ISSLASH (name[i]))
612 break;
613 return i;
614 }
615
616 static union block *
617 write_ustar_long_name (const char *name)
618 {
619 size_t length = strlen (name);
620 size_t i;
621 union block *header;
622
623 if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
624 {
625 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
626 quotearg_colon (name),
627 PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
628 return NULL;
629 }
630
631 i = split_long_name (name, length);
632 if (i == 0 || length - i - 1 > NAME_FIELD_SIZE)
633 {
634 ERROR ((0, 0,
635 _("%s: file name is too long (cannot be split); not dumped"),
636 quotearg_colon (name)));
637 return NULL;
638 }
639
640 header = find_next_block ();
641 memset (header->buffer, 0, sizeof (header->buffer));
642 memcpy (header->header.prefix, name, i);
643 memcpy (header->header.name, name + i + 1, length - i - 1);
644
645 return header;
646 }
647
648 /* Write a long link name, depending on the current archive format */
649 static void
650 write_long_link (struct tar_stat_info *st)
651 {
652 switch (archive_format)
653 {
654 case POSIX_FORMAT:
655 xheader_store ("linkpath", st, NULL);
656 break;
657
658 case V7_FORMAT: /* old V7 tar format */
659 case USTAR_FORMAT:
660 case STAR_FORMAT:
661 ERROR ((0, 0,
662 _("%s: link name is too long; not dumped"),
663 quotearg_colon (st->link_name)));
664 break;
665
666 case OLDGNU_FORMAT:
667 case GNU_FORMAT:
668 write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK);
669 break;
670
671 default:
672 abort(); /*FIXME*/
673 }
674 }
675
676 static union block *
677 write_long_name (struct tar_stat_info *st)
678 {
679 switch (archive_format)
680 {
681 case POSIX_FORMAT:
682 xheader_store ("path", st, NULL);
683 break;
684
685 case V7_FORMAT:
686 if (strlen (st->file_name) > NAME_FIELD_SIZE-1)
687 {
688 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
689 quotearg_colon (st->file_name),
690 NAME_FIELD_SIZE - 1));
691 return NULL;
692 }
693 break;
694
695 case USTAR_FORMAT:
696 case STAR_FORMAT:
697 return write_ustar_long_name (st->file_name);
698
699 case OLDGNU_FORMAT:
700 case GNU_FORMAT:
701 write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
702 break;
703
704 default:
705 abort(); /*FIXME*/
706 }
707 return write_short_name (st);
708 }
709
710 union block *
711 write_extended (bool global, struct tar_stat_info *st, union block *old_header)
712 {
713 union block *header, hp;
714 char *p;
715 int type;
716
717 if (st->xhdr.buffer || st->xhdr.stk == NULL)
718 return old_header;
719
720 xheader_finish (&st->xhdr);
721 memcpy (hp.buffer, old_header, sizeof (hp));
722 if (global)
723 {
724 type = XGLTYPE;
725 p = xheader_ghdr_name ();
726 }
727 else
728 {
729 type = XHDTYPE;
730 p = xheader_xhdr_name (st);
731 }
732 xheader_write (type, p, &st->xhdr);
733 free (p);
734 header = find_next_block ();
735 memcpy (header, &hp.buffer, sizeof (hp.buffer));
736 return header;
737 }
738
739 static union block *
740 write_header_name (struct tar_stat_info *st)
741 {
742 if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
743 {
744 xheader_store ("path", st, NULL);
745 return write_short_name (st);
746 }
747 else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
748 < strlen (st->file_name))
749 return write_long_name (st);
750 else
751 return write_short_name (st);
752 }
753
754 \f
755 /* Header handling. */
756
757 /* Make a header block for the file whose stat info is st,
758 and return its address. */
759
760 union block *
761 start_header (struct tar_stat_info *st)
762 {
763 union block *header;
764
765 header = write_header_name (st);
766 if (!header)
767 return NULL;
768
769 /* Override some stat fields, if requested to do so. */
770
771 if (owner_option != (uid_t) -1)
772 st->stat.st_uid = owner_option;
773 if (group_option != (gid_t) -1)
774 st->stat.st_gid = group_option;
775 if (mode_option)
776 st->stat.st_mode =
777 ((st->stat.st_mode & ~MODE_ALL)
778 | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0,
779 initial_umask, mode_option, NULL));
780
781 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
782 for a few tars and came up with the following interoperability
783 matrix:
784
785 WRITER
786 1 2 3 4 5 6 7 8 9 READER
787 . . . . . . . . . 1 = SunOS 4.2 tar
788 # . . # # . . # # 2 = NEC SVR4.0.2 tar
789 . . . # # . . # . 3 = Solaris 2.1 tar
790 . . . . . . . . . 4 = GNU tar 1.11.1
791 . . . . . . . . . 5 = HP-UX 8.07 tar
792 . . . . . . . . . 6 = Ultrix 4.1
793 . . . . . . . . . 7 = AIX 3.2
794 . . . . . . . . . 8 = Hitachi HI-UX 1.03
795 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
796
797 . = works
798 # = ``impossible file type''
799
800 The following mask for old archive removes the `#'s in column 4
801 above, thus making GNU tar both a universal donor and a universal
802 acceptor for Paul's test. */
803
804 if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
805 MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
806 else
807 MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
808
809 {
810 uid_t uid = st->stat.st_uid;
811 if (archive_format == POSIX_FORMAT
812 && MAX_OCTAL_VAL (header->header.uid) < uid)
813 {
814 xheader_store ("uid", st, NULL);
815 uid = 0;
816 }
817 if (!UID_TO_CHARS (uid, header->header.uid))
818 return NULL;
819 }
820
821 {
822 gid_t gid = st->stat.st_gid;
823 if (archive_format == POSIX_FORMAT
824 && MAX_OCTAL_VAL (header->header.gid) < gid)
825 {
826 xheader_store ("gid", st, NULL);
827 gid = 0;
828 }
829 if (!GID_TO_CHARS (gid, header->header.gid))
830 return NULL;
831 }
832
833 {
834 off_t size = st->stat.st_size;
835 if (archive_format == POSIX_FORMAT
836 && MAX_OCTAL_VAL (header->header.size) < size)
837 {
838 xheader_store ("size", st, NULL);
839 size = 0;
840 }
841 if (!OFF_TO_CHARS (size, header->header.size))
842 return NULL;
843 }
844
845 {
846 struct timespec mtime = set_mtime_option ? mtime_option : st->mtime;
847 if (archive_format == POSIX_FORMAT)
848 {
849 if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
850 || mtime.tv_nsec != 0)
851 xheader_store ("mtime", st, &mtime);
852 if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
853 mtime.tv_sec = 0;
854 }
855 if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime))
856 return NULL;
857 }
858
859 /* FIXME */
860 if (S_ISCHR (st->stat.st_mode)
861 || S_ISBLK (st->stat.st_mode))
862 {
863 major_t devmajor = major (st->stat.st_rdev);
864 minor_t devminor = minor (st->stat.st_rdev);
865
866 if (archive_format == POSIX_FORMAT
867 && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
868 {
869 xheader_store ("devmajor", st, NULL);
870 devmajor = 0;
871 }
872 if (!MAJOR_TO_CHARS (devmajor, header->header.devmajor))
873 return NULL;
874
875 if (archive_format == POSIX_FORMAT
876 && MAX_OCTAL_VAL (header->header.devminor) < devminor)
877 {
878 xheader_store ("devminor", st, NULL);
879 devminor = 0;
880 }
881 if (!MINOR_TO_CHARS (devminor, header->header.devminor))
882 return NULL;
883 }
884 else if (archive_format != GNU_FORMAT && archive_format != OLDGNU_FORMAT)
885 {
886 if (!(MAJOR_TO_CHARS (0, header->header.devmajor)
887 && MINOR_TO_CHARS (0, header->header.devminor)))
888 return NULL;
889 }
890
891 if (archive_format == POSIX_FORMAT)
892 {
893 xheader_store ("atime", st, NULL);
894 xheader_store ("ctime", st, NULL);
895 }
896 else if (incremental_option)
897 if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
898 {
899 TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
900 TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
901 }
902
903 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
904
905 switch (archive_format)
906 {
907 case V7_FORMAT:
908 break;
909
910 case OLDGNU_FORMAT:
911 case GNU_FORMAT: /*FIXME?*/
912 /* Overwrite header->header.magic and header.version in one blow. */
913 strcpy (header->header.magic, OLDGNU_MAGIC);
914 break;
915
916 case POSIX_FORMAT:
917 case USTAR_FORMAT:
918 strncpy (header->header.magic, TMAGIC, TMAGLEN);
919 strncpy (header->header.version, TVERSION, TVERSLEN);
920 break;
921
922 default:
923 abort ();
924 }
925
926 if (archive_format == V7_FORMAT || numeric_owner_option)
927 {
928 /* header->header.[ug]name are left as the empty string. */
929 }
930 else
931 {
932 uid_to_uname (st->stat.st_uid, &st->uname);
933 gid_to_gname (st->stat.st_gid, &st->gname);
934
935 if (archive_format == POSIX_FORMAT
936 && (strlen (st->uname) > UNAME_FIELD_SIZE
937 || !string_ascii_p (st->uname)))
938 xheader_store ("uname", st, NULL);
939 UNAME_TO_CHARS (st->uname, header->header.uname);
940
941 if (archive_format == POSIX_FORMAT
942 && (strlen (st->gname) > GNAME_FIELD_SIZE
943 || !string_ascii_p (st->gname)))
944 xheader_store ("gname", st, NULL);
945 GNAME_TO_CHARS (st->gname, header->header.gname);
946 }
947
948 return header;
949 }
950
951 void
952 simple_finish_header (union block *header)
953 {
954 size_t i;
955 int sum;
956 char *p;
957
958 memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
959
960 sum = 0;
961 p = header->buffer;
962 for (i = sizeof *header; i-- != 0; )
963 /* We can't use unsigned char here because of old compilers, e.g. V7. */
964 sum += 0xFF & *p++;
965
966 /* Fill in the checksum field. It's formatted differently from the
967 other fields: it has [6] digits, a null, then a space -- rather than
968 digits, then a null. We use to_chars.
969 The final space is already there, from
970 checksumming, and to_chars doesn't modify it.
971
972 This is a fast way to do:
973
974 sprintf(header->header.chksum, "%6o", sum); */
975
976 uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
977
978 set_next_block_after (header);
979 }
980
981 /* Finish off a filled-in header block and write it out. We also
982 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
983 is not negative, is the block ordinal of the first record for this
984 file, which may be a preceding long name or long link record. */
985 void
986 finish_header (struct tar_stat_info *st,
987 union block *header, off_t block_ordinal)
988 {
989 /* Note: It is important to do this before the call to write_extended(),
990 so that the actual ustar header is printed */
991 if (verbose_option
992 && header->header.typeflag != GNUTYPE_LONGLINK
993 && header->header.typeflag != GNUTYPE_LONGNAME
994 && header->header.typeflag != XHDTYPE
995 && header->header.typeflag != XGLTYPE)
996 {
997 /* These globals are parameters to print_header, sigh. */
998
999 current_header = header;
1000 current_format = archive_format;
1001 print_header (st, block_ordinal);
1002 }
1003
1004 header = write_extended (false, st, header);
1005 simple_finish_header (header);
1006 }
1007 \f
1008
1009 void
1010 pad_archive (off_t size_left)
1011 {
1012 union block *blk;
1013 while (size_left > 0)
1014 {
1015 mv_size_left (size_left);
1016 blk = find_next_block ();
1017 memset (blk->buffer, 0, BLOCKSIZE);
1018 set_next_block_after (blk);
1019 size_left -= BLOCKSIZE;
1020 }
1021 }
1022
1023 static enum dump_status
1024 dump_regular_file (int fd, struct tar_stat_info *st)
1025 {
1026 off_t size_left = st->stat.st_size;
1027 off_t block_ordinal;
1028 union block *blk;
1029
1030 block_ordinal = current_block_ordinal ();
1031 blk = start_header (st);
1032 if (!blk)
1033 return dump_status_fail;
1034
1035 /* Mark contiguous files, if we support them. */
1036 if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
1037 blk->header.typeflag = CONTTYPE;
1038
1039 finish_header (st, blk, block_ordinal);
1040
1041 mv_begin (st);
1042 while (size_left > 0)
1043 {
1044 size_t bufsize, count;
1045
1046 mv_size_left (size_left);
1047
1048 blk = find_next_block ();
1049
1050 bufsize = available_space_after (blk);
1051
1052 if (size_left < bufsize)
1053 {
1054 /* Last read -- zero out area beyond. */
1055 bufsize = size_left;
1056 count = bufsize % BLOCKSIZE;
1057 if (count)
1058 memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1059 }
1060
1061 count = (fd < 0) ? bufsize : safe_read (fd, blk->buffer, bufsize);
1062 if (count == SAFE_READ_ERROR)
1063 {
1064 read_diag_details (st->orig_file_name,
1065 st->stat.st_size - size_left, bufsize);
1066 pad_archive (size_left);
1067 return dump_status_short;
1068 }
1069 size_left -= count;
1070 set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1071
1072 if (count != bufsize)
1073 {
1074 char buf[UINTMAX_STRSIZE_BOUND];
1075 memset (blk->buffer + count, 0, bufsize - count);
1076 WARNOPT (WARN_FILE_SHRANK,
1077 (0, 0,
1078 ngettext ("%s: File shrank by %s byte; padding with zeros",
1079 "%s: File shrank by %s bytes; padding with zeros",
1080 size_left),
1081 quotearg_colon (st->orig_file_name),
1082 STRINGIFY_BIGINT (size_left, buf)));
1083 if (! ignore_failed_read_option)
1084 exit_status = TAREXIT_DIFFERS;
1085 pad_archive (size_left - (bufsize - count));
1086 return dump_status_short;
1087 }
1088 }
1089 return dump_status_ok;
1090 }
1091
1092 \f
1093 static void
1094 dump_dir0 (char *directory,
1095 struct tar_stat_info *st, bool top_level, dev_t parent_device)
1096 {
1097 dev_t our_device = st->stat.st_dev;
1098 const char *tag_file_name;
1099
1100 if (!is_avoided_name (st->orig_file_name))
1101 {
1102 union block *blk = NULL;
1103 off_t block_ordinal = current_block_ordinal ();
1104 st->stat.st_size = 0; /* force 0 size on dir */
1105
1106 blk = start_header (st);
1107 if (!blk)
1108 return;
1109
1110 if (incremental_option && archive_format != POSIX_FORMAT)
1111 blk->header.typeflag = GNUTYPE_DUMPDIR;
1112 else /* if (standard_option) */
1113 blk->header.typeflag = DIRTYPE;
1114
1115 /* If we're gnudumping, we aren't done yet so don't close it. */
1116
1117 if (!incremental_option)
1118 finish_header (st, blk, block_ordinal);
1119 else if (gnu_list_name->directory)
1120 {
1121 if (archive_format == POSIX_FORMAT)
1122 {
1123 xheader_store ("GNU.dumpdir", st,
1124 safe_directory_contents (gnu_list_name->directory));
1125 finish_header (st, blk, block_ordinal);
1126 }
1127 else
1128 {
1129 off_t size_left;
1130 off_t totsize;
1131 size_t bufsize;
1132 ssize_t count;
1133 const char *buffer, *p_buffer;
1134
1135 block_ordinal = current_block_ordinal ();
1136 buffer = safe_directory_contents (gnu_list_name->directory);
1137 totsize = dumpdir_size (buffer);
1138 OFF_TO_CHARS (totsize, blk->header.size);
1139 finish_header (st, blk, block_ordinal);
1140 p_buffer = buffer;
1141 size_left = totsize;
1142
1143 mv_begin (st);
1144 mv_total_size (totsize);
1145 while (size_left > 0)
1146 {
1147 mv_size_left (size_left);
1148 blk = find_next_block ();
1149 bufsize = available_space_after (blk);
1150 if (size_left < bufsize)
1151 {
1152 bufsize = size_left;
1153 count = bufsize % BLOCKSIZE;
1154 if (count)
1155 memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1156 }
1157 memcpy (blk->buffer, p_buffer, bufsize);
1158 size_left -= bufsize;
1159 p_buffer += bufsize;
1160 set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1161 }
1162 mv_end ();
1163 }
1164 return;
1165 }
1166 }
1167
1168 if (!recursion_option)
1169 return;
1170
1171 if (one_file_system_option
1172 && !top_level
1173 && parent_device != st->stat.st_dev)
1174 {
1175 if (verbose_option)
1176 WARNOPT (WARN_XDEV,
1177 (0, 0,
1178 _("%s: file is on a different filesystem; not dumped"),
1179 quotearg_colon (st->orig_file_name)));
1180 }
1181 else
1182 {
1183 char *name_buf;
1184 size_t name_size;
1185
1186 switch (check_exclusion_tags (st->orig_file_name, &tag_file_name))
1187 {
1188 case exclusion_tag_all:
1189 /* Handled in dump_file0 */
1190 break;
1191
1192 case exclusion_tag_none:
1193 {
1194 char const *entry;
1195 size_t entry_len;
1196 size_t name_len;
1197
1198 name_buf = xstrdup (st->orig_file_name);
1199 name_size = name_len = strlen (name_buf);
1200
1201 /* Now output all the files in the directory. */
1202 /* FIXME: Should speed this up by cd-ing into the dir. */
1203 for (entry = directory; (entry_len = strlen (entry)) != 0;
1204 entry += entry_len + 1)
1205 {
1206 if (name_size < name_len + entry_len)
1207 {
1208 name_size = name_len + entry_len;
1209 name_buf = xrealloc (name_buf, name_size + 1);
1210 }
1211 strcpy (name_buf + name_len, entry);
1212 if (!excluded_name (name_buf))
1213 dump_file (name_buf, false, our_device);
1214 }
1215
1216 free (name_buf);
1217 }
1218 break;
1219
1220 case exclusion_tag_contents:
1221 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1222 _("contents not dumped"));
1223 name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1;
1224 name_buf = xmalloc (name_size);
1225 strcpy (name_buf, st->orig_file_name);
1226 strcat (name_buf, tag_file_name);
1227 dump_file (name_buf, false, our_device);
1228 free (name_buf);
1229 break;
1230
1231 case exclusion_tag_under:
1232 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1233 _("contents not dumped"));
1234 break;
1235 }
1236 }
1237 }
1238
1239 /* Ensure exactly one trailing slash. */
1240 static void
1241 ensure_slash (char **pstr)
1242 {
1243 size_t len = strlen (*pstr);
1244 while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
1245 len--;
1246 if (!ISSLASH ((*pstr)[len]))
1247 *pstr = xrealloc (*pstr, len + 2);
1248 (*pstr)[len++] = '/';
1249 (*pstr)[len] = '\0';
1250 }
1251
1252 static bool
1253 dump_dir (int fd, struct tar_stat_info *st, bool top_level,
1254 dev_t parent_device)
1255 {
1256 char *directory = fdsavedir (fd);
1257 if (!directory)
1258 {
1259 savedir_diag (st->orig_file_name);
1260 return false;
1261 }
1262
1263 dump_dir0 (directory, st, top_level, parent_device);
1264
1265 free (directory);
1266 return true;
1267 }
1268
1269 \f
1270 /* Main functions of this module. */
1271
1272 void
1273 create_archive (void)
1274 {
1275 struct name const *p;
1276
1277 open_archive (ACCESS_WRITE);
1278 buffer_write_global_xheader ();
1279
1280 if (incremental_option)
1281 {
1282 size_t buffer_size = 1000;
1283 char *buffer = xmalloc (buffer_size);
1284 const char *q;
1285
1286 collect_and_sort_names ();
1287
1288 while ((p = name_from_list ()) != NULL)
1289 if (!excluded_name (p->name))
1290 dump_file (p->name, p->cmdline, (dev_t) 0);
1291
1292 blank_name_list ();
1293 while ((p = name_from_list ()) != NULL)
1294 if (!excluded_name (p->name))
1295 {
1296 size_t plen = strlen (p->name);
1297 if (buffer_size <= plen)
1298 {
1299 while ((buffer_size *= 2) <= plen)
1300 continue;
1301 buffer = xrealloc (buffer, buffer_size);
1302 }
1303 memcpy (buffer, p->name, plen);
1304 if (! ISSLASH (buffer[plen - 1]))
1305 buffer[plen++] = DIRECTORY_SEPARATOR;
1306 q = directory_contents (gnu_list_name->directory);
1307 if (q)
1308 while (*q)
1309 {
1310 size_t qlen = strlen (q);
1311 if (*q == 'Y')
1312 {
1313 if (buffer_size < plen + qlen)
1314 {
1315 while ((buffer_size *=2 ) < plen + qlen)
1316 continue;
1317 buffer = xrealloc (buffer, buffer_size);
1318 }
1319 strcpy (buffer + plen, q + 1);
1320 dump_file (buffer, false, (dev_t) 0);
1321 }
1322 q += qlen + 1;
1323 }
1324 }
1325 free (buffer);
1326 }
1327 else
1328 {
1329 const char *name;
1330 while ((name = name_next (1)) != NULL)
1331 if (!excluded_name (name))
1332 dump_file (name, true, (dev_t) 0);
1333 }
1334
1335 write_eot ();
1336 close_archive ();
1337
1338 if (listed_incremental_option)
1339 write_directory_file ();
1340 }
1341
1342
1343 /* Calculate the hash of a link. */
1344 static size_t
1345 hash_link (void const *entry, size_t n_buckets)
1346 {
1347 struct link const *l = entry;
1348 uintmax_t num = l->dev ^ l->ino;
1349 return num % n_buckets;
1350 }
1351
1352 /* Compare two links for equality. */
1353 static bool
1354 compare_links (void const *entry1, void const *entry2)
1355 {
1356 struct link const *link1 = entry1;
1357 struct link const *link2 = entry2;
1358 return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
1359 }
1360
1361 static void
1362 unknown_file_error (char const *p)
1363 {
1364 WARNOPT (WARN_FILE_IGNORED,
1365 (0, 0, _("%s: Unknown file type; file ignored"),
1366 quotearg_colon (p)));
1367 if (!ignore_failed_read_option)
1368 exit_status = TAREXIT_FAILURE;
1369 }
1370
1371 \f
1372 /* Handling of hard links */
1373
1374 /* Table of all non-directories that we've written so far. Any time
1375 we see another, we check the table and avoid dumping the data
1376 again if we've done it once already. */
1377 static Hash_table *link_table;
1378
1379 /* Try to dump stat as a hard link to another file in the archive.
1380 Return true if successful. */
1381 static bool
1382 dump_hard_link (struct tar_stat_info *st)
1383 {
1384 if (link_table && (st->stat.st_nlink > 1 || remove_files_option))
1385 {
1386 struct link lp;
1387 struct link *duplicate;
1388 off_t block_ordinal;
1389 union block *blk;
1390
1391 lp.ino = st->stat.st_ino;
1392 lp.dev = st->stat.st_dev;
1393
1394 if ((duplicate = hash_lookup (link_table, &lp)))
1395 {
1396 /* We found a link. */
1397 char const *link_name = safer_name_suffix (duplicate->name, true,
1398 absolute_names_option);
1399
1400 duplicate->nlink--;
1401
1402 block_ordinal = current_block_ordinal ();
1403 assign_string (&st->link_name, link_name);
1404 if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
1405 < strlen (link_name))
1406 write_long_link (st);
1407
1408 st->stat.st_size = 0;
1409 blk = start_header (st);
1410 if (!blk)
1411 return false;
1412 tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
1413
1414 blk->header.typeflag = LNKTYPE;
1415 finish_header (st, blk, block_ordinal);
1416
1417 if (remove_files_option && unlink (st->orig_file_name) != 0)
1418 unlink_error (st->orig_file_name);
1419
1420 return true;
1421 }
1422 }
1423 return false;
1424 }
1425
1426 static void
1427 file_count_links (struct tar_stat_info *st)
1428 {
1429 if (hard_dereference_option)
1430 return;
1431 if (st->stat.st_nlink > 1)
1432 {
1433 struct link *duplicate;
1434 char *linkname = NULL;
1435 struct link *lp;
1436
1437 assign_string (&linkname, st->orig_file_name);
1438 transform_name (&linkname, XFORM_LINK);
1439
1440 lp = xmalloc (offsetof (struct link, name)
1441 + strlen (linkname) + 1);
1442 lp->ino = st->stat.st_ino;
1443 lp->dev = st->stat.st_dev;
1444 lp->nlink = st->stat.st_nlink;
1445 strcpy (lp->name, linkname);
1446 free (linkname);
1447
1448 if (! ((link_table
1449 || (link_table = hash_initialize (0, 0, hash_link,
1450 compare_links, 0)))
1451 && (duplicate = hash_insert (link_table, lp))))
1452 xalloc_die ();
1453
1454 if (duplicate != lp)
1455 abort ();
1456 lp->nlink--;
1457 }
1458 }
1459
1460 /* For each dumped file, check if all its links were dumped. Emit
1461 warnings if it is not so. */
1462 void
1463 check_links (void)
1464 {
1465 struct link *lp;
1466
1467 if (!link_table)
1468 return;
1469
1470 for (lp = hash_get_first (link_table); lp;
1471 lp = hash_get_next (link_table, lp))
1472 {
1473 if (lp->nlink)
1474 {
1475 WARN ((0, 0, _("Missing links to %s."), quote (lp->name)));
1476 }
1477 }
1478 }
1479
1480 /* Dump a single file, recursing on directories. P is the file name
1481 to dump. TOP_LEVEL tells whether this is a top-level call; zero
1482 means no, positive means yes, and negative means the top level
1483 of an incremental dump. PARENT_DEVICE is the device of P's
1484 parent directory; it is examined only if TOP_LEVEL is zero. */
1485
1486 /* FIXME: One should make sure that for *every* path leading to setting
1487 exit_status to failure, a clear diagnostic has been issued. */
1488
1489 static void
1490 dump_file0 (struct tar_stat_info *st, const char *p,
1491 bool top_level, dev_t parent_device)
1492 {
1493 union block *header;
1494 char type;
1495 off_t original_size;
1496 struct timespec original_ctime;
1497 struct timespec restore_times[2];
1498 off_t block_ordinal = -1;
1499 bool is_dir;
1500
1501 if (interactive_option && !confirm ("add", p))
1502 return;
1503
1504 assign_string (&st->orig_file_name, p);
1505 assign_string (&st->file_name,
1506 safer_name_suffix (p, false, absolute_names_option));
1507
1508 transform_name (&st->file_name, XFORM_REGFILE);
1509
1510 if (deref_stat (dereference_option, p, &st->stat) != 0)
1511 {
1512 file_removed_diag (p, top_level, stat_diag);
1513 return;
1514 }
1515 st->archive_file_size = original_size = st->stat.st_size;
1516 st->atime = restore_times[0] = get_stat_atime (&st->stat);
1517 st->mtime = restore_times[1] = get_stat_mtime (&st->stat);
1518 st->ctime = original_ctime = get_stat_ctime (&st->stat);
1519
1520 #ifdef S_ISHIDDEN
1521 if (S_ISHIDDEN (st->stat.st_mode))
1522 {
1523 char *new = (char *) alloca (strlen (p) + 2);
1524 if (new)
1525 {
1526 strcpy (new, p);
1527 strcat (new, "@");
1528 p = new;
1529 }
1530 }
1531 #endif
1532
1533 /* See if we want only new files, and check if this one is too old to
1534 put in the archive.
1535
1536 This check is omitted if incremental_option is set *and* the
1537 requested file is not explicitely listed in the command line. */
1538
1539 if (!(incremental_option && !is_individual_file (p))
1540 && !S_ISDIR (st->stat.st_mode)
1541 && OLDER_TAR_STAT_TIME (*st, m)
1542 && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
1543 {
1544 if (!incremental_option && verbose_option)
1545 WARNOPT (WARN_FILE_UNCHANGED,
1546 (0, 0, _("%s: file is unchanged; not dumped"),
1547 quotearg_colon (p)));
1548 return;
1549 }
1550
1551 /* See if we are trying to dump the archive. */
1552 if (sys_file_is_archive (st))
1553 {
1554 WARNOPT (WARN_IGNORE_ARCHIVE,
1555 (0, 0, _("%s: file is the archive; not dumped"),
1556 quotearg_colon (p)));
1557 return;
1558 }
1559
1560 if (is_avoided_name (p))
1561 return;
1562
1563 is_dir = S_ISDIR (st->stat.st_mode) != 0;
1564
1565 if (!is_dir && dump_hard_link (st))
1566 return;
1567
1568 if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode))
1569 {
1570 bool ok;
1571 int fd = -1;
1572 struct stat final_stat;
1573
1574 if (is_dir || file_dumpable_p (st))
1575 {
1576 fd = open (p,
1577 (O_RDONLY | O_BINARY
1578 | (is_dir ? O_DIRECTORY | O_NONBLOCK : 0)
1579 | (atime_preserve_option == system_atime_preserve
1580 ? O_NOATIME
1581 : 0)));
1582 if (fd < 0)
1583 {
1584 file_removed_diag (p, top_level, open_diag);
1585 return;
1586 }
1587 }
1588
1589 if (is_dir)
1590 {
1591 const char *tag_file_name;
1592 ensure_slash (&st->orig_file_name);
1593 ensure_slash (&st->file_name);
1594
1595 if (check_exclusion_tags (st->orig_file_name, &tag_file_name)
1596 == exclusion_tag_all)
1597 {
1598 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1599 _("directory not dumped"));
1600 return;
1601 }
1602
1603 ok = dump_dir (fd, st, top_level, parent_device);
1604
1605 /* dump_dir consumes FD if successful. */
1606 if (ok)
1607 fd = -1;
1608 }
1609 else
1610 {
1611 enum dump_status status;
1612
1613 if (fd != -1 && sparse_option && ST_IS_SPARSE (st->stat))
1614 {
1615 status = sparse_dump_file (fd, st);
1616 if (status == dump_status_not_implemented)
1617 status = dump_regular_file (fd, st);
1618 }
1619 else
1620 status = dump_regular_file (fd, st);
1621
1622 switch (status)
1623 {
1624 case dump_status_ok:
1625 case dump_status_short:
1626 mv_end ();
1627 file_count_links (st);
1628 break;
1629
1630 case dump_status_fail:
1631 break;
1632
1633 case dump_status_not_implemented:
1634 abort ();
1635 }
1636
1637 ok = status == dump_status_ok;
1638 }
1639
1640 if (ok)
1641 {
1642 /* If possible, reopen a directory if we are preserving
1643 atimes, so that we can set just the atime on systems with
1644 _FIOSATIME. */
1645 if (fd < 0 && is_dir
1646 && atime_preserve_option == replace_atime_preserve)
1647 fd = open (p, O_RDONLY | O_BINARY | O_DIRECTORY | O_NONBLOCK);
1648
1649 if ((fd < 0
1650 ? deref_stat (dereference_option, p, &final_stat)
1651 : fstat (fd, &final_stat))
1652 != 0)
1653 {
1654 file_removed_diag (p, top_level, stat_diag);
1655 ok = false;
1656 }
1657 }
1658
1659 if (ok)
1660 {
1661 if ((timespec_cmp (get_stat_ctime (&final_stat), original_ctime) != 0
1662 /* Original ctime will change if the file is a directory and
1663 --remove-files is given */
1664 && !(remove_files_option && is_dir))
1665 || original_size < final_stat.st_size)
1666 {
1667 WARNOPT (WARN_FILE_CHANGED,
1668 (0, 0, _("%s: file changed as we read it"),
1669 quotearg_colon (p)));
1670 if (exit_status == TAREXIT_SUCCESS)
1671 exit_status = TAREXIT_DIFFERS;
1672 }
1673 else if (atime_preserve_option == replace_atime_preserve
1674 && set_file_atime (fd, p, restore_times) != 0)
1675 utime_error (p);
1676 }
1677
1678 if (0 <= fd && close (fd) != 0)
1679 {
1680 close_diag (p);
1681 ok = false;
1682 }
1683
1684 if (ok && remove_files_option)
1685 {
1686 if (is_dir)
1687 {
1688 if (rmdir (p) != 0 && errno != ENOTEMPTY)
1689 rmdir_error (p);
1690 }
1691 else
1692 {
1693 if (unlink (p) != 0)
1694 unlink_error (p);
1695 }
1696 }
1697
1698 return;
1699 }
1700 #ifdef HAVE_READLINK
1701 else if (S_ISLNK (st->stat.st_mode))
1702 {
1703 char *buffer;
1704 int size;
1705 size_t linklen = st->stat.st_size;
1706 if (linklen != st->stat.st_size || linklen + 1 == 0)
1707 xalloc_die ();
1708 buffer = (char *) alloca (linklen + 1);
1709 size = readlink (p, buffer, linklen + 1);
1710 if (size < 0)
1711 {
1712 file_removed_diag (p, top_level, readlink_diag);
1713 return;
1714 }
1715 buffer[size] = '\0';
1716 assign_string (&st->link_name, buffer);
1717 transform_name (&st->link_name, XFORM_SYMLINK);
1718 if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) < size)
1719 write_long_link (st);
1720
1721 block_ordinal = current_block_ordinal ();
1722 st->stat.st_size = 0; /* force 0 size on symlink */
1723 header = start_header (st);
1724 if (!header)
1725 return;
1726 tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE);
1727 header->header.typeflag = SYMTYPE;
1728 finish_header (st, header, block_ordinal);
1729 /* nothing more to do to it */
1730
1731 if (remove_files_option)
1732 {
1733 if (unlink (p) == -1)
1734 unlink_error (p);
1735 }
1736 file_count_links (st);
1737 return;
1738 }
1739 #endif
1740 else if (S_ISCHR (st->stat.st_mode))
1741 type = CHRTYPE;
1742 else if (S_ISBLK (st->stat.st_mode))
1743 type = BLKTYPE;
1744 else if (S_ISFIFO (st->stat.st_mode))
1745 type = FIFOTYPE;
1746 else if (S_ISSOCK (st->stat.st_mode))
1747 {
1748 WARNOPT (WARN_FILE_IGNORED,
1749 (0, 0, _("%s: socket ignored"), quotearg_colon (p)));
1750 return;
1751 }
1752 else if (S_ISDOOR (st->stat.st_mode))
1753 {
1754 WARNOPT (WARN_FILE_IGNORED,
1755 (0, 0, _("%s: door ignored"), quotearg_colon (p)));
1756 return;
1757 }
1758 else
1759 {
1760 unknown_file_error (p);
1761 return;
1762 }
1763
1764 if (archive_format == V7_FORMAT)
1765 {
1766 unknown_file_error (p);
1767 return;
1768 }
1769
1770 block_ordinal = current_block_ordinal ();
1771 st->stat.st_size = 0; /* force 0 size */
1772 header = start_header (st);
1773 if (!header)
1774 return;
1775 header->header.typeflag = type;
1776
1777 if (type != FIFOTYPE)
1778 {
1779 MAJOR_TO_CHARS (major (st->stat.st_rdev),
1780 header->header.devmajor);
1781 MINOR_TO_CHARS (minor (st->stat.st_rdev),
1782 header->header.devminor);
1783 }
1784
1785 finish_header (st, header, block_ordinal);
1786 if (remove_files_option)
1787 {
1788 if (unlink (p) == -1)
1789 unlink_error (p);
1790 }
1791 }
1792
1793 void
1794 dump_file (const char *p, bool top_level, dev_t parent_device)
1795 {
1796 struct tar_stat_info st;
1797 tar_stat_init (&st);
1798 dump_file0 (&st, p, top_level, parent_device);
1799 if (listed_incremental_option)
1800 update_parent_directory (p);
1801 tar_stat_destroy (&st);
1802 }
This page took 0.109815 seconds and 5 git commands to generate.