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