1 /* Create a tar archive.
2 Copyright 1985, 92, 93, 94, 96, 97, 1999 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 (with substitute SUBSTITUTE if VALUE is out of range) |
61 | into a size-SIZE field at WHERE, including a |
62 | trailing space. For example, 3 for SIZE means two digits and a space. |
64 | We assume the trailing NUL is already there and don't fill it in. This |
65 | fact is used by start_header and finish_header, so don't change it! |
66 `------------------------------------------------------------------------*/
68 /* This should be equivalent to: sprintf (WHERE, "%*lo", SIZE, VALUE);
69 except that we don't assume VALUE fits in an unsigned long, and
70 except that sprintf fills in the trailing NUL and we don't. */
73 to_oct (uintmax_t value
, uintmax_t substitute
, char *where
, size_t size
, const char *type
)
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. */
94 uintmax_t maxval
= (bits
< sizeof (uintmax_t) * CHAR_BIT
95 ? ((uintmax_t) 1 << bits
) - 1
97 char buf1
[UINTMAX_STRSIZE_BOUND
];
98 char buf2
[UINTMAX_STRSIZE_BOUND
];
99 char buf3
[UINTMAX_STRSIZE_BOUND
];
100 char *value_string
= STRINGIFY_BIGINT (value
, buf1
);
101 char *maxval_string
= STRINGIFY_BIGINT (maxval
, buf2
);
104 substitute
&= maxval
;
105 WARN ((0, 0, _("%s value %s too large (max=%s); substituting %s"),
106 type
, value_string
, maxval_string
,
107 STRINGIFY_BIGINT (substitute
, buf3
)));
108 to_oct (substitute
, (uintmax_t) 0, where
, size
, type
);
111 ERROR ((0, 0, _("%s value %s too large (max=%s)"),
112 type
, value_string
, maxval_string
));
119 gid_to_oct (gid_t v
, char *p
, size_t s
)
121 to_oct ((uintmax_t) v
, (uintmax_t) GID_NOBODY
, p
, s
, "gid_t");
124 major_to_oct (major_t v
, char *p
, size_t s
)
126 to_oct ((uintmax_t) v
, (uintmax_t) 0, p
, s
, "major_t");
129 minor_to_oct (minor_t v
, char *p
, size_t s
)
131 to_oct ((uintmax_t) v
, (uintmax_t) 0, p
, s
, "minor_t");
134 mode_to_oct (mode_t v
, char *p
, size_t s
)
136 to_oct ((uintmax_t) v
, (uintmax_t) 0, p
, s
, "mode_t");
139 off_to_oct (off_t v
, char *p
, size_t s
)
141 to_oct ((uintmax_t) v
, (uintmax_t) 0, p
, s
, "off_t");
144 size_to_oct (size_t v
, char *p
, size_t s
)
146 to_oct ((uintmax_t) v
, (uintmax_t) 0, p
, s
, "size_t");
149 time_to_oct (time_t v
, char *p
, size_t s
)
151 to_oct ((uintmax_t) v
, (uintmax_t) 0, p
, s
, "time_t");
157 uid_to_oct (uid_t v
, char *p
, size_t s
)
159 to_oct ((uintmax_t) v
, (uintmax_t) UID_NOBODY
, p
, s
, "uid_t");
162 uintmax_to_oct (uintmax_t v
, char *p
, size_t s
)
164 to_oct (v
, (uintmax_t) 0, p
, s
, "uintmax_t");
167 /* Writing routines. */
169 /*-----------------------------------------------------------------------.
170 | Just zeroes out the buffer so we don't confuse ourselves with leftover |
172 `-----------------------------------------------------------------------*/
175 clear_buffer (char *buffer
)
177 memset (buffer
, 0, BLOCKSIZE
);
180 /*-------------------------------------------------------------------------.
181 | Write the EOT block(s). We actually zero at least one block, through |
182 | the end of the record. Old tar, as previous versions of GNU tar, writes |
183 | garbage after two zeroed blocks. |
184 `-------------------------------------------------------------------------*/
189 union block
*pointer
= find_next_block ();
193 size_t space
= available_space_after (pointer
);
195 memset (pointer
->buffer
, 0, space
);
196 set_next_block_after (pointer
);
200 /*-----------------------------------------------------.
201 | Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. |
202 `-----------------------------------------------------*/
204 /* FIXME: Cross recursion between start_header and write_long! */
206 static union block
*start_header
PARAMS ((const char *, struct stat
*));
209 write_long (const char *p
, char type
)
211 size_t size
= strlen (p
) + 1;
216 memset (&foo
, 0, sizeof foo
);
219 header
= start_header ("././@LongLink", &foo
);
220 header
->header
.typeflag
= type
;
221 finish_header (header
);
223 header
= find_next_block ();
225 bufsize
= available_space_after (header
);
227 while (bufsize
< size
)
229 memcpy (header
->buffer
, p
, bufsize
);
232 set_next_block_after (header
+ (bufsize
- 1) / BLOCKSIZE
);
233 header
= find_next_block ();
234 bufsize
= available_space_after (header
);
236 memcpy (header
->buffer
, p
, size
);
237 memset (header
->buffer
+ size
, 0, bufsize
- size
);
238 set_next_block_after (header
+ (size
- 1) / BLOCKSIZE
);
241 /* Header handling. */
243 /*---------------------------------------------------------------------.
244 | Make a header block for the file name whose stat info is st. Return |
245 | header pointer for success, NULL if the name is too long. |
246 `---------------------------------------------------------------------*/
249 start_header (const char *name
, struct stat
*st
)
253 if (!absolute_names_option
)
255 static int warned_once
= 0;
264 WARN ((0, 0, _("Removing drive spec from names in the archive")));
271 name
++; /* force relative path */
276 Removing leading `/' from absolute path names in the archive")));
281 /* Check the file name and put it in the block. */
283 if (strlen (name
) >= (size_t) NAME_FIELD_SIZE
)
284 write_long (name
, GNUTYPE_LONGNAME
);
285 header
= find_next_block ();
286 memset (header
->buffer
, 0, sizeof (union block
));
288 assign_string (¤t_file_name
, name
);
290 strncpy (header
->header
.name
, name
, NAME_FIELD_SIZE
);
291 header
->header
.name
[NAME_FIELD_SIZE
- 1] = '\0';
293 /* Override some stat fields, if requested to do so. */
295 if (owner_option
!= (uid_t
) -1)
296 st
->st_uid
= owner_option
;
297 if (group_option
!= (gid_t
) -1)
298 st
->st_gid
= group_option
;
300 st
->st_mode
= ((st
->st_mode
& S_IFMT
)
301 | mode_adjust (st
->st_mode
, mode_option
));
303 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
304 for a few tars and came up with the following interoperability
308 1 2 3 4 5 6 7 8 9 READER
309 . . . . . . . . . 1 = SunOS 4.2 tar
310 # . . # # . . # # 2 = NEC SVR4.0.2 tar
311 . . . # # . . # . 3 = Solaris 2.1 tar
312 . . . . . . . . . 4 = GNU tar 1.11.1
313 . . . . . . . . . 5 = HP-UX 8.07 tar
314 . . . . . . . . . 6 = Ultrix 4.1
315 . . . . . . . . . 7 = AIX 3.2
316 . . . . . . . . . 8 = Hitachi HI-UX 1.03
317 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
320 # = ``impossible file type''
322 The following mask for old archive removes the `#'s in column 4
323 above, thus making GNU tar both a universal donor and a universal
324 acceptor for Paul's test. */
326 if (archive_format
== V7_FORMAT
)
327 MODE_TO_OCT (st
->st_mode
& 07777, header
->header
.mode
);
329 MODE_TO_OCT (st
->st_mode
, header
->header
.mode
);
331 UID_TO_OCT (st
->st_uid
, header
->header
.uid
);
332 GID_TO_OCT (st
->st_gid
, header
->header
.gid
);
333 OFF_TO_OCT (st
->st_size
, header
->header
.size
);
334 TIME_TO_OCT (st
->st_mtime
, header
->header
.mtime
);
336 if (incremental_option
)
337 if (archive_format
== OLDGNU_FORMAT
)
339 TIME_TO_OCT (st
->st_atime
, header
->oldgnu_header
.atime
);
340 TIME_TO_OCT (st
->st_ctime
, header
->oldgnu_header
.ctime
);
343 header
->header
.typeflag
= archive_format
== V7_FORMAT
? AREGTYPE
: REGTYPE
;
345 switch (archive_format
)
352 /* Overwrite header->header.magic and header.version in one blow. */
353 strcpy (header
->header
.magic
, OLDGNU_MAGIC
);
358 strncpy (header
->header
.magic
, TMAGIC
, TMAGLEN
);
359 strncpy (header
->header
.version
, TVERSION
, TVERSLEN
);
363 if (archive_format
== V7_FORMAT
|| numeric_owner_option
)
365 /* header->header.[ug]name are left as the empty string. */
369 uid_to_uname (st
->st_uid
, header
->header
.uname
);
370 gid_to_gname (st
->st_gid
, header
->header
.gname
);
376 /*-------------------------------------------------------------------------.
377 | Finish off a filled-in header block and write it out. We also print the |
378 | file name and/or full info if verbose is on. |
379 `-------------------------------------------------------------------------*/
382 finish_header (union block
*header
)
388 memcpy (header
->header
.chksum
, CHKBLANKS
, sizeof (header
->header
.chksum
));
392 for (i
= sizeof (*header
); i
-- != 0; )
393 /* We can't use unsigned char here because of old compilers, e.g. V7. */
396 /* Fill in the checksum field. It's formatted differently from the
397 other fields: it has [6] digits, a null, then a space -- rather than
398 digits, then a null. We use to_oct then write the null.
399 The final space is already there, from checksumming,
400 and to_oct doesn't modify it.
402 This is a fast way to do:
404 sprintf(header->header.chksum, "%6o", sum); */
406 uintmax_to_oct ((uintmax_t) sum
, header
->header
.chksum
, 6);
407 header
->header
.chksum
[6] = '\0';
409 set_next_block_after (header
);
412 && header
->header
.typeflag
!= GNUTYPE_LONGLINK
413 && header
->header
.typeflag
!= GNUTYPE_LONGNAME
)
415 /* These globals are parameters to print_header, sigh. */
417 current_header
= header
;
418 /* current_stat is already set up. */
419 current_format
= archive_format
;
424 /* Sparse file processing. */
426 /*-------------------------------------------------------------------------.
427 | Takes a blockful of data and basically cruises through it to see if it's |
428 | made *entirely* of zeros, returning a 0 the instant it finds something |
429 | that is a nonzero, i.e., useful data. |
430 `-------------------------------------------------------------------------*/
433 zero_block_p (char *buffer
)
437 for (counter
= 0; counter
< BLOCKSIZE
; counter
++)
438 if (buffer
[counter
] != '\0')
448 init_sparsearray (void)
454 /* Make room for our scratch space -- initially is 10 elts long. */
456 sparsearray
= (struct sp_array
*)
457 xmalloc (sp_array_size
* sizeof (struct sp_array
));
458 for (counter
= 0; counter
< sp_array_size
; counter
++)
460 sparsearray
[counter
].offset
= 0;
461 sparsearray
[counter
].numbytes
= 0;
470 find_new_file_size (off_t
*filesize
, int highest_index
)
476 sparsearray
[counter
].numbytes
&& counter
<= highest_index
;
478 *filesize
+= sparsearray
[counter
].numbytes
;
481 /*-----------------------------------------------------------------------.
482 | Make one pass over the file NAME, studying where any non-zero data is, |
483 | that is, how far into the file each instance of data is, and how many |
484 | bytes are there. Save this information in the sparsearray, which will |
485 | later be translated into header information. |
486 `-----------------------------------------------------------------------*/
488 /* There is little point in trimming small amounts of null data at the head
489 and tail of blocks, only avoid dumping full null blocks. */
491 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
492 too kludgey for my taste... */
495 deal_with_sparse (char *name
, union block
*header
)
500 int sparse_index
= 0;
502 char buffer
[BLOCKSIZE
];
504 if (archive_format
== OLDGNU_FORMAT
)
505 header
->oldgnu_header
.isextended
= 0;
507 if (file
= open (name
, O_RDONLY
), file
< 0)
508 /* This problem will be caught later on, so just return. */
512 clear_buffer (buffer
);
514 while (count
= read (file
, buffer
, sizeof buffer
), count
!= 0)
516 /* Realloc the scratch area as necessary. FIXME: should reallocate
517 only at beginning of a new instance of non-zero data. */
519 if (sparse_index
> sp_array_size
- 1)
522 sparsearray
= (struct sp_array
*)
523 xrealloc (sparsearray
,
524 2 * sp_array_size
* sizeof (struct sp_array
));
528 /* Process one block. */
530 if (count
== sizeof buffer
)
532 if (zero_block_p (buffer
))
536 sparsearray
[sparse_index
++].numbytes
= numbytes
;
543 sparsearray
[sparse_index
].offset
= offset
;
549 /* Since count < sizeof buffer, we have the last bit of the file. */
551 if (!zero_block_p (buffer
))
554 sparsearray
[sparse_index
].offset
= offset
;
558 /* The next two lines are suggested by Andreas Degert, who says
559 they are required for trailing full blocks to be written to the
560 archive, when all zeroed. Yet, it seems to me that the case
561 does not apply. Further, at restore time, the file is not as
562 sparse as it should. So, some serious cleanup is *also* needed
563 in this area. Just one more... :-(. FIXME. */
567 /* Prepare for next block. */
570 /* FIXME: do not clear unless necessary. */
571 clear_buffer (buffer
);
575 sparsearray
[sparse_index
++].numbytes
= numbytes
;
578 sparsearray
[sparse_index
].offset
= offset
- 1;
579 sparsearray
[sparse_index
++].numbytes
= 1;
583 return sparse_index
- 1;
591 finish_sparse_file (int file
, off_t
*sizeleft
, off_t fullsize
, char *name
)
595 int sparse_index
= 0;
598 while (*sizeleft
> 0)
600 start
= find_next_block ();
601 memset (start
->buffer
, 0, BLOCKSIZE
);
602 bufsize
= sparsearray
[sparse_index
].numbytes
;
605 /* We blew it, maybe. */
606 char buf1
[UINTMAX_STRSIZE_BOUND
];
607 char buf2
[UINTMAX_STRSIZE_BOUND
];
609 ERROR ((0, 0, _("Wrote %s of %s bytes to file %s"),
610 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf1
),
611 STRINGIFY_BIGINT (fullsize
, buf2
),
616 if (lseek (file
, sparsearray
[sparse_index
++].offset
, 0) < 0)
618 char buf
[UINTMAX_STRSIZE_BOUND
];
619 ERROR ((0, errno
, _("lseek error at byte %s in file %s"),
620 STRINGIFY_BIGINT (sparsearray
[sparse_index
- 1].offset
, buf
),
625 /* If the number of bytes to be written here exceeds the size of
626 the temporary buffer, do it in steps. */
628 while (bufsize
> BLOCKSIZE
)
633 count
= read (file
, start
->buffer
+ amount_read
,
634 BLOCKSIZE
- amount_read
);
635 bufsize
-= BLOCKSIZE
- amount_read
;
637 set_next_block_after (start
);
638 start
= find_next_block ();
639 memset (start
->buffer
, 0, BLOCKSIZE
);
642 /* Store the data. */
644 count
= read (file
, start
->buffer
, BLOCKSIZE
);
647 char buf
[UINTMAX_STRSIZE_BOUND
];
648 ERROR ((0, errno
, _("\
649 Read error at byte %s, reading %lu bytes, in file %s"),
650 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
651 (unsigned long) bufsize
, name
));
656 set_next_block_after (start
);
657 start
= find_next_block ();
658 memset (start
->buffer
, 0, BLOCKSIZE
);
662 char buffer
[BLOCKSIZE
];
664 clear_buffer (buffer
);
665 count
= read (file
, buffer
, bufsize
);
666 memcpy (start
->buffer
, buffer
, BLOCKSIZE
);
671 char buf
[UINTMAX_STRSIZE_BOUND
];
674 _("Read error at byte %s, reading %lu bytes, in file %s"),
675 STRINGIFY_BIGINT (fullsize
- *sizeleft
, buf
),
676 (unsigned long) bufsize
, name
));
680 if (amount_read
>= BLOCKSIZE
)
683 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
684 if (count
!= bufsize
)
687 _("File %s shrunk, padding with zeros"),
691 start
= find_next_block ();
694 amount_read
+= bufsize
;
697 set_next_block_after (start
);
702 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
707 /* Main functions of this module. */
714 create_archive (void)
718 open_archive (ACCESS_WRITE
);
720 if (incremental_option
)
722 char *buffer
= xmalloc (PATH_MAX
);
726 collect_and_sort_names ();
728 while (p
= name_from_list (), p
)
729 dump_file (p
, (dev_t
) -1, 1);
732 while (p
= name_from_list (), p
)
735 if (p
[strlen (p
) - 1] != '/')
736 strcat (buffer
, "/");
737 bufp
= buffer
+ strlen (buffer
);
738 for (q
= gnu_list_name
->dir_contents
;
744 strcpy (bufp
, q
+ 1);
745 dump_file (buffer
, (dev_t
) -1, 1);
753 while (p
= name_next (1), p
)
754 dump_file (p
, (dev_t
) -1, 1);
760 if (listed_incremental_option
)
764 /*----------------------------------------------------------------------.
765 | Dump a single file. Recurse on directories. Result is nonzero for |
766 | success. P is file name to dump. PARENT_DEVICE is device our parent |
767 | directory was on. TOP_LEVEL tells wether we are a toplevel call. |
769 | Sets global CURRENT_STAT to stat output for this file. |
770 `----------------------------------------------------------------------*/
772 /* FIXME: One should make sure that for *every* path leading to setting
773 exit_status to failure, a clear diagnostic has been issued. */
776 dump_file (char *p
, dev_t parent_device
, int top_level
)
782 struct utimbuf restore_times
;
785 /* FIXME: `header' and `upperbound' might be used uninitialized in this
786 function. Reported by Bruno Haible. */
788 if (interactive_option
&& !confirm ("add", p
))
791 /* Use stat if following (rather than dumping) 4.2BSD's symbolic links.
792 Otherwise, use lstat (which falls back to stat if no symbolic links). */
794 if (dereference_option
!= 0
795 #if STX_HIDDEN && !_LARGE_FILES /* AIX */
796 ? statx (p
, ¤t_stat
, STATSIZE
, STX_HIDDEN
)
797 : statx (p
, ¤t_stat
, STATSIZE
, STX_HIDDEN
| STX_LINK
)
799 ? stat (p
, ¤t_stat
) : lstat (p
, ¤t_stat
)
803 WARN ((0, errno
, _("Cannot add file %s"), p
));
804 if (!ignore_failed_read_option
)
805 exit_status
= TAREXIT_FAILURE
;
809 restore_times
.actime
= current_stat
.st_atime
;
810 restore_times
.modtime
= current_stat
.st_mtime
;
811 restore_size
= current_stat
.st_size
;
814 if (S_ISHIDDEN (current_stat
.st_mode
))
816 char *new = (char *) alloca (strlen (p
) + 2);
826 /* See if we only want new files, and check if this one is too old to
827 put in the archive. */
829 if (!incremental_option
&& !S_ISDIR (current_stat
.st_mode
)
830 && current_stat
.st_mtime
< newer_mtime_option
831 && (!after_date_option
|| current_stat
.st_ctime
< newer_ctime_option
))
833 if (parent_device
== (dev_t
) -1)
834 WARN ((0, 0, _("%s: is unchanged; not dumped"), p
));
835 /* FIXME: recheck this return. */
840 /* See if we are trying to dump the archive. */
842 if (ar_dev
&& current_stat
.st_dev
== ar_dev
&& current_stat
.st_ino
== ar_ino
)
844 WARN ((0, 0, _("%s is the archive; not dumped"), p
));
849 /* Check for multiple links.
851 We maintain a list of all such files that we've written so far. Any
852 time we see another, we check the list and avoid dumping the data
853 again if we've done it once already. */
855 if (current_stat
.st_nlink
> 1
856 && (S_ISREG (current_stat
.st_mode
)
858 || S_ISCTG (current_stat
.st_mode
)
861 || S_ISCHR (current_stat
.st_mode
)
864 || S_ISBLK (current_stat
.st_mode
)
867 || S_ISFIFO (current_stat
.st_mode
)
873 /* FIXME: First quick and dirty. Hashing, etc later. */
875 for (lp
= linklist
; lp
; lp
= lp
->next
)
876 if (lp
->ino
== current_stat
.st_ino
&& lp
->dev
== current_stat
.st_dev
)
878 char *link_name
= lp
->name
;
880 /* We found a link. */
882 while (!absolute_names_option
&& *link_name
== '/')
884 static int warned_once
= 0;
890 Removing leading `/' from absolute links")));
894 if (strlen (link_name
) >= NAME_FIELD_SIZE
)
895 write_long (link_name
, GNUTYPE_LONGLINK
);
896 assign_string (¤t_link_name
, link_name
);
898 current_stat
.st_size
= 0;
899 header
= start_header (p
, ¤t_stat
);
902 exit_status
= TAREXIT_FAILURE
;
905 strncpy (header
->header
.linkname
,
906 link_name
, NAME_FIELD_SIZE
);
908 /* Force null truncated. */
910 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = 0;
912 header
->header
.typeflag
= LNKTYPE
;
913 finish_header (header
);
915 /* FIXME: Maybe remove from list after all links found? */
917 if (remove_files_option
)
918 if (unlink (p
) == -1)
919 ERROR ((0, errno
, _("Cannot remove %s"), p
));
925 /* Not found. Add it to the list of possible links. */
928 xmalloc ((size_t) (sizeof (struct link
) + strlen (p
)));
929 lp
->ino
= current_stat
.st_ino
;
930 lp
->dev
= current_stat
.st_dev
;
931 strcpy (lp
->name
, p
);
936 /* This is not a link to a previously dumped file, so dump it. */
938 if (S_ISREG (current_stat
.st_mode
)
940 || S_ISCTG (current_stat
.st_mode
)
944 int f
; /* file descriptor */
953 static int cried_once
= 0;
960 /* Check the size of the file against the number of blocks
961 allocated for it, counting both data and indirect blocks.
962 If there is a smaller number of blocks that would be
963 necessary to accommodate a file of this size, this is safe
964 to say that we have a sparse file: at least one of those
965 blocks in the file is just a useless hole. For sparse
966 files not having more hole blocks than indirect blocks, the
967 sparseness will go undetected. */
969 /* Bruno Haible sent me these statistics for Linux. It seems
970 that some filesystems count indirect blocks in st_blocks,
971 while others do not seem to:
973 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
974 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
975 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
976 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
978 Dick Streefland reports the previous numbers as misleading,
979 because ext2fs use 12 direct blocks, while minix-fs uses only
980 6 direct blocks. Dick gets:
982 ext2 size=20480 ls listed blocks=21
983 minix size=20480 ls listed blocks=21
984 msdos size=20480 ls listed blocks=20
986 It seems that indirect blocks *are* included in st_blocks.
987 The minix filesystem does not account for phantom blocks in
988 st_blocks, so `du' and `ls -s' give wrong results. So, the
989 --sparse option would not work on a minix filesystem. */
991 if (ST_NBLOCKS (current_stat
)
992 < (current_stat
.st_size
/ ST_NBLOCKSIZE
993 + (current_stat
.st_size
% ST_NBLOCKSIZE
!= 0)))
995 off_t filesize
= current_stat
.st_size
;
998 header
= start_header (p
, ¤t_stat
);
1001 exit_status
= TAREXIT_FAILURE
;
1004 header
->header
.typeflag
= GNUTYPE_SPARSE
;
1007 /* Call the routine that figures out the layout of the
1008 sparse file in question. UPPERBOUND is the index of the
1009 last element of the "sparsearray," i.e., the number of
1010 elements it needed to describe the file. */
1012 upperbound
= deal_with_sparse (p
, header
);
1014 /* See if we'll need an extended header later. */
1016 if (upperbound
> SPARSES_IN_OLDGNU_HEADER
- 1)
1017 header
->oldgnu_header
.isextended
= 1;
1019 /* We store the "real" file size so we can show that in
1020 case someone wants to list the archive, i.e., tar tvf
1021 <file>. It might be kind of disconcerting if the
1022 shrunken file size was the one that showed up. */
1024 OFF_TO_OCT (current_stat
.st_size
,
1025 header
->oldgnu_header
.realsize
);
1027 /* This will be the new "size" of the file, i.e., the size
1028 of the file minus the blocks of holes that we're
1031 find_new_file_size (&filesize
, upperbound
);
1032 current_stat
.st_size
= filesize
;
1033 OFF_TO_OCT (filesize
, header
->header
.size
);
1035 for (counter
= 0; counter
< SPARSES_IN_OLDGNU_HEADER
; counter
++)
1037 if (!sparsearray
[counter
].numbytes
)
1040 OFF_TO_OCT (sparsearray
[counter
].offset
,
1041 header
->oldgnu_header
.sp
[counter
].offset
);
1042 SIZE_TO_OCT (sparsearray
[counter
].numbytes
,
1043 header
->oldgnu_header
.sp
[counter
].numbytes
);
1049 upperbound
= SPARSES_IN_OLDGNU_HEADER
- 1;
1051 sizeleft
= current_stat
.st_size
;
1053 /* Don't bother opening empty, world readable files. Also do not open
1054 files when archive is meant for /dev/null. */
1057 || (sizeleft
== 0 && 0444 == (0444 & current_stat
.st_mode
)))
1061 f
= open (p
, O_RDONLY
| O_BINARY
);
1064 WARN ((0, errno
, _("Cannot add file %s"), p
));
1065 if (!ignore_failed_read_option
)
1066 exit_status
= TAREXIT_FAILURE
;
1071 /* If the file is sparse, we've already taken care of this. */
1075 header
= start_header (p
, ¤t_stat
);
1080 exit_status
= TAREXIT_FAILURE
;
1085 /* Mark contiguous files, if we support them. */
1087 if (archive_format
!= V7_FORMAT
&& S_ISCTG (current_stat
.st_mode
))
1088 header
->header
.typeflag
= CONTTYPE
;
1090 isextended
= header
->oldgnu_header
.isextended
;
1091 save_typeflag
= header
->header
.typeflag
;
1092 finish_header (header
);
1101 int arraybound
= SPARSES_IN_SPARSE_HEADER
;
1103 /* static */ int index_offset
= SPARSES_IN_OLDGNU_HEADER
;
1106 exhdr
= find_next_block ();
1110 exit_status
= TAREXIT_FAILURE
;
1113 memset (exhdr
->buffer
, 0, BLOCKSIZE
);
1114 for (counter
= 0; counter
< SPARSES_IN_SPARSE_HEADER
; counter
++)
1116 if (counter
+ index_offset
> upperbound
)
1119 SIZE_TO_OCT (sparsearray
[counter
+ index_offset
].numbytes
,
1120 exhdr
->sparse_header
.sp
[counter
].numbytes
);
1121 OFF_TO_OCT (sparsearray
[counter
+ index_offset
].offset
,
1122 exhdr
->sparse_header
.sp
[counter
].offset
);
1124 set_next_block_after (exhdr
);
1127 if (sum
< upperbound
)
1130 if (index_offset
+ counter
<= upperbound
)
1132 index_offset
+= counter
;
1133 exhdr
->sparse_header
.isextended
= 1;
1138 if (save_typeflag
== GNUTYPE_SPARSE
)
1140 if (finish_sparse_file (f
, &sizeleft
, current_stat
.st_size
, p
))
1144 while (sizeleft
> 0)
1146 if (multi_volume_option
)
1148 assign_string (&save_name
, p
);
1149 save_sizeleft
= sizeleft
;
1150 save_totsize
= current_stat
.st_size
;
1152 start
= find_next_block ();
1154 bufsize
= available_space_after (start
);
1156 if (sizeleft
< bufsize
)
1158 /* Last read -- zero out area beyond. */
1161 count
= bufsize
% BLOCKSIZE
;
1163 memset (start
->buffer
+ sizeleft
, 0,
1164 (size_t) (BLOCKSIZE
- count
));
1169 count
= read (f
, start
->buffer
, bufsize
);
1172 char buf
[UINTMAX_STRSIZE_BOUND
];
1173 ERROR ((0, errno
, _("\
1174 Read error at byte %s, reading %lu bytes, in file %s"),
1175 STRINGIFY_BIGINT (current_stat
.st_size
- sizeleft
,
1177 (unsigned long) bufsize
, p
));
1182 /* This is nonportable (the type of set_next_block_after's arg). */
1184 set_next_block_after (start
+ (count
- 1) / BLOCKSIZE
);
1186 if (count
== bufsize
)
1190 char buf
[UINTMAX_STRSIZE_BOUND
];
1192 _("File %s shrunk by %s bytes, padding with zeros"),
1193 p
, STRINGIFY_BIGINT (sizeleft
, buf
)));
1194 goto padit
; /* short read */
1198 if (multi_volume_option
)
1199 assign_string (&save_name
, NULL
);
1203 struct stat final_stat
;
1204 if (fstat (f
, &final_stat
) != 0)
1205 ERROR ((0, errno
, "%s: fstat", p
));
1206 else if (final_stat
.st_mtime
!= restore_times
.modtime
1207 || final_stat
.st_size
!= restore_size
)
1208 ERROR ((0, errno
, _("%s: file changed as we read it"), p
));
1210 ERROR ((0, errno
, _("%s: close"), p
));
1211 if (atime_preserve_option
)
1212 utime (p
, &restore_times
);
1214 if (remove_files_option
)
1216 if (unlink (p
) == -1)
1217 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1221 /* File shrunk or gave error, pad out tape to match the size we
1222 specified in the header. */
1225 while (sizeleft
> 0)
1227 save_sizeleft
= sizeleft
;
1228 start
= find_next_block ();
1229 memset (start
->buffer
, 0, BLOCKSIZE
);
1230 set_next_block_after (start
);
1231 sizeleft
-= BLOCKSIZE
;
1233 if (multi_volume_option
)
1234 assign_string (&save_name
, NULL
);
1238 if (atime_preserve_option
)
1239 utime (p
, &restore_times
);
1245 else if (S_ISLNK (current_stat
.st_mode
))
1248 char *buffer
= (char *) alloca (PATH_MAX
+ 1);
1250 size
= readlink (p
, buffer
, PATH_MAX
+ 1);
1253 WARN ((0, errno
, _("Cannot add file %s"), p
));
1254 if (!ignore_failed_read_option
)
1255 exit_status
= TAREXIT_FAILURE
;
1258 buffer
[size
] = '\0';
1259 if (size
>= NAME_FIELD_SIZE
)
1260 write_long (buffer
, GNUTYPE_LONGLINK
);
1261 assign_string (¤t_link_name
, buffer
);
1263 current_stat
.st_size
= 0; /* force 0 size on symlink */
1264 header
= start_header (p
, ¤t_stat
);
1267 exit_status
= TAREXIT_FAILURE
;
1270 strncpy (header
->header
.linkname
, buffer
, NAME_FIELD_SIZE
);
1271 header
->header
.linkname
[NAME_FIELD_SIZE
- 1] = '\0';
1272 header
->header
.typeflag
= SYMTYPE
;
1273 finish_header (header
); /* nothing more to do to it */
1274 if (remove_files_option
)
1276 if (unlink (p
) == -1)
1277 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1281 #endif /* S_ISLNK */
1283 else if (S_ISDIR (current_stat
.st_mode
))
1286 struct dirent
*entry
;
1290 dev_t our_device
= current_stat
.st_dev
;
1292 /* If this tar program is installed suid root, like for Amanda, the
1293 access might look like denied, while it is not really.
1295 FIXME: I have the feeling this test is done too early. Couldn't it
1296 just be bundled in later actions? I guess that the proper support
1297 of --ignore-failed-read is the key of the current writing. */
1299 if (access (p
, R_OK
) == -1 && geteuid () != 0)
1301 WARN ((0, errno
, _("Cannot add directory %s"), p
));
1302 if (!ignore_failed_read_option
)
1303 exit_status
= TAREXIT_FAILURE
;
1307 /* Build new prototype name. Ensure exactly one trailing slash. */
1310 buflen
= len
+ NAME_FIELD_SIZE
;
1311 namebuf
= xmalloc (buflen
+ 1);
1312 strncpy (namebuf
, p
, buflen
);
1313 while (len
>= 1 && namebuf
[len
- 1] == '/')
1315 namebuf
[len
++] = '/';
1316 namebuf
[len
] = '\0';
1320 /* The "1" above used to be "archive_format != V7_FORMAT", GNU tar
1321 was just not writing directory blocks at all. Daniel Trinkle
1322 writes: ``All old versions of tar I have ever seen have
1323 correctly archived an empty directory. The really old ones I
1324 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1325 some subtle reason for the exclusion that I don't know, but the
1326 current behavior is broken.'' I do not know those subtle
1327 reasons either, so until these are reported (anew?), just allow
1328 directory blocks to be written even with old archives. */
1330 current_stat
.st_size
= 0; /* force 0 size on dir */
1332 /* FIXME: If people could really read standard archives, this
1336 = start_header (standard_option ? p : namebuf, ¤t_stat);
1338 but since they'd interpret DIRTYPE blocks as regular
1339 files, we'd better put the / on the name. */
1341 header
= start_header (namebuf
, ¤t_stat
);
1344 exit_status
= TAREXIT_FAILURE
;
1345 return; /* eg name too long */
1348 if (incremental_option
)
1349 header
->header
.typeflag
= GNUTYPE_DUMPDIR
;
1350 else /* if (standard_option) */
1351 header
->header
.typeflag
= DIRTYPE
;
1353 /* If we're gnudumping, we aren't done yet so don't close it. */
1355 if (!incremental_option
)
1356 finish_header (header
); /* done with directory header */
1359 if (incremental_option
&& gnu_list_name
->dir_contents
)
1366 const char *buffer
, *p_buffer
;
1368 buffer
= gnu_list_name
->dir_contents
; /* FOO */
1370 for (p_buffer
= buffer
; p_buffer
&& *p_buffer
;)
1374 tmp
= strlen (p_buffer
) + 1;
1379 OFF_TO_OCT (totsize
, header
->header
.size
);
1380 finish_header (header
);
1383 while (sizeleft
> 0)
1385 if (multi_volume_option
)
1387 assign_string (&save_name
, p
);
1388 save_sizeleft
= sizeleft
;
1389 save_totsize
= totsize
;
1391 start
= find_next_block ();
1392 bufsize
= available_space_after (start
);
1393 if (sizeleft
< bufsize
)
1396 count
= bufsize
% BLOCKSIZE
;
1398 memset (start
->buffer
+ sizeleft
, 0,
1399 (size_t) (BLOCKSIZE
- count
));
1401 memcpy (start
->buffer
, p_buffer
, bufsize
);
1402 sizeleft
-= bufsize
;
1403 p_buffer
+= bufsize
;
1404 set_next_block_after (start
+ (bufsize
- 1) / BLOCKSIZE
);
1406 if (multi_volume_option
)
1407 assign_string (&save_name
, NULL
);
1408 if (atime_preserve_option
)
1409 utime (p
, &restore_times
);
1413 /* See if we are about to recurse into a directory, and avoid doing
1414 so if the user wants that we do not descend into directories. */
1416 if (no_recurse_option
)
1419 /* See if we are crossing from one file system to another, and
1420 avoid doing so if the user only wants to dump one file system. */
1422 if (one_file_system_option
&& !top_level
1423 && parent_device
!= current_stat
.st_dev
)
1426 WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p
));
1430 /* Now output all the files in the directory. */
1432 errno
= 0; /* FIXME: errno should be read-only */
1434 directory
= opendir (p
);
1437 ERROR ((0, errno
, _("Cannot open directory %s"), p
));
1441 /* Hack to remove "./" from the front of all the file names. */
1443 if (len
== 2 && namebuf
[0] == '.' && namebuf
[1] == '/')
1446 /* FIXME: Should speed this up by cd-ing into the dir. */
1448 while (entry
= readdir (directory
), entry
)
1450 /* Skip `.' and `..'. */
1452 if (is_dot_or_dotdot (entry
->d_name
))
1455 if ((int) NAMLEN (entry
) + len
>= buflen
)
1457 buflen
= len
+ NAMLEN (entry
);
1458 namebuf
= (char *) xrealloc (namebuf
, buflen
+ 1);
1460 namebuf
[len
] = '\0';
1461 ERROR ((0, 0, _("File name %s%s too long"),
1462 namebuf
, entry
->d_name
));
1466 strcpy (namebuf
+ len
, entry
->d_name
);
1467 if (exclude_option
&& check_exclude (namebuf
))
1469 dump_file (namebuf
, our_device
, 0);
1472 closedir (directory
);
1474 if (atime_preserve_option
)
1475 utime (p
, &restore_times
);
1480 else if (S_ISCHR (current_stat
.st_mode
))
1485 else if (S_ISBLK (current_stat
.st_mode
))
1489 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK. */
1491 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
1492 else if (S_ISFIFO (current_stat
.st_mode
))
1497 else if (S_ISSOCK (current_stat
.st_mode
))
1504 if (archive_format
== V7_FORMAT
)
1507 current_stat
.st_size
= 0; /* force 0 size */
1508 header
= start_header (p
, ¤t_stat
);
1511 exit_status
= TAREXIT_FAILURE
;
1512 return; /* eg name too long */
1515 header
->header
.typeflag
= type
;
1517 #if defined(S_IFBLK) || defined(S_IFCHR)
1518 if (type
!= FIFOTYPE
)
1520 MAJOR_TO_OCT (major (current_stat
.st_rdev
), header
->header
.devmajor
);
1521 MINOR_TO_OCT (minor (current_stat
.st_rdev
), header
->header
.devminor
);
1525 finish_header (header
);
1526 if (remove_files_option
)
1528 if (unlink (p
) == -1)
1529 ERROR ((0, errno
, _("Cannot remove %s"), p
));
1534 ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p
));