+ typeflag = current_header->header.typeflag;
+ switch (typeflag)
+ {
+ case GNUTYPE_SPARSE:
+ file_size = OFF_FROM_HEADER (current_header->oldgnu_header.realsize);
+ if (! fill_in_sparse_array ())
+ return;
+ /* Fall through. */
+
+ case AREGTYPE:
+ case REGTYPE:
+ case CONTTYPE:
+
+ /* Appears to be a file. But BSD tar uses the convention that a slash
+ suffix means a directory. */
+
+ if (current_stat_info.had_trailing_slash)
+ goto really_dir;
+
+ /* FIXME: deal with protection issues. */
+
+ again_file:
+ openflag = (O_WRONLY | O_BINARY | O_CREAT
+ | (old_files_option == OVERWRITE_OLD_FILES
+ ? O_TRUNC
+ : O_EXCL));
+ mode = current_stat_info.stat.st_mode & MODE_RWX & ~ current_umask;
+
+ if (to_stdout_option)
+ {
+ fd = STDOUT_FILENO;
+ goto extract_file;
+ }
+
+ if (! prepare_to_extract (file_name, 0))
+ {
+ skip_member ();
+ if (backup_option)
+ undo_last_backup ();
+ break;
+ }
+
+#if O_CTG
+ /* Contiguous files (on the Masscomp) have to specify the size in
+ the open call that creates them. */
+
+ if (typeflag == CONTTYPE)
+ fd = open (file_name, openflag | O_CTG, mode, current_stat_info.stat.st_size);
+ else
+ fd = open (file_name, openflag, mode);
+
+#else /* not O_CTG */
+ if (typeflag == CONTTYPE)
+ {
+ static int conttype_diagnosed;
+
+ if (!conttype_diagnosed)
+ {
+ conttype_diagnosed = 1;
+ WARN ((0, 0, _("Extracting contiguous files as regular files")));
+ }
+ }
+ fd = open (file_name, openflag, mode);
+
+#endif /* not O_CTG */
+
+ if (fd < 0)
+ {
+ if (maybe_recoverable (file_name, &interdir_made))
+ goto again_file;
+
+ open_error (file_name);
+ skip_member ();
+ if (backup_option)
+ undo_last_backup ();
+ break;
+ }
+
+ extract_file:
+ if (typeflag == GNUTYPE_SPARSE)
+ {
+ char *name;
+ size_t name_length_bis;
+
+ /* Kludge alert. NAME is assigned to header.name because
+ during the extraction, the space that contains the header
+ will get scribbled on, and the name will get munged, so any
+ error messages that happen to contain the filename will look
+ REAL interesting unless we do this. */
+
+ name_length_bis = strlen (file_name) + 1;
+ name = xmalloc (name_length_bis);
+ memcpy (name, file_name, name_length_bis);
+ size = extract_sparse_file (fd, name,
+ current_stat_info.stat.st_size, file_size);
+ free (sparsearray);
+ }
+ else
+ for (size = current_stat_info.stat.st_size; size > 0; )
+ {
+ if (multi_volume_option)
+ {
+ assign_string (&save_name, current_stat_info.file_name);
+ save_totsize = current_stat_info.stat.st_size;
+ save_sizeleft = 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 = full_write (fd, data_block->buffer, written);
+ size -= count;
+
+ set_next_block_after ((union block *)
+ (data_block->buffer + written - 1));
+ if (count != written)
+ {
+ write_error_details (file_name, count, written);
+ break;
+ }
+ }
+
+ skip_file (size);
+
+ if (multi_volume_option)
+ assign_string (&save_name, 0);
+
+ /* 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)
+ break;
+
+ status = close (fd);
+ if (status < 0)
+ {
+ close_error (file_name);
+ if (backup_option)
+ undo_last_backup ();
+ }
+
+ set_stat (file_name, ¤t_stat_info.stat, 0, 0,
+ (old_files_option == OVERWRITE_OLD_FILES
+ ? UNKNOWN_PERMSTATUS
+ : ARCHIVED_PERMSTATUS),
+ typeflag);
+ break;
+
+ case SYMTYPE:
+#ifdef HAVE_SYMLINK
+ if (! prepare_to_extract (file_name, 0))
+ break;
+
+ if (absolute_names_option
+ || ! (ISSLASH (current_stat_info.link_name
+ [FILESYSTEM_PREFIX_LEN (current_stat_info.link_name)])
+ || contains_dot_dot (current_stat_info.link_name)))
+ {
+ while (status = symlink (current_stat_info.link_name, file_name),
+ status != 0)
+ if (!maybe_recoverable (file_name, &interdir_made))
+ break;
+
+ if (status == 0)
+ set_stat (file_name, ¤t_stat_info.stat, 0, 0, 0, SYMTYPE);
+ else
+ symlink_error (current_stat_info.link_name, file_name);
+ }
+ else
+ {
+ /* This symbolic link is potentially dangerous. Don't
+ create it now; instead, create a placeholder file, which
+ will be replaced after other extraction is done. */
+ struct stat st;
+
+ while (fd = open (file_name, O_WRONLY | O_CREAT | O_EXCL, 0),
+ fd < 0)
+ if (! maybe_recoverable (file_name, &interdir_made))
+ break;
+
+ status = -1;
+ if (fd < 0)
+ open_error (file_name);
+ else 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_symlink *p =
+ xmalloc (offsetof (struct delayed_symlink, target)
+ + strlen (current_stat_info.link_name) + 1);
+ p->next = delayed_symlink_head;
+ delayed_symlink_head = p;
+ p->dev = st.st_dev;
+ p->ino = st.st_ino;
+ p->mtime = st.st_mtime;
+ p->uid = current_stat_info.stat.st_uid;
+ p->gid = current_stat_info.stat.st_gid;
+ 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_symlinks
+ && strncmp (file_name, h->file_name, h->file_name_len) == 0
+ && ISSLASH (file_name[h->file_name_len])
+ && (base_name (file_name)
+ == file_name + h->file_name_len + 1))
+ {
+ do
+ {
+ h->after_symlinks = 1;
+
+ if (stat (h->file_name, &st) != 0)
+ stat_error (h->file_name);
+ else
+ {
+ h->stat_info.st_dev = st.st_dev;
+ h->stat_info.st_ino = st.st_ino;
+ }
+ }
+ while ((h = h->next) && ! h->after_symlinks);