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