Panu Matilainen 5abb20
From b3d672a5523dfec033160e5cc866432a0e808649 Mon Sep 17 00:00:00 2001
Panu Matilainen 5abb20
Message-Id: <b3d672a5523dfec033160e5cc866432a0e808649.1642160603.git.pmatilai@redhat.com>
Panu Matilainen 5abb20
From: Panu Matilainen <pmatilai@redhat.com>
Panu Matilainen 5abb20
Date: Tue, 16 Nov 2021 11:49:18 +0200
Panu Matilainen 5abb20
Subject: [PATCH] Fix spurious %transfiletriggerpostun execution
Panu Matilainen 5abb20
 (RhBug:2023311)
Panu Matilainen 5abb20
Panu Matilainen 5abb20
If a package has multiple %transfiletriggerpostun triggers, any one
Panu Matilainen 5abb20
of them matching would cause all of them to run, due to disconnect
Panu Matilainen 5abb20
in the intel gathering stage: we'd gather all the headers with matching
Panu Matilainen 5abb20
files into a lump, and then add any postun triggers found in them,
Panu Matilainen 5abb20
but this loses the triggering file information and causes all postuns
Panu Matilainen 5abb20
to run.
Panu Matilainen 5abb20
Panu Matilainen 5abb20
The triggers need to be added while looping over the file matches,
Panu Matilainen 5abb20
like runFileTriggers() does. Doing so actually simplifies the code.
Panu Matilainen 5abb20
These should really be unified to use the same code, but leaving
Panu Matilainen 5abb20
that exercise to another rainy day.
Panu Matilainen 5abb20
---
Panu Matilainen 5abb20
 lib/rpmtriggers.c | 64 +++++++++++++++++++++++------------------------
Panu Matilainen 5abb20
 1 file changed, 31 insertions(+), 33 deletions(-)
Panu Matilainen 5abb20
Panu Matilainen 5abb20
diff --git a/lib/rpmtriggers.c b/lib/rpmtriggers.c
Panu Matilainen 5abb20
index d541974e8..6fe4db65d 100644
Panu Matilainen 5abb20
--- a/lib/rpmtriggers.c
Panu Matilainen 5abb20
+++ b/lib/rpmtriggers.c
Panu Matilainen 5abb20
@@ -97,19 +97,37 @@ static void rpmtriggersSortAndUniq(rpmtriggers trigs)
Panu Matilainen 5abb20
     }
Panu Matilainen 5abb20
 }
Panu Matilainen 5abb20
 
Panu Matilainen 5abb20
+static void addTriggers(rpmts ts, Header trigH, rpmsenseFlags filter)
Panu Matilainen 5abb20
+{
Panu Matilainen 5abb20
+    int tix = 0;
Panu Matilainen 5abb20
+    rpmds ds;
Panu Matilainen 5abb20
+    rpmds triggers = rpmdsNew(trigH, RPMTAG_TRANSFILETRIGGERNAME, 0);
Panu Matilainen 5abb20
+
Panu Matilainen 5abb20
+    while ((ds = rpmdsFilterTi(triggers, tix))) {
Panu Matilainen 5abb20
+	if ((rpmdsNext(ds) >= 0) && (rpmdsFlags(ds) & filter)) {
Panu Matilainen 5abb20
+	    struct rpmtd_s priorities;
Panu Matilainen 5abb20
+
Panu Matilainen 5abb20
+	    if (headerGet(trigH, RPMTAG_TRANSFILETRIGGERPRIORITIES,
Panu Matilainen 5abb20
+			&priorities, HEADERGET_MINMEM)) {
Panu Matilainen 5abb20
+		rpmtdSetIndex(&priorities, tix);
Panu Matilainen 5abb20
+		rpmtriggersAdd(ts->trigs2run, headerGetInstance(trigH),
Panu Matilainen 5abb20
+				tix, *rpmtdGetUint32(&priorities));
Panu Matilainen 5abb20
+	    }
Panu Matilainen 5abb20
+	}
Panu Matilainen 5abb20
+	rpmdsFree(ds);
Panu Matilainen 5abb20
+	tix++;
Panu Matilainen 5abb20
+    }
Panu Matilainen 5abb20
+    rpmdsFree(triggers);
Panu Matilainen 5abb20
+}
Panu Matilainen 5abb20
+
Panu Matilainen 5abb20
 void rpmtriggersPrepPostUnTransFileTrigs(rpmts ts, rpmte te)
