Convert file relocation to use headerPut/Mod
[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)
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     free(src);
647     return dest;
648 }
649
650 /**
651  * Relocate files in header.
652  * @todo multilib file dispositions need to be checked.
653  * @param ts            transaction set
654  * @param fi            transaction element file info
655  * @param origH         package header
656  * @param actions       file dispositions
657  * @return              header with relocated files
658  */
659 static
660 Header relocateFileList(const rpmts ts, rpmfi fi,
661                 Header origH, rpmFileAction * actions)
662 {
663     rpmte p = rpmtsRelocateElement(ts);
664     static int _printed = 0;
665     int allowBadRelocate = (rpmtsFilterFlags(ts) & RPMPROB_FILTER_FORCERELOCATE);
666     rpmRelocation * relocations = NULL;
667     int numRelocations;
668     char ** baseNames;
669     char ** dirNames;
670     uint32_t * dirIndexes;
671     uint32_t * newDirIndexes;
672     rpm_count_t fileCount, dirCount, numValid = 0;
673     rpm_color_t * fColors = NULL;
674     rpm_color_t * dColors = NULL;
675     rpm_mode_t * fModes = NULL;
676     Header h;
677     int nrelocated = 0;
678     int fileAlloced = 0;
679     char * fn = NULL;
680     int haveRelocatedBase = 0;
681     int reldel = 0;
682     int len;
683     int i, j;
684     struct rpmtd_s validRelocs;
685     struct rpmtd_s bnames, dnames, dindexes, fcolors, fmodes;
686
687     
688     if (headerGet(origH, RPMTAG_PREFIXES, &validRelocs, HEADERGET_MINMEM)) 
689         numValid = rpmtdCount(&validRelocs);
690
691 assert(p != NULL);
692     numRelocations = 0;
693     if (p->relocs)
694         while (p->relocs[numRelocations].newPath ||
695                p->relocs[numRelocations].oldPath)
696             numRelocations++;
697
698     /*
699      * If no relocations are specified (usually the case), then return the
700      * original header. If there are prefixes, however, then INSTPREFIXES
701      * should be added, but, since relocateFileList() can be called more
702      * than once for the same header, don't bother if already present.
703      */
704     if (p->relocs == NULL || numRelocations == 0) {
705         if (numValid) {
706             if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES)) {
707                 rpmtdSetTag(&validRelocs, RPMTAG_INSTPREFIXES);
708                 headerPut(origH, &validRelocs, HEADERPUT_DEFAULT);
709             }
710             rpmtdFreeData(&validRelocs);
711         }
712         /* XXX FIXME multilib file actions need to be checked. */
713         return headerLink(origH);
714     }
715
716     h = headerLink(origH);
717
718     relocations = xmalloc(sizeof(*relocations) * numRelocations);
719
720     /* Build sorted relocation list from raw relocations. */
721     for (i = 0; i < numRelocations; i++) {
722         char * t;
723
724         /*
725          * Default relocations (oldPath == NULL) are handled in the UI,
726          * not rpmlib.
727          */
728         if (p->relocs[i].oldPath == NULL) continue; /* XXX can't happen */
729
730         /* FIXME: Trailing /'s will confuse us greatly. Internal ones will 
731            too, but those are more trouble to fix up. :-( */
732         t = xstrdup(p->relocs[i].oldPath);
733         relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
734             ? t
735             : stripTrailingChar(t, '/');
736
737         /* An old path w/o a new path is valid, and indicates exclusion */
738         if (p->relocs[i].newPath) {
739             int del;
740             int valid = 0;
741             const char *validprefix;
742
743             t = xstrdup(p->relocs[i].newPath);
744             relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
745                 ? t
746                 : stripTrailingChar(t, '/');
747
748                 /* FIX:  relocations[i].oldPath == NULL */
749             /* Verify that the relocation's old path is in the header. */
750             rpmtdInit(&validRelocs);
751             while ((validprefix = rpmtdNextString(&validRelocs))) {
752                 if (strcmp(validprefix, relocations[i].oldPath) == 0) {
753                     valid = 1;
754                     break;
755                 }
756             }
757
758             /* XXX actions check prevents problem from being appended twice. */
759             if (!valid && !allowBadRelocate && actions) {
760                 rpmps ps = rpmtsProblems(ts);
761                 rpmpsAppend(ps, RPMPROB_BADRELOCATE,
762                         rpmteNEVRA(p), rpmteKey(p),
763                         relocations[i].oldPath, NULL, NULL, 0);
764                 ps = rpmpsFree(ps);
765             }
766             del =
767                 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
768
769             if (del > reldel)
770                 reldel = del;
771         } else {
772             relocations[i].newPath = NULL;
773         }
774     }
775
776     /* stupid bubble sort, but it's probably faster here */
777     for (i = 0; i < numRelocations; i++) {
778         int madeSwap;
779         madeSwap = 0;
780         for (j = 1; j < numRelocations; j++) {
781             rpmRelocation tmpReloc;
782             if (relocations[j - 1].oldPath == NULL || /* XXX can't happen */
783                 relocations[j    ].oldPath == NULL || /* XXX can't happen */
784         strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
785                 continue;
786             /* LCL: ??? */
787             tmpReloc = relocations[j - 1];
788             relocations[j - 1] = relocations[j];
789             relocations[j] = tmpReloc;
790             madeSwap = 1;
791         }
792         if (!madeSwap) break;
793     }
794
795     if (!_printed) {
796         _printed = 1;
797         rpmlog(RPMLOG_DEBUG, "========== relocations\n");
798         for (i = 0; i < numRelocations; i++) {
799             if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
800             if (relocations[i].newPath == NULL)
801                 rpmlog(RPMLOG_DEBUG, "%5d exclude  %s\n",
802                         i, relocations[i].oldPath);
803             else
804                 rpmlog(RPMLOG_DEBUG, "%5d relocate %s -> %s\n",
805                         i, relocations[i].oldPath, relocations[i].newPath);
806         }
807     }
808
809     /* Add relocation values to the header */
810     if (numValid) {
811         const char *validprefix;
812         const char ** actualRelocations;
813         rpm_count_t numActual;
814
815         actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
816         numActual = 0;
817         rpmtdInit(&validRelocs);
818         while ((validprefix = rpmtdNextString(&validRelocs))) {
819             for (j = 0; j < numRelocations; j++) {
820                 if (relocations[j].oldPath == NULL || /* XXX can't happen */
821                     strcmp(validprefix, relocations[j].oldPath))
822                     continue;
823                 /* On install, a relocate to NULL means skip the path. */
824                 if (relocations[j].newPath) {
825                     actualRelocations[numActual] = relocations[j].newPath;
826                     numActual++;
827                 }
828                 break;
829             }
830             if (j == numRelocations) {
831                 actualRelocations[numActual] = validprefix;
832                 numActual++;
833             }
834         }
835
836         if (numActual) {
837             struct rpmtd_s pfx;
838             if (rpmtdFromStringArray(&pfx, RPMTAG_INSTPREFIXES, 
839                                      actualRelocations, numActual)) {
840                 headerPut(h, &pfx, HEADERPUT_DEFAULT);
841             }
842         }
843
844         actualRelocations = _free(actualRelocations);
845         rpmtdFreeData(&validRelocs);
846     }
847
848     headerGet(h, RPMTAG_BASENAMES, &bnames, fi->scareFlags);
849     headerGet(h, RPMTAG_DIRINDEXES, &dindexes, fi->scareFlags);
850     headerGet(h, RPMTAG_DIRNAMES, &dnames, fi->scareFlags);
851     headerGet(h, RPMTAG_FILECOLORS, &fcolors, fi->scareFlags);
852     headerGet(h, RPMTAG_FILEMODES, &fmodes, fi->scareFlags);
853     /* TODO XXX ugh.. use rpmtd iterators & friends instead */
854     baseNames = bnames.data;
855     dirIndexes = dindexes.data;
856     fColors = fcolors.data;
857     fModes = fmodes.data;
858     fileCount = rpmtdCount(&bnames);
859     dirCount = rpmtdCount(&dnames);
860     /* XXX TODO: use rpmtdDup() instead */
861     dirNames = dnames.data = duparray(dnames.data, dirCount);
862     dnames.flags |= RPMTD_PTR_ALLOCED;
863
864     dColors = xcalloc(dirCount, sizeof(*dColors));
865
866     newDirIndexes = xmalloc(sizeof(*newDirIndexes) * fileCount);
867     memcpy(newDirIndexes, dirIndexes, sizeof(*newDirIndexes) * fileCount);
868     dirIndexes = newDirIndexes;
869
870     /*
871      * For all relocations, we go through sorted file/relocation lists 
872      * backwards so that /usr/local relocations take precedence over /usr 
873      * ones.
874      */
875
876     /* Relocate individual paths. */
877
878     for (i = fileCount - 1; i >= 0; i--) {
879         rpmFileTypes ft;
880         int fnlen;
881
882         len = reldel +
883                 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
884         if (len >= fileAlloced) {
885             fileAlloced = len * 2;
886             fn = xrealloc(fn, fileAlloced);
887         }
888
889 assert(fn != NULL);             /* XXX can't happen */
890         *fn = '\0';
891         fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
892
893 if (fColors != NULL) {
894 /* XXX pkgs may not have unique dirNames, so color all dirNames that match. */
895 for (j = 0; j < dirCount; j++) {
896 if (strcmp(dirNames[dirIndexes[i]], dirNames[j])) continue;
897 dColors[j] |= fColors[i];
898 }
899 }
900
901         /*
902          * See if this file path needs relocating.
903          */
904         /*
905          * XXX FIXME: Would a bsearch of the (already sorted) 
906          * relocation list be a good idea?
907          */
908         for (j = numRelocations - 1; j >= 0; j--) {
909             if (relocations[j].oldPath == NULL) /* XXX can't happen */
910                 continue;
911             len = strcmp(relocations[j].oldPath, "/")
912                 ? strlen(relocations[j].oldPath)
913                 : 0;
914
915             if (fnlen < len)
916                 continue;
917             /*
918              * Only subdirectories or complete file paths may be relocated. We
919              * don't check for '\0' as our directory names all end in '/'.
920              */
921             if (!(fn[len] == '/' || fnlen == len))
922                 continue;
923
924             if (strncmp(relocations[j].oldPath, fn, len))
925                 continue;
926             break;
927         }
928         if (j < 0) continue;
929
930 /* FIX: fModes may be NULL */
931         ft = rpmfiWhatis(fModes[i]);
932
933         /* On install, a relocate to NULL means skip the path. */
934         if (relocations[j].newPath == NULL) {
935             if (ft == XDIR) {
936                 /* Start with the parent, looking for directory to exclude. */
937                 for (j = dirIndexes[i]; j < dirCount; j++) {
938                     len = strlen(dirNames[j]) - 1;
939                     while (len > 0 && dirNames[j][len-1] == '/') len--;
940                     if (fnlen != len)
941                         continue;
942                     if (strncmp(fn, dirNames[j], fnlen))
943                         continue;
944                     break;
945                 }
946             }
947             if (actions) {
948                 actions[i] = FA_SKIPNSTATE;
949                 rpmlog(RPMLOG_DEBUG, "excluding %s %s\n",
950                         ftstring(ft), fn);
951             }
952             continue;
953         }
954
955         /* Relocation on full paths only, please. */
956         if (fnlen != len) continue;
957
958         if (actions)
959             rpmlog(RPMLOG_DEBUG, "relocating %s to %s\n",
960                     fn, relocations[j].newPath);
961         nrelocated++;
962
963         strcpy(fn, relocations[j].newPath);
964         {   char * te = strrchr(fn, '/');
965             if (te) {
966                 if (te > fn) te++;      /* root is special */
967                 fnlen = te - fn;
968             } else
969                 te = fn + strlen(fn);
970             if (strcmp(baseNames[i], te)) { /* basename changed too? */
971                 if (!haveRelocatedBase) {
972                     /* XXX TODO: use rpmtdDup() instead */
973                     bnames.data = baseNames = duparray(baseNames, fileCount);
974                     bnames.flags |= RPMTD_PTR_ALLOCED;
975                     haveRelocatedBase = 1;
976                 }
977                 free(baseNames[i]);
978                 baseNames[i] = xstrdup(te);
979             }
980             *te = '\0';                 /* terminate new directory name */
981         }
982
983         /* Does this directory already exist in the directory list? */
984         for (j = 0; j < dirCount; j++) {
985             if (fnlen != strlen(dirNames[j]))
986                 continue;
987             if (strncmp(fn, dirNames[j], fnlen))
988                 continue;
989             break;
990         }
991         
992         if (j < dirCount) {
993             dirIndexes[i] = j;
994             continue;
995         }
996
997         /* Creating new paths is a pita */
998         dirNames = dnames.data = xrealloc(dnames.data, 
999                                sizeof(*dirNames) * (dirCount + 1));
1000
1001         free(dirNames[dirCount]);
1002         dirNames[dirCount] = xstrdup(fn);
1003         dirIndexes[i] = dirCount;
1004         dirCount++;
1005         dnames.count++;
1006     }
1007
1008     /* Finish off by relocating directories. */
1009     for (i = dirCount - 1; i >= 0; i--) {
1010         for (j = numRelocations - 1; j >= 0; j--) {
1011
1012             if (relocations[j].oldPath == NULL) /* XXX can't happen */
1013                 continue;
1014             len = strcmp(relocations[j].oldPath, "/")
1015                 ? strlen(relocations[j].oldPath)
1016                 : 0;
1017
1018             if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
1019                 continue;
1020
1021             /*
1022              * Only subdirectories or complete file paths may be relocated. We
1023              * don't check for '\0' as our directory names all end in '/'.
1024              */
1025             if (dirNames[i][len] != '/')
1026                 continue;
1027
1028             if (relocations[j].newPath) { /* Relocate the path */
1029                 char *t = NULL;
1030                 rstrscat(&t, relocations[j].newPath, (dirNames[i] + len), NULL);
1031                 /* Unfortunatly rpmCleanPath strips the trailing slash.. */
1032                 (void) rpmCleanPath(t);
1033                 rstrcat(&t, "/");
1034
1035                 if (actions)
1036                     rpmlog(RPMLOG_DEBUG,
1037                         "relocating directory %s to %s\n", dirNames[i], t);
1038                 free(dirNames[i]);
1039                 dirNames[i] = t;
1040                 nrelocated++;
1041             }
1042         }
1043     }
1044
1045     /* Save original filenames in header and replace (relocated) filenames. */
1046     if (nrelocated) {
1047         struct rpmtd_s td;
1048
1049         headerGet(h, RPMTAG_BASENAMES, &td, HEADERGET_MINMEM);
1050         rpmtdSetTag(&td, RPMTAG_ORIGBASENAMES);
1051         headerPut(h, &td, HEADERPUT_DEFAULT);
1052         rpmtdFreeData(&td);
1053
1054         headerGet(h, RPMTAG_DIRNAMES, &td, HEADERGET_MINMEM);
1055         rpmtdSetTag(&td, RPMTAG_ORIGDIRNAMES);
1056         headerPut(h, &td, HEADERPUT_DEFAULT);
1057         rpmtdFreeData(&td);
1058
1059         headerGet(h, RPMTAG_DIRINDEXES, &td, HEADERGET_MINMEM);
1060         rpmtdSetTag(&td, RPMTAG_ORIGDIRINDEXES);
1061         headerPut(h, &td, HEADERPUT_DEFAULT);
1062         rpmtdFreeData(&td);
1063
1064         if (rpmtdFromStringArray(&td, RPMTAG_BASENAMES, (const char**) baseNames, fileCount)) {
1065             headerMod(h, &td);
1066         }
1067         free(fi->bnl);
1068         headerGet(h, RPMTAG_BASENAMES, &td, fi->scareFlags);
1069         fi->fc = rpmtdCount(&td);
1070         fi->bnl = td.data;
1071
1072         if (rpmtdFromStringArray(&td, RPMTAG_DIRNAMES, (const char**) dirNames, dirCount)) {
1073             headerMod(h, &td);
1074         }
1075         free(fi->dnl);
1076         headerGet(h, RPMTAG_DIRNAMES, &td, fi->scareFlags);
1077         fi->dc = rpmtdCount(&td);
1078         fi->dnl = td.data;
1079
1080         if (rpmtdFromUint32(&td, RPMTAG_DIRINDEXES, dirIndexes, fileCount)) {
1081             headerMod(h, &td);
1082         }
1083         headerGet(h, RPMTAG_DIRINDEXES, &td, fi->scareFlags);
1084         /* Ugh, nasty games with how dil is alloced depending on scareMem */
1085         if (fi->scareFlags & HEADERGET_ALLOC)
1086             free(fi->dil);
1087         fi->dil = td.data;
1088     }
1089
1090     rpmtdFreeData(&bnames);
1091     rpmtdFreeData(&dnames);
1092     rpmtdFreeData(&dindexes);
1093     rpmtdFreeData(&fcolors);
1094     rpmtdFreeData(&fmodes);
1095     free(fn);
1096     for (i = 0; i < numRelocations; i++) {
1097         free(relocations[i].oldPath);
1098         free(relocations[i].newPath);
1099     }
1100     free(relocations);
1101     free(newDirIndexes);
1102     free(dColors);
1103
1104     return h;
1105 }
1106
1107 rpmfi rpmfiFree(rpmfi fi)
1108 {
1109     if (fi == NULL) return NULL;
1110
1111     if (fi->nrefs > 1)
1112         return rpmfiUnlink(fi, fi->Type);
1113
1114 if (_rpmfi_debug < 0)
1115 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
1116
1117     /* Free pre- and post-transaction script and interpreter strings. */
1118     fi->pretrans = _free(fi->pretrans);
1119     fi->pretransprog = _free(fi->pretransprog);
1120     fi->posttrans = _free(fi->posttrans);
1121     fi->posttransprog = _free(fi->posttransprog);
1122
1123     if (fi->fc > 0) {
1124         fi->bnl = _free(fi->bnl);
1125         fi->dnl = _free(fi->dnl);
1126
1127         fi->flinks = _free(fi->flinks);
1128         fi->flangs = _free(fi->flangs);
1129         fi->digests = _free(fi->digests);
1130
1131         fi->cdict = _free(fi->cdict);
1132
1133         fi->fuser = _free(fi->fuser);
1134         fi->fgroup = _free(fi->fgroup);
1135
1136         fi->fstates = _free(fi->fstates);
1137
1138         if (!fi->keep_header && fi->h == NULL) {
1139             fi->fmtimes = _constfree(fi->fmtimes);
1140             fi->fmodes = _free(fi->fmodes);
1141             fi->fflags = _constfree(fi->fflags);
1142             fi->vflags = _constfree(fi->vflags);
1143             fi->fsizes = _constfree(fi->fsizes);
1144             fi->frdevs = _constfree(fi->frdevs);
1145             fi->finodes = _constfree(fi->finodes);
1146             fi->dil = _free(fi->dil);
1147
1148             fi->fcolors = _constfree(fi->fcolors);
1149             fi->fcdictx = _constfree(fi->fcdictx);
1150             fi->ddict = _constfree(fi->ddict);
1151             fi->fddictx = _constfree(fi->fddictx);
1152             fi->fddictn = _constfree(fi->fddictn);
1153
1154         }
1155     }
1156
1157     fi->fsm = freeFSM(fi->fsm);
1158
1159     fi->fn = _free(fi->fn);
1160     fi->apath = _free(fi->apath);
1161     fi->fmapflags = _free(fi->fmapflags);
1162
1163     fi->obnl = _free(fi->obnl);
1164     fi->odnl = _free(fi->odnl);
1165
1166     fi->actions = _free(fi->actions);
1167     fi->replacedSizes = _free(fi->replacedSizes);
1168     fi->replaced = _free(fi->replaced);
1169
1170     fi->h = headerFree(fi->h);
1171
1172     (void) rpmfiUnlink(fi, fi->Type);
1173     memset(fi, 0, sizeof(*fi));         /* XXX trash and burn */
1174     fi = _free(fi);
1175
1176     return NULL;
1177 }
1178
1179 #define _hgfi(_h, _tag, _td, _flags, _data) \
1180     if (headerGet((_h), (_tag), (_td), (_flags))) \
1181         _data = (td.data)
1182
1183 rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, int scareMem)
1184 {
1185     rpmte p;
1186     rpmfi fi = NULL;
1187     const char * Type;
1188     rpm_loff_t *asize = NULL;
1189     int dnlmax, bnlmax;
1190     unsigned char * t;
1191     struct rpmtd_s fdigests, digalgo;
1192     struct rpmtd_s td;
1193     headerGetFlags scareFlags = scareMem ? HEADERGET_MINMEM : HEADERGET_ALLOC;
1194     headerGetFlags defFlags = HEADERGET_ALLOC;
1195     int len;
1196     int i;
1197
1198     if (tagN == RPMTAG_BASENAMES) {
1199         Type = "Files";
1200     } else {
1201         Type = "?Type?";
1202         goto exit;
1203     }
1204
1205     fi = xcalloc(1, sizeof(*fi));
1206     if (fi == NULL)     /* XXX can't happen */
1207         goto exit;
1208
1209     fi->magic = RPMFIMAGIC;
1210     fi->Type = Type;
1211     fi->i = -1;
1212     fi->tagN = tagN;
1213
1214     fi->hae = (HAE_t) headerAddEntry;
1215     fi->hme = (HME_t) headerModifyEntry;
1216     fi->scareFlags = scareFlags;
1217
1218     fi->h = (scareMem ? headerLink(h) : NULL);
1219
1220     if (fi->fsm == NULL)
1221         fi->fsm = newFSM();
1222
1223     if (headerGet(h, RPMTAG_LONGARCHIVESIZE, &td, HEADERGET_EXT)) {
1224         asize = rpmtdGetUint64(&td);
1225     }
1226     /* 0 means unknown */
1227     fi->archivePos = 0;
1228     fi->archiveSize = asize ? *asize : 0;
1229     rpmtdFreeData(&td);
1230
1231     /* Extract pre- and post-transaction script and interpreter strings. */
1232     _hgfi(h, RPMTAG_PRETRANS, &td, defFlags, fi->pretrans);
1233     _hgfi(h, RPMTAG_PRETRANSPROG, &td, defFlags, fi->pretransprog);
1234     _hgfi(h, RPMTAG_POSTTRANS, &td, defFlags, fi->posttrans);
1235     _hgfi(h, RPMTAG_POSTTRANSPROG, &td, defFlags, fi->posttransprog);
1236
1237     _hgfi(h, RPMTAG_BASENAMES, &td, defFlags, fi->bnl);
1238     fi->fc = rpmtdCount(&td);
1239     if (fi->fc == 0) {
1240         goto exit;
1241     }
1242
1243     _hgfi(h, RPMTAG_DIRNAMES, &td, defFlags, fi->dnl);
1244     fi->dc = rpmtdCount(&td);
1245     _hgfi(h, RPMTAG_DIRINDEXES, &td, scareFlags, fi->dil);
1246     _hgfi(h, RPMTAG_FILEMODES, &td, scareFlags, fi->fmodes);
1247     _hgfi(h, RPMTAG_FILEFLAGS, &td, scareFlags, fi->fflags);
1248     _hgfi(h, RPMTAG_FILEVERIFYFLAGS, &td, scareFlags, fi->vflags);
1249     _hgfi(h, RPMTAG_FILESIZES, &td, scareFlags, fi->fsizes);
1250
1251     _hgfi(h, RPMTAG_FILECOLORS, &td, scareFlags, fi->fcolors);
1252     fi->color = 0;
1253     if (fi->fcolors != NULL)
1254     for (i = 0; i < fi->fc; i++)
1255         fi->color |= fi->fcolors[i];
1256
1257     _hgfi(h, RPMTAG_CLASSDICT, &td, scareFlags, fi->cdict);
1258     fi->ncdict = rpmtdCount(&td);
1259     _hgfi(h, RPMTAG_FILECLASS, &td, scareFlags, fi->fcdictx);
1260     _hgfi(h, RPMTAG_DEPENDSDICT, &td, scareFlags, fi->ddict);
1261     fi->nddict = rpmtdCount(&td);
1262     _hgfi(h, RPMTAG_FILEDEPENDSX, &td, scareFlags, fi->fddictx);
1263     _hgfi(h, RPMTAG_FILEDEPENDSN, &td, scareFlags, fi->fddictn);
1264
1265     _hgfi(h, RPMTAG_FILESTATES, &td, defFlags, fi->fstates);
1266     if (fi->fstates == NULL)
1267         fi->fstates = xcalloc(fi->fc, sizeof(*fi->fstates));
1268
1269     fi->action = FA_UNKNOWN;
1270     fi->flags = 0;
1271
1272 if (fi->actions == NULL)
1273         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1274
1275     fi->keep_header = (scareMem ? 1 : 0);
1276
1277     /* XXX TR_REMOVED needs CPIO_MAP_{ABSOLUTE,ADDDOT} CPIO_ALL_HARDLINKS */
1278     fi->mapflags =
1279                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
1280
1281     _hgfi(h, RPMTAG_FILELINKTOS, &td, defFlags, fi->flinks);
1282     _hgfi(h, RPMTAG_FILELANGS, &td, defFlags, fi->flangs);
1283
1284     /* See if the package has non-md5 file digests */
1285     fi->digestalgo = PGPHASHALGO_MD5;
1286     if (headerGet(h, RPMTAG_FILEDIGESTALGO, &digalgo, HEADERGET_MINMEM)) {
1287         pgpHashAlgo *algo = rpmtdGetUint32(&digalgo);
1288         /* Hmm, what to do with unknown digest algorithms? */
1289         if (algo && rpmDigestLength(*algo) != 0) {
1290             fi->digestalgo = *algo;
1291         }
1292     }
1293
1294     fi->digests = NULL;
1295     /* grab hex digests from header and store in binary format */
1296     if (headerGet(h, RPMTAG_FILEDIGESTS, &fdigests, HEADERGET_MINMEM)) {
1297         const char *fdigest;
1298         size_t diglen = rpmDigestLength(fi->digestalgo);
1299         fi->digests = t = xmalloc(rpmtdCount(&fdigests) * diglen);
1300
1301         while ((fdigest = rpmtdNextString(&fdigests))) {
1302             if (!(fdigest && *fdigest != '\0')) {
1303                 memset(t, 0, diglen);
1304                 t += diglen;
1305                 continue;
1306             }
1307             for (int j = 0; j < diglen; j++, t++, fdigest += 2)
1308                 *t = (rnibble(fdigest[0]) << 4) | rnibble(fdigest[1]);
1309         }
1310         rpmtdFreeData(&fdigests);
1311     }
1312
1313     /* XXX TR_REMOVED doesn;t need fmtimes, frdevs, finodes */
1314     _hgfi(h, RPMTAG_FILEMTIMES, &td, scareFlags, fi->fmtimes);
1315     _hgfi(h, RPMTAG_FILERDEVS, &td, scareFlags, fi->frdevs);
1316     _hgfi(h, RPMTAG_FILEINODES, &td, scareFlags, fi->finodes);
1317
1318     fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1319
1320     _hgfi(h, RPMTAG_FILEUSERNAME, &td, defFlags, fi->fuser);
1321     _hgfi(h, RPMTAG_FILEGROUPNAME, &td, defFlags, fi->fgroup);
1322
1323     if (ts != NULL)
1324     if (fi != NULL)
1325     if ((p = rpmtsRelocateElement(ts)) != NULL && rpmteType(p) == TR_ADDED
1326      && !headerIsSource(h)
1327      && !headerIsEntry(h, RPMTAG_ORIGBASENAMES))
1328     {
1329         Header foo;
1330
1331 /* XXX DYING */
1332 if (fi->actions == NULL)
1333         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1334         /* FIX: fi-digests undefined */
1335         foo = relocateFileList(ts, fi, h, fi->actions);
1336         fi->h = headerFree(fi->h);
1337         fi->h = headerLink(foo);
1338         foo = headerFree(foo);
1339     }
1340
1341     if (!scareMem) {
1342         fi->h = headerFree(fi->h);
1343     }
1344
1345     dnlmax = -1;
1346     for (i = 0; i < fi->dc; i++) {
1347         if ((len = strlen(fi->dnl[i])) > dnlmax)
1348             dnlmax = len;
1349     }
1350     bnlmax = -1;
1351     for (i = 0; i < fi->fc; i++) {
1352         if ((len = strlen(fi->bnl[i])) > bnlmax)
1353             bnlmax = len;
1354     }
1355     fi->fnlen = dnlmax + bnlmax + 1;
1356     fi->fn = NULL;
1357
1358     fi->dperms = 0755;
1359     fi->fperms = 0644;
1360
1361 exit:
1362 if (_rpmfi_debug < 0)
1363 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
1364
1365     /* FIX: rpmfi null annotations */
1366     return rpmfiLink(fi, (fi ? fi->Type : NULL));
1367 }