dcavalca / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone
Florian Festi 0408f9
From 36ee14a07630668629a0d461fba8b5b2248d7d71 Mon Sep 17 00:00:00 2001
Florian Festi 0408f9
From: Florian Festi <ffesti@redhat.com>
Florian Festi 0408f9
Date: Tue, 10 Oct 2023 16:46:17 +0200
Florian Festi 0408f9
Subject: [PATCH] Use file state machine from rpm-4.19
Florian Festi 0408f9
Florian Festi 0408f9
This new implementation fixes several race conditions when placing down
Florian Festi 0408f9
files on disc
Florian Festi 0408f9
---
Florian Festi 0408f9
 lib/fsm.c        | 1164 +++++++++++++++++++++++++---------------------
Florian Festi 0408f9
 lib/rpmarchive.h |    3 +
Florian Festi 0408f9
 lib/rpmfiles.h   |    3 +
Florian Festi 0408f9
Florian Festi 0408f9
diff --git a/lib/rpmarchive.h b/lib/rpmarchive.h
Florian Festi 0408f9
index c864e5b56..e5cda4f97 100644
Florian Festi 0408f9
--- a/lib/rpmarchive.h
Florian Festi 0408f9
+++ b/lib/rpmarchive.h
Florian Festi 0408f9
@@ -26,6 +26,8 @@ enum rpmfilesErrorCodes {
Florian Festi 0408f9
 	RPMERR_FILE_SIZE	= -12,
Florian Festi 0408f9
 	RPMERR_ITER_SKIP	= -13,
Florian Festi 0408f9
 	RPMERR_EXIST_AS_DIR	= -14,
Florian Festi 0408f9
+	RPMERR_INVALID_SYMLINK  = -15,
Florian Festi 0408f9
+	RPMERR_ENOTDIR          = -16,
Florian Festi 0408f9
 
Florian Festi 0408f9
 	RPMERR_OPEN_FAILED	= -32768,
Florian Festi 0408f9
 	RPMERR_CHMOD_FAILED	= -32769,
Florian Festi 0408f9
@@ -47,6 +49,7 @@ enum rpmfilesErrorCodes {
Florian Festi 0408f9
 	RPMERR_COPY_FAILED	= -32785,
Florian Festi 0408f9
 	RPMERR_LSETFCON_FAILED	= -32786,
Florian Festi 0408f9
 	RPMERR_SETCAP_FAILED	= -32787,
Florian Festi 0408f9
+	RPMERR_CLOSE_FAILED     = -32788,
Florian Festi 0408f9
 };
Florian Festi 0408f9
 
Florian Festi 0408f9
 #ifdef __cplusplus
Florian Festi 0408f9
diff --git a/lib/rpmfiles.h b/lib/rpmfiles.h
Florian Festi 0408f9
index daf572cf4..e74bb2201 100644
Florian Festi 0408f9
--- a/lib/rpmfiles.h
Florian Festi 0408f9
+++ b/lib/rpmfiles.h
Florian Festi 0408f9
@@ -90,6 +90,9 @@ typedef enum rpmFileAction_e {
Florian Festi 0408f9
 #define XFA_SKIPPING(_a)	\
Florian Festi 0408f9
     ((_a) == FA_SKIP || (_a) == FA_SKIPNSTATE || (_a) == FA_SKIPNETSHARED || (_a) == FA_SKIPCOLOR)
Florian Festi 0408f9
 
Florian Festi 0408f9
+#define XFA_CREATING(_a)	\
Florian Festi 0408f9
+    ((_a) == FA_CREATE || (_a) == FA_BACKUP || (_a) == FA_SAVE || (_a) == FA_ALTNAME)
Florian Festi 0408f9
+
Florian Festi 0408f9
 /**
Florian Festi 0408f9
  * We pass these around as an array with a sentinel.
Florian Festi 0408f9
  */
Florian Festi 0408f9
--- rpm-4.14.3/lib/fsm.c.orig	2023-10-11 16:00:49.610090807 +0200
Florian Festi 0408f9
+++ rpm-4.14.3/lib/fsm.c	2023-10-11 16:01:16.976451270 +0200
Florian Festi 0408f9
@@ -5,9 +5,11 @@
Florian Festi 0408f9
 
Florian Festi 0408f9
 #include "system.h"
Florian Festi 0408f9
 
Florian Festi 0408f9
+#include <inttypes.h>
Florian Festi 0408f9
 #include <utime.h>
Florian Festi 0408f9
 #include <errno.h>
Florian Festi 0408f9
-#if WITH_CAP
Florian Festi 0408f9
+#include <fcntl.h>
Florian Festi 0408f9
+#ifdef WITH_CAP
Florian Festi 0408f9
 #include <sys/capability.h>
Florian Festi 0408f9
 #endif
Florian Festi 0408f9
 
Florian Festi 0408f9
@@ -17,10 +19,11 @@
Florian Festi 0408f9
 #include <rpm/rpmmacro.h>
Florian Festi 0408f9
 
Florian Festi 0408f9
 #include "rpmio/rpmio_internal.h"	/* fdInit/FiniDigest */
Florian Festi 0408f9
-#include "lib/fsm.h"
Florian Festi 0408f9
-#include "lib/rpmte_internal.h"	/* XXX rpmfs */
Florian Festi 0408f9
-#include "lib/rpmplugins.h"	/* rpm plugins hooks */
Florian Festi 0408f9
-#include "lib/rpmug.h"
Florian Festi 0408f9
+#include "fsm.h"
Florian Festi 0408f9
+#include "rpmte_internal.h"	/* XXX rpmfs */
Florian Festi 0408f9
+#include "rpmfi_internal.h" /* rpmfiSetOnChdir */
Florian Festi 0408f9
+#include "rpmplugins.h"	/* rpm plugins hooks */
Florian Festi 0408f9
+#include "rpmug.h"
Florian Festi 0408f9
 
Florian Festi 0408f9
 #include "debug.h"
Florian Festi 0408f9
 
Florian Festi 0408f9
@@ -38,172 +41,92 @@
Florian Festi 0408f9
 #define _dirPerms 0755
Florian Festi 0408f9
 #define _filePerms 0644
Florian Festi 0408f9
 
Florian Festi 0408f9
+enum filestage_e {
Florian Festi 0408f9
+    FILE_COMMIT = -1,
Florian Festi 0408f9
+    FILE_NONE   = 0,
Florian Festi 0408f9
+    FILE_PRE    = 1,
Florian Festi 0408f9
+    FILE_UNPACK = 2,
Florian Festi 0408f9
+    FILE_PREP   = 3,
Florian Festi 0408f9
+    FILE_POST   = 4,
Florian Festi 0408f9
+};
Florian Festi 0408f9
+
Florian Festi 0408f9
+struct filedata_s {
Florian Festi 0408f9
+    int stage;
Florian Festi 0408f9
+    int setmeta;
Florian Festi 0408f9
+    int skip;
Florian Festi 0408f9
+    rpmFileAction action;
Florian Festi 0408f9
+    const char *suffix;
Florian Festi 0408f9
+    char *fpath;
Florian Festi 0408f9
+    struct stat sb;
Florian Festi 0408f9
+};
Florian Festi 0408f9
+
Florian Festi 0408f9
 /* 
Florian Festi 0408f9
  * XXX Forward declarations for previously exported functions to avoid moving 
Florian Festi 0408f9
  * things around needlessly 
Florian Festi 0408f9
  */ 
Florian Festi 0408f9
 static const char * fileActionString(rpmFileAction a);
Florian Festi 0408f9
+static int fsmOpenat(int dirfd, const char *path, int flags, int dir);
Florian Festi 0408f9
+static int fsmClose(int *wfdp);
Florian Festi 0408f9
 
Florian Festi 0408f9
 /** \ingroup payload
Florian Festi 0408f9
  * Build path to file from file info, optionally ornamented with suffix.
Florian Festi 0408f9
+ * "/" needs special handling to avoid appearing as empty path.
Florian Festi 0408f9
  * @param fi		file info iterator
Florian Festi 0408f9
  * @param suffix	suffix to use (NULL disables)
Florian Festi 0408f9
- * @retval		path to file (malloced)
Florian Festi 0408f9
+ * @param[out]		path to file (malloced)
Florian Festi 0408f9
  */
Florian Festi 0408f9
 static char * fsmFsPath(rpmfi fi, const char * suffix)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    return rstrscat(NULL, rpmfiDN(fi), rpmfiBN(fi), suffix ? suffix : "", NULL);
Florian Festi 0408f9
-}
Florian Festi 0408f9
-
Florian Festi 0408f9
-/** \ingroup payload
Florian Festi 0408f9
- * Directory name iterator.
Florian Festi 0408f9
- */
Florian Festi 0408f9
-typedef struct dnli_s {
Florian Festi 0408f9
-    rpmfiles fi;
Florian Festi 0408f9
-    char * active;
Florian Festi 0408f9
-    int reverse;
Florian Festi 0408f9
-    int isave;
Florian Festi 0408f9
-    int i;
Florian Festi 0408f9
-} * DNLI_t;
Florian Festi 0408f9
-
Florian Festi 0408f9
-/** \ingroup payload
Florian Festi 0408f9
- * Destroy directory name iterator.
Florian Festi 0408f9
- * @param dnli		directory name iterator
Florian Festi 0408f9
- * @retval		NULL always
Florian Festi 0408f9
- */
Florian Festi 0408f9
-static DNLI_t dnlFreeIterator(DNLI_t dnli)
Florian Festi 0408f9
-{
Florian Festi 0408f9
-    if (dnli) {
Florian Festi 0408f9
-	if (dnli->active) free(dnli->active);
Florian Festi 0408f9
-	free(dnli);
Florian Festi 0408f9
-    }
Florian Festi 0408f9
-    return NULL;
Florian Festi 0408f9
+    const char *bn = rpmfiBN(fi);
Florian Festi 0408f9
+    return rstrscat(NULL, *bn ? bn : "/", suffix ? suffix : "", NULL);
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-/** \ingroup payload
Florian Festi 0408f9
- * Create directory name iterator.
Florian Festi 0408f9
- * @param fi		file info set
Florian Festi 0408f9
- * @param fs		file state set
Florian Festi 0408f9
- * @param reverse	traverse directory names in reverse order?
Florian Festi 0408f9
- * @return		directory name iterator
Florian Festi 0408f9
- */
Florian Festi 0408f9
-static DNLI_t dnlInitIterator(rpmfiles fi, rpmfs fs, int reverse)
Florian Festi 0408f9
+static int fsmLink(int odirfd, const char *opath, int dirfd, const char *path)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    DNLI_t dnli;
Florian Festi 0408f9
-    int i, j;
Florian Festi 0408f9
-    int dc;
Florian Festi 0408f9
-
Florian Festi 0408f9
-    if (fi == NULL)
Florian Festi 0408f9
-	return NULL;
Florian Festi 0408f9
-    dc = rpmfilesDC(fi);
Florian Festi 0408f9
-    dnli = xcalloc(1, sizeof(*dnli));
Florian Festi 0408f9
-    dnli->fi = fi;
Florian Festi 0408f9
-    dnli->reverse = reverse;
Florian Festi 0408f9
-    dnli->i = (reverse ? dc : 0);
Florian Festi 0408f9
-
Florian Festi 0408f9
-    if (dc) {
Florian Festi 0408f9
-	dnli->active = xcalloc(dc, sizeof(*dnli->active));
Florian Festi 0408f9
-	int fc = rpmfilesFC(fi);
Florian Festi 0408f9
-
Florian Festi 0408f9
-	/* Identify parent directories not skipped. */
Florian Festi 0408f9
-	for (i = 0; i < fc; i++)
Florian Festi 0408f9
-            if (!XFA_SKIPPING(rpmfsGetAction(fs, i)))
Florian Festi 0408f9
-		dnli->active[rpmfilesDI(fi, i)] = 1;
Florian Festi 0408f9
-
Florian Festi 0408f9
-	/* Exclude parent directories that are explicitly included. */
Florian Festi 0408f9
-	for (i = 0; i < fc; i++) {
Florian Festi 0408f9
-	    int dil;
Florian Festi 0408f9
-	    size_t dnlen, bnlen;
Florian Festi 0408f9
+    int rc = linkat(odirfd, opath, dirfd, path, 0);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	    if (!S_ISDIR(rpmfilesFMode(fi, i)))
Florian Festi 0408f9
-		continue;
Florian Festi 0408f9
-
Florian Festi 0408f9
-	    dil = rpmfilesDI(fi, i);
Florian Festi 0408f9
-	    dnlen = strlen(rpmfilesDN(fi, dil));
Florian Festi 0408f9
-	    bnlen = strlen(rpmfilesBN(fi, i));
Florian Festi 0408f9
-
Florian Festi 0408f9
-	    for (j = 0; j < dc; j++) {
Florian Festi 0408f9
-		const char * dnl;
Florian Festi 0408f9
-		size_t jlen;
Florian Festi 0408f9
-
Florian Festi 0408f9
-		if (!dnli->active[j] || j == dil)
Florian Festi 0408f9
-		    continue;
Florian Festi 0408f9
-		dnl = rpmfilesDN(fi, j);
Florian Festi 0408f9
-		jlen = strlen(dnl);
Florian Festi 0408f9
-		if (jlen != (dnlen+bnlen+1))
Florian Festi 0408f9
-		    continue;
Florian Festi 0408f9
-		if (!rstreqn(dnl, rpmfilesDN(fi, dil), dnlen))
Florian Festi 0408f9
-		    continue;
Florian Festi 0408f9
-		if (!rstreqn(dnl+dnlen, rpmfilesBN(fi, i), bnlen))
Florian Festi 0408f9
-		    continue;
Florian Festi 0408f9
-		if (dnl[dnlen+bnlen] != '/' || dnl[dnlen+bnlen+1] != '\0')
Florian Festi 0408f9
-		    continue;
Florian Festi 0408f9
-		/* This directory is included in the package. */
Florian Festi 0408f9
-		dnli->active[j] = 0;
Florian Festi 0408f9
-		break;
Florian Festi 0408f9
-	    }
Florian Festi 0408f9
-	}
Florian Festi 0408f9
-
Florian Festi 0408f9
-	/* Print only once per package. */
Florian Festi 0408f9
-	if (!reverse) {
Florian Festi 0408f9
-	    j = 0;
Florian Festi 0408f9
-	    for (i = 0; i < dc; i++) {
Florian Festi 0408f9
-		if (!dnli->active[i]) continue;
Florian Festi 0408f9
-		if (j == 0) {
Florian Festi 0408f9
-		    j = 1;
Florian Festi 0408f9
-		    rpmlog(RPMLOG_DEBUG,
Florian Festi 0408f9
-	"========== Directories not explicitly included in package:\n");
Florian Festi 0408f9
-		}
Florian Festi 0408f9
-		rpmlog(RPMLOG_DEBUG, "%10d %s\n", i, rpmfilesDN(fi, i));
Florian Festi 0408f9
-	    }
Florian Festi 0408f9
-	    if (j)
Florian Festi 0408f9
-		rpmlog(RPMLOG_DEBUG, "==========\n");
Florian Festi 0408f9
-	}
Florian Festi 0408f9
+    if (_fsm_debug) {
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d %s, %d %s) %s\n", __func__,
Florian Festi 0408f9
+	       odirfd, opath, dirfd, path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     }
Florian Festi 0408f9
-    return dnli;
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (rc < 0)
Florian Festi 0408f9
+	rc = RPMERR_LINK_FAILED;
Florian Festi 0408f9
+    return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-/** \ingroup payload
Florian Festi 0408f9
- * Return next directory name (from file info).
Florian Festi 0408f9
- * @param dnli		directory name iterator
Florian Festi 0408f9
- * @return		next directory name
Florian Festi 0408f9
- */
Florian Festi 0408f9
-static
Florian Festi 0408f9
-const char * dnlNextIterator(DNLI_t dnli)
Florian Festi 0408f9
+#ifdef WITH_CAP
Florian Festi 0408f9
+static int cap_set_fileat(int dirfd, const char *path, cap_t fcaps)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    const char * dn = NULL;
Florian Festi 0408f9
-
Florian Festi 0408f9
-    if (dnli) {
Florian Festi 0408f9
-	rpmfiles fi = dnli->fi;
Florian Festi 0408f9
-	int dc = rpmfilesDC(fi);
Florian Festi 0408f9
-	int i = -1;
Florian Festi 0408f9
-
Florian Festi 0408f9
-	if (dnli->active)
Florian Festi 0408f9
-	do {
Florian Festi 0408f9
-	    i = (!dnli->reverse ? dnli->i++ : --dnli->i);
Florian Festi 0408f9
-	} while (i >= 0 && i < dc && !dnli->active[i]);
Florian Festi 0408f9
-
Florian Festi 0408f9
-	if (i >= 0 && i < dc)
Florian Festi 0408f9
-	    dn = rpmfilesDN(fi, i);
Florian Festi 0408f9
-	else
Florian Festi 0408f9
-	    i = -1;
Florian Festi 0408f9
-	dnli->isave = i;
Florian Festi 0408f9
+    int rc = -1;
Florian Festi 0408f9
+    int fd = fsmOpenat(dirfd, path, O_RDONLY|O_NOFOLLOW, 0);
Florian Festi 0408f9
+    if (fd >= 0) {
Florian Festi 0408f9
+	rc = cap_set_fd(fd, fcaps);
Florian Festi 0408f9
+	fsmClose(&fd;;
Florian Festi 0408f9
     }
Florian Festi 0408f9
-    return dn;
Florian Festi 0408f9
+    return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
+#endif
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmSetFCaps(const char *path, const char *captxt)
Florian Festi 0408f9
+static int fsmSetFCaps(int fd, int dirfd, const char *path, const char *captxt)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
-#if WITH_CAP
Florian Festi 0408f9
+
Florian Festi 0408f9
+#ifdef WITH_CAP
Florian Festi 0408f9
     if (captxt && *captxt != '\0') {
Florian Festi 0408f9
 	cap_t fcaps = cap_from_text(captxt);
Florian Festi 0408f9
-	if (fcaps == NULL || cap_set_file(path, fcaps) != 0) {
Florian Festi 0408f9
-	    rc = RPMERR_SETCAP_FAILED;
Florian Festi 0408f9
+
Florian Festi 0408f9
+	if (fd >= 0) {
Florian Festi 0408f9
+	    if (fcaps == NULL || cap_set_fd(fd, fcaps))
Florian Festi 0408f9
+		rc = RPMERR_SETCAP_FAILED;
Florian Festi 0408f9
+	} else {
Florian Festi 0408f9
+	    if (fcaps == NULL || cap_set_fileat(dirfd, path, fcaps))
Florian Festi 0408f9
+		rc = RPMERR_SETCAP_FAILED;
Florian Festi 0408f9
 	}
Florian Festi 0408f9
+
Florian Festi 0408f9
 	if (_fsm_debug) {
Florian Festi 0408f9
-	    rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", __func__,
Florian Festi 0408f9
-		   path, captxt, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+	    rpmlog(RPMLOG_DEBUG, " %8s (%d - %d %s, %s) %s\n", __func__,
Florian Festi 0408f9
+		   fd, dirfd, path, captxt, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
 	}
Florian Festi 0408f9
 	cap_free(fcaps);
Florian Festi 0408f9
     } 
Florian Festi 0408f9
@@ -211,101 +134,104 @@
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static void wfd_close(FD_t *wfdp)
Florian Festi 0408f9
+static int fsmClose(int *wfdp)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    if (wfdp && *wfdp) {
Florian Festi 0408f9
+    int rc = 0;
Florian Festi 0408f9
+    if (wfdp && *wfdp >= 0) {
Florian Festi 0408f9
 	int myerrno = errno;
Florian Festi 0408f9
 	static int oneshot = 0;
Florian Festi 0408f9
 	static int flush_io = 0;
Florian Festi 0408f9
+	int fdno = *wfdp;
Florian Festi 0408f9
+
Florian Festi 0408f9
 	if (!oneshot) {
Florian Festi 0408f9
-	    flush_io = rpmExpandNumeric("%{?_flush_io}");
Florian Festi 0408f9
+	    flush_io = (rpmExpandNumeric("%{?_flush_io}") > 0);
Florian Festi 0408f9
 	    oneshot = 1;
Florian Festi 0408f9
 	}
Florian Festi 0408f9
 	if (flush_io) {
Florian Festi 0408f9
-	    int fdno = Fileno(*wfdp);
Florian Festi 0408f9
 	    fsync(fdno);
Florian Festi 0408f9
 	}
Florian Festi 0408f9
-	Fclose(*wfdp);
Florian Festi 0408f9
-	*wfdp = NULL;
Florian Festi 0408f9
+	if (close(fdno))
Florian Festi 0408f9
+	    rc = RPMERR_CLOSE_FAILED;
Florian Festi 0408f9
+
Florian Festi 0408f9
+	if (_fsm_debug) {
Florian Festi 0408f9
+	    rpmlog(RPMLOG_DEBUG, " %8s ([%d]) %s\n", __func__,
Florian Festi 0408f9
+		   fdno, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+	}
Florian Festi 0408f9
+	*wfdp = -1;
Florian Festi 0408f9
 	errno = myerrno;
Florian Festi 0408f9
     }
Florian Festi 0408f9
+    return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int wfd_open(FD_t *wfdp, const char *dest)
Florian Festi 0408f9
+static int fsmOpen(int *wfdp, int dirfd, const char *dest)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
     /* Create the file with 0200 permissions (write by owner). */
Florian Festi 0408f9
-    {
Florian Festi 0408f9
-	mode_t old_umask = umask(0577);
Florian Festi 0408f9
-	*wfdp = Fopen(dest, "wx.ufdio");
Florian Festi 0408f9
-	umask(old_umask);
Florian Festi 0408f9
-    }
Florian Festi 0408f9
-    if (Ferror(*wfdp)) {
Florian Festi 0408f9
+    int fd = openat(dirfd, dest, O_WRONLY|O_EXCL|O_CREAT, 0200);
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (fd < 0)
Florian Festi 0408f9
 	rc = RPMERR_OPEN_FAILED;
Florian Festi 0408f9
-	goto exit;
Florian Festi 0408f9
-    }
Florian Festi 0408f9
 
Florian Festi 0408f9
-    return 0;
Florian Festi 0408f9
+    if (_fsm_debug) {
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%s [%d]) %s\n", __func__,
Florian Festi 0408f9
+	       dest, fd, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+    *wfdp = fd;
Florian Festi 0408f9
 
Florian Festi 0408f9
-exit:
Florian Festi 0408f9
-    wfd_close(wfdp);
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-/** \ingroup payload
Florian Festi 0408f9
- * Create file from payload stream.
Florian Festi 0408f9
- * @return		0 on success
Florian Festi 0408f9
- */
Florian Festi 0408f9
-static int expandRegular(rpmfi fi, const char *dest, rpmpsm psm, int nodigest)
Florian Festi 0408f9
+static int fsmUnpack(rpmfi fi, int fdno, rpmpsm psm, int nodigest)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    FD_t wfd = NULL;
Florian Festi 0408f9
-    int rc;
Florian Festi 0408f9
-
Florian Festi 0408f9
-    rc = wfd_open(&wfd, dest);
Florian Festi 0408f9
-    if (rc != 0)
Florian Festi 0408f9
-        goto exit;
Florian Festi 0408f9
-
Florian Festi 0408f9
-    rc = rpmfiArchiveReadToFilePsm(fi, wfd, nodigest, psm);
Florian Festi 0408f9
-    wfd_close(&wfd;;
Florian Festi 0408f9
-exit:
Florian Festi 0408f9
+    FD_t fd = fdDup(fdno);
Florian Festi 0408f9
+    int rc = rpmfiArchiveReadToFilePsm(fi, fd, nodigest, psm);
Florian Festi 0408f9
+    if (_fsm_debug) {
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%s %" PRIu64 " bytes [%d]) %s\n", __func__,
Florian Festi 0408f9
+	       rpmfiFN(fi), rpmfiFSize(fi), Fileno(fd),
Florian Festi 0408f9
+	       (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+    Fclose(fd);
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmMkfile(rpmfi fi, const char *dest, rpmfiles files,
Florian Festi 0408f9
-		     rpmpsm psm, int nodigest, int *setmeta,
Florian Festi 0408f9
-		     int * firsthardlink, FD_t *firstlinkfile)
Florian Festi 0408f9
+static int fsmMkfile(int dirfd, rpmfi fi, struct filedata_s *fp, rpmfiles files,
Florian Festi 0408f9
+		     rpmpsm psm, int nodigest,
Florian Festi 0408f9
+		     struct filedata_s ** firstlink, int *firstlinkfile,
Florian Festi 0408f9
+		     int *firstdir, int *fdp)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
-    int numHardlinks = rpmfiFNlink(fi);
Florian Festi 0408f9
+    int fd = -1;
Florian Festi 0408f9
 
Florian Festi 0408f9
-    if (numHardlinks > 1) {
Florian Festi 0408f9
-	/* Create first hardlinked file empty */
Florian Festi 0408f9
-	if (*firsthardlink < 0) {
Florian Festi 0408f9
-	    *firsthardlink = rpmfiFX(fi);
Florian Festi 0408f9
-	    rc = wfd_open(firstlinkfile, dest);
Florian Festi 0408f9
-	} else {
Florian Festi 0408f9
-	    /* Create hard links for others */
Florian Festi 0408f9
-	    char *fn = rpmfilesFN(files, *firsthardlink);
Florian Festi 0408f9
-	    rc = link(fn, dest);
Florian Festi 0408f9
-	    if (rc < 0) {
Florian Festi 0408f9
-		rc = RPMERR_LINK_FAILED;
Florian Festi 0408f9
-	    }
Florian Festi 0408f9
-	    free(fn);
Florian Festi 0408f9
+    if (*firstlink == NULL) {
Florian Festi 0408f9
+	/* First encounter, open file for writing */
Florian Festi 0408f9
+	rc = fsmOpen(&fd, dirfd, fp->fpath);
Florian Festi 0408f9
+	/* If it's a part of a hardlinked set, the content may come later */
Florian Festi 0408f9
+	if (fp->sb.st_nlink > 1) {
Florian Festi 0408f9
+	    *firstlink = fp;
Florian Festi 0408f9
+	    *firstlinkfile = fd;
Florian Festi 0408f9
+	    *firstdir = dup(dirfd);
Florian Festi 0408f9
+	}
Florian Festi 0408f9
+    } else {
Florian Festi 0408f9
+	/* Create hard links for others and avoid redundant metadata setting */
Florian Festi 0408f9
+	if (*firstlink != fp) {
Florian Festi 0408f9
+	    rc = fsmLink(*firstdir, (*firstlink)->fpath, dirfd, fp->fpath);
Florian Festi 0408f9
 	}
Florian Festi 0408f9
+	fd = *firstlinkfile;
Florian Festi 0408f9
     }
Florian Festi 0408f9
-    /* Write normal files or fill the last hardlinked (already
Florian Festi 0408f9
-       existing) file with content */
Florian Festi 0408f9
-    if (numHardlinks<=1) {
Florian Festi 0408f9
-	if (!rc)
Florian Festi 0408f9
-	    rc = expandRegular(fi, dest, psm, nodigest);
Florian Festi 0408f9
-    } else if (rpmfiArchiveHasContent(fi)) {
Florian Festi 0408f9
+
Florian Festi 0408f9
+    /* If the file has content, unpack it */
Florian Festi 0408f9
+    if (rpmfiArchiveHasContent(fi)) {
Florian Festi 0408f9
 	if (!rc)
Florian Festi 0408f9
-	    rc = rpmfiArchiveReadToFilePsm(fi, *firstlinkfile, nodigest, psm);
Florian Festi 0408f9
-	wfd_close(firstlinkfile);
Florian Festi 0408f9
-	*firsthardlink = -1;
Florian Festi 0408f9
-    } else {
Florian Festi 0408f9
-	*setmeta = 0;
Florian Festi 0408f9
+	    rc = fsmUnpack(fi, fd, psm, nodigest);
Florian Festi 0408f9
+	/* Last file of hardlink set, ensure metadata gets set */
Florian Festi 0408f9
+	if (*firstlink) {
Florian Festi 0408f9
+	    fp->setmeta = 1;
Florian Festi 0408f9
+	    *firstlink = NULL;
Florian Festi 0408f9
+	    *firstlinkfile = -1;
Florian Festi 0408f9
+	    fsmClose(firstdir);
Florian Festi 0408f9
+	}
Florian Festi 0408f9
     }
Florian Festi 0408f9
+    *fdp = fd;
Florian Festi 0408f9
 
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
@@ -330,18 +256,15 @@
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmStat(const char *path, int dolstat, struct stat *sb)
Florian Festi 0408f9
+static int fsmStat(int dirfd, const char *path, int dolstat, struct stat *sb)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    int rc;
Florian Festi 0408f9
-    if (dolstat){
Florian Festi 0408f9
-	rc = lstat(path, sb);
Florian Festi 0408f9
-    } else {
Florian Festi 0408f9
-        rc = stat(path, sb);
Florian Festi 0408f9
-    }
Florian Festi 0408f9
+    int flags = dolstat ? AT_SYMLINK_NOFOLLOW : 0;
Florian Festi 0408f9
+    int rc = fstatat(dirfd, path, sb, flags);
Florian Festi 0408f9
+
Florian Festi 0408f9
     if (_fsm_debug && rc && errno != ENOENT)
Florian Festi 0408f9
-        rpmlog(RPMLOG_DEBUG, " %8s (%s, ost) %s\n",
Florian Festi 0408f9
+        rpmlog(RPMLOG_DEBUG, " %8s (%d %s, ost) %s\n",
Florian Festi 0408f9
                __func__,
Florian Festi 0408f9
-               path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+               dirfd, path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     if (rc < 0) {
Florian Festi 0408f9
         rc = (errno == ENOENT ? RPMERR_ENOENT : RPMERR_LSTAT_FAILED);
Florian Festi 0408f9
 	/* Ensure consistent struct content on failure */
Florian Festi 0408f9
@@ -350,12 +273,12 @@
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmRmdir(const char *path)
Florian Festi 0408f9
+static int fsmRmdir(int dirfd, const char *path)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    int rc = rmdir(path);
Florian Festi 0408f9
+    int rc = unlinkat(dirfd, path, AT_REMOVEDIR);
Florian Festi 0408f9
     if (_fsm_debug)
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s) %s\n", __func__,
Florian Festi 0408f9
-	       path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d %s) %s\n", __func__,
Florian Festi 0408f9
+	       dirfd, path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     if (rc < 0)
Florian Festi 0408f9
 	switch (errno) {
Florian Festi 0408f9
 	case ENOENT:        rc = RPMERR_ENOENT;    break;
Florian Festi 284b6b
@@ -365,172 +288,194 @@
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmMkdir(const char *path, mode_t mode)
Florian Festi 0408f9
+static int fsmMkdir(int dirfd, const char *path, mode_t mode)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    int rc = mkdir(path, (mode & 07777));
Florian Festi 0408f9
+    int rc = mkdirat(dirfd, path, (mode & 07777));
Florian Festi 0408f9
     if (_fsm_debug)
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n", __func__,
Florian Festi 0408f9
-	       path, (unsigned)(mode & 07777),
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d %s, 0%04o) %s\n", __func__,
Florian Festi 0408f9
+	       dirfd, path, (unsigned)(mode & 07777),
Florian Festi 0408f9
 	       (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     if (rc < 0)	rc = RPMERR_MKDIR_FAILED;
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmMkfifo(const char *path, mode_t mode)
Florian Festi 0408f9
+static int fsmOpenat(int dirfd, const char *path, int flags, int dir)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    int rc = mkfifo(path, (mode & 07777));
Florian Festi 0408f9
-
Florian Festi 0408f9
-    if (_fsm_debug) {
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n",
Florian Festi 0408f9
-	       __func__, path, (unsigned)(mode & 07777),
Florian Festi 0408f9
-	       (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+    struct stat lsb, sb;
Florian Festi 0408f9
+    int sflags = flags | O_NOFOLLOW;
Florian Festi 0408f9
+    int fd = openat(dirfd, path, sflags);
Florian Festi 0408f9
+
Florian Festi 0408f9
+    /*
Florian Festi 0408f9
+     * Only ever follow symlinks by root or target owner. Since we can't
Florian Festi 0408f9
+     * open the symlink itself, the order matters: we stat the link *after*
Florian Festi 0408f9
+     * opening the target, and if the link ownership changed between the calls
Florian Festi 0408f9
+     * it could've only been the link owner or root.
Florian Festi 0408f9
+     */
Florian Festi 0408f9
+    if (fd < 0 && errno == ELOOP && flags != sflags) {
Florian Festi 0408f9
+	int ffd = openat(dirfd, path, flags);
Florian Festi 0408f9
+	if (ffd >= 0) {
Florian Festi 0408f9
+	    if (fstatat(dirfd, path, &lsb, AT_SYMLINK_NOFOLLOW) == 0) {
Florian Festi 0408f9
+		if (fstat(ffd, &sb) == 0) {
Florian Festi 0408f9
+		    if (lsb.st_uid == 0 || lsb.st_uid == sb.st_uid) {
Florian Festi 0408f9
+			fd = ffd;
Florian Festi 0408f9
+		    }
Florian Festi 0408f9
+		}
Florian Festi 0408f9
+	    }
Florian Festi 0408f9
+	    if (ffd != fd)
Florian Festi 0408f9
+		close(ffd);
Florian Festi 0408f9
+	}
Florian Festi 0408f9
     }
Florian Festi 0408f9
 
Florian Festi 0408f9
-    if (rc < 0)
Florian Festi 0408f9
-	rc = RPMERR_MKFIFO_FAILED;
Florian Festi 0408f9
-
Florian Festi 0408f9
-    return rc;
Florian Festi 0408f9
+    /* O_DIRECTORY equivalent */
Florian Festi 0408f9
+    if (dir && fd >= 0 && fstat(fd, &sb) == 0 && !S_ISDIR(sb.st_mode)) {
Florian Festi 0408f9
+	errno = ENOTDIR;
Florian Festi 0408f9
+	fsmClose(&fd;;
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+    return fd;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmMknod(const char *path, mode_t mode, dev_t dev)
Florian Festi 0408f9
+static int fsmDoMkDir(rpmPlugins plugins, int dirfd, const char *dn,
Florian Festi 0408f9
+			const char *apath,
Florian Festi 0408f9
+			int owned, mode_t mode, int *fdp)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    /* FIX: check S_IFIFO or dev != 0 */
Florian Festi 0408f9
-    int rc = mknod(path, (mode & ~07777), dev);
Florian Festi 0408f9
+    int rc;
Florian Festi 0408f9
+    rpmFsmOp op = (FA_CREATE);
Florian Festi 0408f9
+    if (!owned)
Florian Festi 0408f9
+	op |= FAF_UNOWNED;
Florian Festi 0408f9
 
Florian Festi 0408f9
-    if (_fsm_debug) {
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%o, 0x%x) %s\n",
Florian Festi 0408f9
-	       __func__, path, (unsigned)(mode & ~07777),
Florian Festi 0408f9
-	       (unsigned)dev, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+    /* Run fsm file pre hook for all plugins */
Florian Festi 0408f9
+    rc = rpmpluginsCallFsmFilePre(plugins, NULL, apath, mode, op);
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (!rc)
Florian Festi 0408f9
+	rc = fsmMkdir(dirfd, dn, mode);
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (!rc) {
Florian Festi 0408f9
+	*fdp = fsmOpenat(dirfd, dn, O_RDONLY|O_NOFOLLOW, 1);
Florian Festi 0408f9
+	if (*fdp == -1)
Florian Festi 0408f9
+	    rc = RPMERR_ENOTDIR;
Florian Festi 0408f9
     }
Florian Festi 0408f9
 
Florian Festi 0408f9
-    if (rc < 0)
Florian Festi 0408f9
-	rc = RPMERR_MKNOD_FAILED;
Florian Festi 0408f9
+    if (!rc) {
Florian Festi 0408f9
+	rc = rpmpluginsCallFsmFilePrepare(plugins, NULL, *fdp, apath, apath, mode, op);
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+
Florian Festi 0408f9
+    /* Run fsm file post hook for all plugins */
Florian Festi 0408f9
+    rpmpluginsCallFsmFilePost(plugins, NULL, apath, mode, op, rc);
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (!rc) {
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG,
Florian Festi 0408f9
+		"%s directory created with perms %04o\n",
Florian Festi 0408f9
+		apath, (unsigned)(mode & 07777));
Florian Festi 0408f9
+    }
Florian Festi 0408f9
 
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-/**
Florian Festi 0408f9
- * Create (if necessary) directories not explicitly included in package.
Florian Festi 0408f9
- * @param files		file data
Florian Festi 0408f9
- * @param fs		file states
Florian Festi 0408f9
- * @param plugins	rpm plugins handle
Florian Festi 0408f9
- * @return		0 on success
Florian Festi 0408f9
- */
Florian Festi 0408f9
-static int fsmMkdirs(rpmfiles files, rpmfs fs, rpmPlugins plugins)
Florian Festi 0408f9
+static int ensureDir(rpmPlugins plugins, const char *p, int owned, int create,
Florian Festi 0408f9
+		    int quiet, int *dirfdp)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    DNLI_t dnli = dnlInitIterator(files, fs, 0);
Florian Festi 0408f9
-    struct stat sb;
Florian Festi 0408f9
-    const char *dpath;
Florian Festi 0408f9
-    int dc = rpmfilesDC(files);
Florian Festi 0408f9
+    char *sp = NULL, *bn;
Florian Festi 0408f9
+    char *apath = NULL;
Florian Festi 0408f9
+    int oflags = O_RDONLY;
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
-    int i;
Florian Festi 0408f9
-    int ldnlen = 0;
Florian Festi 0408f9
-    int ldnalloc = 0;
Florian Festi 0408f9
-    char * ldn = NULL;
Florian Festi 0408f9
-    short * dnlx = NULL; 
Florian Festi 0408f9
-
Florian Festi 0408f9
-    dnlx = (dc ? xcalloc(dc, sizeof(*dnlx)) : NULL);
Florian Festi 0408f9
-
Florian Festi 0408f9
-    if (dnlx != NULL)
Florian Festi 0408f9
-    while ((dpath = dnlNextIterator(dnli)) != NULL) {
Florian Festi 0408f9
-	size_t dnlen = strlen(dpath);
Florian Festi 0408f9
-	char * te, dn[dnlen+1];
Florian Festi 0408f9
-
Florian Festi 0408f9
-	dc = dnli->isave;
Florian Festi 0408f9
-	if (dc < 0) continue;
Florian Festi 0408f9
-	dnlx[dc] = dnlen;
Florian Festi 0408f9
-	if (dnlen <= 1)
Florian Festi 0408f9
-	    continue;
Florian Festi 0408f9
 
Florian Festi 0408f9
-	if (dnlen <= ldnlen && rstreq(dpath, ldn))
Florian Festi 0408f9
-	    continue;
Florian Festi 0408f9
+    if (*dirfdp >= 0)
Florian Festi 0408f9
+	return rc;
Florian Festi 0408f9
 
Florian Festi 0408f9
-	/* Copy as we need to modify the string */
Florian Festi 0408f9
-	(void) stpcpy(dn, dpath);
Florian Festi 0408f9
+    int dirfd = fsmOpenat(-1, "/", oflags, 1);
Florian Festi 0408f9
+    int fd = dirfd; /* special case of "/" */
Florian Festi 0408f9
 
Florian Festi 0408f9
-	/* Assume '/' directory exists, "mkdir -p" for others if non-existent */
Florian Festi 0408f9
-	for (i = 1, te = dn + 1; *te != '\0'; te++, i++) {
Florian Festi 0408f9
-	    if (*te != '/')
Florian Festi 0408f9
-		continue;
Florian Festi 0408f9
+    char *path = xstrdup(p);
Florian Festi 0408f9
+    char *dp = path;
Florian Festi 0408f9
 
Florian Festi 0408f9
-	    *te = '\0';
Florian Festi 0408f9
+    while ((bn = strtok_r(dp, "/", &sp)) != NULL) {
Florian Festi 0408f9
+	fd = fsmOpenat(dirfd, bn, oflags, 1);
Florian Festi 0408f9
+	/* assemble absolute path for plugins benefit, sigh */
Florian Festi 0408f9
+	apath = rstrscat(&apath, "/", bn, NULL);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	    /* Already validated? */
Florian Festi 0408f9
-	    if (i < ldnlen &&
Florian Festi 0408f9
-		(ldn[i] == '/' || ldn[i] == '\0') && rstreqn(dn, ldn, i))
Florian Festi 0408f9
-	    {
Florian Festi 0408f9
-		*te = '/';
Florian Festi 0408f9
-		/* Move pre-existing path marker forward. */
Florian Festi 0408f9
-		dnlx[dc] = (te - dn);
Florian Festi 0408f9
-		continue;
Florian Festi 0408f9
+	if (fd < 0 && errno == ENOENT && create) {
Florian Festi 0408f9
+	    mode_t mode = S_IFDIR | (_dirPerms & 07777);
Florian Festi 0408f9
+	    rc = fsmDoMkDir(plugins, dirfd, bn, apath, owned, mode, &fd;;
Florian Festi 0408f9
+	}
Florian Festi 0408f9
+
Florian Festi 0408f9
+	fsmClose(&dirfd);
Florian Festi 0408f9
+	if (fd >= 0) {
Florian Festi 0408f9
+	    dirfd = fd;
Florian Festi 0408f9
+	} else {
Florian Festi 0408f9
+	    if (!quiet) {
Florian Festi 0408f9
+		rpmlog(RPMLOG_ERR, _("failed to open dir %s of %s: %s\n"),
Florian Festi 0408f9
+			bn, p, strerror(errno));
Florian Festi 0408f9
 	    }
Florian Festi 0408f9
+	    rc = RPMERR_OPEN_FAILED;
Florian Festi 0408f9
+	    break;
Florian Festi 0408f9
+	}
Florian Festi 0408f9
 
Florian Festi 0408f9
-	    /* Validate next component of path. */
Florian Festi 0408f9
-	    rc = fsmStat(dn, 1, &sb); /* lstat */
Florian Festi 0408f9
-	    *te = '/';
Florian Festi 0408f9
-
Florian Festi 0408f9
-	    /* Directory already exists? */
Florian Festi 0408f9
-	    if (rc == 0 && S_ISDIR(sb.st_mode)) {
Florian Festi 0408f9
-		/* Move pre-existing path marker forward. */
Florian Festi 0408f9
-		dnlx[dc] = (te - dn);
Florian Festi 0408f9
-	    } else if (rc == RPMERR_ENOENT) {
Florian Festi 0408f9
-		*te = '\0';
Florian Festi 0408f9
-		mode_t mode = S_IFDIR | (_dirPerms & 07777);
Florian Festi 0408f9
-		rpmFsmOp op = (FA_CREATE|FAF_UNOWNED);
Florian Festi 0408f9
-
Florian Festi 0408f9
-		/* Run fsm file pre hook for all plugins */
Florian Festi 0408f9
-		rc = rpmpluginsCallFsmFilePre(plugins, NULL, dn, mode, op);
Florian Festi 0408f9
-
Florian Festi 0408f9
-		if (!rc)
Florian Festi 0408f9
-		    rc = fsmMkdir(dn, mode);
Florian Festi 0408f9
-
Florian Festi 0408f9
-		if (!rc) {
Florian Festi 0408f9
-		    rc = rpmpluginsCallFsmFilePrepare(plugins, NULL, dn, dn,
Florian Festi 0408f9
-						      mode, op);
Florian Festi 0408f9
-		}
Florian Festi 0408f9
+	dp = NULL;
Florian Festi 0408f9
+    }
Florian Festi 0408f9
 
Florian Festi 0408f9
-		/* Run fsm file post hook for all plugins */
Florian Festi 0408f9
-		rpmpluginsCallFsmFilePost(plugins, NULL, dn, mode, op, rc);
Florian Festi 0408f9
+    if (rc) {
Florian Festi 0408f9
+	fsmClose(&fd;;
Florian Festi 0408f9
+	fsmClose(&dirfd);
Florian Festi 0408f9
+    } else {
Florian Festi 0408f9
+	rc = 0;
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+    *dirfdp = dirfd;
Florian Festi 0408f9
 
Florian Festi 0408f9
-		if (!rc) {
Florian Festi 0408f9
-		    rpmlog(RPMLOG_DEBUG,
Florian Festi 0408f9
-			    "%s directory created with perms %04o\n",
Florian Festi 0408f9
-			    dn, (unsigned)(mode & 07777));
Florian Festi 0408f9
-		}
Florian Festi 0408f9
-		*te = '/';
Florian Festi 0408f9
-	    }
Florian Festi 0408f9
-	    if (rc)
Florian Festi 0408f9
-		break;
Florian Festi 0408f9
-	}
Florian Festi 0408f9
-	if (rc) break;
Florian Festi 0408f9
+    if (_fsm_debug) {
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%s: %d) %s\n", __func__,
Florian Festi 0408f9
+		p, dirfd, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+    }
Florian Festi 0408f9
 
Florian Festi 0408f9
-	/* Save last validated path. */
Florian Festi 0408f9
-	if (ldnalloc < (dnlen + 1)) {
Florian Festi 0408f9
-	    ldnalloc = dnlen + 100;
Florian Festi 0408f9
-	    ldn = xrealloc(ldn, ldnalloc);
Florian Festi 0408f9
-	}
Florian Festi 0408f9
-	if (ldn != NULL) { /* XXX can't happen */
Florian Festi 0408f9
-	    strcpy(ldn, dn);
Florian Festi 0408f9
-	    ldnlen = dnlen;
Florian Festi 0408f9
-	}
Florian Festi 0408f9
+    free(path);
Florian Festi 0408f9
+    free(apath);
Florian Festi 0408f9
+    return rc;
Florian Festi 0408f9
+}
Florian Festi 0408f9
+
Florian Festi 0408f9
+static int fsmMkfifo(int dirfd, const char *path, mode_t mode)
Florian Festi 0408f9
+{
Florian Festi 0408f9
+    int rc = mkfifoat(dirfd, path, (mode & 07777));
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (_fsm_debug) {
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d %s, 0%04o) %s\n",
Florian Festi 0408f9
+	       __func__, dirfd, path, (unsigned)(mode & 07777),
Florian Festi 0408f9
+	       (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (rc < 0)
Florian Festi 0408f9
+	rc = RPMERR_MKFIFO_FAILED;
Florian Festi 0408f9
+
Florian Festi 0408f9
+    return rc;
Florian Festi 0408f9
+}
Florian Festi 0408f9
+
Florian Festi 0408f9
+static int fsmMknod(int dirfd, const char *path, mode_t mode, dev_t dev)
Florian Festi 0408f9
+{
Florian Festi 0408f9
+    /* FIX: check S_IFIFO or dev != 0 */
Florian Festi 0408f9
+    int rc = mknodat(dirfd, path, (mode & ~07777), dev);
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (_fsm_debug) {
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d %s, 0%o, 0x%x) %s\n",
Florian Festi 0408f9
+	       __func__, dirfd, path, (unsigned)(mode & ~07777),
Florian Festi 0408f9
+	       (unsigned)dev, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     }
Florian Festi 0408f9
-    free(dnlx);
Florian Festi 0408f9
-    free(ldn);
Florian Festi 0408f9
-    dnlFreeIterator(dnli);
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (rc < 0)
Florian Festi 0408f9
+	rc = RPMERR_MKNOD_FAILED;
Florian Festi 0408f9
 
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static void removeSBITS(const char *path)
Florian Festi 0408f9
+static void removeSBITS(int dirfd, const char *path)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     struct stat stb;
Florian Festi 0408f9
-    if (lstat(path, &stb) == 0 && S_ISREG(stb.st_mode)) {
Florian Festi 0408f9
+    int flags = AT_SYMLINK_NOFOLLOW;
Florian Festi 0408f9
+    if (fstatat(dirfd, path, &stb, flags) == 0 && S_ISREG(stb.st_mode)) {
Florian Festi 284b6b
+    	/* We now know it's not a link so no need to worry about following */
Florian Festi 0408f9
 	if ((stb.st_mode & 06000) != 0) {
Florian Festi 0408f9
-	    (void) chmod(path, stb.st_mode & 0777);
Florian Festi 284b6b
+	    (void) fchmodat(dirfd, path, stb.st_mode & 0777, 0);
Florian Festi 0408f9
 	}
Florian Festi 0408f9
-#if WITH_CAP
Florian Festi 0408f9
+#ifdef WITH_CAP
Florian Festi 0408f9
 	if (stb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) {
Florian Festi 0408f9
-	    (void) cap_set_file(path, NULL);
Florian Festi 0408f9
+	    (void) cap_set_fileat(dirfd, path, NULL);
Florian Festi 0408f9
 	}
Florian Festi 0408f9
 #endif
Florian Festi 0408f9
     }
Florian Festi 0408f9
@@ -546,13 +490,13 @@
Florian Festi 0408f9
 	    (fpath ? fpath : ""));
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmSymlink(const char *opath, const char *path)
Florian Festi 0408f9
+static int fsmSymlink(const char *opath, int dirfd, const char *path)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    int rc = symlink(opath, path);
Florian Festi 0408f9
+    int rc = symlinkat(opath, dirfd, path);
Florian Festi 0408f9
 
Florian Festi 0408f9
     if (_fsm_debug) {
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", __func__,
Florian Festi 0408f9
-	       opath, path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%s, %d %s) %s\n", __func__,
Florian Festi 0408f9
+	       opath, dirfd, path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     }
Florian Festi 0408f9
 
Florian Festi 0408f9
     if (rc < 0)
Florian Festi 0408f9
@@ -560,96 +504,125 @@
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmUnlink(const char *path)
Florian Festi 0408f9
+static int fsmUnlink(int dirfd, const char *path)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
-    removeSBITS(path);
Florian Festi 0408f9
-    rc = unlink(path);
Florian Festi 0408f9
+    removeSBITS(dirfd, path);
Florian Festi 0408f9
+    rc = unlinkat(dirfd, path, 0);
Florian Festi 0408f9
     if (_fsm_debug)
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s) %s\n", __func__,
Florian Festi 0408f9
-	       path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d %s) %s\n", __func__,
Florian Festi 0408f9
+	       dirfd, path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     if (rc < 0)
Florian Festi 0408f9
 	rc = (errno == ENOENT ? RPMERR_ENOENT : RPMERR_UNLINK_FAILED);
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmRename(const char *opath, const char *path)
Florian Festi 0408f9
+static int fsmRename(int odirfd, const char *opath, int dirfd, const char *path)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    removeSBITS(path);
Florian Festi 0408f9
-    int rc = rename(opath, path);
Florian Festi 0408f9
+    removeSBITS(dirfd, path);
Florian Festi 0408f9
+    int rc = renameat(odirfd, opath, dirfd, path);
Florian Festi 0408f9
 #if defined(ETXTBSY) && defined(__HPUX__)
Florian Festi 0408f9
     /* XXX HP-UX (and other os'es) don't permit rename to busy files. */
Florian Festi 0408f9
     if (rc && errno == ETXTBSY) {
Florian Festi 0408f9
 	char *rmpath = NULL;
Florian Festi 0408f9
 	rstrscat(&rmpath, path, "-RPMDELETE", NULL);
Florian Festi 0408f9
-	rc = rename(path, rmpath);
Florian Festi 0408f9
-	if (!rc) rc = rename(opath, path);
Florian Festi 0408f9
+	/* Rename within the original directory */
Florian Festi 0408f9
+	rc = renameat(odirfd, path, odirfd, rmpath);
Florian Festi 0408f9
+	if (!rc) rc = renameat(odirfd, opath, dirfd, path);
Florian Festi 0408f9
 	free(rmpath);
Florian Festi 0408f9
     }
Florian Festi 0408f9
 #endif
Florian Festi 0408f9
     if (_fsm_debug)
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s, %s) %s\n", __func__,
Florian Festi 0408f9
-	       opath, path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d %s, %d %s) %s\n", __func__,
Florian Festi 0408f9
+	       odirfd, opath, dirfd, path, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     if (rc < 0)
Florian Festi 0408f9
 	rc = (errno == EISDIR ? RPMERR_EXIST_AS_DIR : RPMERR_RENAME_FAILED);
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmRemove(const char *path, mode_t mode)
Florian Festi 0408f9
+static int fsmRemove(int dirfd, const char *path, mode_t mode)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    return S_ISDIR(mode) ? fsmRmdir(path) : fsmUnlink(path);
Florian Festi 0408f9
+    return S_ISDIR(mode) ? fsmRmdir(dirfd, path) : fsmUnlink(dirfd, path);
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmChown(const char *path, mode_t mode, uid_t uid, gid_t gid)
Florian Festi 0408f9
+static int fsmChown(int fd, int dirfd, const char *path, mode_t mode, uid_t uid, gid_t gid)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    int rc = S_ISLNK(mode) ? lchown(path, uid, gid) : chown(path, uid, gid);
Florian Festi 0408f9
-    if (rc < 0) {
Florian Festi 0408f9
-	struct stat st;
Florian Festi 0408f9
-	if (lstat(path, &st) == 0 && st.st_uid == uid && st.st_gid == gid)
Florian Festi 0408f9
-	    rc = 0;
Florian Festi 0408f9
+    int rc;
Florian Festi 0408f9
+    struct stat st;
Florian Festi 0408f9
+
Florian Festi 0408f9
+    if (fd >= 0) {
Florian Festi 0408f9
+	rc = fchown(fd, uid, gid);
Florian Festi 0408f9
+	if (rc < 0) {
Florian Festi 0408f9
+	    if (fstat(fd, &st) == 0 && (st.st_uid == uid && st.st_gid == gid)) {
Florian Festi 0408f9
+		rc = 0;
Florian Festi 0408f9
+	    }
Florian Festi 0408f9
+	}
Florian Festi 0408f9
+    } else {
Florian Festi 0408f9
+	int flags = AT_SYMLINK_NOFOLLOW;
Florian Festi 0408f9
+	rc = fchownat(dirfd, path, uid, gid, flags);
Florian Festi 0408f9
+	if (rc < 0) {
Florian Festi 0408f9
+	    struct stat st;
Florian Festi 0408f9
+	    if (fstatat(dirfd, path, &st, flags) == 0 &&
Florian Festi 0408f9
+		    (st.st_uid == uid && st.st_gid == gid)) {
Florian Festi 0408f9
+		rc = 0;
Florian Festi 0408f9
+	    }
Florian Festi 0408f9
+	}
Florian Festi 0408f9
     }
Florian Festi 0408f9
-    if (_fsm_debug)
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s, %d, %d) %s\n", __func__,
Florian Festi 0408f9
-	       path, (int)uid, (int)gid,
Florian Festi 0408f9
+    if (_fsm_debug) {
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d - %d %s, %d, %d) %s\n", __func__,
Florian Festi 0408f9
+	       fd, dirfd, path, (int)uid, (int)gid,
Florian Festi 0408f9
 	       (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+    }
Florian Festi 0408f9
     if (rc < 0)	rc = RPMERR_CHOWN_FAILED;
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmChmod(const char *path, mode_t mode)
Florian Festi 0408f9
+static int fsmChmod(int fd, int dirfd, const char *path, mode_t mode)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    int rc = chmod(path, (mode & 07777));
Florian Festi 0408f9
-    if (rc < 0) {
Florian Festi 0408f9
-	struct stat st;
Florian Festi 0408f9
-	if (lstat(path, &st) == 0 && (st.st_mode & 07777) == (mode & 07777))
Florian Festi 0408f9
-	    rc = 0;
Florian Festi 0408f9
+    mode_t fmode = (mode & 07777);
Florian Festi 0408f9
+    int rc;
Florian Festi 0408f9
+    if (fd >= 0) {
Florian Festi 0408f9
+	rc = fchmod(fd, fmode);
Florian Festi 0408f9
+	if (rc < 0) {
Florian Festi 0408f9
+	    struct stat st;
Florian Festi 0408f9
+	    if (fstat(fd, &st) == 0 && (st.st_mode & 07777) == fmode) {
Florian Festi 0408f9
+		rc = 0;
Florian Festi 0408f9
+	    }
Florian Festi 0408f9
+	}
Florian Festi 0408f9
+    } else {
Florian Festi 0408f9
+	rc = fchmodat(dirfd, path, fmode, 0);
Florian Festi 0408f9
+	if (rc < 0) {
Florian Festi 0408f9
+	    struct stat st;
Florian Festi 0408f9
+	    if (fstatat(dirfd, path, &st, AT_SYMLINK_NOFOLLOW) == 0 &&
Florian Festi 0408f9
+		    (st.st_mode & 07777) == fmode) {
Florian Festi 0408f9
+		rc = 0;
Florian Festi 0408f9
+	    }
Florian Festi 0408f9
+	}
Florian Festi 0408f9
     }
Florian Festi 0408f9
     if (_fsm_debug)
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0%04o) %s\n", __func__,
Florian Festi 0408f9
-	       path, (unsigned)(mode & 07777),
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d - %d %s, 0%04o) %s\n", __func__,
Florian Festi 0408f9
+	       fd, dirfd, path, (unsigned)(mode & 07777),
Florian Festi 0408f9
 	       (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     if (rc < 0)	rc = RPMERR_CHMOD_FAILED;
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmUtime(const char *path, mode_t mode, time_t mtime)
Florian Festi 0408f9
+static int fsmUtime(int fd, int dirfd, const char *path, mode_t mode, time_t mtime)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
-    struct timeval stamps[2] = {
Florian Festi 0408f9
-	{ .tv_sec = mtime, .tv_usec = 0 },
Florian Festi 0408f9
-	{ .tv_sec = mtime, .tv_usec = 0 },
Florian Festi 0408f9
+    struct timespec stamps[2] = {
Florian Festi 0408f9
+	{ .tv_sec = mtime, .tv_nsec = 0 },
Florian Festi 0408f9
+	{ .tv_sec = mtime, .tv_nsec = 0 },
Florian Festi 0408f9
     };
Florian Festi 0408f9
 
Florian Festi 0408f9
-#if HAVE_LUTIMES
Florian Festi 0408f9
-    rc = lutimes(path, stamps);
Florian Festi 0408f9
-#else
Florian Festi 0408f9
-    if (!S_ISLNK(mode))
Florian Festi 0408f9
-	rc = utimes(path, stamps);
Florian Festi 0408f9
-#endif
Florian Festi 0408f9
+    if (fd >= 0)
Florian Festi 0408f9
+	rc = futimens(fd, stamps);
Florian Festi 0408f9
+    else
Florian Festi 0408f9
+	rc = utimensat(dirfd, path, stamps, AT_SYMLINK_NOFOLLOW);
Florian Festi 0408f9
     
Florian Festi 0408f9
     if (_fsm_debug)
Florian Festi 0408f9
-	rpmlog(RPMLOG_DEBUG, " %8s (%s, 0x%x) %s\n", __func__,
Florian Festi 0408f9
-	       path, (unsigned)mtime, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
+	rpmlog(RPMLOG_DEBUG, " %8s (%d - %d %s, 0x%x) %s\n", __func__,
Florian Festi 0408f9
+	       fd, dirfd, path, (unsigned)mtime, (rc < 0 ? strerror(errno) : ""));
Florian Festi 0408f9
     if (rc < 0)	rc = RPMERR_UTIME_FAILED;
Florian Festi 0408f9
     /* ...but utime error is not critical for directories */
Florian Festi 0408f9
     if (rc && S_ISDIR(mode))
Florian Festi 0408f9
@@ -657,24 +630,24 @@
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmVerify(const char *path, rpmfi fi)
Florian Festi 0408f9
+static int fsmVerify(int dirfd, const char *path, rpmfi fi)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     int rc;
Florian Festi 0408f9
     int saveerrno = errno;
Florian Festi 0408f9
     struct stat dsb;
Florian Festi 0408f9
     mode_t mode = rpmfiFMode(fi);
Florian Festi 0408f9
 
Florian Festi 0408f9
-    rc = fsmStat(path, 1, &dsb;;
Florian Festi 0408f9
+    rc = fsmStat(dirfd, path, 1, &dsb;;
Florian Festi 0408f9
     if (rc)
Florian Festi 0408f9
 	return rc;
Florian Festi 0408f9
 
Florian Festi 0408f9
     if (S_ISREG(mode)) {
Florian Festi 0408f9
 	/* HP-UX (and other os'es) don't permit unlink on busy files. */
Florian Festi 0408f9
 	char *rmpath = rstrscat(NULL, path, "-RPMDELETE", NULL);
Florian Festi 0408f9
-	rc = fsmRename(path, rmpath);
Florian Festi 0408f9
+	rc = fsmRename(dirfd, path, dirfd, rmpath);
Florian Festi 0408f9
 	/* XXX shouldn't we take unlink return code here? */
Florian Festi 0408f9
 	if (!rc)
Florian Festi 0408f9
-	    (void) fsmUnlink(rmpath);
Florian Festi 0408f9
+	    (void) fsmUnlink(dirfd, rmpath);
Florian Festi 0408f9
 	else
Florian Festi 0408f9
 	    rc = RPMERR_UNLINK_FAILED;
Florian Festi 0408f9
 	free(rmpath);
Florian Festi 0408f9
@@ -683,7 +656,7 @@
Florian Festi 0408f9
         if (S_ISDIR(dsb.st_mode)) return 0;
Florian Festi 0408f9
         if (S_ISLNK(dsb.st_mode)) {
Florian Festi 0408f9
 	    uid_t luid = dsb.st_uid;
Florian Festi 0408f9
-            rc = fsmStat(path, 0, &dsb;;
Florian Festi 0408f9
+            rc = fsmStat(dirfd, path, 0, &dsb;;
Florian Festi 0408f9
             if (rc == RPMERR_ENOENT) rc = 0;
Florian Festi 0408f9
             if (rc) return rc;
Florian Festi 0408f9
             errno = saveerrno;
Florian Festi 0408f9
@@ -709,7 +682,7 @@
Florian Festi 0408f9
         if (S_ISSOCK(dsb.st_mode)) return 0;
Florian Festi 0408f9
     }
Florian Festi 0408f9
     /* XXX shouldn't do this with commit/undo. */
Florian Festi 0408f9
-    rc = fsmUnlink(path);
Florian Festi 0408f9
+    rc = fsmUnlink(dirfd, path);
Florian Festi 0408f9
     if (rc == 0)	rc = RPMERR_ENOENT;
Florian Festi 0408f9
     return (rc ? rc : RPMERR_ENOENT);	/* XXX HACK */
Florian Festi 0408f9
 }
Florian Festi 0408f9
@@ -723,7 +696,7 @@
Florian Festi 0408f9
 
Florian Festi 0408f9
 
Florian Festi 0408f9
 /* Rename pre-existing modified or unmanaged file. */
Florian Festi 0408f9
-static int fsmBackup(rpmfi fi, rpmFileAction action)
Florian Festi 0408f9
+static int fsmBackup(int dirfd, rpmfi fi, rpmFileAction action)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
     const char *suffix = NULL;
Florian Festi 0408f9
@@ -744,9 +717,10 @@
Florian Festi 0408f9
     if (suffix) {
Florian Festi 0408f9
 	char * opath = fsmFsPath(fi, NULL);
Florian Festi 0408f9
 	char * path = fsmFsPath(fi, suffix);
Florian Festi 0408f9
-	rc = fsmRename(opath, path);
Florian Festi 0408f9
+	rc = fsmRename(dirfd, opath, dirfd, path);
Florian Festi 0408f9
 	if (!rc) {
Florian Festi 0408f9
-	    rpmlog(RPMLOG_WARNING, _("%s saved as %s\n"), opath, path);
Florian Festi 0408f9
+	    rpmlog(RPMLOG_WARNING, _("%s%s saved as %s%s\n"),
Florian Festi 0408f9
+		   rpmfiDN(fi), opath, rpmfiDN(fi), path);
Florian Festi 0408f9
 	}
Florian Festi 0408f9
 	free(path);
Florian Festi 0408f9
 	free(opath);
Florian Festi 0408f9
@@ -754,7 +728,8 @@
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmSetmeta(const char *path, rpmfi fi, rpmPlugins plugins,
Florian Festi 0408f9
+static int fsmSetmeta(int fd, int dirfd, const char *path,
Florian Festi 0408f9
+		      rpmfi fi, rpmPlugins plugins,
Florian Festi 0408f9
 		      rpmFileAction action, const struct stat * st,
Florian Festi 0408f9
 		      int nofcaps)
Florian Festi 0408f9
 {
Florian Festi 0408f9
@@ -762,27 +737,28 @@
Florian Festi 0408f9
     const char *dest = rpmfiFN(fi);
Florian Festi 0408f9
 
Florian Festi 0408f9
     if (!rc && !getuid()) {
Florian Festi 0408f9
-	rc = fsmChown(path, st->st_mode, st->st_uid, st->st_gid);
Florian Festi 0408f9
+	rc = fsmChown(fd, dirfd, path, st->st_mode, st->st_uid, st->st_gid);
Florian Festi 0408f9
     }
Florian Festi 0408f9
     if (!rc && !S_ISLNK(st->st_mode)) {
Florian Festi 0408f9
-	rc = fsmChmod(path, st->st_mode);
Florian Festi 0408f9
+	rc = fsmChmod(fd, dirfd, path, st->st_mode);
Florian Festi 0408f9
     }
Florian Festi 0408f9
     /* Set file capabilities (if enabled) */
Florian Festi 0408f9
     if (!rc && !nofcaps && S_ISREG(st->st_mode) && !getuid()) {
Florian Festi 0408f9
-	rc = fsmSetFCaps(path, rpmfiFCaps(fi));
Florian Festi 0408f9
+	rc = fsmSetFCaps(fd, dirfd, path, rpmfiFCaps(fi));
Florian Festi 0408f9
     }
Florian Festi 0408f9
     if (!rc) {
Florian Festi 0408f9
-	rc = fsmUtime(path, st->st_mode, rpmfiFMtime(fi));
Florian Festi 0408f9
+	rc = fsmUtime(fd, dirfd, path, st->st_mode, rpmfiFMtime(fi));
Florian Festi 0408f9
     }
Florian Festi 0408f9
     if (!rc) {
Florian Festi 0408f9
 	rc = rpmpluginsCallFsmFilePrepare(plugins, fi,
Florian Festi 0408f9
-					  path, dest, st->st_mode, action);
Florian Festi 0408f9
+					  fd, path, dest,
Florian Festi 0408f9
+					  st->st_mode, action);
Florian Festi 0408f9
     }
Florian Festi 0408f9
 
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
-static int fsmCommit(char **path, rpmfi fi, rpmFileAction action, const char *suffix)
Florian Festi 0408f9
+static int fsmCommit(int dirfd, char **path, rpmfi fi, rpmFileAction action, const char *suffix)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
 
Florian Festi 0408f9
@@ -796,15 +772,18 @@
Florian Festi 0408f9
 
Florian Festi 0408f9
 	/* Rename temporary to final file name if needed. */
Florian Festi 0408f9
 	if (dest != *path) {
Florian Festi 0408f9
-	    rc = fsmRename(*path, dest);
Florian Festi 0408f9
-	    if (!rc && nsuffix) {
Florian Festi 0408f9
-		char * opath = fsmFsPath(fi, NULL);
Florian Festi 0408f9
-		rpmlog(RPMLOG_WARNING, _("%s created as %s\n"),
Florian Festi 0408f9
-		       opath, dest);
Florian Festi 0408f9
-		free(opath);
Florian Festi 0408f9
-	    }
Florian Festi 0408f9
-	    free(*path);
Florian Festi 0408f9
-	    *path = dest;
Florian Festi 0408f9
+	    rc = fsmRename(dirfd, *path, dirfd, dest);
Florian Festi 0408f9
+	    if (!rc) {
Florian Festi 0408f9
+		if (nsuffix) {
Florian Festi 0408f9
+		    char * opath = fsmFsPath(fi, NULL);
Florian Festi 0408f9
+		    rpmlog(RPMLOG_WARNING, _("%s%s created as %s%s\n"),
Florian Festi 0408f9
+			   rpmfiDN(fi), opath, rpmfiDN(fi), dest);
Florian Festi 0408f9
+		    free(opath);
Florian Festi 0408f9
+		}
Florian Festi 0408f9
+		free(*path);
Florian Festi 0408f9
+		*path = dest;
Florian Festi 0408f9
+	    } else
Florian Festi 0408f9
+		free(dest);
Florian Festi 0408f9
 	}
Florian Festi 0408f9
     }
Florian Festi 0408f9
 
Florian Festi 0408f9
@@ -855,184 +834,277 @@
Florian Festi 0408f9
     }
Florian Festi 0408f9
 }
Florian Festi 0408f9
 
Florian Festi 0408f9
+struct diriter_s {
Florian Festi 0408f9
+    int dirfd;
Florian Festi 0408f9
+    int firstdir;
Florian Festi 0408f9
+};
Florian Festi 0408f9
+
Florian Festi 0408f9
+static int onChdir(rpmfi fi, void *data)
Florian Festi 0408f9
+{
Florian Festi 0408f9
+    struct diriter_s *di = data;
Florian Festi 0408f9
+
Florian Festi 0408f9
+    fsmClose(&(di->dirfd));
Florian Festi 0408f9
+    return 0;
Florian Festi 0408f9
+}
Florian Festi 0408f9
+
Florian Festi 0408f9
+static rpmfi fsmIter(FD_t payload, rpmfiles files, rpmFileIter iter, void *data)
Florian Festi 0408f9
+{
Florian Festi 0408f9
+    rpmfi fi;
Florian Festi 0408f9
+    if (payload)
Florian Festi 0408f9
+	fi = rpmfiNewArchiveReader(payload, files, RPMFI_ITER_READ_ARCHIVE);
Florian Festi 0408f9
+    else
Florian Festi 0408f9
+	fi = rpmfilesIter(files, iter);
Florian Festi 0408f9
+    if (fi && data)
Florian Festi 0408f9
+	rpmfiSetOnChdir(fi, onChdir, data);
Florian Festi 0408f9
+    return fi;
Florian Festi 0408f9
+}
Florian Festi 0408f9
+
Florian Festi 0408f9
+static rpmfi fsmIterFini(rpmfi fi, struct diriter_s *di)
Florian Festi 0408f9
+{
Florian Festi 0408f9
+    fsmClose(&(di->dirfd));
Florian Festi 0408f9
+    fsmClose(&(di->firstdir));
Florian Festi 0408f9
+    return rpmfiFree(fi);
Florian Festi 0408f9
+}
Florian Festi 0408f9
+
Florian Festi 0408f9
 int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
Florian Festi 0408f9
               rpmpsm psm, char ** failedFile)
Florian Festi 0408f9
 {
Florian Festi 0408f9
     FD_t payload = rpmtePayload(te);
Florian Festi 0408f9
-    rpmfi fi = rpmfiNewArchiveReader(payload, files, RPMFI_ITER_READ_ARCHIVE);
Florian Festi 0408f9
+    rpmfi fi = NULL;
Florian Festi 0408f9
     rpmfs fs = rpmteGetFileStates(te);
Florian Festi 0408f9
     rpmPlugins plugins = rpmtsPlugins(ts);
Florian Festi 0408f9
-    struct stat sb;
Florian Festi 0408f9
-    int saveerrno = errno;
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
+    int fx = -1;
Florian Festi 0408f9
+    int fc = rpmfilesFC(files);
Florian Festi 0408f9
     int nodigest = (rpmtsFlags(ts) & RPMTRANS_FLAG_NOFILEDIGEST) ? 1 : 0;
Florian Festi 0408f9
     int nofcaps = (rpmtsFlags(ts) & RPMTRANS_FLAG_NOCAPS) ? 1 : 0;
Florian Festi 0408f9
-    int firsthardlink = -1;
Florian Festi 0408f9
-    FD_t firstlinkfile = NULL;
Florian Festi 0408f9
-    int skip;
Florian Festi 0408f9
-    rpmFileAction action;
Florian Festi 0408f9
+    int firstlinkfile = -1;
Florian Festi 0408f9
     char *tid = NULL;
Florian Festi 0408f9
-    const char *suffix;
Florian Festi 0408f9
-    char *fpath = NULL;
Florian Festi 0408f9
-
Florian Festi 0408f9
-    if (fi == NULL) {
Florian Festi 0408f9
-	rc = RPMERR_BAD_MAGIC;
Florian Festi 0408f9
-	goto exit;
Florian Festi 0408f9
-    }
Florian Festi 0408f9
+    struct filedata_s *fdata = xcalloc(fc, sizeof(*fdata));
Florian Festi 0408f9
+    struct filedata_s *firstlink = NULL;
Florian Festi 0408f9
+    struct diriter_s di = { -1, -1 };
Florian Festi 0408f9
 
Florian Festi 0408f9
     /* transaction id used for temporary path suffix while installing */
Florian Festi 0408f9
     rasprintf(&tid, ";%08x", (unsigned)rpmtsGetTid(ts));
Florian Festi 0408f9
 
Florian Festi 0408f9
-    /* Detect and create directories not explicitly in package. */
Florian Festi 0408f9
-    rc = fsmMkdirs(files, fs, plugins);
Florian Festi 0408f9
-
Florian Festi 0408f9
-    while (!rc) {
Florian Festi 0408f9
-	/* Read next payload header. */
Florian Festi 0408f9
-	rc = rpmfiNext(fi);
Florian Festi 0408f9
+    /* Collect state data for the whole operation */
Florian Festi 0408f9
+    fi = rpmfilesIter(files, RPMFI_ITER_FWD);
Florian Festi 0408f9
+    while (!rc && (fx = rpmfiNext(fi)) >= 0) {
Florian Festi 0408f9
+	struct filedata_s *fp = &fdata[fx];
Florian Festi 0408f9
+	if (rpmfiFFlags(fi) & RPMFILE_GHOST)
Florian Festi 0408f9
+            fp->action = FA_SKIP;
Florian Festi 0408f9
+	else
Florian Festi 0408f9
+	    fp->action = rpmfsGetAction(fs, fx);
Florian Festi 0408f9
+	fp->skip = XFA_SKIPPING(fp->action);
Florian Festi 0408f9
+	if (XFA_CREATING(fp->action) && !S_ISDIR(rpmfiFMode(fi)))
Florian Festi 0408f9
+	    fp->suffix = tid;
Florian Festi 0408f9
+	fp->fpath = fsmFsPath(fi, fp->suffix);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	if (rc < 0) {
Florian Festi 0408f9
-	    if (rc == RPMERR_ITER_END)
Florian Festi 0408f9
-		rc = 0;
Florian Festi 0408f9
-	    break;
Florian Festi 0408f9
-	}
Florian Festi 0408f9
+	/* Remap file perms, owner, and group. */
Florian Festi 0408f9
+	rc = rpmfiStat(fi, 1, &fp->sb);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	action = rpmfsGetAction(fs, rpmfiFX(fi));
Florian Festi 0408f9
-	skip = XFA_SKIPPING(action);
Florian Festi 0408f9
-	if (action != FA_TOUCH) {
Florian Festi 0408f9
-	    suffix = S_ISDIR(rpmfiFMode(fi)) ? NULL : tid;
Florian Festi 0408f9
-	} else {
Florian Festi 0408f9
-	    suffix = NULL;
Florian Festi 0408f9
-	}
Florian Festi 0408f9
-	fpath = fsmFsPath(fi, suffix);
Florian Festi 0408f9
+	/* Hardlinks are tricky and handled elsewhere for install */
Florian Festi 0408f9
+	fp->setmeta = (fp->skip == 0) &&
Florian Festi 0408f9
+		      (fp->sb.st_nlink == 1 || fp->action == FA_TOUCH);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	/* Remap file perms, owner, and group. */
Florian Festi 0408f9
-	rc = rpmfiStat(fi, 1, &sb);
Florian Festi 0408f9
+	setFileState(fs, fx);
Florian Festi 0408f9
+	fsmDebug(fp->fpath, fp->action, &fp->sb);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	fsmDebug(fpath, action, &sb);
Florian Festi 0408f9
+	fp->stage = FILE_PRE;
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+    fi = rpmfiFree(fi);
Florian Festi 0408f9
 
Florian Festi 0408f9
-        /* Exit on error. */
Florian Festi 0408f9
-        if (rc)
Florian Festi 0408f9
-            break;
Florian Festi 0408f9
+    if (rc)
Florian Festi 0408f9
+	goto exit;
Florian Festi 0408f9
 
Florian Festi 0408f9
-	/* Run fsm file pre hook for all plugins */
Florian Festi 0408f9
-	rc = rpmpluginsCallFsmFilePre(plugins, fi, fpath,
Florian Festi 0408f9
-				      sb.st_mode, action);
Florian Festi 0408f9
-	if (rc) {
Florian Festi 0408f9
-	    skip = 1;
Florian Festi 0408f9
-	} else {
Florian Festi 0408f9
-	    setFileState(fs, rpmfiFX(fi));
Florian Festi 0408f9
-	}
Florian Festi 0408f9
+    fi = fsmIter(payload, files,
Florian Festi 0408f9
+		 payload ? RPMFI_ITER_READ_ARCHIVE : RPMFI_ITER_FWD, &di);
Florian Festi 0408f9
 
Florian Festi 0408f9
-        if (!skip) {
Florian Festi 0408f9
-	    int setmeta = 1;
Florian Festi 0408f9
+    if (fi == NULL) {
Florian Festi 0408f9
+        rc = RPMERR_BAD_MAGIC;
Florian Festi 0408f9
+        goto exit;
Florian Festi 0408f9
+    }
Florian Festi 0408f9
 
Florian Festi 0408f9
-	    /* When touching we don't need any of this... */
Florian Festi 0408f9
-	    if (action == FA_TOUCH)
Florian Festi 0408f9
-		goto touch;
Florian Festi 0408f9
+    /* Process the payload */
Florian Festi 0408f9
+    while (!rc && (fx = rpmfiNext(fi)) >= 0) {
Florian Festi 0408f9
+	struct filedata_s *fp = &fdata[fx];
Florian Festi 0408f9
+
Florian Festi 0408f9
+	/*
Florian Festi 0408f9
+	 * Tricksy case: this file is a being skipped, but it's part of
Florian Festi 0408f9
+	 * a hardlinked set and has the actual content linked with it.
Florian Festi 0408f9
+	 * Write the content to the first non-skipped file of the set
Florian Festi 0408f9
+	 * instead.
Florian Festi 0408f9
+	 */
Florian Festi 0408f9
+	if (fp->skip && firstlink && rpmfiArchiveHasContent(fi))
Florian Festi 0408f9
+	    fp = firstlink;
Florian Festi 0408f9
+
Florian Festi 0408f9
+        if (!fp->skip) {
Florian Festi 0408f9
+	    int mayopen = 0;
Florian Festi 0408f9
+	    int fd = -1;
Florian Festi 0408f9
+	    rc = ensureDir(plugins, rpmfiDN(fi), 0,
Florian Festi 0408f9
+			    (fp->action == FA_CREATE), 0, &di.dirfd);
Florian Festi 0408f9
 
Florian Festi 0408f9
 	    /* Directories replacing something need early backup */
Florian Festi 0408f9
-	    if (!suffix) {
Florian Festi 0408f9
-		rc = fsmBackup(fi, action);
Florian Festi 0408f9
+	    if (!rc && !fp->suffix && fp != firstlink) {
Florian Festi 0408f9
+		rc = fsmBackup(di.dirfd, fi, fp->action);
Florian Festi 0408f9
 	    }
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    /* Run fsm file pre hook for all plugins */
Florian Festi 0408f9
+	    if (!rc)
Florian Festi 0408f9
+		rc = rpmpluginsCallFsmFilePre(plugins, fi, fp->fpath,
Florian Festi 0408f9
+					      fp->sb.st_mode, fp->action);
Florian Festi 0408f9
+	    if (rc)
Florian Festi 0408f9
+		goto setmeta; /* for error notification */
Florian Festi 0408f9
+
Florian Festi 0408f9
 	    /* Assume file does't exist when tmp suffix is in use */
Florian Festi 0408f9
-	    if (!suffix) {
Florian Festi 0408f9
-		rc = fsmVerify(fpath, fi);
Florian Festi 0408f9
+	    if (!fp->suffix) {
Florian Festi 0408f9
+		if (fp->action == FA_TOUCH) {
Florian Festi 0408f9
+		    struct stat sb;
Florian Festi 0408f9
+		    rc = fsmStat(di.dirfd, fp->fpath, 1, &sb);
Florian Festi 0408f9
+		} else {
Florian Festi 0408f9
+		    rc = fsmVerify(di.dirfd, fp->fpath, fi);
Florian Festi 0408f9
+		}
Florian Festi 0408f9
 	    } else {
Florian Festi 0408f9
 		rc = RPMERR_ENOENT;
Florian Festi 0408f9
 	    }
Florian Festi 0408f9
 
Florian Festi 0408f9
-            if (S_ISREG(sb.st_mode)) {
Florian Festi 0408f9
+	    /* See if the file was removed while our attention was elsewhere */
Florian Festi 0408f9
+	    if (rc == RPMERR_ENOENT && fp->action == FA_TOUCH) {
Florian Festi 0408f9
+		rpmlog(RPMLOG_DEBUG, "file %s vanished unexpectedly\n",
Florian Festi 0408f9
+			fp->fpath);
Florian Festi 0408f9
+		fp->action = FA_CREATE;
Florian Festi 0408f9
+		fsmDebug(fp->fpath, fp->action, &fp->sb);
Florian Festi 0408f9
+	    }
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    /* When touching we don't need any of this... */
Florian Festi 0408f9
+	    if (fp->action == FA_TOUCH)
Florian Festi 0408f9
+		goto setmeta;
Florian Festi 0408f9
+
Florian Festi 0408f9
+            if (S_ISREG(fp->sb.st_mode)) {
Florian Festi 0408f9
 		if (rc == RPMERR_ENOENT) {
Florian Festi 0408f9
-		    rc = fsmMkfile(fi, fpath, files, psm, nodigest,
Florian Festi 0408f9
-				   &setmeta, &firsthardlink, &firstlinkfile);
Florian Festi 0408f9
+		    rc = fsmMkfile(di.dirfd, fi, fp, files, psm, nodigest,
Florian Festi 0408f9
+				   &firstlink, &firstlinkfile, &di.firstdir,
Florian Festi 0408f9
+				   &fd;;
Florian Festi 0408f9
 		}
Florian Festi 0408f9
-            } else if (S_ISDIR(sb.st_mode)) {
Florian Festi 0408f9
+            } else if (S_ISDIR(fp->sb.st_mode)) {
Florian Festi 0408f9
                 if (rc == RPMERR_ENOENT) {
Florian Festi 0408f9
-                    mode_t mode = sb.st_mode;
Florian Festi 0408f9
+                    mode_t mode = fp->sb.st_mode;
Florian Festi 0408f9
                     mode &= ~07777;
Florian Festi 0408f9
                     mode |=  00700;
Florian Festi 0408f9
-                    rc = fsmMkdir(fpath, mode);
Florian Festi 0408f9
+                    rc = fsmMkdir(di.dirfd, fp->fpath, mode);
Florian Festi 0408f9
                 }
Florian Festi 0408f9
-            } else if (S_ISLNK(sb.st_mode)) {
Florian Festi 0408f9
+            } else if (S_ISLNK(fp->sb.st_mode)) {
Florian Festi 0408f9
 		if (rc == RPMERR_ENOENT) {
Florian Festi 0408f9
-		    rc = fsmSymlink(rpmfiFLink(fi), fpath);
Florian Festi 0408f9
+		    rc = fsmSymlink(rpmfiFLink(fi), di.dirfd, fp->fpath);
Florian Festi 0408f9
 		}
Florian Festi 0408f9
-            } else if (S_ISFIFO(sb.st_mode)) {
Florian Festi 0408f9
+            } else if (S_ISFIFO(fp->sb.st_mode)) {
Florian Festi 0408f9
                 /* This mimics cpio S_ISSOCK() behavior but probably isn't right */
Florian Festi 0408f9
                 if (rc == RPMERR_ENOENT) {
Florian Festi 0408f9
-                    rc = fsmMkfifo(fpath, 0000);
Florian Festi 0408f9
+                    rc = fsmMkfifo(di.dirfd, fp->fpath, 0000);
Florian Festi 0408f9
                 }
Florian Festi 0408f9
-            } else if (S_ISCHR(sb.st_mode) ||
Florian Festi 0408f9
-                       S_ISBLK(sb.st_mode) ||
Florian Festi 0408f9
-                       S_ISSOCK(sb.st_mode))
Florian Festi 0408f9
+            } else if (S_ISCHR(fp->sb.st_mode) ||
Florian Festi 0408f9
+                       S_ISBLK(fp->sb.st_mode) ||
Florian Festi 0408f9
+                       S_ISSOCK(fp->sb.st_mode))
Florian Festi 0408f9
             {
Florian Festi 0408f9
                 if (rc == RPMERR_ENOENT) {
Florian Festi 0408f9
-                    rc = fsmMknod(fpath, sb.st_mode, sb.st_rdev);
Florian Festi 0408f9
+                    rc = fsmMknod(di.dirfd, fp->fpath, fp->sb.st_mode, fp->sb.st_rdev);
Florian Festi 0408f9
                 }
Florian Festi 0408f9
             } else {
Florian Festi 0408f9
                 /* XXX Special case /dev/log, which shouldn't be packaged anyways */
Florian Festi 0408f9
-                if (!IS_DEV_LOG(fpath))
Florian Festi 0408f9
+                if (!IS_DEV_LOG(fp->fpath))
Florian Festi 0408f9
                     rc = RPMERR_UNKNOWN_FILETYPE;
Florian Festi 0408f9
             }
Florian Festi 0408f9
 
Florian Festi 0408f9
-touch:
Florian Festi 0408f9
-	    /* Set permissions, timestamps etc for non-hardlink entries */
Florian Festi 0408f9
-	    if (!rc && setmeta) {
Florian Festi 0408f9
-		rc = fsmSetmeta(fpath, fi, plugins, action, &sb, nofcaps);
Florian Festi 0408f9
+setmeta:
Florian Festi 0408f9
+	    /* Special files require path-based ops */
Florian Festi 0408f9
+	    mayopen = S_ISREG(fp->sb.st_mode) || S_ISDIR(fp->sb.st_mode);
Florian Festi 0408f9
+	    if (!rc && fd == -1 && mayopen) {
Florian Festi 0408f9
+		int flags = O_RDONLY;
Florian Festi 0408f9
+		/* Only follow safe symlinks, and never on temporary files */
Florian Festi 0408f9
+		if (fp->suffix)
Florian Festi 0408f9
+		    flags |= AT_SYMLINK_NOFOLLOW;
Florian Festi 0408f9
+		fd = fsmOpenat(di.dirfd, fp->fpath, flags,
Florian Festi 0408f9
+				S_ISDIR(fp->sb.st_mode));
Florian Festi 0408f9
+		if (fd < 0)
Florian Festi 0408f9
+		    rc = RPMERR_OPEN_FAILED;
Florian Festi 0408f9
 	    }
Florian Festi 0408f9
-        } else if (firsthardlink >= 0 && rpmfiArchiveHasContent(fi)) {
Florian Festi 0408f9
-	    /* On FA_TOUCH no hardlinks are created thus this is skipped. */
Florian Festi 0408f9
-	    /* we skip the hard linked file containing the content */
Florian Festi 0408f9
-	    /* write the content to the first used instead */
Florian Festi 0408f9
-	    char *fn = rpmfilesFN(files, firsthardlink);
Florian Festi 0408f9
-	    rc = rpmfiArchiveReadToFilePsm(fi, firstlinkfile, nodigest, psm);
Florian Festi 0408f9
-	    wfd_close(&firstlinkfile);
Florian Festi 0408f9
-	    firsthardlink = -1;
Florian Festi 0408f9
-	    free(fn);
Florian Festi 0408f9
-	}
Florian Festi 0408f9
-
Florian Festi 0408f9
-        if (rc) {
Florian Festi 0408f9
-            if (!skip) {
Florian Festi 0408f9
-                /* XXX only erase if temp fn w suffix is in use */
Florian Festi 0408f9
-                if (suffix) {
Florian Festi 0408f9
-		    (void) fsmRemove(fpath, sb.st_mode);
Florian Festi 0408f9
-                }
Florian Festi 0408f9
-                errno = saveerrno;
Florian Festi 0408f9
-            }
Florian Festi 0408f9
-        } else {
Florian Festi 0408f9
-	    /* Notify on success. */
Florian Festi 0408f9
-	    rpmpsmNotify(psm, RPMCALLBACK_INST_PROGRESS, rpmfiArchiveTell(fi));
Florian Festi 0408f9
-
Florian Festi 0408f9
-	    if (!skip) {
Florian Festi 0408f9
-		/* Backup file if needed. Directories are handled earlier */
Florian Festi 0408f9
-		if (suffix)
Florian Festi 0408f9
-		    rc = fsmBackup(fi, action);
Florian Festi 0408f9
 
Florian Festi 0408f9
-		if (!rc)
Florian Festi 0408f9
-		    rc = fsmCommit(&fpath, fi, action, suffix);
Florian Festi 0408f9
+	    if (!rc && fp->setmeta) {
Florian Festi 0408f9
+		rc = fsmSetmeta(fd, di.dirfd, fp->fpath,
Florian Festi 0408f9
+				fi, plugins, fp->action,
Florian Festi 0408f9
+				&fp->sb, nofcaps);
Florian Festi 0408f9
 	    }
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    if (fd != firstlinkfile)
Florian Festi 0408f9
+		fsmClose(&fd;;
Florian Festi 0408f9
 	}
Florian Festi 0408f9
 
Florian Festi 0408f9
+	/* Notify on success. */
Florian Festi 0408f9
 	if (rc)
Florian Festi 0408f9
-	    *failedFile = xstrdup(fpath);
Florian Festi 0408f9
+	    *failedFile = rstrscat(NULL, rpmfiDN(fi), fp->fpath, NULL);
Florian Festi 0408f9
+	else
Florian Festi 0408f9
+	    rpmpsmNotify(psm, RPMCALLBACK_INST_PROGRESS, rpmfiArchiveTell(fi));
Florian Festi 0408f9
+	fp->stage = FILE_UNPACK;
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+    fi = fsmIterFini(fi, &di);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	/* Run fsm file post hook for all plugins */
Florian Festi 0408f9
-	rpmpluginsCallFsmFilePost(plugins, fi, fpath,
Florian Festi 0408f9
-				  sb.st_mode, action, rc);
Florian Festi 0408f9
-	fpath = _free(fpath);
Florian Festi 0408f9
+    if (!rc && fx < 0 && fx != RPMERR_ITER_END)
Florian Festi 0408f9
+	rc = fx;
Florian Festi 0408f9
+
Florian Festi 0408f9
+    /* If all went well, commit files to final destination */
Florian Festi 0408f9
+    fi = fsmIter(NULL, files, RPMFI_ITER_FWD, &di);
Florian Festi 0408f9
+    while (!rc && (fx = rpmfiNext(fi)) >= 0) {
Florian Festi 0408f9
+	struct filedata_s *fp = &fdata[fx];
Florian Festi 0408f9
+
Florian Festi 0408f9
+	if (!fp->skip) {
Florian Festi 0408f9
+	    if (!rc)
Florian Festi 0408f9
+		rc = ensureDir(NULL, rpmfiDN(fi), 0, 0, 0, &di.dirfd);
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    /* Backup file if needed. Directories are handled earlier */
Florian Festi 0408f9
+	    if (!rc && fp->suffix)
Florian Festi 0408f9
+		rc = fsmBackup(di.dirfd, fi, fp->action);
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    if (!rc)
Florian Festi 0408f9
+		rc = fsmCommit(di.dirfd, &fp->fpath, fi, fp->action, fp->suffix);
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    if (!rc)
Florian Festi 0408f9
+		fp->stage = FILE_COMMIT;
Florian Festi 0408f9
+	    else
Florian Festi 0408f9
+		*failedFile = rstrscat(NULL, rpmfiDN(fi), fp->fpath, NULL);
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    /* Run fsm file post hook for all plugins for all processed files */
Florian Festi 0408f9
+	    rpmpluginsCallFsmFilePost(plugins, fi, fp->fpath,
Florian Festi 0408f9
+				      fp->sb.st_mode, fp->action, rc);
Florian Festi 0408f9
+	}
Florian Festi 0408f9
+    }
Florian Festi 0408f9
+    fi = fsmIterFini(fi, &di);
Florian Festi 0408f9
+
Florian Festi 0408f9
+    /* On failure, walk backwards and erase non-committed files */
Florian Festi 0408f9
+    if (rc) {
Florian Festi 0408f9
+	fi = fsmIter(NULL, files, RPMFI_ITER_BACK, &di);
Florian Festi 0408f9
+	while ((fx = rpmfiNext(fi)) >= 0) {
Florian Festi 0408f9
+	    struct filedata_s *fp = &fdata[fx];
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    /* If the directory doesn't exist there's nothing to clean up */
Florian Festi 0408f9
+	    if (ensureDir(NULL, rpmfiDN(fi), 0, 0, 1, &di.dirfd))
Florian Festi 0408f9
+		continue;
Florian Festi 0408f9
+
Florian Festi 0408f9
+	    if (fp->stage > FILE_NONE && !fp->skip) {
Florian Festi 0408f9
+		(void) fsmRemove(di.dirfd, fp->fpath, fp->sb.st_mode);
Florian Festi 0408f9
+	    }
Florian Festi 0408f9
+	}
Florian Festi 0408f9
     }
Florian Festi 0408f9
 
Florian Festi 0408f9
     rpmswAdd(rpmtsOp(ts, RPMTS_OP_UNCOMPRESS), fdOp(payload, FDSTAT_READ));
Florian Festi 0408f9
     rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST), fdOp(payload, FDSTAT_DIGEST));
Florian Festi 0408f9
 
Florian Festi 0408f9
 exit:
Florian Festi 0408f9
-
Florian Festi 0408f9
-    /* No need to bother with close errors on read */
Florian Festi 0408f9
-    rpmfiArchiveClose(fi);
Florian Festi 0408f9
-    rpmfiFree(fi);
Florian Festi 0408f9
+    fi = fsmIterFini(fi, &di);
Florian Festi 0408f9
     Fclose(payload);
Florian Festi 0408f9
     free(tid);
Florian Festi 0408f9
-    free(fpath);
Florian Festi 0408f9
+    for (int i = 0; i < fc; i++)
Florian Festi 0408f9
+	free(fdata[i].fpath);
Florian Festi 0408f9
+    free(fdata);
Florian Festi 0408f9
 
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }
Florian Festi 0408f9
@@ -1041,32 +1113,42 @@
Florian Festi 0408f9
 int rpmPackageFilesRemove(rpmts ts, rpmte te, rpmfiles files,
Florian Festi 0408f9
               rpmpsm psm, char ** failedFile)
Florian Festi 0408f9
 {
Florian Festi 0408f9
-    rpmfi fi = rpmfilesIter(files, RPMFI_ITER_BACK);
Florian Festi 0408f9
+    struct diriter_s di = { -1, -1 };
Florian Festi 0408f9
+    rpmfi fi = fsmIter(NULL, files, RPMFI_ITER_BACK, &di);
Florian Festi 0408f9
     rpmfs fs = rpmteGetFileStates(te);
Florian Festi 0408f9
     rpmPlugins plugins = rpmtsPlugins(ts);
Florian Festi 0408f9
-    struct stat sb;
Florian Festi 0408f9
+    int fc = rpmfilesFC(files);
Florian Festi 0408f9
+    int fx = -1;
Florian Festi 0408f9
+    struct filedata_s *fdata = xcalloc(fc, sizeof(*fdata));
Florian Festi 0408f9
     int rc = 0;
Florian Festi 0408f9
-    char *fpath = NULL;
Florian Festi 0408f9
 
Florian Festi 0408f9
-    while (!rc && rpmfiNext(fi) >= 0) {
Florian Festi 0408f9
-	rpmFileAction action = rpmfsGetAction(fs, rpmfiFX(fi));
Florian Festi 0408f9
-	fpath = fsmFsPath(fi, NULL);
Florian Festi 0408f9
-	rc = fsmStat(fpath, 1, &sb);
Florian Festi 0408f9
+    while (!rc && (fx = rpmfiNext(fi)) >= 0) {
Florian Festi 0408f9
+	struct filedata_s *fp = &fdata[fx];
Florian Festi 0408f9
+	fp->action = rpmfsGetAction(fs, rpmfiFX(fi));
Florian Festi 0408f9
+
Florian Festi 0408f9
+	if (XFA_SKIPPING(fp->action))
Florian Festi 0408f9
+	    continue;
Florian Festi 0408f9
+
Florian Festi 0408f9
+	fp->fpath = fsmFsPath(fi, NULL);
Florian Festi 0408f9
+	/* If the directory doesn't exist there's nothing to clean up */
Florian Festi 0408f9
+	if (ensureDir(NULL, rpmfiDN(fi), 0, 0, 1, &di.dirfd))
Florian Festi 0408f9
+	    continue;
Florian Festi 0408f9
+
Florian Festi 0408f9
+	rc = fsmStat(di.dirfd, fp->fpath, 1, &fp->sb);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	fsmDebug(fpath, action, &sb);
Florian Festi 0408f9
+	fsmDebug(fp->fpath, fp->action, &fp->sb);
Florian Festi 0408f9
 
Florian Festi 0408f9
 	/* Run fsm file pre hook for all plugins */
Florian Festi 0408f9
-	rc = rpmpluginsCallFsmFilePre(plugins, fi, fpath,
Florian Festi 0408f9
-				      sb.st_mode, action);
Florian Festi 0408f9
+	rc = rpmpluginsCallFsmFilePre(plugins, fi, fp->fpath,
Florian Festi 0408f9
+				      fp->sb.st_mode, fp->action);
Florian Festi 0408f9
 
Florian Festi 0408f9
-	if (!XFA_SKIPPING(action))
Florian Festi 0408f9
-	    rc = fsmBackup(fi, action);
Florian Festi 0408f9
+	rc = fsmBackup(di.dirfd, fi, fp->action);
Florian Festi 0408f9
 
Florian Festi 0408f9
         /* Remove erased files. */
Florian Festi 0408f9
-        if (action == FA_ERASE) {
Florian Festi 0408f9
+        if (fp->action == FA_ERASE) {
Florian Festi 0408f9
 	    int missingok = (rpmfiFFlags(fi) & (RPMFILE_MISSINGOK | RPMFILE_GHOST));
Florian Festi 0408f9
 
Florian Festi 0408f9
-	    rc = fsmRemove(fpath, sb.st_mode);
Florian Festi 0408f9
+	    rc = fsmRemove(di.dirfd, fp->fpath, fp->sb.st_mode);
Florian Festi 0408f9
 
Florian Festi 0408f9
 	    /*
Florian Festi 0408f9
 	     * Missing %ghost or %missingok entries are not errors.
Florian Festi 0408f9
@@ -1091,20 +1173,20 @@
Florian Festi 0408f9
 	    if (rc) {
Florian Festi 0408f9
 		int lvl = strict_erasures ? RPMLOG_ERR : RPMLOG_WARNING;
Florian Festi 0408f9
 		rpmlog(lvl, _("%s %s: remove failed: %s\n"),
Florian Festi 0408f9
-			S_ISDIR(sb.st_mode) ? _("directory") : _("file"),
Florian Festi 0408f9
-			fpath, strerror(errno));
Florian Festi 0408f9
+			S_ISDIR(fp->sb.st_mode) ? _("directory") : _("file"),
Florian Festi 0408f9
+			fp->fpath, strerror(errno));
Florian Festi 0408f9
             }
Florian Festi 0408f9
         }
Florian Festi 0408f9
 
Florian Festi 0408f9
 	/* Run fsm file post hook for all plugins */
Florian Festi 0408f9
-	rpmpluginsCallFsmFilePost(plugins, fi, fpath,
Florian Festi 0408f9
-				  sb.st_mode, action, rc);
Florian Festi 0408f9
+	rpmpluginsCallFsmFilePost(plugins, fi, fp->fpath,
Florian Festi 0408f9
+				  fp->sb.st_mode, fp->action, rc);
Florian Festi 0408f9
 
Florian Festi 0408f9
         /* XXX Failure to remove is not (yet) cause for failure. */
Florian Festi 0408f9
         if (!strict_erasures) rc = 0;
Florian Festi 0408f9
 
Florian Festi 0408f9
 	if (rc)
Florian Festi 0408f9
-	    *failedFile = xstrdup(fpath);
Florian Festi 0408f9
+	    *failedFile = rstrscat(NULL, rpmfiDN(fi), fp->fpath, NULL);
Florian Festi 0408f9
 
Florian Festi 0408f9
 	if (rc == 0) {
Florian Festi 0408f9
 	    /* Notify on success. */
Florian Festi 0408f9
@@ -1112,11 +1194,12 @@
Florian Festi 0408f9
 	    rpm_loff_t amount = rpmfiFC(fi) - rpmfiFX(fi);
Florian Festi 0408f9
 	    rpmpsmNotify(psm, RPMCALLBACK_UNINST_PROGRESS, amount);
Florian Festi 0408f9
 	}
Florian Festi 0408f9
-	fpath = _free(fpath);
Florian Festi 0408f9
     }
Florian Festi 0408f9
 
Florian Festi 0408f9
-    free(fpath);
Florian Festi 0408f9
-    rpmfiFree(fi);
Florian Festi 0408f9
+    for (int i = 0; i < fc; i++)
Florian Festi 0408f9
+	free(fdata[i].fpath);
Florian Festi 0408f9
+    free(fdata);
Florian Festi 0408f9
+    fsmIterFini(fi, &di);
Florian Festi 0408f9
 
Florian Festi 0408f9
     return rc;
Florian Festi 0408f9
 }