First step towards unifying triggers into rpmScript API
authorPanu Matilainen <pmatilai@redhat.com>
Fri, 2 Sep 2011 07:54:00 +0000 (10:54 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Fri, 2 Sep 2011 07:57:17 +0000 (10:57 +0300)
- Add a lower level script creation function to deal with the
  body expansions and such, use it for triggers as well.
- This is still fairly ugly but its something that can be reasonably
  backported to 4.9.x which needs this too, as currently triggers
  are forgetting to set script->descr, causing "(null) failure" messages
  on glibc and on others, in would just crash on trigger failure
  and/or in debug verbosity level.

lib/psm.c
lib/rpmscript.c
lib/rpmscript.h

index 1bbf60e..5f04b49 100644 (file)
--- a/lib/psm.c
+++ b/lib/psm.c
@@ -517,31 +517,21 @@ static rpmRC handleOneTrigger(const rpmpsm psm,
                arg1 += psm->countCorrection;
 
                if (triggersAlreadyRun == NULL || triggersAlreadyRun[ix] == 0) {
-                   /* XXX TODO add rpmScript API to handle this, ugh */
-                   char *macro = NULL;
-                   char *qformat = NULL;
-                   char *args[2] = { triggerProgs[ix], NULL };
-                   struct rpmScript_s script = {
-                       .tag = triggertag(psm->sense),
-                       .body = triggerScripts[ix],
-                       .flags = triggerFlags ? triggerFlags[ix] : 0,
-                       .args = args
-                   };
-
-                   if (script.body && (script.flags & RPMSCRIPT_EXPAND)) {
-                       macro = rpmExpand(script.body, NULL);
-                       script.body = macro;
-                   }
-                   if (script.body && (script.flags & RPMSCRIPT_QFORMAT)) {
-                       qformat = headerFormat(trigH, script.body, NULL);
-                       script.body = qformat;
-                   }
-
-                   rc = runScript(psm, pfx.data, &script, arg1, arg2);
+                   rpmScript script;
+
+                   script = rpmScriptNew(trigH, triggertag(psm->sense),
+                                         triggerScripts[ix],
+                                         triggerFlags ? triggerFlags[ix] : 0);
+
+                   script->args = xcalloc(2, sizeof(*script->args));
+                   script->args[0] = triggerProgs[ix];
+                   script->args[1] = NULL;
+
+                   rc = runScript(psm, pfx.data, script, arg1, arg2);
                    if (triggersAlreadyRun != NULL)
                        triggersAlreadyRun[ix] = 1;
-                   free(macro);
-                   free(qformat);
+
+                   rpmScriptFree(script);
                }
            }
        }
index 1d5ed4e..8067b85 100644 (file)
@@ -364,6 +364,32 @@ static const char * tag2sln(rpmTagVal tag)
     return "%unknownscript";
 }
 
+rpmScript rpmScriptNew(Header h, rpmTagVal tag, const char *body,
+                      rpmscriptFlags flags)
+{
+    char *nevra = headerGetAsString(h, RPMTAG_NEVRA);
+    rpmScript script = xcalloc(1, sizeof(*script));
+    script->tag = tag;
+    script->body = (body != NULL) ? xstrdup(body) : NULL;
+    rasprintf(&script->descr, "%s(%s)", tag2sln(tag), nevra);
+
+    /* macros need to be expanded before possible queryformat */
+    if (script->body && (flags & RPMSCRIPT_EXPAND)) {
+       char *body = rpmExpand(script->body, NULL);
+       free(script->body);
+       script->body = body;
+    }
+    if (script->body && (flags & RPMSCRIPT_QFORMAT)) {
+       /* XXX TODO: handle queryformat errors */
+       char *body = headerFormat(h, script->body, NULL);
+       free(script->body);
+       script->body = body;
+    }
+
+    free(nevra);
+    return script;
+}
+
 rpmScript rpmScriptFromTag(Header h, rpmTagVal scriptTag)
 {
     rpmScript script = NULL;
@@ -371,31 +397,14 @@ rpmScript rpmScriptFromTag(Header h, rpmTagVal scriptTag)
 
     if (headerIsEntry(h, scriptTag) || headerIsEntry(h, progTag)) {
        struct rpmtd_s prog;
-       char *nevra = headerGetAsString(h, RPMTAG_NEVRA);
-       rpmscriptFlags flags = headerGetNumber(h, getFlagTag(scriptTag));
-
-       script = xcalloc(1, sizeof(*script));
-       script->tag = scriptTag;
-       rasprintf(&script->descr, "%s(%s)", tag2sln(scriptTag), nevra);
-       script->body = headerGetAsString(h, scriptTag);
-
-       /* macros need to be expanded before possible queryformat */
-       if (script->body && (flags & RPMSCRIPT_EXPAND)) {
-           char *body = rpmExpand(script->body, NULL);
-           free(script->body);
-           script->body = body;
-       }
-       if (script->body && (flags & RPMSCRIPT_QFORMAT)) {
-           /* XXX TODO: handle queryformat errors */
-           char *body = headerFormat(h, script->body, NULL);
-           free(script->body);
-           script->body = body;
-       }
+
+       script = rpmScriptNew(h, scriptTag,
+                             headerGetString(h, scriptTag),
+                             headerGetNumber(h, getFlagTag(scriptTag)));
 
        if (headerGet(h, progTag, &prog, (HEADERGET_ALLOC|HEADERGET_ARGV))) {
            script->args = prog.data;
        }
-       free(nevra);
     }
     return script;
 }
index f10770c..7f9ff3b 100644 (file)
@@ -30,6 +30,10 @@ RPM_GNUC_INTERNAL
 rpmScript rpmScriptFromTag(Header h, rpmTagVal scriptTag);
 
 RPM_GNUC_INTERNAL
+rpmScript rpmScriptNew(Header h, rpmTagVal tag, const char *body,
+                      rpmscriptFlags flags);
+
+RPM_GNUC_INTERNAL
 rpmScript rpmScriptFree(rpmScript script);
 
 RPM_GNUC_INTERNAL