From 693d828c035848585b500dfde6f4e58cfb8d4de4 Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Mon, 14 Feb 2022 12:44:42 +0200
Subject: [PATCH] Return descriptor of created file from fsmMkfile()

This will be needed for using fd-based metadata operations.
---
 lib/fsm.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/lib/fsm.c b/lib/fsm.c
index b019f57..7c4796f 100644
--- a/lib/fsm.c
+++ b/lib/fsm.c
@@ -172,7 +172,8 @@ static int fsmUnpack(rpmfi fi, int fdno, rpmpsm psm, int nodigest)
 
 static int fsmMkfile(int dirfd, rpmfi fi, struct filedata_s *fp, rpmfiles files,
 		     rpmpsm psm, int nodigest,
-		     struct filedata_s ** firstlink, int *firstlinkfile)
+		     struct filedata_s ** firstlink, int *firstlinkfile,
+		     int *fdp)
 {
     int rc = 0;
     int fd = -1;
@@ -204,9 +205,7 @@ static int fsmMkfile(int dirfd, rpmfi fi, struct filedata_s *fp, rpmfiles files,
 	    *firstlinkfile = -1;
 	}
     }
-
-    if (fd != *firstlinkfile)
-	fsmClose(&fd);
+    *fdp = fd;
 
     return rc;
 }
@@ -1065,6 +1065,7 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
 	    fp = firstlink;
 
         if (!fp->skip) {
+	    int fd = -1;
 	    /* Directories replacing something need early backup */
 	    if (!fp->suffix && fp != firstlink) {
 		rc = fsmBackup(fi, fp->action);
@@ -910,7 +910,7 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
             if (S_ISREG(fp->sb.st_mode)) {
 		if (rc == RPMERR_ENOENT) {
 		    rc = fsmMkfile(di.dirfd, fi, fp, files, psm, nodigest,
-				   &firstlink, &firstlinkfile);
+				   &firstlink, &firstlinkfile, &fd);
 		}
             } else if (S_ISDIR(fp->sb.st_mode)) {
                 if (rc == RPMERR_ENOENT) {
@@ -1131,6 +1132,9 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
                 if (!IS_DEV_LOG(fp->fpath))
                     rc = RPMERR_UNKNOWN_FILETYPE;
             }
+
+	    if (fd != firstlinkfile)
+		fsmClose(&fd);
 	}
 
 	/* Notify on success. */
-- 
1.8.3.1

