3 * Routines to handle file info tag sets.
8 #include <rpm/rpmlog.h>
10 #include <rpm/rpmfileutil.h> /* XXX rpmDoDigest */
11 #include <rpm/rpmstring.h>
12 #include <rpm/rpmmacro.h> /* XXX rpmCleanPath */
13 #include <rpm/rpmds.h>
15 #include "lib/rpmfi_internal.h"
16 #include "lib/rpmte_internal.h" /* relocations */
17 #include "lib/cpio.h" /* XXX CPIO_FOO */
18 #include "lib/fsm.h" /* XXX newFSM() */
23 * Simple and stupid string "cache."
24 * Store each unique string just once, retrieve by index value.
25 * For data where number of unique names is typically very low,
26 * the dumb linear lookup appears to be fast enough and hash table seems
34 static struct strcache_s _ugcache = { NULL, 0 };
35 static strcache ugcache = &_ugcache;
36 static struct strcache_s _langcache = { NULL, 0 };
37 static strcache langcache = &_langcache;
39 static scidx_t strcachePut(strcache cache, const char *str)
44 for (scidx_t i = 0; i < cache->num; i++) {
45 if (strcmp(str, cache->uniq[i]) == 0) {
52 /* blow up on index wraparound */
53 assert((scidx_t)(cache->num + 1) > cache->num);
54 cache->uniq = xrealloc(cache->uniq,
55 sizeof(cache->uniq) * (cache->num+1));
56 cache->uniq[cache->num] = xstrdup(str);
63 static const char *strcacheGet(strcache cache, scidx_t idx)
65 const char *name = NULL;
66 if (idx >= 0 && idx < cache->num && cache->uniq != NULL)
67 name = cache->uniq[idx];
71 static strcache strcacheNew(void)
73 strcache cache = xcalloc(1, sizeof(*cache));
77 static strcache strcacheFree(strcache cache)
80 for (scidx_t i = 0; i < cache->num; i++) {
83 cache->uniq = _free(cache->uniq);
92 rpmfi rpmfiUnlink(rpmfi fi, const char * msg)
94 if (fi == NULL) return NULL;
95 if (_rpmfi_debug && msg != NULL)
96 fprintf(stderr, "--> fi %p -- %d %s\n", fi, fi->nrefs, msg);
101 rpmfi rpmfiLink(rpmfi fi, const char * msg)
103 if (fi == NULL) return NULL;
105 if (_rpmfi_debug && msg != NULL)
106 fprintf(stderr, "--> fi %p ++ %d %s\n", fi, fi->nrefs, msg);
110 rpm_count_t rpmfiFC(rpmfi fi)
112 return (fi != NULL ? fi->fc : 0);
115 rpm_count_t rpmfiDC(rpmfi fi)
117 return (fi != NULL ? fi->dc : 0);
121 int rpmfiDI(rpmfi fi)
126 int rpmfiFX(rpmfi fi)
128 return (fi != NULL ? fi->i : -1);
131 int rpmfiSetFX(rpmfi fi, int fx)
135 if (fi != NULL && fx >= 0 && fx < fi->fc) {
138 fi->j = fi->dil[fi->i];
143 int rpmfiDX(rpmfi fi)
145 return (fi != NULL ? fi->j : -1);
148 int rpmfiSetDX(rpmfi fi, int dx)
152 if (fi != NULL && dx >= 0 && dx < fi->dc) {
159 int rpmfiDIIndex(rpmfi fi, int dx)
162 if (fi != NULL && dx >= 0 && dx < fi->fc) {
169 const char * rpmfiBNIndex(rpmfi fi, int ix)
171 const char * BN = NULL;
173 if (fi != NULL && ix >= 0 && ix < fi->fc) {
180 const char * rpmfiDNIndex(rpmfi fi, int jx)
182 const char * DN = NULL;
184 if (fi != NULL && jx >= 0 && jx < fi->dc) {
191 const char * rpmfiFNIndex(rpmfi fi, int ix)
193 const char * FN = "";
195 if (fi != NULL && ix >= 0 && ix < fi->fc) {
197 if (fi->fn == NULL) {
198 size_t dnlmax = 0, bnlmax = 0, len;
199 for (int i = 0; i < fi->dc; i++) {
200 if ((len = strlen(fi->dnl[i])) > dnlmax)
203 for (int i = 0; i < fi->fc; i++) {
204 if ((len = strlen(fi->bnl[i])) > bnlmax)
207 fi->fn = xmalloc(dnlmax + bnlmax + 1);
211 t = stpcpy(t, fi->dnl[fi->dil[ix]]);
212 t = stpcpy(t, fi->bnl[ix]);
217 rpmfileAttrs rpmfiFFlagsIndex(rpmfi fi, int ix)
219 rpmfileAttrs FFlags = 0;
221 if (fi != NULL && ix >= 0 && ix < fi->fc) {
222 if (fi->fflags != NULL)
223 FFlags = fi->fflags[ix];
228 rpmVerifyAttrs rpmfiVFlagsIndex(rpmfi fi, int ix)
230 rpmVerifyAttrs VFlags = 0;
232 if (fi != NULL && ix >= 0 && ix < fi->fc) {
233 if (fi->vflags != NULL)
234 VFlags = fi->vflags[ix];
239 rpm_mode_t rpmfiFModeIndex(rpmfi fi, int ix)
241 rpm_mode_t fmode = 0;
243 if (fi != NULL && ix >= 0 && ix < fi->fc) {
244 if (fi->fmodes != NULL)
245 fmode = fi->fmodes[ix];
250 rpmfileState rpmfiFStateIndex(rpmfi fi, int ix)
252 rpmfileState fstate = RPMFILE_STATE_MISSING;
254 if (fi != NULL && ix >= 0 && ix < fi->fc) {
255 if (fi->fstates != NULL)
256 fstate = fi->fstates[ix];
261 const unsigned char * rpmfiMD5(rpmfi fi)
263 const unsigned char *digest;
264 pgpHashAlgo algo = 0;
266 digest = rpmfiFDigest(fi, &algo, NULL);
267 return (algo == PGPHASHALGO_MD5) ? digest : NULL;
270 pgpHashAlgo rpmfiDigestAlgo(rpmfi fi)
272 return fi ? fi->digestalgo : 0;
275 const unsigned char * rpmfiFDigestIndex(rpmfi fi, int ix, pgpHashAlgo *algo, size_t *len)
277 const unsigned char *digest = NULL;
279 if (fi != NULL && ix >= 0 && ix < fi->fc) {
280 size_t diglen = rpmDigestLength(fi->digestalgo);
281 if (fi->digests != NULL)
282 digest = fi->digests + (diglen * ix);
286 *algo = fi->digestalgo;
291 char * rpmfiFDigestHex(rpmfi fi, pgpHashAlgo *algo)
294 char *fdigest = NULL;
295 const unsigned char *digest = rpmfiFDigest(fi, algo, &diglen);
297 fdigest = pgpHexStr(digest, diglen);
302 const char * rpmfiFLinkIndex(rpmfi fi, int ix)
304 const char * flink = NULL;
306 if (fi != NULL && ix >= 0 && ix < fi->fc) {
307 if (fi->flinks != NULL)
308 flink = strcacheGet(fi->flinkcache, fi->flinks[ix]);
313 rpm_loff_t rpmfiFSizeIndex(rpmfi fi, int ix)
315 rpm_loff_t fsize = 0;
317 if (fi != NULL && ix >= 0 && ix < fi->fc) {
318 if (fi->fsizes != NULL)
319 fsize = fi->fsizes[ix];
324 rpm_rdev_t rpmfiFRdevIndex(rpmfi fi, int ix)
326 rpm_rdev_t frdev = 0;
328 if (fi != NULL && ix >= 0 && ix < fi->fc) {
329 if (fi->frdevs != NULL)
330 frdev = fi->frdevs[ix];
335 rpm_ino_t rpmfiFInodeIndex(rpmfi fi, int ix)
337 rpm_ino_t finode = 0;
339 if (fi != NULL && ix >= 0 && ix < fi->fc) {
340 if (fi->finodes != NULL)
341 finode = fi->finodes[ix];
346 rpm_color_t rpmfiColor(rpmfi fi)
348 rpm_color_t color = 0;
350 if (fi != NULL && fi->fcolors != NULL) {
351 for (int i = 0; i < fi->fc; i++)
352 color |= fi->fcolors[i];
353 /* XXX ignore all but lsnibble for now. */
359 rpm_color_t rpmfiFColorIndex(rpmfi fi, int ix)
361 rpm_color_t fcolor = 0;
363 if (fi != NULL && ix >= 0 && ix < fi->fc) {
364 if (fi->fcolors != NULL)
365 /* XXX ignore all but lsnibble for now. */
366 fcolor = (fi->fcolors[ix] & 0x0f);
371 const char * rpmfiFClassIndex(rpmfi fi, int ix)
373 const char * fclass = NULL;
376 if (fi != NULL && fi->fcdictx != NULL && ix >= 0 && ix < fi->fc) {
377 cdictx = fi->fcdictx[ix];
378 if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
379 fclass = fi->cdict[cdictx];
384 uint32_t rpmfiFDependsIndex(rpmfi fi, int ix, const uint32_t ** fddictp)
388 const uint32_t * fddict = NULL;
390 if (fi != NULL && ix >= 0 && ix < fi->fc) {
391 if (fi->fddictn != NULL)
392 fddictn = fi->fddictn[ix];
393 if (fddictn > 0 && fi->fddictx != NULL)
394 fddictx = fi->fddictx[ix];
395 if (fi->ddict != NULL && fddictx >= 0 && (fddictx+fddictn) <= fi->nddict)
396 fddict = fi->ddict + fddictx;
403 uint32_t rpmfiFNlinkIndex(rpmfi fi, int ix)
407 if (fi != NULL && ix >= 0 && ix < fi->fc) {
408 /* XXX rpm-2.3.12 has not RPMTAG_FILEINODES */
409 if (fi->finodes && fi->frdevs) {
410 rpm_ino_t finode = fi->finodes[ix];
411 rpm_rdev_t frdev = fi->frdevs[ix];
414 for (j = 0; j < fi->fc; j++) {
415 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
423 rpm_time_t rpmfiFMtimeIndex(rpmfi fi, int ix)
425 rpm_time_t fmtime = 0;
427 if (fi != NULL && ix >= 0 && ix < fi->fc) {
428 if (fi->fmtimes != NULL)
429 fmtime = fi->fmtimes[ix];
434 const char * rpmfiFUserIndex(rpmfi fi, int ix)
436 const char * fuser = NULL;
438 if (fi != NULL && ix >= 0 && ix < fi->fc) {
439 if (fi->fuser != NULL)
440 fuser = strcacheGet(ugcache, fi->fuser[ix]);
445 const char * rpmfiFGroupIndex(rpmfi fi, int ix)
447 const char * fgroup = NULL;
449 if (fi != NULL && ix >= 0 && ix < fi->fc) {
450 if (fi->fgroup != NULL)
451 fgroup = strcacheGet(ugcache, fi->fgroup[ix]);
456 const char * rpmfiFCapsIndex(rpmfi fi, int ix)
458 const char *fcaps = NULL;
459 if (fi != NULL && ix >= 0 && ix < fi->fc) {
460 fcaps = fi->fcaps ? fi->fcaps[ix] : "";
465 const char * rpmfiFLangsIndex(rpmfi fi, int ix)
467 const char *flangs = NULL;
468 if (fi != NULL && fi->flangs != NULL && ix >= 0 && ix < fi->fc) {
469 flangs = strcacheGet(langcache, fi->flangs[ix]);
474 struct fingerPrint_s *rpmfiFpsIndex(rpmfi fi, int ix)
476 struct fingerPrint_s * fps = NULL;
477 if (fi != NULL && fi->fps != NULL && ix >= 0 && ix < fi->fc) {
483 int rpmfiNext(rpmfi fi)
487 if (fi != NULL && ++fi->i >= 0) {
488 if (fi->i < fi->fc) {
491 fi->j = fi->dil[fi->i];
495 if (_rpmfi_debug < 0 && i != -1)
496 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] : ""));
503 rpmfi rpmfiInit(rpmfi fi, int fx)
506 if (fx >= 0 && fx < fi->fc) {
515 int rpmfiNextD(rpmfi fi)
519 if (fi != NULL && ++fi->j >= 0) {
525 if (_rpmfi_debug < 0 && j != -1)
526 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, (fi->Type ? fi->Type : "?Type?"), j);
533 rpmfi rpmfiInitD(rpmfi fi, int dx)
536 if (dx >= 0 && dx < fi->fc)
546 * Identify a file type.
547 * @param ft file type
548 * @return string to identify a file type
551 const char * ftstring (rpmFileTypes ft)
554 case XDIR: return "directory";
555 case CDEV: return "char dev";
556 case BDEV: return "block dev";
557 case LINK: return "link";
558 case SOCK: return "sock";
559 case PIPE: return "fifo/pipe";
560 case REG: return "file";
561 default: return "unknown file type";
565 rpmFileTypes rpmfiWhatis(rpm_mode_t mode)
567 if (S_ISDIR(mode)) return XDIR;
568 if (S_ISCHR(mode)) return CDEV;
569 if (S_ISBLK(mode)) return BDEV;
570 if (S_ISLNK(mode)) return LINK;
571 if (S_ISSOCK(mode)) return SOCK;
572 if (S_ISFIFO(mode)) return PIPE;
576 int rpmfiCompare(const rpmfi afi, const rpmfi bfi)
578 rpmFileTypes awhat = rpmfiWhatis(rpmfiFMode(afi));
579 rpmFileTypes bwhat = rpmfiWhatis(rpmfiFMode(bfi));
581 if ((rpmfiFFlags(afi) & RPMFILE_GHOST) ||
582 (rpmfiFFlags(bfi) & RPMFILE_GHOST)) return 0;
584 if (awhat != bwhat) return 1;
587 const char * alink = rpmfiFLink(afi);
588 const char * blink = rpmfiFLink(bfi);
589 if (alink == blink) return 0;
590 if (alink == NULL) return 1;
591 if (blink == NULL) return -1;
592 return strcmp(alink, blink);
593 } else if (awhat == REG) {
594 size_t adiglen, bdiglen;
595 pgpHashAlgo aalgo, balgo;
596 const unsigned char * adigest = rpmfiFDigest(afi, &aalgo, &adiglen);
597 const unsigned char * bdigest = rpmfiFDigest(bfi, &balgo, &bdiglen);
598 if (adigest == bdigest) return 0;
599 if (adigest == NULL) return 1;
600 if (bdigest == NULL) return -1;
601 /* can't meaningfully compare different hash types */
602 if (aalgo != balgo || adiglen != bdiglen) return -1;
603 return memcmp(adigest, bdigest, adiglen);
609 rpmFileAction rpmfiDecideFate(const rpmfi ofi, rpmfi nfi, int skipMissing)
611 const char * fn = rpmfiFN(nfi);
612 rpmfileAttrs newFlags = rpmfiFFlags(nfi);
614 rpmFileTypes dbWhat, newWhat, diskWhat;
616 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
618 if (lstat(fn, &sb)) {
620 * The file doesn't exist on the disk. Create it unless the new
621 * package has marked it as missingok, or allfiles is requested.
623 if (skipMissing && (newFlags & RPMFILE_MISSINGOK)) {
624 rpmlog(RPMLOG_DEBUG, "%s skipped due to missingok flag\n",
632 diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
633 dbWhat = rpmfiWhatis(rpmfiFMode(ofi));
634 newWhat = rpmfiWhatis(rpmfiFMode(nfi));
637 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
638 * them in older packages as well.
643 if (diskWhat != newWhat && dbWhat != REG && dbWhat != LINK)
645 else if (newWhat != dbWhat && diskWhat != dbWhat)
647 else if (dbWhat != newWhat)
649 else if (dbWhat != LINK && dbWhat != REG)
653 * This order matters - we'd prefer to CREATE the file if at all
654 * possible in case something else (like the timestamp) has changed.
656 memset(buffer, 0, sizeof(buffer));
658 pgpHashAlgo oalgo, nalgo;
659 size_t odiglen, ndiglen;
660 const unsigned char * odigest, * ndigest;
661 odigest = rpmfiFDigest(ofi, &oalgo, &odiglen);
662 if (diskWhat == REG) {
663 if (rpmDoDigest(oalgo, fn, 0,
664 (unsigned char *)buffer, NULL))
665 return FA_CREATE; /* assume file has been removed */
666 if (odigest && !memcmp(odigest, buffer, odiglen))
667 return FA_CREATE; /* unmodified config file, replace. */
669 ndigest = rpmfiFDigest(nfi, &nalgo, &ndiglen);
670 /* XXX can't compare different hash types, what should we do here? */
671 if (oalgo != nalgo || odiglen != ndiglen)
673 if (odigest && ndigest && !memcmp(odigest, ndigest, odiglen))
674 return FA_SKIP; /* identical file, don't bother. */
675 } else /* dbWhat == LINK */ {
676 const char * oFLink, * nFLink;
677 oFLink = rpmfiFLink(ofi);
678 if (diskWhat == LINK) {
679 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
680 return FA_CREATE; /* assume file has been removed */
681 if (oFLink && !strcmp(oFLink, buffer))
682 return FA_CREATE; /* unmodified config file, replace. */
684 nFLink = rpmfiFLink(nfi);
685 if (oFLink && nFLink && !strcmp(oFLink, nFLink))
686 return FA_SKIP; /* identical file, don't bother. */
690 * The config file on the disk has been modified, but
691 * the ones in the two packages are different. It would
692 * be nice if RPM was smart enough to at least try and
693 * merge the difference ala CVS, but...
698 int rpmfiConfigConflict(const rpmfi fi)
700 const char * fn = rpmfiFN(fi);
701 rpmfileAttrs flags = rpmfiFFlags(fi);
703 rpmFileTypes newWhat, diskWhat;
706 if (!(flags & RPMFILE_CONFIG) || lstat(fn, &sb)) {
710 diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
711 newWhat = rpmfiWhatis(rpmfiFMode(fi));
713 if (newWhat != LINK && newWhat != REG)
716 if (diskWhat != newWhat)
719 memset(buffer, 0, sizeof(buffer));
720 if (newWhat == REG) {
723 const unsigned char *ndigest = rpmfiFDigest(fi, &algo, &diglen);
724 if (rpmDoDigest(algo, fn, 0, (unsigned char *)buffer, NULL))
725 return 0; /* assume file has been removed */
726 if (ndigest && !memcmp(ndigest, buffer, diglen))
727 return 0; /* unmodified config file */
728 } else /* newWhat == LINK */ {
730 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
731 return 0; /* assume file has been removed */
732 nFLink = rpmfiFLink(fi);
733 if (nFLink && !strcmp(nFLink, buffer))
734 return 0; /* unmodified config file */
740 static char **duparray(char ** src, int size)
742 char **dest = xmalloc((size+1) * sizeof(*dest));
743 for (int i = 0; i < size; i++) {
744 dest[i] = xstrdup(src[i]);
751 * Relocate files in header.
752 * @todo multilib file dispositions need to be checked.
753 * @param ts transaction set
754 * @param fi transaction element file info
755 * @param origH package header
756 * @return header with relocated files
759 Header relocateFileList(const rpmts ts, rpmfi fi,
762 rpmte p = rpmtsRelocateElement(ts);
763 static int _printed = 0;
764 int allowBadRelocate = (rpmtsFilterFlags(ts) & RPMPROB_FILTER_FORCERELOCATE);
765 rpmRelocation * relocations = NULL;
769 uint32_t * dirIndexes;
770 rpm_count_t fileCount, dirCount, numValid = 0;
775 int haveRelocatedBase = 0;
779 struct rpmtd_s validRelocs;
780 struct rpmtd_s bnames, dnames, dindexes, fmodes;
783 if (headerGet(origH, RPMTAG_PREFIXES, &validRelocs, HEADERGET_MINMEM))
784 numValid = rpmtdCount(&validRelocs);
789 while (p->relocs[numRelocations].newPath ||
790 p->relocs[numRelocations].oldPath)
794 * If no relocations are specified (usually the case), then return the
795 * original header. If there are prefixes, however, then INSTPREFIXES
796 * should be added, but, since relocateFileList() can be called more
797 * than once for the same header, don't bother if already present.
799 if (p->relocs == NULL || numRelocations == 0) {
801 if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES)) {
802 rpmtdSetTag(&validRelocs, RPMTAG_INSTPREFIXES);
803 headerPut(origH, &validRelocs, HEADERPUT_DEFAULT);
805 rpmtdFreeData(&validRelocs);
807 /* XXX FIXME multilib file actions need to be checked. */
808 return headerLink(origH);
811 h = headerLink(origH);
813 relocations = xmalloc(sizeof(*relocations) * numRelocations);
815 /* Build sorted relocation list from raw relocations. */
816 for (i = 0; i < numRelocations; i++) {
820 * Default relocations (oldPath == NULL) are handled in the UI,
823 if (p->relocs[i].oldPath == NULL) continue; /* XXX can't happen */
825 /* FIXME: Trailing /'s will confuse us greatly. Internal ones will
826 too, but those are more trouble to fix up. :-( */
827 t = xstrdup(p->relocs[i].oldPath);
828 relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
830 : stripTrailingChar(t, '/');
832 /* An old path w/o a new path is valid, and indicates exclusion */
833 if (p->relocs[i].newPath) {
836 const char *validprefix;
838 t = xstrdup(p->relocs[i].newPath);
839 relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
841 : stripTrailingChar(t, '/');
843 /* FIX: relocations[i].oldPath == NULL */
844 /* Verify that the relocation's old path is in the header. */
845 rpmtdInit(&validRelocs);
846 while ((validprefix = rpmtdNextString(&validRelocs))) {
847 if (strcmp(validprefix, relocations[i].oldPath) == 0) {
853 if (!valid && !allowBadRelocate) {
854 rpmps ps = rpmtsProblems(ts);
855 rpmpsAppend(ps, RPMPROB_BADRELOCATE,
856 rpmteNEVRA(p), rpmteKey(p),
857 relocations[i].oldPath, NULL, NULL, 0);
861 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
866 relocations[i].newPath = NULL;
870 /* stupid bubble sort, but it's probably faster here */
871 for (i = 0; i < numRelocations; i++) {
874 for (j = 1; j < numRelocations; j++) {
875 rpmRelocation tmpReloc;
876 if (relocations[j - 1].oldPath == NULL || /* XXX can't happen */
877 relocations[j ].oldPath == NULL || /* XXX can't happen */
878 strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
881 tmpReloc = relocations[j - 1];
882 relocations[j - 1] = relocations[j];
883 relocations[j] = tmpReloc;
886 if (!madeSwap) break;
891 rpmlog(RPMLOG_DEBUG, "========== relocations\n");
892 for (i = 0; i < numRelocations; i++) {
893 if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
894 if (relocations[i].newPath == NULL)
895 rpmlog(RPMLOG_DEBUG, "%5d exclude %s\n",
896 i, relocations[i].oldPath);
898 rpmlog(RPMLOG_DEBUG, "%5d relocate %s -> %s\n",
899 i, relocations[i].oldPath, relocations[i].newPath);
903 /* Add relocation values to the header */
905 const char *validprefix;
906 const char ** actualRelocations;
907 rpm_count_t numActual;
909 actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
911 rpmtdInit(&validRelocs);
912 while ((validprefix = rpmtdNextString(&validRelocs))) {
913 for (j = 0; j < numRelocations; j++) {
914 if (relocations[j].oldPath == NULL || /* XXX can't happen */
915 strcmp(validprefix, relocations[j].oldPath))
917 /* On install, a relocate to NULL means skip the path. */
918 if (relocations[j].newPath) {
919 actualRelocations[numActual] = relocations[j].newPath;
924 if (j == numRelocations) {
925 actualRelocations[numActual] = validprefix;
931 headerPutStringArray(h, RPMTAG_INSTPREFIXES,
932 actualRelocations, numActual);
935 actualRelocations = _free(actualRelocations);
936 rpmtdFreeData(&validRelocs);
939 headerGet(h, RPMTAG_BASENAMES, &bnames, HEADERGET_MINMEM);
940 headerGet(h, RPMTAG_DIRINDEXES, &dindexes, HEADERGET_ALLOC);
941 headerGet(h, RPMTAG_DIRNAMES, &dnames, HEADERGET_MINMEM);
942 headerGet(h, RPMTAG_FILEMODES, &fmodes, HEADERGET_MINMEM);
943 /* TODO XXX ugh.. use rpmtd iterators & friends instead */
944 baseNames = bnames.data;
945 dirIndexes = dindexes.data;
946 fileCount = rpmtdCount(&bnames);
947 dirCount = rpmtdCount(&dnames);
948 /* XXX TODO: use rpmtdDup() instead */
949 dirNames = dnames.data = duparray(dnames.data, dirCount);
950 dnames.flags |= RPMTD_PTR_ALLOCED;
953 * For all relocations, we go through sorted file/relocation lists
954 * backwards so that /usr/local relocations take precedence over /usr
958 /* Relocate individual paths. */
960 for (i = fileCount - 1; i >= 0; i--) {
965 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
966 if (len >= fileAlloced) {
967 fileAlloced = len * 2;
968 fn = xrealloc(fn, fileAlloced);
971 assert(fn != NULL); /* XXX can't happen */
973 fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
976 * See if this file path needs relocating.
979 * XXX FIXME: Would a bsearch of the (already sorted)
980 * relocation list be a good idea?
982 for (j = numRelocations - 1; j >= 0; j--) {
983 if (relocations[j].oldPath == NULL) /* XXX can't happen */
985 len = strcmp(relocations[j].oldPath, "/")
986 ? strlen(relocations[j].oldPath)
992 * Only subdirectories or complete file paths may be relocated. We
993 * don't check for '\0' as our directory names all end in '/'.
995 if (!(fn[len] == '/' || fnlen == len))
998 if (strncmp(relocations[j].oldPath, fn, len))
1002 if (j < 0) continue;
1004 rpmtdSetIndex(&fmodes, i);
1005 ft = rpmfiWhatis(rpmtdGetNumber(&fmodes));
1007 /* On install, a relocate to NULL means skip the path. */
1008 if (relocations[j].newPath == NULL) {
1010 /* Start with the parent, looking for directory to exclude. */
1011 for (j = dirIndexes[i]; j < dirCount; j++) {
1012 len = strlen(dirNames[j]) - 1;
1013 while (len > 0 && dirNames[j][len-1] == '/') len--;
1016 if (strncmp(fn, dirNames[j], fnlen))
1021 rpmfsSetAction(rpmteGetFileStates(p), i, FA_SKIPNSTATE);
1022 rpmlog(RPMLOG_DEBUG, "excluding %s %s\n",
1027 /* Relocation on full paths only, please. */
1028 if (fnlen != len) continue;
1030 rpmlog(RPMLOG_DEBUG, "relocating %s to %s\n",
1031 fn, relocations[j].newPath);
1034 strcpy(fn, relocations[j].newPath);
1035 { char * te = strrchr(fn, '/');
1037 if (te > fn) te++; /* root is special */
1040 te = fn + strlen(fn);
1041 if (strcmp(baseNames[i], te)) { /* basename changed too? */
1042 if (!haveRelocatedBase) {
1043 /* XXX TODO: use rpmtdDup() instead */
1044 bnames.data = baseNames = duparray(baseNames, fileCount);
1045 bnames.flags |= RPMTD_PTR_ALLOCED;
1046 haveRelocatedBase = 1;
1049 baseNames[i] = xstrdup(te);
1051 *te = '\0'; /* terminate new directory name */
1054 /* Does this directory already exist in the directory list? */
1055 for (j = 0; j < dirCount; j++) {
1056 if (fnlen != strlen(dirNames[j]))
1058 if (strncmp(fn, dirNames[j], fnlen))
1068 /* Creating new paths is a pita */
1069 dirNames = dnames.data = xrealloc(dnames.data,
1070 sizeof(*dirNames) * (dirCount + 1));
1072 dirNames[dirCount] = xstrdup(fn);
1073 dirIndexes[i] = dirCount;
1078 /* Finish off by relocating directories. */
1079 for (i = dirCount - 1; i >= 0; i--) {
1080 for (j = numRelocations - 1; j >= 0; j--) {
1082 if (relocations[j].oldPath == NULL) /* XXX can't happen */
1084 len = strcmp(relocations[j].oldPath, "/")
1085 ? strlen(relocations[j].oldPath)
1088 if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
1092 * Only subdirectories or complete file paths may be relocated. We
1093 * don't check for '\0' as our directory names all end in '/'.
1095 if (dirNames[i][len] != '/')
1098 if (relocations[j].newPath) { /* Relocate the path */
1100 rstrscat(&t, relocations[j].newPath, (dirNames[i] + len), NULL);
1101 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
1102 (void) rpmCleanPath(t);
1105 rpmlog(RPMLOG_DEBUG,
1106 "relocating directory %s to %s\n", dirNames[i], t);
1114 /* Save original filenames in header and replace (relocated) filenames. */
1118 headerGet(h, RPMTAG_BASENAMES, &td, HEADERGET_MINMEM);
1119 rpmtdSetTag(&td, RPMTAG_ORIGBASENAMES);
1120 headerPut(h, &td, HEADERPUT_DEFAULT);
1123 headerGet(h, RPMTAG_DIRNAMES, &td, HEADERGET_MINMEM);
1124 rpmtdSetTag(&td, RPMTAG_ORIGDIRNAMES);
1125 headerPut(h, &td, HEADERPUT_DEFAULT);
1128 headerGet(h, RPMTAG_DIRINDEXES, &td, HEADERGET_MINMEM);
1129 rpmtdSetTag(&td, RPMTAG_ORIGDIRINDEXES);
1130 headerPut(h, &td, HEADERPUT_DEFAULT);
1133 if (rpmtdFromStringArray(&td, RPMTAG_BASENAMES, (const char**) baseNames, fileCount)) {
1137 headerGet(h, RPMTAG_BASENAMES, &td, fi->scareFlags);
1138 fi->fc = rpmtdCount(&td);
1141 if (rpmtdFromStringArray(&td, RPMTAG_DIRNAMES, (const char**) dirNames, dirCount)) {
1145 headerGet(h, RPMTAG_DIRNAMES, &td, fi->scareFlags);
1146 fi->dc = rpmtdCount(&td);
1149 if (rpmtdFromUint32(&td, RPMTAG_DIRINDEXES, dirIndexes, fileCount)) {
1152 headerGet(h, RPMTAG_DIRINDEXES, &td, fi->scareFlags);
1153 /* Ugh, nasty games with how dil is alloced depending on scareMem */
1154 if (fi->scareFlags & HEADERGET_ALLOC)
1159 rpmtdFreeData(&bnames);
1160 rpmtdFreeData(&dnames);
1161 rpmtdFreeData(&dindexes);
1162 rpmtdFreeData(&fmodes);
1164 for (i = 0; i < numRelocations; i++) {
1165 free(relocations[i].oldPath);
1166 free(relocations[i].newPath);
1173 rpmfi rpmfiFree(rpmfi fi)
1175 if (fi == NULL) return NULL;
1178 return rpmfiUnlink(fi, fi->Type);
1180 if (_rpmfi_debug < 0)
1181 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
1184 fi->bnl = _free(fi->bnl);
1185 fi->dnl = _free(fi->dnl);
1187 fi->flinkcache = strcacheFree(fi->flinkcache);
1188 fi->flinks = _free(fi->flinks);
1189 fi->flangs = _free(fi->flangs);
1190 fi->digests = _free(fi->digests);
1191 fi->fcaps = _free(fi->fcaps);
1193 fi->cdict = _free(fi->cdict);
1195 fi->fuser = _free(fi->fuser);
1196 fi->fgroup = _free(fi->fgroup);
1198 fi->fstates = _free(fi->fstates);
1199 fi->fps = _free(fi->fps);
1201 if (!(fi->fiflags & RPMFI_KEEPHEADER) && fi->h == NULL) {
1202 fi->fmtimes = _constfree(fi->fmtimes);
1203 fi->fmodes = _free(fi->fmodes);
1204 fi->fflags = _constfree(fi->fflags);
1205 fi->vflags = _constfree(fi->vflags);
1206 fi->fsizes = _constfree(fi->fsizes);
1207 fi->frdevs = _constfree(fi->frdevs);
1208 fi->finodes = _constfree(fi->finodes);
1209 fi->dil = _free(fi->dil);
1211 fi->fcolors = _constfree(fi->fcolors);
1212 fi->fcdictx = _constfree(fi->fcdictx);
1213 fi->ddict = _constfree(fi->ddict);
1214 fi->fddictx = _constfree(fi->fddictx);
1215 fi->fddictn = _constfree(fi->fddictn);
1220 fi->fsm = freeFSM(fi->fsm);
1222 fi->fn = _free(fi->fn);
1223 fi->apath = _free(fi->apath);
1225 fi->replacedSizes = _free(fi->replacedSizes);
1227 fi->h = headerFree(fi->h);
1229 (void) rpmfiUnlink(fi, fi->Type);
1230 memset(fi, 0, sizeof(*fi)); /* XXX trash and burn */
1236 /* Helper to push header tag data into a string cache */
1237 static scidx_t *cacheTag(strcache cache, Header h, rpmTag tag)
1239 scidx_t *idx = NULL;
1241 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1242 idx = xmalloc(sizeof(*idx) * rpmtdCount(&td));
1245 while ((str = rpmtdNextString(&td))) {
1246 idx[i++] = strcachePut(cache, str);
1253 #define _hgfi(_h, _tag, _td, _flags, _data) \
1254 if (headerGet((_h), (_tag), (_td), (_flags))) \
1257 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, rpmfiFlags flags)
1262 rpm_loff_t *asize = NULL;
1264 int isBuild, isSource;
1265 struct rpmtd_s fdigests, digalgo;
1267 headerGetFlags scareFlags = (flags & RPMFI_KEEPHEADER) ?
1268 HEADERGET_MINMEM : HEADERGET_ALLOC;
1269 headerGetFlags defFlags = HEADERGET_ALLOC;
1271 if (tagN == RPMTAG_BASENAMES) {
1278 fi = xcalloc(1, sizeof(*fi));
1279 if (fi == NULL) /* XXX can't happen */
1282 fi->magic = RPMFIMAGIC;
1287 fi->fiflags = flags;
1288 fi->scareFlags = scareFlags;
1290 fi->h = (fi->fiflags & RPMFI_KEEPHEADER) ? headerLink(h) : NULL;
1292 if (headerGet(h, RPMTAG_LONGARCHIVESIZE, &td, HEADERGET_EXT)) {
1293 asize = rpmtdGetUint64(&td);
1295 /* 0 means unknown */
1296 fi->archiveSize = asize ? *asize : 0;
1299 /* Archive size is not set when this gets called from build */
1300 isBuild = (asize == NULL);
1301 isSource = headerIsSource(h);
1302 if (isBuild) fi->fiflags |= RPMFI_ISBUILD;
1303 if (isSource) fi->fiflags |= RPMFI_ISSOURCE;
1305 _hgfi(h, RPMTAG_BASENAMES, &td, defFlags, fi->bnl);
1306 fi->fc = rpmtdCount(&td);
1311 _hgfi(h, RPMTAG_DIRNAMES, &td, defFlags, fi->dnl);
1312 fi->dc = rpmtdCount(&td);
1313 _hgfi(h, RPMTAG_DIRINDEXES, &td, scareFlags, fi->dil);
1314 if (!(flags & RPMFI_NOFILEMODES))
1315 _hgfi(h, RPMTAG_FILEMODES, &td, scareFlags, fi->fmodes);
1316 if (!(flags & RPMFI_NOFILEFLAGS))
1317 _hgfi(h, RPMTAG_FILEFLAGS, &td, scareFlags, fi->fflags);
1318 if (!(flags & RPMFI_NOFILEVERIFYFLAGS))
1319 _hgfi(h, RPMTAG_FILEVERIFYFLAGS, &td, scareFlags, fi->vflags);
1320 if (!(flags & RPMFI_NOFILESIZES))
1321 _hgfi(h, RPMTAG_FILESIZES, &td, scareFlags, fi->fsizes);
1323 if (!(flags & RPMFI_NOFILECOLORS))
1324 _hgfi(h, RPMTAG_FILECOLORS, &td, scareFlags, fi->fcolors);
1326 if (!(flags & RPMFI_NOFILECLASS)) {
1327 _hgfi(h, RPMTAG_CLASSDICT, &td, scareFlags, fi->cdict);
1328 fi->ncdict = rpmtdCount(&td);
1329 _hgfi(h, RPMTAG_FILECLASS, &td, scareFlags, fi->fcdictx);
1331 if (!(flags & RPMFI_NOFILEDEPS)) {
1332 _hgfi(h, RPMTAG_DEPENDSDICT, &td, scareFlags, fi->ddict);
1333 fi->nddict = rpmtdCount(&td);
1334 _hgfi(h, RPMTAG_FILEDEPENDSX, &td, scareFlags, fi->fddictx);
1335 _hgfi(h, RPMTAG_FILEDEPENDSN, &td, scareFlags, fi->fddictn);
1338 if (!(flags & RPMFI_NOFILESTATES))
1339 _hgfi(h, RPMTAG_FILESTATES, &td, defFlags, fi->fstates);
1341 if (!(flags & RPMFI_NOFILECAPS))
1342 _hgfi(h, RPMTAG_FILECAPS, &td, defFlags, fi->fcaps);
1344 if (!(flags & RPMFI_NOFILELINKTOS)) {
1345 fi->flinkcache = strcacheNew();
1346 fi->flinks = cacheTag(fi->flinkcache, h, RPMTAG_FILELINKTOS);
1348 /* FILELANGS are only interesting when installing */
1349 if ((headerGetInstance(h) == 0) && !(flags & RPMFI_NOFILELANGS))
1350 fi->flangs = cacheTag(langcache, h, RPMTAG_FILELANGS);
1352 /* See if the package has non-md5 file digests */
1353 fi->digestalgo = PGPHASHALGO_MD5;
1354 if (headerGet(h, RPMTAG_FILEDIGESTALGO, &digalgo, HEADERGET_MINMEM)) {
1355 pgpHashAlgo *algo = rpmtdGetUint32(&digalgo);
1356 /* Hmm, what to do with unknown digest algorithms? */
1357 if (algo && rpmDigestLength(*algo) != 0) {
1358 fi->digestalgo = *algo;
1363 /* grab hex digests from header and store in binary format */
1364 if (!(flags & RPMFI_NOFILEDIGESTS) &&
1365 headerGet(h, RPMTAG_FILEDIGESTS, &fdigests, HEADERGET_MINMEM)) {
1366 const char *fdigest;
1367 size_t diglen = rpmDigestLength(fi->digestalgo);
1368 fi->digests = t = xmalloc(rpmtdCount(&fdigests) * diglen);
1370 while ((fdigest = rpmtdNextString(&fdigests))) {
1371 if (!(fdigest && *fdigest != '\0')) {
1372 memset(t, 0, diglen);
1376 for (int j = 0; j < diglen; j++, t++, fdigest += 2)
1377 *t = (rnibble(fdigest[0]) << 4) | rnibble(fdigest[1]);
1379 rpmtdFreeData(&fdigests);
1382 /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes */
1383 if (!(flags & RPMFI_NOFILEMTIMES))
1384 _hgfi(h, RPMTAG_FILEMTIMES, &td, scareFlags, fi->fmtimes);
1385 if (!(flags & RPMFI_NOFILERDEVS))
1386 _hgfi(h, RPMTAG_FILERDEVS, &td, scareFlags, fi->frdevs);
1387 if (!(flags & RPMFI_NOFILEINODES))
1388 _hgfi(h, RPMTAG_FILEINODES, &td, scareFlags, fi->finodes);
1390 if (!(flags & RPMFI_NOFILEUSER))
1391 fi->fuser = cacheTag(ugcache, h, RPMTAG_FILEUSERNAME);
1392 if (!(flags & RPMFI_NOFILEGROUP))
1393 fi->fgroup = cacheTag(ugcache, h, RPMTAG_FILEGROUPNAME);
1397 if ((p = rpmtsRelocateElement(ts)) != NULL && rpmteType(p) == TR_ADDED
1398 && !headerIsSource(h)
1399 && !headerIsEntry(h, RPMTAG_ORIGBASENAMES))
1403 /* FIX: fi-digests undefined */
1404 foo = relocateFileList(ts, fi, h);
1405 fi->h = headerFree(fi->h);
1406 fi->h = headerLink(foo);
1407 foo = headerFree(foo);
1410 if (!(fi->fiflags & RPMFI_KEEPHEADER)) {
1411 fi->h = headerFree(fi->h);
1414 /* lazily alloced from rpmfiFN() */
1418 if (_rpmfi_debug < 0)
1419 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
1421 /* FIX: rpmfi null annotations */
1422 return rpmfiLink(fi, (fi ? fi->Type : NULL));
1425 void rpmfiSetFReplacedSize(rpmfi fi, rpm_loff_t newsize)
1427 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
1428 if (fi->replacedSizes == NULL) {
1429 fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1431 /* XXX watch out, replacedSizes is not rpm_loff_t (yet) */
1432 fi->replacedSizes[fi->i] = (rpm_off_t) newsize;
1436 rpm_loff_t rpmfiFReplacedSize(rpmfi fi)
1438 rpm_loff_t rsize = 0;
1439 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
1440 if (fi->replacedSizes) {
1441 rsize = fi->replacedSizes[fi->i];
1447 void rpmfiFpLookup(rpmfi fi, fingerPrintCache fpc)
1449 if (fi->fc > 0 && fi->fps == NULL) {
1450 fi->fps = xcalloc(fi->fc, sizeof(*fi->fps));
1452 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1455 FSM_t rpmfiFSM(rpmfi fi)
1457 if (fi != NULL && fi->fsm == NULL) {
1458 cpioMapFlags mapflags;
1459 /* Figure out mapflags:
1460 * - path, mode, uid and gid are used by everything
1461 * - all binary packages get SBIT_CHECK set
1462 * - if archive size is not known, we're only building this package,
1463 * different rules apply
1465 mapflags = CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
1466 if (fi->fiflags & RPMFI_ISBUILD) {
1467 mapflags |= CPIO_MAP_TYPE;
1468 if (fi->fiflags & RPMFI_ISSOURCE) mapflags |= CPIO_FOLLOW_SYMLINKS;
1470 if (!(fi->fiflags & RPMFI_ISSOURCE)) mapflags |= CPIO_SBIT_CHECK;
1472 fi->fsm = newFSM(mapflags);
1474 return (fi != NULL) ? fi->fsm : NULL;
1478 * Generate iterator accessors function wrappers, these do nothing but
1479 * call the corresponding rpmfiFooIndex(fi, fi->[ij])
1482 #define RPMFI_ITERFUNC(TYPE, NAME, IXV) \
1483 TYPE rpmfi ## NAME(rpmfi fi) { return rpmfi ## NAME ## Index(fi, fi ? fi->IXV : -1); }
1485 RPMFI_ITERFUNC(const char *, BN, i)
1486 RPMFI_ITERFUNC(const char *, DN, j)
1487 RPMFI_ITERFUNC(const char *, FN, i)
1488 RPMFI_ITERFUNC(const char *, FLink, i)
1489 RPMFI_ITERFUNC(const char *, FUser, i)
1490 RPMFI_ITERFUNC(const char *, FGroup, i)
1491 RPMFI_ITERFUNC(const char *, FCaps, i)
1492 RPMFI_ITERFUNC(const char *, FLangs, i)
1493 RPMFI_ITERFUNC(const char *, FClass, i)
1494 RPMFI_ITERFUNC(rpmfileState, FState, i)
1495 RPMFI_ITERFUNC(rpmfileAttrs, FFlags, i)
1496 RPMFI_ITERFUNC(rpmVerifyAttrs, VFlags, i)
1497 RPMFI_ITERFUNC(rpm_mode_t, FMode, i)
1498 RPMFI_ITERFUNC(rpm_rdev_t, FRdev, i)
1499 RPMFI_ITERFUNC(rpm_time_t, FMtime, i)
1500 RPMFI_ITERFUNC(rpm_ino_t, FInode, i)
1501 RPMFI_ITERFUNC(rpm_loff_t, FSize, i)
1502 RPMFI_ITERFUNC(rpm_color_t, FColor, i)
1503 RPMFI_ITERFUNC(uint32_t, FNlink, i)
1505 const unsigned char * rpmfiFDigest(rpmfi fi, pgpHashAlgo *algo, size_t *len)
1507 return rpmfiFDigestIndex(fi, fi ? fi->i : -1, algo, len);
1510 uint32_t rpmfiFDepends(rpmfi fi, const uint32_t ** fddictp)
1512 return rpmfiFDependsIndex(fi, fi ? fi->i : -1, fddictp);