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

Generated on Fri Jul 28 03:09:44 2006 for rpm by doxygen 1.3.5