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