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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
|
/** \ingroup rpmdep
* \file lib/rpmts.c
* Routine(s) to handle a "rpmts" transaction sets.
*/
#include "system.h"
#include <inttypes.h>
#include <rpm/rpmtypes.h>
#include <rpm/rpmlib.h> /* rpmReadPackage etc */
#include <rpm/rpmmacro.h>
#include <rpm/rpmfileutil.h> /* rpmtsOpenDB() needs rpmGetPath */
#include <rpm/rpmstring.h>
#include <rpm/rpmkeyring.h>
#include <rpm/rpmdb.h>
#include <rpm/rpmds.h>
#include <rpm/rpmfi.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmte.h>
#include "rpmio/digest.h"
#include "lib/rpmal.h"
#include "lib/rpmlock.h"
#include "lib/rpmts_internal.h"
#include "lib/misc.h"
#include "debug.h"
/**
* Iterator across transaction elements, forward on install, backward on erase.
*/
struct rpmtsi_s {
rpmts ts; /*!< transaction set. */
int oc; /*!< iterator index. */
};
static void loadKeyring(rpmts ts);
int _rpmts_stats = 0;
rpmts rpmtsUnlink(rpmts ts)
{
if (ts)
ts->nrefs--;
return NULL;
}
rpmts rpmtsLink(rpmts ts)
{
if (ts)
ts->nrefs++;
return ts;
}
int rpmtsCloseDB(rpmts ts)
{
int rc = 0;
if (ts->rdb != NULL) {
(void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DBGET),
rpmdbOp(ts->rdb, RPMDB_OP_DBGET));
(void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DBPUT),
rpmdbOp(ts->rdb, RPMDB_OP_DBPUT));
(void) rpmswAdd(rpmtsOp(ts, RPMTS_OP_DBDEL),
rpmdbOp(ts->rdb, RPMDB_OP_DBDEL));
rc = rpmdbClose(ts->rdb);
ts->rdb = NULL;
}
return rc;
}
int rpmtsOpenDB(rpmts ts, int dbmode)
{
int rc = 0;
if (ts->rdb != NULL && ts->dbmode == dbmode)
return 0;
(void) rpmtsCloseDB(ts);
/* XXX there's a potential db lock race here. */
ts->dbmode = dbmode;
rc = rpmdbOpen(ts->rootDir, &ts->rdb, ts->dbmode, 0644);
if (rc) {
char * dn = rpmGetPath(ts->rootDir, "%{_dbpath}", NULL);
rpmlog(RPMLOG_ERR,
_("cannot open Packages database in %s\n"), dn);
dn = _free(dn);
}
return rc;
}
int rpmtsInitDB(rpmts ts, int dbmode)
{
void *lock = rpmtsAcquireLock(ts);
int rc = -1;
if (lock)
rc = rpmdbInit(ts->rootDir, dbmode);
rpmtsFreeLock(lock);
return rc;
}
int rpmtsGetDBMode(rpmts ts)
{
assert(ts != NULL);
return (ts->dbmode);
}
int rpmtsSetDBMode(rpmts ts, int dbmode)
{
int rc = 1;
/* mode setting only permitted on non-open db */
if (ts != NULL && rpmtsGetRdb(ts) == NULL) {
ts->dbmode = dbmode;
rc = 0;
}
return rc;
}
int rpmtsRebuildDB(rpmts ts)
{
int rc;
void *lock = rpmtsAcquireLock(ts);
if (!lock) return -1;
if (!(ts->vsflags & RPMVSF_NOHDRCHK))
rc = rpmdbRebuild(ts->rootDir, ts, headerCheck);
else
rc = rpmdbRebuild(ts->rootDir, NULL, NULL);
rpmtsFreeLock(lock);
return rc;
}
int rpmtsVerifyDB(rpmts ts)
{
return rpmdbVerify(ts->rootDir);
}
/* keyp might no be defined. */
rpmdbMatchIterator rpmtsInitIterator(const rpmts ts, rpmTag rpmtag,
const void * keyp, size_t keylen)
{
rpmdbMatchIterator mi = NULL;
const char * arch = NULL;
char *tmp = NULL;
int xx;
if (ts->keyring == NULL)
loadKeyring(ts);
if (ts->rdb == NULL && rpmtsOpenDB(ts, ts->dbmode))
return NULL;
/* Parse out "N(EVR).A" tokens from a label key. */
if (rpmtag == RPMDBI_LABEL && keyp != NULL) {
const char *se, *s = keyp;
char *t;
size_t slen = strlen(s);
int level = 0;
int c;
tmp = xmalloc(slen+1);
keyp = t = tmp;
while ((c = *s++) != '\0') {
switch (c) {
default:
*t++ = c;
break;
case '(':
/* XXX Fail if nested parens. */
if (level++ != 0) {
rpmlog(RPMLOG_ERR, _("extra '(' in package label: %s\n"), (const char*)keyp);
goto exit;
}
/* Parse explicit epoch. */
for (se = s; *se && risdigit(*se); se++)
{};
if (*se == ':') {
/* XXX skip explicit epoch's (for now) */
*t++ = '-';
s = se + 1;
} else {
/* No Epoch: found. Convert '(' to '-' and chug. */
*t++ = '-';
}
break;
case ')':
/* XXX Fail if nested parens. */
if (--level != 0) {
rpmlog(RPMLOG_ERR, _("missing '(' in package label: %s\n"), (const char*)keyp);
goto exit;
}
/* Don't copy trailing ')' */
break;
}
}
if (level) {
rpmlog(RPMLOG_ERR, _("missing ')' in package label: %s\n"), (const char*)keyp);
goto exit;
}
*t = '\0';
t = (char *) keyp;
t = strrchr(t, '.');
/* Is this a valid ".arch" suffix? */
if (t != NULL && rpmIsKnownArch(t+1)) {
*t++ = '\0';
arch = t;
}
}
mi = rpmdbInitIterator(ts->rdb, rpmtag, keyp, keylen);
/* Verify header signature/digest during retrieve (if not disabled). */
if (mi && !(ts->vsflags & RPMVSF_NOHDRCHK))
(void) rpmdbSetHdrChk(mi, ts, headerCheck);
/* Select specified arch only. */
if (arch != NULL)
xx = rpmdbSetIteratorRE(mi, RPMTAG_ARCH, RPMMIRE_DEFAULT, arch);
exit:
free(tmp);
return mi;
}
rpmKeyring rpmtsGetKeyring(rpmts ts, int autoload)
{
rpmKeyring keyring = NULL;
if (ts) {
if (ts->keyring == NULL && autoload) {
loadKeyring(ts);
}
keyring = rpmKeyringLink(ts->keyring);
}
return keyring;
}
int rpmtsSetKeyring(rpmts ts, rpmKeyring keyring)
{
/*
* Should we permit switching keyring on the fly? For now, require
* rpmdb isn't open yet (fairly arbitrary limitation)...
*/
if (ts == NULL || rpmtsGetRdb(ts) != NULL)
return -1;
rpmKeyringFree(ts->keyring);
ts->keyring = rpmKeyringLink(keyring);
return 0;
}
static int loadKeyringFromFiles(rpmts ts)
{
ARGV_t files = NULL;
/* XXX TODO: deal with chroot path issues */
char *pkpath = rpmGetPath(ts->rootDir, "%{_keyringpath}/*.key", NULL);
int nkeys = 0;
rpmlog(RPMLOG_DEBUG, "loading keyring from pubkeys in %s\n", pkpath);
if (rpmGlob(pkpath, NULL, &files)) {
rpmlog(RPMLOG_DEBUG, "couldn't find any keys in %s\n", pkpath);
goto exit;
}
for (char **f = files; *f; f++) {
rpmPubkey key = rpmPubkeyRead(*f);
if (!key) {
rpmlog(RPMLOG_ERR, _("%s: reading of public key failed.\n"), *f);
continue;
}
if (rpmKeyringAddKey(ts->keyring, key) == 0) {
nkeys++;
rpmlog(RPMLOG_DEBUG, "added key %s to keyring\n", *f);
}
rpmPubkeyFree(key);
}
exit:
free(pkpath);
argvFree(files);
return nkeys;
}
static int loadKeyringFromDB(rpmts ts)
{
Header h;
rpmdbMatchIterator mi;
int nkeys = 0;
rpmlog(RPMLOG_DEBUG, "loading keyring from rpmdb\n");
mi = rpmtsInitIterator(ts, RPMTAG_NAME, "gpg-pubkey", 0);
while ((h = rpmdbNextIterator(mi)) != NULL) {
struct rpmtd_s pubkeys;
const char *key;
if (!headerGet(h, RPMTAG_PUBKEYS, &pubkeys, HEADERGET_MINMEM))
continue;
while ((key = rpmtdNextString(&pubkeys))) {
uint8_t *pkt;
size_t pktlen;
if (b64decode(key, (void **) &pkt, &pktlen) == 0) {
rpmPubkey key = rpmPubkeyNew(pkt, pktlen);
if (rpmKeyringAddKey(ts->keyring, key) == 0) {
char *nvr = headerGetAsString(h, RPMTAG_NVR);
rpmlog(RPMLOG_DEBUG, "added key %s to keyring\n", nvr);
free(nvr);
nkeys++;
}
rpmPubkeyFree(key);
free(pkt);
}
}
rpmtdFreeData(&pubkeys);
}
rpmdbFreeIterator(mi);
return nkeys;
}
static void loadKeyring(rpmts ts)
{
ts->keyring = rpmKeyringNew();
if (loadKeyringFromFiles(ts) == 0) {
if (loadKeyringFromDB(ts) > 0) {
/* XXX make this a warning someday... */
rpmlog(RPMLOG_DEBUG, "Using legacy gpg-pubkey(s) from rpmdb\n");
}
}
}
/* Build pubkey header. */
static int makePubkeyHeader(rpmts ts, rpmPubkey key, Header h)
{
const char * afmt = "%{pubkeys:armor}";
const char * group = "Public Keys";
const char * license = "pubkey";
const char * buildhost = "localhost";
rpmsenseFlags pflags = (RPMSENSE_KEYRING|RPMSENSE_EQUAL);
uint32_t zero = 0;
pgpDig dig = NULL;
pgpDigParams pubp = NULL;
char * d = NULL;
char * enc = NULL;
char * n = NULL;
char * u = NULL;
char * v = NULL;
char * r = NULL;
char * evr = NULL;
int rc = -1;
if ((enc = rpmPubkeyBase64(key)) == NULL)
goto exit;
if ((dig = rpmPubkeyDig(key)) == NULL)
goto exit;
/* Build header elements. */
pubp = &dig->pubkey;
v = pgpHexStr(pubp->signid, sizeof(pubp->signid));
r = pgpHexStr(pubp->time, sizeof(pubp->time));
rasprintf(&n, "gpg(%s)", v+8);
rasprintf(&u, "gpg(%s)", pubp->userid ? pubp->userid : "none");
rasprintf(&evr, "%d:%s-%s", pubp->version, v, r);
headerPutString(h, RPMTAG_PUBKEYS, enc);
if ((d = headerFormat(h, afmt, NULL)) == NULL)
goto exit;
headerPutString(h, RPMTAG_NAME, "gpg-pubkey");
headerPutString(h, RPMTAG_VERSION, v+8);
headerPutString(h, RPMTAG_RELEASE, r);
headerPutString(h, RPMTAG_DESCRIPTION, d);
headerPutString(h, RPMTAG_GROUP, group);
headerPutString(h, RPMTAG_LICENSE, license);
headerPutString(h, RPMTAG_SUMMARY, u);
headerPutUint32(h, RPMTAG_SIZE, &zero, 1);
headerPutString(h, RPMTAG_PROVIDENAME, u);
headerPutString(h, RPMTAG_PROVIDEVERSION, evr);
headerPutUint32(h, RPMTAG_PROVIDEFLAGS, &pflags, 1);
headerPutString(h, RPMTAG_PROVIDENAME, n);
headerPutString(h, RPMTAG_PROVIDEVERSION, evr);
headerPutUint32(h, RPMTAG_PROVIDEFLAGS, &pflags, 1);
headerPutString(h, RPMTAG_RPMVERSION, RPMVERSION);
headerPutString(h, RPMTAG_BUILDHOST, buildhost);
headerPutString(h, RPMTAG_SOURCERPM, "(none)");
{ rpm_tid_t tid = rpmtsGetTid(ts);
headerPutUint32(h, RPMTAG_INSTALLTIME, &tid, 1);
headerPutUint32(h, RPMTAG_BUILDTIME, &tid, 1);
}
rc = 0;
exit:
pgpFreeDig(dig);
free(n);
free(u);
free(v);
free(r);
free(evr);
free(enc);
free(d);
return rc;
}
rpmRC rpmtsImportPubkey(const rpmts ts, const unsigned char * pkt, size_t pktlen)
{
Header h = headerNew();
rpmRC rc = RPMRC_FAIL; /* assume failure */
rpmPubkey pubkey = NULL;
rpmKeyring keyring = rpmtsGetKeyring(ts, 1);
if ((pubkey = rpmPubkeyNew(pkt, pktlen)) == NULL)
goto exit;
if (rpmKeyringAddKey(keyring, pubkey) != 0)
goto exit;
if (makePubkeyHeader(ts, pubkey, h) != 0)
goto exit;
/* Add header to database. */
if (rpmtsOpenDB(ts, (O_RDWR|O_CREAT)))
goto exit;
if (rpmdbAdd(rpmtsGetRdb(ts), rpmtsGetTid(ts), h, NULL, NULL) != 0)
goto exit;
rc = RPMRC_OK;
exit:
/* Clean up. */
headerFree(h);
rpmPubkeyFree(pubkey);
rpmKeyringFree(keyring);
return rc;
}
int rpmtsSetSolveCallback(rpmts ts,
int (*solve) (rpmts ts, rpmds key, const void * data),
const void * solveData)
{
int rc = 0;
if (ts) {
ts->solve = solve;
ts->solveData = solveData;
}
return rc;
}
int rpmtsSolve(rpmts ts, rpmds key)
{
int rc = 1; /* assume not found */
if (ts && ts->solve) {
rc = (*ts->solve)(ts, key, ts->solveData);
}
return rc;
}
rpmps rpmtsProblems(rpmts ts)
{
rpmps ps = rpmpsCreate();
rpmtsi pi = rpmtsiInit(ts);
rpmte p;
while ((p = rpmtsiNext(pi, 0)) != NULL) {
rpmps teprobs = rpmteProblems(p);
rpmpsMerge(ps, teprobs);
rpmpsFree(teprobs);
}
pi = rpmtsiFree(pi);
return ps;
}
void rpmtsCleanProblems(rpmts ts)
{
rpmte p;
rpmtsi pi = rpmtsiInit(ts);
while ((p = rpmtsiNext(pi, 0)) != NULL)
rpmteCleanProblems(p);
pi = rpmtsiFree(pi);
}
void rpmtsClean(rpmts ts)
{
rpmtsi pi; rpmte p;
tsMembers tsmem = rpmtsMembers(ts);
if (ts == NULL)
return;
/* Clean up after dependency checks. */
pi = rpmtsiInit(ts);
while ((p = rpmtsiNext(pi, 0)) != NULL)
rpmteCleanDS(p);
pi = rpmtsiFree(pi);
tsmem->addedPackages = rpmalFree(tsmem->addedPackages);
tsmem->numAddedPackages = 0;
rpmtsCleanProblems(ts);
}
void rpmtsEmpty(rpmts ts)
{
tsMembers tsmem = rpmtsMembers(ts);
if (ts == NULL)
return;
rpmtsClean(ts);
for (int oc = 0; oc < tsmem->orderCount; oc++) {
tsmem->order[oc] = rpmteFree(tsmem->order[oc]);
}
tsmem->orderCount = 0;
tsmem->numRemovedPackages = 0;
return;
}
static void rpmtsPrintStat(const char * name, struct rpmop_s * op)
{
static const unsigned int scale = (1000 * 1000);
if (op != NULL && op->count > 0)
fprintf(stderr, " %s %6d %6lu.%06lu MB %6lu.%06lu secs\n",
name, op->count,
(unsigned long)op->bytes/scale, (unsigned long)op->bytes%scale,
op->usecs/scale, op->usecs%scale);
}
static void rpmtsPrintStats(rpmts ts)
{
(void) rpmswExit(rpmtsOp(ts, RPMTS_OP_TOTAL), 0);
rpmtsPrintStat("total: ", rpmtsOp(ts, RPMTS_OP_TOTAL));
rpmtsPrintStat("check: ", rpmtsOp(ts, RPMTS_OP_CHECK));
rpmtsPrintStat("order: ", rpmtsOp(ts, RPMTS_OP_ORDER));
rpmtsPrintStat("fingerprint: ", rpmtsOp(ts, RPMTS_OP_FINGERPRINT));
rpmtsPrintStat("install: ", rpmtsOp(ts, RPMTS_OP_INSTALL));
rpmtsPrintStat("erase: ", rpmtsOp(ts, RPMTS_OP_ERASE));
rpmtsPrintStat("scriptlets: ", rpmtsOp(ts, RPMTS_OP_SCRIPTLETS));
rpmtsPrintStat("compress: ", rpmtsOp(ts, RPMTS_OP_COMPRESS));
rpmtsPrintStat("uncompress: ", rpmtsOp(ts, RPMTS_OP_UNCOMPRESS));
rpmtsPrintStat("digest: ", rpmtsOp(ts, RPMTS_OP_DIGEST));
rpmtsPrintStat("signature: ", rpmtsOp(ts, RPMTS_OP_SIGNATURE));
rpmtsPrintStat("dbadd: ", rpmtsOp(ts, RPMTS_OP_DBADD));
rpmtsPrintStat("dbremove: ", rpmtsOp(ts, RPMTS_OP_DBREMOVE));
rpmtsPrintStat("dbget: ", rpmtsOp(ts, RPMTS_OP_DBGET));
rpmtsPrintStat("dbput: ", rpmtsOp(ts, RPMTS_OP_DBPUT));
rpmtsPrintStat("dbdel: ", rpmtsOp(ts, RPMTS_OP_DBDEL));
}
rpmts rpmtsFree(rpmts ts)
{
tsMembers tsmem = rpmtsMembers(ts);
if (ts == NULL)
return NULL;
if (ts->nrefs > 1)
return rpmtsUnlink(ts);
rpmtsEmpty(ts);
(void) rpmtsCloseDB(ts);
tsmem->removedPackages = _free(tsmem->removedPackages);
tsmem->order = _free(tsmem->order);
ts->members = _free(ts->members);
ts->dsi = _free(ts->dsi);
if (ts->scriptFd != NULL) {
ts->scriptFd = fdFree(ts->scriptFd);
ts->scriptFd = NULL;
}
ts->rootDir = _free(ts->rootDir);
ts->currDir = _free(ts->currDir);
ts->keyring = rpmKeyringFree(ts->keyring);
ts->netsharedPaths = argvFree(ts->netsharedPaths);
ts->installLangs = argvFree(ts->installLangs);
if (_rpmts_stats)
rpmtsPrintStats(ts);
(void) rpmtsUnlink(ts);
ts = _free(ts);
return NULL;
}
rpmVSFlags rpmtsVSFlags(rpmts ts)
{
rpmVSFlags vsflags = 0;
if (ts != NULL)
vsflags = ts->vsflags;
return vsflags;
}
rpmVSFlags rpmtsSetVSFlags(rpmts ts, rpmVSFlags vsflags)
{
rpmVSFlags ovsflags = 0;
if (ts != NULL) {
ovsflags = ts->vsflags;
ts->vsflags = vsflags;
}
return ovsflags;
}
const char * rpmtsRootDir(rpmts ts)
{
return ts ? ts->rootDir : NULL;
}
int rpmtsSetRootDir(rpmts ts, const char * rootDir)
{
if (ts == NULL || (rootDir && rootDir[0] != '/')) {
return -1;
}
ts->rootDir = _free(ts->rootDir);
/* Ensure clean path with a trailing slash */
ts->rootDir = rootDir ? rpmGetPath(rootDir, NULL) : xstrdup("/");
if (!rstreq(ts->rootDir, "/")) {
rstrcat(&ts->rootDir, "/");
}
return 0;
}
const char * rpmtsCurrDir(rpmts ts)
{
const char * currDir = NULL;
if (ts != NULL) {
currDir = ts->currDir;
}
return currDir;
}
void rpmtsSetCurrDir(rpmts ts, const char * currDir)
{
if (ts != NULL) {
ts->currDir = _free(ts->currDir);
if (currDir)
ts->currDir = xstrdup(currDir);
}
}
FD_t rpmtsScriptFd(rpmts ts)
{
FD_t scriptFd = NULL;
if (ts != NULL) {
scriptFd = ts->scriptFd;
}
return scriptFd;
}
void rpmtsSetScriptFd(rpmts ts, FD_t scriptFd)
{
if (ts != NULL) {
if (ts->scriptFd != NULL) {
ts->scriptFd = fdFree(ts->scriptFd);
ts->scriptFd = NULL;
}
if (scriptFd != NULL)
ts->scriptFd = fdLink(scriptFd);
}
}
int rpmtsSELinuxEnabled(rpmts ts)
{
return (ts != NULL ? (ts->selinuxEnabled > 0) : 0);
}
int rpmtsChrootDone(rpmts ts)
{
return (ts != NULL ? ts->chrootDone : 0);
}
int rpmtsSetChrootDone(rpmts ts, int chrootDone)
{
int ochrootDone = 0;
if (ts != NULL) {
ochrootDone = ts->chrootDone;
rpmdbSetChrootDone(rpmtsGetRdb(ts), chrootDone);
ts->chrootDone = chrootDone;
}
return ochrootDone;
}
rpm_tid_t rpmtsGetTid(rpmts ts)
{
rpm_tid_t tid = (rpm_tid_t)-1; /* XXX -1 is time(2) error return. */
if (ts != NULL) {
tid = ts->tid;
}
return tid;
}
rpm_tid_t rpmtsSetTid(rpmts ts, rpm_tid_t tid)
{
rpm_tid_t otid = (rpm_tid_t)-1; /* XXX -1 is time(2) error return. */
if (ts != NULL) {
otid = ts->tid;
ts->tid = tid;
}
return otid;
}
rpmdb rpmtsGetRdb(rpmts ts)
{
rpmdb rdb = NULL;
if (ts != NULL) {
rdb = ts->rdb;
}
return rdb;
}
void * rpmtsNotify(rpmts ts, rpmte te,
rpmCallbackType what, rpm_loff_t amount, rpm_loff_t total)
{
void * ptr = NULL;
if (ts && ts->notify) {
Header h = NULL;
fnpyKey cbkey = NULL;
if (te) {
h = rpmteHeader(te);
cbkey = rpmteKey(te);
}
ptr = ts->notify(h, what, amount, total, cbkey, ts->notifyData);
if (h) {
headerUnlink(h); /* undo rpmteHeader() ref */
}
}
return ptr;
}
int rpmtsNElements(rpmts ts)
{
int nelements = 0;
tsMembers tsmem = rpmtsMembers(ts);
if (tsmem != NULL && tsmem->order != NULL) {
nelements = tsmem->orderCount;
}
return nelements;
}
rpmte rpmtsElement(rpmts ts, int ix)
{
rpmte te = NULL;
tsMembers tsmem = rpmtsMembers(ts);
if (tsmem != NULL && tsmem->order != NULL) {
if (ix >= 0 && ix < tsmem->orderCount)
te = tsmem->order[ix];
}
return te;
}
rpmprobFilterFlags rpmtsFilterFlags(rpmts ts)
{
return (ts != NULL ? ts->ignoreSet : 0);
}
rpmtransFlags rpmtsFlags(rpmts ts)
{
return (ts != NULL ? ts->transFlags : 0);
}
rpmtransFlags rpmtsSetFlags(rpmts ts, rpmtransFlags transFlags)
{
rpmtransFlags otransFlags = 0;
if (ts != NULL) {
otransFlags = ts->transFlags;
ts->transFlags = transFlags;
}
return otransFlags;
}
rpmSpec rpmtsSpec(rpmts ts)
{
return ts->spec;
}
rpmSpec rpmtsSetSpec(rpmts ts, rpmSpec spec)
{
rpmSpec ospec = ts->spec;
ts->spec = spec;
return ospec;
}
rpm_color_t rpmtsColor(rpmts ts)
{
return (ts != NULL ? ts->color : 0);
}
rpm_color_t rpmtsSetColor(rpmts ts, rpm_color_t color)
{
rpm_color_t ocolor = 0;
if (ts != NULL) {
ocolor = ts->color;
ts->color = color;
}
return ocolor;
}
rpm_color_t rpmtsPrefColor(rpmts ts)
{
return (ts != NULL ? ts->prefcolor : 0);
}
rpm_color_t rpmtsSetPrefColor(rpmts ts, rpm_color_t color)
{
rpm_color_t ocolor = 0;
if (ts != NULL) {
ocolor = ts->prefcolor;
ts->prefcolor = color;
}
return ocolor;
}
rpmop rpmtsOp(rpmts ts, rpmtsOpX opx)
{
rpmop op = NULL;
if (ts != NULL && opx >= 0 && opx < RPMTS_OP_MAX)
op = ts->ops + opx;
return op;
}
int rpmtsSetNotifyCallback(rpmts ts,
rpmCallbackFunction notify, rpmCallbackData notifyData)
{
if (ts != NULL) {
ts->notify = notify;
ts->notifyData = notifyData;
}
return 0;
}
tsMembers rpmtsMembers(rpmts ts)
{
return (ts != NULL) ? ts->members : NULL;
}
rpmts rpmtsCreate(void)
{
rpmts ts;
tsMembers tsmem;
ts = xcalloc(1, sizeof(*ts));
memset(&ts->ops, 0, sizeof(ts->ops));
(void) rpmswEnter(rpmtsOp(ts, RPMTS_OP_TOTAL), -1);
ts->dsi = NULL;
ts->solve = NULL;
ts->solveData = NULL;
ts->rdb = NULL;
ts->dbmode = O_RDONLY;
ts->scriptFd = NULL;
ts->tid = (rpm_tid_t) time(NULL);
ts->color = rpmExpandNumeric("%{?_transaction_color}");
ts->prefcolor = rpmExpandNumeric("%{?_prefer_color}")?:2;
ts->netsharedPaths = NULL;
ts->installLangs = NULL;
{ char *tmp = rpmExpand("%{_netsharedpath}", NULL);
if (tmp && *tmp != '%') {
argvSplit(&ts->netsharedPaths, tmp, ":");
}
free(tmp);
tmp = rpmExpand("%{_install_langs}", NULL);
if (tmp && *tmp != '%') {
ARGV_t langs = NULL;
argvSplit(&langs, tmp, ":");
/* If we'll be installing all languages anyway, don't bother */
for (ARGV_t l = langs; *l; l++) {
if (rstreq(*l, "all")) {
langs = argvFree(langs);
break;
}
}
ts->installLangs = langs;
}
free(tmp);
}
tsmem = xcalloc(1, sizeof(*ts->members));
tsmem->delta = 5;
tsmem->numAddedPackages = 0;
tsmem->addedPackages = NULL;
tsmem->numRemovedPackages = 0;
tsmem->allocedRemovedPackages = tsmem->delta;
tsmem->removedPackages = xcalloc(tsmem->allocedRemovedPackages,
sizeof(*tsmem->removedPackages));
tsmem->orderAlloced = 0;
tsmem->orderCount = 0;
tsmem->order = NULL;
ts->members = tsmem;
ts->rootDir = NULL;
ts->currDir = NULL;
ts->chrootDone = 0;
ts->selinuxEnabled = is_selinux_enabled();
ts->keyring = NULL;
ts->nrefs = 0;
return rpmtsLink(ts);
}
rpmtsi rpmtsiFree(rpmtsi tsi)
{
/* XXX watchout: a funky recursion segfaults here iff nrefs is wrong. */
if (tsi)
tsi->ts = rpmtsFree(tsi->ts);
return _free(tsi);
}
rpmtsi rpmtsiInit(rpmts ts)
{
rpmtsi tsi = NULL;
tsi = xcalloc(1, sizeof(*tsi));
tsi->ts = rpmtsLink(ts);
tsi->oc = 0;
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->oc < rpmtsNElements(tsi->ts)) oc = tsi->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 || (rpmteType(te) & type) != 0)
break;
}
return te;
}
|