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