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() */
25 rpmfi rpmfiUnlink(rpmfi fi, const char * msg)
27 if (fi == NULL) return NULL;
28 if (_rpmfi_debug && msg != NULL)
29 fprintf(stderr, "--> fi %p -- %d %s\n", fi, fi->nrefs, msg);
34 rpmfi rpmfiLink(rpmfi fi, const char * msg)
36 if (fi == NULL) return NULL;
38 if (_rpmfi_debug && msg != NULL)
39 fprintf(stderr, "--> fi %p ++ %d %s\n", fi, fi->nrefs, msg);
43 rpm_count_t rpmfiFC(rpmfi fi)
45 return (fi != NULL ? fi->fc : 0);
48 rpm_count_t rpmfiDC(rpmfi fi)
50 return (fi != NULL ? fi->dc : 0);
61 return (fi != NULL ? fi->i : -1);
64 int rpmfiSetFX(rpmfi fi, int fx)
68 if (fi != NULL && fx >= 0 && fx < fi->fc) {
71 fi->j = fi->dil[fi->i];
78 return (fi != NULL ? fi->j : -1);
81 int rpmfiSetDX(rpmfi fi, int dx)
85 if (fi != NULL && dx >= 0 && dx < fi->dc) {
92 const char * rpmfiBN(rpmfi fi)
94 const char * BN = NULL;
96 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
103 const char * rpmfiDN(rpmfi fi)
105 const char * DN = NULL;
107 if (fi != NULL && fi->j >= 0 && fi->j < fi->dc) {
114 const char * rpmfiFN(rpmfi fi)
116 const char * FN = "";
118 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
121 fi->fn = xmalloc(fi->fnlen);
124 t = stpcpy(t, fi->dnl[fi->dil[fi->i]]);
125 t = stpcpy(t, fi->bnl[fi->i]);
130 rpmfileAttrs rpmfiFFlags(rpmfi fi)
132 rpmfileAttrs FFlags = 0;
134 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
135 if (fi->fflags != NULL)
136 FFlags = fi->fflags[fi->i];
141 rpmVerifyAttrs rpmfiVFlags(rpmfi fi)
143 rpmVerifyAttrs VFlags = 0;
145 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
146 if (fi->vflags != NULL)
147 VFlags = fi->vflags[fi->i];
152 rpm_mode_t rpmfiFMode(rpmfi fi)
154 rpm_mode_t fmode = 0;
156 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
157 if (fi->fmodes != NULL)
158 fmode = fi->fmodes[fi->i];
163 rpmfileState rpmfiFState(rpmfi fi)
165 rpmfileState fstate = RPMFILE_STATE_MISSING;
167 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
168 if (fi->fstates != NULL)
169 fstate = fi->fstates[fi->i];
174 const unsigned char * rpmfiMD5(rpmfi fi)
176 const unsigned char *digest;
177 pgpHashAlgo algo = 0;
179 digest = rpmfiFDigest(fi, &algo, NULL);
180 return (algo == PGPHASHALGO_MD5) ? digest : NULL;
183 const unsigned char * rpmfiFDigest(rpmfi fi, pgpHashAlgo *algo, size_t *len)
185 const unsigned char *digest = NULL;
187 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
188 size_t diglen = rpmDigestLength(fi->digestalgo);
189 if (fi->digests != NULL)
190 digest = fi->digests + (diglen * fi->i);
194 *algo = fi->digestalgo;
199 char * rpmfiFDigestHex(rpmfi fi, pgpHashAlgo *algo)
202 char *fdigest = NULL;
203 const unsigned char *digest = rpmfiFDigest(fi, algo, &diglen);
205 fdigest = pgpHexStr(digest, diglen);
210 const char * rpmfiFLink(rpmfi fi)
212 const char * flink = NULL;
214 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
215 if (fi->flinks != NULL)
216 flink = fi->flinks[fi->i];
221 rpm_loff_t rpmfiFSize(rpmfi fi)
223 rpm_loff_t fsize = 0;
225 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
226 if (fi->fsizes != NULL)
227 fsize = fi->fsizes[fi->i];
232 rpm_rdev_t rpmfiFRdev(rpmfi fi)
234 rpm_rdev_t frdev = 0;
236 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
237 if (fi->frdevs != NULL)
238 frdev = fi->frdevs[fi->i];
243 rpm_ino_t rpmfiFInode(rpmfi fi)
245 rpm_ino_t finode = 0;
247 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
248 if (fi->finodes != NULL)
249 finode = fi->finodes[fi->i];
254 rpm_color_t rpmfiColor(rpmfi fi)
256 rpm_color_t color = 0;
258 if (fi != NULL && fi->fcolors != NULL) {
259 for (int i = 0; i < fi->fc; i++)
260 color |= fi->fcolors[i];
261 /* XXX ignore all but lsnibble for now. */
267 rpm_color_t rpmfiFColor(rpmfi fi)
269 rpm_color_t fcolor = 0;
271 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
272 if (fi->fcolors != NULL)
273 /* XXX ignore all but lsnibble for now. */
274 fcolor = (fi->fcolors[fi->i] & 0x0f);
279 const char * rpmfiFClass(rpmfi fi)
281 const char * fclass = NULL;
284 if (fi != NULL && fi->fcdictx != NULL && fi->i >= 0 && fi->i < fi->fc) {
285 cdictx = fi->fcdictx[fi->i];
286 if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
287 fclass = fi->cdict[cdictx];
292 const char * rpmfiFContext(rpmfi fi)
294 const char * fcontext = NULL;
296 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
297 if (fi->fcontexts != NULL)
298 fcontext = fi->fcontexts[fi->i];
303 uint32_t rpmfiFDepends(rpmfi fi, const uint32_t ** fddictp)
307 const uint32_t * fddict = NULL;
309 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
310 if (fi->fddictn != NULL)
311 fddictn = fi->fddictn[fi->i];
312 if (fddictn > 0 && fi->fddictx != NULL)
313 fddictx = fi->fddictx[fi->i];
314 if (fi->ddict != NULL && fddictx >= 0 && (fddictx+fddictn) <= fi->nddict)
315 fddict = fi->ddict + fddictx;
322 uint32_t rpmfiFNlink(rpmfi fi)
326 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
327 /* XXX rpm-2.3.12 has not RPMTAG_FILEINODES */
328 if (fi->finodes && fi->frdevs) {
329 rpm_ino_t finode = fi->finodes[fi->i];
330 rpm_rdev_t frdev = fi->frdevs[fi->i];
333 for (j = 0; j < fi->fc; j++) {
334 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
342 rpm_time_t rpmfiFMtime(rpmfi fi)
344 rpm_time_t fmtime = 0;
346 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
347 if (fi->fmtimes != NULL)
348 fmtime = fi->fmtimes[fi->i];
353 const char * rpmfiFUser(rpmfi fi)
355 const char * fuser = NULL;
357 /* XXX add support for ancient RPMTAG_FILEUIDS? */
358 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
359 if (fi->fuser != NULL)
360 fuser = fi->fuser[fi->i];
365 const char * rpmfiFGroup(rpmfi fi)
367 const char * fgroup = NULL;
369 /* XXX add support for ancient RPMTAG_FILEGIDS? */
370 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
371 if (fi->fgroup != NULL)
372 fgroup = fi->fgroup[fi->i];
377 const char * rpmfiFCaps(rpmfi fi)
379 const char *fcaps = NULL;
380 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
381 fcaps = fi->fcaps ? fi->fcaps[fi->i] : "";
386 int rpmfiNext(rpmfi fi)
390 if (fi != NULL && ++fi->i >= 0) {
391 if (fi->i < fi->fc) {
394 fi->j = fi->dil[fi->i];
398 if (_rpmfi_debug < 0 && i != -1)
399 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] : ""));
406 rpmfi rpmfiInit(rpmfi fi, int fx)
409 if (fx >= 0 && fx < fi->fc) {
418 int rpmfiNextD(rpmfi fi)
422 if (fi != NULL && ++fi->j >= 0) {
428 if (_rpmfi_debug < 0 && j != -1)
429 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, (fi->Type ? fi->Type : "?Type?"), j);
436 rpmfi rpmfiInitD(rpmfi fi, int dx)
439 if (dx >= 0 && dx < fi->fc)
449 * Identify a file type.
450 * @param ft file type
451 * @return string to identify a file type
454 const char * ftstring (rpmFileTypes ft)
457 case XDIR: return "directory";
458 case CDEV: return "char dev";
459 case BDEV: return "block dev";
460 case LINK: return "link";
461 case SOCK: return "sock";
462 case PIPE: return "fifo/pipe";
463 case REG: return "file";
464 default: return "unknown file type";
468 rpmFileTypes rpmfiWhatis(rpm_mode_t mode)
470 if (S_ISDIR(mode)) return XDIR;
471 if (S_ISCHR(mode)) return CDEV;
472 if (S_ISBLK(mode)) return BDEV;
473 if (S_ISLNK(mode)) return LINK;
474 if (S_ISSOCK(mode)) return SOCK;
475 if (S_ISFIFO(mode)) return PIPE;
479 int rpmfiCompare(const rpmfi afi, const rpmfi bfi)
481 rpmFileTypes awhat = rpmfiWhatis(rpmfiFMode(afi));
482 rpmFileTypes bwhat = rpmfiWhatis(rpmfiFMode(bfi));
484 if ((rpmfiFFlags(afi) & RPMFILE_GHOST) ||
485 (rpmfiFFlags(bfi) & RPMFILE_GHOST)) return 0;
487 if (awhat != bwhat) return 1;
490 const char * alink = rpmfiFLink(afi);
491 const char * blink = rpmfiFLink(bfi);
492 if (alink == blink) return 0;
493 if (alink == NULL) return 1;
494 if (blink == NULL) return -1;
495 return strcmp(alink, blink);
496 } else if (awhat == REG) {
497 size_t adiglen, bdiglen;
498 pgpHashAlgo aalgo, balgo;
499 const unsigned char * adigest = rpmfiFDigest(afi, &aalgo, &adiglen);
500 const unsigned char * bdigest = rpmfiFDigest(bfi, &balgo, &bdiglen);
501 if (adigest == bdigest) return 0;
502 if (adigest == NULL) return 1;
503 if (bdigest == NULL) return -1;
504 /* can't meaningfully compare different hash types */
505 if (aalgo != balgo || adiglen != bdiglen) return -1;
506 return memcmp(adigest, bdigest, adiglen);
512 rpmFileAction rpmfiDecideFate(const rpmfi ofi, rpmfi nfi, int skipMissing)
514 const char * fn = rpmfiFN(nfi);
515 rpmfileAttrs newFlags = rpmfiFFlags(nfi);
517 rpmFileTypes dbWhat, newWhat, diskWhat;
519 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
521 if (lstat(fn, &sb)) {
523 * The file doesn't exist on the disk. Create it unless the new
524 * package has marked it as missingok, or allfiles is requested.
526 if (skipMissing && (newFlags & RPMFILE_MISSINGOK)) {
527 rpmlog(RPMLOG_DEBUG, "%s skipped due to missingok flag\n",
535 diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
536 dbWhat = rpmfiWhatis(rpmfiFMode(ofi));
537 newWhat = rpmfiWhatis(rpmfiFMode(nfi));
540 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
541 * them in older packages as well.
546 if (diskWhat != newWhat && dbWhat != REG && dbWhat != LINK)
548 else if (newWhat != dbWhat && diskWhat != dbWhat)
550 else if (dbWhat != newWhat)
552 else if (dbWhat != LINK && dbWhat != REG)
556 * This order matters - we'd prefer to CREATE the file if at all
557 * possible in case something else (like the timestamp) has changed.
559 memset(buffer, 0, sizeof(buffer));
561 pgpHashAlgo oalgo, nalgo;
562 size_t odiglen, ndiglen;
563 const unsigned char * odigest, * ndigest;
564 odigest = rpmfiFDigest(ofi, &oalgo, &odiglen);
565 if (diskWhat == REG) {
566 if (rpmDoDigest(oalgo, fn, 0,
567 (unsigned char *)buffer, NULL))
568 return FA_CREATE; /* assume file has been removed */
569 if (odigest && !memcmp(odigest, buffer, odiglen))
570 return FA_CREATE; /* unmodified config file, replace. */
572 ndigest = rpmfiFDigest(nfi, &nalgo, &ndiglen);
573 /* XXX can't compare different hash types, what should we do here? */
574 if (oalgo != nalgo || odiglen != ndiglen)
576 if (odigest && ndigest && !memcmp(odigest, ndigest, odiglen))
577 return FA_SKIP; /* identical file, don't bother. */
578 } else /* dbWhat == LINK */ {
579 const char * oFLink, * nFLink;
580 oFLink = rpmfiFLink(ofi);
581 if (diskWhat == LINK) {
582 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
583 return FA_CREATE; /* assume file has been removed */
584 if (oFLink && !strcmp(oFLink, buffer))
585 return FA_CREATE; /* unmodified config file, replace. */
587 nFLink = rpmfiFLink(nfi);
588 if (oFLink && nFLink && !strcmp(oFLink, nFLink))
589 return FA_SKIP; /* identical file, don't bother. */
593 * The config file on the disk has been modified, but
594 * the ones in the two packages are different. It would
595 * be nice if RPM was smart enough to at least try and
596 * merge the difference ala CVS, but...
601 int rpmfiConfigConflict(const rpmfi fi)
603 const char * fn = rpmfiFN(fi);
604 rpmfileAttrs flags = rpmfiFFlags(fi);
606 rpmFileTypes newWhat, diskWhat;
609 if (!(flags & RPMFILE_CONFIG) || lstat(fn, &sb)) {
613 diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
614 newWhat = rpmfiWhatis(rpmfiFMode(fi));
616 if (newWhat != LINK && newWhat != REG)
619 if (diskWhat != newWhat)
622 memset(buffer, 0, sizeof(buffer));
623 if (newWhat == REG) {
626 const unsigned char *ndigest = rpmfiFDigest(fi, &algo, &diglen);
627 if (rpmDoDigest(algo, fn, 0, (unsigned char *)buffer, NULL))
628 return 0; /* assume file has been removed */
629 if (ndigest && !memcmp(ndigest, buffer, diglen))
630 return 0; /* unmodified config file */
631 } else /* newWhat == LINK */ {
633 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
634 return 0; /* assume file has been removed */
635 nFLink = rpmfiFLink(fi);
636 if (nFLink && !strcmp(nFLink, buffer))
637 return 0; /* unmodified config file */
643 const char * rpmfiTypeString(rpmfi fi)
645 switch(rpmteType(fi->te)) {
646 case TR_ADDED: return " install";
647 case TR_REMOVED: return " erase";
648 default: return "???";
652 static char **duparray(char ** src, int size)
654 char **dest = xmalloc((size+1) * sizeof(*dest));
655 for (int i = 0; i < size; i++) {
656 dest[i] = xstrdup(src[i]);
663 * Relocate files in header.
664 * @todo multilib file dispositions need to be checked.
665 * @param ts transaction set
666 * @param fi transaction element file info
667 * @param origH package header
668 * @param actions file dispositions
669 * @return header with relocated files
672 Header relocateFileList(const rpmts ts, rpmfi fi,
673 Header origH, rpmFileAction * actions)
675 rpmte p = rpmtsRelocateElement(ts);
676 static int _printed = 0;
677 int allowBadRelocate = (rpmtsFilterFlags(ts) & RPMPROB_FILTER_FORCERELOCATE);
678 rpmRelocation * relocations = NULL;
682 uint32_t * dirIndexes;
683 uint32_t * newDirIndexes;
684 rpm_count_t fileCount, dirCount, numValid = 0;
685 rpm_color_t * fColors = NULL;
686 rpm_color_t * dColors = NULL;
687 rpm_mode_t * fModes = NULL;
692 int haveRelocatedBase = 0;
696 struct rpmtd_s validRelocs;
697 struct rpmtd_s bnames, dnames, dindexes, fcolors, fmodes;
700 if (headerGet(origH, RPMTAG_PREFIXES, &validRelocs, HEADERGET_MINMEM))
701 numValid = rpmtdCount(&validRelocs);
706 while (p->relocs[numRelocations].newPath ||
707 p->relocs[numRelocations].oldPath)
711 * If no relocations are specified (usually the case), then return the
712 * original header. If there are prefixes, however, then INSTPREFIXES
713 * should be added, but, since relocateFileList() can be called more
714 * than once for the same header, don't bother if already present.
716 if (p->relocs == NULL || numRelocations == 0) {
718 if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES)) {
719 rpmtdSetTag(&validRelocs, RPMTAG_INSTPREFIXES);
720 headerPut(origH, &validRelocs, HEADERPUT_DEFAULT);
722 rpmtdFreeData(&validRelocs);
724 /* XXX FIXME multilib file actions need to be checked. */
725 return headerLink(origH);
728 h = headerLink(origH);
730 relocations = xmalloc(sizeof(*relocations) * numRelocations);
732 /* Build sorted relocation list from raw relocations. */
733 for (i = 0; i < numRelocations; i++) {
737 * Default relocations (oldPath == NULL) are handled in the UI,
740 if (p->relocs[i].oldPath == NULL) continue; /* XXX can't happen */
742 /* FIXME: Trailing /'s will confuse us greatly. Internal ones will
743 too, but those are more trouble to fix up. :-( */
744 t = xstrdup(p->relocs[i].oldPath);
745 relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
747 : stripTrailingChar(t, '/');
749 /* An old path w/o a new path is valid, and indicates exclusion */
750 if (p->relocs[i].newPath) {
753 const char *validprefix;
755 t = xstrdup(p->relocs[i].newPath);
756 relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
758 : stripTrailingChar(t, '/');
760 /* FIX: relocations[i].oldPath == NULL */
761 /* Verify that the relocation's old path is in the header. */
762 rpmtdInit(&validRelocs);
763 while ((validprefix = rpmtdNextString(&validRelocs))) {
764 if (strcmp(validprefix, relocations[i].oldPath) == 0) {
770 /* XXX actions check prevents problem from being appended twice. */
771 if (!valid && !allowBadRelocate && actions) {
772 rpmps ps = rpmtsProblems(ts);
773 rpmpsAppend(ps, RPMPROB_BADRELOCATE,
774 rpmteNEVRA(p), rpmteKey(p),
775 relocations[i].oldPath, NULL, NULL, 0);
779 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
784 relocations[i].newPath = NULL;
788 /* stupid bubble sort, but it's probably faster here */
789 for (i = 0; i < numRelocations; i++) {
792 for (j = 1; j < numRelocations; j++) {
793 rpmRelocation tmpReloc;
794 if (relocations[j - 1].oldPath == NULL || /* XXX can't happen */
795 relocations[j ].oldPath == NULL || /* XXX can't happen */
796 strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
799 tmpReloc = relocations[j - 1];
800 relocations[j - 1] = relocations[j];
801 relocations[j] = tmpReloc;
804 if (!madeSwap) break;
809 rpmlog(RPMLOG_DEBUG, "========== relocations\n");
810 for (i = 0; i < numRelocations; i++) {
811 if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
812 if (relocations[i].newPath == NULL)
813 rpmlog(RPMLOG_DEBUG, "%5d exclude %s\n",
814 i, relocations[i].oldPath);
816 rpmlog(RPMLOG_DEBUG, "%5d relocate %s -> %s\n",
817 i, relocations[i].oldPath, relocations[i].newPath);
821 /* Add relocation values to the header */
823 const char *validprefix;
824 const char ** actualRelocations;
825 rpm_count_t numActual;
827 actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
829 rpmtdInit(&validRelocs);
830 while ((validprefix = rpmtdNextString(&validRelocs))) {
831 for (j = 0; j < numRelocations; j++) {
832 if (relocations[j].oldPath == NULL || /* XXX can't happen */
833 strcmp(validprefix, relocations[j].oldPath))
835 /* On install, a relocate to NULL means skip the path. */
836 if (relocations[j].newPath) {
837 actualRelocations[numActual] = relocations[j].newPath;
842 if (j == numRelocations) {
843 actualRelocations[numActual] = validprefix;
849 headerPutStringArray(h, RPMTAG_INSTPREFIXES,
850 actualRelocations, numActual);
853 actualRelocations = _free(actualRelocations);
854 rpmtdFreeData(&validRelocs);
857 headerGet(h, RPMTAG_BASENAMES, &bnames, fi->scareFlags);
858 headerGet(h, RPMTAG_DIRINDEXES, &dindexes, fi->scareFlags);
859 headerGet(h, RPMTAG_DIRNAMES, &dnames, fi->scareFlags);
860 headerGet(h, RPMTAG_FILECOLORS, &fcolors, fi->scareFlags);
861 headerGet(h, RPMTAG_FILEMODES, &fmodes, fi->scareFlags);
862 /* TODO XXX ugh.. use rpmtd iterators & friends instead */
863 baseNames = bnames.data;
864 dirIndexes = dindexes.data;
865 fColors = fcolors.data;
866 fModes = fmodes.data;
867 fileCount = rpmtdCount(&bnames);
868 dirCount = rpmtdCount(&dnames);
869 /* XXX TODO: use rpmtdDup() instead */
870 dirNames = dnames.data = duparray(dnames.data, dirCount);
871 dnames.flags |= RPMTD_PTR_ALLOCED;
873 dColors = xcalloc(dirCount, sizeof(*dColors));
875 newDirIndexes = xmalloc(sizeof(*newDirIndexes) * fileCount);
876 memcpy(newDirIndexes, dirIndexes, sizeof(*newDirIndexes) * fileCount);
877 dirIndexes = newDirIndexes;
880 * For all relocations, we go through sorted file/relocation lists
881 * backwards so that /usr/local relocations take precedence over /usr
885 /* Relocate individual paths. */
887 for (i = fileCount - 1; i >= 0; i--) {
892 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
893 if (len >= fileAlloced) {
894 fileAlloced = len * 2;
895 fn = xrealloc(fn, fileAlloced);
898 assert(fn != NULL); /* XXX can't happen */
900 fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
902 if (fColors != NULL) {
903 /* XXX pkgs may not have unique dirNames, so color all dirNames that match. */
904 for (j = 0; j < dirCount; j++) {
905 if (strcmp(dirNames[dirIndexes[i]], dirNames[j])) continue;
906 dColors[j] |= fColors[i];
911 * See if this file path needs relocating.
914 * XXX FIXME: Would a bsearch of the (already sorted)
915 * relocation list be a good idea?
917 for (j = numRelocations - 1; j >= 0; j--) {
918 if (relocations[j].oldPath == NULL) /* XXX can't happen */
920 len = strcmp(relocations[j].oldPath, "/")
921 ? strlen(relocations[j].oldPath)
927 * Only subdirectories or complete file paths may be relocated. We
928 * don't check for '\0' as our directory names all end in '/'.
930 if (!(fn[len] == '/' || fnlen == len))
933 if (strncmp(relocations[j].oldPath, fn, len))
939 /* FIX: fModes may be NULL */
940 ft = rpmfiWhatis(fModes[i]);
942 /* On install, a relocate to NULL means skip the path. */
943 if (relocations[j].newPath == NULL) {
945 /* Start with the parent, looking for directory to exclude. */
946 for (j = dirIndexes[i]; j < dirCount; j++) {
947 len = strlen(dirNames[j]) - 1;
948 while (len > 0 && dirNames[j][len-1] == '/') len--;
951 if (strncmp(fn, dirNames[j], fnlen))
957 actions[i] = FA_SKIPNSTATE;
958 rpmlog(RPMLOG_DEBUG, "excluding %s %s\n",
964 /* Relocation on full paths only, please. */
965 if (fnlen != len) continue;
968 rpmlog(RPMLOG_DEBUG, "relocating %s to %s\n",
969 fn, relocations[j].newPath);
972 strcpy(fn, relocations[j].newPath);
973 { char * te = strrchr(fn, '/');
975 if (te > fn) te++; /* root is special */
978 te = fn + strlen(fn);
979 if (strcmp(baseNames[i], te)) { /* basename changed too? */
980 if (!haveRelocatedBase) {
981 /* XXX TODO: use rpmtdDup() instead */
982 bnames.data = baseNames = duparray(baseNames, fileCount);
983 bnames.flags |= RPMTD_PTR_ALLOCED;
984 haveRelocatedBase = 1;
987 baseNames[i] = xstrdup(te);
989 *te = '\0'; /* terminate new directory name */
992 /* Does this directory already exist in the directory list? */
993 for (j = 0; j < dirCount; j++) {
994 if (fnlen != strlen(dirNames[j]))
996 if (strncmp(fn, dirNames[j], fnlen))
1006 /* Creating new paths is a pita */
1007 dirNames = dnames.data = xrealloc(dnames.data,
1008 sizeof(*dirNames) * (dirCount + 1));
1010 free(dirNames[dirCount]);
1011 dirNames[dirCount] = xstrdup(fn);
1012 dirIndexes[i] = dirCount;
1017 /* Finish off by relocating directories. */
1018 for (i = dirCount - 1; i >= 0; i--) {
1019 for (j = numRelocations - 1; j >= 0; j--) {
1021 if (relocations[j].oldPath == NULL) /* XXX can't happen */
1023 len = strcmp(relocations[j].oldPath, "/")
1024 ? strlen(relocations[j].oldPath)
1027 if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
1031 * Only subdirectories or complete file paths may be relocated. We
1032 * don't check for '\0' as our directory names all end in '/'.
1034 if (dirNames[i][len] != '/')
1037 if (relocations[j].newPath) { /* Relocate the path */
1039 rstrscat(&t, relocations[j].newPath, (dirNames[i] + len), NULL);
1040 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
1041 (void) rpmCleanPath(t);
1045 rpmlog(RPMLOG_DEBUG,
1046 "relocating directory %s to %s\n", dirNames[i], t);
1054 /* Save original filenames in header and replace (relocated) filenames. */
1058 headerGet(h, RPMTAG_BASENAMES, &td, HEADERGET_MINMEM);
1059 rpmtdSetTag(&td, RPMTAG_ORIGBASENAMES);
1060 headerPut(h, &td, HEADERPUT_DEFAULT);
1063 headerGet(h, RPMTAG_DIRNAMES, &td, HEADERGET_MINMEM);
1064 rpmtdSetTag(&td, RPMTAG_ORIGDIRNAMES);
1065 headerPut(h, &td, HEADERPUT_DEFAULT);
1068 headerGet(h, RPMTAG_DIRINDEXES, &td, HEADERGET_MINMEM);
1069 rpmtdSetTag(&td, RPMTAG_ORIGDIRINDEXES);
1070 headerPut(h, &td, HEADERPUT_DEFAULT);
1073 if (rpmtdFromStringArray(&td, RPMTAG_BASENAMES, (const char**) baseNames, fileCount)) {
1077 headerGet(h, RPMTAG_BASENAMES, &td, fi->scareFlags);
1078 fi->fc = rpmtdCount(&td);
1081 if (rpmtdFromStringArray(&td, RPMTAG_DIRNAMES, (const char**) dirNames, dirCount)) {
1085 headerGet(h, RPMTAG_DIRNAMES, &td, fi->scareFlags);
1086 fi->dc = rpmtdCount(&td);
1089 if (rpmtdFromUint32(&td, RPMTAG_DIRINDEXES, dirIndexes, fileCount)) {
1092 headerGet(h, RPMTAG_DIRINDEXES, &td, fi->scareFlags);
1093 /* Ugh, nasty games with how dil is alloced depending on scareMem */
1094 if (fi->scareFlags & HEADERGET_ALLOC)
1099 rpmtdFreeData(&bnames);
1100 rpmtdFreeData(&dnames);
1101 rpmtdFreeData(&dindexes);
1102 rpmtdFreeData(&fcolors);
1103 rpmtdFreeData(&fmodes);
1105 for (i = 0; i < numRelocations; i++) {
1106 free(relocations[i].oldPath);
1107 free(relocations[i].newPath);
1110 free(newDirIndexes);
1116 rpmfi rpmfiFree(rpmfi fi)
1118 if (fi == NULL) return NULL;
1121 return rpmfiUnlink(fi, fi->Type);
1123 if (_rpmfi_debug < 0)
1124 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
1127 fi->bnl = _free(fi->bnl);
1128 fi->dnl = _free(fi->dnl);
1130 fi->flinks = _free(fi->flinks);
1131 fi->flangs = _free(fi->flangs);
1132 fi->digests = _free(fi->digests);
1133 fi->fcaps = _free(fi->fcaps);
1135 fi->cdict = _free(fi->cdict);
1137 fi->fuser = _free(fi->fuser);
1138 fi->fgroup = _free(fi->fgroup);
1140 fi->fstates = _free(fi->fstates);
1142 if (!fi->keep_header && fi->h == NULL) {
1143 fi->fmtimes = _constfree(fi->fmtimes);
1144 fi->fmodes = _free(fi->fmodes);
1145 fi->fflags = _constfree(fi->fflags);
1146 fi->vflags = _constfree(fi->vflags);
1147 fi->fsizes = _constfree(fi->fsizes);
1148 fi->frdevs = _constfree(fi->frdevs);
1149 fi->finodes = _constfree(fi->finodes);
1150 fi->dil = _free(fi->dil);
1152 fi->fcolors = _constfree(fi->fcolors);
1153 fi->fcdictx = _constfree(fi->fcdictx);
1154 fi->ddict = _constfree(fi->ddict);
1155 fi->fddictx = _constfree(fi->fddictx);
1156 fi->fddictn = _constfree(fi->fddictn);
1161 fi->fsm = freeFSM(fi->fsm);
1163 fi->fn = _free(fi->fn);
1164 fi->apath = _free(fi->apath);
1165 fi->fmapflags = _free(fi->fmapflags);
1167 fi->obnl = _free(fi->obnl);
1168 fi->odnl = _free(fi->odnl);
1170 fi->actions = _free(fi->actions);
1171 fi->replacedSizes = _free(fi->replacedSizes);
1172 fi->replaced = _free(fi->replaced);
1174 fi->h = headerFree(fi->h);
1176 (void) rpmfiUnlink(fi, fi->Type);
1177 memset(fi, 0, sizeof(*fi)); /* XXX trash and burn */
1183 #define _hgfi(_h, _tag, _td, _flags, _data) \
1184 if (headerGet((_h), (_tag), (_td), (_flags))) \
1187 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, rpmfiFlags flags)
1192 rpm_loff_t *asize = NULL;
1195 struct rpmtd_s fdigests, digalgo;
1197 headerGetFlags scareFlags = (flags & RPMFI_KEEPHEADER) ?
1198 HEADERGET_MINMEM : HEADERGET_ALLOC;
1199 headerGetFlags defFlags = HEADERGET_ALLOC;
1203 if (tagN == RPMTAG_BASENAMES) {
1210 fi = xcalloc(1, sizeof(*fi));
1211 if (fi == NULL) /* XXX can't happen */
1214 fi->magic = RPMFIMAGIC;
1219 fi->scareFlags = scareFlags;
1221 fi->keep_header = (flags & RPMFI_KEEPHEADER);
1222 fi->h = fi->keep_header ? headerLink(h) : NULL;
1224 if (fi->fsm == NULL)
1227 if (headerGet(h, RPMTAG_LONGARCHIVESIZE, &td, HEADERGET_EXT)) {
1228 asize = rpmtdGetUint64(&td);
1230 /* 0 means unknown */
1232 fi->archiveSize = asize ? *asize : 0;
1235 /* See if we have pre/posttrans scripts. */
1236 fi->transscripts |= (headerIsEntry(h, RPMTAG_PRETRANS) &&
1237 headerIsEntry(h, RPMTAG_PRETRANSPROG)) ?
1238 RPMFI_HAVE_PRETRANS : 0;
1239 fi->transscripts |= (headerIsEntry(h, RPMTAG_POSTTRANS) &&
1240 headerIsEntry(h, RPMTAG_POSTTRANSPROG)) ?
1241 RPMFI_HAVE_POSTTRANS : 0;
1243 _hgfi(h, RPMTAG_BASENAMES, &td, defFlags, fi->bnl);
1244 fi->fc = rpmtdCount(&td);
1249 _hgfi(h, RPMTAG_DIRNAMES, &td, defFlags, fi->dnl);
1250 fi->dc = rpmtdCount(&td);
1251 _hgfi(h, RPMTAG_DIRINDEXES, &td, scareFlags, fi->dil);
1252 _hgfi(h, RPMTAG_FILEMODES, &td, scareFlags, fi->fmodes);
1253 _hgfi(h, RPMTAG_FILEFLAGS, &td, scareFlags, fi->fflags);
1254 _hgfi(h, RPMTAG_FILEVERIFYFLAGS, &td, scareFlags, fi->vflags);
1255 _hgfi(h, RPMTAG_FILESIZES, &td, scareFlags, fi->fsizes);
1257 _hgfi(h, RPMTAG_FILECOLORS, &td, scareFlags, fi->fcolors);
1259 if (!(flags & RPMFI_NOFILECLASS)) {
1260 _hgfi(h, RPMTAG_CLASSDICT, &td, scareFlags, fi->cdict);
1261 fi->ncdict = rpmtdCount(&td);
1262 _hgfi(h, RPMTAG_FILECLASS, &td, scareFlags, fi->fcdictx);
1264 if (!(flags & RPMFI_NOFILEDEPS)) {
1265 _hgfi(h, RPMTAG_DEPENDSDICT, &td, scareFlags, fi->ddict);
1266 fi->nddict = rpmtdCount(&td);
1267 _hgfi(h, RPMTAG_FILEDEPENDSX, &td, scareFlags, fi->fddictx);
1268 _hgfi(h, RPMTAG_FILEDEPENDSN, &td, scareFlags, fi->fddictn);
1271 _hgfi(h, RPMTAG_FILESTATES, &td, defFlags, fi->fstates);
1272 if (fi->fstates == NULL)
1273 fi->fstates = xcalloc(fi->fc, sizeof(*fi->fstates));
1275 _hgfi(h, RPMTAG_FILECAPS, &td, defFlags, fi->fcaps);
1277 fi->action = FA_UNKNOWN;
1280 if (fi->actions == NULL)
1281 fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1283 /* XXX TR_REMOVED needs CPIO_MAP_{ABSOLUTE,ADDDOT} CPIO_ALL_HARDLINKS */
1285 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
1287 _hgfi(h, RPMTAG_FILELINKTOS, &td, defFlags, fi->flinks);
1288 _hgfi(h, RPMTAG_FILELANGS, &td, defFlags, fi->flangs);
1290 /* See if the package has non-md5 file digests */
1291 fi->digestalgo = PGPHASHALGO_MD5;
1292 if (headerGet(h, RPMTAG_FILEDIGESTALGO, &digalgo, HEADERGET_MINMEM)) {
1293 pgpHashAlgo *algo = rpmtdGetUint32(&digalgo);
1294 /* Hmm, what to do with unknown digest algorithms? */
1295 if (algo && rpmDigestLength(*algo) != 0) {
1296 fi->digestalgo = *algo;
1301 /* grab hex digests from header and store in binary format */
1302 if (headerGet(h, RPMTAG_FILEDIGESTS, &fdigests, HEADERGET_MINMEM)) {
1303 const char *fdigest;
1304 size_t diglen = rpmDigestLength(fi->digestalgo);
1305 fi->digests = t = xmalloc(rpmtdCount(&fdigests) * diglen);
1307 while ((fdigest = rpmtdNextString(&fdigests))) {
1308 if (!(fdigest && *fdigest != '\0')) {
1309 memset(t, 0, diglen);
1313 for (int j = 0; j < diglen; j++, t++, fdigest += 2)
1314 *t = (rnibble(fdigest[0]) << 4) | rnibble(fdigest[1]);
1316 rpmtdFreeData(&fdigests);
1319 /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes */
1320 _hgfi(h, RPMTAG_FILEMTIMES, &td, scareFlags, fi->fmtimes);
1321 _hgfi(h, RPMTAG_FILERDEVS, &td, scareFlags, fi->frdevs);
1322 _hgfi(h, RPMTAG_FILEINODES, &td, scareFlags, fi->finodes);
1324 fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1326 _hgfi(h, RPMTAG_FILEUSERNAME, &td, defFlags, fi->fuser);
1327 _hgfi(h, RPMTAG_FILEGROUPNAME, &td, defFlags, fi->fgroup);
1331 if ((p = rpmtsRelocateElement(ts)) != NULL && rpmteType(p) == TR_ADDED
1332 && !headerIsSource(h)
1333 && !headerIsEntry(h, RPMTAG_ORIGBASENAMES))
1338 if (fi->actions == NULL)
1339 fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1340 /* FIX: fi-digests undefined */
1341 foo = relocateFileList(ts, fi, h, fi->actions);
1342 fi->h = headerFree(fi->h);
1343 fi->h = headerLink(foo);
1344 foo = headerFree(foo);
1347 if (!fi->keep_header) {
1348 fi->h = headerFree(fi->h);
1352 for (i = 0; i < fi->dc; i++) {
1353 if ((len = strlen(fi->dnl[i])) > dnlmax)
1357 for (i = 0; i < fi->fc; i++) {
1358 if ((len = strlen(fi->bnl[i])) > bnlmax)
1361 fi->fnlen = dnlmax + bnlmax + 1;
1368 if (_rpmfi_debug < 0)
1369 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
1371 /* FIX: rpmfi null annotations */
1372 return rpmfiLink(fi, (fi ? fi->Type : NULL));