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