00001
00006 #include "system.h"
00007
00008 #include <rpmio_internal.h>
00009 #include <rpmlib.h>
00010
00011 #include "cpio.h"
00012 #include "fsm.h"
00013
00014 #include "rpmds.h"
00015
00016 #define _RPMFI_INTERNAL
00017 #include "rpmfi.h"
00018
00019 #include <selinux/selinux.h>
00020
00021 #define _RPMTE_INTERNAL
00022 #include "rpmte.h"
00023 #include "rpmts.h"
00024
00025 #include "misc.h"
00026 #include "rpmmacro.h"
00027
00028 #include "debug.h"
00029
00030
00031
00032
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
00039 if (_rpmfi_debug && msg != NULL)
00040 fprintf(stderr, "--> fi %p -- %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
00041
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
00051 if (_rpmfi_debug && msg != NULL)
00052 fprintf(stderr, "--> fi %p ++ %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
00053
00054 return fi;
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
00086 fi->j = fi->dil[fi->i];
00087
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
00114 if (fi->bnl != NULL)
00115 BN = fi->bnl[fi->i];
00116
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
00127 if (fi->dnl != NULL)
00128 DN = fi->dnl[fi->j];
00129
00130 }
00131 return DN;
00132 }
00133
00134 const char * rpmfiFN(rpmfi fi)
00135 {
00136 const char * FN = "";
00137
00138
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
00145 *t = '\0';
00146 t = stpcpy(t, fi->dnl[fi->dil[fi->i]]);
00147 t = stpcpy(t, fi->bnl[fi->i]);
00148
00149 }
00150
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
00160 if (fi->fflags != NULL)
00161 FFlags = fi->fflags[fi->i];
00162
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
00173 if (fi->vflags != NULL)
00174 VFlags = fi->vflags[fi->i];
00175
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
00186 if (fi->fmodes != NULL)
00187 fmode = fi->fmodes[fi->i];
00188
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
00199 if (fi->fstates != NULL)
00200 fstate = fi->fstates[fi->i];
00201
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
00212 if (fi->md5s != NULL)
00213 MD5 = fi->md5s + (16 * fi->i);
00214
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
00225 if (fi->flinks != NULL)
00226 flink = fi->flinks[fi->i];
00227
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
00238 if (fi->fsizes != NULL)
00239 fsize = fi->fsizes[fi->i];
00240
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
00251 if (fi->frdevs != NULL)
00252 frdev = fi->frdevs[fi->i];
00253
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
00264 if (fi->finodes != NULL)
00265 finode = fi->finodes[fi->i];
00266
00267 }
00268 return finode;
00269 }
00270
00271 uint_32 rpmfiColor(rpmfi fi)
00272 {
00273 uint_32 color = 0;
00274
00275 if (fi != NULL)
00276
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
00287 if (fi->fcolors != NULL)
00288
00289 fcolor = (fi->fcolors[fi->i] & 0x0f);
00290
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
00302 cdictx = fi->fcdictx[fi->i];
00303 if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
00304 fclass = fi->cdict[cdictx];
00305
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
00316 if (fi->fcontexts != NULL)
00317 fcontext = fi->fcontexts[fi->i];
00318
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
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
00338 }
00339
00340 if (fddictp)
00341 *fddictp = fddict;
00342
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
00352
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
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
00374 if (fi->fmtimes != NULL)
00375 fmtime = fi->fmtimes[fi->i];
00376
00377 }
00378 return fmtime;
00379 }
00380
00381 const char * rpmfiFUser(rpmfi fi)
00382 {
00383 const char * fuser = NULL;
00384
00385
00386 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00387
00388 if (fi->fuser != NULL)
00389 fuser = fi->fuser[fi->i];
00390
00391 }
00392 return fuser;
00393 }
00394
00395 const char * rpmfiFGroup(rpmfi fi)
00396 {
00397 const char * fgroup = NULL;
00398
00399
00400 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
00401
00402 if (fi->fgroup != NULL)
00403 fgroup = fi->fgroup[fi->i];
00404
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
00417 if (fi->dil != NULL)
00418 fi->j = fi->dil[fi->i];
00419
00420 } else
00421 fi->i = -1;
00422
00423
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
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
00443 return fi;
00444
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
00458 if (_rpmfi_debug < 0 && j != -1)
00459 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, (fi->Type ? fi->Type : "?Type?"), j);
00460
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
00477 return fi;
00478
00479 }
00480
00486 static
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
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
00510 if (S_ISSOCK(mode)) return SOCK;
00511
00512 if (S_ISFIFO(mode)) return PIPE;
00513 return REG;
00514 }
00515
00516
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
00544
00545
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
00558
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
00575
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
00591
00592
00593 memset(buffer, 0, sizeof(buffer));
00594 if (dbWhat == REG) {
00595 const unsigned char * omd5, * nmd5;
00596
00597 if (strcmp(fn, "/var/log/lastlog"))
00598 if (domd5(fn, buffer, 0, NULL))
00599 return FA_CREATE;
00600 omd5 = rpmfiMD5(ofi);
00601 if (omd5 && !memcmp(omd5, buffer, 16))
00602 return FA_CREATE;
00603 nmd5 = rpmfiMD5(nfi);
00604
00605 if (omd5 && nmd5 && !memcmp(omd5, nmd5, 16))
00606 return FA_SKIP;
00607
00608 } else {
00609 const char * oFLink, * nFLink;
00610 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
00611 return FA_CREATE;
00612 oFLink = rpmfiFLink(ofi);
00613 if (oFLink && !strcmp(oFLink, buffer))
00614 return FA_CREATE;
00615 nFLink = rpmfiFLink(nfi);
00616
00617 if (oFLink && nFLink && !strcmp(oFLink, nFLink))
00618 return FA_SKIP;
00619
00620 }
00621
00622
00623
00624
00625
00626
00627
00628 return save;
00629 }
00630
00631
00632
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
00641 }
00642
00643 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
00644
00654
00655 static
00656 Header relocateFileList(const rpmts ts, rpmfi fi,
00657 Header origH, fileAction * actions)
00658
00659
00660 {
00661 rpmte p = rpmtsRelocateElement(ts);
00662 HGE_t hge = fi->hge;
00663 HAE_t hae = fi->hae;
00664 HME_t hme = fi->hme;
00665 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
00666 static int _printed = 0;
00667 int allowBadRelocate = (rpmtsFilterFlags(ts) & RPMPROB_FILTER_FORCERELOCATE);
00668 rpmRelocation * relocations = NULL;
00669 int numRelocations;
00670 const char ** validRelocations;
00671 rpmTagType validType;
00672 int numValid;
00673 const char ** baseNames;
00674 const char ** dirNames;
00675 int_32 * dirIndexes;
00676 int_32 * newDirIndexes;
00677 int_32 fileCount;
00678 int_32 dirCount;
00679 uint_32 mydColor = rpmExpandNumeric("%{?_autorelocate_dcolor}");
00680 uint_32 * fFlags = NULL;
00681 uint_32 * fColors = NULL;
00682 uint_32 * dColors = NULL;
00683 uint_16 * fModes = NULL;
00684 Header h;
00685 int nrelocated = 0;
00686 int fileAlloced = 0;
00687 char * fn = NULL;
00688 int haveRelocatedFile = 0;
00689 int reldel = 0;
00690 int len;
00691 int i, j, xx;
00692
00693 if (!hge(origH, RPMTAG_PREFIXES, &validType,
00694 (void **) &validRelocations, &numValid))
00695 numValid = 0;
00696
00697 assert(p != NULL);
00698 numRelocations = 0;
00699 if (p->relocs)
00700 while (p->relocs[numRelocations].newPath ||
00701 p->relocs[numRelocations].oldPath)
00702 numRelocations++;
00703
00704
00705
00706
00707
00708
00709
00710 if (p->relocs == NULL || numRelocations == 0) {
00711 if (numValid) {
00712 if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES))
00713 xx = hae(origH, RPMTAG_INSTPREFIXES,
00714 validType, validRelocations, numValid);
00715 validRelocations = hfd(validRelocations, validType);
00716 }
00717
00718 return headerLink(origH);
00719 }
00720
00721 h = headerLink(origH);
00722
00723 relocations = alloca(sizeof(*relocations) * numRelocations);
00724
00725
00726 for (i = 0; i < numRelocations; i++) {
00727 char * t;
00728
00729
00730
00731
00732
00733 if (p->relocs[i].oldPath == NULL) continue;
00734
00735
00736
00737 t = alloca_strdup(p->relocs[i].oldPath);
00738
00739 relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
00740 ? t
00741 : stripTrailingChar(t, '/');
00742
00743
00744
00745 if (p->relocs[i].newPath) {
00746 int del;
00747
00748 t = alloca_strdup(p->relocs[i].newPath);
00749
00750 relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
00751 ? t
00752 : stripTrailingChar(t, '/');
00753
00754
00755
00756
00757 for (j = 0; j < numValid; j++) {
00758 if (!strcmp(validRelocations[j], relocations[i].oldPath))
00759 break;
00760 }
00761
00762
00763 if (j == numValid && !allowBadRelocate && actions) {
00764 rpmps ps = rpmtsProblems(ts);
00765 rpmpsAppend(ps, RPMPROB_BADRELOCATE,
00766 rpmteNEVR(p), rpmteKey(p),
00767 relocations[i].oldPath, NULL, NULL, 0);
00768 ps = rpmpsFree(ps);
00769 }
00770 del =
00771 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
00772
00773
00774 if (del > reldel)
00775 reldel = del;
00776 } else {
00777 relocations[i].newPath = NULL;
00778 }
00779 }
00780
00781
00782 for (i = 0; i < numRelocations; i++) {
00783 int madeSwap;
00784 madeSwap = 0;
00785 for (j = 1; j < numRelocations; j++) {
00786 rpmRelocation tmpReloc;
00787 if (relocations[j - 1].oldPath == NULL ||
00788 relocations[j ].oldPath == NULL ||
00789 strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
00790 continue;
00791
00792 tmpReloc = relocations[j - 1];
00793 relocations[j - 1] = relocations[j];
00794 relocations[j] = tmpReloc;
00795
00796 madeSwap = 1;
00797 }
00798 if (!madeSwap) break;
00799 }
00800
00801 if (!_printed) {
00802 _printed = 1;
00803 rpmMessage(RPMMESS_DEBUG, _("========== relocations\n"));
00804 for (i = 0; i < numRelocations; i++) {
00805 if (relocations[i].oldPath == NULL) continue;
00806 if (relocations[i].newPath == NULL)
00807 rpmMessage(RPMMESS_DEBUG, _("%5d exclude %s\n"),
00808 i, relocations[i].oldPath);
00809 else
00810 rpmMessage(RPMMESS_DEBUG, _("%5d relocate %s -> %s\n"),
00811 i, relocations[i].oldPath, relocations[i].newPath);
00812 }
00813 }
00814
00815
00816 if (numValid) {
00817 const char ** actualRelocations;
00818 int numActual;
00819
00820 actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
00821 numActual = 0;
00822 for (i = 0; i < numValid; i++) {
00823 for (j = 0; j < numRelocations; j++) {
00824 if (relocations[j].oldPath == NULL ||
00825 strcmp(validRelocations[i], relocations[j].oldPath))
00826 continue;
00827
00828 if (relocations[j].newPath) {
00829 actualRelocations[numActual] = relocations[j].newPath;
00830 numActual++;
00831 }
00832 break;
00833 }
00834 if (j == numRelocations) {
00835 actualRelocations[numActual] = validRelocations[i];
00836 numActual++;
00837 }
00838 }
00839
00840 if (numActual)
00841 xx = hae(h, RPMTAG_INSTPREFIXES, RPM_STRING_ARRAY_TYPE,
00842 (void **) actualRelocations, numActual);
00843
00844 actualRelocations = _free(actualRelocations);
00845 validRelocations = hfd(validRelocations, validType);
00846 }
00847
00848 xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &baseNames, &fileCount);
00849 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
00850 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &dirNames, &dirCount);
00851 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fFlags, NULL);
00852 xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fColors, NULL);
00853 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fModes, NULL);
00854
00855 dColors = alloca(dirCount * sizeof(*dColors));
00856 memset(dColors, 0, dirCount * sizeof(*dColors));
00857
00858 newDirIndexes = alloca(sizeof(*newDirIndexes) * fileCount);
00859 memcpy(newDirIndexes, dirIndexes, sizeof(*newDirIndexes) * fileCount);
00860 dirIndexes = newDirIndexes;
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870 for (i = fileCount - 1; i >= 0; i--) {
00871 fileTypes ft;
00872 int fnlen;
00873
00874 len = reldel +
00875 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
00876
00877 if (len >= fileAlloced) {
00878 fileAlloced = len * 2;
00879 fn = xrealloc(fn, fileAlloced);
00880 }
00881
00882
00883 assert(fn != NULL);
00884 *fn = '\0';
00885 fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
00886
00887 if (fColors != NULL) {
00888
00889 for (j = 0; j < dirCount; j++) {
00890 if (strcmp(dirNames[dirIndexes[i]], dirNames[j])) continue;
00891 dColors[j] |= fColors[i];
00892 }
00893 }
00894
00895
00896
00897
00898
00899
00900
00901
00902 for (j = numRelocations - 1; j >= 0; j--) {
00903 if (relocations[j].oldPath == NULL)
00904 continue;
00905 len = strcmp(relocations[j].oldPath, "/")
00906 ? strlen(relocations[j].oldPath)
00907 : 0;
00908
00909 if (fnlen < len)
00910 continue;
00911
00912
00913
00914
00915 if (!(fn[len] == '/' || fnlen == len))
00916 continue;
00917
00918 if (strncmp(relocations[j].oldPath, fn, len))
00919 continue;
00920 break;
00921 }
00922 if (j < 0) continue;
00923
00924
00925 ft = whatis(fModes[i]);
00926
00927
00928
00929 if (relocations[j].newPath == NULL) {
00930 if (ft == XDIR) {
00931
00932 for (j = dirIndexes[i]; j < dirCount; j++) {
00933 len = strlen(dirNames[j]) - 1;
00934 while (len > 0 && dirNames[j][len-1] == '/') len--;
00935 if (fnlen != len)
00936 continue;
00937 if (strncmp(fn, dirNames[j], fnlen))
00938 continue;
00939 break;
00940 }
00941 }
00942 if (actions) {
00943 actions[i] = FA_SKIPNSTATE;
00944 rpmMessage(RPMMESS_DEBUG, _("excluding %s %s\n"),
00945 ftstring(ft), fn);
00946 }
00947 continue;
00948 }
00949
00950
00951 if (fnlen != len) continue;
00952
00953 if (actions)
00954 rpmMessage(RPMMESS_DEBUG, _("relocating %s to %s\n"),
00955 fn, relocations[j].newPath);
00956 nrelocated++;
00957
00958 strcpy(fn, relocations[j].newPath);
00959 { char * te = strrchr(fn, '/');
00960 if (te) {
00961 if (te > fn) te++;
00962 fnlen = te - fn;
00963 } else
00964 te = fn + strlen(fn);
00965
00966 if (strcmp(baseNames[i], te))
00967 baseNames[i] = alloca_strdup(te);
00968 *te = '\0';
00969
00970 }
00971
00972
00973 for (j = 0; j < dirCount; j++) {
00974 if (fnlen != strlen(dirNames[j]))
00975 continue;
00976 if (strncmp(fn, dirNames[j], fnlen))
00977 continue;
00978 break;
00979 }
00980
00981 if (j < dirCount) {
00982 dirIndexes[i] = j;
00983 continue;
00984 }
00985
00986
00987 if (!haveRelocatedFile) {
00988 const char ** newDirList;
00989
00990 haveRelocatedFile = 1;
00991 newDirList = xmalloc((dirCount + 1) * sizeof(*newDirList));
00992 for (j = 0; j < dirCount; j++)
00993 newDirList[j] = alloca_strdup(dirNames[j]);
00994 dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
00995 dirNames = newDirList;
00996 } else {
00997 dirNames = xrealloc(dirNames,
00998 sizeof(*dirNames) * (dirCount + 1));
00999 }
01000
01001 dirNames[dirCount] = alloca_strdup(fn);
01002 dirIndexes[i] = dirCount;
01003 dirCount++;
01004 }
01005
01006
01007 for (i = dirCount - 1; i >= 0; i--) {
01008 for (j = numRelocations - 1; j >= 0; j--) {
01009
01010
01011 if (j == p->autorelocatex
01012 && (dColors[i] == 0 || !(dColors[i] & mydColor)))
01013 continue;
01014
01015 if (relocations[j].oldPath == NULL)
01016 continue;
01017 len = strcmp(relocations[j].oldPath, "/")
01018 ? strlen(relocations[j].oldPath)
01019 : 0;
01020
01021 if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
01022 continue;
01023
01024
01025
01026
01027
01028 if (dirNames[i][len] != '/')
01029 continue;
01030
01031 if (relocations[j].newPath) {
01032 const char * s = relocations[j].newPath;
01033 char * t = alloca(strlen(s) + strlen(dirNames[i]) - len + 1);
01034 size_t slen;
01035
01036 (void) stpcpy( stpcpy(t, s) , dirNames[i] + len);
01037
01038
01039 (void) rpmCleanPath(t);
01040 slen = strlen(t);
01041 t[slen] = '/';
01042 t[slen+1] = '\0';
01043
01044 if (actions)
01045 rpmMessage(RPMMESS_DEBUG,
01046 _("relocating directory %s to %s\n"), dirNames[i], t);
01047 dirNames[i] = t;
01048 nrelocated++;
01049 }
01050 }
01051 }
01052
01053
01054 if (nrelocated) {
01055 int c;
01056 void * d;
01057 rpmTagType t;
01058
01059 d = NULL;
01060 xx = hge(h, RPMTAG_BASENAMES, &t, &d, &c);
01061 xx = hae(h, RPMTAG_ORIGBASENAMES, t, d, c);
01062 d = hfd(d, t);
01063
01064 d = NULL;
01065 xx = hge(h, RPMTAG_DIRNAMES, &t, &d, &c);
01066 xx = hae(h, RPMTAG_ORIGDIRNAMES, t, d, c);
01067 d = hfd(d, t);
01068
01069 d = NULL;
01070 xx = hge(h, RPMTAG_DIRINDEXES, &t, &d, &c);
01071 xx = hae(h, RPMTAG_ORIGDIRINDEXES, t, d, c);
01072 d = hfd(d, t);
01073
01074 xx = hme(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
01075 baseNames, fileCount);
01076 fi->bnl = hfd(fi->bnl, RPM_STRING_ARRAY_TYPE);
01077 xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc);
01078
01079 xx = hme(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
01080 dirNames, dirCount);
01081 fi->dnl = hfd(fi->dnl, RPM_STRING_ARRAY_TYPE);
01082 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
01083
01084 xx = hme(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE,
01085 dirIndexes, fileCount);
01086 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
01087 }
01088
01089 baseNames = hfd(baseNames, RPM_STRING_ARRAY_TYPE);
01090 dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
01091
01092 fn = _free(fn);
01093
01094
01095 return h;
01096 }
01097
01098
01099 rpmfi rpmfiFree(rpmfi fi)
01100 {
01101 HFD_t hfd = headerFreeData;
01102
01103 if (fi == NULL) return NULL;
01104
01105 if (fi->nrefs > 1)
01106 return rpmfiUnlink(fi, fi->Type);
01107
01108
01109 if (_rpmfi_debug < 0)
01110 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
01111
01112
01113
01114 fi->pretrans = _free(fi->pretrans);
01115 fi->pretransprog = _free(fi->pretransprog);
01116 fi->posttrans = _free(fi->posttrans);
01117 fi->posttransprog = _free(fi->posttransprog);
01118
01119
01120 if (fi->fc > 0) {
01121 fi->bnl = hfd(fi->bnl, -1);
01122 fi->dnl = hfd(fi->dnl, -1);
01123
01124 fi->flinks = hfd(fi->flinks, -1);
01125 fi->flangs = hfd(fi->flangs, -1);
01126 fi->fmd5s = hfd(fi->fmd5s, -1);
01127 fi->md5s = _free(fi->md5s);
01128
01129 fi->cdict = hfd(fi->cdict, -1);
01130
01131 fi->fuser = hfd(fi->fuser, -1);
01132 fi->fgroup = hfd(fi->fgroup, -1);
01133
01134 fi->fstates = _free(fi->fstates);
01135
01136
01137 if (!fi->keep_header && fi->h == NULL) {
01138 fi->fmtimes = _free(fi->fmtimes);
01139 fi->fmodes = _free(fi->fmodes);
01140 fi->fflags = _free(fi->fflags);
01141 fi->vflags = _free(fi->vflags);
01142 fi->fsizes = _free(fi->fsizes);
01143 fi->frdevs = _free(fi->frdevs);
01144 fi->finodes = _free(fi->finodes);
01145 fi->dil = _free(fi->dil);
01146
01147 fi->fcolors = _free(fi->fcolors);
01148 fi->fcdictx = _free(fi->fcdictx);
01149 fi->ddict = _free(fi->ddict);
01150 fi->fddictx = _free(fi->fddictx);
01151 fi->fddictn = _free(fi->fddictn);
01152
01153 }
01154
01155 }
01156
01157
01158 fi->fsm = freeFSM(fi->fsm);
01159
01160 fi->fn = _free(fi->fn);
01161 fi->apath = _free(fi->apath);
01162 fi->fmapflags = _free(fi->fmapflags);
01163
01164 fi->obnl = hfd(fi->obnl, -1);
01165 fi->odnl = hfd(fi->odnl, -1);
01166
01167 fi->fcontexts = hfd(fi->fcontexts, -1);
01168
01169 fi->actions = _free(fi->actions);
01170 fi->replacedSizes = _free(fi->replacedSizes);
01171 fi->replaced = _free(fi->replaced);
01172
01173 fi->h = headerFree(fi->h);
01174
01175
01176 (void) rpmfiUnlink(fi, fi->Type);
01177 memset(fi, 0, sizeof(*fi));
01178 fi = _free(fi);
01179
01180
01181 return NULL;
01182 }
01183
01189 static inline unsigned char nibble(char c)
01190
01191 {
01192 if (c >= '0' && c <= '9')
01193 return (c - '0');
01194 if (c >= 'A' && c <= 'F')
01195 return (c - 'A') + 10;
01196 if (c >= 'a' && c <= 'f')
01197 return (c - 'a') + 10;
01198 return 0;
01199 }
01200
01201 #define _fdupe(_fi, _data) \
01202 if ((_fi)->_data != NULL) \
01203 (_fi)->_data = memcpy(xmalloc((_fi)->fc * sizeof(*(_fi)->_data)), \
01204 (_fi)->_data, (_fi)->fc * sizeof(*(_fi)->_data))
01205
01206
01207 #define _fdupestring(_h, _tag, _data) \
01208 if (hge((_h), (_tag), NULL, (void **) &(_data), NULL)) \
01209 _data = xstrdup(_data)
01210
01211 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, int scareMem)
01212 {
01213 HGE_t hge =
01214 (scareMem ? (HGE_t) headerGetEntryMinMemory : (HGE_t) headerGetEntry);
01215 HFD_t hfd = headerFreeData;
01216 rpmte p;
01217 rpmfi fi = NULL;
01218 const char * Type;
01219 uint_32 * uip;
01220 int dnlmax, bnlmax;
01221 unsigned char * t;
01222 int len;
01223 int xx;
01224 int i;
01225
01226 if (tagN == RPMTAG_BASENAMES) {
01227 Type = "Files";
01228 } else {
01229 Type = "?Type?";
01230 goto exit;
01231 }
01232
01233 fi = xcalloc(1, sizeof(*fi));
01234 if (fi == NULL)
01235 goto exit;
01236
01237 fi->magic = RPMFIMAGIC;
01238 fi->Type = Type;
01239 fi->i = -1;
01240 fi->tagN = tagN;
01241
01242 fi->hge = hge;
01243 fi->hae = (HAE_t) headerAddEntry;
01244 fi->hme = (HME_t) headerModifyEntry;
01245 fi->hre = (HRE_t) headerRemoveEntry;
01246 fi->hfd = headerFreeData;
01247
01248 fi->h = (scareMem ? headerLink(h) : NULL);
01249
01250 if (fi->fsm == NULL)
01251 fi->fsm = newFSM();
01252
01253
01254 xx = hge(h, RPMTAG_ARCHIVESIZE, NULL, (void **) &uip, NULL);
01255 fi->archivePos = 0;
01256 fi->archiveSize = (xx ? *uip : 0);
01257
01258
01259 _fdupestring(h, RPMTAG_PRETRANS, fi->pretrans);
01260 _fdupestring(h, RPMTAG_PRETRANSPROG, fi->pretransprog);
01261 _fdupestring(h, RPMTAG_POSTTRANS, fi->posttrans);
01262 _fdupestring(h, RPMTAG_POSTTRANSPROG, fi->posttransprog);
01263
01264 if (!hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc)) {
01265 fi->fc = 0;
01266 fi->dc = 0;
01267 goto exit;
01268 }
01269 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
01270 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
01271 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fi->fmodes, NULL);
01272 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fi->fflags, NULL);
01273 xx = hge(h, RPMTAG_FILEVERIFYFLAGS, NULL, (void **) &fi->vflags, NULL);
01274 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &fi->fsizes, NULL);
01275
01276 xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fi->fcolors, NULL);
01277 fi->color = 0;
01278 if (fi->fcolors != NULL)
01279 for (i = 0; i < fi->fc; i++)
01280 fi->color |= fi->fcolors[i];
01281 xx = hge(h, RPMTAG_CLASSDICT, NULL, (void **) &fi->cdict, &fi->ncdict);
01282 xx = hge(h, RPMTAG_FILECLASS, NULL, (void **) &fi->fcdictx, NULL);
01283
01284 xx = hge(h, RPMTAG_DEPENDSDICT, NULL, (void **) &fi->ddict, &fi->nddict);
01285 xx = hge(h, RPMTAG_FILEDEPENDSX, NULL, (void **) &fi->fddictx, NULL);
01286 xx = hge(h, RPMTAG_FILEDEPENDSN, NULL, (void **) &fi->fddictn, NULL);
01287
01288 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &fi->fstates, NULL);
01289 if (xx == 0 || fi->fstates == NULL)
01290 fi->fstates = xcalloc(fi->fc, sizeof(*fi->fstates));
01291 else
01292 _fdupe(fi, fstates);
01293
01294 fi->action = FA_UNKNOWN;
01295 fi->flags = 0;
01296
01297 if (fi->actions == NULL)
01298 fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
01299
01300 fi->keep_header = (scareMem ? 1 : 0);
01301
01302
01303 fi->mapflags =
01304 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
01305
01306 xx = hge(h, RPMTAG_FILELINKTOS, NULL, (void **) &fi->flinks, NULL);
01307 xx = hge(h, RPMTAG_FILELANGS, NULL, (void **) &fi->flangs, NULL);
01308
01309 fi->fmd5s = NULL;
01310 xx = hge(h, RPMTAG_FILEMD5S, NULL, (void **) &fi->fmd5s, NULL);
01311
01312 fi->md5s = NULL;
01313 if (fi->fmd5s) {
01314 t = xmalloc(fi->fc * 16);
01315 fi->md5s = t;
01316 for (i = 0; i < fi->fc; i++) {
01317 const char * fmd5;
01318 int j;
01319
01320 fmd5 = fi->fmd5s[i];
01321 if (!(fmd5 && *fmd5 != '\0')) {
01322 memset(t, 0, 16);
01323 t += 16;
01324 continue;
01325 }
01326 for (j = 0; j < 16; j++, t++, fmd5 += 2)
01327 *t = (nibble(fmd5[0]) << 4) | nibble(fmd5[1]);
01328 }
01329 fi->fmd5s = hfd(fi->fmd5s, -1);
01330 }
01331
01332
01333 xx = hge(h, RPMTAG_FILEMTIMES, NULL, (void **) &fi->fmtimes, NULL);
01334 xx = hge(h, RPMTAG_FILERDEVS, NULL, (void **) &fi->frdevs, NULL);
01335 xx = hge(h, RPMTAG_FILEINODES, NULL, (void **) &fi->finodes, NULL);
01336 xx = hge(h, RPMTAG_FILECONTEXTS, NULL, (void **) &fi->fcontexts, NULL);
01337
01338 fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
01339
01340 xx = hge(h, RPMTAG_FILEUSERNAME, NULL, (void **) &fi->fuser, NULL);
01341 xx = hge(h, RPMTAG_FILEGROUPNAME, NULL, (void **) &fi->fgroup, NULL);
01342
01343 if (ts != NULL)
01344 if (fi != NULL)
01345 if ((p = rpmtsRelocateElement(ts)) != NULL && rpmteType(p) == TR_ADDED
01346 && !headerIsEntry(h, RPMTAG_SOURCEPACKAGE)
01347 && !headerIsEntry(h, RPMTAG_ORIGBASENAMES))
01348 {
01349 const char * fmt = rpmGetPath("%{?_autorelocate_path}", NULL);
01350 const char * errstr;
01351 char * newPath;
01352 Header foo;
01353
01354
01355 newPath = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
01356 fmt = _free(fmt);
01357
01358 #if __ia64__
01359
01360 if (newPath != NULL && *newPath != '\0'
01361 && strlen(newPath) >= (sizeof("/emul/i386")-1)
01362 && newPath[0] == '/' && newPath[1] == 'e' && newPath[2] == 'm'
01363 && newPath[3] == 'u' && newPath[4] == 'l' && newPath[5] == '/'
01364 && newPath[6] == 'i' && newPath[8] == '8' && newPath[9] == '6')
01365 {
01366 newPath[7] = 'a';
01367 newPath[8] = '3';
01368 newPath[9] = '2';
01369 }
01370 #endif
01371
01372
01373 i = p->nrelocs;
01374 if (newPath != NULL && *newPath != '\0' && p->relocs != NULL)
01375 for (i = 0; i < p->nrelocs; i++) {
01376
01377 if (strcmp(p->relocs[i].oldPath, "/"))
01378 continue;
01379 if (strcmp(p->relocs[i].newPath, newPath))
01380 continue;
01381
01382 break;
01383 }
01384
01385
01386 if (newPath != NULL && *newPath != '\0' && i == p->nrelocs
01387 && p->archScore == 0)
01388 {
01389
01390 p->relocs =
01391 xrealloc(p->relocs, (p->nrelocs + 2) * sizeof(*p->relocs));
01392 p->relocs[p->nrelocs].oldPath = xstrdup("/");
01393 p->relocs[p->nrelocs].newPath = xstrdup(newPath);
01394 p->autorelocatex = p->nrelocs;
01395 p->nrelocs++;
01396 p->relocs[p->nrelocs].oldPath = NULL;
01397 p->relocs[p->nrelocs].newPath = NULL;
01398 }
01399 newPath = _free(newPath);
01400
01401
01402 if (fi->actions == NULL)
01403 fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
01404
01405 foo = relocateFileList(ts, fi, h, fi->actions);
01406
01407 fi->h = headerFree(fi->h);
01408 fi->h = headerLink(foo);
01409 foo = headerFree(foo);
01410 }
01411
01412 if (!scareMem) {
01413 _fdupe(fi, fmtimes);
01414 _fdupe(fi, frdevs);
01415 _fdupe(fi, finodes);
01416 _fdupe(fi, fsizes);
01417 _fdupe(fi, fflags);
01418 _fdupe(fi, vflags);
01419 _fdupe(fi, fmodes);
01420 _fdupe(fi, dil);
01421
01422 _fdupe(fi, fcolors);
01423 _fdupe(fi, fcdictx);
01424
01425 if (fi->ddict != NULL)
01426 fi->ddict = memcpy(xmalloc(fi->nddict * sizeof(*fi->ddict)),
01427 fi->ddict, fi->nddict * sizeof(*fi->ddict));
01428
01429 _fdupe(fi, fddictx);
01430 _fdupe(fi, fddictn);
01431
01432 fi->h = headerFree(fi->h);
01433 }
01434
01435 dnlmax = -1;
01436 for (i = 0; i < fi->dc; i++) {
01437 if ((len = strlen(fi->dnl[i])) > dnlmax)
01438 dnlmax = len;
01439 }
01440 bnlmax = -1;
01441 for (i = 0; i < fi->fc; i++) {
01442 if ((len = strlen(fi->bnl[i])) > bnlmax)
01443 bnlmax = len;
01444 }
01445 fi->fnlen = dnlmax + bnlmax + 1;
01446 fi->fn = NULL;
01447
01448 fi->dperms = 0755;
01449 fi->fperms = 0644;
01450
01451 exit:
01452
01453 if (_rpmfi_debug < 0)
01454 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
01455
01456
01457
01458 return rpmfiLink(fi, (fi ? fi->Type : NULL));
01459
01460 }
01461
01462 void rpmfiBuildFClasses(Header h,
01463 const char *** fclassp, int * fcp)
01464 {
01465 int scareMem = 0;
01466 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01467 const char * FClass;
01468 const char ** av;
01469 int ac;
01470 size_t nb;
01471 char * t;
01472
01473 if ((ac = rpmfiFC(fi)) <= 0) {
01474 av = NULL;
01475 ac = 0;
01476 goto exit;
01477 }
01478
01479
01480 nb = (ac + 1) * sizeof(*av);
01481 fi = rpmfiInit(fi, 0);
01482 if (fi != NULL)
01483 while (rpmfiNext(fi) >= 0) {
01484 FClass = rpmfiFClass(fi);
01485 if (FClass && *FClass != '\0')
01486 nb += strlen(FClass);
01487 nb += 1;
01488 }
01489
01490
01491 av = xmalloc(nb);
01492 t = ((char *) av) + ((ac + 1) * sizeof(*av));
01493 ac = 0;
01494 fi = rpmfiInit(fi, 0);
01495 if (fi != NULL)
01496 while (rpmfiNext(fi) >= 0) {
01497 FClass = rpmfiFClass(fi);
01498 av[ac++] = t;
01499 if (FClass && *FClass != '\0')
01500 t = stpcpy(t, FClass);
01501 *t++ = '\0';
01502 }
01503 av[ac] = NULL;
01504
01505
01506 exit:
01507 fi = rpmfiFree(fi);
01508
01509 if (fclassp)
01510 *fclassp = av;
01511 else
01512 av = _free(av);
01513
01514 if (fcp) *fcp = ac;
01515 }
01516
01517 void rpmfiBuildFContexts(Header h,
01518 const char *** fcontextp, int * fcp)
01519 {
01520 int scareMem = 0;
01521 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01522 const char * fcontext;
01523 const char ** av;
01524 int ac;
01525 size_t nb;
01526 char * t;
01527
01528 if ((ac = rpmfiFC(fi)) <= 0) {
01529 av = NULL;
01530 ac = 0;
01531 goto exit;
01532 }
01533
01534
01535 nb = (ac + 1) * sizeof(*av);
01536 fi = rpmfiInit(fi, 0);
01537 if (fi != NULL)
01538 while (rpmfiNext(fi) >= 0) {
01539 fcontext = rpmfiFContext(fi);
01540 if (fcontext && *fcontext != '\0')
01541 nb += strlen(fcontext);
01542 nb += 1;
01543 }
01544
01545
01546 av = xmalloc(nb);
01547 t = ((char *) av) + ((ac + 1) * sizeof(*av));
01548 ac = 0;
01549 fi = rpmfiInit(fi, 0);
01550 if (fi != NULL)
01551 while (rpmfiNext(fi) >= 0) {
01552 fcontext = rpmfiFContext(fi);
01553 av[ac++] = t;
01554 if (fcontext && *fcontext != '\0')
01555 t = stpcpy(t, fcontext);
01556 *t++ = '\0';
01557 }
01558 av[ac] = NULL;
01559
01560
01561 exit:
01562 fi = rpmfiFree(fi);
01563
01564 if (fcontextp)
01565 *fcontextp = av;
01566 else
01567 av = _free(av);
01568
01569 if (fcp) *fcp = ac;
01570 }
01571
01572 void rpmfiBuildFSContexts(Header h,
01573 const char *** fcontextp, int * fcp)
01574 {
01575 int scareMem = 0;
01576 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01577 const char ** av;
01578 int ac;
01579 size_t nb;
01580 char * t;
01581 char * fctxt = NULL;
01582 size_t fctxtlen = 0;
01583 int * fcnb;
01584
01585 if ((ac = rpmfiFC(fi)) <= 0) {
01586 av = NULL;
01587 ac = 0;
01588 goto exit;
01589 }
01590
01591
01592 nb = ac * sizeof(*fcnb);
01593 fcnb = memset(alloca(nb), 0, nb);
01594 ac = 0;
01595 fi = rpmfiInit(fi, 0);
01596 if (fi != NULL)
01597 while (rpmfiNext(fi) >= 0) {
01598 const char * fn = rpmfiFN(fi);
01599 security_context_t scon;
01600
01601 fcnb[ac] = lgetfilecon(fn, &scon);
01602
01603 if (fcnb[ac] > 0) {
01604 fctxt = xrealloc(fctxt, fctxtlen + fcnb[ac]);
01605 memcpy(fctxt+fctxtlen, scon, fcnb[ac]);
01606 fctxtlen += fcnb[ac];
01607 freecon(scon);
01608 }
01609
01610 ac++;
01611 }
01612
01613
01614 nb = (ac + 1) * sizeof(*av) + fctxtlen;
01615 av = xmalloc(nb);
01616 t = ((char *) av) + ((ac + 1) * sizeof(*av));
01617 if (fctxt != NULL && fctxtlen > 0)
01618 (void) memcpy(t, fctxt, fctxtlen);
01619 ac = 0;
01620 fi = rpmfiInit(fi, 0);
01621 if (fi != NULL)
01622 while (rpmfiNext(fi) >= 0) {
01623 av[ac] = "";
01624 if (fcnb[ac] > 0) {
01625 av[ac] = t;
01626 t += fcnb[ac];
01627 }
01628 ac++;
01629 }
01630 av[ac] = NULL;
01631
01632 exit:
01633 fi = rpmfiFree(fi);
01634
01635 if (fcontextp)
01636 *fcontextp = av;
01637 else
01638 av = _free(av);
01639
01640 if (fcp) *fcp = ac;
01641 }
01642
01643 void rpmfiBuildREContexts(Header h,
01644 const char *** fcontextp, int * fcp)
01645 {
01646 int scareMem = 0;
01647 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01648 const char ** av = NULL;
01649 const char * myfn = rpmGetPath("%{?__file_context_path}", NULL);
01650 int ac;
01651 size_t nb;
01652 char * t;
01653 char * fctxt = NULL;
01654 size_t fctxtlen = 0;
01655 int * fcnb;
01656
01657 if ((ac = rpmfiFC(fi)) <= 0) {
01658 ac = 0;
01659 goto exit;
01660 }
01661
01662
01663 matchpathcon_init(myfn);
01664
01665
01666 nb = ac * sizeof(*fcnb);
01667 fcnb = memset(alloca(nb), 0, nb);
01668 ac = 0;
01669 fi = rpmfiInit(fi, 0);
01670 if (fi != NULL)
01671 while (rpmfiNext(fi) >= 0) {
01672 const char * fn = rpmfiFN(fi);
01673 mode_t fmode = rpmfiFMode(fi);
01674 security_context_t scon;
01675
01676 if (matchpathcon(fn, fmode, &scon) == 0) {
01677 fcnb[ac] = strlen(scon) + 1;
01678
01679 if (fcnb[ac] > 0) {
01680 fctxt = xrealloc(fctxt, fctxtlen + fcnb[ac]);
01681 memcpy(fctxt+fctxtlen, scon, fcnb[ac]);
01682 fctxtlen += fcnb[ac];
01683 }
01684 freecon(scon);
01685
01686 }
01687 ac++;
01688 }
01689
01690
01691 nb = (ac + 1) * sizeof(*av) + fctxtlen;
01692 av = xmalloc(nb);
01693 t = ((char *) av) + ((ac + 1) * sizeof(*av));
01694 (void) memcpy(t, fctxt, fctxtlen);
01695 ac = 0;
01696 fi = rpmfiInit(fi, 0);
01697 if (fi != NULL)
01698 while (rpmfiNext(fi) >= 0) {
01699 av[ac] = "";
01700 if (fcnb[ac] > 0) {
01701 av[ac] = t;
01702 t += fcnb[ac];
01703 }
01704 ac++;
01705 }
01706 av[ac] = NULL;
01707
01708 exit:
01709 fi = rpmfiFree(fi);
01710
01711 if (fcontextp)
01712 *fcontextp = av;
01713 else
01714 av = _free(av);
01715
01716 if (fcp) *fcp = ac;
01717 }
01718
01719 void rpmfiBuildFDeps(Header h, rpmTag tagN,
01720 const char *** fdepsp, int * fcp)
01721 {
01722 int scareMem = 0;
01723 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
01724 rpmds ds = NULL;
01725 const char ** av;
01726 int ac;
01727 size_t nb;
01728 char * t;
01729 char deptype = 'R';
01730 char mydt;
01731 const char * DNEVR;
01732 const int_32 * ddict;
01733 unsigned ix;
01734 int ndx;
01735
01736 if ((ac = rpmfiFC(fi)) <= 0) {
01737 av = NULL;
01738 ac = 0;
01739 goto exit;
01740 }
01741
01742 if (tagN == RPMTAG_PROVIDENAME)
01743 deptype = 'P';
01744 else if (tagN == RPMTAG_REQUIRENAME)
01745 deptype = 'R';
01746
01747 ds = rpmdsNew(h, tagN, scareMem);
01748
01749
01750 nb = (ac + 1) * sizeof(*av);
01751 fi = rpmfiInit(fi, 0);
01752 if (fi != NULL)
01753 while (rpmfiNext(fi) >= 0) {
01754 ddict = NULL;
01755 ndx = rpmfiFDepends(fi, &ddict);
01756 if (ddict != NULL)
01757 while (ndx-- > 0) {
01758 ix = *ddict++;
01759 mydt = ((ix >> 24) & 0xff);
01760 if (mydt != deptype)
01761 continue;
01762 ix &= 0x00ffffff;
01763 (void) rpmdsSetIx(ds, ix-1);
01764 if (rpmdsNext(ds) < 0)
01765 continue;
01766 DNEVR = rpmdsDNEVR(ds);
01767 if (DNEVR != NULL)
01768 nb += strlen(DNEVR+2) + 1;
01769 }
01770 nb += 1;
01771 }
01772
01773
01774 av = xmalloc(nb);
01775 t = ((char *) av) + ((ac + 1) * sizeof(*av));
01776 ac = 0;
01777
01778 fi = rpmfiInit(fi, 0);
01779 if (fi != NULL)
01780 while (rpmfiNext(fi) >= 0) {
01781 av[ac++] = t;
01782 ddict = NULL;
01783 ndx = rpmfiFDepends(fi, &ddict);
01784 if (ddict != NULL)
01785 while (ndx-- > 0) {
01786 ix = *ddict++;
01787 mydt = ((ix >> 24) & 0xff);
01788 if (mydt != deptype)
01789 continue;
01790 ix &= 0x00ffffff;
01791 (void) rpmdsSetIx(ds, ix-1);
01792 if (rpmdsNext(ds) < 0)
01793 continue;
01794 DNEVR = rpmdsDNEVR(ds);
01795 if (DNEVR != NULL) {
01796 t = stpcpy(t, DNEVR+2);
01797 *t++ = ' ';
01798 *t = '\0';
01799 }
01800 }
01801 *t++ = '\0';
01802 }
01803
01804 av[ac] = NULL;
01805
01806 exit:
01807 fi = rpmfiFree(fi);
01808 ds = rpmdsFree(ds);
01809
01810 if (fdepsp)
01811 *fdepsp = av;
01812 else
01813 av = _free(av);
01814
01815 if (fcp) *fcp = ac;
01816 }