krill 0.16.0

Resource Public Key Infrastructure (RPKI) daemon
Documentation
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
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
//! Management of content for RRDP.

use std::{error, fmt, fs, io, mem};
use std::collections::{HashMap, VecDeque};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use chrono::Duration;
use log::{debug, error, info, warn};
use rpki::uri;
use rpki::ca::publication;
use rpki::ca::idexchange::PublisherHandle;
use rpki::ca::publication::Base64;
use rpki::repository::manifest::Manifest;
use rpki::repository::x509::Time;
use rpki::rrdp::{DeltaInfo, Hash, NotificationFile, SnapshotInfo};
use rpki::xml::decode::Name;
use serde::de;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use uuid::Uuid;
use crate::api::admin::PublishedFile;
use crate::api::pubd::{PublisherManifestStats, PublisherStats};
use crate::commons::file;
use crate::commons::KrillResult;
use crate::commons::error::{Error, KrillIoError};
use crate::constants::{
    REPOSITORY_RRDP_ARCHIVE_DIR, REPOSITORY_RRDP_DIR,
    RRDP_FIRST_SERIAL,
};
use crate::config::RrdpUpdatesConfig;


//------------ RRDP name definitions -----------------------------------------

const VERSION: &str = "1";
const NS: &str = "http://www.ripe.net/rpki/rrdp";

const SNAPSHOT: Name = Name::unqualified(b"snapshot");
const DELTA: Name = Name::unqualified(b"delta");
const PUBLISH: Name = Name::unqualified(b"publish");
const WITHDRAW: Name = Name::unqualified(b"withdraw");


//------------ RrdpServer ----------------------------------------------------

/// The RRDP server used by a repository instance.
///
/// This isn’t the actual server but creates the data to be served by an
/// HTTP server.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RrdpServer {
    /// The base URI for RRDP files.
    rrdp_base_uri: uri::Https,

    /// The directory where RRDP files will be published.
    rrdp_base_dir: PathBuf,

    /// The directory where RRDP files will be archived.
    rrdp_archive_dir: PathBuf,

    /// The current RRDP session.
    session: RrdpSession,

    /// The current serial number within the session.
    serial: u64,

    /// The time we last updated our data.
    last_update: Time,

    /// The data of the current snapshot.
    snapshot: SnapshotData,

    /// The deltas we currently have.
    deltas: VecDeque<DeltaData>,

    /// The elements staged for publishing by each publisher.
    #[serde(default)]
    staged_elements: HashMap<PublisherHandle, StagedElements>,
}

impl RrdpServer {
    /// Create a new instance.
    ///
    /// Intended to be used by migration code only and therefore
    /// marked as deprecated.
    #[deprecated]
    #[allow(clippy::too_many_arguments)]
    pub(super) fn new(
        rrdp_base_uri: uri::Https,
        rrdp_base_dir: PathBuf,
        rrdp_archive_dir: PathBuf,
        session: RrdpSession,
        serial: u64,
        last_update: Time,
        snapshot: SnapshotData,
        deltas: VecDeque<DeltaData>,
        staged_elements: HashMap<PublisherHandle, StagedElements>,
    ) -> Self {
        RrdpServer {
            rrdp_base_uri,
            rrdp_base_dir,
            rrdp_archive_dir,
            session,
            serial,
            last_update,
            snapshot,
            deltas,
            staged_elements,
        }
    }

    /// Creates an RRDP server.
    pub fn create(
        rrdp_base_uri: uri::Https,
        repo_dir: &Path,
        session: RrdpSession,
    ) -> Self {
        let mut rrdp_base_dir = repo_dir.to_path_buf();
        rrdp_base_dir.push(REPOSITORY_RRDP_DIR);

        let mut rrdp_archive_dir = repo_dir.to_path_buf();
        rrdp_archive_dir.push(REPOSITORY_RRDP_ARCHIVE_DIR);

        let serial = RRDP_FIRST_SERIAL;
        let last_update = Time::now();
        let snapshot = SnapshotData::empty();

        RrdpServer {
            rrdp_base_uri,
            rrdp_base_dir,
            rrdp_archive_dir,
            session,
            serial,
            last_update,
            snapshot,
            deltas: VecDeque::new(),
            staged_elements: HashMap::new(),
        }
    }

    /// Removes all files created by the RRDP server.
    pub fn clear(&self) {
        let _ = fs::remove_dir_all(&self.rrdp_base_dir);
        let _ = fs::remove_dir_all(&self.rrdp_archive_dir);
    }

    /// Returns the base of the URI of any RRDP file.
    pub fn rrdp_base_uri(&self) -> &uri::Https {
        &self.rrdp_base_uri
    }

    /// Returns the current RRDP session.
    pub fn session(&self) -> RrdpSession {
        self.session
    }

    /// Returns the current serial number within the current session.
    pub fn serial(&self) -> u64 {
        self.serial
    }

    /// Returns the time of the last update.
    pub fn last_update(&self) -> Time {
        self.last_update
    }

    /// Returns a reference to the snapshot data.
    pub fn snapshot(&self) -> &SnapshotData {
        &self.snapshot
    }

    /// Converts the RRDP path portion of a HTTP request URI to a path.
    ///
    /// The `path` should contain everything after the `/rrdp/` portion of
    /// the URI’s path. If the path is in principle valid, i.e., could
    /// represent an RRDP resource generated by this RRDP sever, the method
    /// will return a file system path representing this path. This does not
    /// mean there will actually be a file there. The file may have been
    /// deleted or may have never existed at all. This is necessary since
    /// the RRDP server doesn’t track past files, only the currently valid
    /// set of resources.
    ///
    /// If the path is definitely not valid, returns `None`. This should
    /// probably be translated into a 404 Not Found response.
    pub fn resolve_request_path(
        &self, path: &str
    ) -> Option<PathBuf> {
        Self::is_request_path_valid(path).map(|_| {
            self.rrdp_base_dir.join(path)
        })
    }

    /// Check if a request path is in principle valid.
    ///
    /// This returns an option so we can use the question mark operator in
    /// the implementation.
    fn is_request_path_valid(path: &str) -> Option<()> {
        // Literal "notification.xml" is fine.
        if path == "notification.xml" {
            return Some(())
        }

        // All other paths are session, serial, random, and then either
        // "snapshot.xml" or "delta.xml"
        let mut path = path.split('/');

        // Check that the three interior items only contains letters, numbers,
        // and hyphens. Technically we only do lower case letters, but since
        // we are also using the Display impls of stuff, that may quietly
        // change.
        for item in [path.next()?, path.next()?, path.next()?] {
            for &ch in item.as_bytes() {
                if !ch.is_ascii_alphanumeric() && ch != b'-' {
                    return None
                }
            }
        }

        // Next is the file name.
        match path.next()? {
            "snapshot.xml" | "delta.xml" => { }
            _ => return None,
        }

        // And then we need to be done.
        if path.next().is_some() {
            return None
        }

        Some(())
    }

    /// Lists all known publishers based on current objects and staged deltas.
    pub fn publishers(&self) -> Vec<PublisherHandle> {
        let publisher_current_objects =
            self.snapshot.publishers_current_objects();

        let mut publishers: Vec<_> =
            publisher_current_objects.keys().cloned().collect();

        for staged_publisher in self.staged_elements.keys() {
            if !publisher_current_objects.contains_key(staged_publisher) {
                publishers.push(staged_publisher.clone())
            }
        }

        publishers
    }

    /// Returns the staged elements for a publisher.
    pub fn get_publisher_staged(
        &self, publisher: &PublisherHandle
    ) -> Option<&StagedElements> {
        self.staged_elements.get(publisher)
    }

    /// Returns a RRDP session reset.
    ///
    /// This will contain a copy of the current snapshot.
    pub fn reset_session(&self) -> RrdpSessionReset {
        let last_update = Time::now();
        let session = RrdpSession::random();
        let snapshot = self.snapshot.clone_with_new_random();

        RrdpSessionReset {
            last_update,
            snapshot,
            session,
        }
    }

    /// Applies the data from an RRDP session reset.
    pub fn apply_session_reset(&mut self, reset: RrdpSessionReset) {
        self.snapshot = reset.snapshot;
        self.session = reset.session;
        self.last_update = reset.last_update;
        self.serial = RRDP_FIRST_SERIAL;
        self.deltas = VecDeque::new();
    }

    /// Applies a change that a publisher was added.
    ///
    /// Cannot fail, so this is made idempotent. No publisher is added
    /// if the publisher already exists. This cannot happen in practice,
    /// because the change would not be created in that case: i.e. the
    /// command to add the publisher would have failed.
    pub fn apply_publisher_added(&mut self, publisher: PublisherHandle) {
        self.snapshot.apply_publisher_added(publisher);
    }

