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