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