Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

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

Generated on Thu Nov 3 20:15:01 2005 for rpm by doxygen 1.3.5