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

psm.c

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

Generated on Tue Sep 7 17:14:28 2010 for rpm by  doxygen 1.3.9.1