]> Dogcows Code - chaz/tar/blob - src/create.c
Use current_stat_info
[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 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 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "system.h"
23
24 #if !MSDOS
25 # include <pwd.h>
26 # include <grp.h>
27 #endif
28
29 #if HAVE_UTIME_H
30 # include <utime.h>
31 #else
32 struct utimbuf
33 {
34 long actime;
35 long modtime;
36 };
37 #endif
38
39 #include <quotearg.h>
40
41 #include "common.h"
42 #include <hash.h>
43
44 #ifndef MSDOS
45 extern dev_t ar_dev;
46 extern ino_t ar_ino;
47 #endif
48
49 struct link
50 {
51 dev_t dev;
52 ino_t ino;
53 size_t nlink;
54 char name[1];
55 };
56 \f
57 /* The maximum uintmax_t value that can be represented with DIGITS digits,
58 assuming that each digit is BITS_PER_DIGIT wide. */
59 #define MAX_VAL_WITH_DIGITS(digits, bits_per_digit) \
60 ((digits) * (bits_per_digit) < sizeof (uintmax_t) * CHAR_BIT \
61 ? ((uintmax_t) 1 << ((digits) * (bits_per_digit))) - 1 \
62 : (uintmax_t) -1)
63
64 /* Convert VALUE to an octal representation suitable for tar headers.
65 Output to buffer WHERE with size SIZE.
66 The result is undefined if SIZE is 0 or if VALUE is too large to fit. */
67
68 static void
69 to_octal (uintmax_t value, char *where, size_t size)
70 {
71 uintmax_t v = value;
72 size_t i = size;
73
74 do
75 {
76 where[--i] = '0' + (v & ((1 << LG_8) - 1));
77 v >>= LG_8;
78 }
79 while (i);
80 }
81
82 /* Convert NEGATIVE VALUE to a base-256 representation suitable for
83 tar headers. NEGATIVE is 1 if VALUE was negative before being cast
84 to uintmax_t, 0 otherwise. Output to buffer WHERE with size SIZE.
85 The result is undefined if SIZE is 0 or if VALUE is too large to
86 fit. */
87
88 static void
89 to_base256 (int negative, uintmax_t value, char *where, size_t size)
90 {
91 uintmax_t v = value;
92 uintmax_t propagated_sign_bits =
93 ((uintmax_t) - negative << (CHAR_BIT * sizeof v - LG_256));
94 size_t i = size;
95
96 do
97 {
98 where[--i] = v & ((1 << LG_256) - 1);
99 v = propagated_sign_bits | (v >> LG_256);
100 }
101 while (i);
102 }
103
104 /* Convert NEGATIVE VALUE (which was originally of size VALSIZE) to
105 external form, using SUBSTITUTE (...) if VALUE won't fit. Output
106 to buffer WHERE with size SIZE. NEGATIVE is 1 iff VALUE was
107 negative before being cast to uintmax_t; its original bitpattern
108 can be deduced from VALSIZE, its original size before casting.
109 TYPE is the kind of value being output (useful for diagnostics).
110 Prefer the POSIX format of SIZE - 1 octal digits (with leading zero
111 digits), followed by '\0'. If this won't work, and if GNU or
112 OLDGNU format is allowed, use '\200' followed by base-256, or (if
113 NEGATIVE is nonzero) '\377' followed by two's complement base-256.
114 If neither format works, use SUBSTITUTE (...) instead. Pass to
115 SUBSTITUTE the address of an 0-or-1 flag recording whether the
116 substitute value is negative. */
117
118 static void
119 to_chars (int negative, uintmax_t value, size_t valsize,
120 uintmax_t (*substitute) (int *),
121 char *where, size_t size, const char *type)
122 {
123 int base256_allowed = (archive_format == GNU_FORMAT
124 || archive_format == OLDGNU_FORMAT);
125
126 /* Generate the POSIX octal representation if the number fits. */
127 if (! negative && value <= MAX_VAL_WITH_DIGITS (size - 1, LG_8))
128 {
129 where[size - 1] = '\0';
130 to_octal (value, where, size - 1);
131 }
132
133 /* Otherwise, generate the base-256 representation if we are
134 generating an old or new GNU format and if the number fits. */
135 else if (((negative ? -1 - value : value)
136 <= MAX_VAL_WITH_DIGITS (size - 1, LG_256))
137 && base256_allowed)
138 {
139 where[0] = negative ? -1 : 1 << (LG_256 - 1);
140 to_base256 (negative, value, where + 1, size - 1);
141 }
142
143 /* Otherwise, if the number is negative, and if it would not cause
144 ambiguity on this host by confusing positive with negative
145 values, then generate the POSIX octal representation of the value
146 modulo 2**(field bits). The resulting tar file is
147 machine-dependent, since it depends on the host word size. Yuck!
148 But this is the traditional behavior. */
149 else if (negative && valsize * CHAR_BIT <= (size - 1) * LG_8)
150 {
151 static int warned_once;
152 if (! warned_once)
153 {
154 warned_once = 1;
155 WARN ((0, 0, _("Generating negative octal headers")));
156 }
157 where[size - 1] = '\0';
158 to_octal (value & MAX_VAL_WITH_DIGITS (valsize * CHAR_BIT, 1),
159 where, size - 1);
160 }
161
162 /* Otherwise, output a substitute value if possible (with a
163 warning), and an error message if not. */
164 else
165 {
166 uintmax_t maxval = (base256_allowed
167 ? MAX_VAL_WITH_DIGITS (size - 1, LG_256)
168 : MAX_VAL_WITH_DIGITS (size - 1, LG_8));
169 char valbuf[UINTMAX_STRSIZE_BOUND + 1];
170 char maxbuf[UINTMAX_STRSIZE_BOUND];
171 char minbuf[UINTMAX_STRSIZE_BOUND + 1];
172 char const *minval_string;
173 char const *maxval_string = STRINGIFY_BIGINT (maxval, maxbuf);
174 char const *value_string;
175
176 if (base256_allowed)
177 {
178 uintmax_t m = maxval + 1 ? maxval + 1 : maxval / 2 + 1;
179 char *p = STRINGIFY_BIGINT (m, minbuf + 1);
180 *--p = '-';
181 minval_string = p;
182 }
183 else
184 minval_string = "0";
185
186 if (negative)
187 {
188 char *p = STRINGIFY_BIGINT (- value, valbuf + 1);
189 *--p = '-';
190 value_string = p;
191 }
192 else
193 value_string = STRINGIFY_BIGINT (value, valbuf);
194
195 if (substitute)
196 {
197 int negsub;
198 uintmax_t sub = substitute (&negsub) & maxval;
199 uintmax_t s = (negsub &= archive_format == GNU_FORMAT) ? - sub : sub;
200 char subbuf[UINTMAX_STRSIZE_BOUND + 1];
201 char *sub_string = STRINGIFY_BIGINT (s, subbuf + 1);
202 if (negsub)
203 *--sub_string = '-';
204 WARN ((0, 0, _("value %s out of %s range %s..%s; substituting %s"),
205 value_string, type, minval_string, maxval_string,
206 sub_string));
207 to_chars (negsub, s, valsize, 0, where, size, type);
208 }
209 else
210 ERROR ((0, 0, _("value %s out of %s range %s..%s"),
211 value_string, type, minval_string, maxval_string));
212 }
213 }
214
215 static uintmax_t
216 gid_substitute (int *negative)
217 {
218 gid_t r;
219 #ifdef GID_NOBODY
220 r = GID_NOBODY;
221 #else
222 static gid_t gid_nobody;
223 if (!gid_nobody && !gname_to_gid ("nobody", &gid_nobody))
224 gid_nobody = -2;
225 r = gid_nobody;
226 #endif
227 *negative = r < 0;
228 return r;
229 }
230
231 void
232 gid_to_chars (gid_t v, char *p, size_t s)
233 {
234 to_chars (v < 0, (uintmax_t) v, sizeof v, gid_substitute, p, s, "gid_t");
235 }
236
237 void
238 major_to_chars (major_t v, char *p, size_t s)
239 {
240 to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "major_t");
241 }
242
243 void
244 minor_to_chars (minor_t v, char *p, size_t s)
245 {
246 to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "minor_t");
247 }
248
249 void
250 mode_to_chars (mode_t v, char *p, size_t s)
251 {
252 /* In the common case where the internal and external mode bits are the same,
253 and we are not using POSIX or GNU format,
254 propagate all unknown bits to the external mode.
255 This matches historical practice.
256 Otherwise, just copy the bits we know about. */
257 int negative;
258 uintmax_t u;
259 if (S_ISUID == TSUID && S_ISGID == TSGID && S_ISVTX == TSVTX
260 && S_IRUSR == TUREAD && S_IWUSR == TUWRITE && S_IXUSR == TUEXEC
261 && S_IRGRP == TGREAD && S_IWGRP == TGWRITE && S_IXGRP == TGEXEC
262 && S_IROTH == TOREAD && S_IWOTH == TOWRITE && S_IXOTH == TOEXEC
263 && archive_format != POSIX_FORMAT
264 && archive_format != GNU_FORMAT)
265 {
266 negative = v < 0;
267 u = v;
268 }
269 else
270 {
271 negative = 0;
272 u = ((v & S_ISUID ? TSUID : 0)
273 | (v & S_ISGID ? TSGID : 0)
274 | (v & S_ISVTX ? TSVTX : 0)
275 | (v & S_IRUSR ? TUREAD : 0)
276 | (v & S_IWUSR ? TUWRITE : 0)
277 | (v & S_IXUSR ? TUEXEC : 0)
278 | (v & S_IRGRP ? TGREAD : 0)
279 | (v & S_IWGRP ? TGWRITE : 0)
280 | (v & S_IXGRP ? TGEXEC : 0)
281 | (v & S_IROTH ? TOREAD : 0)
282 | (v & S_IWOTH ? TOWRITE : 0)
283 | (v & S_IXOTH ? TOEXEC : 0));
284 }
285 to_chars (negative, u, sizeof v, 0, p, s, "mode_t");
286 }
287
288 void
289 off_to_chars (off_t v, char *p, size_t s)
290 {
291 to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "off_t");
292 }
293
294 void
295 size_to_chars (size_t v, char *p, size_t s)
296 {
297 to_chars (0, (uintmax_t) v, sizeof v, 0, p, s, "size_t");
298 }
299
300 void
301 time_to_chars (time_t v, char *p, size_t s)
302 {
303 to_chars (v < 0, (uintmax_t) v, sizeof v, 0, p, s, "time_t");
304 }
305
306 static uintmax_t
307 uid_substitute (int *negative)
308 {
309 uid_t r;
310 #ifdef UID_NOBODY
311 r = UID_NOBODY;
312 #else
313 static uid_t uid_nobody;
314 if (!uid_nobody && !uname_to_uid ("nobody", &uid_nobody))
315 uid_nobody = -2;
316 r = uid_nobody;
317 #endif
318 *negative = r < 0;
319 return r;
320 }
321
322 void
323 uid_to_chars (uid_t v, char *p, size_t s)
324 {
325 to_chars (v < 0, (uintmax_t) v, sizeof v, uid_substitute, p, s, "uid_t");
326 }
327
328 void
329 uintmax_to_chars (uintmax_t v, char *p, size_t s)
330 {
331 to_chars (0, v, sizeof v, 0, p, s, "uintmax_t");
332 }
333 \f
334 /* Writing routines. */
335
336 /* Zero out the buffer so we don't confuse ourselves with leftover
337 data. */
338 static void
339 clear_buffer (char *buffer)
340 {
341 memset (buffer, 0, BLOCKSIZE);
342 }
343
344 /* Write the EOT block(s). Zero at least two blocks, through the end
345 of the record. Old tar, as previous versions of GNU tar, writes
346 garbage after two zeroed blocks. */
347 void
348 write_eot (void)
349 {
350 union block *pointer = find_next_block ();
351 memset (pointer->buffer, 0, BLOCKSIZE);
352 set_next_block_after (pointer);
353 pointer = find_next_block ();
354 memset (pointer->buffer, 0, available_space_after (pointer));
355 set_next_block_after (pointer);
356 }
357
358 /* Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. */
359
360 /* FIXME: Cross recursion between start_header and write_long! */
361
362 static union block *start_header (const char *, struct tar_stat_info *);
363
364 static void
365 write_long (const char *p, char type)
366 {
367 size_t size = strlen (p) + 1;
368 size_t bufsize;
369 union block *header;
370 struct tar_stat_info foo;
371
372 memset (&foo, 0, sizeof foo);
373 foo.stat.st_size = size;
374
375 header = start_header ("././@LongLink", &foo);
376 header->header.typeflag = type;
377 finish_header (header, -1);
378
379 header = find_next_block ();
380
381 bufsize = available_space_after (header);
382
383 while (bufsize < size)
384 {
385 memcpy (header->buffer, p, bufsize);
386 p += bufsize;
387 size -= bufsize;
388 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
389 header = find_next_block ();
390 bufsize = available_space_after (header);
391 }
392 memcpy (header->buffer, p, size);
393 memset (header->buffer + size, 0, bufsize - size);
394 set_next_block_after (header + (size - 1) / BLOCKSIZE);
395 }
396 \f
397 /* Header handling. */
398
399 /* Make a header block for the file whose stat info is st,
400 and return its address. */
401
402 static union block *
403 start_header (const char *name, struct tar_stat_info *st)
404 {
405 union block *header;
406
407 name = safer_name_suffix (name, 0);
408
409 if (sizeof header->header.name <= strlen (name))
410 write_long (name, GNUTYPE_LONGNAME);
411 header = find_next_block ();
412 memset (header->buffer, 0, sizeof (union block));
413
414 assign_string (&current_stat_info.file_name, name);
415
416 strncpy (header->header.name, name, NAME_FIELD_SIZE);
417 header->header.name[NAME_FIELD_SIZE - 1] = '\0';
418
419 /* Override some stat fields, if requested to do so. */
420
421 if (owner_option != (uid_t) -1)
422 st->stat.st_uid = owner_option;
423 if (group_option != (gid_t) -1)
424 st->stat.st_gid = group_option;
425 if (mode_option)
426 st->stat.st_mode = ((st->stat.st_mode & ~MODE_ALL)
427 | mode_adjust (st->stat.st_mode, mode_option));
428
429 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
430 for a few tars and came up with the following interoperability
431 matrix:
432
433 WRITER
434 1 2 3 4 5 6 7 8 9 READER
435 . . . . . . . . . 1 = SunOS 4.2 tar
436 # . . # # . . # # 2 = NEC SVR4.0.2 tar
437 . . . # # . . # . 3 = Solaris 2.1 tar
438 . . . . . . . . . 4 = GNU tar 1.11.1
439 . . . . . . . . . 5 = HP-UX 8.07 tar
440 . . . . . . . . . 6 = Ultrix 4.1
441 . . . . . . . . . 7 = AIX 3.2
442 . . . . . . . . . 8 = Hitachi HI-UX 1.03
443 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
444
445 . = works
446 # = ``impossible file type''
447
448 The following mask for old archive removes the `#'s in column 4
449 above, thus making GNU tar both a universal donor and a universal
450 acceptor for Paul's test. */
451
452 if (archive_format == V7_FORMAT)
453 MODE_TO_CHARS (st->stat.st_mode & MODE_ALL, header->header.mode);
454 else
455 MODE_TO_CHARS (st->stat.st_mode, header->header.mode);
456
457 UID_TO_CHARS (st->stat.st_uid, header->header.uid);
458 GID_TO_CHARS (st->stat.st_gid, header->header.gid);
459 OFF_TO_CHARS (st->stat.st_size, header->header.size);
460 TIME_TO_CHARS (st->stat.st_mtime, header->header.mtime);
461 MAJOR_TO_CHARS (0, header->header.devmajor);
462 MINOR_TO_CHARS (0, header->header.devminor);
463
464 if (incremental_option)
465 if (archive_format == OLDGNU_FORMAT)
466 {
467 TIME_TO_CHARS (st->stat.st_atime, header->oldgnu_header.atime);
468 TIME_TO_CHARS (st->stat.st_ctime, header->oldgnu_header.ctime);
469 }
470
471 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
472
473 switch (archive_format)
474 {
475 case V7_FORMAT:
476 break;
477
478 case OLDGNU_FORMAT:
479 /* Overwrite header->header.magic and header.version in one blow. */
480 strcpy (header->header.magic, OLDGNU_MAGIC);
481 break;
482
483 case POSIX_FORMAT:
484 case GNU_FORMAT:
485 strncpy (header->header.magic, TMAGIC, TMAGLEN);
486 strncpy (header->header.version, TVERSION, TVERSLEN);
487 break;
488
489 default:
490 abort ();
491 }
492
493 if (archive_format == V7_FORMAT || numeric_owner_option)
494 {
495 /* header->header.[ug]name are left as the empty string. */
496 }
497 else
498 {
499 uid_to_uname (st->stat.st_uid, header->header.uname);
500 gid_to_gname (st->stat.st_gid, header->header.gname);
501 }
502
503 return header;
504 }
505
506 /* Finish off a filled-in header block and write it out. We also
507 print the file name and/or full info if verbose is on. If BLOCK_ORDINAL
508 is not negative, is the block ordinal of the first record for this
509 file, which may be a preceding long name or long link record. */
510 void
511 finish_header (union block *header, off_t block_ordinal)
512 {
513 size_t i;
514 int sum;
515 char *p;
516
517 memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
518
519 sum = 0;
520 p = header->buffer;
521 for (i = sizeof *header; i-- != 0; )
522 /* We can't use unsigned char here because of old compilers, e.g. V7. */
523 sum += 0xFF & *p++;
524
525 /* Fill in the checksum field. It's formatted differently from the
526 other fields: it has [6] digits, a null, then a space -- rather than
527 digits, then a null. We use to_chars.
528 The final space is already there, from
529 checksumming, and to_chars doesn't modify it.
530
531 This is a fast way to do:
532
533 sprintf(header->header.chksum, "%6o", sum); */
534
535 uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
536
537 if (verbose_option
538 && header->header.typeflag != GNUTYPE_LONGLINK
539 && header->header.typeflag != GNUTYPE_LONGNAME)
540 {
541 /* These globals are parameters to print_header, sigh. */
542
543 current_header = header;
544 /* current_stat_info is already set up. */
545 current_format = archive_format;
546 print_header (block_ordinal);
547 }
548
549 set_next_block_after (header);
550 }
551 \f
552 /* Sparse file processing. */
553
554 /* Takes a blockful of data and basically cruises through it to see if
555 it's made *entirely* of zeros, returning a 0 the instant it finds
556 something that is a nonzero, i.e., useful data. */
557 static int
558 zero_block_p (char *buffer)
559 {
560 int counter;
561
562 for (counter = 0; counter < BLOCKSIZE; counter++)
563 if (buffer[counter] != '\0')
564 return 0;
565 return 1;
566 }
567
568 void
569 init_sparsearray (void)
570 {
571 if (! sp_array_size)
572 sp_array_size = SPARSES_IN_OLDGNU_HEADER;
573 sparsearray = xmalloc (sp_array_size * sizeof *sparsearray);
574 }
575
576 static off_t
577 find_new_file_size (int sparses)
578 {
579 int i;
580 off_t s = 0;
581 for (i = 0; i < sparses; i++)
582 s += sparsearray[i].numbytes;
583 return s;
584 }
585
586 /* Make one pass over the file NAME, studying where any non-zero data
587 is, that is, how far into the file each instance of data is, and
588 how many bytes are there. Save this information in the
589 sparsearray, which will later be translated into header
590 information. */
591
592 /* There is little point in trimming small amounts of null data at the head
593 and tail of blocks, only avoid dumping full null blocks. */
594
595 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
596 too kludgey for my taste... */
597
598 static int
599 deal_with_sparse (char *name, union block *header)
600 {
601 size_t numbytes = 0;
602 off_t offset = 0;
603 int file;
604 int sparses = 0;
605 ssize_t count;
606 char buffer[BLOCKSIZE];
607
608 if (archive_format == OLDGNU_FORMAT)
609 header->oldgnu_header.isextended = 0;
610
611 if (file = open (name, O_RDONLY), file < 0)
612 /* This problem will be caught later on, so just return. */
613 return 0;
614
615 init_sparsearray ();
616 clear_buffer (buffer);
617
618 for (;;)
619 {
620 /* Realloc the scratch area as necessary. FIXME: should reallocate
621 only at beginning of a new instance of non-zero data. */
622
623 if (sp_array_size <= sparses)
624 {
625 sparsearray =
626 xrealloc (sparsearray,
627 2 * sp_array_size * sizeof (struct sp_array));
628 sp_array_size *= 2;
629 }
630
631 count = safe_read (file, buffer, sizeof buffer);
632 if (count <= 0)
633 break;
634
635 /* Process one block. */
636
637 if (count == sizeof buffer)
638
639 if (zero_block_p (buffer))
640 {
641 if (numbytes)
642 {
643 sparsearray[sparses++].numbytes = numbytes;
644 numbytes = 0;
645 }
646 }
647 else
648 {
649 if (!numbytes)
650 sparsearray[sparses].offset = offset;
651 numbytes += count;
652 }
653
654 else
655
656 /* Since count < sizeof buffer, we have the last bit of the file. */
657
658 if (!zero_block_p (buffer))
659 {
660 if (!numbytes)
661 sparsearray[sparses].offset = offset;
662 numbytes += count;
663 }
664 else
665 /* The next two lines are suggested by Andreas Degert, who says
666 they are required for trailing full blocks to be written to the
667 archive, when all zeroed. Yet, it seems to me that the case
668 does not apply. Further, at restore time, the file is not as
669 sparse as it should. So, some serious cleanup is *also* needed
670 in this area. Just one more... :-(. FIXME. */
671 if (numbytes)
672 numbytes += count;
673
674 /* Prepare for next block. */
675
676 offset += count;
677 /* FIXME: do not clear unless necessary. */
678 clear_buffer (buffer);
679 }
680
681 if (numbytes)
682 sparsearray[sparses++].numbytes = numbytes;
683 else
684 {
685 sparsearray[sparses].offset = offset - 1;
686 sparsearray[sparses++].numbytes = 1;
687 }
688
689 return close (file) == 0 && 0 <= count ? sparses : 0;
690 }
691
692 static int
693 finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
694 {
695 union block *start;
696 size_t bufsize;
697 int sparses = 0;
698 ssize_t count;
699
700 while (*sizeleft > 0)
701 {
702 start = find_next_block ();
703 memset (start->buffer, 0, BLOCKSIZE);
704 bufsize = sparsearray[sparses].numbytes;
705 if (! bufsize)
706 abort ();
707
708 if (lseek (file, sparsearray[sparses++].offset, SEEK_SET) < 0)
709 {
710 (ignore_failed_read_option ? seek_warn_details : seek_error_details)
711 (name, sparsearray[sparses - 1].offset);
712 break;
713 }
714
715 /* If the number of bytes to be written here exceeds the size of
716 the temporary buffer, do it in steps. */
717
718 while (bufsize > BLOCKSIZE)
719 {
720 count = safe_read (file, start->buffer, BLOCKSIZE);
721 if (count < 0)
722 {
723 (ignore_failed_read_option
724 ? read_warn_details
725 : read_error_details)
726 (name, fullsize - *sizeleft, bufsize);
727 return 1;
728 }
729 bufsize -= count;
730 *sizeleft -= count;
731 set_next_block_after (start);
732 start = find_next_block ();
733 memset (start->buffer, 0, BLOCKSIZE);
734 }
735
736 {
737 char buffer[BLOCKSIZE];
738
739 clear_buffer (buffer);
740 count = safe_read (file, buffer, bufsize);
741 memcpy (start->buffer, buffer, BLOCKSIZE);
742 }
743
744 if (count < 0)
745 {
746 (ignore_failed_read_option
747 ? read_warn_details
748 : read_error_details)
749 (name, fullsize - *sizeleft, bufsize);
750 return 1;
751 }
752
753 *sizeleft -= count;
754 set_next_block_after (start);
755 }
756 free (sparsearray);
757 #if 0
758 set_next_block_after (start + (count - 1) / BLOCKSIZE);
759 #endif
760 return 0;
761 }
762 \f
763 /* Main functions of this module. */
764
765 void
766 create_archive (void)
767 {
768 char *p;
769
770 open_archive (ACCESS_WRITE);
771
772 if (incremental_option)
773 {
774 size_t buffer_size = 1000;
775 char *buffer = xmalloc (buffer_size);
776 const char *q;
777
778 collect_and_sort_names ();
779
780 while (p = name_from_list (), p)
781 if (!excluded_name (p))
782 dump_file (p, -1, (dev_t) 0);
783
784 blank_name_list ();
785 while (p = name_from_list (), p)
786 if (!excluded_name (p))
787 {
788 size_t plen = strlen (p);
789 if (buffer_size <= plen)
790 {
791 while ((buffer_size *= 2) <= plen)
792 continue;
793 buffer = xrealloc (buffer, buffer_size);
794 }
795 memcpy (buffer, p, plen);
796 if (! ISSLASH (buffer[plen - 1]))
797 buffer[plen++] = '/';
798 q = gnu_list_name->dir_contents;
799 if (q)
800 while (*q)
801 {
802 size_t qlen = strlen (q);
803 if (*q == 'Y')
804 {
805 if (buffer_size < plen + qlen)
806 {
807 while ((buffer_size *=2 ) < plen + qlen)
808 continue;
809 buffer = xrealloc (buffer, buffer_size);
810 }
811 strcpy (buffer + plen, q + 1);
812 dump_file (buffer, -1, (dev_t) 0);
813 }
814 q += qlen + 1;
815 }
816 }
817 free (buffer);
818 }
819 else
820 {
821 while (p = name_next (1), p)
822 if (!excluded_name (p))
823 dump_file (p, 1, (dev_t) 0);
824 }
825
826 write_eot ();
827 close_archive ();
828
829 if (listed_incremental_option)
830 write_directory_file ();
831 }
832
833
834 /* Calculate the hash of a link. */
835 static unsigned
836 hash_link (void const *entry, unsigned n_buckets)
837 {
838 struct link const *link = entry;
839 return (uintmax_t) (link->dev ^ link->ino) % n_buckets;
840 }
841
842 /* Compare two links for equality. */
843 static bool
844 compare_links (void const *entry1, void const *entry2)
845 {
846 struct link const *link1 = entry1;
847 struct link const *link2 = entry2;
848 return ((link1->dev ^ link2->dev) | (link1->ino ^ link2->ino)) == 0;
849 }
850
851 /* Table of all non-directories that we've written so far. Any time
852 we see another, we check the table and avoid dumping the data
853 again if we've done it once already. */
854 static Hash_table *link_table;
855
856 /* Dump a single file, recursing on directories. P is the file name
857 to dump. TOP_LEVEL tells whether this is a top-level call; zero
858 means no, positive means yes, and negative means the top level
859 of an incremental dump. PARENT_DEVICE is the device of P's
860 parent directory; it is examined only if TOP_LEVEL is zero.
861
862 Set global CURRENT_STAT_INFO to stat output for this file. */
863
864 /* FIXME: One should make sure that for *every* path leading to setting
865 exit_status to failure, a clear diagnostic has been issued. */
866
867 void
868 dump_file (char *p, int top_level, dev_t parent_device)
869 {
870 union block *header;
871 char type;
872 union block *exhdr;
873 char save_typeflag;
874 time_t original_ctime;
875 struct utimbuf restore_times;
876 off_t block_ordinal = -1;
877
878 /* FIXME: `header' might be used uninitialized in this
879 function. Reported by Bruno Haible. */
880
881 if (interactive_option && !confirm ("add", p))
882 return;
883
884 if (deref_stat (dereference_option, p, &current_stat_info.stat) != 0)
885 {
886 if (ignore_failed_read_option)
887 stat_warn (p);
888 else
889 stat_error (p);
890 return;
891 }
892
893 original_ctime = current_stat_info.stat.st_ctime;
894 restore_times.actime = current_stat_info.stat.st_atime;
895 restore_times.modtime = current_stat_info.stat.st_mtime;
896
897 #ifdef S_ISHIDDEN
898 if (S_ISHIDDEN (current_stat_info.stat.st_mode))
899 {
900 char *new = (char *) alloca (strlen (p) + 2);
901 if (new)
902 {
903 strcpy (new, p);
904 strcat (new, "@");
905 p = new;
906 }
907 }
908 #endif
909
910 /* See if we want only new files, and check if this one is too old to
911 put in the archive. */
912
913 if ((0 < top_level || !incremental_option)
914 && !S_ISDIR (current_stat_info.stat.st_mode)
915 && current_stat_info.stat.st_mtime < newer_mtime_option
916 && (!after_date_option || current_stat_info.stat.st_ctime < newer_ctime_option))
917 {
918 if (0 < top_level)
919 WARN ((0, 0, _("%s: file is unchanged; not dumped"),
920 quotearg_colon (p)));
921 /* FIXME: recheck this return. */
922 return;
923 }
924
925 #if !MSDOS
926 /* See if we are trying to dump the archive. */
927
928 if (ar_dev && current_stat_info.stat.st_dev == ar_dev && current_stat_info.stat.st_ino == ar_ino)
929 {
930 WARN ((0, 0, _("%s: file is the archive; not dumped"),
931 quotearg_colon (p)));
932 return;
933 }
934 #endif
935
936 if (S_ISDIR (current_stat_info.stat.st_mode))
937 {
938 char *directory;
939 char const *entry;
940 size_t entrylen;
941 char *namebuf;
942 size_t buflen;
943 size_t len;
944 dev_t our_device = current_stat_info.stat.st_dev;
945
946 errno = 0;
947
948 directory = savedir (p);
949 if (! directory)
950 {
951 if (ignore_failed_read_option)
952 savedir_warn (p);
953 else
954 savedir_error (p);
955 return;
956 }
957
958 /* Build new prototype name. Ensure exactly one trailing slash. */
959
960 len = strlen (p);
961 buflen = len + NAME_FIELD_SIZE;
962 namebuf = xmalloc (buflen + 1);
963 memcpy (namebuf, p, len);
964 while (len >= 1 && ISSLASH (namebuf[len - 1]))
965 len--;
966 namebuf[len++] = '/';
967 namebuf[len] = '\0';
968
969 if (! is_avoided_name (namebuf))
970 {
971 /* The condition above used to be "archive_format != V7_FORMAT".
972 GNU tar was not writing directory blocks at all. Daniel Trinkle
973 writes: ``All old versions of tar I have ever seen have
974 correctly archived an empty directory. The really old ones I
975 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
976 some subtle reason for the exclusion that I don't know, but the
977 current behavior is broken.'' I do not know those subtle
978 reasons either, so until these are reported (anew?), just allow
979 directory blocks to be written even with old archives. */
980
981 block_ordinal = current_block_ordinal ();
982 current_stat_info.stat.st_size = 0; /* force 0 size on dir */
983
984 /* FIXME: If people could really read standard archives, this
985 should be:
986
987 header
988 = start_header (standard_option ? p : namebuf, &current_stat_info);
989
990 but since they'd interpret DIRTYPE blocks as regular
991 files, we'd better put the / on the name. */
992
993 header = start_header (namebuf, &current_stat_info);
994
995 if (incremental_option)
996 header->header.typeflag = GNUTYPE_DUMPDIR;
997 else /* if (standard_option) */
998 header->header.typeflag = DIRTYPE;
999
1000 /* If we're gnudumping, we aren't done yet so don't close it. */
1001
1002 if (!incremental_option)
1003 finish_header (header, block_ordinal);
1004 }
1005
1006 if (incremental_option && gnu_list_name->dir_contents)
1007 {
1008 off_t sizeleft;
1009 off_t totsize;
1010 size_t bufsize;
1011 union block *start;
1012 ssize_t count;
1013 const char *buffer, *p_buffer;
1014
1015 buffer = gnu_list_name->dir_contents; /* FOO */
1016 totsize = 0;
1017 if (buffer)
1018 for (p_buffer = buffer; *p_buffer; )
1019 {
1020 size_t size = strlen (p_buffer) + 1;
1021 totsize += size;
1022 p_buffer += size;
1023 }
1024 totsize++;
1025 OFF_TO_CHARS (totsize, header->header.size);
1026 finish_header (header, block_ordinal);
1027 p_buffer = buffer;
1028 sizeleft = totsize;
1029 while (sizeleft > 0)
1030 {
1031 if (multi_volume_option)
1032 {
1033 assign_string (&save_name, p);
1034 save_sizeleft = sizeleft;
1035 save_totsize = totsize;
1036 }
1037 start = find_next_block ();
1038 bufsize = available_space_after (start);
1039 if (sizeleft < bufsize)
1040 {
1041 bufsize = sizeleft;
1042 count = bufsize % BLOCKSIZE;
1043 if (count)
1044 memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
1045 }
1046 memcpy (start->buffer, p_buffer, bufsize);
1047 sizeleft -= bufsize;
1048 p_buffer += bufsize;
1049 set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1050 }
1051 if (multi_volume_option)
1052 assign_string (&save_name, 0);
1053 goto finish_dir;
1054 }
1055
1056 /* See if we are about to recurse into a directory, and avoid doing
1057 so if the user wants that we do not descend into directories. */
1058
1059 if (! recursion_option)
1060 goto finish_dir;
1061
1062 /* See if we are crossing from one file system to another, and
1063 avoid doing so if the user only wants to dump one file system. */
1064
1065 if (one_file_system_option && !top_level
1066 && parent_device != current_stat_info.stat.st_dev)
1067 {
1068 if (verbose_option)
1069 WARN ((0, 0,
1070 _("%s: file is on a different filesystem; not dumped"),
1071 quotearg_colon (p)));
1072 goto finish_dir;
1073 }
1074
1075 /* Now output all the files in the directory. */
1076
1077 /* FIXME: Should speed this up by cd-ing into the dir. */
1078
1079 for (entry = directory;
1080 (entrylen = strlen (entry)) != 0;
1081 entry += entrylen + 1)
1082 {
1083 if (buflen < len + entrylen)
1084 {
1085 buflen = len + entrylen;
1086 namebuf = xrealloc (namebuf, buflen + 1);
1087 }
1088 strcpy (namebuf + len, entry);
1089 if (!excluded_name (namebuf))
1090 dump_file (namebuf, 0, our_device);
1091 }
1092
1093 finish_dir:
1094
1095 free (directory);
1096 free (namebuf);
1097 if (atime_preserve_option)
1098 utime (p, &restore_times);
1099 return;
1100 }
1101 else if (is_avoided_name (p))
1102 return;
1103 else
1104 {
1105 /* Check for multiple links. */
1106
1107 if (1 < current_stat_info.stat.st_nlink && link_table)
1108 {
1109 struct link lp;
1110 struct link *dup;
1111 lp.ino = current_stat_info.stat.st_ino;
1112 lp.dev = current_stat_info.stat.st_dev;
1113
1114 if ((dup = hash_lookup (link_table, &lp)))
1115 {
1116 /* We found a link. */
1117 char const *link_name = safer_name_suffix (dup->name, 1);
1118
1119 dup->nlink--;
1120
1121 block_ordinal = current_block_ordinal ();
1122 if (NAME_FIELD_SIZE <= strlen (link_name))
1123 write_long (link_name, GNUTYPE_LONGLINK);
1124 assign_string (&current_stat_info.link_name, link_name);
1125
1126 current_stat_info.stat.st_size = 0;
1127 header = start_header (p, &current_stat_info);
1128 strncpy (header->header.linkname, link_name, NAME_FIELD_SIZE);
1129
1130 /* Force null termination. */
1131 header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
1132
1133 header->header.typeflag = LNKTYPE;
1134 finish_header (header, block_ordinal);
1135
1136 /* FIXME: Maybe remove from table after all links found? */
1137
1138 if (remove_files_option && unlink (p) != 0)
1139 unlink_error (p);
1140
1141 /* We dumped it, and we don't need to put it in the
1142 table again. */
1143 return;
1144 }
1145 }
1146
1147 /* This is not a link to a previously dumped file, so dump it. */
1148
1149 if (S_ISREG (current_stat_info.stat.st_mode)
1150 || S_ISCTG (current_stat_info.stat.st_mode))
1151 {
1152 int f; /* file descriptor */
1153 size_t bufsize;
1154 ssize_t count;
1155 off_t sizeleft;
1156 union block *start;
1157 int header_moved;
1158 char isextended = 0;
1159 int sparses = 0;
1160
1161 header_moved = 0;
1162
1163 if (sparse_option)
1164 {
1165 /* Check the size of the file against the number of blocks
1166 allocated for it, counting both data and indirect blocks.
1167 If there is a smaller number of blocks than would be
1168 necessary to accommodate a file of this size, this is safe
1169 to say that we have a sparse file: at least one of those
1170 blocks in the file is just a useless hole. For sparse
1171 files not having more hole blocks than indirect blocks, the
1172 sparseness will go undetected. */
1173
1174 /* Bruno Haible sent me these statistics for Linux. It seems
1175 that some filesystems count indirect blocks in st_blocks,
1176 while others do not seem to:
1177
1178 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1179 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1180 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1181 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1182
1183 Dick Streefland reports the previous numbers as misleading,
1184 because ext2fs use 12 direct blocks, while minix-fs uses only
1185 6 direct blocks. Dick gets:
1186
1187 ext2 size=20480 ls listed blocks=21
1188 minix size=20480 ls listed blocks=21
1189 msdos size=20480 ls listed blocks=20
1190
1191 It seems that indirect blocks *are* included in st_blocks.
1192 The minix filesystem does not account for phantom blocks in
1193 st_blocks, so `du' and `ls -s' give wrong results. So, the
1194 --sparse option would not work on a minix filesystem. */
1195
1196 if (ST_NBLOCKS (current_stat_info.stat)
1197 < (current_stat_info.stat.st_size / ST_NBLOCKSIZE
1198 + (current_stat_info.stat.st_size % ST_NBLOCKSIZE != 0)))
1199 {
1200 int counter;
1201
1202 block_ordinal = current_block_ordinal ();
1203 header = start_header (p, &current_stat_info);
1204 header->header.typeflag = GNUTYPE_SPARSE;
1205 header_moved = 1;
1206
1207 /* Call the routine that figures out the layout of the
1208 sparse file in question. SPARSES is the index of the
1209 first unused element of the "sparsearray," i.e.,
1210 the number of elements it needed to describe the file. */
1211
1212 sparses = deal_with_sparse (p, header);
1213
1214 /* See if we'll need an extended header later. */
1215
1216 if (SPARSES_IN_OLDGNU_HEADER < sparses)
1217 header->oldgnu_header.isextended = 1;
1218
1219 /* We store the "real" file size so we can show that in
1220 case someone wants to list the archive, i.e., tar tvf
1221 <file>. It might be kind of disconcerting if the
1222 shrunken file size was the one that showed up. */
1223
1224 OFF_TO_CHARS (current_stat_info.stat.st_size,
1225 header->oldgnu_header.realsize);
1226
1227 /* This will be the new "size" of the file, i.e., the size
1228 of the file minus the blocks of holes that we're
1229 skipping over. */
1230
1231 current_stat_info.stat.st_size = find_new_file_size (sparses);
1232 OFF_TO_CHARS (current_stat_info.stat.st_size, header->header.size);
1233
1234 for (counter = 0;
1235 counter < sparses && counter < SPARSES_IN_OLDGNU_HEADER;
1236 counter++)
1237 {
1238 OFF_TO_CHARS (sparsearray[counter].offset,
1239 header->oldgnu_header.sp[counter].offset);
1240 SIZE_TO_CHARS (sparsearray[counter].numbytes,
1241 header->oldgnu_header.sp[counter].numbytes);
1242 }
1243 }
1244 }
1245
1246 sizeleft = current_stat_info.stat.st_size;
1247
1248 /* Don't bother opening empty, world readable files. Also do not open
1249 files when archive is meant for /dev/null. */
1250
1251 if (dev_null_output
1252 || (sizeleft == 0
1253 && MODE_R == (MODE_R & current_stat_info.stat.st_mode)))
1254 f = -1;
1255 else
1256 {
1257 f = open (p, O_RDONLY | O_BINARY);
1258 if (f < 0)
1259 {
1260 if (! top_level && errno == ENOENT)
1261 WARN ((0, 0, _("%s: File removed before we read it"),
1262 quotearg_colon (p)));
1263 else
1264 (ignore_failed_read_option ? open_warn : open_error) (p);
1265 return;
1266 }
1267 }
1268
1269 /* If the file is sparse, we've already taken care of this. */
1270
1271 if (!header_moved)
1272 {
1273 block_ordinal = current_block_ordinal ();
1274 header = start_header (p, &current_stat_info);
1275 }
1276
1277 /* Mark contiguous files, if we support them. */
1278
1279 if (archive_format != V7_FORMAT && S_ISCTG (current_stat_info.stat.st_mode))
1280 header->header.typeflag = CONTTYPE;
1281
1282 isextended = header->oldgnu_header.isextended;
1283 save_typeflag = header->header.typeflag;
1284 finish_header (header, block_ordinal);
1285 if (isextended)
1286 {
1287 int sparses_emitted = SPARSES_IN_OLDGNU_HEADER;
1288
1289 for (;;)
1290 {
1291 int i;
1292 exhdr = find_next_block ();
1293 memset (exhdr->buffer, 0, BLOCKSIZE);
1294 for (i = 0;
1295 (i < SPARSES_IN_SPARSE_HEADER
1296 && sparses_emitted + i < sparses);
1297 i++)
1298 {
1299 SIZE_TO_CHARS (sparsearray[sparses_emitted + i].numbytes,
1300 exhdr->sparse_header.sp[i].numbytes);
1301 OFF_TO_CHARS (sparsearray[sparses_emitted + i].offset,
1302 exhdr->sparse_header.sp[i].offset);
1303 }
1304 set_next_block_after (exhdr);
1305 sparses_emitted += i;
1306 if (sparses == sparses_emitted)
1307 break;
1308 exhdr->sparse_header.isextended = 1;
1309 }
1310 }
1311 if (save_typeflag == GNUTYPE_SPARSE)
1312 {
1313 if (f < 0
1314 || finish_sparse_file (f, &sizeleft,
1315 current_stat_info.stat.st_size, p))
1316 goto padit;
1317 }
1318 else
1319 while (sizeleft > 0)
1320 {
1321 if (multi_volume_option)
1322 {
1323 assign_string (&save_name, p);
1324 save_sizeleft = sizeleft;
1325 save_totsize = current_stat_info.stat.st_size;
1326 }
1327 start = find_next_block ();
1328
1329 bufsize = available_space_after (start);
1330
1331 if (sizeleft < bufsize)
1332 {
1333 /* Last read -- zero out area beyond. */
1334
1335 bufsize = sizeleft;
1336 count = bufsize % BLOCKSIZE;
1337 if (count)
1338 memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
1339 }
1340 if (f < 0)
1341 count = bufsize;
1342 else
1343 count = safe_read (f, start->buffer, bufsize);
1344 if (count < 0)
1345 {
1346 (ignore_failed_read_option
1347 ? read_warn_details
1348 : read_error_details)
1349 (p, current_stat_info.stat.st_size - sizeleft, bufsize);
1350 goto padit;
1351 }
1352 sizeleft -= count;
1353
1354 /* This is nonportable (the type of set_next_block_after's arg). */
1355
1356 set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1357
1358
1359 if (count != bufsize)
1360 {
1361 char buf[UINTMAX_STRSIZE_BOUND];
1362 memset (start->buffer + count, 0, bufsize - count);
1363 WARN ((0, 0,
1364 _("%s: File shrank by %s bytes; padding with zeros"),
1365 quotearg_colon (p),
1366 STRINGIFY_BIGINT (sizeleft, buf)));
1367 if (! ignore_failed_read_option)
1368 exit_status = TAREXIT_FAILURE;
1369 goto padit; /* short read */
1370 }
1371 }
1372
1373 if (multi_volume_option)
1374 assign_string (&save_name, 0);
1375
1376 if (f >= 0)
1377 {
1378 struct stat final_stat;
1379 if (fstat (f, &final_stat) != 0)
1380 {
1381 if (ignore_failed_read_option)
1382 stat_warn (p);
1383 else
1384 stat_error (p);
1385 }
1386 else if (final_stat.st_ctime != original_ctime)
1387 {
1388 char const *qp = quotearg_colon (p);
1389 WARN ((0, 0, _("%s: file changed as we read it"), qp));
1390 }
1391 if (close (f) != 0)
1392 {
1393 if (ignore_failed_read_option)
1394 close_warn (p);
1395 else
1396 close_error (p);
1397 }
1398 if (atime_preserve_option)
1399 utime (p, &restore_times);
1400 }
1401 if (remove_files_option)
1402 {
1403 if (unlink (p) == -1)
1404 unlink_error (p);
1405 }
1406 goto file_was_dumped;
1407
1408 /* File shrunk or gave error, pad out tape to match the size we
1409 specified in the header. */
1410
1411 padit:
1412 while (sizeleft > 0)
1413 {
1414 save_sizeleft = sizeleft;
1415 start = find_next_block ();
1416 memset (start->buffer, 0, BLOCKSIZE);
1417 set_next_block_after (start);
1418 sizeleft -= BLOCKSIZE;
1419 }
1420 if (multi_volume_option)
1421 assign_string (&save_name, 0);
1422 if (f >= 0)
1423 {
1424 close (f);
1425 if (atime_preserve_option)
1426 utime (p, &restore_times);
1427 }
1428 goto file_was_dumped;
1429 }
1430 #ifdef HAVE_READLINK
1431 else if (S_ISLNK (current_stat_info.stat.st_mode))
1432 {
1433 char *buffer;
1434 int size;
1435 size_t linklen = current_stat_info.stat.st_size;
1436 if (linklen != current_stat_info.stat.st_size || linklen + 1 == 0)
1437 xalloc_die ();
1438 buffer = (char *) alloca (linklen + 1);
1439 size = readlink (p, buffer, linklen + 1);
1440 if (size < 0)
1441 {
1442 if (ignore_failed_read_option)
1443 readlink_warn (p);
1444 else
1445 readlink_error (p);
1446 return;
1447 }
1448 buffer[size] = '\0';
1449 if (size >= NAME_FIELD_SIZE)
1450 write_long (buffer, GNUTYPE_LONGLINK);
1451 assign_string (&current_stat_info.link_name, buffer);
1452
1453 block_ordinal = current_block_ordinal ();
1454 current_stat_info.stat.st_size = 0; /* force 0 size on symlink */
1455 header = start_header (p, &current_stat_info);
1456 strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
1457 header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
1458 header->header.typeflag = SYMTYPE;
1459 finish_header (header, block_ordinal);
1460 /* nothing more to do to it */
1461
1462 if (remove_files_option)
1463 {
1464 if (unlink (p) == -1)
1465 unlink_error (p);
1466 }
1467 goto file_was_dumped;
1468 }
1469 #endif
1470 else if (S_ISCHR (current_stat_info.stat.st_mode))
1471 type = CHRTYPE;
1472 else if (S_ISBLK (current_stat_info.stat.st_mode))
1473 type = BLKTYPE;
1474 else if (S_ISFIFO (current_stat_info.stat.st_mode))
1475 type = FIFOTYPE;
1476 else if (S_ISSOCK (current_stat_info.stat.st_mode))
1477 {
1478 WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p)));
1479 return;
1480 }
1481 else if (S_ISDOOR (current_stat_info.stat.st_mode))
1482 {
1483 WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p)));
1484 return;
1485 }
1486 else
1487 goto unknown;
1488 }
1489
1490 if (archive_format == V7_FORMAT)
1491 goto unknown;
1492
1493 block_ordinal = current_block_ordinal ();
1494 current_stat_info.stat.st_size = 0; /* force 0 size */
1495 header = start_header (p, &current_stat_info);
1496 header->header.typeflag = type;
1497
1498 if (type != FIFOTYPE)
1499 {
1500 MAJOR_TO_CHARS (major (current_stat_info.stat.st_rdev), header->header.devmajor);
1501 MINOR_TO_CHARS (minor (current_stat_info.stat.st_rdev), header->header.devminor);
1502 }
1503
1504 finish_header (header, block_ordinal);
1505 if (remove_files_option)
1506 {
1507 if (unlink (p) == -1)
1508 unlink_error (p);
1509 }
1510 goto file_was_dumped;
1511
1512 unknown:
1513 WARN ((0, 0, _("%s: Unknown file type; file ignored"),
1514 quotearg_colon (p)));
1515 if (! ignore_failed_read_option)
1516 exit_status = TAREXIT_FAILURE;
1517 return;
1518
1519 file_was_dumped:
1520 if (1 < current_stat_info.stat.st_nlink)
1521 {
1522 struct link *dup;
1523 struct link *lp = xmalloc (offsetof (struct link, name)
1524 + strlen (p) + 1);
1525 lp->ino = current_stat_info.stat.st_ino;
1526 lp->dev = current_stat_info.stat.st_dev;
1527 lp->nlink = current_stat_info.stat.st_nlink;
1528 strcpy (lp->name, p);
1529
1530 if (! ((link_table
1531 || (link_table = hash_initialize (0, 0, hash_link,
1532 compare_links, 0)))
1533 && (dup = hash_insert (link_table, lp))))
1534 xalloc_die ();
1535
1536 if (dup != lp)
1537 abort ();
1538 lp->nlink--;
1539 }
1540
1541 }
1542
1543 /* For each dumped file, check if all its links were dumped. Emit
1544 warnings if it is not so. */
1545 void
1546 check_links ()
1547 {
1548 struct link *lp;
1549
1550 if (!link_table)
1551 return;
1552
1553 for (lp = hash_get_first (link_table); lp;
1554 lp = hash_get_next (link_table, lp))
1555 {
1556 if (lp->nlink)
1557 {
1558 WARN ((0, 0, _("Missing links to '%s'.\n"), lp->name));
1559 }
1560 }
1561 }
This page took 0.100664 seconds and 4 git commands to generate.