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 /*@-boundsread@*/
00061         if (*epochOne < *epochTwo)
00062             return -1;
00063         else if (*epochOne > *epochTwo)
00064             return 1;
00065 /*@=boundsread@*/
00066     }
00067 
00068     rc = headerGetEntry(first, RPMTAG_VERSION, NULL, (void **) &one, NULL);
00069     rc = headerGetEntry(second, RPMTAG_VERSION, NULL, (void **) &two, NULL);
00070 
00071     rc = rpmvercmp(one, two);
00072     if (rc)
00073         return rc;
00074 
00075     rc = headerGetEntry(first, RPMTAG_RELEASE, NULL, (void **) &one, NULL);
00076     rc = headerGetEntry(second, RPMTAG_RELEASE, NULL, (void **) &two, NULL);
00077 
00078     return rpmvercmp(one, two);
00079 }
00080 
00085 /*@observer@*/ /*@unchecked@*/
00086 static struct tagMacro {
00087 /*@observer@*/ /*@null@*/ const char *  macroname; 
00088     rpmTag      tag;            
00089 } tagMacros[] = {
00090     { "name",           RPMTAG_NAME },
00091     { "version",        RPMTAG_VERSION },
00092     { "release",        RPMTAG_RELEASE },
00093     { "epoch",          RPMTAG_EPOCH },
00094     { NULL, 0 }
00095 };
00096 
00103 static int rpmInstallLoadMacros(rpmfi fi, Header h)
00104         /*@globals rpmGlobalMacroContext @*/
00105         /*@modifies rpmGlobalMacroContext @*/
00106 {
00107     HGE_t hge = (HGE_t) fi->hge;
00108     struct tagMacro * tagm;
00109     union {
00110 /*@unused@*/ void * ptr;
00111 /*@unused@*/ const char ** argv;
00112         const char * str;
00113         int_32 * i32p;
00114     } body;
00115     char numbuf[32];
00116     rpmTagType type;
00117 
00118     for (tagm = tagMacros; tagm->macroname != NULL; tagm++) {
00119         if (!hge(h, tagm->tag, &type, (void **) &body, NULL))
00120             continue;
00121         switch (type) {
00122         case RPM_INT32_TYPE:
00123 /*@-boundsread@*/
00124             sprintf(numbuf, "%d", *body.i32p);
00125 /*@=boundsread@*/
00126             addMacro(NULL, tagm->macroname, NULL, numbuf, -1);
00127             /*@switchbreak@*/ break;
00128         case RPM_STRING_TYPE:
00129             addMacro(NULL, tagm->macroname, NULL, body.str, -1);
00130             /*@switchbreak@*/ break;
00131         case RPM_NULL_TYPE:
00132         case RPM_CHAR_TYPE:
00133         case RPM_INT8_TYPE:
00134         case RPM_INT16_TYPE:
00135         case RPM_BIN_TYPE:
00136         case RPM_STRING_ARRAY_TYPE:
00137         case RPM_I18NSTRING_TYPE:
00138         default:
00139             /*@switchbreak@*/ break;
00140         }
00141     }
00142     return 0;
00143 }
00144 
00150 /*@-bounds@*/
00151 static rpmRC markReplacedFiles(const rpmpsm psm)
00152         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00153         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
00154 {
00155     const rpmts ts = psm->ts;
00156     rpmfi fi = psm->fi;
00157     HGE_t hge = (HGE_t)fi->hge;
00158     sharedFileInfo replaced = fi->replaced;
00159     sharedFileInfo sfi;
00160     rpmdbMatchIterator mi;
00161     Header h;
00162     unsigned int * offsets;
00163     unsigned int prev;
00164     int num, xx;
00165 
00166     if (!(rpmfiFC(fi) > 0 && fi->replaced))
00167         return RPMRC_OK;
00168 
00169     num = prev = 0;
00170     for (sfi = replaced; sfi->otherPkg; sfi++) {
00171         if (prev && prev == sfi->otherPkg)
00172             continue;
00173         prev = sfi->otherPkg;
00174         num++;
00175     }
00176     if (num == 0)
00177         return RPMRC_OK;
00178 
00179     offsets = alloca(num * sizeof(*offsets));
00180     offsets[0] = 0;
00181     num = prev = 0;
00182     for (sfi = replaced; sfi->otherPkg; sfi++) {
00183         if (prev && prev == sfi->otherPkg)
00184             continue;
00185         prev = sfi->otherPkg;
00186         offsets[num++] = sfi->otherPkg;
00187     }
00188 
00189     mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
00190     xx = rpmdbAppendIterator(mi, offsets, num);
00191     xx = rpmdbSetIteratorRewrite(mi, 1);
00192 
00193     sfi = replaced;
00194     while ((h = rpmdbNextIterator(mi)) != NULL) {
00195         char * secStates;
00196         int modified;
00197         int count;
00198 
00199         modified = 0;
00200 
00201         if (!hge(h, RPMTAG_FILESTATES, NULL, (void **)&secStates, &count))
00202             continue;
00203         
00204         prev = rpmdbGetIteratorOffset(mi);
00205         num = 0;
00206         while (sfi->otherPkg && sfi->otherPkg == prev) {
00207             assert(sfi->otherFileNum < count);
00208             if (secStates[sfi->otherFileNum] != RPMFILE_STATE_REPLACED) {
00209                 secStates[sfi->otherFileNum] = RPMFILE_STATE_REPLACED;
00210                 if (modified == 0) {
00211                     /* Modified header will be rewritten. */
00212                     modified = 1;
00213                     xx = rpmdbSetIteratorModified(mi, modified);
00214                 }
00215                 num++;
00216             }
00217             sfi++;
00218         }
00219     }
00220     mi = rpmdbFreeIterator(mi);
00221 
00222     return RPMRC_OK;
00223 }
00224 /*@=bounds@*/
00225 
00226 rpmRC rpmInstallSourcePackage(rpmts ts, FD_t fd,
00227                 const char ** specFilePtr, const char ** cookie)
00228 {
00229     int scareMem = 1;
00230     rpmfi fi = NULL;
00231     const char * _sourcedir = NULL;
00232     const char * _specdir = NULL;
00233     const char * specFile = NULL;
00234     HGE_t hge;
00235     HFD_t hfd;
00236     Header h = NULL;
00237     struct rpmpsm_s psmbuf;
00238     rpmpsm psm = &psmbuf;
00239     int isSource;
00240     rpmRC rpmrc;
00241     int i;
00242 
00243     memset(psm, 0, sizeof(*psm));
00244     psm->ts = rpmtsLink(ts, "InstallSourcePackage");
00245 
00246     rpmrc = rpmReadPackageFile(ts, fd, "InstallSourcePackage", &h);
00247     switch (rpmrc) {
00248     case RPMRC_NOTTRUSTED:
00249     case RPMRC_NOKEY:
00250     case RPMRC_OK:
00251         break;
00252     default:
00253         goto exit;
00254         /*@notreached@*/ break;
00255     }
00256     if (h == NULL)
00257         goto exit;
00258 
00259     rpmrc = RPMRC_OK;
00260 
00261     isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
00262 
00263     if (!isSource) {
00264         rpmError(RPMERR_NOTSRPM, _("source package expected, binary found\n"));
00265         rpmrc = RPMRC_FAIL;
00266         goto exit;
00267     }
00268 
00269     (void) rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
00270 
00271     fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, scareMem);
00272     h = headerFree(h);
00273 
00274     if (fi == NULL) {   /* XXX can't happen */
00275         rpmrc = RPMRC_FAIL;
00276         goto exit;
00277     }
00278 
00279 /*@-onlytrans@*/        /* FIX: te reference */
00280     fi->te = rpmtsElement(ts, 0);
00281 /*@=onlytrans@*/
00282     if (fi->te == NULL) {       /* XXX can't happen */
00283         rpmrc = RPMRC_FAIL;
00284         goto exit;
00285     }
00286 
00287 /*@-nullpass@*/         /* FIX fi->h may be null */
00288     fi->te->h = headerLink(fi->h);
00289 /*@=nullpass@*/
00290     fi->te->fd = fdLink(fd, "installSourcePackage");
00291     hge = fi->hge;
00292     hfd = fi->hfd;
00293 
00294 /*@i@*/ (void) rpmInstallLoadMacros(fi, fi->h);
00295 
00296     psm->fi = rpmfiLink(fi, NULL);
00297     /*@-assignexpose -usereleased @*/
00298     psm->te = fi->te;
00299     /*@=assignexpose =usereleased @*/
00300 
00301     if (cookie) {
00302         *cookie = NULL;
00303         if (hge(fi->h, RPMTAG_COOKIE, NULL, (void **) cookie, NULL))
00304             *cookie = xstrdup(*cookie);
00305     }
00306 
00307     /* XXX FIXME: can't do endian neutral MD5 verification yet. */
00308 /*@i@*/ fi->fmd5s = hfd(fi->fmd5s, -1);
00309 
00310     /* XXX FIXME: don't do per-file mapping, force global flags. */
00311     fi->fmapflags = _free(fi->fmapflags);
00312     fi->mapflags = CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
00313 
00314     fi->uid = getuid();
00315     fi->gid = getgid();
00316     fi->astriplen = 0;
00317     fi->striplen = 0;
00318 
00319     for (i = 0; i < fi->fc; i++)
00320         fi->actions[i] = FA_CREATE;
00321 
00322     i = fi->fc;
00323 
00324     if (fi->h != NULL) {        /* XXX can't happen */
00325         rpmfiBuildFNames(fi->h, RPMTAG_BASENAMES, &fi->apath, NULL);
00326 
00327         if (headerIsEntry(fi->h, RPMTAG_COOKIE))
00328             for (i = 0; i < fi->fc; i++)
00329                 if (fi->fflags[i] & RPMFILE_SPECFILE) break;
00330     }
00331 
00332     if (i == fi->fc) {
00333         /* Find the spec file by name. */
00334         for (i = 0; i < fi->fc; i++) {
00335             const char * t = fi->apath[i];
00336             t += strlen(fi->apath[i]) - 5;
00337             if (!strcmp(t, ".spec")) break;
00338         }
00339     }
00340 
00341     _sourcedir = rpmGenPath(rpmtsRootDir(ts), "%{_sourcedir}", "");
00342     rpmrc = rpmMkdirPath(_sourcedir, "sourcedir");
00343     if (rpmrc) {
00344         rpmrc = RPMRC_FAIL;
00345         goto exit;
00346     }
00347 
00348     _specdir = rpmGenPath(rpmtsRootDir(ts), "%{_specdir}", "");
00349     rpmrc = rpmMkdirPath(_specdir, "specdir");
00350     if (rpmrc) {
00351         rpmrc = RPMRC_FAIL;
00352         goto exit;
00353     }
00354 
00355     /* Build dnl/dil with {_sourcedir, _specdir} as values. */
00356     if (i < fi->fc) {
00357         int speclen = strlen(_specdir) + 2;
00358         int sourcelen = strlen(_sourcedir) + 2;
00359         char * t;
00360 
00361 /*@i@*/ fi->dnl = hfd(fi->dnl, -1);
00362 
00363         fi->dc = 2;
00364         fi->dnl = xmalloc(fi->dc * sizeof(*fi->dnl)
00365                         + fi->fc * sizeof(*fi->dil)
00366                         + speclen + sourcelen);
00367         /*@-dependenttrans@*/
00368         fi->dil = (int *)(fi->dnl + fi->dc);
00369         /*@=dependenttrans@*/
00370         memset(fi->dil, 0, fi->fc * sizeof(*fi->dil));
00371         fi->dil[i] = 1;
00372         /*@-dependenttrans@*/
00373         fi->dnl[0] = t = (char *)(fi->dil + fi->fc);
00374         fi->dnl[1] = t = stpcpy( stpcpy(t, _sourcedir), "/") + 1;
00375         /*@=dependenttrans@*/
00376         (void) stpcpy( stpcpy(t, _specdir), "/");
00377 
00378         t = xmalloc(speclen + strlen(fi->bnl[i]) + 1);
00379         (void) stpcpy( stpcpy( stpcpy(t, _specdir), "/"), fi->bnl[i]);
00380         specFile = t;
00381     } else {
00382         rpmError(RPMERR_NOSPEC, _("source package contains no .spec file\n"));
00383         rpmrc = RPMRC_FAIL;
00384         goto exit;
00385     }
00386 
00387     psm->goal = PSM_PKGINSTALL;
00388 
00389     /*@-compmempass@*/  /* FIX: psm->fi->dnl should be owned. */
00390     rpmrc = rpmpsmStage(psm, PSM_PROCESS);
00391 
00392     (void) rpmpsmStage(psm, PSM_FINI);
00393     /*@=compmempass@*/
00394 
00395     if (rpmrc) rpmrc = RPMRC_FAIL;
00396 
00397 exit:
00398     if (specFilePtr && specFile && rpmrc == RPMRC_OK)
00399         *specFilePtr = specFile;
00400     else
00401         specFile = _free(specFile);
00402 
00403     _specdir = _free(_specdir);
00404     _sourcedir = _free(_sourcedir);
00405 
00406     psm->fi = rpmfiFree(psm->fi);
00407     psm->te = NULL;
00408 
00409     if (h != NULL) h = headerFree(h);
00410 
00411     /*@-branchstate@*/
00412     if (fi != NULL) {
00413         fi->te->h = headerFree(fi->te->h);
00414         if (fi->te->fd != NULL)
00415             (void) Fclose(fi->te->fd);
00416         fi->te->fd = NULL;
00417         fi->te = NULL;
00418         fi = rpmfiFree(fi);
00419     }
00420     /*@=branchstate@*/
00421 
00422     /* XXX nuke the added package(s). */
00423     rpmtsClean(ts);
00424 
00425     psm->ts = rpmtsFree(psm->ts);
00426 
00427     return rpmrc;
00428 }
00429 
00430 /*@observer@*/ /*@unchecked@*/
00431 static char * SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin";
00432 
00438 static /*@observer@*/ const char * const tag2sln(int tag)
00439         /*@*/
00440 {
00441     switch (tag) {
00442     case RPMTAG_PREIN:          return "%pre";
00443     case RPMTAG_POSTIN:         return "%post";
00444     case RPMTAG_PREUN:          return "%preun";
00445     case RPMTAG_POSTUN:         return "%postun";
00446     case RPMTAG_VERIFYSCRIPT:   return "%verify";
00447     }
00448     return "%unknownscript";
00449 }
00450 
00456 static pid_t psmWait(rpmpsm psm)
00457         /*@globals fileSystem, internalState @*/
00458         /*@modifies psm, fileSystem, internalState @*/
00459 {
00460     const rpmts ts = psm->ts;
00461     rpmtime_t msecs;
00462 
00463     (void) rpmsqWait(&psm->sq);
00464     msecs = psm->sq.op.usecs/1000;
00465     (void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_SCRIPTLETS), &psm->sq.op);
00466 
00467     rpmMessage(RPMMESS_DEBUG,
00468         _("%s: waitpid(%d) rc %d status %x secs %u.%03u\n"),
00469         psm->stepName, (unsigned)psm->sq.child,
00470         (unsigned)psm->sq.reaped, psm->sq.status,
00471         (unsigned)msecs/1000, (unsigned)msecs%1000);
00472 
00473     return psm->sq.reaped;
00474 }
00475 
00478 /*@unchecked@*/
00479 static int ldconfig_done = 0;
00480 
00481 /*@unchecked@*/ /*@observer@*/ /*@null@*/
00482 #if 0
00483 static const char * ldconfig_path = "/sbin/ldconfig";
00484 #else
00485 static const char * ldconfig_path = NULL;
00486 #endif
00487 
00506 static rpmRC runScript(rpmpsm psm, Header h, const char * sln,
00507                 int progArgc, const char ** progArgv, 
00508                 const char * script, int arg1, int arg2)
00509         /*@globals ldconfig_done, rpmGlobalMacroContext, h_errno,
00510                 fileSystem, internalState@*/
00511         /*@modifies psm, ldconfig_done, rpmGlobalMacroContext,
00512                 fileSystem, internalState @*/
00513 {
00514     const rpmts ts = psm->ts;
00515     rpmfi fi = psm->fi;
00516     HGE_t hge = fi->hge;
00517     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00518     const char ** argv = NULL;
00519     int argc = 0;
00520     const char ** prefixes = NULL;
00521     int numPrefixes;
00522     rpmTagType ipt;
00523     const char * oldPrefix;
00524     int maxPrefixLength;
00525     int len;
00526     char * prefixBuf = NULL;
00527     const char * fn = NULL;
00528     int xx;
00529     int i;
00530     int freePrefixes = 0;
00531     FD_t scriptFd;
00532     FD_t out;
00533     rpmRC rc = RPMRC_OK;
00534     const char *n, *v, *r, *a;
00535 
00536     if (progArgv == NULL && script == NULL)
00537         return rc;
00538 
00539     psm->sq.reaper = 1;
00540 
00541     /* XXX FIXME: except for %verifyscript, rpmteNEVR can be used. */
00542     xx = headerNVR(h, &n, &v, &r);
00543     xx = hge(h, RPMTAG_ARCH, NULL, (void **) &a, NULL);
00544 
00545     /* XXX bash must have functional libtermcap.so.2 */
00546     if (!strcmp(n, "libtermcap"))
00547         ldconfig_done = 0;
00548 
00549     /*
00550      * If a successor node, and ldconfig was just run, don't bother.
00551      */
00552     if (ldconfig_path && progArgv && psm->unorderedSuccessor) {
00553         if (ldconfig_done && !strcmp(progArgv[0], ldconfig_path)) {
00554             rpmMessage(RPMMESS_DEBUG,
00555                 _("%s: %s(%s-%s-%s.%s) skipping redundant \"%s\".\n"),
00556                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a,
00557                 progArgv[0]);
00558             return rc;
00559         }
00560     }
00561 
00562     rpmMessage(RPMMESS_DEBUG,
00563                 _("%s: %s(%s-%s-%s.%s) %ssynchronous scriptlet start\n"),
00564                 psm->stepName, tag2sln(psm->scriptTag), n, v, r, a,
00565                 (psm->unorderedSuccessor ? "a" : ""));
00566 
00567     if (!progArgv) {
00568         argv = alloca(5 * sizeof(*argv));
00569         argv[0] = "/bin/sh";
00570         argc = 1;
00571         ldconfig_done = 0;
00572     } else {
00573         argv = alloca((progArgc + 4) * sizeof(*argv));
00574         memcpy(argv, progArgv, progArgc * sizeof(*argv));
00575         argc = progArgc;
00576         ldconfig_done = (ldconfig_path && !strcmp(argv[0], ldconfig_path)
00577                 ? 1 : 0);
00578     }
00579 
00580 #if __ia64__
00581     /* XXX This assumes that all interpreters are elf executables. */
00582     if ((a != NULL && a[0] == 'i' && a[1] != '\0' && a[2] == '8' && a[3] == '6')
00583      && strcmp(argv[0], "/sbin/ldconfig"))
00584     {
00585         const char * fmt = rpmGetPath("%{?_autorelocate_path}", NULL);
00586         const char * errstr;
00587         char * newPath;
00588         char * t;
00589 
00590         newPath = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
00591         fmt = _free(fmt);
00592 
00593         /* XXX On ia64, change leading /emul/ix86 -> /emul/ia32, ick. */
00594         if (newPath != NULL && *newPath != '\0'
00595          && strlen(newPath) >= (sizeof("/emul/i386")-1)
00596          && newPath[0] == '/' && newPath[1] == 'e' && newPath[2] == 'm'
00597          && newPath[3] == 'u' && newPath[4] == 'l' && newPath[5] == '/'
00598          && newPath[6] == 'i' && newPath[8] == '8' && newPath[9] == '6')
00599         {
00600             newPath[7] = 'a';
00601             newPath[8] = '3';
00602             newPath[9] = '2';
00603         }
00604 
00605         t = alloca(strlen(newPath) + strlen(argv[0]) + 1);
00606         *t = '\0';
00607         (void) stpcpy( stpcpy(t, newPath), argv[0]);
00608         newPath = _free(newPath);
00609         argv[0] = t;
00610     }
00611 #endif
00612 
00613     if (hge(h, RPMTAG_INSTPREFIXES, &ipt, (void **) &prefixes, &numPrefixes)) {
00614         freePrefixes = 1;
00615     } else if (hge(h, RPMTAG_INSTALLPREFIX, NULL, (void **) &oldPrefix, NULL)) {
00616         prefixes = &oldPrefix;
00617         numPrefixes = 1;
00618     } else {
00619         numPrefixes = 0;
00620     }
00621 
00622     maxPrefixLength = 0;
00623     if (prefixes != NULL)
00624     for (i = 0; i < numPrefixes; i++) {
00625         len = strlen(prefixes[i]);
00626         if (len > maxPrefixLength) maxPrefixLength = len;
00627     }
00628     prefixBuf = alloca(maxPrefixLength + 50);
00629 
00630     if (script) {
00631         const char * rootDir = rpmtsRootDir(ts);
00632         FD_t fd;
00633 
00634         /*@-branchstate@*/
00635         if (makeTempFile((!rpmtsChrootDone(ts) ? rootDir : "/"), &fn, &fd)) {
00636             if (prefixes != NULL && freePrefixes) free(prefixes);
00637             return RPMRC_FAIL;
00638         }
00639         /*@=branchstate@*/
00640 
00641         if (rpmIsDebug() &&
00642             (!strcmp(argv[0], "/bin/sh") || !strcmp(argv[0], "/bin/bash")))
00643         {
00644             static const char set_x[] = "set -x\n";
00645             xx = Fwrite(set_x, sizeof(set_x[0]), sizeof(set_x)-1, fd);
00646         }
00647 
00648         if (ldconfig_path && strstr(script, ldconfig_path) != NULL)
00649             ldconfig_done = 1;
00650 
00651         xx = Fwrite(script, sizeof(script[0]), strlen(script), fd);
00652         xx = Fclose(fd);
00653 
00654         {   const char * sn = fn;
00655             if (!rpmtsChrootDone(ts) && rootDir != NULL &&
00656                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00657             {
00658                 sn += strlen(rootDir)-1;
00659             }
00660             argv[argc++] = sn;
00661         }
00662 
00663         if (arg1 >= 0) {
00664             char *av = alloca(20);
00665             sprintf(av, "%d", arg1);
00666             argv[argc++] = av;
00667         }
00668         if (arg2 >= 0) {
00669             char *av = alloca(20);
00670             sprintf(av, "%d", arg2);
00671             argv[argc++] = av;
00672         }
00673     }
00674 
00675     argv[argc] = NULL;
00676 
00677     scriptFd = rpmtsScriptFd(ts);
00678     if (scriptFd != NULL) {
00679         if (rpmIsVerbose()) {
00680             out = fdDup(Fileno(scriptFd));
00681         } else {
00682             out = Fopen("/dev/null", "w.fdio");
00683             if (Ferror(out)) {
00684                 out = fdDup(Fileno(scriptFd));
00685             }
00686         }
00687     } else {
00688         out = fdDup(STDOUT_FILENO);
00689     }
00690     if (out == NULL) return RPMRC_FAIL; /* XXX can't happen */
00691     
00692     /*@-branchstate@*/
00693     xx = rpmsqFork(&psm->sq);
00694     if (psm->sq.child == 0) {
00695         const char * rootDir;
00696         int pipes[2];
00697         int flag;
00698         int fdno;
00699 
00700         pipes[0] = pipes[1] = 0;
00701         /* make stdin inaccessible */
00702         xx = pipe(pipes);
00703         xx = close(pipes[1]);
00704         xx = dup2(pipes[0], STDIN_FILENO);
00705         xx = close(pipes[0]);
00706 
00707         /* XXX Force FD_CLOEXEC on 1st 100 inherited fdno's. */
00708         for (fdno = 3; fdno < 100; fdno++) {
00709             flag = fcntl(fdno, F_GETFD);
00710             if (flag == -1 || (flag & FD_CLOEXEC))
00711                 continue;
00712             xx = fcntl(fdno, F_SETFD, FD_CLOEXEC);
00713             /* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */
00714         }
00715 
00716         if (scriptFd != NULL) {
00717             int sfdno = Fileno(scriptFd);
00718             int ofdno = Fileno(out);
00719             if (sfdno != STDERR_FILENO)
00720                 xx = dup2(sfdno, STDERR_FILENO);
00721             if (ofdno != STDOUT_FILENO)
00722                 xx = dup2(ofdno, STDOUT_FILENO);
00723             /* make sure we don't close stdin/stderr/stdout by mistake! */
00724             if (ofdno > STDERR_FILENO && ofdno != sfdno)
00725                 xx = Fclose (out);
00726             if (sfdno > STDERR_FILENO)
00727                 xx = Fclose (scriptFd);
00728             else {
00729 /*@-usereleased@*/
00730                 xx = Fclose(out);
00731 /*@=usereleased@*/
00732             }
00733         }
00734 
00735         {   const char *ipath = rpmExpand("PATH=%{_install_script_path}", NULL);
00736             const char *path = SCRIPT_PATH;
00737 
00738             if (ipath && ipath[5] != '%')
00739                 path = ipath;
00740 
00741             xx = doputenv(path);
00742             /*@-modobserver@*/
00743             ipath = _free(ipath);
00744             /*@=modobserver@*/
00745         }
00746 
00747         if (prefixes != NULL)
00748         for (i = 0; i < numPrefixes; i++) {
00749             sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
00750             xx = doputenv(prefixBuf);
00751 
00752             /* backwards compatibility */
00753             if (i == 0) {
00754                 sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
00755                 xx = doputenv(prefixBuf);
00756             }
00757         }
00758 
00759         rootDir = rpmtsRootDir(ts);
00760         if (rootDir  != NULL)   /* XXX can't happen */
00761         switch(urlIsURL(rootDir)) {
00762         case URL_IS_PATH:
00763             rootDir += sizeof("file://") - 1;
00764             rootDir = strchr(rootDir, '/');
00765             /*@fallthrough@*/
00766         case URL_IS_UNKNOWN:
00767             if (!rpmtsChrootDone(ts) &&
00768                 !(rootDir[0] == '/' && rootDir[1] == '\0'))
00769             {
00770                 /*@-superuser -noeffect @*/
00771                 xx = chroot(rootDir);
00772                 /*@=superuser =noeffect @*/
00773             }
00774             xx = chdir("/");
00775             rpmMessage(RPMMESS_DEBUG, _("%s: %s(%s-%s-%s.%s)\texecv(%s) pid %d\n"),
00776                         psm->stepName, sln, n, v, r, a,
00777                         argv[0], (unsigned)getpid());
00778 
00779             /* XXX Don't mtrace into children. */
00780             unsetenv("MALLOC_CHECK_");
00781 
00782 /*@-nullstate@*/
00783 
00784 #if defined(__GLIBC__)
00785 
00789             {
00790                char* bypassVar = (char*) malloc(1024*sizeof(char));
00791                if (bypassVar != NULL)
00792                {
00793                   snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00794                   bypassVar[1023] = '\0';
00795                   if (getenv(bypassVar) != NULL)
00796                   {
00797                      char* bypassVal = (char*) malloc(1024*sizeof(char));
00798                      if (bypassVal != NULL)
00799                      {
00800                         rpmMessage(RPMMESS_DEBUG, _("Restoring LD_ASSUME_KERNEL for child scripts.\n"));
00801                         snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00802                         unsetenv(bypassVar);
00803                         snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00804                         bypassVar[1023] = '\0';
00805                         putenv(bypassVar);
00806                         free(bypassVal);
00807                      }
00808                      else
00809                      {
00810                         free(bypassVar);
00811                      }
00812                   }
00813                }
00814             }
00815 #endif
00816 
00817             /* Permit libselinux to do the scriptlet exec. */
00818             if (rpmtsSELinuxEnabled(ts) == 1) {
00819                 xx = rpm_execcon(0, argv[0], argv, environ);
00820                 if (xx != 0)
00821                     break;
00822             }
00823 
00824             xx = execv(argv[0], (char *const *)argv);
00825 /*@=nullstate@*/
00826             break;
00827         default:
00828             break;
00829         }
00830 
00831         _exit(-1);
00832         /*@notreached@*/
00833     }
00834     /*@=branchstate@*/
00835 
00836     (void) psmWait(psm);
00837 
00838   /* XXX filter order dependent multilib "other" arch helper error. */
00839   if (!(psm->sq.reaped >= 0 && !strcmp(argv[0], "/usr/sbin/glibc_post_upgrade") && WEXITSTATUS(psm->sq.status) == 110)) {
00840     if (psm->sq.reaped < 0) {
00841         rpmError(RPMERR_SCRIPT,
00842                 _("%s(%s-%s-%s.%s) scriptlet failed, waitpid(%d) rc %d: %s\n"),
00843                  sln, n, v, r, a, psm->sq.child, psm->sq.reaped, strerror(errno));
00844         rc = RPMRC_FAIL;
00845     } else
00846     if (!WIFEXITED(psm->sq.status) || WEXITSTATUS(psm->sq.status)) {
00847         rpmError(RPMERR_SCRIPT,
00848                 _("%s(%s-%s-%s.%s) scriptlet failed, exit status %d\n"),
00849                 sln, n, v, r, a, WEXITSTATUS(psm->sq.status));
00850         rc = RPMRC_FAIL;
00851     }
00852   }
00853 
00854     if (freePrefixes) prefixes = hfd(prefixes, ipt);
00855 
00856     xx = Fclose(out);   /* XXX dup'd STDOUT_FILENO */
00857     
00858     /*@-branchstate@*/
00859     if (script) {
00860         if (!rpmIsDebug())
00861             xx = unlink(fn);
00862         fn = _free(fn);
00863     }
00864     /*@=branchstate@*/
00865 
00866     return rc;
00867 }
00868 
00874 static rpmRC runInstScript(rpmpsm psm)
00875         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00876         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
00877 {
00878     rpmfi fi = psm->fi;
00879     HGE_t hge = fi->hge;
00880     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00881     void ** progArgv;
00882     int progArgc;
00883     const char ** argv;
00884     rpmTagType ptt, stt;
00885     const char * script;
00886     rpmRC rc = RPMRC_OK;
00887     int xx;
00888 
00889     /*
00890      * headerGetEntry() sets the data pointer to NULL if the entry does
00891      * not exist.
00892      */
00893     xx = hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
00894     xx = hge(fi->h, psm->progTag, &ptt, (void **) &progArgv, &progArgc);
00895     if (progArgv == NULL && script == NULL)
00896         goto exit;
00897 
00898     /*@-branchstate@*/
00899     if (progArgv && ptt == RPM_STRING_TYPE) {
00900         argv = alloca(sizeof(*argv));
00901         *argv = (const char *) progArgv;
00902     } else {
00903         argv = (const char **) progArgv;
00904     }
00905     /*@=branchstate@*/
00906 
00907     if (fi->h != NULL)  /* XXX can't happen */
00908     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), progArgc, argv,
00909                 script, psm->scriptArg, -1);
00910 
00911 exit:
00912     progArgv = hfd(progArgv, ptt);
00913     script = hfd(script, stt);
00914     return rc;
00915 }
00916 
00927 static rpmRC handleOneTrigger(const rpmpsm psm,
00928                         Header sourceH, Header triggeredH,
00929                         int arg2, unsigned char * triggersAlreadyRun)
00930         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState@*/
00931         /*@modifies psm, sourceH, triggeredH, *triggersAlreadyRun,
00932                 rpmGlobalMacroContext, fileSystem, internalState @*/
00933 {
00934     int scareMem = 1;
00935     const rpmts ts = psm->ts;
00936     rpmfi fi = psm->fi;
00937     HGE_t hge = fi->hge;
00938     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00939     rpmds trigger = NULL;
00940     const char ** triggerScripts;
00941     const char ** triggerProgs;
00942     int_32 * triggerIndices;
00943     const char * sourceName;
00944     const char * triggerName;
00945     rpmRC rc = RPMRC_OK;
00946     int xx;
00947     int i;
00948 
00949     xx = headerNVR(sourceH, &sourceName, NULL, NULL);
00950     xx = headerNVR(triggeredH, &triggerName, NULL, NULL);
00951 
00952     trigger = rpmdsInit(rpmdsNew(triggeredH, RPMTAG_TRIGGERNAME, scareMem));
00953     if (trigger == NULL)
00954         return rc;
00955 
00956     (void) rpmdsSetNoPromote(trigger, 1);
00957 
00958     while ((i = rpmdsNext(trigger)) >= 0) {
00959         rpmTagType tit, tst, tpt;
00960         const char * Name;
00961         int_32 Flags = rpmdsFlags(trigger);
00962 
00963         if ((Name = rpmdsN(trigger)) == NULL)
00964             continue;   /* XXX can't happen */
00965 
00966         if (strcmp(Name, sourceName))
00967             continue;
00968         if (!(Flags & psm->sense))
00969             continue;
00970 
00971         /*
00972          * XXX Trigger on any provided dependency, not just the package NEVR.
00973          */
00974         if (!rpmdsAnyMatchesDep(sourceH, trigger, 1))
00975             continue;
00976 
00977         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
00978                        (void **) &triggerIndices, NULL) &&
00979                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
00980                        (void **) &triggerScripts, NULL) &&
00981                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
00982                        (void **) &triggerProgs, NULL))
00983             )
00984             continue;
00985 
00986         {   int arg1;
00987             int index;
00988 
00989             arg1 = rpmdbCountPackages(rpmtsGetRdb(ts), triggerName);
00990             if (arg1 < 0) {
00991                 /* XXX W2DO? fails as "execution of script failed" */
00992                 rc = RPMRC_FAIL;
00993             } else {
00994                 arg1 += psm->countCorrection;
00995                 index = triggerIndices[i];
00996                 if (triggersAlreadyRun == NULL ||
00997                     triggersAlreadyRun[index] == 0)
00998                 {
00999                     rc = runScript(psm, triggeredH, "%trigger", 1,
01000                             triggerProgs + index, triggerScripts[index], 
01001                             arg1, arg2);
01002                     if (triggersAlreadyRun != NULL)
01003                         triggersAlreadyRun[index] = 1;
01004                 }
01005             }
01006         }
01007 
01008         triggerIndices = hfd(triggerIndices, tit);
01009         triggerScripts = hfd(triggerScripts, tst);
01010         triggerProgs = hfd(triggerProgs, tpt);
01011 
01012         /*
01013          * Each target/source header pair can only result in a single
01014          * script being run.
01015          */
01016         break;
01017     }
01018 
01019     trigger = rpmdsFree(trigger);
01020 
01021     return rc;
01022 }
01023 
01029 static rpmRC runTriggers(rpmpsm psm)
01030         /*@globals rpmGlobalMacroContext, h_errno,
01031                 fileSystem, internalState @*/
01032         /*@modifies psm, rpmGlobalMacroContext,
01033                 fileSystem, internalState @*/
01034 {
01035     const rpmts ts = psm->ts;
01036     rpmfi fi = psm->fi;
01037     int numPackage = -1;
01038     rpmRC rc = RPMRC_OK;
01039     const char * N = NULL;
01040 
01041     if (psm->te)        /* XXX can't happen */
01042         N = rpmteN(psm->te);
01043     if (N)              /* XXX can't happen */
01044         numPackage = rpmdbCountPackages(rpmtsGetRdb(ts), N)
01045                                 + psm->countCorrection;
01046     if (numPackage < 0)
01047         return RPMRC_NOTFOUND;
01048 
01049     if (fi != NULL && fi->h != NULL)    /* XXX can't happen */
01050     {   Header triggeredH;
01051         rpmdbMatchIterator mi;
01052         int countCorrection = psm->countCorrection;
01053 
01054         psm->countCorrection = 0;
01055         mi = rpmtsInitIterator(ts, RPMTAG_TRIGGERNAME, N, 0);
01056         while((triggeredH = rpmdbNextIterator(mi)) != NULL)
01057             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01058         mi = rpmdbFreeIterator(mi);
01059         psm->countCorrection = countCorrection;
01060     }
01061 
01062     return rc;
01063 }
01064 
01070 static rpmRC runImmedTriggers(rpmpsm psm)
01071         /*@globals rpmGlobalMacroContext, h_errno,
01072                 fileSystem, internalState @*/
01073         /*@modifies psm, rpmGlobalMacroContext,
01074                 fileSystem, internalState @*/
01075 {
01076     const rpmts ts = psm->ts;
01077     rpmfi fi = psm->fi;
01078     HGE_t hge = fi->hge;
01079     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01080     const char ** triggerNames;
01081     int numTriggers;
01082     int_32 * triggerIndices;
01083     rpmTagType tnt, tit;
01084     int numTriggerIndices;
01085     unsigned char * triggersRun;
01086     rpmRC rc = RPMRC_OK;
01087 
01088     if (fi->h == NULL)  return rc;      /* XXX can't happen */
01089 
01090     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01091                         (void **) &triggerNames, &numTriggers) &&
01092                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01093                         (void **) &triggerIndices, &numTriggerIndices))
01094         )
01095         return rc;
01096 
01097     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01098     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01099 
01100     {   Header sourceH = NULL;
01101         int i;
01102 
01103         for (i = 0; i < numTriggers; i++) {
01104             rpmdbMatchIterator mi;
01105 
01106             if (triggersRun[triggerIndices[i]] != 0) continue;
01107         
01108             mi = rpmtsInitIterator(ts, RPMTAG_NAME, triggerNames[i], 0);
01109 
01110             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01111                 rc |= handleOneTrigger(psm, sourceH, fi->h, 
01112                                 rpmdbGetIteratorCount(mi),
01113                                 triggersRun);
01114             }
01115 
01116             mi = rpmdbFreeIterator(mi);
01117         }
01118     }
01119     triggerIndices = hfd(triggerIndices, tit);
01120     triggerNames = hfd(triggerNames, tnt);
01121     return rc;
01122 }
01123 
01124 /*@observer@*/ static const char *const pkgStageString(pkgStage a)
01125         /*@*/
01126 {
01127     switch(a) {
01128     case PSM_UNKNOWN:           return "unknown";
01129 
01130     case PSM_PKGINSTALL:        return "  install";
01131     case PSM_PKGERASE:          return "    erase";
01132     case PSM_PKGCOMMIT:         return "   commit";
01133     case PSM_PKGSAVE:           return "repackage";
01134 
01135     case PSM_INIT:              return "init";
01136     case PSM_PRE:               return "pre";
01137     case PSM_PROCESS:           return "process";
01138     case PSM_POST:              return "post";
01139     case PSM_UNDO:              return "undo";
01140     case PSM_FINI:              return "fini";
01141 
01142     case PSM_CREATE:            return "create";
01143     case PSM_NOTIFY:            return "notify";
01144     case PSM_DESTROY:           return "destroy";
01145     case PSM_COMMIT:            return "commit";
01146 
01147     case PSM_CHROOT_IN:         return "chrootin";
01148     case PSM_CHROOT_OUT:        return "chrootout";
01149     case PSM_SCRIPT:            return "script";
01150     case PSM_TRIGGERS:          return "triggers";
01151     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01152 
01153     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01154 
01155     case PSM_RPMDB_LOAD:        return "rpmdbload";
01156     case PSM_RPMDB_ADD:         return "rpmdbadd";
01157     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01158 
01159     default:                    return "???";
01160     }
01161     /*@noteached@*/
01162 }
01163 
01164 rpmpsm XrpmpsmUnlink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01165 {
01166     if (psm == NULL) return NULL;
01167 /*@-modfilesys@*/
01168 if (_psm_debug && msg != NULL)
01169 fprintf(stderr, "--> psm %p -- %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01170 /*@=modfilesys@*/
01171     psm->nrefs--;
01172     return NULL;
01173 }
01174 
01175 rpmpsm XrpmpsmLink(rpmpsm psm, const char * msg, const char * fn, unsigned ln)
01176 {
01177     if (psm == NULL) return NULL;
01178     psm->nrefs++;
01179 
01180 /*@-modfilesys@*/
01181 if (_psm_debug && msg != NULL)
01182 fprintf(stderr, "--> psm %p ++ %d %s at %s:%u\n", psm, psm->nrefs, msg, fn, ln);
01183 /*@=modfilesys@*/
01184 
01185     /*@-refcounttrans@*/ return psm; /*@=refcounttrans@*/
01186 }
01187 
01188 rpmpsm rpmpsmFree(rpmpsm psm)
01189 {
01190     const char * msg = "rpmpsmFree";
01191     if (psm == NULL)
01192         return NULL;
01193 
01194     if (psm->nrefs > 1)
01195         return rpmpsmUnlink(psm, msg);
01196 
01197 /*@-nullstate@*/
01198     psm->fi = rpmfiFree(psm->fi);
01199 #ifdef  NOTYET
01200     psm->te = rpmteFree(psm->te);
01201 #else
01202     psm->te = NULL;
01203 #endif
01204 /*@-internalglobs@*/
01205     psm->ts = rpmtsFree(psm->ts);
01206 /*@=internalglobs@*/
01207 
01208     (void) rpmpsmUnlink(psm, msg);
01209 
01210     /*@-refcounttrans -usereleased@*/
01211 /*@-boundswrite@*/
01212     memset(psm, 0, sizeof(*psm));               /* XXX trash and burn */
01213 /*@=boundswrite@*/
01214     psm = _free(psm);
01215     /*@=refcounttrans =usereleased@*/
01216 
01217     return NULL;
01218 /*@=nullstate@*/
01219 }
01220 
01221 rpmpsm rpmpsmNew(rpmts ts, rpmte te, rpmfi fi)
01222 {
01223     const char * msg = "rpmpsmNew";
01224     rpmpsm psm = xcalloc(1, sizeof(*psm));
01225 
01226     if (ts)     psm->ts = rpmtsLink(ts, msg);
01227 #ifdef  NOTYET
01228     if (te)     psm->te = rpmteLink(te, msg);
01229 #else
01230 /*@-assignexpose -temptrans @*/
01231     if (te)     psm->te = te;
01232 /*@=assignexpose =temptrans @*/
01233 #endif
01234     if (fi)     psm->fi = rpmfiLink(fi, msg);
01235 
01236     return rpmpsmLink(psm, msg);
01237 }
01238 
01239 static void * rpmpsmThread(void * arg)
01240         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01241         /*@modifies arg, rpmGlobalMacroContext, fileSystem, internalState @*/
01242 {
01243     rpmpsm psm = arg;
01244 /*@-unqualifiedtrans@*/
01245     return ((void *) rpmpsmStage(psm, psm->nstage));
01246 /*@=unqualifiedtrans@*/
01247 }
01248 
01249 static int rpmpsmNext(rpmpsm psm, pkgStage nstage)
01250         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01251         /*@modifies psm, rpmGlobalMacroContext, fileSystem, internalState @*/
01252 {
01253     psm->nstage = nstage;
01254     if (_psm_threads)
01255         return rpmsqJoin( rpmsqThread(rpmpsmThread, psm) );
01256     return rpmpsmStage(psm, psm->nstage);
01257 }
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_STRCMP,
01303                         rpmteE(psm->te));
01304             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION, RPMMIRE_STRCMP,
01305                         rpmteV(psm->te));
01306             xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE, RPMMIRE_STRCMP,
01307                         rpmteR(psm->te));
01308             if (tscolor) {
01309                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_ARCH, RPMMIRE_STRCMP,
01310                         rpmteA(psm->te));
01311                 xx = rpmdbSetIteratorRE(psm->mi, RPMTAG_OS, RPMMIRE_STRCMP,
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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(psm, PSM_IMMED_TRIGGERS);
01432                 if (rc) break;
01433 
01434                 /* Run triggers in other package(s) this package sets off. */
01435                 rc = rpmpsmNext(psm, PSM_TRIGGERS);
01436                 if (rc) break;
01437             }
01438 
01439             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_NOPREUN))
01440                 rc = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01708                 if (rc) break;
01709             }
01710 
01711             rc = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(psm, PSM_TRIGGERS);
01726                 if (rc) break;
01727 
01728                 /* Run triggers in this package other package(s) set off. */
01729                 rc = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(psm, PSM_TRIGGERS);
01752                 if (rc) break;
01753             }
01754 
01755             if (!(rpmtsFlags(ts) & RPMTRANS_FLAG_APPLYONLY))
01756                 rc = rpmpsmNext(psm, PSM_RPMDB_REMOVE);
01757         }
01758         if (psm->goal == PSM_PKGSAVE) {
01759         }
01760 
01761         /* Restore root directory if changed. */
01762         xx = rpmpsmNext(psm, PSM_CHROOT_OUT);
01763         break;
01764     case PSM_UNDO:
01765         break;
01766     case PSM_FINI:
01767         /* Restore root directory if changed. */
01768         xx = rpmpsmNext(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 = rpmpsmNext(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 = rpmpsmNext(psm, PSM_INIT);
01832         if (!rc) rc = rpmpsmNext(psm, PSM_PRE);
01833         if (!rc) rc = rpmpsmNext(psm, PSM_PROCESS);
01834         if (!rc) rc = rpmpsmNext(psm, PSM_POST);
01835         xx = rpmpsmNext(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 Wed Nov 29 14:24:48 2006 for rpm by  doxygen 1.3.9.1