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.
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
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.
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. */
43 extern struct name
*gnu_list_name
;
45 /* This module is the only one that cares about `struct link's. */
56 struct link
*linklist
= NULL
; /* points to first link in list */
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. |
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 `------------------------------------------------------------------------*/
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. */
72 to_oct (uintmax_t value
, char *where
, size_t size
, const char *type
)
76 where
[--i
] = ' '; /* put in the space, though */
78 /* Produce the digits -- at least one. */
82 where
[--i
] = '0' + (int) (v
& 7); /* one octal digit */
85 while (i
!= 0 && v
!= 0);
87 /* Leading spaces, if necessary. */
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)));
100 gid_to_oct (gid_t v
, char *p
, size_t s
)
102 to_oct (v
, p
, s
, "gid_t");
105 major_to_oct (major_t v
, char *p
, size_t s
)
107 to_oct (v
, p
, s
, "major_t");
110 minor_to_oct (minor_t v
, char *p
, size_t s
)
112 to_oct (v
, p
, s
, "minor_t");
115 mode_to_oct (mode_t v
, char *p
, size_t s
)
117 to_oct (v
, p
, s
, "mode_t");
120 off_to_oct (off_t v
, char *p
, size_t s
)
122 to_oct (v
, p
, s
, "off_t");
125 size_to_oct (size_t v
, char *p
, size_t s
)
127 to_oct (v
, p
, s
, "size_t");
130 time_to_oct (time_t v
, char *p
, size_t s
)
132 to_oct (v
, p
, s
, "time_t");
135 uid_to_oct (uid_t v
, char *p
, size_t s
)
137 to_oct (v
, p
, s
, "uid_t");
140 uintmax_to_oct (uintmax_t v
, char *p
, size_t s
)
142 to_oct (v
, p
, s
, "uintmax_t");
145 /* Writing routines. */
147 /*-----------------------------------------------------------------------.
148 | Just zeroes out the buffer so we don't confuse ourselves with leftover |
150 `-----------------------------------------------------------------------*/
153 clear_buffer (char *buffer
)
155 memset (buffer
, 0, BLOCKSIZE
);
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 `-------------------------------------------------------------------------*/
167 union block
*pointer
= find_next_block ();
171 size_t space
= available_space_after (pointer
);
173 memset (pointer
->buffer
, 0, space
);
174 set_next_block_after (pointer
);
178 /*-----------------------------------------------------.
179 | Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. |
180 `-----------------------------------------------------*/
182 /* FIXME: Cross recursion between start_header and write_long! */
184 static union block
*start_header
PARAMS ((const char *, struct stat
*));
187 write_long (const char *p
, char type
)
189 size_t size
= strlen (p
) + 1;
194 memset (&foo
, 0, sizeof foo
);
197 header
= start_header ("././@LongLink", &foo
);
198 header
->header
.typeflag
= type
;
199 finish_header (header
);
201 header
= find_next_block ();
203 bufsize
= available_space_after (header
);
205 while (bufsize
< size
)
207 memcpy (header
->buffer
, p
, bufsize
);
210 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
211 header
= find_next_block ();
212 bufsize
= available_space_after (header
);
214 memcpy (header
->buffer
, p
, size
);
215 memset (header
->buffer
+ size
, 0, bufsize
- size
);
216 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
219 /* Header handling. */
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 `---------------------------------------------------------------------*/
227 start_header (const char *name
, struct stat
*st
)
231 if (!absolute_names_option
)
233 static int warned_once
= 0;
242 WARN ((0, 0, _("Removing drive spec from names in the archive")));
249 name
++; /* force relative path */
254 Removing leading `/' from absolute path names in the archive")));
259 /* Check the file name and put it in the block. */
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
));
266 assign_string (¤t_file_name
, name
);
268 strncpy (header
->header
.name
, name
, NAME_FIELD_SIZE
);
269 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
271 /* Override some stat fields, if requested to do so. */
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
;
278 st
->st_mode
= ((st
->st_mode
& S_IFMT
)
279 | mode_adjust (st
->st_mode
, mode_option
));
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
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
298 # = ``impossible file type''
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. */
304 if (archive_format
== V7_FORMAT
)
305 MODE_TO_OCT (st
->st_mode
& 07777, header
->header
.mode
);
307 MODE_TO_OCT (st
->st_mode
, header
->header
.mode
);
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
);
314 if (incremental_option
)
315 if (archive_format
== OLDGNU_FORMAT
)
317 TIME_TO_OCT (st
->st_atime
, header
->oldgnu_header
.atime
);
318 TIME_TO_OCT (st
->st_ctime
, header
->oldgnu_header
.ctime
);
321 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
323 switch (archive_format
)
330 /* Overwrite header->header.magic and header.version in one blow. */
331 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
336 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
337 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
341 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
343 /* header->header.[ug]name are left as the empty string. */
347 uid_to_uname (st
->st_uid
, header
->header
.uname
);
348 gid_to_gname (st
->st_gid
, header
->header
.gname
);
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 `-------------------------------------------------------------------------*/
360 finish_header (union block
*header
)
366 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof (header
->header
.chksum
));
370 for (i
= sizeof (*header
); i
-- != 0; )
371 /* We can't use unsigned char here because of old compilers, e.g. V7. */
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.
380 This is a fast way to do:
382 sprintf(header->header.chksum, "%6o", sum); */
384 UINTMAX_TO_OCT ((uintmax_t) sum
, header
->header
.chksum
);
385 header
->header
.chksum
[6] = '\0'; /* zap the space */
387 set_next_block_after (header
);
390 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
391 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
)
393 /* These globals are parameters to print_header, sigh. */
395 current_header
= header
;
396 /* current_stat is already set up. */
397 current_format
= archive_format
;
402 /* Sparse file processing. */
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 `-------------------------------------------------------------------------*/
411 zero_block_p (char *buffer
)
415 for (counter
= 0; counter
< BLOCKSIZE
; counter
++)
416 if (buffer
[counter
] != '\0')
426 init_sparsearray (void)
432 /* Make room for our scratch space -- initially is 10 elts long. */
434 sparsearray
= (struct sp_array
*)
435 xmalloc (sp_array_size
* sizeof (struct sp_array
));
436 for (counter
= 0; counter
< sp_array_size
; counter
++)
438 sparsearray
[counter
].offset
= 0;
439 sparsearray
[counter
].numbytes
= 0;
448 find_new_file_size (off_t
*filesize
, int highest_index
)
454 sparsearray
[counter
].numbytes
&& counter
<= highest_index
;
456 *filesize
+= sparsearray
[counter
].numbytes
;
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 `-----------------------------------------------------------------------*/
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. */
469 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
470 too kludgey for my taste... */
473 deal_with_sparse (char *name
, union block
*header
)
478 int sparse_index
= 0;
480 char buffer
[BLOCKSIZE
];
482 if (archive_format
== OLDGNU_FORMAT
)
483 header
->oldgnu_header
.isextended
= 0;
485 if (file
= open (name
, O_RDONLY
), file
< 0)
486 /* This problem will be caught later on, so just return. */
490 clear_buffer (buffer
);
492 while (count
= read (file
, buffer
, sizeof buffer
), count
!= 0)
494 /* Realloc the scratch area as necessary. FIXME: should reallocate
495 only at beginning of a new instance of non-zero data. */
497 if (sparse_index
> sp_array_size
- 1)
500 sparsearray
= (struct sp_array
*)
501 xrealloc (sparsearray
,
502 2 * sp_array_size
* sizeof (struct sp_array
));
506 /* Process one block. */
508 if (count
== sizeof buffer
)
510 if (zero_block_p (buffer
))
514 sparsearray
[sparse_index
++].numbytes
= numbytes
;
521 sparsearray
[sparse_index
].offset
= offset
;
527 /* Since count < sizeof buffer, we have the last bit of the file. */
529 if (!zero_block_p (buffer
))
532 sparsearray
[sparse_index
].offset
= offset
;
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. */
545 /* Prepare for next block. */
548 /* FIXME: do not clear unless necessary. */
549 clear_buffer (buffer
);
553 sparsearray
[sparse_index
++].numbytes
= numbytes
;
556 sparsearray
[sparse_index
].offset
= offset
- 1;
557 sparsearray
[sparse_index
++].numbytes
= 1;
561 return sparse_index
- 1;
569 finish_sparse_file (int file
, off_t
*sizeleft
, off_t fullsize
, char *name
)
573 int sparse_index
= 0;
576 while (*sizeleft
> 0)
578 start
= find_next_block ();
579 memset (start
->buffer
, 0, BLOCKSIZE
);
580 bufsize
= sparsearray
[sparse_index
].numbytes
;
583 /* We blew it, maybe. */
584 char buf1
[UINTMAX_STRSIZE_BOUND
];
585 char buf2
[UINTMAX_STRSIZE_BOUND
];
587 ERROR ((0, 0, _("Wrote %s of %s bytes to file %s"),
588 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf1
),
589 STRINGIFY_BIGINT (fullsize
, buf2
),
594 if (lseek (file
, sparsearray
[sparse_index
++].offset
, 0) < 0)
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
),
603 /* If the number of bytes to be written here exceeds the size of
604 the temporary buffer, do it in steps. */
606 while (bufsize
> BLOCKSIZE
)
611 count
= read (file
, start
->buffer
+ amount_read
,
612 BLOCKSIZE
- amount_read
);
613 bufsize
-= BLOCKSIZE
- amount_read
;
615 set_next_block_after (start
);
616 start
= find_next_block ();
617 memset (start
->buffer
, 0, BLOCKSIZE
);
620 /* Store the data. */
622 count
= read (file
, start
->buffer
, BLOCKSIZE
);
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
));
634 set_next_block_after (start
);
635 start
= find_next_block ();
636 memset (start
->buffer
, 0, BLOCKSIZE
);
640 char buffer
[BLOCKSIZE
];
642 clear_buffer (buffer
);
643 count
= read (file
, buffer
, bufsize
);
644 memcpy (start
->buffer
, buffer
, BLOCKSIZE
);
649 char buf
[UINTMAX_STRSIZE_BOUND
];
652 _("Read error at byte %s, reading %lu bytes, in file %s"),
653 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
654 (unsigned long) bufsize
, name
));
658 if (amount_read
>= BLOCKSIZE
)
661 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
662 if (count
!= bufsize
)
665 _("File %s shrunk, padding with zeros"),
669 start
= find_next_block ();
672 amount_read
+= bufsize
;
675 set_next_block_after (start
);
680 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
685 /* Main functions of this module. */
692 create_archive (void)
696 open_archive (ACCESS_WRITE
);
698 if (incremental_option
)
700 char *buffer
= xmalloc (PATH_MAX
);
704 collect_and_sort_names ();
706 while (p
= name_from_list (), p
)
707 dump_file (p
, (dev_t
) -1, 1);
710 while (p
= name_from_list (), p
)
713 if (p
[strlen (p
) - 1] != '/')
714 strcat (buffer
, "/");
715 bufp
= buffer
+ strlen (buffer
);
716 for (q
= gnu_list_name
->dir_contents
;
722 strcpy (bufp
, q
+ 1);
723 dump_file (buffer
, (dev_t
) -1, 1);
731 while (p
= name_next (1), p
)
732 dump_file (p
, (dev_t
) -1, 1);
738 if (listed_incremental_option
)
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. |
747 | Sets global CURRENT_STAT to stat output for this file. |
748 `----------------------------------------------------------------------*/
750 /* FIXME: One should make sure that for *every* path leading to setting
751 exit_status to failure, a clear diagnostic has been issued. */
754 dump_file (char *p
, dev_t parent_device
, int top_level
)
760 struct utimbuf restore_times
;
762 /* FIXME: `header' and `upperbound' might be used uninitialized in this
763 function. Reported by Bruno Haible. */
765 if (interactive_option
&& !confirm ("add", p
))
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). */
771 if (dereference_option
!= 0
772 #ifdef STX_HIDDEN /* AIX */
773 ? statx (p
, ¤t_stat
, STATSIZE
, STX_HIDDEN
)
774 : statx (p
, ¤t_stat
, STATSIZE
, STX_HIDDEN
| STX_LINK
)
776 ? stat (p
, ¤t_stat
) : lstat (p
, ¤t_stat
)
780 WARN ((0, errno
, _("Cannot add file %s"), p
));
781 if (!ignore_failed_read_option
)
782 exit_status
= TAREXIT_FAILURE
;
786 restore_times
.actime
= current_stat
.st_atime
;
787 restore_times
.modtime
= current_stat
.st_mtime
;
790 if (S_ISHIDDEN (current_stat
.st_mode
))
792 char *new = (char *) alloca (strlen (p
) + 2);
802 /* See if we only want new files, and check if this one is too old to
803 put in the archive. */
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
))
809 if (parent_device
== (dev_t
) -1)
810 WARN ((0, 0, _("%s: is unchanged; not dumped"), p
));
811 /* FIXME: recheck this return. */
816 /* See if we are trying to dump the archive. */
818 if (ar_dev
&& current_stat
.st_dev
== ar_dev
&& current_stat
.st_ino
== ar_ino
)
820 WARN ((0, 0, _("%s is the archive; not dumped"), p
));
825 /* Check for multiple links.
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. */
831 if (current_stat
.st_nlink
> 1
832 && (S_ISREG (current_stat
.st_mode
)
834 || S_ISCTG (current_stat
.st_mode
)
837 || S_ISCHR (current_stat
.st_mode
)
840 || S_ISBLK (current_stat
.st_mode
)
843 || S_ISFIFO (current_stat
.st_mode
)
849 /* FIXME: First quick and dirty. Hashing, etc later. */
851 for (lp
= linklist
; lp
; lp
= lp
->next
)
852 if (lp
->ino
== current_stat
.st_ino
&& lp
->dev
== current_stat
.st_dev
)
854 char *link_name
= lp
->name
;
856 /* We found a link. */
858 while (!absolute_names_option
&& *link_name
== '/')
860 static int warned_once
= 0;
866 Removing leading `/' from absolute links")));
870 if (strlen (link_name
) >= NAME_FIELD_SIZE
)
871 write_long (link_name
, GNUTYPE_LONGLINK
);
872 assign_string (¤t_link_name
, link_name
);
874 current_stat
.st_size
= 0;
875 header
= start_header (p
, ¤t_stat
);
878 exit_status
= TAREXIT_FAILURE
;
881 strncpy (header
->header
.linkname
,
882 link_name
, NAME_FIELD_SIZE
);
884 /* Force null truncated. */
886 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = 0;
888 header
->header
.typeflag
= LNKTYPE
;
889 finish_header (header
);
891 /* FIXME: Maybe remove from list after all links found? */
893 if (remove_files_option
)
894 if (unlink (p
) == -1)
895 ERROR ((0, errno
, _("Cannot remove %s"), p
));
901 /* Not found. Add it to the list of possible links. */
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
);
912 /* This is not a link to a previously dumped file, so dump it. */
914 if (S_ISREG (current_stat
.st_mode
)
916 || S_ISCTG (current_stat
.st_mode
)
920 int f
; /* file descriptor */
929 static int cried_once
= 0;
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. */
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:
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
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:
958 ext2 size=20480 ls listed blocks=21
959 minix size=20480 ls listed blocks=21
960 msdos size=20480 ls listed blocks=20
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. */
967 if (ST_NBLOCKS (current_stat
)
968 < (current_stat
.st_size
/ ST_NBLOCKSIZE
969 + (current_stat
.st_size
% ST_NBLOCKSIZE
!= 0)))
971 off_t filesize
= current_stat
.st_size
;
974 header
= start_header (p
, ¤t_stat
);
977 exit_status
= TAREXIT_FAILURE
;
980 header
->header
.typeflag
= GNUTYPE_SPARSE
;
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. */
988 upperbound
= deal_with_sparse (p
, header
);
990 /* See if we'll need an extended header later. */
992 if (upperbound
> SPARSES_IN_OLDGNU_HEADER
- 1)
993 header
->oldgnu_header
.isextended
= 1;
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. */
1000 OFF_TO_OCT (current_stat
.st_size
,
1001 header
->oldgnu_header
.realsize
);
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
1007 find_new_file_size (&filesize
, upperbound
);
1008 current_stat
.st_size
= filesize
;
1009 OFF_TO_OCT (filesize
, header
->header
.size
);
1011 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
1013 if (!sparsearray
[counter
].numbytes
)
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
);
1025 upperbound
= SPARSES_IN_OLDGNU_HEADER
- 1;
1027 sizeleft
= current_stat
.st_size
;
1029 /* Don't bother opening empty, world readable files. Also do not open
1030 files when archive is meant for /dev/null. */
1033 || (sizeleft
== 0 && 0444 == (0444 & current_stat
.st_mode
)))
1037 f
= open (p
, O_RDONLY
| O_BINARY
);
1040 WARN ((0, errno
, _("Cannot add file %s"), p
));
1041 if (!ignore_failed_read_option
)
1042 exit_status
= TAREXIT_FAILURE
;
1047 /* If the file is sparse, we've already taken care of this. */
1051 header
= start_header (p
, ¤t_stat
);
1056 exit_status
= TAREXIT_FAILURE
;
1061 /* Mark contiguous files, if we support them. */
1063 if (archive_format
!= V7_FORMAT
&& S_ISCTG (current_stat
.st_mode
))
1064 header
->header
.typeflag
= CONTTYPE
;
1066 isextended
= header
->oldgnu_header
.isextended
;
1067 save_typeflag
= header
->header
.typeflag
;
1068 finish_header (header
);
1077 int arraybound
= SPARSES_IN_SPARSE_HEADER
;
1079 /* static */ int index_offset
= SPARSES_IN_OLDGNU_HEADER
;
1082 exhdr
= find_next_block ();
1086 exit_status
= TAREXIT_FAILURE
;
1089 memset (exhdr
->buffer
, 0, BLOCKSIZE
);
1090 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
1092 if (counter
+ index_offset
> upperbound
)
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
);
1100 set_next_block_after (exhdr
);
1103 if (sum
< upperbound
)
1106 if (index_offset
+ counter
<= upperbound
)
1108 index_offset
+= counter
;
1109 exhdr
->sparse_header
.isextended
= 1;
1114 if (save_typeflag
== GNUTYPE_SPARSE
)
1116 if (finish_sparse_file (f
, &sizeleft
, current_stat
.st_size
, p
))
1120 while (sizeleft
> 0)
1122 if (multi_volume_option
)
1124 assign_string (&save_name
, p
);
1125 save_sizeleft
= sizeleft
;
1126 save_totsize
= current_stat
.st_size
;
1128 start
= find_next_block ();
1130 bufsize
= available_space_after (start
);
1132 if (sizeleft
< bufsize
)
1134 /* Last read -- zero out area beyond. */
1137 count
= bufsize
% BLOCKSIZE
;
1139 memset (start
->buffer
+ sizeleft
, 0,
1140 (size_t) (BLOCKSIZE
- count
));
1145 count
= read (f
, start
->buffer
, bufsize
);
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
,
1153 (unsigned long) bufsize
, p
));
1158 /* This is nonportable (the type of set_next_block_after's arg). */
1160 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
1162 if (count
== bufsize
)
1166 char buf
[UINTMAX_STRSIZE_BOUND
];
1168 _("File %s shrunk by %s bytes, padding with zeros"),
1169 p
, STRINGIFY_BIGINT (sizeleft
, buf
)));
1170 goto padit
; /* short read */
1174 if (multi_volume_option
)
1175 assign_string (&save_name
, NULL
);
1180 if (atime_preserve_option
)
1181 utime (p
, &restore_times
);
1183 if (remove_files_option
)
1185 if (unlink (p
) == -1)
1186 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1190 /* File shrunk or gave error, pad out tape to match the size we
1191 specified in the header. */
1194 while (sizeleft
> 0)
1196 save_sizeleft
= sizeleft
;
1197 start
= find_next_block ();
1198 memset (start
->buffer
, 0, BLOCKSIZE
);
1199 set_next_block_after (start
);
1200 sizeleft
-= BLOCKSIZE
;
1202 if (multi_volume_option
)
1203 assign_string (&save_name
, NULL
);
1207 if (atime_preserve_option
)
1208 utime (p
, &restore_times
);
1214 else if (S_ISLNK (current_stat
.st_mode
))
1217 char *buffer
= (char *) alloca (PATH_MAX
+ 1);
1219 size
= readlink (p
, buffer
, PATH_MAX
+ 1);
1222 WARN ((0, errno
, _("Cannot add file %s"), p
));
1223 if (!ignore_failed_read_option
)
1224 exit_status
= TAREXIT_FAILURE
;
1227 buffer
[size
] = '\0';
1228 if (size
>= NAME_FIELD_SIZE
)
1229 write_long (buffer
, GNUTYPE_LONGLINK
);
1230 assign_string (¤t_link_name
, buffer
);
1232 current_stat
.st_size
= 0; /* force 0 size on symlink */
1233 header
= start_header (p
, ¤t_stat
);
1236 exit_status
= TAREXIT_FAILURE
;
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
)
1245 if (unlink (p
) == -1)
1246 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1250 #endif /* S_ISLNK */
1252 else if (S_ISDIR (current_stat
.st_mode
))
1255 struct dirent
*entry
;
1259 dev_t our_device
= current_stat
.st_dev
;
1261 /* If this tar program is installed suid root, like for Amanda, the
1262 access might look like denied, while it is not really.
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. */
1268 if (access (p
, R_OK
) == -1 && geteuid () != 0)
1270 WARN ((0, errno
, _("Cannot add directory %s"), p
));
1271 if (!ignore_failed_read_option
)
1272 exit_status
= TAREXIT_FAILURE
;
1276 /* Build new prototype name. Ensure exactly one trailing slash. */
1279 buflen
= len
+ NAME_FIELD_SIZE
;
1280 namebuf
= xmalloc (buflen
+ 1);
1281 strncpy (namebuf
, p
, buflen
);
1282 while (len
>= 1 && namebuf
[len
- 1] == '/')
1284 namebuf
[len
++] = '/';
1285 namebuf
[len
] = '\0';
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. */
1299 current_stat
.st_size
= 0; /* force 0 size on dir */
1301 /* FIXME: If people could really read standard archives, this
1305 = start_header (standard_option ? p : namebuf, ¤t_stat);
1307 but since they'd interpret DIRTYPE blocks as regular
1308 files, we'd better put the / on the name. */
1310 header
= start_header (namebuf
, ¤t_stat
);
1313 exit_status
= TAREXIT_FAILURE
;
1314 return; /* eg name too long */
1317 if (incremental_option
)
1318 header
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1319 else /* if (standard_option) */
1320 header
->header
.typeflag
= DIRTYPE
;
1322 /* If we're gnudumping, we aren't done yet so don't close it. */
1324 if (!incremental_option
)
1325 finish_header (header
); /* done with directory header */
1328 if (incremental_option
&& gnu_list_name
->dir_contents
)
1335 const char *buffer
, *p_buffer
;
1337 buffer
= gnu_list_name
->dir_contents
; /* FOO */
1339 for (p_buffer
= buffer
; p_buffer
&& *p_buffer
;)
1343 tmp
= strlen (p_buffer
) + 1;
1348 OFF_TO_OCT (totsize
, header
->header
.size
);
1349 finish_header (header
);
1352 while (sizeleft
> 0)
1354 if (multi_volume_option
)
1356 assign_string (&save_name
, p
);
1357 save_sizeleft
= sizeleft
;
1358 save_totsize
= totsize
;
1360 start
= find_next_block ();
1361 bufsize
= available_space_after (start
);
1362 if (sizeleft
< bufsize
)
1365 count
= bufsize
% BLOCKSIZE
;
1367 memset (start
->buffer
+ sizeleft
, 0,
1368 (size_t) (BLOCKSIZE
- count
));
1370 memcpy (start
->buffer
, p_buffer
, bufsize
);
1371 sizeleft
-= bufsize
;
1372 p_buffer
+= bufsize
;
1373 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1375 if (multi_volume_option
)
1376 assign_string (&save_name
, NULL
);
1377 if (atime_preserve_option
)
1378 utime (p
, &restore_times
);
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. */
1385 if (no_recurse_option
)
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. */
1391 if (one_file_system_option
&& !top_level
1392 && parent_device
!= current_stat
.st_dev
)
1395 WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p
));
1399 /* Now output all the files in the directory. */
1401 errno
= 0; /* FIXME: errno should be read-only */
1403 directory
= opendir (p
);
1406 ERROR ((0, errno
, _("Cannot open directory %s"), p
));
1410 /* Hack to remove "./" from the front of all the file names. */
1412 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1] == '/')
1415 /* FIXME: Should speed this up by cd-ing into the dir. */
1417 while (entry
= readdir (directory
), entry
)
1419 /* Skip `.' and `..'. */
1421 if (is_dot_or_dotdot (entry
->d_name
))
1424 if ((int) NAMLEN (entry
) + len
>= buflen
)
1426 buflen
= len
+ NAMLEN (entry
);
1427 namebuf
= (char *) xrealloc (namebuf
, buflen
+ 1);
1429 namebuf
[len
] = '\0';
1430 ERROR ((0, 0, _("File name %s%s too long"),
1431 namebuf
, entry
->d_name
));
1435 strcpy (namebuf
+ len
, entry
->d_name
);
1436 if (exclude_option
&& check_exclude (namebuf
))
1438 dump_file (namebuf
, our_device
, 0);
1441 closedir (directory
);
1443 if (atime_preserve_option
)
1444 utime (p
, &restore_times
);
1449 else if (S_ISCHR (current_stat
.st_mode
))
1454 else if (S_ISBLK (current_stat
.st_mode
))
1458 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK. */
1460 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
1461 else if (S_ISFIFO (current_stat
.st_mode
))
1466 else if (S_ISSOCK (current_stat
.st_mode
))
1473 if (archive_format
== V7_FORMAT
)
1476 current_stat
.st_size
= 0; /* force 0 size */
1477 header
= start_header (p
, ¤t_stat
);
1480 exit_status
= TAREXIT_FAILURE
;
1481 return; /* eg name too long */
1484 header
->header
.typeflag
= type
;
1486 #if defined(S_IFBLK) || defined(S_IFCHR)
1487 if (type
!= FIFOTYPE
)
1489 MAJOR_TO_OCT (major (current_stat
.st_rdev
), header
->header
.devmajor
);
1490 MINOR_TO_OCT (minor (current_stat
.st_rdev
), header
->header
.devminor
);
1494 finish_header (header
);
1495 if (remove_files_option
)
1497 if (unlink (p
) == -1)
1498 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1503 ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p
));