    /// Apples a change that a publisher was removed.
    pub fn apply_publisher_removed(&mut self, publisher: &PublisherHandle) {
        self.snapshot.apply_publisher_removed(publisher);
    }

    /// Applies staged delta elements.
    pub fn apply_rrdp_staged(
        &mut self,
        publisher: PublisherHandle,
        elements: DeltaElements,
    ) {
        self.staged_elements.entry(
            publisher
        ).or_default().merge_new_elements(elements);
    }

    /// Applies the data from an RrdpUpdated change.
    pub fn apply_rrdp_updated(&mut self, update: RrdpUpdated) {
        self.serial += 1;

        let mut staged_elements = HashMap::default();
        mem::swap(&mut self.staged_elements, &mut staged_elements);

        let mut rrdp_delta_elements = DeltaElements::default();
        for (publisher, staged_elements) in staged_elements {
            let delta: DeltaElements = staged_elements.into();
            // update snapshot
            self.snapshot.apply_delta(&publisher, delta.clone());

            // extend next RRDP delta with elements for this publisher.
            rrdp_delta_elements.append(delta);
        }

        let delta = DeltaData::new(
            self.serial,
            update.time,
            update.random,
            rrdp_delta_elements,
        );

        self.deltas.truncate(update.deltas_truncate);
        self.deltas.push_front(delta);
        self.deltas_truncate_size();

        self.last_update = update.time;
    }

    /// Checks whether an RRDP update is needed
    pub fn update_rrdp_needed(
        &self,
        rrdp_updates_config: RrdpUpdatesConfig,
    ) -> RrdpUpdateNeeded {
        // Check if there are any staged elements entries with staged
        // elements. A simple .is_empty() on the map won't do because there
        // could be (only) entries for publishers containing an empty set of
        // staged elements.
        if self.staged_elements.values().any(|el| !el.0.is_empty()) {
            // There is staged content. Check if it should be published now,
            // or later.
            let interval = Duration::seconds(
                rrdp_updates_config.rrdp_delta_interval_min_seconds.into(),
            );
            let next_update_time = self.last_update + interval;
            if next_update_time > Time::now() {
                debug!(
                    "RRDP update is delayed to: {}",
                    next_update_time.to_rfc3339()
                );
                RrdpUpdateNeeded::Later(next_update_time)
            } else {
                debug!("RRDP update is needed");
                RrdpUpdateNeeded::Yes
            }
        } else {
            debug!("No RRDP update is needed, there are no staged changes");
            RrdpUpdateNeeded::No
        }
    }

    /// Updates the RRDP server with the staged delta elements.
    pub fn update_rrdp(
        &self,
        rrdp_updates_config: RrdpUpdatesConfig,
    ) -> KrillResult<RrdpUpdated> {
        let time = Time::now();
        let random = RrdpFileRandom::default();

        let deltas_truncate =
            self.find_deltas_truncate_age(rrdp_updates_config);

        Ok(RrdpUpdated {
            time,
            random,
            deltas_truncate,
        })
    }

    /// Truncate excessive deltas based on size.
    ///
    /// This is done after applying an RrdpUpdate because the outcome is
    /// deterministic. Compared to truncating the deltas based on age and
    /// number, because *that* depends on when the update was generated,
    /// and what the RrdpUpdatesConfig was set to at the time.
    fn deltas_truncate_size(&mut self) {
        let snapshot_size = self.snapshot().size_approx();
        let mut total_deltas_size = 0;
        let mut keep = 0;

        for delta in &self.deltas {
            total_deltas_size += delta.elements().size_approx();
            if total_deltas_size > snapshot_size {
                // never keep more than the size of the snapshot
                break;
            } else {
                keep += 1;
            }
        }

        self.deltas.truncate(keep);
    }

    /// Returns the position to truncate excessive deltas.
    ///
    ///  - always keep 'rrdp_delta_files_min_nr' files
    ///  - always keep 'rrdp_delta_files_min_seconds' files
    ///  - beyond this:
    ///     - never keep more than 'rrdp_delta_files_max_nr'
    ///     - never keep older than 'rrdp_delta_files_max_seconds'
    ///     - keep the others
    fn find_deltas_truncate_age(
        &self,
        rrdp_updates_config: RrdpUpdatesConfig,
    ) -> usize {
        // We will keep the new delta - not yet added to this.
        let mut keep = 0;

        let min_nr = rrdp_updates_config.rrdp_delta_files_min_nr;
        let min_secs = rrdp_updates_config.rrdp_delta_files_min_seconds;
        let max_nr = rrdp_updates_config.rrdp_delta_files_max_nr;
        let max_secs = rrdp_updates_config.rrdp_delta_files_max_seconds;

        for delta in &self.deltas {
            if keep < min_nr || delta.younger_than_seconds(min_secs.into()) {
                // always keep 'rrdp_delta_files_min_nr' files
                //    we need < min_nr because we will add the new delta later
                // always keep 'rrdp_delta_files_min_seconds' file
                keep += 1
            } else if keep == max_nr - 1
                || delta.older_than_seconds(max_secs.into())
            {
                // never keep more than 'rrdp_delta_files_max_nr'
                //    we need max_nr -1 because we will add the new new delta
                // later never keep older than
                // 'rrdp_delta_files_max_seconds'
                break;
            } else {
                // keep the remainder
                keep += 1;
            }
        }

        keep
    }

    /// Writes the (missing) RRDP files to disk.
    ///
    /// Also removes the files no longer referenced in the notification file.
    pub fn update_rrdp_files(
        &self,
        rrdp_updates_config: RrdpUpdatesConfig,
    ) -> Result<(), Error> {
        // Get the current notification file from disk, if it exists, so
        // we can determine which (new) snapshot and delta files to write,
        // and which old snapshot and delta files may be removed.
        debug!(
            "Write updated RRDP state to disk - \n
            if there are any updates that is."
        );

        // Get the current notification file - as long as it's present and can
        // be parsed. If it cannot be parsed we just ignore it. I.e. we will
        // generate all current files in that case.
        let old_notification_opt: Option<NotificationFile> = file::read(
            &self.notification_path()
        ).ok().and_then(|bytes| {
            rpki::rrdp::NotificationFile::parse(bytes.as_ref()).ok()
        });

        if
            let Some(old_notification) = old_notification_opt.as_ref()
            && old_notification.serial() == self.serial
            && old_notification.session_id() == self.session.uuid()
        {
            debug!(
                "Existing notification file matches current session \
                 and serial. Nothing to write."
            );
            return Ok(());
        }

        let deltas = self.write_delta_files(old_notification_opt)?;
        let snapshot = self.write_snapshot_file()?;
        self.write_notification_file(snapshot, deltas)?;

        // clean up under the base dir:
        self.cleanup_old_rrdp_files(rrdp_updates_config)
    }

    /// Writes the missing delta files.
    fn write_delta_files(
        &self,
        old_notification_opt: Option<NotificationFile>,
    ) -> KrillResult<Vec<DeltaInfo>> {
        // Find existing deltas in current file, if present and still
        // applicable:
        // - there is a notification that can be parsed
        // - session did not change
        // - deltas have an overlap with current deltas (otherwise just
        //   regenerate new deltas)
        //
        // We will assume that files for deltas still exist on disk and were
        // not changed, so we will not regenerate them.
        //
        // NOTE: if both session and serial remain unchanged we just return
        // with Ok(()). There is no work.
        let mut deltas_from_old_notification = match old_notification_opt {
            None => {
                debug!("No old notification file found");
                vec![]
            }
            Some(mut old_notification) => {
                if old_notification.session_id()
                        == self.session.uuid()
                {
                    // Sort the deltas from lowest serial up, and make
                    // sure that there are no gaps.
                    if old_notification.sort_and_verify_deltas(None) {
                        debug!(
                            "Found existing notification file for \
                             current session with deltas."
                        );
                        old_notification.deltas().to_vec()
                    }
                    else {
                        debug!(
                            "Found existing notification file with \
                             incomplete deltas, will regenerate files."
                        );
                        vec![]
                    }
                }
                else {
                    debug!(
                        "Existing notification file was for different \
                        session, will regenerate all files."
                    );
                    vec![]
                }
            }
        };

        // Go over the deltas we found and discard any delta with a serial
        // that we no longer kept. The deltas in the RrdpServer are
        // sorted from highest to lowest serial (to make it easier to
        // truncate).
        if let Some(last) = self.deltas.back() {
            // Only keep deltas are still kept.
            deltas_from_old_notification .retain(|delta| {
                delta.serial() >= last.serial()
            });
        }
        else if !deltas_from_old_notification.is_empty() {
            // We would expect the existing deltas to be empty as well in this
            // case. But in any case, wiping them will ensure we
            // generate a new sane NotificationFile
            deltas_from_old_notification = vec![];
        }

        // Write new delta files and add their DeltaInfo to the list to
        // include in the new notification file. I.e. skip deltas that
        // are still included in the curated list we got from the old
        // notification.
        let last_written_serial = deltas_from_old_notification.last();
        let mut deltas = vec![];
        for delta in &self.deltas {
            if
                let Some(last) = last_written_serial
                && delta.serial() <= last.serial()
            {
                // Already included. We can skip this and assume that it
                // was written to disk before.
                // And no one went in and messed with it..
                debug!(
                    "Skip writing delta for serial {}. \
                    File should exist.",
                    delta.serial()
                );
                continue;
            }
            // New delta, write it and add its distinctiveness to deltas
            // (DeltaInfo vec) to include in the notification file
            // that we will write.
            let path = delta.path(
                self.session, delta.serial(), &self.rrdp_base_dir
            );
            let uri = delta.uri(
                self.session, delta.serial(), &self.rrdp_base_uri
            );
            let xml_bytes = delta.xml(self.session, delta.serial());
            let hash = Hash::from_data(xml_bytes.as_slice());

            debug!("Write delta file to: {}", path.to_string_lossy());
            file::save(&xml_bytes, &path)?;

            deltas.push(DeltaInfo::new(delta.serial(), uri, hash));
        }

        // Reverse the order of the (old) deltas so that it also goes high to
        // low, and we can get the new complete list to include in the
        // notification file.
        deltas_from_old_notification.reverse();
        deltas.append(&mut deltas_from_old_notification);

        Ok(deltas)
    }

