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