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