]> Dogcows Code - chaz/tar/blob - src/create.c
tar: live within system-supplied limits on file descriptors
[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->header.magic, OLDGNU_MAGIC);
566 header->header.typeflag = type;
567 finish_header (st, header, -1);
568
569 header = find_next_block ();
570
571 bufsize = available_space_after (header);
572
573 while (bufsize < size)
574 {
575 memcpy (header->buffer, p, bufsize);
576 p += bufsize;
577 size -= bufsize;
578 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
579 header = find_next_block ();
580 bufsize = available_space_after (header);
581 }
582 memcpy (header->buffer, p, size);
583 memset (header->buffer + size, 0, bufsize - size);
584 set_next_block_after (header + (size - 1) / BLOCKSIZE);
585 }
586
587 static size_t
588 split_long_name (const char *name, size_t length)
589 {
590 size_t i;
591
592 if (length > PREFIX_FIELD_SIZE + 1)
593 length = PREFIX_FIELD_SIZE + 1;
594 else if (ISSLASH (name[length - 1]))
595 length--;
596 for (i = length - 1; i > 0; i--)
597 if (ISSLASH (name[i]))
598 break;
599 return i;
600 }
601
602 static union block *
603 write_ustar_long_name (const char *name)
604 {
605 size_t length = strlen (name);
606 size_t i, nlen;
607 union block *header;
608
609 if (length > PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1)
610 {
611 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
612 quotearg_colon (name),
613 PREFIX_FIELD_SIZE + NAME_FIELD_SIZE + 1));
614 return NULL;
615 }
616
617 i = split_long_name (name, length);
618 if (i == 0 || (nlen = length - i - 1) > NAME_FIELD_SIZE || nlen == 0)
619 {
620 ERROR ((0, 0,
621 _("%s: file name is too long (cannot be split); not dumped"),
622 quotearg_colon (name)));
623 return NULL;
624 }
625
626 header = find_next_block ();
627 memset (header->buffer, 0, sizeof (header->buffer));
628 memcpy (header->header.prefix, name, i);
629 memcpy (header->header.name, name + i + 1, length - i - 1);
630
631 return header;
632 }
633
634 /* Write a long link name, depending on the current archive format */
635 static void
636 write_long_link (struct tar_stat_info *st)
637 {
638 switch (archive_format)
639 {
640 case POSIX_FORMAT:
641 xheader_store ("linkpath", st, NULL);
642 break;
643
644 case V7_FORMAT: /* old V7 tar format */
645 case USTAR_FORMAT:
646 case STAR_FORMAT:
647 ERROR ((0, 0,
648 _("%s: link name is too long; not dumped"),
649 quotearg_colon (st->link_name)));
650 break;
651
652 case OLDGNU_FORMAT:
653 case GNU_FORMAT:
654 write_gnu_long_link (st, st->link_name, GNUTYPE_LONGLINK);
655 break;
656
657 default:
658 abort(); /*FIXME*/
659 }
660 }
661
662 static union block *
663 write_long_name (struct tar_stat_info *st)
664 {
665 switch (archive_format)
666 {
667 case POSIX_FORMAT:
668 xheader_store ("path", st, NULL);
669 break;
670
671 case V7_FORMAT:
672 if (strlen (st->file_name) > NAME_FIELD_SIZE-1)
673 {
674 ERROR ((0, 0, _("%s: file name is too long (max %d); not dumped"),
675 quotearg_colon (st->file_name),
676 NAME_FIELD_SIZE - 1));
677 return NULL;
678 }
679 break;
680
681 case USTAR_FORMAT:
682 case STAR_FORMAT:
683 return write_ustar_long_name (st->file_name);
684
685 case OLDGNU_FORMAT:
686 case GNU_FORMAT:
687 write_gnu_long_link (st, st->file_name, GNUTYPE_LONGNAME);
688 break;
689
690 default:
691 abort(); /*FIXME*/
692 }
693 return write_short_name (st);
694 }
695
696 union block *
697 write_extended (bool global, struct tar_stat_info *st, union block *old_header)
698 {
699 union block *header, hp;
700 char *p;
701 int type;
702 time_t t;
703
704 if (st->xhdr.buffer || st->xhdr.stk == NULL)
705 return old_header;
706
707 xheader_finish (&st->xhdr);
708 memcpy (hp.buffer, old_header, sizeof (hp));
709 if (global)
710 {
711 type = XGLTYPE;
712 p = xheader_ghdr_name ();
713 time (&t);
714 }
715 else
716 {
717 type = XHDTYPE;
718 p = xheader_xhdr_name (st);
719 t = st->stat.st_mtime;
720 }
721 xheader_write (type, p, t, &st->xhdr);
722 free (p);
723 header = find_next_block ();
724 memcpy (header, &hp.buffer, sizeof (hp.buffer));
725 return header;
726 }
727
728 static union block *
729 write_header_name (struct tar_stat_info *st)
730 {
731 if (archive_format == POSIX_FORMAT && !string_ascii_p (st->file_name))
732 {
733 xheader_store ("path", st, NULL);
734 return write_short_name (st);
735 }
736 else if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
737 < strlen (st->file_name))
738 return write_long_name (st);
739 else
740 return write_short_name (st);
741 }
742
743 \f
744 /* Header handling. */
745
746 /* Make a header block for the file whose stat info is st,
747 and return its address. */
748
749 union block *
750 start_header (struct tar_stat_info *st)
751 {
752 union block *header;
753
754 header = write_header_name (st);
755 if (!header)
756 return NULL;
757
758 /* Override some stat fields, if requested to do so. */
759
760 if (owner_option != (uid_t) -1)
761 st->stat.st_uid = owner_option;
762 if (group_option != (gid_t) -1)
763 st->stat.st_gid = group_option;
764 if (mode_option)
765 st->stat.st_mode =
766 ((st->stat.st_mode & ~MODE_ALL)
767 | mode_adjust (st->stat.st_mode, S_ISDIR (st->stat.st_mode) != 0,
768 initial_umask, mode_option, NULL));
769
770 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
771 for a few tars and came up with the following interoperability
772 matrix:
773
774 WRITER
775 1 2 3 4 5 6 7 8 9 READER
776 . . . . . . . . . 1 = SunOS 4.2 tar
777 # . . # # . . # # 2 = NEC SVR4.0.2 tar
778 . . . # # . . # . 3 = Solaris 2.1 tar
779 . . . . . . . . . 4 = GNU tar 1.11.1
780 . . . . . . . . . 5 = HP-UX 8.07 tar
781 . . . . . . . . . 6 = Ultrix 4.1
782 . . . . . . . . . 7 = AIX 3.2
783 . . . . . . . . . 8 = Hitachi HI-UX 1.03
784 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
785
786 . = works
787 # = ``impossible file type''
788
789 The following mask for old archive removes the `#'s in column 4
790 above, thus making GNU tar both a universal donor and a universal
791 acceptor for Paul's test. */
792
793 if (archive_format == V7_FORMAT || archive_format == USTAR_FORMAT)
794 MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
795 else
796 MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
797
798 {
799 uid_t uid = st->stat.st_uid;
800 if (archive_format == POSIX_FORMAT
801 && MAX_OCTAL_VAL (header->header.uid) < uid)
802 {
803 xheader_store ("uid", st, NULL);
804 uid = 0;
805 }
806 if (!UID_TO_CHARS (uid, header->header.uid))
807 return NULL;
808 }
809
810 {
811 gid_t gid = st->stat.st_gid;
812 if (archive_format == POSIX_FORMAT
813 && MAX_OCTAL_VAL (header->header.gid) < gid)
814 {
815 xheader_store ("gid", st, NULL);
816 gid = 0;
817 }
818 if (!GID_TO_CHARS (gid, header->header.gid))
819 return NULL;
820 }
821
822 {
823 off_t size = st->stat.st_size;
824 if (archive_format == POSIX_FORMAT
825 && MAX_OCTAL_VAL (header->header.size) < size)
826 {
827 xheader_store ("size", st, NULL);
828 size = 0;
829 }
830 if (!OFF_TO_CHARS (size, header->header.size))
831 return NULL;
832 }
833
834 {
835 struct timespec mtime = set_mtime_option ? mtime_option : st->mtime;
836 if (archive_format == POSIX_FORMAT)
837 {
838 if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec
839 || mtime.tv_nsec != 0)
840 xheader_store ("mtime", st, &mtime);
841 if (MAX_OCTAL_VAL (header->header.mtime) < mtime.tv_sec)
842 mtime.tv_sec = 0;
843 }
844 if (!TIME_TO_CHARS (mtime.tv_sec, header->header.mtime))
845 return NULL;
846 }
847
848 /* FIXME */
849 if (S_ISCHR (st->stat.st_mode)
850 || S_ISBLK (st->stat.st_mode))
851 {
852 major_t devmajor = major (st->stat.st_rdev);
853 minor_t devminor = minor (st->stat.st_rdev);
854
855 if (archive_format == POSIX_FORMAT
856 && MAX_OCTAL_VAL (header->header.devmajor) < devmajor)
857 {
858 xheader_store ("devmajor", st, NULL);
859 devmajor = 0;
860 }
861 if (!MAJOR_TO_CHARS (devmajor, header->header.devmajor))
862 return NULL;
863
864 if (archive_format == POSIX_FORMAT
865 && MAX_OCTAL_VAL (header->header.devminor) < devminor)
866 {
867 xheader_store ("devminor", st, NULL);
868 devminor = 0;
869 }
870 if (!MINOR_TO_CHARS (devminor, header->header.devminor))
871 return NULL;
872 }
873 else if (archive_format != GNU_FORMAT && archive_format != OLDGNU_FORMAT)
874 {
875 if (!(MAJOR_TO_CHARS (0, header->header.devmajor)
876 && MINOR_TO_CHARS (0, header->header.devminor)))
877 return NULL;
878 }
879
880 if (archive_format == POSIX_FORMAT)
881 {
882 xheader_store ("atime", st, NULL);
883 xheader_store ("ctime", st, NULL);
884 }
885 else if (incremental_option)
886 if (archive_format == OLDGNU_FORMAT || archive_format == GNU_FORMAT)
887 {
888 TIME_TO_CHARS (st->atime.tv_sec, header->oldgnu_header.atime);
889 TIME_TO_CHARS (st->ctime.tv_sec, header->oldgnu_header.ctime);
890 }
891
892 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
893
894 switch (archive_format)
895 {
896 case V7_FORMAT:
897 break;
898
899 case OLDGNU_FORMAT:
900 case GNU_FORMAT: /*FIXME?*/
901 /* Overwrite header->header.magic and header.version in one blow. */
902 strcpy (header->header.magic, OLDGNU_MAGIC);
903 break;
904
905 case POSIX_FORMAT:
906 case USTAR_FORMAT:
907 strncpy (header->header.magic, TMAGIC, TMAGLEN);
908 strncpy (header->header.version, TVERSION, TVERSLEN);
909 break;
910
911 default:
912 abort ();
913 }
914
915 if (archive_format == V7_FORMAT || numeric_owner_option)
916 {
917 /* header->header.[ug]name are left as the empty string. */
918 }
919 else
920 {
921 uid_to_uname (st->stat.st_uid, &st->uname);
922 gid_to_gname (st->stat.st_gid, &st->gname);
923
924 if (archive_format == POSIX_FORMAT
925 && (strlen (st->uname) > UNAME_FIELD_SIZE
926 || !string_ascii_p (st->uname)))
927 xheader_store ("uname", st, NULL);
928 UNAME_TO_CHARS (st->uname, header->header.uname);
929
930 if (archive_format == POSIX_FORMAT
931 && (strlen (st->gname) > GNAME_FIELD_SIZE
932 || !string_ascii_p (st->gname)))
933 xheader_store ("gname", st, NULL);
934 GNAME_TO_CHARS (st->gname, header->header.gname);
935 }
936
937 return header;
938 }
939
940 void
941 simple_finish_header (union block *header)
942 {
943 size_t i;
944 int sum;
945 char *p;
946
947 memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
948
949 sum = 0;
950 p = header->buffer;
951 for (i = sizeof *header; i-- != 0; )
952 /* We can't use unsigned char here because of old compilers, e.g. V7. */
953 sum += 0xFF & *p++;
954
955 /* Fill in the checksum field. It's formatted differently from the
956 other fields: it has [6] digits, a null, then a space -- rather than
957 digits, then a null. We use to_chars.
958 The final space is already there, from
959 checksumming, and to_chars doesn't modify it.
960
961 This is a fast way to do:
962
963 sprintf(header->header.chksum, "%6o", sum); */
964
965 uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
966
967 set_next_block_after (header);
968 }
969
970 /* Finish off a filled-in header block and write it out. We also
971 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
972 is not negative, is the block ordinal of the first record for this
973 file, which may be a preceding long name or long link record. */
974 void
975 finish_header (struct tar_stat_info *st,
976 union block *header, off_t block_ordinal)
977 {
978 /* Note: It is important to do this before the call to write_extended(),
979 so that the actual ustar header is printed */
980 if (verbose_option
981 && header->header.typeflag != GNUTYPE_LONGLINK
982 && header->header.typeflag != GNUTYPE_LONGNAME
983 && header->header.typeflag != XHDTYPE
984 && header->header.typeflag != XGLTYPE)
985 {
986 /* FIXME: This global is used in print_header, sigh. */
987 current_format = archive_format;
988 print_header (st, header, block_ordinal);
989 }
990
991 header = write_extended (false, st, header);
992 simple_finish_header (header);
993 }
994 \f
995
996 void
997 pad_archive (off_t size_left)
998 {
999 union block *blk;
1000 while (size_left > 0)
1001 {
1002 blk = find_next_block ();
1003 memset (blk->buffer, 0, BLOCKSIZE);
1004 set_next_block_after (blk);
1005 size_left -= BLOCKSIZE;
1006 }
1007 }
1008
1009 static enum dump_status
1010 dump_regular_file (int fd, struct tar_stat_info *st)
1011 {
1012 off_t size_left = st->stat.st_size;
1013 off_t block_ordinal;
1014 union block *blk;
1015
1016 block_ordinal = current_block_ordinal ();
1017 blk = start_header (st);
1018 if (!blk)
1019 return dump_status_fail;
1020
1021 /* Mark contiguous files, if we support them. */
1022 if (archive_format != V7_FORMAT && S_ISCTG (st->stat.st_mode))
1023 blk->header.typeflag = CONTTYPE;
1024
1025 finish_header (st, blk, block_ordinal);
1026
1027 mv_begin_write (st->file_name, st->stat.st_size, st->stat.st_size);
1028 while (size_left > 0)
1029 {
1030 size_t bufsize, count;
1031
1032 blk = find_next_block ();
1033
1034 bufsize = available_space_after (blk);
1035
1036 if (size_left < bufsize)
1037 {
1038 /* Last read -- zero out area beyond. */
1039 bufsize = size_left;
1040 count = bufsize % BLOCKSIZE;
1041 if (count)
1042 memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1043 }
1044
1045 count = (fd <= 0) ? bufsize : safe_read (fd, blk->buffer, bufsize);
1046 if (count == SAFE_READ_ERROR)
1047 {
1048 read_diag_details (st->orig_file_name,
1049 st->stat.st_size - size_left, bufsize);
1050 pad_archive (size_left);
1051 return dump_status_short;
1052 }
1053 size_left -= count;
1054 set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1055
1056 if (count != bufsize)
1057 {
1058 char buf[UINTMAX_STRSIZE_BOUND];
1059 memset (blk->buffer + count, 0, bufsize - count);
1060 WARNOPT (WARN_FILE_SHRANK,
1061 (0, 0,
1062 ngettext ("%s: File shrank by %s byte; padding with zeros",
1063 "%s: File shrank by %s bytes; padding with zeros",
1064 size_left),
1065 quotearg_colon (st->orig_file_name),
1066 STRINGIFY_BIGINT (size_left, buf)));
1067 if (! ignore_failed_read_option)
1068 set_exit_status (TAREXIT_DIFFERS);
1069 pad_archive (size_left - (bufsize - count));
1070 return dump_status_short;
1071 }
1072 }
1073 return dump_status_ok;
1074 }
1075
1076 \f
1077 /* Copy info from the directory identified by ST into the archive.
1078 DIRECTORY contains the directory's entries. */
1079
1080 static void
1081 dump_dir0 (struct tar_stat_info *st, char const *directory)
1082 {
1083 bool top_level = ! st->parent;
1084 const char *tag_file_name;
1085 union block *blk = NULL;
1086 off_t block_ordinal = current_block_ordinal ();
1087
1088 st->stat.st_size = 0; /* force 0 size on dir */
1089
1090 blk = start_header (st);
1091 if (!blk)
1092 return;
1093
1094 if (incremental_option && archive_format != POSIX_FORMAT)
1095 blk->header.typeflag = GNUTYPE_DUMPDIR;
1096 else /* if (standard_option) */
1097 blk->header.typeflag = DIRTYPE;
1098
1099 /* If we're gnudumping, we aren't done yet so don't close it. */
1100
1101 if (!incremental_option)
1102 finish_header (st, blk, block_ordinal);
1103 else if (gnu_list_name->directory)
1104 {
1105 if (archive_format == POSIX_FORMAT)
1106 {
1107 xheader_store ("GNU.dumpdir", st,
1108 safe_directory_contents (gnu_list_name->directory));
1109 finish_header (st, blk, block_ordinal);
1110 }
1111 else
1112 {
1113 off_t size_left;
1114 off_t totsize;
1115 size_t bufsize;
1116 ssize_t count;
1117 const char *buffer, *p_buffer;
1118
1119 block_ordinal = current_block_ordinal ();
1120 buffer = safe_directory_contents (gnu_list_name->directory);
1121 totsize = dumpdir_size (buffer);
1122 OFF_TO_CHARS (totsize, blk->header.size);
1123 finish_header (st, blk, block_ordinal);
1124 p_buffer = buffer;
1125 size_left = totsize;
1126
1127 mv_begin_write (st->file_name, totsize, totsize);
1128 while (size_left > 0)
1129 {
1130 blk = find_next_block ();
1131 bufsize = available_space_after (blk);
1132 if (size_left < bufsize)
1133 {
1134 bufsize = size_left;
1135 count = bufsize % BLOCKSIZE;
1136 if (count)
1137 memset (blk->buffer + size_left, 0, BLOCKSIZE - count);
1138 }
1139 memcpy (blk->buffer, p_buffer, bufsize);
1140 size_left -= bufsize;
1141 p_buffer += bufsize;
1142 set_next_block_after (blk + (bufsize - 1) / BLOCKSIZE);
1143 }
1144 }
1145 return;
1146 }
1147
1148 if (!recursion_option)
1149 return;
1150
1151 if (one_file_system_option
1152 && !top_level
1153 && st->parent->stat.st_dev != st->stat.st_dev)
1154 {
1155 if (verbose_option)
1156 WARNOPT (WARN_XDEV,
1157 (0, 0,
1158 _("%s: file is on a different filesystem; not dumped"),
1159 quotearg_colon (st->orig_file_name)));
1160 }
1161 else
1162 {
1163 char *name_buf;
1164 size_t name_size;
1165
1166 switch (check_exclusion_tags (st, &tag_file_name))
1167 {
1168 case exclusion_tag_all:
1169 /* Handled in dump_file0 */
1170 break;
1171
1172 case exclusion_tag_none:
1173 {
1174 char const *entry;
1175 size_t entry_len;
1176 size_t name_len;
1177
1178 name_buf = xstrdup (st->orig_file_name);
1179 name_size = name_len = strlen (name_buf);
1180
1181 /* Now output all the files in the directory. */
1182 for (entry = directory; (entry_len = strlen (entry)) != 0;
1183 entry += entry_len + 1)
1184 {
1185 if (name_size < name_len + entry_len)
1186 {
1187 name_size = name_len + entry_len;
1188 name_buf = xrealloc (name_buf, name_size + 1);
1189 }
1190 strcpy (name_buf + name_len, entry);
1191 if (!excluded_name (name_buf))
1192 dump_file (st, entry, name_buf);
1193 }
1194
1195 free (name_buf);
1196 }
1197 break;
1198
1199 case exclusion_tag_contents:
1200 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1201 _("contents not dumped"));
1202 name_size = strlen (st->orig_file_name) + strlen (tag_file_name) + 1;
1203 name_buf = xmalloc (name_size);
1204 strcpy (name_buf, st->orig_file_name);
1205 strcat (name_buf, tag_file_name);
1206 dump_file (st, tag_file_name, name_buf);
1207 free (name_buf);
1208 break;
1209
1210 case exclusion_tag_under:
1211 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1212 _("contents not dumped"));
1213 break;
1214 }
1215 }
1216 }
1217
1218 /* Ensure exactly one trailing slash. */
1219 static void
1220 ensure_slash (char **pstr)
1221 {
1222 size_t len = strlen (*pstr);
1223 while (len >= 1 && ISSLASH ((*pstr)[len - 1]))
1224 len--;
1225 if (!ISSLASH ((*pstr)[len]))
1226 *pstr = xrealloc (*pstr, len + 2);
1227 (*pstr)[len++] = '/';
1228 (*pstr)[len] = '\0';
1229 }
1230
1231 /* If we just ran out of file descriptors, release a file descriptor
1232 in the directory chain somewhere leading from DIR->parent->parent
1233 up through the root. Return true if successful, false (preserving
1234 errno == EMFILE) otherwise.
1235
1236 Do not release DIR's file descriptor, or DIR's parent, as other
1237 code assumes that they work. On some operating systems, another
1238 process can claim file descriptor resources as we release them, and
1239 some calls or their emulations require multiple file descriptors,
1240 so callers should not give up if a single release doesn't work. */
1241
1242 static bool
1243 open_failure_recover (struct tar_stat_info const *dir)
1244 {
1245 if (errno == EMFILE && dir && dir->parent)
1246 {
1247 struct tar_stat_info *p;
1248 for (p = dir->parent->parent; p; p = p->parent)
1249 if (0 < p->fd && (! p->parent || p->parent->fd <= 0))
1250 {
1251 tar_stat_close (p);
1252 return true;
1253 }
1254 errno = EMFILE;
1255 }
1256
1257 return false;
1258 }
1259
1260 /* Return the directory entries of ST, in a dynamically allocated buffer,
1261 each entry followed by '\0' and the last followed by an extra '\0'.
1262 Return null on failure, setting errno. */
1263 char *
1264 get_directory_entries (struct tar_stat_info *st)
1265 {
1266 DIR *dirstream;
1267 while (! (dirstream = fdopendir (st->fd)) && open_failure_recover (st))
1268 continue;
1269
1270 if (! dirstream)
1271 return 0;
1272 else
1273 {
1274 char *entries = streamsavedir (dirstream);
1275 int streamsavedir_errno = errno;
1276
1277 int fd = dirfd (dirstream);
1278 if (fd < 0)
1279 {
1280 /* The dirent.h implementation doesn't use file descriptors
1281 for directory streams, so open the directory again. */
1282 char const *name = st->orig_file_name;
1283 if (closedir (dirstream) != 0)
1284 close_diag (name);
1285 dirstream = 0;
1286 fd = subfile_open (st->parent,
1287 st->parent ? last_component (name) : name,
1288 open_searchdir_flags);
1289 if (fd < 0)
1290 fd = - errno;
1291 else
1292 {
1293 struct stat dirst;
1294 if (! (fstat (fd, &dirst) == 0
1295 && st->stat.st_ino == dirst.st_ino
1296 && st->stat.st_dev == dirst.st_dev))
1297 {
1298 close (fd);
1299 fd = - IMPOSTOR_ERRNO;
1300 }
1301 }
1302 }
1303
1304 st->fd = fd;
1305 st->dirstream = dirstream;
1306 errno = streamsavedir_errno;
1307 return entries;
1308 }
1309 }
1310
1311 /* Dump the directory ST. Return true if successful, false (emitting
1312 diagnostics) otherwise. Get ST's entries, recurse through its
1313 subdirectories, and clean up file descriptors afterwards. */
1314 static bool
1315 dump_dir (struct tar_stat_info *st)
1316 {
1317 char *directory = get_directory_entries (st);
1318 if (! directory)
1319 {
1320 savedir_diag (st->orig_file_name);
1321 return false;
1322 }
1323
1324 dump_dir0 (st, directory);
1325
1326 restore_parent_fd (st);
1327 free (directory);
1328 return true;
1329 }
1330
1331 \f
1332 /* Number of links a file can have without having to be entered into
1333 the link table. Typically this is 1, but in trickier circumstances
1334 it is 0. */
1335 static nlink_t trivial_link_count;
1336
1337 \f
1338 /* Main functions of this module. */
1339
1340 void
1341 create_archive (void)
1342 {
1343 struct name const *p;
1344
1345 trivial_link_count = name_count <= 1 && ! dereference_option;
1346
1347 open_archive (ACCESS_WRITE);
1348 buffer_write_global_xheader ();
1349
1350 if (incremental_option)
1351 {
1352 size_t buffer_size = 1000;
1353 char *buffer = xmalloc (buffer_size);
1354 const char *q;
1355
1356 collect_and_sort_names ();
1357
1358 while ((p = name_from_list ()) != NULL)
1359 if (!excluded_name (p->name))
1360 dump_file (0, p->name, p->name);
1361
1362 blank_name_list ();
1363 while ((p = name_from_list ()) != NULL)
1364 if (!excluded_name (p->name))
1365 {
1366 struct tar_stat_info st;
1367 size_t plen = strlen (p->name);
1368 if (buffer_size <= plen)
1369 {
1370 while ((buffer_size *= 2) <= plen)
1371 continue;
1372 buffer = xrealloc (buffer, buffer_size);
1373 }
1374 memcpy (buffer, p->name, plen);
1375 if (! ISSLASH (buffer[plen - 1]))
1376 buffer[plen++] = DIRECTORY_SEPARATOR;
1377 tar_stat_init (&st);
1378 q = directory_contents (gnu_list_name->directory);
1379 if (q)
1380 while (*q)
1381 {
1382 size_t qlen = strlen (q);
1383 if (*q == 'Y')
1384 {
1385 if (! st.orig_file_name)
1386 {
1387 int fd = open (p->name, open_searchdir_flags);
1388 if (fd < 0)
1389 {
1390 open_diag (p->name);
1391 break;
1392 }
1393 st.fd = fd;
1394 if (fstat (fd, &st.stat) != 0)
1395 {
1396 stat_diag (p->name);
1397 break;
1398 }
1399 st.orig_file_name = xstrdup (p->name);
1400 }
1401 if (buffer_size < plen + qlen)
1402 {
1403 while ((buffer_size *=2 ) < plen + qlen)
1404 continue;
1405 buffer = xrealloc (buffer, buffer_size);
1406 }
1407 strcpy (buffer + plen, q + 1);
1408 dump_file (&st, q + 1, buffer);
1409 }
1410 q += qlen + 1;
1411 }
1412 tar_stat_destroy (&st);
1413 }
1414 free (buffer);
1415 }
1416 else
1417 {
1418 const char *name;
1419 while ((name = name_next (1)) != NULL)
1420 if (!excluded_name (name))
1421 dump_file (0, name, name);
1422 }
1423
1424 write_eot ();
1425 close_archive ();
1426 finish_deferred_unlinks ();
1427 if (listed_incremental_option)
1428 write_directory_file ();
1429 }
1430
1431
1432 /* Calculate the hash of a link. */
1433 static size_t
1434 hash_link (void const *entry, size_t n_buckets)
1435 {
1436 struct link const *l = entry;
1437 uintmax_t num = l->dev ^ l->ino;
1438 return num % n_buckets;
1439 }
1440
1441 /* Compare two links for equality. */
1442 static bool
1443 compare_links (void const *entry1, void const *entry2)
1444 {
1445 struct link const *link1 = entry1;
1446 struct link const *link2 = entry2;
1447 return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
1448 }
1449
1450 static void
1451 unknown_file_error (char const *p)
1452 {
1453 WARNOPT (WARN_FILE_IGNORED,
1454 (0, 0, _("%s: Unknown file type; file ignored"),
1455 quotearg_colon (p)));
1456 if (!ignore_failed_read_option)
1457 set_exit_status (TAREXIT_FAILURE);
1458 }
1459
1460 \f
1461 /* Handling of hard links */
1462
1463 /* Table of all non-directories that we've written so far. Any time
1464 we see another, we check the table and avoid dumping the data
1465 again if we've done it once already. */
1466 static Hash_table *link_table;
1467
1468 /* Try to dump stat as a hard link to another file in the archive.
1469 Return true if successful. */
1470 static bool
1471 dump_hard_link (struct tar_stat_info *st)
1472 {
1473 if (link_table
1474 && (trivial_link_count < st->stat.st_nlink || remove_files_option))
1475 {
1476 struct link lp;
1477 struct link *duplicate;
1478 off_t block_ordinal;
1479 union block *blk;
1480
1481 lp.ino = st->stat.st_ino;
1482 lp.dev = st->stat.st_dev;
1483
1484 if ((duplicate = hash_lookup (link_table, &lp)))
1485 {
1486 /* We found a link. */
1487 char const *link_name = safer_name_suffix (duplicate->name, true,
1488 absolute_names_option);
1489
1490 duplicate->nlink--;
1491
1492 block_ordinal = current_block_ordinal ();
1493 assign_string (&st->link_name, link_name);
1494 if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT)
1495 < strlen (link_name))
1496 write_long_link (st);
1497
1498 st->stat.st_size = 0;
1499 blk = start_header (st);
1500 if (!blk)
1501 return false;
1502 tar_copy_str (blk->header.linkname, link_name, NAME_FIELD_SIZE);
1503
1504 blk->header.typeflag = LNKTYPE;
1505 finish_header (st, blk, block_ordinal);
1506
1507 if (remove_files_option)
1508 queue_deferred_unlink (st->orig_file_name, false);
1509
1510 return true;
1511 }
1512 }
1513 return false;
1514 }
1515
1516 static void
1517 file_count_links (struct tar_stat_info *st)
1518 {
1519 if (hard_dereference_option)
1520 return;
1521 if (trivial_link_count < st->stat.st_nlink)
1522 {
1523 struct link *duplicate;
1524 char *linkname = NULL;
1525 struct link *lp;
1526
1527 assign_string (&linkname, st->orig_file_name);
1528 transform_name (&linkname, XFORM_LINK);
1529
1530 lp = xmalloc (offsetof (struct link, name)
1531 + strlen (linkname) + 1);
1532 lp->ino = st->stat.st_ino;
1533 lp->dev = st->stat.st_dev;
1534 lp->nlink = st->stat.st_nlink;
1535 strcpy (lp->name, linkname);
1536 free (linkname);
1537
1538 if (! ((link_table
1539 || (link_table = hash_initialize (0, 0, hash_link,
1540 compare_links, 0)))
1541 && (duplicate = hash_insert (link_table, lp))))
1542 xalloc_die ();
1543
1544 if (duplicate != lp)
1545 abort ();
1546 lp->nlink--;
1547 }
1548 }
1549
1550 /* For each dumped file, check if all its links were dumped. Emit
1551 warnings if it is not so. */
1552 void
1553 check_links (void)
1554 {
1555 struct link *lp;
1556
1557 if (!link_table)
1558 return;
1559
1560 for (lp = hash_get_first (link_table); lp;
1561 lp = hash_get_next (link_table, lp))
1562 {
1563 if (lp->nlink)
1564 {
1565 WARN ((0, 0, _("Missing links to %s."), quote (lp->name)));
1566 }
1567 }
1568 }
1569
1570 /* Assuming DIR is the working directory, open FILE, using FLAGS to
1571 control the open. A null DIR means to use ".". If we are low on
1572 file descriptors, try to release one or more from DIR's parents to
1573 reuse it. */
1574 int
1575 subfile_open (struct tar_stat_info const *dir, char const *file, int flags)
1576 {
1577 int fd;
1578
1579 static bool initialized;
1580 if (! initialized)
1581 {
1582 /* Initialize any tables that might be needed when file
1583 descriptors are exhausted, and whose initialization might
1584 require a file descriptor. This includes the system message
1585 catalog and tar's message catalog. */
1586 initialized = true;
1587 strerror (ENOENT);
1588 gettext ("");
1589 }
1590
1591 while ((fd = openat (dir ? dir->fd : AT_FDCWD, file, flags)) < 0
1592 && open_failure_recover (dir))
1593 continue;
1594 return fd;
1595 }
1596
1597 /* Restore the file descriptor for ST->parent, if it was temporarily
1598 closed to conserve file descriptors. On failure, set the file
1599 descriptor to the negative of the corresponding errno value. Call
1600 this every time a subdirectory is ascended from. */
1601 void
1602 restore_parent_fd (struct tar_stat_info const *st)
1603 {
1604 struct tar_stat_info *parent = st->parent;
1605 if (parent && ! parent->fd)
1606 {
1607 int parentfd = openat (st->fd, "..", open_searchdir_flags);
1608 struct stat parentstat;
1609
1610 if (parentfd < 0)
1611 parentfd = - errno;
1612 else if (! (fstat (parentfd, &parentstat) == 0
1613 && parent->stat.st_ino == parentstat.st_ino
1614 && parent->stat.st_dev == parentstat.st_dev))
1615 {
1616 close (parentfd);
1617 parentfd = IMPOSTOR_ERRNO;
1618 }
1619
1620 if (parentfd < 0)
1621 {
1622 int origfd = open (parent->orig_file_name, open_searchdir_flags);
1623 if (0 <= origfd)
1624 {
1625 if (fstat (parentfd, &parentstat) == 0
1626 && parent->stat.st_ino == parentstat.st_ino
1627 && parent->stat.st_dev == parentstat.st_dev)
1628 parentfd = origfd;
1629 else
1630 close (origfd);
1631 }
1632 }
1633
1634 parent->fd = parentfd;
1635 }
1636 }
1637
1638 /* Dump a single file, recursing on directories. ST is the file's
1639 status info, NAME its name relative to the parent directory, and P
1640 its full name (which may be relative to the working directory). */
1641
1642 /* FIXME: One should make sure that for *every* path leading to setting
1643 exit_status to failure, a clear diagnostic has been issued. */
1644
1645 static void
1646 dump_file0 (struct tar_stat_info *st, char const *name, char const *p)
1647 {
1648 union block *header;
1649 char type;
1650 off_t original_size;
1651 struct timespec original_ctime;
1652 struct timespec restore_times[2];
1653 off_t block_ordinal = -1;
1654 int fd = 0;
1655 bool is_dir;
1656 struct tar_stat_info const *parent = st->parent;
1657 bool top_level = ! parent;
1658 int parentfd = top_level ? AT_FDCWD : parent->fd;
1659 void (*diag) (char const *) = 0;
1660
1661 if (interactive_option && !confirm ("add", p))
1662 return;
1663
1664 assign_string (&st->orig_file_name, p);
1665 assign_string (&st->file_name,
1666 safer_name_suffix (p, false, absolute_names_option));
1667
1668 transform_name (&st->file_name, XFORM_REGFILE);
1669
1670 if (parentfd < 0 && ! top_level)
1671 {
1672 errno = - parentfd;
1673 diag = open_diag;
1674 }
1675 else if (fstatat (parentfd, name, &st->stat, fstatat_flags) != 0)
1676 diag = stat_diag;
1677 else if (file_dumpable_p (&st->stat))
1678 {
1679 fd = subfile_open (parent, name, open_read_flags);
1680 if (fd < 0)
1681 diag = open_diag;
1682 else
1683 {
1684 st->fd = fd;
1685 if (fstat (fd, &st->stat) != 0)
1686 diag = stat_diag;
1687 }
1688 }
1689 if (diag)
1690 {
1691 file_removed_diag (p, top_level, diag);
1692 return;
1693 }
1694
1695 st->archive_file_size = original_size = st->stat.st_size;
1696 st->atime = restore_times[0] = get_stat_atime (&st->stat);
1697 st->mtime = restore_times[1] = get_stat_mtime (&st->stat);
1698 st->ctime = original_ctime = get_stat_ctime (&st->stat);
1699
1700 #ifdef S_ISHIDDEN
1701 if (S_ISHIDDEN (st->stat.st_mode))
1702 {
1703 char *new = (char *) alloca (strlen (p) + 2);
1704 if (new)
1705 {
1706 strcpy (new, p);
1707 strcat (new, "@");
1708 p = new;
1709 }
1710 }
1711 #endif
1712
1713 /* See if we want only new files, and check if this one is too old to
1714 put in the archive.
1715
1716 This check is omitted if incremental_option is set *and* the
1717 requested file is not explicitely listed in the command line. */
1718
1719 if (!(incremental_option && !is_individual_file (p))
1720 && !S_ISDIR (st->stat.st_mode)
1721 && OLDER_TAR_STAT_TIME (*st, m)
1722 && (!after_date_option || OLDER_TAR_STAT_TIME (*st, c)))
1723 {
1724 if (!incremental_option && verbose_option)
1725 WARNOPT (WARN_FILE_UNCHANGED,
1726 (0, 0, _("%s: file is unchanged; not dumped"),
1727 quotearg_colon (p)));
1728 return;
1729 }
1730
1731 /* See if we are trying to dump the archive. */
1732 if (sys_file_is_archive (st))
1733 {
1734 WARNOPT (WARN_IGNORE_ARCHIVE,
1735 (0, 0, _("%s: file is the archive; not dumped"),
1736 quotearg_colon (p)));
1737 return;
1738 }
1739
1740 is_dir = S_ISDIR (st->stat.st_mode) != 0;
1741
1742 if (!is_dir && dump_hard_link (st))
1743 return;
1744
1745 if (is_dir || S_ISREG (st->stat.st_mode) || S_ISCTG (st->stat.st_mode))
1746 {
1747 bool ok;
1748 struct stat final_stat;
1749
1750 if (is_dir)
1751 {
1752 const char *tag_file_name;
1753 ensure_slash (&st->orig_file_name);
1754 ensure_slash (&st->file_name);
1755
1756 if (check_exclusion_tags (st, &tag_file_name) == exclusion_tag_all)
1757 {
1758 exclusion_tag_warning (st->orig_file_name, tag_file_name,
1759 _("directory not dumped"));
1760 return;
1761 }
1762
1763 ok = dump_dir (st);
1764
1765 fd = st->fd;
1766 parentfd = top_level ? AT_FDCWD : parent->fd;
1767 }
1768 else
1769 {
1770 enum dump_status status;
1771
1772 if (fd && sparse_option && ST_IS_SPARSE (st->stat))
1773 {
1774 status = sparse_dump_file (fd, st);
1775 if (status == dump_status_not_implemented)
1776 status = dump_regular_file (fd, st);
1777 }
1778 else
1779 status = dump_regular_file (fd, st);
1780
1781 switch (status)
1782 {
1783 case dump_status_ok:
1784 case dump_status_short:
1785 file_count_links (st);
1786 break;
1787
1788 case dump_status_fail:
1789 break;
1790
1791 case dump_status_not_implemented:
1792 abort ();
1793 }
1794
1795 ok = status == dump_status_ok;
1796 }
1797
1798 if (ok)
1799 {
1800 if (fd < 0)
1801 {
1802 errno = - fd;
1803 ok = false;
1804 }
1805 else if (fd == 0)
1806 {
1807 if (parentfd < 0 && ! top_level)
1808 {
1809 errno = - parentfd;
1810 ok = false;
1811 }
1812 else
1813 ok = fstatat (parentfd, name, &final_stat, fstatat_flags) == 0;
1814 }
1815 else
1816 ok = fstat (fd, &final_stat) == 0;
1817
1818 if (! ok)
1819 file_removed_diag (p, top_level, stat_diag);
1820 }
1821
1822 if (ok)
1823 {
1824 if ((timespec_cmp (get_stat_ctime (&final_stat), original_ctime) != 0
1825 /* Original ctime will change if the file is a directory and
1826 --remove-files is given */
1827 && !(remove_files_option && is_dir))
1828 || original_size < final_stat.st_size)
1829 {
1830 WARNOPT (WARN_FILE_CHANGED,
1831 (0, 0, _("%s: file changed as we read it"),
1832 quotearg_colon (p)));
1833 set_exit_status (TAREXIT_DIFFERS);
1834 }
1835 else if (atime_preserve_option == replace_atime_preserve
1836 && set_file_atime (fd, p, restore_times) != 0)
1837 utime_error (p);
1838 }
1839
1840 ok &= tar_stat_close (st);
1841 if (ok && remove_files_option)
1842 queue_deferred_unlink (p, is_dir);
1843
1844 return;
1845 }
1846 #ifdef HAVE_READLINK
1847 else if (S_ISLNK (st->stat.st_mode))
1848 {
1849 char *buffer;
1850 int size;
1851 size_t linklen = st->stat.st_size;
1852 if (linklen != st->stat.st_size || linklen + 1 == 0)
1853 xalloc_die ();
1854 buffer = (char *) alloca (linklen + 1);
1855 size = readlinkat (parentfd, name, buffer, linklen + 1);
1856 if (size < 0)
1857 {
1858 file_removed_diag (p, top_level, readlink_diag);
1859 return;
1860 }
1861 buffer[size] = '\0';
1862 assign_string (&st->link_name, buffer);
1863 transform_name (&st->link_name, XFORM_SYMLINK);
1864 if (NAME_FIELD_SIZE - (archive_format == OLDGNU_FORMAT) < size)
1865 write_long_link (st);
1866
1867 block_ordinal = current_block_ordinal ();
1868 st->stat.st_size = 0; /* force 0 size on symlink */
1869 header = start_header (st);
1870 if (!header)
1871 return;
1872 tar_copy_str (header->header.linkname, st->link_name, NAME_FIELD_SIZE);
1873 header->header.typeflag = SYMTYPE;
1874 finish_header (st, header, block_ordinal);
1875 /* nothing more to do to it */
1876
1877 if (remove_files_option)
1878 queue_deferred_unlink (p, false);
1879
1880 file_count_links (st);
1881 return;
1882 }
1883 #endif
1884 else if (S_ISCHR (st->stat.st_mode))
1885 type = CHRTYPE;
1886 else if (S_ISBLK (st->stat.st_mode))
1887 type = BLKTYPE;
1888 else if (S_ISFIFO (st->stat.st_mode))
1889 type = FIFOTYPE;
1890 else if (S_ISSOCK (st->stat.st_mode))
1891 {
1892 WARNOPT (WARN_FILE_IGNORED,
1893 (0, 0, _("%s: socket ignored"), quotearg_colon (p)));
1894 return;
1895 }
1896 else if (S_ISDOOR (st->stat.st_mode))
1897 {
1898 WARNOPT (WARN_FILE_IGNORED,
1899 (0, 0, _("%s: door ignored"), quotearg_colon (p)));
1900 return;
1901 }
1902 else
1903 {
1904 unknown_file_error (p);
1905 return;
1906 }
1907
1908 if (archive_format == V7_FORMAT)
1909 {
1910 unknown_file_error (p);
1911 return;
1912 }
1913
1914 block_ordinal = current_block_ordinal ();
1915 st->stat.st_size = 0; /* force 0 size */
1916 header = start_header (st);
1917 if (!header)
1918 return;
1919 header->header.typeflag = type;
1920
1921 if (type != FIFOTYPE)
1922 {
1923 MAJOR_TO_CHARS (major (st->stat.st_rdev),
1924 header->header.devmajor);
1925 MINOR_TO_CHARS (minor (st->stat.st_rdev),
1926 header->header.devminor);
1927 }
1928
1929 finish_header (st, header, block_ordinal);
1930 if (remove_files_option)
1931 queue_deferred_unlink (p, false);
1932 }
1933
1934 /* Dump a file, recursively. PARENT describes the file's parent
1935 directory, NAME is the file's name relative to PARENT, and FULLNAME
1936 its full name, possibly relative to the working directory. NAME
1937 may contain slashes at the top level of invocation. */
1938
1939 void
1940 dump_file (struct tar_stat_info *parent, char const *name,
1941 char const *fullname)
1942 {
1943 struct tar_stat_info st;
1944 tar_stat_init (&st);
1945 st.parent = parent;
1946 dump_file0 (&st, name, fullname);
1947 if (parent && listed_incremental_option)
1948 update_parent_directory (parent);
1949 tar_stat_destroy (&st);
1950 }
This page took 0.12192 seconds and 5 git commands to generate.