lib/psm.c

Go to the documentation of this file.
00001 
00006 #include "system.h"
00007 
00008 #include <rpmio_internal.h>
00009 #include <rpmlib.h>
00010 #include <rpmmacro.h>
00011 #include <rpmurl.h>
00012 #include <rpmlua.h>
00013 
00014 #include "cpio.h"
00015 #include "fsm.h"                /* XXX CPIO_FOO/FSM_FOO constants */
00016 #include "psm.h"
00017 
00018 #include "rpmds.h"
00019 
00020 #define _RPMFI_INTERNAL
00021 #include "rpmfi.h"
00022 
00023 #define _RPMTE_INTERNAL
00024 #include "rpmte.h"
00025 
00026 #define _RPMTS_INTERNAL         /* XXX ts->notify */
00027 #include "rpmts.h"
00028 
00029 #include "rpmlead.h"            /* writeLead proto */
00030 #include "signature.h"          /* signature constants */
00031 #include "legacy.h"             /* XXX rpmfiBuildFNames() */
00032 #include "misc.h"               /* XXX stripTrailingChar() */
00033 #include "rpmdb.h"              /* XXX for db_chrootDone */
00034 #include "debug.h"
00035 
00036 #define _PSM_DEBUG      0
00037 /*@unchecked@*/
00038 int _psm_debug = _PSM_DEBUG;
00039 /*@unchecked@*/
00040 int _psm_threads = 0;
00041 
00042 /* Give access to the rpmte global tracking the last instance added
00043  * to the database.
00044  */
00045 /*@-exportheadervar@*/
00046 /*@unchecked@*/
00047 extern unsigned int myinstall_instance;
00048 /*@=exportheadervar@*/
00049 
00050 /*@access FD_t @*/              /* XXX void ptr args */
00051 /*@access rpmpsm @*/
00052 
00053 /*@access rpmfi @*/
00054 /*@access rpmte @*/     /* XXX rpmInstallSourcePackage */
00055 /*@access rpmts @*/     /* XXX ts->notify */
00056 
00057 /*@access rpmluav @*/
00058 /*@access rpmtsScore @*/
00059 /*@access rpmtsScoreEntry @*/
00060 
00061 int rpmVersionCompare(Header first, Header second)
00062 {
00063     const char * one, * two;
00064     int_32 * epochOne, * epochTwo;
00065     static int_32 zero = 0;
00066     int rc;
00067 
00068     if (!headerGetEntry(first, RPMTAG_EPOCH, NULL, (void **) &epochOne, NULL))
00069         epochOne = &zero;
00070     if (!headerGetEntry(second, RPMTAG_EPOCH, NULL, (void **) &epochTwo, NULL))
00071         epochTwo = &zero;
00072 
00073 /*@-boundsread@*/
00074         if (*epochOne < *epochTwo)
00075             return -1;
00076         else if (*epochOne > *epochTwo)
00077             return 1;
00078 /*@=boundsread@*/
00079 
00080     rc = headerGetEntry(first, RPMTAG_VERSION, NULL, (void **) &one, NULL);
00081     rc = headerGetEntry(second, RPMTAG_VERSION, NULL, (void **) &two, NULL);
00082 
00083     rc = rpmvercmp(one, two);
00084     if (rc)
00085         return rc;
00086 
00087     rc = headerGetEntry(first, RPMTAG_RELEASE, NULL, (void **) &one, NULL);
00088     rc = headerGetEntry(second, RPMTAG_RELEASE, NULL, (void **) &two, NULL);
00089 
00090     return rpmvercmp(one, two);
00091 }
00092 
00097 /*@observer@*/ /*@unchecked@*/
00098 static struct tagMacro {
00099 /*@observer@*/ /*@null@*/ const char *  macroname; 
00100     rpmTag      tag;            
00101 } tagMacros[] = {
00102     { "name",           RPMTAG_NAME },
00103     { "version",        RPMTAG_VERSION },
00104     { "release",        RPMTAG_RELEASE },
00105     { "epoch",          RPMTAG_EPOCH },
00106     { NULL, 0 }
00107 };
00108 
00115 static int rpmInstallLoadMacros(rpmfi fi, Header h)
00116         /*@globals rpmGlobalMacroContext @*/
00117         /*@modifies rpmGlobalMacroContext @*/
00118 {
00119     HGE_t hge = (HGE_t) fi->hge;
00120     struct tagMacro * tagm;
00121     union {
00122 /*@unused@*/ void * ptr;
00123 /*@unused@*/ const char ** argv;
00124         const char * str;
00125         int_32 * i32p;
00126     } body;
00127     char numbuf[32];
00128     rpmTagType type;
00129 
00130     for (tagm = tagMacros; tagm->macroname != NULL; tagm++) {
00131         if (!hge(h, tagm->tag, &type, (void **) &body, NULL))
00132             continue;
00133         switch (type) {
00134         case RPM_INT32_TYPE:
00135 /*@-boundsread@*/
00136             sprintf(numbuf, "%d", *body.i32p);
00137 /*@=boundsread@*/
00138             addMacro(NULL, tagm->macroname, NULL, numbuf, -1);
00139             /*@switchbreak@*/ break;
00140         case RPM_STRING_TYPE:
00141             addMacro(NULL, tagm->macroname, NULL, body.str, -1);
00142             /*@switchbreak@*/ break;
00143         case RPM_NULL_TYPE:
00144         case RPM_CHAR_TYPE:
00145         case RPM_INT8_TYPE:
00146         case RPM_INT16_TYPE:
00147         case RPM_BIN_TYPE:
00148         case RPM_STRING_ARRAY_TYPE:
00149         case RPM_I18NSTRING_TYPE:
00150         default:
00151             /*@switchbreak@*/ break;
00152         }
00153     }
00154     return 0;
00155 }
00156 
00162 /*@-bounds@*/
00163 static rpmRC markReplacedFiles(const rpmpsm psm)
00164         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00165         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
00166 {
00167     const rpmts ts = psm->ts;
00168     rpmfi fi = psm->fi;
00169     HGE_t hge = (HGE_t)fi->hge;
00170     sharedFileInfo replaced = fi->replaced;
00171     sharedFileInfo sfi;
00172     rpmdbMatchIterator mi;
00173     Header h;
00174     unsigned int * offsets;
00175     unsigned int prev;
00176     int num, xx;
00177 
00178     if (!(rpmfiFC(fi) > 0 && fi->replaced))
00179         return RPMRC_OK;
00180 
00181     num = prev = 0;
00182     for (sfi = replaced; sfi->otherPkg; sfi++) {
00183         if (prev && prev == sfi->otherPkg)
00184             continue;
00185         prev = sfi->otherPkg;
00186         num++;
00187     }
00188     if (num == 0)
00189         return RPMRC_OK;
00190 
00191     offsets = alloca(num * sizeof(*offsets));
00192     offsets[0] = 0;
00193     num = prev = 0;
00194     for (sfi = replaced; sfi->otherPkg; sfi++) {
00195         if (prev && prev == sfi->otherPkg)
00196             continue;
00197         prev = sfi->otherPkg;
00198         offsets[num++] = sfi->otherPkg;
00199     }
00200 
00201     mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
00202     xx = rpmdbAppendIterator(mi, offsets, num);
00203     xx = rpmdbSetIteratorRewrite(mi, 1);
00204 
00205     sfi = replaced;
00206     while ((h = rpmdbNextIterator(mi)) != NULL) {
00207         char * secStates;
00208         int modified;
00209         int count;
00210 
00211         modified = 0;
00212 
00213         if (!hge(h, RPMTAG_FILESTATES, NULL, (void **)&secStates, &count))
00214             continue;
00215         
00216         prev = rpmdbGetIteratorOffset(mi);
00217         num = 0;
00218         while (sfi->otherPkg && sfi->otherPkg == prev) {
00219             assert(sfi->otherFileNum < count);
00220             if (secStates[sfi->otherFileNum] != RPMFILE_STATE_REPLACED) {
00221                 secStates[sfi->otherFileNum] = RPMFILE_STATE_REPLACED;
00222                 if (modified == 0) {
00223                     /* Modified header will be rewritten. */
00224                     modified = 1;
00225                     xx = rpmdbSetIteratorModified(mi, modified);
00226                 }
00227                 num++;
00228             }
00229             sfi++;
00230         }
00231     }
00232     mi = rpmdbFreeIterator(mi);
00233 
00234     return RPMRC_OK;
00235 }
00236 /*@=bounds@*/
00237 
00238 rpmRC rpmInstallSourcePackage(rpmts ts, FD_t fd,
00239                 const char ** specFilePtr, const char ** cookie)
00240 {
00241     int scareMem = 1;
00242     rpmfi fi = NULL;
00243     const char * _sourcedir = NULL;
00244     const char * _specdir = NULL;
00245     const char * specFile = NULL;
00246     HGE_t hge;
00247     HFD_t hfd;
00248     Header h = NULL;
00249     struct rpmpsm_s psmbuf;
00250     rpmpsm psm = &psmbuf;
00251     int isSource;
00252     rpmRC rpmrc;
00253     int i;
00254 
00255     memset(psm, 0, sizeof(*psm));
00256     psm->ts = rpmtsLink(ts, "InstallSourcePackage");
00257 
00258     rpmrc = rpmReadPackageFile(ts, fd, "InstallSourcePackage", &h);
00259     switch (rpmrc) {
00260     case RPMRC_NOTTRUSTED:
00261     case RPMRC_NOKEY:
00262     case RPMRC_OK:
00263         break;
00264     default:
00265         goto exit;
00266         /*@notreached@*/ break;
00267     }
00268     if (h == NULL)
00269         goto exit;
00270 
00271     rpmrc = RPMRC_OK;
00272 
00273     isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
00274 
00275     if (!isSource) {
00276         rpmError(RPMERR_NOTSRPM, _("source package expected, binary found\n"));
00277         rpmrc = RPMRC_FAIL;
00278         goto exit;
00279     }
00280 
00281     if (rpmtsAddInstallElement(ts, h, NULL, 0, NULL)) {
00282         rpmrc = RPMRC_FAIL;
00283         goto exit;
00284     }
00285 
00286     fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, scareMem);
00287     h = headerFree(h);
00288 
00289     if (fi == NULL) {   /* XXX can't happen */
00290         rpmrc = RPMRC_FAIL;
00291         goto exit;
00292     }
00293 
00294 /*@-onlytrans@*/        /* FIX: te reference */
00295     fi->te = rpmtsElement(ts, 0);
00296 /*@=onlytrans@*/
00297     if (fi->te == NULL) {       /* XXX can't happen */
00298         rpmrc = RPMRC_FAIL;
00299         goto exit;
00300     }
00301 
00302 /*@-nullpass@*/         /* FIX fi->h may be null */
00303     fi->te->h = headerLink(fi->h);
00304 /*@=nullpass@*/
00305     fi->te->fd = fdLink(fd, "installSourcePackage");
00306     hge = fi->hge;
00307     hfd = fi->hfd;
00308 
00309 /*@i@*/ (void) rpmInstallLoadMacros(fi, fi->h);
00310 
00311     psm->fi = rpmfiLink(fi, NULL);
00312     /*@-assignexpose -usereleased @*/
00313     psm->te = fi->te;
00314     /*@=assignexpose =usereleased @*/
00315 
00316     if (cookie) {
00317         *cookie = NULL;
00318         if (hge(fi->h, RPMTAG_COOKIE, NULL, (void **) cookie, NULL))
00319             *cookie = xstrdup(*cookie);
00320     }
00321 
00322     /* XXX FIXME: can't do endian neutral MD5 verification yet. */
00323 /*@i@*/ fi->fmd5s = hfd(fi->fmd5s, -1);
00324 
00325     /* XXX FIXME: don't do per-file mapping, force global flags. */
00326     fi->fmapflags = _free(fi->fmapflags);
00327     fi->mapflags = CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
00328 
00329     fi->uid = getuid();
00330     fi->gid = getgid();
00331     fi->astriplen = 0;
00332     fi->striplen = 0;
00333 
00334     for (i = 0; i < fi->fc; i++)
00335         fi->actions[i] = FA_CREATE;
00336 
00337     i = fi->fc;
00338 
00339     if (fi->h != NULL) {        /* XXX can't happen */
00340         rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
00341 
00342         if (headerIsEntry(fi->h, RPMTAG_COOKIE))
00343             for (i = 0; i < fi->fc; i++)
00344                 if (fi->fflags[i] & RPMFILE_SPECFILE) break;
00345     }
00346 
00347     if (i == fi->fc) {
00348         /* Find the spec file by name. */
00349         for (i = 0; i < fi->fc; i++) {
00350             const char * t = fi->apath[i];
00351             t += strlen(fi->apath[i]) - 5;
00352             if (!strcmp(t, ".spec")) break;
00353         }
00354     }
00355 
00356     _sourcedir = rpmGenPath(rpmtsRootDir(ts), "%{_sourcedir}", "");
00357     rpmrc = rpmMkdirPath(_sourcedir, "sourcedir");
00358     if (rpmrc) {
00359         rpmrc = RPMRC_FAIL;
00360         goto exit;
00361     }
00362 
00363     _specdir = rpmGenPath(rpmtsRootDir(ts), "%{_specdir}", "");
00364     rpmrc = rpmMkdirPath(_specdir, "specdir");
00365     if (rpmrc) {
00366         rpmrc = RPMRC_FAIL;
00367         goto exit;
00368     }
00369 
00370     /* Build dnl/dil with {_sourcedir, _specdir} as values. */
00371     if (i < fi->fc) {
00372         int speclen = strlen(_specdir) + 2;
00373         int sourcelen = strlen(_sourcedir) + 2;
00374         char * t;
00375 
00376 /*@i@*/ fi->dnl = hfd(fi->dnl, -1);
00377 
00378         fi->dc = 2;
00379         fi->dnl = xmalloc(fi->dc * sizeof(*fi->dnl)
00380                         + fi->fc * sizeof(*fi->dil)
00381                         + speclen + sourcelen);
00382         /*@-dependenttrans@*/
00383         fi->dil = (int *)(fi->dnl + fi->dc);
00384         /*@=dependenttrans@*/
00385         memset(fi->dil, 0, fi->fc * sizeof(*fi->dil));
00386         fi->dil[i] = 1;
00387         /*@-dependenttrans@*/
00388         fi->dnl[0] = t = (char *)(fi->dil + fi->fc);
00389         fi->dnl[1] = t = stpcpy( stpcpy(t, _sourcedir), "/") + 1;
00390         /*@=dependenttrans@*/
00391         (void) stpcpy( stpcpy(t, _specdir), "/");
00392 
00393         t = xmalloc(speclen + strlen(fi->bnl[i]) + 1);
00394         (void) stpcpy( stpcpy( stpcpy(t, _specdir), "/"), fi->bnl[i]);
00395         specFile = t;
00396     } else {
00397         rpmError(RPMERR_NOSPEC, _("source package contains no .spec file\n"));
00398         rpmrc = RPMRC_FAIL;
00399         goto exit;
00400     }
00401 
00402     psm->goal = PSM_PKGINSTALL;
00403 
00404     /*@-compmempass@*/  /* FIX: psm->fi->dnl should be owned. */
00405     rpmrc = rpmpsmStage(psm, PSM_PROCESS);
00406 
00407     (void) rpmpsmStage(psm, PSM_FINI);
00408     /*@=compmempass@*/
00409 
00410     if (rpmrc) rpmrc = RPMRC_FAIL;
00411 
00412 exit:
00413     if (specFilePtr && specFile && rpmrc == RPMRC_OK)
00414         *specFilePtr = specFile;
00415     else
00416         specFile = _free(specFile);
00417 
00418     _specdir = _free(_specdir);
00419     _sourcedir = _free(_sourcedir);
00420 
00421     psm->fi = rpmfiFree(psm->fi);
00422     psm->te = NULL;
00423 
00424     if (h != NULL) h = headerFree(h);
00425 
00426     /*@-branchstate@*/
00427     if (fi != NULL) {
00428         fi->te->h = headerFree(fi->te->h);
00429         if (fi->te->fd != NULL)
00430             (void) Fclose(fi->te->fd);
00431         fi->te->fd = NULL;
00432         fi->te = NULL;
00433         fi = rpmfiFree(fi);
00434     }
00435     /*@=branchstate@*/
00436 
00437     /* XXX nuke the added package(s). */
00438     rpmtsClean(ts);
00439 
00440     psm->ts = rpmtsFree(psm->ts);
00441 
00442     return rpmrc;
00443 }
00444 
00445 /*@observer@*/ /*@unchecked@*/
00446 static char * SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin";
00447 
00453 static /*@observer@*/ const char * const tag2sln(int tag)
00454         /*@*/
00455 {
00456     switch (tag) {
00457     case RPMTAG_PREIN:          return "%pre";
00458     case RPMTAG_POSTIN:         return "%post";
00459     case RPMTAG_PREUN:          return "%preun";
00460     case RPMTAG_POSTUN:         return "%postun";
00461     case RPMTAG_VERIFYSCRIPT:   return "%verify";
00462     }
00463     return "%unknownscript";
00464 }
00465 
00471 static pid_t psmWait(rpmpsm psm)
00472         /*@globals fileSystem, internalState @*/
00473         /*@modifies psm, fileSystem, internalState @*/
00474 {
00475     const rpmts ts = psm->ts;
00476     rpmtime_t msecs;
00477 
00478     (void) rpmsqWait(&psm->sq);
00479     msecs = psm->sq.op.usecs/1000;
00480     (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_SCRIPTLETS), &psm->sq.op);
00481 
00482     rpmMessage(RPMMESS_DEBUG,
00483         _("%s: waitpid(%d) rc %d status %x secs %u.%03u\n"),
00484         psm->stepName, (unsigned)psm->sq.child,
00485         (unsigned)psm->sq.reaped, psm->sq.status,
00486         (unsigned)msecs/1000, (unsigned)msecs%1000);
00487 
00488     return psm->sq.reaped;
00489 }
00490 
00491 #ifdef WITH_LUA
00492 
00495 static rpmRC runLuaScript(rpmpsm psm, Header h, const char *sln,
00496                    int progArgc, const char **progArgv,
00497                    const char *script, int arg1, int arg2)
00498         /*@globals fileSystem, internalState @*/
00499         /*@modifies psm, fileSystem, internalState @*/
00500 {
00501     const rpmts ts = psm->ts;
00502     int rootFd = -1;
00503     const char *n, *v, *r;
00504     rpmRC rc = RPMRC_OK;
00505     int i;
00506     int xx;
00507     rpmlua lua = NULL; /* Global state. */
00508     rpmluav var;
00509 
00510     xx = headerNVR(h, &n, &v, &r);
00511 
00512     if (!rpmtsChrootDone(ts)) {
00513         const char *rootDir = rpmtsRootDir(ts);
00514         xx = chdir("/");
00515 /*@-nullpass@*/
00516         rootFd = open(".", O_RDONLY, 0);
00517 /*@=nullpass@*/
00518         if (rootFd >= 0) {
00519             /*@-superuser -noeffect @*/
00520             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
00521                 xx = chroot(rootDir);
00522             /*@=superuser =noeffect @*/
00523             xx = rpmtsSetChrootDone(ts, 1);
00524         }
00525     }
00526 
00527     /* Create arg variable */
00528     rpmluaPushTable(lua, "arg");
00529     var = rpmluavNew();
00530     rpmluavSetListMode(var, 1);
00531 /*@+relaxtypes@*/
00532     if (progArgv) {
00533         for (i = 0; i < progArgc && progArgv[i]; i++) {
00534             rpmluavSetValue(var, RPMLUAV_STRING, progArgv[i]);
00535             rpmluaSetVar(lua, var);
00536         }
00537     }
00538     if (arg1 >= 0) {
00539         rpmluavSetValueNum(var, arg1);
00540         rpmluaSetVar(lua, var);
00541     }
00542     if (arg2 >= 0) {
00543         rpmluavSetValueNum(var, arg2);
00544         rpmluaSetVar(lua, var);
00545     }
00546 /*@=relaxtypes@*/
00547 /*@-moduncon@*/
00548     var = rpmluavFree(var);
00549 /*@=moduncon@*/
00550     rpmluaPop(lua);
00551 
00552     {
00553         char buf[BUFSIZ];
00554         xx = snprintf(buf, BUFSIZ, "%s(%s-%s-%s)", sln, n, v, r);
00555         if (rpmluaRunScript(lua, script, buf) == -1)
00556             rc = RPMRC_FAIL;
00557     }
00558 
00559     rpmluaDelVar(lua, "arg");
00560 
00561     if (rootFd >= 0) {
00562         const char *rootDir = rpmtsRootDir(ts);
00563         xx = fchdir(rootFd);
00564         xx = close(rootFd);
00565         /*@-superuser -noeffect @*/
00566         if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
00567             xx = chroot(".");
00568         /*@=superuser =noeffect @*/
00569         xx = rpmtsSetChrootDone(ts, 0);
00570     }
00571 
00572     return rc;
00573 }
00574 #endif
00575 
00578 /*@unchecked@*/
00579 static int ldconfig_done = 0;
00580 
00581 /*@unchecked@*/ /*@observer@*/ /*@null@*/
00582 static const char * ldconfig_path = "/sbin/ldconfig";
00583 
00602 static rpmRC runScript(rpmpsm psm, Header h, const char * sln,
00603                 int progArgc, const char ** progArgv,
00604                 const char * script, int arg1, int arg2)
00605         /*@globals ldconfig_done, rpmGlobalMacroContext, h_errno,
00606                 fileSystem, internalState@*/
00607         /*@modifies psm, ldconfig_done, rpmGlobalMacroContext,
00608                 fileSystem, internalState @*/
00609 {
00610     const rpmts ts = psm->ts;
00611     rpmfi fi = psm->fi;
00612     HGE_t hge = fi->hge;
00613     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00614     const char ** argv = NULL;
00615     int argc = 0;
00616     const char ** prefixes = NULL;
00617     int numPrefixes;
00618     rpmTagType ipt;
00619     const char * oldPrefix;
00620     int maxPrefixLength;
00621     int len;
00622     char * prefixBuf = NULL;
00623     const char * fn = NULL;
00624     int xx;
00625     int i;
00626     int freePrefixes = 0;
00627     FD_t scriptFd;
00628     FD_t out;
00629     rpmRC rc = RPMRC_OK;
00630     const char *n, *v, *r, *a;
00631 
00632     if (progArgv == NULL && script == NULL)
00633         return rc;
00634 
00635     /* XXX FIXME: except for %verifyscript, rpmteNEVR can be used. */
00636     xx = headerNVR(h, &n, &v, &r);
00637     xx = hge(h, RPMTAG_ARCH, NULL, (void **) &a, NULL);
00638 
00639     if (progArgv && strcmp(progArgv[0], "<lua>") == 0) {
00640 #ifdef WITH_LUA
00641         rpmMessage(RPMMESS_DEBUG,
00642                 _("%s: %s(%s-%s-%s.%s) running <lua> scriptlet.\n"),
00643                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a);
00644         return runLuaScript(psm, h, sln, progArgc, progArgv,
00645                             script, arg1, arg2);
00646 #else
00647         return RPMRC_FAIL;
00648 #endif
00649     }
00650 
00651     psm->sq.reaper = 1;
00652 
00653     /*
00654      * If a successor node, and ldconfig was just run, don't bother.
00655      */
00656     if (ldconfig_path && progArgv != NULL && psm->unorderedSuccessor) {
00657         if (ldconfig_done && !strcmp(progArgv[0], ldconfig_path)) {
00658             rpmMessage(RPMMESS_DEBUG,
00659                 _("%s: %s(%s-%s-%s.%s) skipping redundant \"%s\".\n"),
00660                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a,
00661                 progArgv[0]);
00662             return rc;
00663         }
00664     }
00665 
00666     rpmMessage(RPMMESS_DEBUG,
00667                 _("%s: %s(%s-%s-%s.%s) %ssynchronous scriptlet start\n"),
00668                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a,
00669                 (psm->unorderedSuccessor ? "a" : ""));
00670 
00671     if (!progArgv) {
00672         argv = alloca(5 * sizeof(*argv));
00673         argv[0] = "/bin/sh";
00674         argc = 1;
00675         ldconfig_done = 0;
00676     } else {
00677         argv = alloca((progArgc + 4) * sizeof(*argv));
00678         memcpy(argv, progArgv, progArgc * sizeof(*argv));
00679         argc = progArgc;
00680         ldconfig_done = (ldconfig_path && !strcmp(argv[0], ldconfig_path)
00681                 ? 1 : 0);
00682     }
00683 
00684 #if __ia64__
00685     /* XXX This assumes that all interpreters are elf executables. */
00686     if ((a != NULL && a[0] == 'i' && a[1] != '\0' && a[2] == '8' && a[3] == '6')
00687      && strcmp(argv[0], "/sbin/ldconfig"))
00688     {
00689         const char * fmt = rpmGetPath("%{?_autorelocate_path}", NULL);
00690         const char * errstr;
00691         char * newPath;
00692         char * t;
00693 
00694         newPath = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
00695         fmt = _free(fmt);
00696 
00697         /* XXX On ia64, change leading /emul/ix86 -> /emul/ia32, ick. */
00698         if (newPath != NULL && *newPath != '\0'
00699          && strlen(newPath) >= (sizeof("/emul/i386")-1)
00700          && newPath[0] == '/' && newPath[1] == 'e' && newPath[2] == 'm'
00701          && newPath[3] == 'u' && newPath[4] == 'l' && newPath[5] == '/'
00702          && newPath[6] == 'i' && newPath[8] == '8' && newPath[9] == '6')
00703         {
00704             newPath[7] = 'a';
00705             newPath[8] = '3';
00706             newPath[9] = '2';
00707         }
00708 
00709         t = alloca(strlen(newPath) + strlen(argv[0]) + 1);
00710         *t = '\0';
00711         (void) stpcpy( stpcpy(t, newPath), argv[0]);
00712         newPath = _free(newPath);
00713         argv[0] = t;
00714     }
00715 #endif
00716 
00717     if (hge(h, RPMTAG_INSTPREFIXES, &ipt, (void **) &prefixes, &numPrefixes)) {
00718         freePrefixes = 1;
00719     } else if (hge(h, RPMTAG_INSTALLPREFIX, NULL, (void **) &oldPrefix, NULL)) {
00720         prefixes = &oldPrefix;
00721         numPrefixes = 1;
00722     } else {
00723         numPrefixes = 0;
00724     }
00725 
00726     maxPrefixLength = 0;
00727     if (prefixes != NULL)
00728     for (i = 0; i < numPrefixes; i++) {
00729         len = strlen(prefixes[i]);
00730         if (len > maxPrefixLength) maxPrefixLength = len;
00731     }
00732     prefixBuf = alloca(maxPrefixLength + 50);
00733 
00734     if (script) {
00735         const char * rootDir = rpmtsRootDir(ts);
00736         FD_t fd;
00737 
00738         /*@-branchstate@*/
00739         if (makeTempFile((!rpmtsChrootDone(ts) ? rootDir : "/"), &fn, &fd)) {
00740             if (prefixes != NULL && freePrefixes) free(prefixes);
00741             return RPMRC_FAIL;
00742         }
00743         /*@=branchstate@*/
00744 
00745         if (rpmIsDebug() &&
00746             (!strcmp(argv[0], "/bin/sh") || !strcmp(argv[0], "/bin/bash")))
00747         {
00748             static const char set_x[] = "set -x\n";
00749             xx = Fwrite(set_x, sizeof(set_x[0]), sizeof(set_x)-1, fd);
00750         }
00751 
00752         if (ldconfig_path && strstr(script, ldconfig_path) != NULL)
00753             ldconfig_done = 1;
00754 
00755         xx = Fwrite(script, sizeof(script[0]), strlen(script), fd);
00756         xx = Fclose(fd);
00757 
00758         {   const char * sn = fn;
00759             if (!rpmtsChrootDone(ts) && rootDir != NULL &&
00760                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00761             {
00762                 sn += strlen(rootDir)-1;
00763             }
00764             argv[argc++] = sn;
00765         }
00766 
00767         if (arg1 >= 0) {
00768             char *av = alloca(20);
00769             sprintf(av, "%d", arg1);
00770             argv[argc++] = av;
00771         }
00772         if (arg2 >= 0) {
00773             char *av = alloca(20);
00774             sprintf(av, "%d", arg2);
00775             argv[argc++] = av;
00776         }
00777     }
00778 
00779     argv[argc] = NULL;
00780 
00781     scriptFd = rpmtsScriptFd(ts);
00782     if (scriptFd != NULL) {
00783         if (rpmIsVerbose()) {
00784             out = fdDup(Fileno(scriptFd));
00785         } else {
00786             out = Fopen("/dev/null", "w.fdio");
00787             if (Ferror(out)) {
00788                 out = fdDup(Fileno(scriptFd));
00789             }
00790         }
00791     } else {
00792         out = fdDup(STDOUT_FILENO);
00793     }
00794     if (out == NULL) return RPMRC_FAIL; /* XXX can't happen */
00795 
00796     /*@-branchstate@*/
00797     xx = rpmsqFork(&psm->sq);
00798     if (psm->sq.child == 0) {
00799         const char * rootDir;
00800         int pipes[2];
00801         int flag;
00802         int fdno;
00803 
00804         pipes[0] = pipes[1] = 0;
00805         /* make stdin inaccessible */
00806         xx = pipe(pipes);
00807         xx = close(pipes[1]);
00808         xx = dup2(pipes[0], STDIN_FILENO);
00809         xx = close(pipes[0]);
00810 
00811         /* XXX Force FD_CLOEXEC on 1st 100 inherited fdno's. */
00812         for (fdno = 3; fdno < 100; fdno++) {
00813             flag = fcntl(fdno, F_GETFD);
00814             if (flag == -1 || (flag & FD_CLOEXEC))
00815                 continue;
00816             xx = fcntl(fdno, F_SETFD, FD_CLOEXEC);
00817             /* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */
00818         }
00819 
00820         if (scriptFd != NULL) {
00821             int sfdno = Fileno(scriptFd);
00822             int ofdno = Fileno(out);
00823             if (sfdno != STDERR_FILENO)
00824                 xx = dup2(sfdno, STDERR_FILENO);
00825             if (ofdno != STDOUT_FILENO)
00826                 xx = dup2(ofdno, STDOUT_FILENO);
00827             /* make sure we don't close stdin/stderr/stdout by mistake! */
00828             if (ofdno > STDERR_FILENO && ofdno != sfdno)
00829                 xx = Fclose (out);
00830             if (sfdno > STDERR_FILENO && ofdno != sfdno)
00831                 xx = Fclose (scriptFd);
00832         }
00833 
00834         {   const char *ipath = rpmExpand("PATH=%{_install_script_path}", NULL);
00835             const char *path = SCRIPT_PATH;
00836 
00837             if (ipath && ipath[5] != '%')
00838                 path = ipath;
00839 
00840             xx = doputenv(path);
00841             /*@-modobserver@*/
00842             ipath = _free(ipath);
00843             /*@=modobserver@*/
00844         }
00845 
00846         if (prefixes != NULL)
00847         for (i = 0; i < numPrefixes; i++) {
00848             sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
00849             xx = doputenv(prefixBuf);
00850 
00851             /* backwards compatibility */
00852             if (i == 0) {
00853                 sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
00854                 xx = doputenv(prefixBuf);
00855             }
00856         }
00857 
00858         rootDir = ts->rootDir;  /* HACK: rootDir = rpmtsRootDir(ts); instead */
00859         if (rootDir  != NULL)   /* XXX can't happen */
00860         switch(urlIsURL(rootDir)) {
00861         case URL_IS_PATH:
00862             rootDir += sizeof("file://") - 1;
00863             rootDir = strchr(rootDir, '/');
00864             /*@fallthrough@*/
00865         case URL_IS_UNKNOWN:
00866             if (!rpmtsChrootDone(ts) &&
00867                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00868             {
00869                 /*@-superuser -noeffect @*/
00870                 xx = chroot(rootDir);
00871                 /*@=superuser =noeffect @*/
00872             }
00873             xx = chdir("/");
00874             rpmMessage(RPMMESS_DEBUG, _("%s: %s(%s-%s-%s.%s)\texecv(%s) pid %d\n"),
00875                         psm->stepName, sln, n, v, r, a,
00876                         argv[0], (unsigned)getpid());
00877 
00878             /* XXX Don't mtrace into children. */
00879             unsetenv("MALLOC_CHECK_");
00880 
00881             /* Permit libselinux to do the scriptlet exec. */
00882             if (rpmtsSELinuxEnabled(ts) == 1) { 
00883 /*@-moduncon@*/
00884                 xx = rpm_execcon(0, argv[0], argv, environ);
00885 /*@=moduncon@*/
00886                 if (xx != 0)
00887                     break;
00888             }
00889 
00890 /*@-nullstate@*/
00891             xx = execv(argv[0], (char *const *)argv);
00892 /*@=nullstate@*/
00893             break;
00894         case URL_IS_HTTPS:
00895         case URL_IS_HTTP:
00896         case URL_IS_FTP:
00897         case URL_IS_DASH:
00898         case URL_IS_HKP:
00899         default:
00900             break;
00901         }
00902 
00903         _exit(-1);
00904         /*@notreached@*/
00905     }
00906     /*@=branchstate@*/
00907 
00908     if (psm->sq.child == (pid_t)-1) {
00909         rpmError(RPMERR_FORK, _("Couldn't fork %s: %s\n"), sln, strerror(errno));
00910         rc = RPMRC_FAIL;
00911         goto exit;
00912     }
00913 
00914     (void) psmWait(psm);
00915 
00916   /* XXX filter order dependent multilib "other" arch helper error. */
00917   if (!(psm->sq.reaped >= 0 && !strcmp(argv[0], "/usr/sbin/glibc_post_upgrade") && WEXITSTATUS(psm->sq.status) == 110)) {
00918     if (psm->sq.reaped < 0) {
00919         rpmError(RPMERR_SCRIPT,
00920                 _("%s(%s-%s-%s.%s) scriptlet failed, waitpid(%d) rc %d: %s\n"),
00921                  sln, n, v, r, a, psm->sq.child, psm->sq.reaped, strerror(errno));
00922         rc = RPMRC_FAIL;
00923     } else
00924     if (!WIFEXITED(psm->sq.status) || WEXITSTATUS(psm->sq.status)) {
00925       if (WIFSIGNALED(psm->sq.status)) {
00926         rpmError(RPMERR_SCRIPT,
00927                  _("%s(%s-%s-%s.%s) scriptlet failed, signal %d\n"),
00928                  sln, n, v, r, a, WTERMSIG(psm->sq.status));
00929       } else {
00930         rpmError(RPMERR_SCRIPT,
00931                 _("%s(%s-%s-%s.%s) scriptlet failed, exit status %d\n"),
00932                 sln, n, v, r, a, WEXITSTATUS(psm->sq.status));
00933       }
00934         rc = RPMRC_FAIL;
00935     }
00936   }
00937 
00938 exit:
00939     if (freePrefixes) prefixes = hfd(prefixes, ipt);
00940 
00941     xx = Fclose(out);   /* XXX dup'd STDOUT_FILENO */
00942 
00943     /*@-branchstate@*/
00944     if (script) {
00945         if (!rpmIsDebug())
00946             xx = unlink(fn);
00947         fn = _free(fn);
00948     }
00949     /*@=branchstate@*/
00950 
00951     return rc;
00952 }
00953 
00959 static rpmRC runInstScript(rpmpsm psm)
00960         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00961         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
00962 {
00963     rpmfi fi = psm->fi;
00964     HGE_t hge = fi->hge;
00965     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00966     void ** progArgv;
00967     int progArgc;
00968     const char ** argv;
00969     rpmTagType ptt, stt;
00970     const char * script;
00971     rpmRC rc = RPMRC_OK;
00972     int xx;
00973 
00974     /*
00975      * headerGetEntry() sets the data pointer to NULL if the entry does
00976      * not exist.
00977      */
00978     xx = hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
00979     xx = hge(fi->h, psm->progTag, &ptt, (void **) &progArgv, &progArgc);
00980     if (progArgv == NULL && script == NULL)
00981         goto exit;
00982 
00983     /*@-branchstate@*/
00984     if (progArgv && ptt == RPM_STRING_TYPE) {
00985         argv = alloca(sizeof(*argv));
00986         *argv = (const char *) progArgv;
00987     } else {
00988         argv = (const char **) progArgv;
00989     }
00990     /*@=branchstate@*/
00991 
00992     if (fi->h != NULL)  /* XXX can't happen */
00993     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), progArgc, argv,
00994                 script, psm->scriptArg, -1);
00995 
00996 exit:
00997     progArgv = hfd(progArgv, ptt);
00998     script = hfd(script, stt);
00999     return rc;
01000 }
01001 
01012 static rpmRC handleOneTrigger(const rpmpsm psm,
01013                         Header sourceH, Header triggeredH,
01014                         int arg2, unsigned char * triggersAlreadyRun)
01015         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState@*/
01016         /*@modifies psm, sourceH, triggeredH, *triggersAlreadyRun,
01017                 rpmGlobalMacroContext, fileSystem, internalState @*/
01018 {
01019     int scareMem = 1;
01020     const rpmts ts = psm->ts;
01021     rpmfi fi = psm->fi;
01022     HGE_t hge = fi->hge;
01023     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01024     rpmds trigger = NULL;
01025     const char ** triggerScripts;
01026     const char ** triggerProgs;
01027     int_32 * triggerIndices;
01028     const char * sourceName;
01029     const char * triggerName;
01030     rpmRC rc = RPMRC_OK;
01031     int xx;
01032     int i;
01033 
01034     xx = headerNVR(sourceH, &sourceName, NULL, NULL);
01035     xx = headerNVR(triggeredH, &triggerName, NULL, NULL);
01036 
01037     trigger = rpmdsInit(rpmdsNew(triggeredH, RPMTAG_TRIGGERNAME, scareMem));
01038     if (trigger == NULL)
01039         return rc;
01040 
01041     (void) rpmdsSetNoPromote(trigger, 1);
01042 
01043     while ((i = rpmdsNext(trigger)) >= 0) {
01044         rpmTagType tit, tst, tpt;
01045         const char * Name;
01046         int_32 Flags = rpmdsFlags(trigger);
01047 
01048         if ((Name = rpmdsN(trigger)) == NULL)
01049             continue;   /* XXX can't happen */
01050 
01051         if (strcmp(Name, sourceName))
01052             continue;
01053         if (!(Flags & psm->sense))
01054             continue;
01055 
01056         /*
01057          * XXX Trigger on any provided dependency, not just the package NEVR.
01058          */
01059         if (!rpmdsAnyMatchesDep(sourceH, trigger, 1))
01060             continue;
01061 
01062         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
01063                        (void **) &triggerIndices, NULL) &&
01064                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
01065                        (void **) &triggerScripts, NULL) &&
01066                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
01067                        (void **) &triggerProgs, NULL))
01068             )
01069             continue;
01070 
01071         {   int arg1;
01072             int index;
01073 
01074             arg1 = rpmdbCountPackages(rpmtsGetRdb(ts), triggerName);
01075             if (arg1 < 0) {
01076                 /* XXX W2DO? fails as "execution of script failed" */
01077                 rc = RPMRC_FAIL;
01078             } else {
01079                 arg1 += psm->countCorrection;
01080                 index = triggerIndices[i];
01081                 if (triggersAlreadyRun == NULL ||
01082                     triggersAlreadyRun[index] == 0)
01083                 {
01084                     rc = runScript(psm, triggeredH, "%trigger", 1,
01085                             triggerProgs + index, triggerScripts[index],
01086                             arg1, arg2);
01087                     if (triggersAlreadyRun != NULL)
01088                         triggersAlreadyRun[index] = 1;
01089                 }
01090             }
01091         }
01092 
01093         triggerIndices = hfd(triggerIndices, tit);
01094         triggerScripts = hfd(triggerScripts, tst);
01095         triggerProgs = hfd(triggerProgs, tpt);
01096 
01097         /*
01098          * Each target/source header pair can only result in a single
01099          * script being run.
01100          */
01101         break;
01102     }
01103 
01104     trigger = rpmdsFree(trigger);
01105 
01106     return rc;
01107 }
01108 
01114 static rpmRC runTriggers(rpmpsm psm)
01115         /*@globals rpmGlobalMacroContext, h_errno,
01116                 fileSystem, internalState @*/
01117         /*@modifies psm, rpmGlobalMacroContext,
01118                 fileSystem, internalState @*/
01119 {
01120     const rpmts ts = psm->ts;
01121     rpmfi fi = psm->fi;
01122     int numPackage = -1;
01123     rpmRC rc = RPMRC_OK;
01124     const char * N = NULL;
01125 
01126     if (psm->te)        /* XXX can't happen */
01127         N = rpmteN(psm->te);
01128 /* XXX: Might need to adjust instance counts four autorollback. */
01129     if (N)              /* XXX can't happen */
01130         numPackage = rpmdbCountPackages(rpmtsGetRdb(ts), N)
01131                                 + psm->countCorrection;
01132     if (numPackage < 0)
01133         return RPMRC_NOTFOUND;
01134 
01135     if (fi != NULL && fi->h != NULL)    /* XXX can't happen */
01136     {   Header triggeredH;
01137         rpmdbMatchIterator mi;
01138         int countCorrection = psm->countCorrection;
01139 
01140         psm->countCorrection = 0;
01141         mi = rpmtsInitIterator(ts, RPMTAG_TRIGGERNAME, N, 0);
01142         while((triggeredH = rpmdbNextIterator(mi)) != NULL)
01143             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01144         mi = rpmdbFreeIterator(mi);
01145         psm->countCorrection = countCorrection;
01146     }
01147 
01148     return rc;
01149 }
01150 
01156 static rpmRC runImmedTriggers(rpmpsm psm)
01157         /*@globals rpmGlobalMacroContext, h_errno,
01158                 fileSystem, internalState @*/
01159         /*@modifies psm, rpmGlobalMacroContext,
01160                 fileSystem, internalState @*/
01161 {
01162     const rpmts ts = psm->ts;
01163     rpmfi fi = psm->fi;
01164     HGE_t hge = fi->hge;
01165     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01166     const char ** triggerNames;
01167     int numTriggers;
01168     int_32 * triggerIndices;
01169     rpmTagType tnt, tit;
01170     int numTriggerIndices;
01171     unsigned char * triggersRun;
01172     rpmRC rc = RPMRC_OK;
01173 
01174     if (fi->h == NULL)  return rc;      /* XXX can't happen */
01175 
01176     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01177                         (void **) &triggerNames, &numTriggers) &&
01178                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01179                         (void **) &triggerIndices, &numTriggerIndices))
01180         )
01181         return rc;
01182 
01183     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01184     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01185 
01186     {   Header sourceH = NULL;
01187         int i;
01188 
01189         for (i = 0; i < numTriggers; i++) {
01190             rpmdbMatchIterator mi;
01191 
01192             if (triggersRun[triggerIndices[i]] != 0) continue;
01193         
01194             mi = rpmtsInitIterator(ts, RPMTAG_NAME, triggerNames[i], 0);
01195 
01196             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01197                 rc |= handleOneTrigger(psm, sourceH, fi->h,
01198                                 rpmdbGetIteratorCount(mi),
01199                                 triggersRun);
01200             }
01201 
01202             mi = rpmdbFreeIterator(mi);
01203         }
01204     }
01205     triggerIndices = hfd(triggerIndices, tit);
01206     triggerNames = hfd(triggerNames, tnt);
01207     return rc;
01208 }
01209 
01210 /*@observer@*/ static const char *const pkgStageString(pkgStage a)
01211         /*@*/
01212 {
01213     switch(a) {
01214     case PSM_UNKNOWN:           return "unknown";
01215 
01216     case PSM_PKGINSTALL:        return "  install";
01217     case PSM_PKGERASE:          return "    erase";
01218     case PSM_PKGCOMMIT:         return "   commit";
01219     case PSM_PKGSAVE:           return "repackage";
01220 
01221     case PSM_INIT:              return "init";
01222     case PSM_PRE:               return "pre";
01223     case PSM_PROCESS:           return "process";
01224     case PSM_POST:              return "post";
01225     case PSM_UNDO:              return "undo";
01226     case PSM_FINI:              return "fini";
01227 
01228     case PSM_CREATE:            return "create";
01229     case PSM_NOTIFY:            return "notify";
01230     case PSM_DESTROY:           return "destroy";
01231     case PSM_COMMIT:            return "commit";
01232 
01233     case PSM_CHROOT_IN:         return "chrootin";
01234     case PSM_CHROOT_OUT:        return "chrootout";
01235     case PSM_SCRIPT:            return "script";
01236     case PSM_TRIGGERS:          return "triggers";
01237     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01238 
01239     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01240 
01241     case PSM_RPMDB_LOAD:        return "rpmdbload";
01242     case PSM_RPMDB_ADD:         return "rpmdbadd";
01243     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01244 
01245     default:                    return "???";
01246     }
01247     /*@noteached@*/
01248 }
01249 
01250 rpmpsm XrpmpsmUnlink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01251 {
01252     if (psm == NULL) return NULL;
01253 /*@-modfilesys@*/
01254 if (_psm_debug && msg != NULL)
01255 fprintf(stderr, "--> psm %p -- %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01256 /*@=modfilesys@*/
01257     psm->nrefs--;
01258     return NULL;
01259 }
01260 
01261 rpmpsm XrpmpsmLink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01262 {
01263     if (psm == NULL) return NULL;
01264     psm->nrefs++;
01265 
01266 /*@-modfilesys@*/
01267 if (_psm_debug && msg != NULL)
01268 fprintf(stderr, "--> psm %p ++ %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01269 /*@=modfilesys@*/
01270 
01271     /*@-refcounttrans@*/ return psm; /*@=refcounttrans@*/
01272 }
01273 
01274 rpmpsm rpmpsmFree(rpmpsm psm)
01275 {
01276     const char * msg = "rpmpsmFree";
01277     if (psm == NULL)
01278         return NULL;
01279 
01280     if (psm->nrefs > 1)
01281         return rpmpsmUnlink(psm, msg);
01282 
01283 /*@-nullstate@*/
01284     psm->fi = rpmfiFree(psm->fi);
01285 #ifdef  NOTYET
01286     psm->te = rpmteFree(psm->te);
01287 #else
01288     psm->te = NULL;
01289 #endif
01290 /*@-internalglobs@*/
01291     psm->ts = rpmtsFree(psm->ts);
01292 /*@=internalglobs@*/
01293 
01294     (void) rpmpsmUnlink(psm, msg);
01295 
01296     /*@-refcounttrans -usereleased@*/
01297 /*@-boundswrite@*/
01298     memset(psm, 0, sizeof(*psm));               /* XXX trash and burn */
01299 /*@=boundswrite@*/
01300     psm = _free(psm);
01301     /*@=refcounttrans =usereleased@*/
01302 
01303     return NULL;
01304 /*@=nullstate@*/
01305 }
01306 
01307 rpmpsm rpmpsmNew(rpmts ts, rpmte te, rpmfi fi)
01308 {
01309     const char * msg = "rpmpsmNew";
01310     rpmpsm psm = xcalloc(1, sizeof(*psm));
01311 
01312     if (ts)     psm->ts = rpmtsLink(ts, msg);
01313 #ifdef  NOTYET
01314     if (te)     psm->te = rpmteLink(te, msg);
01315 #else
01316 /*@-assignexpose -temptrans @*/
01317     if (te)     psm->te = te;
01318 /*@=assignexpose =temptrans @*/
01319 #endif
01320     if (fi)     psm->fi = rpmfiLink(fi, msg);
01321 
01322     return rpmpsmLink(psm, msg);
01323 }
01324 
01325 static void * rpmpsmThread(void * arg)
01326         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01327         /*@modifies arg, rpmGlobalMacroContext, fileSystem, internalState @*/
01328 {
01329     rpmpsm psm = arg;
01330 /*@-unqualifiedtrans@*/
01331     return ((void *) rpmpsmStage(psm, psm->nstage));
01332 /*@=unqualifiedtrans@*/
01333 }
01334 
01335 static int rpmpsmNext(rpmpsm psm, pkgStage nstage)
01336         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01337         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
01338 {
01339     psm->nstage = nstage;
01340     if (_psm_threads)
01341         return rpmsqJoin( rpmsqThread(rpmpsmThread, psm) );
01342     return rpmpsmStage(psm, psm->nstage);
01343 }
01344 
01349 /*@-bounds -nullpass@*/ /* FIX: testing null annotation for fi->h */
01350 rpmRC rpmpsmStage(rpmpsm psm, pkgStage stage)
01351 {
01352     const rpmts ts = psm->ts;
01353     uint_32 tscolor = rpmtsColor(ts);
01354     rpmfi fi = psm->fi;
01355     HGE_t hge = fi->hge;
01356     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01357     rpmRC rc = psm->rc;
01358     int saveerrno;
01359     int xx;
01360 
01361     /*@-branchstate@*/
01362     switch (stage) {
01363     case PSM_UNKNOWN:
01364         break;
01365     case PSM_INIT:
01366         rpmMessage(RPMMESS_DEBUG, _("%s: %s has %d files, test = %d\n"),
01367                 psm->stepName, rpmteNEVR(psm->te),
01368                 rpmfiFC(fi), (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST));
01369 
01370         /*
01371          * When we run scripts, we pass an argument which is the number of
01372          * versions of this package that will be installed when we are
01373          * finished.
01374          */
01375         psm->npkgs_installed = rpmdbCountPackages(rpmtsGetRdb(ts), rpmteN(psm->te));
01376         if (psm->npkgs_installed < 0) {
01377             rc = RPMRC_FAIL;
01378             break;
01379         }
01380 
01381         /* If we have a score then autorollback is enabled.  If autorollback is
01382          * enabled, and this is an autorollback transaction, then we may need to
01383          * adjust the pkgs installed count.
01384          *
01385          * If all this is true, this adjustment should only be made if the PSM goal
01386          * is an install.  No need to make this adjustment on the erase
01387          * component of the upgrade, or even more absurd to do this when doing a
01388          * PKGSAVE.
01389          */
01390         if (rpmtsGetScore(ts) != NULL &&
01391             rpmtsGetType(ts) == RPMTRANS_TYPE_AUTOROLLBACK &&
01392             (psm->goal & ~(PSM_PKGSAVE|PSM_PKGERASE))) {
01393             /* Get the score, if its not NULL, get the appropriate
01394              * score entry.
01395              */
01396             rpmtsScore score = rpmtsGetScore(ts);
01397             if (score != NULL) {
01398                 /* OK, we got a real score so lets get the appropriate
01399                  * score entry.
01400                  */
01401                 rpmtsScoreEntry se;
01402                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
01403 
01404                 /* IF the header for the install element has been installed,
01405                  * but the header for the erase element has not been erased,
01406                  * then decrement the instance count.  This is because in an
01407                  * autorollback, if the header was added in the initial transaction
01408                  * then in the case of an upgrade the instance count will be
01409                  * 2 instead of one when re-installing the old package, and 3 when
01410                  * erasing the new package.
01411                  *
01412                  * Another wrinkle is we only want to make this adjustement
01413                  * if the thing we are rollback was an upgrade of package.  A pure
01414                  * install or erase does not need the adjustment
01415                  */
01416                 if (se && se->installed &&
01417                     !se->erased &&
01418                     (se->te_types & (TR_ADDED|TR_REMOVED)))
01419                     psm->npkgs_installed--;
01420            }
01421         }
01422 
01423         if (psm->goal == PSM_PKGINSTALL) {
01424             int fc = rpmfiFC(fi);
01425 
01426             psm->scriptArg = psm->npkgs_installed + 1;
01427 
01428 assert(psm->mi == NULL);
01429             psm->mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(psm->te), 0);
01430             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_EPOCH, RPMMIRE_STRCMP,
01431                         rpmteE(psm->te));
01432             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION, RPMMIRE_STRCMP,
01433                         rpmteV(psm->te));
01434             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE, RPMMIRE_STRCMP,
01435                         rpmteR(psm->te));
01436             if (tscolor) {
01437                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_ARCH, RPMMIRE_STRCMP,
01438                         rpmteA(psm->te));
01439                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_OS, RPMMIRE_STRCMP,
01440                         rpmteO(psm->te));
01441             }
01442 
01443             while ((psm->oh = rpmdbNextIterator(psm->mi)) != NULL) {
01444                 fi->record = rpmdbGetIteratorOffset(psm->mi);
01445                 psm->oh = NULL;
01446                 /*@loopbreak@*/ break;
01447             }
01448             psm->mi = rpmdbFreeIterator(psm->mi);
01449             rc = RPMRC_OK;
01450 
01451             /* XXX lazy alloc here may need to be done elsewhere. */
01452             if (fi->fstates == NULL && fc > 0) {
01453                 fi->fstates = xmalloc(sizeof(*fi->fstates) * fc);
01454                 memset(fi->fstates, RPMFILE_STATE_NORMAL, fc);
01455             }
01456 
01457             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01458             if (fc <= 0)                                break;
01459         
01460             /*
01461              * Old format relocatable packages need the entire default
01462              * prefix stripped to form the cpio list, while all other packages
01463              * need the leading / stripped.
01464              */
01465             {   const char * p;
01466                 xx = hge(fi->h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &p, NULL);
01467                 fi->striplen = (xx ? strlen(p) + 1 : 1);
01468             }
01469             fi->mapflags =
01470                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID | (fi->mapflags & CPIO_SBIT_CHECK);
01471         
01472             if (headerIsEntry(fi->h, RPMTAG_ORIGBASENAMES))
01473                 rpmfiBuildFNames(fi->h, RPMTAG_ORIGBASENAMES, &fi->apath, NULL);
01474             else
01475                 rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
01476         
01477             if (fi->fuser == NULL)
01478                 xx = hge(fi->h, RPMTAG_FILEUSERNAME, NULL,
01479                                 (void **) &fi->fuser, NULL);
01480             if (fi->fgroup == NULL)
01481                 xx = hge(fi->h, RPMTAG_FILEGROUPNAME, NULL,
01482                                 (void **) &fi->fgroup, NULL);
01483             rc = RPMRC_OK;
01484         }
01485         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01486             psm->scriptArg = psm->npkgs_installed - 1;
01487         
01488             /* Retrieve installed header. */
01489             rc = rpmpsmNext(psm, PSM_RPMDB_LOAD);
01490 if (rc == RPMRC_OK)
01491 if (psm->te)
01492 psm->te->h = headerLink(fi->h);
01493         }
01494         if (psm->goal == PSM_PKGSAVE) {
01495             /* Open output package for writing. */
01496             {   const char * bfmt = rpmGetPath("%{_repackage_name_fmt}", NULL);
01497                 const char * pkgbn =
01498                         headerSprintf(fi->h, bfmt, rpmTagTable, rpmHeaderFormats, NULL);
01499 
01500                 bfmt = _free(bfmt);
01501                 psm->pkgURL = rpmGenPath("%{?_repackage_root}",
01502                                          "%{?_repackage_dir}",
01503                                         pkgbn);
01504                 pkgbn = _free(pkgbn);
01505                 (void) urlPath(psm->pkgURL, &psm->pkgfn);
01506                 psm->fd = Fopen(psm->pkgfn, "w.ufdio");
01507                 if (psm->fd == NULL || Ferror(psm->fd)) {
01508                     rc = RPMRC_FAIL;
01509                     break;
01510                 }
01511             }
01512         }
01513         break;
01514     case PSM_PRE:
01515         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01516 
01517 /* XXX insure that trigger index is opened before entering chroot. */
01518 #ifdef  NOTYET
01519  { static int oneshot = 0;
01520    dbiIndex dbi;
01521    if (!oneshot) {
01522      dbi = dbiOpen(rpmtsGetRdb(ts), RPMTAG_TRIGGERNAME, 0);
01523      oneshot++;
01524    }
01525  }
01526 #endif
01527 
01528         /* Change root directory if requested and not already done. */
01529         rc = rpmpsmNext(psm, PSM_CHROOT_IN);
01530 
01531         if (psm->goal == PSM_PKGINSTALL) {
01532             psm->scriptTag = RPMTAG_PREIN;
01533             psm->progTag = RPMTAG_PREINPROG;
01534 
01535             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPREIN)) {
01536                 /* XXX FIXME: implement %triggerprein. */
01537             }
01538 
01539             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPRE)) {
01540                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01541                 if (rc != RPMRC_OK) {
01542                     rpmError(RPMERR_SCRIPT,
01543                         _("%s: %s scriptlet failed (%d), skipping %s\n"),
01544                         psm->stepName, tag2sln(psm->scriptTag), rc,
01545                         rpmteNEVR(psm->te));
01546                     break;
01547                 }
01548             }
01549         }
01550 
01551         if (psm->goal == PSM_PKGERASE) {
01552             psm->scriptTag = RPMTAG_PREUN;
01553             psm->progTag = RPMTAG_PREUNPROG;
01554             psm->sense = RPMSENSE_TRIGGERUN;
01555             psm->countCorrection = -1;
01556 
01557             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERUN)) {
01558                 /* Run triggers in this package other package(s) set off. */
01559                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01560                 if (rc) break;
01561 
01562                 /* Run triggers in other package(s) this package sets off. */
01563                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01564                 if (rc) break;
01565             }
01566 
01567             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPREUN))
01568                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01569         }
01570         if (psm->goal == PSM_PKGSAVE) {
01571             int noArchiveSize = 0;
01572 
01573             /* Regenerate original header. */
01574             {   void * uh = NULL;
01575                 int_32 uht, uhc;
01576 
01577                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)) {
01578                     psm->oh = headerCopyLoad(uh);
01579                     uh = hfd(uh, uht);
01580                 } else
01581                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMAGE, &uht, &uh, &uhc))
01582                 {
01583                     HeaderIterator hi;
01584                     int_32 tag, type, count;
01585                     hPTR_t ptr;
01586                     Header oh;
01587 
01588                     /* Load the original header from the blob. */
01589                     oh = headerCopyLoad(uh);
01590 
01591                     /* XXX this is headerCopy w/o headerReload() */
01592                     psm->oh = headerNew();
01593 
01594                     /*@-branchstate@*/
01595                     for (hi = headerInitIterator(oh);
01596                         headerNextIterator(hi, &tag, &type, &ptr, &count);
01597                         ptr = headerFreeData((void *)ptr, type))
01598                     {
01599                         if (tag == RPMTAG_ARCHIVESIZE)
01600                             noArchiveSize = 1;
01601                         if (ptr) (void) headerAddEntry(psm->oh, tag, type, ptr, count);
01602                     }
01603                     hi = headerFreeIterator(hi);
01604                     /*@=branchstate@*/
01605 
01606                     oh = headerFree(oh);
01607                     uh = hfd(uh, uht);
01608                 } else
01609                     break;      /* XXX shouldn't ever happen */
01610             }
01611 
01612             /* Retrieve type of payload compression. */
01613             /*@-nullstate@*/    /* FIX: psm->oh may be NULL */
01614             rc = rpmpsmNext(psm, PSM_RPMIO_FLAGS);
01615             /*@=nullstate@*/
01616 
01617             /* Write the lead section into the package. */
01618             {   int archnum = -1;
01619                 int osnum = -1;
01620                 struct rpmlead lead;
01621 
01622 #ifndef DYING
01623                 rpmGetArchInfo(NULL, &archnum);
01624                 rpmGetOsInfo(NULL, &osnum);
01625 #endif
01626 
01627                 memset(&lead, 0, sizeof(lead));
01628                 /* XXX Set package version conditioned on noDirTokens. */
01629                 lead.major = 3;
01630                 lead.minor = 0;
01631                 lead.type = RPMLEAD_BINARY;
01632                 lead.archnum = archnum;
01633                 lead.osnum = osnum;
01634                 lead.signature_type = RPMSIGTYPE_HEADERSIG;
01635 
01636                 strncpy(lead.name, rpmteNEVR(psm->te), sizeof(lead.name));
01637 
01638                 rc = writeLead(psm->fd, &lead);
01639                 if (rc != RPMRC_OK) {
01640                     rpmError(RPMERR_NOSPACE, _("Unable to write package: %s\n"),
01641                          Fstrerror(psm->fd));
01642                     break;
01643                 }
01644             }
01645 
01646             /* Write the signature section into the package. */
01647             /* XXX rpm-4.1 and later has archive size in signature header. */
01648             {   Header sigh = headerRegenSigHeader(fi->h, noArchiveSize);
01649                 /* Reallocate the signature into one contiguous region. */
01650                 sigh = headerReload(sigh, RPMTAG_HEADERSIGNATURES);
01651                 if (sigh == NULL) {
01652                     rpmError(RPMERR_NOSPACE, _("Unable to reload signature header\n"));
01653                     rc = RPMRC_FAIL;
01654                     break;
01655                 }
01656                 rc = rpmWriteSignature(psm->fd, sigh);
01657                 sigh = rpmFreeSignature(sigh);
01658                 if (rc) break;
01659             }
01660 
01661             /* Add remove transaction id to header. */
01662             if (psm->oh != NULL)
01663             {   int_32 tid = rpmtsGetTid(ts);
01664                 xx = headerAddEntry(psm->oh, RPMTAG_REMOVETID,
01665                         RPM_INT32_TYPE, &tid, 1);
01666             }
01667 
01668             /* Write the metadata section into the package. */
01669             rc = headerWrite(psm->fd, psm->oh, HEADER_MAGIC_YES);
01670             if (rc) break;
01671         }
01672         break;
01673     case PSM_PROCESS:
01674         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01675 
01676         if (psm->goal == PSM_PKGINSTALL) {
01677 
01678             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01679 
01680             /* XXX Synthesize callbacks for packages with no files. */
01681             if (rpmfiFC(fi) <= 0) {
01682                 void * ptr;
01683                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_START, 0, 100);
01684                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_PROGRESS, 100, 100);
01685                 break;
01686             }
01687 
01688             /* Retrieve type of payload compression. */
01689             rc = rpmpsmNext(psm, PSM_RPMIO_FLAGS);
01690 
01691             if (rpmteFd(fi->te) == NULL) {      /* XXX can't happen */
01692                 rc = RPMRC_FAIL;
01693                 break;
01694             }
01695 
01696             /*@-nullpass@*/     /* LCL: fi->fd != NULL here. */
01697             psm->cfd = Fdopen(fdDup(Fileno(rpmteFd(fi->te))), psm->rpmio_flags);
01698             /*@=nullpass@*/
01699             if (psm->cfd == NULL) {     /* XXX can't happen */
01700                 rc = RPMRC_FAIL;
01701                 break;
01702             }
01703 
01704             rc = fsmSetup(fi->fsm, FSM_PKGINSTALL, ts, fi,
01705                         psm->cfd, NULL, &psm->failedFile);
01706             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_UNCOMPRESS),
01707                         fdstat_op(psm->cfd, FDSTAT_READ));
01708             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST),
01709                         fdstat_op(psm->cfd, FDSTAT_DIGEST));
01710             xx = fsmTeardown(fi->fsm);
01711 
01712             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01713             xx = Fclose(psm->cfd);
01714             psm->cfd = NULL;
01715             /*@-mods@*/
01716             errno = saveerrno; /* XXX FIXME: Fclose with libio destroys errno */
01717             /*@=mods@*/
01718 
01719             if (!rc)
01720                 rc = rpmpsmNext(psm, PSM_COMMIT);
01721 
01722             /* XXX make sure progress is closed out */
01723             psm->what = RPMCALLBACK_INST_PROGRESS;
01724             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01725             psm->total = psm->amount;
01726             xx = rpmpsmNext(psm, PSM_NOTIFY);
01727 
01728             if (rc) {
01729                 rpmError(RPMERR_CPIO,
01730                         _("unpacking of archive failed%s%s: %s\n"),
01731                         (psm->failedFile != NULL ? _(" on file ") : ""),
01732                         (psm->failedFile != NULL ? psm->failedFile : ""),
01733                         cpioStrerror(rc));
01734                 rc = RPMRC_FAIL;
01735 
01736                 /* XXX notify callback on error. */
01737                 psm->what = RPMCALLBACK_UNPACK_ERROR;
01738                 psm->amount = 0;
01739                 psm->total = 0;
01740                 xx = rpmpsmNext(psm, PSM_NOTIFY);
01741 
01742                 break;
01743             }
01744         }
01745         if (psm->goal == PSM_PKGERASE) {
01746             int fc = rpmfiFC(fi);
01747 
01748             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01749             if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)       break;
01750             if (fc <= 0)                                break;
01751 
01752             psm->what = RPMCALLBACK_UNINST_START;
01753             psm->amount = fc;           /* XXX W2DO? looks wrong. */
01754             psm->total = fc;
01755             xx = rpmpsmNext(psm, PSM_NOTIFY);
01756 
01757             rc = fsmSetup(fi->fsm, FSM_PKGERASE, ts, fi,
01758                         NULL, NULL, &psm->failedFile);
01759             xx = fsmTeardown(fi->fsm);
01760 
01761             psm->what = RPMCALLBACK_UNINST_STOP;
01762             psm->amount = 0;            /* XXX W2DO? looks wrong. */
01763             psm->total = fc;
01764             xx = rpmpsmNext(psm, PSM_NOTIFY);
01765 
01766         }
01767         if (psm->goal == PSM_PKGSAVE) {
01768             fileAction * actions = fi->actions;
01769             fileAction action = fi->action;
01770 
01771             fi->action = FA_COPYOUT;
01772             fi->actions = NULL;
01773 
01774             if (psm->fd == NULL) {      /* XXX can't happen */
01775                 rc = RPMRC_FAIL;
01776                 break;
01777             }
01778             /*@-nullpass@*/     /* FIX: fdDup mey return NULL. */
01779             xx = Fflush(psm->fd);
01780             psm->cfd = Fdopen(fdDup(Fileno(psm->fd)), psm->rpmio_flags);
01781             /*@=nullpass@*/
01782             if (psm->cfd == NULL) {     /* XXX can't happen */
01783                 rc = RPMRC_FAIL;
01784                 break;
01785             }
01786 
01787             rc = fsmSetup(fi->fsm, FSM_PKGBUILD, ts, fi, psm->cfd,
01788                         NULL, &psm->failedFile);
01789             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_COMPRESS),
01790                         fdstat_op(psm->cfd, FDSTAT_WRITE));
01791             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST),
01792                         fdstat_op(psm->cfd, FDSTAT_DIGEST));
01793             xx = fsmTeardown(fi->fsm);
01794 
01795             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01796             xx = Fclose(psm->cfd);
01797             psm->cfd = NULL;
01798             /*@-mods@*/
01799             errno = saveerrno;
01800             /*@=mods@*/
01801 
01802             /* XXX make sure progress is closed out */
01803             psm->what = RPMCALLBACK_INST_PROGRESS;
01804             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01805             psm->total = psm->amount;
01806             xx = rpmpsmNext(psm, PSM_NOTIFY);
01807 
01808             fi->action = action;
01809             fi->actions = actions;
01810         }
01811         break;
01812     case PSM_POST:
01813         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01814 
01815         if (psm->goal == PSM_PKGINSTALL) {
01816             int_32 installTime = (int_32) time(NULL);
01817             int fc = rpmfiFC(fi);
01818 
01819             if (fi->h == NULL) break;   /* XXX can't happen */
01820             if (fi->fstates != NULL && fc > 0)
01821                 xx = headerAddEntry(fi->h, RPMTAG_FILESTATES, RPM_CHAR_TYPE,
01822                                 fi->fstates, fc);
01823 
01824             xx = headerAddEntry(fi->h, RPMTAG_INSTALLTIME, RPM_INT32_TYPE,
01825                                 &installTime, 1);
01826 
01827             xx = headerAddEntry(fi->h, RPMTAG_INSTALLCOLOR, RPM_INT32_TYPE,
01828                                 &tscolor, 1);
01829 
01830             /*
01831              * If this package has already been installed, remove it from
01832              * the database before adding the new one.
01833              */
01834             if (fi->record && !(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)) {
01835                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01836                 if (rc) break;
01837             }
01838 
01839             rc = rpmpsmNext(psm, PSM_RPMDB_ADD);
01840             if (rc) break;
01841 
01842             psm->scriptTag = RPMTAG_POSTIN;
01843             psm->progTag = RPMTAG_POSTINPROG;
01844             psm->sense = RPMSENSE_TRIGGERIN;
01845             psm->countCorrection = 0;
01846 
01847             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOST)) {
01848                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01849                 if (rc) break;
01850             }
01851             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERIN)) {
01852                 /* Run triggers in other package(s) this package sets off. */
01853                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01854                 if (rc) break;
01855 
01856                 /* Run triggers in this package other package(s) set off. */
01857                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01858                 if (rc) break;
01859             }
01860 
01861             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01862                 rc = markReplacedFiles(psm);
01863 
01864         }
01865         if (psm->goal == PSM_PKGERASE) {
01866 
01867             psm->scriptTag = RPMTAG_POSTUN;
01868             psm->progTag = RPMTAG_POSTUNPROG;
01869             psm->sense = RPMSENSE_TRIGGERPOSTUN;
01870             psm->countCorrection = -1;
01871 
01872             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOSTUN)) {
01873                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01874                 if (rc) break;
01875             }
01876 
01877             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
01878                 /* Run triggers in other package(s) this package sets off. */
01879                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01880                 if (rc) break;
01881             }
01882 
01883             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01884                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01885         }
01886         if (psm->goal == PSM_PKGSAVE) {
01887         }
01888 
01889         /* Restore root directory if changed. */
01890         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01891         break;
01892     case PSM_UNDO:
01893         break;
01894     case PSM_FINI:
01895         /* Restore root directory if changed. */
01896         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01897 
01898         if (psm->fd != NULL) {
01899             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01900             xx = Fclose(psm->fd);
01901             psm->fd = NULL;
01902             /*@-mods@*/
01903             errno = saveerrno;
01904             /*@=mods@*/
01905         }
01906 
01907         if (psm->goal == PSM_PKGSAVE) {
01908             if (!rc && ts && ts->notify == NULL) {
01909                 rpmMessage(RPMMESS_VERBOSE, _("Wrote: %s\n"),
01910                         (psm->pkgURL ? psm->pkgURL : "???"));
01911             }
01912         }
01913 
01914         if (rc) {
01915             if (psm->failedFile)
01916                 rpmError(RPMERR_CPIO,
01917                         _("%s failed on file %s: %s\n"),
01918                         psm->stepName, psm->failedFile, cpioStrerror(rc));
01919             else
01920                 rpmError(RPMERR_CPIO, _("%s failed: %s\n"),
01921                         psm->stepName, cpioStrerror(rc));
01922 
01923             /* XXX notify callback on error. */
01924             psm->what = RPMCALLBACK_CPIO_ERROR;
01925             psm->amount = 0;
01926             psm->total = 0;
01927             /*@-nullstate@*/ /* FIX: psm->fd may be NULL. */
01928             xx = rpmpsmNext(psm, PSM_NOTIFY);
01929             /*@=nullstate@*/
01930         }
01931 
01932 /*@-branchstate@*/
01933         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01934 if (psm->te != NULL)
01935 if (psm->te->h != NULL)
01936 psm->te->h = headerFree(psm->te->h);
01937             if (fi->h != NULL)
01938                 fi->h = headerFree(fi->h);
01939         }
01940 /*@=branchstate@*/
01941         psm->oh = headerFree(psm->oh);
01942         psm->pkgURL = _free(psm->pkgURL);
01943         psm->rpmio_flags = _free(psm->rpmio_flags);
01944         psm->failedFile = _free(psm->failedFile);
01945 
01946         fi->fgroup = hfd(fi->fgroup, -1);
01947         fi->fuser = hfd(fi->fuser, -1);
01948         fi->apath = _free(fi->apath);
01949         fi->fstates = _free(fi->fstates);
01950         break;
01951 
01952     case PSM_PKGINSTALL:
01953     case PSM_PKGERASE:
01954     case PSM_PKGSAVE:
01955         psm->goal = stage;
01956         psm->rc = RPMRC_OK;
01957         psm->stepName = pkgStageString(stage);
01958 
01959         rc = rpmpsmNext(psm, PSM_INIT);
01960         if (!rc) rc = rpmpsmNext(psm, PSM_PRE);
01961         if (!rc) rc = rpmpsmNext(psm, PSM_PROCESS);
01962         if (!rc) rc = rpmpsmNext(psm, PSM_POST);
01963         xx = rpmpsmNext(psm, PSM_FINI);
01964         break;
01965     case PSM_PKGCOMMIT:
01966         break;
01967 
01968     case PSM_CREATE:
01969         break;
01970     case PSM_NOTIFY:
01971     {   void * ptr;
01972 /*@-nullpass@*/ /* FIX: psm->te may be NULL */
01973         ptr = rpmtsNotify(ts, psm->te, psm->what, psm->amount, psm->total);
01974 /*@-nullpass@*/
01975     }   break;
01976     case PSM_DESTROY:
01977         break;
01978     case PSM_COMMIT:
01979         if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_PKGCOMMIT)) break;
01980         if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY) break;
01981 
01982         rc = fsmSetup(fi->fsm, FSM_PKGCOMMIT, ts, fi,
01983                         NULL, NULL, &psm->failedFile);
01984         xx = fsmTeardown(fi->fsm);
01985         break;
01986 
01987     case PSM_CHROOT_IN:
01988     {   const char * rootDir = rpmtsRootDir(ts);
01989         /* Change root directory if requested and not already done. */
01990         if (rootDir != NULL && !(rootDir[0] == '/' && rootDir[1] == '\0')
01991          && !rpmtsChrootDone(ts) && !psm->chrootDone)
01992         {
01993             static int _pw_loaded = 0;
01994             static int _gr_loaded = 0;
01995 
01996             if (!_pw_loaded) {
01997                 (void)getpwnam("root");
01998                 endpwent();
01999                 _pw_loaded++;
02000             }
02001             if (!_gr_loaded) {
02002                 (void)getgrnam("root");
02003                 endgrent();
02004                 _gr_loaded++;
02005             }
02006 
02007             xx = chdir("/");
02008             /*@-superuser@*/
02009             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
02010                 rc = chroot(rootDir);
02011             /*@=superuser@*/
02012             psm->chrootDone = 1;
02013             (void) rpmtsSetChrootDone(ts, 1);
02014         }
02015     }   break;
02016     case PSM_CHROOT_OUT:
02017         /* Restore root directory if changed. */
02018         if (psm->chrootDone) {
02019             const char * rootDir = rpmtsRootDir(ts);
02020             const char * currDir = rpmtsCurrDir(ts);
02021             /*@-superuser@*/
02022             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
02023                 rc = chroot(".");
02024             /*@=superuser@*/
02025             psm->chrootDone = 0;
02026             (void) rpmtsSetChrootDone(ts, 0);
02027             if (currDir != NULL)        /* XXX can't happen */
02028                 xx = chdir(currDir);
02029         }
02030         break;
02031     case PSM_SCRIPT:    /* Run current package scriptlets. */
02032         rc = runInstScript(psm);
02033         break;
02034     case PSM_TRIGGERS:
02035         /* Run triggers in other package(s) this package sets off. */
02036         rc = runTriggers(psm);
02037         break;
02038     case PSM_IMMED_TRIGGERS:
02039         /* Run triggers in this package other package(s) set off. */
02040         rc = runImmedTriggers(psm);
02041         break;
02042 
02043     case PSM_RPMIO_FLAGS:
02044     {   const char * payload_compressor = NULL;
02045         char * t;
02046 
02047         /*@-branchstate@*/
02048         if (!hge(fi->h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
02049                             (void **) &payload_compressor, NULL))
02050             payload_compressor = "gzip";
02051         /*@=branchstate@*/
02052         psm->rpmio_flags = t = xmalloc(sizeof("w9.gzdio"));
02053         *t = '\0';
02054         t = stpcpy(t, ((psm->goal == PSM_PKGSAVE) ? "w9" : "r"));
02055         if (!strcmp(payload_compressor, "gzip"))
02056             t = stpcpy(t, ".gzdio");
02057         if (!strcmp(payload_compressor, "bzip2"))
02058             t = stpcpy(t, ".bzdio");
02059         rc = RPMRC_OK;
02060     }   break;
02061 
02062     case PSM_RPMDB_LOAD:
02063 assert(psm->mi == NULL);
02064         psm->mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
02065                                 &fi->record, sizeof(fi->record));
02066 
02067         fi->h = rpmdbNextIterator(psm->mi);
02068         if (fi->h != NULL)
02069             fi->h = headerLink(fi->h);
02070 
02071         psm->mi = rpmdbFreeIterator(psm->mi);
02072         rc = (fi->h != NULL ? RPMRC_OK : RPMRC_FAIL);
02073         break;
02074     case PSM_RPMDB_ADD:
02075         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02076         if (fi->h == NULL)      break;  /* XXX can't happen */
02077         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02078         if (!(rpmtsVSFlags(ts) & RPMVSF_NOHDRCHK))
02079             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02080                                 ts, headerCheck);
02081         else
02082             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02083                                 NULL, NULL);
02084 
02085         /* Set the database instance so consumers (i.e. rpmtsRun())
02086          * can add this to a rollback transaction.
02087          */
02088         rpmteSetDBInstance(psm->te, myinstall_instance);
02089 
02090         /*
02091          * If the score exists and this is not a rollback or autorollback
02092          * then lets check off installed for this package.
02093          */
02094         if (rpmtsGetScore(ts) != NULL &&
02095             rpmtsGetType(ts) != RPMTRANS_TYPE_ROLLBACK &&
02096             rpmtsGetType(ts) != RPMTRANS_TYPE_AUTOROLLBACK)
02097         {
02098             /* Get the score, if its not NULL, get the appropriate
02099              * score entry.
02100              */
02101             rpmtsScore score = rpmtsGetScore(ts);
02102             if (score != NULL) {
02103                 rpmtsScoreEntry se;
02104                 /* OK, we got a real score so lets get the appropriate
02105                  * score entry.
02106                  */
02107                 rpmMessage(RPMMESS_DEBUG,
02108                     _("Attempting to mark %s as installed in score board(%u).\n"),
02109                     rpmteN(psm->te), (unsigned) score);
02110                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
02111                 if (se != NULL) se->installed = 1;
02112             }
02113         }
02114         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02115         break;
02116     case PSM_RPMDB_REMOVE:
02117         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02118         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02119         rc = rpmdbRemove(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->record,
02120                                 NULL, NULL);
02121 
02122         /*
02123          * If the score exists and this is not a rollback or autorollback
02124          * then lets check off erased for this package.
02125          */
02126         if (rpmtsGetScore(ts) != NULL &&
02127            rpmtsGetType(ts) != RPMTRANS_TYPE_ROLLBACK &&
02128            rpmtsGetType(ts) != RPMTRANS_TYPE_AUTOROLLBACK)
02129         {
02130             /* Get the score, if its not NULL, get the appropriate
02131              * score entry.
02132              */
02133             rpmtsScore score = rpmtsGetScore(ts);
02134 
02135             if (score != NULL) { /* XXX: Can't happen */
02136                 rpmtsScoreEntry se;
02137                 /* OK, we got a real score so lets get the appropriate
02138                  * score entry.
02139                  */
02140                 rpmMessage(RPMMESS_DEBUG,
02141                     _("Attempting to mark %s as erased in score board(0x%x).\n"),
02142                     rpmteN(psm->te), (unsigned) score);
02143                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
02144                 if (se != NULL) se->erased = 1;
02145             }
02146         }
02147 
02148         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02149         break;
02150 
02151     default:
02152         break;
02153 /*@i@*/    }
02154     /*@=branchstate@*/
02155 
02156     /*@-nullstate@*/    /* FIX: psm->oh and psm->fi->h may be NULL. */
02157     return rc;
02158     /*@=nullstate@*/
02159 }
02160 /*@=bounds =nullpass@*/

Generated on Thu Oct 25 09:23:14 2007 for rpm by  doxygen 1.5.1