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