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