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