Skip to content

Commit

Permalink
Pass arg2 to file trigger scripts
Browse files Browse the repository at this point in the history
Pass the number of instances of the target (i.e. triggering) package
left after the operation as the second argument ($2) to file trigger
scripts, similarly to regular triggers.

Don't pass it to the %transfiletrigger* variants, though, since we can't
easily obtain a count-corrected value when such a file trigger is fired
by a transaction element.  This is because when runFileTriggers() is
called in rpmtsRun(), no psm instance has been created yet and thus
psm->scriptArg or psm->countCorrection, set by rpmpsmNew(), isn't
readily available to us in runHandleTriggersInPkg().  While possible to
work around, having arg2 in transaction file triggers doesn't seem worth
the trouble.

Make arg2 correspond to the first matching package found, as opposed to
being an aggregate count of all the matching packages, even if the
latter would be easier as we wouldn't have to store the matched package
name in the mfi struct and just use rpmdbGetIteratorCount(mfi->pi).  The
reason for this is two-fold: it's consistent with how regular triggers
work and it allows file triggers to detect an upgrade (arg2 > 1) of a
triggering package.

Fixes: #2755
  • Loading branch information
dmnks committed Feb 2, 2024
1 parent d88ca30 commit 398b45b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/manual/file_triggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ directories, symlinks etc.

The file triggers are defined in spec files of packages. E.g. file trigger executing `ldconfig` could be defined in glibc package.

Similarly to regular triggers, file trigger scripts are passed the number of instances of the triggered package (as their first argument `$1`) that will remain when the file trigger has completed. This can be useful in order to perform a certain operation when the triggered package is updated or removed.
Similarly to regular triggers, file trigger scripts (except the `%transfiletrigger*` variants) are passed the number of instances of the triggered and triggering package (as the `$1` and `$2` arguments, respectively) that will remain when the file trigger has completed. This can be useful in order to perform a certain operation when either of the packages is updated or removed.

As was mentioned above there are more types of file triggers. We have two main types. File triggers execute once for package and file triggers executed once for whole transaction a.k.a transaction file triggers. Further file triggers are dived according to time of execution: before/after installation or erasure of a package or before/after a transaction.

