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;
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     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@*/ chroot(rootDir); /*@=unrecog@*/
00987             }
00988             (void) chdir("/");
00989             (void) execv(argv[0], (char *const *)argv);
00990             break;
00991         default:
00992             break;
00993         }
00994 
00995         _exit(-1);
00996         /*@notreached@*/
00997     }
00998 
00999     if (waitpid(child, &status, 0) < 0) {
01000         rpmError(RPMERR_SCRIPT,
01001      _("execution of %s scriptlet from %s-%s-%s failed, waitpid returned %s\n"),
01002                  sln, n, v, r, strerror (errno));
01003         /* XXX what to do here? */
01004         rc = RPMRC_OK;
01005     } else {
01006         if (!WIFEXITED(status) || WEXITSTATUS(status)) {
01007             rpmError(RPMERR_SCRIPT,
01008      _("execution of %s scriptlet from %s-%s-%s failed, exit status %d\n"),
01009                      sln, n, v, r, WEXITSTATUS(status));
01010             rc = RPMRC_FAIL;
01011         }
01012     }
01013 
01014     if (freePrefixes) prefixes = hfd(prefixes, ipt);
01015 
01016     (void) Fclose(out); /* XXX dup'd STDOUT_FILENO */
01017     
01018     if (script) {
01019         if (!rpmIsDebug())
01020             (void) unlink(fn);
01021         fn = _free(fn);
01022     }
01023 
01024     return rc;
01025 }
01026 
01032 static rpmRC runInstScript(PSM_t psm)
01033         /*@modifies psm, fileSystem @*/
01034 {
01035     TFI_t fi = psm->fi;
01036     HGE_t hge = fi->hge;
01037     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01038     void ** programArgv;
01039     int programArgc;
01040     const char ** argv;
01041     rpmTagType ptt, stt;
01042     const char * script;
01043     rpmRC rc = RPMRC_OK;
01044 
01045     /*
01046      * headerGetEntry() sets the data pointer to NULL if the entry does
01047      * not exist.
01048      */
01049     (void) hge(fi->h, psm->progTag, &ptt, (void **) &programArgv, &programArgc);
01050     (void) hge(fi->h, psm->scriptTag, &stt, (void **) &script, NULL);
01051 
01052     if (programArgv && ptt == RPM_STRING_TYPE) {
01053         argv = alloca(sizeof(char *));
01054         *argv = (const char *) programArgv;
01055     } else {
01056         argv = (const char **) programArgv;
01057     }
01058 
01059     rc = runScript(psm, fi->h, tag2sln(psm->scriptTag), programArgc, argv,
01060                 script, psm->scriptArg, -1);
01061 
01062     programArgv = hfd(programArgv, ptt);
01063     script = hfd(script, stt);
01064     return rc;
01065 }
01066 
01075 static int handleOneTrigger(PSM_t psm, Header sourceH, Header triggeredH,
01076                         int arg2, unsigned char * triggersAlreadyRun)
01077         /*@modifies psm, *triggersAlreadyRun, fileSystem @*/
01078 {
01079     const rpmTransactionSet ts = psm->ts;
01080     TFI_t fi = psm->fi;
01081     HGE_t hge = fi->hge;
01082     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01083     const char ** triggerNames;
01084     const char ** triggerEVR;
01085     const char ** triggerScripts;
01086     const char ** triggerProgs;
01087     int_32 * triggerFlags;
01088     int_32 * triggerIndices;
01089     rpmTagType tnt, tvt, tft;
01090     const char * triggerPackageName;
01091     const char * sourceName;
01092     int numTriggers;
01093     rpmRC rc = RPMRC_OK;
01094     int i;
01095     int skip;
01096 
01097     if (!(      hge(triggeredH, RPMTAG_TRIGGERNAME, &tnt, 
01098                         (void **) &triggerNames, &numTriggers) &&
01099                 hge(triggeredH, RPMTAG_TRIGGERFLAGS, &tft,
01100                         (void **) &triggerFlags, NULL) &&
01101                 hge(triggeredH, RPMTAG_TRIGGERVERSION, &tvt,
01102                         (void **) &triggerEVR, NULL))
01103         )
01104         return 0;
01105 
01106     (void) headerNVR(sourceH, &sourceName, NULL, NULL);
01107 
01108     for (i = 0; i < numTriggers; i++) {
01109         rpmTagType tit, tst, tpt;
01110 
01111         if (!(triggerFlags[i] & psm->sense)) continue;
01112         if (strcmp(triggerNames[i], sourceName)) continue;
01113 
01114         /*
01115          * For some reason, the TRIGGERVERSION stuff includes the name of
01116          * the package which the trigger is based on. We need to skip
01117          * over that here. I suspect that we'll change our minds on this
01118          * and remove that, so I'm going to just 'do the right thing'.
01119          */
01120         skip = strlen(triggerNames[i]);
01121         if (!strncmp(triggerEVR[i], triggerNames[i], skip) &&
01122             (triggerEVR[i][skip] == '-'))
01123             skip++;
01124         else
01125             skip = 0;
01126 
01127         if (!headerMatchesDepFlags(sourceH, triggerNames[i],
01128                 triggerEVR[i] + skip, triggerFlags[i]))
01129             continue;
01130 
01131         if (!(  hge(triggeredH, RPMTAG_TRIGGERINDEX, &tit,
01132                        (void **) &triggerIndices, NULL) &&
01133                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTS, &tst,
01134                        (void **) &triggerScripts, NULL) &&
01135                 hge(triggeredH, RPMTAG_TRIGGERSCRIPTPROG, &tpt,
01136                        (void **) &triggerProgs, NULL))
01137             )
01138             continue;
01139 
01140         (void) headerNVR(triggeredH, &triggerPackageName, NULL, NULL);
01141 
01142         {   int arg1;
01143             int index;
01144 
01145             arg1 = rpmdbCountPackages(ts->rpmdb, triggerPackageName);
01146             if (arg1 < 0) {
01147                 /* XXX W2DO? same as "execution of script failed" */
01148                 rc = RPMRC_FAIL;
01149             } else {
01150                 arg1 += psm->countCorrection;
01151                 index = triggerIndices[i];
01152                 if (triggersAlreadyRun == NULL ||
01153                     triggersAlreadyRun[index] == 0)
01154                 {
01155                     rc = runScript(psm, triggeredH, "%trigger", 1,
01156                             triggerProgs + index, triggerScripts[index], 
01157                             arg1, arg2);
01158                     if (triggersAlreadyRun != NULL)
01159                         triggersAlreadyRun[index] = 1;
01160                 }
01161             }
01162         }
01163 
01164         triggerIndices = hfd(triggerIndices, tit);
01165         triggerScripts = hfd(triggerScripts, tst);
01166         triggerProgs = hfd(triggerProgs, tpt);
01167 
01168         /*
01169          * Each target/source header pair can only result in a single
01170          * script being run.
01171          */
01172         break;
01173     }
01174 
01175     triggerNames = hfd(triggerNames, tnt);
01176     triggerFlags = hfd(triggerFlags, tft);
01177     triggerEVR = hfd(triggerEVR, tvt);
01178 
01179     return rc;
01180 }
01181 
01187 static int runTriggers(PSM_t psm)
01188         /*@modifies psm, fileSystem @*/
01189 {
01190     const rpmTransactionSet ts = psm->ts;
01191     TFI_t fi = psm->fi;
01192     int numPackage;
01193     rpmRC rc = RPMRC_OK;
01194 
01195     numPackage = rpmdbCountPackages(ts->rpmdb, fi->name) + psm->countCorrection;
01196     if (numPackage < 0)
01197         return 1;
01198 
01199     {   Header triggeredH;
01200         rpmdbMatchIterator mi;
01201         int countCorrection = psm->countCorrection;
01202 
01203         psm->countCorrection = 0;
01204         mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_TRIGGERNAME, fi->name, 0);
01205         while((triggeredH = rpmdbNextIterator(mi)) != NULL) {
01206             rc |= handleOneTrigger(psm, fi->h, triggeredH, numPackage, NULL);
01207         }
01208 
01209         mi = rpmdbFreeIterator(mi);
01210         psm->countCorrection = countCorrection;
01211     }
01212 
01213     return rc;
01214 }
01215 
01221 static int runImmedTriggers(PSM_t psm)
01222         /*@modifies psm, fileSystem @*/
01223 {
01224     const rpmTransactionSet ts = psm->ts;
01225     TFI_t fi = psm->fi;
01226     HGE_t hge = fi->hge;
01227     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01228     const char ** triggerNames;
01229     int numTriggers;
01230     int_32 * triggerIndices;
01231     rpmTagType tnt, tit;
01232     int numTriggerIndices;
01233     unsigned char * triggersRun;
01234     rpmRC rc = RPMRC_OK;
01235 
01236     if (!(      hge(fi->h, RPMTAG_TRIGGERNAME, &tnt,
01237                         (void **) &triggerNames, &numTriggers) &&
01238                 hge(fi->h, RPMTAG_TRIGGERINDEX, &tit,
01239                         (void **) &triggerIndices, &numTriggerIndices))
01240         )
01241         return 0;
01242 
01243     triggersRun = alloca(sizeof(*triggersRun) * numTriggerIndices);
01244     memset(triggersRun, 0, sizeof(*triggersRun) * numTriggerIndices);
01245 
01246     {   Header sourceH = NULL;
01247         int i;
01248 
01249         for (i = 0; i < numTriggers; i++) {
01250             rpmdbMatchIterator mi;
01251 
01252             if (triggersRun[triggerIndices[i]] != 0) continue;
01253         
01254             mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, triggerNames[i], 0);
01255 
01256             while((sourceH = rpmdbNextIterator(mi)) != NULL) {
01257                 rc |= handleOneTrigger(psm, sourceH, fi->h, 
01258                                 rpmdbGetIteratorCount(mi),
01259                                 triggersRun);
01260             }
01261 
01262             mi = rpmdbFreeIterator(mi);
01263         }
01264     }
01265     triggerIndices = hfd(triggerIndices, tit);
01266     triggerNames = hfd(triggerNames, tnt);
01267     return rc;
01268 }
01269 
01270 /*@observer@*/ static const char *const pkgStageString(pkgStage a) {
01271     switch(a) {
01272     case PSM_UNKNOWN:           return "unknown";
01273 
01274     case PSM_PKGINSTALL:        return "  install";
01275     case PSM_PKGERASE:          return "    erase";
01276     case PSM_PKGCOMMIT:         return "   commit";
01277     case PSM_PKGSAVE:           return "repackage";
01278 
01279     case PSM_INIT:              return "init";
01280     case PSM_PRE:               return "pre";
01281     case PSM_PROCESS:           return "process";
01282     case PSM_POST:              return "post";
01283     case PSM_UNDO:              return "undo";
01284     case PSM_FINI:              return "fini";
01285 
01286     case PSM_CREATE:            return "create";
01287     case PSM_NOTIFY:            return "notify";
01288     case PSM_DESTROY:           return "destroy";
01289     case PSM_COMMIT:            return "commit";
01290 
01291     case PSM_CHROOT_IN:         return "chrootin";
01292     case PSM_CHROOT_OUT:        return "chrootout";
01293     case PSM_SCRIPT:            return "script";
01294     case PSM_TRIGGERS:          return "triggers";
01295     case PSM_IMMED_TRIGGERS:    return "immedtriggers";
01296 
01297     case PSM_RPMIO_FLAGS:       return "rpmioflags";
01298 
01299     case PSM_RPMDB_LOAD:        return "rpmdbload";
01300     case PSM_RPMDB_ADD:         return "rpmdbadd";
01301     case PSM_RPMDB_REMOVE:      return "rpmdbremove";
01302 
01303     default:                    return "???";
01304     }
01305     /*@noteached@*/
01306 }
01307 
01312 int psmStage(PSM_t psm, pkgStage stage)
01313 {
01314     const rpmTransactionSet ts = psm->ts;
01315     TFI_t fi = psm->fi;
01316     HGE_t hge = fi->hge;
01317     HME_t hme = fi->hme;
01318     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
01319     rpmRC rc = psm->rc;
01320     int saveerrno;
01321 
01322     switch (stage) {
01323     case PSM_UNKNOWN:
01324         break;
01325     case PSM_INIT:
01326         rpmMessage(RPMMESS_DEBUG, _("%s: %s-%s-%s has %d files, test = %d\n"),
01327                 psm->stepName, fi->name, fi->version, fi->release,
01328                 fi->fc, (ts->transFlags & RPMTRANS_FLAG_TEST));
01329 
01330         /*
01331          * When we run scripts, we pass an argument which is the number of 
01332          * versions of this package that will be installed when we are
01333          * finished.
01334          */
01335         psm->npkgs_installed = rpmdbCountPackages(ts->rpmdb, fi->name);
01336         if (psm->npkgs_installed < 0) {
01337             rc = RPMRC_FAIL;
01338             break;
01339         }
01340 
01341         if (psm->goal == PSM_PKGINSTALL) {
01342             psm->scriptArg = psm->npkgs_installed + 1;
01343 
01344 assert(psm->mi == NULL);
01345             psm->mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, fi->name, 0);
01346             (void) rpmdbSetIteratorRE(psm->mi, RPMTAG_VERSION,
01347                         RPMMIRE_DEFAULT, fi->version);
01348             (void) rpmdbSetIteratorRE(psm->mi, RPMTAG_RELEASE,
01349                         RPMMIRE_DEFAULT, fi->release);
01350 
01351             while ((psm->oh = rpmdbNextIterator(psm->mi))) {
01352                 fi->record = rpmdbGetIteratorOffset(psm->mi);
01353                 if (ts->transFlags & RPMTRANS_FLAG_MULTILIB)
01354                     psm->oh = headerCopy(psm->oh);
01355                 else
01356                     psm->oh = NULL;
01357                 /*@loopbreak@*/ break;
01358             }
01359             psm->mi = rpmdbFreeIterator(psm->mi);
01360             rc = RPMRC_OK;
01361 
01362             if (fi->fc > 0 && fi->fstates == NULL) {
01363                 fi->fstates = xmalloc(sizeof(*fi->fstates) * fi->fc);
01364                 memset(fi->fstates, RPMFILE_STATE_NORMAL, fi->fc);
01365             }
01366 
01367             if (fi->fc <= 0)                            break;
01368             if (ts->transFlags & RPMTRANS_FLAG_JUSTDB)  break;
01369         
01370             /*
01371              * Old format relocateable packages need the entire default
01372              * prefix stripped to form the cpio list, while all other packages
01373              * need the leading / stripped.
01374              */
01375             {   const char * p;
01376                 rc = hge(fi->h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &p, NULL);
01377                 fi->striplen = (rc ? strlen(p) + 1 : 1); 
01378             }
01379             fi->mapflags =
01380                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01381         
01382             if (headerIsEntry(fi->h, RPMTAG_ORIGBASENAMES))
01383                 buildOrigFileList(fi->h, &fi->apath, NULL);
01384             else
01385                 rpmBuildFileList(fi->h, &fi->apath, NULL);
01386         
01387             if (fi->fuser == NULL)
01388                 (void) hge(fi->h, RPMTAG_FILEUSERNAME, NULL,
01389                                 (void **) &fi->fuser, NULL);
01390             if (fi->fgroup == NULL)
01391                 (void) hge(fi->h, RPMTAG_FILEGROUPNAME, NULL,
01392                                 (void **) &fi->fgroup, NULL);
01393             if (fi->fuids == NULL)
01394                 fi->fuids = xcalloc(sizeof(*fi->fuids), fi->fc);
01395             if (fi->fgids == NULL)
01396                 fi->fgids = xcalloc(sizeof(*fi->fgids), fi->fc);
01397             rc = RPMRC_OK;
01398         }
01399         if (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE) {
01400             psm->scriptArg = psm->npkgs_installed - 1;
01401         
01402             /* Retrieve installed header. */
01403             rc = psmStage(psm, PSM_RPMDB_LOAD);
01404         }
01405         if (psm->goal == PSM_PKGSAVE) {
01406             /* Open output package for writing. */
01407             {   const char * bfmt = rpmGetPath("%{_repackage_name_fmt}", NULL);
01408                 const char * pkgbn =
01409                         headerSprintf(fi->h, bfmt, rpmTagTable, rpmHeaderFormats, NULL);
01410 
01411                 bfmt = _free(bfmt);
01412                 psm->pkgURL = rpmGenPath("%{?_repackage_root:%{_repackage_root}}",
01413                                          "%{?_repackage_dir:%{_repackage_dir}}",
01414                                         pkgbn);
01415                 pkgbn = _free(pkgbn);
01416                 (void) urlPath(psm->pkgURL, &psm->pkgfn);
01417                 psm->fd = Fopen(psm->pkgfn, "w.ufdio");
01418                 if (psm->fd == NULL || Ferror(psm->fd)) {
01419                     rc = RPMRC_FAIL;
01420                     break;
01421                 }
01422             }
01423         }
01424         break;
01425     case PSM_PRE:
01426         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01427 
01428         /* Change root directory if requested and not already done. */
01429         rc = psmStage(psm, PSM_CHROOT_IN);
01430 
01431         if (psm->goal == PSM_PKGINSTALL) {
01432             psm->scriptTag = RPMTAG_PREIN;
01433             psm->progTag = RPMTAG_PREINPROG;
01434 
01435             if (!(ts->transFlags & RPMTRANS_FLAG_NOTRIGGERPREIN)) {
01436                 /* XXX FIXME: implement %triggerprein. */
01437             }
01438 
01439             if (!(ts->transFlags & RPMTRANS_FLAG_NOPRE)) {
01440                 rc = psmStage(psm, PSM_SCRIPT);
01441                 if (rc) {
01442                     rpmError(RPMERR_SCRIPT,
01443                         _("%s: %s scriptlet failed (%d), skipping %s-%s-%s\n"),
01444                         psm->stepName, tag2sln(psm->scriptTag), rc,
01445                         fi->name, fi->version, fi->release);
01446                     break;
01447                 }
01448             }
01449         }
01450 
01451         if (psm->goal == PSM_PKGERASE) {
01452             psm->scriptTag = RPMTAG_PREUN;
01453             psm->progTag = RPMTAG_PREUNPROG;
01454             psm->sense = RPMSENSE_TRIGGERUN;
01455             psm->countCorrection = -1;
01456 
01457             if (!(ts->transFlags & RPMTRANS_FLAG_NOTRIGGERUN)) {
01458                 /* Run triggers in other package(s) this package sets off. */
01459                 rc = psmStage(psm, PSM_TRIGGERS);
01460                 if (rc) break;
01461 
01462                 /* Run triggers in this package other package(s) set off. */
01463                 rc = psmStage(psm, PSM_IMMED_TRIGGERS);
01464                 if (rc) break;
01465             }
01466 
01467             if (!(ts->transFlags & RPMTRANS_FLAG_NOPREUN))
01468                 rc = psmStage(psm, PSM_SCRIPT);
01469         }
01470         if (psm->goal == PSM_PKGSAVE) {
01471             /* Regenerate original header. */
01472             {   void * uh = NULL;
01473                 int_32 uht, uhc;
01474 
01475                 if (headerGetEntry(fi->h, RPMTAG_HEADERIMMUTABLE, &uht, &uh, &uhc)) {
01476                     psm->oh = headerCopyLoad(uh);
01477                     uh = hfd(uh, uht);
01478                 } else {
01479                     psm->oh = headerLink(fi->h);
01480                 }
01481             }
01482 
01483             /* Add remove transaction id to header. */
01484             if (psm->oh)
01485             {   int_32 tid = ts->id;
01486                 (void) headerAddEntry(psm->oh, RPMTAG_REMOVETID,
01487                         RPM_INT32_TYPE, &tid, 1);
01488             }
01489 
01490             /* Retrieve type of payload compression. */
01491             /*@-nullstate@*/    /* FIX: psm->oh may be NULL */
01492             rc = psmStage(psm, PSM_RPMIO_FLAGS);
01493             /*@=nullstate@*/
01494 
01495             /* Write the lead section into the package. */
01496             {   int archnum = -1;
01497                 int osnum = -1;
01498                 struct rpmlead lead;
01499 
01500 #ifndef DYING
01501                 rpmGetArchInfo(NULL, &archnum);
01502                 rpmGetOsInfo(NULL, &osnum);
01503 #endif
01504 
01505                 memset(&lead, 0, sizeof(lead));
01506                 /* XXX Set package version conditioned on noDirTokens. */
01507                 lead.major = 4;
01508                 lead.minor = 0;
01509                 lead.type = RPMLEAD_BINARY;
01510                 lead.archnum = archnum;
01511                 lead.osnum = osnum;
01512                 lead.signature_type = RPMSIGTYPE_HEADERSIG;
01513 
01514                 {   char buf[256];
01515                     sprintf(buf, "%s-%s-%s", fi->name, fi->version, fi->release);
01516                     strncpy(lead.name, buf, sizeof(lead.name));
01517                 }
01518 
01519                 rc = writeLead(psm->fd, &lead);
01520                 if (rc) {
01521                     rpmError(RPMERR_NOSPACE, _("Unable to write package: %s\n"),
01522                          Fstrerror(psm->fd));
01523                     rc = RPMRC_FAIL;
01524                     break;
01525                 }
01526             }
01527 
01528             /* Write the signature section into the package. */
01529             {   Header sig = headerRegenSigHeader(fi->h);
01530                 rc = rpmWriteSignature(psm->fd, sig);
01531                 sig = rpmFreeSignature(sig);
01532                 if (rc) break;
01533             }
01534 
01535             /* Write the metadata section into the package. */
01536             rc = headerWrite(psm->fd, psm->oh, HEADER_MAGIC_YES);
01537             if (rc) break;
01538         }
01539         break;
01540     case PSM_PROCESS:
01541         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01542 
01543         if (psm->goal == PSM_PKGINSTALL) {
01544             struct availablePackage * alp = fi->ap;
01545             int i;
01546 
01547             if (fi->fc <= 0)                            break;
01548             if (ts->transFlags & RPMTRANS_FLAG_JUSTDB)  break;
01549 
01550             for (i = 0; i < fi->fc; i++) {
01551                 uid_t uid;
01552                 gid_t gid;
01553 
01554                 uid = fi->uid;
01555                 gid = fi->gid;
01556                 if (fi->fuser && unameToUid(fi->fuser[i], &uid)) {
01557                     rpmMessage(RPMMESS_WARNING,
01558                         _("user %s does not exist - using root\n"),
01559                         fi->fuser[i]);
01560                     uid = 0;
01561                     /* XXX this diddles header memory. */
01562                     fi->fmodes[i] &= ~S_ISUID;  /* turn off the suid bit */
01563                 }
01564 
01565                 if (fi->fgroup && gnameToGid(fi->fgroup[i], &gid)) {
01566                     rpmMessage(RPMMESS_WARNING,
01567                         _("group %s does not exist - using root\n"),
01568                         fi->fgroup[i]);
01569                     gid = 0;
01570                     /* XXX this diddles header memory. */
01571                     fi->fmodes[i] &= ~S_ISGID;  /* turn off the sgid bit */
01572                 }
01573                 if (fi->fuids) fi->fuids[i] = uid;
01574                 if (fi->fgids) fi->fgids[i] = gid;
01575             }
01576 
01577             /* Retrieve type of payload compression. */
01578             rc = psmStage(psm, PSM_RPMIO_FLAGS);
01579 
01580             if (alp->fd == NULL) {      /* XXX can't happen */
01581                 rc = RPMRC_FAIL;
01582                 break;
01583             }
01584             /*@-nullpass@*/     /* LCL: alp->fd != NULL here. */
01585             psm->cfd = Fdopen(fdDup(Fileno(alp->fd)), psm->rpmio_flags);
01586             /*@=nullpass@*/
01587             if (psm->cfd == NULL) {     /* XXX can't happen */
01588                 rc = RPMRC_FAIL;
01589                 break;
01590             }
01591 
01592             rc = fsmSetup(fi->fsm, FSM_PKGINSTALL, ts, fi,
01593                         psm->cfd, NULL, &psm->failedFile);
01594             (void) fsmTeardown(fi->fsm);
01595 
01596             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01597             (void) Fclose(psm->cfd);
01598             psm->cfd = NULL;
01599             errno = saveerrno; /* XXX FIXME: Fclose with libio destroys errno */
01600 
01601             if (!rc)
01602                 rc = psmStage(psm, PSM_COMMIT);
01603 
01604             if (rc) {
01605                 rpmError(RPMERR_CPIO,
01606                         _("unpacking of archive failed%s%s: %s\n"),
01607                         (psm->failedFile != NULL ? _(" on file ") : ""),
01608                         (psm->failedFile != NULL ? psm->failedFile : ""),
01609                         cpioStrerror(rc));
01610                 rc = RPMRC_FAIL;
01611                 break;
01612             }
01613             psm->what = RPMCALLBACK_INST_PROGRESS;
01614             psm->amount = (fi->archiveSize ? fi->archiveSize : 100);
01615             psm->total = psm->amount;
01616             (void) psmStage(psm, PSM_NOTIFY);
01617         }
01618         if (psm->goal == PSM_PKGERASE) {
01619 
01620             if (fi->fc <= 0)                            break;
01621             if (ts->transFlags & RPMTRANS_FLAG_JUSTDB)  break;
01622             if (ts->transFlags & RPMTRANS_FLAG_APPLYONLY)       break;
01623 
01624             psm->what = RPMCALLBACK_UNINST_START;
01625             psm->amount = fi->fc;       /* XXX W2DO? looks wrong. */
01626             psm->total = fi->fc;
01627             (void) psmStage(psm, PSM_NOTIFY);
01628 
01629             rc = fsmSetup(fi->fsm, FSM_PKGERASE, ts, fi,
01630                         NULL, NULL, &psm->failedFile);
01631             (void) fsmTeardown(fi->fsm);
01632 
01633             psm->what = RPMCALLBACK_UNINST_STOP;
01634             psm->amount = 0;            /* XXX W2DO? looks wrong. */
01635             psm->total = fi->fc;
01636             (void) psmStage(psm, PSM_NOTIFY);
01637 
01638         }
01639         if (psm->goal == PSM_PKGSAVE) {
01640             fileAction * actions = fi->actions;
01641             fileAction action = fi->action;
01642 
01643             fi->action = FA_COPYOUT;
01644             fi->actions = NULL;
01645 
01646             if (psm->fd == NULL) {      /* XXX can't happen */
01647                 rc = RPMRC_FAIL;
01648                 break;
01649             }
01650             /*@-nullpass@*/     /* LCL: psm->fd != NULL here. */
01651             (void) Fflush(psm->fd);
01652             psm->cfd = Fdopen(fdDup(Fileno(psm->fd)), psm->rpmio_flags);
01653             /*@=nullpass@*/
01654             if (psm->cfd == NULL) {     /* XXX can't happen */
01655                 rc = RPMRC_FAIL;
01656                 break;
01657             }
01658 
01659             rc = fsmSetup(fi->fsm, FSM_PKGBUILD, ts, fi, psm->cfd,
01660                         NULL, &psm->failedFile);
01661             (void) fsmTeardown(fi->fsm);
01662 
01663             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01664             (void) Fclose(psm->cfd);
01665             psm->cfd = NULL;
01666             errno = saveerrno;
01667 
01668             fi->action = action;
01669             fi->actions = actions;
01670         }
01671         break;
01672     case PSM_POST:
01673         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01674 
01675         if (psm->goal == PSM_PKGINSTALL) {
01676             int_32 installTime = (int_32) time(NULL);
01677 
01678             if (fi->fstates != NULL && fi->fc > 0)
01679                 (void) headerAddEntry(fi->h, RPMTAG_FILESTATES, RPM_CHAR_TYPE,
01680                                 fi->fstates, fi->fc);
01681 
01682             (void) headerAddEntry(fi->h, RPMTAG_INSTALLTIME, RPM_INT32_TYPE,
01683                                 &installTime, 1);
01684 
01685             if (ts->transFlags & RPMTRANS_FLAG_MULTILIB) {
01686                 uint_32 multiLib, * newMultiLib, * p;
01687 
01688                 if (hge(fi->h, RPMTAG_MULTILIBS, NULL,
01689                                 (void **) &newMultiLib, NULL) &&
01690                     hge(psm->oh, RPMTAG_MULTILIBS, NULL,
01691                                 (void **) &p, NULL))
01692                 {
01693                     multiLib = *p;
01694                     multiLib |= *newMultiLib;
01695                     (void) hme(psm->oh, RPMTAG_MULTILIBS, RPM_INT32_TYPE,
01696                                       &multiLib, 1);
01697                 }
01698                 rc = mergeFiles(fi, psm->oh, fi->h);
01699                 if (rc) break;
01700             }
01701 
01702 
01703             /*
01704              * If this package has already been installed, remove it from
01705              * the database before adding the new one.
01706              */
01707             if (fi->record && !(ts->transFlags & RPMTRANS_FLAG_APPLYONLY)) {
01708                 rc = psmStage(psm, PSM_RPMDB_REMOVE);
01709                 if (rc) break;
01710             }
01711 
01712             rc = psmStage(psm, PSM_RPMDB_ADD);
01713             if (rc) break;
01714 
01715             psm->scriptTag = RPMTAG_POSTIN;
01716             psm->progTag = RPMTAG_POSTINPROG;
01717             psm->sense = RPMSENSE_TRIGGERIN;
01718             psm->countCorrection = 0;
01719 
01720             if (!(ts->transFlags & RPMTRANS_FLAG_NOPOST)) {
01721                 rc = psmStage(psm, PSM_SCRIPT);
01722                 if (rc) break;
01723             }
01724             if (!(ts->transFlags & RPMTRANS_FLAG_NOTRIGGERIN)) {
01725                 /* Run triggers in other package(s) this package sets off. */
01726                 rc = psmStage(psm, PSM_TRIGGERS);
01727                 if (rc) break;
01728 
01729                 /* Run triggers in this package other package(s) set off. */
01730                 rc = psmStage(psm, PSM_IMMED_TRIGGERS);
01731                 if (rc) break;
01732             }
01733 
01734             if (!(ts->transFlags & RPMTRANS_FLAG_APPLYONLY))
01735                 rc = markReplacedFiles(psm);
01736 
01737         }
01738         if (psm->goal == PSM_PKGERASE) {
01739 
01740             psm->scriptTag = RPMTAG_POSTUN;
01741             psm->progTag = RPMTAG_POSTUNPROG;
01742             psm->sense = RPMSENSE_TRIGGERPOSTUN;
01743             psm->countCorrection = -1;
01744 
01745             if (!(ts->transFlags & RPMTRANS_FLAG_NOPOSTUN)) {
01746                 rc = psmStage(psm, PSM_SCRIPT);
01747                 /* XXX WTFO? postun failures don't cause erasure failure. */
01748             }
01749 
01750             if (!(ts->transFlags & RPMTRANS_FLAG_NOTRIGGERPOSTUN)) {
01751                 /* Run triggers in other package(s) this package sets off. */
01752                 rc = psmStage(psm, PSM_TRIGGERS);
01753                 if (rc) break;
01754             }
01755 
01756             if (!(ts->transFlags & RPMTRANS_FLAG_APPLYONLY))
01757                 rc = psmStage(psm, PSM_RPMDB_REMOVE);
01758         }
01759         if (psm->goal == PSM_PKGSAVE) {
01760         }
01761 
01762         /* Restore root directory if changed. */
01763         (void) psmStage(psm, PSM_CHROOT_OUT);
01764         break;
01765     case PSM_UNDO:
01766         break;
01767     case PSM_FINI:
01768         /* Restore root directory if changed. */
01769         (void) psmStage(psm, PSM_CHROOT_OUT);
01770 
01771         if (psm->fd) {
01772             saveerrno = errno; /* XXX FIXME: Fclose with libio destroys errno */
01773             (void) Fclose(psm->fd);
01774             psm->fd = NULL;
01775             errno = saveerrno;
01776         }
01777 
01778         if (psm->goal == PSM_PKGSAVE) {
01779             if (!rc) {
01780                 rpmMessage(RPMMESS_VERBOSE, _("Wrote: %s\n"),
01781                         (psm->pkgURL ? psm->pkgURL : "???"));
01782             }
01783         }
01784 
01785         if (rc) {
01786             if (psm->failedFile)
01787                 rpmError(RPMERR_CPIO,
01788                         _("%s failed on file %s: %s\n"),
01789                         psm->stepName, psm->failedFile, cpioStrerror(rc));
01790             else
01791                 rpmError(RPMERR_CPIO, _("%s failed: %s\n"),
01792                         psm->stepName, cpioStrerror(rc));
01793         }
01794 
01795         if (fi->h && (psm->goal == PSM_PKGERASE || psm->goal == PSM_PKGSAVE))
01796             fi->h = headerFree(fi->h);
01797         psm->oh = headerFree(psm->oh);
01798         psm->pkgURL = _free(psm->pkgURL);
01799         psm->rpmio_flags = _free(psm->rpmio_flags);
01800         psm->failedFile = _free(psm->failedFile);
01801 
01802         fi->fgids = _free(fi->fgids);
01803         fi->fuids = _free(fi->fuids);
01804         fi->fgroup = hfd(fi->fgroup, -1);
01805         fi->fuser = hfd(fi->fuser, -1);
01806         fi->apath = _free(fi->apath);
01807         fi->fstates = _free(fi->fstates);
01808         break;
01809 
01810     case PSM_PKGINSTALL:
01811     case PSM_PKGERASE:
01812     case PSM_PKGSAVE:
01813         psm->goal = stage;
01814         psm->rc = RPMRC_OK;
01815         psm->stepName = pkgStageString(stage);
01816 
01817         rc = psmStage(psm, PSM_INIT);
01818         if (!rc) rc = psmStage(psm, PSM_PRE);
01819         if (!rc) rc = psmStage(psm, PSM_PROCESS);
01820         if (!rc) rc = psmStage(psm, PSM_POST);
01821         (void) psmStage(psm, PSM_FINI);
01822         break;
01823     case PSM_PKGCOMMIT:
01824         break;
01825 
01826     case PSM_CREATE:
01827         break;
01828     case PSM_NOTIFY:
01829         if (ts && ts->notify)
01830             (void) ts->notify(fi->h, psm->what, psm->amount, psm->total,
01831                 (fi->ap ? fi->ap->key : NULL), ts->notifyData);
01832         break;
01833     case PSM_DESTROY:
01834         break;
01835     case PSM_COMMIT:
01836         if (!(ts->transFlags & RPMTRANS_FLAG_PKGCOMMIT)) break;
01837         if (ts->transFlags & RPMTRANS_FLAG_APPLYONLY) break;
01838 
01839         rc = fsmSetup(fi->fsm, FSM_PKGCOMMIT, ts, fi,
01840                         NULL, NULL, &psm->failedFile);
01841         (void) fsmTeardown(fi->fsm);
01842         break;
01843 
01844     case PSM_CHROOT_IN:
01845         /* Change root directory if requested and not already done. */
01846         if (ts->rootDir && !ts->chrootDone && !psm->chrootDone) {
01847             static int _loaded = 0;
01848 
01849             /*
01850              * This loads all of the name services libraries, in case we
01851              * don't have access to them in the chroot().
01852              */
01853             if (!_loaded) {
01854                 (void)getpwnam("root");
01855                 endpwent();
01856                 _loaded++;
01857             }
01858 
01859             (void) chdir("/");
01860             /*@-unrecog@*/
01861             rc = chroot(ts->rootDir);
01862             /*@=unrecog@*/
01863             psm->chrootDone = ts->chrootDone = 1;
01864             if (ts->rpmdb != NULL) ts->rpmdb->db_chrootDone = 1;
01865             /*@-onlytrans@*/
01866             chroot_prefix = ts->rootDir;
01867             /*@=onlytrans@*/
01868         }
01869         break;
01870     case PSM_CHROOT_OUT:
01871         /* Restore root directory if changed. */
01872         if (psm->chrootDone) {
01873             /*@-unrecog@*/
01874             rc = chroot(".");
01875             /*@=unrecog@*/
01876             psm->chrootDone = ts->chrootDone = 0;
01877             if (ts->rpmdb != NULL) ts->rpmdb->db_chrootDone = 0;
01878             chroot_prefix = NULL;
01879             (void) chdir(ts->currDir);
01880         }
01881         break;
01882     case PSM_SCRIPT:
01883         rpmMessage(RPMMESS_DEBUG, _("%s: running %s script(s) (if any)\n"),
01884                 psm->stepName, tag2sln(psm->scriptTag));
01885         rc = runInstScript(psm);
01886         break;
01887     case PSM_TRIGGERS:
01888         /* Run triggers in other package(s) this package sets off. */
01889         rc = runTriggers(psm);
01890         break;
01891     case PSM_IMMED_TRIGGERS:
01892         /* Run triggers in this package other package(s) set off. */
01893         rc = runImmedTriggers(psm);
01894         break;
01895 
01896     case PSM_RPMIO_FLAGS:
01897     {   const char * payload_compressor = NULL;
01898         char * t;
01899 
01900         if (!hge(fi->h, RPMTAG_PAYLOADCOMPRESSOR, NULL,
01901                             (void **) &payload_compressor, NULL))
01902             payload_compressor = "gzip";
01903         psm->rpmio_flags = t = xmalloc(sizeof("w9.gzdio"));
01904         *t = '\0';
01905         t = stpcpy(t, ((psm->goal == PSM_PKGSAVE) ? "w9" : "r"));
01906         if (!strcmp(payload_compressor, "gzip"))
01907             t = stpcpy(t, ".gzdio");
01908         if (!strcmp(payload_compressor, "bzip2"))
01909             t = stpcpy(t, ".bzdio");
01910         rc = RPMRC_OK;
01911     }   break;
01912 
01913     case PSM_RPMDB_LOAD:
01914 assert(psm->mi == NULL);
01915         psm->mi = rpmdbInitIterator(ts->rpmdb, RPMDBI_PACKAGES,
01916                                 &fi->record, sizeof(fi->record));
01917 
01918         fi->h = rpmdbNextIterator(psm->mi);
01919         if (fi->h)
01920             fi->h = headerLink(fi->h);
01921 else {
01922 fprintf(stderr, "*** PSM_RDB_LOAD: header #%u not found\n", fi->record);
01923 }
01924         psm->mi = rpmdbFreeIterator(psm->mi);
01925         rc = (fi->h ? RPMRC_OK : RPMRC_FAIL);
01926         break;
01927     case PSM_RPMDB_ADD:
01928         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01929         rc = rpmdbAdd(ts->rpmdb, ts->id, fi->h);
01930         break;
01931     case PSM_RPMDB_REMOVE:
01932         if (ts->transFlags & RPMTRANS_FLAG_TEST)        break;
01933         rc = rpmdbRemove(ts->rpmdb, ts->id, fi->record);
01934         break;
01935 
01936     default:
01937         break;
01938     }
01939 
01940     /*@-nullstate@*/    /* FIX: psm->oh and psm->fi->h may be NULL. */
01941     return rc;
01942     /*@=nullstate@*/
01943 }

Generated at Thu Sep 6 11:25:41 2001 for rpm by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001