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 */
22 * Simple and stupid string "cache."
23 * Store each unique string just once, retrieve by index value.
24 * For data where number of unique names is typically very low,
25 * the dumb linear lookup appears to be fast enough and hash table seems
33 static struct strcache_s _ugcache = { NULL, 0 };
34 static strcache ugcache = &_ugcache;
35 static struct strcache_s _langcache = { NULL, 0 };
36 static strcache langcache = &_langcache;
38 static scidx_t strcachePut(strcache cache, const char *str)
43 for (scidx_t i = 0; i < cache->num; i++) {
44 if (rstreq(str, cache->uniq[i])) {
51 /* blow up on index wraparound */
52 assert((scidx_t)(cache->num + 1) > cache->num);
53 cache->uniq = xrealloc(cache->uniq,
54 sizeof(*cache->uniq) * (cache->num+1));
55 cache->uniq[cache->num] = xstrdup(str);
62 static const char *strcacheGet(strcache cache, scidx_t idx)
64 const char *name = NULL;
65 if (idx >= 0 && idx < cache->num && cache->uniq != NULL)
66 name = cache->uniq[idx];
70 static strcache strcacheNew(void)
72 strcache cache = xcalloc(1, sizeof(*cache));
76 static strcache strcacheFree(strcache cache)
79 for (scidx_t i = 0; i < cache->num; i++) {
82 cache->uniq = _free(cache->uniq);
88 static rpmfi rpmfiUnlink(rpmfi fi)
95 rpmfi rpmfiLink(rpmfi fi)
102 rpm_count_t rpmfiFC(rpmfi fi)
104 return (fi != NULL ? fi->fc : 0);
107 rpm_count_t rpmfiDC(rpmfi fi)
109 return (fi != NULL ? fi->dc : 0);
113 int rpmfiDI(rpmfi fi)
118 int rpmfiFX(rpmfi fi)
120 return (fi != NULL ? fi->i : -1);
123 int rpmfiSetFX(rpmfi fi, int fx)
127 if (fi != NULL && fx >= 0 && fx < fi->fc) {
130 fi->j = fi->dil[fi->i];
135 int rpmfiDX(rpmfi fi)
137 return (fi != NULL ? fi->j : -1);
140 int rpmfiSetDX(rpmfi fi, int dx)
144 if (fi != NULL && dx >= 0 && dx < fi->dc) {
151 int rpmfiDIIndex(rpmfi fi, int dx)
154 if (fi != NULL && dx >= 0 && dx < fi->fc) {
161 const char * rpmfiBNIndex(rpmfi fi, int ix)
163 const char * BN = NULL;
165 if (fi != NULL && ix >= 0 && ix < fi->fc) {
172 const char * rpmfiDNIndex(rpmfi fi, int jx)
174 const char * DN = NULL;
176 if (fi != NULL && jx >= 0 && jx < fi->dc) {
183 char * rpmfiFNIndex(rpmfi fi, int ix)
186 if (fi != NULL && ix >= 0 && ix < fi->fc) {
187 fn = rstrscat(NULL, fi->dnl[fi->dil[ix]], fi->bnl[ix], NULL);
192 rpmfileAttrs rpmfiFFlagsIndex(rpmfi fi, int ix)
194 rpmfileAttrs FFlags = 0;
196 if (fi != NULL && ix >= 0 && ix < fi->fc) {
197 if (fi->fflags != NULL)
198 FFlags = fi->fflags[ix];
203 rpmVerifyAttrs rpmfiVFlagsIndex(rpmfi fi, int ix)
205 rpmVerifyAttrs VFlags = 0;
207 if (fi != NULL && ix >= 0 && ix < fi->fc) {
208 if (fi->vflags != NULL)
209 VFlags = fi->vflags[ix];
214 rpm_mode_t rpmfiFModeIndex(rpmfi fi, int ix)
216 rpm_mode_t fmode = 0;
218 if (fi != NULL && ix >= 0 && ix < fi->fc) {
219 if (fi->fmodes != NULL)
220 fmode = fi->fmodes[ix];
225 rpmfileState rpmfiFStateIndex(rpmfi fi, int ix)
227 rpmfileState fstate = RPMFILE_STATE_MISSING;
229 if (fi != NULL && ix >= 0 && ix < fi->fc) {
230 if (fi->fstates != NULL)
231 fstate = fi->fstates[ix];
236 const unsigned char * rpmfiMD5(rpmfi fi)
238 const unsigned char *digest;
241 digest = rpmfiFDigest(fi, &algo, NULL);
242 return (algo == PGPHASHALGO_MD5) ? digest : NULL;
245 int rpmfiDigestAlgo(rpmfi fi)
247 return fi ? fi->digestalgo : 0;
250 const unsigned char * rpmfiFDigestIndex(rpmfi fi, int ix, int *algo, size_t *len)
252 const unsigned char *digest = NULL;
254 if (fi != NULL && ix >= 0 && ix < fi->fc) {
255 size_t diglen = rpmDigestLength(fi->digestalgo);
256 if (fi->digests != NULL)
257 digest = fi->digests + (diglen * ix);
261 *algo = fi->digestalgo;
266 char * rpmfiFDigestHex(rpmfi fi, int *algo)
269 char *fdigest = NULL;
270 const unsigned char *digest = rpmfiFDigest(fi, algo, &diglen);
272 fdigest = pgpHexStr(digest, diglen);
277 const char * rpmfiFLinkIndex(rpmfi fi, int ix)
279 const char * flink = NULL;
281 if (fi != NULL && ix >= 0 && ix < fi->fc) {
282 if (fi->flinks != NULL)
283 flink = strcacheGet(fi->flinkcache, fi->flinks[ix]);
288 rpm_loff_t rpmfiFSizeIndex(rpmfi fi, int ix)
290 rpm_loff_t fsize = 0;
292 if (fi != NULL && ix >= 0 && ix < fi->fc) {
293 if (fi->fsizes != NULL)
294 fsize = fi->fsizes[ix];
299 rpm_rdev_t rpmfiFRdevIndex(rpmfi fi, int ix)
301 rpm_rdev_t frdev = 0;
303 if (fi != NULL && ix >= 0 && ix < fi->fc) {
304 if (fi->frdevs != NULL)
305 frdev = fi->frdevs[ix];
310 rpm_ino_t rpmfiFInodeIndex(rpmfi fi, int ix)
312 rpm_ino_t finode = 0;
314 if (fi != NULL && ix >= 0 && ix < fi->fc) {
315 if (fi->finodes != NULL)
316 finode = fi->finodes[ix];
321 rpm_color_t rpmfiColor(rpmfi fi)
323 rpm_color_t color = 0;
325 if (fi != NULL && fi->fcolors != NULL) {
326 for (int i = 0; i < fi->fc; i++)
327 color |= fi->fcolors[i];
328 /* XXX ignore all but lsnibble for now. */
334 rpm_color_t rpmfiFColorIndex(rpmfi fi, int ix)
336 rpm_color_t fcolor = 0;
338 if (fi != NULL && ix >= 0 && ix < fi->fc) {
339 if (fi->fcolors != NULL)
340 /* XXX ignore all but lsnibble for now. */
341 fcolor = (fi->fcolors[ix] & 0x0f);
346 const char * rpmfiFClassIndex(rpmfi fi, int ix)
348 const char * fclass = NULL;
351 if (fi != NULL && fi->fcdictx != NULL && ix >= 0 && ix < fi->fc) {
352 cdictx = fi->fcdictx[ix];
353 if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
354 fclass = fi->cdict[cdictx];
359 uint32_t rpmfiFDependsIndex(rpmfi fi, int ix, const uint32_t ** fddictp)
363 const uint32_t * fddict = NULL;
365 if (fi != NULL && ix >= 0 && ix < fi->fc) {
366 if (fi->fddictn != NULL)
367 fddictn = fi->fddictn[ix];
368 if (fddictn > 0 && fi->fddictx != NULL)
369 fddictx = fi->fddictx[ix];
370 if (fi->ddict != NULL && fddictx >= 0 && (fddictx+fddictn) <= fi->nddict)
371 fddict = fi->ddict + fddictx;
378 uint32_t rpmfiFNlinkIndex(rpmfi fi, int ix)
382 if (fi != NULL && ix >= 0 && ix < fi->fc) {
383 /* XXX rpm-2.3.12 has not RPMTAG_FILEINODES */
384 if (fi->finodes && fi->frdevs) {
385 rpm_ino_t finode = fi->finodes[ix];
386 rpm_rdev_t frdev = fi->frdevs[ix];
389 for (j = 0; j < fi->fc; j++) {
390 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
398 rpm_time_t rpmfiFMtimeIndex(rpmfi fi, int ix)
400 rpm_time_t fmtime = 0;
402 if (fi != NULL && ix >= 0 && ix < fi->fc) {
403 if (fi->fmtimes != NULL)
404 fmtime = fi->fmtimes[ix];
409 const char * rpmfiFUserIndex(rpmfi fi, int ix)
411 const char * fuser = NULL;
413 if (fi != NULL && ix >= 0 && ix < fi->fc) {
414 if (fi->fuser != NULL)
415 fuser = strcacheGet(ugcache, fi->fuser[ix]);
420 const char * rpmfiFGroupIndex(rpmfi fi, int ix)
422 const char * fgroup = NULL;
424 if (fi != NULL && ix >= 0 && ix < fi->fc) {
425 if (fi->fgroup != NULL)
426 fgroup = strcacheGet(ugcache, fi->fgroup[ix]);
431 const char * rpmfiFCapsIndex(rpmfi fi, int ix)
433 const char *fcaps = NULL;
434 if (fi != NULL && ix >= 0 && ix < fi->fc) {
435 fcaps = fi->fcapcache ? strcacheGet(fi->fcapcache, fi->fcaps[ix]) : "";
440 const char * rpmfiFLangsIndex(rpmfi fi, int ix)
442 const char *flangs = NULL;
443 if (fi != NULL && fi->flangs != NULL && ix >= 0 && ix < fi->fc) {
444 flangs = strcacheGet(langcache, fi->flangs[ix]);
449 struct fingerPrint_s *rpmfiFpsIndex(rpmfi fi, int ix)
451 struct fingerPrint_s * fps = NULL;
452 if (fi != NULL && fi->fps != NULL && ix >= 0 && ix < fi->fc) {
458 int rpmfiNext(rpmfi fi)
462 if (fi != NULL && ++fi->i >= 0) {
463 if (fi->i < fi->fc) {
466 fi->j = fi->dil[fi->i];
474 rpmfi rpmfiInit(rpmfi fi, int fx)
477 if (fx >= 0 && fx < fi->fc) {
486 int rpmfiNextD(rpmfi fi)
490 if (fi != NULL && ++fi->j >= 0) {
500 rpmfi rpmfiInitD(rpmfi fi, int dx)
503 if (dx >= 0 && dx < fi->fc)
513 * Identify a file type.
514 * @param ft file type
515 * @return string to identify a file type
518 const char * ftstring (rpmFileTypes ft)
521 case XDIR: return "directory";
522 case CDEV: return "char dev";
523 case BDEV: return "block dev";
524 case LINK: return "link";
525 case SOCK: return "sock";
526 case PIPE: return "fifo/pipe";
527 case REG: return "file";
528 default: return "unknown file type";
532 rpmFileTypes rpmfiWhatis(rpm_mode_t mode)
534 if (S_ISDIR(mode)) return XDIR;
535 if (S_ISCHR(mode)) return CDEV;
536 if (S_ISBLK(mode)) return BDEV;
537 if (S_ISLNK(mode)) return LINK;
538 if (S_ISSOCK(mode)) return SOCK;
539 if (S_ISFIFO(mode)) return PIPE;
543 int rpmfiCompareIndex(rpmfi afi, int aix, rpmfi bfi, int bix)
545 rpmFileTypes awhat = rpmfiWhatis(rpmfiFModeIndex(afi, aix));
546 rpmFileTypes bwhat = rpmfiWhatis(rpmfiFModeIndex(bfi, bix));
548 if ((rpmfiFFlagsIndex(afi, aix) & RPMFILE_GHOST) ||
549 (rpmfiFFlagsIndex(bfi, bix) & RPMFILE_GHOST)) return 0;
551 if (awhat != bwhat) return 1;
554 const char * alink = rpmfiFLinkIndex(afi, aix);
555 const char * blink = rpmfiFLinkIndex(bfi, bix);
556 if (alink == blink) return 0;
557 if (alink == NULL) return 1;
558 if (blink == NULL) return -1;
559 return strcmp(alink, blink);
560 } else if (awhat == REG) {
561 size_t adiglen, bdiglen;
563 const unsigned char * adigest, * bdigest;
564 adigest = rpmfiFDigestIndex(afi, aix, &aalgo, &adiglen);
565 bdigest = rpmfiFDigestIndex(bfi, bix, &balgo, &bdiglen);
566 if (adigest == bdigest) return 0;
567 if (adigest == NULL) return 1;
568 if (bdigest == NULL) return -1;
569 /* can't meaningfully compare different hash types */
570 if (aalgo != balgo || adiglen != bdiglen) return -1;
571 return memcmp(adigest, bdigest, adiglen);
572 } else if (awhat == CDEV || awhat == BDEV) {
573 if (rpmfiFRdevIndex(afi, aix) != rpmfiFRdevIndex(bfi, bix))
580 rpmFileAction rpmfiDecideFateIndex(rpmfi ofi, int oix, rpmfi nfi, int nix,
583 char * fn = rpmfiFNIndex(nfi, nix);
584 rpmfileAttrs newFlags = rpmfiFFlagsIndex(nfi, nix);
586 rpmFileTypes dbWhat, newWhat, diskWhat;
588 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
589 int action = FA_CREATE; /* assume we can create */
591 if (lstat(fn, &sb)) {
593 * The file doesn't exist on the disk. Create it unless the new
594 * package has marked it as missingok, or allfiles is requested.
596 if (skipMissing && (newFlags & RPMFILE_MISSINGOK)) {
597 rpmlog(RPMLOG_DEBUG, "%s skipped due to missingok flag\n",
604 diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
605 dbWhat = rpmfiWhatis(rpmfiFModeIndex(ofi, oix));
606 newWhat = rpmfiWhatis(rpmfiFModeIndex(nfi, nix));
609 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
610 * them in older packages as well.
615 if (diskWhat != newWhat && dbWhat != REG && dbWhat != LINK) {
618 } else if (newWhat != dbWhat && diskWhat != dbWhat) {
621 } else if (dbWhat != newWhat) {
623 } else if (dbWhat != LINK && dbWhat != REG) {
628 * This order matters - we'd prefer to CREATE the file if at all
629 * possible in case something else (like the timestamp) has changed.
631 memset(buffer, 0, sizeof(buffer));
634 size_t odiglen, ndiglen;
635 const unsigned char * odigest, * ndigest;
636 odigest = rpmfiFDigestIndex(ofi, oix, &oalgo, &odiglen);
637 if (diskWhat == REG) {
638 if (rpmDoDigest(oalgo, fn, 0, (unsigned char *)buffer, NULL))
639 goto exit; /* assume file has been removed */
640 if (odigest && !memcmp(odigest, buffer, odiglen))
641 goto exit; /* unmodified config file, replace. */
643 ndigest = rpmfiFDigestIndex(nfi, nix, &nalgo, &ndiglen);
644 /* Can't compare different hash types, backup to avoid data loss */
645 if (oalgo != nalgo || odiglen != ndiglen) {
649 if (odigest && ndigest && !memcmp(odigest, ndigest, odiglen)) {
650 action = FA_SKIP; /* identical file, don't bother. */
653 /* ... but otherwise backup will be needed */
655 } else /* dbWhat == LINK */ {
656 const char * oFLink, * nFLink;
657 oFLink = rpmfiFLinkIndex(ofi, oix);
658 if (diskWhat == LINK) {
659 ssize_t link_len = readlink(fn, buffer, sizeof(buffer) - 1);
661 goto exit; /* assume file has been removed */
662 buffer[link_len] = '\0';
663 if (oFLink && rstreq(oFLink, buffer))
664 goto exit; /* unmodified config file, replace. */
666 nFLink = rpmfiFLinkIndex(nfi, nix);
667 if (oFLink && nFLink && rstreq(oFLink, nFLink)) {
668 action = FA_SKIP; /* identical file, don't bother. */
671 /* ... but otherwise backup will be needed */
680 int rpmfiConfigConflictIndex(rpmfi fi, int ix)
683 rpmfileAttrs flags = rpmfiFFlagsIndex(fi, ix);
685 rpmFileTypes newWhat, diskWhat;
689 if (!(flags & RPMFILE_CONFIG))
692 /* Only links and regular files can be %config, this is kinda moot */
693 /* XXX: Why are we returning 1 here? */
694 newWhat = rpmfiWhatis(rpmfiFModeIndex(fi, ix));
695 if (newWhat != LINK && newWhat != REG)
698 /* If it's not on disk, there's nothing to be saved */
699 fn = rpmfiFNIndex(fi, ix);
703 /* Files of different types obviously are not identical */
704 diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
705 if (diskWhat != newWhat) {
710 memset(buffer, 0, sizeof(buffer));
711 if (newWhat == REG) {
714 const unsigned char *ndigest = rpmfiFDigestIndex(fi,ix, &algo, &diglen);
715 if (rpmDoDigest(algo, fn, 0, (unsigned char *)buffer, NULL))
716 goto exit; /* assume file has been removed */
717 if (ndigest && !memcmp(ndigest, buffer, diglen))
718 goto exit; /* unmodified config file */
719 } else /* newWhat == LINK */ {
721 ssize_t link_len = readlink(fn, buffer, sizeof(buffer) - 1);
723 goto exit; /* assume file has been removed */
724 buffer[link_len] = '\0';
725 nFLink = rpmfiFLinkIndex(fi, ix);
726 if (nFLink && rstreq(nFLink, buffer))
727 goto exit; /* unmodified config file */
737 static char **duparray(char ** src, int size)
739 char **dest = xmalloc((size+1) * sizeof(*dest));
740 for (int i = 0; i < size; i++) {
741 dest[i] = xstrdup(src[i]);
747 static int addPrefixes(Header h, rpmRelocation *relocations, int numRelocations)
749 struct rpmtd_s validRelocs;
750 const char *validprefix;
751 const char ** actualRelocations;
754 headerGet(h, RPMTAG_PREFIXES, &validRelocs, HEADERGET_MINMEM);
756 * If no relocations are specified (usually the case), then return the
757 * original header. If there are prefixes, however, then INSTPREFIXES
758 * should be added for RPM_INSTALL_PREFIX environ variables in scriptlets,
759 * but, since relocateFileList() can be called more than once for
760 * the same header, don't bother if already present.
762 if (relocations == NULL || numRelocations == 0) {
763 if (rpmtdCount(&validRelocs) > 0) {
764 if (!headerIsEntry(h, RPMTAG_INSTPREFIXES)) {
765 rpmtdSetTag(&validRelocs, RPMTAG_INSTPREFIXES);
766 headerPut(h, &validRelocs, HEADERPUT_DEFAULT);
768 rpmtdFreeData(&validRelocs);
773 actualRelocations = xmalloc(rpmtdCount(&validRelocs) * sizeof(*actualRelocations));
774 rpmtdInit(&validRelocs);
775 while ((validprefix = rpmtdNextString(&validRelocs))) {
777 for (j = 0; j < numRelocations; j++) {
778 if (relocations[j].oldPath == NULL || /* XXX can't happen */
779 !rstreq(validprefix, relocations[j].oldPath))
781 /* On install, a relocate to NULL means skip the path. */
782 if (relocations[j].newPath) {
783 actualRelocations[numActual] = relocations[j].newPath;
788 if (j == numRelocations) {
789 actualRelocations[numActual] = validprefix;
793 rpmtdFreeData(&validRelocs);
796 headerPutStringArray(h, RPMTAG_INSTPREFIXES, actualRelocations, numActual);
798 free(actualRelocations);
802 static void saveRelocs(Header h, rpmtd bnames, rpmtd dnames, rpmtd dindexes)
805 headerGet(h, RPMTAG_BASENAMES, &td, HEADERGET_MINMEM);
806 rpmtdSetTag(&td, RPMTAG_ORIGBASENAMES);
807 headerPut(h, &td, HEADERPUT_DEFAULT);
810 headerGet(h, RPMTAG_DIRNAMES, &td, HEADERGET_MINMEM);
811 rpmtdSetTag(&td, RPMTAG_ORIGDIRNAMES);
812 headerPut(h, &td, HEADERPUT_DEFAULT);
815 headerGet(h, RPMTAG_DIRINDEXES, &td, HEADERGET_MINMEM);
816 rpmtdSetTag(&td, RPMTAG_ORIGDIRINDEXES);
817 headerPut(h, &td, HEADERPUT_DEFAULT);
820 headerMod(h, bnames);
821 headerMod(h, dnames);
822 headerMod(h, dindexes);
825 void rpmRelocateFileList(rpmRelocation *relocations, int numRelocations,
828 static int _printed = 0;
831 uint32_t * dirIndexes;
832 rpm_count_t fileCount, dirCount;
836 int haveRelocatedBase = 0;
839 struct rpmtd_s bnames, dnames, dindexes, fmodes;
841 addPrefixes(h, relocations, numRelocations);
845 rpmlog(RPMLOG_DEBUG, "========== relocations\n");
846 for (i = 0; i < numRelocations; i++) {
847 if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
848 if (relocations[i].newPath == NULL)
849 rpmlog(RPMLOG_DEBUG, "%5d exclude %s\n",
850 i, relocations[i].oldPath);
852 rpmlog(RPMLOG_DEBUG, "%5d relocate %s -> %s\n",
853 i, relocations[i].oldPath, relocations[i].newPath);
857 for (i = 0; i < numRelocations; i++) {
858 if (relocations[i].newPath == NULL) continue;
859 size_t len = strlen(relocations[i].newPath);
860 if (len > maxlen) maxlen = len;
863 headerGet(h, RPMTAG_BASENAMES, &bnames, HEADERGET_MINMEM);
864 headerGet(h, RPMTAG_DIRINDEXES, &dindexes, HEADERGET_ALLOC);
865 headerGet(h, RPMTAG_DIRNAMES, &dnames, HEADERGET_MINMEM);
866 headerGet(h, RPMTAG_FILEMODES, &fmodes, HEADERGET_MINMEM);
867 /* TODO XXX ugh.. use rpmtd iterators & friends instead */
868 baseNames = bnames.data;
869 dirIndexes = dindexes.data;
870 fileCount = rpmtdCount(&bnames);
871 dirCount = rpmtdCount(&dnames);
872 /* XXX TODO: use rpmtdDup() instead */
873 dirNames = dnames.data = duparray(dnames.data, dirCount);
874 dnames.flags |= RPMTD_PTR_ALLOCED;
877 * For all relocations, we go through sorted file/relocation lists
878 * backwards so that /usr/local relocations take precedence over /usr
882 /* Relocate individual paths. */
884 for (i = fileCount - 1; i >= 0; i--) {
888 size_t len = maxlen +
889 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
890 if (len >= fileAlloced) {
891 fileAlloced = len * 2;
892 fn = xrealloc(fn, fileAlloced);
895 assert(fn != NULL); /* XXX can't happen */
897 fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
900 * See if this file path needs relocating.
903 * XXX FIXME: Would a bsearch of the (already sorted)
904 * relocation list be a good idea?
906 for (j = numRelocations - 1; j >= 0; j--) {
907 if (relocations[j].oldPath == NULL) /* XXX can't happen */
909 len = !rstreq(relocations[j].oldPath, "/")
910 ? strlen(relocations[j].oldPath)
916 * Only subdirectories or complete file paths may be relocated. We
917 * don't check for '\0' as our directory names all end in '/'.
919 if (!(fn[len] == '/' || fnlen == len))
922 if (!rstreqn(relocations[j].oldPath, fn, len))
928 rpmtdSetIndex(&fmodes, i);
929 ft = rpmfiWhatis(rpmtdGetNumber(&fmodes));
931 /* On install, a relocate to NULL means skip the path. */
932 if (relocations[j].newPath == NULL) {
934 /* Start with the parent, looking for directory to exclude. */
935 for (j = dirIndexes[i]; j < dirCount; j++) {
936 len = strlen(dirNames[j]) - 1;
937 while (len > 0 && dirNames[j][len-1] == '/') len--;
940 if (!rstreqn(fn, dirNames[j], fnlen))
945 rpmfsSetAction(fs, i, FA_SKIPNSTATE);
946 rpmlog(RPMLOG_DEBUG, "excluding %s %s\n",
951 /* Relocation on full paths only, please. */
952 if (fnlen != len) continue;
954 rpmlog(RPMLOG_DEBUG, "relocating %s to %s\n",
955 fn, relocations[j].newPath);
958 strcpy(fn, relocations[j].newPath);
959 { char * te = strrchr(fn, '/');
961 if (te > fn) te++; /* root is special */
964 te = fn + strlen(fn);
965 if (!rstreq(baseNames[i], te)) { /* basename changed too? */
966 if (!haveRelocatedBase) {
967 /* XXX TODO: use rpmtdDup() instead */
968 bnames.data = baseNames = duparray(baseNames, fileCount);
969 bnames.flags |= RPMTD_PTR_ALLOCED;
970 haveRelocatedBase = 1;
973 baseNames[i] = xstrdup(te);
975 *te = '\0'; /* terminate new directory name */
978 /* Does this directory already exist in the directory list? */
979 for (j = 0; j < dirCount; j++) {
980 if (fnlen != strlen(dirNames[j]))
982 if (!rstreqn(fn, dirNames[j], fnlen))
992 /* Creating new paths is a pita */
993 dirNames = dnames.data = xrealloc(dnames.data,
994 sizeof(*dirNames) * (dirCount + 1));
996 dirNames[dirCount] = xstrdup(fn);
997 dirIndexes[i] = dirCount;
1002 /* Finish off by relocating directories. */
1003 for (i = dirCount - 1; i >= 0; i--) {
1004 for (j = numRelocations - 1; j >= 0; j--) {
1006 if (relocations[j].oldPath == NULL) /* XXX can't happen */
1008 size_t len = !rstreq(relocations[j].oldPath, "/")
1009 ? strlen(relocations[j].oldPath)
1012 if (len && !rstreqn(relocations[j].oldPath, dirNames[i], len))
1016 * Only subdirectories or complete file paths may be relocated. We
1017 * don't check for '\0' as our directory names all end in '/'.
1019 if (dirNames[i][len] != '/')
1022 if (relocations[j].newPath) { /* Relocate the path */
1024 rstrscat(&t, relocations[j].newPath, (dirNames[i] + len), NULL);
1025 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
1026 (void) rpmCleanPath(t);
1029 rpmlog(RPMLOG_DEBUG,
1030 "relocating directory %s to %s\n", dirNames[i], t);
1038 /* Save original filenames in header and replace (relocated) filenames. */
1040 saveRelocs(h, &bnames, &dnames, &dindexes);
1043 rpmtdFreeData(&bnames);
1044 rpmtdFreeData(&dnames);
1045 rpmtdFreeData(&dindexes);
1046 rpmtdFreeData(&fmodes);
1050 rpmfi rpmfiFree(rpmfi fi)
1052 if (fi == NULL) return NULL;
1055 return rpmfiUnlink(fi);
1058 fi->bnl = _free(fi->bnl);
1059 fi->dnl = _free(fi->dnl);
1061 fi->flinkcache = strcacheFree(fi->flinkcache);
1062 fi->flinks = _free(fi->flinks);
1063 fi->flangs = _free(fi->flangs);
1064 fi->digests = _free(fi->digests);
1065 fi->fcapcache = strcacheFree(fi->fcapcache);
1066 fi->fcaps = _free(fi->fcaps);
1068 fi->cdict = _free(fi->cdict);
1070 fi->fuser = _free(fi->fuser);
1071 fi->fgroup = _free(fi->fgroup);
1073 fi->fstates = _free(fi->fstates);
1074 fi->fps = _free(fi->fps);
1076 /* these point to header memory if KEEPHEADER is used, dont free */
1077 if (!(fi->fiflags & RPMFI_KEEPHEADER) && fi->h == NULL) {
1078 fi->fmtimes = _free(fi->fmtimes);
1079 fi->fmodes = _free(fi->fmodes);
1080 fi->fflags = _free(fi->fflags);
1081 fi->vflags = _free(fi->vflags);
1082 fi->fsizes = _free(fi->fsizes);
1083 fi->frdevs = _free(fi->frdevs);
1084 fi->finodes = _free(fi->finodes);
1085 fi->dil = _free(fi->dil);
1087 fi->fcolors = _free(fi->fcolors);
1088 fi->fcdictx = _free(fi->fcdictx);
1089 fi->ddict = _free(fi->ddict);
1090 fi->fddictx = _free(fi->fddictx);
1091 fi->fddictn = _free(fi->fddictn);
1096 fi->fn = _free(fi->fn);
1097 fi->apath = _free(fi->apath);
1099 fi->replacedSizes = _free(fi->replacedSizes);
1101 fi->h = headerFree(fi->h);
1103 (void) rpmfiUnlink(fi);
1104 memset(fi, 0, sizeof(*fi)); /* XXX trash and burn */
1110 /* Helper to push header tag data into a string cache */
1111 static scidx_t *cacheTag(strcache cache, Header h, rpmTag tag)
1113 scidx_t *idx = NULL;
1115 if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1116 idx = xmalloc(sizeof(*idx) * rpmtdCount(&td));
1119 while ((str = rpmtdNextString(&td))) {
1120 idx[i++] = strcachePut(cache, str);
1127 #define _hgfi(_h, _tag, _td, _flags, _data) \
1128 if (headerGet((_h), (_tag), (_td), (_flags))) \
1131 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTagVal tagN, rpmfiFlags flags)
1133 rpmfi fi = xcalloc(1, sizeof(*fi));
1135 struct rpmtd_s fdigests, digalgo;
1137 headerGetFlags scareFlags = (flags & RPMFI_KEEPHEADER) ?
1138 HEADERGET_MINMEM : HEADERGET_ALLOC;
1139 headerGetFlags defFlags = HEADERGET_ALLOC;
1141 fi->magic = RPMFIMAGIC;
1144 fi->fiflags = flags;
1146 _hgfi(h, RPMTAG_BASENAMES, &td, defFlags, fi->bnl);
1147 fi->fc = rpmtdCount(&td);
1152 _hgfi(h, RPMTAG_DIRNAMES, &td, defFlags, fi->dnl);
1153 fi->dc = rpmtdCount(&td);
1154 _hgfi(h, RPMTAG_DIRINDEXES, &td, scareFlags, fi->dil);
1156 /* Is our filename triplet sane? */
1157 if (fi->dc == 0 || fi->dc > fi->fc || rpmtdCount(&td) != fi->fc)
1160 for (rpm_count_t i = 0; i < fi->fc; i++) {
1161 if (fi->dil[i] >= fi->fc)
1165 /* XXX TODO: all these should be sanity checked, ugh... */
1166 if (!(flags & RPMFI_NOFILEMODES))
1167 _hgfi(h, RPMTAG_FILEMODES, &td, scareFlags, fi->fmodes);
1168 if (!(flags & RPMFI_NOFILEFLAGS))
1169 _hgfi(h, RPMTAG_FILEFLAGS, &td, scareFlags, fi->fflags);
1170 if (!(flags & RPMFI_NOFILEVERIFYFLAGS))
1171 _hgfi(h, RPMTAG_FILEVERIFYFLAGS, &td, scareFlags, fi->vflags);
1172 if (!(flags & RPMFI_NOFILESIZES))
1173 _hgfi(h, RPMTAG_FILESIZES, &td, scareFlags, fi->fsizes);
1175 if (!(flags & RPMFI_NOFILECOLORS))
1176 _hgfi(h, RPMTAG_FILECOLORS, &td, scareFlags, fi->fcolors);
1178 if (!(flags & RPMFI_NOFILECLASS)) {
1179 _hgfi(h, RPMTAG_CLASSDICT, &td, scareFlags, fi->cdict);
1180 fi->ncdict = rpmtdCount(&td);
1181 _hgfi(h, RPMTAG_FILECLASS, &td, scareFlags, fi->fcdictx);
1183 if (!(flags & RPMFI_NOFILEDEPS)) {
1184 _hgfi(h, RPMTAG_DEPENDSDICT, &td, scareFlags, fi->ddict);
1185 fi->nddict = rpmtdCount(&td);
1186 _hgfi(h, RPMTAG_FILEDEPENDSX, &td, scareFlags, fi->fddictx);
1187 _hgfi(h, RPMTAG_FILEDEPENDSN, &td, scareFlags, fi->fddictn);
1190 if (!(flags & RPMFI_NOFILESTATES))
1191 _hgfi(h, RPMTAG_FILESTATES, &td, defFlags, fi->fstates);
1193 if (!(flags & RPMFI_NOFILECAPS) && headerIsEntry(h, RPMTAG_FILECAPS)) {
1194 fi->fcapcache = strcacheNew();
1195 fi->fcaps = cacheTag(fi->fcapcache, h, RPMTAG_FILECAPS);
1198 if (!(flags & RPMFI_NOFILELINKTOS)) {
1199 fi->flinkcache = strcacheNew();
1200 fi->flinks = cacheTag(fi->flinkcache, h, RPMTAG_FILELINKTOS);
1202 /* FILELANGS are only interesting when installing */
1203 if ((headerGetInstance(h) == 0) && !(flags & RPMFI_NOFILELANGS))
1204 fi->flangs = cacheTag(langcache, h, RPMTAG_FILELANGS);
1206 /* See if the package has non-md5 file digests */
1207 fi->digestalgo = PGPHASHALGO_MD5;
1208 if (headerGet(h, RPMTAG_FILEDIGESTALGO, &digalgo, HEADERGET_MINMEM)) {
1209 uint32_t *algo = rpmtdGetUint32(&digalgo);
1210 /* Hmm, what to do with unknown digest algorithms? */
1211 if (algo && rpmDigestLength(*algo) != 0) {
1212 fi->digestalgo = *algo;
1217 /* grab hex digests from header and store in binary format */
1218 if (!(flags & RPMFI_NOFILEDIGESTS) &&
1219 headerGet(h, RPMTAG_FILEDIGESTS, &fdigests, HEADERGET_MINMEM)) {
1220 const char *fdigest;
1221 size_t diglen = rpmDigestLength(fi->digestalgo);
1222 fi->digests = t = xmalloc(rpmtdCount(&fdigests) * diglen);
1224 while ((fdigest = rpmtdNextString(&fdigests))) {
1225 if (!(fdigest && *fdigest != '\0')) {
1226 memset(t, 0, diglen);
1230 for (int j = 0; j < diglen; j++, t++, fdigest += 2)
1231 *t = (rnibble(fdigest[0]) << 4) | rnibble(fdigest[1]);
1233 rpmtdFreeData(&fdigests);
1236 /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes */
1237 if (!(flags & RPMFI_NOFILEMTIMES))
1238 _hgfi(h, RPMTAG_FILEMTIMES, &td, scareFlags, fi->fmtimes);
1239 if (!(flags & RPMFI_NOFILERDEVS))
1240 _hgfi(h, RPMTAG_FILERDEVS, &td, scareFlags, fi->frdevs);
1241 if (!(flags & RPMFI_NOFILEINODES))
1242 _hgfi(h, RPMTAG_FILEINODES, &td, scareFlags, fi->finodes);
1244 if (!(flags & RPMFI_NOFILEUSER))
1245 fi->fuser = cacheTag(ugcache, h, RPMTAG_FILEUSERNAME);
1246 if (!(flags & RPMFI_NOFILEGROUP))
1247 fi->fgroup = cacheTag(ugcache, h, RPMTAG_FILEGROUPNAME);
1249 /* lazily alloced from rpmfiFN() */
1255 fi->h = (fi->fiflags & RPMFI_KEEPHEADER) ? headerLink(h) : NULL;
1258 /* FIX: rpmfi null annotations */
1259 return rpmfiLink(fi);
1266 void rpmfiSetFReplacedSizeIndex(rpmfi fi, int ix, rpm_loff_t newsize)
1268 if (fi != NULL && ix >= 0 && ix < fi->fc) {
1269 if (fi->replacedSizes == NULL) {
1270 fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1272 /* XXX watch out, replacedSizes is not rpm_loff_t (yet) */
1273 fi->replacedSizes[ix] = (rpm_off_t) newsize;
1277 rpm_loff_t rpmfiFReplacedSizeIndex(rpmfi fi, int ix)
1279 rpm_loff_t rsize = 0;
1280 if (fi != NULL && ix >= 0 && ix < fi->fc) {
1281 if (fi->replacedSizes) {
1282 rsize = fi->replacedSizes[ix];
1288 void rpmfiFpLookup(rpmfi fi, fingerPrintCache fpc)
1290 if (fi->fc > 0 && fi->fps == NULL) {
1291 fi->fps = xcalloc(fi->fc, sizeof(*fi->fps));
1293 fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1297 * Generate iterator accessors function wrappers, these do nothing but
1298 * call the corresponding rpmfiFooIndex(fi, fi->[ij])
1301 #define RPMFI_ITERFUNC(TYPE, NAME, IXV) \
1302 TYPE rpmfi ## NAME(rpmfi fi) { return rpmfi ## NAME ## Index(fi, fi ? fi->IXV : -1); }
1304 RPMFI_ITERFUNC(const char *, BN, i)
1305 RPMFI_ITERFUNC(const char *, DN, j)
1306 RPMFI_ITERFUNC(const char *, FLink, i)
1307 RPMFI_ITERFUNC(const char *, FUser, i)
1308 RPMFI_ITERFUNC(const char *, FGroup, i)
1309 RPMFI_ITERFUNC(const char *, FCaps, i)
1310 RPMFI_ITERFUNC(const char *, FLangs, i)
1311 RPMFI_ITERFUNC(const char *, FClass, i)
1312 RPMFI_ITERFUNC(rpmfileState, FState, i)
1313 RPMFI_ITERFUNC(rpmfileAttrs, FFlags, i)
1314 RPMFI_ITERFUNC(rpmVerifyAttrs, VFlags, i)
1315 RPMFI_ITERFUNC(rpm_mode_t, FMode, i)
1316 RPMFI_ITERFUNC(rpm_rdev_t, FRdev, i)
1317 RPMFI_ITERFUNC(rpm_time_t, FMtime, i)
1318 RPMFI_ITERFUNC(rpm_ino_t, FInode, i)
1319 RPMFI_ITERFUNC(rpm_loff_t, FSize, i)
1320 RPMFI_ITERFUNC(rpm_color_t, FColor, i)
1321 RPMFI_ITERFUNC(uint32_t, FNlink, i)
1323 const char * rpmfiFN(rpmfi fi)
1325 const char *fn = ""; /* preserve behavior on errors */
1328 fi->fn = rpmfiFNIndex(fi, fi->i);
1335 const unsigned char * rpmfiFDigest(rpmfi fi, int *algo, size_t *len)
1337 return rpmfiFDigestIndex(fi, fi ? fi->i : -1, algo, len);
1340 uint32_t rpmfiFDepends(rpmfi fi, const uint32_t ** fddictp)
1342 return rpmfiFDependsIndex(fi, fi ? fi->i : -1, fddictp);
1345 int rpmfiCompare(const rpmfi afi, const rpmfi bfi)
1347 return rpmfiCompareIndex(afi, afi ? afi->i : -1, bfi, bfi ? bfi->i : -1);
1350 rpmFileAction rpmfiDecideFate(const rpmfi ofi, rpmfi nfi, int skipMissing)
1352 return rpmfiDecideFateIndex(ofi, ofi ? ofi->i : -1,
1353 nfi, nfi ? nfi->i : -1,
1357 int rpmfiConfigConflict(const rpmfi fi)
1359 return rpmfiConfigConflictIndex(fi, fi ? fi->i : -1);