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