3 * Routines to handle file info tag sets.
8 #include <rpmio_internal.h>
11 #include "cpio.h" /* XXX CPIO_FOO */
12 #include "fsm.h" /* XXX newFSM() */
16 #define _RPMFI_INTERNAL
19 #define _RPMTE_INTERNAL /* relocations */
23 #include "misc.h" /* XXX stripTrailingChar */
32 rpmfi XrpmfiUnlink(rpmfi fi, const char * msg, const char * fn, unsigned ln)
34 if (fi == NULL) return NULL;
36 if (_rpmfi_debug && msg != NULL)
37 fprintf(stderr, "--> fi %p -- %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
43 rpmfi XrpmfiLink(rpmfi fi, const char * msg, const char * fn, unsigned ln)
45 if (fi == NULL) return NULL;
48 if (_rpmfi_debug && msg != NULL)
49 fprintf(stderr, "--> fi %p ++ %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
51 /*@-refcounttrans@*/ return fi; /*@=refcounttrans@*/
56 return (fi != NULL ? fi->fc : 0);
61 return (fi != NULL ? fi->dc : 0);
72 return (fi != NULL ? fi->i : -1);
75 int rpmfiSetFX(rpmfi fi, int fx)
79 if (fi != NULL && fx >= 0 && fx < fi->fc) {
83 fi->j = fi->dil[fi->i];
91 return (fi != NULL ? fi->j : -1);
94 int rpmfiSetDX(rpmfi fi, int dx)
98 if (fi != NULL && dx >= 0 && dx < fi->dc) {
105 const char * rpmfiBN(rpmfi fi)
107 const char * BN = NULL;
109 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
118 const char * rpmfiDN(rpmfi fi)
120 const char * DN = NULL;
122 if (fi != NULL && fi->j >= 0 && fi->j < fi->dc) {
131 const char * rpmfiFN(rpmfi fi)
133 const char * FN = "";
136 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
139 fi->fn = xmalloc(fi->fnlen);
143 t = stpcpy(t, fi->dnl[fi->dil[fi->i]]);
144 t = stpcpy(t, fi->bnl[fi->i]);
151 int_32 rpmfiFFlags(rpmfi fi)
155 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
157 if (fi->fflags != NULL)
158 FFlags = fi->fflags[fi->i];
164 int_32 rpmfiVFlags(rpmfi fi)
168 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
170 if (fi->vflags != NULL)
171 VFlags = fi->vflags[fi->i];
177 int_16 rpmfiFMode(rpmfi fi)
181 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
183 if (fi->fmodes != NULL)
184 fmode = fi->fmodes[fi->i];
190 rpmfileState rpmfiFState(rpmfi fi)
192 rpmfileState fstate = RPMFILE_STATE_MISSING;
194 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
196 if (fi->fstates != NULL)
197 fstate = fi->fstates[fi->i];
203 const unsigned char * rpmfiMD5(rpmfi fi)
205 unsigned char * MD5 = NULL;
207 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
209 if (fi->md5s != NULL)
210 MD5 = fi->md5s + (16 * fi->i);
216 const char * rpmfiFLink(rpmfi fi)
218 const char * flink = NULL;
220 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
222 if (fi->flinks != NULL)
223 flink = fi->flinks[fi->i];
229 int_32 rpmfiFSize(rpmfi fi)
233 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
235 if (fi->fsizes != NULL)
236 fsize = fi->fsizes[fi->i];
242 int_16 rpmfiFRdev(rpmfi fi)
246 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
248 if (fi->frdevs != NULL)
249 frdev = fi->frdevs[fi->i];
255 int_32 rpmfiFInode(rpmfi fi)
259 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
261 if (fi->finodes != NULL)
262 finode = fi->finodes[fi->i];
268 uint_32 rpmfiFColor(rpmfi fi)
272 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
274 if (fi->fcolors != NULL)
275 /* XXX ignore all but lsnibble for now. */
276 fcolor = (fi->fcolors[fi->i] & 0x0f);
282 const char * rpmfiFClass(rpmfi fi)
284 const char * fclass = NULL;
287 if (fi != NULL && fi->fcdictx != NULL && fi->i >= 0 && fi->i < fi->fc) {
289 cdictx = fi->fcdictx[fi->i];
290 if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
291 fclass = fi->cdict[cdictx];
297 int_32 rpmfiFDepends(rpmfi fi, const int_32 ** fddictp)
301 const int_32 * fddict = NULL;
303 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
305 if (fi->fddictn != NULL)
306 fddictn = fi->fddictn[fi->i];
307 if (fddictn > 0 && fi->fddictx != NULL)
308 fddictx = fi->fddictx[fi->i];
309 if (fi->ddict != NULL && fddictx >= 0 && (fddictx+fddictn) <= fi->nddict)
310 fddict = fi->ddict + fddictx;
313 /*@-boundswrite -dependenttrans -onlytrans @*/
316 /*@=boundswrite =dependenttrans =onlytrans @*/
320 int_32 rpmfiFNlink(rpmfi fi)
324 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
325 /* XXX rpm-2.3.12 has not RPMTAG_FILEINODES */
327 if (fi->finodes && fi->frdevs) {
328 int_32 finode = fi->finodes[fi->i];
329 int_16 frdev = fi->frdevs[fi->i];
332 for (j = 0; j < fi->fc; j++) {
333 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
342 int_32 rpmfiFMtime(rpmfi fi)
346 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
348 if (fi->fmtimes != NULL)
349 fmtime = fi->fmtimes[fi->i];
355 const char * rpmfiFUser(rpmfi fi)
357 const char * fuser = NULL;
359 /* XXX add support for ancient RPMTAG_FILEUIDS? */
360 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
362 if (fi->fuser != NULL)
363 fuser = fi->fuser[fi->i];
369 const char * rpmfiFGroup(rpmfi fi)
371 const char * fgroup = NULL;
373 /* XXX add support for ancient RPMTAG_FILEGIDS? */
374 if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
376 if (fi->fgroup != NULL)
377 fgroup = fi->fgroup[fi->i];
383 int rpmfiNext(rpmfi fi)
387 if (fi != NULL && ++fi->i >= 0) {
388 if (fi->i < fi->fc) {
392 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] : ""));
407 rpmfi rpmfiInit(rpmfi fi, int fx)
410 if (fx >= 0 && fx < fi->fc) {
421 int rpmfiNextD(rpmfi fi)
425 if (fi != NULL && ++fi->j >= 0) {
432 if (_rpmfi_debug < 0 && j != -1)
433 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, (fi->Type ? fi->Type : "?Type?"), j);
441 rpmfi rpmfiInitD(rpmfi fi, int dx)
444 if (dx >= 0 && dx < fi->fc)
456 * Identify a file type.
457 * @param ft file type
458 * @return string to identify a file type
460 static /*@observer@*/
461 const char *const ftstring (fileTypes ft)
465 case XDIR: return "directory";
466 case CDEV: return "char dev";
467 case BDEV: return "block dev";
468 case LINK: return "link";
469 case SOCK: return "sock";
470 case PIPE: return "fifo/pipe";
471 case REG: return "file";
472 default: return "unknown file type";
477 fileTypes whatis(uint_16 mode)
479 if (S_ISDIR(mode)) return XDIR;
480 if (S_ISCHR(mode)) return CDEV;
481 if (S_ISBLK(mode)) return BDEV;
482 if (S_ISLNK(mode)) return LINK;
484 if (S_ISSOCK(mode)) return SOCK;
486 if (S_ISFIFO(mode)) return PIPE;
491 const char *const rpmfiTypeString(rpmfi fi)
493 switch(rpmteType(fi->te)) {
494 case TR_ADDED: return " install";
495 case TR_REMOVED: return " erase";
496 default: return "???";
501 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
504 * Relocate files in header.
505 * @todo multilib file dispositions need to be checked.
506 * @param ts transaction set
507 * @param fi transaction element file info
508 * @param origH package header
509 * @param actions file dispositions
510 * @return header with relocated files
514 Header relocateFileList(const rpmts ts, rpmfi fi,
515 Header origH, fileAction * actions)
516 /*@modifies ts, fi, origH, actions @*/
518 rpmte p = rpmtsRelocateElement(ts);
522 HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
523 static int _printed = 0;
524 int allowBadRelocate = (rpmtsFilterFlags(ts) & RPMPROB_FILTER_FORCERELOCATE);
525 rpmRelocation * relocations = NULL;
527 const char ** validRelocations;
528 rpmTagType validType;
530 const char ** baseNames;
531 const char ** dirNames;
533 int_32 * newDirIndexes;
536 uint_32 * fFlags = NULL;
537 uint_16 * fModes = NULL;
543 int haveRelocatedFile = 0;
548 if (!hge(origH, RPMTAG_PREFIXES, &validType,
549 (void **) &validRelocations, &numValid))
555 while (p->relocs[numRelocations].newPath ||
556 p->relocs[numRelocations].oldPath)
560 * If no relocations are specified (usually the case), then return the
561 * original header. If there are prefixes, however, then INSTPREFIXES
562 * should be added, but, since relocateFileList() can be called more
563 * than once for the same header, don't bother if already present.
565 if (p->relocs == NULL || numRelocations == 0) {
567 if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES))
568 xx = hae(origH, RPMTAG_INSTPREFIXES,
569 validType, validRelocations, numValid);
570 validRelocations = hfd(validRelocations, validType);
572 /* XXX FIXME multilib file actions need to be checked. */
573 return headerLink(origH);
576 h = headerLink(origH);
578 relocations = alloca(sizeof(*relocations) * numRelocations);
580 /* Build sorted relocation list from raw relocations. */
581 for (i = 0; i < numRelocations; i++) {
585 * Default relocations (oldPath == NULL) are handled in the UI,
588 if (p->relocs[i].oldPath == NULL) continue; /* XXX can't happen */
590 /* FIXME: Trailing /'s will confuse us greatly. Internal ones will
591 too, but those are more trouble to fix up. :-( */
592 t = alloca_strdup(p->relocs[i].oldPath);
594 relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
596 : stripTrailingChar(t, '/');
599 /* An old path w/o a new path is valid, and indicates exclusion */
600 if (p->relocs[i].newPath) {
603 t = alloca_strdup(p->relocs[i].newPath);
605 relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
607 : stripTrailingChar(t, '/');
610 /*@-nullpass@*/ /* FIX: relocations[i].oldPath == NULL */
611 /* Verify that the relocation's old path is in the header. */
612 for (j = 0; j < numValid; j++) {
613 if (!strcmp(validRelocations[j], relocations[i].oldPath))
614 /*@innerbreak@*/ break;
617 /* XXX actions check prevents problem from being appended twice. */
618 if (j == numValid && !allowBadRelocate && actions) {
619 rpmps ps = rpmtsProblems(ts);
620 rpmpsAppend(ps, RPMPROB_BADRELOCATE,
621 rpmteNEVR(p), rpmteKey(p),
622 relocations[i].oldPath, NULL, NULL, 0);
626 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
632 relocations[i].newPath = NULL;
636 /* stupid bubble sort, but it's probably faster here */
637 for (i = 0; i < numRelocations; i++) {
640 for (j = 1; j < numRelocations; j++) {
641 rpmRelocation tmpReloc;
642 if (relocations[j - 1].oldPath == NULL || /* XXX can't happen */
643 relocations[j ].oldPath == NULL || /* XXX can't happen */
644 strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
645 /*@innercontinue@*/ continue;
646 /*@-usereleased@*/ /* LCL: ??? */
647 tmpReloc = relocations[j - 1];
648 relocations[j - 1] = relocations[j];
649 relocations[j] = tmpReloc;
653 if (!madeSwap) break;
658 rpmMessage(RPMMESS_DEBUG, _("========== relocations\n"));
659 for (i = 0; i < numRelocations; i++) {
660 if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
661 if (relocations[i].newPath == NULL)
662 rpmMessage(RPMMESS_DEBUG, _("%5d exclude %s\n"),
663 i, relocations[i].oldPath);
665 rpmMessage(RPMMESS_DEBUG, _("%5d relocate %s -> %s\n"),
666 i, relocations[i].oldPath, relocations[i].newPath);
670 /* Add relocation values to the header */
672 const char ** actualRelocations;
675 actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
677 for (i = 0; i < numValid; i++) {
678 for (j = 0; j < numRelocations; j++) {
679 if (relocations[j].oldPath == NULL || /* XXX can't happen */
680 strcmp(validRelocations[i], relocations[j].oldPath))
681 /*@innercontinue@*/ continue;
682 /* On install, a relocate to NULL means skip the path. */
683 if (relocations[j].newPath) {
684 actualRelocations[numActual] = relocations[j].newPath;
687 /*@innerbreak@*/ break;
689 if (j == numRelocations) {
690 actualRelocations[numActual] = validRelocations[i];
696 xx = hae(h, RPMTAG_INSTPREFIXES, RPM_STRING_ARRAY_TYPE,
697 (void **) actualRelocations, numActual);
699 actualRelocations = _free(actualRelocations);
700 validRelocations = hfd(validRelocations, validType);
703 xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &baseNames, &fileCount);
704 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
705 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &dirNames, &dirCount);
706 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fFlags, NULL);
707 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fModes, NULL);
709 skipDirList = alloca(dirCount * sizeof(*skipDirList));
710 memset(skipDirList, 0, dirCount * sizeof(*skipDirList));
712 newDirIndexes = alloca(sizeof(*newDirIndexes) * fileCount);
713 memcpy(newDirIndexes, dirIndexes, sizeof(*newDirIndexes) * fileCount);
714 dirIndexes = newDirIndexes;
717 * For all relocations, we go through sorted file/relocation lists
718 * backwards so that /usr/local relocations take precedence over /usr
722 /* Relocate individual paths. */
724 for (i = fileCount - 1; i >= 0; i--) {
729 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
731 if (len >= fileAlloced) {
732 fileAlloced = len * 2;
733 fn = xrealloc(fn, fileAlloced);
737 fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
740 * See if this file path needs relocating.
743 * XXX FIXME: Would a bsearch of the (already sorted)
744 * relocation list be a good idea?
746 for (j = numRelocations - 1; j >= 0; j--) {
747 if (relocations[j].oldPath == NULL) /* XXX can't happen */
748 /*@innercontinue@*/ continue;
749 len = strcmp(relocations[j].oldPath, "/")
750 ? strlen(relocations[j].oldPath)
754 /*@innercontinue@*/ continue;
756 * Only subdirectories or complete file paths may be relocated. We
757 * don't check for '\0' as our directory names all end in '/'.
759 if (!(fn[len] == '/' || fnlen == len))
760 /*@innercontinue@*/ continue;
762 if (strncmp(relocations[j].oldPath, fn, len))
763 /*@innercontinue@*/ continue;
764 /*@innerbreak@*/ break;
768 ft = whatis(fModes[i]);
770 /* On install, a relocate to NULL means skip the path. */
771 if (relocations[j].newPath == NULL) {
773 /* Start with the parent, looking for directory to exclude. */
774 for (j = dirIndexes[i]; j < dirCount; j++) {
775 len = strlen(dirNames[j]) - 1;
776 while (len > 0 && dirNames[j][len-1] == '/') len--;
778 /*@innercontinue@*/ continue;
779 if (strncmp(fn, dirNames[j], fnlen))
780 /*@innercontinue@*/ continue;
781 /*@innerbreak@*/ break;
787 actions[i] = FA_SKIPNSTATE;
788 rpmMessage(RPMMESS_DEBUG, _("excluding %s %s\n"),
794 /* Relocation on full paths only, please. */
795 if (fnlen != len) continue;
798 rpmMessage(RPMMESS_DEBUG, _("relocating %s to %s\n"),
799 fn, relocations[j].newPath);
802 strcpy(fn, relocations[j].newPath);
803 { char * te = strrchr(fn, '/');
805 if (te > fn) te++; /* root is special */
808 te = fn + strlen(fn);
809 /*@-nullpass -nullderef@*/ /* LCL: te != NULL here. */
810 if (strcmp(baseNames[i], te)) /* basename changed too? */
811 baseNames[i] = alloca_strdup(te);
812 *te = '\0'; /* terminate new directory name */
813 /*@=nullpass =nullderef@*/
816 /* Does this directory already exist in the directory list? */
817 for (j = 0; j < dirCount; j++) {
818 if (fnlen != strlen(dirNames[j]))
819 /*@innercontinue@*/ continue;
820 if (strncmp(fn, dirNames[j], fnlen))
821 /*@innercontinue@*/ continue;
822 /*@innerbreak@*/ break;
830 /* Creating new paths is a pita */
831 if (!haveRelocatedFile) {
832 const char ** newDirList;
834 haveRelocatedFile = 1;
835 newDirList = xmalloc((dirCount + 1) * sizeof(*newDirList));
836 for (j = 0; j < dirCount; j++)
837 newDirList[j] = alloca_strdup(dirNames[j]);
838 dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
839 dirNames = newDirList;
841 dirNames = xrealloc(dirNames,
842 sizeof(*dirNames) * (dirCount + 1));
845 dirNames[dirCount] = alloca_strdup(fn);
846 dirIndexes[i] = dirCount;
850 /* Finish off by relocating directories. */
851 for (i = dirCount - 1; i >= 0; i--) {
852 for (j = numRelocations - 1; j >= 0; j--) {
854 if (relocations[j].oldPath == NULL) /* XXX can't happen */
855 /*@innercontinue@*/ continue;
856 len = strcmp(relocations[j].oldPath, "/")
857 ? strlen(relocations[j].oldPath)
860 if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
861 /*@innercontinue@*/ continue;
864 * Only subdirectories or complete file paths may be relocated. We
865 * don't check for '\0' as our directory names all end in '/'.
867 if (dirNames[i][len] != '/')
868 /*@innercontinue@*/ continue;
870 if (relocations[j].newPath) { /* Relocate the path */
871 const char * s = relocations[j].newPath;
872 char * t = alloca(strlen(s) + strlen(dirNames[i]) - len + 1);
875 (void) stpcpy( stpcpy(t, s) , dirNames[i] + len);
877 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
878 (void) rpmCleanPath(t);
884 rpmMessage(RPMMESS_DEBUG,
885 _("relocating directory %s to %s\n"), dirNames[i], t);
892 /* Save original filenames in header and replace (relocated) filenames. */
899 xx = hge(h, RPMTAG_BASENAMES, &t, &d, &c);
900 xx = hae(h, RPMTAG_ORIGBASENAMES, t, d, c);
904 xx = hge(h, RPMTAG_DIRNAMES, &t, &d, &c);
905 xx = hae(h, RPMTAG_ORIGDIRNAMES, t, d, c);
909 xx = hge(h, RPMTAG_DIRINDEXES, &t, &d, &c);
910 xx = hae(h, RPMTAG_ORIGDIRINDEXES, t, d, c);
913 xx = hme(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
914 baseNames, fileCount);
915 fi->bnl = hfd(fi->bnl, RPM_STRING_ARRAY_TYPE);
916 xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc);
918 xx = hme(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
920 fi->dnl = hfd(fi->dnl, RPM_STRING_ARRAY_TYPE);
921 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
923 xx = hme(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE,
924 dirIndexes, fileCount);
925 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
928 baseNames = hfd(baseNames, RPM_STRING_ARRAY_TYPE);
929 dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
936 rpmfi rpmfiFree(rpmfi fi)
938 HFD_t hfd = headerFreeData;
940 if (fi == NULL) return NULL;
943 return rpmfiUnlink(fi, fi->Type);
946 if (_rpmfi_debug < 0)
947 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
952 fi->bnl = hfd(fi->bnl, -1);
953 fi->dnl = hfd(fi->dnl, -1);
955 fi->flinks = hfd(fi->flinks, -1);
956 fi->flangs = hfd(fi->flangs, -1);
957 fi->fmd5s = hfd(fi->fmd5s, -1);
958 fi->md5s = _free(fi->md5s);
960 fi->cdict = hfd(fi->cdict, -1);
962 fi->fuser = hfd(fi->fuser, -1);
963 fi->fuids = _free(fi->fuids);
964 fi->fgroup = hfd(fi->fgroup, -1);
965 fi->fgids = _free(fi->fgids);
967 fi->fstates = _free(fi->fstates);
970 if (!fi->keep_header && fi->h == NULL) {
971 fi->fmtimes = _free(fi->fmtimes);
972 fi->fmodes = _free(fi->fmodes);
973 fi->fflags = _free(fi->fflags);
974 fi->vflags = _free(fi->vflags);
975 fi->fsizes = _free(fi->fsizes);
976 fi->frdevs = _free(fi->frdevs);
977 fi->finodes = _free(fi->finodes);
978 fi->dil = _free(fi->dil);
980 fi->fcolors = _free(fi->fcolors);
981 fi->fcdictx = _free(fi->fcdictx);
982 fi->ddict = _free(fi->ddict);
983 fi->fddictx = _free(fi->fddictx);
984 fi->fddictn = _free(fi->fddictn);
991 fi->fsm = freeFSM(fi->fsm);
993 fi->fn = _free(fi->fn);
994 fi->apath = _free(fi->apath);
995 fi->fmapflags = _free(fi->fmapflags);
997 fi->obnl = hfd(fi->obnl, -1);
998 fi->odnl = hfd(fi->odnl, -1);
1000 fi->actions = _free(fi->actions);
1001 fi->replacedSizes = _free(fi->replacedSizes);
1002 fi->replaced = _free(fi->replaced);
1004 fi->h = headerFree(fi->h);
1006 /*@-nullstate -refcounttrans -usereleased@*/
1007 (void) rpmfiUnlink(fi, fi->Type);
1008 memset(fi, 0, sizeof(*fi)); /* XXX trash and burn */
1010 /*@=nullstate =refcounttrans =usereleased@*/
1016 * Convert hex to binary nibble.
1017 * @param c hex character
1018 * @return binary nibble
1020 static inline unsigned char nibble(char c)
1023 if (c >= '0' && c <= '9')
1025 if (c >= 'A' && c <= 'F')
1026 return (c - 'A') + 10;
1027 if (c >= 'a' && c <= 'f')
1028 return (c - 'a') + 10;
1032 #define _fdupe(_fi, _data) \
1033 if ((_fi)->_data != NULL) \
1034 (_fi)->_data = memcpy(xmalloc((_fi)->fc * sizeof(*(_fi)->_data)), \
1035 (_fi)->_data, (_fi)->fc * sizeof(*(_fi)->_data))
1037 rpmfi rpmfiNew(rpmts ts, Header h, rpmTag tagN, int scareMem)
1040 (scareMem ? (HGE_t) headerGetEntryMinMemory : (HGE_t) headerGetEntry);
1041 HFD_t hfd = headerFreeData;
1052 if (tagN == RPMTAG_BASENAMES) {
1059 fi = xcalloc(1, sizeof(*fi));
1060 if (fi == NULL) /* XXX can't happen */
1063 fi->magic = RPMFIMAGIC;
1069 fi->hae = (HAE_t) headerAddEntry;
1070 fi->hme = (HME_t) headerModifyEntry;
1071 fi->hre = (HRE_t) headerRemoveEntry;
1072 fi->hfd = headerFreeData;
1074 fi->h = (scareMem ? headerLink(h) : NULL);
1076 if (fi->fsm == NULL)
1079 /* 0 means unknown */
1080 xx = hge(h, RPMTAG_ARCHIVESIZE, NULL, (void **) &uip, NULL);
1082 fi->archiveSize = (xx ? *uip : 0);
1084 if (!hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc)) {
1089 xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
1090 xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
1091 xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fi->fmodes, NULL);
1092 xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fi->fflags, NULL);
1093 xx = hge(h, RPMTAG_FILEVERIFYFLAGS, NULL, (void **) &fi->vflags, NULL);
1094 xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &fi->fsizes, NULL);
1096 xx = hge(h, RPMTAG_FILECOLORS, NULL, (void **) &fi->fcolors, NULL);
1097 xx = hge(h, RPMTAG_CLASSDICT, NULL, (void **) &fi->cdict, &fi->ncdict);
1098 xx = hge(h, RPMTAG_FILECLASS, NULL, (void **) &fi->fcdictx, NULL);
1100 xx = hge(h, RPMTAG_DEPENDSDICT, NULL, (void **) &fi->ddict, &fi->nddict);
1101 xx = hge(h, RPMTAG_FILEDEPENDSX, NULL, (void **) &fi->fddictx, NULL);
1102 xx = hge(h, RPMTAG_FILEDEPENDSN, NULL, (void **) &fi->fddictn, NULL);
1104 xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &fi->fstates, NULL);
1105 if (xx == 0 || fi->fstates == NULL)
1106 fi->fstates = xcalloc(fi->fc, sizeof(*fi->fstates));
1108 _fdupe(fi, fstates);
1110 fi->action = FA_UNKNOWN;
1113 if (fi->actions == NULL)
1114 fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1116 fi->keep_header = (scareMem ? 1 : 0);
1118 /* XXX TR_REMOVED needs CPIO_MAP_{ABSOLUTE,ADDDOT} CPIO_ALL_HARDLINKS */
1120 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
1122 xx = hge(h, RPMTAG_FILELINKTOS, NULL, (void **) &fi->flinks, NULL);
1123 xx = hge(h, RPMTAG_FILELANGS, NULL, (void **) &fi->flangs, NULL);
1126 xx = hge(h, RPMTAG_FILEMD5S, NULL, (void **) &fi->fmd5s, NULL);
1130 t = xmalloc(fi->fc * 16);
1132 for (i = 0; i < fi->fc; i++) {
1136 fmd5 = fi->fmd5s[i];
1137 if (!(fmd5 && *fmd5 != '\0')) {
1142 for (j = 0; j < 16; j++, t++, fmd5 += 2)
1143 *t = (nibble(fmd5[0]) << 4) | nibble(fmd5[1]);
1145 fi->fmd5s = hfd(fi->fmd5s, -1);
1148 /* XXX TR_REMOVED doesn;t need fmtimes, frdevs or finodes */
1149 xx = hge(h, RPMTAG_FILEMTIMES, NULL, (void **) &fi->fmtimes, NULL);
1150 xx = hge(h, RPMTAG_FILERDEVS, NULL, (void **) &fi->frdevs, NULL);
1151 xx = hge(h, RPMTAG_FILEINODES, NULL, (void **) &fi->finodes, NULL);
1153 fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1155 xx = hge(h, RPMTAG_FILEUSERNAME, NULL, (void **) &fi->fuser, NULL);
1157 xx = hge(h, RPMTAG_FILEGROUPNAME, NULL, (void **) &fi->fgroup, NULL);
1162 if ((p = rpmtsRelocateElement(ts)) != NULL && rpmteType(p) == TR_ADDED) {
1165 if (fi->actions == NULL)
1166 fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1167 /*@-compdef@*/ /* FIX: fi-md5s undefined */
1168 foo = relocateFileList(ts, fi, h, fi->actions);
1170 fi->h = headerFree(fi->h);
1171 fi->h = headerLink(foo);
1172 foo = headerFree(foo);
1176 _fdupe(fi, fmtimes);
1178 _fdupe(fi, finodes);
1185 _fdupe(fi, fcolors);
1186 _fdupe(fi, fcdictx);
1188 if (fi->ddict != NULL)
1189 fi->ddict = memcpy(xmalloc(fi->nddict * sizeof(*fi->ddict)),
1190 fi->ddict, fi->nddict * sizeof(*fi->ddict));
1192 _fdupe(fi, fddictx);
1193 _fdupe(fi, fddictn);
1195 fi->h = headerFree(fi->h);
1199 for (i = 0; i < fi->dc; i++) {
1200 if ((len = strlen(fi->dnl[i])) > dnlmax)
1204 for (i = 0; i < fi->fc; i++) {
1205 if ((len = strlen(fi->bnl[i])) > bnlmax)
1208 fi->fnlen = dnlmax + bnlmax + 1;
1216 if (_rpmfi_debug < 0)
1217 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
1220 /*@-compdef -nullstate@*/ /* FIX: rpmfi null annotations */
1221 return rpmfiLink(fi, (fi ? fi->Type : NULL));
1222 /*@=compdef =nullstate@*/
1225 void rpmfiBuildFClasses(Header h,
1226 /*@out@*/ const char *** fclassp, /*@out@*/ int * fcp)
1229 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
1230 const char * FClass;
1236 if ((ac = rpmfiFC(fi)) <= 0) {
1242 /* Compute size of file class argv array blob. */
1243 nb = (ac + 1) * sizeof(*av);
1244 fi = rpmfiInit(fi, 0);
1246 while (rpmfiNext(fi) >= 0) {
1247 FClass = rpmfiFClass(fi);
1248 if (FClass && *FClass != '\0')
1249 nb += strlen(FClass);
1253 /* Create and load file class argv array. */
1255 t = ((char *) av) + ((ac + 1) * sizeof(*av));
1257 fi = rpmfiInit(fi, 0);
1259 while (rpmfiNext(fi) >= 0) {
1260 FClass = rpmfiFClass(fi);
1262 if (FClass && *FClass != '\0')
1263 t = stpcpy(t, FClass);
1280 void rpmfiBuildFDeps(Header h, rpmTag tagN,
1281 /*@out@*/ const char *** fdepsp, /*@out@*/ int * fcp)
1284 rpmfi fi = rpmfiNew(NULL, h, RPMTAG_BASENAMES, scareMem);
1293 const int_32 * ddict;
1297 if ((ac = rpmfiFC(fi)) <= 0) {
1303 if (tagN == RPMTAG_PROVIDENAME)
1305 else if (tagN == RPMTAG_REQUIRENAME)
1308 ds = rpmdsNew(h, tagN, scareMem);
1310 /* Compute size of file depends argv array blob. */
1311 nb = (ac + 1) * sizeof(*av);
1312 fi = rpmfiInit(fi, 0);
1314 while (rpmfiNext(fi) >= 0) {
1316 ndx = rpmfiFDepends(fi, &ddict);
1320 mydt = ((ix >> 24) & 0xff);
1321 if (mydt != deptype)
1322 /*@innercontinue@*/ continue;
1324 (void) rpmdsSetIx(ds, ix-1);
1325 if (rpmdsNext(ds) < 0)
1326 /*@innercontinue@*/ continue;
1327 DNEVR = rpmdsDNEVR(ds);
1329 nb += strlen(DNEVR+2) + 1;
1334 /* Create and load file depends argv array. */
1336 t = ((char *) av) + ((ac + 1) * sizeof(*av));
1339 fi = rpmfiInit(fi, 0);
1341 while (rpmfiNext(fi) >= 0) {
1344 ndx = rpmfiFDepends(fi, &ddict);
1348 mydt = ((ix >> 24) & 0xff);
1349 if (mydt != deptype)
1350 /*@innercontinue@*/ continue;
1352 (void) rpmdsSetIx(ds, ix-1);
1353 if (rpmdsNext(ds) < 0)
1354 /*@innercontinue@*/ continue;
1355 DNEVR = rpmdsDNEVR(ds);
1356 if (DNEVR != NULL) {
1357 t = stpcpy(t, DNEVR+2);