    /// Writes a new snapshot file.
    fn write_snapshot_file(&self) -> KrillResult<SnapshotInfo> {
        let path = self.snapshot().path(
            self.session, self.serial, &self.rrdp_base_dir,
        );
        let uri = self.snapshot().uri(
            self.session, self.serial, &self.rrdp_base_uri,
        );
        let xml_bytes = self.snapshot().xml(self.session, self.serial);
        let hash = Hash::from_data(&xml_bytes);

        debug!("Write snapshot file to: {}", path.to_string_lossy());
        file::save(&xml_bytes, &path)?;

        Ok(SnapshotInfo::new(uri, hash))
    }

    /// Writes the new notification file.
    fn write_notification_file(
        &self, snapshot: SnapshotInfo, deltas: Vec<DeltaInfo>,
    ) -> KrillResult<()> {
        // Write new notification file to new file first.
        // Prevent that half-overwritten files are served.
        let notification = NotificationFile::new(
            self.session.uuid(), self.serial, snapshot, deltas,
        );
        let notification_path_new = self.notification_path_new();
        let mut notification_file_new =
            file::create_file_with_path(&notification_path_new)?;
        notification
            .write_xml(&mut notification_file_new)
            .map_err(|e| {
                KrillIoError::new(
                    format!(
                        "could not write new notification file to {}",
                        notification_path_new.to_string_lossy()
                    ),
                    e,
                )
            })?;

        // Rename the new file so it becomes current.
        let notification_path = self.notification_path();
        fs::rename(&notification_path_new, &notification_path).map_err(
            |e| {
                KrillIoError::new(
                    format!(
                    "Could not rename notification file from '{}' to '{}'",
                    notification_path_new.to_string_lossy(),
                    notification_path.to_string_lossy()
                ),
                    e,
                )
            },
        )?;

        Ok(())
    }

    /// Removes old unreferences RRDP delta file.
    fn cleanup_old_rrdp_files(
        &self,
        rrdp_updates_config: RrdpUpdatesConfig,
    ) -> KrillResult<()> {
        // - old session dirs
        for entry in fs::read_dir(&self.rrdp_base_dir).map_err(|e| {
            KrillIoError::new(
                format!(
                    "Could not read RRDP base directory '{}'",
                    self.rrdp_base_dir.to_string_lossy()
                ),
                e,
            )
        })? {
            let entry = entry.map_err(|e| {
                KrillIoError::new(
                    format!(
                        "Could not read entry in RRDP base directory '{}'",
                        self.rrdp_base_dir.to_string_lossy()
                    ),
                    e,
                )
            })?;
            if self.session.to_string() == entry.file_name().to_string_lossy()
            {
                continue;
            } else {
                let path = entry.path();
                if path.is_dir() {
                    let _best_effort_rm = fs::remove_dir_all(path);
                }
            }
        }

        // clean up under the current session
        let session_dir = self.rrdp_base_dir.join(self.session.to_string());

        // Get the delta range to keep. We use 0 as a special value, because
        // it is never used for deltas: i.e. no delta dirs will match
        // if our delta list is empty.
        let lowest_delta =
            self.deltas.back().map(|delta| delta.serial()).unwrap_or(0);
        let highest_delta =
            self.deltas.front().map(|delta| delta.serial()).unwrap_or(0);

        for entry in fs::read_dir(&session_dir).map_err(|e| {
            KrillIoError::new(
                format!(
                    "Could not read RRDP session directory '{}'",
                    session_dir.to_string_lossy()
                ),
                e,
            )
        })? {
            let entry = entry.map_err(|e| {
                KrillIoError::new(
                    format!(
                        "Could not read entry in RRDP session directory '{}'",
                        session_dir.to_string_lossy()
                    ),
                    e,
                )
            })?;
            let path = entry.path();

            // remove any dir or file that is:
            // - not a number
            // - a number that is higher than the current serial
            // - a number that is lower than the last delta (if set)
            if let Ok(serial) =
                u64::from_str(entry.file_name().to_string_lossy().as_ref())
            {
                // Skip the current serial
                if serial == self.serial {
                    continue;
                // Clean up old serial dirs once deltas are out of scope
                } else if serial < lowest_delta || serial > highest_delta {
                    if rrdp_updates_config.rrdp_files_archive {
                        // If archiving is enabled, then move these
                        // directories under the archive base

                        let mut dest = self.rrdp_archive_dir.clone();
                        dest.push(self.session.to_string());
                        dest.push(format!("{serial}"));

                        info!(
                            "Archiving RRDP serial '{}' to '{}",
                            serial,
                            dest.to_string_lossy()
                        );
                        let _ = fs::create_dir_all(&dest);
                        let _ = fs::rename(path, dest);
                    } else if path.is_dir() {
                        let _best_effort_rm = fs::remove_dir_all(path);
                    } else {
                        let _best_effort_rm = fs::remove_file(path);
                    }
                // We still need this old serial dir for the delta, but may
                // not need the snapshot in it unless
                // archiving is enabled.. in that case leave them and move
                // them when the complete serial dir goes out
                // of scope above.
                } else if !rrdp_updates_config.rrdp_files_archive {
                    // If the there is a snapshot file do a best effort
                    // removal. It shares the same
                    // random dir as the delta that we still need to keep for
                    // this serial, so we just remove the
                    // file and leave its parent directory in place.
                    if
                        let Ok(Some(snapshot_file_to_remove)) =
                            Self::session_dir_snapshot(&session_dir, serial)
                        && let Err(e) =
                            fs::remove_file(&snapshot_file_to_remove)
                    {
                        warn!(
                            "Could not delete snapshot file '{}'. \
                             Error was: {}",
                            snapshot_file_to_remove.to_string_lossy(),
                            e
                        );
                    }
                } else {
                    // archiving was enabled, keep the old snapshot file until
                    // the directory is archived
                }
            } else {
                // clean up dirs or files under the base dir which are not
                // sessions
                warn!(
                    "Found unexpected file or dir in RRDP repository - \
                     will try to remove: {}",
                    path.to_string_lossy()
                );
                if path.is_dir() {
                    let _best_effort_rm = fs::remove_dir_all(path);
                } else {
                    let _best_effort_rm = fs::remove_file(path);
                }
            }
        }

        Ok(())
    }
}

/// rrdp paths and uris
impl RrdpServer {
    fn notification_path_new(&self) -> PathBuf {
        let mut path = self.rrdp_base_dir.clone();
        path.push("new-notification.xml");
        path
    }

    fn notification_path(&self) -> PathBuf {
        let mut path = self.rrdp_base_dir.clone();
        path.push("notification.xml");
        path
    }

    pub fn session_dir_snapshot(
        session_path: &Path,
        serial: u64,
    ) -> KrillResult<Option<PathBuf>> {
        Self::find_in_serial_dir(session_path, serial, "snapshot.xml")
    }

