1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
/** \ingroup rpmdep
* \file lib/rpmte.c
* Routine(s) to handle an "rpmte" transaction element.
*/
#include "system.h"
#include <rpmlib.h>
#include <rpmds.h>
#include <rpmfi.h>
#define _RPMTE_INTERNAL
#include <rpmte.h>
#include <rpmts.h>
#include "debug.h"
int _rpmte_debug = 0;
void rpmteCleanDS(rpmte te)
{
te->this = rpmdsFree(te->this);
te->provides = rpmdsFree(te->provides);
te->requires = rpmdsFree(te->requires);
te->conflicts = rpmdsFree(te->conflicts);
te->obsoletes = rpmdsFree(te->obsoletes);
}
/**
* Destroy transaction element data.
* @param p transaction element
*/
static void delTE(rpmte p)
{
rpmRelocation * r;
if (p->relocs) {
for (r = p->relocs; (r->oldPath || r->newPath); r++) {
r->oldPath = _free(r->oldPath);
r->newPath = _free(r->newPath);
}
p->relocs = _free(p->relocs);
}
rpmteCleanDS(p);
p->fi = rpmfiFree(p->fi);
if (p->fd != NULL)
p->fd = fdFree(p->fd, "delTE");
p->os = _free(p->os);
p->arch = _free(p->arch);
p->epoch = _free(p->epoch);
p->name = _free(p->name);
p->NEVR = _free(p->NEVR);
p->NEVRA = _free(p->NEVRA);
p->h = headerFree(p->h);
memset(p, 0, sizeof(*p)); /* XXX trash and burn */
/* FIX: p->{NEVR,name} annotations */
return;
}
/**
* Initialize transaction element data from header.
* @param ts transaction set
* @param p transaction element
* @param h header
* @param key (TR_ADDED) package retrieval key (e.g. file name)
* @param relocs (TR_ADDED) package file relocations
*/
static void addTE(rpmts ts, rpmte p, Header h,
fnpyKey key,
rpmRelocation * relocs)
{
int scareMem = 0;
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
rpmte savep;
int32_t * ep;
const char * arch, * os;
char * t;
size_t nb;
int xx;
p->NEVR = hGetNEVR(h, NULL);
p->name = xstrdup(p->NEVR);
if ((p->release = strrchr(p->name, '-')) != NULL)
*p->release++ = '\0';
if ((p->version = strrchr(p->name, '-')) != NULL)
*p->version++ = '\0';
/* Set db_instance to 0 as it has not been installed
* necessarily yet.
*/
p->db_instance = 0;
arch = NULL;
xx = hge(h, RPMTAG_ARCH, NULL, (void **)&arch, NULL);
if (arch != NULL) {
p->arch = xstrdup(arch);
p->archScore = rpmMachineScore(RPM_MACHTABLE_INSTARCH, arch);
} else {
p->arch = NULL;
p->archScore = 0;
}
os = NULL;
xx = hge(h, RPMTAG_OS, NULL, (void **)&os, NULL);
if (os != NULL) {
p->os = xstrdup(os);
p->osScore = rpmMachineScore(RPM_MACHTABLE_INSTOS, os);
} else {
p->os = NULL;
p->osScore = 0;
}
p->isSource = headerIsEntry(h, RPMTAG_SOURCEPACKAGE);
nb = strlen(p->NEVR) + 1;
if (p->isSource)
nb += sizeof("src");
else if (p->arch)
nb += strlen(p->arch) + 1;
t = xmalloc(nb);
p->NEVRA = t;
*t = '\0';
t = stpcpy(t, p->NEVR);
if (p->isSource)
t = stpcpy( t, ".src");
else if (p->arch)
t = stpcpy( stpcpy( t, "."), p->arch);
ep = NULL;
xx = hge(h, RPMTAG_EPOCH, NULL, (void **)&ep, NULL);
if (ep) {
p->epoch = xmalloc(20);
sprintf(p->epoch, "%d", *ep);
} else
p->epoch = NULL;
p->nrelocs = 0;
p->relocs = NULL;
if (relocs != NULL) {
rpmRelocation * r;
int i;
for (r = relocs; r->oldPath || r->newPath; r++)
p->nrelocs++;
p->relocs = xmalloc((p->nrelocs + 1) * sizeof(*p->relocs));
for (i = 0, r = relocs; r->oldPath || r->newPath; i++, r++) {
p->relocs[i].oldPath = r->oldPath ? xstrdup(r->oldPath) : NULL;
p->relocs[i].newPath = r->newPath ? xstrdup(r->newPath) : NULL;
}
p->relocs[i].oldPath = NULL;
p->relocs[i].newPath = NULL;
}
p->autorelocatex = -1;
p->key = key;
p->fd = NULL;
p->pkgFileSize = 0;
p->this = rpmdsThis(h, RPMTAG_PROVIDENAME, RPMSENSE_EQUAL);
p->provides = rpmdsNew(h, RPMTAG_PROVIDENAME, scareMem);
p->requires = rpmdsNew(h, RPMTAG_REQUIRENAME, scareMem);
p->conflicts = rpmdsNew(h, RPMTAG_CONFLICTNAME, scareMem);
p->obsoletes = rpmdsNew(h, RPMTAG_OBSOLETENAME, scareMem);
savep = rpmtsSetRelocateElement(ts, p);
p->fi = rpmfiNew(ts, h, RPMTAG_BASENAMES, scareMem);
(void) rpmtsSetRelocateElement(ts, savep);
rpmteColorDS(p, RPMTAG_PROVIDENAME);
rpmteColorDS(p, RPMTAG_REQUIRENAME);
return;
}
rpmte rpmteFree(rpmte te)
{
if (te != NULL) {
delTE(te);
memset(te, 0, sizeof(*te)); /* XXX trash and burn */
te = _free(te);
}
return NULL;
}
rpmte rpmteNew(const rpmts ts, Header h,
rpmElementType type,
fnpyKey key,
rpmRelocation * relocs,
int dboffset,
rpmalKey pkgKey)
{
rpmte p = xcalloc(1, sizeof(*p));
int32_t * ep;
int xx;
p->type = type;
addTE(ts, p, h, key, relocs);
switch (type) {
case TR_ADDED:
p->u.addedKey = pkgKey;
ep = NULL;
xx = headerGetEntry(h, RPMTAG_SIGSIZE, NULL, (void **)&ep, NULL);
/* XXX 256 is only an estimate of signature header. */
if (ep != NULL)
p->pkgFileSize += 96 + 256 + *ep;
break;
case TR_REMOVED:
p->u.removed.dependsOnKey = pkgKey;
p->u.removed.dboffset = dboffset;
break;
}
return p;
}
/* Get the DB Instance value */
unsigned int rpmteDBInstance(rpmte te)
{
return (te != NULL ? te->db_instance : 0);
}
/* Set the DB Instance value */
void rpmteSetDBInstance(rpmte te, unsigned int instance)
{
if (te != NULL)
te->db_instance = instance;
}
Header rpmteHeader(rpmte te)
{
return (te != NULL && te->h != NULL ? headerLink(te->h) : NULL);
}
Header rpmteSetHeader(rpmte te, Header h)
{
if (te != NULL) {
te->h = headerFree(te->h);
if (h != NULL)
te->h = headerLink(h);
}
return NULL;
}
rpmElementType rpmteType(rpmte te)
{
return (te != NULL ? te->type : -1);
}
const char * rpmteN(rpmte te)
{
return (te != NULL ? te->name : NULL);
}
const char * rpmteE(rpmte te)
{
return (te != NULL ? te->epoch : NULL);
}
const char * rpmteV(rpmte te)
{
return (te != NULL ? te->version : NULL);
}
const char * rpmteR(rpmte te)
{
return (te != NULL ? te->release : NULL);
}
const char * rpmteA(rpmte te)
{
return (te != NULL ? te->arch : NULL);
}
const char * rpmteO(rpmte te)
{
return (te != NULL ? te->os : NULL);
}
int rpmteIsSource(rpmte te)
{
return (te != NULL ? te->isSource : 0);
}
uint32_t rpmteColor(rpmte te)
{
return (te != NULL ? te->color : 0);
}
uint32_t rpmteSetColor(rpmte te, uint32_t color)
{
int ocolor = 0;
if (te != NULL) {
ocolor = te->color;
te->color = color;
}
return ocolor;
}
uint32_t rpmtePkgFileSize(rpmte te)
{
return (te != NULL ? te->pkgFileSize : 0);
}
int rpmteDepth(rpmte te)
{
return (te != NULL ? te->depth : 0);
}
int rpmteSetDepth(rpmte te, int ndepth)
{
int odepth = 0;
if (te != NULL) {
odepth = te->depth;
te->depth = ndepth;
}
return odepth;
}
int rpmteBreadth(rpmte te)
{
return (te != NULL ? te->depth : 0);
}
int rpmteSetBreadth(rpmte te, int nbreadth)
{
int obreadth = 0;
if (te != NULL) {
obreadth = te->breadth;
te->breadth = nbreadth;
}
return obreadth;
}
int rpmteNpreds(rpmte te)
{
return (te != NULL ? te->npreds : 0);
}
int rpmteSetNpreds(rpmte te, int npreds)
{
int opreds = 0;
if (te != NULL) {
opreds = te->npreds;
te->npreds = npreds;
}
return opreds;
}
int rpmteTree(rpmte te)
{
return (te != NULL ? te->tree : 0);
}
int rpmteSetTree(rpmte te, int ntree)
{
int otree = 0;
if (te != NULL) {
otree = te->tree;
te->tree = ntree;
}
return otree;
}
rpmte rpmteParent(rpmte te)
{
return (te != NULL ? te->parent : NULL);
}
rpmte rpmteSetParent(rpmte te, rpmte pte)
{
rpmte opte = NULL;
if (te != NULL) {
opte = te->parent;
te->parent = pte;
}
return opte;
}
int rpmteDegree(rpmte te)
{
return (te != NULL ? te->degree : 0);
}
int rpmteSetDegree(rpmte te, int ndegree)
{
int odegree = 0;
if (te != NULL) {
odegree = te->degree;
te->degree = ndegree;
}
return odegree;
}
tsortInfo rpmteTSI(rpmte te)
{
return te->tsi;
}
void rpmteFreeTSI(rpmte te)
{
if (te != NULL && rpmteTSI(te) != NULL) {
tsortInfo tsi;
/* Clean up tsort remnants (if any). */
while ((tsi = rpmteTSI(te)->tsi_next) != NULL) {
rpmteTSI(te)->tsi_next = tsi->tsi_next;
tsi->tsi_next = NULL;
tsi = _free(tsi);
}
te->tsi = _free(te->tsi);
}
/* FIX: te->tsi is NULL */
return;
}
void rpmteNewTSI(rpmte te)
{
if (te != NULL) {
rpmteFreeTSI(te);
te->tsi = xcalloc(1, sizeof(*te->tsi));
}
}
rpmalKey rpmteAddedKey(rpmte te)
{
return (te != NULL ? te->u.addedKey : RPMAL_NOMATCH);
}
rpmalKey rpmteSetAddedKey(rpmte te, rpmalKey npkgKey)
{
rpmalKey opkgKey = RPMAL_NOMATCH;
if (te != NULL) {
opkgKey = te->u.addedKey;
te->u.addedKey = npkgKey;
}
return opkgKey;
}
rpmalKey rpmteDependsOnKey(rpmte te)
{
return (te != NULL ? te->u.removed.dependsOnKey : RPMAL_NOMATCH);
}
int rpmteDBOffset(rpmte te)
{
return (te != NULL ? te->u.removed.dboffset : 0);
}
const char * rpmteNEVR(rpmte te)
{
return (te != NULL ? te->NEVR : NULL);
}
const char * rpmteNEVRA(rpmte te)
{
return (te != NULL ? te->NEVRA : NULL);
}
FD_t rpmteFd(rpmte te)
{
return (te != NULL ? te->fd : NULL);
}
fnpyKey rpmteKey(rpmte te)
{
return (te != NULL ? te->key : NULL);
}
rpmds rpmteDS(rpmte te, rpmTag tag)
{
if (te == NULL)
return NULL;
if (tag == RPMTAG_NAME)
return te->this;
else
if (tag == RPMTAG_PROVIDENAME)
return te->provides;
else
if (tag == RPMTAG_REQUIRENAME)
return te->requires;
else
if (tag == RPMTAG_CONFLICTNAME)
return te->conflicts;
else
if (tag == RPMTAG_OBSOLETENAME)
return te->obsoletes;
else
return NULL;
}
rpmfi rpmteFI(rpmte te, rpmTag tag)
{
if (te == NULL)
return NULL;
if (tag == RPMTAG_BASENAMES)
return te->fi;
else
return NULL;
}
void rpmteColorDS(rpmte te, rpmTag tag)
{
rpmfi fi = rpmteFI(te, RPMTAG_BASENAMES);
rpmds ds = rpmteDS(te, tag);
char deptype = 'R';
char mydt;
const uint32_t * ddict;
int32_t * colors;
int32_t * refs;
int32_t val;
int Count;
size_t nb;
unsigned ix;
int ndx, i;
if (!(te && (Count = rpmdsCount(ds)) > 0 && rpmfiFC(fi) > 0))
return;
switch (tag) {
default:
return;
break;
case RPMTAG_PROVIDENAME:
deptype = 'P';
break;
case RPMTAG_REQUIRENAME:
deptype = 'R';
break;
}
nb = Count * sizeof(*colors);
colors = memset(alloca(nb), 0, nb);
nb = Count * sizeof(*refs);
refs = memset(alloca(nb), -1, nb);
/* Calculate dependency color and reference count. */
fi = rpmfiInit(fi, 0);
if (fi != NULL)
while (rpmfiNext(fi) >= 0) {
val = rpmfiFColor(fi);
ddict = NULL;
ndx = rpmfiFDepends(fi, &ddict);
if (ddict != NULL)
while (ndx-- > 0) {
ix = *ddict++;
mydt = ((ix >> 24) & 0xff);
if (mydt != deptype)
continue;
ix &= 0x00ffffff;
assert (ix < Count);
colors[ix] |= val;
refs[ix]++;
}
}
/* Set color/refs values in dependency set. */
ds = rpmdsInit(ds);
while ((i = rpmdsNext(ds)) >= 0) {
val = colors[i];
te->color |= val;
(void) rpmdsSetColor(ds, val);
val = refs[i];
if (val >= 0)
val++;
(void) rpmdsSetRefs(ds, val);
}
}
int rpmtsiOc(rpmtsi tsi)
{
return tsi->ocsave;
}
rpmtsi XrpmtsiFree(rpmtsi tsi,
const char * fn, unsigned int ln)
{
/* XXX watchout: a funky recursion segfaults here iff nrefs is wrong. */
if (tsi)
tsi->ts = rpmtsFree(tsi->ts);
if (_rpmte_debug)
fprintf(stderr, "*** tsi %p -- %s:%d\n", tsi, fn, ln);
return _free(tsi);
}
rpmtsi XrpmtsiInit(rpmts ts, const char * fn, unsigned int ln)
{
rpmtsi tsi = NULL;
tsi = xcalloc(1, sizeof(*tsi));
tsi->ts = rpmtsLink(ts, "rpmtsi");
tsi->reverse = ((rpmtsFlags(ts) & RPMTRANS_FLAG_REVERSE) ? 1 : 0);
tsi->oc = (tsi->reverse ? (rpmtsNElements(ts) - 1) : 0);
tsi->ocsave = tsi->oc;
if (_rpmte_debug)
fprintf(stderr, "*** tsi %p ++ %s:%d\n", tsi, fn, ln);
return tsi;
}
/**
* Return next transaction element.
* @param tsi transaction element iterator
* @return transaction element, NULL on termination
*/
static
rpmte rpmtsiNextElement(rpmtsi tsi)
{
rpmte te = NULL;
int oc = -1;
if (tsi == NULL || tsi->ts == NULL || rpmtsNElements(tsi->ts) <= 0)
return te;
if (tsi->reverse) {
if (tsi->oc >= 0) oc = tsi->oc--;
} else {
if (tsi->oc < rpmtsNElements(tsi->ts)) oc = tsi->oc++;
}
tsi->ocsave = oc;
if (oc != -1)
te = rpmtsElement(tsi->ts, oc);
return te;
}
rpmte rpmtsiNext(rpmtsi tsi, rpmElementType type)
{
rpmte te;
while ((te = rpmtsiNextElement(tsi)) != NULL) {
if (type == 0 || (te->type & type) != 0)
break;
}
return te;
}
|