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

Generated on Tue Feb 19 22:53:15 2008 for rpm by  doxygen 1.5.1