    /// Expects files (like delta.xml or snapshot.xml) under dir structure
    /// like: `<session_path>/<serial>/<some random>/<filename>`.
    pub fn find_in_serial_dir(
        session_path: &Path,
        serial: u64,
        filename: &str,
    ) -> KrillResult<Option<PathBuf>> {
        let serial_dir = session_path.join(serial.to_string());
        if let Ok(randoms) = fs::read_dir(&serial_dir) {
            for entry in randoms {
                let entry = entry.map_err(|e| {
                    Error::io_error_with_context(
                        format!(
                            "Could not open directory entry under RRDP directory {}",
                            serial_dir.to_string_lossy()
                        ),
                        e,
                    )
                })?;
                if let Ok(files) = fs::read_dir(entry.path()) {
                    for file in files {
                        let file = file.map_err(|e| {
                            Error::io_error_with_context(
                                format!(
                                    "Could not open directory entry under RRDP directory {}",
                                    entry.path().to_string_lossy()
                                ),
                                e,
                            )
                        })?;
                        if file.file_name().to_string_lossy() == filename {
                            return Ok(Some(file.path()));
                        }
                    }
                }
            }
        }
        Ok(None)
    }
}


//------------ RrdpUpdateNeeded ----------------------------------------------

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RrdpUpdateNeeded {
    Yes,
    No,
    Later(Time),
}


//------------ RrdpSessionReset ----------------------------------------------

//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RrdpSessionReset {
    pub last_update: Time,
    pub session: RrdpSession,
    pub snapshot: SnapshotData,
}


//------------ RrdpUpdated ---------------------------------------------------

//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RrdpUpdated {
    pub time: Time,
    pub random: RrdpFileRandom,
    pub deltas_truncate: usize,
}


//------------ RrdpSession ---------------------------------------------------

/// An RRDP session.
///
/// A session is identified by a UUID. By default, a new session will be
/// created with a random V4 UUID.
///
//  *Warning:* This type is used in stored state.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct RrdpSession(Uuid);

impl RrdpSession {
    /// Creates a new session with a random identifier.
    pub fn random() -> Self {
        Self::default()
    }

    /// Creates a session from a UUID.
    pub fn from_uuid(uuid: Uuid) -> Self {
        Self(uuid)
    }

    /// Returns a reference to the session’s UUID.
    pub fn uuid(&self) -> Uuid {
        self.0
    }
}

//--- Default

impl Default for RrdpSession {
    fn default() -> Self {
        RrdpSession(Uuid::new_v4())
    }
}


//--- Display

impl fmt::Display for RrdpSession {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.0.hyphenated())
    }
}


//--- Deserialize and Serialize

impl<'de> Deserialize<'de> for RrdpSession {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let string = String::deserialize(deserializer)?;
        let uuid = Uuid::parse_str(&string).map_err(de::Error::custom)?;

        Ok(RrdpSession(uuid))
    }
}

impl Serialize for RrdpSession {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.to_string().serialize(serializer)
    }
}


//------------ SnapshotData --------------------------------------------------

/// The data needed to create an RRDP Snapshot.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct SnapshotData {
    /// A random value to make the URI unique.
    random: RrdpFileRandom,

    /// The objects of each publisher.
    ///
    /// We keep objects per publisher so that we can respond to
    /// list and publication queries more efficiently.
    publishers_current_objects: HashMap<PublisherHandle, CurrentObjects>,
}

impl SnapshotData {
    /// Creates a new snapshot from its components.
    pub fn new(
        random: RrdpFileRandom,
        publishers_current_objects: HashMap<PublisherHandle, CurrentObjects>,
    ) -> Self {
        SnapshotData {
            random,
            publishers_current_objects,
        }
    }

    /// Creates a new, empty snapshot.
    pub fn empty() -> Self {
        SnapshotData::new(RrdpFileRandom::default(), HashMap::default())
    }

    /// Clones the snapshot but gives it a new random component.
    pub fn clone_with_new_random(&self) -> Self {
        SnapshotData::new(
            RrdpFileRandom::default(),
            self.publishers_current_objects.clone(),
        )
    }

    /// Returns a reference to the map of current objects per publisher.
    pub fn publishers_current_objects(
        &self,
    ) -> &HashMap<PublisherHandle, CurrentObjects> {
        &self.publishers_current_objects
    }

    /// Sets the random component to the given value.
    pub fn set_random(&mut self, random: RrdpFileRandom) {
        self.random = random;
    }

    /// Returns the approximate size for all current objects.
    pub fn size_approx(&self) -> usize {
        self.publishers_current_objects.values().fold(
            0, |tot, objects| tot + objects.size_approx()
        )
    }

    /// Returns the current objects for the given publisher if available.
    pub fn get_publisher_objects(
        &self,
        publisher: &PublisherHandle,
    ) -> Option<&CurrentObjects> {
        self.publishers_current_objects.get(publisher)
    }

    /// Applies the delta for a publisher to this snapshot.
    ///
    /// This assumes that the delta had been checked before.
    pub fn apply_delta(
        &mut self,
        publisher: &PublisherHandle,
        delta: DeltaElements,
    ) {
        if let Some(objects)
            = self.publishers_current_objects.get_mut(publisher)
        {
            objects.apply_delta(delta);
            if objects.is_empty() {
                self.publishers_current_objects.remove(publisher);
            }
        }
        else {
            // This is a new publisher without existing objects. So, just
            // create an default -empty- object set for it, so we
            // can apply the delta to it.
            let mut objects = CurrentObjects::default();
            objects.apply_delta(delta);
            self.publishers_current_objects.insert(
                publisher.clone(), objects
            );
        }
    }

    /// Applies the addition of new publisher with an empty object set.
    ///
    /// This is a no-op in case the publisher already exists.
    pub fn apply_publisher_added(&mut self, publisher: PublisherHandle) {
        self.publishers_current_objects.entry(publisher).or_default();
    }

    /// Applies the removal of a publisher.
    ///
    /// This is a no-op in case the publisher does not exists.
    pub fn apply_publisher_removed(&mut self, publisher: &PublisherHandle) {
        self.publishers_current_objects.remove(publisher);
    }

    /// Creates the relative URI path for the snapshot.
    fn rel_path(&self, session: RrdpSession, serial: u64) -> String {
        format!("{}/{}/{}/snapshot.xml", session, serial, self.random.0)
    }

    /// Returns the URI for the snapshot.
    pub fn uri(
        &self,
        session: RrdpSession,
        serial: u64,
        rrdp_base_uri: &uri::Https,
    ) -> uri::Https {
        rrdp_base_uri.join(self.rel_path(session, serial).as_ref()).unwrap()
    }

    /// Returns the file system path for the snapshot.
    pub fn path(
        &self,
        session: RrdpSession,
        serial: u64,
        base_path: &Path,
    ) -> PathBuf {
        base_path.join(self.rel_path(session, serial))
    }

    /// Writes the snapshot XML to a file under `path`.
    pub fn write_xml(
        &self,
        session: RrdpSession,
        serial: u64,
        path: &Path,
    ) -> Result<(), KrillIoError> {
        debug!("Writing snapshot file: {}", path.to_string_lossy());

        let mut f = file::create_file_with_path(path)?;
        self.write_xml_to_writer(session, serial, &mut f).map_err(|e| {
            KrillIoError::new(
                format!(
                    "cannot write snapshot xml to: {}",
                    path.to_string_lossy()
                ),
                e,
            )
        })?;

        debug!("Finished snapshot xml");
        Ok(())
    }

    /// Returns the snapshot XML.
    pub fn xml(&self, session: RrdpSession, serial: u64) -> Vec<u8> {
        let mut res = vec![];
        self.write_xml_to_writer(session, serial, &mut res).unwrap();
        res
    }

    /// Writes the snapshot XML.
    //
    // Note: we do not use the rpki-rs Snapshot implementation because we
    // would need to transform and copy quite a lot of data
    fn write_xml_to_writer(
        &self,
        session: RrdpSession,
        serial: u64,
        writer: &mut impl io::Write,
    ) -> Result<(), io::Error> {
        let mut writer = rpki::xml::encode::Writer::new(writer);
        writer
            .element(SNAPSHOT)?
            .attr("xmlns", NS)?
            .attr("version", VERSION)?
            .attr("session_id", &session)?
            .attr("serial", &serial)?
            .content(|content| {
                for publisher_objects in
                    self.publishers_current_objects.values()
                {
                    for (uri, base64) in publisher_objects.iter() {
                        content
                            .element(PUBLISH)?
                            .attr("uri", uri.as_str())?
                            .content(|content| {
                                content.raw(base64.as_str())
                            })?;
                    }
                }
                Ok(())
            })?;
        writer.done()
    }
}


//------------ CurrentObjects ------------------------------------------------

