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