Remove unused proof-of-concept arch-filtering of dependencies
[platform/upstream/rpm.git] / lib / rpmds.c
1 /** \ingroup rpmdep
2  * \file lib/rpmds.c
3  */
4 #include "system.h"
5
6 #include <rpmlib.h>
7
8 #define _RPMDS_INTERNAL
9 #include "rpmds.h"
10
11 #include "debug.h"
12
13 /**
14  * Enable noisy range comparison debugging message?
15  */
16 static int _noisy_range_comparison_debug_message = 0;
17
18 int _rpmds_debug = 0;
19
20 int _rpmds_nopromote = 1;
21
22 int _rpmds_unspecified_epoch_noise = 0;
23
24 rpmds XrpmdsUnlink(rpmds ds, const char * msg, const char * fn, unsigned ln)
25 {
26     if (ds == NULL) return NULL;
27 if (_rpmds_debug && msg != NULL)
28 fprintf(stderr, "--> ds %p -- %d %s at %s:%u\n", ds, ds->nrefs, msg, fn, ln);
29     ds->nrefs--;
30     return NULL;
31 }
32
33 rpmds XrpmdsLink(rpmds ds, const char * msg, const char * fn, unsigned ln)
34 {
35     if (ds == NULL) return NULL;
36     ds->nrefs++;
37
38 if (_rpmds_debug && msg != NULL)
39 fprintf(stderr, "--> ds %p ++ %d %s at %s:%u\n", ds, ds->nrefs, msg, fn, ln);
40
41     return ds;
42 }
43
44 rpmds rpmdsFree(rpmds ds)
45 {
46     HFD_t hfd = headerFreeData;
47     rpmTag tagEVR, tagF;
48
49     if (ds == NULL)
50         return NULL;
51
52     if (ds->nrefs > 1)
53         return rpmdsUnlink(ds, ds->Type);
54
55 if (_rpmds_debug < 0)
56 fprintf(stderr, "*** ds %p\t%s[%d]\n", ds, ds->Type, ds->Count);
57
58     if (ds->tagN == RPMTAG_PROVIDENAME) {
59         tagEVR = RPMTAG_PROVIDEVERSION;
60         tagF = RPMTAG_PROVIDEFLAGS;
61     } else
62     if (ds->tagN == RPMTAG_REQUIRENAME) {
63         tagEVR = RPMTAG_REQUIREVERSION;
64         tagF = RPMTAG_REQUIREFLAGS;
65     } else
66     if (ds->tagN == RPMTAG_CONFLICTNAME) {
67         tagEVR = RPMTAG_CONFLICTVERSION;
68         tagF = RPMTAG_CONFLICTFLAGS;
69     } else
70     if (ds->tagN == RPMTAG_OBSOLETENAME) {
71         tagEVR = RPMTAG_OBSOLETEVERSION;
72         tagF = RPMTAG_OBSOLETEFLAGS;
73     } else
74     if (ds->tagN == RPMTAG_TRIGGERNAME) {
75         tagEVR = RPMTAG_TRIGGERVERSION;
76         tagF = RPMTAG_TRIGGERFLAGS;
77     } else
78         return NULL;
79
80     if (ds->Count > 0) {
81         ds->N = hfd(ds->N, ds->Nt);
82         ds->EVR = hfd(ds->EVR, ds->EVRt);
83         ds->Flags = (ds->h != NULL ? hfd(ds->Flags, ds->Ft) : _free(ds->Flags));
84         ds->h = headerFree(ds->h);
85     }
86
87     ds->DNEVR = _free(ds->DNEVR);
88     ds->Color = _free(ds->Color);
89     ds->Refs = _free(ds->Refs);
90
91     (void) rpmdsUnlink(ds, ds->Type);
92     memset(ds, 0, sizeof(*ds));         /* XXX trash and burn */
93     ds = _free(ds);
94     return NULL;
95 }
96
97 rpmds rpmdsNew(Header h, rpmTag tagN, int flags)
98 {
99     int scareMem = (flags & 0x1);
100     HGE_t hge =
101         (scareMem ? (HGE_t) headerGetEntryMinMemory : (HGE_t) headerGetEntry);
102     rpmTag tagBT = RPMTAG_BUILDTIME;
103     rpmTagType BTt;
104     int_32 * BTp;
105     rpmTag tagEVR, tagF;
106     rpmds ds = NULL;
107     const char * Type;
108     const char ** N;
109     rpmTagType Nt;
110     int_32 Count;
111
112     if (tagN == RPMTAG_PROVIDENAME) {
113         Type = "Provides";
114         tagEVR = RPMTAG_PROVIDEVERSION;
115         tagF = RPMTAG_PROVIDEFLAGS;
116     } else
117     if (tagN == RPMTAG_REQUIRENAME) {
118         Type = "Requires";
119         tagEVR = RPMTAG_REQUIREVERSION;
120         tagF = RPMTAG_REQUIREFLAGS;
121     } else
122     if (tagN == RPMTAG_CONFLICTNAME) {
123         Type = "Conflicts";
124         tagEVR = RPMTAG_CONFLICTVERSION;
125         tagF = RPMTAG_CONFLICTFLAGS;
126     } else
127     if (tagN == RPMTAG_OBSOLETENAME) {
128         Type = "Obsoletes";
129         tagEVR = RPMTAG_OBSOLETEVERSION;
130         tagF = RPMTAG_OBSOLETEFLAGS;
131     } else
132     if (tagN == RPMTAG_TRIGGERNAME) {
133         Type = "Trigger";
134         tagEVR = RPMTAG_TRIGGERVERSION;
135         tagF = RPMTAG_TRIGGERFLAGS;
136     } else
137         goto exit;
138
139     if (hge(h, tagN, &Nt, (void **) &N, &Count)
140      && N != NULL && Count > 0)
141     {
142         int xx;
143
144         ds = xcalloc(1, sizeof(*ds));
145         ds->Type = Type;
146         ds->h = (scareMem ? headerLink(h) : NULL);
147         ds->i = -1;
148         ds->DNEVR = NULL;
149         ds->tagN = tagN;
150         ds->N = N;
151         ds->Nt = Nt;
152         ds->Count = Count;
153         ds->nopromote = _rpmds_nopromote;
154
155         xx = hge(h, tagEVR, &ds->EVRt, (void **) &ds->EVR, NULL);
156         xx = hge(h, tagF, &ds->Ft, (void **) &ds->Flags, NULL);
157         if (!scareMem && ds->Flags != NULL)
158             ds->Flags = memcpy(xmalloc(ds->Count * sizeof(*ds->Flags)),
159                                 ds->Flags, ds->Count * sizeof(*ds->Flags));
160         xx = hge(h, tagBT, &BTt, (void **) &BTp, NULL);
161         ds->BT = (xx && BTp != NULL && BTt == RPM_INT32_TYPE ? *BTp : 0);
162         ds->Color = xcalloc(Count, sizeof(*ds->Color));
163         ds->Refs = xcalloc(Count, sizeof(*ds->Refs));
164
165 if (_rpmds_debug < 0)
166 fprintf(stderr, "*** ds %p\t%s[%d]\n", ds, ds->Type, ds->Count);
167
168     }
169
170 exit:
171     /* FIX: ds->Flags may be NULL */
172     ds = rpmdsLink(ds, (ds ? ds->Type : NULL));
173
174     return ds;
175 }
176
177 char * rpmdsNewDNEVR(const char * dspfx, const rpmds ds)
178 {
179     char * tbuf, * t;
180     size_t nb;
181
182     nb = 0;
183     if (dspfx)  nb += strlen(dspfx) + 1;
184     if (ds->N[ds->i])   nb += strlen(ds->N[ds->i]);
185     /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
186     if (ds->Flags != NULL && (ds->Flags[ds->i] & RPMSENSE_SENSEMASK)) {
187         if (nb) nb++;
188         if (ds->Flags[ds->i] & RPMSENSE_LESS)   nb++;
189         if (ds->Flags[ds->i] & RPMSENSE_GREATER) nb++;
190         if (ds->Flags[ds->i] & RPMSENSE_EQUAL)  nb++;
191     }
192     /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
193     if (ds->EVR != NULL && ds->EVR[ds->i] && *ds->EVR[ds->i]) {
194         if (nb) nb++;
195         nb += strlen(ds->EVR[ds->i]);
196     }
197
198     t = tbuf = xmalloc(nb + 1);
199     if (dspfx) {
200         t = stpcpy(t, dspfx);
201         *t++ = ' ';
202     }
203     if (ds->N[ds->i])
204         t = stpcpy(t, ds->N[ds->i]);
205     /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
206     if (ds->Flags != NULL && (ds->Flags[ds->i] & RPMSENSE_SENSEMASK)) {
207         if (t != tbuf)  *t++ = ' ';
208         if (ds->Flags[ds->i] & RPMSENSE_LESS)   *t++ = '<';
209         if (ds->Flags[ds->i] & RPMSENSE_GREATER) *t++ = '>';
210         if (ds->Flags[ds->i] & RPMSENSE_EQUAL)  *t++ = '=';
211     }
212     /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
213     if (ds->EVR != NULL && ds->EVR[ds->i] && *ds->EVR[ds->i]) {
214         if (t != tbuf)  *t++ = ' ';
215         t = stpcpy(t, ds->EVR[ds->i]);
216     }
217     *t = '\0';
218     return tbuf;
219 }
220
221 rpmds rpmdsThis(Header h, rpmTag tagN, int_32 Flags)
222 {
223     HGE_t hge = (HGE_t) headerGetEntryMinMemory;
224     rpmds ds = NULL;
225     const char * Type;
226     const char * n, * v, * r;
227     int_32 * ep;
228     const char ** N, ** EVR;
229     char * t;
230     int xx;
231
232     if (tagN == RPMTAG_PROVIDENAME) {
233         Type = "Provides";
234     } else
235     if (tagN == RPMTAG_REQUIRENAME) {
236         Type = "Requires";
237     } else
238     if (tagN == RPMTAG_CONFLICTNAME) {
239         Type = "Conflicts";
240     } else
241     if (tagN == RPMTAG_OBSOLETENAME) {
242         Type = "Obsoletes";
243     } else
244     if (tagN == RPMTAG_TRIGGERNAME) {
245         Type = "Trigger";
246     } else
247         goto exit;
248
249     xx = headerNVR(h, &n, &v, &r);
250     ep = NULL;
251     xx = hge(h, RPMTAG_EPOCH, NULL, (void **)&ep, NULL);
252
253     t = xmalloc(sizeof(*N) + strlen(n) + 1);
254     N = (const char **) t;
255     t += sizeof(*N);
256     *t = '\0';
257     N[0] = t;
258     t = stpcpy(t, n);
259
260     t = xmalloc(sizeof(*EVR) +
261                 (ep ? 20 : 0) + strlen(v) + strlen(r) + sizeof("-"));
262     EVR = (const char **) t;
263     t += sizeof(*EVR);
264     *t = '\0';
265     EVR[0] = t;
266     if (ep) {
267         sprintf(t, "%d:", *ep);
268         t += strlen(t);
269     }
270     t = stpcpy( stpcpy( stpcpy( t, v), "-"), r);
271
272     ds = xcalloc(1, sizeof(*ds));
273     ds->h = NULL;
274     ds->Type = Type;
275     ds->tagN = tagN;
276     ds->Count = 1;
277     ds->N = N;
278     ds->Nt = -1;        /* XXX to insure that hfd will free */
279     ds->EVR = EVR;
280     ds->EVRt = -1;      /* XXX to insure that hfd will free */
281     ds->Flags = xmalloc(sizeof(*ds->Flags));    ds->Flags[0] = Flags;
282     ds->i = 0;
283     {   char pre[2];
284         pre[0] = ds->Type[0];
285         pre[1] = '\0';
286         /* LCL: ds->Type may be NULL ??? */
287         ds->DNEVR = rpmdsNewDNEVR(pre, ds);
288     }
289
290 exit:
291     return rpmdsLink(ds, (ds ? ds->Type : NULL));
292 }
293
294 rpmds rpmdsSingle(rpmTag tagN, const char * N, const char * EVR, int_32 Flags)
295 {
296     rpmds ds = NULL;
297     const char * Type;
298
299     if (tagN == RPMTAG_PROVIDENAME) {
300         Type = "Provides";
301     } else
302     if (tagN == RPMTAG_REQUIRENAME) {
303         Type = "Requires";
304     } else
305     if (tagN == RPMTAG_CONFLICTNAME) {
306         Type = "Conflicts";
307     } else
308     if (tagN == RPMTAG_OBSOLETENAME) {
309         Type = "Obsoletes";
310     } else
311     if (tagN == RPMTAG_TRIGGERNAME) {
312         Type = "Trigger";
313     } else
314         goto exit;
315
316     ds = xcalloc(1, sizeof(*ds));
317     ds->h = NULL;
318     ds->Type = Type;
319     ds->tagN = tagN;
320     {   time_t now = time(NULL);
321         ds->BT = now;
322     }
323     ds->Count = 1;
324     ds->N = xmalloc(sizeof(*ds->N));            ds->N[0] = N;
325     ds->Nt = -1;        /* XXX to insure that hfd will free */
326     ds->EVR = xmalloc(sizeof(*ds->EVR));        ds->EVR[0] = EVR;
327     ds->EVRt = -1;      /* XXX to insure that hfd will free */
328     ds->Flags = xmalloc(sizeof(*ds->Flags));    ds->Flags[0] = Flags;
329     ds->i = 0;
330     {   char t[2];
331         t[0] = ds->Type[0];
332         t[1] = '\0';
333         ds->DNEVR = rpmdsNewDNEVR(t, ds);
334     }
335
336 exit:
337     return rpmdsLink(ds, (ds ? ds->Type : NULL));
338 }
339
340 int rpmdsCount(const rpmds ds)
341 {
342     return (ds != NULL ? ds->Count : 0);
343 }
344
345 int rpmdsIx(const rpmds ds)
346 {
347     return (ds != NULL ? ds->i : -1);
348 }
349
350 int rpmdsSetIx(rpmds ds, int ix)
351 {
352     int i = -1;
353
354     if (ds != NULL) {
355         i = ds->i;
356         ds->i = ix;
357     }
358     return i;
359 }
360
361 const char * rpmdsDNEVR(const rpmds ds)
362 {
363     const char * DNEVR = NULL;
364
365     if (ds != NULL && ds->i >= 0 && ds->i < ds->Count) {
366         if (ds->DNEVR != NULL)
367             DNEVR = ds->DNEVR;
368     }
369     return DNEVR;
370 }
371
372 const char * rpmdsN(const rpmds ds)
373 {
374     const char * N = NULL;
375
376     if (ds != NULL && ds->i >= 0 && ds->i < ds->Count) {
377         if (ds->N != NULL)
378             N = ds->N[ds->i];
379     }
380     return N;
381 }
382
383 const char * rpmdsEVR(const rpmds ds)
384 {
385     const char * EVR = NULL;
386
387     if (ds != NULL && ds->i >= 0 && ds->i < ds->Count) {
388         if (ds->EVR != NULL)
389             EVR = ds->EVR[ds->i];
390     }
391     return EVR;
392 }
393
394 int_32 rpmdsFlags(const rpmds ds)
395 {
396     int_32 Flags = 0;
397
398     if (ds != NULL && ds->i >= 0 && ds->i < ds->Count) {
399         if (ds->Flags != NULL)
400             Flags = ds->Flags[ds->i];
401     }
402     return Flags;
403 }
404
405 rpmTag rpmdsTagN(const rpmds ds)
406 {
407     rpmTag tagN = 0;
408
409     if (ds != NULL)
410         tagN = ds->tagN;
411     return tagN;
412 }
413
414 time_t rpmdsBT(const rpmds ds)
415 {
416     time_t BT = 0;
417     if (ds != NULL && ds->BT > 0)
418         BT = ds->BT;
419     return BT;
420 }
421
422 time_t rpmdsSetBT(const rpmds ds, time_t BT)
423 {
424     time_t oBT = 0;
425     if (ds != NULL) {
426         oBT = ds->BT;
427         ds->BT = BT;
428     }
429     return oBT;
430 }
431
432 int rpmdsNoPromote(const rpmds ds)
433 {
434     int nopromote = 0;
435
436     if (ds != NULL)
437         nopromote = ds->nopromote;
438     return nopromote;
439 }
440
441 int rpmdsSetNoPromote(rpmds ds, int nopromote)
442 {
443     int onopromote = 0;
444
445     if (ds != NULL) {
446         onopromote = ds->nopromote;
447         ds->nopromote = nopromote;
448     }
449     return onopromote;
450 }
451
452 uint_32 rpmdsColor(const rpmds ds)
453 {
454     uint_32 Color = 0;
455
456     if (ds != NULL && ds->i >= 0 && ds->i < ds->Count) {
457         if (ds->Color != NULL)
458             Color = ds->Color[ds->i];
459     }
460     return Color;
461 }
462
463 uint_32 rpmdsSetColor(const rpmds ds, uint_32 color)
464 {
465     uint_32 ocolor = 0;
466
467     if (ds != NULL && ds->i >= 0 && ds->i < ds->Count) {
468         if (ds->Color != NULL) {
469             ocolor = ds->Color[ds->i];
470             ds->Color[ds->i] = color;
471         }
472     }
473     return ocolor;
474 }
475
476 int_32 rpmdsRefs(const rpmds ds)
477 {
478     int_32 Refs = 0;
479
480     if (ds != NULL && ds->i >= 0 && ds->i < ds->Count) {
481         if (ds->Refs != NULL)
482             Refs = ds->Refs[ds->i];
483     }
484     return Refs;
485 }
486
487 int_32 rpmdsSetRefs(const rpmds ds, int_32 refs)
488 {
489     int_32 orefs = 0;
490
491     if (ds != NULL && ds->i >= 0 && ds->i < ds->Count) {
492         if (ds->Refs != NULL) {
493             orefs = ds->Refs[ds->i];
494             ds->Refs[ds->i] = refs;
495         }
496     }
497     return orefs;
498 }
499
500 void rpmdsNotify(rpmds ds, const char * where, int rc)
501 {
502     if (!(ds != NULL && ds->i >= 0 && ds->i < ds->Count))
503         return;
504     if (!(ds->Type != NULL && ds->DNEVR != NULL))
505         return;
506
507     rpmMessage(RPMMESS_DEBUG, "%9s: %-45s %-s %s\n", ds->Type,
508                 (!strcmp(ds->DNEVR, "cached") ? ds->DNEVR : ds->DNEVR+2),
509                 (rc ? _("NO ") : _("YES")),
510                 (where != NULL ? where : ""));
511 }
512
513 int rpmdsNext(rpmds ds)
514 {
515     int i = -1;
516
517     if (ds != NULL && ++ds->i >= 0) {
518         if (ds->i < ds->Count) {
519             char t[2];
520             i = ds->i;
521             ds->DNEVR = _free(ds->DNEVR);
522             t[0] = ((ds->Type != NULL) ? ds->Type[0] : '\0');
523             t[1] = '\0';
524             ds->DNEVR = rpmdsNewDNEVR(t, ds);
525
526         } else
527             ds->i = -1;
528
529 if (_rpmds_debug  < 0 && i != -1)
530 fprintf(stderr, "*** ds %p\t%s[%d]: %s\n", ds, (ds->Type ? ds->Type : "?Type?"), i, (ds->DNEVR ? ds->DNEVR : "?DNEVR?"));
531
532     }
533
534     return i;
535 }
536
537 rpmds rpmdsInit(rpmds ds)
538 {
539     if (ds != NULL)
540         ds->i = -1;
541     return ds;
542 }
543
544 static
545 const char ** rpmdsDupArgv(const char ** argv, int argc)
546 {
547     const char ** av;
548     size_t nb = 0;
549     int ac = 0;
550     char * t;
551
552     if (argv == NULL)
553         return NULL;
554     for (ac = 0; ac < argc; ac++) {
555 assert(argv[ac] != NULL);
556         nb += strlen(argv[ac]) + 1;
557     }
558     nb += (ac + 1) * sizeof(*av);
559
560     av = xmalloc(nb);
561     t = (char *) (av + ac + 1);
562     for (ac = 0; ac < argc; ac++) {
563         av[ac] = t;
564         t = stpcpy(t, argv[ac]) + 1;
565     }
566     av[ac] = NULL;
567     return av;
568 }
569
570 static rpmds rpmdsDup(const rpmds ods)
571 {
572     rpmds ds = xcalloc(1, sizeof(*ds));
573     size_t nb;
574
575     ds->h = (ods->h != NULL ? headerLink(ods->h) : NULL);
576     ds->Type = ods->Type;
577     ds->tagN = ods->tagN;
578     ds->Count = ods->Count;
579     ds->i = ods->i;
580     ds->l = ods->l;
581     ds->u = ods->u;
582
583     nb = (ds->Count+1) * sizeof(*ds->N);
584     ds->N = (ds->h != NULL
585         ? memcpy(xmalloc(nb), ods->N, nb)
586         : rpmdsDupArgv(ods->N, ods->Count) );
587     ds->Nt = ods->Nt;
588
589     /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
590 assert(ods->EVR != NULL);
591 assert(ods->Flags != NULL);
592
593     nb = (ds->Count+1) * sizeof(*ds->EVR);
594     ds->EVR = (ds->h != NULL
595         ? memcpy(xmalloc(nb), ods->EVR, nb)
596         : rpmdsDupArgv(ods->EVR, ods->Count) );
597     ds->EVRt = ods->EVRt;
598
599     nb = (ds->Count * sizeof(*ds->Flags));
600     ds->Flags = (ds->h != NULL
601         ? ods->Flags
602         : memcpy(xmalloc(nb), ods->Flags, nb) );
603     ds->Ft = ods->Ft;
604
605 /* FIX: ds->Flags is kept, not only */
606     return rpmdsLink(ds, (ds ? ds->Type : NULL));
607
608 }
609
610 int rpmdsFind(rpmds ds, const rpmds ods)
611 {
612     int comparison;
613
614     if (ds == NULL || ods == NULL)
615         return -1;
616
617     ds->l = 0;
618     ds->u = ds->Count;
619     while (ds->l < ds->u) {
620         ds->i = (ds->l + ds->u) / 2;
621
622         comparison = strcmp(ods->N[ods->i], ds->N[ds->i]);
623
624         /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
625         if (comparison == 0 && ods->EVR && ds->EVR)
626             comparison = strcmp(ods->EVR[ods->i], ds->EVR[ds->i]);
627         if (comparison == 0 && ods->Flags && ds->Flags)
628             comparison = (ods->Flags[ods->i] - ds->Flags[ds->i]);
629
630         if (comparison < 0)
631             ds->u = ds->i;
632         else if (comparison > 0)
633             ds->l = ds->i + 1;
634         else
635             return ds->i;
636     }
637     return -1;
638 }
639
640 int rpmdsMerge(rpmds * dsp, rpmds ods)
641 {
642     rpmds ds;
643     const char ** N;
644     const char ** EVR;
645     int_32 * Flags;
646     int j;
647 int save;
648
649     if (dsp == NULL || ods == NULL)
650         return -1;
651
652     /* If not initialized yet, dup the 1st entry. */
653     if (*dsp == NULL) {
654         save = ods->Count;
655         ods->Count = 1;
656         *dsp = rpmdsDup(ods);
657         ods->Count = save;
658     }
659     ds = *dsp;
660     if (ds == NULL)
661         return -1;
662
663     /*
664      * Add new entries.
665      */
666 save = ods->i;
667     ods = rpmdsInit(ods);
668     if (ods != NULL)
669     while (rpmdsNext(ods) >= 0) {
670         /*
671          * If this entry is already present, don't bother.
672          */
673         if (rpmdsFind(ds, ods) >= 0)
674             continue;
675
676         /*
677          * Insert new entry.
678          */
679         for (j = ds->Count; j > ds->u; j--)
680             ds->N[j] = ds->N[j-1];
681         ds->N[ds->u] = ods->N[ods->i];
682         N = rpmdsDupArgv(ds->N, ds->Count+1);
683         ds->N = _free(ds->N);
684         ds->N = N;
685         
686         /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
687 assert(ods->EVR != NULL);
688 assert(ods->Flags != NULL);
689
690         for (j = ds->Count; j > ds->u; j--)
691             ds->EVR[j] = ds->EVR[j-1];
692         ds->EVR[ds->u] = ods->EVR[ods->i];
693         EVR = rpmdsDupArgv(ds->EVR, ds->Count+1);
694         ds->EVR = _free(ds->EVR);
695         ds->EVR = EVR;
696
697         Flags = xmalloc((ds->Count+1) * sizeof(*Flags));
698         if (ds->u > 0)
699             memcpy(Flags, ds->Flags, ds->u * sizeof(*Flags));
700         if (ds->u < ds->Count)
701             memcpy(Flags + ds->u + 1, ds->Flags + ds->u, (ds->Count - ds->u) * sizeof(*Flags));
702         Flags[ds->u] = ods->Flags[ods->i];
703         ds->Flags = _free(ds->Flags);
704         ds->Flags = Flags;
705
706         ds->i = ds->Count;
707         ds->Count++;
708
709     }
710 ods->i = save;
711     return 0;
712 }
713
714 /**
715  * Split EVR into epoch, version, and release components.
716  * @param evr           [epoch:]version[-release] string
717  * @retval *ep          pointer to epoch
718  * @retval *vp          pointer to version
719  * @retval *rp          pointer to release
720  */
721 static
722 void parseEVR(char * evr,
723                 const char ** ep,
724                 const char ** vp,
725                 const char ** rp)
726 {
727     const char *epoch;
728     const char *version;                /* assume only version is present */
729     const char *release;
730     char *s, *se;
731
732     s = evr;
733     while (*s && xisdigit(*s)) s++;     /* s points to epoch terminator */
734     se = strrchr(s, '-');               /* se points to version terminator */
735
736     if (*s == ':') {
737         epoch = evr;
738         *s++ = '\0';
739         version = s;
740         if (*epoch == '\0') epoch = "0";
741     } else {
742         epoch = NULL;   /* XXX disable epoch compare if missing */
743         version = evr;
744     }
745     if (se) {
746         *se++ = '\0';
747         release = se;
748     } else {
749         release = NULL;
750     }
751
752     if (ep) *ep = epoch;
753     if (vp) *vp = version;
754     if (rp) *rp = release;
755 }
756
757 int rpmdsCompare(const rpmds A, const rpmds B)
758 {
759     const char *aDepend = (A->DNEVR != NULL ? xstrdup(A->DNEVR+2) : "");
760     const char *bDepend = (B->DNEVR != NULL ? xstrdup(B->DNEVR+2) : "");
761     char *aEVR, *bEVR;
762     const char *aE, *aV, *aR, *bE, *bV, *bR;
763     int result;
764     int sense;
765
766     /* Different names don't overlap. */
767     if (strcmp(A->N[A->i], B->N[B->i])) {
768         result = 0;
769         goto exit;
770     }
771
772     /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
773     if (!(A->EVR && A->Flags && B->EVR && B->Flags)) {
774         result = 1;
775         goto exit;
776     }
777
778     /* Same name. If either A or B is an existence test, always overlap. */
779     if (!((A->Flags[A->i] & RPMSENSE_SENSEMASK) && (B->Flags[B->i] & RPMSENSE_SENSEMASK))) {
780         result = 1;
781         goto exit;
782     }
783
784     /* If either EVR is non-existent or empty, always overlap. */
785     if (!(A->EVR[A->i] && *A->EVR[A->i] && B->EVR[B->i] && *B->EVR[B->i])) {
786         result = 1;
787         goto exit;
788     }
789
790     /* Both AEVR and BEVR exist. */
791     aEVR = xstrdup(A->EVR[A->i]);
792     parseEVR(aEVR, &aE, &aV, &aR);
793     bEVR = xstrdup(B->EVR[B->i]);
794     parseEVR(bEVR, &bE, &bV, &bR);
795
796     /* Compare {A,B} [epoch:]version[-release] */
797     sense = 0;
798     if (aE && *aE && bE && *bE)
799         sense = rpmvercmp(aE, bE);
800     else if (aE && *aE && atol(aE) > 0) {
801         if (!B->nopromote) {
802             int lvl = (_rpmds_unspecified_epoch_noise  ? RPMMESS_WARNING : RPMMESS_DEBUG);
803             rpmMessage(lvl, _("The \"B\" dependency needs an epoch (assuming same epoch as \"A\")\n\tA = \"%s\"\tB = \"%s\"\n"),
804                 aDepend, bDepend);
805             sense = 0;
806         } else
807             sense = 1;
808     } else if (bE && *bE && atol(bE) > 0)
809         sense = -1;
810
811     if (sense == 0) {
812         sense = rpmvercmp(aV, bV);
813         if (sense == 0 && aR && *aR && bR && *bR)
814             sense = rpmvercmp(aR, bR);
815     }
816     aEVR = _free(aEVR);
817     bEVR = _free(bEVR);
818
819     /* Detect overlap of {A,B} range. */
820     result = 0;
821     if (sense < 0 && ((A->Flags[A->i] & RPMSENSE_GREATER) || (B->Flags[B->i] & RPMSENSE_LESS))) {
822         result = 1;
823     } else if (sense > 0 && ((A->Flags[A->i] & RPMSENSE_LESS) || (B->Flags[B->i] & RPMSENSE_GREATER))) {
824         result = 1;
825     } else if (sense == 0 &&
826         (((A->Flags[A->i] & RPMSENSE_EQUAL) && (B->Flags[B->i] & RPMSENSE_EQUAL)) ||
827          ((A->Flags[A->i] & RPMSENSE_LESS) && (B->Flags[B->i] & RPMSENSE_LESS)) ||
828          ((A->Flags[A->i] & RPMSENSE_GREATER) && (B->Flags[B->i] & RPMSENSE_GREATER)))) {
829         result = 1;
830     }
831
832 exit:
833     if (_noisy_range_comparison_debug_message)
834     rpmMessage(RPMMESS_DEBUG, _("  %s    A %s\tB %s\n"),
835         (result ? _("YES") : _("NO ")), aDepend, bDepend);
836     aDepend = _free(aDepend);
837     bDepend = _free(bDepend);
838     return result;
839 }
840
841 void rpmdsProblem(rpmps ps, const char * pkgNEVR, const rpmds ds,
842         const fnpyKey * suggestedKeys, int adding)
843 {
844     const char * Name =  rpmdsN(ds);
845     const char * DNEVR = rpmdsDNEVR(ds);
846     const char * EVR = rpmdsEVR(ds);
847     rpmProblemType type;
848     fnpyKey key;
849
850     if (ps == NULL) return;
851
852     if (Name == NULL) Name = "?N?";
853     if (EVR == NULL) EVR = "?EVR?";
854     if (DNEVR == NULL) DNEVR = "? ?N? ?OP? ?EVR?";
855
856     rpmMessage(RPMMESS_DEBUG, _("package %s has unsatisfied %s: %s\n"),
857             pkgNEVR, ds->Type, DNEVR+2);
858
859     switch ((unsigned)DNEVR[0]) {
860     case 'C':   type = RPMPROB_CONFLICT;        break;
861     default:
862     case 'R':   type = RPMPROB_REQUIRES;        break;
863     }
864
865     key = (suggestedKeys ? suggestedKeys[0] : NULL);
866     rpmpsAppend(ps, type, pkgNEVR, key, NULL, NULL, DNEVR, adding);
867 }
868
869 int rpmdsAnyMatchesDep (const Header h, const rpmds req, int nopromote)
870 {
871     int scareMem = 0;
872     rpmds provides = NULL;
873     int result = 0;
874
875     /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
876     if (req->EVR == NULL || req->Flags == NULL)
877         return 1;
878
879     if (!(req->Flags[req->i] & RPMSENSE_SENSEMASK) || !req->EVR[req->i] || *req->EVR[req->i] == '\0')
880         return 1;
881
882     /* Get provides information from header */
883     provides = rpmdsInit(rpmdsNew(h, RPMTAG_PROVIDENAME, scareMem));
884     if (provides == NULL)
885         goto exit;      /* XXX should never happen */
886     if (nopromote)
887         (void) rpmdsSetNoPromote(provides, nopromote);
888
889     /*
890      * Rpm prior to 3.0.3 did not have versioned provides.
891      * If no provides version info is available, match any/all requires
892      * with same name.
893      */
894     if (provides->EVR == NULL) {
895         result = 1;
896         goto exit;
897     }
898
899     result = 0;
900     if (provides != NULL)
901     while (rpmdsNext(provides) >= 0) {
902
903         /* Filter out provides that came along for the ride. */
904         if (strcmp(provides->N[provides->i], req->N[req->i]))
905             continue;
906
907         result = rpmdsCompare(provides, req);
908
909         /* If this provide matches the require, we're done. */
910         if (result)
911             break;
912     }
913
914 exit:
915     provides = rpmdsFree(provides);
916
917     return result;
918 }
919
920 int rpmdsNVRMatchesDep(const Header h, const rpmds req, int nopromote)
921 {
922     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
923     const char * pkgN, * v, * r;
924     int_32 * epoch;
925     const char * pkgEVR;
926     char * t;
927     int_32 pkgFlags = RPMSENSE_EQUAL;
928     rpmds pkg;
929     int rc = 1; /* XXX assume match, names already match here */
930
931     /* XXX rpm prior to 3.0.2 did not always supply EVR and Flags. */
932     if (req->EVR == NULL || req->Flags == NULL)
933         return rc;
934
935     if (!((req->Flags[req->i] & RPMSENSE_SENSEMASK) && req->EVR[req->i] && *req->EVR[req->i]))
936         return rc;
937
938     /* Get package information from header */
939     (void) headerNVR(h, &pkgN, &v, &r);
940
941     t = alloca(21 + strlen(v) + 1 + strlen(r) + 1);
942     pkgEVR = t;
943     *t = '\0';
944     if (hge(h, RPMTAG_EPOCH, NULL, (void **) &epoch, NULL)) {
945         sprintf(t, "%d:", *epoch);
946         while (*t != '\0')
947             t++;
948     }
949     (void) stpcpy( stpcpy( stpcpy(t, v) , "-") , r);
950
951     if ((pkg = rpmdsSingle(RPMTAG_PROVIDENAME, pkgN, pkgEVR, pkgFlags)) != NULL) {
952         if (nopromote)
953             (void) rpmdsSetNoPromote(pkg, nopromote);
954         rc = rpmdsCompare(pkg, req);
955         pkg = rpmdsFree(pkg);
956     }
957
958     return rc;
959 }