/// The current set of published objects.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct CurrentObjects(HashMap<CurrentObjectUri, Base64>);

impl CurrentObjects {
    /// Returns the number of objects in the set.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns whether the set is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the approximate size of all objects.
    pub fn size_approx(&self) -> usize {
        self.0.values().fold(0, |tot, el| tot + el.size_approx())
    }

    /// Extends the objects by another set.
    ///
    /// The content of objects already present will be overwritten.
    pub fn extend(&mut self, other: Self) {
        self.0.extend(other.0)
    }

    /// Returns the delta elements needed to turn this set into the other.
    pub fn diff(&self, other: &Self) -> KrillResult<DeltaElements> {
        let mut publishes = vec![];
        let mut updates = vec![];
        let mut withdraws = vec![];

        // find new and updated stuff
        for (uri_key, base64) in &other.0 {
            match self.0.get(uri_key) {
                None => {
                    publishes.push(PublishElement {
                        uri: uri_key.try_into()?,
                        base64: base64.clone(),
                    });
                }
                Some(existing_b64) => {
                    if base64 != existing_b64 {
                        updates.push(UpdateElement {
                            uri: uri_key.try_into()?,
                            hash: existing_b64.to_hash(),
                            base64: base64.clone()
                        });
                    }
                }
            }
        }

        // find removed stuff
        for (uri_key, base64) in &self.0 {
            if !other.0.contains_key(uri_key) {
                let wdr = WithdrawElement {
                    uri: uri_key.try_into()?,
                    hash: base64.to_hash(),
                };
                withdraws.push(wdr);
            }
        }

        Ok(DeltaElements::new(publishes, updates, withdraws))
    }

    /// Returns an iterator over the current objects.
    pub fn iter(&self) -> impl Iterator<Item = (&CurrentObjectUri, &Base64)> {
        self.0.iter()
    }

    /// Converts the set into a list of publish elements.
    pub fn try_into_published_files(
        self,
    ) -> KrillResult<Vec<PublishedFile>> {
        let mut elements = Vec::new();

        for (uri_key, base64) in self.0.into_iter() {
            elements.push(PublishedFile { uri: uri_key.try_into()?, base64 });
        }

        Ok(elements)
    }

    /// Creates a list of withdraw elements for all current objects.
    pub fn try_to_withdraw_elements(
        &self
    ) -> KrillResult<Vec<WithdrawElement>> {
        let mut elements = Vec::new();

        for (uri_key, base64) in self.0.iter() {
            elements.push(WithdrawElement {
                uri: uri_key.try_into()?,
                hash: base64.to_hash(),
            });
        }

        Ok(elements)
    }

    /// Verifies that a delta can be applied to this set of objects.
    ///
    /// Checks that all object URIs are under `jail`, that published objects
    /// aren’t in the set, and that updated and deleted objects are in the set
    /// with the
    /// given hash.
    pub fn verify_delta_applies(
        &self,
        delta: &DeltaElements,
        jail: &uri::Rsync,
    ) -> Result<(), PublicationDeltaError> {
        for p in delta.publishes() {
            if !jail.is_parent_of(&p.uri) {
                return Err(PublicationDeltaError::outside(jail, &p.uri));
            }
            if self.0.contains_key(&CurrentObjectUri::from(&p.uri)) {
                return Err(PublicationDeltaError::present(&p.uri));
            }
        }

        for u in delta.updates() {
            if !jail.is_parent_of(&u.uri) {
                return Err(PublicationDeltaError::outside(jail, &u.uri));
            }
            if !self.contains(u.hash, &u.uri) {
                return Err(PublicationDeltaError::no_match(&u.uri));
            }
        }

        for w in delta.withdraws() {
            if !jail.is_parent_of(&w.uri) {
                return Err(PublicationDeltaError::outside(jail, &w.uri));
            }
            if !self.contains(w.hash, &w.uri) {
                return Err(PublicationDeltaError::no_match(&w.uri));
            }
        }

        Ok(())
    }

    /// Returns whether the set contains an object with the given URI and hash.
    fn contains(&self, hash: Hash, uri: &uri::Rsync) -> bool {
        match self.0.get(&CurrentObjectUri::from(uri)) {
            Some(base64) => base64.to_hash() == hash,
            None => false,
        }
    }

    /// Applies a delta to CurrentObjects.
    ///
    /// Assumes that the delta was checked using
    /// [`Self::verify_delta_applies`].
    pub fn apply_delta(&mut self, delta: DeltaElements) {
        let (publishes, updates, withdraws) = delta.unpack();

        for p in publishes {
            self.0.insert(CurrentObjectUri::from(p.uri), p.base64);
        }

        for u in updates {
            // we ignore the hash of the old object when inserting
            // the update, as it has already been verified.
            self.0.insert(CurrentObjectUri::from(u.uri), u.base64);
        }

        for w in withdraws {
            self.0.remove(&CurrentObjectUri::from(w.uri));
        }
    }

    /// Returns the withdraws for elements matching the given URI.
    pub fn get_matching_withdraws(
        &self,
        match_uri: &uri::Rsync,
    ) -> KrillResult<Vec<WithdrawElement>> {
        let match_uri = CurrentObjectUri::from(match_uri);

        let mut withdraws = Vec::new();
        for (uri_key, base64) in &self.0 {
            if uri_key == &match_uri
                || (match_uri.as_str().ends_with('/')
                    && uri_key.as_str().starts_with(match_uri.as_str()))
            {
                withdraws.push(WithdrawElement {
                    uri: uri_key.try_into()?,
                    hash: base64.to_hash(),
                });
            }
        }

        Ok(withdraws)
    }

    /// Creates a publication list reply for the set.
    pub fn get_list_reply(&self) -> KrillResult<publication::ListReply> {
        let mut elements = Vec::new();

        for (key, base64) in &self.0 {
            elements.push(publication::ListElement::new(
                key.try_into()?, base64.to_hash()
            ));
        }

        Ok(publication::ListReply::new(elements))
    }

    /// Returns the stats.
    pub fn get_stats(&self) -> PublisherStats {
        let mut manifests = vec![];
        for (uri_key, base64) in self.iter() {
            // Add all manifests - as long as they are syntactically correct -
            // do not crash on incorrect objects.
            if
                uri_key.as_str().ends_with("mft") 
                && let Ok(mft) = Manifest::decode(
                    base64.to_bytes().as_ref(), false
                ) && let Ok(stats) = PublisherManifestStats::try_from(&mft)
            {
                manifests.push(stats)
            }
        }

        PublisherStats {
            objects: self.len(),
            size: self.size_approx(),
            manifests,
        }
    }
}


//--- FromIterator

impl<K> FromIterator<(K, Base64)> for CurrentObjects
where K: Into<CurrentObjectUri> {
    fn from_iter<T: IntoIterator<Item = (K, Base64)>>(
        iter: T
    ) -> Self {
        Self(iter.into_iter().map(|(k, v)| (k.into(), v)).collect())
    }
}


//------------ CurrentObjectUri ----------------------------------------------

/// An object’s rsync URI as a simple map key.
///
/// We use this separate type rather than [`rpki::uri::Rsync`] because the
/// latter is not very suitable for use in hash maps: it is mutable and
/// its hash function is slow due to the fact that it needs
/// to accommodate for the fact that the URI scheme and hostname are
/// case insensitive.
///
/// This type can still be cloned cheaply since it holds an arc to an
/// allocated string.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub struct CurrentObjectUri(Arc<str>);

impl CurrentObjectUri {
    /// Returns a string reference of the rsync URI.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<&uri::Rsync> for CurrentObjectUri {
    fn from(value: &uri::Rsync) -> Self {
        // use canonical scheme and hostname (converts to lowercase if needed)
        CurrentObjectUri(
            format!("{}{}", value.canonical_module(), value.path()).into()
        )
    }
}

impl From<uri::Rsync> for CurrentObjectUri {
    fn from(value: uri::Rsync) -> Self {
        Self::from(&value)
    }
}

impl TryFrom<&CurrentObjectUri> for uri::Rsync {
    type Error = Error;

    fn try_from(key: &CurrentObjectUri) -> Result<Self, Self::Error> {
        uri::Rsync::from_slice(key.0.as_bytes()).map_err(|e| {
            Error::Custom(format!(
                "Found invalid object uri: {}. Error: {}",
                key.0, e
            ))
        })
    }
}

impl TryFrom<CurrentObjectUri> for uri::Rsync {
    type Error = Error;

    fn try_from(key: CurrentObjectUri) -> Result<Self, Self::Error> {
        uri::Rsync::try_from(&key)
    }
}


//------------ RrdpFileRandom ------------------------------------------------

