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

lib/psm.c

Go to the documentation of this file.
00001 
00006 #include "system.h"
00007 
00008 #include <rpmlib.h>
00009 #include <rpmmacro.h>
00010 #include <rpmurl.h>
00011 
00012 #include "psm.h"
00013 #include "rpmlead.h"            /* writeLead proto */
00014 #include "signature.h"          /* signature constants */
00015 #include "misc.h"
00016 #include "rpmdb.h"              /* XXX for db_chrootDone */
00017 #include "debug.h"
00018 
00019 /*@access Header @*/            /* compared with NULL */
00020 /*@access rpmTransactionSet @*/ /* compared with NULL */
00021 /*@access rpmdbMatchIterator @*/ /* compared with NULL */
00022 /*@access TFI_t @*/             /* compared with NULL */
00023 /*@access FSM_t @*/             /* compared with NULL */
00024 /*@access PSM_t @*/             /* compared with NULL */
00025 /*@access FD_t @*/              /* compared with NULL */
00026 /*@access rpmdb @*/             /* compared with NULL */
00027 
00028 /*@-redecl@*/
00029 extern int _fsm_debug;
00030 /*@=redecl@*/
00031 
00032 /*@-redecl -declundef -exportheadervar@*/
00033 extern const char * chroot_prefix;
00034 /*@=redecl =declundef =exportheadervar@*/
00035 
00036 int rpmVersionCompare(Header first, Header second)
00037 {
00038     const char * one, * two;
00039     int_32 * epochOne, * epochTwo;
00040     int rc;
00041 
00042     if (!headerGetEntry(first, RPMTAG_EPOCH, NULL, (void **) &epochOne, NULL))
00043         epochOne = NULL;
00044     if (!headerGetEntry(second, RPMTAG_EPOCH, NULL, (void **) &epochTwo,
00045                         NULL))
00046         epochTwo = NULL;
00047 
00048     if (epochOne && !epochTwo)
00049         return 1;
00050     else if (!epochOne && epochTwo)
00051         return -1;
00052     else if (epochOne && epochTwo) {
00053         if (*epochOne < *epochTwo)
00054             return -1;
00055         else if (*epochOne > *epochTwo)
00056             return 1;
00057     }
00058 
00059     (void) headerGetEntry(first, RPMTAG_VERSION, NULL, (void **) &one, NULL);
00060     (void) headerGetEntry(second, RPMTAG_VERSION, NULL, (void **) &two, NULL);
00061 
00062     rc = rpmvercmp(one, two);
00063     if (rc)
00064         return rc;
00065 
00066     (void) headerGetEntry(first, RPMTAG_RELEASE, NULL, (void **) &one, NULL);
00067     (void) headerGetEntry(second, RPMTAG_RELEASE, NULL, (void **) &two, NULL);
00068 
00069     return rpmvercmp(one, two);
00070 }
00071 
00072 void loadFi(Header h, TFI_t fi)
00073 {
00074     HGE_t hge;
00075     HFD_t hfd;
00076     uint_32 * uip;
00077     int len;
00078     int rc;
00079     int i;
00080     
00081     if (fi->fsm == NULL)
00082         fi->fsm = newFSM();
00083 
00084     /* XXX avoid gcc noise on pointer (4th arg) cast(s) */
00085     hge = (fi->type == TR_ADDED)
00086         ? (HGE_t) headerGetEntryMinMemory : (HGE_t) headerGetEntry;
00087     fi->hge = hge;
00088     fi->hae = (HAE_t) headerAddEntry;
00089     fi->hme = (HME_t) headerModifyEntry;
00090     fi->hre = (HRE_t) headerRemoveEntry;
00091     fi->hfd = hfd = headerFreeData;
00092 
00093     if (h && fi->h == NULL)     fi->h = headerLink(h);
00094 
00095     /* Duplicate name-version-release so that headers can be free'd. */
00096     rc = hge(fi->h, RPMTAG_NAME, NULL, (void **) &fi->name, NULL);
00097     fi->name = xstrdup(fi->name);
00098     rc = hge(fi->h, RPMTAG_VERSION, NULL, (void **) &fi->version, NULL);
00099     fi->version = xstrdup(fi->version);
00100     rc = hge(fi->h, RPMTAG_RELEASE, NULL, (void **) &fi->release, NULL);
00101     fi->release = xstrdup(fi->release);
00102 
00103     /* -1 means not found */
00104     rc = hge(fi->h, RPMTAG_EPOCH, NULL, (void **) &uip, NULL);
00105     fi->epoch = (rc ? *uip : -1);
00106     /* 0 means unknown */
00107     rc = hge(fi->h, RPMTAG_ARCHIVESIZE, NULL, (void **) &uip, NULL);
00108     fi->archiveSize = (rc ? *uip : 0);
00109 
00110     if (!hge(fi->h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc)) {
00111         fi->dc = 0;
00112         fi->fc = 0;
00113         return;
00114     }
00115 
00116     rc = hge(fi->h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
00117     rc = hge(fi->h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
00118     rc = hge(fi->h, RPMTAG_FILEMODES, NULL, (void **) &fi->fmodes, NULL);
00119     rc = hge(fi->h, RPMTAG_FILEFLAGS, NULL, (void **) &fi->fflags, NULL);
00120     rc = hge(fi->h, RPMTAG_FILESIZES, NULL, (void **) &fi->fsizes, NULL);
00121     rc = hge(fi->h, RPMTAG_FILESTATES, NULL, (void **) &fi->fstates, NULL);
00122 
00123     fi->action = FA_UNKNOWN;
00124     fi->flags = 0;
00125 
00126     /* actions is initialized earlier for added packages */
00127     if (fi->actions == NULL)
00128         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
00129 
00130     switch (fi->type) {
00131     case TR_ADDED:
00132         fi->mapflags =
00133                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
00134         rc = hge(fi->h, RPMTAG_FILEMD5S, NULL, (void **) &fi->fmd5s, NULL);
00135         rc = hge(fi->h, RPMTAG_FILELINKTOS, NULL, (void **) &fi->flinks, NULL);
00136         rc = hge(fi->h, RPMTAG_FILELANGS, NULL, (void **) &fi->flangs, NULL);
00137         rc = hge(fi->h, RPMTAG_FILEMTIMES, NULL, (void **) &fi->fmtimes, NULL);
00138         rc = hge(fi->h, RPMTAG_FILERDEVS, NULL, (void **) &fi->frdevs, NULL);
00139 
00140         /* 0 makes for noops */
00141         fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
00142 
00143         break;
00144     case TR_REMOVED:
00145         fi->mapflags = 
00146                 CPIO_MAP_ABSOLUTE | CPIO_MAP_ADDDOT | CPIO_ALL_HARDLINKS |
00147                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
00148         rc = hge(fi->h, RPMTAG_FILEMD5S, NULL, (void **) &fi->fmd5s, NULL);
00149         rc = hge(fi->h, RPMTAG_FILELINKTOS, NULL, (void **) &fi->flinks, NULL);
00150         fi->fsizes = memcpy(xmalloc(fi->fc * sizeof(*fi->fsizes)),
00151                                 fi->fsizes, fi->fc * sizeof(*fi->fsizes));
00152         fi->fflags = memcpy(xmalloc(fi->fc * sizeof(*fi->fflags)),
00153                                 fi->fflags, fi->fc * sizeof(*fi->fflags));
00154         fi->fmodes = memcpy(xmalloc(fi->fc * sizeof(*fi->fmodes)),
00155                                 fi->fmodes, fi->fc * sizeof(*fi->fmodes));
00156         /* XXX there's a tedious segfault here for some version(s) of rpm */
00157         if (fi->fstates)
00158             fi->fstates = memcpy(xmalloc(fi->fc * sizeof(*fi->fstates)),
00159                                 fi->fstates, fi->fc * sizeof(*fi->fstates));
00160         else
00161             fi->fstates = xcalloc(1, fi->fc * sizeof(*fi->fstates));
00162         fi->dil = memcpy(xmalloc(fi->fc * sizeof(*fi->dil)),
00163                                 fi->dil, fi->fc * sizeof(*fi->dil));
00164         fi->h = headerFree(fi->h);
00165         break;
00166     }
00167 
00168     fi->dnlmax = -1;
00169     for (i = 0; i < fi->dc; i++) {
00170         if ((len = strlen(fi->dnl[i])) > fi->dnlmax)
00171             fi->dnlmax = len;
00172     }
00173 
00174     fi->bnlmax = -1;
00175     for (i = 0; i < fi->fc; i++) {
00176         if ((len = strlen(fi->bnl[i])) > fi->bnlmax)
00177             fi->bnlmax = len;
00178     }
00179 
00180     fi->dperms = 0755;
00181     fi->fperms = 0644;
00182 
00183     /*@-nullstate@*/    /* FIX: fi->h is NULL for TR_REMOVED */
00184     return;
00185     /*@=nullstate@*/
00186 }
00187 
00188 void freeFi(TFI_t fi)
00189 {
00190     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00191 
00192     fi->name = _free(fi->name);
00193     fi->version = _free(fi->version);
00194     fi->release = _free(fi->release);
00195     fi->actions = _free(fi->actions);
00196     fi->replacedSizes = _free(fi->replacedSizes);
00197     fi->replaced = _free(fi->replaced);
00198 
00199     fi->bnl = hfd(fi->bnl, -1);
00200     fi->dnl = hfd(fi->dnl, -1);
00201     fi->obnl = hfd(fi->obnl, -1);
00202     fi->odnl = hfd(fi->odnl, -1);
00203     fi->flinks = hfd(fi->flinks, -1);
00204     fi->fmd5s = hfd(fi->fmd5s, -1);
00205     fi->fuser = hfd(fi->fuser, -1);
00206     fi->fgroup = hfd(fi->fgroup, -1);
00207     fi->flangs = hfd(fi->flangs, -1);
00208 
00209     fi->apath = _free(fi->apath);
00210     fi->fuids = _free(fi->fuids);
00211     fi->fgids = _free(fi->fgids);
00212     fi->fmapflags = _free(fi->fmapflags);
00213 
00214     fi->fsm = freeFSM(fi->fsm);
00215 
00216     switch (fi->type) {
00217     case TR_ADDED:
00218             break;
00219     case TR_REMOVED:
00220         fi->fsizes = hfd(fi->fsizes, -1);
00221         fi->fflags = hfd(fi->fflags, -1);
00222         fi->fmodes = hfd(fi->fmodes, -1);
00223         fi->fstates = hfd(fi->fstates, -1);
00224         fi->dil = hfd(fi->dil, -1);
00225         break;
00226     }
00227 
00228     fi->h = headerFree(fi->h);
00229 
00230     /*@-nullstate@*/
00231     return;
00232     /*@=nullstate@*/
00233 }
00234 
00235 /*@observer@*/ const char *const fiTypeString(TFI_t fi)
00236 {
00237     switch(fi->type) {
00238     case TR_ADDED:      return " install";
00239     case TR_REMOVED:    return "   erase";
00240     default:            return "???";
00241     }
00242     /*@noteached@*/
00243 }
00244 
00249 static struct tagMacro {
00250 /*@observer@*/ /*@null@*/ const char *  macroname; 
00251     rpmTag      tag;            
00252 } tagMacros[] = {
00253     { "name",           RPMTAG_NAME },
00254     { "version",        RPMTAG_VERSION },
00255     { "release",        RPMTAG_RELEASE },
00256     { "epoch",          RPMTAG_EPOCH },
00257     { NULL, 0 }
00258 };
00259 
00266 static int rpmInstallLoadMacros(TFI_t fi, Header h)
00267         /*@modifies internalState @*/
00268 {
00269     HGE_t hge = (HGE_t) fi->hge;
00270     struct tagMacro * tagm;
00271     union {
00272 /*@unused@*/ void * ptr;
00273 /*@unused@*/ const char ** argv;
00274         const char * str;
00275         int_32 * i32p;
00276     } body;
00277     char numbuf[32];
00278     rpmTagType type;
00279 
00280     for (tagm = tagMacros; tagm->macroname != NULL; tagm++) {
00281         if (!hge(h, tagm->tag, &type, (void **) &body, NULL))
00282             continue;
00283         switch (type) {
00284         case RPM_INT32_TYPE:
00285             sprintf(numbuf, "%d", *body.i32p);
00286             addMacro(NULL, tagm->macroname, NULL, numbuf, -1);
00287             break;
00288         case RPM_STRING_TYPE:
00289             addMacro(NULL, tagm->macroname, NULL, body.str, -1);
00290             break;
00291         case RPM_NULL_TYPE:
00292         case RPM_CHAR_TYPE:
00293         case RPM_INT8_TYPE:
00294         case RPM_INT16_TYPE:
00295         case RPM_BIN_TYPE:
00296         case RPM_STRING_ARRAY_TYPE:
00297         case RPM_I18NSTRING_TYPE:
00298         default:
00299             break;
00300         }
00301     }
00302     return 0;
00303 }
00304 
00312 static int mergeFiles(TFI_t fi, Header h, Header newH)
00313         /*@modifies h @*/
00314 {
00315     HGE_t hge = (HGE_t)fi->hge;
00316     HME_t hme = (HME_t)fi->hme;
00317     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00318     fileAction * actions = fi->actions;
00319     int i, j, k, fc;
00320     rpmTagType type = 0;
00321     int_32 count = 0;
00322     int_32 dirNamesCount, dirCount;
00323     void * data, * newdata;
00324     int_32 * dirIndexes, * newDirIndexes;
00325     uint_32 * fileSizes, fileSize;
00326     const char ** dirNames;
00327     const char ** newDirNames;
00328     static rpmTag mergeTags[] = {
00329         RPMTAG_FILESIZES,
00330         RPMTAG_FILESTATES,
00331         RPMTAG_FILEMODES,
00332         RPMTAG_FILERDEVS,
00333         RPMTAG_FILEMTIMES,
00334         RPMTAG_FILEMD5S,
00335         RPMTAG_FILELINKTOS,
00336         RPMTAG_FILEFLAGS,
00337         RPMTAG_FILEUSERNAME,
00338         RPMTAG_FILEGROUPNAME,
00339         RPMTAG_FILEVERIFYFLAGS,
00340         RPMTAG_FILEDEVICES,
00341         RPMTAG_FILEINODES,
00342         RPMTAG_FILELANGS,
00343         RPMTAG_BASENAMES,
00344         0,
00345     };
00346     static rpmTag requireTags[] = {
00347         RPMTAG_REQUIRENAME, RPMTAG_REQUIREVERSION, RPMTAG_REQUIREFLAGS,
00348         RPMTAG_PROVIDENAME, RPMTAG_PROVIDEVERSION, RPMTAG_PROVIDEFLAGS,
00349         RPMTAG_CONFLICTNAME, RPMTAG_CONFLICTVERSION, RPMTAG_CONFLICTFLAGS
00350     };
00351 
00352     (void) hge(h, RPMTAG_SIZE, NULL, (void **) &fileSizes, NULL);
00353     fileSize = *fileSizes;
00354     (void) hge(newH, RPMTAG_FILESIZES, NULL, (void **) &fileSizes, &count);
00355     for (i = 0, fc = 0; i < count; i++)
00356         if (actions[i] != FA_SKIPMULTILIB) {
00357             fc++;
00358             fileSize += fileSizes[i];
00359         }
00360     (void) hme(h, RPMTAG_SIZE, RPM_INT32_TYPE, &fileSize, 1);
00361 
00362     for (i = 0; mergeTags[i]; i++) {
00363         if (!hge(newH, mergeTags[i], &type, (void **) &data, &count))
00364             continue;
00365         switch (type) {
00366         case RPM_CHAR_TYPE:
00367         case RPM_INT8_TYPE:
00368             newdata = xmalloc(fc * sizeof(int_8));
00369             for (j = 0, k = 0; j < count; j++)
00370                 if (actions[j] != FA_SKIPMULTILIB)
00371                         ((int_8 *) newdata)[k++] = ((int_8 *) data)[j];
00372             (void) headerAddOrAppendEntry(h, mergeTags[i], type, newdata, fc);
00373             free (newdata);
00374             break;
00375         case RPM_INT16_TYPE:
00376             newdata = xmalloc(fc * sizeof(int_16));
00377             for (j = 0, k = 0; j < count; j++)
00378                 if (actions[j] != FA_SKIPMULTILIB)
00379                     ((int_16 *) newdata)[k++] = ((int_16 *) data)[j];
00380             (void) headerAddOrAppendEntry(h, mergeTags[i], type, newdata, fc);
00381             free (newdata);
00382             break;
00383         case RPM_INT32_TYPE:
00384             newdata = xmalloc(fc * sizeof(int_32));
00385             for (j = 0, k = 0; j < count; j++)
00386                 if (actions[j] != FA_SKIPMULTILIB)
00387                     ((int_32 *) newdata)[k++] = ((int_32 *) data)[j];
00388             (void) headerAddOrAppendEntry(h, mergeTags[i], type, newdata, fc);
00389             free (newdata);
00390             break;
00391         case RPM_STRING_ARRAY_TYPE:
00392             newdata = xmalloc(fc * sizeof(char *));
00393             for (j = 0, k = 0; j < count; j++)
00394                 if (actions[j] != FA_SKIPMULTILIB)
00395                     ((char **) newdata)[k++] = ((char **) data)[j];
00396             (void) headerAddOrAppendEntry(h, mergeTags[i], type, newdata, fc);
00397             free (newdata);
00398             break;
00399         default:
00400             rpmError(RPMERR_DATATYPE, _("Data type %d not supported\n"),
00401                         (int) type);
00402             return 1;
00403             /*@notreached@*/ break;
00404         }
00405         data = hfd(data, type);
00406     }
00407     (void) hge(newH, RPMTAG_DIRINDEXES, NULL, (void **) &newDirIndexes, &count);
00408     (void) hge(newH, RPMTAG_DIRNAMES, NULL, (void **) &newDirNames, NULL);
00409     (void) hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
00410     (void) hge(h, RPMTAG_DIRNAMES, NULL, (void **) &data, &dirNamesCount);
00411 
00412     dirNames = xcalloc(dirNamesCount + fc, sizeof(char *));
00413     for (i = 0; i < dirNamesCount; i++)
00414         dirNames[i] = ((char **) data)[i];
00415     dirCount = dirNamesCount;
00416     newdata = xmalloc(fc * sizeof(int_32));
00417     for (i = 0, k = 0; i < count; i++) {
00418         if (actions[i] == FA_SKIPMULTILIB)
00419             continue;
00420         for (j = 0; j < dirCount; j++)
00421             if (!strcmp(dirNames[j], newDirNames[newDirIndexes[i]]))
00422                 /*@innerbreak@*/ break;
00423         if (j == dirCount)
00424             dirNames[dirCount++] = newDirNames[newDirIndexes[i]];
00425         ((int_32 *) newdata)[k++] = j;
00426     }
00427     (void) headerAddOrAppendEntry(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, newdata, fc);
00428     if (dirCount > dirNamesCount)
00429         (void) headerAddOrAppendEntry(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
00430                                dirNames + dirNamesCount,
00431                                dirCount - dirNamesCount);
00432     data = hfd(data, -1);
00433     newDirNames = hfd(newDirNames, -1);
00434     free (newdata);
00435     free (dirNames);
00436 
00437     for (i = 0; i < 9; i += 3) {
00438         const char **Names, **EVR, **newNames, **newEVR;
00439         rpmTagType nnt, nvt, rnt;
00440         uint_32 *Flags, *newFlags;
00441         int Count = 0, newCount = 0;
00442 
00443         if (!hge(newH, requireTags[i], &nnt, (void **) &newNames, &newCount))
00444             continue;
00445 
00446         (void) hge(newH, requireTags[i+1], &nvt, (void **) &newEVR, NULL);
00447         (void) hge(newH, requireTags[i+2], NULL, (void **) &newFlags, NULL);
00448         if (hge(h, requireTags[i], &rnt, (void **) &Names, &Count))
00449         {
00450             (void) hge(h, requireTags[i+1], NULL, (void **) &EVR, NULL);
00451             (void) hge(h, requireTags[i+2], NULL, (void **) &Flags, NULL);
00452             for (j = 0; j < newCount; j++)
00453                 for (k = 0; k < Count; k++)
00454                     if (!strcmp (newNames[j], Names[k])
00455                         && !strcmp (newEVR[j], EVR[k])
00456                         && (newFlags[j] & RPMSENSE_SENSEMASK) ==
00457                            (Flags[k] & RPMSENSE_SENSEMASK))
00458                     {
00459                         newNames[j] = NULL;
00460                         /*@innerbreak@*/ break;
00461                     }
00462         }
00463         for (j = 0, k = 0; j < newCount; j++) {
00464             if (!newNames[j] || !isDependsMULTILIB(newFlags[j]))
00465                 continue;
00466             if (j != k) {
00467                 newNames[k] = newNames[j];
00468                 newEVR[k] = newEVR[j];
00469                 newFlags[k] = newFlags[j];
00470             }
00471             k++;
00472         }
00473         if (k) {
00474             (void) headerAddOrAppendEntry(h, requireTags[i],
00475                                        RPM_STRING_ARRAY_TYPE, newNames, k);
00476             (void) headerAddOrAppendEntry(h, requireTags[i+1],
00477                                        RPM_STRING_ARRAY_TYPE, newEVR, k);
00478             (void) headerAddOrAppendEntry(h, requireTags[i+2], RPM_INT32_TYPE,
00479                                        newFlags, k);
00480         }
00481         newNames = hfd(newNames, nnt);
00482         newEVR = hfd(newEVR, nvt);
00483         Names = hfd(Names, rnt);
00484     }
00485     return 0;
00486 }
00487 
00493 static int markReplacedFiles(PSM_t psm)
00494         /*@modifies psm, fileSystem @*/
00495 {
00496     const rpmTransactionSet ts = psm->ts;
00497     TFI_t fi = psm->fi;
00498     HGE_t hge = (HGE_t)fi->hge;
00499     const struct sharedFileInfo * replaced = fi->replaced;
00500     const struct sharedFileInfo * sfi;
00501     rpmdbMatchIterator mi;
00502     Header h;
00503     unsigned int * offsets;
00504     unsigned int prev;
00505     int num;
00506 
00507     if (!(fi->fc > 0 && fi->replaced))
00508         return 0;
00509 
00510     num = prev = 0;
00511     for (sfi = replaced; sfi->otherPkg; sfi++) {
00512         if (prev && prev == sfi->otherPkg)
00513             continue;
00514         prev = sfi->otherPkg;
00515         num++;
00516     }
00517     if (num == 0)
00518         return 0;
00519 
00520     offsets = alloca(num * sizeof(*offsets));
00521     num = prev = 0;
00522     for (sfi = replaced; sfi->otherPkg; sfi++) {
00523         if (prev && prev == sfi->otherPkg)
00524             continue;
00525         prev = sfi->otherPkg;
00526         offsets[num++] = sfi->otherPkg;
00527     }
00528 
00529     mi = rpmdbInitIterator(ts->rpmdb, RPMDBI_PACKAGES, NULL, 0);
00530     (void) rpmdbAppendIterator(mi, offsets, num);
00531     (void) rpmdbSetIteratorRewrite(mi, 1);
00532 
00533     sfi = replaced;
00534     while ((h = rpmdbNextIterator(mi)) != NULL) {
00535         char * secStates;
00536         int modified;
00537         int count;
00538 
00539         modified = 0;
00540 
00541         if (!hge(h, RPMTAG_FILESTATES, NULL, (void **)&secStates, &count))
00542             continue;
00543         
00544         prev = rpmdbGetIteratorOffset(mi);
00545         num = 0;
00546         while (sfi->otherPkg && sfi->otherPkg == prev) {
00547             assert(sfi->otherFileNum < count);
00548             if (secStates[sfi->otherFileNum] != RPMFILE_STATE_REPLACED) {
00549                 secStates[sfi->otherFileNum] = RPMFILE_STATE_REPLACED;
00550                 if (modified == 0) {
00551                     /* Modified header will be rewritten. */
00552                     modified = 1;
00553                     (void) rpmdbSetIteratorModified(mi, modified);
00554                 }
00555                 num++;
00556             }
00557             sfi++;
00558         }
00559     }
00560     mi = rpmdbFreeIterator(mi);
00561 
00562     return 0;
00563 }
00564 
00572 static rpmRC chkdir (const char * dpath, const char * dname)
00573         /*@modifies fileSystem @*/
00574 {
00575     struct stat st;
00576     int rc;
00577 
00578     if ((rc = Stat(dpath, &st)) < 0) {
00579         int ut = urlPath(dpath, NULL);
00580         switch (ut) {
00581         case URL_IS_PATH:
00582         case URL_IS_UNKNOWN:
00583             if (errno != ENOENT)
00584                 break;
00585             /*@fallthrough@*/
00586         case URL_IS_FTP:
00587         case URL_IS_HTTP:
00588             rc = Mkdir(dpath, 0755);
00589             break;
00590         case URL_IS_DASH:
00591             break;
00592         }
00593         if (rc < 0) {
00594             rpmError(RPMERR_CREATE, _("cannot create %%%s %s\n"),
00595                         dname, dpath);
00596             return RPMRC_FAIL;
00597         }
00598     }
00599     if ((rc = Access(dpath, W_OK))) {
00600         rpmError(RPMERR_CREATE, _("cannot write to %%%s %s\n"), dname, dpath);
00601         return RPMRC_FAIL;
00602     }
00603     return RPMRC_OK;
00604 }
00605 
00606 rpmRC rpmInstallSourcePackage(const char * rootDir, FD_t fd,
00607                         const char ** specFilePtr,
00608                         rpmCallbackFunction notify, rpmCallbackData notifyData,
00609                         char ** cookie)
00610 {
00611     rpmdb rpmdb = NULL;
00612     rpmTransactionSet ts = rpmtransCreateSet(rpmdb, rootDir);
00613     TFI_t fi = xcalloc(sizeof(*fi), 1);
00614     const char * _sourcedir = NULL;
00615     const char * _specdir = NULL;
00616     const char * specFile = NULL;
00617     HGE_t hge;
00618     HFD_t hfd;
00619     Header h = NULL;
00620     struct psm_s psmbuf;
00621     PSM_t psm = &psmbuf;
00622     int isSource;
00623     rpmRC rc;
00624     int i;
00625 
00626     ts->notify = notify;
00627     /*@-temptrans -assignexpose@*/
00628     ts->notifyData = notifyData;
00629     /*@=temptrans =assignexpose@*/
00630 
00631     /*@-mustmod@*/      /* LCL: segfault */
00632     rc = rpmReadPackageHeader(fd, &h, &isSource, NULL, NULL);
00633     /*@=mustmod@*/
00634     if (rc)
00635         goto exit;
00636 
00637     if (!isSource) {
00638         rpmError(RPMERR_NOTSRPM, _("source package expected, binary found\n"));
00639         rc = RPMRC_FAIL;
00640         goto exit;
00641     }
00642 
00643     (void) rpmtransAddPackage(ts, h, fd, NULL, 0, NULL);
00644     if (ts->addedPackages.list == NULL) {       /* XXX can't happen */
00645         rc = RPMRC_FAIL;
00646         goto exit;
00647     }
00648 
00649     fi->type = TR_ADDED;
00650     fi->ap = ts->addedPackages.list;
00651     loadFi(h, fi);
00652     hge = fi->hge;
00653     hfd = (fi->hfd ? fi->hfd : headerFreeData);
00654     h = headerFree(h);  /* XXX reference held by transaction set */
00655 
00656     (void) rpmInstallLoadMacros(fi, fi->h);
00657 
00658     memset(psm, 0, sizeof(*psm));
00659     psm->ts = ts;
00660     psm->fi = fi;
00661 
00662     if (cookie) {
00663         *cookie = NULL;
00664         if (hge(fi->h, RPMTAG_COOKIE, NULL, (void **) cookie, NULL))
00665             *cookie = xstrdup(*cookie);
00666     }
00667 
00668     /* XXX FIXME: can't do endian neutral MD5 verification yet. */
00669     fi->fmd5s = hfd(fi->fmd5s, -1);
00670 
00671     /* XXX FIXME: don't do per-file mapping, force global flags. */
00672     fi->fmapflags = _free(fi->fmapflags);
00673     fi->mapflags = CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
00674 
00675     fi->uid = getuid();
00676     fi->gid = getgid();
00677     fi->astriplen = 0;
00678     fi->striplen = 0;
00679 
00680     fi->fuids = xcalloc(sizeof(*fi->fuids), fi->fc);
00681     fi->fgids = xcalloc(sizeof(*fi->fgids), fi->fc);
00682     for (i = 0; i < fi->fc; i++) {
00683         fi->fuids[i] = fi->uid;
00684         fi->fgids[i] = fi->gid;
00685     }
00686 
00687     for (i = 0; i < fi->fc; i++) {
00688         fi->actions[i] = FA_CREATE;
00689     }
00690 
00691     rpmBuildFileList(fi->h, &fi->apath, NULL);
00692 
00693     i = fi->fc;
00694     if (headerIsEntry(fi->h, RPMTAG_COOKIE))
00695         for (i = 0; i < fi->fc; i++)
00696                 if (fi->fflags[i] & RPMFILE_SPECFILE) break;
00697 
00698     if (i == fi->fc) {
00699         /* Find the spec file by name. */
00700         for (i = 0; i < fi->fc; i++) {
00701             const char * t = fi->apath[i];
00702             t += strlen(fi->apath[i]) - 5;
00703             if (!strcmp(t, ".spec")) break;
00704         }
00705     }
00706 
00707     _sourcedir = rpmGenPath(ts->rootDir, "%{_sourcedir}", "");
00708     rc = chkdir(_sourcedir, "sourcedir");
00709     if (rc) {
00710         rc = RPMRC_FAIL;
00711         goto exit;
00712     }
00713 
00714     _specdir = rpmGenPath(ts->rootDir, "%{_specdir}", "");
00715     rc = chkdir(_specdir, "specdir");
00716     if (rc) {
00717         rc = RPMRC_FAIL;
00718         goto exit;
00719     }
00720 
00721     /* Build dnl/dil with {_sourcedir, _specdir} as values. */
00722     if (i < fi->fc) {
00723         int speclen = strlen(_specdir) + 2;
00724         int sourcelen = strlen(_sourcedir) + 2;
00725         char * t;
00726 
00727         fi->dnl = hfd(fi->dnl, -1);
00728 
00729         fi->dc = 2;
00730         fi->dnl = xmalloc(fi->dc * sizeof(*fi->dnl) + fi->fc * sizeof(*fi->dil) +
00731                         speclen + sourcelen);
00732         fi->dil = (int *)(fi->dnl + fi->dc);
00733         memset(fi->dil, 0, fi->fc * sizeof(*fi->dil));
00734         fi->dil[i] = 1;
00735         /*@-dependenttrans@*/
00736         fi->dnl[0] = t = (char *)(fi->dil + fi->fc);
00737         fi->dnl[1] = t = stpcpy( stpcpy(t, _sourcedir), "/") + 1;
00738         /*@=dependenttrans@*/
00739         (void) stpcpy( stpcpy(t, _specdir), "/");
00740 
00741         t = xmalloc(speclen + strlen(fi->bnl[i]) + 1);
00742         (void) stpcpy( stpcpy( stpcpy(t, _specdir), "/"), fi->bnl[i]);
00743         specFile = t;
00744     } else {
00745         rpmError(RPMERR_NOSPEC, _("source package contains no .spec file\n"));
00746         rc = RPMRC_FAIL;
00747         goto exit;
00748     }
00749 
00750     psm->goal = PSM_PKGINSTALL;
00751 
00752     /*@-compmempass@*/  /* FIX: psm->fi->dnl should be owned. */
00753     rc = psmStage(psm, PSM_PROCESS);
00754 
00755     (void) psmStage(psm, PSM_FINI);
00756     /*@=compmempass@*/
00757 
00758     if (rc) rc = RPMRC_FAIL;
00759 
00760 exit:
00761     if (specFilePtr && specFile && rc == RPMRC_OK)
00762         *specFilePtr = specFile;
00763     else
00764         specFile = _free(specFile);
00765 
00766     _specdir = _free(_specdir);
00767     _sourcedir = _free(_sourcedir);
00768 
00769     if (h) h = headerFree(h);
00770 
00771     if (fi) {
00772         freeFi(fi);
00773         fi = _free(fi);
00774     }
00775     ts = rpmtransFree(ts);
00776 
00777     return rc;
00778 }
00779 
00780 /*@observer@*/ static char * SCRIPT_PATH =
00781         "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin";
00782 
00788 static /*@observer@*/ const char * const tag2sln(int tag)
00789         /*@*/
00790 {
00791     switch (tag) {
00792     case RPMTAG_PREIN:          return "%pre";
00793     case RPMTAG_POSTIN:         return "%post";
00794     case RPMTAG_PREUN:          return "%preun";
00795     case RPMTAG_POSTUN:         return "%postun";
00796     case RPMTAG_VERIFYSCRIPT:   return "%verify";
00797     }
00798     return "%unknownscript";
00799 }
00800 
00819 static int runScript(PSM_t psm, Header h,
00820                 const char * sln,
00821                 int progArgc, const char ** progArgv, 
00822                 const char * script, int arg1, int arg2)
00823         /*@modifies psm, fileSystem @*/
00824 {
00825     const rpmTransactionSet ts = psm->ts;
00826     TFI_t fi = psm->fi;
00827     HGE_t hge = fi->hge;
00828     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00829     const char ** argv = NULL;
00830     int argc = 0;
00831     const char ** prefixes = NULL;
00832     int numPrefixes;
00833     rpmTagType ipt;
00834     const char * oldPrefix;
00835     int maxPrefixLength;
00836     int len;
00837     char * prefixBuf = NULL;
00838     pid_t child;
00839     int status = 0;
00840     const char * fn = NULL;
00841     int i;
00842     int freePrefixes = 0;
00843     FD_t out;
00844     rpmRC rc = RPMRC_OK;
00845     const char *n, *v, *r;
00846 
00847     if (!progArgv && !script)
00848         return 0;
00849 
00850     if (!progArgv) {
00851         argv = alloca(5 * sizeof(char *));
00852         argv[0] = "/bin/sh";
00853         argc = 1;
00854     } else {
00855         argv = alloca((progArgc + 4) * sizeof(char *));
00856         memcpy(argv, progArgv, progArgc * sizeof(char *));
00857         argc = progArgc;
00858     }
00859 
00860     (void) headerNVR(h, &n, &v, &r);
00861     if (hge(h, RPMTAG_INSTPREFIXES, &ipt, (void **) &prefixes, &numPrefixes)) {
00862         freePrefixes = 1;
00863     } else if (hge(h, RPMTAG_INSTALLPREFIX, NULL, (void **) &oldPrefix, NULL)) {
00864         prefixes = &oldPrefix;
00865         numPrefixes = 1;
00866     } else {
00867         numPrefixes = 0;
00868     }
00869 
00870     maxPrefixLength = 0;
00871     for (i = 0; i < numPrefixes; i++) {
00872         len = strlen(prefixes[i]);
00873         if (len > maxPrefixLength) maxPrefixLength = len;
00874     }
00875     prefixBuf = alloca(maxPrefixLength + 50);
00876 
00877     if (script) {
00878         FD_t fd;
00879         if (makeTempFile((!ts->chrootDone ? ts->rootDir : "/"), &fn, &fd)) {
00880             if (freePrefixes) free(prefixes);
00881             return 1;
00882         }
00883 
00884         if (rpmIsDebug() &&
00885             (!strcmp(argv[0], "/bin/sh") || !strcmp(argv[0], "/bin/bash")))
00886             (void) Fwrite("set -x\n", sizeof(char), 7, fd);
00887 
00888         (void) Fwrite(script, sizeof(script[0]), strlen(script), fd);
00889         (void) Fclose(fd);
00890 
00891         {   const char * sn = fn;
00892             if (!ts->chrootDone &&
00893                 !(ts->rootDir[0] == '/' && ts->rootDir[1] == '\0'))
00894             {
00895                 sn += strlen(ts->rootDir)-1;
00896             }
00897             argv[argc++] = sn;
00898         }
00899 
00900         if (arg1 >= 0) {
00901             char *av = alloca(20);
00902             sprintf(av, "%d", arg1);
00903             argv[argc++] = av;
00904         }
00905         if (arg2 >= 0) {
00906             char *av = alloca(20);
00907             sprintf(av, "%d", arg2);
00908             argv[argc++] = av;
00909         }
00910     }
00911 
00912     argv[argc] = NULL;
00913 
00914     if (ts->scriptFd != NULL) {
00915         if (rpmIsVerbose()) {
00916             out = fdDup(Fileno(ts->scriptFd));
00917         } else {
00918             out = Fopen("/dev/null", "w.fdio");
00919             if (Ferror(out)) {
00920                 out = fdDup(Fileno(ts->scriptFd));
00921             }
00922         }
00923     } else {
00924         out = fdDup(STDOUT_FILENO);
00925 #ifdef  DYING
00926         out = fdLink(out, "runScript persist");
00927 #endif
00928     }
00929     if (out == NULL) return 1;  /* XXX can't happen */
00930     
00931     if (!(child = fork())) {
00932         const char * rootDir;
00933         int pipes[2];
00934 
00935         pipes[0] = pipes[1] = 0;
00936         /* make stdin inaccessible */
00937         (void) pipe(pipes);
00938         (void) close(pipes[1]);
00939         (void) dup2(pipes[0], STDIN_FILENO);
00940         (void) close(pipes[0]);
00941 
00942         if (ts->scriptFd != NULL) {
00943             if (Fileno(ts->scriptFd) != STDERR_FILENO)
00944                 (void) dup2(Fileno(ts->scriptFd), STDERR_FILENO);
00945             if (Fileno(out) != STDOUT_FILENO)
00946                 (void) dup2(Fileno(out), STDOUT_FILENO);
00947             /* make sure we don't close stdin/stderr/stdout by mistake! */
00948             if (Fileno(out) > STDERR_FILENO && Fileno(out) != Fileno(ts->scriptFd)) {
00949                 (void) Fclose (out);
00950             }
00951             if (Fileno(ts->scriptFd) > STDERR_FILENO) {
00952                 (void) Fclose (ts->scriptFd);
00953             }
00954         }
00955 
00956         {   const char *ipath = rpmExpand("PATH=%{_install_script_path}", NULL);
00957             const char *path = SCRIPT_PATH;
00958 
00959             if (ipath && ipath[5] != '%')
00960                 path = ipath;
00961             (void) doputenv(path);
00962             /*@-modobserver@*/
00963             ipath = _free(ipath);
00964             /*@=modobserver@*/
00965         }
00966 
00967         for (i = 0; i < numPrefixes; i++) {
00968             sprintf(prefixBuf, "RPM_INSTALL_PREFIX%d=%s", i, prefixes[i]);
00969             (void) doputenv(prefixBuf);
00970 
00971             /* backwards compatibility */
00972             if (i == 0) {
00973                 sprintf(prefixBuf, "RPM_INSTALL_PREFIX=%s", prefixes[i]);
00974                 (void) doputenv(prefixBuf);
00975             }
00976         }
00977 
00978         if ((rootDir = ts->rootDir) != NULL)    /* XXX can't happen */
00979         switch(urlIsURL(rootDir)) {
00980         case URL_IS_PATH:
00981             rootDir += sizeof("file://") - 1;
00982             rootDir = strchr(rootDir, '/');
00983             /*@fallthrough@*/
00984         case URL_IS_UNKNOWN:
00985             if (!ts->chrootDone && !(rootDir[0] == '/' && rootDir[1] == '\0')) {
00986                 /*@-unrecog -superuser @*/
00987                 (void) chroot(rootDir);
00988                 /*@=unrecog =superuser @*/
00989             }
00990             (void) chdir("/");
00991             (void) execv(argv[0], (char *const *)argv);
00992             break;
00993         default:
00994             break;
00995         }
00996 
00997         _exit(-1);
00998         /*@notreached@*/
00999     }
01000 
01001     if (waitpid(child, &status, 0) < 0) {
01002         rpmError(RPMERR_SCRIPT,
01003      _("execution of %s scriptlet from %s-%s-%s failed, waitpid returned %s\n"),
01004                  sln, n, v, r, strerror (errno));
01005         /* XXX what to do here? */
01006         rc = RPMRC_OK;
01007     } else {
01008         if (!WIFEXITED(status) || WEXITSTATUS(status)) {
01009             rpmError(RPMERR_SCRIPT,
01010      _("execution of %s scriptlet from %s-%s-%s failed, exit status %d\n"),
01011                      sln, n, v, r, WEXITSTATUS(status));
01012             rc = RPMRC_FAIL;
01013         }
01014     }
01015 
01016     if (freePrefixes) prefixes = hfd(prefixes, ipt);
01017 
01018     (void) Fclose(out); /* XXX dup'd STDOUT_FILENO */
01019     
01020     if (script) {
01021         if (!rpmIsDebug())
01022             (void) unlink(fn);
01023         fn = _free(fn);
01024     }
01025 
01026     return rc;
01027 }
01028 
01034 static rpmRC runInstScript(PSM_t psm)
01035         /*@modifies psm, fileSystem @*/
01036 {
01037     TFI_t fi = psm->fi;
01038     HGE_t hge = fi->hge;
01039     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01040     void ** programArgv;
01041     int programArgc;
01042     const char ** argv;
01043     rpmTagType ptt, stt;
01044     const char * script;
01045     rpmRC rc = RPMRC_OK;
01046 
01047     /*
01048      * headerGetEntry() sets the data pointer to NULL if the entry does
01049      * not exist.
01050      */
01051     (void) hge(fi->h, psm->progTag, &ptt, (void **) &programArgv, &programArgc);
01052     (void) hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
01053 
01054     if (programArgv && ptt == RPM_STRING_TYPE) {
01055         argv = alloca(sizeof(char *));
01056         *argv = (const char *) programArgv;
01057     } else {
01058         argv = (const char **) programArgv;
01059     }
01060 
01061     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), programArgc, argv,
01062                 script, psm->scriptArg, -1);
01063 
01064     programArgv = hfd(programArgv, ptt);
01065     script = hfd(script, stt);
01066     return rc;
01067 }
01068 
01077 static int handleOneTrigger(PSM_t psm, Header sourceH, Header triggeredH,
01078                         int arg2, unsigned char * triggersAlreadyRun)
01079         /*@modifies psm, *triggersAlreadyRun, fileSystem @*/
01080 {
01081     const rpmTransactionSet ts = psm->ts;
01082     TFI_t fi = psm->fi;
01083     HGE_t hge = fi->hge;
01084     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01085     const char ** triggerNames;
01086     const char ** triggerEVR;
01087     const char ** triggerScripts;
01088     const char ** triggerProgs;
01089     int_32 * triggerFlags;
01090     int_32 * triggerIndices;
01091     rpmTagType tnt, tvt, tft;
01092     const char * triggerPackageName;
01093     const char * sourceName;
01094     int numTriggers;
01095     rpmRC rc = RPMRC_OK;
01096     int i;
01097     int skip;
01098 
01099     if (!(      hge(triggeredH, RPMTAG_TRIGGERNAME, &tnt, 
01100                         (void **) &triggerNames, &numTriggers) &&
01101                 hge(triggeredH, RPMTAG_TRIGGERFLAGS, &tft,
01102                         (void **) &triggerFlags, NULL) &&
01103                 hge(triggeredH, RPMTAG_TRIGGERVERSION, &tvt,
01104                         (void **) &triggerEVR, NULL))
01105         )
01106         return 0;
01107 
01108     (void) headerNVR(sourceH, &sourceName, NULL, NULL);
01109 
01110     for (i = 0; i < numTriggers; i++) {
01111         rpmTagType tit, tst, tpt;
01112 
01113         if (!(triggerFlags[i] & psm->sense)) continue;
01114         if (strcmp(triggerNames[i], sourceName)) continue;
01115 
01116         /*
01117          * For some reason, the TRIGGERVERSION stuff includes the name of
01118          * the package which the trigger is based on. We need to skip
01119          * over that here. I suspect that we'll change our minds on this
01120          * and remove that, so I'm going to just 'do the right thing'.
01121          */
01122         skip = strlen(triggerNames[i]);
01123         if (!strncmp(triggerEVR[i], triggerNames[i], skip) &&
01124             (triggerEVR[i][skip] == '-'))
01125             skip++;
01126         else
01127             skip = 0;
01128 
01129         if (!headerMatchesDepFlags(sourceH, triggerNames[i],
01130                 triggerEVR[i] + skip, triggerFlags[i]))
01131             continue;
01132 
01133         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
01134                        (void **) &triggerIndices, NULL) &&
01135                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
01136                        (void **) &triggerScripts, NULL) &&
01137                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
01138                        (void **) &triggerProgs, NULL))
01139             )
01140             continue;
01141 
01142         (void) headerNVR(triggeredH, &triggerPackageName, NULL, NULL);
01143 
01144         {   int arg1;
01145             int index;
01146 
01147             arg1 = rpmdbCountPackages(ts->rpmdb, triggerPackageName);
01148             if (arg1 < 0) {
01149                 /* XXX W2DO? same as "execution of script failed" */
01150                 rc = RPMRC_FAIL;
01151             } else {
01152                 arg1 += psm->countCorrection;
01153                 index = triggerIndices[i];
01154                 if (triggersAlreadyRun == NULL ||
01155                     triggersAlreadyRun[index] == 0)
01156                 {
01157                     rc = runScript(psm, triggeredH, "%trigger", 1,
01158                             triggerProgs + index, triggerScripts[index], 
01159                             arg1, arg2);
01160                     if (triggersAlreadyRun != NULL)
01161                         triggersAlreadyRun[index] = 1;
01162                 }
01163             }
01164         }
01165 
01166         triggerIndices = hfd(triggerIndices, tit);
01167         triggerScripts = hfd(triggerScripts, tst);
01168         triggerProgs = hfd(triggerProgs, tpt);
01169 
01170         /*
01171          * Each target/source header pair can only result in a single
01172          * script being run.
01173          */
01174         break;
01175     }
01176 
01177     triggerNames = hfd(triggerNames, tnt);
01178     triggerFlags = hfd(triggerFlags, tft);
01179     triggerEVR = hfd(triggerEVR, tvt);
01180 
01181     return rc;
01182 }
01183 
01189 static int runTriggers(PSM_t psm)
01190         /*@modifies psm, fileSystem @*/
01191 {
01192     const rpmTransactionSet ts = psm->ts;
01193     TFI_t fi = psm->fi;
01194     int numPackage;
01195     rpmRC rc = RPMRC_OK;
01196 
01197     numPackage = rpmdbCountPackages(ts->rpmdb, fi->name) + psm->countCorrection;
01198     if (numPackage < 0)
01199         return 1;
01200 
01201     {   Header triggeredH;
01202         rpmdbMatchIterator mi;
01203         int countCorrection = psm->countCorrection;
01204 
01205         psm->countCorrection = 0;
01206         mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_TRIGGERNAME, fi->name, 0);
01207         while((triggeredH = rpmdbNextIterator(mi)) != NULL) {
01208             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01209         }
01210 
01211         mi = rpmdbFreeIterator(mi);
01212         psm->countCorrection = countCorrection;
01213     }
01214 
01215     return rc;
01216 }
01217 
01223 static int runImmedTriggers(PSM_t psm)
01224         /*@modifies psm, fileSystem @*/
01225 {
01226     const rpmTransactionSet ts = psm->ts;
01227     TFI_t fi = psm->fi;
01228     HGE_t hge = fi->hge;
01229     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01230     const char ** triggerNames;
01231     int numTriggers;
01232     int_32 * triggerIndices;
01233     rpmTagType tnt, tit;
01234     int numTriggerIndices;
01235     unsigned char * triggersRun;
01236     rpmRC rc = RPMRC_OK;
01237 
01238     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01239                         (void **) &triggerNames, &numTriggers) &&
01240                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01241                         (void **) &triggerIndices, &numTriggerIndices))
01242         )
01243         return 0;
01244 
01245     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01246     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01247 
01248     {   Header sourceH = NULL;
01249         int i;
01250 
01251         for (i = 0; i < numTriggers; i++) {
01252             rpmdbMatchIterator mi;
01253 
01254             if (triggersRun[triggerIndices[i]] != 0) continue;
01255         
01256             mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, triggerNames[i], 0);
01257 
01258             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01259                 rc |= handleOneTrigger(psm, sourceH, fi->h, 
01260                                 rpmdbGetIteratorCount(mi),
01261                                 triggersRun);
01262             }
01263 
01264             mi = rpmdbFreeIterator(mi);
01265         }
01266     }
01267     triggerIndices = hfd(triggerIndices, tit);
01268     triggerNames = hfd(triggerNames, tnt);
01269     return rc;
01270 }
01271 
01272 /*@observer@*/ static const char *const pkgStageString(pkgStage a) {
01273     switch(a) {
01274     case PSM_UNKNOWN:           return "unknown";
01275 
01276     case PSM_PKGINSTALL:        return "  install";
01277     case PSM_PKGERASE:          return "    erase";
01278     case PSM_PKGCOMMIT:         return "   commit";
01279     case PSM_PKGSAVE:           return "repackage";
01280 
01281     case PSM_INIT:              return "init";
01282     case PSM_PRE:               return "pre";
01283     case PSM_PROCESS:           return "process";
01284     case PSM_POST:              return "post";
01285     case PSM_UNDO:              return "undo";
01286     case PSM_FINI:              return "fini";
01287 
01288     case PSM_CREATE:            return "create";
01289     case PSM_NOTIFY:            return "notify";
01290     case PSM_DESTROY:           return "destroy";
01291     case PSM_COMMIT:            return "commit";
01292 
01293     case PSM_CHROOT_IN:         return "chrootin";
01294     case PSM_CHROOT_OUT:        return "chrootout";
01295     case PSM_SCRIPT:            return "script";
01296     case PSM_TRIGGERS:          return "triggers";
01297     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01298 
01299     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01300 
01301     case PSM_RPMDB_LOAD:        return "rpmdbload";
01302     case PSM_RPMDB_ADD:         return "rpmdbadd";
01303     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01304 
01305     default:                    return "???";
01306     }
01307     /*@noteached@*/
01308 }
01309 
01314 int psmStage(PSM_t psm, pkgStage stage)
01315 {
01316     const rpmTransactionSet ts = psm->ts;
01317     TFI_t fi = psm->fi;
01318     HGE_t hge = fi->hge;
01319     HME_t hme = fi->hme;
01320     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01321     rpmRC rc = psm->rc;
01322     int saveerrno;
01323 
01324     switch (stage) {
01325     case PSM_UNKNOWN:
01326         break;
01327     case PSM_INIT:
01328         rpmMessage(RPMMESS_DEBUG, _("%s: %s-%s-%s has %d files, test = %d\n"),
01329                 psm->stepName, fi->name, fi->version, fi->release,
01330                 fi->fc, (ts->transFlags & RPMTRANS_FLAG_TEST));
01331 
01332         /*
01333          * When we run scripts, we pass an argument which is the number of 
01334          * versions of this package that will be installed when we are
01335          * finished.
01336          */
01337         psm->npkgs_installed = rpmdbCountPackages(ts->rpmdb, fi->name);
01338         if (psm->npkgs_installed < 0) {
01339             rc = RPMRC_FAIL;
01340             break;
01341         }
01342 
01343         if (psm->goal == PSM_PKGINSTALL) {
01344             psm->scriptArg = psm->npkgs_installed + 1;
01345 
01346 assert(psm->mi == NULL);
01347             psm->mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, fi->name, 0);
01348             (void) rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION,
01349                         RPMMIRE_DEFAULT, fi->version);
01350             (void) rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE,
01351                         RPMMIRE_DEFAULT, fi->release);
01352 
01353             while ((psm->oh = rpmdbNextIterator(psm->mi))) {
01354                 fi->record = rpmdbGetIteratorOffset(psm->mi);
01355                 if (ts->transFlags & RPMTRANS_FLAG_MULTILIB)
01356                     psm->oh = headerCopy(psm->oh);
01357                 else
01358                     psm->oh = NULL;
01359                 /*@loopbreak@*/ break;
01360             }
01361             psm->mi = rpmdbFreeIterator(psm->mi);
01362             rc = RPMRC_OK;
01363 
01364             if (fi->fc > 0 && fi->fstates == NULL) {
01365                 fi->fstates = xmalloc(sizeof(*fi->fstates) * fi->fc);
01366                 memset(fi->fstates, RPMFILE_STATE_NORMAL, fi->fc);
01367             }
01368 
01369             if (fi->fc <= 0)                            break;
01370             if (ts->transFlags & RPMTRANS_FLAG_JUSTDB)  break;
01371         
01372             /*
01373              * Old format relocateable packages need the entire default
01374              * prefix stripped to form the cpio list, while all other packages
01375              * need the leading / stripped.
01376              */
01377             {   const char * p;
01378                 rc = hge(fi->h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &p, NULL);
01379                 fi->striplen = (rc ? strlen(p) + 1 : 1); 
01380             }
01381             fi->mapflags =
01382                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01383         
01384             if (headerIsEntry(fi->h, RPMTAG_ORIGBASENAMES))
01385                 buildOrigFileList(fi->h, &fi->apath, NULL);
01386             else
01387                 rpmBuildFileList(fi->h, &fi->apath, NULL);
01388         
01389             if (fi->fuser == NULL)
01390                 (void) hge(fi->h, RPMTAG_FILEUSERNAME, NULL,
01391                                 (void **) &fi->fuser, NULL);
01392             if (fi->fgroup == NULL)
01393                 (void) hge(fi->h, RPMTAG_FILEGROUPNAME, NULL,
01394                                 (void **) &fi->fgroup, NULL);
01395             if (fi->fuids == NULL)
01396                 fi->fuids = xcalloc(sizeof(*fi->fuids), fi->fc);
01397             if (fi->fgids == NULL)
01398                 fi->fgids = xcalloc(sizeof(*fi->fgids), fi->fc);
01399             rc = RPMRC_OK;
01400         }
01401         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01402             psm->scriptArg = psm->npkgs_installed - 1;
01403         
01404             /* Retrieve installed header. */
01405             rc = psmStage(psm, PSM_RPMDB_LOAD);
01406         }
01407         if (psm->goal == PSM_PKGSAVE) {
01408             /* Open output package for writing. */
01409             {   const char * bfmt = rpmGetPath("%{_repackage_name_fmt}", NULL);
01410                 const char * pkgbn =
01411                         headerSprintf(fi->h, bfmt, rpmTagTable, rpmHeaderFormats, NULL);
01412 
01413                 bfmt = _free(bfmt);
01414                 psm->pkgURL = rpmGenPath("%{?_repackage_root:%{_repackage_root}}",
01415                                          "%{?_repackage_dir:%{_repackage_dir}}",
01416                                         pkgbn);
01417                 pkgbn = _free(pkgbn);
01418                 (void) urlPath(psm->pkgURL, &psm->pkgfn);
01419                 psm->fd = Fopen(psm->pkgfn, "w.ufdio");
01420                 if (psm->fd == NULL || Ferror(psm->fd)) {
01421                     rc = RPMRC_FAIL;
01422                     break;
01423                 }
01424             }
01425         }
01426         break;
01427     case PSM_PRE:
01428         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01429 
01430         /* Change root directory if requested and not already done. */
01431         rc = psmStage(psm, PSM_CHROOT_IN);
01432 
01433         if (psm->goal == PSM_PKGINSTALL) {
01434             psm->scriptTag = RPMTAG_PREIN;
01435             psm->progTag = RPMTAG_PREINPROG;
01436 
01437             if (!(ts->transFlags & RPMTRANS_FLAG_NOTRIGGERPREIN)) {
01438                 /* XXX FIXME: implement %triggerprein. */
01439             }
01440 
01441             if (!(ts->transFlags & RPMTRANS_FLAG_NOPRE)) {
01442                 rc = psmStage(psm, PSM_SCRIPT);
01443                 if (rc) {
01444                     rpmError(RPMERR_SCRIPT,
01445                         _("%s: %s scriptlet failed (%d), skipping %s-%s-%s\n"),
01446                         psm->stepName, tag2sln(psm->scriptTag), rc,
01447                         fi->name, fi->version, fi->release);
01448                     break;
01449                 }
01450             }
01451         }
01452 
01453         if (psm->goal == PSM_PKGERASE) {
01454             psm->scriptTag = RPMTAG_PREUN;
01455             psm->progTag = RPMTAG_PREUNPROG;
01456             psm->sense = RPMSENSE_TRIGGERUN;
01457             psm->countCorrection = -1;
01458 
01459             if (!(ts->transFlags & RPMTRANS_FLAG_NOTRIGGERUN)) {
01460                 /* Run triggers in other package(s) this package sets off. */
01461                 rc = psmStage(psm, PSM_TRIGGERS);
01462                 if (rc) break;
01463 
01464                 /* Run triggers in this package other package(s) set off. */
01465                 rc = psmStage(psm, PSM_IMMED_TRIGGERS);
01466                 if (rc) break;
01467             }
01468 
01469             if (!(ts->transFlags & RPMTRANS_FLAG_NOPREUN))
01470                 rc = psmStage(psm, PSM_SCRIPT);
01471         }
01472         if (psm->goal == PSM_PKGSAVE) {
01473             /* Regenerate original header. */
01474             {   void * uh = NULL;
01475                 int_32 uht, uhc;
01476 
01477                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)) {
01478                     psm->oh = headerCopyLoad(uh);
01479                     uh = hfd(uh, uht);
01480                 } else {
01481                     psm->oh = headerLink(fi->h);
01482                 }
01483             }
01484 
01485             /* Add remove transaction id to header. */
01486             if (psm->oh)
01487             {   int_32 tid = ts->id;
01488                 (void) headerAddEntry(psm->oh, RPMTAG_REMOVETID,
01489                         RPM_INT32_TYPE, &tid, 1);
01490             }
01491 
01492             /* Retrieve type of payload compression. */
01493             /*@-nullstate@*/    /* FIX: psm->oh may be NULL */
01494             rc = psmStage(psm, PSM_RPMIO_FLAGS);
01495             /*@=nullstate@*/
01496 
01497             /* Write the lead section into the package. */
01498             {   int archnum = -1;
01499                 int osnum = -1;
01500                 struct rpmlead lead;
01501 
01502 #ifndef DYING
01503                 rpmGetArchInfo(NULL, &archnum);
01504                 rpmGetOsInfo(NULL, &osnum);
01505 #endif
01506 
01507                 memset(&lead, 0, sizeof(lead));
01508                 /* XXX Set package version conditioned on noDirTokens. */
01509                 lead.major = 4;
01510                 lead.minor = 0;
01511                 lead.type = RPMLEAD_BINARY;
01512                 lead.archnum = archnum;
01513                 lead.osnum = osnum;
01514                 lead.signature_type = RPMSIGTYPE_HEADERSIG;
01515 
01516                 {   char buf[256];
01517                     sprintf(buf, "%s-%s-%s", fi->name, fi->version, fi->release);
01518                     strncpy(lead.name, buf, sizeof(lead.name));
01519                 }
01520 
01521                 rc = writeLead(psm->fd, &lead);
01522                 if (rc) {
01523                     rpmError(RPMERR_NOSPACE, _("Unable to write package: %s\n"),
01524                          Fstrerror(psm->fd));
01525                     rc = RPMRC_FAIL;
01526                     break;
01527                 }
01528             }
01529 
01530             /* Write the signature section into the package. */
01531             {   Header sig = headerRegenSigHeader(fi->h);
01532                 rc = rpmWriteSignature(psm->fd, sig);
01533                 sig = rpmFreeSignature(sig);
01534                 if (rc) break;
01535             }
01536 
01537             /* Write the metadata section into the package. */
01538             rc = headerWrite(psm->fd, psm->oh, HEADER_MAGIC_YES);
01539             if (rc) break;
01540         }
01541         break;
01542     case PSM_PROCESS:
01543         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01544 
01545         if (psm->goal == PSM_PKGINSTALL) {
01546             struct availablePackage * alp = fi->ap;
01547             int i;
01548 
01549             if (fi->fc <= 0)                            break;
01550             if (ts->transFlags & RPMTRANS_FLAG_JUSTDB)  break;
01551 
01552             for (i = 0; i < fi->fc; i++) {
01553                 uid_t uid;
01554                 gid_t gid;
01555 
01556                 uid = fi->uid;
01557                 gid = fi->gid;
01558                 if (fi->fuser && unameToUid(fi->fuser[i], &uid)) {
01559                     rpmMessage(RPMMESS_WARNING,
01560                         _("user %s does not exist - using root\n"),
01561                         fi->fuser[i]);
01562                     uid = 0;
01563                     /* XXX this diddles header memory. */
01564                     fi->fmodes[i] &= ~S_ISUID;  /* turn off the suid bit */
01565                 }
01566 
01567                 if (fi->fgroup && gnameToGid(fi->fgroup[i], &gid)) {
01568                     rpmMessage(RPMMESS_WARNING,
01569                         _("group %s does not exist - using root\n"),
01570                         fi->fgroup[i]);
01571                     gid = 0;
01572                     /* XXX this diddles header memory. */
01573                     fi->fmodes[i] &= ~S_ISGID;  /* turn off the sgid bit */
01574                 }
01575                 if (fi->fuids) fi->fuids[i] = uid;
01576                 if (fi->fgids) fi->fgids[i] = gid;
01577             }
01578 
01579             /* Retrieve type of payload compression. */
01580             rc = psmStage(psm, PSM_RPMIO_FLAGS);
01581 
01582             if (alp->fd == NULL) {      /* XXX can't happen */
01583                 rc = RPMRC_FAIL;
01584                 break;
01585             }
01586             /*@-nullpass@*/     /* LCL: alp->fd != NULL here. */
01587             psm->cfd = Fdopen(fdDup(Fileno(alp->fd)), psm->rpmio_flags);
01588             /*@=nullpass@*/
01589             if (psm->cfd == NULL) {     /* XXX can't happen */
01590                 rc = RPMRC_FAIL;
01591                 break;
01592             }
01593 
01594             rc = fsmSetup(fi->fsm, FSM_PKGINSTALL, ts, fi,
01595                         psm->cfd, NULL, &psm->failedFile);
01596             (void) fsmTeardown(fi->fsm);
01597 
01598             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01599             (void) Fclose(psm->cfd);
01600             psm->cfd = NULL;
01601             errno = saveerrno; /* XXX FIXME: Fclose with libio destroys errno */
01602 
01603             if (!rc)
01604                 rc = psmStage(psm, PSM_COMMIT);
01605 
01606             if (rc) {
01607                 rpmError(RPMERR_CPIO,
01608                         _("unpacking of archive failed%s%s: %s\n"),
01609                         (psm->failedFile != NULL ? _(" on file ") : ""),
01610                         (psm->failedFile != NULL ? psm->failedFile : ""),
01611                         cpioStrerror(rc));
01612                 rc = RPMRC_FAIL;
01613                 break;
01614             }
01615             psm->what = RPMCALLBACK_INST_PROGRESS;
01616             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01617             psm->total = psm->amount;
01618             (void) psmStage(psm, PSM_NOTIFY);
01619         }
01620         if (psm->goal == PSM_PKGERASE) {
01621 
01622             if (fi->fc <= 0)                            break;
01623             if (ts->transFlags & RPMTRANS_FLAG_JUSTDB)  break;
01624             if (ts->transFlags & RPMTRANS_FLAG_APPLYONLY)       break;
01625 
01626             psm->what = RPMCALLBACK_UNINST_START;
01627             psm->amount = fi->fc;       /* XXX W2DO? looks wrong. */
01628             psm->total = fi->fc;
01629             (void) psmStage(psm, PSM_NOTIFY);
01630 
01631             rc = fsmSetup(fi->fsm, FSM_PKGERASE, ts, fi,
01632                         NULL, NULL, &psm->failedFile);
01633             (void) fsmTeardown(fi->fsm);
01634 
01635             psm->what = RPMCALLBACK_UNINST_STOP;
01636             psm->amount = 0;            /* XXX W2DO? looks wrong. */
01637             psm->total = fi->fc;
01638             (void) psmStage(psm, PSM_NOTIFY);
01639 
01640         }
01641         if (psm->goal == PSM_PKGSAVE) {
01642             fileAction * actions = fi->actions;
01643             fileAction action = fi->action;
01644 
01645             fi->action = FA_COPYOUT;
01646             fi->actions = NULL;
01647 
01648             if (psm->fd == NULL) {      /* XXX can't happen */
01649                 rc = RPMRC_FAIL;
01650                 break;
01651             }
01652             /*@-nullpass@*/     /* LCL: psm->fd != NULL here. */
01653             (void) Fflush(psm->fd);
01654             psm->cfd = Fdopen(fdDup(Fileno(psm->fd)), psm->rpmio_flags);
01655             /*@=nullpass@*/
01656             if (psm->cfd == NULL) {     /* XXX can't happen */
01657                 rc = RPMRC_FAIL;
01658                 break;
01659             }
01660 
01661             rc = fsmSetup(fi->fsm, FSM_PKGBUILD, ts, fi, psm->cfd,
01662                         NULL, &psm->failedFile);
01663             (void) fsmTeardown(fi->fsm);
01664 
01665             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01666             (void) Fclose(psm->cfd);
01667             psm->cfd = NULL;
01668             errno = saveerrno;
01669 
01670             fi->action = action;
01671             fi->actions = actions;
01672         }
01673         break;
01674     case PSM_POST:
01675         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01676 
01677         if (psm->goal == PSM_PKGINSTALL) {
01678             int_32 installTime = (int_32) time(NULL);
01679 
01680             if (fi->fstates != NULL && fi->fc > 0)
01681                 (void) headerAddEntry(fi->h, RPMTAG_FILESTATES, RPM_CHAR_TYPE,
01682                                 fi->fstates, fi->fc);
01683 
01684             (void) headerAddEntry(fi->h, RPMTAG_INSTALLTIME, RPM_INT32_TYPE,
01685                                 &installTime, 1);
01686 
01687             if (ts->transFlags & RPMTRANS_FLAG_MULTILIB) {
01688                 uint_32 multiLib, * newMultiLib, * p;
01689 
01690                 if (hge(fi->h, RPMTAG_MULTILIBS, NULL,
01691                                 (void **) &newMultiLib, NULL) &&
01692                     hge(psm->oh, RPMTAG_MULTILIBS, NULL,
01693                                 (void **) &p, NULL))
01694                 {
01695                     multiLib = *p;
01696                     multiLib |= *newMultiLib;
01697                     (void) hme(psm->oh, RPMTAG_MULTILIBS, RPM_INT32_TYPE,
01698                                       &multiLib, 1);
01699                 }
01700                 rc = mergeFiles(fi, psm->oh, fi->h);
01701                 if (rc) break;
01702             }
01703 
01704 
01705             /*
01706              * If this package has already been installed, remove it from
01707              * the database before adding the new one.
01708              */
01709             if (fi->record && !(ts->transFlags & RPMTRANS_FLAG_APPLYONLY)) {
01710                 rc = psmStage(psm, PSM_RPMDB_REMOVE);
01711                 if (rc) break;
01712             }
01713 
01714             rc = psmStage(psm, PSM_RPMDB_ADD);
01715             if (rc) break;
01716 
01717             psm->scriptTag = RPMTAG_POSTIN;
01718             psm->progTag = RPMTAG_POSTINPROG;
01719             psm->sense = RPMSENSE_TRIGGERIN;
01720             psm->countCorrection = 0;
01721 
01722             if (!(ts->transFlags & RPMTRANS_FLAG_NOPOST)) {
01723                 rc = psmStage(psm, PSM_SCRIPT);
01724                 if (rc) break;
01725             }
01726             if (!(ts->transFlags & RPMTRANS_FLAG_NOTRIGGERIN)) {
01727                 /* Run triggers in other package(s) this package sets off. */
01728                 rc = psmStage(psm, PSM_TRIGGERS);
01729                 if (rc) break;
01730 
01731                 /* Run triggers in this package other package(s) set off. */
01732                 rc = psmStage(psm, PSM_IMMED_TRIGGERS);
01733                 if (rc) break;
01734             }
01735 
01736             if (!(ts->transFlags & RPMTRANS_FLAG_APPLYONLY))
01737                 rc = markReplacedFiles(psm);
01738 
01739         }
01740         if (psm->goal == PSM_PKGERASE) {
01741 
01742             psm->scriptTag = RPMTAG_POSTUN;
01743             psm->progTag = RPMTAG_POSTUNPROG;
01744             psm->sense = RPMSENSE_TRIGGERPOSTUN;
01745             psm->countCorrection = -1;
01746 
01747             if (!(ts->transFlags & RPMTRANS_FLAG_NOPOSTUN)) {
01748                 rc = psmStage(psm, PSM_SCRIPT);
01749                 /* XXX WTFO? postun failures don't cause erasure failure. */
01750             }
01751 
01752             if (!(ts->transFlags & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
01753                 /* Run triggers in other package(s) this package sets off. */
01754                 rc = psmStage(psm, PSM_TRIGGERS);
01755                 if (rc) break;
01756             }
01757 
01758             if (!(ts->transFlags & RPMTRANS_FLAG_APPLYONLY))
01759                 rc = psmStage(psm, PSM_RPMDB_REMOVE);
01760         }
01761         if (psm->goal == PSM_PKGSAVE) {
01762         }
01763 
01764         /* Restore root directory if changed. */
01765         (void) psmStage(psm, PSM_CHROOT_OUT);
01766         break;
01767     case PSM_UNDO:
01768         break;
01769     case PSM_FINI:
01770         /* Restore root directory if changed. */
01771         (void) psmStage(psm, PSM_CHROOT_OUT);
01772 
01773         if (psm->fd) {
01774             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01775             (void) Fclose(psm->fd);
01776             psm->fd = NULL;
01777             errno = saveerrno;
01778         }
01779 
01780         if (psm->goal == PSM_PKGSAVE) {
01781             if (!rc) {
01782                 rpmMessage(RPMMESS_VERBOSE, _("Wrote: %s\n"),
01783                         (psm->pkgURL ? psm->pkgURL : "???"));
01784             }
01785         }
01786 
01787         if (rc) {
01788             if (psm->failedFile)
01789                 rpmError(RPMERR_CPIO,
01790                         _("%s failed on file %s: %s\n"),
01791                         psm->stepName, psm->failedFile, cpioStrerror(rc));
01792             else
01793                 rpmError(RPMERR_CPIO, _("%s failed: %s\n"),
01794                         psm->stepName, cpioStrerror(rc));
01795         }
01796 
01797         if (fi->h && (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE))
01798             fi->h = headerFree(fi->h);
01799         psm->oh = headerFree(psm->oh);
01800         psm->pkgURL = _free(psm->pkgURL);
01801         psm->rpmio_flags = _free(psm->rpmio_flags);
01802         psm->failedFile = _free(psm->failedFile);
01803 
01804         fi->fgids = _free(fi->fgids);
01805         fi->fuids = _free(fi->fuids);
01806         fi->fgroup = hfd(fi->fgroup, -1);
01807         fi->fuser = hfd(fi->fuser, -1);
01808         fi->apath = _free(fi->apath);
01809         fi->fstates = _free(fi->fstates);
01810         break;
01811 
01812     case PSM_PKGINSTALL:
01813     case PSM_PKGERASE:
01814     case PSM_PKGSAVE:
01815         psm->goal = stage;
01816         psm->rc = RPMRC_OK;
01817         psm->stepName = pkgStageString(stage);
01818 
01819         rc = psmStage(psm, PSM_INIT);
01820         if (!rc) rc = psmStage(psm, PSM_PRE);
01821         if (!rc) rc = psmStage(psm, PSM_PROCESS);
01822         if (!rc) rc = psmStage(psm, PSM_POST);
01823         (void) psmStage(psm, PSM_FINI);
01824         break;
01825     case PSM_PKGCOMMIT:
01826         break;
01827 
01828     case PSM_CREATE:
01829         break;
01830     case PSM_NOTIFY:
01831         if (ts && ts->notify)
01832             (void) ts->notify(fi->h, psm->what, psm->amount, psm->total,
01833                 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
01834         break;
01835     case PSM_DESTROY:
01836         break;
01837     case PSM_COMMIT:
01838         if (!(ts->transFlags & RPMTRANS_FLAG_PKGCOMMIT)) break;
01839         if (ts->transFlags & RPMTRANS_FLAG_APPLYONLY) break;
01840 
01841         rc = fsmSetup(fi->fsm, FSM_PKGCOMMIT, ts, fi,
01842                         NULL, NULL, &psm->failedFile);
01843         (void) fsmTeardown(fi->fsm);
01844         break;
01845 
01846     case PSM_CHROOT_IN:
01847         /* Change root directory if requested and not already done. */
01848         if (ts->rootDir && !ts->chrootDone && !psm->chrootDone) {
01849             static int _loaded = 0;
01850 
01851             /*
01852              * This loads all of the name services libraries, in case we
01853              * don't have access to them in the chroot().
01854              */
01855             if (!_loaded) {
01856                 (void)getpwnam("root");
01857                 endpwent();
01858                 _loaded++;
01859             }
01860 
01861             (void) chdir("/");
01862             /*@-unrecog -superuser @*/
01863             rc = chroot(ts->rootDir);
01864             /*@=unrecog =superuser @*/
01865             psm->chrootDone = ts->chrootDone = 1;
01866             if (ts->rpmdb != NULL) ts->rpmdb->db_chrootDone = 1;
01867             /*@-onlytrans@*/
01868             chroot_prefix = ts->rootDir;
01869             /*@=onlytrans@*/
01870         }
01871         break;
01872     case PSM_CHROOT_OUT:
01873         /* Restore root directory if changed. */
01874         if (psm->chrootDone) {
01875             /*@-unrecog -superuser @*/
01876             rc = chroot(".");
01877             /*@=unrecog =superuser @*/
01878             psm->chrootDone = ts->chrootDone = 0;
01879             if (ts->rpmdb != NULL) ts->rpmdb->db_chrootDone = 0;
01880             chroot_prefix = NULL;
01881             (void) chdir(ts->currDir);
01882         }
01883         break;
01884     case PSM_SCRIPT:
01885         rpmMessage(RPMMESS_DEBUG, _("%s: running %s script(s) (if any)\n"),
01886                 psm->stepName, tag2sln(psm->scriptTag));
01887         rc = runInstScript(psm);
01888         break;
01889     case PSM_TRIGGERS:
01890         /* Run triggers in other package(s) this package sets off. */
01891         rc = runTriggers(psm);
01892         break;
01893     case PSM_IMMED_TRIGGERS:
01894         /* Run triggers in this package other package(s) set off. */
01895         rc = runImmedTriggers(psm);
01896         break;
01897 
01898     case PSM_RPMIO_FLAGS:
01899     {   const char * payload_compressor = NULL;
01900         char * t;
01901 
01902         if (!hge(fi->h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
01903                             (void **) &payload_compressor, NULL))
01904             payload_compressor = "gzip";
01905         psm->rpmio_flags = t = xmalloc(sizeof("w9.gzdio"));
01906         *t = '\0';
01907         t = stpcpy(t, ((psm->goal == PSM_PKGSAVE) ? "w9" : "r"));
01908         if (!strcmp(payload_compressor, "gzip"))
01909             t = stpcpy(t, ".gzdio");
01910         if (!strcmp(payload_compressor, "bzip2"))
01911             t = stpcpy(t, ".bzdio");
01912         rc = RPMRC_OK;
01913     }   break;
01914 
01915     case PSM_RPMDB_LOAD:
01916 assert(psm->mi == NULL);
01917         psm->mi = rpmdbInitIterator(ts->rpmdb, RPMDBI_PACKAGES,
01918                                 &fi->record, sizeof(fi->record));
01919 
01920         fi->h = rpmdbNextIterator(psm->mi);
01921         if (fi->h)
01922             fi->h = headerLink(fi->h);
01923 else {
01924 fprintf(stderr, "*** PSM_RDB_LOAD: header #%u not found\n", fi->record);
01925 }
01926         psm->mi = rpmdbFreeIterator(psm->mi);
01927         rc = (fi->h ? RPMRC_OK : RPMRC_FAIL);
01928         break;
01929     case PSM_RPMDB_ADD:
01930         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01931         rc = rpmdbAdd(ts->rpmdb, ts->id, fi->h);
01932         break;
01933     case PSM_RPMDB_REMOVE:
01934         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01935         rc = rpmdbRemove(ts->rpmdb, ts->id, fi->record);
01936         break;
01937 
01938     default:
01939         break;
01940     }
01941 
01942     /*@-nullstate@*/    /* FIX: psm->oh and psm->fi->h may be NULL. */
01943     return rc;
01944     /*@=nullstate@*/
01945 }

Generated at Mon Dec 3 13:19:36 2001 for rpm by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001