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     }
00465     return "%unknownscript";
00466 }
00467 
00473 static pid_t psmWait(rpmpsm psm)
00474         /*@globals fileSystem, internalState @*/
00475         /*@modifies psm, fileSystem, internalState @*/
00476 {
00477     const rpmts ts = psm->ts;
00478     rpmtime_t msecs;
00479 
00480     (void) rpmsqWait(&psm->sq);
00481     msecs = psm->sq.op.usecs/1000;
00482     (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_SCRIPTLETS), &psm->sq.op);
00483 
00484     rpmMessage(RPMMESS_DEBUG,
00485         _("%s: waitpid(%d) rc %d status %x secs %u.%03u\n"),
00486         psm->stepName, (unsigned)psm->sq.child,
00487         (unsigned)psm->sq.reaped, psm->sq.status,
00488         (unsigned)msecs/1000, (unsigned)msecs%1000);
00489 
00490     return psm->sq.reaped;
00491 }
00492 
00496 static rpmRC runLuaScript(rpmpsm psm, Header h, const char *sln,
00497                    int progArgc, const char **progArgv,
00498                    const char *script, int arg1, int arg2)
00499         /*@globals fileSystem, internalState @*/
00500         /*@modifies psm, fileSystem, internalState @*/
00501 {
00502     const rpmts ts = psm->ts;
00503     int rootFd = -1;
00504     const char *n, *v, *r;
00505     rpmRC rc = RPMRC_OK;
00506     int i;
00507     int xx;
00508     rpmlua lua = NULL; /* Global state. */
00509     rpmluav var;
00510 
00511     xx = headerNVR(h, &n, &v, &r);
00512 
00513     if (!rpmtsChrootDone(ts)) {
00514         const char *rootDir = rpmtsRootDir(ts);
00515         xx = chdir("/");
00516 /*@-nullpass@*/
00517         rootFd = open(".", O_RDONLY, 0);
00518 /*@=nullpass@*/
00519         if (rootFd >= 0) {
00520             /*@-superuser -noeffect @*/
00521             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
00522                 xx = chroot(rootDir);
00523             /*@=superuser =noeffect @*/
00524             xx = rpmtsSetChrootDone(ts, 1);
00525         }
00526     }
00527 
00528     /* Create arg variable */
00529     rpmluaPushTable(lua, "arg");
00530     var = rpmluavNew();
00531     rpmluavSetListMode(var, 1);
00532 /*@+relaxtypes@*/
00533     if (progArgv) {
00534         for (i = 0; i < progArgc && progArgv[i]; i++) {
00535             rpmluavSetValue(var, RPMLUAV_STRING, progArgv[i]);
00536             rpmluaSetVar(lua, var);
00537         }
00538     }
00539     if (arg1 >= 0) {
00540         rpmluavSetValueNum(var, arg1);
00541         rpmluaSetVar(lua, var);
00542     }
00543     if (arg2 >= 0) {
00544         rpmluavSetValueNum(var, arg2);
00545         rpmluaSetVar(lua, var);
00546     }
00547 /*@=relaxtypes@*/
00548 /*@-moduncon@*/
00549     var = rpmluavFree(var);
00550 /*@=moduncon@*/
00551     rpmluaPop(lua);
00552 
00553     {
00554         char buf[BUFSIZ];
00555         xx = snprintf(buf, BUFSIZ, "%s(%s-%s-%s)", sln, n, v, r);
00556         if (rpmluaRunScript(lua, script, buf) == -1)
00557             rc = RPMRC_FAIL;
00558     }
00559 
00560     rpmluaDelVar(lua, "arg");
00561 
00562     if (rootFd >= 0) {
00563         const char *rootDir = rpmtsRootDir(ts);
00564         xx = fchdir(rootFd);
00565         xx = close(rootFd);
00566         /*@-superuser -noeffect @*/
00567         if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
00568             xx = chroot(".");
00569         /*@=superuser =noeffect @*/
00570         xx = rpmtsSetChrootDone(ts, 0);
00571     }
00572 
00573     return rc;
00574 }
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         rpmMessage(RPMMESS_DEBUG,
00641                 _("%s: %s(%s-%s-%s.%s) running <lua> scriptlet.\n"),
00642                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a);
00643         return runLuaScript(psm, h, sln, progArgc, progArgv,
00644                             script, arg1, arg2);
00645     }
00646 
00647     psm->sq.reaper = 1;
00648 
00649     /* XXX bash must have functional libtermcap.so.2 */
00650     if (!strcmp(n, "libtermcap"))
00651         ldconfig_done = 0;
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)
00831                 xx = Fclose (scriptFd);
00832             else {
00833 /*@-usereleased@*/
00834                 xx = Fclose(out);
00835 /*@=usereleased@*/
00836             }
00837         }
00838 
00839         {   const char *ipath = rpmExpand("PATH=%{_install_script_path}", NULL);
00840             const char *path = SCRIPT_PATH;
00841 
00842             if (ipath && ipath[5] != '%')
00843                 path = ipath;
00844 
00845             xx = doputenv(path);
00846             /*@-modobserver@*/
00847             ipath = _free(ipath);
00848             /*@=modobserver@*/
00849         }
00850 
00851         if (prefixes != NULL)
00852         for (i = 0; i < numPrefixes; i++) {
00853             sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
00854             xx = doputenv(prefixBuf);
00855 
00856             /* backwards compatibility */
00857             if (i == 0) {
00858                 sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
00859                 xx = doputenv(prefixBuf);
00860             }
00861         }
00862 
00863         rootDir = ts->rootDir;  /* HACK: rootDir = rpmtsRootDir(ts); instead */
00864         if (rootDir  != NULL)   /* XXX can't happen */
00865         switch(urlIsURL(rootDir)) {
00866         case URL_IS_PATH:
00867             rootDir += sizeof("file://") - 1;
00868             rootDir = strchr(rootDir, '/');
00869             /*@fallthrough@*/
00870         case URL_IS_UNKNOWN:
00871             if (!rpmtsChrootDone(ts) &&
00872                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00873             {
00874                 /*@-superuser -noeffect @*/
00875                 xx = chroot(rootDir);
00876                 /*@=superuser =noeffect @*/
00877             }
00878             xx = chdir("/");
00879             rpmMessage(RPMMESS_DEBUG, _("%s: %s(%s-%s-%s.%s)\texecv(%s) pid %d\n"),
00880                         psm->stepName, sln, n, v, r, a,
00881                         argv[0], (unsigned)getpid());
00882 
00883             /* XXX Don't mtrace into children. */
00884             unsetenv("MALLOC_CHECK_");
00885 
00886             /* Permit libselinux to do the scriptlet exec. */
00887             if (rpmtsSELinuxEnabled(ts) == 1) { 
00888 /*@-moduncon@*/
00889                 xx = rpm_execcon(0, argv[0], argv, environ);
00890 /*@=moduncon@*/
00891                 if (xx != 0)
00892                     break;
00893             }
00894 
00895 /*@-nullstate@*/
00896             xx = execv(argv[0], (char *const *)argv);
00897 /*@=nullstate@*/
00898             break;
00899         case URL_IS_HTTPS:
00900         case URL_IS_HTTP:
00901         case URL_IS_FTP:
00902         case URL_IS_DASH:
00903         case URL_IS_HKP:
00904         default:
00905             break;
00906         }
00907 
00908         _exit(-1);
00909         /*@notreached@*/
00910     }
00911     /*@=branchstate@*/
00912 
00913     (void) psmWait(psm);
00914 
00915   /* XXX filter order dependent multilib "other" arch helper error. */
00916   if (!(psm->sq.reaped >= 0 && !strcmp(argv[0], "/usr/sbin/glibc_post_upgrade") && WEXITSTATUS(psm->sq.status) == 110)) {
00917     if (psm->sq.reaped < 0) {
00918         rpmError(RPMERR_SCRIPT,
00919                 _("%s(%s-%s-%s.%s) scriptlet failed, waitpid(%d) rc %d: %s\n"),
00920                  sln, n, v, r, a, psm->sq.child, psm->sq.reaped, strerror(errno));
00921         rc = RPMRC_FAIL;
00922     } else
00923     if (!WIFEXITED(psm->sq.status) || WEXITSTATUS(psm->sq.status)) {
00924       if (WIFSIGNALED(psm->sq.status)) {
00925         rpmError(RPMERR_SCRIPT,
00926                  _("%s(%s-%s-%s.%s) scriptlet failed, signal %d\n"),
00927                  sln, n, v, r, a, WTERMSIG(psm->sq.status));
00928       } else {
00929         rpmError(RPMERR_SCRIPT,
00930                 _("%s(%s-%s-%s.%s) scriptlet failed, exit status %d\n"),
00931                 sln, n, v, r, a, WEXITSTATUS(psm->sq.status));
00932       }
00933         rc = RPMRC_FAIL;
00934     }
00935   }
00936 
00937     if (freePrefixes) prefixes = hfd(prefixes, ipt);
00938 
00939     xx = Fclose(out);   /* XXX dup'd STDOUT_FILENO */
00940 
00941     /*@-branchstate@*/
00942     if (script) {
00943         if (!rpmIsDebug())
00944             xx = unlink(fn);
00945         fn = _free(fn);
00946     }
00947     /*@=branchstate@*/
00948 
00949     return rc;
00950 }
00951 
00957 static rpmRC runInstScript(rpmpsm psm)
00958         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00959         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
00960 {
00961     rpmfi fi = psm->fi;
00962     HGE_t hge = fi->hge;
00963     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00964     void ** progArgv;
00965     int progArgc;
00966     const char ** argv;
00967     rpmTagType ptt, stt;
00968     const char * script;
00969     rpmRC rc = RPMRC_OK;
00970     int xx;
00971 
00972     /*
00973      * headerGetEntry() sets the data pointer to NULL if the entry does
00974      * not exist.
00975      */
00976     xx = hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
00977     xx = hge(fi->h, psm->progTag, &ptt, (void **) &progArgv, &progArgc);
00978     if (progArgv == NULL && script == NULL)
00979         goto exit;
00980 
00981     /*@-branchstate@*/
00982     if (progArgv && ptt == RPM_STRING_TYPE) {
00983         argv = alloca(sizeof(*argv));
00984         *argv = (const char *) progArgv;
00985     } else {
00986         argv = (const char **) progArgv;
00987     }
00988     /*@=branchstate@*/
00989 
00990     if (fi->h != NULL)  /* XXX can't happen */
00991     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), progArgc, argv,
00992                 script, psm->scriptArg, -1);
00993 
00994 exit:
00995     progArgv = hfd(progArgv, ptt);
00996     script = hfd(script, stt);
00997     return rc;
00998 }
00999 
01010 static rpmRC handleOneTrigger(const rpmpsm psm,
01011                         Header sourceH, Header triggeredH,
01012                         int arg2, unsigned char * triggersAlreadyRun)
01013         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState@*/
01014         /*@modifies psm, sourceH, triggeredH, *triggersAlreadyRun,
01015                 rpmGlobalMacroContext, fileSystem, internalState @*/
01016 {
01017     int scareMem = 1;
01018     const rpmts ts = psm->ts;
01019     rpmfi fi = psm->fi;
01020     HGE_t hge = fi->hge;
01021     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01022     rpmds trigger = NULL;
01023     const char ** triggerScripts;
01024     const char ** triggerProgs;
01025     int_32 * triggerIndices;
01026     const char * sourceName;
01027     const char * triggerName;
01028     rpmRC rc = RPMRC_OK;
01029     int xx;
01030     int i;
01031 
01032     xx = headerNVR(sourceH, &sourceName, NULL, NULL);
01033     xx = headerNVR(triggeredH, &triggerName, NULL, NULL);
01034 
01035     trigger = rpmdsInit(rpmdsNew(triggeredH, RPMTAG_TRIGGERNAME, scareMem));
01036     if (trigger == NULL)
01037         return rc;
01038 
01039     (void) rpmdsSetNoPromote(trigger, 1);
01040 
01041     while ((i = rpmdsNext(trigger)) >= 0) {
01042         rpmTagType tit, tst, tpt;
01043         const char * Name;
01044         int_32 Flags = rpmdsFlags(trigger);
01045 
01046         if ((Name = rpmdsN(trigger)) == NULL)
01047             continue;   /* XXX can't happen */
01048 
01049         if (strcmp(Name, sourceName))
01050             continue;
01051         if (!(Flags & psm->sense))
01052             continue;
01053 
01054         /*
01055          * XXX Trigger on any provided dependency, not just the package NEVR.
01056          */
01057         if (!rpmdsAnyMatchesDep(sourceH, trigger, 1))
01058             continue;
01059 
01060         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
01061                        (void **) &triggerIndices, NULL) &&
01062                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
01063                        (void **) &triggerScripts, NULL) &&
01064                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
01065                        (void **) &triggerProgs, NULL))
01066             )
01067             continue;
01068 
01069         {   int arg1;
01070             int index;
01071 
01072             arg1 = rpmdbCountPackages(rpmtsGetRdb(ts), triggerName);
01073             if (arg1 < 0) {
01074                 /* XXX W2DO? fails as "execution of script failed" */
01075                 rc = RPMRC_FAIL;
01076             } else {
01077                 arg1 += psm->countCorrection;
01078                 index = triggerIndices[i];
01079                 if (triggersAlreadyRun == NULL ||
01080                     triggersAlreadyRun[index] == 0)
01081                 {
01082                     rc = runScript(psm, triggeredH, "%trigger", 1,
01083                             triggerProgs + index, triggerScripts[index],
01084                             arg1, arg2);
01085                     if (triggersAlreadyRun != NULL)
01086                         triggersAlreadyRun[index] = 1;
01087                 }
01088             }
01089         }
01090 
01091         triggerIndices = hfd(triggerIndices, tit);
01092         triggerScripts = hfd(triggerScripts, tst);
01093         triggerProgs = hfd(triggerProgs, tpt);
01094 
01095         /*
01096          * Each target/source header pair can only result in a single
01097          * script being run.
01098          */
01099         break;
01100     }
01101 
01102     trigger = rpmdsFree(trigger);
01103 
01104     return rc;
01105 }
01106 
01112 static rpmRC runTriggers(rpmpsm psm)
01113         /*@globals rpmGlobalMacroContext, h_errno,
01114                 fileSystem, internalState @*/
01115         /*@modifies psm, rpmGlobalMacroContext,
01116                 fileSystem, internalState @*/
01117 {
01118     const rpmts ts = psm->ts;
01119     rpmfi fi = psm->fi;
01120     int numPackage = -1;
01121     rpmRC rc = RPMRC_OK;
01122     const char * N = NULL;
01123 
01124     if (psm->te)        /* XXX can't happen */
01125         N = rpmteN(psm->te);
01126 /* XXX: Might need to adjust instance counts four autorollback. */
01127     if (N)              /* XXX can't happen */
01128         numPackage = rpmdbCountPackages(rpmtsGetRdb(ts), N)
01129                                 + psm->countCorrection;
01130     if (numPackage < 0)
01131         return RPMRC_NOTFOUND;
01132 
01133     if (fi != NULL && fi->h != NULL)    /* XXX can't happen */
01134     {   Header triggeredH;
01135         rpmdbMatchIterator mi;
01136         int countCorrection = psm->countCorrection;
01137 
01138         psm->countCorrection = 0;
01139         mi = rpmtsInitIterator(ts, RPMTAG_TRIGGERNAME, N, 0);
01140         while((triggeredH = rpmdbNextIterator(mi)) != NULL)
01141             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01142         mi = rpmdbFreeIterator(mi);
01143         psm->countCorrection = countCorrection;
01144     }
01145 
01146     return rc;
01147 }
01148 
01154 static rpmRC runImmedTriggers(rpmpsm psm)
01155         /*@globals rpmGlobalMacroContext, h_errno,
01156                 fileSystem, internalState @*/
01157         /*@modifies psm, rpmGlobalMacroContext,
01158                 fileSystem, internalState @*/
01159 {
01160     const rpmts ts = psm->ts;
01161     rpmfi fi = psm->fi;
01162     HGE_t hge = fi->hge;
01163     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01164     const char ** triggerNames;
01165     int numTriggers;
01166     int_32 * triggerIndices;
01167     rpmTagType tnt, tit;
01168     int numTriggerIndices;
01169     unsigned char * triggersRun;
01170     rpmRC rc = RPMRC_OK;
01171 
01172     if (fi->h == NULL)  return rc;      /* XXX can't happen */
01173 
01174     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01175                         (void **) &triggerNames, &numTriggers) &&
01176                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01177                         (void **) &triggerIndices, &numTriggerIndices))
01178         )
01179         return rc;
01180 
01181     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01182     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01183 
01184     {   Header sourceH = NULL;
01185         int i;
01186 
01187         for (i = 0; i < numTriggers; i++) {
01188             rpmdbMatchIterator mi;
01189 
01190             if (triggersRun[triggerIndices[i]] != 0) continue;
01191         
01192             mi = rpmtsInitIterator(ts, RPMTAG_NAME, triggerNames[i], 0);
01193 
01194             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01195                 rc |= handleOneTrigger(psm, sourceH, fi->h,
01196                                 rpmdbGetIteratorCount(mi),
01197                                 triggersRun);
01198             }
01199 
01200             mi = rpmdbFreeIterator(mi);
01201         }
01202     }
01203     triggerIndices = hfd(triggerIndices, tit);
01204     triggerNames = hfd(triggerNames, tnt);
01205     return rc;
01206 }
01207 
01208 /*@observer@*/ static const char *const pkgStageString(pkgStage a)
01209         /*@*/
01210 {
01211     switch(a) {
01212     case PSM_UNKNOWN:           return "unknown";
01213 
01214     case PSM_PKGINSTALL:        return "  install";
01215     case PSM_PKGERASE:          return "    erase";
01216     case PSM_PKGCOMMIT:         return "   commit";
01217     case PSM_PKGSAVE:           return "repackage";
01218 
01219     case PSM_INIT:              return "init";
01220     case PSM_PRE:               return "pre";
01221     case PSM_PROCESS:           return "process";
01222     case PSM_POST:              return "post";
01223     case PSM_UNDO:              return "undo";
01224     case PSM_FINI:              return "fini";
01225 
01226     case PSM_CREATE:            return "create";
01227     case PSM_NOTIFY:            return "notify";
01228     case PSM_DESTROY:           return "destroy";
01229     case PSM_COMMIT:            return "commit";
01230 
01231     case PSM_CHROOT_IN:         return "chrootin";
01232     case PSM_CHROOT_OUT:        return "chrootout";
01233     case PSM_SCRIPT:            return "script";
01234     case PSM_TRIGGERS:          return "triggers";
01235     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01236 
01237     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01238 
01239     case PSM_RPMDB_LOAD:        return "rpmdbload";
01240     case PSM_RPMDB_ADD:         return "rpmdbadd";
01241     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01242 
01243     default:                    return "???";
01244     }
01245     /*@noteached@*/
01246 }
01247 
01248 rpmpsm XrpmpsmUnlink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01249 {
01250     if (psm == NULL) return NULL;
01251 /*@-modfilesys@*/
01252 if (_psm_debug && msg != NULL)
01253 fprintf(stderr, "--> psm %p -- %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01254 /*@=modfilesys@*/
01255     psm->nrefs--;
01256     return NULL;
01257 }
01258 
01259 rpmpsm XrpmpsmLink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01260 {
01261     if (psm == NULL) return NULL;
01262     psm->nrefs++;
01263 
01264 /*@-modfilesys@*/
01265 if (_psm_debug && msg != NULL)
01266 fprintf(stderr, "--> psm %p ++ %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01267 /*@=modfilesys@*/
01268 
01269     /*@-refcounttrans@*/ return psm; /*@=refcounttrans@*/
01270 }
01271 
01272 rpmpsm rpmpsmFree(rpmpsm psm)
01273 {
01274     const char * msg = "rpmpsmFree";
01275     if (psm == NULL)
01276         return NULL;
01277 
01278     if (psm->nrefs > 1)
01279         return rpmpsmUnlink(psm, msg);
01280 
01281 /*@-nullstate@*/
01282     psm->fi = rpmfiFree(psm->fi);
01283 #ifdef  NOTYET
01284     psm->te = rpmteFree(psm->te);
01285 #else
01286     psm->te = NULL;
01287 #endif
01288 /*@-internalglobs@*/
01289     psm->ts = rpmtsFree(psm->ts);
01290 /*@=internalglobs@*/
01291 
01292     (void) rpmpsmUnlink(psm, msg);
01293 
01294     /*@-refcounttrans -usereleased@*/
01295 /*@-boundswrite@*/
01296     memset(psm, 0, sizeof(*psm));               /* XXX trash and burn */
01297 /*@=boundswrite@*/
01298     psm = _free(psm);
01299     /*@=refcounttrans =usereleased@*/
01300 
01301     return NULL;
01302 /*@=nullstate@*/
01303 }
01304 
01305 rpmpsm rpmpsmNew(rpmts ts, rpmte te, rpmfi fi)
01306 {
01307     const char * msg = "rpmpsmNew";
01308     rpmpsm psm = xcalloc(1, sizeof(*psm));
01309 
01310     if (ts)     psm->ts = rpmtsLink(ts, msg);
01311 #ifdef  NOTYET
01312     if (te)     psm->te = rpmteLink(te, msg);
01313 #else
01314 /*@-assignexpose -temptrans @*/
01315     if (te)     psm->te = te;
01316 /*@=assignexpose =temptrans @*/
01317 #endif
01318     if (fi)     psm->fi = rpmfiLink(fi, msg);
01319 
01320     return rpmpsmLink(psm, msg);
01321 }
01322 
01323 static void * rpmpsmThread(void * arg)
01324         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01325         /*@modifies arg, rpmGlobalMacroContext, fileSystem, internalState @*/
01326 {
01327     rpmpsm psm = arg;
01328 /*@-unqualifiedtrans@*/
01329     return ((void *) rpmpsmStage(psm, psm->nstage));
01330 /*@=unqualifiedtrans@*/
01331 }
01332 
01333 static int rpmpsmNext(rpmpsm psm, pkgStage nstage)
01334         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01335         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
01336 {
01337     psm->nstage = nstage;
01338     if (_psm_threads)
01339         return rpmsqJoin( rpmsqThread(rpmpsmThread, psm) );
01340     return rpmpsmStage(psm, psm->nstage);
01341 }
01342 
01347 /*@-bounds -nullpass@*/ /* FIX: testing null annotation for fi->h */
01348 rpmRC rpmpsmStage(rpmpsm psm, pkgStage stage)
01349 {
01350     const rpmts ts = psm->ts;
01351     uint_32 tscolor = rpmtsColor(ts);
01352     rpmfi fi = psm->fi;
01353     HGE_t hge = fi->hge;
01354     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01355     rpmRC rc = psm->rc;
01356     int saveerrno;
01357     int xx;
01358 
01359     /*@-branchstate@*/
01360     switch (stage) {
01361     case PSM_UNKNOWN:
01362         break;
01363     case PSM_INIT:
01364         rpmMessage(RPMMESS_DEBUG, _("%s: %s has %d files, test = %d\n"),
01365                 psm->stepName, rpmteNEVR(psm->te),
01366                 rpmfiFC(fi), (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST));
01367 
01368         /*
01369          * When we run scripts, we pass an argument which is the number of
01370          * versions of this package that will be installed when we are
01371          * finished.
01372          */
01373         psm->npkgs_installed = rpmdbCountPackages(rpmtsGetRdb(ts), rpmteN(psm->te));
01374         if (psm->npkgs_installed < 0) {
01375             rc = RPMRC_FAIL;
01376             break;
01377         }
01378 
01379         /* If we have a score then autorollback is enabled.  If autorollback is
01380          * enabled, and this is an autorollback transaction, then we may need to
01381          * adjust the pkgs installed count.
01382          *
01383          * If all this is true, this adjustment should only be made if the PSM goal
01384          * is an install.  No need to make this adjustment on the erase
01385          * component of the upgrade, or even more absurd to do this when doing a
01386          * PKGSAVE.
01387          */
01388         if (rpmtsGetScore(ts) != NULL &&
01389             rpmtsGetType(ts) == RPMTRANS_TYPE_AUTOROLLBACK &&
01390             (psm->goal & ~(PSM_PKGSAVE|PSM_PKGERASE))) {
01391             /* Get the score, if its not NULL, get the appropriate
01392              * score entry.
01393              */
01394             rpmtsScore score = rpmtsGetScore(ts);
01395             if (score != NULL) {
01396                 /* OK, we got a real score so lets get the appropriate
01397                  * score entry.
01398                  */
01399                 rpmtsScoreEntry se;
01400                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
01401 
01402                 /* IF the header for the install element has been installed,
01403                  * but the header for the erase element has not been erased,
01404                  * then decrement the instance count.  This is because in an
01405                  * autorollback, if the header was added in the initial transaction
01406                  * then in the case of an upgrade the instance count will be
01407                  * 2 instead of one when re-installing the old package, and 3 when
01408                  * erasing the new package.
01409                  *
01410                  * Another wrinkle is we only want to make this adjustement
01411                  * if the thing we are rollback was an upgrade of package.  A pure
01412                  * install or erase does not need the adjustment
01413                  */
01414                 if (se && se->installed &&
01415                     !se->erased &&
01416                     (se->te_types & (TR_ADDED|TR_REMOVED)))
01417                     psm->npkgs_installed--;
01418            }
01419         }
01420 
01421         if (psm->goal == PSM_PKGINSTALL) {
01422             int fc = rpmfiFC(fi);
01423 
01424             psm->scriptArg = psm->npkgs_installed + 1;
01425 
01426 assert(psm->mi == NULL);
01427             psm->mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(psm->te), 0);
01428             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_EPOCH, RPMMIRE_STRCMP,
01429                         rpmteE(psm->te));
01430             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION, RPMMIRE_STRCMP,
01431                         rpmteV(psm->te));
01432             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE, RPMMIRE_STRCMP,
01433                         rpmteR(psm->te));
01434             if (tscolor) {
01435                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_ARCH, RPMMIRE_STRCMP,
01436                         rpmteA(psm->te));
01437                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_OS, RPMMIRE_STRCMP,
01438                         rpmteO(psm->te));
01439             }
01440 
01441             while ((psm->oh = rpmdbNextIterator(psm->mi)) != NULL) {
01442                 fi->record = rpmdbGetIteratorOffset(psm->mi);
01443                 psm->oh = NULL;
01444                 /*@loopbreak@*/ break;
01445             }
01446             psm->mi = rpmdbFreeIterator(psm->mi);
01447             rc = RPMRC_OK;
01448 
01449             /* XXX lazy alloc here may need to be done elsewhere. */
01450             if (fi->fstates == NULL && fc > 0) {
01451                 fi->fstates = xmalloc(sizeof(*fi->fstates) * fc);
01452                 memset(fi->fstates, RPMFILE_STATE_NORMAL, fc);
01453             }
01454 
01455             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01456             if (fc <= 0)                                break;
01457         
01458             /*
01459              * Old format relocatable packages need the entire default
01460              * prefix stripped to form the cpio list, while all other packages
01461              * need the leading / stripped.
01462              */
01463             {   const char * p;
01464                 xx = hge(fi->h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &p, NULL);
01465                 fi->striplen = (xx ? strlen(p) + 1 : 1);
01466             }
01467             fi->mapflags =
01468                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01469         
01470             if (headerIsEntry(fi->h, RPMTAG_ORIGBASENAMES))
01471                 rpmfiBuildFNames(fi->h, RPMTAG_ORIGBASENAMES, &fi->apath, NULL);
01472             else
01473                 rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
01474         
01475             if (fi->fuser == NULL)
01476                 xx = hge(fi->h, RPMTAG_FILEUSERNAME, NULL,
01477                                 (void **) &fi->fuser, NULL);
01478             if (fi->fgroup == NULL)
01479                 xx = hge(fi->h, RPMTAG_FILEGROUPNAME, NULL,
01480                                 (void **) &fi->fgroup, NULL);
01481             rc = RPMRC_OK;
01482         }
01483         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01484             psm->scriptArg = psm->npkgs_installed - 1;
01485         
01486             /* Retrieve installed header. */
01487             rc = rpmpsmNext(psm, PSM_RPMDB_LOAD);
01488 if (rc == RPMRC_OK)
01489 if (psm->te)
01490 psm->te->h = headerLink(fi->h);
01491         }
01492         if (psm->goal == PSM_PKGSAVE) {
01493             /* Open output package for writing. */
01494             {   const char * bfmt = rpmGetPath("%{_repackage_name_fmt}", NULL);
01495                 const char * pkgbn =
01496                         headerSprintf(fi->h, bfmt, rpmTagTable, rpmHeaderFormats, NULL);
01497 
01498                 bfmt = _free(bfmt);
01499                 psm->pkgURL = rpmGenPath("%{?_repackage_root}",
01500                                          "%{?_repackage_dir}",
01501                                         pkgbn);
01502                 pkgbn = _free(pkgbn);
01503                 (void) urlPath(psm->pkgURL, &psm->pkgfn);
01504                 psm->fd = Fopen(psm->pkgfn, "w.ufdio");
01505                 if (psm->fd == NULL || Ferror(psm->fd)) {
01506                     rc = RPMRC_FAIL;
01507                     break;
01508                 }
01509             }
01510         }
01511         break;
01512     case PSM_PRE:
01513         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01514 
01515 /* XXX insure that trigger index is opened before entering chroot. */
01516 #ifdef  NOTYET
01517  { static int oneshot = 0;
01518    dbiIndex dbi;
01519    if (!oneshot) {
01520      dbi = dbiOpen(rpmtsGetRdb(ts), RPMTAG_TRIGGERNAME, 0);
01521      oneshot++;
01522    }
01523  }
01524 #endif
01525 
01526         /* Change root directory if requested and not already done. */
01527         rc = rpmpsmNext(psm, PSM_CHROOT_IN);
01528 
01529         if (psm->goal == PSM_PKGINSTALL) {
01530             psm->scriptTag = RPMTAG_PREIN;
01531             psm->progTag = RPMTAG_PREINPROG;
01532 
01533             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPREIN)) {
01534                 /* XXX FIXME: implement %triggerprein. */
01535             }
01536 
01537             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPRE)) {
01538                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01539                 if (rc != RPMRC_OK) {
01540                     rpmError(RPMERR_SCRIPT,
01541                         _("%s: %s scriptlet failed (%d), skipping %s\n"),
01542                         psm->stepName, tag2sln(psm->scriptTag), rc,
01543                         rpmteNEVR(psm->te));
01544                     break;
01545                 }
01546             }
01547         }
01548 
01549         if (psm->goal == PSM_PKGERASE) {
01550             psm->scriptTag = RPMTAG_PREUN;
01551             psm->progTag = RPMTAG_PREUNPROG;
01552             psm->sense = RPMSENSE_TRIGGERUN;
01553             psm->countCorrection = -1;
01554 
01555             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERUN)) {
01556                 /* Run triggers in this package other package(s) set off. */
01557                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01558                 if (rc) break;
01559 
01560                 /* Run triggers in other package(s) this package sets off. */
01561                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01562                 if (rc) break;
01563             }
01564 
01565             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPREUN))
01566                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01567         }
01568         if (psm->goal == PSM_PKGSAVE) {
01569             int noArchiveSize = 0;
01570 
01571             /* Regenerate original header. */
01572             {   void * uh = NULL;
01573                 int_32 uht, uhc;
01574 
01575                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)) {
01576                     psm->oh = headerCopyLoad(uh);
01577                     uh = hfd(uh, uht);
01578                 } else
01579                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMAGE, &uht, &uh, &uhc))
01580                 {
01581                     HeaderIterator hi;
01582                     int_32 tag, type, count;
01583                     hPTR_t ptr;
01584                     Header oh;
01585 
01586                     /* Load the original header from the blob. */
01587                     oh = headerCopyLoad(uh);
01588 
01589                     /* XXX this is headerCopy w/o headerReload() */
01590                     psm->oh = headerNew();
01591 
01592                     /*@-branchstate@*/
01593                     for (hi = headerInitIterator(oh);
01594                         headerNextIterator(hi, &tag, &type, &ptr, &count);
01595                         ptr = headerFreeData((void *)ptr, type))
01596                     {
01597                         if (tag == RPMTAG_ARCHIVESIZE)
01598                             noArchiveSize = 1;
01599                         if (ptr) (void) headerAddEntry(psm->oh, tag, type, ptr, count);
01600                     }
01601                     hi = headerFreeIterator(hi);
01602                     /*@=branchstate@*/
01603 
01604                     oh = headerFree(oh);
01605                     uh = hfd(uh, uht);
01606                 } else
01607                     break;      /* XXX shouldn't ever happen */
01608             }
01609 
01610             /* Retrieve type of payload compression. */
01611             /*@-nullstate@*/    /* FIX: psm->oh may be NULL */
01612             rc = rpmpsmNext(psm, PSM_RPMIO_FLAGS);
01613             /*@=nullstate@*/
01614 
01615             /* Write the lead section into the package. */
01616             {   int archnum = -1;
01617                 int osnum = -1;
01618                 struct rpmlead lead;
01619 
01620 #ifndef DYING
01621                 rpmGetArchInfo(NULL, &archnum);
01622                 rpmGetOsInfo(NULL, &osnum);
01623 #endif
01624 
01625                 memset(&lead, 0, sizeof(lead));
01626                 /* XXX Set package version conditioned on noDirTokens. */
01627                 lead.major = 3;
01628                 lead.minor = 0;
01629                 lead.type = RPMLEAD_BINARY;
01630                 lead.archnum = archnum;
01631                 lead.osnum = osnum;
01632                 lead.signature_type = RPMSIGTYPE_HEADERSIG;
01633 
01634                 strncpy(lead.name, rpmteNEVR(psm->te), sizeof(lead.name));
01635 
01636                 rc = writeLead(psm->fd, &lead);
01637                 if (rc != RPMRC_OK) {
01638                     rpmError(RPMERR_NOSPACE, _("Unable to write package: %s\n"),
01639                          Fstrerror(psm->fd));
01640                     break;
01641                 }
01642             }
01643 
01644             /* Write the signature section into the package. */
01645             /* XXX rpm-4.1 and later has archive size in signature header. */
01646             {   Header sigh = headerRegenSigHeader(fi->h, noArchiveSize);
01647                 /* Reallocate the signature into one contiguous region. */
01648                 sigh = headerReload(sigh, RPMTAG_HEADERSIGNATURES);
01649                 if (sigh == NULL) {
01650                     rpmError(RPMERR_NOSPACE, _("Unable to reload signature header\n"));
01651                     rc = RPMRC_FAIL;
01652                     break;
01653                 }
01654                 rc = rpmWriteSignature(psm->fd, sigh);
01655                 sigh = rpmFreeSignature(sigh);
01656                 if (rc) break;
01657             }
01658 
01659             /* Add remove transaction id to header. */
01660             if (psm->oh != NULL)
01661             {   int_32 tid = rpmtsGetTid(ts);
01662                 xx = headerAddEntry(psm->oh, RPMTAG_REMOVETID,
01663                         RPM_INT32_TYPE, &tid, 1);
01664             }
01665 
01666             /* Write the metadata section into the package. */
01667             rc = headerWrite(psm->fd, psm->oh, HEADER_MAGIC_YES);
01668             if (rc) break;
01669         }
01670         break;
01671     case PSM_PROCESS:
01672         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01673 
01674         if (psm->goal == PSM_PKGINSTALL) {
01675 
01676             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01677 
01678             /* XXX Synthesize callbacks for packages with no files. */
01679             if (rpmfiFC(fi) <= 0) {
01680                 void * ptr;
01681                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_START, 0, 100);
01682                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_PROGRESS, 100, 100);
01683                 break;
01684             }
01685 
01686             /* Retrieve type of payload compression. */
01687             rc = rpmpsmNext(psm, PSM_RPMIO_FLAGS);
01688 
01689             if (rpmteFd(fi->te) == NULL) {      /* XXX can't happen */
01690                 rc = RPMRC_FAIL;
01691                 break;
01692             }
01693 
01694             /*@-nullpass@*/     /* LCL: fi->fd != NULL here. */
01695             psm->cfd = Fdopen(fdDup(Fileno(rpmteFd(fi->te))), psm->rpmio_flags);
01696             /*@=nullpass@*/
01697             if (psm->cfd == NULL) {     /* XXX can't happen */
01698                 rc = RPMRC_FAIL;
01699                 break;
01700             }
01701 
01702             rc = fsmSetup(fi->fsm, FSM_PKGINSTALL, ts, fi,
01703                         psm->cfd, NULL, &psm->failedFile);
01704             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_UNCOMPRESS),
01705                         fdstat_op(psm->cfd, FDSTAT_READ));
01706             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST),
01707                         fdstat_op(psm->cfd, FDSTAT_DIGEST));
01708             xx = fsmTeardown(fi->fsm);
01709 
01710             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01711             xx = Fclose(psm->cfd);
01712             psm->cfd = NULL;
01713             /*@-mods@*/
01714             errno = saveerrno; /* XXX FIXME: Fclose with libio destroys errno */
01715             /*@=mods@*/
01716 
01717             if (!rc)
01718                 rc = rpmpsmNext(psm, PSM_COMMIT);
01719 
01720             /* XXX make sure progress is closed out */
01721             psm->what = RPMCALLBACK_INST_PROGRESS;
01722             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01723             psm->total = psm->amount;
01724             xx = rpmpsmNext(psm, PSM_NOTIFY);
01725 
01726             if (rc) {
01727                 rpmError(RPMERR_CPIO,
01728                         _("unpacking of archive failed%s%s: %s\n"),
01729                         (psm->failedFile != NULL ? _(" on file ") : ""),
01730                         (psm->failedFile != NULL ? psm->failedFile : ""),
01731                         cpioStrerror(rc));
01732                 rc = RPMRC_FAIL;
01733 
01734                 /* XXX notify callback on error. */
01735                 psm->what = RPMCALLBACK_UNPACK_ERROR;
01736                 psm->amount = 0;
01737                 psm->total = 0;
01738                 xx = rpmpsmNext(psm, PSM_NOTIFY);
01739 
01740                 break;
01741             }
01742         }
01743         if (psm->goal == PSM_PKGERASE) {
01744             int fc = rpmfiFC(fi);
01745 
01746             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01747             if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)       break;
01748             if (fc <= 0)                                break;
01749 
01750             psm->what = RPMCALLBACK_UNINST_START;
01751             psm->amount = fc;           /* XXX W2DO? looks wrong. */
01752             psm->total = fc;
01753             xx = rpmpsmNext(psm, PSM_NOTIFY);
01754 
01755             rc = fsmSetup(fi->fsm, FSM_PKGERASE, ts, fi,
01756                         NULL, NULL, &psm->failedFile);
01757             xx = fsmTeardown(fi->fsm);
01758 
01759             psm->what = RPMCALLBACK_UNINST_STOP;
01760             psm->amount = 0;            /* XXX W2DO? looks wrong. */
01761             psm->total = fc;
01762             xx = rpmpsmNext(psm, PSM_NOTIFY);
01763 
01764         }
01765         if (psm->goal == PSM_PKGSAVE) {
01766             fileAction * actions = fi->actions;
01767             fileAction action = fi->action;
01768 
01769             fi->action = FA_COPYOUT;
01770             fi->actions = NULL;
01771 
01772             if (psm->fd == NULL) {      /* XXX can't happen */
01773                 rc = RPMRC_FAIL;
01774                 break;
01775             }
01776             /*@-nullpass@*/     /* FIX: fdDup mey return NULL. */
01777             xx = Fflush(psm->fd);
01778             psm->cfd = Fdopen(fdDup(Fileno(psm->fd)), psm->rpmio_flags);
01779             /*@=nullpass@*/
01780             if (psm->cfd == NULL) {     /* XXX can't happen */
01781                 rc = RPMRC_FAIL;
01782                 break;
01783             }
01784 
01785             rc = fsmSetup(fi->fsm, FSM_PKGBUILD, ts, fi, psm->cfd,
01786                         NULL, &psm->failedFile);
01787             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_COMPRESS),
01788                         fdstat_op(psm->cfd, FDSTAT_WRITE));
01789             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST),
01790                         fdstat_op(psm->cfd, FDSTAT_DIGEST));
01791             xx = fsmTeardown(fi->fsm);
01792 
01793             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01794             xx = Fclose(psm->cfd);
01795             psm->cfd = NULL;
01796             /*@-mods@*/
01797             errno = saveerrno;
01798             /*@=mods@*/
01799 
01800             /* XXX make sure progress is closed out */
01801             psm->what = RPMCALLBACK_INST_PROGRESS;
01802             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01803             psm->total = psm->amount;
01804             xx = rpmpsmNext(psm, PSM_NOTIFY);
01805 
01806             fi->action = action;
01807             fi->actions = actions;
01808         }
01809         break;
01810     case PSM_POST:
01811         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01812 
01813         if (psm->goal == PSM_PKGINSTALL) {
01814             int_32 installTime = (int_32) time(NULL);
01815             int fc = rpmfiFC(fi);
01816 
01817             if (fi->h == NULL) break;   /* XXX can't happen */
01818             if (fi->fstates != NULL && fc > 0)
01819                 xx = headerAddEntry(fi->h, RPMTAG_FILESTATES, RPM_CHAR_TYPE,
01820                                 fi->fstates, fc);
01821 
01822             xx = headerAddEntry(fi->h, RPMTAG_INSTALLTIME, RPM_INT32_TYPE,
01823                                 &installTime, 1);
01824 
01825             xx = headerAddEntry(fi->h, RPMTAG_INSTALLCOLOR, RPM_INT32_TYPE,
01826                                 &tscolor, 1);
01827 
01828             /*
01829              * If this package has already been installed, remove it from
01830              * the database before adding the new one.
01831              */
01832             if (fi->record && !(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)) {
01833                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01834                 if (rc) break;
01835             }
01836 
01837             rc = rpmpsmNext(psm, PSM_RPMDB_ADD);
01838             if (rc) break;
01839 
01840             psm->scriptTag = RPMTAG_POSTIN;
01841             psm->progTag = RPMTAG_POSTINPROG;
01842             psm->sense = RPMSENSE_TRIGGERIN;
01843             psm->countCorrection = 0;
01844 
01845             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOST)) {
01846                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01847                 if (rc) break;
01848             }
01849             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERIN)) {
01850                 /* Run triggers in other package(s) this package sets off. */
01851                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01852                 if (rc) break;
01853 
01854                 /* Run triggers in this package other package(s) set off. */
01855                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01856                 if (rc) break;
01857             }
01858 
01859             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01860                 rc = markReplacedFiles(psm);
01861 
01862         }
01863         if (psm->goal == PSM_PKGERASE) {
01864 
01865             psm->scriptTag = RPMTAG_POSTUN;
01866             psm->progTag = RPMTAG_POSTUNPROG;
01867             psm->sense = RPMSENSE_TRIGGERPOSTUN;
01868             psm->countCorrection = -1;
01869 
01870             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOSTUN)) {
01871                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01872                 if (rc) break;
01873             }
01874 
01875             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
01876                 /* Run triggers in other package(s) this package sets off. */
01877                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01878                 if (rc) break;
01879             }
01880 
01881             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01882                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01883         }
01884         if (psm->goal == PSM_PKGSAVE) {
01885         }
01886 
01887         /* Restore root directory if changed. */
01888         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01889         break;
01890     case PSM_UNDO:
01891         break;
01892     case PSM_FINI:
01893         /* Restore root directory if changed. */
01894         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01895 
01896         if (psm->fd != NULL) {
01897             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01898             xx = Fclose(psm->fd);
01899             psm->fd = NULL;
01900             /*@-mods@*/
01901             errno = saveerrno;
01902             /*@=mods@*/
01903         }
01904 
01905         if (psm->goal == PSM_PKGSAVE) {
01906             if (!rc && ts && ts->notify == NULL) {
01907                 rpmMessage(RPMMESS_VERBOSE, _("Wrote: %s\n"),
01908                         (psm->pkgURL ? psm->pkgURL : "???"));
01909             }
01910         }
01911 
01912         if (rc) {
01913             if (psm->failedFile)
01914                 rpmError(RPMERR_CPIO,
01915                         _("%s failed on file %s: %s\n"),
01916                         psm->stepName, psm->failedFile, cpioStrerror(rc));
01917             else
01918                 rpmError(RPMERR_CPIO, _("%s failed: %s\n"),
01919                         psm->stepName, cpioStrerror(rc));
01920 
01921             /* XXX notify callback on error. */
01922             psm->what = RPMCALLBACK_CPIO_ERROR;
01923             psm->amount = 0;
01924             psm->total = 0;
01925             /*@-nullstate@*/ /* FIX: psm->fd may be NULL. */
01926             xx = rpmpsmNext(psm, PSM_NOTIFY);
01927             /*@=nullstate@*/
01928         }
01929 
01930 /*@-branchstate@*/
01931         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01932 if (psm->te != NULL)
01933 if (psm->te->h != NULL)
01934 psm->te->h = headerFree(psm->te->h);
01935             if (fi->h != NULL)
01936                 fi->h = headerFree(fi->h);
01937         }
01938 /*@=branchstate@*/
01939         psm->oh = headerFree(psm->oh);
01940         psm->pkgURL = _free(psm->pkgURL);
01941         psm->rpmio_flags = _free(psm->rpmio_flags);
01942         psm->failedFile = _free(psm->failedFile);
01943 
01944         fi->fgroup = hfd(fi->fgroup, -1);
01945         fi->fuser = hfd(fi->fuser, -1);
01946         fi->apath = _free(fi->apath);
01947         fi->fstates = _free(fi->fstates);
01948         break;
01949 
01950     case PSM_PKGINSTALL:
01951     case PSM_PKGERASE:
01952     case PSM_PKGSAVE:
01953         psm->goal = stage;
01954         psm->rc = RPMRC_OK;
01955         psm->stepName = pkgStageString(stage);
01956 
01957         rc = rpmpsmNext(psm, PSM_INIT);
01958         if (!rc) rc = rpmpsmNext(psm, PSM_PRE);
01959         if (!rc) rc = rpmpsmNext(psm, PSM_PROCESS);
01960         if (!rc) rc = rpmpsmNext(psm, PSM_POST);
01961         xx = rpmpsmNext(psm, PSM_FINI);
01962         break;
01963     case PSM_PKGCOMMIT:
01964         break;
01965 
01966     case PSM_CREATE:
01967         break;
01968     case PSM_NOTIFY:
01969     {   void * ptr;
01970 /*@-nullpass@*/ /* FIX: psm->te may be NULL */
01971         ptr = rpmtsNotify(ts, psm->te, psm->what, psm->amount, psm->total);
01972 /*@-nullpass@*/
01973     }   break;
01974     case PSM_DESTROY:
01975         break;
01976     case PSM_COMMIT:
01977         if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_PKGCOMMIT)) break;
01978         if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY) break;
01979 
01980         rc = fsmSetup(fi->fsm, FSM_PKGCOMMIT, ts, fi,
01981                         NULL, NULL, &psm->failedFile);
01982         xx = fsmTeardown(fi->fsm);
01983         break;
01984 
01985     case PSM_CHROOT_IN:
01986     {   const char * rootDir = rpmtsRootDir(ts);
01987         /* Change root directory if requested and not already done. */
01988         if (rootDir != NULL && !(rootDir[0] == '/' && rootDir[1] == '\0')
01989          && !rpmtsChrootDone(ts) && !psm->chrootDone)
01990         {
01991             static int _pw_loaded = 0;
01992             static int _gr_loaded = 0;
01993 
01994             if (!_pw_loaded) {
01995                 (void)getpwnam("root");
01996                 endpwent();
01997                 _pw_loaded++;
01998             }
01999             if (!_gr_loaded) {
02000                 (void)getgrnam("root");
02001                 endgrent();
02002                 _gr_loaded++;
02003             }
02004 
02005             xx = chdir("/");
02006             /*@-superuser@*/
02007             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
02008                 rc = chroot(rootDir);
02009             /*@=superuser@*/
02010             psm->chrootDone = 1;
02011             (void) rpmtsSetChrootDone(ts, 1);
02012         }
02013     }   break;
02014     case PSM_CHROOT_OUT:
02015         /* Restore root directory if changed. */
02016         if (psm->chrootDone) {
02017             const char * rootDir = rpmtsRootDir(ts);
02018             const char * currDir = rpmtsCurrDir(ts);
02019             /*@-superuser@*/
02020             if (rootDir != NULL && strcmp(rootDir, "/") && *rootDir == '/')
02021                 rc = chroot(".");
02022             /*@=superuser@*/
02023             psm->chrootDone = 0;
02024             (void) rpmtsSetChrootDone(ts, 0);
02025             if (currDir != NULL)        /* XXX can't happen */
02026                 xx = chdir(currDir);
02027         }
02028         break;
02029     case PSM_SCRIPT:    /* Run current package scriptlets. */
02030         rc = runInstScript(psm);
02031         break;
02032     case PSM_TRIGGERS:
02033         /* Run triggers in other package(s) this package sets off. */
02034         rc = runTriggers(psm);
02035         break;
02036     case PSM_IMMED_TRIGGERS:
02037         /* Run triggers in this package other package(s) set off. */
02038         rc = runImmedTriggers(psm);
02039         break;
02040 
02041     case PSM_RPMIO_FLAGS:
02042     {   const char * payload_compressor = NULL;
02043         char * t;
02044 
02045         /*@-branchstate@*/
02046         if (!hge(fi->h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
02047                             (void **) &payload_compressor, NULL))
02048             payload_compressor = "gzip";
02049         /*@=branchstate@*/
02050         psm->rpmio_flags = t = xmalloc(sizeof("w9.gzdio"));
02051         *t = '\0';
02052         t = stpcpy(t, ((psm->goal == PSM_PKGSAVE) ? "w9" : "r"));
02053         if (!strcmp(payload_compressor, "gzip"))
02054             t = stpcpy(t, ".gzdio");
02055         if (!strcmp(payload_compressor, "bzip2"))
02056             t = stpcpy(t, ".bzdio");
02057         rc = RPMRC_OK;
02058     }   break;
02059 
02060     case PSM_RPMDB_LOAD:
02061 assert(psm->mi == NULL);
02062         psm->mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
02063                                 &fi->record, sizeof(fi->record));
02064 
02065         fi->h = rpmdbNextIterator(psm->mi);
02066         if (fi->h != NULL)
02067             fi->h = headerLink(fi->h);
02068 
02069         psm->mi = rpmdbFreeIterator(psm->mi);
02070         rc = (fi->h != NULL ? RPMRC_OK : RPMRC_FAIL);
02071         break;
02072     case PSM_RPMDB_ADD:
02073         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02074         if (fi->h == NULL)      break;  /* XXX can't happen */
02075         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02076         if (!(rpmtsVSFlags(ts) & RPMVSF_NOHDRCHK))
02077             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02078                                 ts, headerCheck);
02079         else
02080             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
02081                                 NULL, NULL);
02082 
02083         /* Set the database instance so consumers (i.e. rpmtsRun())
02084          * can add this to a rollback transaction.
02085          */
02086         rpmteSetDBInstance(psm->te, myinstall_instance);
02087 
02088         /*
02089          * If the score exists and this is not a rollback or autorollback
02090          * then lets check off installed for this package.
02091          */
02092         if (rpmtsGetScore(ts) != NULL &&
02093             rpmtsGetType(ts) != RPMTRANS_TYPE_ROLLBACK &&
02094             rpmtsGetType(ts) != RPMTRANS_TYPE_AUTOROLLBACK)
02095         {
02096             /* Get the score, if its not NULL, get the appropriate
02097              * score entry.
02098              */
02099             rpmtsScore score = rpmtsGetScore(ts);
02100             if (score != NULL) {
02101                 rpmtsScoreEntry se;
02102                 /* OK, we got a real score so lets get the appropriate
02103                  * score entry.
02104                  */
02105                 rpmMessage(RPMMESS_DEBUG,
02106                     _("Attempting to mark %s as installed in score board(%u).\n"),
02107                     rpmteN(psm->te), (unsigned) score);
02108                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
02109                 if (se != NULL) se->installed = 1;
02110             }
02111         }
02112         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
02113         break;
02114     case PSM_RPMDB_REMOVE:
02115         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
02116         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02117         rc = rpmdbRemove(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->record,
02118                                 NULL, NULL);
02119 
02120         /*
02121          * If the score exists and this is not a rollback or autorollback
02122          * then lets check off erased for this package.
02123          */
02124         if (rpmtsGetScore(ts) != NULL &&
02125            rpmtsGetType(ts) != RPMTRANS_TYPE_ROLLBACK &&
02126            rpmtsGetType(ts) != RPMTRANS_TYPE_AUTOROLLBACK)
02127         {
02128             /* Get the score, if its not NULL, get the appropriate
02129              * score entry.
02130              */
02131             rpmtsScore score = rpmtsGetScore(ts);
02132 
02133             if (score != NULL) { /* XXX: Can't happen */
02134                 rpmtsScoreEntry se;
02135                 /* OK, we got a real score so lets get the appropriate
02136                  * score entry.
02137                  */
02138                 rpmMessage(RPMMESS_DEBUG,
02139                     _("Attempting to mark %s as erased in score board(0x%x).\n"),
02140                     rpmteN(psm->te), (unsigned) score);
02141                 se = rpmtsScoreGetEntry(score, rpmteN(psm->te));
02142                 if (se != NULL) se->erased = 1;
02143             }
02144         }
02145 
02146         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
02147         break;
02148 
02149     default:
02150         break;
02151 /*@i@*/    }
02152     /*@=branchstate@*/
02153 
02154     /*@-nullstate@*/    /* FIX: psm->oh and psm->fi->h may be NULL. */
02155     return rc;
02156     /*@=nullstate@*/
02157 }
02158 /*@=bounds =nullpass@*/

Generated on Wed Sep 26 03:00:54 2007 for rpm by  doxygen 1.4.7