/// A random component included in the name of RRDP files.
///
/// The component will make the URIs unguessable and prevent cache poisoning
/// (through CDNs caching a 404 not found).
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct RrdpFileRandom(String);

impl Default for RrdpFileRandom {
    fn default() -> Self {
        let mut bytes = [0; 8];
        openssl::rand::rand_bytes(&mut bytes).unwrap();
        let s = hex::encode(bytes);
        RrdpFileRandom(s)
    }
}


//------------ DeltaData -----------------------------------------------------

/// The data needed to create an RRDP delta XML file.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DeltaData {
    /// A random value to make the URI unique.
    random: RrdpFileRandom,

    /// The serial number of the delta.
    ///
    /// The session is implied by owning RRDP server, but deltas carry a
    /// serial.
    serial: u64,

    /// The time the delta was created at.
    ///
    /// This is used to determine how long we need to keep it around for.
    time: Time,

    /// The objects changed by this delta.
    ///
    /// Note that we do not need to keep track of the owning publisher in this
    /// context. This value represents a change that has already been
    /// applied.
    elements: DeltaElements,
}

impl DeltaData {
    /// Creates a new delta from its components.
    pub fn new(
        serial: u64,
        time: Time,
        random: RrdpFileRandom,
        elements: DeltaElements,
    ) -> Self {
        DeltaData {
            serial,
            random,
            time,
            elements,
        }
    }

    /// Returns the serial number of the delta.
    pub fn serial(&self) -> u64 {
        self.serial
    }

    /// Returns the random component of the delta URI.
    pub fn random(&self) -> &RrdpFileRandom {
        &self.random
    }

    /// Returns whether the delta is older than the given number of seconds.
    pub fn older_than_seconds(&self, seconds: i64) -> bool {
        let then = Time::now() - Duration::seconds(seconds);
        self.time < then
    }

    /// Returns whether the delta is younger than the given number of seconds.
    pub fn younger_than_seconds(&self, seconds: i64) -> bool {
        let then = Time::now() - Duration::seconds(seconds);
        self.time > then
    }

    /// Returns a reference to the delta elements.
    pub fn elements(&self) -> &DeltaElements {
        &self.elements
    }

    /// Returns the relative path to the delta.
    fn rel_path(&self, session: RrdpSession, serial: u64) -> String {
        format!("{}/{}/{}/delta.xml", session, serial, self.random.0)
    }

    /// Returns the RRDP URI for the delta.
    pub fn uri(
        &self,
        session: RrdpSession,
        serial: u64,
        rrdp_base_uri: &uri::Https,
    ) -> uri::Https {
        rrdp_base_uri
            .join(self.rel_path(session, serial).as_ref())
            .unwrap()
    }

    /// Returns the local file path for the delta.
    pub fn path(
        &self,
        session: RrdpSession,
        serial: u64,
        base_path: &Path,
    ) -> PathBuf {
        base_path.join(self.rel_path(session, serial))
    }

    /// Returns the delta XML.
    pub fn xml(&self, session: RrdpSession, serial: u64) -> Vec<u8> {
        let mut res = vec![];
        self.write_xml_to_writer(session, serial, &mut res).unwrap();
        res
    }

    /// Writes the delta XML.
    //
    // Note: we do not use the rpki-rs Delta implementation because we
    // potentially would need to transform and copy quite a lot of data.
    fn write_xml_to_writer(
        &self,
        session: RrdpSession,
        serial: u64,
        writer: &mut impl io::Write,
    ) -> Result<(), io::Error> {
        let mut writer = rpki::xml::encode::Writer::new(writer);
        writer
            .element(DELTA)?
            .attr("xmlns", NS)?
            .attr("version", VERSION)?
            .attr("session_id", &session)?
            .attr("serial", &serial)?
            .content(|content| {
                for el in self.elements().publishes() {
                    content
                        .element(PUBLISH.into_unqualified())?
                        .attr("uri", &el.uri)?
                        .content(|content| {
                            content.raw(el.base64.as_str())
                        })?;
                }
                for el in self.elements().updates() {
                    content
                        .element(PUBLISH.into_unqualified())?
                        .attr("uri", &el.uri)?
                        .attr("hash", &el.hash)?
                        .content(|content| {
                            content.raw(el.base64.as_str())
                        })?;
                }
                for el in self.elements().withdraws() {
                    content
                        .element(WITHDRAW.into_unqualified())?
                        .attr("uri", &el.uri)?
                        .attr("hash", &el.hash)?;
                }
                Ok(())
            })?;

        writer.done()
    }
}


//------------ DeltaElements -------------------------------------------------

/// The elements of an RRDP delta.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct DeltaElements {
    /// The objects to be published.
    publishes: Vec<PublishElement>,

    /// The objects to be updated.
    updates: Vec<UpdateElement>,

    /// The objects to be withdrawn.
    withdraws: Vec<WithdrawElement>,
}

impl DeltaElements {
    /// Creates the delta from the various elements.
    pub fn new(
        publishes: Vec<PublishElement>,
        updates: Vec<UpdateElement>,
        withdraws: Vec<WithdrawElement>,
    ) -> Self {
        DeltaElements {
            publishes,
            updates,
            withdraws,
        }
    }

    /// Converts the value into its three constituent portions.
    pub fn unpack(
        self
    ) -> (
        Vec<PublishElement>,
        Vec<UpdateElement>,
        Vec<WithdrawElement>,
    ) {
        (self.publishes, self.updates, self.withdraws)
    }

    /// Returns the overall number of elements.
    pub fn len(&self) -> usize {
        self.publishes.len() + self.updates.len() + self.withdraws.len()
    }

    /// Returns whether there are no delta elements.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the approximate size of the published and updated objects.
    pub fn size_approx(&self) -> usize {
        let sum_publishes = self
            .publishes
            .iter()
            .fold(0, |sum, p| sum + p.base64.size_approx());
        let sum_updates =
            self.updates.iter().fold(0, |sum, u| sum + u.base64.size_approx());

        sum_publishes + sum_updates
    }

    /// Appends all elements from `other` to `self`.
    ///
    /// The method performs a dumb append, i.e., it will not check for
    /// duplicate operations such as withdrawing a previously published
    /// objected.
    pub fn append(&mut self, mut other: Self) {
        self.publishes.append(&mut other.publishes);
        self.updates.append(&mut other.updates);
        self.withdraws.append(&mut other.withdraws);
    }

    /// Returns a reference to the published elements.
    pub fn publishes(&self) -> &[PublishElement] {
        &self.publishes
    }

    /// Returns a reference to the updated elements.
    pub fn updates(&self) -> &[UpdateElement] {
        &self.updates
    }

    /// Returns a reference to the withdrawn elements.
    pub fn withdraws(&self) -> &[WithdrawElement] {
        &self.withdraws
    }
}


//--- From

impl From<publication::PublishDelta> for DeltaElements {
    fn from(d: publication::PublishDelta) -> Self {
        let mut publishes = vec![];
        let mut updates = vec![];
        let mut withdraws = vec![];

        for el in d.into_elements() {
            match el {
                publication::PublishDeltaElement::Publish(p) => {
                    publishes.push(p.into())
                }
                publication::PublishDeltaElement::Update(u) => {
                    updates.push(u.into())
                }
                publication::PublishDeltaElement::Withdraw(w) => {
                    withdraws.push(w.into())
                }
            }
        }

        DeltaElements {
            publishes,
            updates,
            withdraws,
        }
    }
}


//------------ StagedElements ------------------------------------------------

/// This type is used to combine staged delta elements for publishers.
///
/// It uses a map with object URIs as key, because this is the unique key that
/// identifies objects in the publication protocol.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct StagedElements(HashMap<uri::Rsync, DeltaElement>);

