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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
|
/** \ingroup rpmdep
* \file lib/rpmts.c
* Routine(s) to handle a "rpmts" transaction sets.
*/
#include "system.h"
#include "rpmio_internal.h" /* XXX for pgp and beecrypt */
#include <rpmlib.h>
#include <rpmmacro.h> /* XXX rpmtsOpenDB() needs rpmGetPath */
#include "rpmdb.h" /* XXX stealing db->db_mode. */
#include "rpmal.h"
#include "rpmds.h"
#include "rpmfi.h"
#define _RPMTE_INTERNAL /* XXX te->h */
#include "rpmte.h"
#define _RPMTS_INTERNAL
#include "rpmts.h"
/* XXX FIXME: merge with existing (broken?) tests in system.h */
/* portability fiddles */
#if STATFS_IN_SYS_STATVFS
/*@-incondefs@*/
#if defined(__LCLINT__)
/*@-declundef -exportheader -protoparammatch @*/ /* LCL: missing annotation */
extern int statvfs (const char * file, /*@out@*/ struct statvfs * buf)
/*@globals fileSystem @*/
/*@modifies *buf, fileSystem @*/;
/*@=declundef =exportheader =protoparammatch @*/
/*@=incondefs@*/
#else
# include <sys/statvfs.h>
#endif
#else
# if STATFS_IN_SYS_VFS
# include <sys/vfs.h>
# else
# if STATFS_IN_SYS_MOUNT
# include <sys/mount.h>
# else
# if STATFS_IN_SYS_STATFS
# include <sys/statfs.h>
# endif
# endif
# endif
#endif
#include "debug.h"
/*@access rpmdb @*/ /* XXX db->db_chrootDone, NULL */
/*@access FD_t @*/ /* XXX compared with NULL */
/*@access rpmps @*/
/*@access rpmDiskSpaceInfo @*/
/*@access rpmte @*/
/*@access rpmtsi @*/
/*@access rpmts @*/
/*@access fnpyKey @*/
/*@access pgpDig @*/
/*@access pgpDigParams @*/
/*@unchecked@*/
int _rpmts_debug = 0;
char * hGetNEVR(Header h, const char ** np)
{
const char * n, * v, * r;
char * NVR, * t;
(void) headerNVR(h, &n, &v, &r);
NVR = t = xcalloc(1, strlen(n) + strlen(v) + strlen(r) + sizeof("--"));
/*@-boundswrite@*/
t = stpcpy(t, n);
t = stpcpy(t, "-");
t = stpcpy(t, v);
t = stpcpy(t, "-");
t = stpcpy(t, r);
if (np)
*np = n;
/*@=boundswrite@*/
return NVR;
}
rpmts XrpmtsUnlink(rpmts ts, const char * msg, const char * fn, unsigned ln)
{
/*@-modfilesystem@*/
if (_rpmts_debug)
fprintf(stderr, "--> ts %p -- %d %s at %s:%u\n", ts, ts->nrefs, msg, fn, ln);
/*@=modfilesystem@*/
ts->nrefs--;
return NULL;
}
rpmts XrpmtsLink(rpmts ts, const char * msg, const char * fn, unsigned ln)
{
ts->nrefs++;
/*@-modfilesystem@*/
if (_rpmts_debug)
fprintf(stderr, "--> ts %p ++ %d %s at %s:%u\n", ts, ts->nrefs, msg, fn, ln);
/*@=modfilesystem@*/
/*@-refcounttrans@*/ return ts; /*@=refcounttrans@*/
}
int rpmtsCloseDB(rpmts ts)
{
int rc = 0;
if (ts->rdb != NULL) {
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) {
const char * dn;
dn = rpmGetPath(ts->rootDir, "%{_dbpath}", NULL);
rpmMessage(RPMMESS_ERROR,
_("cannot open Packages database in %s\n"), dn);
dn = _free(dn);
}
return rc;
}
int rpmtsInitDB(rpmts ts, int dbmode)
{
return rpmdbInit(ts->rootDir, dbmode);
}
int rpmtsRebuildDB(rpmts ts)
{
int rc;
if (!(ts->vsflags & RPMVSF_NOHDRCHK))
rc = rpmdbRebuild(ts->rootDir, ts, headerCheck);
else
rc = rpmdbRebuild(ts->rootDir, NULL, NULL);
return rc;
}
int rpmtsVerifyDB(rpmts ts)
{
return rpmdbVerify(ts->rootDir);
}
rpmdbMatchIterator rpmtsInitIterator(const rpmts ts, rpmTag rpmtag,
const void * keyp, size_t keylen)
{
rpmdbMatchIterator mi;
if (ts->rdb == NULL && rpmtsOpenDB(ts, ts->dbmode))
return NULL;
mi = rpmdbInitIterator(ts->rdb, rpmtag, keyp, keylen);
if (mi && !(ts->vsflags & RPMVSF_NOHDRCHK))
(void) rpmdbSetHdrChk(mi, ts, headerCheck);
return mi;
}
rpmVerifySignatureReturn rpmtsFindPubkey(rpmts ts)
{
const void * sig = rpmtsSig(ts);
pgpDig dig = rpmtsDig(ts);
pgpDigParams sigp = rpmtsSignature(ts);
pgpDigParams pubp = rpmtsSignature(ts);
rpmVerifySignatureReturn res;
int xx;
if (sig == NULL || dig == NULL || sigp == NULL || pubp == NULL) {
res = RPMSIG_NOKEY; /* XXX RPMSIG_ARGS */
goto exit;
}
if (ts->pkpkt == NULL
|| memcmp(sigp->signid, ts->pksignid, sizeof(ts->pksignid)))
{
int ix = -1;
rpmdbMatchIterator mi;
Header h;
ts->pkpkt = _free(ts->pkpkt);
ts->pkpktlen = 0;
memset(ts->pksignid, 0, sizeof(ts->pksignid));
/* Retrieve the pubkey that matches the signature. */
mi = rpmtsInitIterator(ts, RPMTAG_PUBKEYS, sigp->signid, sizeof(sigp->signid));
while ((h = rpmdbNextIterator(mi)) != NULL) {
const char ** pubkeys;
int_32 pt, pc;
if (!headerGetEntry(h, RPMTAG_PUBKEYS, &pt, (void **)&pubkeys, &pc))
continue;
ix = rpmdbGetIteratorFileNum(mi);
/*@-boundsread@*/
if (ix >= pc
|| b64decode(pubkeys[ix], (void **) &ts->pkpkt, &ts->pkpktlen))
ix = -1;
/*@=boundsread@*/
pubkeys = headerFreeData(pubkeys, pt);
break;
}
mi = rpmdbFreeIterator(mi);
/* Was a matching pubkey found? */
if (ix < 0 || ts->pkpkt == NULL) {
res = RPMSIG_NOKEY;
goto exit;
}
/*
* Can the pubkey packets be parsed?
* Do the parameters match the signature?
*/
if (pgpPrtPkts(ts->pkpkt, ts->pkpktlen, NULL, 0)
&& sigp->pubkey_algo == pubp->pubkey_algo
#ifdef NOTYET
&& sigp->hash_algo == pubp->hash_algo
#endif
&& !memcmp(sigp->signid, pubp->signid, sizeof(sigp->signid)))
{
ts->pkpkt = _free(ts->pkpkt);
ts->pkpktlen = 0;
res = RPMSIG_NOKEY;
goto exit;
}
/* XXX Verify the pubkey signature. */
/* Packet looks good, save the signer id. */
/*@-boundsread@*/
memcpy(ts->pksignid, sigp->signid, sizeof(ts->pksignid));
/*@=boundsread@*/
rpmMessage(RPMMESS_DEBUG, "========== %s pubkey id %s\n",
(sigp->pubkey_algo == PGPPUBKEYALGO_DSA ? "DSA" :
(sigp->pubkey_algo == PGPPUBKEYALGO_RSA ? "RSA" : "???")),
pgpHexStr(sigp->signid, sizeof(sigp->signid)));
}
#ifdef NOTNOW
{
if (ts->pkpkt == NULL) {
const char * pkfn = rpmExpand("%{_gpg_pubkey}", NULL);
if (pgpReadPkts(pkfn, &ts->pkpkt, &ts->pkpktlen) != PGPARMOR_PUBKEY) {
pkfn = _free(pkfn);
res = RPMSIG_NOKEY;
goto exit;
}
pkfn = _free(pkfn);
}
}
#endif
/* Retrieve parameters from pubkey packet(s). */
xx = pgpPrtPkts(ts->pkpkt, ts->pkpktlen, dig, 0);
/* Do the parameters match the signature? */
if (sigp->pubkey_algo == pubp->pubkey_algo
#ifdef NOTYET
&& sigp->hash_algo == pubp->hash_algo
#endif
&& !memcmp(sigp->signid, pubp->signid, sizeof(sigp->signid)) )
res = RPMSIG_OK;
else
res = RPMSIG_NOKEY;
/* XXX Verify the signature signature. */
exit:
return res;
}
int rpmtsCloseSDB(rpmts ts)
{
int rc = 0;
if (ts->sdb != NULL) {
rc = rpmdbClose(ts->sdb);
ts->sdb = NULL;
}
return rc;
}
int rpmtsOpenSDB(rpmts ts, int dbmode)
{
static int has_sdbpath = -1;
int rc = 0;
if (ts->sdb != NULL && ts->sdbmode == dbmode)
return 0;
if (has_sdbpath < 0)
has_sdbpath = rpmExpandNumeric("%{?_solve_dbpath:1}");
/* If not configured, don't try to open. */
if (has_sdbpath <= 0)
return 1;
addMacro(NULL, "_dbpath", NULL, "%{_solve_dbpath}", RMIL_DEFAULT);
rc = rpmdbOpen(ts->rootDir, &ts->sdb, ts->sdbmode, 0644);
if (rc) {
const char * dn;
dn = rpmGetPath(ts->rootDir, "%{_dbpath}", NULL);
rpmMessage(RPMMESS_DEBUG,
_("cannot open Solve database in %s\n"), dn);
dn = _free(dn);
}
delMacro(NULL, "_dbpath");
return rc;
}
/**
* Compare suggested package resolutions (qsort/bsearch).
* @param a 1st instance address
* @param b 2nd instance address
* @return result of comparison
*/
static int sugcmp(const void * a, const void * b)
/*@*/
{
/*@-boundsread@*/
const char * astr = *(const char **)a;
const char * bstr = *(const char **)b;
/*@=boundsread@*/
return strcmp(astr, bstr);
}
/*@-bounds@*/
int rpmtsSolve(rpmts ts, rpmds ds, /*@unused@*/ const void * data)
{
const char * errstr;
const char * str;
const char * qfmt;
rpmdbMatchIterator mi;
Header bh;
Header h;
time_t bhtime;
rpmTag rpmtag;
const char * keyp;
size_t keylen;
int rc = 1; /* assume not found */
int xx;
/* Make suggestions only for install Requires: */
if (ts->goal != TSM_INSTALL)
return rc;
if (rpmdsTagN(ds) != RPMTAG_REQUIRENAME)
return rc;
keyp = rpmdsN(ds);
if (keyp == NULL)
return rc;
if (ts->sdb == NULL) {
xx = rpmtsOpenSDB(ts, ts->sdbmode);
if (xx) return rc;
}
/* Look for a matching Provides: in suggested universe. */
rpmtag = (*keyp == '/' ? RPMTAG_BASENAMES : RPMTAG_PROVIDENAME);
keylen = 0;
mi = rpmdbInitIterator(ts->sdb, rpmtag, keyp, keylen);
bhtime = 0;
bh = NULL;
while ((h = rpmdbNextIterator(mi)) != NULL) {
time_t htime;
int_32 * ip;
if (rpmtag == RPMTAG_PROVIDENAME && !rpmdsAnyMatchesDep(h, ds, 1))
continue;
htime = 0;
if (headerGetEntry(h, RPMTAG_BUILDTIME, NULL, (void **)&ip, NULL))
htime = (time_t)*ip;
/* XXX Prefer the newest build if given alternatives. */
if (htime <= bhtime)
continue;
bh = headerFree(bh);
bh = headerLink(h);
bhtime = htime;
}
mi = rpmdbFreeIterator(mi);
/* Is there a suggested resolution? */
if (bh == NULL)
goto exit;
/* Format the suggestion. */
qfmt = rpmExpand("%{?_solve_name_fmt}", NULL);
if (qfmt == NULL || *qfmt == '\0')
goto exit;
str = headerSprintf(bh, qfmt, rpmTagTable, rpmHeaderFormats, &errstr);
bh = headerFree(bh);
qfmt = _free(qfmt);
if (str == NULL) {
rpmError(RPMERR_QFMT, _("incorrect format: %s\n"), errstr);
goto exit;
}
/* If suggestion is already present, don't bother. */
if (ts->suggests != NULL && ts->nsuggests > 0) {
if (bsearch(&str, ts->suggests, ts->nsuggests,
sizeof(*ts->suggests), sugcmp))
goto exit;
}
/* Add a new (unique) suggestion. */
ts->suggests = xrealloc(ts->suggests,
sizeof(*ts->suggests) * (ts->nsuggests + 2));
ts->suggests[ts->nsuggests] = str;
ts->nsuggests++;
ts->suggests[ts->nsuggests] = NULL;
if (ts->nsuggests > 1)
qsort(ts->suggests, ts->nsuggests, sizeof(*ts->suggests), sugcmp);
exit:
/*@-nullstate@*/ /* FIX: ts->suggests[] may be NULL */
return rc;
/*@=nullstate@*/
}
/*@=bounds@*/
int rpmtsAvailable(rpmts ts, const rpmds ds)
{
fnpyKey * sugkey;
int rc = 1; /* assume not found */
if (ts->availablePackages == NULL)
return rc;
sugkey = rpmalAllSatisfiesDepend(ts->availablePackages, ds, NULL);
if (sugkey == NULL)
return rc;
/* XXX no alternatives yet */
if (sugkey[0] != NULL) {
ts->suggests = xrealloc(ts->suggests,
sizeof(*ts->suggests) * (ts->nsuggests + 2));
ts->suggests[ts->nsuggests] = sugkey[0];
sugkey[0] = NULL;
ts->nsuggests++;
ts->suggests[ts->nsuggests] = NULL;
}
sugkey = _free(sugkey);
/*@-nullstate@*/ /* FIX: ts->suggests[] may be NULL */
return rc;
/*@=nullstate@*/
}
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;
}
rpmps rpmtsProblems(rpmts ts)
{
rpmps ps = NULL;
if (ts) {
if (ts->probs)
ps = rpmpsLink(ts->probs, NULL);
}
return ps;
}
void rpmtsCleanDig(rpmts ts)
{
ts->sig = headerFreeData(ts->sig, ts->sigtype);
ts->dig = pgpFreeDig(ts->dig);
}
void rpmtsClean(rpmts ts)
{
rpmtsi pi; rpmte p;
if (ts == NULL)
return;
/* Clean up after dependency checks. */
pi = rpmtsiInit(ts);
while ((p = rpmtsiNext(pi, 0)) != NULL)
rpmteCleanDS(p);
pi = rpmtsiFree(pi);
ts->addedPackages = rpmalFree(ts->addedPackages);
ts->numAddedPackages = 0;
ts->suggests = _free(ts->suggests);
ts->nsuggests = 0;
ts->probs = rpmpsFree(ts->probs);
rpmtsCleanDig(ts);
}
rpmts rpmtsFree(rpmts ts)
{
rpmtsi pi; rpmte p;
int oc;
if (ts == NULL)
return NULL;
(void) rpmtsUnlink(ts, "tsCreate");
/*@-usereleased@*/
if (ts->nrefs > 0)
return NULL;
(void) rpmtsCloseDB(ts);
(void) rpmtsCloseSDB(ts);
ts->availablePackages = rpmalFree(ts->availablePackages);
ts->numAvailablePackages = 0;
ts->dsi = _free(ts->dsi);
ts->removedPackages = _free(ts->removedPackages);
if (ts->scriptFd != NULL) {
ts->scriptFd = fdFree(ts->scriptFd, "rpmtsFree");
ts->scriptFd = NULL;
}
ts->rootDir = _free(ts->rootDir);
ts->currDir = _free(ts->currDir);
for (pi = rpmtsiInit(ts), oc = 0; (p = rpmtsiNext(pi, 0)) != NULL; oc++) {
/*@-type -unqualifiedtrans @*/
ts->order[oc] = rpmteFree(ts->order[oc]);
/*@=type =unqualifiedtrans @*/
}
pi = rpmtsiFree(pi);
/*@-type +voidabstract @*/ /* FIX: double indirection */
ts->order = _free(ts->order);
/*@=type =voidabstract @*/
if (ts->pkpkt != NULL)
ts->pkpkt = _free(ts->pkpkt);
ts->pkpktlen = 0;
memset(ts->pksignid, 0, sizeof(ts->pksignid));
/*@-nullstate@*/ /* FIX: partial annotations */
rpmtsClean(ts);
/*@=nullstate@*/
/*@-refcounttrans@*/ ts = _free(ts); /*@=refcounttrans@*/
/*@=usereleased@*/
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 != NULL ? ts->rootDir : NULL);
}
void rpmtsSetRootDir(rpmts ts, const char * rootDir)
{
if (ts != NULL) {
size_t rootLen;
ts->rootDir = _free(ts->rootDir);
if (rootDir == NULL) {
#ifndef DYING
ts->rootDir = xstrdup("");
#endif
return;
}
rootLen = strlen(rootDir);
/*@-branchstate@*/
/* Make sure that rootDir has trailing / */
if (!(rootLen && rootDir[rootLen - 1] == '/')) {
char * t = alloca(rootLen + 2);
*t = '\0';
(void) stpcpy( stpcpy(t, rootDir), "/");
rootDir = t;
}
/*@=branchstate@*/
ts->rootDir = xstrdup(rootDir);
}
}
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;
}
/*@-compdef -refcounttrans -usereleased@*/
return scriptFd;
/*@=compdef =refcounttrans =usereleased@*/
}
void rpmtsSetScriptFd(rpmts ts, FD_t scriptFd)
{
if (ts != NULL) {
if (ts->scriptFd != NULL) {
ts->scriptFd = fdFree(ts->scriptFd, "rpmtsSetScriptFd");
ts->scriptFd = NULL;
}
if (scriptFd != NULL)
ts->scriptFd = fdLink(scriptFd, "rpmtsSetScriptFd");
}
}
int rpmtsChrootDone(rpmts ts)
{
int chrootDone = 0;
if (ts != NULL) {
chrootDone = ts->chrootDone;
}
return chrootDone;
}
int rpmtsSetChrootDone(rpmts ts, int chrootDone)
{
int ochrootDone = 0;
if (ts != NULL) {
ochrootDone = ts->chrootDone;
if (ts->rdb != NULL)
ts->rdb->db_chrootDone = chrootDone;
ts->chrootDone = chrootDone;
}
return ochrootDone;
}
int_32 rpmtsGetTid(rpmts ts)
{
int_32 tid = 0;
if (ts != NULL) {
tid = ts->tid;
}
return tid;
}
int_32 rpmtsSetTid(rpmts ts, int_32 tid)
{
int_32 otid = 0;
if (ts != NULL) {
otid = ts->tid;
ts->tid = tid;
}
return otid;
}
int_32 rpmtsSigtag(const rpmts ts)
{
int_32 sigtag = 0;
if (ts != NULL)
sigtag = ts->sigtag;
return sigtag;
}
int_32 rpmtsSigtype(const rpmts ts)
{
int_32 sigtag = 0;
if (ts != NULL)
sigtag = ts->sigtag;
return sigtag;
}
const void * rpmtsSig(const rpmts ts)
{
const void * sig = NULL;
if (ts != NULL)
sig = ts->sig;
return sig;
}
int_32 rpmtsSiglen(const rpmts ts)
{
int_32 siglen = 0;
if (ts != NULL)
siglen = ts->siglen;
return siglen;
}
int rpmtsSetSig(rpmts ts,
int_32 sigtag, int_32 sigtype, const void * sig, int_32 siglen)
{
if (ts != NULL) {
if (ts->sig)
ts->sig = headerFreeData(ts->sig, ts->sigtype);
ts->sigtag = sigtag;
ts->sigtype = sigtype;
/*@-assignexpose -kepttrans@*/
ts->sig = sig;
/*@=assignexpose =kepttrans@*/
ts->siglen = siglen;
}
return 0;
}
pgpDig rpmtsDig(rpmts ts)
{
/*@-mods@*/ /* FIX: hide lazy malloc for now */
if (ts->dig == NULL)
ts->dig = pgpNewDig();
/*@=mods@*/
if (ts->dig == NULL)
return NULL;
return ts->dig;
}
pgpDigParams rpmtsSignature(const rpmts ts)
{
pgpDig dig = rpmtsDig(ts);
if (dig == NULL) return NULL;
/*@-immediatetrans@*/
return &dig->signature;
/*@=immediatetrans@*/
}
pgpDigParams rpmtsPubkey(const rpmts ts)
{
pgpDig dig = rpmtsDig(ts);
if (dig == NULL) return NULL;
/*@-immediatetrans@*/
return &dig->pubkey;
/*@=immediatetrans@*/
}
rpmdb rpmtsGetRdb(rpmts ts)
{
rpmdb rdb = NULL;
if (ts != NULL) {
rdb = ts->rdb;
}
/*@-compdef -refcounttrans -usereleased @*/
return rdb;
/*@=compdef =refcounttrans =usereleased @*/
}
int rpmtsInitDSI(const rpmts ts)
{
rpmDiskSpaceInfo dsi;
struct stat sb;
int rc;
int i;
if (rpmtsFilterFlags(ts) & RPMPROB_FILTER_DISKSPACE)
return 0;
rc = rpmGetFilesystemList(&ts->filesystems, &ts->filesystemCount);
if (rc || ts->filesystems == NULL || ts->filesystemCount <= 0)
return rc;
/* Get available space on mounted file systems. */
rpmMessage(RPMMESS_DEBUG, _("getting list of mounted filesystems\n"));
ts->dsi = _free(ts->dsi);
ts->dsi = xcalloc((ts->filesystemCount + 1), sizeof(*ts->dsi));
dsi = ts->dsi;
if (dsi != NULL)
for (i = 0; (i < ts->filesystemCount) && dsi; i++, dsi++) {
#if STATFS_IN_SYS_STATVFS
struct statvfs sfb;
memset(&sfb, 0, sizeof(sfb));
rc = statvfs(ts->filesystems[i], &sfb);
#else
struct statfs sfb;
memset(&sfb, 0, sizeof(sfb));
# if STAT_STATFS4
/* This platform has the 4-argument version of the statfs call. The last two
* should be the size of struct statfs and 0, respectively. The 0 is the
* filesystem type, and is always 0 when statfs is called on a mounted
* filesystem, as we're doing.
*/
rc = statfs(ts->filesystems[i], &sfb, sizeof(sfb), 0);
# else
rc = statfs(ts->filesystems[i], &sfb);
# endif
#endif
if (rc)
break;
rc = stat(ts->filesystems[i], &sb);
if (rc)
break;
dsi->dev = sb.st_dev;
dsi->bsize = sfb.f_bsize;
dsi->bneeded = 0;
dsi->ineeded = 0;
#ifdef STATFS_HAS_F_BAVAIL
dsi->bavail = sfb.f_bavail;
#else
/* FIXME: the statfs struct doesn't have a member to tell how many blocks are
* available for non-superusers. f_blocks - f_bfree is probably too big, but
* it's about all we can do.
*/
dsi->bavail = sfb.f_blocks - sfb.f_bfree;
#endif
/* XXX Avoid FAT and other file systems that have not inodes. */
dsi->iavail = !(sfb.f_ffree == 0 && sfb.f_files == 0)
? sfb.f_ffree : -1;
}
return rc;
}
void rpmtsUpdateDSI(const rpmts ts, dev_t dev,
uint_32 fileSize, uint_32 prevSize, uint_32 fixupSize,
fileAction action)
{
rpmDiskSpaceInfo dsi;
uint_32 bneeded;
dsi = ts->dsi;
if (dsi) {
while (dsi->bsize && dsi->dev != dev)
dsi++;
if (dsi->bsize == 0)
dsi = NULL;
}
if (dsi == NULL)
return;
bneeded = BLOCK_ROUND(fileSize, dsi->bsize);
switch (action) {
case FA_BACKUP:
case FA_SAVE:
case FA_ALTNAME:
dsi->ineeded++;
dsi->bneeded += bneeded;
/*@switchbreak@*/ break;
/*
* FIXME: If two packages share a file (same md5sum), and
* that file is being replaced on disk, will dsi->bneeded get
* adjusted twice? Quite probably!
*/
case FA_CREATE:
dsi->bneeded += bneeded;
dsi->bneeded -= BLOCK_ROUND(prevSize, dsi->bsize);
/*@switchbreak@*/ break;
case FA_ERASE:
dsi->ineeded--;
dsi->bneeded -= bneeded;
/*@switchbreak@*/ break;
default:
/*@switchbreak@*/ break;
}
if (fixupSize)
dsi->bneeded -= BLOCK_ROUND(fixupSize, dsi->bsize);
}
void rpmtsCheckDSIProblems(const rpmts ts, const rpmte te)
{
rpmDiskSpaceInfo dsi;
rpmps ps;
int fc;
int i;
if (ts->filesystems == NULL || ts->filesystemCount <= 0)
return;
dsi = ts->dsi;
if (dsi == NULL)
return;
fc = rpmfiFC( rpmteFI(te, RPMTAG_BASENAMES) );
if (fc <= 0)
return;
ps = rpmtsProblems(ts);
for (i = 0; i < ts->filesystemCount; i++, dsi++) {
if (dsi->bavail > 0 && adj_fs_blocks(dsi->bneeded) > dsi->bavail) {
rpmpsAppend(ps, RPMPROB_DISKSPACE,
rpmteNEVR(te), rpmteKey(te),
ts->filesystems[i], NULL, NULL,
(adj_fs_blocks(dsi->bneeded) - dsi->bavail) * dsi->bsize);
}
if (dsi->iavail > 0 && adj_fs_blocks(dsi->ineeded) > dsi->iavail) {
rpmpsAppend(ps, RPMPROB_DISKNODES,
rpmteNEVR(te), rpmteKey(te),
ts->filesystems[i], NULL, NULL,
(adj_fs_blocks(dsi->ineeded) - dsi->iavail));
}
}
ps = rpmpsFree(ps);
}
void * rpmtsNotify(rpmts ts, rpmte te,
rpmCallbackType what, unsigned long amount, unsigned long total)
{
void * ptr = NULL;
if (ts && ts->notify && te) {
assert(!(te->type == TR_ADDED && te->h == NULL));
/*@-type@*/ /* FIX: cast? */
/*@-noeffectuncon @*/ /* FIX: check rc */
ptr = ts->notify(te->h, what, amount, total,
rpmteKey(te), ts->notifyData);
/*@=noeffectuncon @*/
/*@=type@*/
}
return ptr;
}
int rpmtsNElements(rpmts ts)
{
int nelements = 0;
if (ts != NULL && ts->order != NULL) {
nelements = ts->orderCount;
}
return nelements;
}
rpmte rpmtsElement(rpmts ts, int ix)
{
rpmte te = NULL;
if (ts != NULL && ts->order != NULL) {
if (ix >= 0 && ix < ts->orderCount)
te = ts->order[ix];
}
/*@-compdef@*/
return te;
/*@=compdef@*/
}
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;
}
int rpmtsSetNotifyCallback(rpmts ts,
rpmCallbackFunction notify, rpmCallbackData notifyData)
{
if (ts != NULL) {
ts->notify = notify;
ts->notifyData = notifyData;
}
return 0;
}
int rpmtsGetKeys(const rpmts ts, fnpyKey ** ep, int * nep)
{
int rc = 0;
if (nep) *nep = ts->orderCount;
if (ep) {
rpmtsi pi; rpmte p;
fnpyKey * e;
*ep = e = xmalloc(ts->orderCount * sizeof(*e));
pi = rpmtsiInit(ts);
while ((p = rpmtsiNext(pi, 0)) != NULL) {
switch (rpmteType(p)) {
case TR_ADDED:
/*@-dependenttrans@*/
*e = rpmteKey(p);
/*@=dependenttrans@*/
/*@switchbreak@*/ break;
case TR_REMOVED:
default:
*e = NULL;
/*@switchbreak@*/ break;
}
e++;
}
pi = rpmtsiFree(pi);
}
return rc;
}
rpmts rpmtsCreate(void)
{
rpmts ts;
ts = xcalloc(1, sizeof(*ts));
ts->goal = TSM_UNKNOWN;
ts->filesystemCount = 0;
ts->filesystems = NULL;
ts->dsi = NULL;
ts->solve = rpmtsSolve;
ts->solveData = NULL;
ts->nsuggests = 0;
ts->suggests = NULL;
ts->sdb = NULL;
ts->sdbmode = O_RDONLY;
ts->rdb = NULL;
ts->dbmode = O_RDONLY;
ts->scriptFd = NULL;
ts->tid = (int_32) time(NULL);
ts->delta = 5;
ts->numRemovedPackages = 0;
ts->allocedRemovedPackages = ts->delta;
ts->removedPackages = xcalloc(ts->allocedRemovedPackages,
sizeof(*ts->removedPackages));
ts->rootDir = NULL;
ts->currDir = NULL;
ts->chrootDone = 0;
ts->numAddedPackages = 0;
ts->addedPackages = NULL;
ts->numAvailablePackages = 0;
ts->availablePackages = NULL;
ts->orderAlloced = 0;
ts->orderCount = 0;
ts->order = NULL;
ts->probs = NULL;
ts->sig = NULL;
ts->pkpkt = NULL;
ts->pkpktlen = 0;
memset(ts->pksignid, 0, sizeof(ts->pksignid));
ts->dig = NULL;
ts->nrefs = 0;
return rpmtsLink(ts, "tsCreate");
}
|