Expand Down
12 changes: 6 additions & 6 deletions lib/psm.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ static rpmRC rpmPackageInstall(rpmts ts, rpmpsm psm)
if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERIN)) {
/* Run upper file triggers i. e. with higher priorities */
/* Run file triggers in other package(s) this package sets off. */
rc = runFileTriggers(psm->ts, psm->te, RPMSENSE_TRIGGERIN,
rc = runFileTriggers(psm->ts, psm->te, psm->scriptArg, RPMSENSE_TRIGGERIN,
RPMSCRIPT_FILETRIGGER, 1);
if (rc) break;

Expand All @@ -911,7 +911,7 @@ static rpmRC rpmPackageInstall(rpmts ts, rpmpsm psm)

/* Run lower file triggers i. e. with lower priorities */
/* Run file triggers in other package(s) this package sets off. */
rc = runFileTriggers(psm->ts, psm->te, RPMSENSE_TRIGGERIN,
rc = runFileTriggers(psm->ts, psm->te, psm->scriptArg, RPMSENSE_TRIGGERIN,
RPMSCRIPT_FILETRIGGER, 2);
if (rc) break;

Expand Down Expand Up @@ -944,7 +944,7 @@ static rpmRC rpmPackageErase(rpmts ts, rpmpsm psm)
if (rc) break;

/* Run file triggers in other package(s) this package sets off. */
rc = runFileTriggers(psm->ts, psm->te, RPMSENSE_TRIGGERUN,
rc = runFileTriggers(psm->ts, psm->te, psm->scriptArg, RPMSENSE_TRIGGERUN,
RPMSCRIPT_FILETRIGGER, 1);
if (rc) break;

Expand All @@ -969,7 +969,7 @@ static rpmRC rpmPackageErase(rpmts ts, rpmpsm psm)
if (rc) break;

/* Run file triggers in other package(s) this package sets off. */
rc = runFileTriggers(psm->ts, psm->te, RPMSENSE_TRIGGERUN,
rc = runFileTriggers(psm->ts, psm->te, psm->scriptArg, RPMSENSE_TRIGGERUN,
RPMSCRIPT_FILETRIGGER, 2);
if (rc) break;
}
Expand All @@ -982,7 +982,7 @@ static rpmRC rpmPackageErase(rpmts ts, rpmpsm psm)

/* Run file triggers in other package(s) this package sets off. */
if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
rc = runFileTriggers(psm->ts, psm->te, RPMSENSE_TRIGGERPOSTUN,
rc = runFileTriggers(psm->ts, psm->te, psm->scriptArg, RPMSENSE_TRIGGERPOSTUN,
RPMSCRIPT_FILETRIGGER, 1);
}

Expand All @@ -997,7 +997,7 @@ static rpmRC rpmPackageErase(rpmts ts, rpmpsm psm)
if (rc) break;

/* Run file triggers in other package(s) this package sets off. */
rc = runFileTriggers(psm->ts, psm->te, RPMSENSE_TRIGGERPOSTUN,
rc = runFileTriggers(psm->ts, psm->te, psm->scriptArg, RPMSENSE_TRIGGERPOSTUN,
RPMSCRIPT_FILETRIGGER, 2);
}
if (rc) break;
Expand Down
24 changes: 18 additions & 6 deletions lib/rpmtriggers.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ typedef struct matchFilesIter_s {
rpmfi fi;
rpmfs fs;
const char *pfx;
const char *pkgname;
rpmdbMatchIterator pi;
packageHash tranPkgs;
} *matchFilesIter;
Expand Down Expand Up @@ -243,12 +244,14 @@ static rpmfiles rpmtsNextFiles(matchFilesIter mfi)

if (files) {
rpmdbSetIteratorIndex(mfi->pi, ix + 1);
mfi->pkgname = rpmteN(te[0]);
} else {
/* Files are not available in memory. Read them from rpmdb */
h = rpmdbNextIterator(mfi->pi);
if (h) {
files = rpmfilesNew(pool, h, RPMTAG_BASENAMES,
RPMFI_FLAGS_FILETRIGGER);
mfi->pkgname = headerGetString(h, RPMTAG_NAME);
}
}

Expand Down Expand Up @@ -384,7 +387,7 @@ static matchFilesIter matchFilesIteratorFree(matchFilesIter mfi)
*/
static int runHandleTriggersInPkg(rpmts ts, rpmte te, Header h,
rpmsenseFlags sense, rpmscriptTriggerModes tm,
int searchMode, int ti, int arg1)
int searchMode, int ti, int arg1, int arg2)
{
int nerrors = 0;
rpmds rpmdsTriggers, rpmdsTrigger;
Expand Down Expand Up @@ -438,8 +441,12 @@ static int runHandleTriggersInPkg(rpmts ts, rpmte te, Header h,
inputFunc = (nextfilefunc) matchFilesNext;
rpmScriptSetNextFileFunc(script, inputFunc, mfi);

/* Compute our own arg2 if asked */
if (arg2 == -1 && mfi->pkgname)
arg2 = rpmdbCountPackages(rpmtsGetRdb(ts), mfi->pkgname);

nerrors += runScript(ts, NULL, h, installPrefixes.data,
script, arg1, -1);
script, arg1, arg2);
rpmtdFreeData(&installPrefixes);
rpmScriptFree(script);
}
Expand Down Expand Up @@ -489,7 +496,7 @@ static int matchFilesInTran(rpmts ts, rpmte te, const char *pfx,
return rc;
}

rpmRC runFileTriggers(rpmts ts, rpmte te, rpmsenseFlags sense,
rpmRC runFileTriggers(rpmts ts, rpmte te, int arg2, rpmsenseFlags sense,
rpmscriptTriggerModes tm, int priorityClass)
{
int nerrors = 0, i;
Expand Down Expand Up @@ -577,11 +584,11 @@ rpmRC runFileTriggers(rpmts ts, rpmte te, rpmsenseFlags sense,
if (tm == RPMSCRIPT_FILETRIGGER)
nerrors += runHandleTriggersInPkg(ts, te, trigH, sense, tm, 0,
triggers->triggerInfo[i].tix,
arg1);
arg1, arg2);
else
nerrors += runHandleTriggersInPkg(ts, te, trigH, sense, tm, 1,
triggers->triggerInfo[i].tix,
arg1);
arg1, arg2);
headerFree(trigH);
}
rpmtriggersFree(triggers);
Expand All @@ -594,15 +601,20 @@ rpmRC runImmedFileTriggers(rpmts ts, rpmte te, int arg1, rpmsenseFlags sense,
{
int nerrors = 0;
int triggersCount, i;
int arg2;
Header trigH = rpmteHeader(te);
struct rpmtd_s priorities;
rpmTagVal priorityTag;
rpmtriggers triggers;

if (tm == RPMSCRIPT_FILETRIGGER) {
priorityTag = RPMTAG_FILETRIGGERPRIORITIES;
/* Request triggering package count */
arg2 = -1;
} else {
priorityTag = RPMTAG_TRANSFILETRIGGERPRIORITIES;
/* Disable arg2 */
arg2 = -2;
}
headerGet(trigH, priorityTag, &priorities, HEADERGET_MINMEM);

Expand All @@ -629,7 +641,7 @@ rpmRC runImmedFileTriggers(rpmts ts, rpmte te, int arg1, rpmsenseFlags sense,

nerrors += runHandleTriggersInPkg(ts, te, trigH, sense, tm, 2,
triggers->triggerInfo[i].tix,
arg1);
arg1, arg2);
}
rpmtriggersFree(triggers);
headerFree(trigH);
Expand Down
2 changes: 1 addition & 1 deletion lib/rpmtriggers.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int runPostUnTransFileTrigs(rpmts ts);
* 0 to run all triggers
*/
RPM_GNUC_INTERNAL
rpmRC runFileTriggers(rpmts ts, rpmte te, rpmsenseFlags sense,
rpmRC runFileTriggers(rpmts ts, rpmte te, int arg2, rpmsenseFlags sense,
rpmscriptTriggerModes tm, int priorityClass);

/* Run file triggers in this te other package(s) set off.
Expand Down
4 changes: 2 additions & 2 deletions lib/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)
}
/* Run %transfiletriggerun scripts unless disabled */
if (!(rpmtsFlags(ts) & (RPMTRANS_FLAG_NOPRETRANS|RPMTRANS_FLAG_NOTRIGGERUN))) {
runFileTriggers(ts, NULL, RPMSENSE_TRIGGERUN,
runFileTriggers(ts, NULL, -2, RPMSENSE_TRIGGERUN,
RPMSCRIPT_TRANSFILETRIGGER, 0);
runTransScripts(ts, PKG_TRANSFILETRIGGERUN);
}
Expand All @@ -1871,7 +1871,7 @@ int rpmtsRun(rpmts ts, rpmps okProbs, rpmprobFilterFlags ignoreSet)

/* Run %transfiletriggerpostun scripts unless disabled */
if (!(rpmtsFlags(ts) & (RPMTRANS_FLAG_NOPOSTTRANS|RPMTRANS_FLAG_NOTRIGGERIN))) {
runFileTriggers(ts, NULL, RPMSENSE_TRIGGERIN, RPMSCRIPT_TRANSFILETRIGGER, 0);
runFileTriggers(ts, NULL, -2, RPMSENSE_TRIGGERIN, RPMSCRIPT_TRANSFILETRIGGER, 0);
}
if (!(rpmtsFlags(ts) & (RPMTRANS_FLAG_NOPOSTTRANS|RPMTRANS_FLAG_NOTRIGGERPOSTUN))) {
runPostUnTransFileTrigs(ts);
Expand Down
2 changes: 1 addition & 1 deletion tests/data/SPECS/filetriggers.spec
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cat
echo

%filetriggerin -p <lua> -- /usr/bin
print("filetriggerin(/usr/bin*)<lua>: "..arg[2])
print("filetriggerin(/usr/bin*)<lua>: "..arg[2].." "..arg[3])
a = rpm.next_file()
while a do
print(a)
Expand Down
22 changes: 11 additions & 11 deletions tests/rpmscript.at
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ filetriggerin(/foo*)<lua>:
/foo/hello-world
/foo/zzzz

filetriggerin(/usr/bin*): 1
filetriggerin(/usr/bin*): 1 1
/usr/bin/hello

filetriggerin(/usr/bin*)<lua>: 1
filetriggerin(/usr/bin*)<lua>: 1 1
/usr/bin/hello

transfiletriggerin(/usr/bin*): 1
Expand Down Expand Up @@ -458,10 +458,10 @@ filetriggerpostun(/foo*):
/foo/hello-world
/foo/zzzz

filetriggerun(/usr/bin*): 1
filetriggerun(/usr/bin*): 1 0
/usr/bin/hello

filetriggerpostun(/usr/bin*): 1
filetriggerpostun(/usr/bin*): 1 0
/usr/bin/hello

transfiletriggerpostun(/usr/bin*): 1
Expand All @@ -488,7 +488,7 @@ RPMTEST_CHECK([
runroot rpm -i /build/RPMS/noarch/parallel-1.0-1.noarch.rpm
],
[0],
[parallel-trigger-1.0-1 FILETRIGGERIN 1 | 12
[parallel-trigger-1.0-1 FILETRIGGERIN 1 1 | 12
parallel-trigger-1.0-1 TRANSFILETRIGGERIN 1 | 12
],
[])
Expand All @@ -498,8 +498,8 @@ runroot rpm -U /build/RPMS/noarch/parallel-2.0-1.noarch.rpm
],
[0],
[parallel-trigger-1.0-1 TRANSFILETRIGGERUN 1 | 12
parallel-trigger-1.0-1 FILETRIGGERIN 1 | 12
parallel-trigger-1.0-1 FILETRIGGERPOSTUN 1 | 11
parallel-trigger-1.0-1 FILETRIGGERIN 1 2 | 12
parallel-trigger-1.0-1 FILETRIGGERPOSTUN 1 1 | 11
parallel-trigger-1.0-1 TRANSFILETRIGGERIN 1 | 12
parallel-trigger-1.0-1 TRANSFILETRIGGERPOSTUN 1 | 0
],
Expand All @@ -510,7 +510,7 @@ runroot rpm -U /build/RPMS/noarch/parallel-trigger-2.0-1.noarch.rpm
],
[0],
[parallel-trigger-1.0-1 TRANSFILETRIGGERUN 1 | 12
parallel-trigger-2.0-1 FILETRIGGERIN 2 | 12
parallel-trigger-2.0-1 FILETRIGGERIN 2 1 | 12
parallel-trigger-2.0-1 TRANSFILETRIGGERIN 2 | 12
],
[])
Expand All @@ -520,7 +520,7 @@ runroot rpm -e parallel
],
[0],
[parallel-trigger-2.0-1 TRANSFILETRIGGERUN 1 | 12
parallel-trigger-2.0-1 FILETRIGGERPOSTUN 1 | 12
parallel-trigger-2.0-1 FILETRIGGERPOSTUN 1 0 | 12
parallel-trigger-2.0-1 TRANSFILETRIGGERPOSTUN 1 | 0
],
[])
Expand All @@ -540,7 +540,7 @@ runroot rpm -i \
runroot rpm -U /build/RPMS/noarch/parallel-trigger-1.0-1.noarch.rpm
],
[0],
[parallel-trigger-1.0-1 FILETRIGGERIN 1 | 36
[parallel-trigger-1.0-1 FILETRIGGERIN 1 3 | 36
parallel-trigger-1.0-1 TRANSFILETRIGGERIN 1 | 36
],
[])
Expand All @@ -550,7 +550,7 @@ runroot rpm -U /build/RPMS/noarch/parallel-trigger-2.0-1.noarch.rpm
],
[0],
[parallel-trigger-1.0-1 TRANSFILETRIGGERUN 1 | 36
parallel-trigger-2.0-1 FILETRIGGERIN 2 | 36
parallel-trigger-2.0-1 FILETRIGGERIN 2 3 | 36
parallel-trigger-2.0-1 TRANSFILETRIGGERIN 2 | 36
],
[])
Expand Down

0 comments on commit 398b45b

Please sign in to comment.