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

Generated on Tue Dec 28 15:17:14 2004 for rpm by doxygen 1.3.6