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

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

Generated on Fri Sep 30 02:03:26 2005 for rpm by doxygen1.2.18