lib/psm.c

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

Generated on Wed Jan 28 12:45:24 2009 for rpm by  doxygen 1.4.7