+#endif
+
+ /* See if we want only new files, and check if this one is too old to
+ put in the archive. */
+
+ if ((0 < top_level || !incremental_option)
+ && !S_ISDIR (current_stat.st_mode)
+ && current_stat.st_mtime < newer_mtime_option
+ && (!after_date_option || current_stat.st_ctime < newer_ctime_option))
+ {
+ if (0 < top_level)
+ WARN ((0, 0, _("%s: file is unchanged; not dumped"),
+ quotearg_colon (p)));
+ /* FIXME: recheck this return. */
+ return;
+ }
+
+#if !MSDOS
+ /* See if we are trying to dump the archive. */
+
+ if (ar_dev && current_stat.st_dev == ar_dev && current_stat.st_ino == ar_ino)
+ {
+ WARN ((0, 0, _("%s: file is the archive; not dumped"),
+ quotearg_colon (p)));
+ return;
+ }
+#endif
+
+ if (S_ISDIR (current_stat.st_mode))
+ {
+ char *directory;
+ char const *entry;
+ size_t entrylen;
+ char *namebuf;
+ size_t buflen;
+ size_t len;
+ dev_t our_device = current_stat.st_dev;
+
+ errno = 0;
+
+ directory = savedir (p);
+ if (! directory)
+ {
+ if (ignore_failed_read_option)
+ savedir_warn (p);
+ else
+ savedir_error (p);
+ return;
+ }
+
+ /* Build new prototype name. Ensure exactly one trailing slash. */
+
+ len = strlen (p);
+ buflen = len + NAME_FIELD_SIZE;
+ namebuf = xmalloc (buflen + 1);
+ memcpy (namebuf, p, len);
+ while (len >= 1 && ISSLASH (namebuf[len - 1]))
+ len--;
+ namebuf[len++] = '/';
+ namebuf[len] = '\0';
+
+ if (! is_avoided_name (namebuf))
+ {
+ /* The condition above used to be "archive_format != V7_FORMAT".
+ GNU tar was not writing directory blocks at all. Daniel Trinkle
+ writes: ``All old versions of tar I have ever seen have
+ correctly archived an empty directory. The really old ones I
+ checked included HP-UX 7 and Mt. Xinu More/BSD. There may be
+ some subtle reason for the exclusion that I don't know, but the
+ current behavior is broken.'' I do not know those subtle
+ reasons either, so until these are reported (anew?), just allow
+ directory blocks to be written even with old archives. */
+
+ current_stat.st_size = 0; /* force 0 size on dir */
+
+ /* FIXME: If people could really read standard archives, this
+ should be:
+
+ header
+ = start_header (standard_option ? p : namebuf, ¤t_stat);
+
+ but since they'd interpret DIRTYPE blocks as regular
+ files, we'd better put the / on the name. */
+
+ header = start_header (namebuf, ¤t_stat);
+
+ if (incremental_option)
+ header->header.typeflag = GNUTYPE_DUMPDIR;
+ else /* if (standard_option) */
+ header->header.typeflag = DIRTYPE;
+
+ /* If we're gnudumping, we aren't done yet so don't close it. */
+
+ if (!incremental_option)
+ finish_header (header); /* done with directory header */
+ }
+
+ if (incremental_option && gnu_list_name->dir_contents)
+ {
+ off_t sizeleft;
+ off_t totsize;
+ size_t bufsize;
+ union block *start;
+ ssize_t count;
+ const char *buffer, *p_buffer;
+
+ buffer = gnu_list_name->dir_contents; /* FOO */
+ totsize = 0;
+ for (p_buffer = buffer; p_buffer && *p_buffer;)
+ {
+ size_t tmp;
+
+ tmp = strlen (p_buffer) + 1;
+ totsize += tmp;
+ p_buffer += tmp;
+ }
+ totsize++;
+ OFF_TO_CHARS (totsize, header->header.size);
+ finish_header (header);
+ p_buffer = buffer;
+ sizeleft = totsize;
+ while (sizeleft > 0)
+ {
+ if (multi_volume_option)
+ {
+ assign_string (&save_name, p);
+ save_sizeleft = sizeleft;
+ save_totsize = totsize;
+ }
+ start = find_next_block ();
+ bufsize = available_space_after (start);
+ if (sizeleft < bufsize)
+ {
+ bufsize = sizeleft;
+ count = bufsize % BLOCKSIZE;
+ if (count)
+ memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
+ }
+ memcpy (start->buffer, p_buffer, bufsize);
+ sizeleft -= bufsize;
+ p_buffer += bufsize;
+ set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
+ }
+ if (multi_volume_option)
+ assign_string (&save_name, 0);
+ goto finish_dir;
+ }
+
+ /* See if we are about to recurse into a directory, and avoid doing
+ so if the user wants that we do not descend into directories. */
+
+ if (! recursion_option)
+ goto finish_dir;
+
+ /* See if we are crossing from one file system to another, and
+ avoid doing so if the user only wants to dump one file system. */
+
+ if (one_file_system_option && !top_level
+ && parent_device != current_stat.st_dev)
+ {
+ if (verbose_option)
+ WARN ((0, 0,
+ _("%s: file is on a different filesystem; not dumped"),
+ quotearg_colon (p)));
+ goto finish_dir;
+ }
+
+ /* Now output all the files in the directory. */
+
+ /* FIXME: Should speed this up by cd-ing into the dir. */
+
+ for (entry = directory;
+ (entrylen = strlen (entry)) != 0;
+ entry += entrylen + 1)
+ {
+ if (buflen <= len + entrylen)
+ {
+ buflen = len + entrylen;
+ namebuf = xrealloc (namebuf, buflen + 1);
+ }
+ strcpy (namebuf + len, entry);
+ if (!excluded_name (namebuf))
+ dump_file (namebuf, 0, our_device);
+ }
+
+ finish_dir:
+
+ free (directory);
+ free (namebuf);
+ if (atime_preserve_option)
+ utime (p, &restore_times);
+ return;
+ }
+ else if (is_avoided_name (p))
+ return;
+ else
+ {
+ /* Check for multiple links.
+
+ We maintain a table of all such files that we've written so
+ far. Any time we see another, we check the table and avoid
+ dumping the data again if we've done it once already. */
+
+ if (1 < current_stat.st_nlink)
+ {
+ static Hash_table *link_table;
+ struct link *lp = xmalloc (offsetof (struct link, name)
+ + strlen (p) + 1);
+ struct link *dup;
+ lp->ino = current_stat.st_ino;
+ lp->dev = current_stat.st_dev;
+ strcpy (lp->name, p);
+
+ if (! ((link_table
+ || (link_table = hash_initialize (0, 0, hash_link,
+ compare_links, 0)))
+ && (dup = hash_insert (link_table, lp))))
+ xalloc_die ();
+
+ if (dup != lp)
+ {
+ /* We found a link. */
+ char const *link_name = relativize (dup->name);
+
+ free (lp);
+
+ if (NAME_FIELD_SIZE <= strlen (link_name))
+ write_long (link_name, GNUTYPE_LONGLINK);
+ assign_string (¤t_link_name, link_name);
+
+ current_stat.st_size = 0;
+ header = start_header (p, ¤t_stat);
+ strncpy (header->header.linkname, link_name, NAME_FIELD_SIZE);
+
+ /* Force null termination. */
+ header->header.linkname[NAME_FIELD_SIZE - 1] = 0;
+
+ header->header.typeflag = LNKTYPE;
+ finish_header (header);
+
+ /* FIXME: Maybe remove from table after all links found? */
+
+ if (remove_files_option && unlink (p) != 0)
+ unlink_error (p);
+
+ /* We dumped it. */
+ return;
+ }
+ }
+
+ /* This is not a link to a previously dumped file, so dump it. */
+
+ if (S_ISREG (current_stat.st_mode)
+ || S_ISCTG (current_stat.st_mode))
+ {
+ int f; /* file descriptor */
+ size_t bufsize;
+ ssize_t count;
+ off_t sizeleft;
+ union block *start;
+ int header_moved;
+ char isextended = 0;
+ int sparses = 0;
+
+ header_moved = 0;
+
+ if (sparse_option)
+ {
+ /* Check the size of the file against the number of blocks
+ allocated for it, counting both data and indirect blocks.
+ If there is a smaller number of blocks that would be
+ necessary to accommodate a file of this size, this is safe
+ to say that we have a sparse file: at least one of those
+ blocks in the file is just a useless hole. For sparse
+ files not having more hole blocks than indirect blocks, the
+ sparseness will go undetected. */
+
+ /* Bruno Haible sent me these statistics for Linux. It seems
+ that some filesystems count indirect blocks in st_blocks,
+ while others do not seem to:
+
+ minix-fs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
+ extfs tar: size=7205, st_blocks=18 and ST_NBLOCKS=18
+ ext2fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
+ msdos-fs tar: size=7205, st_blocks=16 and ST_NBLOCKS=16
+
+ Dick Streefland reports the previous numbers as misleading,
+ because ext2fs use 12 direct blocks, while minix-fs uses only
+ 6 direct blocks. Dick gets:
+
+ ext2 size=20480 ls listed blocks=21
+ minix size=20480 ls listed blocks=21
+ msdos size=20480 ls listed blocks=20
+
+ It seems that indirect blocks *are* included in st_blocks.
+ The minix filesystem does not account for phantom blocks in
+ st_blocks, so `du' and `ls -s' give wrong results. So, the
+ --sparse option would not work on a minix filesystem. */
+
+ if (ST_NBLOCKS (current_stat)
+ < (current_stat.st_size / ST_NBLOCKSIZE
+ + (current_stat.st_size % ST_NBLOCKSIZE != 0)))
+ {
+ int counter;
+
+ header = start_header (p, ¤t_stat);
+ header->header.typeflag = GNUTYPE_SPARSE;
+ header_moved = 1;
+
+ /* Call the routine that figures out the layout of the
+ sparse file in question. SPARSES is the index of the
+ first unused element of the "sparsearray," i.e.,
+ the number of elements it needed to describe the file. */
+
+ sparses = deal_with_sparse (p, header);
+
+ /* See if we'll need an extended header later. */
+
+ if (SPARSES_IN_OLDGNU_HEADER < sparses)
+ header->oldgnu_header.isextended = 1;
+
+ /* We store the "real" file size so we can show that in
+ case someone wants to list the archive, i.e., tar tvf
+ <file>. It might be kind of disconcerting if the
+ shrunken file size was the one that showed up. */
+
+ OFF_TO_CHARS (current_stat.st_size,
+ header->oldgnu_header.realsize);
+
+ /* This will be the new "size" of the file, i.e., the size
+ of the file minus the blocks of holes that we're
+ skipping over. */
+
+ current_stat.st_size = find_new_file_size (sparses);
+ OFF_TO_CHARS (current_stat.st_size, header->header.size);
+
+ for (counter = 0;
+ counter < sparses && counter < SPARSES_IN_OLDGNU_HEADER;
+ counter++)
+ {
+ OFF_TO_CHARS (sparsearray[counter].offset,
+ header->oldgnu_header.sp[counter].offset);
+ SIZE_TO_CHARS (sparsearray[counter].numbytes,
+ header->oldgnu_header.sp[counter].numbytes);
+ }
+ }
+ }
+
+ sizeleft = current_stat.st_size;
+
+ /* Don't bother opening empty, world readable files. Also do not open
+ files when archive is meant for /dev/null. */
+
+ if (dev_null_output
+ || (sizeleft == 0
+ && MODE_R == (MODE_R & current_stat.st_mode)))
+ f = -1;
+ else
+ {
+ f = open (p, O_RDONLY | O_BINARY);
+ if (f < 0)
+ {
+ if (! top_level && errno == ENOENT)
+ WARN ((0, 0, _("%s: File removed before we read it"),
+ quotearg_colon (p)));
+ else
+ (ignore_failed_read_option ? open_warn : open_error) (p);
+ return;
+ }
+ }
+
+ /* If the file is sparse, we've already taken care of this. */
+
+ if (!header_moved)
+ header = start_header (p, ¤t_stat);
+
+ /* Mark contiguous files, if we support them. */
+
+ if (archive_format != V7_FORMAT && S_ISCTG (current_stat.st_mode))
+ header->header.typeflag = CONTTYPE;
+
+ isextended = header->oldgnu_header.isextended;
+ save_typeflag = header->header.typeflag;
+ finish_header (header);
+ if (isextended)
+ {
+ int sparses_emitted = SPARSES_IN_OLDGNU_HEADER;
+
+ for (;;)
+ {
+ int i;
+ exhdr = find_next_block ();
+ memset (exhdr->buffer, 0, BLOCKSIZE);
+ for (i = 0;
+ (i < SPARSES_IN_SPARSE_HEADER
+ && sparses_emitted + i < sparses);
+ i++)
+ {
+ SIZE_TO_CHARS (sparsearray[sparses_emitted + i].numbytes,
+ exhdr->sparse_header.sp[i].numbytes);
+ OFF_TO_CHARS (sparsearray[sparses_emitted + i].offset,
+ exhdr->sparse_header.sp[i].offset);
+ }
+ set_next_block_after (exhdr);
+ sparses_emitted += i;
+ if (sparses == sparses_emitted)
+ break;
+ exhdr->sparse_header.isextended = 1;
+ }
+ }
+ if (save_typeflag == GNUTYPE_SPARSE)
+ {
+ if (f < 0
+ || finish_sparse_file (f, &sizeleft,
+ current_stat.st_size, p))
+ goto padit;
+ }
+ else
+ while (sizeleft > 0)
+ {
+ if (multi_volume_option)
+ {
+ assign_string (&save_name, p);
+ save_sizeleft = sizeleft;
+ save_totsize = current_stat.st_size;
+ }
+ start = find_next_block ();
+
+ bufsize = available_space_after (start);
+
+ if (sizeleft < bufsize)
+ {
+ /* Last read -- zero out area beyond. */
+
+ bufsize = sizeleft;
+ count = bufsize % BLOCKSIZE;
+ if (count)
+ memset (start->buffer + sizeleft, 0, BLOCKSIZE - count);
+ }
+ if (f < 0)
+ count = bufsize;
+ else
+ count = safe_read (f, start->buffer, bufsize);
+ if (count < 0)
+ {
+ (ignore_failed_read_option
+ ? read_warn_details
+ : read_error_details)
+ (p, current_stat.st_size - sizeleft, bufsize);
+ goto padit;
+ }
+ sizeleft -= bufsize;
+
+ /* This is nonportable (the type of set_next_block_after's arg). */
+
+ set_next_block_after (start + (bufsize - 1) / BLOCKSIZE);
+
+
+ if (count != bufsize)
+ {
+ char buf[UINTMAX_STRSIZE_BOUND];
+ memset (start->buffer + count, 0, bufsize - count);
+ WARN ((0, 0,
+ _("%s: File shrank by %s bytes; padding with zeros"),
+ quotearg_colon (p),
+ STRINGIFY_BIGINT (sizeleft, buf)));
+ if (! ignore_failed_read_option)
+ exit_status = TAREXIT_FAILURE;
+ goto padit; /* short read */
+ }
+ }
+
+ if (multi_volume_option)
+ assign_string (&save_name, 0);
+
+ if (f >= 0)
+ {
+ struct stat final_stat;
+ if (fstat (f, &final_stat) != 0)
+ {
+ if (ignore_failed_read_option)
+ stat_warn (p);
+ else
+ stat_error (p);
+ }
+ else if (final_stat.st_ctime != original_ctime)
+ {
+ char const *qp = quotearg_colon (p);
+ WARN ((0, 0, _("%s: file changed as we read it"), qp));
+ if (! ignore_failed_read_option)
+ exit_status = TAREXIT_FAILURE;
+ }
+ if (close (f) != 0)
+ {
+ if (ignore_failed_read_option)
+ close_warn (p);
+ else
+ close_error (p);
+ }
+ if (atime_preserve_option)
+ utime (p, &restore_times);
+ }
+ if (remove_files_option)
+ {
+ if (unlink (p) == -1)
+ unlink_error (p);
+ }
+ return;
+
+ /* File shrunk or gave error, pad out tape to match the size we
+ specified in the header. */
+
+ padit:
+ while (sizeleft > 0)
+ {
+ save_sizeleft = sizeleft;
+ start = find_next_block ();
+ memset (start->buffer, 0, BLOCKSIZE);
+ set_next_block_after (start);
+ sizeleft -= BLOCKSIZE;
+ }
+ if (multi_volume_option)
+ assign_string (&save_name, 0);
+ if (f >= 0)
+ {
+ close (f);
+ if (atime_preserve_option)
+ utime (p, &restore_times);
+ }
+ return;
+ }
+#ifdef HAVE_READLINK
+ else if (S_ISLNK (current_stat.st_mode))
+ {
+ char *buffer;
+ int size;
+ size_t linklen = current_stat.st_size;
+ if (linklen != current_stat.st_size || linklen + 1 == 0)
+ xalloc_die ();
+ buffer = (char *) alloca (linklen + 1);
+ size = readlink (p, buffer, linklen + 1);
+ if (size < 0)
+ {
+ if (ignore_failed_read_option)
+ readlink_warn (p);
+ else
+ readlink_error (p);
+ return;
+ }
+ buffer[size] = '\0';
+ if (size >= NAME_FIELD_SIZE)
+ write_long (buffer, GNUTYPE_LONGLINK);
+ assign_string (¤t_link_name, buffer);
+
+ current_stat.st_size = 0; /* force 0 size on symlink */
+ header = start_header (p, ¤t_stat);
+ strncpy (header->header.linkname, buffer, NAME_FIELD_SIZE);
+ header->header.linkname[NAME_FIELD_SIZE - 1] = '\0';
+ header->header.typeflag = SYMTYPE;
+ finish_header (header); /* nothing more to do to it */
+ if (remove_files_option)
+ {
+ if (unlink (p) == -1)
+ unlink_error (p);
+ }
+ return;
+ }
+#endif
+ else if (S_ISCHR (current_stat.st_mode))
+ type = CHRTYPE;
+ else if (S_ISBLK (current_stat.st_mode))
+ type = BLKTYPE;
+ else if (S_ISFIFO (current_stat.st_mode))
+ type = FIFOTYPE;
+ else if (S_ISSOCK (current_stat.st_mode))
+ {
+ WARN ((0, 0, _("%s: socket ignored"), quotearg_colon (p)));
+ return;
+ }
+ else if (S_ISDOOR (current_stat.st_mode))
+ {
+ WARN ((0, 0, _("%s: door ignored"), quotearg_colon (p)));
+ return;
+ }
+ else
+ goto unknown;
+ }
+
+ if (archive_format == V7_FORMAT)
+ goto unknown;
+
+ current_stat.st_size = 0; /* force 0 size */
+ header = start_header (p, ¤t_stat);
+ header->header.typeflag = type;
+
+ if (type != FIFOTYPE)
+ {
+ MAJOR_TO_CHARS (major (current_stat.st_rdev), header->header.devmajor);
+ MINOR_TO_CHARS (minor (current_stat.st_rdev), header->header.devminor);
+ }
+
+ finish_header (header);
+ if (remove_files_option)
+ {
+ if (unlink (p) == -1)
+ unlink_error (p);
+ }
+ return;
+
+unknown:
+ WARN ((0, 0, _("%s: Unknown file type; file ignored"),
+ quotearg_colon (p)));
+ if (! ignore_failed_read_option)
+ exit_status = TAREXIT_FAILURE;