Sanitize python object -> tag number exception handling
[platform/upstream/rpm.git] / 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 #include "lib/fsm.h"    /* XXX newFSM() */
19
20 #include "debug.h"
21
22 /*
23  * Simple and stupid string "cache."
24  * Store each unique string just once, retrieve by index value. 
25  * For data where number of unique names is typically very low,
26  * the dumb linear lookup appears to be fast enough and hash table seems
27  * like an overkill.
28  */
29 struct strcache_s {
30     char **uniq;
31     scidx_t num;
32 };
33
34 static struct strcache_s _ugcache = { NULL, 0 };
35 static strcache ugcache = &_ugcache;
36 static struct strcache_s _langcache = { NULL, 0 };
37 static strcache langcache = &_langcache;
38
39 static scidx_t strcachePut(strcache cache, const char *str)
40 {
41     int found = 0;
42     scidx_t ret;
43
44     for (scidx_t i = 0; i < cache->num; i++) {
45         if (rstreq(str, cache->uniq[i])) {
46             ret = i;
47             found = 1;
48             break;
49         }
50     }
51     if (!found) {
52         /* blow up on index wraparound */
53         assert((scidx_t)(cache->num + 1) > cache->num);
54         cache->uniq = xrealloc(cache->uniq, 
55                                 sizeof(cache->uniq) * (cache->num+1));
56         cache->uniq[cache->num] = xstrdup(str);
57         ret = cache->num;
58         cache->num++;
59     }
60     return ret;
61 }
62
63 static const char *strcacheGet(strcache cache, scidx_t idx)
64 {
65     const char *name = NULL;
66     if (idx >= 0 && idx < cache->num && cache->uniq != NULL)
67         name = cache->uniq[idx];
68     return name;
69 }
70     
71 static strcache strcacheNew(void)
72 {
73     strcache cache = xcalloc(1, sizeof(*cache));
74     return cache;
75 }
76
77 static strcache strcacheFree(strcache cache)
78 {
79     if (cache != NULL) {
80         for (scidx_t i = 0; i < cache->num; i++) {
81             free(cache->uniq[i]);
82         }
83         cache->uniq = _free(cache->uniq);
84         free(cache);
85     }
86     return NULL;
87
88
89
90 int _rpmfi_debug = 0;
91
92 rpmfi rpmfiUnlink(rpmfi fi, const char * msg)
93 {
94     if (fi == NULL) return NULL;
95 if (_rpmfi_debug && msg != NULL)
96 fprintf(stderr, "--> fi %p -- %d %s\n", fi, fi->nrefs, msg);
97     fi->nrefs--;
98     return NULL;
99 }
100
101 rpmfi rpmfiLink(rpmfi fi, const char * msg)
102 {
103     if (fi == NULL) return NULL;
104     fi->nrefs++;
105 if (_rpmfi_debug && msg != NULL)
106 fprintf(stderr, "--> fi %p ++ %d %s\n", fi, fi->nrefs, msg);
107     return fi;
108 }
109
110 rpm_count_t rpmfiFC(rpmfi fi)
111 {
112     return (fi != NULL ? fi->fc : 0);
113 }
114
115 rpm_count_t rpmfiDC(rpmfi fi)
116 {
117     return (fi != NULL ? fi->dc : 0);
118 }
119
120 #ifdef  NOTYET
121 int rpmfiDI(rpmfi fi)
122 {
123 }
124 #endif
125
126 int rpmfiFX(rpmfi fi)
127 {
128     return (fi != NULL ? fi->i : -1);
129 }
130
131 int rpmfiSetFX(rpmfi fi, int fx)
132 {
133     int i = -1;
134
135     if (fi != NULL && fx >= 0 && fx < fi->fc) {
136         i = fi->i;
137         fi->i = fx;
138         fi->j = fi->dil[fi->i];
139     }
140     return i;
141 }
142
143 int rpmfiDX(rpmfi fi)
144 {
145     return (fi != NULL ? fi->j : -1);
146 }
147
148 int rpmfiSetDX(rpmfi fi, int dx)
149 {
150     int j = -1;
151
152     if (fi != NULL && dx >= 0 && dx < fi->dc) {
153         j = fi->j;
154         fi->j = dx;
155     }
156     return j;
157 }
158
159 int rpmfiDIIndex(rpmfi fi, int dx)
160 {
161     int j = -1;
162     if (fi != NULL && dx >= 0 && dx < fi->fc) {
163         if (fi->dil != NULL)
164             j = fi->dil[dx];
165     }
166     return j;
167 }
168
169 const char * rpmfiBNIndex(rpmfi fi, int ix)
170 {
171     const char * BN = NULL;
172
173     if (fi != NULL && ix >= 0 && ix < fi->fc) {
174         if (fi->bnl != NULL)
175             BN = fi->bnl[ix];
176     }
177     return BN;
178 }
179
180 const char * rpmfiDNIndex(rpmfi fi, int jx)
181 {
182     const char * DN = NULL;
183
184     if (fi != NULL && jx >= 0 && jx < fi->dc) {
185         if (fi->dnl != NULL)
186             DN = fi->dnl[jx];
187     }
188     return DN;
189 }
190
191 const char * rpmfiFNIndex(rpmfi fi, int ix)
192 {
193     const char * FN = "";
194
195     if (fi != NULL && ix >= 0 && ix < fi->fc) {
196         char * t;
197         if (fi->fn == NULL) {
198             size_t dnlmax = 0, bnlmax = 0, len;
199             for (int i = 0; i < fi->dc; i++) {
200                 if ((len = strlen(fi->dnl[i])) > dnlmax)
201                     dnlmax = len;
202             }
203             for (int i = 0; i < fi->fc; i++) {
204                 if ((len = strlen(fi->bnl[i])) > bnlmax)
205                     bnlmax = len;
206             }
207             fi->fn = xmalloc(dnlmax + bnlmax + 1);
208         }
209         FN = t = fi->fn;
210         *t = '\0';
211         t = stpcpy(t, fi->dnl[fi->dil[ix]]);
212         t = stpcpy(t, fi->bnl[ix]);
213     }
214     return FN;
215 }
216
217 rpmfileAttrs rpmfiFFlagsIndex(rpmfi fi, int ix)
218 {
219     rpmfileAttrs FFlags = 0;
220
221     if (fi != NULL && ix >= 0 && ix < fi->fc) {
222         if (fi->fflags != NULL)
223             FFlags = fi->fflags[ix];
224     }
225     return FFlags;
226 }
227
228 rpmVerifyAttrs rpmfiVFlagsIndex(rpmfi fi, int ix)
229 {
230     rpmVerifyAttrs VFlags = 0;
231
232     if (fi != NULL && ix >= 0 && ix < fi->fc) {
233         if (fi->vflags != NULL)
234             VFlags = fi->vflags[ix];
235     }
236     return VFlags;
237 }
238
239 rpm_mode_t rpmfiFModeIndex(rpmfi fi, int ix)
240 {
241     rpm_mode_t fmode = 0;
242
243     if (fi != NULL && ix >= 0 && ix < fi->fc) {
244         if (fi->fmodes != NULL)
245             fmode = fi->fmodes[ix];
246     }
247     return fmode;
248 }
249
250 rpmfileState rpmfiFStateIndex(rpmfi fi, int ix)
251 {
252     rpmfileState fstate = RPMFILE_STATE_MISSING;
253
254     if (fi != NULL && ix >= 0 && ix < fi->fc) {
255         if (fi->fstates != NULL)
256             fstate = fi->fstates[ix];
257     }
258     return fstate;
259 }
260
261 const unsigned char * rpmfiMD5(rpmfi fi)
262 {
263     const unsigned char *digest;
264     pgpHashAlgo algo = 0;
265
266     digest = rpmfiFDigest(fi, &algo, NULL);
267     return (algo == PGPHASHALGO_MD5) ? digest : NULL;
268 }
269
270 pgpHashAlgo rpmfiDigestAlgo(rpmfi fi)
271 {
272     return fi ? fi->digestalgo : 0;
273 }
274
275 const unsigned char * rpmfiFDigestIndex(rpmfi fi, int ix, pgpHashAlgo *algo, size_t *len)
276 {
277     const unsigned char *digest = NULL;
278
279     if (fi != NULL && ix >= 0 && ix < fi->fc) {
280         size_t diglen = rpmDigestLength(fi->digestalgo);
281         if (fi->digests != NULL)
282             digest = fi->digests + (diglen * ix);
283         if (len) 
284             *len = diglen;
285         if (algo) 
286             *algo = fi->digestalgo;
287     }
288     return digest;
289 }
290
291 char * rpmfiFDigestHex(rpmfi fi, pgpHashAlgo *algo)
292 {
293     size_t diglen = 0;
294     char *fdigest = NULL;
295     const unsigned char *digest = rpmfiFDigest(fi, algo, &diglen);
296     if (digest) {
297         fdigest = pgpHexStr(digest, diglen);
298     }
299     return fdigest;
300 }
301
302 const char * rpmfiFLinkIndex(rpmfi fi, int ix)
303 {
304     const char * flink = NULL;
305
306     if (fi != NULL && ix >= 0 && ix < fi->fc) {
307         if (fi->flinks != NULL)
308             flink = strcacheGet(fi->flinkcache, fi->flinks[ix]);
309     }
310     return flink;
311 }
312
313 rpm_loff_t rpmfiFSizeIndex(rpmfi fi, int ix)
314 {
315     rpm_loff_t fsize = 0;
316
317     if (fi != NULL && ix >= 0 && ix < fi->fc) {
318         if (fi->fsizes != NULL)
319             fsize = fi->fsizes[ix];
320     }
321     return fsize;
322 }
323
324 rpm_rdev_t rpmfiFRdevIndex(rpmfi fi, int ix)
325 {
326     rpm_rdev_t frdev = 0;
327
328     if (fi != NULL && ix >= 0 && ix < fi->fc) {
329         if (fi->frdevs != NULL)
330             frdev = fi->frdevs[ix];
331     }
332     return frdev;
333 }
334
335 rpm_ino_t rpmfiFInodeIndex(rpmfi fi, int ix)
336 {
337     rpm_ino_t finode = 0;
338
339     if (fi != NULL && ix >= 0 && ix < fi->fc) {
340         if (fi->finodes != NULL)
341             finode = fi->finodes[ix];
342     }
343     return finode;
344 }
345
346 rpm_color_t rpmfiColor(rpmfi fi)
347 {
348     rpm_color_t color = 0;
349
350     if (fi != NULL && fi->fcolors != NULL) {
351         for (int i = 0; i < fi->fc; i++)
352             color |= fi->fcolors[i];
353         /* XXX ignore all but lsnibble for now. */
354         color &= 0xf;
355     }
356     return color;
357 }
358
359 rpm_color_t rpmfiFColorIndex(rpmfi fi, int ix)
360 {
361     rpm_color_t fcolor = 0;
362
363     if (fi != NULL && ix >= 0 && ix < fi->fc) {
364         if (fi->fcolors != NULL)
365             /* XXX ignore all but lsnibble for now. */
366             fcolor = (fi->fcolors[ix] & 0x0f);
367     }
368     return fcolor;
369 }
370
371 const char * rpmfiFClassIndex(rpmfi fi, int ix)
372 {
373     const char * fclass = NULL;
374     int cdictx;
375
376     if (fi != NULL && fi->fcdictx != NULL && ix >= 0 && ix < fi->fc) {
377         cdictx = fi->fcdictx[ix];
378         if (fi->cdict != NULL && cdictx >= 0 && cdictx < fi->ncdict)
379             fclass = fi->cdict[cdictx];
380     }
381     return fclass;
382 }
383
384 uint32_t rpmfiFDependsIndex(rpmfi fi, int ix, const uint32_t ** fddictp)
385 {
386     int fddictx = -1;
387     int fddictn = 0;
388     const uint32_t * fddict = NULL;
389
390     if (fi != NULL && ix >= 0 && ix < fi->fc) {
391         if (fi->fddictn != NULL)
392             fddictn = fi->fddictn[ix];
393         if (fddictn > 0 && fi->fddictx != NULL)
394             fddictx = fi->fddictx[ix];
395         if (fi->ddict != NULL && fddictx >= 0 && (fddictx+fddictn) <= fi->nddict)
396             fddict = fi->ddict + fddictx;
397     }
398     if (fddictp)
399         *fddictp = fddict;
400     return fddictn;
401 }
402
403 uint32_t rpmfiFNlinkIndex(rpmfi fi, int ix)
404 {
405     uint32_t nlink = 0;
406
407     if (fi != NULL && ix >= 0 && ix < fi->fc) {
408         /* XXX rpm-2.3.12 has not RPMTAG_FILEINODES */
409         if (fi->finodes && fi->frdevs) {
410             rpm_ino_t finode = fi->finodes[ix];
411             rpm_rdev_t frdev = fi->frdevs[ix];
412             int j;
413
414             for (j = 0; j < fi->fc; j++) {
415                 if (fi->frdevs[j] == frdev && fi->finodes[j] == finode)
416                     nlink++;
417             }
418         }
419     }
420     return nlink;
421 }
422
423 rpm_time_t rpmfiFMtimeIndex(rpmfi fi, int ix)
424 {
425     rpm_time_t fmtime = 0;
426
427     if (fi != NULL && ix >= 0 && ix < fi->fc) {
428         if (fi->fmtimes != NULL)
429             fmtime = fi->fmtimes[ix];
430     }
431     return fmtime;
432 }
433
434 const char * rpmfiFUserIndex(rpmfi fi, int ix)
435 {
436     const char * fuser = NULL;
437
438     if (fi != NULL && ix >= 0 && ix < fi->fc) {
439         if (fi->fuser != NULL)
440             fuser = strcacheGet(ugcache, fi->fuser[ix]);
441     }
442     return fuser;
443 }
444
445 const char * rpmfiFGroupIndex(rpmfi fi, int ix)
446 {
447     const char * fgroup = NULL;
448
449     if (fi != NULL && ix >= 0 && ix < fi->fc) {
450         if (fi->fgroup != NULL)
451             fgroup = strcacheGet(ugcache, fi->fgroup[ix]);
452     }
453     return fgroup;
454 }
455
456 const char * rpmfiFCapsIndex(rpmfi fi, int ix)
457 {
458     const char *fcaps = NULL;
459     if (fi != NULL && ix >= 0 && ix < fi->fc) {
460         fcaps = fi->fcapcache ? strcacheGet(fi->fcapcache, fi->fcaps[ix]) : "";
461     }
462     return fcaps;
463 }
464
465 const char * rpmfiFLangsIndex(rpmfi fi, int ix)
466 {
467     const char *flangs = NULL;
468     if (fi != NULL && fi->flangs != NULL && ix >= 0 && ix < fi->fc) {
469         flangs = strcacheGet(langcache, fi->flangs[ix]);
470     }
471     return flangs;
472 }
473
474 struct fingerPrint_s *rpmfiFpsIndex(rpmfi fi, int ix)
475 {
476     struct fingerPrint_s * fps = NULL;
477     if (fi != NULL && fi->fps != NULL && ix >= 0 && ix < fi->fc) {
478         fps = fi->fps + ix;
479     }
480     return fps;
481 }
482
483 int rpmfiNext(rpmfi fi)
484 {
485     int i = -1;
486
487     if (fi != NULL && ++fi->i >= 0) {
488         if (fi->i < fi->fc) {
489             i = fi->i;
490             if (fi->dil != NULL)
491                 fi->j = fi->dil[fi->i];
492         } else
493             fi->i = -1;
494
495 if (_rpmfi_debug  < 0 && i != -1)
496 fprintf(stderr, "*** fi %p\t[%d] %s%s\n", fi, i, (i >= 0 ? fi->dnl[fi->j] : ""), (i >= 0 ? fi->bnl[fi->i] : ""));
497
498     }
499
500     return i;
501 }
502
503 rpmfi rpmfiInit(rpmfi fi, int fx)
504 {
505     if (fi != NULL) {
506         if (fx >= 0 && fx < fi->fc) {
507             fi->i = fx - 1;
508             fi->j = -1;
509         }
510     }
511
512     return fi;
513 }
514
515 int rpmfiNextD(rpmfi fi)
516 {
517     int j = -1;
518
519     if (fi != NULL && ++fi->j >= 0) {
520         if (fi->j < fi->dc)
521             j = fi->j;
522         else
523             fi->j = -1;
524
525 if (_rpmfi_debug  < 0 && j != -1)
526 fprintf(stderr, "*** fi %p\t[%d]\n", fi, j);
527
528     }
529
530     return j;
531 }
532
533 rpmfi rpmfiInitD(rpmfi fi, int dx)
534 {
535     if (fi != NULL) {
536         if (dx >= 0 && dx < fi->fc)
537             fi->j = dx - 1;
538         else
539             fi = NULL;
540     }
541
542     return fi;
543 }
544
545 /**
546  * Identify a file type.
547  * @param ft            file type
548  * @return              string to identify a file type
549  */
550 static
551 const char * ftstring (rpmFileTypes ft)
552 {
553     switch (ft) {
554     case XDIR:  return "directory";
555     case CDEV:  return "char dev";
556     case BDEV:  return "block dev";
557     case LINK:  return "link";
558     case SOCK:  return "sock";
559     case PIPE:  return "fifo/pipe";
560     case REG:   return "file";
561     default:    return "unknown file type";
562     }
563 }
564
565 rpmFileTypes rpmfiWhatis(rpm_mode_t mode)
566 {
567     if (S_ISDIR(mode))  return XDIR;
568     if (S_ISCHR(mode))  return CDEV;
569     if (S_ISBLK(mode))  return BDEV;
570     if (S_ISLNK(mode))  return LINK;
571     if (S_ISSOCK(mode)) return SOCK;
572     if (S_ISFIFO(mode)) return PIPE;
573     return REG;
574 }
575
576 int rpmfiCompare(const rpmfi afi, const rpmfi bfi)
577 {
578     rpmFileTypes awhat = rpmfiWhatis(rpmfiFMode(afi));
579     rpmFileTypes bwhat = rpmfiWhatis(rpmfiFMode(bfi));
580
581     if ((rpmfiFFlags(afi) & RPMFILE_GHOST) ||
582         (rpmfiFFlags(bfi) & RPMFILE_GHOST)) return 0;
583
584     if (awhat != bwhat) return 1;
585
586     if (awhat == LINK) {
587         const char * alink = rpmfiFLink(afi);
588         const char * blink = rpmfiFLink(bfi);
589         if (alink == blink) return 0;
590         if (alink == NULL) return 1;
591         if (blink == NULL) return -1;
592         return strcmp(alink, blink);
593     } else if (awhat == REG) {
594         size_t adiglen, bdiglen;
595         pgpHashAlgo aalgo, balgo;
596         const unsigned char * adigest = rpmfiFDigest(afi, &aalgo, &adiglen);
597         const unsigned char * bdigest = rpmfiFDigest(bfi, &balgo, &bdiglen);
598         if (adigest == bdigest) return 0;
599         if (adigest == NULL) return 1;
600         if (bdigest == NULL) return -1;
601         /* can't meaningfully compare different hash types */
602         if (aalgo != balgo || adiglen != bdiglen) return -1;
603         return memcmp(adigest, bdigest, adiglen);
604     }
605
606     return 0;
607 }
608
609 rpmFileAction rpmfiDecideFate(const rpmfi ofi, rpmfi nfi, int skipMissing)
610 {
611     const char * fn = rpmfiFN(nfi);
612     rpmfileAttrs newFlags = rpmfiFFlags(nfi);
613     char buffer[1024];
614     rpmFileTypes dbWhat, newWhat, diskWhat;
615     struct stat sb;
616     int save = (newFlags & RPMFILE_NOREPLACE) ? FA_ALTNAME : FA_SAVE;
617
618     if (lstat(fn, &sb)) {
619         /*
620          * The file doesn't exist on the disk. Create it unless the new
621          * package has marked it as missingok, or allfiles is requested.
622          */
623         if (skipMissing && (newFlags & RPMFILE_MISSINGOK)) {
624             rpmlog(RPMLOG_DEBUG, "%s skipped due to missingok flag\n",
625                         fn);
626             return FA_SKIP;
627         } else {
628             return FA_CREATE;
629         }
630     }
631
632     diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
633     dbWhat = rpmfiWhatis(rpmfiFMode(ofi));
634     newWhat = rpmfiWhatis(rpmfiFMode(nfi));
635
636     /*
637      * RPM >= 2.3.10 shouldn't create config directories -- we'll ignore
638      * them in older packages as well.
639      */
640     if (newWhat == XDIR)
641         return FA_CREATE;
642
643     if (diskWhat != newWhat && dbWhat != REG && dbWhat != LINK)
644         return save;
645     else if (newWhat != dbWhat && diskWhat != dbWhat)
646         return save;
647     else if (dbWhat != newWhat)
648         return FA_CREATE;
649     else if (dbWhat != LINK && dbWhat != REG)
650         return FA_CREATE;
651
652     /*
653      * This order matters - we'd prefer to CREATE the file if at all
654      * possible in case something else (like the timestamp) has changed.
655      */
656     memset(buffer, 0, sizeof(buffer));
657     if (dbWhat == REG) {
658         pgpHashAlgo oalgo, nalgo;
659         size_t odiglen, ndiglen;
660         const unsigned char * odigest, * ndigest;
661         odigest = rpmfiFDigest(ofi, &oalgo, &odiglen);
662         if (diskWhat == REG) {
663             if (rpmDoDigest(oalgo, fn, 0, 
664                 (unsigned char *)buffer, NULL))
665                 return FA_CREATE;       /* assume file has been removed */
666             if (odigest && !memcmp(odigest, buffer, odiglen))
667                 return FA_CREATE;       /* unmodified config file, replace. */
668         }
669         ndigest = rpmfiFDigest(nfi, &nalgo, &ndiglen);
670         /* Can't compare different hash types, backup to avoid data loss */
671         if (oalgo != nalgo || odiglen != ndiglen)
672             return save;
673         if (odigest && ndigest && !memcmp(odigest, ndigest, odiglen))
674             return FA_SKIP;     /* identical file, don't bother. */
675     } else /* dbWhat == LINK */ {
676         const char * oFLink, * nFLink;
677         oFLink = rpmfiFLink(ofi);
678         if (diskWhat == LINK) {
679             if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
680                 return FA_CREATE;       /* assume file has been removed */
681             if (oFLink && rstreq(oFLink, buffer))
682                 return FA_CREATE;       /* unmodified config file, replace. */
683         }
684         nFLink = rpmfiFLink(nfi);
685         if (oFLink && nFLink && rstreq(oFLink, nFLink))
686             return FA_SKIP;     /* identical file, don't bother. */
687     }
688
689     /*
690      * The config file on the disk has been modified, but
691      * the ones in the two packages are different. It would
692      * be nice if RPM was smart enough to at least try and
693      * merge the difference ala CVS, but...
694      */
695     return save;
696 }
697
698 int rpmfiConfigConflict(const rpmfi fi)
699 {
700     const char * fn = rpmfiFN(fi);
701     rpmfileAttrs flags = rpmfiFFlags(fi);
702     char buffer[1024];
703     rpmFileTypes newWhat, diskWhat;
704     struct stat sb;
705
706     if (!(flags & RPMFILE_CONFIG) || lstat(fn, &sb)) {
707         return 0;
708     }
709
710     diskWhat = rpmfiWhatis((rpm_mode_t)sb.st_mode);
711     newWhat = rpmfiWhatis(rpmfiFMode(fi));
712
713     if (newWhat != LINK && newWhat != REG)
714         return 1;
715
716     if (diskWhat != newWhat)
717         return 1;
718     
719     memset(buffer, 0, sizeof(buffer));
720     if (newWhat == REG) {
721         pgpHashAlgo algo;
722         size_t diglen;
723         const unsigned char *ndigest = rpmfiFDigest(fi, &algo, &diglen);
724         if (rpmDoDigest(algo, fn, 0, (unsigned char *)buffer, NULL))
725             return 0;   /* assume file has been removed */
726         if (ndigest && !memcmp(ndigest, buffer, diglen))
727             return 0;   /* unmodified config file */
728     } else /* newWhat == LINK */ {
729         const char * nFLink;
730         if (readlink(fn, buffer, sizeof(buffer) - 1) == -1)
731             return 0;   /* assume file has been removed */
732         nFLink = rpmfiFLink(fi);
733         if (nFLink && rstreq(nFLink, buffer))
734             return 0;   /* unmodified config file */
735     }
736
737     return 1;
738 }
739
740 static char **duparray(char ** src, int size)
741 {
742     char **dest = xmalloc((size+1) * sizeof(*dest));
743     for (int i = 0; i < size; i++) {
744         dest[i] = xstrdup(src[i]);
745     }
746     free(src);
747     return dest;
748 }
749
750 static int addPrefixes(Header h, rpmRelocation *relocations, int numRelocations)
751 {
752     struct rpmtd_s validRelocs;
753     const char *validprefix;
754     const char ** actualRelocations;
755     int numActual = 0;
756
757     headerGet(h, RPMTAG_PREFIXES, &validRelocs, HEADERGET_MINMEM);
758     /*
759      * If no relocations are specified (usually the case), then return the
760      * original header. If there are prefixes, however, then INSTPREFIXES
761      * should be added for RPM_INSTALL_PREFIX environ variables in scriptlets, 
762      * but, since relocateFileList() can be called more than once for 
763      * the same header, don't bother if already present.
764      */
765     if (relocations == NULL || numRelocations == 0) {
766         if (rpmtdCount(&validRelocs) > 0) {
767             if (!headerIsEntry(h, RPMTAG_INSTPREFIXES)) {
768                 rpmtdSetTag(&validRelocs, RPMTAG_INSTPREFIXES);
769                 headerPut(h, &validRelocs, HEADERPUT_DEFAULT);
770             }
771             rpmtdFreeData(&validRelocs);
772         }
773         return 0;
774     }
775
776     actualRelocations = xmalloc(rpmtdCount(&validRelocs) * sizeof(*actualRelocations));
777     rpmtdInit(&validRelocs);
778     while ((validprefix = rpmtdNextString(&validRelocs))) {
779         int j;
780         for (j = 0; j < numRelocations; j++) {
781             if (relocations[j].oldPath == NULL || /* XXX can't happen */
782                 !rstreq(validprefix, relocations[j].oldPath))
783                 continue;
784             /* On install, a relocate to NULL means skip the path. */
785             if (relocations[j].newPath) {
786                 actualRelocations[numActual] = relocations[j].newPath;
787                 numActual++;
788             }
789             break;
790         }
791         if (j == numRelocations) {
792             actualRelocations[numActual] = validprefix;
793             numActual++;
794         }
795     }
796     rpmtdFreeData(&validRelocs);
797
798     if (numActual) {
799         headerPutStringArray(h, RPMTAG_INSTPREFIXES, actualRelocations, numActual);
800     }
801     actualRelocations = _free(actualRelocations);
802     return numActual;
803 }
804
805 static void saveRelocs(Header h, rpmtd bnames, rpmtd dnames, rpmtd dindexes)
806 {
807         struct rpmtd_s td;
808         headerGet(h, RPMTAG_BASENAMES, &td, HEADERGET_MINMEM);
809         rpmtdSetTag(&td, RPMTAG_ORIGBASENAMES);
810         headerPut(h, &td, HEADERPUT_DEFAULT);
811         rpmtdFreeData(&td);
812
813         headerGet(h, RPMTAG_DIRNAMES, &td, HEADERGET_MINMEM);
814         rpmtdSetTag(&td, RPMTAG_ORIGDIRNAMES);
815         headerPut(h, &td, HEADERPUT_DEFAULT);
816         rpmtdFreeData(&td);
817
818         headerGet(h, RPMTAG_DIRINDEXES, &td, HEADERGET_MINMEM);
819         rpmtdSetTag(&td, RPMTAG_ORIGDIRINDEXES);
820         headerPut(h, &td, HEADERPUT_DEFAULT);
821         rpmtdFreeData(&td);
822
823         headerMod(h, bnames);
824         headerMod(h, dnames);
825         headerMod(h, dindexes);
826 }
827
828 void rpmRelocateFileList(rpmRelocation *relocations, int numRelocations, 
829                          rpmfs fs, Header h)
830 {
831     static int _printed = 0;
832     char ** baseNames;
833     char ** dirNames;
834     uint32_t * dirIndexes;
835     rpm_count_t fileCount, dirCount;
836     int nrelocated = 0;
837     int fileAlloced = 0;
838     char * fn = NULL;
839     int haveRelocatedBase = 0;
840     size_t maxlen = 0;
841     int i, j;
842     struct rpmtd_s bnames, dnames, dindexes, fmodes;
843
844     addPrefixes(h, relocations, numRelocations);
845
846     if (!_printed) {
847         _printed = 1;
848         rpmlog(RPMLOG_DEBUG, "========== relocations\n");
849         for (i = 0; i < numRelocations; i++) {
850             if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
851             if (relocations[i].newPath == NULL)
852                 rpmlog(RPMLOG_DEBUG, "%5d exclude  %s\n",
853                         i, relocations[i].oldPath);
854             else
855                 rpmlog(RPMLOG_DEBUG, "%5d relocate %s -> %s\n",
856                         i, relocations[i].oldPath, relocations[i].newPath);
857         }
858     }
859
860     for (i = 0; i < numRelocations; i++) {
861         if (relocations[i].newPath == NULL) continue;
862         size_t len = strlen(relocations[i].newPath);
863         if (len > maxlen) maxlen = len;
864     }
865
866     headerGet(h, RPMTAG_BASENAMES, &bnames, HEADERGET_MINMEM);
867     headerGet(h, RPMTAG_DIRINDEXES, &dindexes, HEADERGET_ALLOC);
868     headerGet(h, RPMTAG_DIRNAMES, &dnames, HEADERGET_MINMEM);
869     headerGet(h, RPMTAG_FILEMODES, &fmodes, HEADERGET_MINMEM);
870     /* TODO XXX ugh.. use rpmtd iterators & friends instead */
871     baseNames = bnames.data;
872     dirIndexes = dindexes.data;
873     fileCount = rpmtdCount(&bnames);
874     dirCount = rpmtdCount(&dnames);
875     /* XXX TODO: use rpmtdDup() instead */
876     dirNames = dnames.data = duparray(dnames.data, dirCount);
877     dnames.flags |= RPMTD_PTR_ALLOCED;
878
879     /*
880      * For all relocations, we go through sorted file/relocation lists 
881      * backwards so that /usr/local relocations take precedence over /usr 
882      * ones.
883      */
884
885     /* Relocate individual paths. */
886
887     for (i = fileCount - 1; i >= 0; i--) {
888         rpmFileTypes ft;
889         int fnlen;
890
891         size_t len = maxlen +
892                 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
893         if (len >= fileAlloced) {
894             fileAlloced = len * 2;
895             fn = xrealloc(fn, fileAlloced);
896         }
897
898 assert(fn != NULL);             /* XXX can't happen */
899         *fn = '\0';
900         fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
901
902         /*
903          * See if this file path needs relocating.
904          */
905         /*
906          * XXX FIXME: Would a bsearch of the (already sorted) 
907          * relocation list be a good idea?
908          */
909         for (j = numRelocations - 1; j >= 0; j--) {
910             if (relocations[j].oldPath == NULL) /* XXX can't happen */
911                 continue;
912             len = !rstreq(relocations[j].oldPath, "/")
913                 ? strlen(relocations[j].oldPath)
914                 : 0;
915
916             if (fnlen < len)
917                 continue;
918             /*
919              * Only subdirectories or complete file paths may be relocated. We
920              * don't check for '\0' as our directory names all end in '/'.
921              */
922             if (!(fn[len] == '/' || fnlen == len))
923                 continue;
924
925             if (!rstreqn(relocations[j].oldPath, fn, len))
926                 continue;
927             break;
928         }
929         if (j < 0) continue;
930
931         rpmtdSetIndex(&fmodes, i);
932         ft = rpmfiWhatis(rpmtdGetNumber(&fmodes));
933
934         /* On install, a relocate to NULL means skip the path. */
935         if (relocations[j].newPath == NULL) {
936             if (ft == XDIR) {
937                 /* Start with the parent, looking for directory to exclude. */
938                 for (j = dirIndexes[i]; j < dirCount; j++) {
939                     len = strlen(dirNames[j]) - 1;
940                     while (len > 0 && dirNames[j][len-1] == '/') len--;
941                     if (fnlen != len)
942                         continue;
943                     if (!rstreqn(fn, dirNames[j], fnlen))
944                         continue;
945                     break;
946                 }
947             }
948             rpmfsSetAction(fs, i, FA_SKIPNSTATE);
949             rpmlog(RPMLOG_DEBUG, "excluding %s %s\n",
950                    ftstring(ft), fn);
951             continue;
952         }
953
954         /* Relocation on full paths only, please. */
955         if (fnlen != len) continue;
956
957         rpmlog(RPMLOG_DEBUG, "relocating %s to %s\n",
958                fn, relocations[j].newPath);
959         nrelocated++;
960
961         strcpy(fn, relocations[j].newPath);
962         {   char * te = strrchr(fn, '/');
963             if (te) {
964                 if (te > fn) te++;      /* root is special */
965                 fnlen = te - fn;
966             } else
967                 te = fn + strlen(fn);
968             if (!rstreq(baseNames[i], te)) { /* basename changed too? */
969                 if (!haveRelocatedBase) {
970                     /* XXX TODO: use rpmtdDup() instead */
971                     bnames.data = baseNames = duparray(baseNames, fileCount);
972                     bnames.flags |= RPMTD_PTR_ALLOCED;
973                     haveRelocatedBase = 1;
974                 }
975                 free(baseNames[i]);
976                 baseNames[i] = xstrdup(te);
977             }
978             *te = '\0';                 /* terminate new directory name */
979         }
980
981         /* Does this directory already exist in the directory list? */
982         for (j = 0; j < dirCount; j++) {
983             if (fnlen != strlen(dirNames[j]))
984                 continue;
985             if (!rstreqn(fn, dirNames[j], fnlen))
986                 continue;
987             break;
988         }
989         
990         if (j < dirCount) {
991             dirIndexes[i] = j;
992             continue;
993         }
994
995         /* Creating new paths is a pita */
996         dirNames = dnames.data = xrealloc(dnames.data, 
997                                sizeof(*dirNames) * (dirCount + 1));
998
999         dirNames[dirCount] = xstrdup(fn);
1000         dirIndexes[i] = dirCount;
1001         dirCount++;
1002         dnames.count++;
1003     }
1004
1005     /* Finish off by relocating directories. */
1006     for (i = dirCount - 1; i >= 0; i--) {
1007         for (j = numRelocations - 1; j >= 0; j--) {
1008
1009             if (relocations[j].oldPath == NULL) /* XXX can't happen */
1010                 continue;
1011             size_t len = !rstreq(relocations[j].oldPath, "/")
1012                 ? strlen(relocations[j].oldPath)
1013                 : 0;
1014
1015             if (len && !rstreqn(relocations[j].oldPath, dirNames[i], len))
1016                 continue;
1017
1018             /*
1019              * Only subdirectories or complete file paths may be relocated. We
1020              * don't check for '\0' as our directory names all end in '/'.
1021              */
1022             if (dirNames[i][len] != '/')
1023                 continue;
1024
1025             if (relocations[j].newPath) { /* Relocate the path */
1026                 char *t = NULL;
1027                 rstrscat(&t, relocations[j].newPath, (dirNames[i] + len), NULL);
1028                 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
1029                 (void) rpmCleanPath(t);
1030                 rstrcat(&t, "/");
1031
1032                 rpmlog(RPMLOG_DEBUG,
1033                        "relocating directory %s to %s\n", dirNames[i], t);
1034                 free(dirNames[i]);
1035                 dirNames[i] = t;
1036                 nrelocated++;
1037             }
1038         }
1039     }
1040
1041     /* Save original filenames in header and replace (relocated) filenames. */
1042     if (nrelocated) {
1043         saveRelocs(h, &bnames, &dnames, &dindexes);
1044     }
1045
1046     rpmtdFreeData(&bnames);
1047     rpmtdFreeData(&dnames);
1048     rpmtdFreeData(&dindexes);
1049     rpmtdFreeData(&fmodes);
1050     free(fn);
1051 }
1052
1053 rpmfi rpmfiFree(rpmfi fi)
1054 {
1055     if (fi == NULL) return NULL;
1056
1057     if (fi->nrefs > 1)
1058         return rpmfiUnlink(fi, __FUNCTION__);
1059
1060 if (_rpmfi_debug < 0)
1061 fprintf(stderr, "*** fi %p\t[%d]\n", fi, fi->fc);
1062
1063     if (fi->fc > 0) {
1064         fi->bnl = _free(fi->bnl);
1065         fi->dnl = _free(fi->dnl);
1066
1067         fi->flinkcache = strcacheFree(fi->flinkcache);
1068         fi->flinks = _free(fi->flinks);
1069         fi->flangs = _free(fi->flangs);
1070         fi->digests = _free(fi->digests);
1071         fi->fcapcache = strcacheFree(fi->fcapcache);
1072         fi->fcaps = _free(fi->fcaps);
1073
1074         fi->cdict = _free(fi->cdict);
1075
1076         fi->fuser = _free(fi->fuser);
1077         fi->fgroup = _free(fi->fgroup);
1078
1079         fi->fstates = _free(fi->fstates);
1080         fi->fps = _free(fi->fps);
1081
1082         if (!(fi->fiflags & RPMFI_KEEPHEADER) && fi->h == NULL) {
1083             fi->fmtimes = _constfree(fi->fmtimes);
1084             fi->fmodes = _free(fi->fmodes);
1085             fi->fflags = _constfree(fi->fflags);
1086             fi->vflags = _constfree(fi->vflags);
1087             fi->fsizes = _constfree(fi->fsizes);
1088             fi->frdevs = _constfree(fi->frdevs);
1089             fi->finodes = _constfree(fi->finodes);
1090             fi->dil = _free(fi->dil);
1091
1092             fi->fcolors = _constfree(fi->fcolors);
1093             fi->fcdictx = _constfree(fi->fcdictx);
1094             fi->ddict = _constfree(fi->ddict);
1095             fi->fddictx = _constfree(fi->fddictx);
1096             fi->fddictn = _constfree(fi->fddictn);
1097
1098         }
1099     }
1100
1101     fi->fsm = freeFSM(fi->fsm);
1102
1103     fi->fn = _free(fi->fn);
1104     fi->apath = _free(fi->apath);
1105
1106     fi->replacedSizes = _free(fi->replacedSizes);
1107
1108     fi->h = headerFree(fi->h);
1109
1110     (void) rpmfiUnlink(fi, __FUNCTION__);
1111     memset(fi, 0, sizeof(*fi));         /* XXX trash and burn */
1112     fi = _free(fi);
1113
1114     return NULL;
1115 }
1116
1117 /* Helper to push header tag data into a string cache */
1118 static scidx_t *cacheTag(strcache cache, Header h, rpmTag tag)
1119 {
1120     scidx_t *idx = NULL;
1121     struct rpmtd_s td;
1122     if (headerGet(h, tag, &td, HEADERGET_MINMEM)) {
1123        idx = xmalloc(sizeof(*idx) * rpmtdCount(&td));
1124        int i = 0;
1125        const char *str;
1126        while ((str = rpmtdNextString(&td))) {
1127            idx[i++] = strcachePut(cache, str);
1128        }
1129        rpmtdFreeData(&td);
1130     }
1131     return idx;
1132 }
1133
1134 #define _hgfi(_h, _tag, _td, _flags, _data) \
1135     if (headerGet((_h), (_tag), (_td), (_flags))) \
1136         _data = (td.data)
1137
1138 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, rpmfiFlags flags)
1139 {
1140     rpmfi fi = NULL;
1141     rpm_loff_t *asize = NULL;
1142     unsigned char * t;
1143     int isBuild, isSource;
1144     struct rpmtd_s fdigests, digalgo;
1145     struct rpmtd_s td;
1146     headerGetFlags scareFlags = (flags & RPMFI_KEEPHEADER) ? 
1147                                 HEADERGET_MINMEM : HEADERGET_ALLOC;
1148     headerGetFlags defFlags = HEADERGET_ALLOC;
1149
1150     fi = xcalloc(1, sizeof(*fi));
1151     if (fi == NULL)     /* XXX can't happen */
1152         goto exit;
1153
1154     fi->magic = RPMFIMAGIC;
1155     fi->i = -1;
1156
1157     fi->fiflags = flags;
1158     fi->scareFlags = scareFlags;
1159
1160     if (headerGet(h, RPMTAG_LONGARCHIVESIZE, &td, HEADERGET_EXT)) {
1161         asize = rpmtdGetUint64(&td);
1162     }
1163     /* 0 means unknown */
1164     fi->archiveSize = asize ? *asize : 0;
1165     rpmtdFreeData(&td);
1166     
1167     /* Archive size is not set when this gets called from build */
1168     isBuild = (asize == NULL);
1169     isSource = headerIsSource(h);
1170     if (isBuild) fi->fiflags |= RPMFI_ISBUILD;
1171     if (isSource) fi->fiflags |= RPMFI_ISSOURCE;
1172
1173     _hgfi(h, RPMTAG_BASENAMES, &td, defFlags, fi->bnl);
1174     fi->fc = rpmtdCount(&td);
1175     if (fi->fc == 0) {
1176         goto exit;
1177     }
1178
1179     _hgfi(h, RPMTAG_DIRNAMES, &td, defFlags, fi->dnl);
1180     fi->dc = rpmtdCount(&td);
1181     _hgfi(h, RPMTAG_DIRINDEXES, &td, scareFlags, fi->dil);
1182     if (!(flags & RPMFI_NOFILEMODES))
1183         _hgfi(h, RPMTAG_FILEMODES, &td, scareFlags, fi->fmodes);
1184     if (!(flags & RPMFI_NOFILEFLAGS))
1185         _hgfi(h, RPMTAG_FILEFLAGS, &td, scareFlags, fi->fflags);
1186     if (!(flags & RPMFI_NOFILEVERIFYFLAGS))
1187         _hgfi(h, RPMTAG_FILEVERIFYFLAGS, &td, scareFlags, fi->vflags);
1188     if (!(flags & RPMFI_NOFILESIZES))
1189         _hgfi(h, RPMTAG_FILESIZES, &td, scareFlags, fi->fsizes);
1190
1191     if (!(flags & RPMFI_NOFILECOLORS))
1192         _hgfi(h, RPMTAG_FILECOLORS, &td, scareFlags, fi->fcolors);
1193
1194     if (!(flags & RPMFI_NOFILECLASS)) {
1195         _hgfi(h, RPMTAG_CLASSDICT, &td, scareFlags, fi->cdict);
1196         fi->ncdict = rpmtdCount(&td);
1197         _hgfi(h, RPMTAG_FILECLASS, &td, scareFlags, fi->fcdictx);
1198     }
1199     if (!(flags & RPMFI_NOFILEDEPS)) {
1200         _hgfi(h, RPMTAG_DEPENDSDICT, &td, scareFlags, fi->ddict);
1201         fi->nddict = rpmtdCount(&td);
1202         _hgfi(h, RPMTAG_FILEDEPENDSX, &td, scareFlags, fi->fddictx);
1203         _hgfi(h, RPMTAG_FILEDEPENDSN, &td, scareFlags, fi->fddictn);
1204     }
1205
1206     if (!(flags & RPMFI_NOFILESTATES))
1207         _hgfi(h, RPMTAG_FILESTATES, &td, defFlags, fi->fstates);
1208
1209     if (!(flags & RPMFI_NOFILECAPS) && headerIsEntry(h, RPMTAG_FILECAPS)) {
1210         fi->fcapcache = strcacheNew();
1211         fi->fcaps = cacheTag(fi->fcapcache, h, RPMTAG_FILECAPS);
1212     }
1213
1214     if (!(flags & RPMFI_NOFILELINKTOS)) {
1215         fi->flinkcache = strcacheNew();
1216         fi->flinks = cacheTag(fi->flinkcache, h, RPMTAG_FILELINKTOS);
1217     }
1218     /* FILELANGS are only interesting when installing */
1219     if ((headerGetInstance(h) == 0) && !(flags & RPMFI_NOFILELANGS))
1220         fi->flangs = cacheTag(langcache, h, RPMTAG_FILELANGS);
1221
1222     /* See if the package has non-md5 file digests */
1223     fi->digestalgo = PGPHASHALGO_MD5;
1224     if (headerGet(h, RPMTAG_FILEDIGESTALGO, &digalgo, HEADERGET_MINMEM)) {
1225         pgpHashAlgo *algo = rpmtdGetUint32(&digalgo);
1226         /* Hmm, what to do with unknown digest algorithms? */
1227         if (algo && rpmDigestLength(*algo) != 0) {
1228             fi->digestalgo = *algo;
1229         }
1230     }
1231
1232     fi->digests = NULL;
1233     /* grab hex digests from header and store in binary format */
1234     if (!(flags & RPMFI_NOFILEDIGESTS) &&
1235         headerGet(h, RPMTAG_FILEDIGESTS, &fdigests, HEADERGET_MINMEM)) {
1236         const char *fdigest;
1237         size_t diglen = rpmDigestLength(fi->digestalgo);
1238         fi->digests = t = xmalloc(rpmtdCount(&fdigests) * diglen);
1239
1240         while ((fdigest = rpmtdNextString(&fdigests))) {
1241             if (!(fdigest && *fdigest != '\0')) {
1242                 memset(t, 0, diglen);
1243                 t += diglen;
1244                 continue;
1245             }
1246             for (int j = 0; j < diglen; j++, t++, fdigest += 2)
1247                 *t = (rnibble(fdigest[0]) << 4) | rnibble(fdigest[1]);
1248         }
1249         rpmtdFreeData(&fdigests);
1250     }
1251
1252     /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes */
1253     if (!(flags & RPMFI_NOFILEMTIMES))
1254         _hgfi(h, RPMTAG_FILEMTIMES, &td, scareFlags, fi->fmtimes);
1255     if (!(flags & RPMFI_NOFILERDEVS))
1256         _hgfi(h, RPMTAG_FILERDEVS, &td, scareFlags, fi->frdevs);
1257     if (!(flags & RPMFI_NOFILEINODES))
1258         _hgfi(h, RPMTAG_FILEINODES, &td, scareFlags, fi->finodes);
1259
1260     if (!(flags & RPMFI_NOFILEUSER)) 
1261         fi->fuser = cacheTag(ugcache, h, RPMTAG_FILEUSERNAME);
1262     if (!(flags & RPMFI_NOFILEGROUP)) 
1263         fi->fgroup = cacheTag(ugcache, h, RPMTAG_FILEGROUPNAME);
1264
1265     /* lazily alloced from rpmfiFN() */
1266     fi->fn = NULL;
1267
1268 exit:
1269 if (_rpmfi_debug < 0)
1270 fprintf(stderr, "*** fi %p\t[%d]\n", fi, (fi ? fi->fc : 0));
1271
1272     if (fi != NULL) {
1273         fi->h = (fi->fiflags & RPMFI_KEEPHEADER) ? headerLink(h) : NULL;
1274     }
1275
1276     /* FIX: rpmfi null annotations */
1277     return rpmfiLink(fi, __FUNCTION__);
1278 }
1279
1280 void rpmfiSetFReplacedSize(rpmfi fi, rpm_loff_t newsize)
1281 {
1282     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
1283         if (fi->replacedSizes == NULL) {
1284             fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1285         }
1286         /* XXX watch out, replacedSizes is not rpm_loff_t (yet) */
1287         fi->replacedSizes[fi->i] = (rpm_off_t) newsize;
1288     }
1289 }
1290
1291 rpm_loff_t rpmfiFReplacedSize(rpmfi fi)
1292 {
1293     rpm_loff_t rsize = 0;
1294     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
1295         if (fi->replacedSizes) {
1296             rsize = fi->replacedSizes[fi->i];
1297         }
1298     }
1299     return rsize;
1300 }
1301
1302 void rpmfiFpLookup(rpmfi fi, fingerPrintCache fpc)
1303 {
1304     if (fi->fc > 0 && fi->fps == NULL) {
1305         fi->fps = xcalloc(fi->fc, sizeof(*fi->fps));
1306     }
1307     fpLookupList(fpc, fi->dnl, fi->bnl, fi->dil, fi->fc, fi->fps);
1308 }
1309
1310 FSM_t rpmfiFSM(rpmfi fi)
1311 {
1312     if (fi != NULL && fi->fsm == NULL) {
1313         cpioMapFlags mapflags;
1314         /* Figure out mapflags: 
1315          * - path, mode, uid and gid are used by everything
1316          * - all binary packages get SBIT_CHECK set
1317          * - if archive size is not known, we're only building this package,
1318          *   different rules apply 
1319          */
1320         mapflags = CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
1321         if (fi->fiflags & RPMFI_ISBUILD) {
1322             mapflags |= CPIO_MAP_TYPE;
1323             if (fi->fiflags & RPMFI_ISSOURCE) mapflags |= CPIO_FOLLOW_SYMLINKS;
1324         } else {
1325             if (!(fi->fiflags & RPMFI_ISSOURCE)) mapflags |= CPIO_SBIT_CHECK;
1326         }
1327         fi->fsm = newFSM(mapflags);
1328     }
1329     return (fi != NULL) ? fi->fsm : NULL;
1330 }
1331
1332 /* 
1333  * Generate iterator accessors function wrappers, these do nothing but
1334  * call the corresponding rpmfiFooIndex(fi, fi->[ij])
1335  */
1336
1337 #define RPMFI_ITERFUNC(TYPE, NAME, IXV) \
1338     TYPE rpmfi ## NAME(rpmfi fi) { return rpmfi ## NAME ## Index(fi, fi ? fi->IXV : -1); }
1339
1340 RPMFI_ITERFUNC(const char *, BN, i)
1341 RPMFI_ITERFUNC(const char *, DN, j)
1342 RPMFI_ITERFUNC(const char *, FN, i)
1343 RPMFI_ITERFUNC(const char *, FLink, i)
1344 RPMFI_ITERFUNC(const char *, FUser, i)
1345 RPMFI_ITERFUNC(const char *, FGroup, i)
1346 RPMFI_ITERFUNC(const char *, FCaps, i)
1347 RPMFI_ITERFUNC(const char *, FLangs, i)
1348 RPMFI_ITERFUNC(const char *, FClass, i)
1349 RPMFI_ITERFUNC(rpmfileState, FState, i)
1350 RPMFI_ITERFUNC(rpmfileAttrs, FFlags, i)
1351 RPMFI_ITERFUNC(rpmVerifyAttrs, VFlags, i)
1352 RPMFI_ITERFUNC(rpm_mode_t, FMode, i)
1353 RPMFI_ITERFUNC(rpm_rdev_t, FRdev, i)
1354 RPMFI_ITERFUNC(rpm_time_t, FMtime, i)
1355 RPMFI_ITERFUNC(rpm_ino_t, FInode, i)
1356 RPMFI_ITERFUNC(rpm_loff_t, FSize, i)
1357 RPMFI_ITERFUNC(rpm_color_t, FColor, i)
1358 RPMFI_ITERFUNC(uint32_t, FNlink, i)
1359
1360 const unsigned char * rpmfiFDigest(rpmfi fi, pgpHashAlgo *algo, size_t *len)
1361 {
1362     return rpmfiFDigestIndex(fi, fi ? fi->i : -1, algo, len);
1363 }
1364
1365 uint32_t rpmfiFDepends(rpmfi fi, const uint32_t ** fddictp)
1366 {
1367     return rpmfiFDependsIndex(fi,  fi ? fi->i : -1, fddictp);
1368 }