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

rpmfi.c

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

Generated on Sun Jun 25 14:13:34 2006 for rpm by  doxygen 1.3.9.1