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 | Converts long VALUE into a DIGS-digit field at WHERE, including a |
61 | trailing space and room for a NUL. For example, 3 for DIGS 3 means one |
62 | digit, a space, and room for a NUL. |
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 ", DIGS - 2, VALUE);
69 except that sprintf fills in the trailing NUL and we don't. */
72 to_oct (long value, int digs, char *where)
74 --digs; /* Trailing null slot is left alone */
75 where[--digs] = ' '; /* put in the space, though */
77 /* Produce the digits -- at least one. */
81 where[--digs] = '0' + (char) (value & 7); /* one octal digit */
84 while (digs > 0 && value != 0);
86 /* Leading spaces, if necessary. */
91 /* Writing routines. */
93 /*-----------------------------------------------------------------------.
94 | Just zeroes out the buffer so we don't confuse ourselves with leftover |
96 `-----------------------------------------------------------------------*/
99 clear_buffer (char *buffer)
101 memset (buffer, 0, BLOCKSIZE);
104 /*-------------------------------------------------------------------------.
105 | Write the EOT block(s). We actually zero at least one block, through |
106 | the end of the record. Old tar, as previous versions of GNU tar, writes |
107 | garbage after two zeroed blocks. |
108 `-------------------------------------------------------------------------*/
113 union block *pointer = find_next_block ();
117 int space = available_space_after (pointer);
119 memset (pointer->buffer, 0, (size_t) space);
120 set_next_block_after (pointer);
124 /*-----------------------------------------------------.
125 | Write a GNUTYPE_LONGLINK or GNUTYPE_LONGNAME block. |
126 `-----------------------------------------------------*/
128 /* FIXME: Cross recursion between start_header and write_long! */
130 static union block *start_header PARAMS ((const char *, struct stat *));
133 write_long (const char *p, char type)
135 int size = strlen (p) + 1;
140 memset (&foo, 0, sizeof foo);
143 header = start_header ("././@LongLink", &foo);
144 header->header.typeflag = type;
145 finish_header (header);
147 header = find_next_block ();
149 bufsize = available_space_after (header);
151 while (bufsize < size)
153 memcpy (header->buffer, p, (size_t) bufsize);
156 set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
157 header = find_next_block ();
158 bufsize = available_space_after (header);
160 memcpy (header->buffer, p, (size_t) size);
161 memset (header->buffer + size, 0, (size_t) (bufsize - size));
162 set_next_block_after (header + (size - 1) / BLOCKSIZE);
165 /* Header handling. */
167 /*---------------------------------------------------------------------.
168 | Make a header block for the file name whose stat info is st. Return |
169 | header pointer for success, NULL if the name is too long. |
170 `---------------------------------------------------------------------*/
173 start_header (const char *name, struct stat *st)
177 if (!absolute_names_option)
179 static int warned_once = 0;
188 WARN ((0, 0, _("Removing drive spec from names in the archive")));
195 name++; /* force relative path */
200 Removing leading `/' from absolute path names in the archive")));
205 /* Check the file name and put it in the block. */
207 if (strlen (name) >= (size_t) NAME_FIELD_SIZE)
208 write_long (name, GNUTYPE_LONGNAME);
209 header = find_next_block ();
210 memset (header->buffer, 0, sizeof (union block));
212 assign_string (¤t_file_name, name);
214 strncpy (header->header.name, name, NAME_FIELD_SIZE);
215 header->header.name[NAME_FIELD_SIZE - 1] = '\0';
217 /* Override some stat fields, if requested to do so. */
219 if (owner_option != (uid_t) -1)
220 st->st_uid = owner_option;
221 if (group_option != (gid_t) -1)
222 st->st_gid = group_option;
224 st->st_mode = ((st->st_mode & S_IFMT)
225 | mode_adjust (st->st_mode, mode_option));
227 /* Paul Eggert tried the trivial test ($WRITER cf a b; $READER tvf a)
228 for a few tars and came up with the following interoperability
232 1 2 3 4 5 6 7 8 9 READER
233 . . . . . . . . . 1 = SunOS 4.2 tar
234 # . . # # . . # # 2 = NEC SVR4.0.2 tar
235 . . . # # . . # . 3 = Solaris 2.1 tar
236 . . . . . . . . . 4 = GNU tar 1.11.1
237 . . . . . . . . . 5 = HP-UX 8.07 tar
238 . . . . . . . . . 6 = Ultrix 4.1
239 . . . . . . . . . 7 = AIX 3.2
240 . . . . . . . . . 8 = Hitachi HI-UX 1.03
241 . . . . . . . . . 9 = Omron UNIOS-B 4.3BSD 1.60Beta
244 # = ``impossible file type''
246 The following mask for old archive removes the `#'s in column 4
247 above, thus making GNU tar both a universal donor and a universal
248 acceptor for Paul's test. */
250 if (archive_format == V7_FORMAT)
251 to_oct ((long) st->st_mode & 07777, 8, header->header.mode);
253 to_oct ((long) st->st_mode, 8, header->header.mode);
255 to_oct ((long) st->st_uid, 8, header->header.uid);
256 to_oct ((long) st->st_gid, 8, header->header.gid);
257 to_oct ((long) st->st_size, 1 + 12, header->header.size);
258 to_oct ((long) st->st_mtime, 1 + 12, header->header.mtime);
260 if (incremental_option)
261 if (archive_format == OLDGNU_FORMAT)
263 to_oct ((long) st->st_atime, 1 + 12, header->oldgnu_header.atime);
264 to_oct ((long) st->st_ctime, 1 + 12, header->oldgnu_header.ctime);
267 header->header.typeflag = archive_format == V7_FORMAT ? AREGTYPE : REGTYPE;
269 switch (archive_format)
276 /* Overwrite header->header.magic and header.version in one blow. */
277 strcpy (header->header.magic, OLDGNU_MAGIC);
282 strncpy (header->header.magic, TMAGIC, TMAGLEN);
283 strncpy (header->header.version, TVERSION, TVERSLEN);
287 if (archive_format == V7_FORMAT || numeric_owner_option)
289 /* header->header.[ug]name are left as the empty string. */
293 uid_to_uname (st->st_uid, header->header.uname);
294 gid_to_gname (st->st_gid, header->header.gname);
300 /*-------------------------------------------------------------------------.
301 | Finish off a filled-in header block and write it out. We also print the |
302 | file name and/or full info if verbose is on. |
303 `-------------------------------------------------------------------------*/
306 finish_header (union block *header)
311 memcpy (header->header.chksum, CHKBLANKS, sizeof (header->header.chksum));
315 for (i = sizeof (*header); --i >= 0; )
316 /* We can't use unsigned char here because of old compilers, e.g. V7. */
319 /* Fill in the checksum field. It's formatted differently from the
320 other fields: it has [6] digits, a null, then a space -- rather than
321 digits, a space, then a null. We use to_oct then write the null in
322 over to_oct's space. The final space is already there, from
323 checksumming, and to_oct doesn't modify it.
325 This is a fast way to do:
327 sprintf(header->header.chksum, "%6o", sum); */
329 to_oct ((long) sum, 8, header->header.chksum);
330 header->header.chksum[6] = '\0'; /* zap the space */
332 set_next_block_after (header);
335 && header->header.typeflag != GNUTYPE_LONGLINK
336 && header->header.typeflag != GNUTYPE_LONGNAME)
338 /* These globals are parameters to print_header, sigh. */
340 current_header = header;
341 /* current_stat is already set up. */
342 current_format = archive_format;
347 /* Sparse file processing. */
349 /*-------------------------------------------------------------------------.
350 | Takes a blockful of data and basically cruises through it to see if it's |
351 | made *entirely* of zeros, returning a 0 the instant it finds something |
352 | that is a nonzero, i.e., useful data. |
353 `-------------------------------------------------------------------------*/
356 zero_block_p (char *buffer)
360 for (counter = 0; counter < BLOCKSIZE; counter++)
361 if (buffer[counter] != '\0')
371 init_sparsearray (void)
377 /* Make room for our scratch space -- initially is 10 elts long. */
379 sparsearray = (struct sp_array *)
380 xmalloc (sp_array_size * sizeof (struct sp_array));
381 for (counter = 0; counter < sp_array_size; counter++)
383 sparsearray[counter].offset = 0;
384 sparsearray[counter].numbytes = 0;
393 find_new_file_size (int *filesize, int highest_index)
399 sparsearray[counter].numbytes && counter <= highest_index;
401 *filesize += sparsearray[counter].numbytes;
404 /*-----------------------------------------------------------------------.
405 | Make one pass over the file NAME, studying where any non-zero data is, |
406 | that is, how far into the file each instance of data is, and how many |
407 | bytes are there. Save this information in the sparsearray, which will |
408 | later be translated into header information. |
409 `-----------------------------------------------------------------------*/
411 /* There is little point in trimming small amounts of null data at the head
412 and tail of blocks, only avoid dumping full null blocks. */
414 /* FIXME: this routine might accept bits of algorithmic cleanup, it is
415 too kludgey for my taste... */
418 deal_with_sparse (char *name, union block *header)
423 int sparse_index = 0;
425 char buffer[BLOCKSIZE];
427 if (archive_format == OLDGNU_FORMAT)
428 header->oldgnu_header.isextended = 0;
430 if (file = open (name, O_RDONLY), file < 0)
431 /* This problem will be caught later on, so just return. */
435 clear_buffer (buffer);
437 while (count = read (file, buffer, sizeof buffer), count != 0)
439 /* Realloc the scratch area as necessary. FIXME: should reallocate
440 only at beginning of a new instance of non-zero data. */
442 if (sparse_index > sp_array_size - 1)
445 sparsearray = (struct sp_array *)
446 xrealloc (sparsearray,
447 2 * sp_array_size * sizeof (struct sp_array));
451 /* Process one block. */
453 if (count == sizeof buffer)
455 if (zero_block_p (buffer))
459 sparsearray[sparse_index++].numbytes = numbytes;
466 sparsearray[sparse_index].offset = offset;
472 /* Since count < sizeof buffer, we have the last bit of the file. */
474 if (!zero_block_p (buffer))
477 sparsearray[sparse_index].offset = offset;
481 /* The next two lines are suggested by Andreas Degert, who says
482 they are required for trailing full blocks to be written to the
483 archive, when all zeroed. Yet, it seems to me that the case
484 does not apply. Further, at restore time, the file is not as
485 sparse as it should. So, some serious cleanup is *also* needed
486 in this area. Just one more... :-(. FIXME. */
490 /* Prepare for next block. */
493 /* FIXME: do not clear unless necessary. */
494 clear_buffer (buffer);
498 sparsearray[sparse_index++].numbytes = numbytes;
501 sparsearray[sparse_index].offset = offset - 1;
502 sparsearray[sparse_index++].numbytes = 1;
506 return sparse_index - 1;
514 finish_sparse_file (int file, long *sizeleft, long fullsize, char *name)
518 int sparse_index = 0;
523 while (*sizeleft > 0)
525 start = find_next_block ();
526 memset (start->buffer, 0, BLOCKSIZE);
527 bufsize = sparsearray[sparse_index].numbytes;
530 /* We blew it, maybe. */
532 ERROR ((0, 0, _("Wrote %ld of %ld bytes to file %s"),
533 fullsize - *sizeleft, fullsize, name));
536 pos = lseek (file, sparsearray[sparse_index++].offset, 0);
538 /* If the number of bytes to be written here exceeds the size of
539 the temporary buffer, do it in steps. */
541 while (bufsize > BLOCKSIZE)
546 count = read (file, start->buffer + amount_read,
547 BLOCKSIZE - amount_read);
548 bufsize -= BLOCKSIZE - amount_read;
550 set_next_block_after (start);
551 start = find_next_block ();
552 memset (start->buffer, 0, BLOCKSIZE);
555 /* Store the data. */
557 count = read (file, start->buffer, BLOCKSIZE);
560 ERROR ((0, errno, _("\
561 Read error at byte %ld, reading %d bytes, in file %s"),
562 fullsize - *sizeleft, bufsize, name));
567 set_next_block_after (start);
568 nwritten += BLOCKSIZE; /* FIXME: ??? */
569 start = find_next_block ();
570 memset (start->buffer, 0, BLOCKSIZE);
574 char buffer[BLOCKSIZE];
576 clear_buffer (buffer);
577 count = read (file, buffer, (size_t) bufsize);
578 memcpy (start->buffer, buffer, BLOCKSIZE);
584 _("Read error at byte %ld, reading %d bytes, in file %s"),
585 fullsize - *sizeleft, bufsize, name));
589 if (amount_read >= BLOCKSIZE)
592 set_next_block_after (start + (count - 1) / BLOCKSIZE);
593 if (count != bufsize)
596 _("File %s shrunk by %d bytes, padding with zeros"),
600 start = find_next_block ();
603 amount_read += bufsize;
605 nwritten += count; /* FIXME: ??? */
607 set_next_block_after (start);
612 printf (_("Amount actually written is (I hope) %d.\n"), nwritten);
613 set_next_block_after (start + (count - 1) / BLOCKSIZE);
618 /* Main functions of this module. */
625 create_archive (void)
629 open_archive (ACCESS_WRITE);
631 if (incremental_option)
633 char *buffer = xmalloc (PATH_MAX);
636 collect_and_sort_names ();
638 while (p = name_from_list (), p)
639 dump_file (p, -1, 1);
642 while (p = name_from_list (), p)
645 if (p[strlen (p) - 1] != '/')
646 strcat (buffer, "/");
647 bufp = buffer + strlen (buffer);
648 for (q = gnu_list_name->dir_contents;
654 strcpy (bufp, q + 1);
655 dump_file (buffer, -1, 1);
663 while (p = name_next (1), p)
664 dump_file (p, -1, 1);
670 if (listed_incremental_option)
674 /*----------------------------------------------------------------------.
675 | Dump a single file. Recurse on directories. Result is nonzero for |
676 | success. P is file name to dump. PARENT_DEVICE is device our parent |
677 | directory was on. TOP_LEVEL tells wether we are a toplevel call. |
679 | Sets global CURRENT_STAT to stat output for this file. |
680 `----------------------------------------------------------------------*/
682 /* FIXME: One should make sure that for *every* path leading to setting
683 exit_status to failure, a clear diagnostic has been issued. */
686 dump_file (char *p, int parent_device, int top_level)
692 struct utimbuf restore_times;
694 /* FIXME: `header' and `upperbound' might be used uninitialized in this
695 function. Reported by Bruno Haible. */
697 if (interactive_option && !confirm ("add", p))
700 /* Use stat if following (rather than dumping) 4.2BSD's symbolic links.
701 Otherwise, use lstat (which falls back to stat if no symbolic links). */
703 if (dereference_option != 0
704 #ifdef STX_HIDDEN /* AIX */
705 ? statx (p, ¤t_stat, STATSIZE, STX_HIDDEN)
706 : statx (p, ¤t_stat, STATSIZE, STX_HIDDEN | STX_LINK)
708 ? stat (p, ¤t_stat) : lstat (p, ¤t_stat)
712 WARN ((0, errno, _("Cannot add file %s"), p));
713 if (!ignore_failed_read_option)
714 exit_status = TAREXIT_FAILURE;
718 restore_times.actime = current_stat.st_atime;
719 restore_times.modtime = current_stat.st_mtime;
722 if (S_ISHIDDEN (current_stat.st_mode))
724 char *new = (char *) alloca (strlen (p) + 2);
734 /* See if we only want new files, and check if this one is too old to
735 put in the archive. */
737 if (!incremental_option && !S_ISDIR (current_stat.st_mode)
738 && current_stat.st_mtime < newer_mtime_option
739 && (!after_date_option || current_stat.st_ctime < newer_ctime_option))
741 if (parent_device == -1)
742 WARN ((0, 0, _("%s: is unchanged; not dumped"), p));
743 /* FIXME: recheck this return. */
748 /* See if we are trying to dump the archive. */
750 if (ar_dev && current_stat.st_dev == ar_dev && current_stat.st_ino == ar_ino)
752 WARN ((0, 0, _("%s is the archive; not dumped"), p));
757 /* Check for multiple links.
759 We maintain a list of all such files that we've written so far. Any
760 time we see another, we check the list and avoid dumping the data
761 again if we've done it once already. */
763 if (current_stat.st_nlink > 1
764 && (S_ISREG (current_stat.st_mode)
766 || S_ISCTG (current_stat.st_mode)
769 || S_ISCHR (current_stat.st_mode)
772 || S_ISBLK (current_stat.st_mode)
775 || S_ISFIFO (current_stat.st_mode)
781 /* FIXME: First quick and dirty. Hashing, etc later. */
783 for (lp = linklist; lp; lp = lp->next)
784 if (lp->ino == current_stat.st_ino && lp->dev == current_stat.st_dev)
786 char *link_name = lp->name;
788 /* We found a link. */
790 while (!absolute_names_option && *link_name == '/')
792 static int warned_once = 0;
798 Removing leading `/' from absolute links")));
802 if (strlen (link_name) >= NAME_FIELD_SIZE)
803 write_long (link_name, GNUTYPE_LONGLINK);
804 assign_string (¤t_link_name, link_name);
806 current_stat.st_size = 0;
807 header = start_header (p, ¤t_stat);
810 exit_status = TAREXIT_FAILURE;
813 strncpy (header->header.linkname,
814 link_name, NAME_FIELD_SIZE);
816 /* Force null truncated. */
818 header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
820 header->header.typeflag = LNKTYPE;
821 finish_header (header);
823 /* FIXME: Maybe remove from list after all links found? */
825 if (remove_files_option)
826 if (unlink (p) == -1)
827 ERROR ((0, errno, _("Cannot remove %s"), p));
833 /* Not found. Add it to the list of possible links. */
836 xmalloc ((size_t) (sizeof (struct link) + strlen (p)));
837 lp->ino = current_stat.st_ino;
838 lp->dev = current_stat.st_dev;
839 strcpy (lp->name, p);
844 /* This is not a link to a previously dumped file, so dump it. */
846 if (S_ISREG (current_stat.st_mode)
848 || S_ISCTG (current_stat.st_mode)
852 int f; /* file descriptor */
860 static int cried_once = 0;
867 /* Check the size of the file against the number of blocks
868 allocated for it, counting both data and indirect blocks.
869 If there is a smaller number of blocks that would be
870 necessary to accommodate a file of this size, this is safe
871 to say that we have a sparse file: at least one of those
872 blocks in the file is just a useless hole. For sparse
873 files not having more hole blocks than indirect blocks, the
874 sparseness will go undetected. */
876 /* tar.h defines ST_NBLOCKS in term of 512 byte sectors, even
877 for HP-UX's which count in 1024 byte units and AIX's which
878 count in 4096 byte units. So this should work... */
880 /* Bruno Haible sent me these statistics for Linux. It seems
881 that some filesystems count indirect blocks in st_blocks,
882 while others do not seem to:
884 minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
885 extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
886 ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
887 msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
889 Dick Streefland reports the previous numbers as misleading,
890 because ext2fs use 12 direct blocks, while minix-fs uses only
891 6 direct blocks. Dick gets:
893 ext2 size=20480 ls listed blocks=21
894 minix size=20480 ls listed blocks=21
895 msdos size=20480 ls listed blocks=20
897 It seems that indirect blocks *are* included in st_blocks.
898 The minix filesystem does not account for phantom blocks in
899 st_blocks, so `du' and `ls -s' give wrong results. So, the
900 --sparse option would not work on a minix filesystem. */
902 if (current_stat.st_size > ST_NBLOCKS (current_stat) * BLOCKSIZE)
904 int filesize = current_stat.st_size;
907 header = start_header (p, ¤t_stat);
910 exit_status = TAREXIT_FAILURE;
913 header->header.typeflag = GNUTYPE_SPARSE;
916 /* Call the routine that figures out the layout of the
917 sparse file in question. UPPERBOUND is the index of the
918 last element of the "sparsearray," i.e., the number of
919 elements it needed to describe the file. */
921 upperbound = deal_with_sparse (p, header);
923 /* See if we'll need an extended header later. */
925 if (upperbound > SPARSES_IN_OLDGNU_HEADER - 1)
926 header->oldgnu_header.isextended = 1;
928 /* We store the "real" file size so we can show that in
929 case someone wants to list the archive, i.e., tar tvf
930 <file>. It might be kind of disconcerting if the
931 shrunken file size was the one that showed up. */
933 to_oct ((long) current_stat.st_size, 1 + 12,
934 header->oldgnu_header.realsize);
936 /* This will be the new "size" of the file, i.e., the size
937 of the file minus the blocks of holes that we're
940 find_new_file_size (&filesize, upperbound);
941 current_stat.st_size = filesize;
942 to_oct ((long) filesize, 1 + 12, header->header.size);
944 for (counter = 0; counter < SPARSES_IN_OLDGNU_HEADER; counter++)
946 if (!sparsearray[counter].numbytes)
949 to_oct (sparsearray[counter].offset, 1 + 12,
950 header->oldgnu_header.sp[counter].offset);
951 to_oct (sparsearray[counter].numbytes, 1 + 12,
952 header->oldgnu_header.sp[counter].numbytes);
958 upperbound = SPARSES_IN_OLDGNU_HEADER - 1;
960 sizeleft = current_stat.st_size;
962 /* Don't bother opening empty, world readable files. Also do not open
963 files when archive is meant for /dev/null. */
966 || (sizeleft == 0 && 0444 == (0444 & current_stat.st_mode)))
970 f = open (p, O_RDONLY | O_BINARY);
973 WARN ((0, errno, _("Cannot add file %s"), p));
974 if (!ignore_failed_read_option)
975 exit_status = TAREXIT_FAILURE;
980 /* If the file is sparse, we've already taken care of this. */
984 header = start_header (p, ¤t_stat);
989 exit_status = TAREXIT_FAILURE;
994 /* Mark contiguous files, if we support them. */
996 if (archive_format != V7_FORMAT && S_ISCTG (current_stat.st_mode))
997 header->header.typeflag = CONTTYPE;
999 isextended = header->oldgnu_header.isextended;
1000 save_typeflag = header->header.typeflag;
1001 finish_header (header);
1010 int arraybound = SPARSES_IN_SPARSE_HEADER;
1012 /* static */ int index_offset = SPARSES_IN_OLDGNU_HEADER;
1015 exhdr = find_next_block ();
1019 exit_status = TAREXIT_FAILURE;
1022 memset (exhdr->buffer, 0, BLOCKSIZE);
1023 for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
1025 if (counter + index_offset > upperbound)
1028 to_oct ((long) sparsearray[counter + index_offset].numbytes,
1030 exhdr->sparse_header.sp[counter].numbytes);
1031 to_oct ((long) sparsearray[counter + index_offset].offset,
1033 exhdr->sparse_header.sp[counter].offset);
1035 set_next_block_after (exhdr);
1038 if (sum < upperbound)
1041 if (index_offset + counter <= upperbound)
1043 index_offset += counter;
1044 exhdr->sparse_header.isextended = 1;
1049 if (save_typeflag == GNUTYPE_SPARSE)
1051 if (finish_sparse_file (f, &sizeleft, current_stat.st_size, p))
1055 while (sizeleft > 0)
1057 if (multi_volume_option)
1059 assign_string (&save_name, p);
1060 save_sizeleft = sizeleft;
1061 save_totsize = current_stat.st_size;
1063 start = find_next_block ();
1065 bufsize = available_space_after (start);
1067 if (sizeleft < bufsize)
1069 /* Last read -- zero out area beyond. */
1071 bufsize = (int) sizeleft;
1072 count = bufsize % BLOCKSIZE;
1074 memset (start->buffer + sizeleft, 0,
1075 (size_t) (BLOCKSIZE - count));
1080 count = read (f, start->buffer, (size_t) bufsize);
1083 ERROR ((0, errno, _("\
1084 Read error at byte %ld, reading %d bytes, in file %s"),
1085 (long) (current_stat.st_size - sizeleft), bufsize, p));
1090 /* This is nonportable (the type of set_next_block_after's arg). */
1092 set_next_block_after (start + (count - 1) / BLOCKSIZE);
1094 if (count == bufsize)
1096 ERROR ((0, 0, _("File %s shrunk by %d bytes, padding with zeros"),
1098 goto padit; /* short read */
1101 if (multi_volume_option)
1102 assign_string (&save_name, NULL);
1107 if (atime_preserve_option)
1108 utime (p, &restore_times);
1110 if (remove_files_option)
1112 if (unlink (p) == -1)
1113 ERROR ((0, errno, _("Cannot remove %s"), p));
1117 /* File shrunk or gave error, pad out tape to match the size we
1118 specified in the header. */
1121 while (sizeleft > 0)
1123 save_sizeleft = sizeleft;
1124 start = find_next_block ();
1125 memset (start->buffer, 0, BLOCKSIZE);
1126 set_next_block_after (start);
1127 sizeleft -= BLOCKSIZE;
1129 if (multi_volume_option)
1130 assign_string (&save_name, NULL);
1134 if (atime_preserve_option)
1135 utime (p, &restore_times);
1141 else if (S_ISLNK (current_stat.st_mode))
1144 char *buffer = (char *) alloca (PATH_MAX + 1);
1146 size = readlink (p, buffer, PATH_MAX + 1);
1149 WARN ((0, errno, _("Cannot add file %s"), p));
1150 if (!ignore_failed_read_option)
1151 exit_status = TAREXIT_FAILURE;
1154 buffer[size] = '\0';
1155 if (size >= NAME_FIELD_SIZE)
1156 write_long (buffer, GNUTYPE_LONGLINK);
1157 assign_string (¤t_link_name, buffer);
1159 current_stat.st_size = 0; /* force 0 size on symlink */
1160 header = start_header (p, ¤t_stat);
1163 exit_status = TAREXIT_FAILURE;
1166 strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
1167 header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
1168 header->header.typeflag = SYMTYPE;
1169 finish_header (header); /* nothing more to do to it */
1170 if (remove_files_option)
1172 if (unlink (p) == -1)
1173 ERROR ((0, errno, _("Cannot remove %s"), p));
1177 #endif /* S_ISLNK */
1179 else if (S_ISDIR (current_stat.st_mode))
1182 struct dirent *entry;
1186 int our_device = current_stat.st_dev;
1188 /* If this tar program is installed suid root, like for Amanda, the
1189 access might look like denied, while it is not really.
1191 FIXME: I have the feeling this test is done too early. Couldn't it
1192 just be bundled in later actions? I guess that the proper support
1193 of --ignore-failed-read is the key of the current writing. */
1195 if (access (p, R_OK) == -1 && geteuid () != 0)
1197 WARN ((0, errno, _("Cannot add directory %s"), p));
1198 if (!ignore_failed_read_option)
1199 exit_status = TAREXIT_FAILURE;
1203 /* Build new prototype name. Ensure exactly one trailing slash. */
1206 buflen = len + NAME_FIELD_SIZE;
1207 namebuf = xmalloc ((size_t) (buflen + 1));
1208 strncpy (namebuf, p, (size_t) buflen);
1209 while (len >= 1 && namebuf[len - 1] == '/')
1211 namebuf[len++] = '/';
1212 namebuf[len] = '\0';
1216 /* The "1" above used to be "archive_format != V7_FORMAT", GNU tar
1217 was just not writing directory blocks at all. Daniel Trinkle
1218 writes: ``All old versions of tar I have ever seen have
1219 correctly archived an empty directory. The really old ones I
1220 checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
1221 some subtle reason for the exclusion that I don't know, but the
1222 current behavior is broken.'' I do not know those subtle
1223 reasons either, so until these are reported (anew?), just allow
1224 directory blocks to be written even with old archives. */
1226 current_stat.st_size = 0; /* force 0 size on dir */
1228 /* FIXME: If people could really read standard archives, this
1232 = start_header (standard_option ? p : namebuf, ¤t_stat);
1234 but since they'd interpret DIRTYPE blocks as regular
1235 files, we'd better put the / on the name. */
1237 header = start_header (namebuf, ¤t_stat);
1240 exit_status = TAREXIT_FAILURE;
1241 return; /* eg name too long */
1244 if (incremental_option)
1245 header->header.typeflag = GNUTYPE_DUMPDIR;
1246 else /* if (standard_option) */
1247 header->header.typeflag = DIRTYPE;
1249 /* If we're gnudumping, we aren't done yet so don't close it. */
1251 if (!incremental_option)
1252 finish_header (header); /* done with directory header */
1255 if (incremental_option && gnu_list_name->dir_contents)
1262 char *buffer, *p_buffer;
1264 buffer = gnu_list_name->dir_contents; /* FOO */
1266 for (p_buffer = buffer; p_buffer && *p_buffer;)
1270 tmp = strlen (p_buffer) + 1;
1275 to_oct ((long) totsize, 1 + 12, header->header.size);
1276 finish_header (header);
1279 while (sizeleft > 0)
1281 if (multi_volume_option)
1283 assign_string (&save_name, p);
1284 save_sizeleft = sizeleft;
1285 save_totsize = totsize;
1287 start = find_next_block ();
1288 bufsize = available_space_after (start);
1289 if (sizeleft < bufsize)
1292 count = bufsize % BLOCKSIZE;
1294 memset (start->buffer + sizeleft, 0,
1295 (size_t) (BLOCKSIZE - count));
1297 memcpy (start->buffer, p_buffer, (size_t) bufsize);
1298 sizeleft -= bufsize;
1299 p_buffer += bufsize;
1300 set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
1302 if (multi_volume_option)
1303 assign_string (&save_name, NULL);
1304 if (atime_preserve_option)
1305 utime (p, &restore_times);
1309 /* See if we are about to recurse into a directory, and avoid doing
1310 so if the user wants that we do not descend into directories. */
1312 if (no_recurse_option)
1315 /* See if we are crossing from one file system to another, and
1316 avoid doing so if the user only wants to dump one file system. */
1318 if (one_file_system_option && !top_level
1319 && parent_device != current_stat.st_dev)
1322 WARN ((0, 0, _("%s: On a different filesystem; not dumped"), p));
1326 /* Now output all the files in the directory. */
1328 errno = 0; /* FIXME: errno should be read-only */
1330 directory = opendir (p);
1333 ERROR ((0, errno, _("Cannot open directory %s"), p));
1337 /* Hack to remove "./" from the front of all the file names. */
1339 if (len == 2 && namebuf[0] == '.' && namebuf[1] == '/')
1342 /* FIXME: Should speed this up by cd-ing into the dir. */
1344 while (entry = readdir (directory), entry)
1346 /* Skip `.' and `..'. */
1348 if (is_dot_or_dotdot (entry->d_name))
1351 if ((int) NAMLEN (entry) + len >= buflen)
1353 buflen = len + NAMLEN (entry);
1354 namebuf = (char *) xrealloc (namebuf, (size_t) (buflen + 1));
1356 namebuf[len] = '\0';
1357 ERROR ((0, 0, _("File name %s%s too long"),
1358 namebuf, entry->d_name));
1362 strcpy (namebuf + len, entry->d_name);
1363 if (exclude_option && check_exclude (namebuf))
1365 dump_file (namebuf, our_device, 0);
1368 closedir (directory);
1370 if (atime_preserve_option)
1371 utime (p, &restore_times);
1376 else if (S_ISCHR (current_stat.st_mode))
1381 else if (S_ISBLK (current_stat.st_mode))
1385 /* Avoid screwy apollo lossage where S_IFIFO == S_IFSOCK. */
1387 #if (_ISP__M68K == 0) && (_ISP__A88K == 0) && defined(S_ISFIFO)
1388 else if (S_ISFIFO (current_stat.st_mode))
1393 else if (S_ISSOCK (current_stat.st_mode))
1400 if (archive_format == V7_FORMAT)
1403 current_stat.st_size = 0; /* force 0 size */
1404 header = start_header (p, ¤t_stat);
1407 exit_status = TAREXIT_FAILURE;
1408 return; /* eg name too long */
1411 header->header.typeflag = type;
1413 #if defined(S_IFBLK) || defined(S_IFCHR)
1414 if (type != FIFOTYPE)
1416 to_oct ((long) major (current_stat.st_rdev), 8,
1417 header->header.devmajor);
1418 to_oct ((long) minor (current_stat.st_rdev), 8,
1419 header->header.devminor);
1423 finish_header (header);
1424 if (remove_files_option)
1426 if (unlink (p) == -1)
1427 ERROR ((0, errno, _("Cannot remove %s"), p));
1432 ERROR ((0, 0, _("%s: Unknown file type; file ignored"), p));