lib/rpmfi.c

Go to the documentation of this file.
00001 
00006 #include "system.h"
00007 
00008 #include <rpmio_internal.h>
00009 #include <rpmlib.h>
00010 
00011 #include "cpio.h"       /* XXX CPIO_FOO */
00012 #include "fsm.h"        /* XXX newFSM() */
00013 
00014 #include "rpmds.h"
00015 
00016 #define _RPMFI_INTERNAL
00017 #include "rpmfi.h"
00018 
00019 #include <selinux/selinux.h>
00020 
00021 #define _RPMTE_INTERNAL /* relocations */
00022 #include "rpmte.h"
00023 #include "rpmts.h"
00024 
00025 #include "misc.h"       /* XXX stripTrailingChar */
00026 #include "rpmmacro.h"   /* XXX rpmCleanPath */
00027 
00028 #include "debug.h"
00029 
00030 /*@access rpmte @*/
00031 
00032 /*@unchecked@*/
00033 int _rpmfi_debug = 0;
00034 
00035 rpmfi XrpmfiUnlink(rpmfi fi, const char * msg, const char * fn, unsigned ln)
00036 {
00037     if (fi == NULL) return NULL;
00038 /*@-modfilesys@*/
00039 if (_rpmfi_debug && msg != NULL)
00040 fprintf(stderr, "--> fi %p -- %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
00041 /*@=modfilesys@*/
00042     fi->nrefs--;
00043     return NULL;
00044 }
00045 
00046 rpmfi XrpmfiLink(rpmfi fi, const char * msg, const char * fn, unsigned ln)
00047 {
00048     if (fi == NULL) return NULL;
00049     fi->nrefs++;
00050 /*@-modfilesys@*/
00051 if (_rpmfi_debug && msg != NULL)
00052 fprintf(stderr, "--> fi %p ++ %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
00053 /*@=modfilesys@*/
00054     /*@-refcounttrans@*/ return fi; /*@=refcounttrans@*/
00055 }
00056 
00057 int rpmfiFC(rpmfi fi)
00058 {
00059     return (fi != NULL ? fi->fc : 0);
00060 }
00061 
00062 int rpmfiDC(rpmfi fi)
00063 {
00064     return (fi != NULL ? fi->dc : 0);
00065 }
00066 
00067 #ifdef  NOTYET
00068 int rpmfiDI(rpmfi fi)
00069 {
00070 }
00071 #endif
00072 
00073 int rpmfiFX(rpmfi fi)
00074 {
00075     return (fi != NULL ? fi->i : -1);
00076 }
00077 
00078 int rpmfiSetFX(rpmfi fi, int fx)
00079 {
00080     int i = -1;
00081 
00082     if (fi != NULL && fx >= 0 && fx < fi->fc) {
00083         i = fi->i;
00084         fi->i = fx;
00085 /*@-boundsread@*/
00086         fi->j = fi->dil[fi->i];
00087 /*@=boundsread@*/
00088     }
00089     return i;
00090 }
00091 
00092 int rpmfiDX(rpmfi fi)
00093 {
00094     return (fi != NULL ? fi->j : -1);
00095 }
00096 
00097 int rpmfiSetDX(rpmfi fi, int dx)
00098 {
00099     int j = -1;
00100 
00101     if (fi != NULL && dx >= 0 && dx < fi->dc) {
00102         j = fi->j;
00103         fi->j = dx;
00104     }
00105     return j;
00106 }
00107 
00108 const char * rpmfiBN(rpmfi fi)
00109 {
00110     const char * BN = NULL;
00111 
00112     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00113 /*@-boundsread@*/
00114         if (fi->bnl != NULL)
00115             BN = fi->bnl[fi->i];
00116 /*@=boundsread@*/
00117     }
00118     return BN;
00119 }
00120 
00121 const char * rpmfiDN(rpmfi fi)
00122 {
00123     const char * DN = NULL;
00124 
00125     if (fi != NULL && fi->j >= 0 && fi->j < fi->dc) {
00126 /*@-boundsread@*/
00127         if (fi->dnl != NULL)
00128             DN = fi->dnl[fi->j];
00129 /*@=boundsread@*/
00130     }
00131     return DN;
00132 }
00133 
00134 const char * rpmfiFN(rpmfi fi)
00135 {
00136     const char * FN = "";
00137 
00138     /*@-branchstate@*/
00139     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00140         char * t;
00141         if (fi->fn == NULL)
00142             fi->fn = xmalloc(fi->fnlen);
00143         FN = t = fi->fn;
00144 /*@-boundswrite@*/
00145         *t = '\0';
00146         t = stpcpy(t, fi->dnl[fi->dil[fi->i]]);
00147         t = stpcpy(t, fi->bnl[fi->i]);
00148 /*@=boundswrite@*/
00149     }
00150     /*@=branchstate@*/
00151     return FN;
00152 }
00153 
00154 int_32 rpmfiFFlags(rpmfi fi)
00155 {
00156     int_32 FFlags = 0;
00157 
00158     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00159 /*@-boundsread@*/
00160         if (fi->fflags != NULL)
00161             FFlags = fi->fflags[fi->i];
00162 /*@=boundsread@*/
00163     }
00164     return FFlags;
00165 }
00166 
00167 int_32 rpmfiVFlags(rpmfi fi)
00168 {
00169     int_32 VFlags = 0;
00170 
00171     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00172 /*@-boundsread@*/
00173         if (fi->vflags != NULL)
00174             VFlags = fi->vflags[fi->i];
00175 /*@=boundsread@*/
00176     }
00177     return VFlags;
00178 }
00179 
00180 int_16 rpmfiFMode(rpmfi fi)
00181 {
00182     int_16 fmode = 0;
00183 
00184     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00185 /*@-boundsread@*/
00186         if (fi->fmodes != NULL)
00187             fmode = fi->fmodes[fi->i];
00188 /*@=boundsread@*/
00189     }
00190     return fmode;
00191 }
00192 
00193 rpmfileState rpmfiFState(rpmfi fi)
00194 {
00195     rpmfileState fstate = RPMFILE_STATE_MISSING;
00196 
00197     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00198 /*@-boundsread@*/
00199         if (fi->fstates != NULL)
00200             fstate = fi->fstates[fi->i];
00201 /*@=boundsread@*/
00202     }
00203     return fstate;
00204 }
00205 
00206 const unsigned char * rpmfiMD5(rpmfi fi)
00207 {
00208     unsigned char * MD5 = NULL;
00209 
00210     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00211 /*@-boundsread@*/
00212         if (fi->md5s != NULL)
00213             MD5 = fi->md5s + (16 * fi->i);
00214 /*@=boundsread@*/
00215     }
00216     return MD5;
00217 }
00218 
00219 const char * rpmfiFLink(rpmfi fi)
00220 {
00221     const char * flink = NULL;
00222 
00223     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00224 /*@-boundsread@*/
00225         if (fi->flinks != NULL)
00226             flink = fi->flinks[fi->i];
00227 /*@=boundsread@*/
00228     }
00229     return flink;
00230 }
00231 
00232 int_32 rpmfiFSize(rpmfi fi)
00233 {
00234     int_32 fsize = 0;
00235 
00236     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00237 /*@-boundsread@*/
00238         if (fi->fsizes != NULL)
00239             fsize = fi->fsizes[fi->i];
00240 /*@=boundsread@*/
00241     }
00242     return fsize;
00243 }
00244 
00245 int_16 rpmfiFRdev(rpmfi fi)
00246 {
00247     int_16 frdev = 0;
00248 
00249     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00250 /*@-boundsread@*/
00251         if (fi->frdevs != NULL)
00252             frdev = fi->frdevs[fi->i];
00253 /*@=boundsread@*/
00254     }
00255     return frdev;
00256 }
00257 
00258 int_32 rpmfiFInode(rpmfi fi)
00259 {
00260     int_32 finode = 0;
00261 
00262     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00263 /*@-boundsread@*/
00264         if (fi->finodes != NULL)
00265             finode = fi->finodes[fi->i];
00266 /*@=boundsread@*/
00267     }
00268     return finode;
00269 }
00270 
00271 uint_32 rpmfiColor(rpmfi fi)
00272 {
00273     uint_32 color = 0;
00274 
00275     if (fi != NULL)
00276         /* XXX ignore all but lsnibble for now. */
00277         color = fi->color & 0xf;
00278     return color;
00279 }
00280 
00281 uint_32 rpmfiFColor(rpmfi fi)
00282 {
00283     uint_32 fcolor = 0;
00284 
00285     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00286 /*@-boundsread@*/
00287         if (fi->fcolors != NULL)
00288             /* XXX ignore all but lsnibble for now. */
00289             fcolor = (fi->fcolors[fi->i] & 0x0f);
00290 /*@=boundsread@*/
00291     }
00292     return fcolor;
00293 }
00294 
00295 const char * rpmfiFClass(rpmfi fi)
00296 {
00297     const char * fclass = NULL;
00298     int cdictx;
00299 
00300     if (fi != NULL && fi->fcdictx != NULL && fi->i >= 0 && fi->i < fi->fc) {
00301 /*@-boundsread@*/
00302         cdictx = fi->fcdictx[fi->i];
00303         if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
00304             fclass = fi->cdict[cdictx];
00305 /*@=boundsread@*/
00306     }
00307     return fclass;
00308 }
00309 
00310 const char * rpmfiFContext(rpmfi fi)
00311 {
00312     const char * fcontext = NULL;
00313 
00314     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00315 /*@-boundsread@*/
00316         if (fi->fcontexts != NULL)
00317             fcontext = fi->fcontexts[fi->i];
00318 /*@=boundsread@*/
00319     }
00320     return fcontext;
00321 }
00322 
00323 int_32 rpmfiFDepends(rpmfi fi, const int_32 ** fddictp)
00324 {
00325     int fddictx = -1;
00326     int fddictn = 0;
00327     const int_32 * fddict = NULL;
00328 
00329     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00330 /*@-boundsread@*/
00331         if (fi->fddictn != NULL)
00332             fddictn = fi->fddictn[fi->i];
00333         if (fddictn > 0 && fi->fddictx != NULL)
00334             fddictx = fi->fddictx[fi->i];
00335         if (fi->ddict != NULL && fddictx >= 0 && (fddictx+fddictn) <= fi->nddict)
00336             fddict = fi->ddict + fddictx;
00337 /*@=boundsread@*/
00338     }
00339 /*@-boundswrite -dependenttrans -onlytrans @*/
00340     if (fddictp)
00341         *fddictp = fddict;
00342 /*@=boundswrite =dependenttrans =onlytrans @*/
00343     return fddictn;
00344 }
00345 
00346 int_32 rpmfiFNlink(rpmfi fi)
00347 {
00348     int_32 nlink = 0;
00349 
00350     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00351         /* XXX rpm-2.3.12 has not RPMTAG_FILEINODES */
00352 /*@-boundsread@*/
00353         if (fi->finodes && fi->frdevs) {
00354             int_32 finode = fi->finodes[fi->i];
00355             int_16 frdev = fi->frdevs[fi->i];
00356             int j;
00357 
00358             for (j = 0; j < fi->fc; j++) {
00359                 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
00360                     nlink++;
00361             }
00362         }
00363 /*@=boundsread@*/
00364     }
00365     return nlink;
00366 }
00367 
00368 int_32 rpmfiFMtime(rpmfi fi)
00369 {
00370     int_32 fmtime = 0;
00371 
00372     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00373 /*@-boundsread@*/
00374         if (fi->fmtimes != NULL)
00375             fmtime = fi->fmtimes[fi->i];
00376 /*@=boundsread@*/
00377     }
00378     return fmtime;
00379 }
00380 
00381 const char * rpmfiFUser(rpmfi fi)
00382 {
00383     const char * fuser = NULL;
00384 
00385     /* XXX add support for ancient RPMTAG_FILEUIDS? */
00386     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00387 /*@-boundsread@*/
00388         if (fi->fuser != NULL)
00389             fuser = fi->fuser[fi->i];
00390 /*@=boundsread@*/
00391     }
00392     return fuser;
00393 }
00394 
00395 const char * rpmfiFGroup(rpmfi fi)
00396 {
00397     const char * fgroup = NULL;
00398 
00399     /* XXX add support for ancient RPMTAG_FILEGIDS? */
00400     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00401 /*@-boundsread@*/
00402         if (fi->fgroup != NULL)
00403             fgroup = fi->fgroup[fi->i];
00404 /*@=boundsread@*/
00405     }
00406     return fgroup;
00407 }
00408 
00409 int rpmfiNext(rpmfi fi)
00410 {
00411     int i = -1;
00412 
00413     if (fi != NULL && ++fi->i >= 0) {
00414         if (fi->i < fi->fc) {
00415             i = fi->i;
00416 /*@-boundsread@*/
00417             if (fi->dil != NULL)
00418                 fi->j = fi->dil[fi->i];
00419 /*@=boundsread@*/
00420         } else
00421             fi->i = -1;
00422 
00423 /*@-modfilesys @*/
00424 if (_rpmfi_debug  < 0 && i != -1)
00425 fprintf(stderr, "*** fi %p\t%s[%d] %s%s\n", fi, (fi->Type ? fi->Type : "?Type?"), i, (i >= 0 ? fi->dnl[fi->j] : ""), (i >= 0 ? fi->bnl[fi->i] : ""));
00426 /*@=modfilesys @*/
00427 
00428     }
00429 
00430     return i;
00431 }
00432 
00433 rpmfi rpmfiInit(rpmfi fi, int fx)
00434 {
00435     if (fi != NULL) {
00436         if (fx >= 0 && fx < fi->fc) {
00437             fi->i = fx - 1;
00438             fi->j = -1;
00439         }
00440     }
00441 
00442     /*@-refcounttrans@*/
00443     return fi;
00444     /*@=refcounttrans@*/
00445 }
00446 
00447 int rpmfiNextD(rpmfi fi)
00448 {
00449     int j = -1;
00450 
00451     if (fi != NULL && ++fi->j >= 0) {
00452         if (fi->j < fi->dc)
00453             j = fi->j;
00454         else
00455             fi->j = -1;
00456 
00457 /*@-modfilesys @*/
00458 if (_rpmfi_debug  < 0 && j != -1)
00459 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, (fi->Type ? fi->Type : "?Type?"), j);
00460 /*@=modfilesys @*/
00461 
00462     }
00463 
00464     return j;
00465 }
00466 
00467 rpmfi rpmfiInitD(rpmfi fi, int dx)
00468 {
00469     if (fi != NULL) {
00470         if (dx >= 0 && dx < fi->fc)
00471             fi->j = dx - 1;
00472         else
00473             fi = NULL;
00474     }
00475 
00476     /*@-refcounttrans@*/
00477     return fi;
00478     /*@=refcounttrans@*/
00479 }
00480 
00486 static /*@observer@*/
00487 const char *const ftstring (fileTypes ft)
00488         /*@*/
00489 {
00490     switch (ft) {
00491     case XDIR:  return "directory";
00492     case CDEV:  return "char dev";
00493     case BDEV:  return "block dev";
00494     case LINK:  return "link";
00495     case SOCK:  return "sock";
00496     case PIPE:  return "fifo/pipe";
00497     case REG:   return "file";
00498     default:    return "unknown file type";
00499     }
00500     /*@notreached@*/
00501 }
00502 
00503 fileTypes whatis(uint_16 mode)
00504 {
00505     if (S_ISDIR(mode))  return XDIR;
00506     if (S_ISCHR(mode))  return CDEV;
00507     if (S_ISBLK(mode))  return BDEV;
00508     if (S_ISLNK(mode))  return LINK;
00509 /*@-unrecog@*/
00510     if (S_ISSOCK(mode)) return SOCK;
00511 /*@=unrecog@*/
00512     if (S_ISFIFO(mode)) return PIPE;
00513     return REG;
00514 }
00515 
00516 /*@-boundsread@*/
00517 int rpmfiCompare(const rpmfi afi, const rpmfi bfi)
00518         /*@*/
00519 {
00520     fileTypes awhat = whatis(rpmfiFMode(afi));
00521     fileTypes bwhat = whatis(rpmfiFMode(bfi));
00522 
00523     if (awhat != bwhat) return 1;
00524 
00525     if (awhat == LINK) {
00526         const char * alink = rpmfiFLink(afi);
00527         const char * blink = rpmfiFLink(bfi);
00528         if (alink == blink) return 0;
00529         if (alink == NULL) return 1;
00530         if (blink == NULL) return -1;
00531         return strcmp(alink, blink);
00532     } else if (awhat == REG) {
00533         const unsigned char * amd5 = rpmfiMD5(afi);
00534         const unsigned char * bmd5 = rpmfiMD5(bfi);
00535         if (amd5 == bmd5) return 0;
00536         if (amd5 == NULL) return 1;
00537         if (bmd5 == NULL) return -1;
00538         return memcmp(amd5, bmd5, 16);
00539     }
00540 
00541     return 0;
00542 }
00543 /*@=boundsread@*/
00544 
00545 /*@-boundsread@*/
00546 fileAction rpmfiDecideFate(const rpmfi ofi, rpmfi nfi, int skipMissing)
00547 {
00548     const char * fn = rpmfiFN(nfi);
00549     int newFlags = rpmfiFFlags(nfi);
00550     char buffer[1024];
00551     fileTypes dbWhat, newWhat, diskWhat;
00552     struct stat sb;
00553     int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
00554 
00555     if (lstat(fn, &sb)) {
00556         /*
00557          * The file doesn't exist on the disk. Create it unless the new
00558          * package has marked it as missingok, or allfiles is requested.
00559          */
00560         if (skipMissing && (newFlags & RPMFILE_MISSINGOK)) {
00561             rpmMessage(RPMMESS_DEBUG, _("%s skipped due to missingok flag\n"),
00562                         fn);
00563             return FA_SKIP;
00564         } else {
00565             return FA_CREATE;
00566         }
00567     }
00568 
00569     diskWhat = whatis((int_16)sb.st_mode);
00570     dbWhat = whatis(rpmfiFMode(ofi));
00571     newWhat = whatis(rpmfiFMode(nfi));
00572 
00573     /*
00574      * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
00575      * them in older packages as well.
00576      */
00577     if (newWhat == XDIR)
00578         return FA_CREATE;
00579 
00580     if (diskWhat != newWhat)
00581         return save;
00582     else if (newWhat != dbWhat && diskWhat != dbWhat)
00583         return save;
00584     else if (dbWhat != newWhat)
00585         return FA_CREATE;
00586     else if (dbWhat != LINK && dbWhat != REG)
00587         return FA_CREATE;
00588 
00589     /*
00590      * This order matters - we'd prefer to CREATE the file if at all
00591      * possible in case something else (like the timestamp) has changed.
00592      */
00593     memset(buffer, 0, sizeof(buffer));
00594     if (dbWhat == REG) {
00595         const unsigned char * omd5, * nmd5;
00596         if (domd5(fn, buffer, 0, NULL))
00597             return FA_CREATE;   /* assume file has been removed */
00598         omd5 = rpmfiMD5(ofi);
00599         if (omd5 && !memcmp(omd5, buffer, 16))
00600             return FA_CREATE;   /* unmodified config file, replace. */
00601         nmd5 = rpmfiMD5(nfi);
00602 /*@-nullpass@*/
00603         if (omd5 && nmd5 && !memcmp(omd5, nmd5, 16))
00604             return FA_SKIP;     /* identical file, don't bother. */
00605 /*@=nullpass@*/
00606     } else /* dbWhat == LINK */ {
00607         const char * oFLink, * nFLink;
00608         if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
00609             return FA_CREATE;   /* assume file has been removed */
00610         oFLink = rpmfiFLink(ofi);
00611         if (oFLink && !strcmp(oFLink, buffer))
00612             return FA_CREATE;   /* unmodified config file, replace. */
00613         nFLink = rpmfiFLink(nfi);
00614 /*@-nullpass@*/
00615         if (oFLink && nFLink && !strcmp(oFLink, nFLink))
00616             return FA_SKIP;     /* identical file, don't bother. */
00617 /*@=nullpass@*/
00618     }
00619 
00620     /*
00621      * The config file on the disk has been modified, but
00622      * the ones in the two packages are different. It would
00623      * be nice if RPM was smart enough to at least try and
00624      * merge the difference ala CVS, but...
00625      */
00626     return save;
00627 }
00628 /*@=boundsread@*/
00629 
00630 /*@observer@*/
00631 const char *const rpmfiTypeString(rpmfi fi)
00632 {
00633     switch(rpmteType(fi->te)) {
00634     case TR_ADDED:      return " install";
00635     case TR_REMOVED:    return "   erase";
00636     default:            return "???";
00637     }
00638     /*@noteached@*/
00639 }
00640 
00641 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
00642 
00652 /*@-bounds@*/
00653 static
00654 Header relocateFileList(const rpmts ts, rpmfi fi,
00655                 Header origH, fileAction * actions)
00656         /*@globals h_errno, rpmGlobalMacroContext @*/
00657         /*@modifies ts, fi, origH, actions, rpmGlobalMacroContext @*/
00658 {
00659     rpmte p = rpmtsRelocateElement(ts);
00660     HGE_t hge = fi->hge;
00661     HAE_t hae = fi->hae;
00662     HME_t hme = fi->hme;
00663     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00664     static int _printed = 0;
00665     int allowBadRelocate = (rpmtsFilterFlags(ts) & RPMPROB_FILTER_FORCERELOCATE);
00666     rpmRelocation * relocations = NULL;
00667     int numRelocations;
00668     const char ** validRelocations;
00669     rpmTagType validType;
00670     int numValid;
00671     const char ** baseNames;
00672     const char ** dirNames;
00673     int_32 * dirIndexes;
00674     int_32 * newDirIndexes;
00675     int_32 fileCount;
00676     int_32 dirCount;
00677     uint_32 mydColor = rpmExpandNumeric("%{?_autorelocate_dcolor}");
00678     uint_32 * fFlags = NULL;
00679     uint_32 * fColors = NULL;
00680     uint_32 * dColors = NULL;
00681     uint_16 * fModes = NULL;
00682     Header h;
00683     int nrelocated = 0;
00684     int fileAlloced = 0;
00685     char * fn = NULL;
00686     int haveRelocatedFile = 0;
00687     int reldel = 0;
00688     int len;
00689     int i, j, xx;
00690 
00691     if (!hge(origH, RPMTAG_PREFIXES, &validType,
00692                         (void **) &validRelocations, &numValid))
00693         numValid = 0;
00694 
00695 assert(p != NULL);
00696     numRelocations = 0;
00697     if (p->relocs)
00698         while (p->relocs[numRelocations].newPath ||
00699                p->relocs[numRelocations].oldPath)
00700             numRelocations++;
00701 
00702     /*
00703      * If no relocations are specified (usually the case), then return the
00704      * original header. If there are prefixes, however, then INSTPREFIXES
00705      * should be added, but, since relocateFileList() can be called more
00706      * than once for the same header, don't bother if already present.
00707      */
00708     if (p->relocs == NULL || numRelocations == 0) {
00709         if (numValid) {
00710             if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES))
00711                 xx = hae(origH, RPMTAG_INSTPREFIXES,
00712                         validType, validRelocations, numValid);
00713             validRelocations = hfd(validRelocations, validType);
00714         }
00715         /* XXX FIXME multilib file actions need to be checked. */
00716         return headerLink(origH);
00717     }
00718 
00719     h = headerLink(origH);
00720 
00721     relocations = alloca(sizeof(*relocations) * numRelocations);
00722 
00723     /* Build sorted relocation list from raw relocations. */
00724     for (i = 0; i < numRelocations; i++) {
00725         char * t;
00726 
00727         /*
00728          * Default relocations (oldPath == NULL) are handled in the UI,
00729          * not rpmlib.
00730          */
00731         if (p->relocs[i].oldPath == NULL) continue; /* XXX can't happen */
00732 
00733         /* FIXME: Trailing /'s will confuse us greatly. Internal ones will 
00734            too, but those are more trouble to fix up. :-( */
00735         t = alloca_strdup(p->relocs[i].oldPath);
00736         /*@-branchstate@*/
00737         relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
00738             ? t
00739             : stripTrailingChar(t, '/');
00740         /*@=branchstate@*/
00741 
00742         /* An old path w/o a new path is valid, and indicates exclusion */
00743         if (p->relocs[i].newPath) {
00744             int del;
00745 
00746             t = alloca_strdup(p->relocs[i].newPath);
00747             /*@-branchstate@*/
00748             relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
00749                 ? t
00750                 : stripTrailingChar(t, '/');
00751             /*@=branchstate@*/
00752 
00753             /*@-nullpass@*/     /* FIX:  relocations[i].oldPath == NULL */
00754             /* Verify that the relocation's old path is in the header. */
00755             for (j = 0; j < numValid; j++) {
00756                 if (!strcmp(validRelocations[j], relocations[i].oldPath))
00757                     /*@innerbreak@*/ break;
00758             }
00759 
00760             /* XXX actions check prevents problem from being appended twice. */
00761             if (j == numValid && !allowBadRelocate && actions) {
00762                 rpmps ps = rpmtsProblems(ts);
00763                 rpmpsAppend(ps, RPMPROB_BADRELOCATE,
00764                         rpmteNEVR(p), rpmteKey(p),
00765                         relocations[i].oldPath, NULL, NULL, 0);
00766                 ps = rpmpsFree(ps);
00767             }
00768             del =
00769                 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
00770             /*@=nullpass@*/
00771 
00772             if (del > reldel)
00773                 reldel = del;
00774         } else {
00775             relocations[i].newPath = NULL;
00776         }
00777     }
00778 
00779     /* stupid bubble sort, but it's probably faster here */
00780     for (i = 0; i < numRelocations; i++) {
00781         int madeSwap;
00782         madeSwap = 0;
00783         for (j = 1; j < numRelocations; j++) {
00784             rpmRelocation tmpReloc;
00785             if (relocations[j - 1].oldPath == NULL || /* XXX can't happen */
00786                 relocations[j    ].oldPath == NULL || /* XXX can't happen */
00787         strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
00788                 /*@innercontinue@*/ continue;
00789             /*@-usereleased@*/ /* LCL: ??? */
00790             tmpReloc = relocations[j - 1];
00791             relocations[j - 1] = relocations[j];
00792             relocations[j] = tmpReloc;
00793             /*@=usereleased@*/
00794             madeSwap = 1;
00795         }
00796         if (!madeSwap) break;
00797     }
00798 
00799     if (!_printed) {
00800         _printed = 1;
00801         rpmMessage(RPMMESS_DEBUG, _("========== relocations\n"));
00802         for (i = 0; i < numRelocations; i++) {
00803             if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
00804             if (relocations[i].newPath == NULL)
00805                 rpmMessage(RPMMESS_DEBUG, _("%5d exclude  %s\n"),
00806                         i, relocations[i].oldPath);
00807             else
00808                 rpmMessage(RPMMESS_DEBUG, _("%5d relocate %s -> %s\n"),
00809                         i, relocations[i].oldPath, relocations[i].newPath);
00810         }
00811     }
00812 
00813     /* Add relocation values to the header */
00814     if (numValid) {
00815         const char ** actualRelocations;
00816         int numActual;
00817 
00818         actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
00819         numActual = 0;
00820         for (i = 0; i < numValid; i++) {
00821             for (j = 0; j < numRelocations; j++) {
00822                 if (relocations[j].oldPath == NULL || /* XXX can't happen */
00823                     strcmp(validRelocations[i], relocations[j].oldPath))
00824                     /*@innercontinue@*/ continue;
00825                 /* On install, a relocate to NULL means skip the path. */
00826                 if (relocations[j].newPath) {
00827                     actualRelocations[numActual] = relocations[j].newPath;
00828                     numActual++;
00829                 }
00830                 /*@innerbreak@*/ break;
00831             }
00832             if (j == numRelocations) {
00833                 actualRelocations[numActual] = validRelocations[i];
00834                 numActual++;
00835             }
00836         }
00837 
00838         if (numActual)
00839             xx = hae(h, RPMTAG_INSTPREFIXES, RPM_STRING_ARRAY_TYPE,
00840                        (void **) actualRelocations, numActual);
00841 
00842         actualRelocations = _free(actualRelocations);
00843         validRelocations = hfd(validRelocations, validType);
00844     }
00845 
00846     xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &baseNames, &fileCount);
00847     xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
00848     xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &dirNames, &dirCount);
00849     xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fFlags, NULL);
00850     xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fColors, NULL);
00851     xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fModes, NULL);
00852 
00853     dColors = alloca(dirCount * sizeof(*dColors));
00854     memset(dColors, 0, dirCount * sizeof(*dColors));
00855 
00856     newDirIndexes = alloca(sizeof(*newDirIndexes) * fileCount);
00857     memcpy(newDirIndexes, dirIndexes, sizeof(*newDirIndexes) * fileCount);
00858     dirIndexes = newDirIndexes;
00859 
00860     /*
00861      * For all relocations, we go through sorted file/relocation lists 
00862      * backwards so that /usr/local relocations take precedence over /usr 
00863      * ones.
00864      */
00865 
00866     /* Relocate individual paths. */
00867 
00868     for (i = fileCount - 1; i >= 0; i--) {
00869         fileTypes ft;
00870         int fnlen;
00871 
00872         len = reldel +
00873                 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
00874         /*@-branchstate@*/
00875         if (len >= fileAlloced) {
00876             fileAlloced = len * 2;
00877             fn = xrealloc(fn, fileAlloced);
00878         }
00879         /*@=branchstate@*/
00880 
00881 assert(fn != NULL);             /* XXX can't happen */
00882         *fn = '\0';
00883         fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
00884 
00885 if (fColors != NULL) {
00886 /* XXX pkgs may not have unique dirNames, so color all dirNames that match. */
00887 for (j = 0; j < dirCount; j++) {
00888 if (strcmp(dirNames[dirIndexes[i]], dirNames[j])) /*@innercontinue@*/ continue;
00889 dColors[j] |= fColors[i];
00890 }
00891 }
00892 
00893         /*
00894          * See if this file path needs relocating.
00895          */
00896         /*
00897          * XXX FIXME: Would a bsearch of the (already sorted) 
00898          * relocation list be a good idea?
00899          */
00900         for (j = numRelocations - 1; j >= 0; j--) {
00901             if (relocations[j].oldPath == NULL) /* XXX can't happen */
00902                 /*@innercontinue@*/ continue;
00903             len = strcmp(relocations[j].oldPath, "/")
00904                 ? strlen(relocations[j].oldPath)
00905                 : 0;
00906 
00907             if (fnlen < len)
00908                 /*@innercontinue@*/ continue;
00909             /*
00910              * Only subdirectories or complete file paths may be relocated. We
00911              * don't check for '\0' as our directory names all end in '/'.
00912              */
00913             if (!(fn[len] == '/' || fnlen == len))
00914                 /*@innercontinue@*/ continue;
00915 
00916             if (strncmp(relocations[j].oldPath, fn, len))
00917                 /*@innercontinue@*/ continue;
00918             /*@innerbreak@*/ break;
00919         }
00920         if (j < 0) continue;
00921 
00922 /*@-nullderef@*/ /* FIX: fModes may be NULL */
00923         ft = whatis(fModes[i]);
00924 /*@=nullderef@*/
00925 
00926         /* On install, a relocate to NULL means skip the path. */
00927         if (relocations[j].newPath == NULL) {
00928             if (ft == XDIR) {
00929                 /* Start with the parent, looking for directory to exclude. */
00930                 for (j = dirIndexes[i]; j < dirCount; j++) {
00931                     len = strlen(dirNames[j]) - 1;
00932                     while (len > 0 && dirNames[j][len-1] == '/') len--;
00933                     if (fnlen != len)
00934                         /*@innercontinue@*/ continue;
00935                     if (strncmp(fn, dirNames[j], fnlen))
00936                         /*@innercontinue@*/ continue;
00937                     /*@innerbreak@*/ break;
00938                 }
00939             }
00940             if (actions) {
00941                 actions[i] = FA_SKIPNSTATE;
00942                 rpmMessage(RPMMESS_DEBUG, _("excluding %s %s\n"),
00943                         ftstring(ft), fn);
00944             }
00945             continue;
00946         }
00947 
00948         /* Relocation on full paths only, please. */
00949         if (fnlen != len) continue;
00950 
00951         if (actions)
00952             rpmMessage(RPMMESS_DEBUG, _("relocating %s to %s\n"),
00953                     fn, relocations[j].newPath);
00954         nrelocated++;
00955 
00956         strcpy(fn, relocations[j].newPath);
00957         {   char * te = strrchr(fn, '/');
00958             if (te) {
00959                 if (te > fn) te++;      /* root is special */
00960                 fnlen = te - fn;
00961             } else
00962                 te = fn + strlen(fn);
00963             /*@-nullpass -nullderef@*/  /* LCL: te != NULL here. */
00964             if (strcmp(baseNames[i], te)) /* basename changed too? */
00965                 baseNames[i] = alloca_strdup(te);
00966             *te = '\0';                 /* terminate new directory name */
00967             /*@=nullpass =nullderef@*/
00968         }
00969 
00970         /* Does this directory already exist in the directory list? */
00971         for (j = 0; j < dirCount; j++) {
00972             if (fnlen != strlen(dirNames[j]))
00973                 /*@innercontinue@*/ continue;
00974             if (strncmp(fn, dirNames[j], fnlen))
00975                 /*@innercontinue@*/ continue;
00976             /*@innerbreak@*/ break;
00977         }
00978         
00979         if (j < dirCount) {
00980             dirIndexes[i] = j;
00981             continue;
00982         }
00983 
00984         /* Creating new paths is a pita */
00985         if (!haveRelocatedFile) {
00986             const char ** newDirList;
00987 
00988             haveRelocatedFile = 1;
00989             newDirList = xmalloc((dirCount + 1) * sizeof(*newDirList));
00990             for (j = 0; j < dirCount; j++)
00991                 newDirList[j] = alloca_strdup(dirNames[j]);
00992             dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
00993             dirNames = newDirList;
00994         } else {
00995             dirNames = xrealloc(dirNames, 
00996                                sizeof(*dirNames) * (dirCount + 1));
00997         }
00998 
00999         dirNames[dirCount] = alloca_strdup(fn);
01000         dirIndexes[i] = dirCount;
01001         dirCount++;
01002     }
01003 
01004     /* Finish off by relocating directories. */
01005     for (i = dirCount - 1; i >= 0; i--) {
01006         for (j = numRelocations - 1; j >= 0; j--) {
01007 
01008            /* XXX Don't autorelocate uncolored directories. */
01009            if (j == p->autorelocatex
01010             && (dColors[i] == 0 || !(dColors[i] & mydColor)))
01011                /*@innercontinue@*/ continue;
01012 
01013             if (relocations[j].oldPath == NULL) /* XXX can't happen */
01014                 /*@innercontinue@*/ continue;
01015             len = strcmp(relocations[j].oldPath, "/")
01016                 ? strlen(relocations[j].oldPath)
01017                 : 0;
01018 
01019             if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
01020                 /*@innercontinue@*/ continue;
01021 
01022             /*
01023              * Only subdirectories or complete file paths may be relocated. We
01024              * don't check for '\0' as our directory names all end in '/'.
01025              */
01026             if (dirNames[i][len] != '/')
01027                 /*@innercontinue@*/ continue;
01028 
01029             if (relocations[j].newPath) { /* Relocate the path */
01030                 const char * s = relocations[j].newPath;
01031                 char * t = alloca(strlen(s) + strlen(dirNames[i]) - len + 1);
01032                 size_t slen;
01033 
01034                 (void) stpcpy( stpcpy(t, s) , dirNames[i] + len);
01035 
01036                 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
01037                 (void) rpmCleanPath(t);
01038                 slen = strlen(t);
01039                 t[slen] = '/';
01040                 t[slen+1] = '\0';
01041 
01042                 if (actions)
01043                     rpmMessage(RPMMESS_DEBUG,
01044                         _("relocating directory %s to %s\n"), dirNames[i], t);
01045                 dirNames[i] = t;
01046                 nrelocated++;
01047             }
01048         }
01049     }
01050 
01051     /* Save original filenames in header and replace (relocated) filenames. */
01052     if (nrelocated) {
01053         int c;
01054         void * d;
01055         rpmTagType t;
01056 
01057         d = NULL;
01058         xx = hge(h, RPMTAG_BASENAMES, &t, &d, &c);
01059         xx = hae(h, RPMTAG_ORIGBASENAMES, t, d, c);
01060         d = hfd(d, t);
01061 
01062         d = NULL;
01063         xx = hge(h, RPMTAG_DIRNAMES, &t, &d, &c);
01064         xx = hae(h, RPMTAG_ORIGDIRNAMES, t, d, c);
01065         d = hfd(d, t);
01066 
01067         d = NULL;
01068         xx = hge(h, RPMTAG_DIRINDEXES, &t, &d, &c);
01069         xx = hae(h, RPMTAG_ORIGDIRINDEXES, t, d, c);
01070         d = hfd(d, t);
01071 
01072         xx = hme(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
01073                           baseNames, fileCount);
01074         fi->bnl = hfd(fi->bnl, RPM_STRING_ARRAY_TYPE);
01075         xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc);
01076 
01077         xx = hme(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
01078                           dirNames, dirCount);
01079         fi->dnl = hfd(fi->dnl, RPM_STRING_ARRAY_TYPE);
01080         xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
01081 
01082         xx = hme(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE,
01083                           dirIndexes, fileCount);
01084         xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
01085     }
01086 
01087     baseNames = hfd(baseNames, RPM_STRING_ARRAY_TYPE);
01088     dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
01089 /*@-dependenttrans@*/
01090     fn = _free(fn);
01091 /*@=dependenttrans@*/
01092 
01093     return h;
01094 }
01095 /*@=bounds@*/
01096 
01097 rpmfi rpmfiFree(rpmfi fi)
01098 {
01099     HFD_t hfd = headerFreeData;
01100 
01101     if (fi == NULL) return NULL;
01102 
01103     if (fi->nrefs > 1)
01104         return rpmfiUnlink(fi, fi->Type);
01105 
01106 /*@-modfilesys@*/
01107 if (_rpmfi_debug < 0)
01108 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
01109 /*@=modfilesys@*/
01110 
01111     /* Free pre- and post-transaction script and interpreter strings. */
01112     fi->pretrans = _free(fi->pretrans);
01113     fi->pretransprog = _free(fi->pretransprog);
01114     fi->posttrans = _free(fi->posttrans);
01115     fi->posttransprog = _free(fi->posttransprog);
01116 
01117     /*@-branchstate@*/
01118     if (fi->fc > 0) {
01119         fi->bnl = hfd(fi->bnl, -1);
01120         fi->dnl = hfd(fi->dnl, -1);
01121 
01122         fi->flinks = hfd(fi->flinks, -1);
01123         fi->flangs = hfd(fi->flangs, -1);
01124         fi->fmd5s = hfd(fi->fmd5s, -1);
01125         fi->md5s = _free(fi->md5s);
01126 
01127         fi->cdict = hfd(fi->cdict, -1);
01128 
01129         fi->fuser = hfd(fi->fuser, -1);
01130         fi->fgroup = hfd(fi->fgroup, -1);
01131 
01132         fi->fstates = _free(fi->fstates);
01133 
01134         /*@-evalorder@*/
01135         if (!fi->keep_header && fi->h == NULL) {
01136             fi->fmtimes = _free(fi->fmtimes);
01137             fi->fmodes = _free(fi->fmodes);
01138             fi->fflags = _free(fi->fflags);
01139             fi->vflags = _free(fi->vflags);
01140             fi->fsizes = _free(fi->fsizes);
01141             fi->frdevs = _free(fi->frdevs);
01142             fi->finodes = _free(fi->finodes);
01143             fi->dil = _free(fi->dil);
01144 
01145             fi->fcolors = _free(fi->fcolors);
01146             fi->fcdictx = _free(fi->fcdictx);
01147             fi->ddict = _free(fi->ddict);
01148             fi->fddictx = _free(fi->fddictx);
01149             fi->fddictn = _free(fi->fddictn);
01150 
01151         }
01152         /*@=evalorder@*/
01153     }
01154     /*@=branchstate@*/
01155 
01156     fi->fsm = freeFSM(fi->fsm);
01157 
01158     fi->fn = _free(fi->fn);
01159     fi->apath = _free(fi->apath);
01160     fi->fmapflags = _free(fi->fmapflags);
01161 
01162     fi->obnl = hfd(fi->obnl, -1);
01163     fi->odnl = hfd(fi->odnl, -1);
01164 
01165     fi->fcontexts = hfd(fi->fcontexts, -1);
01166 
01167     fi->actions = _free(fi->actions);
01168     fi->replacedSizes = _free(fi->replacedSizes);
01169     fi->replaced = _free(fi->replaced);
01170 
01171     fi->h = headerFree(fi->h);
01172 
01173     /*@-nullstate -refcounttrans -usereleased@*/
01174     (void) rpmfiUnlink(fi, fi->Type);
01175     memset(fi, 0, sizeof(*fi));         /* XXX trash and burn */
01176     fi = _free(fi);
01177     /*@=nullstate =refcounttrans =usereleased@*/
01178 
01179     return NULL;
01180 }
01181 
01187 static inline unsigned char nibble(char c)
01188         /*@*/
01189 {
01190     if (c >= '0' && c <= '9')
01191         return (c - '0');
01192     if (c >= 'A' && c <= 'F')
01193         return (c - 'A') + 10;
01194     if (c >= 'a' && c <= 'f')
01195         return (c - 'a') + 10;
01196     return 0;
01197 }
01198 
01199 #define _fdupe(_fi, _data)      \
01200     if ((_fi)->_data != NULL)   \
01201         (_fi)->_data = memcpy(xmalloc((_fi)->fc * sizeof(*(_fi)->_data)), \
01202                         (_fi)->_data, (_fi)->fc * sizeof(*(_fi)->_data))
01203 
01204 /* XXX Ick, not SEF. */
01205 #define _fdupestring(_h, _tag, _data) \
01206     if (hge((_h), (_tag), NULL, (void **) &(_data), NULL)) \
01207         _data = xstrdup(_data)
01208 
01209 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, int scareMem)
01210 {
01211     HGE_t hge =
01212         (scareMem ? (HGE_t) headerGetEntryMinMemory : (HGE_t) headerGetEntry);
01213     HFD_t hfd = headerFreeData;
01214     rpmte p;
01215     rpmfi fi = NULL;
01216     const char * Type;
01217     uint_32 * uip;
01218     int dnlmax, bnlmax;
01219     unsigned char * t;
01220     int len;
01221     int xx;
01222     int i;
01223 
01224     if (tagN == RPMTAG_BASENAMES) {
01225         Type = "Files";
01226     } else {
01227         Type = "?Type?";
01228         goto exit;
01229     }
01230 
01231     fi = xcalloc(1, sizeof(*fi));
01232     if (fi == NULL)     /* XXX can't happen */
01233         goto exit;
01234 
01235     fi->magic = RPMFIMAGIC;
01236     fi->Type = Type;
01237     fi->i = -1;
01238     fi->tagN = tagN;
01239 
01240     fi->hge = hge;
01241     fi->hae = (HAE_t) headerAddEntry;
01242     fi->hme = (HME_t) headerModifyEntry;
01243     fi->hre = (HRE_t) headerRemoveEntry;
01244     fi->hfd = headerFreeData;
01245 
01246     fi->h = (scareMem ? headerLink(h) : NULL);
01247 
01248     if (fi->fsm == NULL)
01249         fi->fsm = newFSM();
01250 
01251     /* 0 means unknown */
01252     xx = hge(h, RPMTAG_ARCHIVESIZE, NULL, (void **) &uip, NULL);
01253     fi->archivePos = 0;
01254     fi->archiveSize = (xx ? *uip : 0);
01255 
01256     /* Extract pre- and post-transaction script and interpreter strings. */
01257     _fdupestring(h, RPMTAG_PRETRANS, fi->pretrans);
01258     _fdupestring(h, RPMTAG_PRETRANSPROG, fi->pretransprog);
01259     _fdupestring(h, RPMTAG_POSTTRANS, fi->posttrans);
01260     _fdupestring(h, RPMTAG_POSTTRANSPROG, fi->posttransprog);
01261 
01262     if (!hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc)) {
01263         fi->fc = 0;
01264         fi->dc = 0;
01265         goto exit;
01266     }
01267     xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
01268     xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
01269     xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fi->fmodes, NULL);
01270     xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fi->fflags, NULL);
01271     xx = hge(h, RPMTAG_FILEVERIFYFLAGS, NULL, (void **) &fi->vflags, NULL);
01272     xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &fi->fsizes, NULL);
01273 
01274     xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fi->fcolors, NULL);
01275     fi->color = 0;
01276     if (fi->fcolors != NULL)
01277     for (i = 0; i < fi->fc; i++)
01278         fi->color |= fi->fcolors[i];
01279     xx = hge(h, RPMTAG_CLASSDICT, NULL, (void **) &fi->cdict, &fi->ncdict);
01280     xx = hge(h, RPMTAG_FILECLASS, NULL, (void **) &fi->fcdictx, NULL);
01281 
01282     xx = hge(h, RPMTAG_DEPENDSDICT, NULL, (void **) &fi->ddict, &fi->nddict);
01283     xx = hge(h, RPMTAG_FILEDEPENDSX, NULL, (void **) &fi->fddictx, NULL);
01284     xx = hge(h, RPMTAG_FILEDEPENDSN, NULL, (void **) &fi->fddictn, NULL);
01285 
01286     xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &fi->fstates, NULL);
01287     if (xx == 0 || fi->fstates == NULL)
01288         fi->fstates = xcalloc(fi->fc, sizeof(*fi->fstates));
01289     else
01290         _fdupe(fi, fstates);
01291 
01292     fi->action = FA_UNKNOWN;
01293     fi->flags = 0;
01294 
01295 if (fi->actions == NULL)
01296         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
01297 
01298     fi->keep_header = (scareMem ? 1 : 0);
01299 
01300     /* XXX TR_REMOVED needs CPIO_MAP_{ABSOLUTE,ADDDOT} CPIO_ALL_HARDLINKS */
01301     fi->mapflags =
01302                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01303 
01304     xx = hge(h, RPMTAG_FILELINKTOS, NULL, (void **) &fi->flinks, NULL);
01305     xx = hge(h, RPMTAG_FILELANGS, NULL, (void **) &fi->flangs, NULL);
01306 
01307     fi->fmd5s = NULL;
01308     xx = hge(h, RPMTAG_FILEMD5S, NULL, (void **) &fi->fmd5s, NULL);
01309 
01310     fi->md5s = NULL;
01311     if (fi->fmd5s) {
01312         t = xmalloc(fi->fc * 16);
01313         fi->md5s = t;
01314         for (i = 0; i < fi->fc; i++) {
01315             const char * fmd5;
01316             int j;
01317 
01318             fmd5 = fi->fmd5s[i];
01319             if (!(fmd5 && *fmd5 != '\0')) {
01320                 memset(t, 0, 16);
01321                 t += 16;
01322                 continue;
01323             }
01324             for (j = 0; j < 16; j++, t++, fmd5 += 2)
01325                 *t = (nibble(fmd5[0]) << 4) | nibble(fmd5[1]);
01326         }
01327         fi->fmd5s = hfd(fi->fmd5s, -1);
01328     }
01329 
01330     /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes, or fcontexts */
01331     xx = hge(h, RPMTAG_FILEMTIMES, NULL, (void **) &fi->fmtimes, NULL);
01332     xx = hge(h, RPMTAG_FILERDEVS, NULL, (void **) &fi->frdevs, NULL);
01333     xx = hge(h, RPMTAG_FILEINODES, NULL, (void **) &fi->finodes, NULL);
01334     xx = hge(h, RPMTAG_FILECONTEXTS, NULL, (void **) &fi->fcontexts, NULL);
01335 
01336     fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
01337 
01338     xx = hge(h, RPMTAG_FILEUSERNAME, NULL, (void **) &fi->fuser, NULL);
01339     xx = hge(h, RPMTAG_FILEGROUPNAME, NULL, (void **) &fi->fgroup, NULL);
01340 
01341     if (ts != NULL)
01342     if (fi != NULL)
01343     if ((p = rpmtsRelocateElement(ts)) != NULL && rpmteType(p) == TR_ADDED
01344      && !headerIsEntry(h, RPMTAG_SOURCEPACKAGE)
01345      && !headerIsEntry(h, RPMTAG_ORIGBASENAMES))
01346     {
01347         const char * fmt = rpmGetPath("%{?_autorelocate_path}", NULL);
01348         const char * errstr;
01349         char * newPath;
01350         Header foo;
01351 
01352         /* XXX error handling. */
01353         newPath = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
01354         fmt = _free(fmt);
01355 
01356 #if __ia64__
01357         /* XXX On ia64, change leading /emul/ix86 -> /emul/ia32, ick. */
01358         if (newPath != NULL && *newPath != '\0'
01359          && strlen(newPath) >= (sizeof("/emul/i386")-1)
01360          && newPath[0] == '/' && newPath[1] == 'e' && newPath[2] == 'm'
01361          && newPath[3] == 'u' && newPath[4] == 'l' && newPath[5] == '/'
01362          && newPath[6] == 'i' && newPath[8] == '8' && newPath[9] == '6')
01363         {
01364             newPath[7] = 'a';
01365             newPath[8] = '3';
01366             newPath[9] = '2';
01367         }
01368 #endif
01369  
01370         /* XXX Make sure autoreloc is not already specified. */
01371         i = p->nrelocs;
01372         if (newPath != NULL && *newPath != '\0' && p->relocs != NULL)
01373         for (i = 0; i < p->nrelocs; i++) {
01374 /*@-nullpass@*/ /* XXX {old,new}Path might be NULL */
01375            if (strcmp(p->relocs[i].oldPath, "/"))
01376                 continue;
01377            if (strcmp(p->relocs[i].newPath, newPath))
01378                 continue;
01379 /*@=nullpass@*/
01380            break;
01381         }
01382 
01383         /* XXX test for incompatible arch triggering autorelocation is dumb. */
01384         if (newPath != NULL && *newPath != '\0' && i == p->nrelocs
01385          && p->archScore == 0)
01386         {
01387 
01388             p->relocs =
01389                 xrealloc(p->relocs, (p->nrelocs + 2) * sizeof(*p->relocs));
01390             p->relocs[p->nrelocs].oldPath = xstrdup("/");
01391             p->relocs[p->nrelocs].newPath = xstrdup(newPath);
01392             p->autorelocatex = p->nrelocs;
01393             p->nrelocs++;
01394             p->relocs[p->nrelocs].oldPath = NULL;
01395             p->relocs[p->nrelocs].newPath = NULL;
01396         }
01397         newPath = _free(newPath);
01398 
01399 /* XXX DYING */
01400 if (fi->actions == NULL)
01401         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
01402         /*@-compdef@*/ /* FIX: fi-md5s undefined */
01403         foo = relocateFileList(ts, fi, h, fi->actions);
01404         /*@=compdef@*/
01405         fi->h = headerFree(fi->h);
01406         fi->h = headerLink(foo);
01407         foo = headerFree(foo);
01408     }
01409 
01410     if (!scareMem) {
01411         _fdupe(fi, fmtimes);
01412         _fdupe(fi, frdevs);
01413         _fdupe(fi, finodes);
01414         _fdupe(fi, fsizes);
01415         _fdupe(fi, fflags);
01416         _fdupe(fi, vflags);
01417         _fdupe(fi, fmodes);
01418         _fdupe(fi, dil);
01419 
01420         _fdupe(fi, fcolors);
01421         _fdupe(fi, fcdictx);
01422 
01423         if (fi->ddict != NULL)
01424             fi->ddict = memcpy(xmalloc(fi->nddict * sizeof(*fi->ddict)),
01425                         fi->ddict, fi->nddict * sizeof(*fi->ddict));
01426 
01427         _fdupe(fi, fddictx);
01428         _fdupe(fi, fddictn);
01429 
01430         fi->h = headerFree(fi->h);
01431     }
01432 
01433     dnlmax = -1;
01434     for (i = 0; i < fi->dc; i++) {
01435         if ((len = strlen(fi->dnl[i])) > dnlmax)
01436             dnlmax = len;
01437     }
01438     bnlmax = -1;
01439     for (i = 0; i < fi->fc; i++) {
01440         if ((len = strlen(fi->bnl[i])) > bnlmax)
01441             bnlmax = len;
01442     }
01443     fi->fnlen = dnlmax + bnlmax + 1;
01444     fi->fn = NULL;
01445 
01446     fi->dperms = 0755;
01447     fi->fperms = 0644;
01448 
01449 exit:
01450 /*@-modfilesys@*/
01451 if (_rpmfi_debug < 0)
01452 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
01453 /*@=modfilesys@*/
01454 
01455     /*@-compdef -nullstate@*/ /* FIX: rpmfi null annotations */
01456     return rpmfiLink(fi, (fi ? fi->Type : NULL));
01457     /*@=compdef =nullstate@*/
01458 }
01459 
01460 void rpmfiBuildFClasses(Header h,
01461         /*@out@*/ const char *** fclassp, /*@out@*/ int * fcp)
01462 {
01463     int scareMem = 0;
01464     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01465     const char * FClass;
01466     const char ** av;
01467     int ac;
01468     size_t nb;
01469     char * t;
01470 
01471     if ((ac = rpmfiFC(fi)) <= 0) {
01472         av = NULL;
01473         ac = 0;
01474         goto exit;
01475     }
01476 
01477     /* Compute size of file class argv array blob. */
01478     nb = (ac + 1) * sizeof(*av);
01479     fi = rpmfiInit(fi, 0);
01480     if (fi != NULL)
01481     while (rpmfiNext(fi) >= 0) {
01482         FClass = rpmfiFClass(fi);
01483         if (FClass && *FClass != '\0')
01484             nb += strlen(FClass);
01485         nb += 1;
01486     }
01487 
01488     /* Create and load file class argv array. */
01489     av = xmalloc(nb);
01490     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01491     ac = 0;
01492     fi = rpmfiInit(fi, 0);
01493     if (fi != NULL)
01494     while (rpmfiNext(fi) >= 0) {
01495         FClass = rpmfiFClass(fi);
01496         av[ac++] = t;
01497         if (FClass && *FClass != '\0')
01498             t = stpcpy(t, FClass);
01499         *t++ = '\0';
01500     }
01501     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01502     /*@=branchstate@*/
01503 
01504 exit:
01505     fi = rpmfiFree(fi);
01506     /*@-branchstate@*/
01507     if (fclassp)
01508         *fclassp = av;
01509     else
01510         av = _free(av);
01511     /*@=branchstate@*/
01512     if (fcp) *fcp = ac;
01513 }
01514 
01515 void rpmfiBuildFContexts(Header h,
01516         /*@out@*/ const char *** fcontextp, /*@out@*/ int * fcp)
01517 {
01518     int scareMem = 0;
01519     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01520     const char * fcontext;
01521     const char ** av;
01522     int ac;
01523     size_t nb;
01524     char * t;
01525 
01526     if ((ac = rpmfiFC(fi)) <= 0) {
01527         av = NULL;
01528         ac = 0;
01529         goto exit;
01530     }
01531 
01532     /* Compute size of argv array blob. */
01533     nb = (ac + 1) * sizeof(*av);
01534     fi = rpmfiInit(fi, 0);
01535     if (fi != NULL)
01536     while (rpmfiNext(fi) >= 0) {
01537         fcontext = rpmfiFContext(fi);
01538         if (fcontext && *fcontext != '\0')
01539             nb += strlen(fcontext);
01540         nb += 1;
01541     }
01542 
01543     /* Create and load argv array. */
01544     av = xmalloc(nb);
01545     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01546     ac = 0;
01547     fi = rpmfiInit(fi, 0);
01548     if (fi != NULL)
01549     while (rpmfiNext(fi) >= 0) {
01550         fcontext = rpmfiFContext(fi);
01551         av[ac++] = t;
01552         if (fcontext && *fcontext != '\0')
01553             t = stpcpy(t, fcontext);
01554         *t++ = '\0';
01555     }
01556     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01557     /*@=branchstate@*/
01558 
01559 exit:
01560     fi = rpmfiFree(fi);
01561     /*@-branchstate@*/
01562     if (fcontextp)
01563         *fcontextp = av;
01564     else
01565         av = _free(av);
01566     /*@=branchstate@*/
01567     if (fcp) *fcp = ac;
01568 }
01569 
01570 void rpmfiBuildFSContexts(Header h,
01571         /*@out@*/ const char *** fcontextp, /*@out@*/ int * fcp)
01572 {
01573     int scareMem = 0;
01574     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01575     const char ** av;
01576     int ac;
01577     size_t nb;
01578     char * t;
01579     char * fctxt = NULL;
01580     size_t fctxtlen = 0;
01581     int * fcnb;
01582 
01583     if ((ac = rpmfiFC(fi)) <= 0) {
01584         av = NULL;
01585         ac = 0;
01586         goto exit;
01587     }
01588 
01589     /* Compute size of argv array blob, concatenating file contexts. */
01590     nb = ac * sizeof(*fcnb);
01591     fcnb = memset(alloca(nb), 0, nb);
01592     ac = 0;
01593     fi = rpmfiInit(fi, 0);
01594     if (fi != NULL)
01595     while (rpmfiNext(fi) >= 0) {
01596         const char * fn = rpmfiFN(fi);
01597         security_context_t scon;
01598 
01599         fcnb[ac] = lgetfilecon(fn, &scon);
01600 /*@-branchstate@*/
01601         if (fcnb[ac] > 0) {
01602             fctxt = xrealloc(fctxt, fctxtlen + fcnb[ac]);
01603             memcpy(fctxt+fctxtlen, scon, fcnb[ac]);
01604             fctxtlen += fcnb[ac];
01605             freecon(scon);
01606         }
01607 /*@=branchstate@*/
01608         ac++;
01609     }
01610 
01611     /* Create and load argv array from concatenated file contexts. */
01612     nb = (ac + 1) * sizeof(*av) + fctxtlen;
01613     av = xmalloc(nb);
01614     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01615     if (fctxt != NULL && fctxtlen > 0)
01616         (void) memcpy(t, fctxt, fctxtlen);
01617     ac = 0;
01618     fi = rpmfiInit(fi, 0);
01619     if (fi != NULL)
01620     while (rpmfiNext(fi) >= 0) {
01621         av[ac] = "";
01622         if (fcnb[ac] > 0) {
01623             av[ac] = t;
01624             t += fcnb[ac];
01625         }
01626         ac++;
01627     }
01628     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01629 
01630 exit:
01631     fi = rpmfiFree(fi);
01632     /*@-branchstate@*/
01633     if (fcontextp)
01634         *fcontextp = av;
01635     else
01636         av = _free(av);
01637     /*@=branchstate@*/
01638     if (fcp) *fcp = ac;
01639 }
01640 
01641 void rpmfiBuildREContexts(Header h,
01642         /*@out@*/ const char *** fcontextp, /*@out@*/ int * fcp)
01643 {
01644     int scareMem = 0;
01645     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01646     const char ** av = NULL;
01647     const char * myfn = rpmGetPath("%{?__file_context_path}", NULL);
01648     int ac;
01649     size_t nb;
01650     char * t;
01651     char * fctxt = NULL;
01652     size_t fctxtlen = 0;
01653     int * fcnb;
01654 
01655     if ((ac = rpmfiFC(fi)) <= 0) {
01656         ac = 0;
01657         goto exit;
01658     }
01659 
01660     /* Read security context patterns. */
01661     matchpathcon_init(myfn);
01662 
01663     /* Compute size of argv array blob, concatenating file contexts. */
01664     nb = ac * sizeof(*fcnb);
01665     fcnb = memset(alloca(nb), 0, nb);
01666     ac = 0;
01667     fi = rpmfiInit(fi, 0);
01668     if (fi != NULL)
01669     while (rpmfiNext(fi) >= 0) {
01670         const char * fn = rpmfiFN(fi);
01671         mode_t fmode = rpmfiFMode(fi);
01672         security_context_t scon;
01673 
01674         if (matchpathcon(fn, fmode, &scon) == 0) {
01675             fcnb[ac] = strlen(scon) + 1;
01676 /*@-branchstate@*/
01677             if (fcnb[ac] > 0) {
01678                 fctxt = xrealloc(fctxt, fctxtlen + fcnb[ac]);
01679                 memcpy(fctxt+fctxtlen, scon, fcnb[ac]);
01680                 fctxtlen += fcnb[ac];
01681             }
01682             freecon(scon); 
01683 /*@=branchstate@*/
01684         }
01685         ac++;
01686     }
01687 
01688     /* Create and load argv array from concatenated file contexts. */
01689     nb = (ac + 1) * sizeof(*av) + fctxtlen;
01690     av = xmalloc(nb);
01691     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01692     (void) memcpy(t, fctxt, fctxtlen);
01693     ac = 0;
01694     fi = rpmfiInit(fi, 0);
01695     if (fi != NULL)
01696     while (rpmfiNext(fi) >= 0) {
01697         av[ac] = "";
01698         if (fcnb[ac] > 0) {
01699             av[ac] = t;
01700             t += fcnb[ac];
01701         }
01702         ac++;
01703     }
01704     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01705 
01706 exit:
01707     fi = rpmfiFree(fi);
01708     /*@-branchstate@*/
01709     if (fcontextp)
01710         *fcontextp = av;
01711     else
01712         av = _free(av);
01713     /*@=branchstate@*/
01714     if (fcp) *fcp = ac;
01715 }
01716 
01717 void rpmfiBuildFDeps(Header h, rpmTag tagN,
01718         /*@out@*/ const char *** fdepsp, /*@out@*/ int * fcp)
01719 {
01720     int scareMem = 0;
01721     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01722     rpmds ds = NULL;
01723     const char ** av;
01724     int ac;
01725     size_t nb;
01726     char * t;
01727     char deptype = 'R';
01728     char mydt;
01729     const char * DNEVR;
01730     const int_32 * ddict;
01731     unsigned ix;
01732     int ndx;
01733 
01734     if ((ac = rpmfiFC(fi)) <= 0) {
01735         av = NULL;
01736         ac = 0;
01737         goto exit;
01738     }
01739 
01740     if (tagN == RPMTAG_PROVIDENAME)
01741         deptype = 'P';
01742     else if (tagN == RPMTAG_REQUIRENAME)
01743         deptype = 'R';
01744 
01745     ds = rpmdsNew(h, tagN, scareMem);
01746 
01747     /* Compute size of file depends argv array blob. */
01748     nb = (ac + 1) * sizeof(*av);
01749     fi = rpmfiInit(fi, 0);
01750     if (fi != NULL)
01751     while (rpmfiNext(fi) >= 0) {
01752         ddict = NULL;
01753         ndx = rpmfiFDepends(fi, &ddict);
01754         if (ddict != NULL)
01755         while (ndx-- > 0) {
01756             ix = *ddict++;
01757             mydt = ((ix >> 24) & 0xff);
01758             if (mydt != deptype)
01759                 /*@innercontinue@*/ continue;
01760             ix &= 0x00ffffff;
01761             (void) rpmdsSetIx(ds, ix-1);
01762             if (rpmdsNext(ds) < 0)
01763                 /*@innercontinue@*/ continue;
01764             DNEVR = rpmdsDNEVR(ds);
01765             if (DNEVR != NULL)
01766                 nb += strlen(DNEVR+2) + 1;
01767         }
01768         nb += 1;
01769     }
01770 
01771     /* Create and load file depends argv array. */
01772     av = xmalloc(nb);
01773     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01774     ac = 0;
01775     /*@-branchstate@*/
01776     fi = rpmfiInit(fi, 0);
01777     if (fi != NULL)
01778     while (rpmfiNext(fi) >= 0) {
01779         av[ac++] = t;
01780         ddict = NULL;
01781         ndx = rpmfiFDepends(fi, &ddict);
01782         if (ddict != NULL)
01783         while (ndx-- > 0) {
01784             ix = *ddict++;
01785             mydt = ((ix >> 24) & 0xff);
01786             if (mydt != deptype)
01787                 /*@innercontinue@*/ continue;
01788             ix &= 0x00ffffff;
01789             (void) rpmdsSetIx(ds, ix-1);
01790             if (rpmdsNext(ds) < 0)
01791                 /*@innercontinue@*/ continue;
01792             DNEVR = rpmdsDNEVR(ds);
01793             if (DNEVR != NULL) {
01794                 t = stpcpy(t, DNEVR+2);
01795                 *t++ = ' ';
01796                 *t = '\0';
01797             }
01798         }
01799         *t++ = '\0';
01800     }
01801     /*@=branchstate@*/
01802     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01803 
01804 exit:
01805     fi = rpmfiFree(fi);
01806     ds = rpmdsFree(ds);
01807     /*@-branchstate@*/
01808     if (fdepsp)
01809         *fdepsp = av;
01810     else
01811         av = _free(av);
01812     /*@=branchstate@*/
01813     if (fcp) *fcp = ac;
01814 }

Generated on Tue Aug 14 19:42:09 2007 for rpm by  doxygen 1.5.2