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