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