summaryrefslogtreecommitdiff
path: root/backport-Bury-rpmio-FD-use-to-fsmUnpack.patch
blob: c73590909d29ad1251b383b82fad26bbcec94e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
From bbc270d78fb361bd78eac9a9117070caeb537d4a Mon Sep 17 00:00:00 2001
From: Panu Matilainen <pmatilai@redhat.com>
Date: Mon, 14 Feb 2022 12:35:58 +0200
Subject: [PATCH] Bury rpmio FD use to fsmUnpack()

fsmUnpack() is the only place in FSM that needs to deal with rpmio FD
types, everywhere else they are nothing but a hindrance that need to
be converted to OS level descriptors for use. Better deal with OS
level descriptors to begin with.
---
 lib/fsm.c | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/lib/fsm.c b/lib/fsm.c
index 13b1142..b019f57 100644
--- a/lib/fsm.c
+++ b/lib/fsm.c
@@ -110,14 +110,14 @@ static int fsmSetFCaps(const char *path, const char *captxt)
     return rc;
 }
 
-static int fsmClose(FD_t *wfdp)
+static int fsmClose(int *wfdp)
 {
     int rc = 0;
-    if (wfdp && *wfdp) {
+    if (wfdp && *wfdp >= 0) {
 	int myerrno = errno;
 	static int oneshot = 0;
 	static int flush_io = 0;
-	int fdno = Fileno(*wfdp);
+	int fdno = *wfdp;
 
 	if (!oneshot) {
 	    flush_io = (rpmExpandNumeric("%{?_flush_io}") > 0);
@@ -126,61 +126,56 @@ static int fsmClose(FD_t *wfdp)
 	if (flush_io) {
 	    fsync(fdno);
 	}
-	if (Fclose(*wfdp))
+	if (close(fdno))
 	    rc = RPMERR_CLOSE_FAILED;
 
 	if (_fsm_debug) {
 	    rpmlog(RPMLOG_DEBUG, " %8s ([%d]) %s\n", __func__,
 		   fdno, (rc < 0 ? strerror(errno) : ""));
 	}
-	*wfdp = NULL;
+	*wfdp = -1;
 	errno = myerrno;
     }
     return rc;
 }
 
-static int fsmOpen(FD_t *wfdp, int dirfd, const char *dest)
+static int fsmOpen(int *wfdp, int dirfd, const char *dest)
 {
     int rc = 0;
     /* Create the file with 0200 permissions (write by owner). */
     int fd = openat(dirfd, dest, O_WRONLY|O_EXCL|O_CREAT, 0200);
 
-    if (fd >= 0) {
-	*wfdp = fdDup(fd);
-	close(fd);
-    }
-
-    if (fd < 0 || Ferror(*wfdp))
+    if (fd < 0)
 	rc = RPMERR_OPEN_FAILED;
 
     if (_fsm_debug) {
 	rpmlog(RPMLOG_DEBUG, " %8s (%s [%d]) %s\n", __func__,
-	       dest, Fileno(*wfdp), (rc < 0 ? strerror(errno) : ""));
+	       dest, fd, (rc < 0 ? strerror(errno) : ""));
     }
-
-    if (rc)
-	fsmClose(wfdp);
+    *wfdp = fd;
 
     return rc;
 }
 
-static int fsmUnpack(rpmfi fi, FD_t fd, rpmpsm psm, int nodigest)
+static int fsmUnpack(rpmfi fi, int fdno, rpmpsm psm, int nodigest)
 {
+    FD_t fd = fdDup(fdno);
     int rc = rpmfiArchiveReadToFilePsm(fi, fd, nodigest, psm);
     if (_fsm_debug) {
 	rpmlog(RPMLOG_DEBUG, " %8s (%s %" PRIu64 " bytes [%d]) %s\n", __func__,
 	       rpmfiFN(fi), rpmfiFSize(fi), Fileno(fd),
 	       (rc < 0 ? strerror(errno) : ""));
     }
+    Fclose(fd);
     return rc;
 }
 
 static int fsmMkfile(int dirfd, rpmfi fi, struct filedata_s *fp, rpmfiles files,
 		     rpmpsm psm, int nodigest,
-		     struct filedata_s ** firstlink, FD_t *firstlinkfile)
+		     struct filedata_s ** firstlink, int *firstlinkfile)
 {
     int rc = 0;
-    FD_t fd = NULL;
+    int fd = -1;
 
     if (*firstlink == NULL) {
 	/* First encounter, open file for writing */
@@ -206,7 +201,7 @@ static int fsmMkfile(int dirfd, rpmfi fi, struct filedata_s *fp, rpmfiles files,
 	if (*firstlink) {
 	    fp->setmeta = 1;
 	    *firstlink = NULL;
-	    *firstlinkfile = NULL;
+	    *firstlinkfile = -1;
 	}
     }
 
@@ -811,7 +806,7 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
     int fc = rpmfilesFC(files);
     int nodigest = (rpmtsFlags(ts) & RPMTRANS_FLAG_NOFILEDIGEST) ? 1 : 0;
     int nofcaps = (rpmtsFlags(ts) & RPMTRANS_FLAG_NOCAPS) ? 1 : 0;
-    FD_t firstlinkfile = NULL;
+    int firstlinkfile = -1;
     char *tid = NULL;
     struct filedata_s *fdata = xcalloc(fc, sizeof(*fdata));
     struct filedata_s *firstlink = NULL;
-- 
1.8.3.1