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

psm.c

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

Generated on Fri Jul 25 16:45:18 2008 for rpm by  doxygen 1.3.9.1