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