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