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