]> Dogcows Code - chaz/tar/blob - src/create.c
(dump_file): Undo previous change about diagnosing unexpectely missing
[chaz/tar] / src / create.c
1 /* Create a tar archive.
2 Copyright 1985, 92, 93, 94, 96, 97, 1999 Free Software Foundation, Inc.
3 Written by John Gilmore, on 1985-08-25.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any later
8 version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 #include "system.h"
20
21 #if !MSDOS
22 # include <pwd.h>
23 # include <grp.h>
24 #endif
25
26 #if HAVE_UTIME_H
27 # include <utime.h>
28 #else
29 struct utimbuf
30 {
31 long actime;
32 long modtime;
33 };
34 #endif
35
36 #include "common.h"
37
38 #ifndef MSDOS
39 extern dev_t ar_dev;
40 extern ino_t ar_ino;
41 #endif
42
43 extern struct name *gnu_list_name;
44
45 /* This module is the only one that cares about `struct link's. */
46
47 struct link
48 {
49 struct link *next;
50 dev_t dev;
51 ino_t ino;
52 char name[1];
53 };
54
55 static struct link *linklist; /* points to first link in list */
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) PARAMS ((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 /*-----------------------------------------------------------------------.
337 | Just zeroes out the buffer so we don't confuse ourselves with leftover |
338 | data. |
339 `-----------------------------------------------------------------------*/
340
341 static void
342 clear_buffer (char *buffer)
343 {
344 memset (buffer, 0, BLOCKSIZE);
345 }
346
347 /*-------------------------------------------------------------------------.
348 | Write the EOT block(s). We zero at least two blocks, through |
349 | the end of the record. Old tar, as previous versions of GNU tar, writes |
350 | garbage after two zeroed blocks. |
351 `-------------------------------------------------------------------------*/
352
353 void
354 write_eot (void)
355 {
356 union block *pointer = find_next_block ();
357 memset (pointer->buffer, 0, BLOCKSIZE);
358 set_next_block_after (pointer);
359 pointer = find_next_block ();
360 memset (pointer->buffer, 0, available_space_after (pointer));
361 set_next_block_after (pointer);
362 }
363
364 /*-----------------------------------------------------.
365 | Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. |
366 `-----------------------------------------------------*/
367
368 /* FIXME: Cross recursion between start_header and write_long! */
369
370 static union block *start_header PARAMS ((const char *, struct stat *));
371
372 static void
373 write_long (const char *p, char type)
374 {
375 size_t size = strlen (p) + 1;
376 size_t bufsize;
377 union block *header;
378 struct stat foo;
379
380 memset (&foo, 0, sizeof foo);
381 foo.st_size = size;
382
383 header = start_header ("././@LongLink", &foo);
384 header->header.typeflag = type;
385 finish_header (header);
386
387 header = find_next_block ();
388
389 bufsize = available_space_after (header);
390
391 while (bufsize < size)
392 {
393 memcpy (header->buffer, p, bufsize);
394 p += bufsize;
395 size -= bufsize;
396 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
397 header = find_next_block ();
398 bufsize = available_space_after (header);
399 }
400 memcpy (header->buffer, p, size);
401 memset (header->buffer + size, 0, bufsize - size);
402 set_next_block_after (header + (size - 1) / BLOCKSIZE);
403 }
404 \f
405 /* Header handling. */
406
407 /* Make a header block for the file whose stat info is st,
408 and return its address. */
409
410 static union block *
411 start_header (const char *name, struct stat *st)
412 {
413 union block *header;
414
415 if (!absolute_names_option)
416 {
417 size_t prefix_len = FILESYSTEM_PREFIX_LEN (name);
418
419 if (prefix_len)
420 {
421 static int warned_once;
422 if (!warned_once)
423 {
424 warned_once = 1;
425 WARN ((0, 0, _("Removing `%.*s' prefix from archive names"),
426 (int) prefix_len, name));
427 }
428 name += prefix_len;
429 }
430
431 while (*name == '/')
432 {
433 static int warned_once;
434 if (!warned_once)
435 {
436 warned_once = 1;
437 WARN ((0, 0, _("Removing leading `/' from archive names")));
438 }
439 name++;
440 }
441 }
442
443 if (sizeof header->header.name <= strlen (name))
444 write_long (name, GNUTYPE_LONGNAME);
445 header = find_next_block ();
446 memset (header->buffer, 0, sizeof (union block));
447
448 assign_string (&current_file_name, name);
449
450 strncpy (header->header.name, name, NAME_FIELD_SIZE);
451 header->header.name[NAME_FIELD_SIZE - 1] = '\0';
452
453 /* Override some stat fields, if requested to do so. */
454
455 if (owner_option != (uid_t) -1)
456 st->st_uid = owner_option;
457 if (group_option != (gid_t) -1)
458 st->st_gid = group_option;
459 if (mode_option)
460 st->st_mode = ((st->st_mode & ~MODE_ALL)
461 | mode_adjust (st->st_mode, mode_option));
462
463 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
464 for a few tars and came up with the following interoperability
465 matrix:
466
467 WRITER
468 1 2 3 4 5 6 7 8 9 READER
469 . . . . . . . . . 1 = SunOS 4.2 tar
470 # . . # # . . # # 2 = NEC SVR4.0.2 tar
471 . . . # # . . # . 3 = Solaris 2.1 tar
472 . . . . . . . . . 4 = GNU tar 1.11.1
473 . . . . . . . . . 5 = HP-UX 8.07 tar
474 . . . . . . . . . 6 = Ultrix 4.1
475 . . . . . . . . . 7 = AIX 3.2
476 . . . . . . . . . 8 = Hitachi HI-UX 1.03
477 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
478
479 . = works
480 # = ``impossible file type''
481
482 The following mask for old archive removes the `#'s in column 4
483 above, thus making GNU tar both a universal donor and a universal
484 acceptor for Paul's test. */
485
486 if (archive_format == V7_FORMAT)
487 MODE_TO_CHARS (st->st_mode & MODE_ALL, header->header.mode);
488 else
489 MODE_TO_CHARS (st->st_mode, header->header.mode);
490
491 UID_TO_CHARS (st->st_uid, header->header.uid);
492 GID_TO_CHARS (st->st_gid, header->header.gid);
493 OFF_TO_CHARS (st->st_size, header->header.size);
494 TIME_TO_CHARS (st->st_mtime, header->header.mtime);
495
496 if (incremental_option)
497 if (archive_format == OLDGNU_FORMAT)
498 {
499 TIME_TO_CHARS (st->st_atime, header->oldgnu_header.atime);
500 TIME_TO_CHARS (st->st_ctime, header->oldgnu_header.ctime);
501 }
502
503 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
504
505 switch (archive_format)
506 {
507 case V7_FORMAT:
508 break;
509
510 case OLDGNU_FORMAT:
511 /* Overwrite header->header.magic and header.version in one blow. */
512 strcpy (header->header.magic, OLDGNU_MAGIC);
513 break;
514
515 case POSIX_FORMAT:
516 case GNU_FORMAT:
517 strncpy (header->header.magic, TMAGIC, TMAGLEN);
518 strncpy (header->header.version, TVERSION, TVERSLEN);
519 break;
520
521 default:
522 abort ();
523 }
524
525 if (archive_format == V7_FORMAT || numeric_owner_option)
526 {
527 /* header->header.[ug]name are left as the empty string. */
528 }
529 else
530 {
531 uid_to_uname (st->st_uid, header->header.uname);
532 gid_to_gname (st->st_gid, header->header.gname);
533 }
534
535 return header;
536 }
537
538 /*-------------------------------------------------------------------------.
539 | Finish off a filled-in header block and write it out. We also print the |
540 | file name and/or full info if verbose is on. |
541 `-------------------------------------------------------------------------*/
542
543 void
544 finish_header (union block *header)
545 {
546 size_t i;
547 int sum;
548 char *p;
549
550 memcpy (header->header.chksum, CHKBLANKS, sizeof header->header.chksum);
551
552 sum = 0;
553 p = header->buffer;
554 for (i = sizeof *header; i-- != 0; )
555 /* We can't use unsigned char here because of old compilers, e.g. V7. */
556 sum += 0xFF & *p++;
557
558 /* Fill in the checksum field. It's formatted differently from the
559 other fields: it has [6] digits, a null, then a space -- rather than
560 digits, then a null. We use to_chars.
561 The final space is already there, from
562 checksumming, and to_chars doesn't modify it.
563
564 This is a fast way to do:
565
566 sprintf(header->header.chksum, "%6o", sum); */
567
568 uintmax_to_chars ((uintmax_t) sum, header->header.chksum, 7);
569
570 if (verbose_option
571 && header->header.typeflag != GNUTYPE_LONGLINK
572 && header->header.typeflag != GNUTYPE_LONGNAME)
573 {
574 /* These globals are parameters to print_header, sigh. */
575
576 current_header = header;
577 /* current_stat is already set up. */
578 current_format = archive_format;
579 print_header ();
580 }
581
582 set_next_block_after (header);
583 }
584 \f
585 /* Sparse file processing. */
586
587 /*-------------------------------------------------------------------------.
588 | Takes a blockful of data and basically cruises through it to see if it's |
589 | made *entirely* of zeros, returning a 0 the instant it finds something |
590 | that is a nonzero, i.e., useful data. |
591 `-------------------------------------------------------------------------*/
592
593 static int
594 zero_block_p (char *buffer)
595 {
596 int counter;
597
598 for (counter = 0; counter < BLOCKSIZE; counter++)
599 if (buffer[counter] != '\0')
600 return 0;
601 return 1;
602 }
603
604 /*---.
605 | ? |
606 `---*/
607
608 static void
609 init_sparsearray (void)
610 {
611 int counter;
612
613 sp_array_size = 10;
614
615 /* Make room for our scratch space -- initially is 10 elts long. */
616
617 sparsearray = xmalloc (sp_array_size * sizeof (struct sp_array));
618 for (counter = 0; counter < sp_array_size; counter++)
619 {
620 sparsearray[counter].offset = 0;
621 sparsearray[counter].numbytes = 0;
622 }
623 }
624
625 /*---.
626 | ? |
627 `---*/
628
629 static void
630 find_new_file_size (off_t *filesize, int highest_index)
631 {
632 int counter;
633
634 *filesize = 0;
635 for (counter = 0;
636 sparsearray[counter].numbytes && counter <= highest_index;
637 counter++)
638 *filesize += sparsearray[counter].numbytes;
639 }
640
641 /*-----------------------------------------------------------------------.
642 | Make one pass over the file NAME, studying where any non-zero data is, |
643 | that is, how far into the file each instance of data is, and how many |
644 | bytes are there. Save this information in the sparsearray, which will |
645 | later be translated into header information. |
646 `-----------------------------------------------------------------------*/
647
648 /* There is little point in trimming small amounts of null data at the head
649 and tail of blocks, only avoid dumping full null blocks. */
650
651 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
652 too kludgey for my taste... */
653
654 static int
655 deal_with_sparse (char *name, union block *header)
656 {
657 size_t numbytes = 0;
658 off_t offset = 0;
659 int file;
660 int sparse_index = 0;
661 ssize_t count;
662 char buffer[BLOCKSIZE];
663
664 if (archive_format == OLDGNU_FORMAT)
665 header->oldgnu_header.isextended = 0;
666
667 if (file = open (name, O_RDONLY), file < 0)
668 /* This problem will be caught later on, so just return. */
669 return 0;
670
671 init_sparsearray ();
672 clear_buffer (buffer);
673
674 while (count = safe_read (file, buffer, sizeof buffer), count != 0)
675 {
676 /* Realloc the scratch area as necessary. FIXME: should reallocate
677 only at beginning of a new instance of non-zero data. */
678
679 if (sparse_index > sp_array_size - 1)
680 {
681 sparsearray =
682 xrealloc (sparsearray,
683 2 * sp_array_size * sizeof (struct sp_array));
684 sp_array_size *= 2;
685 }
686
687 /* Process one block. */
688
689 if (count == sizeof buffer)
690
691 if (zero_block_p (buffer))
692 {
693 if (numbytes)
694 {
695 sparsearray[sparse_index++].numbytes = numbytes;
696 numbytes = 0;
697 }
698 }
699 else
700 {
701 if (!numbytes)
702 sparsearray[sparse_index].offset = offset;
703 numbytes += count;
704 }
705
706 else
707
708 /* Since count < sizeof buffer, we have the last bit of the file. */
709
710 if (!zero_block_p (buffer))
711 {
712 if (!numbytes)
713 sparsearray[sparse_index].offset = offset;
714 numbytes += count;
715 }
716 else
717 /* The next two lines are suggested by Andreas Degert, who says
718 they are required for trailing full blocks to be written to the
719 archive, when all zeroed. Yet, it seems to me that the case
720 does not apply. Further, at restore time, the file is not as
721 sparse as it should. So, some serious cleanup is *also* needed
722 in this area. Just one more... :-(. FIXME. */
723 if (numbytes)
724 numbytes += count;
725
726 /* Prepare for next block. */
727
728 offset += count;
729 /* FIXME: do not clear unless necessary. */
730 clear_buffer (buffer);
731 }
732
733 if (numbytes)
734 sparsearray[sparse_index++].numbytes = numbytes;
735 else
736 {
737 sparsearray[sparse_index].offset = offset - 1;
738 sparsearray[sparse_index++].numbytes = 1;
739 }
740
741 close (file);
742 return sparse_index - 1;
743 }
744
745 /*---.
746 | ? |
747 `---*/
748
749 static int
750 finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
751 {
752 union block *start;
753 size_t bufsize;
754 int sparse_index = 0;
755 ssize_t count;
756
757 while (*sizeleft > 0)
758 {
759 start = find_next_block ();
760 memset (start->buffer, 0, BLOCKSIZE);
761 bufsize = sparsearray[sparse_index].numbytes;
762 if (!bufsize)
763 {
764 /* We blew it, maybe. */
765 char buf1[UINTMAX_STRSIZE_BOUND];
766 char buf2[UINTMAX_STRSIZE_BOUND];
767
768 ERROR ((0, 0, _("Wrote %s of %s bytes to file %s"),
769 STRINGIFY_BIGINT (fullsize - *sizeleft, buf1),
770 STRINGIFY_BIGINT (fullsize, buf2),
771 name));
772 break;
773 }
774
775 if (lseek (file, sparsearray[sparse_index++].offset, SEEK_SET) < 0)
776 {
777 char buf[UINTMAX_STRSIZE_BOUND];
778 ERROR ((0, errno, _("lseek error at byte %s in file %s"),
779 STRINGIFY_BIGINT (sparsearray[sparse_index - 1].offset, buf),
780 name));
781 break;
782 }
783
784 /* If the number of bytes to be written here exceeds the size of
785 the temporary buffer, do it in steps. */
786
787 while (bufsize > BLOCKSIZE)
788 {
789 #if 0
790 if (amount_read)
791 {
792 count = safe_read (file, start->buffer + amount_read,
793 BLOCKSIZE - amount_read);
794 bufsize -= BLOCKSIZE - amount_read;
795 amount_read = 0;
796 set_next_block_after (start);
797 start = find_next_block ();
798 memset (start->buffer, 0, BLOCKSIZE);
799 }
800 #endif
801 /* Store the data. */
802
803 count = safe_read (file, start->buffer, BLOCKSIZE);
804 if (count < 0)
805 {
806 char buf[UINTMAX_STRSIZE_BOUND];
807 ERROR ((0, errno,
808 _("Read error at byte %s, reading %lu bytes, in file %s"),
809 STRINGIFY_BIGINT (fullsize - *sizeleft, buf),
810 (unsigned long) bufsize, name));
811 return 1;
812 }
813 bufsize -= count;
814 *sizeleft -= count;
815 set_next_block_after (start);
816 start = find_next_block ();
817 memset (start->buffer, 0, BLOCKSIZE);
818 }
819
820 {
821 char buffer[BLOCKSIZE];
822
823 clear_buffer (buffer);
824 count = safe_read (file, buffer, bufsize);
825 memcpy (start->buffer, buffer, BLOCKSIZE);
826 }
827
828 if (count < 0)
829 {
830 char buf[UINTMAX_STRSIZE_BOUND];
831
832 ERROR ((0, errno,
833 _("Read error at byte %s, reading %lu bytes, in file %s"),
834 STRINGIFY_BIGINT (fullsize - *sizeleft, buf),
835 (unsigned long) bufsize, name));
836 return 1;
837 }
838 #if 0
839 if (amount_read >= BLOCKSIZE)
840 {
841 amount_read = 0;
842 set_next_block_after (start + (count - 1) / BLOCKSIZE);
843 if (count != bufsize)
844 {
845 ERROR ((0, 0,
846 _("File %s shrunk, padding with zeros"),
847 name));
848 return 1;
849 }
850 start = find_next_block ();
851 }
852 else
853 amount_read += bufsize;
854 #endif
855 *sizeleft -= count;
856 set_next_block_after (start);
857
858 }
859 free (sparsearray);
860 #if 0
861 set_next_block_after (start + (count - 1) / BLOCKSIZE);
862 #endif
863 return 0;
864 }
865 \f
866 /* Main functions of this module. */
867
868 /*---.
869 | ? |
870 `---*/
871
872 void
873 create_archive (void)
874 {
875 char *p;
876
877 open_archive (ACCESS_WRITE);
878
879 if (incremental_option)
880 {
881 char *buffer = xmalloc (PATH_MAX);
882 const char *q;
883 char *bufp;
884
885 collect_and_sort_names ();
886
887 while (p = name_from_list (), p)
888 if (!excluded_name (p))
889 dump_file (p, -1, (dev_t) 0);
890
891 blank_name_list ();
892 while (p = name_from_list (), p)
893 if (!excluded_name (p))
894 {
895 strcpy (buffer, p);
896 if (p[strlen (p) - 1] != '/')
897 strcat (buffer, "/");
898 bufp = buffer + strlen (buffer);
899 q = gnu_list_name->dir_contents;
900 if (q)
901 for (; *q; q += strlen (q) + 1)
902 if (*q == 'Y')
903 {
904 strcpy (bufp, q + 1);
905 dump_file (buffer, -1, (dev_t) 0);
906 }
907 }
908 free (buffer);
909 }
910 else
911 {
912 while (p = name_next (1), p)
913 if (!excluded_name (p))
914 dump_file (p, 1, (dev_t) 0);
915 }
916
917 write_eot ();
918 close_archive ();
919
920 if (listed_incremental_option)
921 write_directory_file ();
922 }
923
924 /* Dump a single file, recursing on directories. P is the file name
925 to dump. TOP_LEVEL tells whether this is a top-level call; zero
926 means no, positive means yes, and negative means an incremental
927 dump. PARENT_DEVICE is the device of P's
928 parent directory; it is examined only if TOP_LEVEL is zero.
929
930 Set global CURRENT_STAT to stat output for this file. */
931
932 /* FIXME: One should make sure that for *every* path leading to setting
933 exit_status to failure, a clear diagnostic has been issued. */
934
935 void
936 dump_file (char *p, int top_level, dev_t parent_device)
937 {
938 union block *header;
939 char type;
940 union block *exhdr;
941 char save_typeflag;
942 time_t original_ctime;
943 struct utimbuf restore_times;
944
945 /* FIXME: `header' and `upperbound' might be used uninitialized in this
946 function. Reported by Bruno Haible. */
947
948 if (interactive_option && !confirm ("add", p))
949 return;
950
951 if (deref_stat (dereference_option, p, &current_stat) != 0)
952 {
953 WARN ((0, errno, _("Cannot add file %s"), p));
954 if (!ignore_failed_read_option)
955 exit_status = TAREXIT_FAILURE;
956 return;
957 }
958
959 original_ctime = current_stat.st_ctime;
960 restore_times.actime = current_stat.st_atime;
961 restore_times.modtime = current_stat.st_mtime;
962
963 #ifdef S_ISHIDDEN
964 if (S_ISHIDDEN (current_stat.st_mode))
965 {
966 char *new = (char *) alloca (strlen (p) + 2);
967 if (new)
968 {
969 strcpy (new, p);
970 strcat (new, "@");
971 p = new;
972 }
973 }
974 #endif
975
976 /* See if we want only new files, and check if this one is too old to
977 put in the archive. */
978
979 if ((0 < top_level || !incremental_option)
980 && !S_ISDIR (current_stat.st_mode)
981 && current_stat.st_mtime < newer_mtime_option
982 && (!after_date_option || current_stat.st_ctime < newer_ctime_option))
983 {
984 if (0 < top_level)
985 WARN ((0, 0, _("%s: is unchanged; not dumped"), p));
986 /* FIXME: recheck this return. */
987 return;
988 }
989
990 #if !MSDOS
991 /* See if we are trying to dump the archive. */
992
993 if (ar_dev && current_stat.st_dev == ar_dev && current_stat.st_ino == ar_ino)
994 {
995 WARN ((0, 0, _("%s is the archive; not dumped"), p));
996 return;
997 }
998 #endif
999
1000 if (S_ISDIR (current_stat.st_mode))
1001 {
1002 DIR *directory;
1003 struct dirent *entry;
1004 char *namebuf;
1005 size_t buflen;
1006 size_t len;
1007 dev_t our_device = current_stat.st_dev;
1008
1009 /* If this tar program is installed suid root, like for Amanda, the
1010 access might look like denied, while it is not really.
1011
1012 FIXME: I have the feeling this test is done too early. Couldn't it
1013 just be bundled in later actions? I guess that the proper support
1014 of --ignore-failed-read is the key of the current writing. */
1015
1016 if (access (p, R_OK) == -1 && geteuid () != 0)
1017 {
1018 WARN ((0, errno, _("Cannot add directory %s"), p));
1019 if (!ignore_failed_read_option)
1020 exit_status = TAREXIT_FAILURE;
1021 return;
1022 }
1023
1024 /* Build new prototype name. Ensure exactly one trailing slash. */
1025
1026 len = strlen (p);
1027 buflen = len + NAME_FIELD_SIZE;
1028 namebuf = xmalloc (buflen + 1);
1029 strncpy (namebuf, p, buflen);
1030 while (len >= 1 && namebuf[len - 1] == '/')
1031 len--;
1032 namebuf[len++] = '/';
1033 namebuf[len] = '\0';
1034
1035 if (! is_avoided_name (namebuf))
1036 {
1037 /* The condition above used to be "archive_format != V7_FORMAT".
1038 GNU tar was not writing directory blocks at all. Daniel Trinkle
1039 writes: ``All old versions of tar I have ever seen have
1040 correctly archived an empty directory. The really old ones I
1041 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1042 some subtle reason for the exclusion that I don't know, but the
1043 current behavior is broken.'' I do not know those subtle
1044 reasons either, so until these are reported (anew?), just allow
1045 directory blocks to be written even with old archives. */
1046
1047 current_stat.st_size = 0; /* force 0 size on dir */
1048
1049 /* FIXME: If people could really read standard archives, this
1050 should be:
1051
1052 header
1053 = start_header (standard_option ? p : namebuf, &current_stat);
1054
1055 but since they'd interpret DIRTYPE blocks as regular
1056 files, we'd better put the / on the name. */
1057
1058 header = start_header (namebuf, &current_stat);
1059
1060 if (incremental_option)
1061 header->header.typeflag = GNUTYPE_DUMPDIR;
1062 else /* if (standard_option) */
1063 header->header.typeflag = DIRTYPE;
1064
1065 /* If we're gnudumping, we aren't done yet so don't close it. */
1066
1067 if (!incremental_option)
1068 finish_header (header); /* done with directory header */
1069 }
1070
1071 if (incremental_option && gnu_list_name->dir_contents)
1072 {
1073 off_t sizeleft;
1074 off_t totsize;
1075 size_t bufsize;
1076 union block *start;
1077 ssize_t count;
1078 const char *buffer, *p_buffer;
1079
1080 buffer = gnu_list_name->dir_contents; /* FOO */
1081 totsize = 0;
1082 for (p_buffer = buffer; p_buffer && *p_buffer;)
1083 {
1084 size_t tmp;
1085
1086 tmp = strlen (p_buffer) + 1;
1087 totsize += tmp;
1088 p_buffer += tmp;
1089 }
1090 totsize++;
1091 OFF_TO_CHARS (totsize, header->header.size);
1092 finish_header (header);
1093 p_buffer = buffer;
1094 sizeleft = totsize;
1095 while (sizeleft > 0)
1096 {
1097 if (multi_volume_option)
1098 {
1099 assign_string (&save_name, p);
1100 save_sizeleft = sizeleft;
1101 save_totsize = totsize;
1102 }
1103 start = find_next_block ();
1104 bufsize = available_space_after (start);
1105 if (sizeleft < bufsize)
1106 {
1107 bufsize = sizeleft;
1108 count = bufsize % BLOCKSIZE;
1109 if (count)
1110 memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
1111 }
1112 memcpy (start->buffer, p_buffer, bufsize);
1113 sizeleft -= bufsize;
1114 p_buffer += bufsize;
1115 set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1116 }
1117 if (multi_volume_option)
1118 assign_string (&save_name, 0);
1119 if (atime_preserve_option)
1120 utime (p, &restore_times);
1121 return;
1122 }
1123
1124 /* See if we are about to recurse into a directory, and avoid doing
1125 so if the user wants that we do not descend into directories. */
1126
1127 if (no_recurse_option)
1128 return;
1129
1130 /* See if we are crossing from one file system to another, and
1131 avoid doing so if the user only wants to dump one file system. */
1132
1133 if (one_file_system_option && !top_level
1134 && parent_device != current_stat.st_dev)
1135 {
1136 if (verbose_option)
1137 WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p));
1138 return;
1139 }
1140
1141 /* Now output all the files in the directory. */
1142
1143 errno = 0;
1144
1145 directory = opendir (p);
1146 if (!directory)
1147 {
1148 ERROR ((0, errno, _("Cannot open directory %s"), p));
1149 return;
1150 }
1151
1152 /* FIXME: Should speed this up by cd-ing into the dir. */
1153
1154 while (entry = readdir (directory), entry)
1155 {
1156 /* Skip `.', `..', and excluded file names. */
1157
1158 if (is_dot_or_dotdot (entry->d_name))
1159 continue;
1160
1161 if ((int) NAMLEN (entry) + len >= buflen)
1162 {
1163 buflen = len + NAMLEN (entry);
1164 namebuf = xrealloc (namebuf, buflen + 1);
1165 }
1166 strcpy (namebuf + len, entry->d_name);
1167 if (!excluded_name (namebuf))
1168 dump_file (namebuf, 0, our_device);
1169 }
1170
1171 closedir (directory);
1172 free (namebuf);
1173 if (atime_preserve_option)
1174 utime (p, &restore_times);
1175 return;
1176 }
1177 else if (is_avoided_name (p))
1178 return;
1179 else
1180 {
1181 /* Check for multiple links.
1182
1183 We maintain a list of all such files that we've written so far. Any
1184 time we see another, we check the list and avoid dumping the data
1185 again if we've done it once already. */
1186
1187 if (1 < current_stat.st_nlink)
1188 {
1189 struct link *lp;
1190
1191 /* FIXME: First quick and dirty. Hashing, etc later. */
1192
1193 for (lp = linklist; lp; lp = lp->next)
1194 if (lp->ino == current_stat.st_ino
1195 && lp->dev == current_stat.st_dev)
1196 {
1197 char *link_name = lp->name;
1198
1199 /* We found a link. */
1200
1201 while (!absolute_names_option && *link_name == '/')
1202 {
1203 static int warned_once;
1204 if (!warned_once)
1205 {
1206 warned_once = 1;
1207 WARN ((0, 0,
1208 _("Removing leading `/' from link names")));
1209 }
1210 link_name++;
1211 }
1212 if (strlen (link_name) >= NAME_FIELD_SIZE)
1213 write_long (link_name, GNUTYPE_LONGLINK);
1214 assign_string (&current_link_name, link_name);
1215
1216 current_stat.st_size = 0;
1217 header = start_header (p, &current_stat);
1218 strncpy (header->header.linkname,
1219 link_name, NAME_FIELD_SIZE);
1220
1221 /* Force null truncated. */
1222
1223 header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
1224
1225 header->header.typeflag = LNKTYPE;
1226 finish_header (header);
1227
1228 /* FIXME: Maybe remove from list after all links found? */
1229
1230 if (remove_files_option)
1231 if (unlink (p) == -1)
1232 ERROR ((0, errno, _("Cannot remove %s"), p));
1233
1234 /* We dumped it. */
1235 return;
1236 }
1237
1238 /* Not found. Add it to the list of possible links. */
1239
1240 lp = xmalloc (sizeof (struct link) + strlen (p));
1241 lp->ino = current_stat.st_ino;
1242 lp->dev = current_stat.st_dev;
1243 strcpy (lp->name, p);
1244 lp->next = linklist;
1245 linklist = lp;
1246 }
1247
1248 /* This is not a link to a previously dumped file, so dump it. */
1249
1250 if (S_ISREG (current_stat.st_mode)
1251 || S_ISCTG (current_stat.st_mode))
1252 {
1253 int f; /* file descriptor */
1254 size_t bufsize;
1255 ssize_t count;
1256 off_t sizeleft;
1257 union block *start;
1258 int header_moved;
1259 char isextended = 0;
1260 int upperbound;
1261
1262 header_moved = 0;
1263
1264 if (sparse_option)
1265 {
1266 /* Check the size of the file against the number of blocks
1267 allocated for it, counting both data and indirect blocks.
1268 If there is a smaller number of blocks that would be
1269 necessary to accommodate a file of this size, this is safe
1270 to say that we have a sparse file: at least one of those
1271 blocks in the file is just a useless hole. For sparse
1272 files not having more hole blocks than indirect blocks, the
1273 sparseness will go undetected. */
1274
1275 /* Bruno Haible sent me these statistics for Linux. It seems
1276 that some filesystems count indirect blocks in st_blocks,
1277 while others do not seem to:
1278
1279 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1280 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
1281 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1282 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
1283
1284 Dick Streefland reports the previous numbers as misleading,
1285 because ext2fs use 12 direct blocks, while minix-fs uses only
1286 6 direct blocks. Dick gets:
1287
1288 ext2 size=20480 ls listed blocks=21
1289 minix size=20480 ls listed blocks=21
1290 msdos size=20480 ls listed blocks=20
1291
1292 It seems that indirect blocks *are* included in st_blocks.
1293 The minix filesystem does not account for phantom blocks in
1294 st_blocks, so `du' and `ls -s' give wrong results. So, the
1295 --sparse option would not work on a minix filesystem. */
1296
1297 if (ST_NBLOCKS (current_stat)
1298 < (current_stat.st_size / ST_NBLOCKSIZE
1299 + (current_stat.st_size % ST_NBLOCKSIZE != 0)))
1300 {
1301 off_t filesize = current_stat.st_size;
1302 int counter;
1303
1304 header = start_header (p, &current_stat);
1305 header->header.typeflag = GNUTYPE_SPARSE;
1306 header_moved = 1;
1307
1308 /* Call the routine that figures out the layout of the
1309 sparse file in question. UPPERBOUND is the index of the
1310 last element of the "sparsearray," i.e., the number of
1311 elements it needed to describe the file. */
1312
1313 upperbound = deal_with_sparse (p, header);
1314
1315 /* See if we'll need an extended header later. */
1316
1317 if (upperbound > SPARSES_IN_OLDGNU_HEADER - 1)
1318 header->oldgnu_header.isextended = 1;
1319
1320 /* We store the "real" file size so we can show that in
1321 case someone wants to list the archive, i.e., tar tvf
1322 <file>. It might be kind of disconcerting if the
1323 shrunken file size was the one that showed up. */
1324
1325 OFF_TO_CHARS (current_stat.st_size,
1326 header->oldgnu_header.realsize);
1327
1328 /* This will be the new "size" of the file, i.e., the size
1329 of the file minus the blocks of holes that we're
1330 skipping over. */
1331
1332 find_new_file_size (&filesize, upperbound);
1333 current_stat.st_size = filesize;
1334 OFF_TO_CHARS (filesize, header->header.size);
1335
1336 for (counter = 0; counter < SPARSES_IN_OLDGNU_HEADER; counter++)
1337 {
1338 if (!sparsearray[counter].numbytes)
1339 break;
1340
1341 OFF_TO_CHARS (sparsearray[counter].offset,
1342 header->oldgnu_header.sp[counter].offset);
1343 SIZE_TO_CHARS (sparsearray[counter].numbytes,
1344 header->oldgnu_header.sp[counter].numbytes);
1345 }
1346
1347 }
1348 }
1349 else
1350 upperbound = SPARSES_IN_OLDGNU_HEADER - 1;
1351
1352 sizeleft = current_stat.st_size;
1353
1354 /* Don't bother opening empty, world readable files. Also do not open
1355 files when archive is meant for /dev/null. */
1356
1357 if (dev_null_output
1358 || (sizeleft == 0
1359 && MODE_R == (MODE_R & current_stat.st_mode)))
1360 f = -1;
1361 else
1362 {
1363 f = open (p, O_RDONLY | O_BINARY);
1364 if (f < 0)
1365 {
1366 if (! top_level && errno == ENOENT)
1367 WARN ((0, 0, _("%s: file removed before we read it"), p));
1368 else
1369 WARN ((0, errno, _("Cannot add file %s"), p));
1370 if (!ignore_failed_read_option)
1371 exit_status = TAREXIT_FAILURE;
1372 return;
1373 }
1374 }
1375
1376 /* If the file is sparse, we've already taken care of this. */
1377
1378 if (!header_moved)
1379 header = start_header (p, &current_stat);
1380
1381 /* Mark contiguous files, if we support them. */
1382
1383 if (archive_format != V7_FORMAT && S_ISCTG (current_stat.st_mode))
1384 header->header.typeflag = CONTTYPE;
1385
1386 isextended = header->oldgnu_header.isextended;
1387 save_typeflag = header->header.typeflag;
1388 finish_header (header);
1389 if (isextended)
1390 {
1391 int counter;
1392 /* static */ int index_offset = SPARSES_IN_OLDGNU_HEADER;
1393
1394 extend:
1395 exhdr = find_next_block ();
1396 memset (exhdr->buffer, 0, BLOCKSIZE);
1397 for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
1398 {
1399 if (counter + index_offset > upperbound)
1400 break;
1401
1402 SIZE_TO_CHARS (sparsearray[counter + index_offset].numbytes,
1403 exhdr->sparse_header.sp[counter].numbytes);
1404 OFF_TO_CHARS (sparsearray[counter + index_offset].offset,
1405 exhdr->sparse_header.sp[counter].offset);
1406 }
1407 set_next_block_after (exhdr);
1408 if (index_offset + counter <= upperbound)
1409 {
1410 index_offset += counter;
1411 exhdr->sparse_header.isextended = 1;
1412 goto extend;
1413 }
1414
1415 }
1416 if (save_typeflag == GNUTYPE_SPARSE)
1417 {
1418 if (f < 0
1419 || finish_sparse_file (f, &sizeleft, current_stat.st_size, p))
1420 goto padit;
1421 }
1422 else
1423 while (sizeleft > 0)
1424 {
1425 if (multi_volume_option)
1426 {
1427 assign_string (&save_name, p);
1428 save_sizeleft = sizeleft;
1429 save_totsize = current_stat.st_size;
1430 }
1431 start = find_next_block ();
1432
1433 bufsize = available_space_after (start);
1434
1435 if (sizeleft < bufsize)
1436 {
1437 /* Last read -- zero out area beyond. */
1438
1439 bufsize = sizeleft;
1440 count = bufsize % BLOCKSIZE;
1441 if (count)
1442 memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
1443 }
1444 if (f < 0)
1445 count = bufsize;
1446 else
1447 count = safe_read (f, start->buffer, bufsize);
1448 if (count < 0)
1449 {
1450 char buf[UINTMAX_STRSIZE_BOUND];
1451 ERROR ((0, errno,
1452 _("Read error at byte %s, reading %lu bytes, in file %s"),
1453 STRINGIFY_BIGINT (current_stat.st_size - sizeleft,
1454 buf),
1455 (unsigned long) bufsize, p));
1456 goto padit;
1457 }
1458 sizeleft -= count;
1459
1460 /* This is nonportable (the type of set_next_block_after's arg). */
1461
1462 set_next_block_after (start + (count - 1) / BLOCKSIZE);
1463
1464 if (count == bufsize)
1465 continue;
1466 else
1467 {
1468 char buf[UINTMAX_STRSIZE_BOUND];
1469 ERROR ((0, 0,
1470 _("File %s shrunk by %s bytes, padding with zeros"),
1471 p, STRINGIFY_BIGINT (sizeleft, buf)));
1472 goto padit; /* short read */
1473 }
1474 }
1475
1476 if (multi_volume_option)
1477 assign_string (&save_name, 0);
1478
1479 if (f >= 0)
1480 {
1481 struct stat final_stat;
1482 if (fstat (f, &final_stat) != 0)
1483 ERROR ((0, errno, "%s: fstat", p));
1484 else if (final_stat.st_ctime != original_ctime)
1485 ERROR ((0, 0, _("%s: file changed as we read it"), p));
1486 if (close (f) != 0)
1487 ERROR ((0, errno, _("%s: close"), p));
1488 if (atime_preserve_option)
1489 utime (p, &restore_times);
1490 }
1491 if (remove_files_option)
1492 {
1493 if (unlink (p) == -1)
1494 ERROR ((0, errno, _("Cannot remove %s"), p));
1495 }
1496 return;
1497
1498 /* File shrunk or gave error, pad out tape to match the size we
1499 specified in the header. */
1500
1501 padit:
1502 while (sizeleft > 0)
1503 {
1504 save_sizeleft = sizeleft;
1505 start = find_next_block ();
1506 memset (start->buffer, 0, BLOCKSIZE);
1507 set_next_block_after (start);
1508 sizeleft -= BLOCKSIZE;
1509 }
1510 if (multi_volume_option)
1511 assign_string (&save_name, 0);
1512 if (f >= 0)
1513 {
1514 close (f);
1515 if (atime_preserve_option)
1516 utime (p, &restore_times);
1517 }
1518 return;
1519 }
1520 #ifdef HAVE_READLINK
1521 else if (S_ISLNK (current_stat.st_mode))
1522 {
1523 int size;
1524 char *buffer = (char *) alloca (PATH_MAX + 1);
1525
1526 size = readlink (p, buffer, PATH_MAX + 1);
1527 if (size < 0)
1528 {
1529 WARN ((0, errno, _("Cannot add file %s"), p));
1530 if (!ignore_failed_read_option)
1531 exit_status = TAREXIT_FAILURE;
1532 return;
1533 }
1534 buffer[size] = '\0';
1535 if (size >= NAME_FIELD_SIZE)
1536 write_long (buffer, GNUTYPE_LONGLINK);
1537 assign_string (&current_link_name, buffer);
1538
1539 current_stat.st_size = 0; /* force 0 size on symlink */
1540 header = start_header (p, &current_stat);
1541 strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
1542 header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
1543 header->header.typeflag = SYMTYPE;
1544 finish_header (header); /* nothing more to do to it */
1545 if (remove_files_option)
1546 {
1547 if (unlink (p) == -1)
1548 ERROR ((0, errno, _("Cannot remove %s"), p));
1549 }
1550 return;
1551 }
1552 #endif
1553 else if (S_ISCHR (current_stat.st_mode))
1554 type = CHRTYPE;
1555 else if (S_ISBLK (current_stat.st_mode))
1556 type = BLKTYPE;
1557 else if (S_ISFIFO (current_stat.st_mode)
1558 || S_ISSOCK (current_stat.st_mode))
1559 type = FIFOTYPE;
1560 #ifdef S_ISDOOR
1561 else if (S_ISDOOR (current_stat.st_mode))
1562 {
1563 WARN ((0, 0, _("%s: door ignored"), p));
1564 return;
1565 }
1566 #endif
1567 else
1568 goto unknown;
1569 }
1570
1571 if (archive_format == V7_FORMAT)
1572 goto unknown;
1573
1574 current_stat.st_size = 0; /* force 0 size */
1575 header = start_header (p, &current_stat);
1576 header->header.typeflag = type;
1577
1578 if (type != FIFOTYPE)
1579 {
1580 MAJOR_TO_CHARS (major (current_stat.st_rdev), header->header.devmajor);
1581 MINOR_TO_CHARS (minor (current_stat.st_rdev), header->header.devminor);
1582 }
1583
1584 finish_header (header);
1585 if (remove_files_option)
1586 {
1587 if (unlink (p) == -1)
1588 ERROR ((0, errno, _("Cannot remove %s"), p));
1589 }
1590 return;
1591
1592 unknown:
1593 ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p));
1594 }
This page took 0.103878 seconds and 5 git commands to generate.