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