+/* Extract the symbolic links whose final extraction were delayed. */
+static void
+apply_delayed_links (void)
+{
+ struct delayed_link *ds;
+
+ for (ds = delayed_link_head; ds; )
+ {
+ struct string_list *sources = ds->sources;
+ char const *valid_source = 0;
+
+ for (sources = ds->sources; sources; sources = sources->next)
+ {
+ char const *source = sources->string;
+ struct stat st;
+
+ /* Make sure the placeholder file is still there. If not,
+ don't create a link, as the placeholder was probably
+ removed by a later extraction. */
+ if (lstat (source, &st) == 0
+ && st.st_dev == ds->dev
+ && st.st_ino == ds->ino
+ && timespec_cmp (get_stat_mtime (&st), ds->mtime) == 0)
+ {
+ /* Unlink the placeholder, then create a hard link if possible,
+ a symbolic link otherwise. */
+ if (unlink (source) != 0)
+ unlink_error (source);
+ else if (valid_source && link (valid_source, source) == 0)
+ ;
+ else if (!ds->is_symlink)
+ {
+ if (link (ds->target, source) != 0)
+ link_error (ds->target, source);
+ }
+ else if (symlink (ds->target, source) != 0)
+ symlink_error (ds->target, source);
+ else
+ {
+ struct tar_stat_info st1;
+ st1.stat.st_uid = ds->uid;
+ st1.stat.st_gid = ds->gid;
+ set_stat (source, &st1, NULL, 0, 0, SYMTYPE);
+ valid_source = source;
+ }
+ }
+ }
+
+ for (sources = ds->sources; sources; )
+ {
+ struct string_list *next = sources->next;
+ free (sources);
+ sources = next;
+ }
+
+ {
+ struct delayed_link *next = ds->next;
+ free (ds);
+ ds = next;
+ }
+ }
+
+ delayed_link_head = 0;
+}
+
+/* Finish the extraction of an archive. */
+void
+extract_finish (void)
+{
+ /* First, fix the status of ordinary directories that need fixing. */
+ apply_nonancestor_delayed_set_stat ("", 0);
+
+ /* Then, apply delayed links, so that they don't affect delayed
+ directory status-setting for ordinary directories. */
+ apply_delayed_links ();
+
+ /* Finally, fix the status of directories that are ancestors
+ of delayed links. */
+ apply_nonancestor_delayed_set_stat ("", 1);
+}
+
+bool
+rename_directory (char *src, char *dst)
+{
+ if (rename (src, dst))
+ {
+ int e = errno;
+
+ switch (e)
+ {
+ case ENOENT:
+ if (make_directories (dst))
+ {
+ if (rename (src, dst) == 0)
+ return true;
+ e = errno;
+ }
+ break;
+
+ case EXDEV:
+ /* FIXME: Fall back to recursive copying */
+
+ default:
+ break;
+ }
+
+ ERROR ((0, e, _("Cannot rename %s to %s"),
+ quote_n (0, src),
+ quote_n (1, dst)));
+ return false;
+ }
+ return true;
+}
+