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         /* XXX avoid md5 on sparse /var/log/lastlog file. */
00597         if (strcmp(fn, "/var/log/lastlog"))
00598         if (domd5(fn, buffer, 0, NULL))
00599             return FA_CREATE;   /* assume file has been removed */
00600         omd5 = rpmfiMD5(ofi);
00601         if (omd5 && !memcmp(omd5, buffer, 16))
00602             return FA_CREATE;   /* unmodified config file, replace. */
00603         nmd5 = rpmfiMD5(nfi);
00604 /*@-nullpass@*/
00605         if (omd5 && nmd5 && !memcmp(omd5, nmd5, 16))
00606             return FA_SKIP;     /* identical file, don't bother. */
00607 /*@=nullpass@*/
00608     } else /* dbWhat == LINK */ {
00609         const char * oFLink, * nFLink;
00610         if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
00611             return FA_CREATE;   /* assume file has been removed */
00612         oFLink = rpmfiFLink(ofi);
00613         if (oFLink && !strcmp(oFLink, buffer))
00614             return FA_CREATE;   /* unmodified config file, replace. */
00615         nFLink = rpmfiFLink(nfi);
00616 /*@-nullpass@*/
00617         if (oFLink && nFLink && !strcmp(oFLink, nFLink))
00618             return FA_SKIP;     /* identical file, don't bother. */
00619 /*@=nullpass@*/
00620     }
00621 
00622     /*
00623      * The config file on the disk has been modified, but
00624      * the ones in the two packages are different. It would
00625      * be nice if RPM was smart enough to at least try and
00626      * merge the difference ala CVS, but...
00627      */
00628     return save;
00629 }
00630 /*@=boundsread@*/
00631 
00632 /*@observer@*/
00633 const char *const rpmfiTypeString(rpmfi fi)
00634 {
00635     switch(rpmteType(fi->te)) {
00636     case TR_ADDED:      return " install";
00637     case TR_REMOVED:    return "   erase";
00638     default:            return "???";
00639     }
00640     /*@noteached@*/
00641 }
00642 
00643 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
00644 
00654 /*@-bounds@*/
00655 static
00656 Header relocateFileList(const rpmts ts, rpmfi fi,
00657                 Header origH, fileAction * actions)
00658         /*@globals h_errno, rpmGlobalMacroContext @*/
00659         /*@modifies ts, fi, origH, actions, rpmGlobalMacroContext @*/
00660 {
00661     rpmte p = rpmtsRelocateElement(ts);
00662     HGE_t hge = fi->hge;
00663     HAE_t hae = fi->hae;
00664     HME_t hme = fi->hme;
00665     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00666     static int _printed = 0;
00667     int allowBadRelocate = (rpmtsFilterFlags(ts) & RPMPROB_FILTER_FORCERELOCATE);
00668     rpmRelocation * relocations = NULL;
00669     int numRelocations;
00670     const char ** validRelocations;
00671     rpmTagType validType;
00672     int numValid;
00673     const char ** baseNames;
00674     const char ** dirNames;
00675     int_32 * dirIndexes;
00676     int_32 * newDirIndexes;
00677     int_32 fileCount;
00678     int_32 dirCount;
00679     uint_32 mydColor = rpmExpandNumeric("%{?_autorelocate_dcolor}");
00680     uint_32 * fFlags = NULL;
00681     uint_32 * fColors = NULL;
00682     uint_32 * dColors = NULL;
00683     uint_16 * fModes = NULL;
00684     Header h;
00685     int nrelocated = 0;
00686     int fileAlloced = 0;
00687     char * fn = NULL;
00688     int haveRelocatedFile = 0;
00689     int reldel = 0;
00690     int len;
00691     int i, j, xx;
00692 
00693     if (!hge(origH, RPMTAG_PREFIXES, &validType,
00694                         (void **) &validRelocations, &numValid))
00695         numValid = 0;
00696 
00697 assert(p != NULL);
00698     numRelocations = 0;
00699     if (p->relocs)
00700         while (p->relocs[numRelocations].newPath ||
00701                p->relocs[numRelocations].oldPath)
00702             numRelocations++;
00703 
00704     /*
00705      * If no relocations are specified (usually the case), then return the
00706      * original header. If there are prefixes, however, then INSTPREFIXES
00707      * should be added, but, since relocateFileList() can be called more
00708      * than once for the same header, don't bother if already present.
00709      */
00710     if (p->relocs == NULL || numRelocations == 0) {
00711         if (numValid) {
00712             if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES))
00713                 xx = hae(origH, RPMTAG_INSTPREFIXES,
00714                         validType, validRelocations, numValid);
00715             validRelocations = hfd(validRelocations, validType);
00716         }
00717         /* XXX FIXME multilib file actions need to be checked. */
00718         return headerLink(origH);
00719     }
00720 
00721     h = headerLink(origH);
00722 
00723     relocations = alloca(sizeof(*relocations) * numRelocations);
00724 
00725     /* Build sorted relocation list from raw relocations. */
00726     for (i = 0; i < numRelocations; i++) {
00727         char * t;
00728 
00729         /*
00730          * Default relocations (oldPath == NULL) are handled in the UI,
00731          * not rpmlib.
00732          */
00733         if (p->relocs[i].oldPath == NULL) continue; /* XXX can't happen */
00734 
00735         /* FIXME: Trailing /'s will confuse us greatly. Internal ones will 
00736            too, but those are more trouble to fix up. :-( */
00737         t = alloca_strdup(p->relocs[i].oldPath);
00738         /*@-branchstate@*/
00739         relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
00740             ? t
00741             : stripTrailingChar(t, '/');
00742         /*@=branchstate@*/
00743 
00744         /* An old path w/o a new path is valid, and indicates exclusion */
00745         if (p->relocs[i].newPath) {
00746             int del;
00747 
00748             t = alloca_strdup(p->relocs[i].newPath);
00749             /*@-branchstate@*/
00750             relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
00751                 ? t
00752                 : stripTrailingChar(t, '/');
00753             /*@=branchstate@*/
00754 
00755             /*@-nullpass@*/     /* FIX:  relocations[i].oldPath == NULL */
00756             /* Verify that the relocation's old path is in the header. */
00757             for (j = 0; j < numValid; j++) {
00758                 if (!strcmp(validRelocations[j], relocations[i].oldPath))
00759                     /*@innerbreak@*/ break;
00760             }
00761 
00762             /* XXX actions check prevents problem from being appended twice. */
00763             if (j == numValid && !allowBadRelocate && actions) {
00764                 rpmps ps = rpmtsProblems(ts);
00765                 rpmpsAppend(ps, RPMPROB_BADRELOCATE,
00766                         rpmteNEVR(p), rpmteKey(p),
00767                         relocations[i].oldPath, NULL, NULL, 0);
00768                 ps = rpmpsFree(ps);
00769             }
00770             del =
00771                 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
00772             /*@=nullpass@*/
00773 
00774             if (del > reldel)
00775                 reldel = del;
00776         } else {
00777             relocations[i].newPath = NULL;
00778         }
00779     }
00780 
00781     /* stupid bubble sort, but it's probably faster here */
00782     for (i = 0; i < numRelocations; i++) {
00783         int madeSwap;
00784         madeSwap = 0;
00785         for (j = 1; j < numRelocations; j++) {
00786             rpmRelocation tmpReloc;
00787             if (relocations[j - 1].oldPath == NULL || /* XXX can't happen */
00788                 relocations[j    ].oldPath == NULL || /* XXX can't happen */
00789         strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
00790                 /*@innercontinue@*/ continue;
00791             /*@-usereleased@*/ /* LCL: ??? */
00792             tmpReloc = relocations[j - 1];
00793             relocations[j - 1] = relocations[j];
00794             relocations[j] = tmpReloc;
00795             /*@=usereleased@*/
00796             madeSwap = 1;
00797         }
00798         if (!madeSwap) break;
00799     }
00800 
00801     if (!_printed) {
00802         _printed = 1;
00803         rpmMessage(RPMMESS_DEBUG, _("========== relocations\n"));
00804         for (i = 0; i < numRelocations; i++) {
00805             if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
00806             if (relocations[i].newPath == NULL)
00807                 rpmMessage(RPMMESS_DEBUG, _("%5d exclude  %s\n"),
00808                         i, relocations[i].oldPath);
00809             else
00810                 rpmMessage(RPMMESS_DEBUG, _("%5d relocate %s -> %s\n"),
00811                         i, relocations[i].oldPath, relocations[i].newPath);
00812         }
00813     }
00814 
00815     /* Add relocation values to the header */
00816     if (numValid) {
00817         const char ** actualRelocations;
00818         int numActual;
00819 
00820         actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
00821         numActual = 0;
00822         for (i = 0; i < numValid; i++) {
00823             for (j = 0; j < numRelocations; j++) {
00824                 if (relocations[j].oldPath == NULL || /* XXX can't happen */
00825                     strcmp(validRelocations[i], relocations[j].oldPath))
00826                     /*@innercontinue@*/ continue;
00827                 /* On install, a relocate to NULL means skip the path. */
00828                 if (relocations[j].newPath) {
00829                     actualRelocations[numActual] = relocations[j].newPath;
00830                     numActual++;
00831                 }
00832                 /*@innerbreak@*/ break;
00833             }
00834             if (j == numRelocations) {
00835                 actualRelocations[numActual] = validRelocations[i];
00836                 numActual++;
00837             }
00838         }
00839 
00840         if (numActual)
00841             xx = hae(h, RPMTAG_INSTPREFIXES, RPM_STRING_ARRAY_TYPE,
00842                        (void **) actualRelocations, numActual);
00843 
00844         actualRelocations = _free(actualRelocations);
00845         validRelocations = hfd(validRelocations, validType);
00846     }
00847 
00848     xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &baseNames, &fileCount);
00849     xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
00850     xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &dirNames, &dirCount);
00851     xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fFlags, NULL);
00852     xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fColors, NULL);
00853     xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fModes, NULL);
00854 
00855     dColors = alloca(dirCount * sizeof(*dColors));
00856     memset(dColors, 0, dirCount * sizeof(*dColors));
00857 
00858     newDirIndexes = alloca(sizeof(*newDirIndexes) * fileCount);
00859     memcpy(newDirIndexes, dirIndexes, sizeof(*newDirIndexes) * fileCount);
00860     dirIndexes = newDirIndexes;
00861 
00862     /*
00863      * For all relocations, we go through sorted file/relocation lists 
00864      * backwards so that /usr/local relocations take precedence over /usr 
00865      * ones.
00866      */
00867 
00868     /* Relocate individual paths. */
00869 
00870     for (i = fileCount - 1; i >= 0; i--) {
00871         fileTypes ft;
00872         int fnlen;
00873 
00874         len = reldel +
00875                 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
00876         /*@-branchstate@*/
00877         if (len >= fileAlloced) {
00878             fileAlloced = len * 2;
00879             fn = xrealloc(fn, fileAlloced);
00880         }
00881         /*@=branchstate@*/
00882 
00883 assert(fn != NULL);             /* XXX can't happen */
00884         *fn = '\0';
00885         fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
00886 
00887 if (fColors != NULL) {
00888 /* XXX pkgs may not have unique dirNames, so color all dirNames that match. */
00889 for (j = 0; j < dirCount; j++) {
00890 if (strcmp(dirNames[dirIndexes[i]], dirNames[j])) /*@innercontinue@*/ continue;
00891 dColors[j] |= fColors[i];
00892 }
00893 }
00894 
00895         /*
00896          * See if this file path needs relocating.
00897          */
00898         /*
00899          * XXX FIXME: Would a bsearch of the (already sorted) 
00900          * relocation list be a good idea?
00901          */
00902         for (j = numRelocations - 1; j >= 0; j--) {
00903             if (relocations[j].oldPath == NULL) /* XXX can't happen */
00904                 /*@innercontinue@*/ continue;
00905             len = strcmp(relocations[j].oldPath, "/")
00906                 ? strlen(relocations[j].oldPath)
00907                 : 0;
00908 
00909             if (fnlen < len)
00910                 /*@innercontinue@*/ continue;
00911             /*
00912              * Only subdirectories or complete file paths may be relocated. We
00913              * don't check for '\0' as our directory names all end in '/'.
00914              */
00915             if (!(fn[len] == '/' || fnlen == len))
00916                 /*@innercontinue@*/ continue;
00917 
00918             if (strncmp(relocations[j].oldPath, fn, len))
00919                 /*@innercontinue@*/ continue;
00920             /*@innerbreak@*/ break;
00921         }
00922         if (j < 0) continue;
00923 
00924 /*@-nullderef@*/ /* FIX: fModes may be NULL */
00925         ft = whatis(fModes[i]);
00926 /*@=nullderef@*/
00927 
00928         /* On install, a relocate to NULL means skip the path. */
00929         if (relocations[j].newPath == NULL) {
00930             if (ft == XDIR) {
00931                 /* Start with the parent, looking for directory to exclude. */
00932                 for (j = dirIndexes[i]; j < dirCount; j++) {
00933                     len = strlen(dirNames[j]) - 1;
00934                     while (len > 0 && dirNames[j][len-1] == '/') len--;
00935                     if (fnlen != len)
00936                         /*@innercontinue@*/ continue;
00937                     if (strncmp(fn, dirNames[j], fnlen))
00938                         /*@innercontinue@*/ continue;
00939                     /*@innerbreak@*/ break;
00940                 }
00941             }
00942             if (actions) {
00943                 actions[i] = FA_SKIPNSTATE;
00944                 rpmMessage(RPMMESS_DEBUG, _("excluding %s %s\n"),
00945                         ftstring(ft), fn);
00946             }
00947             continue;
00948         }
00949 
00950         /* Relocation on full paths only, please. */
00951         if (fnlen != len) continue;
00952 
00953         if (actions)
00954             rpmMessage(RPMMESS_DEBUG, _("relocating %s to %s\n"),
00955                     fn, relocations[j].newPath);
00956         nrelocated++;
00957 
00958         strcpy(fn, relocations[j].newPath);
00959         {   char * te = strrchr(fn, '/');
00960             if (te) {
00961                 if (te > fn) te++;      /* root is special */
00962                 fnlen = te - fn;
00963             } else
00964                 te = fn + strlen(fn);
00965             /*@-nullpass -nullderef@*/  /* LCL: te != NULL here. */
00966             if (strcmp(baseNames[i], te)) /* basename changed too? */
00967                 baseNames[i] = alloca_strdup(te);
00968             *te = '\0';                 /* terminate new directory name */
00969             /*@=nullpass =nullderef@*/
00970         }
00971 
00972         /* Does this directory already exist in the directory list? */
00973         for (j = 0; j < dirCount; j++) {
00974             if (fnlen != strlen(dirNames[j]))
00975                 /*@innercontinue@*/ continue;
00976             if (strncmp(fn, dirNames[j], fnlen))
00977                 /*@innercontinue@*/ continue;
00978             /*@innerbreak@*/ break;
00979         }
00980         
00981         if (j < dirCount) {
00982             dirIndexes[i] = j;
00983             continue;
00984         }
00985 
00986         /* Creating new paths is a pita */
00987         if (!haveRelocatedFile) {
00988             const char ** newDirList;
00989 
00990             haveRelocatedFile = 1;
00991             newDirList = xmalloc((dirCount + 1) * sizeof(*newDirList));
00992             for (j = 0; j < dirCount; j++)
00993                 newDirList[j] = alloca_strdup(dirNames[j]);
00994             dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
00995             dirNames = newDirList;
00996         } else {
00997             dirNames = xrealloc(dirNames, 
00998                                sizeof(*dirNames) * (dirCount + 1));
00999         }
01000 
01001         dirNames[dirCount] = alloca_strdup(fn);
01002         dirIndexes[i] = dirCount;
01003         dirCount++;
01004     }
01005 
01006     /* Finish off by relocating directories. */
01007     for (i = dirCount - 1; i >= 0; i--) {
01008         for (j = numRelocations - 1; j >= 0; j--) {
01009 
01010            /* XXX Don't autorelocate uncolored directories. */
01011            if (j == p->autorelocatex
01012             && (dColors[i] == 0 || !(dColors[i] & mydColor)))
01013                /*@innercontinue@*/ continue;
01014 
01015             if (relocations[j].oldPath == NULL) /* XXX can't happen */
01016                 /*@innercontinue@*/ continue;
01017             len = strcmp(relocations[j].oldPath, "/")
01018                 ? strlen(relocations[j].oldPath)
01019                 : 0;
01020 
01021             if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
01022                 /*@innercontinue@*/ continue;
01023 
01024             /*
01025              * Only subdirectories or complete file paths may be relocated. We
01026              * don't check for '\0' as our directory names all end in '/'.
01027              */
01028             if (dirNames[i][len] != '/')
01029                 /*@innercontinue@*/ continue;
01030 
01031             if (relocations[j].newPath) { /* Relocate the path */
01032                 const char * s = relocations[j].newPath;
01033                 char * t = alloca(strlen(s) + strlen(dirNames[i]) - len + 1);
01034                 size_t slen;
01035 
01036                 (void) stpcpy( stpcpy(t, s) , dirNames[i] + len);
01037 
01038                 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
01039                 (void) rpmCleanPath(t);
01040                 slen = strlen(t);
01041                 t[slen] = '/';
01042                 t[slen+1] = '\0';
01043 
01044                 if (actions)
01045                     rpmMessage(RPMMESS_DEBUG,
01046                         _("relocating directory %s to %s\n"), dirNames[i], t);
01047                 dirNames[i] = t;
01048                 nrelocated++;
01049             }
01050         }
01051     }
01052 
01053     /* Save original filenames in header and replace (relocated) filenames. */
01054     if (nrelocated) {
01055         int c;
01056         void * d;
01057         rpmTagType t;
01058 
01059         d = NULL;
01060         xx = hge(h, RPMTAG_BASENAMES, &t, &d, &c);
01061         xx = hae(h, RPMTAG_ORIGBASENAMES, t, d, c);
01062         d = hfd(d, t);
01063 
01064         d = NULL;
01065         xx = hge(h, RPMTAG_DIRNAMES, &t, &d, &c);
01066         xx = hae(h, RPMTAG_ORIGDIRNAMES, t, d, c);
01067         d = hfd(d, t);
01068 
01069         d = NULL;
01070         xx = hge(h, RPMTAG_DIRINDEXES, &t, &d, &c);
01071         xx = hae(h, RPMTAG_ORIGDIRINDEXES, t, d, c);
01072         d = hfd(d, t);
01073 
01074         xx = hme(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
01075                           baseNames, fileCount);
01076         fi->bnl = hfd(fi->bnl, RPM_STRING_ARRAY_TYPE);
01077         xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc);
01078 
01079         xx = hme(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
01080                           dirNames, dirCount);
01081         fi->dnl = hfd(fi->dnl, RPM_STRING_ARRAY_TYPE);
01082         xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
01083 
01084         xx = hme(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE,
01085                           dirIndexes, fileCount);
01086         xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
01087     }
01088 
01089     baseNames = hfd(baseNames, RPM_STRING_ARRAY_TYPE);
01090     dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
01091 /*@-dependenttrans@*/
01092     fn = _free(fn);
01093 /*@=dependenttrans@*/
01094 
01095     return h;
01096 }
01097 /*@=bounds@*/
01098 
01099 rpmfi rpmfiFree(rpmfi fi)
01100 {
01101     HFD_t hfd = headerFreeData;
01102 
01103     if (fi == NULL) return NULL;
01104 
01105     if (fi->nrefs > 1)
01106         return rpmfiUnlink(fi, fi->Type);
01107 
01108 /*@-modfilesys@*/
01109 if (_rpmfi_debug < 0)
01110 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
01111 /*@=modfilesys@*/
01112 
01113     /* Free pre- and post-transaction script and interpreter strings. */
01114     fi->pretrans = _free(fi->pretrans);
01115     fi->pretransprog = _free(fi->pretransprog);
01116     fi->posttrans = _free(fi->posttrans);
01117     fi->posttransprog = _free(fi->posttransprog);
01118 
01119     /*@-branchstate@*/
01120     if (fi->fc > 0) {
01121         fi->bnl = hfd(fi->bnl, -1);
01122         fi->dnl = hfd(fi->dnl, -1);
01123 
01124         fi->flinks = hfd(fi->flinks, -1);
01125         fi->flangs = hfd(fi->flangs, -1);
01126         fi->fmd5s = hfd(fi->fmd5s, -1);
01127         fi->md5s = _free(fi->md5s);
01128 
01129         fi->cdict = hfd(fi->cdict, -1);
01130 
01131         fi->fuser = hfd(fi->fuser, -1);
01132         fi->fgroup = hfd(fi->fgroup, -1);
01133 
01134         fi->fstates = _free(fi->fstates);
01135 
01136         /*@-evalorder@*/
01137         if (!fi->keep_header && fi->h == NULL) {
01138             fi->fmtimes = _free(fi->fmtimes);
01139             fi->fmodes = _free(fi->fmodes);
01140             fi->fflags = _free(fi->fflags);
01141             fi->vflags = _free(fi->vflags);
01142             fi->fsizes = _free(fi->fsizes);
01143             fi->frdevs = _free(fi->frdevs);
01144             fi->finodes = _free(fi->finodes);
01145             fi->dil = _free(fi->dil);
01146 
01147             fi->fcolors = _free(fi->fcolors);
01148             fi->fcdictx = _free(fi->fcdictx);
01149             fi->ddict = _free(fi->ddict);
01150             fi->fddictx = _free(fi->fddictx);
01151             fi->fddictn = _free(fi->fddictn);
01152 
01153         }
01154         /*@=evalorder@*/
01155     }
01156     /*@=branchstate@*/
01157 
01158     fi->fsm = freeFSM(fi->fsm);
01159 
01160     fi->fn = _free(fi->fn);
01161     fi->apath = _free(fi->apath);
01162     fi->fmapflags = _free(fi->fmapflags);
01163 
01164     fi->obnl = hfd(fi->obnl, -1);
01165     fi->odnl = hfd(fi->odnl, -1);
01166 
01167     fi->fcontexts = hfd(fi->fcontexts, -1);
01168 
01169     fi->actions = _free(fi->actions);
01170     fi->replacedSizes = _free(fi->replacedSizes);
01171     fi->replaced = _free(fi->replaced);
01172 
01173     fi->h = headerFree(fi->h);
01174 
01175     /*@-nullstate -refcounttrans -usereleased@*/
01176     (void) rpmfiUnlink(fi, fi->Type);
01177     memset(fi, 0, sizeof(*fi));         /* XXX trash and burn */
01178     fi = _free(fi);
01179     /*@=nullstate =refcounttrans =usereleased@*/
01180 
01181     return NULL;
01182 }
01183 
01189 static inline unsigned char nibble(char c)
01190         /*@*/
01191 {
01192     if (c >= '0' && c <= '9')
01193         return (c - '0');
01194     if (c >= 'A' && c <= 'F')
01195         return (c - 'A') + 10;
01196     if (c >= 'a' && c <= 'f')
01197         return (c - 'a') + 10;
01198     return 0;
01199 }
01200 
01201 #define _fdupe(_fi, _data)      \
01202     if ((_fi)->_data != NULL)   \
01203         (_fi)->_data = memcpy(xmalloc((_fi)->fc * sizeof(*(_fi)->_data)), \
01204                         (_fi)->_data, (_fi)->fc * sizeof(*(_fi)->_data))
01205 
01206 /* XXX Ick, not SEF. */
01207 #define _fdupestring(_h, _tag, _data) \
01208     if (hge((_h), (_tag), NULL, (void **) &(_data), NULL)) \
01209         _data = xstrdup(_data)
01210 
01211 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, int scareMem)
01212 {
01213     HGE_t hge =
01214         (scareMem ? (HGE_t) headerGetEntryMinMemory : (HGE_t) headerGetEntry);
01215     HFD_t hfd = headerFreeData;
01216     rpmte p;
01217     rpmfi fi = NULL;
01218     const char * Type;
01219     uint_32 * uip;
01220     int dnlmax, bnlmax;
01221     unsigned char * t;
01222     int len;
01223     int xx;
01224     int i;
01225 
01226     if (tagN == RPMTAG_BASENAMES) {
01227         Type = "Files";
01228     } else {
01229         Type = "?Type?";
01230         goto exit;
01231     }
01232 
01233     fi = xcalloc(1, sizeof(*fi));
01234     if (fi == NULL)     /* XXX can't happen */
01235         goto exit;
01236 
01237     fi->magic = RPMFIMAGIC;
01238     fi->Type = Type;
01239     fi->i = -1;
01240     fi->tagN = tagN;
01241 
01242     fi->hge = hge;
01243     fi->hae = (HAE_t) headerAddEntry;
01244     fi->hme = (HME_t) headerModifyEntry;
01245     fi->hre = (HRE_t) headerRemoveEntry;
01246     fi->hfd = headerFreeData;
01247 
01248     fi->h = (scareMem ? headerLink(h) : NULL);
01249 
01250     if (fi->fsm == NULL)
01251         fi->fsm = newFSM();
01252 
01253     /* 0 means unknown */
01254     xx = hge(h, RPMTAG_ARCHIVESIZE, NULL, (void **) &uip, NULL);
01255     fi->archivePos = 0;
01256     fi->archiveSize = (xx ? *uip : 0);
01257 
01258     /* Extract pre- and post-transaction script and interpreter strings. */
01259     _fdupestring(h, RPMTAG_PRETRANS, fi->pretrans);
01260     _fdupestring(h, RPMTAG_PRETRANSPROG, fi->pretransprog);
01261     _fdupestring(h, RPMTAG_POSTTRANS, fi->posttrans);
01262     _fdupestring(h, RPMTAG_POSTTRANSPROG, fi->posttransprog);
01263 
01264     if (!hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc)) {
01265         fi->fc = 0;
01266         fi->dc = 0;
01267         goto exit;
01268     }
01269     xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
01270     xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
01271     xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fi->fmodes, NULL);
01272     xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fi->fflags, NULL);
01273     xx = hge(h, RPMTAG_FILEVERIFYFLAGS, NULL, (void **) &fi->vflags, NULL);
01274     xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &fi->fsizes, NULL);
01275 
01276     xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fi->fcolors, NULL);
01277     fi->color = 0;
01278     if (fi->fcolors != NULL)
01279     for (i = 0; i < fi->fc; i++)
01280         fi->color |= fi->fcolors[i];
01281     xx = hge(h, RPMTAG_CLASSDICT, NULL, (void **) &fi->cdict, &fi->ncdict);
01282     xx = hge(h, RPMTAG_FILECLASS, NULL, (void **) &fi->fcdictx, NULL);
01283 
01284     xx = hge(h, RPMTAG_DEPENDSDICT, NULL, (void **) &fi->ddict, &fi->nddict);
01285     xx = hge(h, RPMTAG_FILEDEPENDSX, NULL, (void **) &fi->fddictx, NULL);
01286     xx = hge(h, RPMTAG_FILEDEPENDSN, NULL, (void **) &fi->fddictn, NULL);
01287 
01288     xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &fi->fstates, NULL);
01289     if (xx == 0 || fi->fstates == NULL)
01290         fi->fstates = xcalloc(fi->fc, sizeof(*fi->fstates));
01291     else
01292         _fdupe(fi, fstates);
01293 
01294     fi->action = FA_UNKNOWN;
01295     fi->flags = 0;
01296 
01297 if (fi->actions == NULL)
01298         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
01299 
01300     fi->keep_header = (scareMem ? 1 : 0);
01301 
01302     /* XXX TR_REMOVED needs CPIO_MAP_{ABSOLUTE,ADDDOT} CPIO_ALL_HARDLINKS */
01303     fi->mapflags =
01304                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01305 
01306     xx = hge(h, RPMTAG_FILELINKTOS, NULL, (void **) &fi->flinks, NULL);
01307     xx = hge(h, RPMTAG_FILELANGS, NULL, (void **) &fi->flangs, NULL);
01308 
01309     fi->fmd5s = NULL;
01310     xx = hge(h, RPMTAG_FILEMD5S, NULL, (void **) &fi->fmd5s, NULL);
01311 
01312     fi->md5s = NULL;
01313     if (fi->fmd5s) {
01314         t = xmalloc(fi->fc * 16);
01315         fi->md5s = t;
01316         for (i = 0; i < fi->fc; i++) {
01317             const char * fmd5;
01318             int j;
01319 
01320             fmd5 = fi->fmd5s[i];
01321             if (!(fmd5 && *fmd5 != '\0')) {
01322                 memset(t, 0, 16);
01323                 t += 16;
01324                 continue;
01325             }
01326             for (j = 0; j < 16; j++, t++, fmd5 += 2)
01327                 *t = (nibble(fmd5[0]) << 4) | nibble(fmd5[1]);
01328         }
01329         fi->fmd5s = hfd(fi->fmd5s, -1);
01330     }
01331 
01332     /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes, or fcontexts */
01333     xx = hge(h, RPMTAG_FILEMTIMES, NULL, (void **) &fi->fmtimes, NULL);
01334     xx = hge(h, RPMTAG_FILERDEVS, NULL, (void **) &fi->frdevs, NULL);
01335     xx = hge(h, RPMTAG_FILEINODES, NULL, (void **) &fi->finodes, NULL);
01336     xx = hge(h, RPMTAG_FILECONTEXTS, NULL, (void **) &fi->fcontexts, NULL);
01337 
01338     fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
01339 
01340     xx = hge(h, RPMTAG_FILEUSERNAME, NULL, (void **) &fi->fuser, NULL);
01341     xx = hge(h, RPMTAG_FILEGROUPNAME, NULL, (void **) &fi->fgroup, NULL);
01342 
01343     if (ts != NULL)
01344     if (fi != NULL)
01345     if ((p = rpmtsRelocateElement(ts)) != NULL && rpmteType(p) == TR_ADDED
01346      && !headerIsEntry(h, RPMTAG_SOURCEPACKAGE)
01347      && !headerIsEntry(h, RPMTAG_ORIGBASENAMES))
01348     {
01349         const char * fmt = rpmGetPath("%{?_autorelocate_path}", NULL);
01350         const char * errstr;
01351         char * newPath;
01352         Header foo;
01353 
01354         /* XXX error handling. */
01355         newPath = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
01356         fmt = _free(fmt);
01357 
01358 #if __ia64__
01359         /* XXX On ia64, change leading /emul/ix86 -> /emul/ia32, ick. */
01360         if (newPath != NULL && *newPath != '\0'
01361          && strlen(newPath) >= (sizeof("/emul/i386")-1)
01362          && newPath[0] == '/' && newPath[1] == 'e' && newPath[2] == 'm'
01363          && newPath[3] == 'u' && newPath[4] == 'l' && newPath[5] == '/'
01364          && newPath[6] == 'i' && newPath[8] == '8' && newPath[9] == '6')
01365         {
01366             newPath[7] = 'a';
01367             newPath[8] = '3';
01368             newPath[9] = '2';
01369         }
01370 #endif
01371  
01372         /* XXX Make sure autoreloc is not already specified. */
01373         i = p->nrelocs;
01374         if (newPath != NULL && *newPath != '\0' && p->relocs != NULL)
01375         for (i = 0; i < p->nrelocs; i++) {
01376 /*@-nullpass@*/ /* XXX {old,new}Path might be NULL */
01377            if (strcmp(p->relocs[i].oldPath, "/"))
01378                 continue;
01379            if (strcmp(p->relocs[i].newPath, newPath))
01380                 continue;
01381 /*@=nullpass@*/
01382            break;
01383         }
01384 
01385         /* XXX test for incompatible arch triggering autorelocation is dumb. */
01386         if (newPath != NULL && *newPath != '\0' && i == p->nrelocs
01387          && p->archScore == 0)
01388         {
01389 
01390             p->relocs =
01391                 xrealloc(p->relocs, (p->nrelocs + 2) * sizeof(*p->relocs));
01392             p->relocs[p->nrelocs].oldPath = xstrdup("/");
01393             p->relocs[p->nrelocs].newPath = xstrdup(newPath);
01394             p->autorelocatex = p->nrelocs;
01395             p->nrelocs++;
01396             p->relocs[p->nrelocs].oldPath = NULL;
01397             p->relocs[p->nrelocs].newPath = NULL;
01398         }
01399         newPath = _free(newPath);
01400 
01401 /* XXX DYING */
01402 if (fi->actions == NULL)
01403         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
01404         /*@-compdef@*/ /* FIX: fi-md5s undefined */
01405         foo = relocateFileList(ts, fi, h, fi->actions);
01406         /*@=compdef@*/
01407         fi->h = headerFree(fi->h);
01408         fi->h = headerLink(foo);
01409         foo = headerFree(foo);
01410     }
01411 
01412     if (!scareMem) {
01413         _fdupe(fi, fmtimes);
01414         _fdupe(fi, frdevs);
01415         _fdupe(fi, finodes);
01416         _fdupe(fi, fsizes);
01417         _fdupe(fi, fflags);
01418         _fdupe(fi, vflags);
01419         _fdupe(fi, fmodes);
01420         _fdupe(fi, dil);
01421 
01422         _fdupe(fi, fcolors);
01423         _fdupe(fi, fcdictx);
01424 
01425         if (fi->ddict != NULL)
01426             fi->ddict = memcpy(xmalloc(fi->nddict * sizeof(*fi->ddict)),
01427                         fi->ddict, fi->nddict * sizeof(*fi->ddict));
01428 
01429         _fdupe(fi, fddictx);
01430         _fdupe(fi, fddictn);
01431 
01432         fi->h = headerFree(fi->h);
01433     }
01434 
01435     dnlmax = -1;
01436     for (i = 0; i < fi->dc; i++) {
01437         if ((len = strlen(fi->dnl[i])) > dnlmax)
01438             dnlmax = len;
01439     }
01440     bnlmax = -1;
01441     for (i = 0; i < fi->fc; i++) {
01442         if ((len = strlen(fi->bnl[i])) > bnlmax)
01443             bnlmax = len;
01444     }
01445     fi->fnlen = dnlmax + bnlmax + 1;
01446     fi->fn = NULL;
01447 
01448     fi->dperms = 0755;
01449     fi->fperms = 0644;
01450 
01451 exit:
01452 /*@-modfilesys@*/
01453 if (_rpmfi_debug < 0)
01454 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
01455 /*@=modfilesys@*/
01456 
01457     /*@-compdef -nullstate@*/ /* FIX: rpmfi null annotations */
01458     return rpmfiLink(fi, (fi ? fi->Type : NULL));
01459     /*@=compdef =nullstate@*/
01460 }
01461 
01462 void rpmfiBuildFClasses(Header h,
01463         /*@out@*/ const char *** fclassp, /*@out@*/ int * fcp)
01464 {
01465     int scareMem = 0;
01466     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01467     const char * FClass;
01468     const char ** av;
01469     int ac;
01470     size_t nb;
01471     char * t;
01472 
01473     if ((ac = rpmfiFC(fi)) <= 0) {
01474         av = NULL;
01475         ac = 0;
01476         goto exit;
01477     }
01478 
01479     /* Compute size of file class argv array blob. */
01480     nb = (ac + 1) * sizeof(*av);
01481     fi = rpmfiInit(fi, 0);
01482     if (fi != NULL)
01483     while (rpmfiNext(fi) >= 0) {
01484         FClass = rpmfiFClass(fi);
01485         if (FClass && *FClass != '\0')
01486             nb += strlen(FClass);
01487         nb += 1;
01488     }
01489 
01490     /* Create and load file class argv array. */
01491     av = xmalloc(nb);
01492     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01493     ac = 0;
01494     fi = rpmfiInit(fi, 0);
01495     if (fi != NULL)
01496     while (rpmfiNext(fi) >= 0) {
01497         FClass = rpmfiFClass(fi);
01498         av[ac++] = t;
01499         if (FClass && *FClass != '\0')
01500             t = stpcpy(t, FClass);
01501         *t++ = '\0';
01502     }
01503     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01504     /*@=branchstate@*/
01505 
01506 exit:
01507     fi = rpmfiFree(fi);
01508     /*@-branchstate@*/
01509     if (fclassp)
01510         *fclassp = av;
01511     else
01512         av = _free(av);
01513     /*@=branchstate@*/
01514     if (fcp) *fcp = ac;
01515 }
01516 
01517 void rpmfiBuildFContexts(Header h,
01518         /*@out@*/ const char *** fcontextp, /*@out@*/ int * fcp)
01519 {
01520     int scareMem = 0;
01521     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01522     const char * fcontext;
01523     const char ** av;
01524     int ac;
01525     size_t nb;
01526     char * t;
01527 
01528     if ((ac = rpmfiFC(fi)) <= 0) {
01529         av = NULL;
01530         ac = 0;
01531         goto exit;
01532     }
01533 
01534     /* Compute size of argv array blob. */
01535     nb = (ac + 1) * sizeof(*av);
01536     fi = rpmfiInit(fi, 0);
01537     if (fi != NULL)
01538     while (rpmfiNext(fi) >= 0) {
01539         fcontext = rpmfiFContext(fi);
01540         if (fcontext && *fcontext != '\0')
01541             nb += strlen(fcontext);
01542         nb += 1;
01543     }
01544 
01545     /* Create and load argv array. */
01546     av = xmalloc(nb);
01547     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01548     ac = 0;
01549     fi = rpmfiInit(fi, 0);
01550     if (fi != NULL)
01551     while (rpmfiNext(fi) >= 0) {
01552         fcontext = rpmfiFContext(fi);
01553         av[ac++] = t;
01554         if (fcontext && *fcontext != '\0')
01555             t = stpcpy(t, fcontext);
01556         *t++ = '\0';
01557     }
01558     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01559     /*@=branchstate@*/
01560 
01561 exit:
01562     fi = rpmfiFree(fi);
01563     /*@-branchstate@*/
01564     if (fcontextp)
01565         *fcontextp = av;
01566     else
01567         av = _free(av);
01568     /*@=branchstate@*/
01569     if (fcp) *fcp = ac;
01570 }
01571 
01572 void rpmfiBuildFSContexts(Header h,
01573         /*@out@*/ const char *** fcontextp, /*@out@*/ int * fcp)
01574 {
01575     int scareMem = 0;
01576     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01577     const char ** av;
01578     int ac;
01579     size_t nb;
01580     char * t;
01581     char * fctxt = NULL;
01582     size_t fctxtlen = 0;
01583     int * fcnb;
01584 
01585     if ((ac = rpmfiFC(fi)) <= 0) {
01586         av = NULL;
01587         ac = 0;
01588         goto exit;
01589     }
01590 
01591     /* Compute size of argv array blob, concatenating file contexts. */
01592     nb = ac * sizeof(*fcnb);
01593     fcnb = memset(alloca(nb), 0, nb);
01594     ac = 0;
01595     fi = rpmfiInit(fi, 0);
01596     if (fi != NULL)
01597     while (rpmfiNext(fi) >= 0) {
01598         const char * fn = rpmfiFN(fi);
01599         security_context_t scon;
01600 
01601         fcnb[ac] = lgetfilecon(fn, &scon);
01602 /*@-branchstate@*/
01603         if (fcnb[ac] > 0) {
01604             fctxt = xrealloc(fctxt, fctxtlen + fcnb[ac]);
01605             memcpy(fctxt+fctxtlen, scon, fcnb[ac]);
01606             fctxtlen += fcnb[ac];
01607             freecon(scon);
01608         }
01609 /*@=branchstate@*/
01610         ac++;
01611     }
01612 
01613     /* Create and load argv array from concatenated file contexts. */
01614     nb = (ac + 1) * sizeof(*av) + fctxtlen;
01615     av = xmalloc(nb);
01616     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01617     if (fctxt != NULL && fctxtlen > 0)
01618         (void) memcpy(t, fctxt, fctxtlen);
01619     ac = 0;
01620     fi = rpmfiInit(fi, 0);
01621     if (fi != NULL)
01622     while (rpmfiNext(fi) >= 0) {
01623         av[ac] = "";
01624         if (fcnb[ac] > 0) {
01625             av[ac] = t;
01626             t += fcnb[ac];
01627         }
01628         ac++;
01629     }
01630     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01631 
01632 exit:
01633     fi = rpmfiFree(fi);
01634     /*@-branchstate@*/
01635     if (fcontextp)
01636         *fcontextp = av;
01637     else
01638         av = _free(av);
01639     /*@=branchstate@*/
01640     if (fcp) *fcp = ac;
01641 }
01642 
01643 void rpmfiBuildREContexts(Header h,
01644         /*@out@*/ const char *** fcontextp, /*@out@*/ int * fcp)
01645 {
01646     int scareMem = 0;
01647     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01648     const char ** av = NULL;
01649     const char * myfn = rpmGetPath("%{?__file_context_path}", NULL);
01650     int ac;
01651     size_t nb;
01652     char * t;
01653     char * fctxt = NULL;
01654     size_t fctxtlen = 0;
01655     int * fcnb;
01656 
01657     if ((ac = rpmfiFC(fi)) <= 0) {
01658         ac = 0;
01659         goto exit;
01660     }
01661 
01662     /* Read security context patterns. */
01663     matchpathcon_init(myfn);
01664 
01665     /* Compute size of argv array blob, concatenating file contexts. */
01666     nb = ac * sizeof(*fcnb);
01667     fcnb = memset(alloca(nb), 0, nb);
01668     ac = 0;
01669     fi = rpmfiInit(fi, 0);
01670     if (fi != NULL)
01671     while (rpmfiNext(fi) >= 0) {
01672         const char * fn = rpmfiFN(fi);
01673         mode_t fmode = rpmfiFMode(fi);
01674         security_context_t scon;
01675 
01676         if (matchpathcon(fn, fmode, &scon) == 0) {
01677             fcnb[ac] = strlen(scon) + 1;
01678 /*@-branchstate@*/
01679             if (fcnb[ac] > 0) {
01680                 fctxt = xrealloc(fctxt, fctxtlen + fcnb[ac]);
01681                 memcpy(fctxt+fctxtlen, scon, fcnb[ac]);
01682                 fctxtlen += fcnb[ac];
01683             }
01684             freecon(scon); 
01685 /*@=branchstate@*/
01686         }
01687         ac++;
01688     }
01689 
01690     /* Create and load argv array from concatenated file contexts. */
01691     nb = (ac + 1) * sizeof(*av) + fctxtlen;
01692     av = xmalloc(nb);
01693     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01694     (void) memcpy(t, fctxt, fctxtlen);
01695     ac = 0;
01696     fi = rpmfiInit(fi, 0);
01697     if (fi != NULL)
01698     while (rpmfiNext(fi) >= 0) {
01699         av[ac] = "";
01700         if (fcnb[ac] > 0) {
01701             av[ac] = t;
01702             t += fcnb[ac];
01703         }
01704         ac++;
01705     }
01706     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01707 
01708 exit:
01709     fi = rpmfiFree(fi);
01710     /*@-branchstate@*/
01711     if (fcontextp)
01712         *fcontextp = av;
01713     else
01714         av = _free(av);
01715     /*@=branchstate@*/
01716     if (fcp) *fcp = ac;
01717 }
01718 
01719 void rpmfiBuildFDeps(Header h, rpmTag tagN,
01720         /*@out@*/ const char *** fdepsp, /*@out@*/ int * fcp)
01721 {
01722     int scareMem = 0;
01723     rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01724     rpmds ds = NULL;
01725     const char ** av;
01726     int ac;
01727     size_t nb;
01728     char * t;
01729     char deptype = 'R';
01730     char mydt;
01731     const char * DNEVR;
01732     const int_32 * ddict;
01733     unsigned ix;
01734     int ndx;
01735 
01736     if ((ac = rpmfiFC(fi)) <= 0) {
01737         av = NULL;
01738         ac = 0;
01739         goto exit;
01740     }
01741 
01742     if (tagN == RPMTAG_PROVIDENAME)
01743         deptype = 'P';
01744     else if (tagN == RPMTAG_REQUIRENAME)
01745         deptype = 'R';
01746 
01747     ds = rpmdsNew(h, tagN, scareMem);
01748 
01749     /* Compute size of file depends argv array blob. */
01750     nb = (ac + 1) * sizeof(*av);
01751     fi = rpmfiInit(fi, 0);
01752     if (fi != NULL)
01753     while (rpmfiNext(fi) >= 0) {
01754         ddict = NULL;
01755         ndx = rpmfiFDepends(fi, &ddict);
01756         if (ddict != NULL)
01757         while (ndx-- > 0) {
01758             ix = *ddict++;
01759             mydt = ((ix >> 24) & 0xff);
01760             if (mydt != deptype)
01761                 /*@innercontinue@*/ continue;
01762             ix &= 0x00ffffff;
01763             (void) rpmdsSetIx(ds, ix-1);
01764             if (rpmdsNext(ds) < 0)
01765                 /*@innercontinue@*/ continue;
01766             DNEVR = rpmdsDNEVR(ds);
01767             if (DNEVR != NULL)
01768                 nb += strlen(DNEVR+2) + 1;
01769         }
01770         nb += 1;
01771     }
01772 
01773     /* Create and load file depends argv array. */
01774     av = xmalloc(nb);
01775     t = ((char *) av) + ((ac + 1) * sizeof(*av));
01776     ac = 0;
01777     /*@-branchstate@*/
01778     fi = rpmfiInit(fi, 0);
01779     if (fi != NULL)
01780     while (rpmfiNext(fi) >= 0) {
01781         av[ac++] = t;
01782         ddict = NULL;
01783         ndx = rpmfiFDepends(fi, &ddict);
01784         if (ddict != NULL)
01785         while (ndx-- > 0) {
01786             ix = *ddict++;
01787             mydt = ((ix >> 24) & 0xff);
01788             if (mydt != deptype)
01789                 /*@innercontinue@*/ continue;
01790             ix &= 0x00ffffff;
01791             (void) rpmdsSetIx(ds, ix-1);
01792             if (rpmdsNext(ds) < 0)
01793                 /*@innercontinue@*/ continue;
01794             DNEVR = rpmdsDNEVR(ds);
01795             if (DNEVR != NULL) {
01796                 t = stpcpy(t, DNEVR+2);
01797                 *t++ = ' ';
01798                 *t = '\0';
01799             }
01800         }
01801         *t++ = '\0';
01802     }
01803     /*@=branchstate@*/
01804     av[ac] = NULL;      /* XXX tag arrays are not NULL terminated. */
01805 
01806 exit:
01807     fi = rpmfiFree(fi);
01808     ds = rpmdsFree(ds);
01809     /*@-branchstate@*/
01810     if (fdepsp)
01811         *fdepsp = av;
01812     else
01813         av = _free(av);
01814     /*@=branchstate@*/
01815     if (fcp) *fcp = ac;
01816 }

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