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