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

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, *a;
00535 
00536     if (progArgv == NULL && script == NULL)
00537         return rc;
00538 
00539     psm->sq.reaper = 1;
00540 
00541     /* XXX FIXME: except for %verifyscript, rpmteNEVR can be used. */
00542     xx = headerNVR(h, &n, &v, &r);
00543     xx = hge(h, RPMTAG_ARCH, NULL, (void **) &a, NULL);
00544 
00545     /* XXX bash must have functional libtermcap.so.2 */
00546     if (!strcmp(n, "libtermcap"))
00547         ldconfig_done = 0;
00548 
00549     /*
00550      * If a successor node, and ldconfig was just run, don't bother.
00551      */
00552     if (ldconfig_path && progArgv && psm->unorderedSuccessor) {
00553         if (ldconfig_done && !strcmp(progArgv[0], ldconfig_path)) {
00554             rpmMessage(RPMMESS_DEBUG,
00555                 _("%s: %s(%s-%s-%s.%s) skipping redundant \"%s\".\n"),
00556                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a,
00557                 progArgv[0]);
00558             return rc;
00559         }
00560     }
00561 
00562     rpmMessage(RPMMESS_DEBUG,
00563                 _("%s: %s(%s-%s-%s.%s) %ssynchronous scriptlet start\n"),
00564                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a,
00565                 (psm->unorderedSuccessor ? "a" : ""));
00566 
00567     if (!progArgv) {
00568         argv = alloca(5 * sizeof(*argv));
00569         argv[0] = "/bin/sh";
00570         argc = 1;
00571         ldconfig_done = 0;
00572     } else {
00573         argv = alloca((progArgc + 4) * sizeof(*argv));
00574         memcpy(argv, progArgv, progArgc * sizeof(*argv));
00575         argc = progArgc;
00576         ldconfig_done = (ldconfig_path && !strcmp(argv[0], ldconfig_path)
00577                 ? 1 : 0);
00578     }
00579 
00580 #if __ia64__
00581     /* XXX This assumes that all interpreters are elf executables. */
00582     if ((a != NULL && a[0] == 'i' && a[1] != '\0' && a[2] == '8' && a[3] == '6')
00583      && strcmp(argv[0], "/sbin/ldconfig"))
00584     {
00585         const char * fmt = rpmGetPath("%{?_autorelocate_path}", NULL);
00586         const char * errstr;
00587         char * newPath;
00588         char * t;
00589 
00590         newPath = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
00591         fmt = _free(fmt);
00592 
00593         /* XXX On ia64, change leading /emul/ix86 -> /emul/ia32, ick. */
00594         if (newPath != NULL && *newPath != '\0'
00595          && strlen(newPath) >= (sizeof("/emul/i386")-1)
00596          && newPath[0] == '/' && newPath[1] == 'e' && newPath[2] == 'm'
00597          && newPath[3] == 'u' && newPath[4] == 'l' && newPath[5] == '/'
00598          && newPath[6] == 'i' && newPath[8] == '8' && newPath[9] == '6')
00599         {
00600             newPath[7] = 'a';
00601             newPath[8] = '3';
00602             newPath[9] = '2';
00603         }
00604 
00605         t = alloca(strlen(newPath) + strlen(argv[0]) + 1);
00606         *t = '\0';
00607         (void) stpcpy( stpcpy(t, newPath), argv[0]);
00608         newPath = _free(newPath);
00609         argv[0] = t;
00610     }
00611 #endif
00612 
00613     if (hge(h, RPMTAG_INSTPREFIXES, &ipt, (void **) &prefixes, &numPrefixes)) {
00614         freePrefixes = 1;
00615     } else if (hge(h, RPMTAG_INSTALLPREFIX, NULL, (void **) &oldPrefix, NULL)) {
00616         prefixes = &oldPrefix;
00617         numPrefixes = 1;
00618     } else {
00619         numPrefixes = 0;
00620     }
00621 
00622     maxPrefixLength = 0;
00623     if (prefixes != NULL)
00624     for (i = 0; i < numPrefixes; i++) {
00625         len = strlen(prefixes[i]);
00626         if (len > maxPrefixLength) maxPrefixLength = len;
00627     }
00628     prefixBuf = alloca(maxPrefixLength + 50);
00629 
00630     if (script) {
00631         const char * rootDir = rpmtsRootDir(ts);
00632         FD_t fd;
00633 
00634         /*@-branchstate@*/
00635         if (makeTempFile((!rpmtsChrootDone(ts) ? rootDir : "/"), &fn, &fd)) {
00636             if (prefixes != NULL && freePrefixes) free(prefixes);
00637             return RPMRC_FAIL;
00638         }
00639         /*@=branchstate@*/
00640 
00641         if (rpmIsDebug() &&
00642             (!strcmp(argv[0], "/bin/sh") || !strcmp(argv[0], "/bin/bash")))
00643         {
00644             static const char set_x[] = "set -x\n";
00645             xx = Fwrite(set_x, sizeof(set_x[0]), sizeof(set_x)-1, fd);
00646         }
00647 
00648         if (ldconfig_path && strstr(script, ldconfig_path) != NULL)
00649             ldconfig_done = 1;
00650 
00651         xx = Fwrite(script, sizeof(script[0]), strlen(script), fd);
00652         xx = Fclose(fd);
00653 
00654         {   const char * sn = fn;
00655             if (!rpmtsChrootDone(ts) && rootDir != NULL &&
00656                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00657             {
00658                 sn += strlen(rootDir)-1;
00659             }
00660             argv[argc++] = sn;
00661         }
00662 
00663         if (arg1 >= 0) {
00664             char *av = alloca(20);
00665             sprintf(av, "%d", arg1);
00666             argv[argc++] = av;
00667         }
00668         if (arg2 >= 0) {
00669             char *av = alloca(20);
00670             sprintf(av, "%d", arg2);
00671             argv[argc++] = av;
00672         }
00673     }
00674 
00675     argv[argc] = NULL;
00676 
00677     scriptFd = rpmtsScriptFd(ts);
00678     if (scriptFd != NULL) {
00679         if (rpmIsVerbose()) {
00680             out = fdDup(Fileno(scriptFd));
00681         } else {
00682             out = Fopen("/dev/null", "w.fdio");
00683             if (Ferror(out)) {
00684                 out = fdDup(Fileno(scriptFd));
00685             }
00686         }
00687     } else {
00688         out = fdDup(STDOUT_FILENO);
00689     }
00690     if (out == NULL) return RPMRC_FAIL; /* XXX can't happen */
00691     
00692     /*@-branchstate@*/
00693     xx = rpmsqFork(&psm->sq);
00694     if (psm->sq.child == 0) {
00695         const char * rootDir;
00696         int pipes[2];
00697         int flag;
00698         int fdno;
00699         int open_max;
00700 
00701         pipes[0] = pipes[1] = 0;
00702         /* make stdin inaccessible */
00703         xx = pipe(pipes);
00704         xx = close(pipes[1]);
00705         xx = dup2(pipes[0], STDIN_FILENO);
00706         xx = close(pipes[0]);
00707 
00708         /* XXX Force FD_CLOEXEC on all inherited fdno's. */
00709         open_max = sysconf(_SC_OPEN_MAX);
00710         if (open_max == -1) {
00711             open_max = 1024;
00712         }
00713         for (fdno = 3; fdno < open_max; fdno++) {
00714             flag = fcntl(fdno, F_GETFD);
00715             if (flag == -1 || (flag & FD_CLOEXEC))
00716                 continue;
00717             xx = fcntl(fdno, F_SETFD, FD_CLOEXEC);
00718             /* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */
00719         }
00720 
00721         if (scriptFd != NULL) {
00722             int sfdno = Fileno(scriptFd);
00723             int ofdno = Fileno(out);
00724             if (sfdno != STDERR_FILENO)
00725                 xx = dup2(sfdno, STDERR_FILENO);
00726             if (ofdno != STDOUT_FILENO)
00727                 xx = dup2(ofdno, STDOUT_FILENO);
00728             /* make sure we don't close stdin/stderr/stdout by mistake! */
00729             if (ofdno > STDERR_FILENO && ofdno != sfdno)
00730                 xx = Fclose (out);
00731             if (sfdno > STDERR_FILENO)
00732                 xx = Fclose (scriptFd);
00733             else {
00734 /*@-usereleased@*/
00735                 xx = Fclose(out);
00736 /*@=usereleased@*/
00737             }
00738         }
00739 
00740         {   const char *ipath = rpmExpand("PATH=%{_install_script_path}", NULL);
00741             const char *path = SCRIPT_PATH;
00742 
00743             if (ipath && ipath[5] != '%')
00744                 path = ipath;
00745 
00746             xx = doputenv(path);
00747             /*@-modobserver@*/
00748             ipath = _free(ipath);
00749             /*@=modobserver@*/
00750         }
00751 
00752         if (prefixes != NULL)
00753         for (i = 0; i < numPrefixes; i++) {
00754             sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
00755             xx = doputenv(prefixBuf);
00756 
00757             /* backwards compatibility */
00758             if (i == 0) {
00759                 sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
00760                 xx = doputenv(prefixBuf);
00761             }
00762         }
00763 
00764         rootDir = rpmtsRootDir(ts);
00765         if (rootDir  != NULL)   /* XXX can't happen */
00766         switch(urlIsURL(rootDir)) {
00767         case URL_IS_PATH:
00768             rootDir += sizeof("file://") - 1;
00769             rootDir = strchr(rootDir, '/');
00770             /*@fallthrough@*/
00771         case URL_IS_UNKNOWN:
00772             if (!rpmtsChrootDone(ts) &&
00773                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00774             {
00775                 /*@-superuser -noeffect @*/
00776                 xx = chroot(rootDir);
00777                 /*@=superuser =noeffect @*/
00778             }
00779             xx = chdir("/");
00780             rpmMessage(RPMMESS_DEBUG, _("%s: %s(%s-%s-%s.%s)\texecv(%s) pid %d\n"),
00781                         psm->stepName, sln, n, v, r, a,
00782                         argv[0], (unsigned)getpid());
00783 
00784             /* XXX Don't mtrace into children. */
00785             unsetenv("MALLOC_CHECK_");
00786 
00787 /*@-nullstate@*/
00788 
00789 #if defined(__GLIBC__)
00790 
00794             {
00795                char* bypassVar = (char*) malloc(1024*sizeof(char));
00796                if (bypassVar != NULL)
00797                {
00798                   snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00799                   bypassVar[1023] = '\0';
00800                   if (getenv(bypassVar) != NULL)
00801                   {
00802                      char* bypassVal = (char*) malloc(1024*sizeof(char));
00803                      if (bypassVal != NULL)
00804                      {
00805                         rpmMessage(RPMMESS_DEBUG, _("Restoring LD_ASSUME_KERNEL for child scripts.\n"));
00806                         snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00807                         unsetenv(bypassVar);
00808                         snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00809                         bypassVar[1023] = '\0';
00810                         putenv(bypassVar);
00811                         free(bypassVal);
00812                      }
00813                      else
00814                      {
00815                         free(bypassVar);
00816                      }
00817                   }
00818                }
00819             }
00820 #endif
00821 
00822             /* Permit libselinux to do the scriptlet exec. */
00823             if (rpmtsSELinuxEnabled(ts) == 1) {
00824                 xx = rpm_execcon(0, argv[0], argv, environ);
00825                 if (xx != 0)
00826                     break;
00827             }
00828 
00829             xx = execv(argv[0], (char *const *)argv);
00830 /*@=nullstate@*/
00831             break;
00832         default:
00833             break;
00834         }
00835 
00836         _exit(-1);
00837         /*@notreached@*/
00838     }
00839     /*@=branchstate@*/
00840 
00841     (void) psmWait(psm);
00842 
00843   /* XXX filter order dependent multilib "other" arch helper error. */
00844   if (!(psm->sq.reaped >= 0 && !strcmp(argv[0], "/usr/sbin/glibc_post_upgrade") && WEXITSTATUS(psm->sq.status) == 110)) {
00845     if (psm->sq.reaped < 0) {
00846         rpmError(RPMERR_SCRIPT,
00847                 _("%s(%s-%s-%s.%s) scriptlet failed, waitpid(%d) rc %d: %s\n"),
00848                  sln, n, v, r, a, psm->sq.child, psm->sq.reaped, strerror(errno));
00849         rc = RPMRC_FAIL;
00850     } else
00851     if (!WIFEXITED(psm->sq.status) || WEXITSTATUS(psm->sq.status)) {
00852         rpmError(RPMERR_SCRIPT,
00853                 _("%s(%s-%s-%s.%s) scriptlet failed, exit status %d\n"),
00854                 sln, n, v, r, a, WEXITSTATUS(psm->sq.status));
00855         rc = RPMRC_FAIL;
00856     }
00857   }
00858 
00859     if (freePrefixes) prefixes = hfd(prefixes, ipt);
00860 
00861     xx = Fclose(out);   /* XXX dup'd STDOUT_FILENO */
00862     
00863     /*@-branchstate@*/
00864     if (script) {
00865         if (!rpmIsDebug())
00866             xx = unlink(fn);
00867         fn = _free(fn);
00868     }
00869     /*@=branchstate@*/
00870 
00871     return rc;
00872 }
00873 
00879 static rpmRC runInstScript(rpmpsm psm)
00880         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00881         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
00882 {
00883     rpmfi fi = psm->fi;
00884     HGE_t hge = fi->hge;
00885     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00886     void ** progArgv;
00887     int progArgc;
00888     const char ** argv;
00889     rpmTagType ptt, stt;
00890     const char * script;
00891     rpmRC rc = RPMRC_OK;
00892     int xx;
00893 
00894     /*
00895      * headerGetEntry() sets the data pointer to NULL if the entry does
00896      * not exist.
00897      */
00898     xx = hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
00899     xx = hge(fi->h, psm->progTag, &ptt, (void **) &progArgv, &progArgc);
00900     if (progArgv == NULL && script == NULL)
00901         goto exit;
00902 
00903     /*@-branchstate@*/
00904     if (progArgv && ptt == RPM_STRING_TYPE) {
00905         argv = alloca(sizeof(*argv));
00906         *argv = (const char *) progArgv;
00907     } else {
00908         argv = (const char **) progArgv;
00909     }
00910     /*@=branchstate@*/
00911 
00912     if (fi->h != NULL)  /* XXX can't happen */
00913     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), progArgc, argv,
00914                 script, psm->scriptArg, -1);
00915 
00916 exit:
00917     progArgv = hfd(progArgv, ptt);
00918     script = hfd(script, stt);
00919     return rc;
00920 }
00921 
00932 static rpmRC handleOneTrigger(const rpmpsm psm,
00933                         Header sourceH, Header triggeredH,
00934                         int arg2, unsigned char * triggersAlreadyRun)
00935         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState@*/
00936         /*@modifies psm, sourceH, triggeredH, *triggersAlreadyRun,
00937                 rpmGlobalMacroContext, fileSystem, internalState @*/
00938 {
00939     int scareMem = 1;
00940     const rpmts ts = psm->ts;
00941     rpmfi fi = psm->fi;
00942     HGE_t hge = fi->hge;
00943     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00944     rpmds trigger = NULL;
00945     const char ** triggerScripts;
00946     const char ** triggerProgs;
00947     int_32 * triggerIndices;
00948     const char * sourceName;
00949     const char * triggerName;
00950     rpmRC rc = RPMRC_OK;
00951     int xx;
00952     int i;
00953 
00954     xx = headerNVR(sourceH, &sourceName, NULL, NULL);
00955     xx = headerNVR(triggeredH, &triggerName, NULL, NULL);
00956 
00957     trigger = rpmdsInit(rpmdsNew(triggeredH, RPMTAG_TRIGGERNAME, scareMem));
00958     if (trigger == NULL)
00959         return rc;
00960 
00961     (void) rpmdsSetNoPromote(trigger, 1);
00962 
00963     while ((i = rpmdsNext(trigger)) >= 0) {
00964         rpmTagType tit, tst, tpt;
00965         const char * Name;
00966         int_32 Flags = rpmdsFlags(trigger);
00967 
00968         if ((Name = rpmdsN(trigger)) == NULL)
00969             continue;   /* XXX can't happen */
00970 
00971         if (strcmp(Name, sourceName))
00972             continue;
00973         if (!(Flags & psm->sense))
00974             continue;
00975 
00976         /*
00977          * XXX Trigger on any provided dependency, not just the package NEVR.
00978          */
00979         if (!rpmdsAnyMatchesDep(sourceH, trigger, 1))
00980             continue;
00981 
00982         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
00983                        (void **) &triggerIndices, NULL) &&
00984                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
00985                        (void **) &triggerScripts, NULL) &&
00986                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
00987                        (void **) &triggerProgs, NULL))
00988             )
00989             continue;
00990 
00991         {   int arg1;
00992             int index;
00993 
00994             arg1 = rpmdbCountPackages(rpmtsGetRdb(ts), triggerName);
00995             if (arg1 < 0) {
00996                 /* XXX W2DO? fails as "execution of script failed" */
00997                 rc = RPMRC_FAIL;
00998             } else {
00999                 arg1 += psm->countCorrection;
01000                 index = triggerIndices[i];
01001                 if (triggersAlreadyRun == NULL ||
01002                     triggersAlreadyRun[index] == 0)
01003                 {
01004                     rc = runScript(psm, triggeredH, "%trigger", 1,
01005                             triggerProgs + index, triggerScripts[index], 
01006                             arg1, arg2);
01007                     if (triggersAlreadyRun != NULL)
01008                         triggersAlreadyRun[index] = 1;
01009                 }
01010             }
01011         }
01012 
01013         triggerIndices = hfd(triggerIndices, tit);
01014         triggerScripts = hfd(triggerScripts, tst);
01015         triggerProgs = hfd(triggerProgs, tpt);
01016 
01017         /*
01018          * Each target/source header pair can only result in a single
01019          * script being run.
01020          */
01021         break;
01022     }
01023 
01024     trigger = rpmdsFree(trigger);
01025 
01026     return rc;
01027 }
01028 
01034 static rpmRC runTriggers(rpmpsm psm)
01035         /*@globals rpmGlobalMacroContext, h_errno,
01036                 fileSystem, internalState @*/
01037         /*@modifies psm, rpmGlobalMacroContext,
01038                 fileSystem, internalState @*/
01039 {
01040     const rpmts ts = psm->ts;
01041     rpmfi fi = psm->fi;
01042     int numPackage = -1;
01043     rpmRC rc = RPMRC_OK;
01044     const char * N = NULL;
01045 
01046     if (psm->te)        /* XXX can't happen */
01047         N = rpmteN(psm->te);
01048     if (N)              /* XXX can't happen */
01049         numPackage = rpmdbCountPackages(rpmtsGetRdb(ts), N)
01050                                 + psm->countCorrection;
01051     if (numPackage < 0)
01052         return RPMRC_NOTFOUND;
01053 
01054     if (fi != NULL && fi->h != NULL)    /* XXX can't happen */
01055     {   Header triggeredH;
01056         rpmdbMatchIterator mi;
01057         int countCorrection = psm->countCorrection;
01058 
01059         psm->countCorrection = 0;
01060         mi = rpmtsInitIterator(ts, RPMTAG_TRIGGERNAME, N, 0);
01061         while((triggeredH = rpmdbNextIterator(mi)) != NULL)
01062             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01063         mi = rpmdbFreeIterator(mi);
01064         psm->countCorrection = countCorrection;
01065     }
01066 
01067     return rc;
01068 }
01069 
01075 static rpmRC runImmedTriggers(rpmpsm psm)
01076         /*@globals rpmGlobalMacroContext, h_errno,
01077                 fileSystem, internalState @*/
01078         /*@modifies psm, rpmGlobalMacroContext,
01079                 fileSystem, internalState @*/
01080 {
01081     const rpmts ts = psm->ts;
01082     rpmfi fi = psm->fi;
01083     HGE_t hge = fi->hge;
01084     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01085     const char ** triggerNames;
01086     int numTriggers;
01087     int_32 * triggerIndices;
01088     rpmTagType tnt, tit;
01089     int numTriggerIndices;
01090     unsigned char * triggersRun;
01091     rpmRC rc = RPMRC_OK;
01092 
01093     if (fi->h == NULL)  return rc;      /* XXX can't happen */
01094 
01095     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01096                         (void **) &triggerNames, &numTriggers) &&
01097                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01098                         (void **) &triggerIndices, &numTriggerIndices))
01099         )
01100         return rc;
01101 
01102     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01103     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01104 
01105     {   Header sourceH = NULL;
01106         int i;
01107 
01108         for (i = 0; i < numTriggers; i++) {
01109             rpmdbMatchIterator mi;
01110 
01111             if (triggersRun[triggerIndices[i]] != 0) continue;
01112         
01113             mi = rpmtsInitIterator(ts, RPMTAG_NAME, triggerNames[i], 0);
01114 
01115             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01116                 rc |= handleOneTrigger(psm, sourceH, fi->h, 
01117                                 rpmdbGetIteratorCount(mi),
01118                                 triggersRun);
01119             }
01120 
01121             mi = rpmdbFreeIterator(mi);
01122         }
01123     }
01124     triggerIndices = hfd(triggerIndices, tit);
01125     triggerNames = hfd(triggerNames, tnt);
01126     return rc;
01127 }
01128 
01129 /*@observer@*/ static const char *const pkgStageString(pkgStage a)
01130         /*@*/
01131 {
01132     switch(a) {
01133     case PSM_UNKNOWN:           return "unknown";
01134 
01135     case PSM_PKGINSTALL:        return "  install";
01136     case PSM_PKGERASE:          return "    erase";
01137     case PSM_PKGCOMMIT:         return "   commit";
01138     case PSM_PKGSAVE:           return "repackage";
01139 
01140     case PSM_INIT:              return "init";
01141     case PSM_PRE:               return "pre";
01142     case PSM_PROCESS:           return "process";
01143     case PSM_POST:              return "post";
01144     case PSM_UNDO:              return "undo";
01145     case PSM_FINI:              return "fini";
01146 
01147     case PSM_CREATE:            return "create";
01148     case PSM_NOTIFY:            return "notify";
01149     case PSM_DESTROY:           return "destroy";
01150     case PSM_COMMIT:            return "commit";
01151 
01152     case PSM_CHROOT_IN:         return "chrootin";
01153     case PSM_CHROOT_OUT:        return "chrootout";
01154     case PSM_SCRIPT:            return "script";
01155     case PSM_TRIGGERS:          return "triggers";
01156     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01157 
01158     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01159 
01160     case PSM_RPMDB_LOAD:        return "rpmdbload";
01161     case PSM_RPMDB_ADD:         return "rpmdbadd";
01162     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01163 
01164     default:                    return "???";
01165     }
01166     /*@noteached@*/
01167 }
01168 
01169 rpmpsm XrpmpsmUnlink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01170 {
01171     if (psm == NULL) return NULL;
01172 /*@-modfilesys@*/
01173 if (_psm_debug && msg != NULL)
01174 fprintf(stderr, "--> psm %p -- %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01175 /*@=modfilesys@*/
01176     psm->nrefs--;
01177     return NULL;
01178 }
01179 
01180 rpmpsm XrpmpsmLink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01181 {
01182     if (psm == NULL) return NULL;
01183     psm->nrefs++;
01184 
01185 /*@-modfilesys@*/
01186 if (_psm_debug && msg != NULL)
01187 fprintf(stderr, "--> psm %p ++ %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01188 /*@=modfilesys@*/
01189 
01190     /*@-refcounttrans@*/ return psm; /*@=refcounttrans@*/
01191 }
01192 
01193 rpmpsm rpmpsmFree(rpmpsm psm)
01194 {
01195     const char * msg = "rpmpsmFree";
01196     if (psm == NULL)
01197         return NULL;
01198 
01199     if (psm->nrefs > 1)
01200         return rpmpsmUnlink(psm, msg);
01201 
01202 /*@-nullstate@*/
01203     psm->fi = rpmfiFree(psm->fi);
01204 #ifdef  NOTYET
01205     psm->te = rpmteFree(psm->te);
01206 #else
01207     psm->te = NULL;
01208 #endif
01209 /*@-internalglobs@*/
01210     psm->ts = rpmtsFree(psm->ts);
01211 /*@=internalglobs@*/
01212 
01213     (void) rpmpsmUnlink(psm, msg);
01214 
01215     /*@-refcounttrans -usereleased@*/
01216 /*@-boundswrite@*/
01217     memset(psm, 0, sizeof(*psm));               /* XXX trash and burn */
01218 /*@=boundswrite@*/
01219     psm = _free(psm);
01220     /*@=refcounttrans =usereleased@*/
01221 
01222     return NULL;
01223 /*@=nullstate@*/
01224 }
01225 
01226 rpmpsm rpmpsmNew(rpmts ts, rpmte te, rpmfi fi)
01227 {
01228     const char * msg = "rpmpsmNew";
01229     rpmpsm psm = xcalloc(1, sizeof(*psm));
01230 
01231     if (ts)     psm->ts = rpmtsLink(ts, msg);
01232 #ifdef  NOTYET
01233     if (te)     psm->te = rpmteLink(te, msg);
01234 #else
01235 /*@-assignexpose -temptrans @*/
01236     if (te)     psm->te = te;
01237 /*@=assignexpose =temptrans @*/
01238 #endif
01239     if (fi)     psm->fi = rpmfiLink(fi, msg);
01240 
01241     return rpmpsmLink(psm, msg);
01242 }
01243 
01244 static void * rpmpsmThread(void * arg)
01245         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01246         /*@modifies arg, rpmGlobalMacroContext, fileSystem, internalState @*/
01247 {
01248     rpmpsm psm = arg;
01249 /*@-unqualifiedtrans@*/
01250     return ((void *) rpmpsmStage(psm, psm->nstage));
01251 /*@=unqualifiedtrans@*/
01252 }
01253 
01254 static int rpmpsmNext(rpmpsm psm, pkgStage nstage)
01255         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01256         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
01257 {
01258     psm->nstage = nstage;
01259     if (_psm_threads)
01260         return rpmsqJoin( rpmsqThread(rpmpsmThread, psm) );
01261     return rpmpsmStage(psm, psm->nstage);
01262 }
01263 
01268 /*@-bounds -nullpass@*/ /* FIX: testing null annotation for fi->h */
01269 rpmRC rpmpsmStage(rpmpsm psm, pkgStage stage)
01270 {
01271     const rpmts ts = psm->ts;
01272     uint_32 tscolor = rpmtsColor(ts);
01273     rpmfi fi = psm->fi;
01274     HGE_t hge = fi->hge;
01275     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01276     rpmRC rc = psm->rc;
01277     int saveerrno;
01278     int xx;
01279 
01280     /*@-branchstate@*/
01281     switch (stage) {
01282     case PSM_UNKNOWN:
01283         break;
01284     case PSM_INIT:
01285         rpmMessage(RPMMESS_DEBUG, _("%s: %s has %d files, test = %d\n"),
01286                 psm->stepName, rpmteNEVR(psm->te),
01287                 rpmfiFC(fi), (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST));
01288 
01289         /*
01290          * When we run scripts, we pass an argument which is the number of 
01291          * versions of this package that will be installed when we are
01292          * finished.
01293          */
01294         psm->npkgs_installed = rpmdbCountPackages(rpmtsGetRdb(ts), rpmteN(psm->te));
01295         if (psm->npkgs_installed < 0) {
01296             rc = RPMRC_FAIL;
01297             break;
01298         }
01299 
01300         if (psm->goal == PSM_PKGINSTALL) {
01301             int fc = rpmfiFC(fi);
01302 
01303             psm->scriptArg = psm->npkgs_installed + 1;
01304 
01305 assert(psm->mi == NULL);
01306             psm->mi = rpmtsInitIterator(ts, RPMTAG_NAME, rpmteN(psm->te), 0);
01307             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_EPOCH, RPMMIRE_STRCMP,
01308                         rpmteE(psm->te));
01309             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION, RPMMIRE_STRCMP,
01310                         rpmteV(psm->te));
01311             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE, RPMMIRE_STRCMP,
01312                         rpmteR(psm->te));
01313             if (tscolor) {
01314                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_ARCH, RPMMIRE_STRCMP,
01315                         rpmteA(psm->te));
01316                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_OS, RPMMIRE_STRCMP,
01317                         rpmteO(psm->te));
01318             }
01319 
01320             while ((psm->oh = rpmdbNextIterator(psm->mi)) != NULL) {
01321                 fi->record = rpmdbGetIteratorOffset(psm->mi);
01322                 psm->oh = NULL;
01323                 /*@loopbreak@*/ break;
01324             }
01325             psm->mi = rpmdbFreeIterator(psm->mi);
01326             rc = RPMRC_OK;
01327 
01328             /* XXX lazy alloc here may need to be done elsewhere. */
01329             if (fi->fstates == NULL && fc > 0) {
01330                 fi->fstates = xmalloc(sizeof(*fi->fstates) * fc);
01331                 memset(fi->fstates, RPMFILE_STATE_NORMAL, fc);
01332             }
01333 
01334             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01335             if (fc <= 0)                                break;
01336         
01337             /*
01338              * Old format relocatable packages need the entire default
01339              * prefix stripped to form the cpio list, while all other packages
01340              * need the leading / stripped.
01341              */
01342             {   const char * p;
01343                 xx = hge(fi->h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &p, NULL);
01344                 fi->striplen = (xx ? strlen(p) + 1 : 1); 
01345             }
01346             fi->mapflags =
01347                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01348         
01349             if (headerIsEntry(fi->h, RPMTAG_ORIGBASENAMES))
01350                 rpmfiBuildFNames(fi->h, RPMTAG_ORIGBASENAMES, &fi->apath, NULL);
01351             else
01352                 rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
01353         
01354             if (fi->fuser == NULL)
01355                 xx = hge(fi->h, RPMTAG_FILEUSERNAME, NULL,
01356                                 (void **) &fi->fuser, NULL);
01357             if (fi->fgroup == NULL)
01358                 xx = hge(fi->h, RPMTAG_FILEGROUPNAME, NULL,
01359                                 (void **) &fi->fgroup, NULL);
01360             rc = RPMRC_OK;
01361         }
01362         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01363             psm->scriptArg = psm->npkgs_installed - 1;
01364         
01365             /* Retrieve installed header. */
01366             rc = rpmpsmNext(psm, PSM_RPMDB_LOAD);
01367 if (rc == RPMRC_OK)
01368 if (psm->te)
01369 psm->te->h = headerLink(fi->h);
01370         }
01371         if (psm->goal == PSM_PKGSAVE) {
01372             /* Open output package for writing. */
01373             {   const char * bfmt = rpmGetPath("%{_repackage_name_fmt}", NULL);
01374                 const char * pkgbn =
01375                         headerSprintf(fi->h, bfmt, rpmTagTable, rpmHeaderFormats, NULL);
01376 
01377                 bfmt = _free(bfmt);
01378                 psm->pkgURL = rpmGenPath("%{?_repackage_root}",
01379                                          "%{?_repackage_dir}",
01380                                         pkgbn);
01381                 pkgbn = _free(pkgbn);
01382                 (void) urlPath(psm->pkgURL, &psm->pkgfn);
01383                 psm->fd = Fopen(psm->pkgfn, "w.ufdio");
01384                 if (psm->fd == NULL || Ferror(psm->fd)) {
01385                     rc = RPMRC_FAIL;
01386                     break;
01387                 }
01388             }
01389         }
01390         break;
01391     case PSM_PRE:
01392         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01393 
01394 /* XXX insure that trigger index is opened before entering chroot. */
01395 #ifdef  NOTYET
01396  { static int oneshot = 0;
01397    dbiIndex dbi;
01398    if (!oneshot) {
01399      dbi = dbiOpen(rpmtsGetRdb(ts), RPMTAG_TRIGGERNAME, 0);
01400      oneshot++;
01401    }
01402  }
01403 #endif
01404 
01405         /* Change root directory if requested and not already done. */
01406         rc = rpmpsmNext(psm, PSM_CHROOT_IN);
01407 
01408         if (psm->goal == PSM_PKGINSTALL) {
01409             psm->scriptTag = RPMTAG_PREIN;
01410             psm->progTag = RPMTAG_PREINPROG;
01411 
01412             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPREIN)) {
01413                 /* XXX FIXME: implement %triggerprein. */
01414             }
01415 
01416             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPRE)) {
01417                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01418                 if (rc != RPMRC_OK) {
01419                     rpmError(RPMERR_SCRIPT,
01420                         _("%s: %s scriptlet failed (%d), skipping %s\n"),
01421                         psm->stepName, tag2sln(psm->scriptTag), rc,
01422                         rpmteNEVR(psm->te));
01423                     break;
01424                 }
01425             }
01426         }
01427 
01428         if (psm->goal == PSM_PKGERASE) {
01429             psm->scriptTag = RPMTAG_PREUN;
01430             psm->progTag = RPMTAG_PREUNPROG;
01431             psm->sense = RPMSENSE_TRIGGERUN;
01432             psm->countCorrection = -1;
01433 
01434             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERUN)) {
01435                 /* Run triggers in this package other package(s) set off. */
01436                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01437                 if (rc) break;
01438 
01439                 /* Run triggers in other package(s) this package sets off. */
01440                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01441                 if (rc) break;
01442             }
01443 
01444             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPREUN))
01445                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01446         }
01447         if (psm->goal == PSM_PKGSAVE) {
01448             int noArchiveSize = 0;
01449 
01450             /* Regenerate original header. */
01451             {   void * uh = NULL;
01452                 int_32 uht, uhc;
01453 
01454                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)) {
01455                     psm->oh = headerCopyLoad(uh);
01456                     uh = hfd(uh, uht);
01457                 } else
01458                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMAGE, &uht, &uh, &uhc))
01459                 {
01460                     HeaderIterator hi;
01461                     int_32 tag, type, count;
01462                     hPTR_t ptr;
01463                     Header oh;
01464 
01465                     /* Load the original header from the blob. */
01466                     oh = headerCopyLoad(uh);
01467 
01468                     /* XXX this is headerCopy w/o headerReload() */
01469                     psm->oh = headerNew();
01470 
01471                     /*@-branchstate@*/
01472                     for (hi = headerInitIterator(oh);
01473                         headerNextIterator(hi, &tag, &type, &ptr, &count);
01474                         ptr = headerFreeData((void *)ptr, type))
01475                     {
01476                         if (tag == RPMTAG_ARCHIVESIZE)
01477                             noArchiveSize = 1;
01478                         if (ptr) (void) headerAddEntry(psm->oh, tag, type, ptr, count);
01479                     }
01480                     hi = headerFreeIterator(hi);
01481                     /*@=branchstate@*/
01482 
01483                     oh = headerFree(oh);
01484                     uh = hfd(uh, uht);
01485                 } else
01486                     break;      /* XXX shouldn't ever happen */
01487             }
01488 
01489             /* Retrieve type of payload compression. */
01490             /*@-nullstate@*/    /* FIX: psm->oh may be NULL */
01491             rc = rpmpsmNext(psm, PSM_RPMIO_FLAGS);
01492             /*@=nullstate@*/
01493 
01494             /* Write the lead section into the package. */
01495             {   int archnum = -1;
01496                 int osnum = -1;
01497                 struct rpmlead lead;
01498 
01499 #ifndef DYING
01500                 rpmGetArchInfo(NULL, &archnum);
01501                 rpmGetOsInfo(NULL, &osnum);
01502 #endif
01503 
01504                 memset(&lead, 0, sizeof(lead));
01505                 /* XXX Set package version conditioned on noDirTokens. */
01506                 lead.major = 3;
01507                 lead.minor = 0;
01508                 lead.type = RPMLEAD_BINARY;
01509                 lead.archnum = archnum;
01510                 lead.osnum = osnum;
01511                 lead.signature_type = RPMSIGTYPE_HEADERSIG;
01512 
01513                 strncpy(lead.name, rpmteNEVR(psm->te), sizeof(lead.name));
01514 
01515                 rc = writeLead(psm->fd, &lead);
01516                 if (rc != RPMRC_OK) {
01517                     rpmError(RPMERR_NOSPACE, _("Unable to write package: %s\n"),
01518                          Fstrerror(psm->fd));
01519                     break;
01520                 }
01521             }
01522 
01523             /* Write the signature section into the package. */
01524             /* XXX rpm-4.1 and later has archive size in signature header. */
01525             {   Header sigh = headerRegenSigHeader(fi->h, noArchiveSize);
01526                 /* Reallocate the signature into one contiguous region. */
01527                 sigh = headerReload(sigh, RPMTAG_HEADERSIGNATURES);
01528                 if (sigh == NULL) {
01529                     rpmError(RPMERR_NOSPACE, _("Unable to reload signature header\n"));
01530                     rc = RPMRC_FAIL;
01531                     break;
01532                 }
01533                 rc = rpmWriteSignature(psm->fd, sigh);
01534                 sigh = rpmFreeSignature(sigh);
01535                 if (rc) break;
01536             }
01537 
01538             /* Add remove transaction id to header. */
01539             if (psm->oh != NULL)
01540             {   int_32 tid = rpmtsGetTid(ts);
01541                 xx = headerAddEntry(psm->oh, RPMTAG_REMOVETID,
01542                         RPM_INT32_TYPE, &tid, 1);
01543             }
01544 
01545             /* Write the metadata section into the package. */
01546             rc = headerWrite(psm->fd, psm->oh, HEADER_MAGIC_YES);
01547             if (rc) break;
01548         }
01549         break;
01550     case PSM_PROCESS:
01551         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01552 
01553         if (psm->goal == PSM_PKGINSTALL) {
01554 
01555             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01556 
01557             /* XXX Synthesize callbacks for packages with no files. */
01558             if (rpmfiFC(fi) <= 0) {
01559                 void * ptr;
01560                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_START, 0, 100);
01561                 ptr = rpmtsNotify(ts, fi->te, RPMCALLBACK_INST_PROGRESS, 100, 100);
01562                 break;
01563             }
01564 
01565             /* Retrieve type of payload compression. */
01566             rc = rpmpsmNext(psm, PSM_RPMIO_FLAGS);
01567 
01568             if (rpmteFd(fi->te) == NULL) {      /* XXX can't happen */
01569                 rc = RPMRC_FAIL;
01570                 break;
01571             }
01572 
01573             /*@-nullpass@*/     /* LCL: fi->fd != NULL here. */
01574             psm->cfd = Fdopen(fdDup(Fileno(rpmteFd(fi->te))), psm->rpmio_flags);
01575             /*@=nullpass@*/
01576             if (psm->cfd == NULL) {     /* XXX can't happen */
01577                 rc = RPMRC_FAIL;
01578                 break;
01579             }
01580 
01581             rc = fsmSetup(fi->fsm, FSM_PKGINSTALL, ts, fi,
01582                         psm->cfd, NULL, &psm->failedFile);
01583             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_UNCOMPRESS),
01584                         fdstat_op(psm->cfd, FDSTAT_READ));
01585             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST),
01586                         fdstat_op(psm->cfd, FDSTAT_DIGEST));
01587             xx = fsmTeardown(fi->fsm);
01588 
01589             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01590             xx = Fclose(psm->cfd);
01591             psm->cfd = NULL;
01592             /*@-mods@*/
01593             errno = saveerrno; /* XXX FIXME: Fclose with libio destroys errno */
01594             /*@=mods@*/
01595 
01596             if (!rc)
01597                 rc = rpmpsmNext(psm, PSM_COMMIT);
01598 
01599             /* XXX make sure progress is closed out */
01600             psm->what = RPMCALLBACK_INST_PROGRESS;
01601             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01602             psm->total = psm->amount;
01603             xx = rpmpsmNext(psm, PSM_NOTIFY);
01604 
01605             if (rc) {
01606                 rpmError(RPMERR_CPIO,
01607                         _("unpacking of archive failed%s%s: %s\n"),
01608                         (psm->failedFile != NULL ? _(" on file ") : ""),
01609                         (psm->failedFile != NULL ? psm->failedFile : ""),
01610                         cpioStrerror(rc));
01611                 rc = RPMRC_FAIL;
01612 
01613                 /* XXX notify callback on error. */
01614                 psm->what = RPMCALLBACK_UNPACK_ERROR;
01615                 psm->amount = 0;
01616                 psm->total = 0;
01617                 xx = rpmpsmNext(psm, PSM_NOTIFY);
01618 
01619                 break;
01620             }
01621         }
01622         if (psm->goal == PSM_PKGERASE) {
01623             int fc = rpmfiFC(fi);
01624 
01625             if (rpmtsFlags(ts) & RPMTRANS_FLAG_JUSTDB)  break;
01626             if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)       break;
01627             if (fc <= 0)                                break;
01628 
01629             psm->what = RPMCALLBACK_UNINST_START;
01630             psm->amount = fc;           /* XXX W2DO? looks wrong. */
01631             psm->total = fc;
01632             xx = rpmpsmNext(psm, PSM_NOTIFY);
01633 
01634             rc = fsmSetup(fi->fsm, FSM_PKGERASE, ts, fi,
01635                         NULL, NULL, &psm->failedFile);
01636             xx = fsmTeardown(fi->fsm);
01637 
01638             psm->what = RPMCALLBACK_UNINST_STOP;
01639             psm->amount = 0;            /* XXX W2DO? looks wrong. */
01640             psm->total = fc;
01641             xx = rpmpsmNext(psm, PSM_NOTIFY);
01642 
01643         }
01644         if (psm->goal == PSM_PKGSAVE) {
01645             fileAction * actions = fi->actions;
01646             fileAction action = fi->action;
01647 
01648             fi->action = FA_COPYOUT;
01649             fi->actions = NULL;
01650 
01651             if (psm->fd == NULL) {      /* XXX can't happen */
01652                 rc = RPMRC_FAIL;
01653                 break;
01654             }
01655             /*@-nullpass@*/     /* FIX: fdDup mey return NULL. */
01656             xx = Fflush(psm->fd);
01657             psm->cfd = Fdopen(fdDup(Fileno(psm->fd)), psm->rpmio_flags);
01658             /*@=nullpass@*/
01659             if (psm->cfd == NULL) {     /* XXX can't happen */
01660                 rc = RPMRC_FAIL;
01661                 break;
01662             }
01663 
01664             rc = fsmSetup(fi->fsm, FSM_PKGBUILD, ts, fi, psm->cfd,
01665                         NULL, &psm->failedFile);
01666             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_COMPRESS),
01667                         fdstat_op(psm->cfd, FDSTAT_WRITE));
01668             (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST),
01669                         fdstat_op(psm->cfd, FDSTAT_DIGEST));
01670             xx = fsmTeardown(fi->fsm);
01671 
01672             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01673             xx = Fclose(psm->cfd);
01674             psm->cfd = NULL;
01675             /*@-mods@*/
01676             errno = saveerrno;
01677             /*@=mods@*/
01678 
01679             /* XXX make sure progress is closed out */
01680             psm->what = RPMCALLBACK_INST_PROGRESS;
01681             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01682             psm->total = psm->amount;
01683             xx = rpmpsmNext(psm, PSM_NOTIFY);
01684 
01685             fi->action = action;
01686             fi->actions = actions;
01687         }
01688         break;
01689     case PSM_POST:
01690         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01691 
01692         if (psm->goal == PSM_PKGINSTALL) {
01693             int_32 installTime = (int_32) time(NULL);
01694             int fc = rpmfiFC(fi);
01695 
01696             if (fi->h == NULL) break;   /* XXX can't happen */
01697             if (fi->fstates != NULL && fc > 0)
01698                 xx = headerAddEntry(fi->h, RPMTAG_FILESTATES, RPM_CHAR_TYPE,
01699                                 fi->fstates, fc);
01700 
01701             xx = headerAddEntry(fi->h, RPMTAG_INSTALLTIME, RPM_INT32_TYPE,
01702                                 &installTime, 1);
01703 
01704             xx = headerAddEntry(fi->h, RPMTAG_INSTALLCOLOR, RPM_INT32_TYPE,
01705                                 &tscolor, 1);
01706 
01707             /*
01708              * If this package has already been installed, remove it from
01709              * the database before adding the new one.
01710              */
01711             if (fi->record && !(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY)) {
01712                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01713                 if (rc) break;
01714             }
01715 
01716             rc = rpmpsmNext(psm, PSM_RPMDB_ADD);
01717             if (rc) break;
01718 
01719             psm->scriptTag = RPMTAG_POSTIN;
01720             psm->progTag = RPMTAG_POSTINPROG;
01721             psm->sense = RPMSENSE_TRIGGERIN;
01722             psm->countCorrection = 0;
01723 
01724             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOST)) {
01725                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01726                 if (rc) break;
01727             }
01728             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERIN)) {
01729                 /* Run triggers in other package(s) this package sets off. */
01730                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01731                 if (rc) break;
01732 
01733                 /* Run triggers in this package other package(s) set off. */
01734                 rc = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01735                 if (rc) break;
01736             }
01737 
01738             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01739                 rc = markReplacedFiles(psm);
01740 
01741         }
01742         if (psm->goal == PSM_PKGERASE) {
01743 
01744             psm->scriptTag = RPMTAG_POSTUN;
01745             psm->progTag = RPMTAG_POSTUNPROG;
01746             psm->sense = RPMSENSE_TRIGGERPOSTUN;
01747             psm->countCorrection = -1;
01748 
01749             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPOSTUN)) {
01750                 rc = rpmpsmNext(psm, PSM_SCRIPT);
01751                 /* XXX WTFO? postun failures don't cause erasure failure. */
01752             }
01753 
01754             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
01755                 /* Run triggers in other package(s) this package sets off. */
01756                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01757                 if (rc) break;
01758             }
01759 
01760             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01761                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01762         }
01763         if (psm->goal == PSM_PKGSAVE) {
01764         }
01765 
01766         /* Restore root directory if changed. */
01767         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01768         break;
01769     case PSM_UNDO:
01770         break;
01771     case PSM_FINI:
01772         /* Restore root directory if changed. */
01773         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01774 
01775         if (psm->fd != NULL) {
01776             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01777             xx = Fclose(psm->fd);
01778             psm->fd = NULL;
01779             /*@-mods@*/
01780             errno = saveerrno;
01781             /*@=mods@*/
01782         }
01783 
01784         if (psm->goal == PSM_PKGSAVE) {
01785             if (!rc && ts && ts->notify == NULL) {
01786                 rpmMessage(RPMMESS_VERBOSE, _("Wrote: %s\n"),
01787                         (psm->pkgURL ? psm->pkgURL : "???"));
01788             }
01789         }
01790 
01791         if (rc) {
01792             if (psm->failedFile)
01793                 rpmError(RPMERR_CPIO,
01794                         _("%s failed on file %s: %s\n"),
01795                         psm->stepName, psm->failedFile, cpioStrerror(rc));
01796             else
01797                 rpmError(RPMERR_CPIO, _("%s failed: %s\n"),
01798                         psm->stepName, cpioStrerror(rc));
01799 
01800             /* XXX notify callback on error. */
01801             psm->what = RPMCALLBACK_CPIO_ERROR;
01802             psm->amount = 0;
01803             psm->total = 0;
01804             /*@-nullstate@*/ /* FIX: psm->fd may be NULL. */
01805             xx = rpmpsmNext(psm, PSM_NOTIFY);
01806             /*@=nullstate@*/
01807         }
01808 
01809 /*@-branchstate@*/
01810         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01811 if (psm->te != NULL)
01812 if (psm->te->h != NULL)
01813 psm->te->h = headerFree(psm->te->h);
01814             if (fi->h != NULL)
01815                 fi->h = headerFree(fi->h);
01816         }
01817 /*@=branchstate@*/
01818         psm->oh = headerFree(psm->oh);
01819         psm->pkgURL = _free(psm->pkgURL);
01820         psm->rpmio_flags = _free(psm->rpmio_flags);
01821         psm->failedFile = _free(psm->failedFile);
01822 
01823         fi->fgroup = hfd(fi->fgroup, -1);
01824         fi->fuser = hfd(fi->fuser, -1);
01825         fi->apath = _free(fi->apath);
01826         fi->fstates = _free(fi->fstates);
01827         break;
01828 
01829     case PSM_PKGINSTALL:
01830     case PSM_PKGERASE:
01831     case PSM_PKGSAVE:
01832         psm->goal = stage;
01833         psm->rc = RPMRC_OK;
01834         psm->stepName = pkgStageString(stage);
01835 
01836         rc = rpmpsmNext(psm, PSM_INIT);
01837         if (!rc) rc = rpmpsmNext(psm, PSM_PRE);
01838         if (!rc) rc = rpmpsmNext(psm, PSM_PROCESS);
01839         if (!rc) rc = rpmpsmNext(psm, PSM_POST);
01840         xx = rpmpsmNext(psm, PSM_FINI);
01841         break;
01842     case PSM_PKGCOMMIT:
01843         break;
01844 
01845     case PSM_CREATE:
01846         break;
01847     case PSM_NOTIFY:
01848     {   void * ptr;
01849 /*@-nullpass@*/ /* FIX: psm->te may be NULL */
01850         ptr = rpmtsNotify(ts, psm->te, psm->what, psm->amount, psm->total);
01851 /*@-nullpass@*/
01852     }   break;
01853     case PSM_DESTROY:
01854         break;
01855     case PSM_COMMIT:
01856         if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_PKGCOMMIT)) break;
01857         if (rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY) break;
01858 
01859         rc = fsmSetup(fi->fsm, FSM_PKGCOMMIT, ts, fi,
01860                         NULL, NULL, &psm->failedFile);
01861         xx = fsmTeardown(fi->fsm);
01862         break;
01863 
01864     case PSM_CHROOT_IN:
01865     {   const char * rootDir = rpmtsRootDir(ts);
01866         /* Change root directory if requested and not already done. */
01867         if (rootDir != NULL && !(rootDir[0] == '/' && rootDir[1] == '\0')
01868          && !rpmtsChrootDone(ts) && !psm->chrootDone)
01869         {
01870             static int _pw_loaded = 0;
01871             static int _gr_loaded = 0;
01872 
01873             if (!_pw_loaded) {
01874                 (void)getpwnam("root");
01875                 endpwent();
01876                 _pw_loaded++;
01877             }
01878             if (!_gr_loaded) {
01879                 (void)getgrnam("root");
01880                 endgrent();
01881                 _gr_loaded++;
01882             }
01883 
01884             xx = chdir("/");
01885             /*@-superuser@*/
01886             rc = chroot(rootDir);
01887             /*@=superuser@*/
01888             psm->chrootDone = 1;
01889             (void) rpmtsSetChrootDone(ts, 1);
01890         }
01891     }   break;
01892     case PSM_CHROOT_OUT:
01893         /* Restore root directory if changed. */
01894         if (psm->chrootDone) {
01895             const char * currDir = rpmtsCurrDir(ts);
01896             /*@-superuser@*/
01897             rc = chroot(".");
01898             /*@=superuser@*/
01899             psm->chrootDone = 0;
01900             (void) rpmtsSetChrootDone(ts, 0);
01901             if (currDir != NULL)        /* XXX can't happen */
01902                 xx = chdir(currDir);
01903         }
01904         break;
01905     case PSM_SCRIPT:    /* Run current package scriptlets. */
01906         rc = runInstScript(psm);
01907         break;
01908     case PSM_TRIGGERS:
01909         /* Run triggers in other package(s) this package sets off. */
01910         rc = runTriggers(psm);
01911         break;
01912     case PSM_IMMED_TRIGGERS:
01913         /* Run triggers in this package other package(s) set off. */
01914         rc = runImmedTriggers(psm);
01915         break;
01916 
01917     case PSM_RPMIO_FLAGS:
01918     {   const char * payload_compressor = NULL;
01919         char * t;
01920 
01921         /*@-branchstate@*/
01922         if (!hge(fi->h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
01923                             (void **) &payload_compressor, NULL))
01924             payload_compressor = "gzip";
01925         /*@=branchstate@*/
01926         psm->rpmio_flags = t = xmalloc(sizeof("w9.gzdio"));
01927         *t = '\0';
01928         t = stpcpy(t, ((psm->goal == PSM_PKGSAVE) ? "w9" : "r"));
01929         if (!strcmp(payload_compressor, "gzip"))
01930             t = stpcpy(t, ".gzdio");
01931         if (!strcmp(payload_compressor, "bzip2"))
01932             t = stpcpy(t, ".bzdio");
01933         rc = RPMRC_OK;
01934     }   break;
01935 
01936     case PSM_RPMDB_LOAD:
01937 assert(psm->mi == NULL);
01938         psm->mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES,
01939                                 &fi->record, sizeof(fi->record));
01940 
01941         fi->h = rpmdbNextIterator(psm->mi);
01942         if (fi->h != NULL)
01943             fi->h = headerLink(fi->h);
01944 
01945         psm->mi = rpmdbFreeIterator(psm->mi);
01946         rc = (fi->h != NULL ? RPMRC_OK : RPMRC_FAIL);
01947         break;
01948     case PSM_RPMDB_ADD:
01949         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01950         if (fi->h == NULL)      break;  /* XXX can't happen */
01951         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
01952         if (!(rpmtsVSFlags(ts) & RPMVSF_NOHDRCHK))
01953             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
01954                                 ts, headerCheck);
01955         else
01956             rc = rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->h,
01957                                 NULL, NULL);
01958         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBADD), 0);
01959         break;
01960     case PSM_RPMDB_REMOVE:
01961         if (rpmtsFlags(ts) & RPMTRANS_FLAG_TEST)        break;
01962         (void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
01963         rc = rpmdbRemove(rpmtsGetRdb(ts), rpmtsGetTid(ts), fi->record,
01964                                 NULL, NULL);
01965         (void) rpmswExit(rpmtsOp(ts, RPMTS_OP_DBREMOVE), 0);
01966         break;
01967 
01968     default:
01969         break;
01970 /*@i@*/    }
01971     /*@=branchstate@*/
01972 
01973     /*@-nullstate@*/    /* FIX: psm->oh and psm->fi->h may be NULL. */
01974     return rc;
01975     /*@=nullstate@*/
01976 }
01977 /*@=bounds =nullpass@*/

Generated on Sun Apr 8 21:29:36 2012 for rpm by  doxygen 1.3.9.1