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