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