Panu Matilainen 5abb20
 {
Panu Matilainen 5abb20
-    rpmdbMatchIterator mi;
Panu Matilainen 5abb20
     rpmdbIndexIterator ii;
Panu Matilainen 5abb20
-    Header trigH;
Panu Matilainen 5abb20
     const void *key;
Panu Matilainen 5abb20
     size_t keylen;
Panu Matilainen 5abb20
     rpmfiles files;
Panu Matilainen 5abb20
-    rpmds rpmdsTriggers;
Panu Matilainen 5abb20
-    rpmds rpmdsTrigger;
Panu Matilainen 5abb20
 
Panu Matilainen 5abb20
     ii = rpmdbIndexIteratorInit(rpmtsGetRdb(ts), RPMDBI_TRANSFILETRIGGERNAME);
Panu Matilainen 5abb20
-    mi = rpmdbNewIterator(rpmtsGetRdb(ts), RPMDBI_PACKAGES);
Panu Matilainen 5abb20
     files = rpmteFiles(te);
Panu Matilainen 5abb20
 
Panu Matilainen 5abb20
     /* Iterate over file triggers in rpmdb */
Panu Matilainen 5abb20
@@ -121,39 +139,19 @@ void rpmtriggersPrepPostUnTransFileTrigs(rpmts ts, rpmte te)
Panu Matilainen 5abb20
 	rpmfi fi = rpmfilesFindPrefix(files, pfx);
Panu Matilainen 5abb20
 	while (rpmfiNext(fi) >= 0) {
Panu Matilainen 5abb20
 	    if (RPMFILE_IS_INSTALLED(rpmfiFState(fi))) {
Panu Matilainen 5abb20
-		/* If yes then store it */
Panu Matilainen 5abb20
-		rpmdbAppendIterator(mi, rpmdbIndexIteratorPkgOffsets(ii),
Panu Matilainen 5abb20
-				rpmdbIndexIteratorNumPkgs(ii));
Panu Matilainen 5abb20
-		break;
Panu Matilainen 5abb20
+		unsigned int npkg = rpmdbIndexIteratorNumPkgs(ii);
Panu Matilainen 5abb20
+		const unsigned int *offs = rpmdbIndexIteratorPkgOffsets(ii);
Panu Matilainen 5abb20
+		/* Save any matching postun triggers */
Panu Matilainen 5abb20
+		for (int i = 0; i < npkg; i++) {
Panu Matilainen 5abb20
+		    Header h = rpmdbGetHeaderAt(rpmtsGetRdb(ts), offs[i]);
Panu Matilainen 5abb20
+		    addTriggers(ts, h, RPMSENSE_TRIGGERPOSTUN);
Panu Matilainen 5abb20
+		    headerFree(h);
Panu Matilainen 5abb20
+		}
Panu Matilainen 5abb20
 	    }
Panu Matilainen 5abb20
 	}
Panu Matilainen 5abb20
 	rpmfiFree(fi);
Panu Matilainen 5abb20
     }
Panu Matilainen 5abb20
     rpmdbIndexIteratorFree(ii);
Panu Matilainen 5abb20
-
Panu Matilainen 5abb20
-    if (rpmdbGetIteratorCount(mi)) {
Panu Matilainen 5abb20
-	/* Filter triggers and save only trans postun triggers into ts */
Panu Matilainen 5abb20
-	while ((trigH = rpmdbNextIterator(mi)) != NULL) {
Panu Matilainen 5abb20
-	    int tix = 0;
Panu Matilainen 5abb20
-	    rpmdsTriggers = rpmdsNew(trigH, RPMTAG_TRANSFILETRIGGERNAME, 0);
Panu Matilainen 5abb20
-	    while ((rpmdsTrigger = rpmdsFilterTi(rpmdsTriggers, tix))) {
Panu Matilainen 5abb20
-		if ((rpmdsNext(rpmdsTrigger) >= 0) &&
Panu Matilainen 5abb20
-		    (rpmdsFlags(rpmdsTrigger) & RPMSENSE_TRIGGERPOSTUN)) {
Panu Matilainen 5abb20
-		    struct rpmtd_s priorities;
Panu Matilainen 5abb20
-
Panu Matilainen 5abb20
-		    headerGet(trigH, RPMTAG_TRANSFILETRIGGERPRIORITIES,
Panu Matilainen 5abb20
-				&priorities, HEADERGET_MINMEM);
Panu Matilainen 5abb20
-		    rpmtdSetIndex(&priorities, tix);
Panu Matilainen 5abb20
-		    rpmtriggersAdd(ts->trigs2run, rpmdbGetIteratorOffset(mi),
Panu Matilainen 5abb20
-				    tix, *rpmtdGetUint32(&priorities));
Panu Matilainen 5abb20
-		}
Panu Matilainen 5abb20
-		rpmdsFree(rpmdsTrigger);
Panu Matilainen 5abb20
-		tix++;
Panu Matilainen 5abb20
-	    }
Panu Matilainen 5abb20
-	    rpmdsFree(rpmdsTriggers);
Panu Matilainen 5abb20
-	}
Panu Matilainen 5abb20
-    }
Panu Matilainen 5abb20
-    rpmdbFreeIterator(mi);
Panu Matilainen 5abb20
     rpmfilesFree(files);
Panu Matilainen 5abb20
 }
Panu Matilainen 5abb20
 
Panu Matilainen 5abb20
-- 
Panu Matilainen 5abb20
2.34.1
Panu Matilainen 5abb20