impl StagedElements {
    /// Merge a new DeltaElements into this existing (unpublished)
    /// StagedElements.
    ///
    /// It is assumed that both sets make sense with regards to the current
    /// files as published in the RRDP snapshot. I.e. *this* StagedElements
    /// is assumed to be verified and can be applied to the current snapshot.
    ///
    /// The new StagedElements is assumed to be verified against the current
    /// snapshot after *this* existing StagedElements has been applied.
    ///
    /// We keep a single StagedElements per CA, for simplicity. The
    /// StagedElements contains all changes that the CA published at the
    /// Publication Server, which are not *yet* published in the public
    /// RPKI repository.
    ///
    /// CAs may wish to publish further changes, even before the staged
    /// changes become visible in the public RPKI repository. When merging
    /// these changes we need to take care to ensure that the changes make
    /// sense in relation to the current published *snapshot*.
    ///
    /// This operation is not entirely trivial as we need to make sure that
    /// any hashes in updates and withdraws after merge match the current
    /// snapshot, i.e. the new snapshot could refer to hashes of not yet
    /// published objects. We may also simply "forget" objects that were
    /// staged for publication, and then withdrawn, without ever have been
    /// published.
    ///
    /// Also note (again) that all changes have been verified before when
    /// this is called. That means that while certain corner cases could be
    /// problematic (e.g. double withdraw of an object), we will largely
    /// ignore such issues here. We need to do this, because we get these
    /// changes as write-ahead-log (WAL) changes and therefore applying
    /// them is not allowed to fail. In these cases we will log a warning
    /// that a "publish merge conflict" was found and resolved.
    fn merge_new_elements(&mut self, elements: DeltaElements) {
        let (publishes, updates, withdraws) = elements.unpack();

        let general_merge_message = "Non-critical publish merge conflict resolved. Please contact rpki-team@nlnetlabs.nl if this happens more frequently.";

        for pbl in publishes {
            let uri = pbl.uri.clone();
            match self.0.get_mut(&uri) {
                Some(DeltaElement::Publish(staged_publish)) => {
                    error!(
                        "{} Received new publish element for {} with content hash {} while another publish element with content hash {} was already staged. Expected new *update* element instead. Will use new publish.",
                        general_merge_message,
                        uri,
                        pbl.base64.to_hash(),
                        staged_publish.base64.to_hash()
                    );
                    self.0.insert(uri, DeltaElement::Publish(pbl));
                }

                Some(DeltaElement::Update(staged_update)) => {
                    error!(
                        "{} Received new publish element for {} with content hash {} while an *update* with content hash {} was already staged. Expected new *update* element instead. Will merge content of publish into staged update.",
                        general_merge_message,
                        uri,
                        pbl.base64.to_hash(),
                        staged_update.base64.to_hash()
                    );
                    staged_update.base64 = pbl.base64;
                }
                Some(DeltaElement::Withdraw(staged_withdraw)) => {
                    // A new publish that follows a withdraw for the same URI
                    // should be an Update of the original
                    // file.
                    let hash = staged_withdraw.hash;
                    let update = UpdateElement {
                        uri: uri.clone(), hash, base64: pbl.base64
                    };
                    self.0.insert(uri, DeltaElement::Update(update));
                }
                None => {
                    // This is just a fresh publish element, nothing to merge,
                    // we can just insert it.
                    self.0.insert(uri, DeltaElement::Publish(pbl));
                }
            }
        }

        for mut upd in updates {
            let uri = upd.uri.clone();
            match self.0.get_mut(&uri) {
                Some(DeltaElement::Publish(staged_publish)) => {
                    // An update that follows a *staged* publish, should be
                    // merged into a fresh new publish
                    // with the updated content.
                    //
                    // To the outside world (RRDP delta in particular) this
                    // will look like a single publish.
                    staged_publish.base64 = upd.base64;
                }
                Some(DeltaElement::Update(staged_update)) => {
                    // An update that follows a *staged* update, should be
                    // merged into an update with the
                    // updated content, but it should keep
                    // the hash (i.e. object it replaces) from the existing
                    // staged update.
                    //
                    // To the outside world (RRDP delta in particular) this
                    // will look like a single update.
                    staged_update.base64 = upd.base64;
                }
                Some(DeltaElement::Withdraw(staged_withdraw)) => {
                    error!(
                        "{} Received new update element for {} with content hash {} and replacing object hash {}, while a *withdraw* for content hash {} was already staged. Expected new *update* element instead. Will stage the update instead of withdraw.",
                        general_merge_message,
                        uri,
                        upd.base64.to_hash(),
                        upd.hash,
                        staged_withdraw.hash,
                    );
                    upd.hash = staged_withdraw.hash;
                    self.0.insert(uri, DeltaElement::Update(upd));
                }
                None => {
                    // A new update, nothing to merge. Just include it.
                    self.0.insert(uri, DeltaElement::Update(upd));
                }
            }
        }

        for mut wdr in withdraws {
            let uri = wdr.uri.clone();
            match self.0.get(&uri) {
                Some(DeltaElement::Publish(_)) => {
                    // We had a staged fresh publish for this object. So when
                    // combining we should just remove the
                    // staged entry completely. I.e. it won't
                    // have been visible to the outside world.
                    self.0.remove(&uri);
                }
                Some(DeltaElement::Update(staged_update)) => {
                    // We had a staged update for a file we now wish to
                    // remove. But, the staged updated
                    // files was never visible in public RRDP. Therefore,
                    // we should update the hash of the withdraw to the
                    // original hash.
                    wdr.hash = staged_update.hash;
                    self.0.insert(uri, DeltaElement::Withdraw(wdr));
                }
                Some(DeltaElement::Withdraw(staged_wdr)) => {
                    // This should never happen. But leave the original
                    // withdraw in place because
                    // that already matches the current file in public RRDP.
                    error!("{} We received a withdraw for an object that was already withdrawn.\nExisting withdraw: {} {}\nReceived withdraw: {} {}\n", general_merge_message, staged_wdr.hash, staged_wdr.uri, wdr.hash, wdr.uri)
                }
                None => {
                    // No staged changes for this element, so we can just add
                    // the withdraw as-is. It should match
                    // the current file in public RRDP.
                    self.0.insert(uri, DeltaElement::Withdraw(wdr));
                }
            }
        }
    }
}

impl From<StagedElements> for DeltaElements {
    fn from(staged: StagedElements) -> Self {
        let mut publishes = vec![];
        let mut updates = vec![];
        let mut withdraws = vec![];

        for el in staged.0.into_values() {
            match el {
                DeltaElement::Publish(publish) => publishes.push(publish),
                DeltaElement::Update(update) => updates.push(update),
                DeltaElement::Withdraw(withdraw) => withdraws.push(withdraw),
            }
        }
        DeltaElements::new(publishes, updates, withdraws)
    }
}


//============ RRDP Elements =================================================


//------------ PublishElement ------------------------------------------------

/// A publish element as used in the RRDP protocol.
///
/// Note that the difference with the publication protocol is the absence of
/// the tag.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct PublishElement {
    /// The URI identifying the object to be published.
    pub uri: uri::Rsync,

    /// The Base64 encoded content of the object to be published.
    pub base64: Base64,
}

impl From<publication::Publish> for PublishElement {
    fn from(p: publication::Publish) -> Self {
        let (_tag, uri, base64) = p.unpack();
        PublishElement { base64, uri }
    }
}


//------------ UpdateElement -------------------------------------------------

/// An update element as used in the RRDP protocol.
///
/// Note that the difference with the publication protocol is the absence of
/// the tag.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct UpdateElement {
    /// The URI identifying the object to be updated.
    pub uri: uri::Rsync,

    /// The hash of the current content of the object to be updated.
    pub hash: Hash,

    /// The new content of the object to be updated.
    pub base64: Base64,
}

impl UpdateElement {
    /// Converts the update element into a publish element.
    pub fn into_publish(self) -> PublishElement {
        PublishElement {
            uri: self.uri,
            base64: self.base64,
        }
    }
}

impl From<publication::Update> for UpdateElement {
    fn from(u: publication::Update) -> Self {
        let (_tag, uri, base64, hash) = u.unpack();
        UpdateElement { uri, hash, base64 }
    }
}


//------------ WithdrawElement -----------------------------------------------

/// A withdraw element as used in the RRDP protocol.
///
/// Note that the difference with the publication protocol is the absence of
//
//  *Warning:* This type is used in stored state.
/// the tag.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct WithdrawElement {
    /// The URI identifying the object to be withdrawn.
    pub uri: uri::Rsync,

    /// The hash of the current content of the object to be withdrawn.
    pub hash: Hash,
}

impl From<publication::Withdraw> for WithdrawElement {
    fn from(w: publication::Withdraw) -> Self {
        let (_tag, uri, hash) = w.unpack();
        WithdrawElement { uri, hash }
    }
}


//------------ DeltaElement --------------------------------------------------

/// An element in an RRDP delta.
//
//  *Warning:* This type is used in stored state.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum DeltaElement {
    Publish(PublishElement),
    Update(UpdateElement),
    Withdraw(WithdrawElement),
}


//============ Error Types ===================================================

//------------ PublicationDeltaError -----------------------------------------

/// An error happened while verifying a delta.
#[derive(Clone, Debug)]
pub enum PublicationDeltaError {
    /// An object URI is outside the rsync base path.
    UriOutsideJail(uri::Rsync, uri::Rsync),

    /// A published object is already present.
    ObjectAlreadyPresent(uri::Rsync),

    /// An updated or deleted object is not present with the right hash.
    NoObjectForHashAndOrUri(uri::Rsync),
}

