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 (rstreq(str, cache->uniq[i])) {
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);
89 static rpmfi rpmfiUnlink(rpmfi fi)
96 rpmfi rpmfiLink(rpmfi fi)
103 rpm_count_t rpmfiFC(rpmfi fi)
105 return (fi != NULL ? fi->fc : 0);
108 rpm_count_t rpmfiDC(rpmfi fi)
110 return (fi != NULL ? fi->dc : 0);
114 int rpmfiDI(rpmfi fi)
119 int rpmfiFX(rpmfi fi)
121 return (fi != NULL ? fi->i : -1);
124 int rpmfiSetFX(rpmfi fi, int fx)
128 if (fi != NULL && fx >= 0 && fx < fi->fc) {
131 fi->j = fi->dil[fi->i];
136 int rpmfiDX(rpmfi fi)
138 return (fi != NULL ? fi->j : -1);
141 int rpmfiSetDX(rpmfi fi, int dx)
145 if (fi != NULL && dx >= 0 && dx < fi->dc) {
152 int rpmfiDIIndex(rpmfi fi, int dx)
155 if (fi != NULL && dx >= 0 && dx < fi->fc) {
162 const char * rpmfiBNIndex(rpmfi fi, int ix)
164 const char * BN = NULL;
166 if (fi != NULL && ix >= 0 && ix < fi->fc) {
173 const char * rpmfiDNIndex(rpmfi fi, int jx)
175 const char * DN = NULL;
177 if (fi != NULL && jx >= 0 && jx < fi->dc) {
184 const char * rpmfiFNIndex(rpmfi fi, int ix)
186 const char * FN = "";
188 if (fi != NULL && ix >= 0 && ix < fi->fc) {
190 if (fi->fn == NULL) {
191 size_t dnlmax = 0, bnlmax = 0, len;
192 for (int i = 0; i < fi->dc; i++) {
193 if ((len = strlen(fi->dnl[i])) > dnlmax)
196 for (int i = 0; i < fi->fc; i++) {
197 if ((len = strlen(fi->bnl[i])) > bnlmax)
200 fi->fn = xmalloc(dnlmax + bnlmax + 1);
204 t = stpcpy(t, fi->dnl[fi->dil[ix]]);
205 t = stpcpy(t, fi->bnl[ix]);
210 rpmfileAttrs rpmfiFFlagsIndex(rpmfi fi, int ix)
212 rpmfileAttrs FFlags = 0;
214 if (fi != NULL && ix >= 0 && ix < fi->fc) {
215 if (fi->fflags != NULL)
216 FFlags = fi->fflags[ix];
221 rpmVerifyAttrs rpmfiVFlagsIndex(rpmfi fi, int ix)
223 rpmVerifyAttrs VFlags = 0;
225 if (fi != NULL && ix >= 0 && ix < fi->fc) {
226 if (fi->vflags != NULL)
227 VFlags = fi->vflags[ix];
232 rpm_mode_t rpmfiFModeIndex(rpmfi fi, int ix)
234 rpm_mode_t fmode = 0;
236 if (fi != NULL && ix >= 0 && ix < fi->fc) {
237 if (fi->fmodes != NULL)
238 fmode = fi->fmodes[ix];
243 rpmfileState rpmfiFStateIndex(rpmfi fi, int ix)
245 rpmfileState fstate = RPMFILE_STATE_MISSING;
247 if (fi != NULL && ix >= 0 && ix < fi->fc) {
248 if (fi->fstates != NULL)
249 fstate = fi->fstates[ix];
254 const unsigned char * rpmfiMD5(rpmfi fi)
256 const unsigned char *digest;
259 digest = rpmfiFDigest(fi, &algo, NULL);
260 return (algo == PGPHASHALGO_MD5) ? digest : NULL;
263 int rpmfiDigestAlgo(rpmfi fi)
265 return fi ? fi->digestalgo : 0;
268 const unsigned char * rpmfiFDigestIndex(rpmfi fi, int ix, int *algo, size_t *len)
270 const unsigned char *digest = NULL;
272 if (fi != NULL && ix >= 0 && ix < fi->fc) {
273 size_t diglen = rpmDigestLength(fi->digestalgo);
274 if (fi->digests != NULL)
275 digest = fi->digests + (diglen * ix);
279 *algo = fi->digestalgo;
284 char * rpmfiFDigestHex(rpmfi fi, int *algo)
287 char *fdigest = NULL;
288 const unsigned char *digest = rpmfiFDigest(fi, algo, &diglen);
290 fdigest = pgpHexStr(digest, diglen);
295 const char * rpmfiFLinkIndex(rpmfi fi, int ix)
297 const char * flink = NULL;
299 if (fi != NULL && ix >= 0 && ix < fi->fc) {
300 if (fi->flinks != NULL)
301 flink = strcacheGet(fi->flinkcache, fi->flinks[ix]);
306 rpm_loff_t rpmfiFSizeIndex(rpmfi fi, int ix)
308 rpm_loff_t fsize = 0;
310 if (fi != NULL && ix >= 0 && ix < fi->fc) {
311 if (fi->fsizes != NULL)
312 fsize = fi->fsizes[ix];
317 rpm_rdev_t rpmfiFRdevIndex(rpmfi fi, int ix)
319 rpm_rdev_t frdev = 0;
321 if (fi != NULL && ix >= 0 && ix < fi->fc) {
322 if (fi->frdevs != NULL)
323 frdev = fi->frdevs[ix];
328 rpm_ino_t rpmfiFInodeIndex(rpmfi fi, int ix)
330 rpm_ino_t finode = 0;
332 if (fi != NULL && ix >= 0 && ix < fi->fc) {
333 if (fi->finodes != NULL)
334 finode = fi->finodes[ix];
339 rpm_color_t rpmfiColor(rpmfi fi)
341 rpm_color_t color = 0;
343 if (fi != NULL && fi->fcolors != NULL) {
344 for (int i = 0; i < fi->fc; i++)
345 color |= fi->fcolors[i];
346 /* XXX ignore all but lsnibble for now. */
352 rpm_color_t rpmfiFColorIndex(rpmfi fi, int ix)
354 rpm_color_t fcolor = 0;
356 if (fi != NULL && ix >= 0 && ix < fi->fc) {
357 if (fi->fcolors != NULL)
358 /* XXX ignore all but lsnibble for now. */
359 fcolor = (fi->fcolors[ix] & 0x0f);
364 const char * rpmfiFClassIndex(rpmfi fi, int ix)
366 const char * fclass = NULL;
369 if (fi != NULL && fi->fcdictx != NULL && ix >= 0 && ix < fi->fc) {
370 cdictx = fi->fcdictx[ix];
371 if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
372 fclass = fi->cdict[cdictx];
377 uint32_t rpmfiFDependsIndex(rpmfi fi, int ix, const uint32_t ** fddictp)
381 const uint32_t * fddict = NULL;
383 if (fi != NULL && ix >= 0 && ix < fi->fc) {
384 if (fi->fddictn != NULL)
385 fddictn = fi->fddictn[ix];
386 if (fddictn > 0 && fi->fddictx != NULL)
387 fddictx = fi->fddictx[ix];
388 if (fi->ddict != NULL && fddictx >= 0 && (fddictx+fddictn) <= fi->nddict)
389 fddict = fi->ddict + fddictx;
396 uint32_t rpmfiFNlinkIndex(rpmfi fi, int ix)
400 if (fi != NULL && ix >= 0 && ix < fi->fc) {
401 /* XXX rpm-2.3.12 has not RPMTAG_FILEINODES */
402 if (fi->finodes && fi->frdevs) {
403 rpm_ino_t finode = fi->finodes[ix];
404 rpm_rdev_t frdev = fi->frdevs[ix];
407 for (j = 0; j < fi->fc; j++) {
408 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
416 rpm_time_t rpmfiFMtimeIndex(rpmfi fi, int ix)
418 rpm_time_t fmtime = 0;
420 if (fi != NULL && ix >= 0 && ix < fi->fc) {
421 if (fi->fmtimes != NULL)
422 fmtime = fi->fmtimes[ix];
427 const char * rpmfiFUserIndex(rpmfi fi, int ix)
429 const char * fuser = NULL;
431 if (fi != NULL && ix >= 0 && ix < fi->fc) {
432 if (fi->fuser != NULL)
433 fuser = strcacheGet(ugcache, fi->fuser[ix]);
438 const char * rpmfiFGroupIndex(rpmfi fi, int ix)
440 const char * fgroup = NULL;
442 if (fi != NULL && ix >= 0 && ix < fi->fc) {
443 if (fi->fgroup != NULL)
444 fgroup = strcacheGet(ugcache, fi->fgroup[ix]);
449 const char * rpmfiFCapsIndex(rpmfi fi, int ix)
451 const char *fcaps = NULL;
452 if (fi != NULL && ix >= 0 && ix < fi->fc) {
453 fcaps = fi->fcapcache ? strcacheGet(fi->fcapcache, fi->fcaps[ix]) : "";
458 const char * rpmfiFLangsIndex(rpmfi fi, int ix)
460 const char *flangs = NULL;
461 if (fi != NULL && fi->flangs != NULL && ix >= 0 && ix < fi->fc) {
462 flangs = strcacheGet(langcache, fi->flangs[ix]);
467 struct fingerPrint_s *rpmfiFpsIndex(rpmfi fi, int ix)
469 struct fingerPrint_s * fps = NULL;
470 if (fi != NULL && fi->fps != NULL && ix >= 0 && ix < fi->fc) {
476 int rpmfiNext(rpmfi fi)
480 if (fi != NULL && ++fi->i >= 0) {
481 if (fi->i < fi->fc) {
484 fi->j = fi->dil[fi->i];
492 rpmfi rpmfiInit(rpmfi fi, int fx)
495 if (fx >= 0 && fx < fi->fc) {
504 int rpmfiNextD(rpmfi fi)
508 if (fi != NULL && ++fi->j >= 0) {
518 rpmfi rpmfiInitD(rpmfi fi, int dx)
521 if (dx >= 0 && dx < fi->fc)
531 * Identify a file type.
532 * @param ft file type
533 * @return string to identify a file type
536 const char * ftstring (rpmFileTypes ft)
539 case XDIR: return "directory";
540 case CDEV: return "char dev";
541 case BDEV: return "block dev";
542 case LINK: return "link";
543 case SOCK: return "sock";
544 case PIPE: return "fifo/pipe";
545 case REG: return "file";
546 default: return "unknown file type";
550 rpmFileTypes rpmfiWhatis(rpm_mode_t mode)
552 if (S_ISDIR(mode)) return XDIR;
553 if (S_ISCHR(mode)) return CDEV;
554 if (S_ISBLK(mode)) return BDEV;
555 if (S_ISLNK(mode)) return LINK;
556 if (S_ISSOCK(mode)) return SOCK;
557 if (S_ISFIFO(mode)) return PIPE;
561 int rpmfiCompare(const rpmfi afi, const rpmfi bfi)
563 rpmFileTypes awhat = rpmfiWhatis(rpmfiFMode(afi));
564 rpmFileTypes bwhat = rpmfiWhatis(rpmfiFMode(bfi));
566 if ((rpmfiFFlags(afi) & RPMFILE_GHOST) ||
567 (rpmfiFFlags(bfi) & RPMFILE_GHOST)) return 0;
569 if (awhat != bwhat) return 1;
572 const char * alink = rpmfiFLink(afi);
573 const char * blink = rpmfiFLink(bfi);
574 if (alink == blink) return 0;
575 if (alink == NULL) return 1;
576 if (blink == NULL) return -1;
577 return strcmp(alink, blink);
578 } else if (awhat == REG) {
579 size_t adiglen, bdiglen;
581 const unsigned char * adigest = rpmfiFDigest(afi, &aalgo, &adiglen);
582 const unsigned char * bdigest = rpmfiFDigest(bfi, &balgo, &bdiglen);
583 if (adigest == bdigest) return 0;
584 if (adigest == NULL) return 1;
585 if (bdigest == NULL) return -1;
586 /* can't meaningfully compare different hash types */
587 if (aalgo != balgo || adiglen != bdiglen) return -1;
588 return memcmp(adigest, bdigest, adiglen);
594 rpmFileAction rpmfiDecideFate(const rpmfi ofi, rpmfi nfi, int skipMissing)
596 const char * fn = rpmfiFN(nfi);
597 rpmfileAttrs newFlags = rpmfiFFlags(nfi);
599 rpmFileTypes dbWhat, newWhat, diskWhat;
601 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
603 if (lstat(fn, &sb)) {
605 * The file doesn't exist on the disk. Create it unless the new
606 * package has marked it as missingok, or allfiles is requested.
608 if (skipMissing && (newFlags & RPMFILE_MISSINGOK)) {
609 rpmlog(RPMLOG_DEBUG, "%s skipped due to missingok flag\n",
617 diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
618 dbWhat = rpmfiWhatis(rpmfiFMode(ofi));
619 newWhat = rpmfiWhatis(rpmfiFMode(nfi));
622 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
623 * them in older packages as well.
628 if (diskWhat != newWhat && dbWhat != REG && dbWhat != LINK)
630 else if (newWhat != dbWhat && diskWhat != dbWhat)
632 else if (dbWhat != newWhat)
634 else if (dbWhat != LINK && dbWhat != REG)
638 * This order matters - we'd prefer to CREATE the file if at all
639 * possible in case something else (like the timestamp) has changed.
641 memset(buffer, 0, sizeof(buffer));
644 size_t odiglen, ndiglen;
645 const unsigned char * odigest, * ndigest;
646 odigest = rpmfiFDigest(ofi, &oalgo, &odiglen);
647 if (diskWhat == REG) {
648 if (rpmDoDigest(oalgo, fn, 0,
649 (unsigned char *)buffer, NULL))
650 return FA_CREATE; /* assume file has been removed */
651 if (odigest && !memcmp(odigest, buffer, odiglen))
652 return FA_CREATE; /* unmodified config file, replace. */
654 ndigest = rpmfiFDigest(nfi, &nalgo, &ndiglen);
655 /* Can't compare different hash types, backup to avoid data loss */
656 if (oalgo != nalgo || odiglen != ndiglen)
658 if (odigest && ndigest && !memcmp(odigest, ndigest, odiglen))
659 return FA_SKIP; /* identical file, don't bother. */
660 } else /* dbWhat == LINK */ {
661 const char * oFLink, * nFLink;
662 oFLink = rpmfiFLink(ofi);
663 if (diskWhat == LINK) {
664 ssize_t link_len = readlink(fn, buffer, sizeof(buffer) - 1);
666 return FA_CREATE; /* assume file has been removed */
667 buffer[link_len] = '\0';
668 if (oFLink && rstreq(oFLink, buffer))
669 return FA_CREATE; /* unmodified config file, replace. */
671 nFLink = rpmfiFLink(nfi);
672 if (oFLink && nFLink && rstreq(oFLink, nFLink))
673 return FA_SKIP; /* identical file, don't bother. */
677 * The config file on the disk has been modified, but
678 * the ones in the two packages are different. It would
679 * be nice if RPM was smart enough to at least try and
680 * merge the difference ala CVS, but...
685 int rpmfiConfigConflict(const rpmfi fi)
687 const char * fn = rpmfiFN(fi);
688 rpmfileAttrs flags = rpmfiFFlags(fi);
690 rpmFileTypes newWhat, diskWhat;
693 if (!(flags & RPMFILE_CONFIG) || lstat(fn, &sb)) {
697 diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
698 newWhat = rpmfiWhatis(rpmfiFMode(fi));
700 if (newWhat != LINK && newWhat != REG)
703 if (diskWhat != newWhat)
706 memset(buffer, 0, sizeof(buffer));
707 if (newWhat == REG) {
710 const unsigned char *ndigest = rpmfiFDigest(fi, &algo, &diglen);
711 if (rpmDoDigest(algo, fn, 0, (unsigned char *)buffer, NULL))
712 return 0; /* assume file has been removed */
713 if (ndigest && !memcmp(ndigest, buffer, diglen))
714 return 0; /* unmodified config file */
715 } else /* newWhat == LINK */ {
717 ssize_t link_len = readlink(fn, buffer, sizeof(buffer) - 1);
719 return 0; /* assume file has been removed */
720 buffer[link_len] = '\0';
721 nFLink = rpmfiFLink(fi);
722 if (nFLink && rstreq(nFLink, buffer))
723 return 0; /* unmodified config file */
729 static char **duparray(char ** src, int size)
731 char **dest = xmalloc((size+1) * sizeof(*dest));
732 for (int i = 0; i < size; i++) {
733 dest[i] = xstrdup(src[i]);
739 static int addPrefixes(Header h, rpmRelocation *relocations, int numRelocations)
741 struct rpmtd_s validRelocs;
742 const char *validprefix;
743 const char ** actualRelocations;
746 headerGet(h, RPMTAG_PREFIXES, &validRelocs, HEADERGET_MINMEM);
748 * If no relocations are specified (usually the case), then return the
749 * original header. If there are prefixes, however, then INSTPREFIXES
750 * should be added for RPM_INSTALL_PREFIX environ variables in scriptlets,
751 * but, since relocateFileList() can be called more than once for
752 * the same header, don't bother if already present.
754 if (relocations == NULL || numRelocations == 0) {
755 if (rpmtdCount(&validRelocs) > 0) {
756 if (!headerIsEntry(h, RPMTAG_INSTPREFIXES)) {
757 rpmtdSetTag(&validRelocs, RPMTAG_INSTPREFIXES);
758 headerPut(h, &validRelocs, HEADERPUT_DEFAULT);
760 rpmtdFreeData(&validRelocs);
765 actualRelocations = xmalloc(rpmtdCount(&validRelocs) * sizeof(*actualRelocations));
766 rpmtdInit(&validRelocs);
767 while ((validprefix = rpmtdNextString(&validRelocs))) {
769 for (j = 0; j < numRelocations; j++) {
770 if (relocations[j].oldPath == NULL || /* XXX can't happen */
771 !rstreq(validprefix, relocations[j].oldPath))
773 /* On install, a relocate to NULL means skip the path. */
774 if (relocations[j].newPath) {
775 actualRelocations[numActual] = relocations[j].newPath;
780 if (j == numRelocations) {
781 actualRelocations[numActual] = validprefix;
785 rpmtdFreeData(&validRelocs);
788 headerPutStringArray(h, RPMTAG_INSTPREFIXES, actualRelocations, numActual);
790 free(actualRelocations);
794 static void saveRelocs(Header h, rpmtd bnames, rpmtd dnames, rpmtd dindexes)
797 headerGet(h, RPMTAG_BASENAMES, &td, HEADERGET_MINMEM);
798 rpmtdSetTag(&td, RPMTAG_ORIGBASENAMES);
799 headerPut(h, &td, HEADERPUT_DEFAULT);
802 headerGet(h, RPMTAG_DIRNAMES, &td, HEADERGET_MINMEM);
803 rpmtdSetTag(&td, RPMTAG_ORIGDIRNAMES);
804 headerPut(h, &td, HEADERPUT_DEFAULT);
807 headerGet(h, RPMTAG_DIRINDEXES, &td, HEADERGET_MINMEM);
808 rpmtdSetTag(&td, RPMTAG_ORIGDIRINDEXES);
809 headerPut(h, &td, HEADERPUT_DEFAULT);
812 headerMod(h, bnames);
813 headerMod(h, dnames);
814 headerMod(h, dindexes);
817 void rpmRelocateFileList(rpmRelocation *relocations, int numRelocations,
820 static int _printed = 0;
823 uint32_t * dirIndexes;
824 rpm_count_t fileCount, dirCount;
828 int haveRelocatedBase = 0;
831 struct rpmtd_s bnames, dnames, dindexes, fmodes;
833 addPrefixes(h, relocations, numRelocations);
837 rpmlog(RPMLOG_DEBUG, "========== relocations\n");
838 for (i = 0; i < numRelocations; i++) {
839 if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
840 if (relocations[i].newPath == NULL)
841 rpmlog(RPMLOG_DEBUG, "%5d exclude %s\n",
842 i, relocations[i].oldPath);
844 rpmlog(RPMLOG_DEBUG, "%5d relocate %s -> %s\n",
845 i, relocations[i].oldPath, relocations[i].newPath);
849 for (i = 0; i < numRelocations; i++) {
850 if (relocations[i].newPath == NULL) continue;
851 size_t len = strlen(relocations[i].newPath);
852 if (len > maxlen) maxlen = len;
855 headerGet(h, RPMTAG_BASENAMES, &bnames, HEADERGET_MINMEM);
856 headerGet(h, RPMTAG_DIRINDEXES, &dindexes, HEADERGET_ALLOC);
857 headerGet(h, RPMTAG_DIRNAMES, &dnames, HEADERGET_MINMEM);
858 headerGet(h, RPMTAG_FILEMODES, &fmodes, HEADERGET_MINMEM);
859 /* TODO XXX ugh.. use rpmtd iterators & friends instead */
860 baseNames = bnames.data;
861 dirIndexes = dindexes.data;
862 fileCount = rpmtdCount(&bnames);
863 dirCount = rpmtdCount(&dnames);
864 /* XXX TODO: use rpmtdDup() instead */
865 dirNames = dnames.data = duparray(dnames.data, dirCount);
866 dnames.flags |= RPMTD_PTR_ALLOCED;
869 * For all relocations, we go through sorted file/relocation lists
870 * backwards so that /usr/local relocations take precedence over /usr
874 /* Relocate individual paths. */
876 for (i = fileCount - 1; i >= 0; i--) {
880 size_t len = maxlen +
881 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
882 if (len >= fileAlloced) {
883 fileAlloced = len * 2;
884 fn = xrealloc(fn, fileAlloced);
887 assert(fn != NULL); /* XXX can't happen */
889 fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
892 * See if this file path needs relocating.
895 * XXX FIXME: Would a bsearch of the (already sorted)
896 * relocation list be a good idea?
898 for (j = numRelocations - 1; j >= 0; j--) {
899 if (relocations[j].oldPath == NULL) /* XXX can't happen */
901 len = !rstreq(relocations[j].oldPath, "/")
902 ? strlen(relocations[j].oldPath)
908 * Only subdirectories or complete file paths may be relocated. We
909 * don't check for '\0' as our directory names all end in '/'.
911 if (!(fn[len] == '/' || fnlen == len))
914 if (!rstreqn(relocations[j].oldPath, fn, len))
920 rpmtdSetIndex(&fmodes, i);
921 ft = rpmfiWhatis(rpmtdGetNumber(&fmodes));
923 /* On install, a relocate to NULL means skip the path. */
924 if (relocations[j].newPath == NULL) {
926 /* Start with the parent, looking for directory to exclude. */
927 for (j = dirIndexes[i]; j < dirCount; j++) {
928 len = strlen(dirNames[j]) - 1;
929 while (len > 0 && dirNames[j][len-1] == '/') len--;
932 if (!rstreqn(fn, dirNames[j], fnlen))
937 rpmfsSetAction(fs, i, FA_SKIPNSTATE);
938 rpmlog(RPMLOG_DEBUG, "excluding %s %s\n",
943 /* Relocation on full paths only, please. */
944 if (fnlen != len) continue;
946 rpmlog(RPMLOG_DEBUG, "relocating %s to %s\n",
947 fn, relocations[j].newPath);
950 strcpy(fn, relocations[j].newPath);
951 { char * te = strrchr(fn, '/');
953 if (te > fn) te++; /* root is special */
956 te = fn + strlen(fn);
957 if (!rstreq(baseNames[i], te)) { /* basename changed too? */
958 if (!haveRelocatedBase) {
959 /* XXX TODO: use rpmtdDup() instead */
960 bnames.data = baseNames = duparray(baseNames, fileCount);
961 bnames.flags |= RPMTD_PTR_ALLOCED;
962 haveRelocatedBase = 1;
965 baseNames[i] = xstrdup(te);
967 *te = '\0'; /* terminate new directory name */
970 /* Does this directory already exist in the directory list? */
971 for (j = 0; j < dirCount; j++) {
972 if (fnlen != strlen(dirNames[j]))
974 if (!rstreqn(fn, dirNames[j], fnlen))
984 /* Creating new paths is a pita */
985 dirNames = dnames.data = xrealloc(dnames.data,
986 sizeof(*dirNames) * (dirCount + 1));
988 dirNames[dirCount] = xstrdup(fn);
989 dirIndexes[i] = dirCount;
994 /* Finish off by relocating directories. */
995 for (i = dirCount - 1; i >= 0; i--) {
996 for (j = numRelocations - 1; j >= 0; j--) {
998 if (relocations[j].oldPath == NULL) /* XXX can't happen */
1000 size_t len = !rstreq(relocations[j].oldPath, "/")
1001 ? strlen(relocations[j].oldPath)
1004 if (len && !rstreqn(relocations[j].oldPath, dirNames[i], len))
1008 * Only subdirectories or complete file paths may be relocated. We
1009 * don't check for '\0' as our directory names all end in '/'.
1011 if (dirNames[i][len] != '/')
1014 if (relocations[j].newPath) { /* Relocate the path */
1016 rstrscat(&t, relocations[j].newPath, (dirNames[i] + len), NULL);
1017 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
1018 (void) rpmCleanPath(t);
1021 rpmlog(RPMLOG_DEBUG,
1022 "relocating directory %s to %s\n", dirNames[i], t);
1030 /* Save original filenames in header and replace (relocated) filenames. */
1032 saveRelocs(h, &bnames, &dnames, &dindexes);
1035 rpmtdFreeData(&bnames);
1036 rpmtdFreeData(&dnames);
1037 rpmtdFreeData(&dindexes);
1038 rpmtdFreeData(&fmodes);
1042 rpmfi rpmfiFree(rpmfi fi)
1044 if (fi == NULL) return NULL;
1047 return rpmfiUnlink(fi);
1050 fi->bnl = _free(fi->bnl);
1051 fi->dnl = _free(fi->dnl);
1053 fi->flinkcache = strcacheFree(fi->flinkcache);
1054 fi->flinks = _free(fi->flinks);
1055 fi->flangs = _free(fi->flangs);
1056 fi->digests = _free(fi->digests);
1057 fi->fcapcache = strcacheFree(fi->fcapcache);
1058 fi->fcaps = _free(fi->fcaps);
1060 fi->cdict = _free(fi->cdict);
1062 fi->fuser = _free(fi->fuser);
1063 fi->fgroup = _free(fi->fgroup);
1065 fi->fstates = _free(fi->fstates);
1066 fi->fps = _free(fi->fps);
1068 /* these point to header memory if KEEPHEADER is used, dont free */
1069 if (!(fi->fiflags & RPMFI_KEEPHEADER) && fi->h == NULL) {
1070 fi->fmtimes = _free(fi->fmtimes);
1071 fi->fmodes = _free(fi->fmodes);
1072 fi->fflags = _free(fi->fflags);
1073 fi->vflags = _free(fi->vflags);
1074 fi->fsizes = _free(fi->fsizes);
1075 fi->frdevs = _free(fi->frdevs);
1076 fi->finodes = _free(fi->finodes);
1077 fi->dil = _free(fi->dil);
1079 fi->fcolors = _free(fi->fcolors);
1080 fi->fcdictx = _free(fi->fcdictx);
1081 fi->ddict = _free(fi->ddict);
1082 fi->fddictx = _free(fi->fddictx);
1083 fi->fddictn = _free(fi->fddictn);
1088 fi->fsm = freeFSM(fi->fsm);
1090 fi->fn = _free(fi->fn);
1091 fi->apath = _free(fi->apath);
1093 fi->replacedSizes = _free(fi->replacedSizes);
1095 fi->h = headerFree(fi->h);
1097 (void) rpmfiUnlink(fi);
1098 memset(fi, 0, sizeof(*fi)); /* XXX trash and burn */
1104 /* Helper to push header tag data into a string cache */
1105 static scidx_t *cacheTag(strcache cache, Header h, rpmTag tag)
1107 scidx_t *idx = NULL;
1109 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1110 idx = xmalloc(sizeof(*idx) * rpmtdCount(&td));
1113 while ((str = rpmtdNextString(&td))) {
1114 idx[i++] = strcachePut(cache, str);
1121 #define _hgfi(_h, _tag, _td, _flags, _data) \
1122 if (headerGet((_h), (_tag), (_td), (_flags))) \
1125 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTagVal tagN, rpmfiFlags flags)
1128 rpm_loff_t *asize = NULL;
1130 struct rpmtd_s fdigests, digalgo;
1132 headerGetFlags scareFlags = (flags & RPMFI_KEEPHEADER) ?
1133 HEADERGET_MINMEM : HEADERGET_ALLOC;
1134 headerGetFlags defFlags = HEADERGET_ALLOC;
1136 fi = xcalloc(1, sizeof(*fi));
1137 if (fi == NULL) /* XXX can't happen */
1140 fi->magic = RPMFIMAGIC;
1143 fi->fiflags = flags;
1144 fi->scareFlags = scareFlags;
1146 if (headerGet(h, RPMTAG_LONGARCHIVESIZE, &td, HEADERGET_EXT)) {
1147 asize = rpmtdGetUint64(&td);
1149 /* 0 means unknown */
1150 fi->archiveSize = asize ? *asize : 0;
1153 if (headerIsSource(h)) fi->fiflags |= RPMFI_ISSOURCE;
1155 _hgfi(h, RPMTAG_BASENAMES, &td, defFlags, fi->bnl);
1156 fi->fc = rpmtdCount(&td);
1161 _hgfi(h, RPMTAG_DIRNAMES, &td, defFlags, fi->dnl);
1162 fi->dc = rpmtdCount(&td);
1163 _hgfi(h, RPMTAG_DIRINDEXES, &td, scareFlags, fi->dil);
1164 if (!(flags & RPMFI_NOFILEMODES))
1165 _hgfi(h, RPMTAG_FILEMODES, &td, scareFlags, fi->fmodes);
1166 if (!(flags & RPMFI_NOFILEFLAGS))
1167 _hgfi(h, RPMTAG_FILEFLAGS, &td, scareFlags, fi->fflags);
1168 if (!(flags & RPMFI_NOFILEVERIFYFLAGS))
1169 _hgfi(h, RPMTAG_FILEVERIFYFLAGS, &td, scareFlags, fi->vflags);
1170 if (!(flags & RPMFI_NOFILESIZES))
1171 _hgfi(h, RPMTAG_FILESIZES, &td, scareFlags, fi->fsizes);
1173 if (!(flags & RPMFI_NOFILECOLORS))
1174 _hgfi(h, RPMTAG_FILECOLORS, &td, scareFlags, fi->fcolors);
1176 if (!(flags & RPMFI_NOFILECLASS)) {
1177 _hgfi(h, RPMTAG_CLASSDICT, &td, scareFlags, fi->cdict);
1178 fi->ncdict = rpmtdCount(&td);
1179 _hgfi(h, RPMTAG_FILECLASS, &td, scareFlags, fi->fcdictx);
1181 if (!(flags & RPMFI_NOFILEDEPS)) {
1182 _hgfi(h, RPMTAG_DEPENDSDICT, &td, scareFlags, fi->ddict);
1183 fi->nddict = rpmtdCount(&td);
1184 _hgfi(h, RPMTAG_FILEDEPENDSX, &td, scareFlags, fi->fddictx);
1185 _hgfi(h, RPMTAG_FILEDEPENDSN, &td, scareFlags, fi->fddictn);
1188 if (!(flags & RPMFI_NOFILESTATES))
1189 _hgfi(h, RPMTAG_FILESTATES, &td, defFlags, fi->fstates);
1191 if (!(flags & RPMFI_NOFILECAPS) && headerIsEntry(h, RPMTAG_FILECAPS)) {
1192 fi->fcapcache = strcacheNew();
1193 fi->fcaps = cacheTag(fi->fcapcache, h, RPMTAG_FILECAPS);
1196 if (!(flags & RPMFI_NOFILELINKTOS)) {
1197 fi->flinkcache = strcacheNew();
1198 fi->flinks = cacheTag(fi->flinkcache, h, RPMTAG_FILELINKTOS);
1200 /* FILELANGS are only interesting when installing */
1201 if ((headerGetInstance(h) == 0) && !(flags & RPMFI_NOFILELANGS))
1202 fi->flangs = cacheTag(langcache, h, RPMTAG_FILELANGS);
1204 /* See if the package has non-md5 file digests */
1205 fi->digestalgo = PGPHASHALGO_MD5;
1206 if (headerGet(h, RPMTAG_FILEDIGESTALGO, &digalgo, HEADERGET_MINMEM)) {
1207 uint32_t *algo = rpmtdGetUint32(&digalgo);
1208 /* Hmm, what to do with unknown digest algorithms? */
1209 if (algo && rpmDigestLength(*algo) != 0) {
1210 fi->digestalgo = *algo;
1215 /* grab hex digests from header and store in binary format */
1216 if (!(flags & RPMFI_NOFILEDIGESTS) &&
1217 headerGet(h, RPMTAG_FILEDIGESTS, &fdigests, HEADERGET_MINMEM)) {
1218 const char *fdigest;
1219 size_t diglen = rpmDigestLength(fi->digestalgo);
1220 fi->digests = t = xmalloc(rpmtdCount(&fdigests) * diglen);
1222 while ((fdigest = rpmtdNextString(&fdigests))) {
1223 if (!(fdigest && *fdigest != '\0')) {
1224 memset(t, 0, diglen);
1228 for (int j = 0; j < diglen; j++, t++, fdigest += 2)
1229 *t = (rnibble(fdigest[0]) << 4) | rnibble(fdigest[1]);
1231 rpmtdFreeData(&fdigests);
1234 /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes */
1235 if (!(flags & RPMFI_NOFILEMTIMES))
1236 _hgfi(h, RPMTAG_FILEMTIMES, &td, scareFlags, fi->fmtimes);
1237 if (!(flags & RPMFI_NOFILERDEVS))
1238 _hgfi(h, RPMTAG_FILERDEVS, &td, scareFlags, fi->frdevs);
1239 if (!(flags & RPMFI_NOFILEINODES))
1240 _hgfi(h, RPMTAG_FILEINODES, &td, scareFlags, fi->finodes);
1242 if (!(flags & RPMFI_NOFILEUSER))
1243 fi->fuser = cacheTag(ugcache, h, RPMTAG_FILEUSERNAME);
1244 if (!(flags & RPMFI_NOFILEGROUP))
1245 fi->fgroup = cacheTag(ugcache, h, RPMTAG_FILEGROUPNAME);
1247 /* lazily alloced from rpmfiFN() */
1253 fi->h = (fi->fiflags & RPMFI_KEEPHEADER) ? headerLink(h) : NULL;
1256 /* FIX: rpmfi null annotations */
1257 return rpmfiLink(fi);
1260 void rpmfiSetFReplacedSize(rpmfi fi, rpm_loff_t newsize)
1262 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
1263 if (fi->replacedSizes == NULL) {
1264 fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1266 /* XXX watch out, replacedSizes is not rpm_loff_t (yet) */
1267 fi->replacedSizes[fi->i] = (rpm_off_t) newsize;
1271 rpm_loff_t rpmfiFReplacedSize(rpmfi fi)
1273 rpm_loff_t rsize = 0;
1274 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
1275 if (fi->replacedSizes) {
1276 rsize = fi->replacedSizes[fi->i];
1282 void rpmfiFpLookup(rpmfi fi, fingerPrintCache fpc)
1284 if (fi->fc > 0 && fi->fps == NULL) {
1285 fi->fps = xcalloc(fi->fc, sizeof(*fi->fps));
1287 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1290 FSM_t rpmfiFSM(rpmfi fi)
1292 if (fi != NULL && fi->fsm == NULL) {
1293 cpioMapFlags mapflags;
1294 /* Figure out mapflags:
1295 * - path, mode, uid and gid are used by everything
1296 * - all binary packages get SBIT_CHECK set
1297 * - if archive size is not known, we're only building this package,
1298 * different rules apply
1300 mapflags = CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
1301 if (fi->fiflags & RPMFI_ISBUILD) {
1302 mapflags |= CPIO_MAP_TYPE;
1303 if (fi->fiflags & RPMFI_ISSOURCE) mapflags |= CPIO_FOLLOW_SYMLINKS;
1305 if (!(fi->fiflags & RPMFI_ISSOURCE)) mapflags |= CPIO_SBIT_CHECK;
1307 fi->fsm = newFSM(mapflags);
1309 return (fi != NULL) ? fi->fsm : NULL;
1313 * Generate iterator accessors function wrappers, these do nothing but
1314 * call the corresponding rpmfiFooIndex(fi, fi->[ij])
1317 #define RPMFI_ITERFUNC(TYPE, NAME, IXV) \
1318 TYPE rpmfi ## NAME(rpmfi fi) { return rpmfi ## NAME ## Index(fi, fi ? fi->IXV : -1); }
1320 RPMFI_ITERFUNC(const char *, BN, i)
1321 RPMFI_ITERFUNC(const char *, DN, j)
1322 RPMFI_ITERFUNC(const char *, FN, i)
1323 RPMFI_ITERFUNC(const char *, FLink, i)
1324 RPMFI_ITERFUNC(const char *, FUser, i)
1325 RPMFI_ITERFUNC(const char *, FGroup, i)
1326 RPMFI_ITERFUNC(const char *, FCaps, i)
1327 RPMFI_ITERFUNC(const char *, FLangs, i)
1328 RPMFI_ITERFUNC(const char *, FClass, i)
1329 RPMFI_ITERFUNC(rpmfileState, FState, i)
1330 RPMFI_ITERFUNC(rpmfileAttrs, FFlags, i)
1331 RPMFI_ITERFUNC(rpmVerifyAttrs, VFlags, i)
1332 RPMFI_ITERFUNC(rpm_mode_t, FMode, i)
1333 RPMFI_ITERFUNC(rpm_rdev_t, FRdev, i)
1334 RPMFI_ITERFUNC(rpm_time_t, FMtime, i)
1335 RPMFI_ITERFUNC(rpm_ino_t, FInode, i)
1336 RPMFI_ITERFUNC(rpm_loff_t, FSize, i)
1337 RPMFI_ITERFUNC(rpm_color_t, FColor, i)
1338 RPMFI_ITERFUNC(uint32_t, FNlink, i)
1340 const unsigned char * rpmfiFDigest(rpmfi fi, int *algo, size_t *len)
1342 return rpmfiFDigestIndex(fi, fi ? fi->i : -1, algo, len);
1345 uint32_t rpmfiFDepends(rpmfi fi, const uint32_t ** fddictp)
1347 return rpmfiFDependsIndex(fi, fi ? fi->i : -1, fddictp);