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