3 * Routines to handle file info tag sets.
11 #include "lib/cpio.h" /* XXX CPIO_FOO */
12 #include "lib/fsm.h" /* XXX newFSM() */
16 #define _RPMFI_INTERNAL
19 #define _RPMTE_INTERNAL /* relocations */
23 #include <rpmfileutil.h>/* XXX domd5 */
24 #include <rpmstring.h>
25 #include <rpmmacro.h> /* XXX rpmCleanPath */
32 rpmfi XrpmfiUnlink(rpmfi fi, const char * msg, const char * fn, unsigned ln)
34 if (fi == NULL) return NULL;
35 if (_rpmfi_debug && msg != NULL)
36 fprintf(stderr, "--> fi %p -- %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
41 rpmfi XrpmfiLink(rpmfi fi, const char * msg, const char * fn, unsigned ln)
43 if (fi == NULL) return NULL;
45 if (_rpmfi_debug && msg != NULL)
46 fprintf(stderr, "--> fi %p ++ %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
52 return (fi != NULL ? fi->fc : 0);
57 return (fi != NULL ? fi->dc : 0);
68 return (fi != NULL ? fi->i : -1);
71 int rpmfiSetFX(rpmfi fi, int fx)
75 if (fi != NULL && fx >= 0 && fx < fi->fc) {
78 fi->j = fi->dil[fi->i];
85 return (fi != NULL ? fi->j : -1);
88 int rpmfiSetDX(rpmfi fi, int dx)
92 if (fi != NULL && dx >= 0 && dx < fi->dc) {
99 const char * rpmfiBN(rpmfi fi)
101 const char * BN = NULL;
103 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
110 const char * rpmfiDN(rpmfi fi)
112 const char * DN = NULL;
114 if (fi != NULL && fi->j >= 0 && fi->j < fi->dc) {
121 const char * rpmfiFN(rpmfi fi)
123 const char * FN = "";
125 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
128 fi->fn = xmalloc(fi->fnlen);
131 t = stpcpy(t, fi->dnl[fi->dil[fi->i]]);
132 t = stpcpy(t, fi->bnl[fi->i]);
137 uint32_t rpmfiFFlags(rpmfi fi)
141 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
142 if (fi->fflags != NULL)
143 FFlags = fi->fflags[fi->i];
148 uint32_t rpmfiVFlags(rpmfi fi)
152 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
153 if (fi->vflags != NULL)
154 VFlags = fi->vflags[fi->i];
159 int16_t rpmfiFMode(rpmfi fi)
163 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
164 if (fi->fmodes != NULL)
165 fmode = fi->fmodes[fi->i];
170 rpmfileState rpmfiFState(rpmfi fi)
172 rpmfileState fstate = RPMFILE_STATE_MISSING;
174 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
175 if (fi->fstates != NULL)
176 fstate = fi->fstates[fi->i];
181 const unsigned char * rpmfiMD5(rpmfi fi)
183 unsigned char * MD5 = NULL;
185 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
186 if (fi->md5s != NULL)
187 MD5 = fi->md5s + (16 * fi->i);
192 const char * rpmfiFLink(rpmfi fi)
194 const char * flink = NULL;
196 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
197 if (fi->flinks != NULL)
198 flink = fi->flinks[fi->i];
203 int32_t rpmfiFSize(rpmfi fi)
207 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
208 if (fi->fsizes != NULL)
209 fsize = fi->fsizes[fi->i];
214 int16_t rpmfiFRdev(rpmfi fi)
218 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
219 if (fi->frdevs != NULL)
220 frdev = fi->frdevs[fi->i];
225 int32_t rpmfiFInode(rpmfi fi)
229 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
230 if (fi->finodes != NULL)
231 finode = fi->finodes[fi->i];
236 uint32_t rpmfiColor(rpmfi fi)
241 /* XXX ignore all but lsnibble for now. */
242 color = fi->color & 0xf;
246 uint32_t rpmfiFColor(rpmfi fi)
250 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
251 if (fi->fcolors != NULL)
252 /* XXX ignore all but lsnibble for now. */
253 fcolor = (fi->fcolors[fi->i] & 0x0f);
258 const char * rpmfiFClass(rpmfi fi)
260 const char * fclass = NULL;
263 if (fi != NULL && fi->fcdictx != NULL && fi->i >= 0 && fi->i < fi->fc) {
264 cdictx = fi->fcdictx[fi->i];
265 if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
266 fclass = fi->cdict[cdictx];
271 const char * rpmfiFContext(rpmfi fi)
273 const char * fcontext = NULL;
275 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
276 if (fi->fcontexts != NULL)
277 fcontext = fi->fcontexts[fi->i];
282 int32_t rpmfiFDepends(rpmfi fi, const uint32_t ** fddictp)
286 const uint32_t * fddict = NULL;
288 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
289 if (fi->fddictn != NULL)
290 fddictn = fi->fddictn[fi->i];
291 if (fddictn > 0 && fi->fddictx != NULL)
292 fddictx = fi->fddictx[fi->i];
293 if (fi->ddict != NULL && fddictx >= 0 && (fddictx+fddictn) <= fi->nddict)
294 fddict = fi->ddict + fddictx;
301 int32_t rpmfiFNlink(rpmfi fi)
305 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
306 /* XXX rpm-2.3.12 has not RPMTAG_FILEINODES */
307 if (fi->finodes && fi->frdevs) {
308 int32_t finode = fi->finodes[fi->i];
309 int16_t frdev = fi->frdevs[fi->i];
312 for (j = 0; j < fi->fc; j++) {
313 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
321 int32_t rpmfiFMtime(rpmfi fi)
325 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
326 if (fi->fmtimes != NULL)
327 fmtime = fi->fmtimes[fi->i];
332 const char * rpmfiFUser(rpmfi fi)
334 const char * fuser = NULL;
336 /* XXX add support for ancient RPMTAG_FILEUIDS? */
337 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
338 if (fi->fuser != NULL)
339 fuser = fi->fuser[fi->i];
344 const char * rpmfiFGroup(rpmfi fi)
346 const char * fgroup = NULL;
348 /* XXX add support for ancient RPMTAG_FILEGIDS? */
349 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
350 if (fi->fgroup != NULL)
351 fgroup = fi->fgroup[fi->i];
356 int rpmfiNext(rpmfi fi)
360 if (fi != NULL && ++fi->i >= 0) {
361 if (fi->i < fi->fc) {
364 fi->j = fi->dil[fi->i];
368 if (_rpmfi_debug < 0 && i != -1)
369 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] : ""));
376 rpmfi rpmfiInit(rpmfi fi, int fx)
379 if (fx >= 0 && fx < fi->fc) {
388 int rpmfiNextD(rpmfi fi)
392 if (fi != NULL && ++fi->j >= 0) {
398 if (_rpmfi_debug < 0 && j != -1)
399 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, (fi->Type ? fi->Type : "?Type?"), j);
406 rpmfi rpmfiInitD(rpmfi fi, int dx)
409 if (dx >= 0 && dx < fi->fc)
419 * Identify a file type.
420 * @param ft file type
421 * @return string to identify a file type
424 const char * ftstring (rpmFileTypes ft)
427 case XDIR: return "directory";
428 case CDEV: return "char dev";
429 case BDEV: return "block dev";
430 case LINK: return "link";
431 case SOCK: return "sock";
432 case PIPE: return "fifo/pipe";
433 case REG: return "file";
434 default: return "unknown file type";
438 rpmFileTypes rpmfiWhatis(uint16_t mode)
440 if (S_ISDIR(mode)) return XDIR;
441 if (S_ISCHR(mode)) return CDEV;
442 if (S_ISBLK(mode)) return BDEV;
443 if (S_ISLNK(mode)) return LINK;
444 if (S_ISSOCK(mode)) return SOCK;
445 if (S_ISFIFO(mode)) return PIPE;
449 int rpmfiCompare(const rpmfi afi, const rpmfi bfi)
451 rpmFileTypes awhat = rpmfiWhatis(rpmfiFMode(afi));
452 rpmFileTypes bwhat = rpmfiWhatis(rpmfiFMode(bfi));
454 if (awhat != bwhat) return 1;
457 const char * alink = rpmfiFLink(afi);
458 const char * blink = rpmfiFLink(bfi);
459 if (alink == blink) return 0;
460 if (alink == NULL) return 1;
461 if (blink == NULL) return -1;
462 return strcmp(alink, blink);
463 } else if (awhat == REG) {
464 const unsigned char * amd5 = rpmfiMD5(afi);
465 const unsigned char * bmd5 = rpmfiMD5(bfi);
466 if (amd5 == bmd5) return 0;
467 if (amd5 == NULL) return 1;
468 if (bmd5 == NULL) return -1;
469 return memcmp(amd5, bmd5, 16);
475 rpmFileAction rpmfiDecideFate(const rpmfi ofi, rpmfi nfi, int skipMissing)
477 const char * fn = rpmfiFN(nfi);
478 int newFlags = rpmfiFFlags(nfi);
480 rpmFileTypes dbWhat, newWhat, diskWhat;
482 int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
484 if (lstat(fn, &sb)) {
486 * The file doesn't exist on the disk. Create it unless the new
487 * package has marked it as missingok, or allfiles is requested.
489 if (skipMissing && (newFlags & RPMFILE_MISSINGOK)) {
490 rpmlog(RPMLOG_DEBUG, _("%s skipped due to missingok flag\n"),
498 diskWhat = rpmfiWhatis((int16_t)sb.st_mode);
499 dbWhat = rpmfiWhatis(rpmfiFMode(ofi));
500 newWhat = rpmfiWhatis(rpmfiFMode(nfi));
503 * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
504 * them in older packages as well.
509 if (diskWhat != newWhat && dbWhat != REG && dbWhat != LINK)
511 else if (newWhat != dbWhat && diskWhat != dbWhat)
513 else if (dbWhat != newWhat)
515 else if (dbWhat != LINK && dbWhat != REG)
519 * This order matters - we'd prefer to CREATE the file if at all
520 * possible in case something else (like the timestamp) has changed.
522 memset(buffer, 0, sizeof(buffer));
524 const unsigned char * omd5, * nmd5;
525 omd5 = rpmfiMD5(ofi);
526 if (diskWhat == REG) {
527 if (rpmDoDigest(PGPHASHALGO_MD5, fn, 0,
528 (unsigned char *)buffer, NULL))
529 return FA_CREATE; /* assume file has been removed */
530 if (omd5 && !memcmp(omd5, buffer, 16))
531 return FA_CREATE; /* unmodified config file, replace. */
533 nmd5 = rpmfiMD5(nfi);
534 if (omd5 && nmd5 && !memcmp(omd5, nmd5, 16))
535 return FA_SKIP; /* identical file, don't bother. */
536 } else /* dbWhat == LINK */ {
537 const char * oFLink, * nFLink;
538 oFLink = rpmfiFLink(ofi);
539 if (diskWhat == LINK) {
540 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
541 return FA_CREATE; /* assume file has been removed */
542 if (oFLink && !strcmp(oFLink, buffer))
543 return FA_CREATE; /* unmodified config file, replace. */
545 nFLink = rpmfiFLink(nfi);
546 if (oFLink && nFLink && !strcmp(oFLink, nFLink))
547 return FA_SKIP; /* identical file, don't bother. */
551 * The config file on the disk has been modified, but
552 * the ones in the two packages are different. It would
553 * be nice if RPM was smart enough to at least try and
554 * merge the difference ala CVS, but...
559 int rpmfiConfigConflict(const rpmfi fi)
561 const char * fn = rpmfiFN(fi);
562 int flags = rpmfiFFlags(fi);
564 rpmFileTypes newWhat, diskWhat;
567 if (!(flags & RPMFILE_CONFIG) || lstat(fn, &sb)) {
571 diskWhat = rpmfiWhatis((int16_t)sb.st_mode);
572 newWhat = rpmfiWhatis(rpmfiFMode(fi));
574 if (newWhat != LINK && newWhat != REG)
577 if (diskWhat != newWhat)
580 memset(buffer, 0, sizeof(buffer));
581 if (newWhat == REG) {
582 const unsigned char * nmd5;
583 if (rpmDoDigest(PGPHASHALGO_MD5, fn, 0, (unsigned char *)buffer, NULL))
584 return 0; /* assume file has been removed */
586 if (nmd5 && !memcmp(nmd5, buffer, 16))
587 return 0; /* unmodified config file */
588 } else /* newWhat == LINK */ {
590 if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
591 return 0; /* assume file has been removed */
592 nFLink = rpmfiFLink(fi);
593 if (nFLink && !strcmp(nFLink, buffer))
594 return 0; /* unmodified config file */
600 const char * rpmfiTypeString(rpmfi fi)
602 switch(rpmteType(fi->te)) {
603 case TR_ADDED: return " install";
604 case TR_REMOVED: return " erase";
605 default: return "???";
609 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
612 * Relocate files in header.
613 * @todo multilib file dispositions need to be checked.
614 * @param ts transaction set
615 * @param fi transaction element file info
616 * @param origH package header
617 * @param actions file dispositions
618 * @return header with relocated files
621 Header relocateFileList(const rpmts ts, rpmfi fi,
622 Header origH, rpmFileAction * actions)
624 rpmte p = rpmtsRelocateElement(ts);
628 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
629 static int _printed = 0;
630 int allowBadRelocate = (rpmtsFilterFlags(ts) & RPMPROB_FILTER_FORCERELOCATE);
631 rpmRelocation * relocations = NULL;
633 const char ** validRelocations;
634 rpmTagType validType;
636 const char ** baseNames;
637 const char ** dirNames;
638 uint32_t * dirIndexes;
639 uint32_t * newDirIndexes;
642 uint32_t mydColor = rpmExpandNumeric("%{?_autorelocate_dcolor}");
643 uint32_t * fFlags = NULL;
644 uint32_t * fColors = NULL;
645 uint32_t * dColors = NULL;
646 uint16_t * fModes = NULL;
651 int haveRelocatedFile = 0;
656 if (!hge(origH, RPMTAG_PREFIXES, &validType,
657 (void **) &validRelocations, &numValid))
663 while (p->relocs[numRelocations].newPath ||
664 p->relocs[numRelocations].oldPath)
668 * If no relocations are specified (usually the case), then return the
669 * original header. If there are prefixes, however, then INSTPREFIXES
670 * should be added, but, since relocateFileList() can be called more
671 * than once for the same header, don't bother if already present.
673 if (p->relocs == NULL || numRelocations == 0) {
675 if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES))
676 xx = hae(origH, RPMTAG_INSTPREFIXES,
677 validType, validRelocations, numValid);
678 validRelocations = hfd(validRelocations, validType);
680 /* XXX FIXME multilib file actions need to be checked. */
681 return headerLink(origH);
684 h = headerLink(origH);
686 relocations = alloca(sizeof(*relocations) * numRelocations);
688 /* Build sorted relocation list from raw relocations. */
689 for (i = 0; i < numRelocations; i++) {
693 * Default relocations (oldPath == NULL) are handled in the UI,
696 if (p->relocs[i].oldPath == NULL) continue; /* XXX can't happen */
698 /* FIXME: Trailing /'s will confuse us greatly. Internal ones will
699 too, but those are more trouble to fix up. :-( */
700 t = alloca_strdup(p->relocs[i].oldPath);
701 relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
703 : stripTrailingChar(t, '/');
705 /* An old path w/o a new path is valid, and indicates exclusion */
706 if (p->relocs[i].newPath) {
709 t = alloca_strdup(p->relocs[i].newPath);
710 relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
712 : stripTrailingChar(t, '/');
714 /* FIX: relocations[i].oldPath == NULL */
715 /* Verify that the relocation's old path is in the header. */
716 for (j = 0; j < numValid; j++) {
717 if (!strcmp(validRelocations[j], relocations[i].oldPath))
721 /* XXX actions check prevents problem from being appended twice. */
722 if (j == numValid && !allowBadRelocate && actions) {
723 rpmps ps = rpmtsProblems(ts);
724 rpmpsAppend(ps, RPMPROB_BADRELOCATE,
725 rpmteNEVRA(p), rpmteKey(p),
726 relocations[i].oldPath, NULL, NULL, 0);
730 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
735 relocations[i].newPath = NULL;
739 /* stupid bubble sort, but it's probably faster here */
740 for (i = 0; i < numRelocations; i++) {
743 for (j = 1; j < numRelocations; j++) {
744 rpmRelocation tmpReloc;
745 if (relocations[j - 1].oldPath == NULL || /* XXX can't happen */
746 relocations[j ].oldPath == NULL || /* XXX can't happen */
747 strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
750 tmpReloc = relocations[j - 1];
751 relocations[j - 1] = relocations[j];
752 relocations[j] = tmpReloc;
755 if (!madeSwap) break;
760 rpmlog(RPMLOG_DEBUG, _("========== relocations\n"));
761 for (i = 0; i < numRelocations; i++) {
762 if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
763 if (relocations[i].newPath == NULL)
764 rpmlog(RPMLOG_DEBUG, _("%5d exclude %s\n"),
765 i, relocations[i].oldPath);
767 rpmlog(RPMLOG_DEBUG, _("%5d relocate %s -> %s\n"),
768 i, relocations[i].oldPath, relocations[i].newPath);
772 /* Add relocation values to the header */
774 const char ** actualRelocations;
777 actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
779 for (i = 0; i < numValid; i++) {
780 for (j = 0; j < numRelocations; j++) {
781 if (relocations[j].oldPath == NULL || /* XXX can't happen */
782 strcmp(validRelocations[i], relocations[j].oldPath))
784 /* On install, a relocate to NULL means skip the path. */
785 if (relocations[j].newPath) {
786 actualRelocations[numActual] = relocations[j].newPath;
791 if (j == numRelocations) {
792 actualRelocations[numActual] = validRelocations[i];
798 xx = hae(h, RPMTAG_INSTPREFIXES, RPM_STRING_ARRAY_TYPE,
799 (void **) actualRelocations, numActual);
801 actualRelocations = _free(actualRelocations);
802 validRelocations = hfd(validRelocations, validType);
805 xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &baseNames, &fileCount);
806 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
807 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &dirNames, &dirCount);
808 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fFlags, NULL);
809 xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fColors, NULL);
810 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fModes, NULL);
812 dColors = alloca(dirCount * sizeof(*dColors));
813 memset(dColors, 0, dirCount * sizeof(*dColors));
815 newDirIndexes = alloca(sizeof(*newDirIndexes) * fileCount);
816 memcpy(newDirIndexes, dirIndexes, sizeof(*newDirIndexes) * fileCount);
817 dirIndexes = newDirIndexes;
820 * For all relocations, we go through sorted file/relocation lists
821 * backwards so that /usr/local relocations take precedence over /usr
825 /* Relocate individual paths. */
827 for (i = fileCount - 1; i >= 0; i--) {
832 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
833 if (len >= fileAlloced) {
834 fileAlloced = len * 2;
835 fn = xrealloc(fn, fileAlloced);
838 assert(fn != NULL); /* XXX can't happen */
840 fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
842 if (fColors != NULL) {
843 /* XXX pkgs may not have unique dirNames, so color all dirNames that match. */
844 for (j = 0; j < dirCount; j++) {
845 if (strcmp(dirNames[dirIndexes[i]], dirNames[j])) continue;
846 dColors[j] |= fColors[i];
851 * See if this file path needs relocating.
854 * XXX FIXME: Would a bsearch of the (already sorted)
855 * relocation list be a good idea?
857 for (j = numRelocations - 1; j >= 0; j--) {
858 if (relocations[j].oldPath == NULL) /* XXX can't happen */
860 len = strcmp(relocations[j].oldPath, "/")
861 ? strlen(relocations[j].oldPath)
867 * Only subdirectories or complete file paths may be relocated. We
868 * don't check for '\0' as our directory names all end in '/'.
870 if (!(fn[len] == '/' || fnlen == len))
873 if (strncmp(relocations[j].oldPath, fn, len))
879 /* FIX: fModes may be NULL */
880 ft = rpmfiWhatis(fModes[i]);
882 /* On install, a relocate to NULL means skip the path. */
883 if (relocations[j].newPath == NULL) {
885 /* Start with the parent, looking for directory to exclude. */
886 for (j = dirIndexes[i]; j < dirCount; j++) {
887 len = strlen(dirNames[j]) - 1;
888 while (len > 0 && dirNames[j][len-1] == '/') len--;
891 if (strncmp(fn, dirNames[j], fnlen))
897 actions[i] = FA_SKIPNSTATE;
898 rpmlog(RPMLOG_DEBUG, _("excluding %s %s\n"),
904 /* Relocation on full paths only, please. */
905 if (fnlen != len) continue;
908 rpmlog(RPMLOG_DEBUG, _("relocating %s to %s\n"),
909 fn, relocations[j].newPath);
912 strcpy(fn, relocations[j].newPath);
913 { char * te = strrchr(fn, '/');
915 if (te > fn) te++; /* root is special */
918 te = fn + strlen(fn);
919 /* LCL: te != NULL here. */
920 if (strcmp(baseNames[i], te)) /* basename changed too? */
921 baseNames[i] = alloca_strdup(te);
922 *te = '\0'; /* terminate new directory name */
925 /* Does this directory already exist in the directory list? */
926 for (j = 0; j < dirCount; j++) {
927 if (fnlen != strlen(dirNames[j]))
929 if (strncmp(fn, dirNames[j], fnlen))
939 /* Creating new paths is a pita */
940 if (!haveRelocatedFile) {
941 const char ** newDirList;
943 haveRelocatedFile = 1;
944 newDirList = xmalloc((dirCount + 1) * sizeof(*newDirList));
945 for (j = 0; j < dirCount; j++)
946 newDirList[j] = alloca_strdup(dirNames[j]);
947 dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
948 dirNames = newDirList;
950 dirNames = xrealloc(dirNames,
951 sizeof(*dirNames) * (dirCount + 1));
954 dirNames[dirCount] = alloca_strdup(fn);
955 dirIndexes[i] = dirCount;
959 /* Finish off by relocating directories. */
960 for (i = dirCount - 1; i >= 0; i--) {
961 for (j = numRelocations - 1; j >= 0; j--) {
963 /* XXX Don't autorelocate uncolored directories. */
964 if (j == p->autorelocatex
965 && (dColors[i] == 0 || !(dColors[i] & mydColor)))
968 if (relocations[j].oldPath == NULL) /* XXX can't happen */
970 len = strcmp(relocations[j].oldPath, "/")
971 ? strlen(relocations[j].oldPath)
974 if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
978 * Only subdirectories or complete file paths may be relocated. We
979 * don't check for '\0' as our directory names all end in '/'.
981 if (dirNames[i][len] != '/')
984 if (relocations[j].newPath) { /* Relocate the path */
985 const char * s = relocations[j].newPath;
986 char * t = alloca(strlen(s) + strlen(dirNames[i]) - len + 1);
989 (void) stpcpy( stpcpy(t, s) , dirNames[i] + len);
991 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
992 (void) rpmCleanPath(t);
999 _("relocating directory %s to %s\n"), dirNames[i], t);
1006 /* Save original filenames in header and replace (relocated) filenames. */
1013 xx = hge(h, RPMTAG_BASENAMES, &t, &d, &c);
1014 xx = hae(h, RPMTAG_ORIGBASENAMES, t, d, c);
1018 xx = hge(h, RPMTAG_DIRNAMES, &t, &d, &c);
1019 xx = hae(h, RPMTAG_ORIGDIRNAMES, t, d, c);
1023 xx = hge(h, RPMTAG_DIRINDEXES, &t, &d, &c);
1024 xx = hae(h, RPMTAG_ORIGDIRINDEXES, t, d, c);
1027 xx = hme(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
1028 baseNames, fileCount);
1029 fi->bnl = hfd(fi->bnl, RPM_STRING_ARRAY_TYPE);
1030 xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc);
1032 xx = hme(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
1033 dirNames, dirCount);
1034 fi->dnl = hfd(fi->dnl, RPM_STRING_ARRAY_TYPE);
1035 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
1037 xx = hme(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE,
1038 dirIndexes, fileCount);
1039 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
1042 baseNames = hfd(baseNames, RPM_STRING_ARRAY_TYPE);
1043 dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
1049 rpmfi rpmfiFree(rpmfi fi)
1051 HFD_t hfd = headerFreeData;
1053 if (fi == NULL) return NULL;
1056 return rpmfiUnlink(fi, fi->Type);
1058 if (_rpmfi_debug < 0)
1059 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
1061 /* Free pre- and post-transaction script and interpreter strings. */
1062 fi->pretrans = _free(fi->pretrans);
1063 fi->pretransprog = _free(fi->pretransprog);
1064 fi->posttrans = _free(fi->posttrans);
1065 fi->posttransprog = _free(fi->posttransprog);
1068 fi->bnl = hfd(fi->bnl, -1);
1069 fi->dnl = hfd(fi->dnl, -1);
1071 fi->flinks = hfd(fi->flinks, -1);
1072 fi->flangs = hfd(fi->flangs, -1);
1073 fi->fmd5s = hfd(fi->fmd5s, -1);
1074 fi->md5s = _free(fi->md5s);
1076 fi->cdict = hfd(fi->cdict, -1);
1078 fi->fuser = hfd(fi->fuser, -1);
1079 fi->fgroup = hfd(fi->fgroup, -1);
1081 fi->fstates = _free(fi->fstates);
1083 if (!fi->keep_header && fi->h == NULL) {
1084 fi->fmtimes = _free(fi->fmtimes);
1085 fi->fmodes = _free(fi->fmodes);
1086 fi->fflags = _free(fi->fflags);
1087 fi->vflags = _free(fi->vflags);
1088 fi->fsizes = _free(fi->fsizes);
1089 fi->frdevs = _free(fi->frdevs);
1090 fi->finodes = _free(fi->finodes);
1091 fi->dil = _free(fi->dil);
1093 fi->fcolors = _free(fi->fcolors);
1094 fi->fcdictx = _free(fi->fcdictx);
1095 fi->ddict = _free(fi->ddict);
1096 fi->fddictx = _free(fi->fddictx);
1097 fi->fddictn = _free(fi->fddictn);
1102 fi->fsm = freeFSM(fi->fsm);
1104 fi->fn = _free(fi->fn);
1105 fi->apath = _free(fi->apath);
1106 fi->fmapflags = _free(fi->fmapflags);
1108 fi->obnl = hfd(fi->obnl, -1);
1109 fi->odnl = hfd(fi->odnl, -1);
1111 fi->fcontexts = hfd(fi->fcontexts, -1);
1113 fi->actions = _free(fi->actions);
1114 fi->replacedSizes = _free(fi->replacedSizes);
1115 fi->replaced = _free(fi->replaced);
1117 fi->h = headerFree(fi->h);
1119 (void) rpmfiUnlink(fi, fi->Type);
1120 memset(fi, 0, sizeof(*fi)); /* XXX trash and burn */
1127 * Convert hex to binary nibble.
1128 * @param c hex character
1129 * @return binary nibble
1131 static inline unsigned char nibble(char c)
1133 if (c >= '0' && c <= '9')
1135 if (c >= 'A' && c <= 'F')
1136 return (c - 'A') + 10;
1137 if (c >= 'a' && c <= 'f')
1138 return (c - 'a') + 10;
1142 #define _fdupe(_fi, _data) \
1143 if ((_fi)->_data != NULL) \
1144 (_fi)->_data = memcpy(xmalloc((_fi)->fc * sizeof(*(_fi)->_data)), \
1145 (_fi)->_data, (_fi)->fc * sizeof(*(_fi)->_data))
1147 /* XXX Ick, not SEF. */
1148 #define _fdupestring(_h, _tag, _data) \
1149 if (hge((_h), (_tag), NULL, (void **) &(_data), NULL)) \
1150 _data = xstrdup(_data)
1152 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, int scareMem)
1155 (scareMem ? (HGE_t) headerGetEntryMinMemory : (HGE_t) headerGetEntry);
1156 HFD_t hfd = headerFreeData;
1167 if (tagN == RPMTAG_BASENAMES) {
1174 fi = xcalloc(1, sizeof(*fi));
1175 if (fi == NULL) /* XXX can't happen */
1178 fi->magic = RPMFIMAGIC;
1184 fi->hae = (HAE_t) headerAddEntry;
1185 fi->hme = (HME_t) headerModifyEntry;
1186 fi->hre = (HRE_t) headerRemoveEntry;
1187 fi->hfd = headerFreeData;
1189 fi->h = (scareMem ? headerLink(h) : NULL);
1191 if (fi->fsm == NULL)
1194 /* 0 means unknown */
1195 xx = hge(h, RPMTAG_ARCHIVESIZE, NULL, (void **) &uip, NULL);
1197 fi->archiveSize = (xx ? *uip : 0);
1199 /* Extract pre- and post-transaction script and interpreter strings. */
1200 _fdupestring(h, RPMTAG_PRETRANS, fi->pretrans);
1201 _fdupestring(h, RPMTAG_PRETRANSPROG, fi->pretransprog);
1202 _fdupestring(h, RPMTAG_POSTTRANS, fi->posttrans);
1203 _fdupestring(h, RPMTAG_POSTTRANSPROG, fi->posttransprog);
1205 if (!hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc)) {
1210 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
1211 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
1212 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fi->fmodes, NULL);
1213 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fi->fflags, NULL);
1214 xx = hge(h, RPMTAG_FILEVERIFYFLAGS, NULL, (void **) &fi->vflags, NULL);
1215 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &fi->fsizes, NULL);
1217 xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fi->fcolors, NULL);
1219 if (fi->fcolors != NULL)
1220 for (i = 0; i < fi->fc; i++)
1221 fi->color |= fi->fcolors[i];
1222 xx = hge(h, RPMTAG_CLASSDICT, NULL, (void **) &fi->cdict, &fi->ncdict);
1223 xx = hge(h, RPMTAG_FILECLASS, NULL, (void **) &fi->fcdictx, NULL);
1225 xx = hge(h, RPMTAG_DEPENDSDICT, NULL, (void **) &fi->ddict, &fi->nddict);
1226 xx = hge(h, RPMTAG_FILEDEPENDSX, NULL, (void **) &fi->fddictx, NULL);
1227 xx = hge(h, RPMTAG_FILEDEPENDSN, NULL, (void **) &fi->fddictn, NULL);
1229 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &fi->fstates, NULL);
1230 if (xx == 0 || fi->fstates == NULL)
1231 fi->fstates = xcalloc(fi->fc, sizeof(*fi->fstates));
1233 _fdupe(fi, fstates);
1235 fi->action = FA_UNKNOWN;
1238 if (fi->actions == NULL)
1239 fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1241 fi->keep_header = (scareMem ? 1 : 0);
1243 /* XXX TR_REMOVED needs CPIO_MAP_{ABSOLUTE,ADDDOT} CPIO_ALL_HARDLINKS */
1245 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
1247 xx = hge(h, RPMTAG_FILELINKTOS, NULL, (void **) &fi->flinks, NULL);
1248 xx = hge(h, RPMTAG_FILELANGS, NULL, (void **) &fi->flangs, NULL);
1251 xx = hge(h, RPMTAG_FILEMD5S, NULL, (void **) &fi->fmd5s, NULL);
1255 t = xmalloc(fi->fc * 16);
1257 for (i = 0; i < fi->fc; i++) {
1261 fmd5 = fi->fmd5s[i];
1262 if (!(fmd5 && *fmd5 != '\0')) {
1267 for (j = 0; j < 16; j++, t++, fmd5 += 2)
1268 *t = (nibble(fmd5[0]) << 4) | nibble(fmd5[1]);
1270 fi->fmd5s = hfd(fi->fmd5s, -1);
1273 /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes, or fcontexts */
1274 xx = hge(h, RPMTAG_FILEMTIMES, NULL, (void **) &fi->fmtimes, NULL);
1275 xx = hge(h, RPMTAG_FILERDEVS, NULL, (void **) &fi->frdevs, NULL);
1276 xx = hge(h, RPMTAG_FILEINODES, NULL, (void **) &fi->finodes, NULL);
1278 fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1280 xx = hge(h, RPMTAG_FILEUSERNAME, NULL, (void **) &fi->fuser, NULL);
1281 xx = hge(h, RPMTAG_FILEGROUPNAME, NULL, (void **) &fi->fgroup, NULL);
1285 if ((p = rpmtsRelocateElement(ts)) != NULL && rpmteType(p) == TR_ADDED
1286 && !headerIsEntry(h, RPMTAG_SOURCEPACKAGE)
1287 && !headerIsEntry(h, RPMTAG_ORIGBASENAMES))
1289 const char * fmt = rpmGetPath("%{?_autorelocate_path}", NULL);
1290 const char * errstr;
1294 /* XXX error handling. */
1295 newPath = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
1299 /* XXX On ia64, change leading /emul/ix86 -> /emul/ia32, ick. */
1300 if (newPath != NULL && *newPath != '\0'
1301 && strlen(newPath) >= (sizeof("/emul/i386")-1)
1302 && newPath[0] == '/' && newPath[1] == 'e' && newPath[2] == 'm'
1303 && newPath[3] == 'u' && newPath[4] == 'l' && newPath[5] == '/'
1304 && newPath[6] == 'i' && newPath[8] == '8' && newPath[9] == '6')
1312 /* XXX Make sure autoreloc is not already specified. */
1314 if (newPath != NULL && *newPath != '\0' && p->relocs != NULL)
1315 for (i = 0; i < p->nrelocs; i++) {
1316 /* XXX {old,new}Path might be NULL */
1317 if (strcmp(p->relocs[i].oldPath, "/"))
1319 if (strcmp(p->relocs[i].newPath, newPath))
1324 /* XXX test for incompatible arch triggering autorelocation is dumb. */
1325 if (newPath != NULL && *newPath != '\0' && i == p->nrelocs
1326 && p->archScore == 0)
1330 xrealloc(p->relocs, (p->nrelocs + 2) * sizeof(*p->relocs));
1331 p->relocs[p->nrelocs].oldPath = xstrdup("/");
1332 p->relocs[p->nrelocs].newPath = xstrdup(newPath);
1333 p->autorelocatex = p->nrelocs;
1335 p->relocs[p->nrelocs].oldPath = NULL;
1336 p->relocs[p->nrelocs].newPath = NULL;
1338 newPath = _free(newPath);
1341 if (fi->actions == NULL)
1342 fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1343 /* FIX: fi-md5s undefined */
1344 foo = relocateFileList(ts, fi, h, fi->actions);
1345 fi->h = headerFree(fi->h);
1346 fi->h = headerLink(foo);
1347 foo = headerFree(foo);
1351 _fdupe(fi, fmtimes);
1353 _fdupe(fi, finodes);
1360 _fdupe(fi, fcolors);
1361 _fdupe(fi, fcdictx);
1363 if (fi->ddict != NULL)
1364 fi->ddict = memcpy(xmalloc(fi->nddict * sizeof(*fi->ddict)),
1365 fi->ddict, fi->nddict * sizeof(*fi->ddict));
1367 _fdupe(fi, fddictx);
1368 _fdupe(fi, fddictn);
1370 fi->h = headerFree(fi->h);
1374 for (i = 0; i < fi->dc; i++) {
1375 if ((len = strlen(fi->dnl[i])) > dnlmax)
1379 for (i = 0; i < fi->fc; i++) {
1380 if ((len = strlen(fi->bnl[i])) > bnlmax)
1383 fi->fnlen = dnlmax + bnlmax + 1;
1390 if (_rpmfi_debug < 0)
1391 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
1393 /* FIX: rpmfi null annotations */
1394 return rpmfiLink(fi, (fi ? fi->Type : NULL));
1397 void rpmfiBuildFClasses(Header h,
1398 const char *** fclassp, int * fcp)
1401 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
1402 const char * FClass;
1408 if ((ac = rpmfiFC(fi)) <= 0) {
1414 /* Compute size of file class argv array blob. */
1415 nb = (ac + 1) * sizeof(*av);
1416 fi = rpmfiInit(fi, 0);
1418 while (rpmfiNext(fi) >= 0) {
1419 FClass = rpmfiFClass(fi);
1420 if (FClass && *FClass != '\0')
1421 nb += strlen(FClass);
1425 /* Create and load file class argv array. */
1427 t = ((char *) av) + ((ac + 1) * sizeof(*av));
1429 fi = rpmfiInit(fi, 0);
1431 while (rpmfiNext(fi) >= 0) {
1432 FClass = rpmfiFClass(fi);
1434 if (FClass && *FClass != '\0')
1435 t = stpcpy(t, FClass);
1438 av[ac] = NULL; /* XXX tag arrays are not NULL terminated. */
1449 void rpmfiBuildFDeps(Header h, rpmTag tagN,
1450 const char *** fdepsp, int * fcp)
1453 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
1462 const uint32_t * ddict;
1466 if ((ac = rpmfiFC(fi)) <= 0) {
1472 if (tagN == RPMTAG_PROVIDENAME)
1474 else if (tagN == RPMTAG_REQUIRENAME)
1477 ds = rpmdsNew(h, tagN, scareMem);
1479 /* Compute size of file depends argv array blob. */
1480 nb = (ac + 1) * sizeof(*av);
1481 fi = rpmfiInit(fi, 0);
1483 while (rpmfiNext(fi) >= 0) {
1485 ndx = rpmfiFDepends(fi, &ddict);
1489 mydt = ((ix >> 24) & 0xff);
1490 if (mydt != deptype)
1493 (void) rpmdsSetIx(ds, ix-1);
1494 if (rpmdsNext(ds) < 0)
1496 DNEVR = rpmdsDNEVR(ds);
1498 nb += strlen(DNEVR+2) + 1;
1503 /* Create and load file depends argv array. */
1505 t = ((char *) av) + ((ac + 1) * sizeof(*av));
1507 fi = rpmfiInit(fi, 0);
1509 while (rpmfiNext(fi) >= 0) {
1512 ndx = rpmfiFDepends(fi, &ddict);
1516 mydt = ((ix >> 24) & 0xff);
1517 if (mydt != deptype)
1520 (void) rpmdsSetIx(ds, ix-1);
1521 if (rpmdsNext(ds) < 0)
1523 DNEVR = rpmdsDNEVR(ds);
1524 if (DNEVR != NULL) {
1525 t = stpcpy(t, DNEVR+2);
1532 av[ac] = NULL; /* XXX tag arrays are not NULL terminated. */
1544 void rpmfiBuildFNames(Header h, rpmTag tagN,
1545 const char *** fnp, int * fcp)
1547 HGE_t hge = (HGE_t)headerGetEntryMinMemory;
1548 HFD_t hfd = headerFreeData;
1549 const char ** baseNames;
1550 const char ** dirNames;
1551 uint32_t * dirIndexes;
1553 const char ** fileNames;
1555 rpmTag dirNameTag = 0;
1556 rpmTag dirIndexesTag = 0;
1557 rpmTagType bnt, dnt;
1561 if (tagN == RPMTAG_BASENAMES) {
1562 dirNameTag = RPMTAG_DIRNAMES;
1563 dirIndexesTag = RPMTAG_DIRINDEXES;
1564 } else if (tagN == RPMTAG_ORIGBASENAMES) {
1565 dirNameTag = RPMTAG_ORIGDIRNAMES;
1566 dirIndexesTag = RPMTAG_ORIGDIRINDEXES;
1569 if (!hge(h, tagN, &bnt, (void **) &baseNames, &count)) {
1570 if (fnp) *fnp = NULL;
1572 return; /* no file list */
1575 xx = hge(h, dirNameTag, &dnt, (void **) &dirNames, NULL);
1576 xx = hge(h, dirIndexesTag, NULL, (void **) &dirIndexes, &count);
1578 size = sizeof(*fileNames) * count;
1579 for (i = 0; i < count; i++)
1580 size += strlen(baseNames[i]) + strlen(dirNames[dirIndexes[i]]) + 1;
1582 fileNames = xmalloc(size);
1583 t = ((char *) fileNames) + (sizeof(*fileNames) * count);
1584 for (i = 0; i < count; i++) {
1586 t = stpcpy( stpcpy(t, dirNames[dirIndexes[i]]), baseNames[i]);
1589 baseNames = hfd(baseNames, bnt);
1590 dirNames = hfd(dirNames, dnt);
1595 fileNames = _free(fileNames);
1596 if (fcp) *fcp = count;