gathering as much data as we can on the f14 boxes setup
[infrastructure.git] / bbox-f14 / rpm-changes / b / lib / rpmfi.c
1 /** \ingroup rpmdep
2  * \file lib/rpmfi.c
3  * Routines to handle file info tag sets.
4  */
5
6 #include "system.h"
7
8 #include <rpm/rpmlog.h>
9 #include <rpm/rpmts.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>
14
15 #include "lib/rpmfi_internal.h"
16 #include "lib/rpmte_internal.h" /* relocations */
17 #include "lib/cpio.h"   /* XXX CPIO_FOO */
18
19 #include "debug.h"
20
21 /*
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
26  * like an overkill.
27  */
28 struct strcache_s {
29     char **uniq;
30     scidx_t num;
31 };
32
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;
37
38 static scidx_t strcachePut(strcache cache, const char *str)
39 {
40     int found = 0;
41     scidx_t ret;
42
43     for (scidx_t i = 0; i < cache->num; i++) {
44         if (rstreq(str, cache->uniq[i])) {
45             ret = i;
46             found = 1;
47             break;
48         }
49     }
50     if (!found) {
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);
56         ret = cache->num;
57         cache->num++;
58     }
59     return ret;
60 }
61
62 static const char *strcacheGet(strcache cache, scidx_t idx)
63 {
64     const char *name = NULL;
65     if (idx >= 0 && idx < cache->num && cache->uniq != NULL)
66         name = cache->uniq[idx];
67     return name;
68 }
69     
70 static strcache strcacheNew(void)
71 {
72     strcache cache = xcalloc(1, sizeof(*cache));
73     return cache;
74 }
75
76 static strcache strcacheFree(strcache cache)
77 {
78     if (cache != NULL) {
79         for (scidx_t i = 0; i < cache->num; i++) {
80             free(cache->uniq[i]);
81         }
82         cache->uniq = _free(cache->uniq);
83         free(cache);
84     }
85     return NULL;
86
87
88 static rpmfi rpmfiUnlink(rpmfi fi)
89 {
90     if (fi)
91         fi->nrefs--;
92     return NULL;
93 }
94
95 rpmfi rpmfiLink(rpmfi fi)
96 {
97     if (fi)
98         fi->nrefs++;
99     return fi;
100 }
101
102 rpm_count_t rpmfiFC(rpmfi fi)
103 {
104     return (fi != NULL ? fi->fc : 0);
105 }
106
107 rpm_count_t rpmfiDC(rpmfi fi)
108 {
109     return (fi != NULL ? fi->dc : 0);
110 }
111
112 #ifdef  NOTYET
113 int rpmfiDI(rpmfi fi)
114 {
115 }
116 #endif
117
118 int rpmfiFX(rpmfi fi)
119 {
120     return (fi != NULL ? fi->i : -1);
121 }
122
123 int rpmfiSetFX(rpmfi fi, int fx)
124 {
125     int i = -1;
126
127     if (fi != NULL && fx >= 0 && fx < fi->fc) {
128         i = fi->i;
129         fi->i = fx;
130         fi->j = fi->dil[fi->i];
131     }
132     return i;
133 }
134
135 int rpmfiDX(rpmfi fi)
136 {
137     return (fi != NULL ? fi->j : -1);
138 }
139
140 int rpmfiSetDX(rpmfi fi, int dx)
141 {
142     int j = -1;
143
144     if (fi != NULL && dx >= 0 && dx < fi->dc) {
145         j = fi->j;
146         fi->j = dx;
147     }
148     return j;
149 }
150
151 int rpmfiDIIndex(rpmfi fi, int dx)
152 {
153     int j = -1;
154     if (fi != NULL && dx >= 0 && dx < fi->fc) {
155         if (fi->dil != NULL)
156             j = fi->dil[dx];
157     }
158     return j;
159 }
160
161 const char * rpmfiBNIndex(rpmfi fi, int ix)
162 {
163     const char * BN = NULL;
164
165     if (fi != NULL && ix >= 0 && ix < fi->fc) {
166         if (fi->bnl != NULL)
167             BN = fi->bnl[ix];
168     }
169     return BN;
170 }
171
172 const char * rpmfiDNIndex(rpmfi fi, int jx)
173 {
174     const char * DN = NULL;
175
176     if (fi != NULL && jx >= 0 && jx < fi->dc) {
177         if (fi->dnl != NULL)
178             DN = fi->dnl[jx];
179     }
180     return DN;
181 }
182
183 char * rpmfiFNIndex(rpmfi fi, int ix)
184 {
185     char *fn = NULL;
186     if (fi != NULL && ix >= 0 && ix < fi->fc) {
187         fn = rstrscat(NULL, fi->dnl[fi->dil[ix]], fi->bnl[ix], NULL);
188     }
189     return fn;
190 }
191
192 rpmfileAttrs rpmfiFFlagsIndex(rpmfi fi, int ix)
193 {
194     rpmfileAttrs FFlags = 0;
195
196     if (fi != NULL && ix >= 0 && ix < fi->fc) {
197         if (fi->fflags != NULL)
198             FFlags = fi->fflags[ix];
199     }
200     return FFlags;
201 }
202
203 rpmVerifyAttrs rpmfiVFlagsIndex(rpmfi fi, int ix)
204 {
205     rpmVerifyAttrs VFlags = 0;
206
207     if (fi != NULL && ix >= 0 && ix < fi->fc) {
208         if (fi->vflags != NULL)
209             VFlags = fi->vflags[ix];
210     }
211     return VFlags;
212 }
213
214 rpm_mode_t rpmfiFModeIndex(rpmfi fi, int ix)
215 {
216     rpm_mode_t fmode = 0;
217
218     if (fi != NULL && ix >= 0 && ix < fi->fc) {
219         if (fi->fmodes != NULL)
220             fmode = fi->fmodes[ix];
221     }
222     return fmode;
223 }
224
225 rpmfileState rpmfiFStateIndex(rpmfi fi, int ix)
226 {
227     rpmfileState fstate = RPMFILE_STATE_MISSING;
228
229     if (fi != NULL && ix >= 0 && ix < fi->fc) {
230         if (fi->fstates != NULL)
231             fstate = fi->fstates[ix];
232     }
233     return fstate;
234 }
235
236 const unsigned char * rpmfiMD5(rpmfi fi)
237 {
238     const unsigned char *digest;
239     int algo = 0;
240
241     digest = rpmfiFDigest(fi, &algo, NULL);
242     return (algo == PGPHASHALGO_MD5) ? digest : NULL;
243 }
244
245 int rpmfiDigestAlgo(rpmfi fi)
246 {
247     return fi ? fi->digestalgo : 0;
248 }
249
250 const unsigned char * rpmfiFDigestIndex(rpmfi fi, int ix, int *algo, size_t *len)
251 {
252     const unsigned char *digest = NULL;
253
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);
258         if (len) 
259             *len = diglen;
260         if (algo) 
261             *algo = fi->digestalgo;
262     }
263     return digest;
264 }
265
266 char * rpmfiFDigestHex(rpmfi fi, int *algo)
267 {
268     size_t diglen = 0;
269     char *fdigest = NULL;
270     const unsigned char *digest = rpmfiFDigest(fi, algo, &diglen);
271     if (digest) {
272         fdigest = pgpHexStr(digest, diglen);
273     }
274     return fdigest;
275 }
276
277 const char * rpmfiFLinkIndex(rpmfi fi, int ix)
278 {
279     const char * flink = NULL;
280
281     if (fi != NULL && ix >= 0 && ix < fi->fc) {
282         if (fi->flinks != NULL)
283             flink = strcacheGet(fi->flinkcache, fi->flinks[ix]);
284     }
285     return flink;
286 }
287
288 rpm_loff_t rpmfiFSizeIndex(rpmfi fi, int ix)
289 {
290     rpm_loff_t fsize = 0;
291
292     if (fi != NULL && ix >= 0 && ix < fi->fc) {
293         if (fi->fsizes != NULL)
294             fsize = fi->fsizes[ix];
295     }
296     return fsize;
297 }
298
299 rpm_rdev_t rpmfiFRdevIndex(rpmfi fi, int ix)
300 {
301     rpm_rdev_t frdev = 0;
302
303     if (fi != NULL && ix >= 0 && ix < fi->fc) {
304         if (fi->frdevs != NULL)
305             frdev = fi->frdevs[ix];
306     }
307     return frdev;
308 }
309
310 rpm_ino_t rpmfiFInodeIndex(rpmfi fi, int ix)
311 {
312     rpm_ino_t finode = 0;
313
314     if (fi != NULL && ix >= 0 && ix < fi->fc) {
315         if (fi->finodes != NULL)
316             finode = fi->finodes[ix];
317     }
318     return finode;
319 }
320
321 rpm_color_t rpmfiColor(rpmfi fi)
322 {
323     rpm_color_t color = 0;
324
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. */
329         color &= 0xf;
330     }
331     return color;
332 }
333
334 rpm_color_t rpmfiFColorIndex(rpmfi fi, int ix)
335 {
336     rpm_color_t fcolor = 0;
337
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);
342     }
343     return fcolor;
344 }
345
346 const char * rpmfiFClassIndex(rpmfi fi, int ix)
347 {
348     const char * fclass = NULL;
349     int cdictx;
350
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];
355     }
356     return fclass;
357 }
358
359 uint32_t rpmfiFDependsIndex(rpmfi fi, int ix, const uint32_t ** fddictp)
360 {
361     int fddictx = -1;
362     int fddictn = 0;
363     const uint32_t * fddict = NULL;
364
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;
372     }
373     if (fddictp)
374         *fddictp = fddict;
375     return fddictn;
376 }
377
378 uint32_t rpmfiFNlinkIndex(rpmfi fi, int ix)
379 {
380     uint32_t nlink = 0;
381
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];
387             int j;
388
389             for (j = 0; j < fi->fc; j++) {
390                 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
391                     nlink++;
392             }
393         }
394     }
395     return nlink;
396 }
397
398 rpm_time_t rpmfiFMtimeIndex(rpmfi fi, int ix)
399 {
400     rpm_time_t fmtime = 0;
401
402     if (fi != NULL && ix >= 0 && ix < fi->fc) {
403         if (fi->fmtimes != NULL)
404             fmtime = fi->fmtimes[ix];
405     }
406     return fmtime;
407 }
408
409 const char * rpmfiFUserIndex(rpmfi fi, int ix)
410 {
411     const char * fuser = NULL;
412
413     if (fi != NULL && ix >= 0 && ix < fi->fc) {
414         if (fi->fuser != NULL)
415             fuser = strcacheGet(ugcache, fi->fuser[ix]);
416     }
417     return fuser;
418 }
419
420 const char * rpmfiFGroupIndex(rpmfi fi, int ix)
421 {
422     const char * fgroup = NULL;
423
424     if (fi != NULL && ix >= 0 && ix < fi->fc) {
425         if (fi->fgroup != NULL)
426             fgroup = strcacheGet(ugcache, fi->fgroup[ix]);
427     }
428     return fgroup;
429 }
430
431 const char * rpmfiFCapsIndex(rpmfi fi, int ix)
432 {
433     const char *fcaps = NULL;
434     if (fi != NULL && ix >= 0 && ix < fi->fc) {
435         fcaps = fi->fcapcache ? strcacheGet(fi->fcapcache, fi->fcaps[ix]) : "";
436     }
437     return fcaps;
438 }
439
440 const char * rpmfiFLangsIndex(rpmfi fi, int ix)
441 {
442     const char *flangs = NULL;
443     if (fi != NULL && fi->flangs != NULL && ix >= 0 && ix < fi->fc) {
444         flangs = strcacheGet(langcache, fi->flangs[ix]);
445     }
446     return flangs;
447 }
448
449 struct fingerPrint_s *rpmfiFpsIndex(rpmfi fi, int ix)
450 {
451     struct fingerPrint_s * fps = NULL;
452     if (fi != NULL && fi->fps != NULL && ix >= 0 && ix < fi->fc) {
453         fps = fi->fps + ix;
454     }
455     return fps;
456 }
457
458 int rpmfiNext(rpmfi fi)
459 {
460     int i = -1;
461
462     if (fi != NULL && ++fi->i >= 0) {
463         if (fi->i < fi->fc) {
464             i = fi->i;
465             if (fi->dil != NULL)
466                 fi->j = fi->dil[fi->i];
467         } else
468             fi->i = -1;
469     }
470
471     return i;
472 }
473
474 rpmfi rpmfiInit(rpmfi fi, int fx)
475 {
476     if (fi != NULL) {
477         if (fx >= 0 && fx < fi->fc) {
478             fi->i = fx - 1;
479             fi->j = -1;
480         }
481     }
482
483     return fi;
484 }
485
486 int rpmfiNextD(rpmfi fi)
487 {
488     int j = -1;
489
490     if (fi != NULL && ++fi->j >= 0) {
491         if (fi->j < fi->dc)
492             j = fi->j;
493         else
494             fi->j = -1;
495     }
496
497     return j;
498 }
499
500 rpmfi rpmfiInitD(rpmfi fi, int dx)
501 {
502     if (fi != NULL) {
503         if (dx >= 0 && dx < fi->fc)
504             fi->j = dx - 1;
505         else
506             fi = NULL;
507     }
508
509     return fi;
510 }
511
512 /**
513  * Identify a file type.
514  * @param ft            file type
515  * @return              string to identify a file type
516  */
517 static
518 const char * ftstring (rpmFileTypes ft)
519 {
520     switch (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";
529     }
530 }
531
532 rpmFileTypes rpmfiWhatis(rpm_mode_t mode)
533 {
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;
540     return REG;
541 }
542
543 int rpmfiCompareIndex(rpmfi afi, int aix, rpmfi bfi, int bix)
544 {
545     rpmFileTypes awhat = rpmfiWhatis(rpmfiFModeIndex(afi, aix));
546     rpmFileTypes bwhat = rpmfiWhatis(rpmfiFModeIndex(bfi, bix));
547
548     if ((rpmfiFFlagsIndex(afi, aix) & RPMFILE_GHOST) ||
549         (rpmfiFFlagsIndex(bfi, bix) & RPMFILE_GHOST)) return 0;
550
551     if (awhat != bwhat) return 1;
552
553     if (awhat == LINK) {
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;
562         int aalgo, balgo;
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))
574             return 1;
575     }
576
577     return 0;
578 }
579
580 rpmFileAction rpmfiDecideFateIndex(rpmfi ofi, int oix, rpmfi nfi, int nix,
581                                    int skipMissing)
582 {
583     char * fn = rpmfiFNIndex(nfi, nix);
584     rpmfileAttrs newFlags = rpmfiFFlagsIndex(nfi, nix);
585     char buffer[1024];
586     rpmFileTypes dbWhat, newWhat, diskWhat;
587     struct stat sb;
588     int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
589     int action = FA_CREATE; /* assume we can create */
590
591     if (lstat(fn, &sb)) {
592         /*
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.
595          */
596         if (skipMissing && (newFlags & RPMFILE_MISSINGOK)) {
597             rpmlog(RPMLOG_DEBUG, "%s skipped due to missingok flag\n",
598                         fn);
599             action = FA_SKIP;
600         }
601         goto exit;
602     }
603
604     diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
605     dbWhat = rpmfiWhatis(rpmfiFModeIndex(ofi, oix));
606     newWhat = rpmfiWhatis(rpmfiFModeIndex(nfi, nix));
607
608     /*
609      * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
610      * them in older packages as well.
611      */
612     if (newWhat == XDIR)
613         goto exit;
614
615     if (diskWhat != newWhat && dbWhat != REG && dbWhat != LINK) {
616         action = save;
617         goto exit;
618     } else if (newWhat != dbWhat && diskWhat != dbWhat) {
619         action = save;
620         goto exit;
621     } else if (dbWhat != newWhat) {
622         goto exit;
623     } else if (dbWhat != LINK && dbWhat != REG) {
624         goto exit;
625     }
626
627     /*
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.
630      */
631     memset(buffer, 0, sizeof(buffer));
632     if (dbWhat == REG) {
633         int oalgo, nalgo;
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. */
642         }
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) {
646             action = save;
647             goto exit;
648         }
649         if (odigest && ndigest && !memcmp(odigest, ndigest, odiglen)) {
650             action = FA_SKIP;   /* identical file, don't bother. */
651             goto exit;
652         }
653         /* ... but otherwise backup will be needed */
654         action = save;
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);
660             if (link_len == -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. */
665         }
666         nFLink = rpmfiFLinkIndex(nfi, nix);
667         if (oFLink && nFLink && rstreq(oFLink, nFLink)) {
668             action = FA_SKIP;   /* identical file, don't bother. */
669             goto exit;
670         }
671         /* ... but otherwise backup will be needed */
672         action = save;
673     }
674
675 exit:
676     free(fn);
677     return action;
678 }
679
680 int rpmfiConfigConflictIndex(rpmfi fi, int ix)
681 {
682     char * fn = NULL;
683     rpmfileAttrs flags = rpmfiFFlagsIndex(fi, ix);
684     char buffer[1024];
685     rpmFileTypes newWhat, diskWhat;
686     struct stat sb;
687     int rc = 0;
688
689     if (!(flags & RPMFILE_CONFIG))
690         return 0;
691
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)
696         return 1;
697
698     /* If it's not on disk, there's nothing to be saved */
699     fn = rpmfiFNIndex(fi, ix);
700     if (lstat(fn, &sb))
701         goto exit;
702
703     /* Files of different types obviously are not identical */
704     diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
705     if (diskWhat != newWhat) {
706         rc = 1;
707         goto exit;
708     }
709     
710     memset(buffer, 0, sizeof(buffer));
711     if (newWhat == REG) {
712         int algo;
713         size_t diglen;
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 */ {
720         const char * nFLink;
721         ssize_t link_len = readlink(fn, buffer, sizeof(buffer) - 1);
722         if (link_len == -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 */
728     }
729
730     rc = 1;
731
732 exit:
733     free(fn);
734     return rc;
735 }
736
737 static char **duparray(char ** src, int size)
738 {
739     char **dest = xmalloc((size+1) * sizeof(*dest));
740     for (int i = 0; i < size; i++) {
741         dest[i] = xstrdup(src[i]);
742     }
743     free(src);
744     return dest;
745 }
746
747 static int addPrefixes(Header h, rpmRelocation *relocations, int numRelocations)
748 {
749     struct rpmtd_s validRelocs;
750     const char *validprefix;
751     const char ** actualRelocations;
752     int numActual = 0;
753
754     headerGet(h, RPMTAG_PREFIXES, &validRelocs, HEADERGET_MINMEM);
755     /*
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.
761      */
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);
767             }
768             rpmtdFreeData(&validRelocs);
769         }
770         return 0;
771     }
772
773     actualRelocations = xmalloc(rpmtdCount(&validRelocs) * sizeof(*actualRelocations));
774     rpmtdInit(&validRelocs);
775     while ((validprefix = rpmtdNextString(&validRelocs))) {
776         int j;
777         for (j = 0; j < numRelocations; j++) {
778             if (relocations[j].oldPath == NULL || /* XXX can't happen */
779                 !rstreq(validprefix, relocations[j].oldPath))
780                 continue;
781             /* On install, a relocate to NULL means skip the path. */
782             if (relocations[j].newPath) {
783                 actualRelocations[numActual] = relocations[j].newPath;
784                 numActual++;
785             }
786             break;
787         }
788         if (j == numRelocations) {
789             actualRelocations[numActual] = validprefix;
790             numActual++;
791         }
792     }
793     rpmtdFreeData(&validRelocs);
794
795     if (numActual) {
796         headerPutStringArray(h, RPMTAG_INSTPREFIXES, actualRelocations, numActual);
797     }
798     free(actualRelocations);
799     return numActual;
800 }
801
802 static void saveRelocs(Header h, rpmtd bnames, rpmtd dnames, rpmtd dindexes)
803 {
804         struct rpmtd_s td;
805         headerGet(h, RPMTAG_BASENAMES, &td, HEADERGET_MINMEM);
806         rpmtdSetTag(&td, RPMTAG_ORIGBASENAMES);
807         headerPut(h, &td, HEADERPUT_DEFAULT);
808         rpmtdFreeData(&td);
809
810         headerGet(h, RPMTAG_DIRNAMES, &td, HEADERGET_MINMEM);
811         rpmtdSetTag(&td, RPMTAG_ORIGDIRNAMES);
812         headerPut(h, &td, HEADERPUT_DEFAULT);
813         rpmtdFreeData(&td);
814
815         headerGet(h, RPMTAG_DIRINDEXES, &td, HEADERGET_MINMEM);
816         rpmtdSetTag(&td, RPMTAG_ORIGDIRINDEXES);
817         headerPut(h, &td, HEADERPUT_DEFAULT);
818         rpmtdFreeData(&td);
819
820         headerMod(h, bnames);
821         headerMod(h, dnames);
822         headerMod(h, dindexes);
823 }
824
825 void rpmRelocateFileList(rpmRelocation *relocations, int numRelocations, 
826                          rpmfs fs, Header h)
827 {
828     static int _printed = 0;
829     char ** baseNames;
830     char ** dirNames;
831     uint32_t * dirIndexes;
832     rpm_count_t fileCount, dirCount;
833     int nrelocated = 0;
834     int fileAlloced = 0;
835     char * fn = NULL;
836     int haveRelocatedBase = 0;
837     size_t maxlen = 0;
838     int i, j;
839     struct rpmtd_s bnames, dnames, dindexes, fmodes;
840
841     addPrefixes(h, relocations, numRelocations);
842
843     if (!_printed) {
844         _printed = 1;
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);
851             else
852                 rpmlog(RPMLOG_DEBUG, "%5d relocate %s -> %s\n",
853                         i, relocations[i].oldPath, relocations[i].newPath);
854         }
855     }
856
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;
861     }
862
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;
875
876     /*
877      * For all relocations, we go through sorted file/relocation lists 
878      * backwards so that /usr/local relocations take precedence over /usr 
879      * ones.
880      */
881
882     /* Relocate individual paths. */
883
884     for (i = fileCount - 1; i >= 0; i--) {
885         rpmFileTypes ft;
886         int fnlen;
887
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);
893         }
894
895 assert(fn != NULL);             /* XXX can't happen */
896         *fn = '\0';
897         fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
898
899         /*
900          * See if this file path needs relocating.
901          */
902         /*
903          * XXX FIXME: Would a bsearch of the (already sorted) 
904          * relocation list be a good idea?
905          */
906         for (j = numRelocations - 1; j >= 0; j--) {
907             if (relocations[j].oldPath == NULL) /* XXX can't happen */
908                 continue;
909             len = !rstreq(relocations[j].oldPath, "/")
910                 ? strlen(relocations[j].oldPath)
911                 : 0;
912
913             if (fnlen < len)
914                 continue;
915             /*
916              * Only subdirectories or complete file paths may be relocated. We
917              * don't check for '\0' as our directory names all end in '/'.
918              */
919             if (!(fn[len] == '/' || fnlen == len))
920                 continue;
921
922             if (!rstreqn(relocations[j].oldPath, fn, len))
923                 continue;
924             break;
925         }
926         if (j < 0) continue;
927
928         rpmtdSetIndex(&fmodes, i);
929         ft = rpmfiWhatis(rpmtdGetNumber(&fmodes));
930
931         /* On install, a relocate to NULL means skip the path. */
932         if (relocations[j].newPath == NULL) {
933             if (ft == XDIR) {
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--;
938                     if (fnlen != len)
939                         continue;
940                     if (!rstreqn(fn, dirNames[j], fnlen))
941                         continue;
942                     break;
943                 }
944             }
945             rpmfsSetAction(fs, i, FA_SKIPNSTATE);
946             rpmlog(RPMLOG_DEBUG, "excluding %s %s\n",
947                    ftstring(ft), fn);
948             continue;
949         }
950
951         /* Relocation on full paths only, please. */
952         if (fnlen != len) continue;
953
954         rpmlog(RPMLOG_DEBUG, "relocating %s to %s\n",
955                fn, relocations[j].newPath);
956         nrelocated++;
957
958         strcpy(fn, relocations[j].newPath);
959         {   char * te = strrchr(fn, '/');
960             if (te) {
961                 if (te > fn) te++;      /* root is special */
962                 fnlen = te - fn;
963             } else
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;
971                 }
972                 free(baseNames[i]);
973                 baseNames[i] = xstrdup(te);
974             }
975             *te = '\0';                 /* terminate new directory name */
976         }
977
978         /* Does this directory already exist in the directory list? */
979         for (j = 0; j < dirCount; j++) {
980             if (fnlen != strlen(dirNames[j]))
981                 continue;
982             if (!rstreqn(fn, dirNames[j], fnlen))
983                 continue;
984             break;
985         }
986         
987         if (j < dirCount) {
988             dirIndexes[i] = j;
989             continue;
990         }
991
992         /* Creating new paths is a pita */
993         dirNames = dnames.data = xrealloc(dnames.data, 
994                                sizeof(*dirNames) * (dirCount + 1));
995
996         dirNames[dirCount] = xstrdup(fn);
997         dirIndexes[i] = dirCount;
998         dirCount++;
999         dnames.count++;
1000     }
1001
1002     /* Finish off by relocating directories. */
1003     for (i = dirCount - 1; i >= 0; i--) {
1004         for (j = numRelocations - 1; j >= 0; j--) {
1005
1006             if (relocations[j].oldPath == NULL) /* XXX can't happen */
1007                 continue;
1008             size_t len = !rstreq(relocations[j].oldPath, "/")
1009                 ? strlen(relocations[j].oldPath)
1010                 : 0;
1011
1012             if (len && !rstreqn(relocations[j].oldPath, dirNames[i], len))
1013                 continue;
1014
1015             /*
1016              * Only subdirectories or complete file paths may be relocated. We
1017              * don't check for '\0' as our directory names all end in '/'.
1018              */
1019             if (dirNames[i][len] != '/')
1020                 continue;
1021
1022             if (relocations[j].newPath) { /* Relocate the path */
1023                 char *t = NULL;
1024                 rstrscat(&t, relocations[j].newPath, (dirNames[i] + len), NULL);
1025                 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
1026                 (void) rpmCleanPath(t);
1027                 rstrcat(&t, "/");
1028
1029                 rpmlog(RPMLOG_DEBUG,
1030                        "relocating directory %s to %s\n", dirNames[i], t);
1031                 free(dirNames[i]);
1032                 dirNames[i] = t;
1033                 nrelocated++;
1034             }
1035         }
1036     }
1037
1038     /* Save original filenames in header and replace (relocated) filenames. */
1039     if (nrelocated) {
1040         saveRelocs(h, &bnames, &dnames, &dindexes);
1041     }
1042
1043     rpmtdFreeData(&bnames);
1044     rpmtdFreeData(&dnames);
1045     rpmtdFreeData(&dindexes);
1046     rpmtdFreeData(&fmodes);
1047     free(fn);
1048 }
1049
1050 rpmfi rpmfiFree(rpmfi fi)
1051 {
1052     if (fi == NULL) return NULL;
1053
1054     if (fi->nrefs > 1)
1055         return rpmfiUnlink(fi);
1056
1057     if (fi->fc > 0) {
1058         fi->bnl = _free(fi->bnl);
1059         fi->dnl = _free(fi->dnl);
1060
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);
1067
1068         fi->cdict = _free(fi->cdict);
1069
1070         fi->fuser = _free(fi->fuser);
1071         fi->fgroup = _free(fi->fgroup);
1072
1073         fi->fstates = _free(fi->fstates);
1074         fi->fps = _free(fi->fps);
1075
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);
1086
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);
1092
1093         }
1094     }
1095
1096     fi->fn = _free(fi->fn);
1097     fi->apath = _free(fi->apath);
1098
1099     fi->replacedSizes = _free(fi->replacedSizes);
1100
1101     fi->h = headerFree(fi->h);
1102
1103     (void) rpmfiUnlink(fi);
1104     memset(fi, 0, sizeof(*fi));         /* XXX trash and burn */
1105     fi = _free(fi);
1106
1107     return NULL;
1108 }
1109
1110 /* Helper to push header tag data into a string cache */
1111 static scidx_t *cacheTag(strcache cache, Header h, rpmTag tag)
1112 {
1113     scidx_t *idx = NULL;
1114     struct rpmtd_s td;
1115     if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1116        idx = xmalloc(sizeof(*idx) * rpmtdCount(&td));
1117        int i = 0;
1118        const char *str;
1119        while ((str = rpmtdNextString(&td))) {
1120            idx[i++] = strcachePut(cache, str);
1121        }
1122        rpmtdFreeData(&td);
1123     }
1124     return idx;
1125 }
1126
1127 #define _hgfi(_h, _tag, _td, _flags, _data) \
1128     if (headerGet((_h), (_tag), (_td), (_flags))) \
1129         _data = (td.data)
1130
1131 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTagVal tagN, rpmfiFlags flags)
1132 {
1133     rpmfi fi = xcalloc(1, sizeof(*fi)); 
1134     unsigned char * t;
1135     struct rpmtd_s fdigests, digalgo;
1136     struct rpmtd_s td;
1137     headerGetFlags scareFlags = (flags & RPMFI_KEEPHEADER) ? 
1138                                 HEADERGET_MINMEM : HEADERGET_ALLOC;
1139     headerGetFlags defFlags = HEADERGET_ALLOC;
1140
1141     fi->magic = RPMFIMAGIC;
1142     fi->i = -1;
1143
1144     fi->fiflags = flags;
1145
1146     _hgfi(h, RPMTAG_BASENAMES, &td, defFlags, fi->bnl);
1147     fi->fc = rpmtdCount(&td);
1148     if (fi->fc == 0) {
1149         goto exit;
1150     }
1151
1152     _hgfi(h, RPMTAG_DIRNAMES, &td, defFlags, fi->dnl);
1153     fi->dc = rpmtdCount(&td);
1154     _hgfi(h, RPMTAG_DIRINDEXES, &td, scareFlags, fi->dil);
1155
1156     /* Is our filename triplet sane? */
1157     if (fi->dc == 0 || fi->dc > fi->fc || rpmtdCount(&td) != fi->fc)
1158         goto errxit;
1159
1160     for (rpm_count_t i = 0; i < fi->fc; i++) {
1161         if (fi->dil[i] >= fi->fc)
1162             goto errxit;
1163     }
1164
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);
1174
1175     if (!(flags & RPMFI_NOFILECOLORS))
1176         _hgfi(h, RPMTAG_FILECOLORS, &td, scareFlags, fi->fcolors);
1177
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);
1182     }
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);
1188     }
1189
1190     if (!(flags & RPMFI_NOFILESTATES))
1191         _hgfi(h, RPMTAG_FILESTATES, &td, defFlags, fi->fstates);
1192
1193     if (!(flags & RPMFI_NOFILECAPS) && headerIsEntry(h, RPMTAG_FILECAPS)) {
1194         fi->fcapcache = strcacheNew();
1195         fi->fcaps = cacheTag(fi->fcapcache, h, RPMTAG_FILECAPS);
1196     }
1197
1198     if (!(flags & RPMFI_NOFILELINKTOS)) {
1199         fi->flinkcache = strcacheNew();
1200         fi->flinks = cacheTag(fi->flinkcache, h, RPMTAG_FILELINKTOS);
1201     }
1202     /* FILELANGS are only interesting when installing */
1203     if ((headerGetInstance(h) == 0) && !(flags & RPMFI_NOFILELANGS))
1204         fi->flangs = cacheTag(langcache, h, RPMTAG_FILELANGS);
1205
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;
1213         }
1214     }
1215
1216     fi->digests = NULL;
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);
1223
1224         while ((fdigest = rpmtdNextString(&fdigests))) {
1225             if (!(fdigest && *fdigest != '\0')) {
1226                 memset(t, 0, diglen);
1227                 t += diglen;
1228                 continue;
1229             }
1230             for (int j = 0; j < diglen; j++, t++, fdigest += 2)
1231                 *t = (rnibble(fdigest[0]) << 4) | rnibble(fdigest[1]);
1232         }
1233         rpmtdFreeData(&fdigests);
1234     }
1235
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);
1243
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);
1248
1249     /* lazily alloced from rpmfiFN() */
1250     fi->fn = NULL;
1251
1252 exit:
1253
1254     if (fi != NULL) {
1255         fi->h = (fi->fiflags & RPMFI_KEEPHEADER) ? headerLink(h) : NULL;
1256     }
1257
1258     /* FIX: rpmfi null annotations */
1259     return rpmfiLink(fi);
1260
1261 errxit:
1262     rpmfiFree(fi);
1263     return NULL;
1264 }
1265
1266 void rpmfiSetFReplacedSizeIndex(rpmfi fi, int ix, rpm_loff_t newsize)
1267 {
1268     if (fi != NULL && ix >= 0 && ix < fi->fc) {
1269         if (fi->replacedSizes == NULL) {
1270             fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1271         }
1272         /* XXX watch out, replacedSizes is not rpm_loff_t (yet) */
1273         fi->replacedSizes[ix] = (rpm_off_t) newsize;
1274     }
1275 }
1276
1277 rpm_loff_t rpmfiFReplacedSizeIndex(rpmfi fi, int ix)
1278 {
1279     rpm_loff_t rsize = 0;
1280     if (fi != NULL && ix >= 0 && ix < fi->fc) {
1281         if (fi->replacedSizes) {
1282             rsize = fi->replacedSizes[ix];
1283         }
1284     }
1285     return rsize;
1286 }
1287
1288 void rpmfiFpLookup(rpmfi fi, fingerPrintCache fpc)
1289 {
1290     if (fi->fc > 0 && fi->fps == NULL) {
1291         fi->fps = xcalloc(fi->fc, sizeof(*fi->fps));
1292     }
1293     fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1294 }
1295
1296 /* 
1297  * Generate iterator accessors function wrappers, these do nothing but
1298  * call the corresponding rpmfiFooIndex(fi, fi->[ij])
1299  */
1300
1301 #define RPMFI_ITERFUNC(TYPE, NAME, IXV) \
1302     TYPE rpmfi ## NAME(rpmfi fi) { return rpmfi ## NAME ## Index(fi, fi ? fi->IXV : -1); }
1303
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)
1322
1323 const char * rpmfiFN(rpmfi fi)
1324 {
1325     const char *fn = ""; /* preserve behavior on errors */
1326     if (fi != NULL) {
1327         free(fi->fn);
1328         fi->fn = rpmfiFNIndex(fi, fi->i);
1329         if (fi->fn != NULL)
1330             fn = fi->fn;
1331     }
1332     return fn;
1333 }
1334
1335 const unsigned char * rpmfiFDigest(rpmfi fi, int *algo, size_t *len)
1336 {
1337     return rpmfiFDigestIndex(fi, fi ? fi->i : -1, algo, len);
1338 }
1339
1340 uint32_t rpmfiFDepends(rpmfi fi, const uint32_t ** fddictp)
1341 {
1342     return rpmfiFDependsIndex(fi,  fi ? fi->i : -1, fddictp);
1343 }
1344
1345 int rpmfiCompare(const rpmfi afi, const rpmfi bfi)
1346 {
1347     return rpmfiCompareIndex(afi, afi ? afi->i : -1, bfi, bfi ? bfi->i : -1);
1348 }
1349
1350 rpmFileAction rpmfiDecideFate(const rpmfi ofi, rpmfi nfi, int skipMissing)
1351 {
1352     return rpmfiDecideFateIndex(ofi, ofi ? ofi->i : -1,
1353                                 nfi, nfi ? nfi->i : -1,
1354                                 skipMissing);
1355 }
1356
1357 int rpmfiConfigConflict(const rpmfi fi)
1358 {
1359     return rpmfiConfigConflictIndex(fi, fi ? fi->i : -1);
1360 }