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

lib/psm.c

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

Generated on Tue Jun 12 18:41:33 2007 for rpm by doxygen 1.3.5