- drill ts/fi through verify mode, add methods to keep fi abstract.
[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 "fsm.h"        /* XXX newFSM and CPIO_MAP_* */
9
10 #include "depends.h"
11 #include "misc.h"       /* XXX stripTrailingChar */
12
13 #include "debug.h"
14
15 /*@access TFI_t @*/
16 /*@access transactionElement @*/
17 /*@access rpmTransactionSet @*/ /* XXX for ts->ignoreSet and ts->probs */
18
19 /*@unchecked@*/
20 static int _fi_debug = 0;
21
22 TFI_t XrpmfiUnlink(TFI_t fi, const char * msg, const char * fn, unsigned ln)
23 {
24     if (fi == NULL) return NULL;
25 /*@-modfilesystem@*/
26 if (_fi_debug && msg != NULL)
27 fprintf(stderr, "--> fi %p -- %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
28 /*@=modfilesystem@*/
29     fi->nrefs--;
30     return NULL;
31 }
32
33 TFI_t XrpmfiLink(TFI_t fi, const char * msg, const char * fn, unsigned ln)
34 {
35     if (fi == NULL) return NULL;
36     fi->nrefs++;
37 /*@-modfilesystem@*/
38 if (_fi_debug && msg != NULL)
39 fprintf(stderr, "--> fi %p ++ %d %s at %s:%u\n", fi, fi->nrefs, msg, fn, ln);
40 /*@=modfilesystem@*/
41     /*@-refcounttrans@*/ return fi; /*@=refcounttrans@*/
42 }
43
44 fnpyKey rpmfiGetKey(TFI_t fi)
45 {
46     return (fi != NULL ? teGetKey(fi->te) : NULL);
47 }
48
49 int tfiGetFC(TFI_t fi)
50 {
51     return (fi != NULL ? fi->fc : 0);
52 }
53
54 int tfiGetDC(TFI_t fi)
55 {
56     return (fi != NULL ? fi->dc : 0);
57 }
58
59 #ifdef  NOTYET
60 int tfiGetDI(TFI_t fi)
61 {
62 }
63 #endif
64
65 int tfiGetFX(TFI_t fi)
66 {
67     return (fi != NULL ? fi->i : -1);
68 }
69
70 int tfiSetFX(TFI_t fi, int fx)
71 {
72     int i = -1;
73
74     if (fi != NULL && fx >= 0 && fx < fi->fc) {
75         i = fi->i;
76         fi->i = fx;
77         fi->j = fi->dil[fi->i];
78     }
79     return i;
80 }
81
82 int tfiGetDX(TFI_t fi)
83 {
84     return (fi != NULL ? fi->j : -1);
85 }
86
87 int tfiSetDX(TFI_t fi, int dx)
88 {
89     int j = -1;
90
91     if (fi != NULL && dx >= 0 && dx < fi->dc) {
92         j = fi->j;
93         fi->j = dx;
94     }
95     return j;
96 }
97
98 const char * tfiGetBN(TFI_t fi)
99 {
100     const char * BN = NULL;
101
102     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
103         if (fi->bnl != NULL)
104             BN = fi->bnl[fi->i];
105     }
106     return BN;
107 }
108
109 const char * tfiGetDN(TFI_t fi)
110 {
111     const char * DN = NULL;
112
113     if (fi != NULL && fi->j >= 0 && fi->j < fi->dc) {
114         if (fi->dnl != NULL)
115             DN = fi->dnl[fi->j];
116     }
117     return DN;
118 }
119
120 const char * tfiGetFN(TFI_t fi)
121 {
122     const char * FN = "";
123
124     /*@-branchstate@*/
125     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
126         char * t;
127         if (fi->fn == NULL)
128             fi->fn = xmalloc(fi->fnlen);
129         FN = t = fi->fn;
130         *t = '\0';
131         t = stpcpy(t, fi->dnl[fi->dil[fi->i]]);
132         t = stpcpy(t, fi->bnl[fi->i]);
133     }
134     /*@=branchstate@*/
135     return FN;
136 }
137
138 int_32 tfiGetFFlags(TFI_t fi)
139 {
140     int_32 FFlags = 0;
141
142     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
143         if (fi->fflags != NULL)
144             FFlags = fi->fflags[fi->i];
145     }
146     return FFlags;
147 }
148
149 int_32 tfiGetVFlags(TFI_t fi)
150 {
151     int_32 VFlags = 0;
152
153     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
154         if (fi->vflags != NULL)
155             VFlags = fi->vflags[fi->i];
156     }
157     return VFlags;
158 }
159
160 int_16 tfiGetFMode(TFI_t fi)
161 {
162     int_16 fmode = 0;
163
164     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
165         if (fi->fmodes != NULL)
166             fmode = fi->fmodes[fi->i];
167     }
168     return fmode;
169 }
170
171 rpmfileState tfiGetFState(TFI_t fi)
172 {
173     char fstate = 0;
174
175     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
176         if (fi->fstates != NULL)
177             fstate = fi->fstates[fi->i];
178     }
179     return fstate;
180 }
181
182 const unsigned char * tfiGetMD5(TFI_t fi)
183 {
184     unsigned char * md5 = NULL;
185
186     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
187         if (fi->md5s != NULL)
188             md5 = fi->md5s + (16 * fi->i);
189     }
190     return md5;
191 }
192
193 const char * tfiGetFLink(TFI_t fi)
194 {
195     const char * flink = NULL;
196
197     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
198         if (fi->flinks != NULL)
199             flink = fi->flinks[fi->i];
200     }
201     return flink;
202 }
203
204 int_32 tfiGetFSize(TFI_t fi)
205 {
206     int_32 fsize = 0;
207
208     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
209         if (fi->fsizes != NULL)
210             fsize = fi->fsizes[fi->i];
211     }
212     return fsize;
213 }
214
215 int_16 tfiGetFRdev(TFI_t fi)
216 {
217     int_16 frdev = 0;
218
219     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
220         if (fi->frdevs != NULL)
221             frdev = fi->frdevs[fi->i];
222     }
223     return frdev;
224 }
225
226 int_32 tfiGetFMtime(TFI_t fi)
227 {
228     int_32 fmtime = 0;
229
230     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
231         if (fi->fmtimes != NULL)
232             fmtime = fi->fmtimes[fi->i];
233     }
234     return fmtime;
235 }
236
237 const char * tfiGetFUser(TFI_t fi)
238 {
239     const char * fuser = NULL;
240
241     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
242         if (fi->fuser != NULL)
243             fuser = fi->fuser[fi->i];
244     }
245     return fuser;
246 }
247
248 const char * tfiGetFGroup(TFI_t fi)
249 {
250     const char * fgroup = NULL;
251
252     if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) {
253         if (fi->fgroup != NULL)
254             fgroup = fi->fgroup[fi->i];
255     }
256     return fgroup;
257 }
258
259 int tfiNext(TFI_t fi)
260 {
261     int i = -1;
262
263     if (fi != NULL && ++fi->i >= 0) {
264         if (fi->i < fi->fc) {
265             i = fi->i;
266             if (fi->dil != NULL)
267                 fi->j = fi->dil[fi->i];
268         } else
269             fi->i = -1;
270
271 /*@-modfilesystem @*/
272 if (_fi_debug  < 0 && i != -1)
273 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] : ""));
274 /*@=modfilesystem @*/
275
276     }
277
278     return i;
279 }
280
281 TFI_t tfiInit(TFI_t fi, int fx)
282 {
283     if (fi != NULL) {
284         if (fx >= 0 && fx < fi->fc) {
285             fi->i = fx - 1;
286             fi->j = -1;
287         }
288 #ifdef  DYING
289         else
290             fi = NULL;
291 #endif
292     }
293
294     /*@-refcounttrans@*/
295     return fi;
296     /*@=refcounttrans@*/
297 }
298
299 int tdiNext(TFI_t fi)
300 {
301     int j = -1;
302
303     if (fi != NULL && ++fi->j >= 0) {
304         if (fi->j < fi->dc)
305             j = fi->j;
306         else
307             fi->j = -1;
308
309 /*@-modfilesystem @*/
310 if (_fi_debug  < 0 && j != -1)
311 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, (fi->Type ? fi->Type : "?Type?"), j);
312 /*@=modfilesystem @*/
313
314     }
315
316     return j;
317 }
318
319 TFI_t tdiInit(TFI_t fi, int dx)
320 {
321     if (fi != NULL) {
322         if (dx >= 0 && dx < fi->fc)
323             fi->j = dx - 1;
324         else
325             fi = NULL;
326     }
327
328     /*@-refcounttrans@*/
329     return fi;
330     /*@=refcounttrans@*/
331 }
332
333 /**
334  * Identify a file type.
335  * @param ft            file type
336  * @return              string to identify a file type
337  */
338 static /*@observer@*/
339 const char *const ftstring (fileTypes ft)
340         /*@*/
341 {
342     switch (ft) {
343     case XDIR:  return "directory";
344     case CDEV:  return "char dev";
345     case BDEV:  return "block dev";
346     case LINK:  return "link";
347     case SOCK:  return "sock";
348     case PIPE:  return "fifo/pipe";
349     case REG:   return "file";
350     default:    return "unknown file type";
351     }
352     /*@notreached@*/
353 }
354
355 fileTypes whatis(uint_16 mode)
356 {
357     if (S_ISDIR(mode))  return XDIR;
358     if (S_ISCHR(mode))  return CDEV;
359     if (S_ISBLK(mode))  return BDEV;
360     if (S_ISLNK(mode))  return LINK;
361     if (S_ISSOCK(mode)) return SOCK;
362     if (S_ISFIFO(mode)) return PIPE;
363     return REG;
364 }
365
366 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
367
368 /**
369  * Relocate files in header.
370  * @todo multilib file dispositions need to be checked.
371  * @param ts            transaction set
372  * @param fi            transaction element file info
373  * @param origH         package header
374  * @param actions       file dispositions
375  * @return              header with relocated files
376  */
377 static
378 Header relocateFileList(const rpmTransactionSet ts, TFI_t fi,
379                 Header origH, fileAction * actions)
380         /*@modifies ts, fi, origH, actions @*/
381 {
382     transactionElement p = fi->te;
383     HGE_t hge = fi->hge;
384     HAE_t hae = fi->hae;
385     HME_t hme = fi->hme;
386     HFD_t hfd = (fi->hfd ? fi->hfd : headerFreeData);
387     static int _printed = 0;
388     int allowBadRelocate = (ts->ignoreSet & RPMPROB_FILTER_FORCERELOCATE);
389     rpmRelocation * relocations = NULL;
390     int numRelocations;
391     const char ** validRelocations;
392     rpmTagType validType;
393     int numValid;
394     const char ** baseNames;
395     const char ** dirNames;
396     int_32 * dirIndexes;
397     int_32 * newDirIndexes;
398     int_32 fileCount;
399     int_32 dirCount;
400     uint_32 * fFlags = NULL;
401     uint_16 * fModes = NULL;
402     char * skipDirList;
403     Header h;
404     int nrelocated = 0;
405     int fileAlloced = 0;
406     char * fn = NULL;
407     int haveRelocatedFile = 0;
408     int reldel = 0;
409     int len;
410     int i, j, xx;
411
412     if (!hge(origH, RPMTAG_PREFIXES, &validType,
413                         (void **) &validRelocations, &numValid))
414         numValid = 0;
415
416     numRelocations = 0;
417     if (p->relocs)
418         while (p->relocs[numRelocations].newPath ||
419                p->relocs[numRelocations].oldPath)
420             numRelocations++;
421
422     /*
423      * If no relocations are specified (usually the case), then return the
424      * original header. If there are prefixes, however, then INSTPREFIXES
425      * should be added, but, since relocateFileList() can be called more
426      * than once for the same header, don't bother if already present.
427      */
428     if (p->relocs == NULL || numRelocations == 0) {
429         if (numValid) {
430             if (!headerIsEntry(origH, RPMTAG_INSTPREFIXES))
431                 xx = hae(origH, RPMTAG_INSTPREFIXES,
432                         validType, validRelocations, numValid);
433             validRelocations = hfd(validRelocations, validType);
434         }
435         /* XXX FIXME multilib file actions need to be checked. */
436         return headerLink(origH, "relocate(return)");
437     }
438
439     h = headerLink(origH, "relocate(orig)");
440
441     relocations = alloca(sizeof(*relocations) * numRelocations);
442
443     /* Build sorted relocation list from raw relocations. */
444     for (i = 0; i < numRelocations; i++) {
445         char * t;
446
447         /*
448          * Default relocations (oldPath == NULL) are handled in the UI,
449          * not rpmlib.
450          */
451         if (p->relocs[i].oldPath == NULL) continue; /* XXX can't happen */
452
453         /* FIXME: Trailing /'s will confuse us greatly. Internal ones will 
454            too, but those are more trouble to fix up. :-( */
455         t = alloca_strdup(p->relocs[i].oldPath);
456         /*@-branchstate@*/
457         relocations[i].oldPath = (t[0] == '/' && t[1] == '\0')
458             ? t
459             : stripTrailingChar(t, '/');
460         /*@=branchstate@*/
461
462         /* An old path w/o a new path is valid, and indicates exclusion */
463         if (p->relocs[i].newPath) {
464             int del;
465
466             t = alloca_strdup(p->relocs[i].newPath);
467             /*@-branchstate@*/
468             relocations[i].newPath = (t[0] == '/' && t[1] == '\0')
469                 ? t
470                 : stripTrailingChar(t, '/');
471             /*@=branchstate@*/
472
473             /*@-nullpass@*/     /* FIX:  relocations[i].oldPath == NULL */
474             /* Verify that the relocation's old path is in the header. */
475             for (j = 0; j < numValid; j++) {
476                 if (!strcmp(validRelocations[j], relocations[i].oldPath))
477                     /*@innerbreak@*/ break;
478             }
479
480             /* XXX actions check prevents problem from being appended twice. */
481             if (j == numValid && !allowBadRelocate && actions) {
482                 rpmProblemSetAppend(ts->probs, RPMPROB_BADRELOCATE,
483                         p->NEVR, p->key,
484                         relocations[i].oldPath, NULL, NULL, 0);
485             }
486             del =
487                 strlen(relocations[i].newPath) - strlen(relocations[i].oldPath);
488             /*@=nullpass@*/
489
490             if (del > reldel)
491                 reldel = del;
492         } else {
493             relocations[i].newPath = NULL;
494         }
495     }
496
497     /* stupid bubble sort, but it's probably faster here */
498     for (i = 0; i < numRelocations; i++) {
499         int madeSwap;
500         madeSwap = 0;
501         for (j = 1; j < numRelocations; j++) {
502             rpmRelocation tmpReloc;
503             if (relocations[j - 1].oldPath == NULL || /* XXX can't happen */
504                 relocations[j    ].oldPath == NULL || /* XXX can't happen */
505         strcmp(relocations[j - 1].oldPath, relocations[j].oldPath) <= 0)
506                 /*@innercontinue@*/ continue;
507             /*@-usereleased@*/ /* LCL: ??? */
508             tmpReloc = relocations[j - 1];
509             relocations[j - 1] = relocations[j];
510             relocations[j] = tmpReloc;
511             /*@=usereleased@*/
512             madeSwap = 1;
513         }
514         if (!madeSwap) break;
515     }
516
517     if (!_printed) {
518         _printed = 1;
519         rpmMessage(RPMMESS_DEBUG, _("========== relocations\n"));
520         for (i = 0; i < numRelocations; i++) {
521             if (relocations[i].oldPath == NULL) continue; /* XXX can't happen */
522             if (relocations[i].newPath == NULL)
523                 rpmMessage(RPMMESS_DEBUG, _("%5d exclude  %s\n"),
524                         i, relocations[i].oldPath);
525             else
526                 rpmMessage(RPMMESS_DEBUG, _("%5d relocate %s -> %s\n"),
527                         i, relocations[i].oldPath, relocations[i].newPath);
528         }
529     }
530
531     /* Add relocation values to the header */
532     if (numValid) {
533         const char ** actualRelocations;
534         int numActual;
535
536         actualRelocations = xmalloc(numValid * sizeof(*actualRelocations));
537         numActual = 0;
538         for (i = 0; i < numValid; i++) {
539             for (j = 0; j < numRelocations; j++) {
540                 if (relocations[j].oldPath == NULL || /* XXX can't happen */
541                     strcmp(validRelocations[i], relocations[j].oldPath))
542                     /*@innercontinue@*/ continue;
543                 /* On install, a relocate to NULL means skip the path. */
544                 if (relocations[j].newPath) {
545                     actualRelocations[numActual] = relocations[j].newPath;
546                     numActual++;
547                 }
548                 /*@innerbreak@*/ break;
549             }
550             if (j == numRelocations) {
551                 actualRelocations[numActual] = validRelocations[i];
552                 numActual++;
553             }
554         }
555
556         if (numActual)
557             xx = hae(h, RPMTAG_INSTPREFIXES, RPM_STRING_ARRAY_TYPE,
558                        (void **) actualRelocations, numActual);
559
560         actualRelocations = _free(actualRelocations);
561         validRelocations = hfd(validRelocations, validType);
562     }
563
564     xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &baseNames, &fileCount);
565     xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
566     xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &dirNames, &dirCount);
567     xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fFlags, NULL);
568     xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fModes, NULL);
569
570     skipDirList = alloca(dirCount * sizeof(*skipDirList));
571     memset(skipDirList, 0, dirCount * sizeof(*skipDirList));
572
573     newDirIndexes = alloca(sizeof(*newDirIndexes) * fileCount);
574     memcpy(newDirIndexes, dirIndexes, sizeof(*newDirIndexes) * fileCount);
575     dirIndexes = newDirIndexes;
576
577     /*
578      * For all relocations, we go through sorted file/relocation lists 
579      * backwards so that /usr/local relocations take precedence over /usr 
580      * ones.
581      */
582
583     /* Relocate individual paths. */
584
585     for (i = fileCount - 1; i >= 0; i--) {
586         fileTypes ft;
587         int fnlen;
588
589         /*
590          * If only adding libraries of different arch into an already
591          * installed package, skip all other files.
592          */
593         if (p->multiLib && !isFileMULTILIB((fFlags[i]))) {
594             if (actions) {
595                 actions[i] = FA_SKIPMULTILIB;
596                 rpmMessage(RPMMESS_DEBUG, _("excluding multilib path %s%s\n"), 
597                         dirNames[dirIndexes[i]], baseNames[i]);
598             }
599             continue;
600         }
601
602         len = reldel +
603                 strlen(dirNames[dirIndexes[i]]) + strlen(baseNames[i]) + 1;
604         /*@-branchstate@*/
605         if (len >= fileAlloced) {
606             fileAlloced = len * 2;
607             fn = xrealloc(fn, fileAlloced);
608         }
609         /*@=branchstate@*/
610         *fn = '\0';
611         fnlen = stpcpy( stpcpy(fn, dirNames[dirIndexes[i]]), baseNames[i]) - fn;
612
613         /*
614          * See if this file path needs relocating.
615          */
616         /*
617          * XXX FIXME: Would a bsearch of the (already sorted) 
618          * relocation list be a good idea?
619          */
620         for (j = numRelocations - 1; j >= 0; j--) {
621             if (relocations[j].oldPath == NULL) /* XXX can't happen */
622                 /*@innercontinue@*/ continue;
623             len = strcmp(relocations[j].oldPath, "/")
624                 ? strlen(relocations[j].oldPath)
625                 : 0;
626
627             if (fnlen < len)
628                 /*@innercontinue@*/ continue;
629             /*
630              * Only subdirectories or complete file paths may be relocated. We
631              * don't check for '\0' as our directory names all end in '/'.
632              */
633             if (!(fn[len] == '/' || fnlen == len))
634                 /*@innercontinue@*/ continue;
635
636             if (strncmp(relocations[j].oldPath, fn, len))
637                 /*@innercontinue@*/ continue;
638             /*@innerbreak@*/ break;
639         }
640         if (j < 0) continue;
641
642         ft = whatis(fModes[i]);
643
644         /* On install, a relocate to NULL means skip the path. */
645         if (relocations[j].newPath == NULL) {
646             if (ft == XDIR) {
647                 /* Start with the parent, looking for directory to exclude. */
648                 for (j = dirIndexes[i]; j < dirCount; j++) {
649                     len = strlen(dirNames[j]) - 1;
650                     while (len > 0 && dirNames[j][len-1] == '/') len--;
651                     if (fnlen != len)
652                         /*@innercontinue@*/ continue;
653                     if (strncmp(fn, dirNames[j], fnlen))
654                         /*@innercontinue@*/ continue;
655                     /*@innerbreak@*/ break;
656                 }
657                 if (j < dirCount)
658                     skipDirList[j] = 1;
659             }
660             if (actions) {
661                 actions[i] = FA_SKIPNSTATE;
662                 rpmMessage(RPMMESS_DEBUG, _("excluding %s %s\n"),
663                         ftstring(ft), fn);
664             }
665             continue;
666         }
667
668         /* Relocation on full paths only, please. */
669         if (fnlen != len) continue;
670
671         if (actions)
672             rpmMessage(RPMMESS_DEBUG, _("relocating %s to %s\n"),
673                     fn, relocations[j].newPath);
674         nrelocated++;
675
676         strcpy(fn, relocations[j].newPath);
677         {   char * te = strrchr(fn, '/');
678             if (te) {
679                 if (te > fn) te++;      /* root is special */
680                 fnlen = te - fn;
681             } else
682                 te = fn + strlen(fn);
683             /*@-nullpass -nullderef@*/  /* LCL: te != NULL here. */
684             if (strcmp(baseNames[i], te)) /* basename changed too? */
685                 baseNames[i] = alloca_strdup(te);
686             *te = '\0';                 /* terminate new directory name */
687             /*@=nullpass =nullderef@*/
688         }
689
690         /* Does this directory already exist in the directory list? */
691         for (j = 0; j < dirCount; j++) {
692             if (fnlen != strlen(dirNames[j]))
693                 /*@innercontinue@*/ continue;
694             if (strncmp(fn, dirNames[j], fnlen))
695                 /*@innercontinue@*/ continue;
696             /*@innerbreak@*/ break;
697         }
698         
699         if (j < dirCount) {
700             dirIndexes[i] = j;
701             continue;
702         }
703
704         /* Creating new paths is a pita */
705         if (!haveRelocatedFile) {
706             const char ** newDirList;
707
708             haveRelocatedFile = 1;
709             newDirList = xmalloc((dirCount + 1) * sizeof(*newDirList));
710             for (j = 0; j < dirCount; j++)
711                 newDirList[j] = alloca_strdup(dirNames[j]);
712             dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
713             dirNames = newDirList;
714         } else {
715             dirNames = xrealloc(dirNames, 
716                                sizeof(*dirNames) * (dirCount + 1));
717         }
718
719         dirNames[dirCount] = alloca_strdup(fn);
720         dirIndexes[i] = dirCount;
721         dirCount++;
722     }
723
724     /* Finish off by relocating directories. */
725     for (i = dirCount - 1; i >= 0; i--) {
726         for (j = numRelocations - 1; j >= 0; j--) {
727
728             if (relocations[j].oldPath == NULL) /* XXX can't happen */
729                 /*@innercontinue@*/ continue;
730             len = strcmp(relocations[j].oldPath, "/")
731                 ? strlen(relocations[j].oldPath)
732                 : 0;
733
734             if (len && strncmp(relocations[j].oldPath, dirNames[i], len))
735                 /*@innercontinue@*/ continue;
736
737             /*
738              * Only subdirectories or complete file paths may be relocated. We
739              * don't check for '\0' as our directory names all end in '/'.
740              */
741             if (dirNames[i][len] != '/')
742                 /*@innercontinue@*/ continue;
743
744             if (relocations[j].newPath) { /* Relocate the path */
745                 const char * s = relocations[j].newPath;
746                 char * t = alloca(strlen(s) + strlen(dirNames[i]) - len + 1);
747
748                 (void) stpcpy( stpcpy(t, s) , dirNames[i] + len);
749                 if (actions)
750                     rpmMessage(RPMMESS_DEBUG,
751                         _("relocating directory %s to %s\n"), dirNames[i], t);
752                 dirNames[i] = t;
753                 nrelocated++;
754             }
755         }
756     }
757
758     /* Save original filenames in header and replace (relocated) filenames. */
759     if (nrelocated) {
760         int c;
761         void * d;
762         rpmTagType t;
763
764         d = NULL;
765         xx = hge(h, RPMTAG_BASENAMES, &t, &d, &c);
766         xx = hae(h, RPMTAG_ORIGBASENAMES, t, d, c);
767         d = hfd(d, t);
768
769         d = NULL;
770         xx = hge(h, RPMTAG_DIRNAMES, &t, &d, &c);
771         xx = hae(h, RPMTAG_ORIGDIRNAMES, t, d, c);
772         d = hfd(d, t);
773
774         d = NULL;
775         xx = hge(h, RPMTAG_DIRINDEXES, &t, &d, &c);
776         xx = hae(h, RPMTAG_ORIGDIRINDEXES, t, d, c);
777         d = hfd(d, t);
778
779         xx = hme(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
780                           baseNames, fileCount);
781         fi->bnl = hfd(fi->bnl, RPM_STRING_ARRAY_TYPE);
782         xx = hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc);
783
784         xx = hme(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
785                           dirNames, dirCount);
786         fi->dnl = hfd(fi->dnl, RPM_STRING_ARRAY_TYPE);
787         xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
788
789         xx = hme(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE,
790                           dirIndexes, fileCount);
791         xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
792     }
793
794     baseNames = hfd(baseNames, RPM_STRING_ARRAY_TYPE);
795     dirNames = hfd(dirNames, RPM_STRING_ARRAY_TYPE);
796     fn = _free(fn);
797
798     return h;
799 }
800
801 TFI_t fiFree(TFI_t fi, int freefimem)
802 {
803     HFD_t hfd = headerFreeData;
804
805     if (fi == NULL) return NULL;
806
807     if (fi->nrefs > 1)
808         return rpmfiUnlink(fi, fi->Type);
809
810 /*@-modfilesystem@*/
811 if (_fi_debug < 0)
812 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, fi->Type, fi->fc);
813 /*@=modfilesystem@*/
814
815     /*@-branchstate@*/
816     if (fi->fc > 0) {
817         fi->bnl = hfd(fi->bnl, -1);
818         fi->dnl = hfd(fi->dnl, -1);
819
820         fi->flinks = hfd(fi->flinks, -1);
821         fi->flangs = hfd(fi->flangs, -1);
822         fi->fmd5s = hfd(fi->fmd5s, -1);
823         fi->md5s = _free(fi->md5s);
824
825         fi->fuser = hfd(fi->fuser, -1);
826         fi->fuids = _free(fi->fuids);
827         fi->fgroup = hfd(fi->fgroup, -1);
828         fi->fgids = _free(fi->fgids);
829
830         fi->fstates = _free(fi->fstates);
831
832         /*@-evalorder@*/
833         if (!fi->keep_header && fi->h == NULL) {
834             fi->fmtimes = _free(fi->fmtimes);
835             fi->fmodes = _free(fi->fmodes);
836             fi->fflags = _free(fi->fflags);
837             fi->vflags = _free(fi->vflags);
838             fi->fsizes = _free(fi->fsizes);
839             fi->frdevs = _free(fi->frdevs);
840             fi->dil = _free(fi->dil);
841         }
842         /*@=evalorder@*/
843     }
844     /*@=branchstate@*/
845
846     fi->fsm = freeFSM(fi->fsm);
847
848     fi->fn = _free(fi->fn);
849     fi->apath = _free(fi->apath);
850     fi->fmapflags = _free(fi->fmapflags);
851
852     fi->obnl = hfd(fi->obnl, -1);
853     fi->odnl = hfd(fi->odnl, -1);
854
855     fi->actions = _free(fi->actions);
856     fi->replacedSizes = _free(fi->replacedSizes);
857     fi->replaced = _free(fi->replaced);
858
859     fi->h = headerFree(fi->h, fi->Type);
860
861     /*@-nullstate -refcounttrans -usereleased@*/
862     (void) rpmfiUnlink(fi, fi->Type);
863     /*@-branchstate@*/
864     if (freefimem) {
865         memset(fi, 0, sizeof(*fi));             /* XXX trash and burn */
866         fi = _free(fi);
867     }
868     /*@=branchstate@*/
869     /*@=nullstate =refcounttrans =usereleased@*/
870
871     return NULL;
872 }
873
874 /**
875  * Convert hex to binary nibble.
876  * @param c             hex character
877  * @return              binary nibble
878  */
879 static inline unsigned char nibble(char c)
880         /*@*/
881 {
882     if (c >= '0' && c <= '9')
883         return (c - '0');
884     if (c >= 'A' && c <= 'F')
885         return (c - 'A') + 10;
886     if (c >= 'a' && c <= 'f')
887         return (c - 'a') + 10;
888     return 0;
889 }
890
891 #define _fdupe(_fi, _data)      \
892     if ((_fi)->_data != NULL)   \
893         (_fi)->_data = memcpy(xmalloc((_fi)->fc * sizeof(*(_fi)->_data)), \
894                         (_fi)->_data, (_fi)->fc * sizeof(*(_fi)->_data))
895
896 TFI_t fiNew(rpmTransactionSet ts, TFI_t fi,
897                 Header h, rpmTag tagN, int scareMem)
898 {
899     HGE_t hge =
900         (scareMem ? (HGE_t) headerGetEntryMinMemory : (HGE_t) headerGetEntry);
901     HFD_t hfd = headerFreeData;
902     const char * Type;
903     uint_32 * uip;
904     int malloced = 0;
905     int dnlmax, bnlmax;
906     unsigned char * t;
907     int len;
908     int xx;
909     int i;
910
911     if (tagN == RPMTAG_BASENAMES) {
912         Type = "Files";
913     } else {
914         Type = "?Type?";
915         goto exit;
916     }
917
918     /*@-branchstate@*/
919     if (fi == NULL) {
920         fi = xcalloc(1, sizeof(*fi));
921         malloced = 0;   /* XXX always return with memory alloced. */
922     }
923     /*@=branchstate@*/
924
925     fi->magic = TFIMAGIC;
926     fi->Type = Type;
927     fi->i = -1;
928     fi->tagN = tagN;
929
930     fi->hge = hge;
931     fi->hae = (HAE_t) headerAddEntry;
932     fi->hme = (HME_t) headerModifyEntry;
933     fi->hre = (HRE_t) headerRemoveEntry;
934     fi->hfd = headerFreeData;
935
936     fi->h = (scareMem ? headerLink(h, fi->Type) : NULL);
937
938     if (fi->fsm == NULL)
939         fi->fsm = newFSM();
940
941     /* 0 means unknown */
942     xx = hge(h, RPMTAG_ARCHIVESIZE, NULL, (void **) &uip, NULL);
943     fi->archiveSize = (xx ? *uip : 0);
944
945     if (!hge(h, RPMTAG_BASENAMES, NULL, (void **) &fi->bnl, &fi->fc)) {
946         /*@-branchstate@*/
947         if (malloced) {
948             if (scareMem && fi->h)
949                 fi->h = headerFree(fi->h, fi->Type);
950             fi->fsm = freeFSM(fi->fsm);
951             /*@-refcounttrans@*/
952             fi = _free(fi);
953             /*@=refcounttrans@*/
954         } else {
955             fi->fc = 0;
956             fi->dc = 0;
957         }
958         /*@=branchstate@*/
959         goto exit;
960     }
961     xx = hge(h, RPMTAG_DIRNAMES, NULL, (void **) &fi->dnl, &fi->dc);
962     xx = hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &fi->dil, NULL);
963     xx = hge(h, RPMTAG_FILEMODES, NULL, (void **) &fi->fmodes, NULL);
964     xx = hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fi->fflags, NULL);
965     xx = hge(h, RPMTAG_FILEVERIFYFLAGS, NULL, (void **) &fi->vflags, NULL);
966     xx = hge(h, RPMTAG_FILESIZES, NULL, (void **) &fi->fsizes, NULL);
967     xx = hge(h, RPMTAG_FILESTATES, NULL, (void **) &fi->fstates, NULL);
968     if (xx == 0 || fi->fstates == NULL)
969         fi->fstates = xcalloc(fi->fc, sizeof(*fi->fstates));
970     else
971         _fdupe(fi, fstates);
972
973     fi->action = FA_UNKNOWN;
974     fi->flags = 0;
975     if (fi->actions == NULL)
976         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
977     fi->keep_header = (scareMem ? 1 : 0);
978
979     /* XXX TR_REMOVED needs CPIO_MAP_{ABSOLUTE,ADDDOT} CPIO_ALL_HARDLINKS */
980     fi->mapflags =
981                 CPIO_MAP_PATH | CPIO_MAP_MODE | CPIO_MAP_UID | CPIO_MAP_GID;
982
983     xx = hge(h, RPMTAG_FILELINKTOS, NULL, (void **) &fi->flinks, NULL);
984     xx = hge(h, RPMTAG_FILELANGS, NULL, (void **) &fi->flangs, NULL);
985
986     xx = hge(h, RPMTAG_FILEMD5S, NULL, (void **) &fi->fmd5s, NULL);
987     t = xmalloc(fi->fc * 16);
988     fi->md5s = t;
989     for (i = 0; i < fi->fc; i++) {
990         const char * fmd5;
991         int j;
992
993         fmd5 = fi->fmd5s[i];
994         if (!(fmd5 && *fmd5 != '\0')) {
995             memset(t, 0, 16);
996             t += 16;
997             continue;
998         }
999         for (j = 0; j < 16; j++, t++, fmd5 += 2)
1000             *t = (nibble(fmd5[0]) << 4) | nibble(fmd5[1]);
1001     }
1002     fi->fmd5s = hfd(fi->fmd5s, -1);
1003
1004     /* XXX TR_REMOVED doesn;t need fmtimes or frdevs */
1005     xx = hge(h, RPMTAG_FILEMTIMES, NULL, (void **) &fi->fmtimes, NULL);
1006     xx = hge(h, RPMTAG_FILERDEVS, NULL, (void **) &fi->frdevs, NULL);
1007     fi->replacedSizes = xcalloc(fi->fc, sizeof(*fi->replacedSizes));
1008
1009     xx = hge(h, RPMTAG_FILEUSERNAME, NULL, (void **) &fi->fuser, NULL);
1010     fi->fuids = NULL;
1011     xx = hge(h, RPMTAG_FILEGROUPNAME, NULL, (void **) &fi->fgroup, NULL);
1012     fi->fgids = NULL;
1013
1014     if (ts != NULL)
1015     if (fi != NULL)
1016     if (fi->te != NULL && fi->te->type == TR_ADDED) {
1017         Header foo;
1018         fi->actions = xcalloc(fi->fc, sizeof(*fi->actions));
1019         /*@-compdef@*/ /* FIX: fi-md5s undefined */
1020         foo = relocateFileList(ts, fi, h, fi->actions);
1021         /*@=compdef@*/
1022         fi->h = headerFree(fi->h, "fiNew fi->h");
1023         fi->h = headerLink(foo, "fiNew fi->h = foo");
1024         foo = headerFree(foo, "fiNew foo");
1025     }
1026
1027     if (!scareMem) {
1028         _fdupe(fi, fmtimes);
1029         _fdupe(fi, frdevs);
1030         _fdupe(fi, fsizes);
1031         _fdupe(fi, fflags);
1032         _fdupe(fi, vflags);
1033         _fdupe(fi, fmodes);
1034         _fdupe(fi, dil);
1035         fi->h = headerFree(fi->h, fi->Type);
1036     }
1037
1038     dnlmax = -1;
1039     for (i = 0; i < fi->dc; i++) {
1040         if ((len = strlen(fi->dnl[i])) > dnlmax)
1041             dnlmax = len;
1042     }
1043     bnlmax = -1;
1044     for (i = 0; i < fi->fc; i++) {
1045         if ((len = strlen(fi->bnl[i])) > bnlmax)
1046             bnlmax = len;
1047     }
1048     fi->fnlen = dnlmax + bnlmax + 1;
1049     fi->fn = NULL;
1050
1051     fi->dperms = 0755;
1052     fi->fperms = 0644;
1053
1054 exit:
1055 /*@-modfilesystem@*/
1056 if (_fi_debug < 0)
1057 fprintf(stderr, "*** fi %p\t%s[%d]\n", fi, Type, (fi ? fi->fc : 0));
1058 /*@=modfilesystem@*/
1059
1060     /*@-compdef -nullstate@*/ /* FIX: TFI null annotations */
1061     return rpmfiLink(fi, (fi ? fi->Type : NULL));
1062     /*@=compdef =nullstate@*/
1063 }