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

Generated on Thu Jul 17 22:45:29 2003 for rpm by doxygen1.3