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
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
|
/** \ingroup rpmdep
* \file lib/depends.c
*/
/*@-exportheadervar@*/
/*@unchecked@*/ /*@unused@*/
static int _depends_debug = 0;
/*@=exportheadervar@*/
#include "system.h"
#include <rpmlib.h>
#include "depends.h"
#include "rpmdb.h" /* XXX response cache needs dbiOpen et al. */
#include "debug.h"
/*@access dbiIndex@*/ /* XXX compared with NULL */
/*@access dbiIndexSet@*/ /* XXX compared with NULL */
/*@access Header@*/ /* XXX compared with NULL */
/*@access FD_t@*/ /* XXX compared with NULL */
/*@access rpmdb@*/ /* XXX compared with NULL */
/*@access rpmdbMatchIterator@*/ /* XXX compared with NULL */
/*@access rpmTransactionSet@*/
/*@access rpmDependencyConflict@*/
/*@access availableList@*/
/*@unchecked@*/
static int _cacheDependsRC = 1;
int headerNVR(Header h, const char **np, const char **vp, const char **rp)
{
int type;
int count;
if (np) {
if (!(headerGetEntry(h, RPMTAG_NAME, &type, (void **) np, &count)
&& type == RPM_STRING_TYPE && count == 1))
*np = NULL;
}
if (vp) {
if (!(headerGetEntry(h, RPMTAG_VERSION, &type, (void **) vp, &count)
&& type == RPM_STRING_TYPE && count == 1))
*vp = NULL;
}
if (rp) {
if (!(headerGetEntry(h, RPMTAG_RELEASE, &type, (void **) rp, &count)
&& type == RPM_STRING_TYPE && count == 1))
*rp = NULL;
}
return 0;
}
/**
* Return formatted dependency string.
* @param depend type of dependency ("R" == Requires, "C" == Conflcts)
* @param key dependency name string
* @param keyEVR dependency [epoch:]version[-release] string
* @param keyFlags dependency logical range qualifiers
* @return formatted dependency (malloc'ed)
*/
static /*@only@*/ char * printDepend(const char * depend, const char * key,
const char * keyEVR, int keyFlags)
/*@*/
{
char * tbuf, * t;
size_t nb;
nb = 0;
if (depend) nb += strlen(depend) + 1;
if (key) nb += strlen(key);
if (keyFlags & RPMSENSE_SENSEMASK) {
if (nb) nb++;
if (keyFlags & RPMSENSE_LESS) nb++;
if (keyFlags & RPMSENSE_GREATER) nb++;
if (keyFlags & RPMSENSE_EQUAL) nb++;
}
if (keyEVR && *keyEVR) {
if (nb) nb++;
nb += strlen(keyEVR);
}
t = tbuf = xmalloc(nb + 1);
if (depend) {
while(*depend != '\0') *t++ = *depend++;
*t++ = ' ';
}
if (key)
while(*key != '\0') *t++ = *key++;
if (keyFlags & RPMSENSE_SENSEMASK) {
if (t != tbuf) *t++ = ' ';
if (keyFlags & RPMSENSE_LESS) *t++ = '<';
if (keyFlags & RPMSENSE_GREATER) *t++ = '>';
if (keyFlags & RPMSENSE_EQUAL) *t++ = '=';
}
if (keyEVR && *keyEVR) {
if (t != tbuf) *t++ = ' ';
while(*keyEVR != '\0') *t++ = *keyEVR++;
}
*t = '\0';
return tbuf;
}
#ifdef UNUSED
static /*@only@*/ const char *buildEVR(int_32 *e, const char *v, const char *r)
{
const char *pEVR;
char *p;
pEVR = p = xmalloc(21 + strlen(v) + 1 + strlen(r) + 1);
*p = '\0';
if (e) {
sprintf(p, "%d:", *e);
while (*p)
p++;
}
(void) stpcpy( stpcpy( stpcpy(p, v) , "-") , r);
return pEVR;
}
#endif
struct orderListIndex {
int alIndex;
int orIndex;
};
/**
* Destroy available item index.
* @param al available list
*/
static void alFreeIndex(availableList al)
/*@modifies al @*/
{
if (al->index.size) {
al->index.index = _free(al->index.index);
al->index.size = 0;
}
}
/**
* Initialize available packckages, items, and directories list.
* @param al available list
*/
static void alCreate(availableList al)
/*@modifies al @*/
{
al->alloced = al->delta;
al->size = 0;
al->list = xcalloc(al->alloced, sizeof(*al->list));
al->index.index = NULL;
al->index.size = 0;
al->numDirs = 0;
al->dirs = NULL;
}
/**
* Free available packages, items, and directories members.
* @param al available list
*/
static void alFree(availableList al)
/*@modifies al @*/
{
HFD_t hfd = headerFreeData;
struct availablePackage * p;
rpmRelocation * r;
int i;
if ((p = al->list) != NULL)
for (i = 0; i < al->size; i++, p++) {
{ struct tsortInfo * tsi;
while ((tsi = p->tsi.tsi_next) != NULL) {
p->tsi.tsi_next = tsi->tsi_next;
tsi->tsi_next = NULL;
tsi = _free(tsi);
}
}
p->provides = hfd(p->provides, -1);
p->providesEVR = hfd(p->providesEVR, -1);
p->requires = hfd(p->requires, -1);
p->requiresEVR = hfd(p->requiresEVR, -1);
p->baseNames = hfd(p->baseNames, -1);
p->h = headerFree(p->h);
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);
}
if (p->fd != NULL)
p->fd = fdFree(p->fd, "alAddPackage (alFree)");
}
if (al->dirs != NULL)
for (i = 0; i < al->numDirs; i++) {
al->dirs[i].dirName = _free(al->dirs[i].dirName);
al->dirs[i].files = _free(al->dirs[i].files);
}
al->dirs = _free(al->dirs);
al->numDirs = 0;
al->list = _free(al->list);
al->alloced = 0;
alFreeIndex(al);
}
/**
* Compare two directory info entries by name (qsort/bsearch).
* @param one 1st directory info
* @param two 2nd directory info
* @return result of comparison
*/
static int dirInfoCompare(const void * one, const void * two) /*@*/
{
const dirInfo a = (const dirInfo) one;
const dirInfo b = (const dirInfo) two;
int lenchk = a->dirNameLen - b->dirNameLen;
if (lenchk)
return lenchk;
/* XXX FIXME: this might do "backward" strcmp for speed */
return strcmp(a->dirName, b->dirName);
}
/**
* Add package to available list.
* @param al available list
* @param h package header
* @param key package private data
* @param fd package file handle
* @param relocs package file relocations
* @return available package pointer
*/
static /*@exposed@*/ struct availablePackage *
alAddPackage(availableList al,
Header h, /*@null@*/ /*@dependent@*/ const void * key,
/*@null@*/ FD_t fd, /*@null@*/ rpmRelocation * relocs)
/*@modifies al, h @*/
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HFD_t hfd = headerFreeData;
rpmTagType dnt, bnt;
struct availablePackage * p;
rpmRelocation * r;
int i;
int_32 * dirIndexes;
const char ** dirNames;
int numDirs, dirNum;
int * dirMapping;
struct dirInfo_s dirNeedle;
dirInfo dirMatch;
int first, last, fileNum;
int origNumDirs;
int pkgNum;
uint_32 multiLibMask = 0;
uint_32 * fileFlags = NULL;
uint_32 * pp = NULL;
if (al->size == al->alloced) {
al->alloced += al->delta;
al->list = xrealloc(al->list, sizeof(*al->list) * al->alloced);
}
pkgNum = al->size++;
p = al->list + pkgNum;
p->h = headerLink(h); /* XXX reference held by transaction set */
p->depth = p->npreds = 0;
memset(&p->tsi, 0, sizeof(p->tsi));
p->multiLib = 0; /* MULTILIB */
(void) headerNVR(p->h, &p->name, &p->version, &p->release);
/* XXX This should be added always so that packages look alike.
* XXX However, there is logic in files.c/depends.c that checks for
* XXX existence (rather than value) that will need to change as well.
*/
if (hge(p->h, RPMTAG_MULTILIBS, NULL, (void **) &pp, NULL))
multiLibMask = *pp;
if (multiLibMask) {
for (i = 0; i < pkgNum - 1; i++) {
if (!strcmp (p->name, al->list[i].name)
&& hge(al->list[i].h, RPMTAG_MULTILIBS, NULL,
(void **) &pp, NULL)
&& !rpmVersionCompare(p->h, al->list[i].h)
&& *pp && !(*pp & multiLibMask))
p->multiLib = multiLibMask;
}
}
if (!hge(h, RPMTAG_EPOCH, NULL, (void **) &p->epoch, NULL))
p->epoch = NULL;
if (!hge(h, RPMTAG_PROVIDENAME, NULL, (void **) &p->provides,
&p->providesCount)) {
p->providesCount = 0;
p->provides = NULL;
p->providesEVR = NULL;
p->provideFlags = NULL;
} else {
if (!hge(h, RPMTAG_PROVIDEVERSION,
NULL, (void **) &p->providesEVR, NULL))
p->providesEVR = NULL;
if (!hge(h, RPMTAG_PROVIDEFLAGS,
NULL, (void **) &p->provideFlags, NULL))
p->provideFlags = NULL;
}
if (!hge(h, RPMTAG_REQUIRENAME, NULL, (void **) &p->requires,
&p->requiresCount)) {
p->requiresCount = 0;
p->requires = NULL;
p->requiresEVR = NULL;
p->requireFlags = NULL;
} else {
if (!hge(h, RPMTAG_REQUIREVERSION,
NULL, (void **) &p->requiresEVR, NULL))
p->requiresEVR = NULL;
if (!hge(h, RPMTAG_REQUIREFLAGS,
NULL, (void **) &p->requireFlags, NULL))
p->requireFlags = NULL;
}
if (!hge(h, RPMTAG_BASENAMES, &bnt, (void **)&p->baseNames, &p->filesCount))
{
p->filesCount = 0;
p->baseNames = NULL;
} else {
(void) hge(h, RPMTAG_DIRNAMES, &dnt, (void **) &dirNames, &numDirs);
(void) hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes, NULL);
(void) hge(h, RPMTAG_FILEFLAGS, NULL, (void **) &fileFlags, NULL);
/* XXX FIXME: We ought to relocate the directory list here */
dirMapping = alloca(sizeof(*dirMapping) * numDirs);
/* allocated enough space for all the directories we could possible
need to add */
al->dirs = xrealloc(al->dirs,
sizeof(*al->dirs) * (al->numDirs + numDirs));
origNumDirs = al->numDirs;
for (dirNum = 0; dirNum < numDirs; dirNum++) {
dirNeedle.dirName = (char *) dirNames[dirNum];
dirNeedle.dirNameLen = strlen(dirNames[dirNum]);
dirMatch = bsearch(&dirNeedle, al->dirs, origNumDirs,
sizeof(dirNeedle), dirInfoCompare);
if (dirMatch) {
dirMapping[dirNum] = dirMatch - al->dirs;
} else {
dirMapping[dirNum] = al->numDirs;
al->dirs[al->numDirs].dirName = xstrdup(dirNames[dirNum]);
al->dirs[al->numDirs].dirNameLen = strlen(dirNames[dirNum]);
al->dirs[al->numDirs].files = NULL;
al->dirs[al->numDirs].numFiles = 0;
al->numDirs++;
}
}
dirNames = hfd(dirNames, dnt);
first = 0;
while (first < p->filesCount) {
last = first;
while ((last + 1) < p->filesCount) {
if (dirIndexes[first] != dirIndexes[last + 1])
/*@innerbreak@*/ break;
last++;
}
dirMatch = al->dirs + dirMapping[dirIndexes[first]];
dirMatch->files = xrealloc(dirMatch->files,
sizeof(*dirMatch->files) *
(dirMatch->numFiles + last - first + 1));
if (p->baseNames != NULL) /* XXX can't happen */
for (fileNum = first; fileNum <= last; fileNum++) {
dirMatch->files[dirMatch->numFiles].baseName =
p->baseNames[fileNum];
dirMatch->files[dirMatch->numFiles].pkgNum = pkgNum;
dirMatch->files[dirMatch->numFiles].fileFlags =
fileFlags[fileNum];
dirMatch->numFiles++;
}
first = last + 1;
}
if (origNumDirs + al->numDirs)
qsort(al->dirs, al->numDirs, sizeof(dirNeedle), dirInfoCompare);
}
p->key = key;
p->fd = (fd != NULL ? fdLink(fd, "alAddPackage") : NULL);
if (relocs) {
for (i = 0, r = relocs; r->oldPath || r->newPath; i++, r++)
{};
p->relocs = xmalloc((i + 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;
} else {
p->relocs = NULL;
}
alFreeIndex(al);
return p;
}
/**
* Compare two available index entries by name (qsort/bsearch).
* @param one 1st available index entry
* @param two 2nd available index entry
* @return result of comparison
*/
static int indexcmp(const void * one, const void * two) /*@*/
{
const struct availableIndexEntry * a = one;
const struct availableIndexEntry * b = two;
int lenchk = a->entryLen - b->entryLen;
if (lenchk)
return lenchk;
return strcmp(a->entry, b->entry);
}
/**
* Generate index for available list.
* @param al available list
*/
static void alMakeIndex(availableList al)
/*@modifies al @*/
{
struct availableIndex * ai = &al->index;
int i, j, k;
if (ai->size || al->list == NULL) return;
for (i = 0; i < al->size; i++)
ai->size += al->list[i].providesCount;
if (ai->size) {
ai->index = xcalloc(ai->size, sizeof(*ai->index));
k = 0;
for (i = 0; i < al->size; i++) {
for (j = 0; j < al->list[i].providesCount; j++) {
/* If multilib install, skip non-multilib provides. */
if (al->list[i].multiLib &&
!isDependsMULTILIB(al->list[i].provideFlags[j])) {
ai->size--;
/*@innercontinue@*/ continue;
}
ai->index[k].package = al->list + i;
ai->index[k].entry = al->list[i].provides[j];
ai->index[k].entryLen = strlen(al->list[i].provides[j]);
ai->index[k].type = IET_PROVIDES;
k++;
}
}
qsort(ai->index, ai->size, sizeof(*ai->index), indexcmp);
}
}
/**
* Split EVR into epoch, version, and release components.
* @param evr [epoch:]version[-release] string
* @retval *ep pointer to epoch
* @retval *vp pointer to version
* @retval *rp pointer to release
*/
static void parseEVR(char * evr,
/*@exposed@*/ /*@out@*/ const char ** ep,
/*@exposed@*/ /*@out@*/ const char ** vp,
/*@exposed@*/ /*@out@*/ const char ** rp)
/*@modifies *ep, *vp, *rp @*/
{
const char *epoch;
const char *version; /* assume only version is present */
const char *release;
char *s, *se;
s = evr;
while (*s && xisdigit(*s)) s++; /* s points to epoch terminator */
se = strrchr(s, '-'); /* se points to version terminator */
if (*s == ':') {
epoch = evr;
*s++ = '\0';
version = s;
if (*epoch == '\0') epoch = "0";
} else {
epoch = NULL; /* XXX disable epoch compare if missing */
version = evr;
}
if (se) {
*se++ = '\0';
release = se;
} else {
release = NULL;
}
if (ep) *ep = epoch;
if (vp) *vp = version;
if (rp) *rp = release;
}
/*@observer@*/ /*@unchecked@*/
const char *rpmNAME = PACKAGE;
/*@observer@*/ /*@unchecked@*/
const char *rpmEVR = VERSION;
/*@unchecked@*/
int rpmFLAGS = RPMSENSE_EQUAL;
int rpmRangesOverlap(const char * AName, const char * AEVR, int AFlags,
const char * BName, const char * BEVR, int BFlags)
{
const char *aDepend = printDepend(NULL, AName, AEVR, AFlags);
const char *bDepend = printDepend(NULL, BName, BEVR, BFlags);
char *aEVR, *bEVR;
const char *aE, *aV, *aR, *bE, *bV, *bR;
int result;
int sense;
/* Different names don't overlap. */
if (strcmp(AName, BName)) {
result = 0;
goto exit;
}
/* Same name. If either A or B is an existence test, always overlap. */
if (!((AFlags & RPMSENSE_SENSEMASK) && (BFlags & RPMSENSE_SENSEMASK))) {
result = 1;
goto exit;
}
/* If either EVR is non-existent or empty, always overlap. */
if (!(AEVR && *AEVR && BEVR && *BEVR)) {
result = 1;
goto exit;
}
/* Both AEVR and BEVR exist. */
aEVR = xstrdup(AEVR);
parseEVR(aEVR, &aE, &aV, &aR);
bEVR = xstrdup(BEVR);
parseEVR(bEVR, &bE, &bV, &bR);
/* Compare {A,B} [epoch:]version[-release] */
sense = 0;
if (aE && *aE && bE && *bE)
sense = rpmvercmp(aE, bE);
else if (aE && *aE && atol(aE) > 0) {
/* XXX legacy epoch-less requires/conflicts compatibility */
rpmMessage(RPMMESS_DEBUG, _("the \"B\" dependency needs an epoch (assuming same as \"A\")\n\tA %s\tB %s\n"),
aDepend, bDepend);
sense = 0;
} else if (bE && *bE && atol(bE) > 0)
sense = -1;
if (sense == 0) {
sense = rpmvercmp(aV, bV);
if (sense == 0 && aR && *aR && bR && *bR) {
sense = rpmvercmp(aR, bR);
}
}
aEVR = _free(aEVR);
bEVR = _free(bEVR);
/* Detect overlap of {A,B} range. */
result = 0;
if (sense < 0 && ((AFlags & RPMSENSE_GREATER) || (BFlags & RPMSENSE_LESS))) {
result = 1;
} else if (sense > 0 && ((AFlags & RPMSENSE_LESS) || (BFlags & RPMSENSE_GREATER))) {
result = 1;
} else if (sense == 0 &&
(((AFlags & RPMSENSE_EQUAL) && (BFlags & RPMSENSE_EQUAL)) ||
((AFlags & RPMSENSE_LESS) && (BFlags & RPMSENSE_LESS)) ||
((AFlags & RPMSENSE_GREATER) && (BFlags & RPMSENSE_GREATER)))) {
result = 1;
}
exit:
rpmMessage(RPMMESS_DEBUG, _(" %s A %s\tB %s\n"),
(result ? _("YES") : _("NO ")), aDepend, bDepend);
aDepend = _free(aDepend);
bDepend = _free(bDepend);
return result;
}
static int rangeMatchesDepFlags (Header h,
const char * reqName, const char * reqEVR, int reqFlags)
/*@*/
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HFD_t hfd = headerFreeData;
rpmTagType pnt, pvt;
const char ** provides;
const char ** providesEVR;
int_32 * provideFlags;
int providesCount;
int result;
int i;
if (!(reqFlags & RPMSENSE_SENSEMASK) || !reqEVR || !strlen(reqEVR))
return 1;
/* Get provides information from header */
/*
* Rpm prior to 3.0.3 does not have versioned provides.
* If no provides version info is available, match any requires.
*/
if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt,
(void **) &providesEVR, &providesCount))
return 1;
(void) hge(h, RPMTAG_PROVIDEFLAGS, NULL, (void **) &provideFlags, NULL);
if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (void **) &provides, &providesCount))
{
providesEVR = hfd(providesEVR, pvt);
return 0; /* XXX should never happen */
}
result = 0;
for (i = 0; i < providesCount; i++) {
/* Filter out provides that came along for the ride. */
if (strcmp(provides[i], reqName))
continue;
result = rpmRangesOverlap(provides[i], providesEVR[i], provideFlags[i],
reqName, reqEVR, reqFlags);
/* If this provide matches the require, we're done. */
if (result)
break;
}
provides = hfd(provides, pnt);
providesEVR = hfd(providesEVR, pvt);
return result;
}
int headerMatchesDepFlags(Header h,
const char * reqName, const char * reqEVR, int reqFlags)
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
const char *name, *version, *release;
int_32 * epoch;
const char *pkgEVR;
char *p;
int pkgFlags = RPMSENSE_EQUAL;
if (!((reqFlags & RPMSENSE_SENSEMASK) && reqEVR && *reqEVR))
return 1;
/* Get package information from header */
(void) headerNVR(h, &name, &version, &release);
pkgEVR = p = alloca(21 + strlen(version) + 1 + strlen(release) + 1);
*p = '\0';
if (hge(h, RPMTAG_EPOCH, NULL, (void **) &epoch, NULL)) {
sprintf(p, "%d:", *epoch);
while (*p != '\0')
p++;
}
(void) stpcpy( stpcpy( stpcpy(p, version) , "-") , release);
return rpmRangesOverlap(name, pkgEVR, pkgFlags, reqName, reqEVR, reqFlags);
}
rpmTransactionSet rpmtransCreateSet(rpmdb rpmdb, const char * rootDir)
{
rpmTransactionSet ts;
int rootLen;
if (!rootDir) rootDir = "";
ts = xcalloc(1, sizeof(*ts));
ts->filesystemCount = 0;
ts->filesystems = NULL;
ts->di = NULL;
/*@-assignexpose@*/
ts->rpmdb = rpmdb;
/*@=assignexpose@*/
ts->scriptFd = NULL;
ts->id = 0;
ts->delta = 5;
ts->numRemovedPackages = 0;
ts->allocedRemovedPackages = ts->delta;
ts->removedPackages = xcalloc(ts->allocedRemovedPackages,
sizeof(*ts->removedPackages));
/* This canonicalizes the root */
rootLen = strlen(rootDir);
if (!(rootLen && rootDir[rootLen - 1] == '/')) {
char * t;
t = alloca(rootLen + 2);
*t = '\0';
(void) stpcpy( stpcpy(t, rootDir), "/");
rootDir = t;
}
ts->rootDir = xstrdup(rootDir);
ts->currDir = NULL;
ts->chrootDone = 0;
ts->addedPackages.delta = ts->delta;
alCreate(&ts->addedPackages);
ts->availablePackages.delta = ts->delta;
alCreate(&ts->availablePackages);
ts->orderAlloced = ts->delta;
ts->orderCount = 0;
ts->order = xcalloc(ts->orderAlloced, sizeof(*ts->order));
return ts;
}
/**
* Compare removed package instances (qsort/bsearch).
* @param a 1st instance address
* @param b 2nd instance address
* @return result of comparison
*/
static int intcmp(const void * a, const void * b) /*@*/
{
const int * aptr = a;
const int * bptr = b;
int rc = (*aptr - *bptr);
return rc;
}
/**
* Add removed package instance to ordered transaction set.
* @param ts transaction set
* @param dboffset rpm database instance
* @param depends installed package of pair (or -1 on erase)
* @return 0 on success
*/
static int removePackage(rpmTransactionSet ts, int dboffset, int depends)
/*@modifies ts @*/
{
/* Filter out duplicate erasures. */
if (ts->numRemovedPackages > 0 && ts->removedPackages != NULL) {
if (bsearch(&dboffset, ts->removedPackages, ts->numRemovedPackages,
sizeof(int), intcmp) != NULL)
return 0;
}
if (ts->numRemovedPackages == ts->allocedRemovedPackages) {
ts->allocedRemovedPackages += ts->delta;
ts->removedPackages = xrealloc(ts->removedPackages,
sizeof(int *) * ts->allocedRemovedPackages);
}
if (ts->removedPackages != NULL) { /* XXX can't happen. */
ts->removedPackages[ts->numRemovedPackages++] = dboffset;
qsort(ts->removedPackages, ts->numRemovedPackages, sizeof(int), intcmp);
}
if (ts->orderCount == ts->orderAlloced) {
ts->orderAlloced += ts->delta;
ts->order = xrealloc(ts->order, sizeof(*ts->order) * ts->orderAlloced);
}
ts->order[ts->orderCount].type = TR_REMOVED;
ts->order[ts->orderCount].u.removed.dboffset = dboffset;
ts->order[ts->orderCount++].u.removed.dependsOnIndex = depends;
return 0;
}
int rpmtransAddPackage(rpmTransactionSet ts, Header h, FD_t fd,
const void * key, int upgrade, rpmRelocation * relocs)
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HFD_t hfd = headerFreeData;
rpmTagType ont, ovt;
/* this is an install followed by uninstalls */
const char * name;
int count;
const char ** obsoletes;
int alNum;
/*
* FIXME: handling upgrades like this is *almost* okay. It doesn't
* check to make sure we're upgrading to a newer version, and it
* makes it difficult to generate a return code based on the number of
* packages which failed.
*/
if (ts->orderCount == ts->orderAlloced) {
ts->orderAlloced += ts->delta;
ts->order = xrealloc(ts->order, sizeof(*ts->order) * ts->orderAlloced);
}
ts->order[ts->orderCount].type = TR_ADDED;
if (ts->addedPackages.list == NULL)
return 0;
alNum = alAddPackage(&ts->addedPackages, h, key, fd, relocs) -
ts->addedPackages.list;
ts->order[ts->orderCount++].u.addedIndex = alNum;
if (!upgrade || ts->rpmdb == NULL)
return 0;
/* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
if (headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
return 0;
(void) headerNVR(h, &name, NULL, NULL);
{ rpmdbMatchIterator mi;
Header h2;
mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, name, 0);
while((h2 = rpmdbNextIterator(mi)) != NULL) {
if (rpmVersionCompare(h, h2))
(void) removePackage(ts, rpmdbGetIteratorOffset(mi), alNum);
else {
uint_32 *p, multiLibMask = 0, oldmultiLibMask = 0;
if (hge(h2, RPMTAG_MULTILIBS, NULL, (void **) &p, NULL))
oldmultiLibMask = *p;
if (hge(h, RPMTAG_MULTILIBS, NULL, (void **) &p, NULL))
multiLibMask = *p;
if (oldmultiLibMask && multiLibMask
&& !(oldmultiLibMask & multiLibMask)) {
ts->addedPackages.list[alNum].multiLib = multiLibMask;
}
}
}
mi = rpmdbFreeIterator(mi);
}
if (hge(h, RPMTAG_OBSOLETENAME, &ont, (void **) &obsoletes, &count)) {
const char ** obsoletesEVR;
int_32 * obsoletesFlags;
int j;
(void) hge(h, RPMTAG_OBSOLETEVERSION, &ovt, (void **) &obsoletesEVR,
NULL);
(void) hge(h, RPMTAG_OBSOLETEFLAGS, NULL, (void **) &obsoletesFlags,
NULL);
for (j = 0; j < count; j++) {
/* XXX avoid self-obsoleting packages. */
if (!strcmp(name, obsoletes[j]))
continue;
{ rpmdbMatchIterator mi;
Header h2;
mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, obsoletes[j], 0);
(void) rpmdbPruneIterator(mi,
ts->removedPackages, ts->numRemovedPackages, 1);
while((h2 = rpmdbNextIterator(mi)) != NULL) {
/*
* Rpm prior to 3.0.3 does not have versioned obsoletes.
* If no obsoletes version info is available, match all names.
*/
if (obsoletesEVR == NULL ||
headerMatchesDepFlags(h2,
obsoletes[j], obsoletesEVR[j], obsoletesFlags[j]))
{
(void) removePackage(ts, rpmdbGetIteratorOffset(mi), alNum);
}
}
mi = rpmdbFreeIterator(mi);
}
}
obsoletesEVR = hfd(obsoletesEVR, ovt);
obsoletes = hfd(obsoletes, ont);
}
return 0;
}
void rpmtransAvailablePackage(rpmTransactionSet ts, Header h, const void * key)
{
struct availablePackage * al;
al = alAddPackage(&ts->availablePackages, h, key, NULL, NULL);
}
int rpmtransRemovePackage(rpmTransactionSet ts, int dboffset)
{
return removePackage(ts, dboffset, -1);
}
rpmTransactionSet rpmtransFree(rpmTransactionSet ts)
{
if (ts) {
alFree(&ts->addedPackages);
alFree(&ts->availablePackages);
ts->di = _free(ts->di);
ts->removedPackages = _free(ts->removedPackages);
ts->order = _free(ts->order);
if (ts->scriptFd != NULL)
ts->scriptFd =
fdFree(ts->scriptFd, "rpmtransSetScriptFd (rpmtransFree");
ts->rootDir = _free(ts->rootDir);
ts->currDir = _free(ts->currDir);
ts = _free(ts);
}
return NULL;
}
rpmDependencyConflict rpmdepFreeConflicts(rpmDependencyConflict conflicts,
int numConflicts)
{
int i;
if (conflicts)
for (i = 0; i < numConflicts; i++) {
conflicts[i].byHeader = headerFree(conflicts[i].byHeader);
conflicts[i].byName = _free(conflicts[i].byName);
conflicts[i].byVersion = _free(conflicts[i].byVersion);
conflicts[i].byRelease = _free(conflicts[i].byRelease);
conflicts[i].needsName = _free(conflicts[i].needsName);
conflicts[i].needsVersion = _free(conflicts[i].needsVersion);
conflicts[i].suggestedPackages = _free(conflicts[i].suggestedPackages);
}
return (conflicts = _free(conflicts));
}
/**
* Check added package file lists for package(s) that provide a file.
* @param al available list
* @param keyType type of dependency
* @param fileName file name to search for
* @return available package pointer
*/
static /*@only@*/ /*@null@*/ struct availablePackage **
alAllFileSatisfiesDepend(const availableList al,
const char * keyType, const char * fileName)
/*@*/
{
int i, found = 0;
const char * dirName;
const char * baseName;
struct dirInfo_s dirNeedle;
dirInfo dirMatch;
struct availablePackage ** ret = NULL;
/* Solaris 2.6 bsearch sucks down on this. */
if (al->numDirs == 0 || al->dirs == NULL || al->list == NULL)
return NULL;
{ char * t;
dirName = t = xstrdup(fileName);
if ((t = strrchr(t, '/')) != NULL) {
t++; /* leave the trailing '/' */
*t = '\0';
}
}
dirNeedle.dirName = (char *) dirName;
dirNeedle.dirNameLen = strlen(dirName);
dirMatch = bsearch(&dirNeedle, al->dirs, al->numDirs,
sizeof(dirNeedle), dirInfoCompare);
if (dirMatch == NULL)
goto exit;
/* rewind to the first match */
while (dirMatch > al->dirs && dirInfoCompare(dirMatch-1, &dirNeedle) == 0)
dirMatch--;
if ((baseName = strrchr(fileName, '/')) == NULL)
goto exit;
baseName++;
for (found = 0, ret = NULL;
dirMatch <= al->dirs + al->numDirs &&
dirInfoCompare(dirMatch, &dirNeedle) == 0;
dirMatch++)
{
/* XXX FIXME: these file lists should be sorted and bsearched */
for (i = 0; i < dirMatch->numFiles; i++) {
if (dirMatch->files[i].baseName == NULL ||
strcmp(dirMatch->files[i].baseName, baseName))
/*@innercontinue@*/ continue;
/*
* If a file dependency would be satisfied by a file
* we are not going to install, skip it.
*/
if (al->list[dirMatch->files[i].pkgNum].multiLib &&
!isFileMULTILIB(dirMatch->files[i].fileFlags))
/*@innercontinue@*/ continue;
if (keyType)
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s YES (added files)\n"),
keyType, fileName);
ret = xrealloc(ret, (found+2) * sizeof(*ret));
if (ret) /* can't happen */
ret[found++] = al->list + dirMatch->files[i].pkgNum;
/*@innerbreak@*/ break;
}
}
exit:
dirName = _free(dirName);
/*@-mods@*/ /* FIX: al->list might be modified. */
if (ret)
ret[found] = NULL;
/*@=mods@*/
return ret;
}
#ifdef DYING
/**
* Check added package file lists for first package that provides a file.
* @param al available list
* @param keyType type of dependency
* @param fileName file name to search for
* @return available package pointer
*/
/*@unused@*/ static /*@dependent@*/ /*@null@*/ struct availablePackage *
alFileSatisfiesDepend(const availableList al,
const char * keyType, const char * fileName)
/*@*/
{
struct availablePackage * ret;
struct availablePackage ** tmp =
alAllFileSatisfiesDepend(al, keyType, fileName);
if (tmp) {
ret = tmp[0];
tmp = _free(tmp);
return ret;
}
return NULL;
}
#endif /* DYING */
/**
* Check added package file lists for package(s) that have a provide.
* @param al available list
* @param keyType type of dependency
* @param keyDepend dependency string representation
* @param keyName dependency name string
* @param keyEVR dependency [epoch:]version[-release] string
* @param keyFlags dependency logical range qualifiers
* @return available package pointer
*/
static /*@only@*/ /*@null@*/ struct availablePackage **
alAllSatisfiesDepend(const availableList al,
const char * keyType, const char * keyDepend,
const char * keyName, const char * keyEVR, int keyFlags)
/*@*/
{
struct availableIndexEntry needle, * match;
struct availablePackage * p, ** ret = NULL;
int i, rc, found;
if (*keyName == '/') {
ret = alAllFileSatisfiesDepend(al, keyType, keyName);
/* XXX Provides: /path was broken with added packages (#52183). */
if (ret != NULL && *ret != NULL)
return ret;
}
if (!al->index.size || al->index.index == NULL) return NULL;
needle.entry = keyName;
needle.entryLen = strlen(keyName);
match = bsearch(&needle, al->index.index, al->index.size,
sizeof(*al->index.index), indexcmp);
if (match == NULL) return NULL;
/* rewind to the first match */
while (match > al->index.index && indexcmp(match-1,&needle) == 0)
match--;
for (ret = NULL, found = 0;
match <= al->index.index + al->index.size &&
indexcmp(match,&needle) == 0;
match++)
{
p = match->package;
rc = 0;
switch (match->type) {
case IET_PROVIDES:
for (i = 0; i < p->providesCount; i++) {
const char * proEVR;
int proFlags;
/* Filter out provides that came along for the ride. */
if (strcmp(p->provides[i], keyName))
/*@innercontinue@*/ continue;
proEVR = (p->providesEVR ? p->providesEVR[i] : NULL);
proFlags = (p->provideFlags ? p->provideFlags[i] : 0);
rc = rpmRangesOverlap(p->provides[i], proEVR, proFlags,
keyName, keyEVR, keyFlags);
if (rc)
/*@innerbreak@*/ break;
}
if (keyType && keyDepend && rc)
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s YES (added provide)\n"),
keyType, keyDepend+2);
/*@switchbreak@*/ break;
}
if (rc) {
ret = xrealloc(ret, (found + 2) * sizeof(*ret));
if (ret) /* can't happen */
ret[found++] = p;
}
}
if (ret)
ret[found] = NULL;
return ret;
}
/**
* Check added package file lists for first package that has a provide.
* @todo Eliminate.
* @param al available list
* @param keyType type of dependency
* @param keyDepend dependency string representation
* @param keyName dependency name string
* @param keyEVR dependency [epoch:]version[-release] string
* @param keyFlags dependency logical range qualifiers
* @return available package pointer
*/
static inline /*@only@*/ /*@null@*/ struct availablePackage *
alSatisfiesDepend(const availableList al,
const char * keyType, const char * keyDepend,
const char * keyName, const char * keyEVR, int keyFlags)
/*@*/
{
struct availablePackage * ret;
struct availablePackage ** tmp =
alAllSatisfiesDepend(al, keyType, keyDepend, keyName, keyEVR, keyFlags);
if (tmp) {
ret = tmp[0];
tmp = _free(tmp);
return ret;
}
return NULL;
}
/**
* Check key for an unsatisfied dependency.
* @todo Eliminate rpmrc provides.
* @param al available list
* @param keyType type of dependency
* @param keyDepend dependency string representation
* @param keyName dependency name string
* @param keyEVR dependency [epoch:]version[-release] string
* @param keyFlags dependency logical range qualifiers
* @retval suggestion possible package(s) to resolve dependency
* @return 0 if satisfied, 1 if not satisfied, 2 if error
*/
static int unsatisfiedDepend(rpmTransactionSet ts,
const char * keyType, const char * keyDepend,
const char * keyName, const char * keyEVR, int keyFlags,
/*@null@*/ /*@out@*/ struct availablePackage *** suggestion)
/*@globals fileSystem @*/
/*@modifies ts, *suggestion, fileSystem @*/
{
rpmdbMatchIterator mi;
Header h;
int rc = 0; /* assume dependency is satisfied */
if (suggestion) *suggestion = NULL;
/*
* Check if dbiOpen/dbiPut failed (e.g. permissions), we can't cache.
*/
if (_cacheDependsRC) {
dbiIndex dbi;
dbi = dbiOpen(ts->rpmdb, RPMDBI_DEPENDS, 0);
if (dbi == NULL)
_cacheDependsRC = 0;
else {
DBC * dbcursor = NULL;
size_t keylen = strlen(keyDepend);
void * datap = NULL;
size_t datalen = 0;
int xx;
xx = dbiCopen(dbi, &dbcursor, 0);
/*@-mods@*/ /* FIX: keyDepends mod undocumented. */
xx = dbiGet(dbi, dbcursor, (void **)&keyDepend, &keylen, &datap, &datalen, 0);
/*@=mods@*/
if (xx == 0 && datap && datalen == 4) {
memcpy(&rc, datap, datalen);
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s %-s (cached)\n"),
keyType, keyDepend, (rc ? _("NO ") : _("YES")));
xx = dbiCclose(dbi, NULL, 0);
if (suggestion && rc == 1)
*suggestion = alAllSatisfiesDepend(&ts->availablePackages,
NULL, NULL, keyName, keyEVR, keyFlags);
return rc;
}
xx = dbiCclose(dbi, dbcursor, 0);
}
}
#ifdef DYING
{ static /*@observer@*/ const char noProvidesString[] = "nada";
static /*@observer@*/ const char * rcProvidesString = noProvidesString;
const char * start;
int i;
if (rcProvidesString == noProvidesString)
rcProvidesString = rpmGetVar(RPMVAR_PROVIDES);
if (rcProvidesString != NULL && !(keyFlags & RPMSENSE_SENSEMASK)) {
i = strlen(keyName);
/*@-observertrans -mayaliasunique@*/
while ((start = strstr(rcProvidesString, keyName))) {
/*@=observertrans =mayaliasunique@*/
if (xisspace(start[i]) || start[i] == '\0' || start[i] == ',') {
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s YES (rpmrc provides)\n"),
keyType, keyDepend+2);
goto exit;
}
rcProvidesString = start + 1;
}
}
}
#endif
/*
* New features in rpm packaging implicitly add versioned dependencies
* on rpmlib provides. The dependencies look like "rpmlib(YaddaYadda)".
* Check those dependencies now.
*/
if (!strncmp(keyName, "rpmlib(", sizeof("rpmlib(")-1)) {
if (rpmCheckRpmlibProvides(keyName, keyEVR, keyFlags)) {
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s YES (rpmlib provides)\n"),
keyType, keyDepend+2);
goto exit;
}
goto unsatisfied;
}
if (alSatisfiesDepend(&ts->addedPackages, keyType, keyDepend,
keyName, keyEVR, keyFlags))
{
goto exit;
}
/* XXX only the installer does not have the database open here. */
if (ts->rpmdb != NULL) {
if (*keyName == '/') {
/* keyFlags better be 0! */
mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_BASENAMES, keyName, 0);
(void) rpmdbPruneIterator(mi,
ts->removedPackages, ts->numRemovedPackages, 1);
while ((h = rpmdbNextIterator(mi)) != NULL) {
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s YES (db files)\n"),
keyType, keyDepend+2);
mi = rpmdbFreeIterator(mi);
goto exit;
}
mi = rpmdbFreeIterator(mi);
}
mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_PROVIDENAME, keyName, 0);
(void) rpmdbPruneIterator(mi,
ts->removedPackages, ts->numRemovedPackages, 1);
while ((h = rpmdbNextIterator(mi)) != NULL) {
if (rangeMatchesDepFlags(h, keyName, keyEVR, keyFlags)) {
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s YES (db provides)\n"),
keyType, keyDepend+2);
mi = rpmdbFreeIterator(mi);
goto exit;
}
}
mi = rpmdbFreeIterator(mi);
#ifdef DYING
mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_NAME, keyName, 0);
(void) rpmdbPruneIterator(mi,
ts->removedPackages, ts->numRemovedPackages, 1);
while ((h = rpmdbNextIterator(mi)) != NULL) {
if (rangeMatchesDepFlags(h, keyName, keyEVR, keyFlags)) {
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s YES (db package)\n"),
keyType, keyDepend+2);
mi = rpmdbFreeIterator(mi);
goto exit;
}
}
mi = rpmdbFreeIterator(mi);
#endif
}
if (suggestion)
*suggestion = alAllSatisfiesDepend(&ts->availablePackages, NULL, NULL,
keyName, keyEVR, keyFlags);
unsatisfied:
rpmMessage(RPMMESS_DEBUG, _("%s: %-45s NO\n"), keyType, keyDepend+2);
rc = 1; /* dependency is unsatisfied */
exit:
/*
* If dbiOpen/dbiPut fails (e.g. permissions), we can't cache.
*/
if (_cacheDependsRC) {
dbiIndex dbi;
dbi = dbiOpen(ts->rpmdb, RPMDBI_DEPENDS, 0);
if (dbi == NULL) {
_cacheDependsRC = 0;
} else {
DBC * dbcursor = NULL;
int xx;
xx = dbiCopen(dbi, &dbcursor, DBI_WRITECURSOR);
xx = dbiPut(dbi, dbcursor, keyDepend, strlen(keyDepend), &rc, sizeof(rc), 0);
if (xx)
_cacheDependsRC = 0;
#if 0 /* XXX NOISY */
else
rpmMessage(RPMMESS_DEBUG, _("%s: (%s, %s) added to Depends cache.\n"), keyType, keyDepend, (rc ? _("NO ") : _("YES")));
#endif
xx = dbiCclose(dbi, dbcursor, DBI_WRITECURSOR);
}
}
return rc;
}
/**
* Check header requires/conflicts against against installed+added packages.
* @param ts transaction set
* @param psp dependency problems
* @param h header to check
* @param keyName dependency name
* @param multiLib skip multilib colored dependencies?
* @return 0 no problems found
*/
static int checkPackageDeps(rpmTransactionSet ts, problemsSet psp,
Header h, const char * keyName, uint_32 multiLib)
/*@globals fileSystem @*/
/*@modifies ts, h, psp, fileSystem */
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HFD_t hfd = headerFreeData;
rpmTagType rnt, rvt;
rpmTagType cnt, cvt;
const char * name, * version, * release;
const char ** requires;
const char ** requiresEVR = NULL;
int_32 * requireFlags = NULL;
int requiresCount = 0;
const char ** conflicts;
const char ** conflictsEVR = NULL;
int_32 * conflictFlags = NULL;
int conflictsCount = 0;
rpmTagType type;
int i, rc;
int ourrc = 0;
struct availablePackage ** suggestion;
(void) headerNVR(h, &name, &version, &release);
if (!hge(h, RPMTAG_REQUIRENAME, &rnt, (void **) &requires, &requiresCount))
{
requiresCount = 0;
rvt = RPM_STRING_ARRAY_TYPE;
} else {
(void)hge(h, RPMTAG_REQUIREFLAGS, NULL, (void **) &requireFlags, NULL);
(void)hge(h, RPMTAG_REQUIREVERSION, &rvt, (void **) &requiresEVR, NULL);
}
for (i = 0; i < requiresCount && !ourrc; i++) {
const char * keyDepend;
/* Filter out requires that came along for the ride. */
if (keyName && strcmp(keyName, requires[i]))
continue;
/* If this requirement comes from the core package only, not libraries,
then if we're installing the libraries only, don't count it in. */
if (multiLib && !isDependsMULTILIB(requireFlags[i]))
continue;
keyDepend = printDepend("R",
requires[i], requiresEVR[i], requireFlags[i]);
rc = unsatisfiedDepend(ts, " Requires", keyDepend,
requires[i], requiresEVR[i], requireFlags[i], &suggestion);
switch (rc) {
case 0: /* requirements are satisfied. */
/*@switchbreak@*/ break;
case 1: /* requirements are not satisfied. */
rpmMessage(RPMMESS_DEBUG, _("package %s-%s-%s require not satisfied: %s\n"),
name, version, release, keyDepend+2);
if (psp->num == psp->alloced) {
psp->alloced += 5;
psp->problems = xrealloc(psp->problems, sizeof(*psp->problems) *
psp->alloced);
}
{ rpmDependencyConflict pp = psp->problems + psp->num;
pp->byHeader = headerLink(h);
pp->byName = xstrdup(name);
pp->byVersion = xstrdup(version);
pp->byRelease = xstrdup(release);
pp->needsName = xstrdup(requires[i]);
pp->needsVersion = xstrdup(requiresEVR[i]);
pp->needsFlags = requireFlags[i];
pp->sense = RPMDEP_SENSE_REQUIRES;
if (suggestion) {
int j;
for (j = 0; suggestion[j]; j++)
{};
pp->suggestedPackages =
xmalloc( (j + 1) * sizeof(*pp->suggestedPackages) );
for (j = 0; suggestion[j]; j++)
pp->suggestedPackages[j] = suggestion[j]->key;
pp->suggestedPackages[j] = NULL;
} else {
pp->suggestedPackages = NULL;
}
}
psp->num++;
/*@switchbreak@*/ break;
case 2: /* something went wrong! */
default:
ourrc = 1;
/*@switchbreak@*/ break;
}
keyDepend = _free(keyDepend);
}
if (requiresCount) {
requiresEVR = hfd(requiresEVR, rvt);
requires = hfd(requires, rnt);
}
if (!hge(h, RPMTAG_CONFLICTNAME, &cnt, (void **)&conflicts, &conflictsCount))
{
conflictsCount = 0;
cvt = RPM_STRING_ARRAY_TYPE;
} else {
(void) hge(h, RPMTAG_CONFLICTFLAGS, &type,
(void **) &conflictFlags, &conflictsCount);
(void) hge(h, RPMTAG_CONFLICTVERSION, &cvt,
(void **) &conflictsEVR, &conflictsCount);
}
for (i = 0; i < conflictsCount && !ourrc; i++) {
const char * keyDepend;
/* Filter out conflicts that came along for the ride. */
if (keyName && strcmp(keyName, conflicts[i]))
continue;
/* If this requirement comes from the core package only, not libraries,
then if we're installing the libraries only, don't count it in. */
if (multiLib && !isDependsMULTILIB(conflictFlags[i]))
continue;
keyDepend = printDepend("C", conflicts[i], conflictsEVR[i], conflictFlags[i]);
rc = unsatisfiedDepend(ts, "Conflicts", keyDepend,
conflicts[i], conflictsEVR[i], conflictFlags[i], NULL);
/* 1 == unsatisfied, 0 == satsisfied */
switch (rc) {
case 0: /* conflicts exist. */
rpmMessage(RPMMESS_DEBUG, _("package %s conflicts: %s\n"),
name, keyDepend+2);
if (psp->num == psp->alloced) {
psp->alloced += 5;
psp->problems = xrealloc(psp->problems,
sizeof(*psp->problems) * psp->alloced);
}
{ rpmDependencyConflict pp = psp->problems + psp->num;
pp->byHeader = headerLink(h);
pp->byName = xstrdup(name);
pp->byVersion = xstrdup(version);
pp->byRelease = xstrdup(release);
pp->needsName = xstrdup(conflicts[i]);
pp->needsVersion = xstrdup(conflictsEVR[i]);
pp->needsFlags = conflictFlags[i];
pp->sense = RPMDEP_SENSE_CONFLICTS;
pp->suggestedPackages = NULL;
}
psp->num++;
/*@switchbreak@*/ break;
case 1: /* conflicts don't exist. */
/*@switchbreak@*/ break;
case 2: /* something went wrong! */
default:
ourrc = 1;
/*@switchbreak@*/ break;
}
keyDepend = _free(keyDepend);
}
if (conflictsCount) {
conflictsEVR = hfd(conflictsEVR, cvt);
conflicts = hfd(conflicts, cnt);
}
return ourrc;
}
/**
* Check dependency against installed packages.
* Adding: check name/provides key against each conflict match,
* Erasing: check name/provides/filename key against each requiredby match.
* @param ts transaction set
* @param psp dependency problems
* @param key dependency name
* @param mi rpm database iterator
* @return 0 no problems found
*/
static int checkPackageSet(rpmTransactionSet ts, problemsSet psp,
const char * key, /*@only@*/ /*@null@*/ rpmdbMatchIterator mi)
/*@globals fileSystem @*/
/*@modifies ts, mi, psp, fileSystem @*/
{
Header h;
int rc = 0;
(void) rpmdbPruneIterator(mi,
ts->removedPackages, ts->numRemovedPackages, 1);
while ((h = rpmdbNextIterator(mi)) != NULL) {
if (checkPackageDeps(ts, psp, h, key, 0)) {
rc = 1;
break;
}
}
mi = rpmdbFreeIterator(mi);
return rc;
}
/**
* Erasing: check name/provides/filename key against requiredby matches.
* @param ts transaction set
* @param psp dependency problems
* @param key requires name
* @return 0 no problems found
*/
static int checkDependentPackages(rpmTransactionSet ts,
problemsSet psp, const char * key)
/*@globals fileSystem @*/
/*@modifies ts, psp, fileSystem @*/
{
rpmdbMatchIterator mi;
mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_REQUIRENAME, key, 0);
return checkPackageSet(ts, psp, key, mi);
}
/**
* Adding: check name/provides key against conflicts matches.
* @param ts transaction set
* @param psp dependency problems
* @param key conflicts name
* @return 0 no problems found
*/
static int checkDependentConflicts(rpmTransactionSet ts,
problemsSet psp, const char * key)
/*@globals fileSystem @*/
/*@modifies ts, psp, fileSystem @*/
{
int rc = 0;
if (ts->rpmdb) { /* XXX is this necessary? */
rpmdbMatchIterator mi;
mi = rpmdbInitIterator(ts->rpmdb, RPMTAG_CONFLICTNAME, key, 0);
rc = checkPackageSet(ts, psp, key, mi);
}
return rc;
}
/*
* XXX Hack to remove known Red Hat dependency loops, will be removed
* as soon as rpm's legacy permits.
*/
#define DEPENDENCY_WHITEOUT
#if defined(DEPENDENCY_WHITEOUT)
/*@observer@*/ /*@unchecked@*/
static struct badDeps_s {
/*@observer@*/ /*@null@*/ const char * pname;
/*@observer@*/ /*@null@*/ const char * qname;
} badDeps[] = {
{ "libtermcap", "bash" },
{ "modutils", "vixie-cron" },
{ "ypbind", "yp-tools" },
{ "ghostscript-fonts", "ghostscript" },
/* 7.2 only */
{ "libgnomeprint15", "gnome-print" },
{ "nautilus", "nautilus-mozilla" },
/* 7.1 only */
{ "arts", "kdelibs-sound" },
/* 7.0 only */
{ "pango-gtkbeta-devel", "pango-gtkbeta" },
{ "XFree86", "Mesa" },
{ "compat-glibc", "db2" },
{ "compat-glibc", "db1" },
{ "pam", "initscripts" },
{ "initscripts", "sysklogd" },
/* 6.2 */
{ "egcs-c++", "libstdc++" },
/* 6.1 */
{ "pilot-link-devel", "pilot-link" },
/* 5.2 */
{ "pam", "pamconfig" },
{ NULL, NULL }
};
static int ignoreDep(const struct availablePackage * p,
const struct availablePackage * q)
/*@*/
{
struct badDeps_s * bdp = badDeps;
while (bdp->pname != NULL && bdp->qname != NULL) {
if (!strcmp(p->name, bdp->pname) && !strcmp(q->name, bdp->qname))
return 1;
bdp++;
}
return 0;
}
#endif
/**
* Recursively mark all nodes with their predecessors.
* @param tsi successor chain
* @param q predecessor
*/
static void markLoop(/*@special@*/ struct tsortInfo * tsi,
struct availablePackage * q)
/*@uses tsi @*/
/*@modifies internalState @*/
{
struct availablePackage * p;
while (tsi != NULL && (p = tsi->tsi_suc) != NULL) {
tsi = tsi->tsi_next;
if (p->tsi.tsi_pkg != NULL)
continue;
p->tsi.tsi_pkg = q;
if (p->tsi.tsi_next != NULL)
markLoop(p->tsi.tsi_next, p);
}
}
static inline /*@observer@*/ const char * const identifyDepend(int_32 f)
/*@*/
{
if (isLegacyPreReq(f))
return "PreReq:";
f = _notpre(f);
if (f & RPMSENSE_SCRIPT_PRE)
return "Requires(pre):";
if (f & RPMSENSE_SCRIPT_POST)
return "Requires(post):";
if (f & RPMSENSE_SCRIPT_PREUN)
return "Requires(preun):";
if (f & RPMSENSE_SCRIPT_POSTUN)
return "Requires(postun):";
if (f & RPMSENSE_SCRIPT_VERIFY)
return "Requires(verify):";
if (f & RPMSENSE_FIND_REQUIRES)
return "Requires(auto):";
return "Requires:";
}
/**
* Find (and eliminate co-requisites) "q <- p" relation in dependency loop.
* Search all successors of q for instance of p. Format the specific relation,
* (e.g. p contains "Requires: q"). Unlink and free co-requisite (i.e.
* pure Requires: dependencies) successor node(s).
* @param q sucessor (i.e. package required by p)
* @param p predecessor (i.e. package that "Requires: q")
* @param zap max. no. of co-requisites to remove (-1 is all)?
* @retval nzaps address of no. of relations removed
* @return (possibly NULL) formatted "q <- p" releation (malloc'ed)
*/
static /*@owned@*/ /*@null@*/ const char *
zapRelation(struct availablePackage * q, struct availablePackage * p,
int zap, /*@in@*/ /*@out@*/ int * nzaps)
/*@modifies q, p, *nzaps @*/
{
struct tsortInfo * tsi_prev;
struct tsortInfo * tsi;
const char *dp = NULL;
for (tsi_prev = &q->tsi, tsi = q->tsi.tsi_next;
tsi != NULL;
/* XXX Note: the loop traverses "not found", break on "found". */
/*@-nullderef@*/
tsi_prev = tsi, tsi = tsi->tsi_next)
/*@=nullderef@*/
{
int j;
if (tsi->tsi_suc != p)
continue;
if (p->requires == NULL) continue; /* XXX can't happen */
if (p->requireFlags == NULL) continue; /* XXX can't happen */
if (p->requiresEVR == NULL) continue; /* XXX can't happen */
j = tsi->tsi_reqx;
dp = printDepend( identifyDepend(p->requireFlags[j]),
p->requires[j], p->requiresEVR[j], p->requireFlags[j]);
/*
* Attempt to unravel a dependency loop by eliminating Requires's.
*/
if (zap && !(p->requireFlags[j] & RPMSENSE_PREREQ)) {
rpmMessage(RPMMESS_DEBUG,
_("removing %s-%s-%s \"%s\" from tsort relations.\n"),
p->name, p->version, p->release, dp);
p->tsi.tsi_count--;
if (tsi_prev) tsi_prev->tsi_next = tsi->tsi_next;
tsi->tsi_next = NULL;
tsi->tsi_suc = NULL;
tsi = _free(tsi);
if (nzaps)
(*nzaps)++;
if (zap)
zap--;
}
/* XXX Note: the loop traverses "not found", get out now! */
break;
}
return dp;
}
/**
* Record next "q <- p" relation (i.e. "p" requires "q").
* @param ts transaction set
* @param p predecessor (i.e. package that "Requires: q")
* @param selected boolean package selected array
* @param j relation index
* @return 0 always
*/
static inline int addRelation( const rpmTransactionSet ts,
struct availablePackage * p, unsigned char * selected, int j)
/*@modifies p->tsi.tsi_u.count, p->depth, *selected @*/
{
struct availablePackage * q;
struct tsortInfo * tsi;
int matchNum;
if (!p->requires || !p->requiresEVR || !p->requireFlags)
return 0;
q = alSatisfiesDepend(&ts->addedPackages, NULL, NULL,
p->requires[j], p->requiresEVR[j], p->requireFlags[j]);
/* Ordering depends only on added package relations. */
if (q == NULL)
return 0;
/* Avoid rpmlib feature dependencies. */
if (!strncmp(p->requires[j], "rpmlib(", sizeof("rpmlib(")-1))
return 0;
#if defined(DEPENDENCY_WHITEOUT)
/* Avoid certain dependency relations. */
if (ignoreDep(p, q))
return 0;
#endif
/* Avoid redundant relations. */
/* XXX TODO: add control bit. */
matchNum = q - ts->addedPackages.list;
if (selected[matchNum] != 0)
return 0;
selected[matchNum] = 1;
/* T3. Record next "q <- p" relation (i.e. "p" requires "q"). */
p->tsi.tsi_count++; /* bump p predecessor count */
if (p->depth <= q->depth) /* Save max. depth in dependency tree */
p->depth = q->depth + 1;
tsi = xmalloc(sizeof(*tsi));
tsi->tsi_suc = p;
tsi->tsi_reqx = j;
tsi->tsi_next = q->tsi.tsi_next;
q->tsi.tsi_next = tsi;
q->tsi.tsi_qcnt++; /* bump q successor count */
return 0;
}
/**
* Compare ordered list entries by index (qsort/bsearch).
* @param a 1st ordered list entry
* @param b 2nd ordered list entry
* @return result of comparison
*/
static int orderListIndexCmp(const void * one, const void * two) /*@*/
{
int a = ((const struct orderListIndex *)one)->alIndex;
int b = ((const struct orderListIndex *)two)->alIndex;
return (a - b);
}
/**
* Add element to list sorting by initial successor count.
* @param p new element
* @retval qp address of first element
* @retval rp address of last element
*/
static void addQ(struct availablePackage * p,
/*@in@*/ /*@out@*/ struct availablePackage ** qp,
/*@in@*/ /*@out@*/ struct availablePackage ** rp)
/*@modifies p->tsi, *qp, *rp @*/
{
struct availablePackage *q, *qprev;
if ((*rp) == NULL) { /* 1st element */
(*rp) = (*qp) = p;
return;
}
for (qprev = NULL, q = (*qp); q != NULL; qprev = q, q = q->tsi.tsi_suc) {
if (q->tsi.tsi_qcnt <= p->tsi.tsi_qcnt)
break;
}
if (qprev == NULL) { /* insert at beginning of list */
p->tsi.tsi_suc = q;
(*qp) = p; /* new head */
} else if (q == NULL) { /* insert at end of list */
qprev->tsi.tsi_suc = p;
(*rp) = p; /* new tail */
} else { /* insert between qprev and q */
p->tsi.tsi_suc = q;
qprev->tsi.tsi_suc = p;
}
}
int rpmdepOrder(rpmTransactionSet ts)
{
int npkgs = ts->addedPackages.size;
int chainsaw = ts->transFlags & RPMTRANS_FLAG_CHAINSAW;
struct availablePackage * p;
struct availablePackage * q;
struct availablePackage * r;
struct tsortInfo * tsi;
struct tsortInfo * tsi_next;
int * ordering = alloca(sizeof(*ordering) * (npkgs + 1));
int orderingCount = 0;
unsigned char * selected = alloca(sizeof(*selected) * (npkgs + 1));
int loopcheck;
struct transactionElement * newOrder;
int newOrderCount = 0;
struct orderListIndex * orderList;
int nrescans = 10;
int _printed = 0;
int qlen;
int i, j;
alMakeIndex(&ts->addedPackages);
alMakeIndex(&ts->availablePackages);
/* T1. Initialize. */
loopcheck = npkgs;
/* Record all relations. */
rpmMessage(RPMMESS_DEBUG, _("========== recording tsort relations\n"));
if ((p = ts->addedPackages.list) != NULL)
for (i = 0; i < npkgs; i++, p++) {
int matchNum;
if (p->requiresCount <= 0)
continue;
memset(selected, 0, sizeof(*selected) * npkgs);
/* Avoid narcisstic relations. */
matchNum = p - ts->addedPackages.list;
selected[matchNum] = 1;
/* T2. Next "q <- p" relation. */
/* First, do pre-requisites. */
for (j = 0; j < p->requiresCount; j++) {
if (p->requireFlags == NULL) /* XXX can't happen */
/*@innercontinue@*/ continue;
/* Skip if not %pre/%post requires or legacy prereq. */
if (isErasePreReq(p->requireFlags[j]) ||
!( isInstallPreReq(p->requireFlags[j]) ||
isLegacyPreReq(p->requireFlags[j]) ))
/*@innercontinue@*/ continue;
/* T3. Record next "q <- p" relation (i.e. "p" requires "q"). */
(void) addRelation(ts, p, selected, j);
}
/* Then do co-requisites. */
for (j = 0; j < p->requiresCount; j++) {
if (p->requireFlags == NULL) /* XXX can't happen */
/*@innercontinue@*/ continue;
/* Skip if %pre/%post requires or legacy prereq. */
if (isErasePreReq(p->requireFlags[j]) ||
( isInstallPreReq(p->requireFlags[j]) ||
isLegacyPreReq(p->requireFlags[j]) ))
/*@innercontinue@*/ continue;
/* T3. Record next "q <- p" relation (i.e. "p" requires "q"). */
(void) addRelation(ts, p, selected, j);
}
}
/* Save predecessor count. */
if ((p = ts->addedPackages.list) != NULL)
for (i = 0; i < npkgs; i++, p++) {
p->npreds = p->tsi.tsi_count;
}
/* T4. Scan for zeroes. */
rpmMessage(RPMMESS_DEBUG, _("========== tsorting packages (order, #predecessors, #succesors, depth)\n"));
rescan:
q = r = NULL;
qlen = 0;
if ((p = ts->addedPackages.list) != NULL)
for (i = 0; i < npkgs; i++, p++) {
/* Prefer packages in presentation order. */
if (!chainsaw)
p->tsi.tsi_qcnt = (npkgs - i);
if (p->tsi.tsi_count != 0)
continue;
p->tsi.tsi_suc = NULL;
addQ(p, &q, &r);
qlen++;
}
/* T5. Output front of queue (T7. Remove from queue.) */
for (; q != NULL; q = q->tsi.tsi_suc) {
rpmMessage(RPMMESS_DEBUG, "%5d%5d%5d%3d %*s %s-%s-%s\n",
orderingCount, q->npreds, q->tsi.tsi_qcnt, q->depth,
2*q->depth, "",
q->name, q->version, q->release);
ordering[orderingCount++] = q - ts->addedPackages.list;
qlen--;
loopcheck--;
/* T6. Erase relations. */
tsi_next = q->tsi.tsi_next;
q->tsi.tsi_next = NULL;
while ((tsi = tsi_next) != NULL) {
tsi_next = tsi->tsi_next;
tsi->tsi_next = NULL;
p = tsi->tsi_suc;
if (p && (--p->tsi.tsi_count) <= 0) {
/* XXX TODO: add control bit. */
p->tsi.tsi_suc = NULL;
/*@-nullstate@*/ /* FIX: q->tsi.tsi_u.suc may be NULL */
addQ(p, &q->tsi.tsi_suc, &r);
/*@=nullstate@*/
qlen++;
}
tsi = _free(tsi);
}
if (!_printed && loopcheck == qlen && q->tsi.tsi_suc != NULL) {
_printed++;
rpmMessage(RPMMESS_DEBUG,
_("========== successors only (presentation order)\n"));
}
}
/* T8. End of process. Check for loops. */
if (loopcheck != 0) {
int nzaps;
/* T9. Initialize predecessor chain. */
nzaps = 0;
if ((q = ts->addedPackages.list) != NULL)
for (i = 0; i < npkgs; i++, q++) {
q->tsi.tsi_pkg = NULL;
q->tsi.tsi_reqx = 0;
/* Mark packages already sorted. */
if (q->tsi.tsi_count == 0)
q->tsi.tsi_count = -1;
}
/* T10. Mark all packages with their predecessors. */
if ((q = ts->addedPackages.list) != NULL)
for (i = 0; i < npkgs; i++, q++) {
if ((tsi = q->tsi.tsi_next) == NULL)
continue;
q->tsi.tsi_next = NULL;
markLoop(tsi, q);
q->tsi.tsi_next = tsi;
}
/* T11. Print all dependency loops. */
if ((r = ts->addedPackages.list) != NULL)
for (i = 0; i < npkgs; i++, r++) {
int printed;
printed = 0;
/* T12. Mark predecessor chain, looking for start of loop. */
for (q = r->tsi.tsi_pkg; q != NULL; q = q->tsi.tsi_pkg) {
if (q->tsi.tsi_reqx)
/*@innerbreak@*/ break;
q->tsi.tsi_reqx = 1;
}
/* T13. Print predecessor chain from start of loop. */
while ((p = q) != NULL && (q = p->tsi.tsi_pkg) != NULL) {
const char * dp;
char buf[4096];
/* Unchain predecessor loop. */
p->tsi.tsi_pkg = NULL;
if (!printed) {
rpmMessage(RPMMESS_DEBUG, _("LOOP:\n"));
printed = 1;
}
/* Find (and destroy if co-requisite) "q <- p" relation. */
dp = zapRelation(q, p, 1, &nzaps);
/* Print next member of loop. */
sprintf(buf, "%s-%s-%s", p->name, p->version, p->release);
rpmMessage(RPMMESS_DEBUG, " %-40s %s\n", buf,
(dp ? dp : "not found!?!"));
dp = _free(dp);
}
/* Walk (and erase) linear part of predecessor chain as well. */
for (p = r, q = r->tsi.tsi_pkg;
q != NULL;
p = q, q = q->tsi.tsi_pkg)
{
/* Unchain linear part of predecessor loop. */
p->tsi.tsi_pkg = NULL;
p->tsi.tsi_reqx = 0;
}
}
/* If a relation was eliminated, then continue sorting. */
/* XXX TODO: add control bit. */
if (nzaps && nrescans-- > 0) {
rpmMessage(RPMMESS_DEBUG, _("========== continuing tsort ...\n"));
goto rescan;
}
return 1;
}
/*
* The order ends up as installed packages followed by removed packages,
* with removes for upgrades immediately following the installation of
* the new package. This would be easier if we could sort the
* addedPackages array, but we store indexes into it in various places.
*/
orderList = xcalloc(npkgs, sizeof(*orderList));
for (i = 0, j = 0; i < ts->orderCount; i++) {
if (ts->order[i].type == TR_ADDED) {
orderList[j].alIndex = ts->order[i].u.addedIndex;
orderList[j].orIndex = i;
j++;
}
}
assert(j <= npkgs);
qsort(orderList, npkgs, sizeof(*orderList), orderListIndexCmp);
newOrder = xcalloc(ts->orderCount, sizeof(*newOrder));
for (i = 0, newOrderCount = 0; i < orderingCount; i++) {
struct orderListIndex * needle, key;
key.alIndex = ordering[i];
needle = bsearch(&key, orderList, npkgs, sizeof(key),orderListIndexCmp);
/* bsearch should never, ever fail */
if (needle == NULL) continue;
newOrder[newOrderCount++] = ts->order[needle->orIndex];
for (j = needle->orIndex + 1; j < ts->orderCount; j++) {
if (ts->order[j].type == TR_REMOVED &&
ts->order[j].u.removed.dependsOnIndex == needle->alIndex) {
newOrder[newOrderCount++] = ts->order[j];
} else
/*@innerbreak@*/ break;
}
}
for (i = 0; i < ts->orderCount; i++) {
if (ts->order[i].type == TR_REMOVED &&
ts->order[i].u.removed.dependsOnIndex == -1) {
newOrder[newOrderCount++] = ts->order[i];
}
}
assert(newOrderCount == ts->orderCount);
ts->order = _free(ts->order);
ts->order = newOrder;
ts->orderAlloced = ts->orderCount;
orderList = _free(orderList);
return 0;
}
/**
* Close a single database index.
* @param db rpm database
* @param rpmtag rpm tag
* @return 0 on success
*/
static int rpmdbCloseDBI(/*@null@*/ rpmdb db, int rpmtag)
/*@globals fileSystem @*/
/*@modifies db, fileSystem @*/
{
int dbix;
int rc = 0;
if (db == NULL || db->_dbi == NULL || dbiTags == NULL)
return 0;
for (dbix = 0; dbix < dbiTagsMax; dbix++) {
if (dbiTags[dbix] != rpmtag)
continue;
if (db->_dbi[dbix] != NULL) {
int xx;
/*@-unqualifiedtrans@*/ /* FIX: double indirection. */
xx = dbiClose(db->_dbi[dbix], 0);
if (xx && rc == 0) rc = xx;
db->_dbi[dbix] = NULL;
/*@=unqualifiedtrans@*/
}
break;
}
return rc;
}
int rpmdepCheck(rpmTransactionSet ts,
rpmDependencyConflict * conflicts, int * numConflicts)
{
HGE_t hge = (HGE_t)headerGetEntryMinMemory;
HFD_t hfd = headerFreeData;
rpmdbMatchIterator mi = NULL;
Header h = NULL;
struct availablePackage * p;
problemsSet ps;
int npkgs;
int i, j;
int rc;
npkgs = ts->addedPackages.size;
ps = xcalloc(1, sizeof(*ps));
ps->alloced = 5;
ps->num = 0;
ps->problems = xcalloc(ps->alloced, sizeof(*ps->problems));
*conflicts = NULL;
*numConflicts = 0;
alMakeIndex(&ts->addedPackages);
alMakeIndex(&ts->availablePackages);
/*
* Look at all of the added packages and make sure their dependencies
* are satisfied.
*/
if ((p = ts->addedPackages.list) != NULL)
for (i = 0; i < npkgs; i++, p++)
{
rpmMessage(RPMMESS_DEBUG, "========== +++ %s-%s-%s\n" ,
p->name, p->version, p->release);
rc = checkPackageDeps(ts, ps, p->h, NULL, p->multiLib);
if (rc)
goto exit;
/* Adding: check name against conflicts matches. */
rc = checkDependentConflicts(ts, ps, p->name);
if (rc)
goto exit;
if (p->providesCount == 0 || p->provides == NULL)
continue;
rc = 0;
for (j = 0; j < p->providesCount; j++) {
/* Adding: check provides key against conflicts matches. */
if (!checkDependentConflicts(ts, ps, p->provides[j]))
/*@innercontinue@*/ continue;
rc = 1;
/*@innerbreak@*/ break;
}
if (rc)
goto exit;
}
/*
* Look at the removed packages and make sure they aren't critical.
*/
if (ts->numRemovedPackages > 0) {
mi = rpmdbInitIterator(ts->rpmdb, RPMDBI_PACKAGES, NULL, 0);
(void) rpmdbAppendIterator(mi,
ts->removedPackages, ts->numRemovedPackages);
while ((h = rpmdbNextIterator(mi)) != NULL) {
{ const char * name, * version, * release;
(void) headerNVR(h, &name, &version, &release);
rpmMessage(RPMMESS_DEBUG, "========== --- %s-%s-%s\n" ,
name, version, release);
/* Erasing: check name against requiredby matches. */
rc = checkDependentPackages(ts, ps, name);
if (rc)
goto exit;
}
{ const char ** provides;
int providesCount;
rpmTagType pnt;
if (hge(h, RPMTAG_PROVIDENAME, &pnt, (void **) &provides,
&providesCount))
{
rc = 0;
for (j = 0; j < providesCount; j++) {
/* Erasing: check provides against requiredby matches. */
if (!checkDependentPackages(ts, ps, provides[j]))
/*@innercontinue@*/ continue;
rc = 1;
/*@innerbreak@*/ break;
}
provides = hfd(provides, pnt);
if (rc)
goto exit;
}
}
{ const char ** baseNames, ** dirNames;
int_32 * dirIndexes;
rpmTagType dnt, bnt;
int fileCount;
char * fileName = NULL;
int fileAlloced = 0;
int len;
if (hge(h, RPMTAG_BASENAMES, &bnt, (void **) &baseNames, &fileCount))
{
(void) hge(h, RPMTAG_DIRNAMES, &dnt, (void **) &dirNames, NULL);
(void) hge(h, RPMTAG_DIRINDEXES, NULL, (void **) &dirIndexes,
NULL);
rc = 0;
for (j = 0; j < fileCount; j++) {
len = strlen(baseNames[j]) + 1 +
strlen(dirNames[dirIndexes[j]]);
if (len > fileAlloced) {
fileAlloced = len * 2;
fileName = xrealloc(fileName, fileAlloced);
}
*fileName = '\0';
(void) stpcpy( stpcpy(fileName, dirNames[dirIndexes[j]]) , baseNames[j]);
/* Erasing: check filename against requiredby matches. */
if (!checkDependentPackages(ts, ps, fileName))
/*@innercontinue@*/ continue;
rc = 1;
/*@innerbreak@*/ break;
}
fileName = _free(fileName);
baseNames = hfd(baseNames, bnt);
dirNames = hfd(dirNames, dnt);
if (rc)
goto exit;
}
}
}
mi = rpmdbFreeIterator(mi);
}
if (ps->num) {
*conflicts = ps->problems;
ps->problems = NULL;
*numConflicts = ps->num;
}
rc = 0;
exit:
mi = rpmdbFreeIterator(mi);
ps->problems = _free(ps->problems);
ps = _free(ps);
if (_cacheDependsRC)
(void) rpmdbCloseDBI(ts->rpmdb, RPMDBI_DEPENDS);
return rc;
}
|