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