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
|
/*
* Copyright (c) 2007, Novell Inc.
*
* This program is licensed under the BSD license, read LICENSE.BSD
* for further information
*/
/*
* repodata.c
*
* Manage data coming from one repository
*
*/
#define _GNU_SOURCE
#include <string.h>
#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include "repo.h"
#include "pool.h"
#include "poolid_private.h"
#include "util.h"
#include "repopack.h"
extern unsigned int compress_buf (const unsigned char *in, unsigned int in_len,
unsigned char *out, unsigned int out_len);
extern unsigned int unchecked_decompress_buf (const unsigned char *in,
unsigned int in_len,
unsigned char *out,
unsigned int out_len);
#define REPODATA_BLOCK 255
void
repodata_init(Repodata *data, Repo *repo, int localpool)
{
memset(data, 0, sizeof (*data));
data->repo = repo;
data->localpool = localpool;
if (localpool)
stringpool_init_empty(&data->spool);
data->keys = sat_calloc(1, sizeof(Repokey));
data->nkeys = 1;
data->schemata = sat_calloc(1, sizeof(Id));
data->schemadata = sat_calloc(1, sizeof(Id));
data->nschemata = 1;
data->schemadatalen = 1;
data->start = repo->start;
data->end = repo->end;
data->nextra = repo->nextra;
data->extrastart = 0;
data->incoreoffset = sat_extend_resize(0, data->end - data->start, sizeof(Id), REPODATA_BLOCK);
data->extraoffset = sat_extend_resize(0, repo->nextra, sizeof(Id), REPODATA_BLOCK);
data->pagefd = -1;
}
void
repodata_free(Repodata *data)
{
sat_free(data->keys);
sat_free(data->schemata);
sat_free(data->schemadata);
sat_free(data->spool.strings);
sat_free(data->spool.stringspace);
sat_free(data->spool.stringhashtbl);
sat_free(data->dirpool.dirs);
sat_free(data->dirpool.dirtraverse);
sat_free(data->incoredata);
sat_free(data->incoreoffset);
sat_free(data->extraoffset);
sat_free(data->verticaloffset);
sat_free(data->blob_store);
sat_free(data->pages);
sat_free(data->mapped);
sat_free(data->vincore);
sat_free(data->attrs);
sat_free(data->extraattrs);
sat_free(data->attrdata);
sat_free(data->attriddata);
sat_free(data->location);
sat_free(data->addedfileprovides);
if (data->pagefd != -1)
close(data->pagefd);
}
unsigned char *
data_skip_recursive(Repodata *data, unsigned char *dp, Repokey *key)
{
KeyValue kv;
if (key->type != REPOKEY_TYPE_COUNTED)
return data_skip(dp, key->type);
dp = data_fetch(dp, &kv, key);
int num = kv.num;
int schema = kv.id;
while (num--)
{
Id *keyp = data->schemadata + data->schemata[schema];
for (; *keyp; keyp++)
dp = data_skip_recursive(data, dp, data->keys + *keyp);
}
return dp;
}
static unsigned char *
forward_to_key(Repodata *data, Id keyid, Id schemaid, unsigned char *dp)
{
Id k, *keyp;
keyp = data->schemadata + data->schemata[schemaid];
while ((k = *keyp++) != 0)
{
if (k == keyid)
return dp;
if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
{
dp = data_skip(dp, REPOKEY_TYPE_ID); /* skip that offset */
dp = data_skip(dp, REPOKEY_TYPE_ID); /* skip that length */
continue;
}
if (data->keys[k].storage != KEY_STORAGE_INCORE)
continue;
dp = data_skip_recursive(data, dp, data->keys + k);
}
return 0;
}
#define BLOB_PAGEBITS 15
#define BLOB_PAGESIZE (1 << BLOB_PAGEBITS)
static unsigned char *
load_page_range(Repodata *data, unsigned int pstart, unsigned int pend)
{
/* Make sure all pages from PSTART to PEND (inclusive) are loaded,
and are consecutive. Return a pointer to the mapping of PSTART. */
unsigned char buf[BLOB_PAGESIZE];
unsigned int i;
/* Quick check in case all pages are there already and consecutive. */
for (i = pstart; i <= pend; i++)
if (data->pages[i].mapped_at == -1
|| (i > pstart
&& data->pages[i].mapped_at
!= data->pages[i-1].mapped_at + BLOB_PAGESIZE))
break;
if (i > pend)
return data->blob_store + data->pages[pstart].mapped_at;
if (data->pagefd == -1)
return 0;
/* Ensure that we can map the numbers of pages we need at all. */
if (pend - pstart + 1 > data->ncanmap)
{
unsigned int oldcan = data->ncanmap;
data->ncanmap = pend - pstart + 1;
if (data->ncanmap < 4)
data->ncanmap = 4;
data->mapped = sat_realloc2(data->mapped, data->ncanmap, sizeof(data->mapped[0]));
memset (data->mapped + oldcan, 0, (data->ncanmap - oldcan) * sizeof (data->mapped[0]));
data->blob_store = sat_realloc2(data->blob_store, data->ncanmap, BLOB_PAGESIZE);
#ifdef DEBUG_PAGING
fprintf (stderr, "PAGE: can map %d pages\n", data->ncanmap);
#endif
}
/* Now search for "cheap" space in our store. Space is cheap if it's either
free (very cheap) or contains pages we search for anyway. */
/* Setup cost array. */
unsigned int cost[data->ncanmap];
for (i = 0; i < data->ncanmap; i++)
{
unsigned int pnum = data->mapped[i];
if (pnum == 0)
cost[i] = 0;
else
{
pnum--;
Attrblobpage *p = data->pages + pnum;
assert (p->mapped_at != -1);
if (pnum >= pstart && pnum <= pend)
cost[i] = 1;
else
cost[i] = 3;
}
}
/* And search for cheapest space. */
unsigned int best_cost = -1;
unsigned int best = 0;
unsigned int same_cost = 0;
for (i = 0; i + pend - pstart < data->ncanmap; i++)
{
unsigned int c = cost[i];
unsigned int j;
for (j = 0; j < pend - pstart + 1; j++)
c += cost[i+j];
if (c < best_cost)
best_cost = c, best = i;
else if (c == best_cost)
same_cost++;
/* A null cost won't become better. */
if (c == 0)
break;
}
/* If all places have the same cost we would thrash on slot 0. Avoid
this by doing a round-robin strategy in this case. */
if (same_cost == data->ncanmap - pend + pstart - 1)
best = data->rr_counter++ % (data->ncanmap - pend + pstart);
/* So we want to map our pages from [best] to [best+pend-pstart].
Use a very simple strategy, which doesn't make the best use of
our resources, but works. Throw away all pages in that range
(even ours) then copy around ours (in case they were outside the
range) or read them in. */
for (i = best; i < best + pend - pstart + 1; i++)
{
unsigned int pnum = data->mapped[i];
if (pnum--
/* If this page is exactly at the right place already,
no need to evict it. */
&& pnum != pstart + i - best)
{
/* Evict this page. */
#ifdef DEBUG_PAGING
fprintf (stderr, "PAGE: evict page %d from %d\n", pnum, i);
#endif
cost[i] = 0;
data->mapped[i] = 0;
data->pages[pnum].mapped_at = -1;
}
}
/* Everything is free now. Read in the pages we want. */
for (i = pstart; i <= pend; i++)
{
Attrblobpage *p = data->pages + i;
unsigned int pnum = i - pstart + best;
void *dest = data->blob_store + pnum * BLOB_PAGESIZE;
if (p->mapped_at != -1)
{
if (p->mapped_at != pnum * BLOB_PAGESIZE)
{
#ifdef DEBUG_PAGING
fprintf (stderr, "PAGECOPY: %d to %d\n", i, pnum);
#endif
/* Still mapped somewhere else, so just copy it from there. */
memcpy (dest, data->blob_store + p->mapped_at, BLOB_PAGESIZE);
data->mapped[p->mapped_at / BLOB_PAGESIZE] = 0;
}
}
else
{
unsigned int in_len = p->file_size;
unsigned int compressed = in_len & 1;
in_len >>= 1;
#ifdef DEBUG_PAGING
fprintf (stderr, "PAGEIN: %d to %d", i, pnum);
#endif
if (pread(data->pagefd, compressed ? buf : dest, in_len, p->file_offset) != in_len)
{
perror ("mapping pread");
return 0;
}
if (compressed)
{
unsigned int out_len;
out_len = unchecked_decompress_buf(buf, in_len,
dest, BLOB_PAGESIZE);
if (out_len != BLOB_PAGESIZE && i < data->num_pages - 1)
{
fprintf(stderr, "can't decompress\n");
return 0;
}
#ifdef DEBUG_PAGING
fprintf (stderr, " (expand %d to %d)", in_len, out_len);
#endif
}
#ifdef DEBUG_PAGING
fprintf (stderr, "\n");
#endif
}
p->mapped_at = pnum * BLOB_PAGESIZE;
data->mapped[pnum] = i + 1;
}
return data->blob_store + best * BLOB_PAGESIZE;
}
static unsigned char *
make_vertical_available(Repodata *data, Repokey *key, Id off, Id len)
{
unsigned char *dp;
if (!len)
return 0;
if (off >= data->lastverticaloffset)
{
off -= data->lastverticaloffset;
if (off + len > data->vincorelen)
return 0;
return data->vincore + off;
}
if (off + len > key->size)
return 0;
/* we now have the offset, go into vertical */
off += data->verticaloffset[key - data->keys];
/* fprintf(stderr, "key %d page %d\n", key->name, off / BLOB_PAGESIZE); */
dp = load_page_range(data, off / BLOB_PAGESIZE, (off + len - 1) / BLOB_PAGESIZE);
if (dp)
dp += off % BLOB_PAGESIZE;
return dp;
}
static inline unsigned char *
get_data(Repodata *data, Repokey *key, unsigned char **dpp)
{
unsigned char *dp = *dpp;
if (!dp)
return 0;
if (key->storage == KEY_STORAGE_INCORE)
{
/* hmm, this is a bit expensive */
*dpp = data_skip_recursive(data, dp, key);
return dp;
}
else if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
{
Id off, len;
dp = data_read_id(dp, &off);
dp = data_read_id(dp, &len);
*dpp = dp;
return make_vertical_available(data, key, off, len);
}
return 0;
}
static inline int
maybe_load_repodata(Repodata *data, Id *keyid)
{
if (data->state == REPODATA_STUB)
{
if (data->loadcallback)
{
if (keyid)
{
/* key order may change when loading */
int i;
Id name = data->keys[*keyid].name;
Id type = data->keys[*keyid].type;
data->loadcallback(data);
if (data->state == REPODATA_AVAILABLE)
{
for (i = 1; i < data->nkeys; i++)
if (data->keys[i].name == name && data->keys[i].type == type)
break;
if (i < data->nkeys)
*keyid = i;
else
return 0;
}
}
else
data->loadcallback(data);
}
else
data->state = REPODATA_ERROR;
}
if (data->state == REPODATA_AVAILABLE)
return 1;
data->state = REPODATA_ERROR;
return 0;
}
static inline unsigned char*
entry2data(Repodata *data, Id entry)
{
if (entry < 0)
return data->incoredata + data->extraoffset[-1 - entry];
else
return data->incoredata + data->incoreoffset[entry];
}
Id
repodata_lookup_id(Repodata *data, Id entry, Id keyid)
{
Id schema;
Repokey *key;
Id id, *keyp;
unsigned char *dp;
if (!maybe_load_repodata(data, &keyid))
return 0;
dp = entry2data(data, entry);
if (!dp)
return 0;
dp = data_read_id(dp, &schema);
/* make sure the schema of this solvable contains the key */
for (keyp = data->schemadata + data->schemata[schema]; *keyp != keyid; keyp++)
if (!*keyp)
return 0;
dp = forward_to_key(data, keyid, schema, dp);
key = data->keys + keyid;
dp = get_data(data, key, &dp);
if (!dp)
return 0;
if (key->type == REPOKEY_TYPE_CONSTANTID)
return key->size;
if (key->type != REPOKEY_TYPE_ID)
return 0;
dp = data_read_id(dp, &id);
return id;
}
const char *
repodata_lookup_str(Repodata *data, Id entry, Id keyid)
{
Id schema;
Repokey *key;
Id id, *keyp;
unsigned char *dp;
if (!maybe_load_repodata(data, &keyid))
return 0;
dp = entry2data(data, entry);
if (!dp)
return 0;
dp = data_read_id(dp, &schema);
/* make sure the schema of this solvable contains the key */
for (keyp = data->schemadata + data->schemata[schema]; *keyp != keyid; keyp++)
if (!*keyp)
return 0;
dp = forward_to_key(data, keyid, schema, dp);
key = data->keys + keyid;
dp = get_data(data, key, &dp);
if (!dp)
return 0;
if (key->type == REPOKEY_TYPE_STR)
return (const char *)dp;
if (key->type == REPOKEY_TYPE_CONSTANTID)
return id2str(data->repo->pool, key->size);
if (key->type == REPOKEY_TYPE_ID)
dp = data_read_id(dp, &id);
else
return 0;
if (data->localpool)
return data->spool.stringspace + data->spool.strings[id];
return id2str(data->repo->pool, id);
}
int
repodata_lookup_num(Repodata *data, Id entry, Id keyid, unsigned int *value)
{
Id schema;
Repokey *key;
Id *keyp;
KeyValue kv;
unsigned char *dp;
*value = 0;
if (!maybe_load_repodata(data, &keyid))
return 0;
dp = entry2data(data, entry);
if (!dp)
return 0;
dp = data_read_id(dp, &schema);
/* make sure the schema of this solvable contains the key */
for (keyp = data->schemadata + data->schemata[schema]; *keyp != keyid; keyp++)
if (!*keyp)
return 0;
dp = forward_to_key(data, keyid, schema, dp);
key = data->keys + keyid;
dp = get_data(data, key, &dp);
if (!dp)
return 0;
if (key->type == REPOKEY_TYPE_NUM
|| key->type == REPOKEY_TYPE_U32
|| key->type == REPOKEY_TYPE_CONSTANT)
{
dp = data_fetch(dp, &kv, key);
*value = kv.num;
return 1;
}
return 0;
}
int
repodata_lookup_void(Repodata *data, Id entry, Id keyid)
{
Id schema;
Id *keyp;
unsigned char *dp;
if (!maybe_load_repodata(data, &keyid))
return 0;
dp = entry2data(data, entry);
if (!dp)
return 0;
dp = data_read_id(dp, &schema);
for (keyp = data->schemadata + data->schemata[schema]; *keyp != keyid; keyp++)
if (!*keyp)
return 0;
return 1;
}
const unsigned char *
repodata_lookup_bin_checksum(Repodata *data, Id entry, Id keyid, Id *typep)
{
Id schema;
Id *keyp;
Repokey *key;
unsigned char *dp;
if (!maybe_load_repodata(data, &keyid))
return 0;
dp = entry2data(data, entry);
if (!dp)
return 0;
dp = data_read_id(dp, &schema);
for (keyp = data->schemadata + data->schemata[schema]; *keyp != keyid; keyp++)
if (!*keyp)
return 0;
dp = forward_to_key(data, keyid, schema, dp);
key = data->keys + keyid;
*typep = key->type;
return get_data(data, key, &dp);
}
void
repodata_search(Repodata *data, Id entry, Id keyname, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv), void *cbdata)
{
Id schema;
Repokey *key;
Id k, keyid, *kp, *keyp;
unsigned char *dp, *ddp;
int onekey = 0;
int stop;
KeyValue kv;
if (entry < 0
|| !maybe_load_repodata(data, 0))
return;
dp = entry2data(data, entry);
if (!dp)
return;
dp = data_read_id(dp, &schema);
keyp = data->schemadata + data->schemata[schema];
if (keyname)
{
/* search in a specific key */
for (kp = keyp; (k = *kp++) != 0; )
if (data->keys[k].name == keyname)
break;
if (k == 0)
return;
dp = forward_to_key(data, k, schema, dp);
if (!dp)
return;
keyp = kp - 1;
onekey = 1;
}
while ((keyid = *keyp++) != 0)
{
stop = 0;
key = data->keys + keyid;
ddp = get_data(data, key, &dp);
do
{
ddp = data_fetch(ddp, &kv, key);
if (!ddp)
break;
if (key->type == REPOKEY_TYPE_COUNTED)
{
int num = kv.num;
int subschema = kv.id;
Repokey *countkey = key;
kv.eof = 0;
callback(cbdata, data->repo->pool->solvables + data->start + entry, data, countkey, &kv);
while (num--)
{
Id *kp = data->schemadata + data->schemata[subschema];
for (; *kp; kp++)
{
key = data->keys + *kp;
ddp = data_fetch(ddp, &kv, key);
if (!ddp)
exit(1);
callback(cbdata, data->repo->pool->solvables + data->start + entry, data, key, &kv);
}
kv.eof = 1;
callback(cbdata, data->repo->pool->solvables + data->start + entry, data, countkey, &kv);
}
kv.eof = 2;
stop = callback(cbdata, data->repo->pool->solvables + data->start + entry, data, countkey, &kv);
}
else
stop = callback(cbdata, data->repo->pool->solvables + data->start + entry, data, key, &kv);
}
while (!kv.eof && !stop);
if (onekey || stop > SEARCH_NEXT_KEY)
return;
}
}
static void
dataiterator_newdata(Dataiterator *di)
{
Id keyname = di->keyname;
Repodata *data = di->data;
di->nextkeydp = 0;
if (data->state == REPODATA_STUB)
{
if (keyname)
{
int j;
for (j = 1; j < data->nkeys; j++)
if (keyname == data->keys[j].name)
break;
if (j == data->nkeys)
return;
}
/* load it */
if (data->loadcallback)
data->loadcallback(data);
else
data->state = REPODATA_ERROR;
}
if (data->state == REPODATA_ERROR)
return;
Id schema;
unsigned char *dp = data->incoredata;
if (!dp)
return;
if (di->solvid >= 0)
dp += data->incoreoffset[di->solvid - data->start];
else
dp += data->extraoffset[-1 - di->solvid - data->extrastart];
dp = data_read_id(dp, &schema);
Id *keyp = data->schemadata + data->schemata[schema];
if (keyname)
{
Id k, *kp;
/* search in a specific key */
for (kp = keyp; (k = *kp++) != 0; )
if (data->keys[k].name == keyname)
break;
if (k == 0)
return;
dp = forward_to_key(data, k, schema, dp);
if (!dp)
return;
keyp = kp - 1;
}
Id keyid = *keyp++;
if (!keyid)
return;
di->data = data;
di->key = di->data->keys + keyid;
di->keyp = keyp;
di->dp = 0;
di->nextkeydp = dp;
di->dp = get_data(di->data, di->key, &di->nextkeydp);
di->kv.eof = 0;
}
void
dataiterator_init(Dataiterator *di, Repo *repo, Id p, Id keyname,
const char *match, int flags)
{
di->flags = flags;
if (p)
{
di->solvid = p;
di->flags |= __SEARCH_ONESOLVABLE;
di->data = repo->repodata - 1;
if (flags & SEARCH_NO_STORAGE_SOLVABLE)
di->state = 0;
else
di->state = 1;
}
else
{
di->solvid = repo->start - 1;
if (di->solvid < 0)
{
fprintf(stderr, "A repo contains the NULL solvable!\n");
exit(1);
}
di->data = repo->repodata + repo->nrepodata - 1;
di->state = 0;
}
di->match = match;
if ((di->flags & SEARCH_STRINGMASK) == SEARCH_REGEX)
{
if (di->match)
{
/* We feed multiple lines eventually (e.g. authors or descriptions),
so set REG_NEWLINE. */
di->regex_err =
regcomp(&di->regex, di->match,
REG_EXTENDED | REG_NOSUB | REG_NEWLINE
| ((di->flags & SEARCH_NOCASE) ? REG_ICASE : 0));
#if 0
if (di->regex_err != 0)
{
fprintf(stderr, "Given regex failed to compile: %s\n", di->match);
fprintf(stderr, "regcomp error code: %d\n", di->regex_err);
exit(1);
}
#else
}
else
{
di->flags |= (di->flags & SEARCH_STRINGMASK) | SEARCH_STRING;
di->regex_err = 0;
#endif
}
}
di->keyname = keyname;
static Id zeroid = 0;
di->keyp = &zeroid;
di->kv.eof = 1;
di->repo = repo;
di->idp = 0;
di->subkeyp = 0;
}
/* FIXME factor and merge with repo_matchvalue */
static int
dataiterator_match_int_real(Dataiterator *di, int flags, const void *vmatch)
{
KeyValue *kv = &di->kv;
const char *match = vmatch;
if ((flags & SEARCH_STRINGMASK) != 0)
{
switch (di->key->type)
{
case REPOKEY_TYPE_ID:
case REPOKEY_TYPE_IDARRAY:
if (di->data && di->data->localpool)
kv->str = stringpool_id2str(&di->data->spool, kv->id);
else
kv->str = id2str(di->repo->pool, kv->id);
break;
case REPOKEY_TYPE_STR:
break;
case REPOKEY_TYPE_DIRSTRARRAY:
if (!(flags & SEARCH_FILES))
return 0;
/* Put the full filename into kv->str. */
kv->str = repodata_dir2str(di->data, kv->id, kv->str);
/* And to compensate for that put the "empty" directory into
kv->id, so that later calls to repodata_dir2str on this data
come up with the same filename again. */
kv->id = 0;
break;
default:
return 0;
}
/* Maybe skip the kind specifier. Do this only for SOLVABLE attributes,
for the others we can't know if a colon separates a kind or not. */
if ((flags & SEARCH_SKIP_KIND)
&& di->key->storage == KEY_STORAGE_SOLVABLE)
{
const char *s = strchr(kv->str, ':');
if (s)
kv->str = s + 1;
}
switch ((flags & SEARCH_STRINGMASK))
{
case SEARCH_SUBSTRING:
if (flags & SEARCH_NOCASE)
{
if (!strcasestr(kv->str, match))
return 0;
}
else
{
if (!strstr(kv->str, match))
return 0;
}
break;
case SEARCH_STRING:
if (flags & SEARCH_NOCASE)
{
if (strcasecmp(match, kv->str))
return 0;
}
else
{
if (strcmp(match, kv->str))
return 0;
}
break;
case SEARCH_GLOB:
if (fnmatch(match, kv->str, (flags & SEARCH_NOCASE) ? FNM_CASEFOLD : 0))
return 0;
break;
case SEARCH_REGEX:
if (regexec((const regex_t *)vmatch, kv->str, 0, NULL, 0))
return 0;
break;
default:
return 0;
}
}
return 1;
}
static int
dataiterator_match_int(Dataiterator *di)
{
if ((di->flags & SEARCH_STRINGMASK) == SEARCH_REGEX)
return dataiterator_match_int_real(di, di->flags, &di->regex);
else
return dataiterator_match_int_real(di, di->flags, di->match);
}
int
dataiterator_match(Dataiterator *di, int flags, const void *vmatch)
{
return dataiterator_match_int_real(di, flags, vmatch);
}
static Repokey solvablekeys[RPM_RPMDBID - SOLVABLE_NAME + 1] = {
{ SOLVABLE_NAME, REPOKEY_TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_ARCH, REPOKEY_TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_EVR, REPOKEY_TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_VENDOR, REPOKEY_TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_PROVIDES, REPOKEY_TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_OBSOLETES, REPOKEY_TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_CONFLICTS, REPOKEY_TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_REQUIRES, REPOKEY_TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_RECOMMENDS, REPOKEY_TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_SUGGESTS, REPOKEY_TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_SUPPLEMENTS, REPOKEY_TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
{ SOLVABLE_ENHANCES, REPOKEY_TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
{ RPM_RPMDBID, REPOKEY_TYPE_U32, 0, KEY_STORAGE_SOLVABLE },
};
int
dataiterator_step(Dataiterator *di)
{
restart:
while (1)
{
if (di->state)
{
if (di->idp)
{
Id *idp = di->idp;
if (*idp)
{
di->kv.id = *idp;
di->idp++;
di->kv.eof = idp[1] ? 0 : 1;
goto weg2;
}
else
di->idp = 0;
}
Solvable *s = di->repo->pool->solvables + di->solvid;
int state = di->state;
di->key = solvablekeys + state - 1;
if (di->keyname)
di->state = RPM_RPMDBID;
else
di->state++;
if (state == 1)
{
di->data = 0;
if (di->keyname)
state = di->keyname - 1;
}
switch (state + 1)
{
case SOLVABLE_NAME:
if (!s->name)
continue;
di->kv.id = s->name;
di->kv.eof = 1;
break;
case SOLVABLE_ARCH:
if (!s->arch)
continue;
di->kv.id = s->arch;
di->kv.eof = 1;
break;
case SOLVABLE_EVR:
if (!s->evr)
continue;
di->kv.id = s->evr;
di->kv.eof = 1;
break;
case SOLVABLE_VENDOR:
if (!s->vendor)
continue;
di->kv.id = s->vendor;
di->kv.eof = 1;
break;
case SOLVABLE_PROVIDES:
di->idp = s->provides
? di->repo->idarraydata + s->provides : 0;
continue;
case SOLVABLE_OBSOLETES:
di->idp = s->obsoletes
? di->repo->idarraydata + s->obsoletes : 0;
continue;
case SOLVABLE_CONFLICTS:
di->idp = s->conflicts
? di->repo->idarraydata + s->conflicts : 0;
continue;
case SOLVABLE_REQUIRES:
di->idp = s->requires
? di->repo->idarraydata + s->requires : 0;
continue;
case SOLVABLE_RECOMMENDS:
di->idp = s->recommends
? di->repo->idarraydata + s->recommends : 0;
continue;
case SOLVABLE_SUPPLEMENTS:
di->idp = s->supplements
? di->repo->idarraydata + s->supplements : 0;
continue;
case SOLVABLE_SUGGESTS:
di->idp = s->suggests
? di->repo->idarraydata + s->suggests : 0;
continue;
case SOLVABLE_ENHANCES:
di->idp = s->enhances
? di->repo->idarraydata + s->enhances : 0;
continue;
case RPM_RPMDBID:
if (!di->repo->rpmdbid)
continue;
di->kv.num = di->repo->rpmdbid[di->solvid - di->repo->start];
di->kv.eof = 1;
break;
default:
di->data = di->repo->repodata - 1;
di->kv.eof = 1;
di->state = 0;
continue;
}
}
else if (di->subkeyp)
{
Id keyid;
if (!di->subnum)
{
/* Send end-of-substruct. We are here only when we saw a
_COUNTED key one level up. Since then we didn't increment
->keyp, so it still can be found at keyp[-1]. */
di->kv.eof = 2;
di->key = di->data->keys + di->keyp[-1];
di->subkeyp = 0;
}
else if (!(keyid = *di->subkeyp++))
{
/* Send end-of-element. See above for keyp[-1]. */
di->kv.eof = 1;
di->key = di->data->keys + di->keyp[-1];
di->subkeyp = di->data->schemadata + di->data->schemata[di->subschema];
di->subnum--;
}
else
{
di->key = di->data->keys + keyid;
di->dp = data_fetch(di->dp, &di->kv, di->key);
if (!di->dp)
exit(1);
}
}
else
{
if (di->kv.eof)
di->dp = 0;
else
di->dp = data_fetch(di->dp, &di->kv, di->key);
while (!di->dp)
{
Id keyid;
if (di->keyname || !(keyid = *di->keyp++))
{
while (1)
{
Repo *repo = di->repo;
Repodata *data = ++di->data;
if (data >= repo->repodata + repo->nrepodata)
{
if (di->flags & __SEARCH_ONESOLVABLE)
return 0;
if (di->solvid >= 0)
{
while (++di->solvid < repo->end)
if (repo->pool->solvables[di->solvid].repo == repo)
break;
if (di->solvid >= repo->end)
{
if (!(di->flags & SEARCH_EXTRA))
goto skiprepo;
di->solvid = -1;
if (di->solvid < -repo->nextra)
goto skiprepo;
}
}
else
{
--di->solvid;
if (di->solvid < -repo->nextra)
{
skiprepo:;
Pool *pool = di->repo->pool;
if (!(di->flags & SEARCH_ALL_REPOS)
|| di->repo == pool->repos[pool->nrepos - 1])
return 0;
int i;
for (i = 0; i < pool->nrepos; i++)
if (di->repo == pool->repos[i])
break;
di->repo = pool->repos[i + 1];
dataiterator_init(di, di->repo, 0, di->keyname, di->match, di->flags);
continue;
}
}
di->data = repo->repodata - 1;
if (di->solvid < 0
|| (di->flags & SEARCH_NO_STORAGE_SOLVABLE))
continue;
static Id zeroid = 0;
di->keyp = &zeroid;
di->state = 1;
goto restart;
}
if ((di->solvid < 0 && (-1 - di->solvid) >= data->extrastart && (-1 - di->solvid) < (data->extrastart + data->nextra))
|| (di->solvid >= 0 && di->solvid >= data->start && di->solvid < data->end))
{
dataiterator_newdata(di);
if (di->nextkeydp)
break;
}
}
}
else
{
di->key = di->data->keys + keyid;
di->dp = get_data(di->data, di->key, &di->nextkeydp);
}
di->dp = data_fetch(di->dp, &di->kv, di->key);
}
if (di->key->type == REPOKEY_TYPE_COUNTED)
{
di->subnum = di->kv.num;
di->subschema = di->kv.id;
di->kv.eof = 0;
di->subkeyp = di->data->schemadata + di->data->schemata[di->subschema];
}
}
weg2:
if (!di->match
|| dataiterator_match_int(di))
break;
}
return 1;
}
void
dataiterator_skip_attribute(Dataiterator *di)
{
if (di->state)
di->idp = 0;
/* This will make the next _step call to retrieve the next field. */
di->kv.eof = 1;
}
void
dataiterator_skip_solvable(Dataiterator *di)
{
/* We're done with this field. */
di->kv.eof = 1;
/* And with solvable data. */
di->state = 0;
/* And with all keys for this repodata and thing. */
static Id zeroid = 0;
di->keyp = &zeroid;
/* And with all repodatas for this thing. */
di->data = di->repo->repodata + di->repo->nrepodata - 1;
/* Hence the next call to _step will retrieve the next thing. */
}
void
dataiterator_skip_repo(Dataiterator *di)
{
dataiterator_skip_solvable(di);
/* We're done with all solvables and all extra things for this repo. */
di->solvid = -1 - di->repo->nextra;
}
void
dataiterator_jump_to_solvable(Dataiterator *di, Solvable *s)
{
di->repo = s->repo;
/* Simulate us being done with the solvable before the requested one. */
dataiterator_skip_solvable(di);
di->solvid = s - s->repo->pool->solvables;
di->solvid--;
}
void
dataiterator_jump_to_repo(Dataiterator *di, Repo *repo)
{
di->repo = repo;
dataiterator_skip_solvable(di);
di->solvid = repo->start - 1;
}
/* extend repodata so that it includes solvables p */
void
repodata_extend(Repodata *data, Id p)
{
if (data->start == data->end)
data->start = data->end = p;
if (p >= data->end)
{
int old = data->end - data->start;
int new = p - data->end + 1;
if (data->attrs)
{
data->attrs = sat_extend(data->attrs, old, new, sizeof(Id), REPODATA_BLOCK);
memset(data->attrs + old, 0, new * sizeof(Id));
}
data->incoreoffset = sat_extend(data->incoreoffset, old, new, sizeof(Id), REPODATA_BLOCK);
memset(data->incoreoffset + old, 0, new * sizeof(Id));
data->end = p + 1;
}
if (p < data->start)
{
int old = data->end - data->start;
int new = data->start - p;
if (data->attrs)
{
data->attrs = sat_extend_resize(data->attrs, old + new, sizeof(Id), REPODATA_BLOCK);
memmove(data->attrs + new, data->attrs, old * sizeof(Id));
memset(data->attrs, 0, new * sizeof(Id));
}
data->incoreoffset = sat_extend_resize(data->incoreoffset, old + new, sizeof(Id), REPODATA_BLOCK);
memmove(data->incoreoffset + new, data->incoreoffset, old * sizeof(Id));
memset(data->incoreoffset, 0, new * sizeof(Id));
data->start = p;
}
}
void
repodata_extend_extra(Repodata *data, int nextra)
{
if (nextra <= data->nextra)
return;
if (data->extraattrs)
{
data->extraattrs = sat_extend(data->extraattrs, data->nextra, nextra - data->nextra, sizeof(Id), REPODATA_BLOCK);
memset(data->extraattrs + data->nextra, 0, (nextra - data->nextra) * sizeof (Id));
}
data->extraoffset = sat_extend(data->extraoffset, data->nextra, nextra - data->nextra, sizeof(Id), REPODATA_BLOCK);
memset(data->extraoffset + data->nextra, 0, (nextra - data->nextra) * sizeof(Id));
data->nextra = nextra;
}
void
repodata_extend_block(Repodata *data, Id start, Id num)
{
if (!num)
return;
if (!data->incoreoffset)
{
data->incoreoffset = sat_calloc_block(num, sizeof(Id), REPODATA_BLOCK);
data->start = start;
data->end = start + num;
return;
}
repodata_extend(data, start);
if (num > 1)
repodata_extend(data, start + num - 1);
}
/**********************************************************************/
#define REPODATA_ATTRS_BLOCK 63
#define REPODATA_ATTRDATA_BLOCK 1023
#define REPODATA_ATTRIDDATA_BLOCK 63
static inline Id
get_new_struct(Repodata *data)
{
/* Make sure to never give out struct id 0. */
if (!data->structs)
{
data->structs = sat_extend(0, 0, 2, sizeof(Id *), REPODATA_BLOCK);
data->structs[0] = 0;
data->structs[1] = 0;
data->nstructs = 2;
return 1;
}
data->structs = sat_extend(data->structs, data->nstructs, 1, sizeof(Id *), REPODATA_BLOCK);
data->structs[data->nstructs] = 0;
return data->nstructs++;
}
static Id
repodata_get_handle_int(Repodata *data, Id entry)
{
Id *ap;
if (!data->attrs && entry >= 0)
{
data->attrs = sat_calloc_block(data->end - data->start, sizeof(Id),
REPODATA_BLOCK);
}
else if (!data->extraattrs && entry < 0)
data->extraattrs = sat_calloc_block(data->nextra, sizeof(Id), REPODATA_BLOCK);
if (entry < 0)
ap = &data->extraattrs[-1 - entry];
else
ap = &data->attrs[entry];
if (!*ap)
*ap = get_new_struct(data);
return *ap;
}
Id
repodata_get_handle(Repodata *data, Id entry)
{
return repodata_get_handle_int(data, entry);
}
static void
repodata_insert_keyid(Repodata *data, Id handle, Id keyid, Id val, int overwrite)
{
Id *pp;
Id *ap;
int i;
ap = data->structs[handle];
i = 0;
if (ap)
{
for (pp = ap; *pp; pp += 2)
/* Determine equality based on the name only, allows us to change
type (when overwrite is set), and makes TYPE_CONSTANT work. */
if (data->keys[*pp].name == data->keys[keyid].name)
break;
if (*pp)
{
if (overwrite)
{
pp[0] = keyid;
pp[1] = val;
}
return;
}
i = pp - ap;
}
ap = sat_extend(ap, i, 3, sizeof(Id), REPODATA_ATTRS_BLOCK);
data->structs[handle] = ap;
pp = ap + i;
*pp++ = keyid;
*pp++ = val;
*pp = 0;
}
void
repodata_set(Repodata *data, Id handle, Repokey *key, Id val)
{
Id keyid;
/* find key in keys */
for (keyid = 1; keyid < data->nkeys; keyid++)
if (data->keys[keyid].name == key->name && data->keys[keyid].type == key->type)
{
if ((key->type == REPOKEY_TYPE_CONSTANT || key->type == REPOKEY_TYPE_CONSTANTID) && key->size != data->keys[keyid].size)
continue;
break;
}
if (keyid == data->nkeys)
{
/* allocate new key */
data->keys = sat_realloc2(data->keys, data->nkeys + 1, sizeof(Repokey));
data->keys[data->nkeys++] = *key;
if (data->verticaloffset)
{
data->verticaloffset = sat_realloc2(data->verticaloffset, data->nkeys, sizeof(Id));
data->verticaloffset[data->nkeys - 1] = 0;
}
}
repodata_insert_keyid(data, handle, keyid, val, 1);
}
void
repodata_set_id(Repodata *data, Id handle, Id keyname, Id id)
{
Repokey key;
key.name = keyname;
key.type = REPOKEY_TYPE_ID;
key.size = 0;
key.storage = KEY_STORAGE_INCORE;
repodata_set(data, handle, &key, id);
}
void
repodata_set_num(Repodata *data, Id handle, Id keyname, unsigned int num)
{
Repokey key;
key.name = keyname;
key.type = REPOKEY_TYPE_NUM;
key.size = 0;
key.storage = KEY_STORAGE_INCORE;
repodata_set(data, handle, &key, (Id)num);
}
void
repodata_set_poolstr(Repodata *data, Id handle, Id keyname, const char *str)
{
Repokey key;
Id id;
if (data->localpool)
id = stringpool_str2id(&data->spool, str, 1);
else
id = str2id(data->repo->pool, str, 1);
key.name = keyname;
key.type = REPOKEY_TYPE_ID;
key.size = 0;
key.storage = KEY_STORAGE_INCORE;
repodata_set(data, handle, &key, id);
}
void
repodata_set_constant(Repodata *data, Id handle, Id keyname, unsigned int constant)
{
Repokey key;
key.name = keyname;
key.type = REPOKEY_TYPE_CONSTANT;
key.size = constant;
key.storage = KEY_STORAGE_INCORE;
repodata_set(data, handle, &key, 0);
}
void
repodata_set_constantid(Repodata *data, Id handle, Id keyname, Id id)
{
Repokey key;
key.name = keyname;
key.type = REPOKEY_TYPE_CONSTANTID;
key.size = id;
key.storage = KEY_STORAGE_INCORE;
repodata_set(data, handle, &key, 0);
}
void
repodata_set_void(Repodata *data, Id handle, Id keyname)
{
Repokey key;
key.name = keyname;
key.type = REPOKEY_TYPE_VOID;
key.size = 0;
key.storage = KEY_STORAGE_INCORE;
repodata_set(data, handle, &key, 0);
}
void
repodata_set_str(Repodata *data, Id handle, Id keyname, const char *str)
{
Repokey key;
int l;
l = strlen(str) + 1;
key.name = keyname;
key.type = REPOKEY_TYPE_STR;
key.size = 0;
key.storage = KEY_STORAGE_INCORE;
data->attrdata = sat_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
memcpy(data->attrdata + data->attrdatalen, str, l);
repodata_set(data, handle, &key, data->attrdatalen);
data->attrdatalen += l;
}
static void
repodata_add_array(Repodata *data, Id handle, Id keyname, Id keytype, int entrysize)
{
int oldsize;
Id *ida, *pp;
if (handle == data->lasthandle && data->keys[data->lastkey].name == keyname && data->keys[data->lastkey].type == keytype && data->attriddatalen == data->lastdatalen)
{
/* great! just append the new data */
data->attriddata = sat_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
data->attriddatalen--; /* overwrite terminating 0 */
data->lastdatalen += entrysize;
return;
}
pp = data->structs[handle];
if (pp)
for (; *pp; pp += 2)
if (data->keys[*pp].name == keyname && data->keys[*pp].type == keytype)
break;
if (!pp || !*pp)
{
/* not found. allocate new key */
Repokey key;
key.name = keyname;
key.type = keytype;
key.size = 0;
key.storage = KEY_STORAGE_INCORE;
data->attriddata = sat_extend(data->attriddata, data->attriddatalen, entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
repodata_set(data, handle, &key, data->attriddatalen);
data->lasthandle = 0; /* next time... */
return;
}
oldsize = 0;
for (ida = data->attriddata + pp[1]; *ida; ida += entrysize)
oldsize += entrysize;
if (ida + 1 == data->attriddata + data->attriddatalen)
{
/* this was the last entry, just append it */
data->attriddata = sat_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
data->attriddatalen--; /* overwrite terminating 0 */
}
else
{
/* too bad. move to back. */
data->attriddata = sat_extend(data->attriddata, data->attriddatalen, oldsize + entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
memcpy(data->attriddata + data->attriddatalen, data->attriddata + pp[1], oldsize * sizeof(Id));
pp[1] = data->attriddatalen;
data->attriddatalen += oldsize;
}
data->lasthandle = handle;
data->lastkey = *pp;
data->lastdatalen = data->attriddatalen + entrysize + 1;
}
static inline int
checksumtype2len(Id type)
{
switch (type)
{
case REPOKEY_TYPE_MD5:
return SIZEOF_MD5;
case REPOKEY_TYPE_SHA1:
return SIZEOF_SHA1;
case REPOKEY_TYPE_SHA256:
return SIZEOF_SHA256;
default:
return 0;
}
}
void
repodata_set_bin_checksum(Repodata *data, Id handle, Id keyname, Id type,
const unsigned char *str)
{
Repokey key;
int l = checksumtype2len(type);
if (!l)
return;
key.name = keyname;
key.type = type;
key.size = 0;
key.storage = KEY_STORAGE_INCORE;
data->attrdata = sat_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
memcpy(data->attrdata + data->attrdatalen, str, l);
repodata_set(data, handle, &key, data->attrdatalen);
data->attrdatalen += l;
}
static int
hexstr2bytes(unsigned char *buf, const char *str, int buflen)
{
int i;
for (i = 0; i < buflen; i++)
{
#define c2h(c) (((c)>='0' && (c)<='9') ? ((c)-'0') \
: ((c)>='a' && (c)<='f') ? ((c)-'a'+10) \
: ((c)>='A' && (c)<='F') ? ((c)-'A'+10) \
: -1)
int v = c2h(*str);
str++;
if (v < 0)
return 0;
buf[i] = v;
v = c2h(*str);
str++;
if (v < 0)
return 0;
buf[i] = (buf[i] << 4) | v;
#undef c2h
}
return buflen;
}
void
repodata_set_checksum(Repodata *data, Id handle, Id keyname, Id type,
const char *str)
{
unsigned char buf[64];
int l = checksumtype2len(type);
if (!l)
return;
if (hexstr2bytes(buf, str, l) != l)
{
fprintf(stderr, "Invalid hex character in '%s'\n", str);
return;
}
repodata_set_bin_checksum(data, handle, keyname, type, buf);
}
const char *
repodata_chk2str(Repodata *data, Id type, const unsigned char *buf)
{
int i, l;
char *str, *s;
l = checksumtype2len(type);
if (!l)
return "";
s = str = pool_alloctmpspace(data->repo->pool, 2 * l + 1);
for (i = 0; i < l; i++)
{
unsigned char v = buf[i];
unsigned char w = v >> 4;
*s++ = w >= 10 ? w + ('a' - 10) : w + '0';
w = v & 15;
*s++ = w >= 10 ? w + ('a' - 10) : w + '0';
}
*s = 0;
return str;
}
Id
repodata_globalize_id(Repodata *data, Id id)
{
if (!data || !data->localpool)
return id;
return str2id(data->repo->pool, stringpool_id2str(&data->spool, id), 1);
}
void
repodata_add_dirnumnum(Repodata *data, Id handle, Id keyname, Id dir, Id num, Id num2)
{
assert(dir);
#if 0
fprintf(stderr, "repodata_add_dirnumnum %d %d %d %d (%d)\n", handle, dir, num, num2, data->attriddatalen);
#endif
repodata_add_array(data, handle, keyname, REPOKEY_TYPE_DIRNUMNUMARRAY, 3);
data->attriddata[data->attriddatalen++] = dir;
data->attriddata[data->attriddatalen++] = num;
data->attriddata[data->attriddatalen++] = num2;
data->attriddata[data->attriddatalen++] = 0;
}
void
repodata_add_dirstr(Repodata *data, Id handle, Id keyname, Id dir, const char *str)
{
Id stroff;
int l;
assert(dir);
l = strlen(str) + 1;
data->attrdata = sat_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
memcpy(data->attrdata + data->attrdatalen, str, l);
stroff = data->attrdatalen;
data->attrdatalen += l;
#if 0
fprintf(stderr, "repodata_add_dirstr %d %d %s (%d)\n", handle, dir, str, data->attriddatalen);
#endif
repodata_add_array(data, handle, keyname, REPOKEY_TYPE_DIRSTRARRAY, 2);
data->attriddata[data->attriddatalen++] = dir;
data->attriddata[data->attriddatalen++] = stroff;
data->attriddata[data->attriddatalen++] = 0;
}
void
repodata_add_idarray(Repodata *data, Id handle, Id keyname, Id id)
{
#if 0
fprintf(stderr, "repodata_add_idarray %d %d (%d)\n", handle, id, data->attriddatalen);
#endif
repodata_add_array(data, handle, keyname, REPOKEY_TYPE_IDARRAY, 1);
data->attriddata[data->attriddatalen++] = id;
data->attriddata[data->attriddatalen++] = 0;
}
void
repodata_add_poolstr_array(Repodata *data, Id handle, Id keyname,
const char *str)
{
Id id;
if (data->localpool)
id = stringpool_str2id(&data->spool, str, 1);
else
id = str2id(data->repo->pool, str, 1);
repodata_add_idarray(data, handle, keyname, id);
}
Id
repodata_create_struct(Repodata *data, Id handle, Id keyname)
{
Id newhandle = get_new_struct(data);
repodata_add_array(data, handle, keyname, REPOKEY_TYPE_COUNTED, 1);
data->attriddata[data->attriddatalen++] = newhandle;
data->attriddata[data->attriddatalen++] = 0;
return newhandle;
}
void
repodata_merge_attrs(Repodata *data, Id dest, Id src)
{
Id *keyp;
if (dest == src
|| !(keyp = data->structs[src < 0
? data->extraattrs[-1 - src]
: data->attrs[src]]))
return;
dest = repodata_get_handle_int(data, dest);
for (; *keyp; keyp += 2)
repodata_insert_keyid(data, dest, keyp[0], keyp[1], 0);
}
/*********************************/
/* unify with repo_write! */
#define EXTDATA_BLOCK 1023
#define SCHEMATA_BLOCK 31
#define SCHEMATADATA_BLOCK 255
struct extdata {
unsigned char *buf;
int len;
};
static void
data_addid(struct extdata *xd, Id x)
{
unsigned char *dp;
xd->buf = sat_extend(xd->buf, xd->len, 5, 1, EXTDATA_BLOCK);
dp = xd->buf + xd->len;
if (x >= (1 << 14))
{
if (x >= (1 << 28))
*dp++ = (x >> 28) | 128;
if (x >= (1 << 21))
*dp++ = (x >> 21) | 128;
*dp++ = (x >> 14) | 128;
}
if (x >= (1 << 7))
*dp++ = (x >> 7) | 128;
*dp++ = x & 127;
xd->len = dp - xd->buf;
}
static void
data_addideof(struct extdata *xd, Id x, int eof)
{
if (x >= 64)
x = (x & 63) | ((x & ~63) << 1);
data_addid(xd, (eof ? x: x | 64));
}
static void
data_addblob(struct extdata *xd, unsigned char *blob, int len)
{
xd->buf = sat_extend(xd->buf, xd->len, len, 1, EXTDATA_BLOCK);
memcpy(xd->buf + xd->len, blob, len);
xd->len += len;
}
/*********************************/
static void
addschema_prepare(Repodata *data, Id *schematacache)
{
int h, len, i;
Id *sp;
memset(schematacache, 0, 256 * sizeof(Id));
for (i = 0; i < data->nschemata; i++)
{
for (sp = data->schemadata + data->schemata[i], h = 0; *sp; len++)
h = h * 7 + *sp++;
h &= 255;
schematacache[h] = i + 1;
}
data->schemadata = sat_extend_resize(data->schemadata, data->schemadatalen, sizeof(Id), SCHEMATADATA_BLOCK);
data->schemata = sat_extend_resize(data->schemata, data->nschemata, sizeof(Id), SCHEMATA_BLOCK);
}
static Id
addschema(Repodata *data, Id *schema, Id *schematacache)
{
int h, len;
Id *sp, cid;
for (sp = schema, len = 0, h = 0; *sp; len++)
h = h * 7 + *sp++;
h &= 255;
len++;
cid = schematacache[h];
if (cid)
{
cid--;
if (!memcmp(data->schemadata + data->schemata[cid], schema, len * sizeof(Id)))
return cid;
/* cache conflict */
for (cid = 0; cid < data->nschemata; cid++)
if (!memcmp(data->schemadata + data->schemata[cid], schema, len * sizeof(Id)))
return cid;
}
/* a new one. make room. */
data->schemadata = sat_extend(data->schemadata, data->schemadatalen, len, sizeof(Id), SCHEMATADATA_BLOCK);
data->schemata = sat_extend(data->schemata, data->nschemata, 1, sizeof(Id), SCHEMATA_BLOCK);
/* add schema */
memcpy(data->schemadata + data->schemadatalen, schema, len * sizeof(Id));
data->schemata[data->nschemata] = data->schemadatalen;
data->schemadatalen += len;
schematacache[h] = data->nschemata + 1;
#if 0
fprintf(stderr, "addschema: new schema\n");
#endif
return data->nschemata++;
}
static void
repodata_serialize_key(Repodata *data, struct extdata *newincore,
struct extdata *newvincore,
Id *schema, Id *schematacache,
Repokey *key, Id val)
{
/* Otherwise we have a new value. Parse it into the internal
form. */
Id *ida;
struct extdata *xd;
unsigned int oldvincorelen = 0;
Id schemaid, *sp;
xd = newincore;
if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
{
xd = newvincore;
oldvincorelen = xd->len;
}
switch (key->type)
{
case REPOKEY_TYPE_VOID:
case REPOKEY_TYPE_CONSTANT:
case REPOKEY_TYPE_CONSTANTID:
break;
case REPOKEY_TYPE_STR:
data_addblob(xd, data->attrdata + val, strlen((char *)(data->attrdata + val)) + 1);
break;
case REPOKEY_TYPE_MD5:
data_addblob(xd, data->attrdata + val, SIZEOF_MD5);
break;
case REPOKEY_TYPE_SHA1:
data_addblob(xd, data->attrdata + val, SIZEOF_SHA1);
break;
case REPOKEY_TYPE_ID:
case REPOKEY_TYPE_NUM:
case REPOKEY_TYPE_DIR:
data_addid(xd, val);
break;
case REPOKEY_TYPE_IDARRAY:
for (ida = data->attriddata + val; *ida; ida++)
data_addideof(xd, ida[0], ida[1] ? 0 : 1);
break;
case REPOKEY_TYPE_DIRNUMNUMARRAY:
for (ida = data->attriddata + val; *ida; ida += 3)
{
data_addid(xd, ida[0]);
data_addid(xd, ida[1]);
data_addideof(xd, ida[2], ida[3] ? 0 : 1);
}
break;
case REPOKEY_TYPE_DIRSTRARRAY:
for (ida = data->attriddata + val; *ida; ida += 2)
{
data_addideof(xd, ida[0], ida[2] ? 0 : 1);
data_addblob(xd, data->attrdata + ida[1], strlen((char *)(data->attrdata + ida[1])) + 1);
}
break;
case REPOKEY_TYPE_COUNTED:
{
int num = 0;
schemaid = 0;
for (ida = data->attriddata + val; *ida; ida++)
{
#if 0
fprintf(stderr, "serialize struct %d\n", *ida);
#endif
sp = schema;
Id *kp = data->structs[*ida];
if (!kp)
continue;
num++;
for (;*kp; kp += 2)
{
#if 0
fprintf(stderr, " %s:%d\n", id2str(data->repo->pool, data->keys[*kp].name), kp[1]);
#endif
*sp++ = *kp;
}
*sp = 0;
if (!schemaid)
schemaid = addschema(data, schema, schematacache);
else if (schemaid != addschema(data, schema, schematacache))
{
fprintf(stderr, " not yet implemented: substructs with different schemas\n");
exit(1);
}
#if 0
fprintf(stderr, " schema %d\n", schemaid);
#endif
}
if (!num)
break;
data_addid(xd, num);
data_addid(xd, schemaid);
for (ida = data->attriddata + val; *ida; ida++)
{
Id *kp = data->structs[*ida];
if (!kp)
continue;
for (;*kp; kp += 2)
{
repodata_serialize_key(data, newincore, newvincore,
schema, schematacache,
data->keys + *kp, kp[1]);
}
}
break;
}
default:
fprintf(stderr, "don't know how to handle type %d\n", key->type);
exit(1);
}
if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
{
/* put offset/len in incore */
data_addid(newincore, data->lastverticaloffset + oldvincorelen);
oldvincorelen = xd->len - oldvincorelen;
data_addid(newincore, oldvincorelen);
}
}
void
repodata_internalize(Repodata *data)
{
Repokey *key;
Id entry, nentry;
Id schematacache[256];
Id schemaid, *schema, *sp, oldschema, *keyp, *seen;
unsigned char *dp, *ndp;
int newschema, oldcount;
struct extdata newincore;
struct extdata newvincore;
if (!data->attrs && !data->extraattrs)
return;
newvincore.buf = data->vincore;
newvincore.len = data->vincorelen;
schema = sat_malloc2(data->nkeys, sizeof(Id));
seen = sat_malloc2(data->nkeys, sizeof(Id));
/* Merge the data already existing (in data->schemata, ->incoredata and
friends) with the new attributes in data->attrs[]. */
nentry = data->end - data->start;
addschema_prepare(data, schematacache);
memset(&newincore, 0, sizeof(newincore));
data_addid(&newincore, 0);
if (!data->attrs)
nentry = 0;
for (entry = data->extraattrs ? -data->nextra : 0; entry < nentry; entry++)
{
Id handle;
memset(seen, 0, data->nkeys * sizeof(Id));
sp = schema;
dp = entry2data(data, entry);
if (data->incoredata)
dp = data_read_id(dp, &oldschema);
else
oldschema = 0;
#if 0
fprintf(stderr, "oldschema %d\n", oldschema);
fprintf(stderr, "schemata %d\n", data->schemata[oldschema]);
fprintf(stderr, "schemadata %p\n", data->schemadata);
#endif
/* seen: -1: old data 0: skipped >0: id + 1 */
newschema = 0;
oldcount = 0;
for (keyp = data->schemadata + data->schemata[oldschema]; *keyp; keyp++)
{
if (seen[*keyp])
{
fprintf(stderr, "Inconsistent old data (key occured twice).\n");
exit(1);
}
seen[*keyp] = -1;
*sp++ = *keyp;
oldcount++;
}
handle = entry < 0 ? data->extraattrs[-1 - entry] : data->attrs[entry];
keyp = data->structs[handle];
if (keyp)
for (; *keyp; keyp += 2)
{
if (!seen[*keyp])
{
newschema = 1;
*sp++ = *keyp;
}
seen[*keyp] = keyp[1] + 1;
}
*sp++ = 0;
if (newschema)
/* Ideally we'd like to sort the new schema here, to ensure
schema equality independend of the ordering. We can't do that
yet. For once see below (old ids need to come before new ids).
An additional difficulty is that we also need to move
the values with the keys. */
schemaid = addschema(data, schema, schematacache);
else
schemaid = oldschema;
/* Now create data blob. We walk through the (possibly new) schema
and either copy over old data, or insert the new. */
/* XXX Here we rely on the fact that the (new) schema has the form
o1 o2 o3 o4 ... | n1 n2 n3 ...
(oX being the old keyids (possibly overwritten), and nX being
the new keyids). This rules out sorting the keyids in order
to ensure a small schema count. */
if (entry < 0)
data->extraoffset[-1 - entry] = newincore.len;
else
data->incoreoffset[entry] = newincore.len;
data_addid(&newincore, schemaid);
for (keyp = data->schemadata + data->schemata[schemaid]; *keyp; keyp++)
{
key = data->keys + *keyp;
#if 0
fprintf(stderr, "internalize %d:%s:%s\n", entry, id2str(data->repo->pool, key->name), id2str(data->repo->pool, key->type));
#endif
ndp = dp;
if (oldcount)
{
/* Skip the data associated with this old key. */
if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
{
ndp = data_skip(dp, REPOKEY_TYPE_ID);
ndp = data_skip(ndp, REPOKEY_TYPE_ID);
}
else if (key->storage == KEY_STORAGE_INCORE)
ndp = data_skip_recursive(data, dp, key);
oldcount--;
}
if (seen[*keyp] == -1)
{
/* If this key was an old one _and_ was not overwritten with
a different value copy over the old value (we skipped it
above). */
if (dp != ndp)
data_addblob(&newincore, dp, ndp - dp);
seen[*keyp] = 0;
}
else if (seen[*keyp])
{
/* Otherwise we have a new value. Parse it into the internal
form. */
repodata_serialize_key(data, &newincore, &newvincore,
schema, schematacache,
key, seen[*keyp] - 1);
}
dp = ndp;
}
if (data->structs[handle])
data->structs[handle] = sat_free(data->structs[handle]);
}
for (entry = 0; entry < data->nstructs; entry++)
if (data->structs[entry])
sat_free(data->structs[entry]);
data->structs = sat_free(data->structs);
data->lasthandle = 0;
data->lastkey = 0;
data->lastdatalen = 0;
sat_free(schema);
sat_free(seen);
sat_free(data->incoredata);
data->incoredata = newincore.buf;
data->incoredatalen = newincore.len;
data->incoredatafree = 0;
sat_free(data->vincore);
data->vincore = newvincore.buf;
data->vincorelen = newvincore.len;
data->attrs = sat_free(data->attrs);
data->extraattrs = sat_free(data->extraattrs);
data->attrdata = sat_free(data->attrdata);
data->attriddata = sat_free(data->attriddata);
data->attrdatalen = 0;
data->attriddatalen = 0;
}
Id
repodata_str2dir(Repodata *data, const char *dir, int create)
{
Id id, parent;
const char *dire;
parent = 0;
while (*dir == '/' && dir[1] == '/')
dir++;
if (*dir == '/' && !dir[1])
return 1;
while (*dir)
{
dire = strchrnul(dir, '/');
if (data->localpool)
id = stringpool_strn2id(&data->spool, dir, dire - dir, create);
else
id = strn2id(data->repo->pool, dir, dire - dir, create);
if (!id)
return 0;
parent = dirpool_add_dir(&data->dirpool, parent, id, create);
if (!parent)
return 0;
if (!*dire)
break;
dir = dire + 1;
while (*dir == '/')
dir++;
}
return parent;
}
const char *
repodata_dir2str(Repodata *data, Id did, const char *suf)
{
Pool *pool = data->repo->pool;
int l = 0;
Id parent, comp;
const char *comps;
char *p;
if (!did)
return suf ? suf : "";
parent = did;
while (parent)
{
comp = dirpool_compid(&data->dirpool, parent);
comps = stringpool_id2str(data->localpool ? &data->spool : &pool->ss, comp);
l += strlen(comps);
parent = dirpool_parent(&data->dirpool, parent);
if (parent)
l++;
}
if (suf)
l += strlen(suf) + 1;
p = pool_alloctmpspace(pool, l + 1) + l;
*p = 0;
if (suf)
{
p -= strlen(suf);
strcpy(p, suf);
*--p = '/';
}
parent = did;
while (parent)
{
comp = dirpool_compid(&data->dirpool, parent);
comps = stringpool_id2str(data->localpool ? &data->spool : &pool->ss, comp);
l = strlen(comps);
p -= l;
strncpy(p, comps, l);
parent = dirpool_parent(&data->dirpool, parent);
if (parent)
*--p = '/';
}
return p;
}
unsigned int
repodata_compress_page(unsigned char *page, unsigned int len, unsigned char *cpage, unsigned int max)
{
return compress_buf(page, len, cpage, max);
}
#define SOLV_ERROR_EOF 3
static inline unsigned int
read_u32(FILE *fp)
{
int c, i;
unsigned int x = 0;
for (i = 0; i < 4; i++)
{
c = getc(fp);
if (c == EOF)
return 0;
x = (x << 8) | c;
}
return x;
}
#define SOLV_ERROR_EOF 3
#define SOLV_ERROR_CORRUPT 6
/* Try to either setup on-demand paging (using FP as backing
file), or in case that doesn't work (FP not seekable) slurps in
all pages and deactivates paging. */
void
repodata_read_or_setup_pages(Repodata *data, unsigned int pagesz, unsigned int blobsz)
{
FILE *fp = data->fp;
unsigned int npages;
unsigned int i;
unsigned int can_seek;
long cur_file_ofs;
unsigned char buf[BLOB_PAGESIZE];
if (pagesz != BLOB_PAGESIZE)
{
/* We could handle this by slurping in everything. */
data->error = SOLV_ERROR_CORRUPT;
return;
}
can_seek = 1;
if ((cur_file_ofs = ftell(fp)) < 0)
can_seek = 0;
clearerr(fp);
if (can_seek)
data->pagefd = dup(fileno(fp));
if (data->pagefd == -1)
can_seek = 0;
#ifdef DEBUG_PAGING
fprintf (stderr, "can %sseek\n", can_seek ? "" : "NOT ");
#endif
npages = (blobsz + BLOB_PAGESIZE - 1) / BLOB_PAGESIZE;
data->num_pages = npages;
data->pages = sat_malloc2(npages, sizeof(data->pages[0]));
/* If we can't seek on our input we have to slurp in everything. */
if (!can_seek)
data->blob_store = sat_malloc(npages * BLOB_PAGESIZE);
for (i = 0; i < npages; i++)
{
unsigned int in_len = read_u32(fp);
unsigned int compressed = in_len & 1;
Attrblobpage *p = data->pages + i;
in_len >>= 1;
#ifdef DEBUG_PAGING
fprintf (stderr, "page %d: len %d (%scompressed)\n",
i, in_len, compressed ? "" : "not ");
#endif
if (can_seek)
{
cur_file_ofs += 4;
p->mapped_at = -1;
p->file_offset = cur_file_ofs;
p->file_size = in_len * 2 + compressed;
if (fseek(fp, in_len, SEEK_CUR) < 0)
{
perror ("fseek");
fprintf (stderr, "can't seek after we thought we can\n");
/* We can't fall back to non-seeking behaviour as we already
read over some data pages without storing them away. */
data->error = SOLV_ERROR_EOF;
close(data->pagefd);
data->pagefd = -1;
return;
}
cur_file_ofs += in_len;
}
else
{
unsigned int out_len;
void *dest = data->blob_store + i * BLOB_PAGESIZE;
p->mapped_at = i * BLOB_PAGESIZE;
p->file_offset = 0;
p->file_size = 0;
/* We can't seek, so suck everything in. */
if (fread(compressed ? buf : dest, in_len, 1, fp) != 1)
{
perror("fread");
data->error = SOLV_ERROR_EOF;
return;
}
if (compressed)
{
out_len = unchecked_decompress_buf(buf, in_len, dest, BLOB_PAGESIZE);
if (out_len != BLOB_PAGESIZE && i < npages - 1)
{
data->error = SOLV_ERROR_CORRUPT;
return;
}
}
}
}
}
void
repodata_disable_paging(Repodata *data)
{
if (maybe_load_repodata(data, 0)
&& data->num_pages)
load_page_range (data, 0, data->num_pages - 1);
}
/*
vim:cinoptions={.5s,g0,p5,t0,(0,^-0.5s,n-0.5s:tw=78:cindent:sw=4:
*/
|