+ }
+ errno = EEXIST;
+ }
+
+ switch (maybe_recoverable (file_name, false, &interdir_made))
+ {
+ case RECOVER_OK:
+ continue;
+
+ case RECOVER_SKIP:
+ break;
+
+ case RECOVER_NO:
+ if (errno != EEXIST)
+ {
+ mkdir_error (file_name);
+ return 1;
+ }
+ break;
+ }
+ break;
+ }
+
+ if (status == 0
+ || old_files_option == DEFAULT_OLD_FILES
+ || old_files_option == OVERWRITE_OLD_FILES)
+ delay_set_stat (file_name, ¤t_stat_info,
+ current_mode, current_mode_mask,
+ current_stat_info.stat.st_mode, atflag);
+ return status;
+}
+
+
+
+static int
+open_output_file (char const *file_name, int typeflag, mode_t mode,
+ mode_t *current_mode, mode_t *current_mode_mask)
+{
+ int fd;
+ bool overwriting_old_files = old_files_option == OVERWRITE_OLD_FILES;
+ int openflag = (O_WRONLY | O_BINARY | O_CLOEXEC | O_NOCTTY | O_NONBLOCK
+ | O_CREAT
+ | (overwriting_old_files
+ ? O_TRUNC | (dereference_option ? 0 : O_NOFOLLOW)
+ : O_EXCL));
+
+ if (typeflag == CONTTYPE)
+ {
+ static int conttype_diagnosed;
+
+ if (!conttype_diagnosed)
+ {
+ conttype_diagnosed = 1;
+ WARNOPT (WARN_CONTIGUOUS_CAST,
+ (0, 0, _("Extracting contiguous files as regular files")));
+ }
+ }
+
+ /* If O_NOFOLLOW is needed but does not work, check for a symlink
+ separately. There's a race condition, but that cannot be avoided
+ on hosts lacking O_NOFOLLOW. */
+ if (! HAVE_WORKING_O_NOFOLLOW
+ && overwriting_old_files && ! dereference_option)
+ {
+ struct stat st;
+ if (fstatat (chdir_fd, file_name, &st, AT_SYMLINK_NOFOLLOW) == 0
+ && S_ISLNK (st.st_mode))
+ {
+ errno = ELOOP;
+ return -1;
+ }
+ }
+
+ fd = openat (chdir_fd, file_name, openflag, mode);
+ if (0 <= fd)
+ {
+ if (overwriting_old_files)
+ {
+ struct stat st;
+ if (fstat (fd, &st) != 0)
+ {
+ int e = errno;
+ close (fd);
+ errno = e;
+ return -1;
+ }
+ if (! S_ISREG (st.st_mode))
+ {
+ close (fd);
+ errno = EEXIST;
+ return -1;
+ }
+ *current_mode = st.st_mode;
+ *current_mode_mask = ALL_MODE_BITS;
+ }
+ else
+ {
+ *current_mode = mode & ~ current_umask;
+ *current_mode_mask = MODE_RWX;
+ }
+ }
+
+ return fd;
+}
+
+static int
+extract_file (char *file_name, int typeflag)
+{
+ int fd;
+ off_t size;
+ union block *data_block;
+ int status;
+ size_t count;
+ size_t written;
+ bool interdir_made = false;
+ mode_t mode = (current_stat_info.stat.st_mode & MODE_RWX
+ & ~ (0 < same_owner_option ? S_IRWXG | S_IRWXO : 0));
+ mode_t current_mode = 0;
+ mode_t current_mode_mask = 0;
+
+ if (to_stdout_option)
+ fd = STDOUT_FILENO;
+ else if (to_command_option)
+ {
+ fd = sys_exec_command (file_name, 'f', ¤t_stat_info);
+ if (fd < 0)
+ {
+ skip_member ();
+ return 0;
+ }
+ }
+ else
+ {
+ while ((fd = open_output_file (file_name, typeflag, mode,
+ ¤t_mode, ¤t_mode_mask))
+ < 0)
+ {
+ int recover = maybe_recoverable (file_name, true, &interdir_made);
+ if (recover != RECOVER_OK)
+ {
+ skip_member ();
+ if (recover == RECOVER_SKIP)
+ return 0;
+ open_error (file_name);
+ return 1;
+ }
+ }
+ }
+
+ mv_begin_read (¤t_stat_info);
+ if (current_stat_info.is_sparse)
+ sparse_extract_file (fd, ¤t_stat_info, &size);
+ else
+ for (size = current_stat_info.stat.st_size; size > 0; )
+ {
+ mv_size_left (size);
+
+ /* Locate data, determine max length writeable, write it,
+ block that we have used the data, then check if the write
+ worked. */
+
+ data_block = find_next_block ();
+ if (! data_block)
+ {
+ ERROR ((0, 0, _("Unexpected EOF in archive")));
+ break; /* FIXME: What happens, then? */
+ }
+
+ written = available_space_after (data_block);
+
+ if (written > size)
+ written = size;
+ errno = 0;
+ count = blocking_write (fd, data_block->buffer, written);
+ size -= written;
+
+ set_next_block_after ((union block *)
+ (data_block->buffer + written - 1));
+ if (count != written)
+ {
+ if (!to_command_option)
+ write_error_details (file_name, count, written);
+ /* FIXME: shouldn't we restore from backup? */
+ break;
+ }
+ }
+
+ skip_file (size);
+
+ mv_end ();
+
+ /* If writing to stdout, don't try to do anything to the filename;
+ it doesn't exist, or we don't want to touch it anyway. */
+
+ if (to_stdout_option)
+ return 0;
+
+ if (! to_command_option)
+ set_stat (file_name, ¤t_stat_info, fd,
+ current_mode, current_mode_mask, typeflag, false,
+ (old_files_option == OVERWRITE_OLD_FILES
+ ? 0 : AT_SYMLINK_NOFOLLOW));
+
+ status = close (fd);
+ if (status < 0)
+ close_error (file_name);
+
+ if (to_command_option)
+ sys_wait_command ();
+
+ return status;
+}
+
+/* Create a placeholder file with name FILE_NAME, which will be
+ replaced after other extraction is done by a symbolic link if
+ IS_SYMLINK is true, and by a hard link otherwise. Set
+ *INTERDIR_MADE if an intermediate directory is made in the
+ process. */
+
+static int
+create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made)
+{
+ int fd;
+ struct stat st;
+
+ while ((fd = openat (chdir_fd, file_name, O_WRONLY | O_CREAT | O_EXCL, 0)) < 0)
+ {
+ switch (maybe_recoverable (file_name, false, interdir_made))
+ {
+ case RECOVER_OK:
+ continue;
+
+ case RECOVER_SKIP:
+ return 0;
+
+ case RECOVER_NO:
+ open_error (file_name);
+ return -1;
+ }
+ }
+
+ if (fstat (fd, &st) != 0)
+ {
+ stat_error (file_name);
+ close (fd);
+ }
+ else if (close (fd) != 0)
+ close_error (file_name);
+ else
+ {
+ struct delayed_set_stat *h;
+ struct delayed_link *p =
+ xmalloc (offsetof (struct delayed_link, target)
+ + strlen (current_stat_info.link_name)
+ + 1);
+ p->next = delayed_link_head;
+ delayed_link_head = p;
+ p->dev = st.st_dev;
+ p->ino = st.st_ino;
+ p->birthtime = get_stat_birthtime (&st);
+ p->is_symlink = is_symlink;
+ if (is_symlink)
+ {
+ p->mode = current_stat_info.stat.st_mode;
+ p->uid = current_stat_info.stat.st_uid;
+ p->gid = current_stat_info.stat.st_gid;
+ p->atime = current_stat_info.atime;
+ p->mtime = current_stat_info.mtime;
+ }
+ p->change_dir = chdir_current;
+ p->sources = xmalloc (offsetof (struct string_list, string)
+ + strlen (file_name) + 1);
+ p->sources->next = 0;
+ strcpy (p->sources->string, file_name);
+ strcpy (p->target, current_stat_info.link_name);
+
+ h = delayed_set_stat_head;
+ if (h && ! h->after_links
+ && strncmp (file_name, h->file_name, h->file_name_len) == 0
+ && ISSLASH (file_name[h->file_name_len])
+ && (last_component (file_name) == file_name + h->file_name_len + 1))
+ mark_after_links (h);
+
+ return 0;
+ }
+
+ return -1;
+}
+
+static int
+extract_link (char *file_name, int typeflag)
+{
+ bool interdir_made = false;
+ char const *link_name;
+ int rc;
+
+ link_name = current_stat_info.link_name;
+
+ if (! absolute_names_option && contains_dot_dot (link_name))
+ return create_placeholder_file (file_name, false, &interdir_made);
+
+ do
+ {
+ struct stat st1, st2;
+ int e;
+ int status = linkat (chdir_fd, link_name, chdir_fd, file_name, 0);
+ e = errno;
+
+ if (status == 0)
+ {
+ struct delayed_link *ds = delayed_link_head;
+ if (ds
+ && fstatat (chdir_fd, link_name, &st1, AT_SYMLINK_NOFOLLOW) == 0)
+ for (; ds; ds = ds->next)
+ if (ds->change_dir == chdir_current
+ && ds->dev == st1.st_dev
+ && ds->ino == st1.st_ino
+ && (timespec_cmp (ds->birthtime, get_stat_birthtime (&st1))
+ == 0))
+ {
+ struct string_list *p = xmalloc (offsetof (struct string_list, string)
+ + strlen (file_name) + 1);
+ strcpy (p->string, file_name);
+ p->next = ds->sources;
+ ds->sources = p;
+ break;