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

Generated on Fri Jul 15 10:22:07 2005 for rpm by doxygen 1.3.5