impl PublicationDeltaError {
    fn outside(jail: &uri::Rsync, uri: &uri::Rsync) -> Self {
        PublicationDeltaError::UriOutsideJail(uri.clone(), jail.clone())
    }

    fn present(uri: &uri::Rsync) -> Self {
        PublicationDeltaError::ObjectAlreadyPresent(uri.clone())
    }

    fn no_match(uri: &uri::Rsync) -> Self {
        PublicationDeltaError::NoObjectForHashAndOrUri(uri.clone())
    }
}

impl fmt::Display for PublicationDeltaError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            PublicationDeltaError::UriOutsideJail(uri, jail) => {
                write!(f,
                    "Publishing '{uri}' outside of jail URI '{jail}'"
                )
            }
            PublicationDeltaError::ObjectAlreadyPresent(uri) => {
                write!(f,
                    "File already exists for uri (use update!): {uri}"
                )
            }
            PublicationDeltaError::NoObjectForHashAndOrUri(uri) => {
                write!(f, "File does not match hash at uri: {uri}")
            }
        }
    }
}

impl error::Error for PublicationDeltaError { }


//============ Tests =========================================================

#[cfg(test)]
mod test {
    use crate::commons::test::*;
    use super::*;

    #[test]
    fn current_objects_delta() {
        let jail = rsync("rsync://example.krill.cloud/repo/publisher");
        let file1_uri =
            rsync("rsync://example.krill.cloud/repo/publisher/file1.txt");

        let file1_content = Base64::from_content(&[1]);
        let file1_content_2 = Base64::from_content(&[1, 2]);
        let file2_content = Base64::from_content(&[2]);

        let mut objects = CurrentObjects::default();

        let publish_file1 = DeltaElements {
            publishes: vec![PublishElement {
                uri: file1_uri.clone(),
                base64: file1_content.clone(),
            }],
            updates: vec![],
            withdraws: vec![],
        };

        // adding a file to an empty current objects is okay
        assert!(objects.verify_delta_applies(&publish_file1, &jail).is_ok());

        // The actual application of the delta is infallible, because event
        // replays may not fail. It is assumed deltas were verified
        // before they were persisted in events.
        objects.apply_delta(publish_file1.clone());

        // Now adding the same file for the same URI and same hash, as a
        // publish will fail.
        assert!(objects.verify_delta_applies(&publish_file1, &jail).is_err());

        // Adding a different file as a publish element, rather than update,
        // for the same URI will also fail. Checks fix for issue #981.
        let publish_file2 = DeltaElements {
            publishes: vec![PublishElement {
                uri: file1_uri.clone(),
                base64: file2_content,
            }],
            updates: vec![],
            withdraws: vec![],
        };
        assert!(objects.verify_delta_applies(&publish_file2, &jail).is_err());

        // Updates

        // Updating a file should work
        let update_file1 = DeltaElements {
            publishes: vec![],
            updates: vec![UpdateElement {
                uri: file1_uri.clone(),
                hash: file1_content.to_hash(),
                base64: file1_content_2.clone(),
            }],
            withdraws: vec![],
        };
        assert!(objects.verify_delta_applies(&update_file1, &jail).is_ok());
        objects.apply_delta(update_file1.clone());

        // Updating again with the same delta will now fail - there is no
        // longer and object with that uri and hash it was updated to
        // the new content.
        assert!(objects.verify_delta_applies(&update_file1, &jail).is_err());

        // Withdraws

        // Withdrawing file with wrong hash should fail
        let withdraw_file1 = DeltaElements {
            publishes: vec![],
            updates: vec![],
            withdraws: vec![WithdrawElement {
                uri: file1_uri.clone(),
                hash: file1_content.to_hash(),
            }],
        };
        assert!(
            objects.verify_delta_applies(&withdraw_file1, &jail).is_err()
        );

        // Withdrawing file with the right hash should work
        let withdraw_file1_updated = DeltaElements {
            publishes: vec![],
            updates: vec![],
            withdraws: vec![WithdrawElement {
                uri: file1_uri,
                hash: file1_content_2.to_hash(),
            }],
        };
        assert!(
            objects.verify_delta_applies(
                &withdraw_file1_updated, &jail
            ).is_ok()
        );
    }

    #[test]
    fn current_objects_deltas() {
        fn file_rsync_uri(name: &str) -> uri::Rsync {
            let jail = rsync("rsync://example.krill.cloud/repo/publisher");
            jail.join(name.as_bytes()).unwrap()
        }

        fn file_uri(name: &str) -> CurrentObjectUri {
            CurrentObjectUri(
                format!(
                    "rsync://example.krill.cloud/repo/publisher/{name}"
                )
                .into(),
            )
        }

        fn random_content() -> Base64 {
            let mut bytes = [0; 8];
            openssl::rand::rand_bytes(&mut bytes).unwrap();
            Base64::from_content(&bytes)
        }

        // True if the delta contains the same content, even if the ordering
        // is different.
        pub fn equivalent(
            mut this: DeltaElements, mut other: DeltaElements
        ) -> bool {
            this.publishes
                .sort_by(|a, b| a.uri.as_str().cmp(b.uri.as_str()));
            other.publishes
                .sort_by(|a, b| a.uri.as_str().cmp(b.uri.as_str()));
            this.updates
                .sort_by(|a, b| a.uri.as_str().cmp(b.uri.as_str()));
            other.updates
                .sort_by(|a, b| a.uri.as_str().cmp(b.uri.as_str()));
            this.withdraws
                .sort_by(|a, b| a.uri.as_str().cmp(b.uri.as_str()));
            other.withdraws
                .sort_by(|a, b| a.uri.as_str().cmp(b.uri.as_str()));

            this.publishes == other.publishes
                && this.updates == other.updates
                && this.withdraws == other.withdraws
        }

        let mut objects: HashMap<CurrentObjectUri, Base64> = HashMap::new();
        objects.insert(file_uri("file1"), random_content());
        objects.insert(file_uri("file2"), random_content());
        objects.insert(file_uri("file3"), random_content());
        objects.insert(file_uri("file4"), random_content());

        let publishes = vec![
            PublishElement {
                uri: file_rsync_uri("file5"), base64: random_content()
            },
            PublishElement {
                uri: file_rsync_uri("file6"), base64: random_content(), 
            },
        ];

        let updates = vec![
            UpdateElement {
                uri: file_rsync_uri("file1"),
                hash: objects.get(&file_uri("file1")).unwrap().to_hash(),
                base64: random_content(),
            },
            UpdateElement {
                uri: file_rsync_uri("file2"),
                hash: objects.get(&file_uri("file2")).unwrap().to_hash(),
                base64: random_content(),
            },
        ];

        let withdraws = vec![WithdrawElement {
            uri: file_rsync_uri("file3"),
            hash: objects.get(&file_uri("file3")).unwrap().to_hash(),
        }];

        let delta_a_b = DeltaElements::new(publishes, updates, withdraws);
        let objects_a = CurrentObjects(objects);

        let mut objects_b = objects_a.clone();
        objects_b.apply_delta(delta_a_b.clone());
        let derived_delta_a_b = objects_a.diff(&objects_b).unwrap();

        // eprintln!("-----------------GIVEN--------------------");
        // eprintln!("objects: ");
        // eprintln!("{}", serde_json::to_string_pretty(&objects_a).unwrap());
        // eprintln!("delta: ");
        // eprintln!("{}", serde_json::to_string_pretty(&delta_a_b).unwrap());
        // eprintln!("------------------------------------------");

        // eprintln!();

        // eprintln!("-----------------RESULT B------------------");
        // eprintln!("{}", serde_json::to_string_pretty(&objects_b).unwrap());
        // eprintln!("------------------------------------------");

        // eprintln!();

        // eprintln!("-----------------DERIVE A -> B -----------");
        // eprintln!("{}",
        // serde_json::to_string_pretty(&derived_delta_a_b).unwrap());
        // eprintln!("------------------------------------------");

        assert!(equivalent(delta_a_b, derived_delta_a_b));

        let derived_delta_b_a = objects_b.diff(&objects_a).unwrap();
        // eprintln!();
        // eprintln!("-----------------DERIVE B -> A -----------");
        // eprintln!("{}",
        // serde_json::to_string_pretty(&derived_delta_b_a).unwrap());
        // eprintln!("------------------------------------------");

        let mut objects_a_from_b = objects_b.clone();
        objects_a_from_b.apply_delta(derived_delta_b_a);

        // eprintln!();
        // eprintln!("-----------------RESULT B------------------");
        // eprintln!("{}",
        // serde_json::to_string_pretty(&objects_a_from_b).unwrap());
        // eprintln!("------------------------------------------");

        assert_eq!(objects_a, objects_a_from_b);
    }
}