Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
/***********************************************************************************************************************************
Test Restore Command
***********************************************************************************************************************************/
#include "command/backup/backup.h"
#include "command/backup/blockIncr.h"
#include "command/backup/protocol.h"
#include "command/stanza/create.h"
#include "common/compress/helper.h"
#include "common/crypto/cipherBlock.h"
#include "common/io/bufferRead.h"
#include "info/infoArchive.intern.h"
#include "postgres/version.h"
#include "storage/helper.h"
#include "storage/posix/storage.h"
 
#include "harness/backup.h"
#include "harness/blockIncr.h"
#include "harness/config.h"
#include "harness/info.h"
#include "harness/manifest.h"
#include "harness/postgres.h"
#include "harness/protocol.h"
#include "harness/restore.h"
#include "harness/storage.h"
#include "harness/storageHelper.h"
#include "harness/time.h"
 
/***********************************************************************************************************************************
Special string constants
***********************************************************************************************************************************/
// UTF-8 database name as \xNN escapes to keep the source 7-bit ASCII; decodes to the Greek "eureka"
#define UTF8_DB_NAME                                                "\xce\xb5\xcf\x8d\xcf\x81\xce\xb7\xce\xba\xce\xb1"
 
/***********************************************************************************************************************************
Test data for backup.info
***********************************************************************************************************************************/
#define TEST_RESTORE_BACKUP_INFO_DB                                                                                                \
    "[db]\n"                                                                                                                       \
    "db-catalog-version=202506291\n"                                                                                               \
    "db-control-version=1100\n"                                                                                                    \
    "db-id=1\n"                                                                                                                    \
    "db-system-id=6569239123849665679\n"                                                                                           \
    "db-version=\"18\"\n"                                                                                                          \
    "\n"                                                                                                                           \
    "[db:history]\n"                                                                                                               \
    "1={\"db-catalog-version\":202506291,\"db-control-version\":1100,\"db-system-id\":6569239123849665679,"                        \
        "\"db-version\":\"18\"}\n"
 
#define TEST_RESTORE_BACKUP_INFO                                                                                                   \
    "[backup:current]\n"                                                                                                           \
    "20161219-212741F={\"backrest-format\":5,\"backrest-version\":\"2.04\","                                                       \
    "\"backup-archive-start\":\"00000007000000000000001C\",\"backup-archive-stop\":\"00000007000000000000001C\","                  \
    "\"backup-info-repo-size\":3159776,\"backup-info-repo-size-delta\":3159776,\"backup-info-size\":26897030,"                     \
    "\"backup-info-size-delta\":26897030,\"backup-lsn-stop\":\"0/1C000101\",\"backup-timestamp-start\":1482182846,"                \
    "\"backup-timestamp-stop\":1482182861,\"backup-type\":\"full\",\"db-id\":1,\"option-archive-check\":true,"                     \
    "\"option-archive-copy\":false,\"option-backup-standby\":false,\"option-checksum-page\":false,\"option-compress\":true,"       \
    "\"option-hardlink\":false,\"option-online\":true}\n"                                                                          \
    "20161219-212741F_20161219-212803D={\"backrest-format\":5,\"backrest-version\":\"2.04\","                                      \
    "\"backup-archive-start\":\"00000008000000000000001E\",\"backup-archive-stop\":\"00000008000000000000001E\","                  \
    "\"backup-info-repo-size\":3159811,\"backup-info-repo-size-delta\":15765,\"backup-info-size\":26897030,"                       \
    "\"backup-info-size-delta\":163866,\"backup-lsn-stop\":\"0/1E000101\",\"backup-prior\":\"20161219-212741F\","                  \
    "\"backup-reference\":[\"20161219-212741F\"],\"backup-timestamp-start\":1482182877,\"backup-timestamp-stop\":1482182883,"      \
    "\"backup-type\":\"diff\",\"db-id\":1,\"option-archive-check\":true,\"option-archive-copy\":false,"                            \
    "\"option-backup-standby\":false,\"option-checksum-page\":false,\"option-compress\":true,\"option-hardlink\":false,"           \
    "\"option-online\":true}\n"                                                                                                    \
    "20161219-212741F_20161219-212918I={\"backrest-format\":5,\"backrest-version\":\"2.04\","                                      \
    "\"backup-archive-start\":null,\"backup-archive-stop\":null,"                                                                  \
    "\"backup-info-repo-size\":3159811,\"backup-info-repo-size-delta\":15765,\"backup-info-size\":26897030,"                       \
    "\"backup-info-size-delta\":163866,\"backup-lsn-stop\":\"0/1E000105\",\"backup-prior\":\"20161219-212741F\","                  \
    "\"backup-reference\":[\"20161219-212741F\",\"20161219-212741F_20161219-212803D\"],\"backup-timestamp-start\":1482182884,"     \
    "\"backup-timestamp-stop\":1482182985,\"backup-type\":\"incr\",\"db-id\":1,\"option-archive-check\":true,"                     \
    "\"option-archive-copy\":false,\"option-backup-standby\":false,\"option-checksum-page\":false,\"option-compress\":true,"       \
    "\"option-hardlink\":false,\"option-online\":true}\n"
 
// To verify handling of missing backup-lsn-stop for --type=lsn --target=<lsn> backup
#define TEST_RESTORE_BACKUP_INFO1                                                                                                  \
    "[backup:current]\n"                                                                                                           \
    "20161219-212741F={\"backrest-format\":5,\"backrest-version\":\"2.04\","                                                       \
    "\"backup-archive-start\":\"00000007000000000000001C\",\"backup-archive-stop\":\"00000007000000000000001C\","                  \
    "\"backup-info-repo-size\":3159776,\"backup-info-repo-size-delta\":3159776,\"backup-info-size\":26897030,"                     \
    "\"backup-info-size-delta\":26897030,\"backup-timestamp-start\":1482182846,\"backup-timestamp-stop\":1482182861,"              \
    "\"backup-type\":\"full\",\"db-id\":1,\"option-archive-check\":true,\"option-archive-copy\":false,"                            \
    "\"option-backup-standby\":false,\"option-checksum-page\":false,\"option-compress\":true,\"option-hardlink\":false,"           \
    "\"option-online\":true}\n"                                                                                                    \
    "20161219-212741F_20161219-212803D={\"backrest-format\":5,\"backrest-version\":\"2.04\","                                      \
    "\"backup-archive-start\":\"00000008000000000000001E\",\"backup-archive-stop\":\"00000008000000000000001E\","                  \
    "\"backup-info-repo-size\":3159811,\"backup-info-repo-size-delta\":15765,\"backup-info-size\":26897030,"                       \
    "\"backup-info-size-delta\":163866,\"backup-lsn-stop\":\"0/1E000101\",\"backup-prior\":\"20161219-212741F\","                  \
    "\"backup-reference\":[\"20161219-212741F\"],\"backup-timestamp-start\":1482182877,\"backup-timestamp-stop\":1482182883,"      \
    "\"backup-type\":\"diff\",\"db-id\":1,\"option-archive-check\":true,\"option-archive-copy\":false,"                            \
    "\"option-backup-standby\":false,\"option-checksum-page\":false,\"option-compress\":true,\"option-hardlink\":false,"           \
    "\"option-online\":true}\n"                                                                                                    \
    "20161219-212741F_20161219-212918I={\"backrest-format\":5,\"backrest-version\":\"2.04\","                                      \
    "\"backup-archive-start\":null,\"backup-archive-stop\":null,"                                                                  \
    "\"backup-info-repo-size\":3159811,\"backup-info-repo-size-delta\":15765,\"backup-info-size\":26897030,"                       \
    "\"backup-info-size-delta\":163866,\"backup-lsn-stop\":\"0/1E000105\",\"backup-prior\":\"20161219-212741F\","                  \
    "\"backup-reference\":[\"20161219-212741F\",\"20161219-212741F_20161219-212803D\"],\"backup-timestamp-start\":1482182884,"     \
    "\"backup-timestamp-stop\":1482182985,\"backup-type\":\"incr\",\"db-id\":1,\"option-archive-check\":true,"                     \
    "\"option-archive-copy\":false,\"option-backup-standby\":false,\"option-checksum-page\":false,\"option-compress\":true,"       \
    "\"option-hardlink\":false,\"option-online\":true}\n"
 
/***********************************************************************************************************************************
Build a simple manifest for testing
***********************************************************************************************************************************/
static Manifest *
testManifestMinimal(const String *label, unsigned int pgVersion, const String *pgPath)
{
    FUNCTION_HARNESS_BEGIN();
        FUNCTION_HARNESS_PARAM(STRING, label);
        FUNCTION_HARNESS_PARAM(UINT, pgVersion);
        FUNCTION_HARNESS_PARAM(STRING, pgPath);
    FUNCTION_HARNESS_END();
 
    ASSERT(label != NULL);
    ASSERT(pgVersion != 0);
    ASSERT(pgPath != NULL);
 
    Manifest *result = NULL;
 
    OBJ_NEW_BASE_BEGIN(Manifest, .childQty = MEM_CONTEXT_QTY_MAX)
    {
        result = manifestNewInternal();
        result->pub.info = infoNew(REPOSITORY_FORMAT_DEFAULT, NULL);
 
        result->pub.data.backupLabel = strDup(label);
        result->pub.data.backupTimestampStart = 1;
        result->pub.data.pgVersion = pgVersion;
 
        if (strEndsWithZ(label, "I"))
            result->pub.data.backupType = backupTypeIncr;
        else if (strEndsWithZ(label, "D"))
            result->pub.data.backupType = backupTypeDiff;
        else
            result->pub.data.backupType = backupTypeFull;
 
        ManifestTarget targetBase = {.name = MANIFEST_TARGET_PGDATA_STR, .path = pgPath};
        manifestTargetAdd(result, &targetBase);
        ManifestPath pathBase = {.name = MANIFEST_TARGET_PGDATA_STR, .mode = 0700, .group = groupName(), .user = userName()};
        manifestPathAdd(result, &pathBase);
        HRN_MANIFEST_FILE_ADD(result, .name = "pg_data/" PG_FILE_PGVERSION);
    }
    OBJ_NEW_END();
 
    FUNCTION_HARNESS_RETURN(MANIFEST, result);
}
 
/***********************************************************************************************************************************
Test Run
***********************************************************************************************************************************/
static void
testRun(void)
{
    FUNCTION_HARNESS_VOID();
 
    // Install local command handler shim
    static const ProtocolServerHandler testLocalHandlerList[] =
    {
        PROTOCOL_SERVER_HANDLER_BACKUP_LIST
        PROTOCOL_SERVER_HANDLER_RESTORE_LIST
    };
 
    hrnProtocolLocalShimInstall(LSTDEF(testLocalHandlerList));
 
    // Create default storage object for testing
    Storage *storageTest = storagePosixNewP(TEST_PATH_STR, .write = true);
 
    // *****************************************************************************************************************************
    if (testBegin("BlockChecksum"))
    {
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("too large for one buffer");
 
        Buffer *output = bufNew(0);
        IoWrite *write = ioBufferWriteNew(output);
        ioFilterGroupAdd(ioWriteFilterGroup(write), blockChecksumNewPack(ioFilterParamList(blockChecksumNew(3, 8))));
        ioWriteOpen(write);
 
        TEST_RESULT_VOID(ioWrite(write, BUFSTRDEF("ABCDEF")), "write");
        TEST_RESULT_VOID(ioWrite(write, BUFSTRDEF("ABC")), "write");
        TEST_RESULT_VOID(ioWriteClose(write), "close");
 
        TEST_RESULT_STR_Z(
            strNewEncode(encodingHex, pckReadBinP(ioFilterGroupResultP(ioWriteFilterGroup(write), BLOCK_CHECKSUM_FILTER_TYPE))),
            "9e947f00ecd6acb2"
            "cb221327e5a387af"
            "9e947f00ecd6acb2",
            "block checksum list");
 
        ioWriteFree(write);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("buffer smaller than block and remainder");
 
        output = bufNew(0);
        write = ioBufferWriteNew(output);
        ioFilterGroupAdd(ioWriteFilterGroup(write), blockChecksumNew(3, 8));
        ioWriteOpen(write);
 
        TEST_RESULT_VOID(ioWrite(write, BUFSTRDEF("DE")), "write");
        TEST_RESULT_VOID(ioWrite(write, BUFSTRDEF("FA")), "write");
        TEST_RESULT_VOID(ioWrite(write, BUFSTRDEF("BC")), "write");
        TEST_RESULT_VOID(ioWrite(write, BUFSTRDEF("AB")), "write");
        TEST_RESULT_VOID(ioWrite(write, BUFSTRDEF("CX")), "write");
        TEST_RESULT_VOID(ioWriteClose(write), "close");
 
        TEST_RESULT_STR_Z(
            strNewEncode(encodingHex, pckReadBinP(ioFilterGroupResultP(ioWriteFilterGroup(write), BLOCK_CHECKSUM_FILTER_TYPE))),
            "cb221327e5a387af"
            "9e947f00ecd6acb2"
            "9e947f00ecd6acb2"
            "92c3453969207870",
            "block checksum list");
 
        ioWriteFree(write);
    }
 
    // *****************************************************************************************************************************
    if (testBegin("BlockDelta"))
    {
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("super blocks are read until the end");
 
        // Write block incremental with multiple super blocks
        const Buffer *source = BUFSTRZ("123456789ABC");
        Buffer *destination = bufNew(256);
        IoWrite *write = ioBufferWriteNew(destination);
 
        ioFilterGroupAdd(
            ioWriteFilterGroup(write), blockIncrNew(6, 3, 5, 0, 0, 0, NULL, compressFilterP(compressTypeGz, 1, .raw = true), NULL));
        ioWriteOpen(write);
        ioWrite(write, source);
        ioWriteClose(write);
 
        // Extract block map
        uint64_t mapSize = pckReadU64P(ioFilterGroupResultP(ioWriteFilterGroup(write), BLOCK_INCR_FILTER_TYPE));
        const BlockMap *blockMap = blockMapNewRead(
            ioBufferReadNewOpen(BUF(bufPtr(destination) + (bufUsed(destination) - (size_t)mapSize), (size_t)mapSize)), 3, 5);
 
        // Perform block delta
        BlockDelta *blockDelta = blockDeltaNew(blockMap, 3, 5, NULL, cipherSpecNewNone(), compressTypeGz);
        const BlockDeltaRead *blockDeltaRead = blockDeltaReadGet(blockDelta, 0);
        IoRead *read = ioBufferReadNewOpen(destination);
 
        // Set buffer size to one to make sure super blocks are properly flushed
        size_t bufferSizeOld = ioBufferSize();
        ioBufferSizeSet(1);
 
        // Read all blocks from multiple super blocks
        TEST_RESULT_STR_Z(strNewBuf(blockDeltaNext(blockDelta, blockDeltaRead, read)->block), "123", "read block");
        TEST_RESULT_STR_Z(strNewBuf(blockDeltaNext(blockDelta, blockDeltaRead, read)->block), "456", "read block");
        TEST_RESULT_STR_Z(strNewBuf(blockDeltaNext(blockDelta, blockDeltaRead, read)->block), "789", "read block");
        TEST_RESULT_STR_Z(strNewBuf(blockDeltaNext(blockDelta, blockDeltaRead, read)->block), "ABC", "read block");
        TEST_RESULT_PTR(blockDeltaNext(blockDelta, blockDeltaRead, read), NULL, "no more blocks");
 
        // Reset buffer size
        ioBufferSizeSet(bufferSizeOld);
 
        // Now test the block map to show that we are reading in the right position after multiple super blocks
        TEST_RESULT_STR_Z(
            hrnBlockDeltaRender(blockMapNewRead(read, 3, 5), 3, 5),
            "read {reference: 0, bundleId: 0, offset: 0, size: 28}\n"
            "  super block {max: 6, size: 14}\n"
            "    block {no: 0, offset: 0}\n"
            "    block {no: 1, offset: 3}\n"
            "  super block {max: 6, size: 14}\n"
            "    block {no: 0, offset: 6}\n"
            "    block {no: 1, offset: 9}\n",
            "check delta");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreFile()"))
    {
        const String *repoFileReferenceFull = STRDEF("20190509F");
        const String *repoFile1 = STRDEF("pg_data/testfile");
        unsigned int repoIdx = 0;
 
        // Load Parameters
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, TEST_PATH "/repo");
        hrnCfgArgRawZ(argList, cfgOptPgPath, TEST_PATH "/pg");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // Create the pg path
        HRN_STORAGE_PATH_CREATE(storagePgWrite(), NULL, .mode = 0700);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("compressed encrypted repo file - fail");
 
        HRN_STORAGE_PUT_Z(
            storageRepoWrite(), zNewFmt(STORAGE_REPO_BACKUP "/%s/%s", strZ(repoFileReferenceFull), strZ(repoFile1)),
            "acefile", .compressType = compressTypeGz, .cipherSpec = TEST_CIPHER_SPEC_PASS("badpass"),
            .comment = "create a compressed encrypted repo file");
 
        List *fileList = lstNewP(sizeof(RestoreFile));
 
        RestoreFile file =
        {
            .name = STRDEF("normal"),
            .checksum = bufNewDecode(encodingHex, STRDEF("ffffffffffffffffffffffffffffffffffffffff")),
            .size = 7,
            .timeModified = 1557432154,
            .mode = 0600,
            .zero = false,
            .user = NULL,
            .group = NULL,
        };
 
        lstAdd(fileList, &file);
 
        TEST_ERROR(
            restoreFile(
                strNewFmt(STORAGE_REPO_BACKUP "/%s/%s.gz", strZ(repoFileReferenceFull), strZ(repoFile1)), repoIdx, compressTypeGz,
                0, false, false, false, cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("badpass"), .digest = hashTypeSha1), NULL,
                fileList),
            ChecksumError,
            "error restoring 'normal': actual checksum 'd1cd8a7d11daa26814b93eb604e1d49ab4b43770' does not match expected checksum"
            " 'ffffffffffffffffffffffffffffffffffffffff'");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restorePathValidate()"))
    {
        const String *pgPath = STRDEF(TEST_PATH "/pg");
        const String *repoPath = STRDEF(TEST_PATH "/repo");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error when pg appears to be running");
 
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_POSTMTRPID);
 
        TEST_ERROR(
            restorePathValidate(), PgRunningError,
            "unable to restore while PostgreSQL is running\n"
            "HINT: presence of '" PG_FILE_POSTMTRPID "' in '" TEST_PATH "/pg' indicates PostgreSQL is running.\n"
            "HINT: remove '" PG_FILE_POSTMTRPID "' only if PostgreSQL is not running.");
 
        HRN_STORAGE_REMOVE(storagePgWrite(), PG_FILE_POSTMTRPID, .errorOnMissing = true);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on data directory does not look valid - delta");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawBool(argList, cfgOptDelta, true);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restorePathValidate(), "restore --delta with invalid PGDATA");
        TEST_RESULT_BOOL(cfgOptionBool(cfgOptDelta), false, "--delta set to false");
        TEST_RESULT_LOG(
            "P00   WARN: --delta or --force specified but unable to find 'PG_VERSION' or 'backup.manifest' in '" TEST_PATH "/pg' to"
            " confirm that this is a valid $PGDATA directory. --delta and --force have been disabled and if any files exist in the"
            " destination directories the restore will be aborted.");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), "backup.manifest");
        TEST_RESULT_VOID(restorePathValidate(), "restore --delta with valid PGDATA");
        HRN_STORAGE_REMOVE(storagePgWrite(), "backup.manifest", .errorOnMissing = true);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on data directory does not look valid - force");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawBool(argList, cfgOptForce, true);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restorePathValidate(), "restore --force with invalid PGDATA");
        TEST_RESULT_BOOL(cfgOptionBool(cfgOptForce), false, "--force set to false");
        TEST_RESULT_LOG(
            "P00   WARN: --delta or --force specified but unable to find 'PG_VERSION' or 'backup.manifest' in '" TEST_PATH "/pg' to"
            " confirm that this is a valid $PGDATA directory. --delta and --force have been disabled and if any files exist in the"
            " destination directories the restore will be aborted.");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_PGVERSION);
        TEST_RESULT_VOID(restorePathValidate(), "restore --force with valid PGDATA");
        HRN_STORAGE_REMOVE(storagePgWrite(), PG_FILE_PGVERSION, .errorOnMissing = true);
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreBackupSet()"))
    {
        const String *pgPath = STRDEF(TEST_PATH "/pg");
        const String *repoPath = STRDEF(TEST_PATH "/repo");
 
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error when no backups are present");
 
        HRN_INFO_PUT(storageRepoWrite(), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO_DB);
        TEST_ERROR(restoreBackupSet(), BackupSetInvalidError, "no backup set found to restore");
        TEST_RESULT_LOG("P00   WARN: repo1: [BackupSetInvalidError] no backup sets to restore");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on invalid backup set");
 
        HRN_INFO_PUT(
            storageRepoWrite(), INFO_BACKUP_PATH_FILE,
            TEST_RESTORE_BACKUP_INFO
            "\n"
            TEST_RESTORE_BACKUP_INFO_DB);
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptSet, "BOGUS");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(restoreBackupSet(), BackupSetInvalidError, "backup set BOGUS is not valid");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("Fail restore when no backup found in the latest db history");
 
        HRN_INFO_PUT(
            storageRepoWrite(), INFO_BACKUP_PATH_FILE,
            TEST_RESTORE_BACKUP_INFO
            "\n"
            "[db]\n"
            "db-catalog-version=201707211\n"
            "db-control-version=1002\n"
            "db-id=2\n"
            "db-system-id=6626363367545678089\n"
            "db-version=\"10\"\n"
            "\n"
            "[db:history]\n"
            "1={\"db-catalog-version\":202506291,\"db-control-version\":1100,\"db-system-id\":6569239123849665679"
            ",\"db-version\":\"18\"}\n"
            "2={\"db-catalog-version\":201707211,\"db-control-version\":1002,\"db-system-id\":6626363367545678089"
            ",\"db-version\":\"10\"}\n");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreBackupSet(), BackupSetInvalidError,
            "the latest backup set found '20161219-212741F_20161219-212918I' is from a prior version of PostgreSQL\n"
            "HINT: was a backup created after the stanza-upgrade?\n"
            "HINT: specify --set or --type=time/lsn to restore from a prior version of PostgreSQL.");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("target time");
 
        hrnTzSet("UTC");
 
        const String *repoPath2 = STRDEF(TEST_PATH "/repo2");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "time");
        hrnCfgArgRawZ(argList, cfgOptTarget, "2016-12-19 16:28:04-0500");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // Write out backup.info with no current backups to repo1
        HRN_INFO_PUT(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO_DB);
 
        RestoreBackupData backupData = {0};
        TEST_ASSIGN(backupData, restoreBackupSet(), "get backup set");
        TEST_RESULT_STR_Z(backupData.backupSet, "20161219-212741F_20161219-212803D", "backup set found");
        TEST_RESULT_UINT(backupData.repoIdx, 1, "backup set found, repo2");
        TEST_RESULT_LOG("P00   WARN: repo1: [BackupSetInvalidError] no backup sets to restore");
 
        // Switch repo paths and confirm same result but on repo1
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "time");
        hrnCfgArgRawZ(argList, cfgOptTarget, "2016-12-19 16:28:04-0500");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ASSIGN(backupData, restoreBackupSet(), "get backup set");
        TEST_RESULT_STR_Z(backupData.backupSet, "20161219-212741F_20161219-212803D", "backup set found");
        TEST_RESULT_UINT(backupData.repoIdx, 0, "backup set found, repo1");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("target time, multi repo");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "time");
        hrnCfgArgRawZ(argList, cfgOptTarget, "2016-12-19 16:27:30-0500");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        #define TEST_RESTORE_BACKUP_INFO_NEWEST                                                                                    \
            "[backup:current]\n"                                                                                                   \
            "20201212-201243F={\"backrest-format\":5,\"backrest-version\":\"2.04\","                                               \
            "\"backup-archive-start\":\"00000007000000000000001C\",\"backup-archive-stop\":\"00000007000000000000001C\","          \
            "\"backup-info-repo-size\":3159776,\"backup-info-repo-size-delta\":3159776,\"backup-info-size\":26897030,"             \
            "\"backup-info-size-delta\":26897030,\"backup-timestamp-start\":1607803000,\"backup-timestamp-stop\":1607803963,"      \
            "\"backup-type\":\"full\",\"db-id\":1,\"option-archive-check\":true,\"option-archive-copy\":false,"                    \
            "\"option-backup-standby\":false,\"option-checksum-page\":false,\"option-compress\":true,\"option-hardlink\":false,"   \
            "\"option-online\":true}\n"
 
        // Write out backup.info with current backup newest to repo2 but still does not satisfy time requirement, so repo1 chosen
        HRN_INFO_PUT(
            storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE,
            TEST_RESTORE_BACKUP_INFO_NEWEST
            "\n"
            TEST_RESTORE_BACKUP_INFO_DB);
 
        TEST_ERROR(
            restoreBackupSet(), BackupSetInvalidError,
            "unable to find backup set with stop time less than '2016-12-19 16:27:30-0500'");
 
        // Request repo2 - latest from repo2 will be chosen
        hrnCfgArgRawZ(argList, cfgOptRepo, "2");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreBackupSet(), BackupSetInvalidError,
            "unable to find backup set with stop time less than '2016-12-19 16:27:30-0500'");
 
        // Switch paths so newest on repo1
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "time");
        hrnCfgArgRawZ(argList, cfgOptTarget, "2016-12-19 16:27:30-0500");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreBackupSet(), BackupSetInvalidError,
            "unable to find backup set with stop time less than '2016-12-19 16:27:30-0500'");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "time");
        hrnCfgArgRawZ(argList, cfgOptTarget, "Tue, 15 Nov 1994 12:45:26");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreBackupSet(), FormatError,
            "automatic backup set selection cannot be performed with provided time 'Tue, 15 Nov 1994 12:45:26'\n"
            "HINT: time format must be YYYY-MM-DD HH:MM:SS with optional msec and optional timezone (+/- HH or HHMM or HH:MM) - if"
            " timezone is omitted, local time is assumed (for UTC use +00)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("target time, multi repo, no candidates found");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "time");
        hrnCfgArgRawZ(argList, cfgOptTarget, "2016-12-19 16:27:30-0500");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // Write out backup.info with no current backups to repo1 and repo2
        HRN_INFO_PUT(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO_DB);
        HRN_INFO_PUT(storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO_DB);
 
        TEST_ERROR(
            restoreBackupSet(), BackupSetInvalidError,
            "unable to find backup set with stop time less than '2016-12-19 16:27:30-0500'");
        TEST_RESULT_LOG(
            "P00   WARN: repo1: [BackupSetInvalidError] no backup sets to restore\n"
            "P00   WARN: repo2: [BackupSetInvalidError] no backup sets to restore");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("target lsn");
 
        // Match oldest backup on repo 2
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "lsn");
        hrnCfgArgRawZ(argList, cfgOptTarget, "0/1C000101");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // Write out backup.info with no current backups to repo1, with current backups to repo2
        HRN_INFO_PUT(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO_DB);
        HRN_INFO_PUT(storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO "\n" TEST_RESTORE_BACKUP_INFO_DB);
 
        TEST_ASSIGN(backupData, restoreBackupSet(), "get backup set for lsn 0/1C000101");
        TEST_RESULT_STR_Z(backupData.backupSet, "20161219-212741F", "backup set found");
        TEST_RESULT_UINT(backupData.repoIdx, 1, "backup set found, repo2");
        TEST_RESULT_LOG("P00   WARN: repo1: [BackupSetInvalidError] no backup sets to restore");
 
        // Switch repo paths and target lsn to match newer backup on repo1
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "lsn");
        hrnCfgArgRawZ(argList, cfgOptTarget, "0/1E000105");
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ASSIGN(backupData, restoreBackupSet(), "get backup set for lsn 0/1E000105");
        TEST_RESULT_STR_Z(backupData.backupSet, "20161219-212741F_20161219-212918I", "backup set found");
        TEST_RESULT_UINT(backupData.repoIdx, 0, "backup set found, repo1");
 
        // Log warning if missing backup-lsn-stop is found before finding a match
        // Missing backup-lsn-stop in repo1, no backups in repo2, no qualifying auto-selectable backup in either repo
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "lsn");
        hrnCfgArgRawZ(argList, cfgOptTarget, "0/1C000101");
 
        // Re-write repo information with set missing backup-lsn-stop
        HRN_INFO_PUT(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO1 "\n" TEST_RESTORE_BACKUP_INFO_DB);
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreBackupSet(), BackupSetInvalidError, "unable to find backup set with lsn less than or equal to '0/1C000101'");
        TEST_RESULT_LOG(
            "P00   WARN: repo1 reached backup from prior version missing required LSN info before finding a match -- backup"
            " auto-select has been disabled for this repo\n"
            "            HINT: you may specify a backup to restore using the --set option.\n"
            "P00   WARN: repo2: [BackupSetInvalidError] no backup sets to restore");
 
        // Log warning if missing backup-lsn-stop is found before finding a match
        // Missing backup-lsn-stop in repo1, qualifying auto-selectable backup set in repo2
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath2);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "lsn");
        hrnCfgArgRawZ(argList, cfgOptTarget, "0/1C000102");
 
        // Write repo2 information with data required to find backup
        HRN_INFO_PUT(storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO "\n" TEST_RESTORE_BACKUP_INFO_DB);
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ASSIGN(backupData, restoreBackupSet(), "get backup set for lsn 0/1C000102");
        TEST_RESULT_STR_Z(backupData.backupSet, "20161219-212741F", "backup set found");
        TEST_RESULT_UINT(backupData.repoIdx, 1, "backup set found, repo2");
        TEST_RESULT_LOG(
            "P00   WARN: repo1 reached backup from prior version missing required LSN info before finding a match -- backup"
            " auto-select has been disabled for this repo\n"
            "            HINT: you may specify a backup to restore using the --set option.");
 
        // No backups to search for qualifying backup set
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptPgPath, 1, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "lsn");
        hrnCfgArgRawZ(argList, cfgOptTarget, "0/1A000102");
 
        // Write repo info with no current backups
        HRN_INFO_PUT(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO_DB);
 
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreBackupSet(), BackupSetInvalidError, "unable to find backup set with lsn less than or equal to '0/1A000102'");
        TEST_RESULT_LOG(
            "P00   WARN: repo2: [BackupSetInvalidError] no backup sets to restore");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreManifestValidate()"))
    {
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on mismatched label");
 
        Manifest *manifest = testManifestMinimal(STRDEF("20161219-212741F"), PG_VERSION_18, STRDEF("/pg"));
 
        TEST_ERROR(
            restoreManifestValidate(manifest, STRDEF("20161219-212741F_20161219-212918I")), FormatError,
            "requested backup '20161219-212741F_20161219-212918I' and manifest label '20161219-212741F' do not match\n"
            "HINT: this indicates some sort of corruption (at the very least paths have been renamed).");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on unsupported version");
 
        manifest = testManifestMinimal(STRDEF("20161219-212741F"), 90600, STRDEF("/pg"));
 
        TEST_ERROR(
            restoreManifestValidate(manifest, STRDEF("20161219-212741F")), VersionNotSupportedError,
            "invalid PostgreSQL version 9.6\n"
            "HINT: is this version of PostgreSQL supported?\n"
            "HINT: is pgBackRest up to date on all hosts?");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreManifestMap()"))
    {
        const String *pgPath = STRDEF(TEST_PATH "/pg");
        const String *repoPath = STRDEF(TEST_PATH "/repo");
        Manifest *manifest = testManifestMinimal(STRDEF("20161219-212741F"), PG_VERSION_18, pgPath);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("remap data directory");
 
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "base directory is not remapped");
        TEST_RESULT_STR(manifestTargetFind(manifest, MANIFEST_TARGET_PGDATA_STR)->path, pgPath, "base directory is not remapped");
 
        // Now change pg1-path so the data directory gets remapped
        pgPath = STRDEF(TEST_PATH "/pg2");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "base directory is remapped");
        TEST_RESULT_STR(manifestTargetFind(manifest, MANIFEST_TARGET_PGDATA_STR)->path, pgPath, "base directory is remapped");
        TEST_RESULT_LOG("P00   INFO: remap data directory to '" TEST_PATH "/pg2'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("remap tablespaces");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptTablespaceMap, "bogus=/bogus");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(restoreManifestMap(manifest), TablespaceMapError, "unable to remap invalid tablespace 'bogus'");
 
        // Add some tablespaces
        HRN_MANIFEST_TARGET_ADD(
            manifest, .name = "pg_tblspc/1", .path = "/1", .tablespaceId = 1, .tablespaceName = "1",
            .type = manifestTargetTypeLink);
        HRN_MANIFEST_LINK_ADD(manifest, .name = "pg_data/pg_tblspc/1", .destination = "/1");
        HRN_MANIFEST_TARGET_ADD(
            manifest, .name = "pg_tblspc/2", .path = "/2", .tablespaceId = 2, .tablespaceName = "ts2",
            .type = manifestTargetTypeLink);
        HRN_MANIFEST_LINK_ADD(manifest, .name = "pg_data/pg_tblspc/2", .destination = "/2");
 
        // Error on different paths
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptTablespaceMap, "2=/2");
        hrnCfgArgRawZ(argList, cfgOptTablespaceMap, "ts2=/ts2");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreManifestMap(manifest), TablespaceMapError, "tablespace remapped by name 'ts2' and id 2 with different paths");
 
        // Remap one tablespace using the id and another with the name
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptTablespaceMap, "1=/1-2");
        hrnCfgArgRawZ(argList, cfgOptTablespaceMap, "ts2=/2-2");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "remap tablespaces");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_tblspc/1"))->path, "/1-2", "check tablespace 1 target");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_tblspc/1"))->destination, "/1-2", "check tablespace 1 link");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_tblspc/2"))->path, "/2-2", "check tablespace 1 target");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_tblspc/2"))->destination, "/2-2", "check tablespace 1 link");
 
        TEST_RESULT_LOG(
            "P00   INFO: map tablespace 'pg_tblspc/1' to '/1-2'\n"
            "P00   INFO: map tablespace 'pg_tblspc/2' to '/2-2'");
 
        // Remap a tablespace using just the id and map the rest with tablespace-map-all
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptTablespaceMap, "2=/2-3");
        hrnCfgArgRawZ(argList, cfgOptTablespaceMapAll, "/all");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "remap tablespaces");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_tblspc/1"))->path, "/all/1", "check tablespace 1 target");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_tblspc/1"))->destination, "/all/1", "check tablespace 1 link");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_tblspc/2"))->path, "/2-3", "check tablespace 1 target");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_tblspc/2"))->destination, "/2-3", "check tablespace 1 link");
 
        TEST_RESULT_LOG(
            "P00   INFO: map tablespace 'pg_tblspc/1' to '/all/1'\n"
            "P00   INFO: map tablespace 'pg_tblspc/2' to '/2-3'");
 
        // Remap all tablespaces with tablespace-map-all
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptTablespaceMapAll, "/all2");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "remap tablespaces");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_tblspc/1"))->path, "/all2/1", "check tablespace 1 target");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_tblspc/1"))->destination, "/all2/1", "check tablespace 1 link");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_tblspc/2"))->path, "/all2/ts2", "check tablespace 1 target");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_tblspc/2"))->destination, "/all2/ts2", "check tablespace 1 link");
 
        TEST_RESULT_LOG(
            "P00   INFO: map tablespace 'pg_tblspc/1' to '/all2/1'\n"
            "P00   INFO: map tablespace 'pg_tblspc/2' to '/all2/ts2'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on invalid link");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "bogus=bogus");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreManifestMap(manifest), LinkMapError,
            "unable to map link 'bogus'\n"
            "HINT: Does the link reference a valid backup path or file?");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on tablespace remap");
 
        // Add tablespace link which will be ignored unless specified with link-map
        HRN_MANIFEST_TARGET_ADD(
            manifest, .name = "pg_data/pg_tblspc/1", .path = "/tblspc1", .type = manifestTargetTypeLink, .tablespaceId = 1);
        HRN_MANIFEST_LINK_ADD(manifest, .name = "pg_data/pg_tblspc/1", .destination = "/tblspc1");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_tblspc/1=/ignored");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreManifestMap(manifest), LinkMapError,
            "unable to remap tablespace 'pg_tblspc/1'\n"
            "HINT: use 'tablespace-map' option to remap tablespaces.");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("add file link");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_hba.conf=../conf/pg_hba.conf");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/pg_hba.conf", .size = 4, .timestamp = 1482182860);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "remap links");
 
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_hba.conf"))->path, "../conf", "check link path");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_hba.conf"))->file, "pg_hba.conf", "check link file");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_hba.conf"))->destination, "../conf/pg_hba.conf", "check link dest");
        TEST_RESULT_STR(manifestLinkFind(manifest, STRDEF("pg_data/pg_hba.conf"))->group, groupName(), "check link group");
        TEST_RESULT_STR(manifestLinkFind(manifest, STRDEF("pg_data/pg_hba.conf"))->user, userName(), "check link user");
 
        TEST_RESULT_LOG("P00   INFO: link 'pg_hba.conf' to '../conf/pg_hba.conf'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on invalid file link path");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_hba.conf=bogus");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreManifestMap(manifest), LinkMapError,
            "'bogus' is not long enough to be the destination for file link 'pg_hba.conf'");
 
        TEST_RESULT_LOG("P00   INFO: map link 'pg_hba.conf' to 'bogus'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("add path link");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_wal=/wal");
        hrnCfgArgRawBool(argList, cfgOptLinkAll, true);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/pg_wal");
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "remap links");
 
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_wal"))->path, "/wal", "check link path");
        TEST_RESULT_PTR(manifestTargetFind(manifest, STRDEF("pg_data/pg_wal"))->file, NULL, "check link file");
        TEST_RESULT_STR_Z(manifestLinkFind(manifest, STRDEF("pg_data/pg_wal"))->destination, "/wal", "check link dest");
        TEST_RESULT_STR(manifestLinkFind(manifest, STRDEF("pg_data/pg_wal"))->group, groupName(), "check link group");
        TEST_RESULT_STR(manifestLinkFind(manifest, STRDEF("pg_data/pg_wal"))->user, userName(), "check link user");
 
        TEST_RESULT_LOG("P00   INFO: link 'pg_wal' to '/wal'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("remap file and path links");
 
        // Add path link that will not be remapped
        HRN_MANIFEST_TARGET_ADD(manifest, .name = "pg_data/pg_xact", .path = "/pg_xact", .type = manifestTargetTypeLink);
        HRN_MANIFEST_LINK_ADD(manifest, .name = "pg_data/pg_xact", .destination = "/pg_xact");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_hba.conf=../conf2/pg_hba2.conf");
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_wal=/wal2");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "remap links");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_hba.conf"))->path, "../conf2", "check link path");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_hba.conf"))->file, "pg_hba2.conf", "check link file");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_hba.conf"))->destination, "../conf2/pg_hba2.conf", "check link dest");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_wal"))->path, "/wal2", "check link path");
        TEST_RESULT_STR_Z(manifestLinkFind(manifest, STRDEF("pg_data/pg_wal"))->destination, "/wal2", "check link dest");
        TEST_RESULT_PTR(manifestTargetFindDefault(manifest, STRDEF("pg_data/pg_xact"), NULL), NULL, "pg_xact target missing");
        TEST_RESULT_PTR(manifestLinkFindDefault(manifest, STRDEF("pg_data/pg_xact"), NULL), NULL, "pg_xact link missing");
 
        TEST_RESULT_LOG(
            "P00   INFO: map link 'pg_hba.conf' to '../conf2/pg_hba2.conf'\n"
            "P00   INFO: map link 'pg_wal' to '/wal2'\n"
            "P00   WARN: contents of directory link 'pg_xact' will be restored in a directory at the same location");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("preserve all links");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawBool(argList, cfgOptLinkAll, true);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "leave links as they are");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_hba.conf"))->path, "../conf2", "check link path");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_hba.conf"))->file, "pg_hba2.conf", "check link file");
        TEST_RESULT_STR_Z(
            manifestLinkFind(manifest, STRDEF("pg_data/pg_hba.conf"))->destination, "../conf2/pg_hba2.conf", "check link dest");
        TEST_RESULT_STR_Z(manifestTargetFind(manifest, STRDEF("pg_data/pg_wal"))->path, "/wal2", "check link path");
        TEST_RESULT_STR_Z(manifestLinkFind(manifest, STRDEF("pg_data/pg_wal"))->destination, "/wal2", "check link dest");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("remove all links");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreManifestMap(manifest), "remove all links");
 
        TEST_RESULT_PTR(
            manifestTargetFindDefault(manifest, STRDEF("pg_data/pg_hba.conf"), NULL), NULL, "pg_hba.conf target missing");
        TEST_RESULT_PTR(manifestLinkFindDefault(manifest, STRDEF("pg_data/pg_hba.conf"), NULL), NULL, "pg_hba.conf link missing");
        TEST_RESULT_PTR(manifestTargetFindDefault(manifest, STRDEF("pg_data/pg_wal"), NULL), NULL, "pg_wal target missing");
        TEST_RESULT_PTR(manifestLinkFindDefault(manifest, STRDEF("pg_data/pg_wal"), NULL), NULL, "pg_wal link missing");
 
        TEST_RESULT_LOG(
            "P00   WARN: file link 'pg_hba.conf' will be restored as a file at the same location\n"
            "P00   WARN: contents of directory link 'pg_wal' will be restored in a directory at the same location");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreManifestOwner()"))
    {
        userInitInternal();
 
        const String *pgPath = STRDEF(TEST_PATH "/pg");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("owner is not root and all ownership is good");
 
        Manifest *manifest = testManifestMinimal(STRDEF("20161219-212741F_20161219-21275D"), PG_VERSION_10, pgPath);
        const String *rootReplaceUser = NULL;
        const String *rootReplaceGroup = NULL;
 
        TEST_RESULT_VOID(restoreManifestOwner(manifest, &rootReplaceUser, &rootReplaceGroup), "check ownership");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("owner is not root but has no user name");
 
        manifest = testManifestMinimal(STRDEF("20161219-212741F_20161219-21275I"), PG_VERSION_10, pgPath);
 
        userLocalData.groupName = NULL;
        userLocalData.userName = NULL;
 
        TEST_RESULT_VOID(restoreManifestOwner(manifest, &rootReplaceUser, &rootReplaceGroup), "check ownership");
 
        TEST_RESULT_STR(rootReplaceUser, NULL, "root replace user not set");
        TEST_RESULT_STR(rootReplaceGroup, NULL, "root replace group not set");
 
        TEST_RESULT_LOG(
            "P00   WARN: unknown user '" TEST_USER "' in backup manifest mapped to current user\n"
            "P00   WARN: unknown group '" TEST_GROUP "' in backup manifest mapped to current group");
 
        userInitInternal();
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("owner is not root and some ownership is bad");
 
        manifest = testManifestMinimal(STRDEF("20161219-212741F_20161219-21275D"), PG_VERSION_10, pgPath);
 
        ManifestPath path = {.name = STRDEF("pg_data/bogus_path"), .user = STRDEF("path-user-bogus")};
        manifestPathAdd(manifest, &path);
        ManifestFile file = {.name = STRDEF("pg_data/bogus_file"), .mode = 0600, .group = STRDEF("file-group-bogus")};
        manifestFileAdd(manifest, &file);
        ManifestLink link = {.name = STRDEF("pg_data/bogus_link"), .destination = STRDEF("/"), .group = STRDEF("link-group-bogus")};
        manifestLinkAdd(manifest, &link);
 
        TEST_RESULT_VOID(restoreManifestOwner(manifest, &rootReplaceUser, &rootReplaceGroup), "check ownership");
 
        TEST_RESULT_STR(rootReplaceUser, NULL, "root replace user not set");
        TEST_RESULT_STR(rootReplaceGroup, NULL, "root replace group not set");
 
        TEST_RESULT_LOG(
            "P00   WARN: unknown user in backup manifest mapped to current user\n"
            "P00   WARN: unknown user 'path-user-bogus' in backup manifest mapped to current user\n"
            "P00   WARN: unknown group in backup manifest mapped to current group\n"
            "P00   WARN: unknown group 'file-group-bogus' in backup manifest mapped to current group\n"
            "P00   WARN: unknown group 'link-group-bogus' in backup manifest mapped to current group");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("owner is root and ownership is good");
 
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, "/repo");
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        manifest = testManifestMinimal(STRDEF("20161219-212741F_20161219-21275D"), PG_VERSION_10, pgPath);
        userLocalData.userRoot = true;
 
        HRN_STORAGE_PATH_CREATE(storagePgWrite(), NULL, .mode = 0700);
 
        TEST_RESULT_VOID(restoreManifestOwner(manifest, &rootReplaceUser, &rootReplaceGroup), "check ownership");
 
        TEST_RESULT_STR(rootReplaceUser, NULL, "root replace user not set");
        TEST_RESULT_STR(rootReplaceGroup, NULL, "root replace group not set");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("owner is root and user is bad");
 
        manifestPathAdd(manifest, &path);
 
        TEST_RESULT_VOID(restoreManifestOwner(manifest, &rootReplaceUser, &rootReplaceGroup), "check ownership");
 
        TEST_RESULT_STR(rootReplaceUser, TEST_USER_STR, "root replace user set");
        TEST_RESULT_STR(rootReplaceGroup, TEST_GROUP_STR, "root replace group set");
 
        TEST_RESULT_LOG("P00   WARN: unknown group in backup manifest mapped to '" TEST_GROUP "'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("owner is root and group is bad");
 
        manifest = testManifestMinimal(STRDEF("20161219-212741F_20161219-21275D"), PG_VERSION_10, pgPath);
 
        manifestFileAdd(manifest, &file);
        manifestLinkAdd(manifest, &link);
 
        TEST_RESULT_VOID(restoreManifestOwner(manifest, &rootReplaceUser, &rootReplaceGroup), "check ownership");
 
        TEST_RESULT_STR(rootReplaceUser, TEST_USER_STR, "root replace user set");
        TEST_RESULT_STR(rootReplaceGroup, TEST_GROUP_STR, "root replace group set");
 
        TEST_RESULT_LOG("P00   WARN: unknown user in backup manifest mapped to '" TEST_USER "'");
 
        userInitInternal();
 
        // -------------------------------------------------------------------------------------------------------------------------
#ifdef TEST_CONTAINER_REQUIRED
        TEST_TITLE("owner is root and ownership of pg_data is bad");
 
        manifest = testManifestMinimal(STRDEF("20161219-212741F_20161219-21275D"), PG_VERSION_10, pgPath);
 
        manifestPathAdd(manifest, &path);
        manifestFileAdd(manifest, &file);
 
        HRN_SYSTEM_FMT("sudo chown 77777:77777 %s", strZ(pgPath));
 
        userLocalData.userName = STRDEF("root");
        userLocalData.groupName = STRDEF("root");
        userLocalData.userRoot = true;
 
        TEST_RESULT_VOID(restoreManifestOwner(manifest, &rootReplaceUser, &rootReplaceGroup), "check ownership");
 
        TEST_RESULT_STR(rootReplaceUser, STRDEF("root"), "root replace user set");
        TEST_RESULT_STR(rootReplaceGroup, STRDEF("root"), "root replace group set");
 
        TEST_RESULT_LOG(
            "P00   WARN: unknown user in backup manifest mapped to 'root'\n"
            "P00   WARN: unknown group in backup manifest mapped to 'root'");
 
        userInitInternal();
 
#endif // TEST_CONTAINER_REQUIRED
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreClean*()"))
    {
        // Set log level to detail
        harnessLogLevelSet(logLevelDetail);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("restoreCleanOwnership() update to root (existing)");
 
        userLocalData.userRoot = true;
 
        // Expect an error here since we can't really set ownership to root
        TEST_ERROR(
            restoreCleanOwnership(
                TEST_PATH_STR, STRDEF("root"), STRDEF("root"), STRDEF("root"), STRDEF("root"), userId(), groupId(), false),
            FileOwnerError, "unable to set ownership for '" TEST_PATH "': [1] Operation not permitted");
 
        TEST_RESULT_LOG("P00 DETAIL: update ownership for '" TEST_PATH "'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("restoreCleanOwnership() update to bogus (new)");
 
        // Will succeed because bogus will be remapped to the current user/group
        restoreCleanOwnership(TEST_PATH_STR, STRDEF("bogus"), NULL, STRDEF("bogus"), NULL, 0, 0, true);
 
        // Test again with only group for coverage
        restoreCleanOwnership(TEST_PATH_STR, STRDEF("bogus"), NULL, STRDEF("bogus"), NULL, userId(), 0, true);
 
        userInitInternal();
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("directory with bad permissions/mode");
 
        const String *pgPath = STRDEF(TEST_PATH "/pg");
        const String *repoPath = STRDEF(TEST_PATH "/repo");
        Manifest *manifest = testManifestMinimal(STRDEF("20161219-212741F_20161219-21275D"), PG_VERSION_10, pgPath);
 
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        HRN_STORAGE_PATH_CREATE(storagePgWrite(), NULL, .mode = 0600);
 
        userLocalData.userId = TEST_USER_ID + 1;
 
        TEST_ERROR(
            restoreCleanBuild(manifest, NULL, NULL), PathOpenError,
            "unable to restore to path '" TEST_PATH "/pg' not owned by current user");
 
        TEST_RESULT_LOG("P00 DETAIL: check '" TEST_PATH "/pg' exists");
 
        userLocalData.userRoot = true;
 
        TEST_ERROR(
            restoreCleanBuild(manifest, TEST_USER_STR, TEST_GROUP_STR), PathOpenError,
            "unable to restore to path '" TEST_PATH "/pg' without rwx permissions");
 
        TEST_RESULT_LOG("P00 DETAIL: check '" TEST_PATH "/pg' exists");
 
        userInitInternal();
 
        TEST_ERROR(
            restoreCleanBuild(manifest, NULL, NULL), PathOpenError,
            "unable to restore to path '" TEST_PATH "/pg' without rwx permissions");
 
        TEST_RESULT_LOG("P00 DETAIL: check '" TEST_PATH "/pg' exists");
 
        HRN_STORAGE_PATH_REMOVE(storagePgWrite(), NULL);
        HRN_STORAGE_PATH_CREATE(storagePgWrite(), NULL, .mode = 0700);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("fail on restore with directory not empty");
 
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_RECOVERYCONF);
 
        TEST_ERROR(
            restoreCleanBuild(manifest, NULL, NULL), PathNotEmptyError,
            "unable to restore to path '" TEST_PATH "/pg' because it contains files\n"
            "HINT: try using --delta if this is what you intended.");
 
        TEST_RESULT_LOG("P00 DETAIL: check '" TEST_PATH "/pg' exists");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("succeed when all directories empty");
 
        HRN_STORAGE_REMOVE(storagePgWrite(), PG_FILE_RECOVERYCONF);
 
        HRN_MANIFEST_TARGET_ADD(
            manifest, .name = "pg_data/pg_hba.conf", .path = "../conf", .file = "pg_hba.conf", .type = manifestTargetTypeLink);
        HRN_MANIFEST_LINK_ADD(manifest, .name = "pg_data/pg_hba.conf", .destination = "../conf/pg_hba.conf");
 
        HRN_STORAGE_PATH_CREATE(storageTest, "conf", .mode = 0700);
 
        TEST_RESULT_VOID(restoreCleanBuild(manifest, NULL, NULL), "restore");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/conf' exists\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_hba.conf' to '../conf/pg_hba.conf'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error when linked file already exists without delta");
 
        HRN_STORAGE_REMOVE(storagePgWrite(), "pg_hba.conf");
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), "../conf/pg_hba.conf");
 
        TEST_ERROR(
            restoreCleanBuild(manifest, NULL, NULL), FileExistsError,
            "unable to restore file '" TEST_PATH "/conf/pg_hba.conf' because it already exists\n"
            "HINT: try using --delta if this is what you intended.");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/conf' exists");
 
        HRN_STORAGE_REMOVE(storagePgWrite(), "../conf/pg_hba.conf", .errorOnMissing = true);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("succeed when all directories empty and ignore recovery.conf");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "preserve");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreCleanBuild(manifest, NULL, NULL), "restore");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/conf' exists\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_hba.conf' to '../conf/pg_hba.conf'");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_RECOVERYCONF);
        TEST_RESULT_VOID(restoreCleanBuild(manifest, NULL, NULL), "normal restore ignore recovery.conf");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/conf' exists\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_hba.conf' to '../conf/pg_hba.conf'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("succeed when all directories empty and PG12 and preserve but no recovery files");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        manifest->pub.data.pgVersion = PG_VERSION_12;
 
        TEST_RESULT_VOID(restoreCleanBuild(manifest, NULL, NULL), "restore");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/conf' exists\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_hba.conf' to '../conf/pg_hba.conf'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("succeed when all directories empty and ignore PG12 recovery files");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" PG_FILE_POSTGRESQLAUTOCONF);
 
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_POSTGRESQLAUTOCONF);
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_RECOVERYSIGNAL);
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_STANDBYSIGNAL);
 
        TEST_RESULT_VOID(restoreCleanBuild(manifest, NULL, NULL), "restore");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/conf' exists\n"
            "P00 DETAIL: skip 'postgresql.auto.conf' -- recovery type is preserve\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_hba.conf' to '../conf/pg_hba.conf'");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("succeed when all directories empty and PG12");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(restoreCleanBuild(manifest, NULL, NULL), "restore");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/conf' exists\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_hba.conf' to '../conf/pg_hba.conf'");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreSelectiveExpression()"))
    {
        // Set log level to detail
        harnessLogLevelSet(logLevelDetail);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("no valid databases");
 
        StringList *argListClean = strLstNew();
        hrnCfgArgRawZ(argListClean, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argListClean, cfgOptRepoPath, "/repo");
        hrnCfgArgRawZ(argListClean, cfgOptPgPath, "/pg");
 
        StringList *argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, UTF8_DB_NAME);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        Manifest *manifest = NULL;
 
        OBJ_NEW_BASE_BEGIN(Manifest, .childQty = MEM_CONTEXT_QTY_MAX)
        {
            manifest = manifestNewInternal();
            manifest->pub.data.pgVersion = PG_VERSION_18;
            manifest->pub.data.pgCatalogVersion = hrnPgCatalogVersion(PG_VERSION_18);
            manifest->pub.data.backupTimestampStart = 1;
 
            HRN_MANIFEST_TARGET_ADD(manifest, .name = MANIFEST_TARGET_PGDATA, .path = "/pg");
            HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" PG_FILE_PGVERSION);
        }
        OBJ_NEW_END();
 
        TEST_ERROR(
            restoreSelectiveExpression(manifest), FormatError,
            "no databases found for selective restore\n"
            "HINT: is this a valid cluster?");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("database id is missing on disk");
 
        MEM_CONTEXT_BEGIN(manifest->pub.memContext)
        {
            // Give non-systemId to postgres db
            HRN_MANIFEST_DB_ADD(manifest, .name = "postgres", .id = 16385, .lastSystemId = 99999);
            HRN_MANIFEST_DB_ADD(manifest, .name = "template0", .id = 12168, .lastSystemId = 99999);
            HRN_MANIFEST_DB_ADD(manifest, .name = "template1", .id = 1, .lastSystemId = 99999);
            HRN_MANIFEST_DB_ADD(manifest, .name = "user-made-system-db", .id = 16380, .lastSystemId = 99999);
            HRN_MANIFEST_DB_ADD(manifest, .name = UTF8_DB_NAME, .id = 16384, .lastSystemId = 99999);
            HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" PG_PATH_BASE "/1/" PG_FILE_PGVERSION);
            HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" PG_PATH_BASE "/16381/" PG_FILE_PGVERSION);
            HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" PG_PATH_BASE "/16385/" PG_FILE_PGVERSION);
        }
        MEM_CONTEXT_END();
 
        TEST_ERROR(restoreSelectiveExpression(manifest), DbMissingError, "database to include '" UTF8_DB_NAME "' does not exist");
 
        TEST_RESULT_LOG("P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16385)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("database id to exclude is missing on disk");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbExclude, UTF8_DB_NAME);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(restoreSelectiveExpression(manifest), DbMissingError, "database to exclude '" UTF8_DB_NAME "' does not exist");
 
        TEST_RESULT_LOG("P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16385)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("all databases selected");
 
        MEM_CONTEXT_BEGIN(manifest->pub.memContext)
        {
            HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" PG_PATH_BASE "/16384/" PG_FILE_PGVERSION);
        }
        MEM_CONTEXT_END();
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, UTF8_DB_NAME);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR(restoreSelectiveExpression(manifest), NULL, "all databases selected");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385)\n"
            "P00   INFO: nothing to filter - all user databases have been selected");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on system database selected");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, "1");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreSelectiveExpression(manifest), DbInvalidError,
            "system databases (template0, postgres, etc.) are included by default");
 
        TEST_RESULT_LOG("P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on system database with non-systemId selected");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, "16385");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreSelectiveExpression(manifest), DbInvalidError,
            "system databases (template0, postgres, etc.) are included by default");
 
        TEST_RESULT_LOG("P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on system database with non-systemId selected, by name");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, "postgres");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreSelectiveExpression(manifest), DbInvalidError,
            "system databases (template0, postgres, etc.) are included by default");
 
        TEST_RESULT_LOG("P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on missing database selected");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, "7777777");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(restoreSelectiveExpression(manifest), DbMissingError, "database to include '7777777' does not exist");
 
        TEST_RESULT_LOG("P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("select database by id");
 
        MEM_CONTEXT_BEGIN(manifest->pub.memContext)
        {
            HRN_MANIFEST_DB_ADD(manifest, .name = "test2", .id = 32768, .lastSystemId = 99999);
            HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" PG_PATH_BASE "/32768/" PG_FILE_PGVERSION);
        }
        MEM_CONTEXT_END();
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, "16384");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(restoreSelectiveExpression(manifest), "(^pg_data/base/32768/)", "check expression");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768)\n"
            "P00 DETAIL: databases excluded (zeroed) from selective restore (32768)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("one database selected without tablespace id");
 
        MEM_CONTEXT_BEGIN(manifest->pub.memContext)
        {
            HRN_MANIFEST_TARGET_ADD(
                manifest, .name = MANIFEST_TARGET_PGTBLSPC "/16387", .tablespaceId = 16387, .tablespaceName = "ts1",
                .path = "/ts1");
        }
        MEM_CONTEXT_END();
 
        TEST_RESULT_STR_Z(
            restoreSelectiveExpression(manifest), "(^pg_data/base/32768/)|(^pg_tblspc/16387/PG_18_202506291/32768/)",
            "check expression");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768)\n"
            "P00 DETAIL: databases excluded (zeroed) from selective restore (32768)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("one database selected with tablespace id");
 
        manifest->pub.data.pgVersion = PG_VERSION_18;
        manifest->pub.data.pgCatalogVersion = hrnPgCatalogVersion(PG_VERSION_18);
 
        MEM_CONTEXT_BEGIN(manifest->pub.memContext)
        {
            HRN_MANIFEST_DB_ADD(manifest, .name = "test3", .id = 65536, .lastSystemId = 99999);
            HRN_MANIFEST_FILE_ADD(manifest, .name = MANIFEST_TARGET_PGTBLSPC "/16387/PG_18_202506291/65536/" PG_FILE_PGVERSION);
        }
        MEM_CONTEXT_END();
 
        TEST_RESULT_STR_Z(
            restoreSelectiveExpression(manifest),
            "(^pg_data/base/32768/)|(^pg_tblspc/16387/PG_18_202506291/32768/)|(^pg_data/base/65536/)"
            "|(^pg_tblspc/16387/PG_18_202506291/65536/)",
            "check expression");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768, 65536)\n"
            "P00 DETAIL: databases excluded (zeroed) from selective restore (32768, 65536)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exclude database by id");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbExclude, "16384");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreSelectiveExpression(manifest),
            "(^pg_data/base/16384/)|(^pg_tblspc/16387/PG_18_202506291/16384/)",
            "check expression");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768, 65536)\n"
            "P00 DETAIL: databases excluded (zeroed) from selective restore (16384)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exclude database by name");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbExclude, UTF8_DB_NAME);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreSelectiveExpression(manifest),
            "(^pg_data/base/16384/)|(^pg_tblspc/16387/PG_18_202506291/16384/)",
            "check expression");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768, 65536)\n"
            "P00 DETAIL: databases excluded (zeroed) from selective restore (16384)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("exclude system database");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbExclude, "16385");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreSelectiveExpression(manifest),
            "(^pg_data/base/16385/)|(^pg_tblspc/16387/PG_18_202506291/16385/)",
            "check expression");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768, 65536)\n"
            "P00 DETAIL: databases excluded (zeroed) from selective restore (16385)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on missing database to exclude selected");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbExclude, "7777777");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(restoreSelectiveExpression(manifest), DbMissingError, "database to exclude '7777777' does not exist");
 
        TEST_RESULT_LOG("P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768, 65536)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on combining include and exclude options");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, "test2");
        hrnCfgArgRawZ(argList, cfgOptDbExclude, "test2");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(restoreSelectiveExpression(manifest), DbInvalidError, "database to include '32768' is in the exclude list");
 
        TEST_RESULT_LOG("P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768, 65536)");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("combine include and exclude options");
 
        argList = strLstDup(argListClean);
        hrnCfgArgRawZ(argList, cfgOptDbInclude, "16384");
        hrnCfgArgRawZ(argList, cfgOptDbExclude, "1");
        hrnCfgArgRawZ(argList, cfgOptDbExclude, "16385");
        hrnCfgArgRawZ(argList, cfgOptDbExclude, "32768");  // user databases excluded will be silently ignored
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreSelectiveExpression(manifest),
            "(^pg_data/base/1/)|(^pg_tblspc/16387/PG_18_202506291/1/)|"
            "(^pg_data/base/16385/)|(^pg_tblspc/16387/PG_18_202506291/16385/)|"
            "(^pg_data/base/32768/)|(^pg_tblspc/16387/PG_18_202506291/32768/)|"
            "(^pg_data/base/65536/)|(^pg_tblspc/16387/PG_18_202506291/65536/)",
            "check expression");
 
        TEST_RESULT_LOG(
            "P00 DETAIL: databases found for selective restore (1, 12168, 16380, 16381, 16384, 16385, 32768, 65536)\n"
            "P00 DETAIL: databases excluded (zeroed) from selective restore (1, 16385, 32768, 65536)");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreRecoveryOption() and restoreRecoveryConf()"))
    {
        StringList *argBaseList = strLstNew();
        hrnCfgArgRawZ(argBaseList, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argBaseList, cfgOptRepoPath, "/repo");
        hrnCfgArgRawZ(argBaseList, cfgOptPgPath, "/pg");
 
        const String *restoreLabel = STRDEF("LABEL");
        #define RECOVERY_SETTING_HEADER                             "# Recovery settings generated by pgBackRest restore on LABEL\n"
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("user-specified options");
 
        StringList *argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptRecoveryOption, "a-setting=a");
        hrnCfgArgRawZ(argList, cfgOptRecoveryOption, "b_setting=b");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_18, restoreLabel),
            RECOVERY_SETTING_HEADER
            "a_setting = 'a'\n"
            "b_setting = 'b'\n"
            "restore_command = '" TEST_PROJECT_EXE " --beta --lock-path=" HRN_PATH "/lock --log-path=" HRN_PATH " --pg1-path=/pg"
            " --repo1-path=/repo --stanza=test1 archive-get %f \"%p\"'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("user-specified cmd");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptCmd, "/usr/local/bin/pg_wrapper.sh");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_18, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = '/usr/local/bin/pg_wrapper.sh --beta --lock-path=" HRN_PATH "/lock --log-path=" HRN_PATH
            " --pg1-path=/pg --repo1-path=/repo --stanza=test1 archive-get %f \"%p\"'\n",
            "restore_command invokes /usr/local/bin/pg_wrapper.sh per --cmd option");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("override restore_command");
 
        hrnCfgArgRawZ(argBaseList, cfgOptRecoveryOption, "restore-command=my_restore_command");
        argList = strLstDup(argBaseList);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_18, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery target immediate, pg < 12");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "immediate");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_11, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "recovery_target = 'immediate'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery target immediate, pg >= 12");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "immediate");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_12, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "recovery_target = 'immediate'\n"
            "recovery_target_timeline = 'current'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery target time with timeline");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "time");
        hrnCfgArgRawZ(argList, cfgOptTarget, "TIME");
        hrnCfgArgRawZ(argList, cfgOptTargetTimeline, "3");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_11, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "recovery_target_time = 'TIME'\n"
            "recovery_target_timeline = '3'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery target inclusive");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "time");
        hrnCfgArgRawZ(argList, cfgOptTarget, "TIME");
        hrnCfgArgRawBool(argList, cfgOptTargetExclusive, true);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_18, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "recovery_target_time = 'TIME'\n"
            "recovery_target_inclusive = 'false'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("no recovery_target_inclusive for target=name");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "name");
        hrnCfgArgRawZ(argList, cfgOptTarget, "NAME");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_18, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "recovery_target_name = 'NAME'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery target lsn");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "lsn");
        hrnCfgArgRawZ(argList, cfgOptTarget, "5218/5E35BBA8");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_10, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "recovery_target_lsn = '5218/5E35BBA8'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery target action = pause");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "immediate");
        hrnCfgArgRawZ(argList, cfgOptTargetAction, "promote");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_11, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "recovery_target = 'immediate'\n"
            "recovery_target_action = 'promote'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery type = standby");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "standby");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_11, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "standby_mode = 'on'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery type = standby with timeline");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "standby");
        hrnCfgArgRawZ(argList, cfgOptTargetTimeline, "current");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_11, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "standby_mode = 'on'\n",
            "check recovery options");
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_12, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "recovery_target_timeline = 'current'\n",
            "check recovery options");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error when archive-mode set on PG < 12");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptArchiveMode, "off");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreRecoveryConf(PG_VERSION_11, restoreLabel), OptionInvalidError,
            "option 'archive-mode' is not supported on PostgreSQL < 12\n"
            "HINT: 'archive_mode' should be manually set to 'off' in postgresql.conf.");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("recovery type = standby with recovery GUCs and archive-mode=off");
 
        argList = strLstDup(argBaseList);
        hrnCfgArgRawZ(argList, cfgOptType, "standby");
        hrnCfgArgRawZ(argList, cfgOptArchiveMode, "off");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_STR_Z(
            restoreRecoveryConf(PG_VERSION_12, restoreLabel),
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n"
            "archive_mode = 'off'\n",
            "check recovery options");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreRecoveryWrite*()"))
    {
        const String *pgPath = STRDEF(TEST_PATH "/pg");
        const StorageInfo fileInfo = {.user = NULL, .group = NULL, .mode = 0600};
        HRN_STORAGE_PATH_CREATE(storageTest, strZ(pgPath), .mode = 0700);
 
        const String *restoreLabel = STRDEF("LABEL");
        #define RECOVERY_SETTING_PREFIX                             "# Removed by pgBackRest restore on LABEL # "
 
        Manifest *manifest = testManifestMinimal(STRDEF("20161219-212741F"), PG_VERSION_12, STRDEF("/pg"));
        manifest->pub.data.backupOptionOnline = true;
        manifest->pub.data.archiveStart = strNewZ("000000010000000000000007");
        manifest->pub.data.lsnStart = strNewZ("0/7000028");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error when standby_mode setting is present");
 
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, "/repo");
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "default");
        hrnCfgArgRawZ(argList, cfgOptRecoveryOption, "standby-mode=on");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            restoreRecoveryWriteAutoConf(&fileInfo, PG_VERSION_12, restoreLabel), OptionInvalidError,
            "'standby_mode' setting is not valid for PostgreSQL >= 12\n"
            "HINT: use --type=standby instead of --recovery-option=standby_mode=on.");
 
        TEST_RESULT_LOG("P00   WARN: postgresql.auto.conf does not exist -- creating to contain recovery settings");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("PG12 restore missing postgresql.auto.conf");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, "/repo");
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "none");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        restoreRecoveryWriteAutoConf(&fileInfo, PG_VERSION_12, restoreLabel);
 
        TEST_STORAGE_GET_EMPTY(storagePg(), PG_FILE_POSTGRESQLAUTOCONF, .comment = "check postgresql.auto.conf");
        TEST_STORAGE_LIST(
            storagePg(), NULL, PG_FILE_POSTGRESQLAUTOCONF "\n", .comment = "recovery.signal exists, standby.signal missing");
 
        TEST_RESULT_LOG(
            "P00   WARN: postgresql.auto.conf does not exist -- creating to contain recovery settings\n"
            "P00   INFO: write " TEST_PATH "/pg/postgresql.auto.conf");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("PG12 restore type none");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        HRN_STORAGE_PUT_Z(
            storagePgWrite(), PG_FILE_POSTGRESQLAUTOCONF,
            "# DO NOT MODIFY\n"
            "\t recovery_target_action='promote'\n\n");
 
        restoreRecoveryWriteAutoConf(&fileInfo, PG_VERSION_12, restoreLabel);
 
        TEST_STORAGE_GET(
            storagePg(), PG_FILE_POSTGRESQLAUTOCONF,
            "# DO NOT MODIFY\n"
            RECOVERY_SETTING_PREFIX "\t recovery_target_action='promote'\n\n",
            .comment = "check postgresql.auto.conf");
        TEST_STORAGE_LIST(
            storagePg(), NULL, PG_FILE_POSTGRESQLAUTOCONF "\n", .comment = "recovery.signal exists, standby.signal missing");
 
        TEST_RESULT_LOG("P00   INFO: write updated " TEST_PATH "/pg/postgresql.auto.conf");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("PG12 restore type none and offline");
 
        manifest->pub.data.backupOptionOnline = false;
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        HRN_STORAGE_PUT_Z(
            storagePgWrite(), PG_FILE_POSTGRESQLAUTOCONF,
            "# DO NOT MODIFY\n"
            "\t recovery_target_action='promote'\n\n");
 
        restoreRecoveryWriteAutoConf(&fileInfo, PG_VERSION_12, restoreLabel);
 
        TEST_STORAGE_GET(
            storagePg(), PG_FILE_POSTGRESQLAUTOCONF,
            "# DO NOT MODIFY\n"
            RECOVERY_SETTING_PREFIX "\t recovery_target_action='promote'\n\n",
            .comment = "check postgresql.auto.conf");
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            PG_FILE_POSTGRESQLAUTOCONF "\n",
            .comment = "recovery.signal missing, standby.signal missing");
 
        TEST_RESULT_LOG("P00   INFO: write updated " TEST_PATH "/pg/postgresql.auto.conf");
 
        manifest->pub.data.backupOptionOnline = true;
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("PG12 restore type standby and remove existing recovery settings");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        HRN_STORAGE_PUT_Z(
            storagePgWrite(), PG_FILE_POSTGRESQLAUTOCONF,
            "# DO NOT MODIFY\n"
            "recovery_target_name\t='name'\n"
            "recovery_target_inclusive = false\n");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, "/repo");
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "standby");
        hrnCfgArgRawZ(argList, cfgOptRecoveryOption, "restore-command=my_restore_command");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        restoreRecoveryWriteAutoConf(&fileInfo, PG_VERSION_12, restoreLabel);
 
        TEST_STORAGE_GET(
            storagePg(), PG_FILE_POSTGRESQLAUTOCONF,
            "# DO NOT MODIFY\n"
            RECOVERY_SETTING_PREFIX "recovery_target_name\t='name'\n"
            RECOVERY_SETTING_PREFIX "recovery_target_inclusive = false\n"
            "\n"
            RECOVERY_SETTING_HEADER
            "restore_command = 'my_restore_command'\n",
            .comment = "check postgresql.auto.conf");
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            PG_FILE_POSTGRESQLAUTOCONF "\n"
            PG_FILE_STANDBYSIGNAL "\n",
            .comment = "recovery.signal missing, standby.signal exists");
 
        TEST_RESULT_LOG("P00   INFO: write updated " TEST_PATH "/pg/postgresql.auto.conf");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("PG12 restore type preserve");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        HRN_STORAGE_PUT_Z(storagePgWrite(), PG_FILE_POSTGRESQLAUTOCONF, "# DO NOT MODIFY\n");
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_STANDBYSIGNAL);
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, "/repo");
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "preserve");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        restoreRecoveryWrite(manifest, &fileInfo);
 
        TEST_STORAGE_GET(
            storagePg(), PG_FILE_POSTGRESQLAUTOCONF, "# DO NOT MODIFY\n", .comment = "check postgresql.auto.conf");
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            PG_FILE_POSTGRESQLAUTOCONF "\n"
            PG_FILE_STANDBYSIGNAL "\n",
            .comment = "recovery.signal missing, standby.signal exists");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("PG12 restore type default");
 
        HRN_SYSTEM_FMT("rm -rf %s/*", strZ(pgPath));
 
        HRN_STORAGE_PUT_Z(storagePgWrite(), PG_FILE_POSTGRESQLAUTOCONF, "# DO NOT MODIFY\n");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRawZ(argList, cfgOptRepoPath, "/repo");
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        restoreRecoveryWrite(manifest, &fileInfo);
 
        TEST_RESULT_BOOL(
            bufEq(storageGetP(storageNewReadP(storagePg(), PG_FILE_POSTGRESQLAUTOCONF_STR)), BUFSTRDEF("# DO NOT MODIFY\n")),
            false, "check postgresql.auto.conf has changed");
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            PG_FILE_POSTGRESQLAUTOCONF "\n"
            PG_FILE_RECOVERYSIGNAL "\n",
            .comment = "recovery.signal exists, standby.signal missing");
 
        TEST_RESULT_LOG("P00   INFO: write updated " TEST_PATH "/pg/postgresql.auto.conf");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("timeline*()"))
    {
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, STRDEF(TEST_PATH "/repo"));
        hrnCfgArgRawZ(argList, cfgOptPgPath, TEST_PATH "/pg");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("timelineVerify()");
 
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_11, 1, 0xA1, NULL, CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "follow current timeline because of version");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_11, 1, 0xA1, STRDEF("latest"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "follow latest timeline as requested");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 1, 0xA1, NULL, CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "follow latest timeline because of version");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 1, 0xA1, STRDEF("current"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "follow current timeline as requested");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 1, 0xA1, STRDEF("1"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "follow requested timeline (same as current)");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 0x10, 0xA1, STRDEF("0x10"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "follow requested hex timeline (same as current)");
        TEST_ERROR(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 0x10, 0xA1, STRDEF("bogus"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            DbMismatchError, "invalid target timeline 'bogus'");
 
        HRN_STORAGE_PUT_Z(storageTest, "repo/archive/test1/17-1/00000009.history", "8");
        TEST_ERROR(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 8, 0xA1, STRDEF("9"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            FormatError,
            "invalid timeline '9' at '" TEST_PATH "/repo/archive/test1/17-1/00000009.history':"
            " invalid history line format '8'");
 
        HRN_STORAGE_PUT_Z(
            storageTest, "repo/archive/test1/17-1/0000000A.history",
            "# comment\n"
            "8\t0/4000000\tcomment\n"
            "9\t0/5000000\tcomment\n");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 10, 0x4FFFFFF, NULL, CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "follow current timeline");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 9, 0x4FFFFFF, NULL, CFGOPTVAL_RESTORE_TYPE_IMMEDIATE,
                cipherSpecMapNew()),
            "follow current timeline (based on type immediate)");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 9, 0x4FFFFFF, NULL, CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "follow latest timeline");
        TEST_RESULT_VOID(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 9, 0x4FFFFFF, STRDEF("10"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            "target timeline found");
        TEST_ERROR(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 9, 0x6000000, STRDEF("10"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            DbMismatchError,
            "target timeline A forked from backup timeline 9 at 0/5000000 which is before backup lsn of 0/6000000\n"
            "HINT: was the target timeline created by accidentally promoting a standby?\n"
            "HINT: was the target timeline created by testing a restore without --archive-mode=off?\n"
            "HINT: was the backup made after the target timeline was created?");
 
        HRN_STORAGE_PUT_Z(
            storageTest, "repo/archive/test1/17-1/0000000B.history",
            "7\t0/4000000\tcomment\n"
            "8\t0/5000000\tcomment\n");
        TEST_ERROR(
            timelineVerify(
                storageRepoIdx(0), STRDEF("17-1"), PG_VERSION_12, 6, 0x4FFFFFF, STRDEF("11"), CFGOPTVAL_RESTORE_TYPE_DEFAULT,
                cipherSpecMapNew()),
            DbMismatchError, "backup timeline 6, lsn 0/4ffffff is not in the history of target timeline B\n"
            "HINT: was the target timeline created by promoting from a timeline < latest?");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("restoreJobResult()"))
    {
        // Set log level to detail
        harnessLogLevelSet(logLevelDetail);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("report host/100% progress on noop result");
 
        // Create job that skips file
        ProtocolParallelJob *job = protocolParallelJobNew(VARSTRDEF("pg_data/test"), strIdFromZ("x"), NULL);
 
        PackWrite *const resultPack = protocolPackNew();
        pckWriteStrP(resultPack, STRDEF("pg_data/test"));
        pckWriteU32P(resultPack, restoreResultZero);
        // No more fields need to be written since noop will ignore them anyway
        pckWriteEndP(resultPack);
 
        protocolParallelJobResultSet(job, pckReadNew(pckWriteResult(resultPack)));
 
        // Create manifest with file
        Manifest *manifest = NULL;
 
        OBJ_NEW_BASE_BEGIN(Manifest, .childQty = MEM_CONTEXT_QTY_MAX)
        {
            manifest = manifestNewInternal();
            manifest->pub.data.backupTimestampStart = 1;
 
            HRN_MANIFEST_TARGET_ADD(manifest, .name = MANIFEST_TARGET_PGDATA, .path = "pg_data");
            HRN_MANIFEST_FILE_ADD(manifest, .name = "pg_data/test");
        }
        OBJ_NEW_END();
 
        unsigned int currentPercentComplete = 4567;
        unsigned int checksumErrorFileTotal = 0;
 
        lockInit(TEST_PATH_STR, cfgOptionStr(cfgOptExecId));
        TEST_RESULT_VOID(cmdLockAcquireP(), "acquire restore lock");
 
        TEST_RESULT_UINT(
            restoreJobResult(manifest, job, NULL, 0, 0,
                             &checksumErrorFileTotal, &currentPercentComplete), 0, "log noop result");
        TEST_RESULT_VOID(cmdLockReleaseP(), "release restore lock");
 
        TEST_RESULT_LOG("P00 DETAIL: restore file pg_data/test (0B, 100.00%)");
    }
 
    // *****************************************************************************************************************************
    if (testBegin("cmdRestore()"))
    {
        const String *pgPath = STRDEF(TEST_PATH "/pg");
        const String *repoPath = STRDEF(TEST_PATH "/repo");
        const String *repoPathEncrypt = STRDEF(TEST_PATH "/repo-encrypt");
 
        // Set log level to detail
        harnessLogLevelSet(logLevelDetail);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("verify next queue calculations");
 
        TEST_RESULT_INT(restoreJobQueueNext(0, 0, 1), 0, "client idx 0, queue idx 0, 1 queue");
        TEST_RESULT_INT(restoreJobQueueNext(0, 0, 2), 1, "client idx 0, queue idx 0, 2 queues");
        TEST_RESULT_INT(restoreJobQueueNext(1, 1, 2), 0, "client idx 1, queue idx 1, 2 queues");
        TEST_RESULT_INT(restoreJobQueueNext(0, 1, 2), 0, "client idx 0, queue idx 1, 2 queues");
        TEST_RESULT_INT(restoreJobQueueNext(1, 0, 2), 1, "client idx 1, queue idx 0, 2 queues");
 
        // Locality error
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("incorrect locality");
 
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptPgHost, "pg1");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(hrnCmdRestore(), HostInvalidError, "restore command must be run on the PostgreSQL host");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("full restore without delta, multi-repo");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPathEncrypt);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptSet, "20161219-212741F");
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 2, cipherTypeAes256Cbc);
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, TEST_CIPHER_PASS);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        #define TEST_LABEL                                          "20161219-212741F"
        #define TEST_PGDATA                                         MANIFEST_TARGET_PGDATA "/"
        #define TEST_REPO_PATH                                      STORAGE_REPO_BACKUP "/" TEST_LABEL "/" TEST_PGDATA
 
        Manifest *manifest = NULL;
 
        OBJ_NEW_BASE_BEGIN(Manifest, .childQty = MEM_CONTEXT_QTY_MAX)
        {
            manifest = manifestNewInternal();
            manifest->pub.info = infoNew(REPOSITORY_FORMAT_DEFAULT, NULL);
            manifest->pub.data.backupLabel = strNewZ(TEST_LABEL);
            manifest->pub.data.pgVersion = PG_VERSION_11;
            manifest->pub.data.pgCatalogVersion = hrnPgCatalogVersion(PG_VERSION_11);
            manifest->pub.data.backupType = backupTypeFull;
            manifest->pub.data.backupTimestampStart = 1482182860;
            manifest->pub.data.backupTimestampCopyStart = 1482182861; // So file timestamps should be less than this
            manifest->pub.data.backupOptionOnline = true;
            manifest->pub.data.archiveStart = strNewZ("000000010000000000000007");
            manifest->pub.data.lsnStart = strNewZ("0/7000028");
 
            // Data directory
            HRN_MANIFEST_TARGET_ADD(manifest, .name = MANIFEST_TARGET_PGDATA, .path = strZ(pgPath));
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGDATA);
 
            // Global directory
            HRN_MANIFEST_PATH_ADD(manifest, .name = TEST_PGDATA PG_PATH_GLOBAL);
 
            // PG_VERSION
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA PG_FILE_PGVERSION, .size = 3, .timestamp = 1482182860,
                .checksumSha1 = "dd71038f3463f511ee7403dbcbc87195302d891c");
            HRN_STORAGE_PUT_Z(storageRepoIdxWrite(0), TEST_REPO_PATH PG_FILE_PGVERSION, PG_VERSION_11_Z "\n");
 
            // Store the file also to the encrypted repo
            HRN_STORAGE_PUT_Z(
                storageRepoIdxWrite(1), TEST_REPO_PATH PG_FILE_PGVERSION, PG_VERSION_11_Z "\n",
                .cipherSpec = TEST_CIPHER_SPEC_PASS(TEST_CIPHER_PASS_ARCHIVE));
 
            // pg_tblspc
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" MANIFEST_TARGET_PGTBLSPC);
 
            // Always sort
            lstSort(manifest->pub.targetList, sortOrderAsc);
            lstSort(manifest->pub.fileList, sortOrderAsc);
            lstSort(manifest->pub.linkList, sortOrderAsc);
            lstSort(manifest->pub.pathList, sortOrderAsc);
        }
        OBJ_NEW_END();
 
        manifestSave(
            manifest,
            storageWriteIo(
                storageNewWriteP(storageRepoIdxWrite(0), STRDEF(STORAGE_REPO_BACKUP "/" TEST_LABEL "/" BACKUP_MANIFEST_FILE))));
 
        // Read the manifest, set a cipher passphrase and store it to the encrypted repo
        Manifest *manifestEncrypted = manifestLoadFile(
            storageRepoIdxWrite(0), STRDEF(STORAGE_REPO_BACKUP "/" TEST_LABEL "/" BACKUP_MANIFEST_FILE),
            cipherSpecNewNone());
        manifestCipherSpecSet(
            manifestEncrypted, cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(TEST_CIPHER_PASS_ARCHIVE), .digest = hashTypeSha1));
 
        // Open file for write
        IoWrite *write = storageWriteIo(
            storageNewWriteP(
                storageRepoIdxWrite(1),
                STRDEF(STORAGE_REPO_BACKUP "/" TEST_LABEL "/" BACKUP_MANIFEST_FILE)));
 
        // Add encryption filter and save the encrypted manifest
        #define TEST_CIPHER_PASS_MANIFEST "backpass"
        cipherBlockFilterGroupAdd(
            ioWriteFilterGroup(write), cipherModeEncrypt,
            cipherSpecNewP(
                cfgOptionIdxStrId(cfgOptRepoCipherType, 1), BUFSTRDEF(TEST_CIPHER_PASS_MANIFEST), .digest = hashTypeSha1));
        manifestSave(manifestEncrypted, write);
 
        // Write backup.info to the encrypted repo
        HRN_INFO_PUT(
            storageRepoIdxWrite(1), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO "\n[cipher]\ncipher-pass=\""
            TEST_CIPHER_PASS_MANIFEST "\"\n\n" TEST_RESTORE_BACKUP_INFO_DB, .cipherSpec = TEST_CIPHER_SPEC);
 
        // Write archive.info to the encrypted repo
        CipherSpecMap *const cipherSpecMapArchive = cipherSpecMapNew();
        cipherSpecMapAdd(
            cipherSpecMapArchive, CIPHER_SPEC_MAP_ID_DEFAULT_STR,
            cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(TEST_CIPHER_PASS_ARCHIVE), .digest = hashTypeSha1));
 
        InfoArchive *infoArchive = infoArchiveNew(
            PG_VERSION_11, 6569239123849665679, REPOSITORY_FORMAT_DEFAULT, cipherSpecMapArchive);
        infoArchiveSaveFile(
            infoArchive, storageRepoIdxWrite(1), INFO_ARCHIVE_PATH_FILE_STR,
            cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(TEST_CIPHER_PASS)));
 
        TEST_RESULT_VOID(hrnCmdRestore(), "successful restore");
 
        TEST_RESULT_LOG(
            zNewFmt(
                "P00   WARN: repo1: [FileMissingError] unable to load info file"
                " '%s/repo/backup/test1/backup.info' or '%s/repo/backup/test1/backup.info.copy':\n"
                "            FileMissingError: unable to open missing file '%s/repo/backup/test1/backup.info' for read\n"
                "            FileMissingError: unable to open missing file '%s/repo/backup/test1/backup.info.copy' for read\n"
                "            HINT: backup.info cannot be opened and is required to perform a backup.\n"
                "            HINT: has a stanza-create been performed?\n"
                "P00   INFO: repo2: restore backup set 20161219-212741F, recovery will start at 2016-12-19 21:27:40\n"
                "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
                "P00 DETAIL: create path '" TEST_PATH "/pg/global'\n"
                "P00 DETAIL: create path '" TEST_PATH "/pg/pg_tblspc'\n"
                "P01 DETAIL: restore file " TEST_PATH "/pg/PG_VERSION (3B, 100.00%%) checksum"
                " dd71038f3463f511ee7403dbcbc87195302d891c\n"
                "P00   INFO: write " TEST_PATH "/pg/recovery.conf\n"
                "P00 DETAIL: sync path '" TEST_PATH "/pg'\n"
                "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc'\n"
                "P00   WARN: backup does not contain 'global/pg_control' -- cluster will not start\n"
                "P00 DETAIL: sync path '" TEST_PATH "/pg/global'\n"
                "P00   INFO: restore size = 3B, file total = 1",
                TEST_PATH, TEST_PATH, TEST_PATH, TEST_PATH));
 
        // Remove recovery.conf before file comparison since it will have a new timestamp. Make sure it existed, though.
        HRN_STORAGE_REMOVE(storagePgWrite(), PG_FILE_RECOVERYCONF, .errorOnMissing = true);
 
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            "./\n"
            "PG_VERSION {s=3, t=1482182860}\n"
            "global/\n"
            "pg_tblspc/\n",
            .level = storageInfoLevelBasic, .includeDot = true);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on invalid timeline");
 
        // Store backup.info to repo1 - repo1 will be selected because of the priority order
        HRN_INFO_PUT(storageRepoIdxWrite(0), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO "\n" TEST_RESTORE_BACKUP_INFO_DB);
 
        // Store archive.info to repo1 - repo1 will be selected because of the priority order
        infoArchive = infoArchiveNew(PG_VERSION_11, 6569239123849665679, REPOSITORY_FORMAT_DEFAULT, NULL);
        infoArchiveSaveFile(
            infoArchive, storageRepoIdxWrite(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone());
 
        hrnCfgArgRawZ(argList, cfgOptTargetTimeline, "0xff");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_ERROR(
            cmdRestore(), FileMissingError,
            "unable to open missing file '" TEST_PATH "/repo/archive/test1/11-0/000000FF.history' for read");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("full restore with delta force");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 1, repoPath);
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPathEncrypt);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "preserve");
        hrnCfgArgRawZ(argList, cfgOptSet, "20161219-212741F");
        hrnCfgArgRawBool(argList, cfgOptDelta, true);
        hrnCfgArgRawBool(argList, cfgOptForce, true);
        hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 2, cipherTypeAes256Cbc);
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, TEST_CIPHER_PASS);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // Munge PGDATA mode so it gets fixed
        HRN_STORAGE_MODE(storagePg(), NULL, 0777);
 
        // Make sure existing backup.manifest file is ignored
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), BACKUP_MANIFEST_FILE);
 
        // Add a bogus file that will be removed
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), "bogus-file");
 
        // Add a special file that will be removed
        HRN_SYSTEM_FMT("mkfifo %s/pipe", strZ(pgPath));
 
        // Modify time of postgresql.conf so it will be copied even though content is the same
        HRN_STORAGE_PUT_Z(storagePgWrite(), "postgresql.conf", "VALID_CONF", .modeFile = 0600);
 
        // Modify time of postgresql.auto.conf so it will be copied even though content is the same and timestamp matches
        HRN_STORAGE_PUT_Z(
            storagePgWrite(), "postgresql.auto.conf", "VALID_CONF_AUTO", .modeFile = 0600, .timeModified = 1482182861);
 
        // Size mismatch
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), "size-mismatch", .modeFile = 0600);
 
        // Overwrite PG_VERSION with bogus content that will not be detected by delta force because the time and size are the same
        HRN_STORAGE_PUT_Z(storagePgWrite(), PG_FILE_PGVERSION, "BG\n", .modeFile = 0600, .timeModified = 1482182860);
 
        // Change destination of tablespace link
        THROW_ON_SYS_ERROR(
            symlink("/bogus", zNewFmt("%s/pg_tblspc/1", strZ(pgPath))) == -1, FileOpenError, "unable to create symlink");
 
        MEM_CONTEXT_BEGIN(manifest->pub.memContext)
        {
            // tablespace_map (will be ignored during restore)
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA PG_FILE_TABLESPACEMAP, .timestamp = 1482182860, .checksumSha1 = HASH_TYPE_SHA1_ZERO);
            HRN_STORAGE_PUT_EMPTY(storageRepoWrite(), TEST_REPO_PATH PG_FILE_TABLESPACEMAP);
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = MANIFEST_TARGET_PGDATA "/postgresql.conf", .size = 10, .timestamp = 1482182860,
                .checksumSha1 = "1a49a3c2240449fee1422e4afcf44d5b96378511");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_REPO_PATH "/postgresql.conf", "VALID_CONF");
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = MANIFEST_TARGET_PGDATA "/postgresql.auto.conf", .size = 15, .timestamp = 1482182861,
                .checksumSha1 = "37a0c84d42c3ec3d08c311cec2cef2a7ab55a7c3");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_REPO_PATH "/postgresql.auto.conf", "VALID_CONF_AUTO");
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = MANIFEST_TARGET_PGDATA "/size-mismatch", .size = 1, .timestamp = 1482182861,
                .checksumSha1 = "c032adc1ff629c9b66f22749ad667e6beadf144b");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_REPO_PATH "/size-mismatch", "X");
 
            // pg_tblspc/1
            HRN_MANIFEST_TARGET_ADD(
                manifest, .type = manifestTargetTypeLink, .name = MANIFEST_TARGET_PGTBLSPC "/1", .path = TEST_PATH "/ts/1",
                .tablespaceId = 1, .tablespaceName = "ts1");
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGTBLSPC);
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGTBLSPC "/1");
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGTBLSPC "/1/PG_11_201809051");
            HRN_MANIFEST_LINK_ADD(
                manifest, .name = MANIFEST_TARGET_PGDATA "/" MANIFEST_TARGET_PGTBLSPC "/1", .destination = TEST_PATH "/ts/1");
 
            // pg_tblspc/1/16384 path
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGTBLSPC "/1/16384");
 
            // pg_tblspc/1/16384/PG_VERSION
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = MANIFEST_TARGET_PGTBLSPC "/1/16384/" PG_FILE_PGVERSION, .size = 3,
                .timestamp = 1482182860, .checksumSha1 = "dd71038f3463f511ee7403dbcbc87195302d891c");
 
            HRN_STORAGE_PUT_Z(
                storageRepoWrite(), STORAGE_REPO_BACKUP "/" TEST_LABEL "/" MANIFEST_TARGET_PGTBLSPC "/1/16384/" PG_FILE_PGVERSION,
                PG_VERSION_11_Z "\n");
 
            // Always sort
            lstSort(manifest->pub.targetList, sortOrderAsc);
            lstSort(manifest->pub.fileList, sortOrderAsc);
            lstSort(manifest->pub.linkList, sortOrderAsc);
            lstSort(manifest->pub.pathList, sortOrderAsc);
        }
        MEM_CONTEXT_END();
 
        manifestSave(
            manifest,
            storageWriteIo(
                storageNewWriteP(storageRepoWrite(), STRDEF(STORAGE_REPO_BACKUP "/" TEST_LABEL "/" BACKUP_MANIFEST_FILE))));
 
        #undef TEST_LABEL
        #undef TEST_PGDATA
        #undef TEST_REPO_PATH
 
        hrnCmdRestore();
 
        TEST_RESULT_LOG(
            "P00   INFO: repo1: restore backup set 20161219-212741F, recovery will start at 2016-12-19 21:27:40\n"
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/ts/1/PG_11_201809051' exists\n"
            "P00 DETAIL: skip 'tablespace_map' -- tablespace links will be created based on mappings\n"
            "P00   INFO: remove invalid files/links/paths from '" TEST_PATH "/pg'\n"
            "P00 DETAIL: update mode for '" TEST_PATH "/pg' to 0700\n"
            "P00 DETAIL: remove invalid file '" TEST_PATH "/pg/bogus-file'\n"
            "P00 DETAIL: remove link '" TEST_PATH "/pg/pg_tblspc/1' because destination changed\n"
            "P00 DETAIL: remove special file '" TEST_PATH "/pg/pipe'\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_tblspc/1' to '" TEST_PATH "/ts/1'\n"
            "P00 DETAIL: create path '" TEST_PATH "/pg/pg_tblspc/1/16384'\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/postgresql.auto.conf (15B, 46.88%)"
            " checksum 37a0c84d42c3ec3d08c311cec2cef2a7ab55a7c3\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/postgresql.conf (10B, 78.13%) checksum"
            " 1a49a3c2240449fee1422e4afcf44d5b96378511\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/PG_VERSION - exists and matches size 3 and modification time 1482182860"
            " (3B, 87.50%) checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/size-mismatch (1B, 90.63%) checksum"
            " c032adc1ff629c9b66f22749ad667e6beadf144b\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/pg_tblspc/1/16384/PG_VERSION (3B, 100.00%)"
            " checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P00   WARN: recovery type is preserve but recovery file does not exist at '" TEST_PATH "/pg/recovery.conf'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1/16384'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1/PG_11_201809051'\n"
            "P00   WARN: backup does not contain 'global/pg_control' -- cluster will not start\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/global'\n"
            "P00   INFO: restore size = 32B, file total = 5");
 
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            "./\n"
            "PG_VERSION {s=3, t=1482182860}\n"
            "global/\n"
            "pg_tblspc/\n"
            "pg_tblspc/1> {d=" TEST_PATH "/ts/1}\n"
            "postgresql.auto.conf {s=15, t=1482182861}\n"
            "postgresql.conf {s=10, t=1482182860}\n"
            "size-mismatch {s=1, t=1482182861}\n",
            .level = storageInfoLevelBasic, .includeDot = true);
 
        TEST_STORAGE_LIST(
            storagePg(), "pg_tblspc/1",
            ".> {d=" TEST_PATH "/ts/1}\n"
            "16384/\n"
            "16384/PG_VERSION {s=3, t=1482182860}\n"
            "PG_11_201809051/\n",
            .level = storageInfoLevelBasic, .includeDot = true);
 
        // PG_VERSION was not restored because delta force relies on time and size which were the same in the manifest and on disk
        TEST_STORAGE_GET(storagePg(), PG_FILE_PGVERSION, "BG\n", .comment = "check PG_VERSION was not restored");
 
        // Cleanup
        hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPass, 2);
        HRN_STORAGE_PATH_REMOVE(storageRepoIdxWrite(1), NULL, .recurse = true);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("full restore with force");
 
        // Replace percent complete and restore size since they can cause a lot of churn when files are added/removed
        hrnLogReplaceAdd(", [0-9]{1,3}\\.[0-9]{2}%\\)", "[0-9]+\\.[0-9]+%", "PCT", false);
        hrnLogReplaceAdd(" restore size = [0-9]+(\\.[0-9]+){0,1}[A-Z]+", "[^ ]+$", "SIZE", false);
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptType, "preserve");
        hrnCfgArgRawZ(argList, cfgOptSet, "20161219-212741F");
        hrnCfgArgRawBool(argList, cfgOptForce, true);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        hrnCmdRestore();
 
        TEST_RESULT_LOG(
            "P00   INFO: repo1: restore backup set 20161219-212741F, recovery will start at 2016-12-19 21:27:40\n"
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/ts/1/PG_11_201809051' exists\n"
            "P00 DETAIL: skip 'tablespace_map' -- tablespace links will be created based on mappings\n"
            "P00   INFO: remove invalid files/links/paths from '" TEST_PATH "/pg'\n"
            "P00   INFO: remove invalid files/links/paths from '" TEST_PATH "/ts/1/PG_11_201809051'\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/postgresql.auto.conf (15B, [PCT]) checksum"
            " 37a0c84d42c3ec3d08c311cec2cef2a7ab55a7c3\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/postgresql.conf (10B, [PCT]) checksum"
            " 1a49a3c2240449fee1422e4afcf44d5b96378511\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/PG_VERSION (3B, [PCT]) checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/size-mismatch (1B, [PCT]) checksum"
            " c032adc1ff629c9b66f22749ad667e6beadf144b\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/pg_tblspc/1/16384/PG_VERSION (3B, [PCT])"
            " checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P00   WARN: recovery type is preserve but recovery file does not exist at '" TEST_PATH "/pg/recovery.conf'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1/16384'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1/PG_11_201809051'\n"
            "P00   WARN: backup does not contain 'global/pg_control' -- cluster will not start\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/global'\n"
            "P00   INFO: restore size = [SIZE], file total = 5");
 
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            "./\n"
            "PG_VERSION {s=3, t=1482182860}\n"
            "global/\n"
            "pg_tblspc/\n"
            "pg_tblspc/1> {d=" TEST_PATH "/ts/1}\n"
            "postgresql.auto.conf {s=15, t=1482182861}\n"
            "postgresql.conf {s=10, t=1482182860}\n"
            "size-mismatch {s=1, t=1482182861}\n",
            .level = storageInfoLevelBasic, .includeDot = true);
 
        TEST_STORAGE_LIST(
            storagePg(), "pg_tblspc/1",
            ".> {d=" TEST_PATH "/ts/1}\n"
            "16384/\n"
            "16384/PG_VERSION {s=3, t=1482182860}\n"
            "PG_11_201809051/\n",
            .level = storageInfoLevelBasic, .includeDot = true);
 
        // PG_VERSION was restored by the force option
        TEST_STORAGE_GET(storagePg(), PG_FILE_PGVERSION, PG_VERSION_11_Z "\n", .comment = "check PG_VERSION was restored");
 
        // Remove tablespace
        HRN_STORAGE_PATH_REMOVE(storagePgWrite(), MANIFEST_TARGET_PGTBLSPC "/1/PG_11_201809051", .recurse = true);
 
        // Remove files
        HRN_STORAGE_REMOVE(storagePgWrite(), "postgresql.conf");
        HRN_STORAGE_REMOVE(storagePgWrite(), "postgresql.auto.conf");
        HRN_STORAGE_REMOVE(storagePgWrite(), "size-mismatch");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("incremental delta");
 
        const char *pgControlSha1 = NULL;
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawBool(argList, cfgOptDelta, true);
        hrnCfgArgRawZ(argList, cfgOptType, "none");
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_wal=../wal");
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "postgresql.conf=../config/postgresql.conf");
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_hba.conf=../config/pg_hba.conf");
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_xact=../xact");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        #define TEST_LABEL_FULL                                     "20161219-212741F"
        #define TEST_LABEL_DIFF                                     "20161219-212741F_20161219-212800D"
        #define TEST_LABEL_INCR                                     "20161219-212741F_20161219-212900I"
        #define TEST_LABEL                                          "20161219-212741F_20161219-212918I"
        #define TEST_PGDATA                                         MANIFEST_TARGET_PGDATA "/"
        #define TEST_REPO_PATH                                      STORAGE_REPO_BACKUP "/" TEST_LABEL "/" TEST_PGDATA
 
        OBJ_NEW_BASE_BEGIN(Manifest, .childQty = MEM_CONTEXT_QTY_MAX)
        {
            manifest = manifestNewInternal();
            manifest->pub.info = infoNew(REPOSITORY_FORMAT_DEFAULT, NULL);
            manifest->pub.data.backupLabel = strNewZ(TEST_LABEL);
            manifest->pub.data.backupTimestampStart = 1482182860;
            manifest->pub.data.pgVersion = PG_VERSION_10;
            manifest->pub.data.pgCatalogVersion = hrnPgCatalogVersion(PG_VERSION_10);
            manifest->pub.data.backupType = backupTypeIncr;
            manifest->pub.data.blockIncr = true;
            manifest->pub.data.backupTimestampCopyStart = 1482182861; // So file timestamps should be less than this
            manifest->pub.data.archiveStart = strNewZ("000000010000000000000007");
            manifest->pub.data.lsnStart = strNewZ("0/7000028");
 
            manifest->pub.referenceList = strLstNew();
            strLstAddZ(manifest->pub.referenceList, TEST_LABEL_FULL);
            strLstAddZ(manifest->pub.referenceList, TEST_LABEL_DIFF);
            strLstAddZ(manifest->pub.referenceList, TEST_LABEL_INCR);
            strLstAddZ(manifest->pub.referenceList, TEST_LABEL);
 
            // Data directory
            HRN_MANIFEST_TARGET_ADD(manifest, .name = MANIFEST_TARGET_PGDATA, .path = strZ(pgPath));
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGDATA);
            HRN_STORAGE_PATH_CREATE(storagePgWrite(), NULL, .noErrorOnExists = true);
 
            // global directory
            HRN_MANIFEST_PATH_ADD(manifest, .name = TEST_PGDATA PG_PATH_GLOBAL);
 
            // global/pg_control
            Buffer *fileBuffer = hrnPgControlToBuffer(0, 0, (PgControl){.version = PG_VERSION_10});
            pgControlSha1 = strZ(strNewEncode(encodingHex, cryptoHashOne(hashTypeSha1, fileBuffer)));
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL, .size = 8192, .timestamp = 1482182860,
                .checksumSha1 = pgControlSha1);
            HRN_STORAGE_PUT(storageRepoWrite(), TEST_REPO_PATH PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL, fileBuffer);
 
            // global/888
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA PG_PATH_GLOBAL "/888", .size = 0, .timestamp = 1482182860,
                .checksumSha1 = HASH_TYPE_SHA1_ZERO);
            HRN_STORAGE_PUT_EMPTY(storageRepoWrite(), TEST_REPO_PATH PG_PATH_GLOBAL "/888");
 
            // global/999
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA PG_PATH_GLOBAL "/999", .size = 0, .timestamp = 1482182860,
                .checksumSha1 = HASH_TYPE_SHA1_ZERO);
            HRN_STORAGE_PUT_EMPTY(storageRepoWrite(), TEST_REPO_PATH PG_PATH_GLOBAL "/999");
 
            // PG_VERSION
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA PG_FILE_PGVERSION, .size = 3, .sizeRepo = 3, .timestamp = 1482182860,
                .bundleId = 1, .checksumSha1 = "4143d3a341877154d6e95211464e1df1015b74bd");
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "yyy", .size = 3, .sizeRepo = 3, .timestamp = 1482182860, .bundleId = 1,
                .bundleOffset = 8, .checksumSha1 = "186154712b2d5f6791d85b9a0987b98fa231779c");
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "xxxxx", .size = 5, .sizeRepo = 5, .timestamp = 1482182860,
                .bundleId = 1, .bundleOffset = 11, .checksumSha1 = "9addbf544119efa4a64223b649750a510f0d463f");
            // Set bogus sizeRepo and checksumSha1 to ensure this is not handled as a regular file
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "zero-length", .size = 0, .sizeRepo = 1, .timestamp = 1482182866,
                .bundleId = 1, .bundleOffset = 16, .checksumSha1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "zz", .size = 2, .sizeRepo = 2, .timestamp = 1482182860,
                .bundleId = 1, .bundleOffset = 17, .checksumSha1 = "d7dacae2c968388960bf8970080a980ed5c5dcb7");
            HRN_STORAGE_PUT_Z(
                storageRepoWrite(), STORAGE_REPO_BACKUP "/" TEST_LABEL "/bundle/1",
                PG_VERSION_10_Z "\n\n" PG_VERSION_10_Z "\n\nyyyxxxxxAzzA");
 
            // base directory
            HRN_MANIFEST_PATH_ADD(manifest, .name = TEST_PGDATA PG_PATH_BASE);
 
            // base/1 directory
            HRN_MANIFEST_PATH_ADD(manifest, .name = TEST_PGDATA PG_PATH_BASE "/1");
 
            // base/1/PG_VERSION. File was written as part of bundle 1 above
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/" PG_FILE_PGVERSION, .size = 3, .sizeRepo = 3,
                .timestamp = 1482182860, .bundleId = 1, .bundleOffset = 4,
                .checksumSha1 = "4143d3a341877154d6e95211464e1df1015b74bd");
 
            // base/1/2
            fileBuffer = bufNew(8192);
            memset(bufPtr(fileBuffer), 1, bufSize(fileBuffer));
            bufUsedSet(fileBuffer, bufSize(fileBuffer));
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/2", .size = 8192, .timestamp = 1482182860,
                .checksumSha1 = "4d7b2a36c5387decf799352a3751883b7ceb96aa");
            HRN_STORAGE_PUT(storageRepoWrite(), TEST_REPO_PATH "base/1/2", fileBuffer);
 
            // base/1/10
            fileBuffer = bufNew(8194);
            memset(bufPtr(fileBuffer), 10, bufSize(fileBuffer));
            bufPtr(fileBuffer)[0] = 0xFF;
            bufPtr(fileBuffer)[8193] = 0xFF;
            bufUsedSet(fileBuffer, bufSize(fileBuffer));
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/10", .size = 8192, .sizeRepo = 8192, .timestamp = 1482182860,
                .bundleId = 1, .bundleOffset = 1, .reference = TEST_LABEL_FULL,
                .checksumSha1 = "28757c756c03c37aca13692cb719c18d1510c190");
            HRN_STORAGE_PUT(storageRepoWrite(), STORAGE_REPO_BACKUP "/" TEST_LABEL_FULL "/bundle/1", fileBuffer);
 
            // base/1/20 and base/1/21
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/20", .size = 1, .sizeRepo = 1, .timestamp = 1482182860, .bundleId = 2,
                .bundleOffset = 1, .reference = TEST_LABEL_DIFF, .checksumSha1 = "c032adc1ff629c9b66f22749ad667e6beadf144b");
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/21", .size = 1, .sizeRepo = 1, .timestamp = 1482182860, .bundleId = 2,
                .bundleOffset = 2, .reference = TEST_LABEL_DIFF, .checksumSha1 = "e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), STORAGE_REPO_BACKUP "/" TEST_LABEL_DIFF "/bundle/2", "aXb");
 
            // base/1/30 and base/1/31
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/30", .size = 1, .sizeRepo = 1, .timestamp = 1482182860, .bundleId = 2,
                .bundleOffset = 1, .reference = TEST_LABEL_INCR, .checksumSha1 = "c032adc1ff629c9b66f22749ad667e6beadf144b");
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/31", .size = 1, .sizeRepo = 1, .timestamp = 1482182860, .bundleId = 2,
                .bundleOffset = 2, .reference = TEST_LABEL_INCR, .checksumSha1 = "e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), STORAGE_REPO_BACKUP "/" TEST_LABEL_INCR "/bundle/2", "aXb");
 
            // system db name
            HRN_MANIFEST_DB_ADD(manifest, .name = "template1", .id = 1, .lastSystemId = 99999);
 
            // base/16384 directory
            HRN_MANIFEST_PATH_ADD(manifest, .name = TEST_PGDATA PG_PATH_BASE "/16384");
 
            // base/16384/PG_VERSION
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/16384/" PG_FILE_PGVERSION, .size = 3, .timestamp = 1482182860,
                .checksumSha1 = "dd71038f3463f511ee7403dbcbc87195302d891c");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_REPO_PATH "base/16384/" PG_FILE_PGVERSION, PG_VERSION_11_Z "\n");
 
            // base/16384/16385
            fileBuffer = bufNew(16384);
            memset(bufPtr(fileBuffer), 2, bufSize(fileBuffer));
            bufUsedSet(fileBuffer, bufSize(fileBuffer));
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/16384/16385", .size = 16384, .timestamp = 1482182860,
                .checksumSha1 = "d74e5f7ebe52a3ed468ba08c5b6aefaccd1ca88f");
            HRN_STORAGE_PUT(storageRepoWrite(), TEST_REPO_PATH "base/16384/16385", fileBuffer);
 
            // base/32768 directory
            HRN_MANIFEST_DB_ADD(manifest, .name = "test2", .id = 32768, .lastSystemId = 99999);
            HRN_MANIFEST_PATH_ADD(manifest, .name = TEST_PGDATA PG_PATH_BASE "/32768");
 
            // base/32768/PG_VERSION
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/32768/" PG_FILE_PGVERSION, .size = 3, .timestamp = 1482182860,
                .checksumSha1 = "dd71038f3463f511ee7403dbcbc87195302d891c");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_REPO_PATH "base/32768/" PG_FILE_PGVERSION, PG_VERSION_11_Z "\n");
 
            // base/32768/32769
            fileBuffer = bufNew(32768);
            memset(bufPtr(fileBuffer), 2, bufSize(fileBuffer));
            bufUsedSet(fileBuffer, bufSize(fileBuffer));
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/32768/32769", .size = 32768, .timestamp = 1482182860,
                .checksumSha1 = "a40f0986acb1531ce0cc75a23dcf8aa406ae9081");
            HRN_STORAGE_PUT(storageRepoWrite(), TEST_REPO_PATH "base/32768/32769", fileBuffer);
 
            // File link to postgresql.conf
            const String *name = STRDEF(MANIFEST_TARGET_PGDATA "/postgresql.conf");
 
            HRN_MANIFEST_TARGET_ADD(
                manifest, .type = manifestTargetTypeLink, .name = strZ(name), .path = "../config", .file = "postgresql.conf");
            HRN_MANIFEST_LINK_ADD(manifest, .name = strZ(name), .destination = "../config/postgresql.conf");
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "postgresql.conf", .size = 15, .timestamp = 1482182860,
                .checksumSha1 = "98b8abb2e681e2a5a7d8ab082c0a79727887558d");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_REPO_PATH "postgresql.conf", "POSTGRESQL.CONF");
 
            // File link to pg_hba.conf
            name = STRDEF(MANIFEST_TARGET_PGDATA "/pg_hba.conf");
 
            HRN_MANIFEST_TARGET_ADD(
                manifest, .type = manifestTargetTypeLink, .name = strZ(name), .path = "../config", .file = "pg_hba.conf");
            HRN_MANIFEST_LINK_ADD(manifest, .name = strZ(name), .destination = "../config/pg_hba.conf");
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "pg_hba.conf", .size = 11, .timestamp = 1482182860,
                .checksumSha1 = "401215e092779574988a854d8c7caed7f91dba4b");
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_REPO_PATH "pg_hba.conf", "PG_HBA.CONF");
 
            // Block incremental with no references to a prior backup
            fileBuffer = bufNew(8192 * 3);
            memset(bufPtr(fileBuffer), 1, 8192);
            memset(bufPtr(fileBuffer) + 8192, 2, 8192);
            memset(bufPtr(fileBuffer) + 16384, 3, 8192);
            bufUsedSet(fileBuffer, bufSize(fileBuffer));
 
            IoWrite *write = storageWriteIo(storageNewWriteP(storageRepoWrite(), STRDEF(TEST_REPO_PATH "base/1/bi-no-ref.pgbi")));
            ioFilterGroupAdd(ioWriteFilterGroup(write), blockIncrNew(8192, 8192, 11, 3, 0, 0, NULL, NULL, NULL));
            ioFilterGroupAdd(ioWriteFilterGroup(write), ioSizeNew());
 
            ioWriteOpen(write);
            ioWrite(write, fileBuffer);
            ioWriteClose(write);
 
            uint64_t blockIncrMapSize = pckReadU64P(ioFilterGroupResultP(ioWriteFilterGroup(write), BLOCK_INCR_FILTER_TYPE));
            uint64_t repoSize = pckReadU64P(ioFilterGroupResultP(ioWriteFilterGroup(write), SIZE_FILTER_TYPE));
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/bi-no-ref", .size = bufUsed(fileBuffer), .sizeRepo = repoSize,
                .blockIncrSize = 8192, .blockIncrChecksumSize = 11, .blockIncrMapSize = blockIncrMapSize, .timestamp = 1482182860,
                .checksumSha1 = "953cdcc904c5d4135d96fc0833f121bf3033c74c");
 
            // Block incremental with a broken reference to show that unneeded references will not be used
            Buffer *fileUnused = bufNew(8192 * 6);
            memset(bufPtr(fileUnused), 1, bufSize(fileUnused));
            bufUsedSet(fileUnused, bufSize(fileUnused));
 
            Buffer *fileUnusedMap = bufNew(0);
            write = ioBufferWriteNew(fileUnusedMap);
            ioFilterGroupAdd(ioWriteFilterGroup(write), blockIncrNew(8192, 8192, 11, 0, 0, 0, NULL, NULL, NULL));
 
            ioWriteOpen(write);
            ioWrite(write, fileUnused);
            ioWriteClose(write);
 
            size_t fileUnusedMapSize = (size_t)pckReadU64P(ioFilterGroupResultP(ioWriteFilterGroup(write), BLOCK_INCR_FILTER_TYPE));
 
            Buffer *fileUsed = bufDup(fileUnused);
            memset(bufPtr(fileUsed), 3, 8192);
            memset(bufPtr(fileUsed) + (8192 * 2), 3, 24576);
 
            size_t bufferSizeOld = ioBufferSize();
            ioBufferSizeSet(777);
 
            write = storageWriteIo(storageNewWriteP(storageRepoWrite(), STRDEF(TEST_REPO_PATH "base/1/bi-unused-ref.pgbi")));
            ioFilterGroupAdd(
                ioWriteFilterGroup(write),
                blockIncrNew(
                    8192, 8192, 11, 3, 0, 0,
                    BUF(bufPtr(fileUnusedMap) + bufUsed(fileUnusedMap) - fileUnusedMapSize, fileUnusedMapSize), NULL, NULL));
            ioFilterGroupAdd(ioWriteFilterGroup(write), ioSizeNew());
 
            ioWriteOpen(write);
            ioWrite(write, fileUsed);
            ioWriteClose(write);
 
            ioBufferSizeSet(bufferSizeOld);
 
            uint64_t fileUsedMapSize = pckReadU64P(ioFilterGroupResultP(ioWriteFilterGroup(write), BLOCK_INCR_FILTER_TYPE));
            uint64_t fileUsedRepoSize = pckReadU64P(ioFilterGroupResultP(ioWriteFilterGroup(write), SIZE_FILTER_TYPE));
 
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA "base/1/bi-unused-ref", .size = bufUsed(fileUsed), .sizeRepo = fileUsedRepoSize,
                .blockIncrSize = 8192, .blockIncrChecksumSize = 11, .blockIncrMapSize = fileUsedMapSize, .timestamp = 1482182860,
                .checksumSha1 = "febd680181d4cd315dce942348862c25fbd731f3");
 
            memset(bufPtr(fileUnused) + (8192 * 4), 3, 8192);
            HRN_STORAGE_PATH_CREATE(storagePgWrite(), "base/1", .mode = 0700);
            HRN_STORAGE_PUT(storagePgWrite(), "base/1/bi-unused-ref", fileUnused, .modeFile = 0600);
 
            // tablespace_map (will be ignored during restore)
            HRN_MANIFEST_FILE_ADD(
                manifest, .name = TEST_PGDATA PG_FILE_TABLESPACEMAP, .size = 0, .timestamp = 1482182860,
                .checksumSha1 = HASH_TYPE_SHA1_ZERO);
            HRN_STORAGE_PUT_EMPTY(storageRepoWrite(), TEST_REPO_PATH PG_FILE_TABLESPACEMAP);
 
            // Path link to pg_wal
            name = STRDEF(MANIFEST_TARGET_PGDATA "/pg_wal");
            const String *destination = STRDEF("../wal");
 
            HRN_MANIFEST_TARGET_ADD(manifest, .type = manifestTargetTypeLink, .name = strZ(name), .path = strZ(destination));
            HRN_MANIFEST_PATH_ADD(manifest, .name = strZ(name));
            HRN_MANIFEST_LINK_ADD(manifest, .name = strZ(name), .destination = strZ(destination));
            THROW_ON_SYS_ERROR(
                symlink("../wal", zNewFmt("%s/pg_wal", strZ(pgPath))) == -1, FileOpenError, "unable to create symlink");
 
            // pg_xact path
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/pg_xact");
 
            // pg_tblspc/1
            HRN_MANIFEST_TARGET_ADD(
                manifest, .type = manifestTargetTypeLink, .name = MANIFEST_TARGET_PGTBLSPC "/1", .path = TEST_PATH "/ts/1",
                .tablespaceId = 1, .tablespaceName = "ts1");
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGDATA "/" MANIFEST_TARGET_PGTBLSPC);
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGTBLSPC);
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGTBLSPC "/1");
            HRN_MANIFEST_PATH_ADD(manifest, .name = MANIFEST_TARGET_PGTBLSPC "/1/PG_10_201707211");
            HRN_MANIFEST_LINK_ADD(
                manifest, .name = MANIFEST_TARGET_PGDATA "/" MANIFEST_TARGET_PGTBLSPC "/1", .destination = TEST_PATH "/ts/1");
 
            // Always sort
            lstSort(manifest->pub.targetList, sortOrderAsc);
            lstSort(manifest->pub.fileList, sortOrderAsc);
            lstSort(manifest->pub.linkList, sortOrderAsc);
            lstSort(manifest->pub.pathList, sortOrderAsc);
        }
        OBJ_NEW_END();
 
        manifestSave(
            manifest,
            storageWriteIo(
                storageNewWriteP(storageRepoWrite(), STRDEF(STORAGE_REPO_BACKUP "/" TEST_LABEL "/" BACKUP_MANIFEST_FILE))));
 
        // Add a few bogus paths/files/links to be removed in delta
        HRN_STORAGE_PATH_CREATE(storagePgWrite(), "bogus1/bogus2");
        HRN_STORAGE_PATH_CREATE(storagePgWrite(), PG_PATH_GLOBAL "/bogus3");
 
        // Create yyy file so it is not copied
        HRN_STORAGE_PUT_Z(storagePgWrite(), "yyy", "yyy", .modeFile = 0600);
 
        // Add a few bogus links to be deleted
        THROW_ON_SYS_ERROR(symlink("../wal", zNewFmt("%s/pg_wal2", strZ(pgPath))) == -1, FileOpenError, "unable to create symlink");
 
        TEST_RESULT_VOID(hrnCmdRestore(), "successful restore");
 
        TEST_RESULT_LOG_FMT(
            "P00   INFO: repo1: restore backup set 20161219-212741F_20161219-212918I\n"
            "P00   INFO: map link 'pg_hba.conf' to '../config/pg_hba.conf'\n"
            "P00   INFO: map link 'pg_wal' to '../wal'\n"
            "P00   INFO: link 'pg_xact' to '../xact'\n"
            "P00   INFO: map link 'postgresql.conf' to '../config/postgresql.conf'\n"
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/config' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/wal' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/ts/1/PG_10_201707211' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/xact' exists\n"
            "P00 DETAIL: skip 'tablespace_map' -- tablespace links will be created based on mappings\n"
            "P00   INFO: remove invalid files/links/paths from '" TEST_PATH "/pg'\n"
            "P00 DETAIL: remove invalid path '" TEST_PATH "/pg/bogus1'\n"
            "P00 DETAIL: remove invalid path '" TEST_PATH "/pg/global/bogus3'\n"
            "P00 DETAIL: remove invalid link '" TEST_PATH "/pg/pg_wal2'\n"
            "P00 DETAIL: create path '" TEST_PATH "/pg/base/16384'\n"
            "P00 DETAIL: create path '" TEST_PATH "/pg/base/32768'\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_xact' to '../xact'\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_hba.conf' to '../config/pg_hba.conf'\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/postgresql.conf' to '../config/postgresql.conf'\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/bi-unused-ref (bi 24KB/48KB, [PCT]) checksum"
            " febd680181d4cd315dce942348862c25fbd731f3\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/32768/32769 (32KB, [PCT]) checksum"
            " a40f0986acb1531ce0cc75a23dcf8aa406ae9081\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/bi-no-ref (bi 24KB, [PCT]) checksum"
            " 953cdcc904c5d4135d96fc0833f121bf3033c74c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/16384/16385 (16KB, [PCT]) checksum"
            " d74e5f7ebe52a3ed468ba08c5b6aefaccd1ca88f\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/global/pg_control.pgbackrest.tmp (8KB, [PCT])"
            " checksum %s\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/2 (8KB, [PCT]) checksum 4d7b2a36c5387decf799352a3751883b7ceb96aa\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/postgresql.conf (15B, [PCT]) checksum"
            " 98b8abb2e681e2a5a7d8ab082c0a79727887558d\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/pg_hba.conf (11B, [PCT]) checksum"
            " 401215e092779574988a854d8c7caed7f91dba4b\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/32768/PG_VERSION (3B, [PCT])"
            " checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/16384/PG_VERSION (3B, [PCT])"
            " checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/10 (bundle 20161219-212741F/1/1, 8KB, [PCT])"
            " checksum 28757c756c03c37aca13692cb719c18d1510c190\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/PG_VERSION (bundle 1/0, 3B, [PCT]) checksum"
            " 4143d3a341877154d6e95211464e1df1015b74bd\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/PG_VERSION (bundle 1/4, 3B, [PCT]) checksum"
            " 4143d3a341877154d6e95211464e1df1015b74bd\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/yyy - exists and matches backup (bundle 1/8, 3B, [PCT]) checksum"
            " 186154712b2d5f6791d85b9a0987b98fa231779c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/xxxxx (bundle 1/11, 5B, [PCT]) checksum"
            " 9addbf544119efa4a64223b649750a510f0d463f\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/zz (bundle 1/17, 2B, [PCT]) checksum"
            " d7dacae2c968388960bf8970080a980ed5c5dcb7\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/20 (bundle 20161219-212741F_20161219-212800D/2/1, 1B, [PCT]) checksum"
            " c032adc1ff629c9b66f22749ad667e6beadf144b\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/21 (bundle 20161219-212741F_20161219-212800D/2/2, 1B, [PCT]) checksum"
            " e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/30 (bundle 20161219-212741F_20161219-212900I/2/1, 1B, [PCT]) checksum"
            " c032adc1ff629c9b66f22749ad667e6beadf144b\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/31 (bundle 20161219-212741F_20161219-212900I/2/2, 1B, [PCT]) checksum"
            " e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/global/999 (0B, [PCT])\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/global/888 (0B, [PCT])\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/zero-length (bundle 1/16, 0B, [PCT])\n"
            "P00 DETAIL: sync path '" TEST_PATH "/config'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/base'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/base/1'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/base/16384'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/base/32768'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_wal'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_xact'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1/PG_10_201707211'\n"
            "P00   INFO: restore global/pg_control (performed last to ensure aborted restores cannot be started)\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/global'\n"
            "P00   INFO: restore size = [SIZE], file total = 23",
            pgControlSha1);
 
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            "./\n"
            "PG_VERSION {s=3, t=1482182860}\n"
            "base/\n"
            "base/1/\n"
            "base/1/10 {s=8192, t=1482182860}\n"
            "base/1/2 {s=8192, t=1482182860}\n"
            "base/1/20 {s=1, t=1482182860}\n"
            "base/1/21 {s=1, t=1482182860}\n"
            "base/1/30 {s=1, t=1482182860}\n"
            "base/1/31 {s=1, t=1482182860}\n"
            "base/1/PG_VERSION {s=3, t=1482182860}\n"
            "base/1/bi-no-ref {s=24576, t=1482182860}\n"
            "base/1/bi-unused-ref {s=49152, t=1482182860}\n"
            "base/16384/\n"
            "base/16384/16385 {s=16384, t=1482182860}\n"
            "base/16384/PG_VERSION {s=3, t=1482182860}\n"
            "base/32768/\n"
            "base/32768/32769 {s=32768, t=1482182860}\n"
            "base/32768/PG_VERSION {s=3, t=1482182860}\n"
            "global/\n"
            "global/888 {s=0, t=1482182860}\n"
            "global/999 {s=0, t=1482182860}\n"
            "global/pg_control {s=8192, t=1482182860}\n"
            "pg_hba.conf> {d=../config/pg_hba.conf}\n"
            "pg_tblspc/\n"
            "pg_tblspc/1> {d=" TEST_PATH "/ts/1}\n"
            "pg_wal> {d=../wal}\n"
            "pg_xact> {d=../xact}\n"
            "postgresql.conf> {d=../config/postgresql.conf}\n"
            "xxxxx {s=5, t=1482182860}\n"
            "yyy {s=3, t=1482182860}\n"
            "zero-length {s=0, t=1482182866}\n"
            "zz {s=2, t=1482182860}\n",
            .level = storageInfoLevelBasic, .includeDot = true);
 
        TEST_STORAGE_LIST(
            storagePg(), "pg_tblspc/1",
            ".> {d=" TEST_PATH "/ts/1}\n"
            "16384/\n"
            "16384/PG_VERSION {s=3, t=1482182860}\n"
            "PG_10_201707211/\n",
            .level = storageInfoLevelBasic, .includeDot = true);
 
        TEST_STORAGE_LIST(
            storagePg(), "../wal",
            "./\n",
            .level = storageInfoLevelBasic, .includeDot = true);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("incremental delta selective restore");
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 1, "/repo-bogus");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath);
        hrnCfgArgRawZ(argList, cfgOptRepo, "2");
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptSpoolPath, TEST_PATH "/spool");
        hrnCfgArgRawBool(argList, cfgOptDelta, true);
        hrnCfgArgRawZ(argList, cfgOptType, "preserve");
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_wal=../wal");
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "postgresql.conf=../config/postgresql.conf");
        hrnCfgArgRawZ(argList, cfgOptLinkMap, "pg_hba.conf=../config/pg_hba.conf");
        hrnCfgArgRawZ(argList, cfgOptDbInclude, "16384");
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // Move pg1-path and put a link in its place. This tests that restore works when pg1-path is a symlink yet should be
        // completely invisible in the manifest and logging.
        HRN_SYSTEM_FMT("mv %s %s-data", strZ(pgPath), strZ(pgPath));
        HRN_SYSTEM_FMT("ln -s %s-data %s ", strZ(pgPath), strZ(pgPath));
 
        // Create the stanza archive pool path to check that it gets removed
        HRN_STORAGE_PUT_EMPTY(storageSpoolWrite(), STORAGE_SPOOL_ARCHIVE "/empty.txt");
 
        // Write recovery.conf so we don't get a preserve warning
        HRN_STORAGE_PUT_Z(storagePgWrite(), PG_FILE_RECOVERYCONF, "Some Settings");
 
        // Change timestamp so it will be updated
        HRN_STORAGE_TIME(storagePgWrite(), "global/999", 777);
 
        // Enlarge a file so it gets truncated. Keep timestamp the same to prove that it gets updated after the truncate.
        HRN_STORAGE_PUT_Z(
            storagePgWrite(), "base/16384/" PG_FILE_PGVERSION, PG_VERSION_11_Z "\n\n", .modeFile = 0600,
            .timeModified = 1482182860);
 
        // Enlarge a zero-length file so it gets truncated
        HRN_STORAGE_PUT_Z(storagePgWrite(), "global/888", "X", .modeFile = 0600);
 
        // Change size so delta will skip based on size
        HRN_STORAGE_PUT_Z(storagePgWrite(), "base/1/2", BOGUS_STR);
        HRN_STORAGE_MODE(storagePgWrite(), "base/1/2", 0600);
 
        // Convert pg_wal to a path so it will be removed
        HRN_STORAGE_REMOVE(storagePgWrite(), "pg_wal");
        HRN_STORAGE_PATH_CREATE(storagePgWrite(), "pg_wal");
 
        // Convert pg_hba.conf to a path so it will be removed
        HRN_STORAGE_REMOVE(storagePgWrite(), "pg_hba.conf");
        HRN_STORAGE_PUT_Z(storagePgWrite(), "pg_hba.conf", BOGUS_STR);
 
        // Update the manifest with online = true to test recovery start time logging
        manifest->pub.data.backupOptionOnline = true;
        manifest->pub.data.backupTimestampStart = 1482182860;
 
        hrnLogReplaceAdd("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}", NULL, "TIME", false);
 
        manifestSave(
            manifest,
            storageWriteIo(
                storageNewWriteP(storageRepoWrite(), STRDEF(STORAGE_REPO_BACKUP "/" TEST_LABEL "/" BACKUP_MANIFEST_FILE))));
 
        TEST_RESULT_VOID(hrnCmdRestore(), "successful restore");
 
        TEST_RESULT_LOG_FMT(
            "P00   INFO: repo2: restore backup set 20161219-212741F_20161219-212918I, recovery will start at [TIME]\n"
            "P00   INFO: map link 'pg_hba.conf' to '../config/pg_hba.conf'\n"
            "P00   INFO: map link 'pg_wal' to '../wal'\n"
            "P00   INFO: map link 'postgresql.conf' to '../config/postgresql.conf'\n"
            "P00 DETAIL: databases found for selective restore (1, 16384, 32768)\n"
            "P00 DETAIL: databases excluded (zeroed) from selective restore (32768)\n"
            "P00 DETAIL: check '" TEST_PATH "/pg' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/config' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/wal' exists\n"
            "P00 DETAIL: check '" TEST_PATH "/ts/1/PG_10_201707211' exists\n"
            "P00 DETAIL: skip 'tablespace_map' -- tablespace links will be created based on mappings\n"
            "P00 DETAIL: remove 'global/pg_control' so cluster will not start if restore does not complete\n"
            "P00   INFO: remove invalid files/links/paths from '" TEST_PATH "/pg'\n"
            "P00 DETAIL: remove invalid file '" TEST_PATH "/pg/pg_hba.conf'\n"
            "P00 DETAIL: remove invalid path '" TEST_PATH "/pg/pg_wal'\n"
            "P00 DETAIL: remove invalid link '" TEST_PATH "/pg/pg_xact'\n"
            "P00   INFO: remove invalid files/links/paths from '" TEST_PATH "/wal'\n"
            "P00   INFO: remove invalid files/links/paths from '" TEST_PATH "/ts/1/PG_10_201707211'\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_wal' to '../wal'\n"
            "P00 DETAIL: create path '" TEST_PATH "/pg/pg_xact'\n"
            "P00 DETAIL: create symlink '" TEST_PATH "/pg/pg_hba.conf' to '../config/pg_hba.conf'\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/bi-unused-ref - exists and matches backup (48KB, [PCT]) checksum"
            " febd680181d4cd315dce942348862c25fbd731f3\n"
            "P01 DETAIL: restore zeroed file " TEST_PATH "/pg/base/32768/32769 (32KB, [PCT])\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/bi-no-ref - exists and matches backup (24KB, [PCT]) checksum"
            " 953cdcc904c5d4135d96fc0833f121bf3033c74c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/16384/16385 - exists and matches backup (16KB, [PCT])"
            " checksum d74e5f7ebe52a3ed468ba08c5b6aefaccd1ca88f\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/global/pg_control.pgbackrest.tmp (8KB, [PCT])"
            " checksum %s\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/2 (8KB, [PCT]) checksum 4d7b2a36c5387decf799352a3751883b7ceb96aa\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/postgresql.conf - exists and matches backup (15B, [PCT])"
            " checksum 98b8abb2e681e2a5a7d8ab082c0a79727887558d\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/pg_hba.conf - exists and matches backup (11B, [PCT])"
            " checksum 401215e092779574988a854d8c7caed7f91dba4b\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/32768/PG_VERSION - exists and matches backup (3B, [PCT])"
            " checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/16384/PG_VERSION - exists and matches backup (3B, [PCT])"
            " checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/10 - exists and matches backup (bundle 20161219-212741F/1/1, 8KB,"
            " [PCT]) checksum 28757c756c03c37aca13692cb719c18d1510c190\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/PG_VERSION - exists and matches backup (bundle 1/0, 3B, [PCT])"
            " checksum 4143d3a341877154d6e95211464e1df1015b74bd\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/PG_VERSION - exists and matches backup (bundle 1/4, 3B, [PCT])"
            " checksum 4143d3a341877154d6e95211464e1df1015b74bd\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/yyy - exists and matches backup (bundle 1/8, 3B, [PCT]) checksum"
            " 186154712b2d5f6791d85b9a0987b98fa231779c\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/xxxxx - exists and matches backup (bundle 1/11, 5B, [PCT]) checksum"
            " 9addbf544119efa4a64223b649750a510f0d463f\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/zz - exists and matches backup (bundle 1/17, 2B, [PCT]) checksum"
            " d7dacae2c968388960bf8970080a980ed5c5dcb7\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/20 - exists and matches backup (bundle"
            " 20161219-212741F_20161219-212800D/2/1, 1B, [PCT]) checksum c032adc1ff629c9b66f22749ad667e6beadf144b\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/21 - exists and matches backup (bundle"
            " 20161219-212741F_20161219-212800D/2/2, 1B, [PCT]) checksum e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/30 - exists and matches backup (bundle"
            " 20161219-212741F_20161219-212900I/2/1, 1B, [PCT]) checksum c032adc1ff629c9b66f22749ad667e6beadf144b\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/base/1/31 - exists and matches backup (bundle"
            " 20161219-212741F_20161219-212900I/2/2, 1B, [PCT]) checksum e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/global/999 - exists and is zero size (0B, [PCT])\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/global/888 - exists and is zero size (0B, [PCT])\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg/zero-length - exists and is zero size (bundle 1/16, 0B, [PCT])\n"
            "P00 DETAIL: sync path '" TEST_PATH "/config'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/base'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/base/1'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/base/16384'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/base/32768'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_wal'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_xact'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/pg_tblspc/1/PG_10_201707211'\n"
            "P00   INFO: restore global/pg_control (performed last to ensure aborted restores cannot be started)\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg/global'\n"
            "P00   INFO: restore size = [SIZE], file total = 23",
            pgControlSha1);
 
        // Check stanza archive spool path was removed
        TEST_STORAGE_LIST_EMPTY(storageSpool(), STORAGE_PATH_ARCHIVE);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("warn on invalid page checksum when checksum-page-error=n");
 
        const String *const repoPathChecksumWarn = STRDEF(TEST_PATH "/repo-checksum-warn");
        const String *const pgPathChecksumWarn = STRDEF(TEST_PATH "/pg-checksum-warn");
 
        // Use a dedicated arg list (rather than reassigning the shared argList) so the config loaded by the prior test remains
        // available to restore before the "remove a repo file" test below, which relies on it still being active
        StringList *argListChecksum = strLstNew();
        hrnCfgArgRawZ(argListChecksum, cfgOptStanza, "test1");
        hrnCfgArgRaw(argListChecksum, cfgOptRepoPath, repoPathChecksumWarn);
        hrnCfgArgRaw(argListChecksum, cfgOptPgPath, pgPathChecksumWarn);
        hrnCfgArgRawZ(argListChecksum, cfgOptSet, "20161219-212741F");
        hrnCfgArgRawBool(argListChecksum, cfgOptChecksumPageError, false);
        HRN_CFG_LOAD(cfgCmdRestore, argListChecksum);
 
        #define TEST_CKSUM_LABEL                                   "20161219-212741F"
        #define TEST_CKSUM_PGDATA                                  MANIFEST_TARGET_PGDATA "/"
        #define TEST_CKSUM_REPO_PATH                               STORAGE_REPO_BACKUP "/" TEST_CKSUM_LABEL "/" TEST_CKSUM_PGDATA
 
        Manifest *manifestChecksum = NULL;
 
        OBJ_NEW_BASE_BEGIN(Manifest, .childQty = MEM_CONTEXT_QTY_MAX)
        {
            manifestChecksum = manifestNewInternal();
            manifestChecksum->pub.info = infoNew(REPOSITORY_FORMAT_DEFAULT, NULL);
            manifestChecksum->pub.data.backupLabel = strNewZ(TEST_CKSUM_LABEL);
            manifestChecksum->pub.data.pgVersion = PG_VERSION_11;
            manifestChecksum->pub.data.pgCatalogVersion = hrnPgCatalogVersion(PG_VERSION_11);
            manifestChecksum->pub.data.backupType = backupTypeFull;
            manifestChecksum->pub.data.backupTimestampStart = 1482182860;
            manifestChecksum->pub.data.backupTimestampCopyStart = 1482182861; // So file timestamps should be less than this
            manifestChecksum->pub.data.backupOptionOnline = true;
            manifestChecksum->pub.data.archiveStart = strNewZ("000000010000000000000007");
            manifestChecksum->pub.data.lsnStart = strNewZ("0/7000028");
 
            // Data directory
            HRN_MANIFEST_TARGET_ADD(manifestChecksum, .name = MANIFEST_TARGET_PGDATA, .path = strZ(pgPathChecksumWarn));
            HRN_MANIFEST_PATH_ADD(manifestChecksum, .name = MANIFEST_TARGET_PGDATA);
 
            // Global directory
            HRN_MANIFEST_PATH_ADD(manifestChecksum, .name = TEST_CKSUM_PGDATA PG_PATH_GLOBAL);
 
            // PG_VERSION -- marked with a page checksum error so it triggers the checksum-page-error handling on restore
            HRN_MANIFEST_FILE_ADD(
                manifestChecksum, .name = TEST_CKSUM_PGDATA PG_FILE_PGVERSION, .size = 3, .timestamp = 1482182860,
                .checksumSha1 = "dd71038f3463f511ee7403dbcbc87195302d891c", .checksumPage = true, .checksumPageError = true);
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_CKSUM_REPO_PATH PG_FILE_PGVERSION, PG_VERSION_11_Z "\n");
 
            // postgresql.conf -- page checksums are enabled but there is no error, so no warning should be triggered for it
            HRN_MANIFEST_FILE_ADD(
                manifestChecksum, .name = MANIFEST_TARGET_PGDATA "/postgresql.conf", .size = 10, .timestamp = 1482182860,
                .checksumSha1 = "1a49a3c2240449fee1422e4afcf44d5b96378511", .checksumPage = true);
            HRN_STORAGE_PUT_Z(storageRepoWrite(), TEST_CKSUM_REPO_PATH "postgresql.conf", "VALID_CONF");
 
            // pg_tblspc
            HRN_MANIFEST_PATH_ADD(manifestChecksum, .name = MANIFEST_TARGET_PGDATA "/" MANIFEST_TARGET_PGTBLSPC);
 
            // Always sort
            lstSort(manifestChecksum->pub.targetList, sortOrderAsc);
            lstSort(manifestChecksum->pub.fileList, sortOrderAsc);
            lstSort(manifestChecksum->pub.linkList, sortOrderAsc);
            lstSort(manifestChecksum->pub.pathList, sortOrderAsc);
        }
        OBJ_NEW_END();
 
        manifestSave(
            manifestChecksum,
            storageWriteIo(
                storageNewWriteP(storageRepoWrite(), STRDEF(STORAGE_REPO_BACKUP "/" TEST_CKSUM_LABEL "/" BACKUP_MANIFEST_FILE))));
 
        // Write backup.info
        HRN_INFO_PUT(storageRepoWrite(), INFO_BACKUP_PATH_FILE, TEST_RESTORE_BACKUP_INFO "\n" TEST_RESTORE_BACKUP_INFO_DB);
 
        // Write archive.info
        InfoArchive *infoArchiveChecksum = infoArchiveNew(PG_VERSION_11, 6569239123849665679, REPOSITORY_FORMAT_DEFAULT, NULL);
        infoArchiveSaveFile(infoArchiveChecksum, storageRepoWrite(), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewNone());
 
        TEST_RESULT_VOID(hrnCmdRestore(), "successful restore");
 
        TEST_RESULT_LOG(
            "P00   INFO: repo1: restore backup set 20161219-212741F, recovery will start at [TIME]\n"
            "P00 DETAIL: check '" TEST_PATH "/pg-checksum-warn' exists\n"
            "P00 DETAIL: create path '" TEST_PATH "/pg-checksum-warn/global'\n"
            "P00 DETAIL: create path '" TEST_PATH "/pg-checksum-warn/pg_tblspc'\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg-checksum-warn/postgresql.conf (10B, [PCT]) checksum"
            " 1a49a3c2240449fee1422e4afcf44d5b96378511\n"
            "P00   WARN: invalid page checksum(s) found in file " TEST_PATH "/pg-checksum-warn/PG_VERSION\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg-checksum-warn/PG_VERSION (3B, [PCT]) checksum"
            " dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P00   INFO: write " TEST_PATH "/pg-checksum-warn/recovery.conf\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg-checksum-warn'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg-checksum-warn/pg_tblspc'\n"
            "P00   WARN: backup does not contain 'global/pg_control' -- cluster will not start\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg-checksum-warn/global'\n"
            "P00   INFO: restore size = [SIZE], file total = 2\n"
            "P00   WARN: restore command encountered page checksum error(s) in 1 file(s), check the log file for details");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("delta restore preserves file despite recorded page checksum error");
 
        // The file already matches the backup so it will be preserved rather than copied, which means the page checksum error
        // recorded against it in the manifest must not trigger a warning
        hrnCfgArgRawBool(argListChecksum, cfgOptDelta, true);
        HRN_CFG_LOAD(cfgCmdRestore, argListChecksum);
 
        TEST_RESULT_VOID(hrnCmdRestore(), "successful delta restore");
 
        TEST_RESULT_LOG(
            "P00   INFO: repo1: restore backup set 20161219-212741F, recovery will start at [TIME]\n"
            "P00 DETAIL: check '" TEST_PATH "/pg-checksum-warn' exists\n"
            "P00   INFO: remove invalid files/links/paths from '" TEST_PATH "/pg-checksum-warn'\n"
            "P00 DETAIL: remove invalid file '" TEST_PATH "/pg-checksum-warn/recovery.conf'\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg-checksum-warn/postgresql.conf - exists and matches backup (10B, [PCT])"
            " checksum 1a49a3c2240449fee1422e4afcf44d5b96378511\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg-checksum-warn/PG_VERSION - exists and matches backup (3B, [PCT])"
            " checksum dd71038f3463f511ee7403dbcbc87195302d891c\n"
            "P00   INFO: write " TEST_PATH "/pg-checksum-warn/recovery.conf\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg-checksum-warn'\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg-checksum-warn/pg_tblspc'\n"
            "P00   WARN: backup does not contain 'global/pg_control' -- cluster will not start\n"
            "P00 DETAIL: sync path '" TEST_PATH "/pg-checksum-warn/global'\n"
            "P00   INFO: restore size = [SIZE], file total = 2");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("error on invalid page checksum when checksum-page-error=y");
 
        // Reuse the backup written for the checksum-page-error=n test above
        const String *const pgPathChecksumThrow = STRDEF(TEST_PATH "/pg-checksum-throw");
 
        argListChecksum = strLstNew();
        hrnCfgArgRawZ(argListChecksum, cfgOptStanza, "test1");
        hrnCfgArgRaw(argListChecksum, cfgOptRepoPath, repoPathChecksumWarn);
        hrnCfgArgRaw(argListChecksum, cfgOptPgPath, pgPathChecksumThrow);
        hrnCfgArgRawZ(argListChecksum, cfgOptSet, TEST_CKSUM_LABEL);
        hrnCfgArgRawBool(argListChecksum, cfgOptChecksumPageError, true);
        HRN_CFG_LOAD(cfgCmdRestore, argListChecksum);
 
        TEST_ERROR(
            hrnCmdRestore(), ChecksumError,
            "invalid page checksum(s) found in file " TEST_PATH "/pg-checksum-throw/PG_VERSION");
 
        TEST_RESULT_LOG(
            "P00   INFO: repo1: restore backup set 20161219-212741F, recovery will start at [TIME]\n"
            "P00   INFO: remap data directory to '" TEST_PATH "/pg-checksum-throw'\n"
            "P00 DETAIL: check '" TEST_PATH "/pg-checksum-throw' exists\n"
            "P00 DETAIL: create path '" TEST_PATH "/pg-checksum-throw/global'\n"
            "P00 DETAIL: create path '" TEST_PATH "/pg-checksum-throw/pg_tblspc'\n"
            "P01 DETAIL: restore file " TEST_PATH "/pg-checksum-throw/postgresql.conf (10B, [PCT]) checksum"
            " 1a49a3c2240449fee1422e4afcf44d5b96378511");
 
        #undef TEST_CKSUM_LABEL
        #undef TEST_CKSUM_PGDATA
        #undef TEST_CKSUM_REPO_PATH
 
        // Restore the config from the prior test since the "remove a repo file" test below still relies on it being active
        // (it reuses the shared argList, which was left untouched by the checksum-page-error tests above)
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // -------------------------------------------------------------------------------------------------------------------------
        // Keep this test at the end since is corrupts the repo
        TEST_TITLE("remove a repo file so a restore job errors");
 
        HRN_STORAGE_REMOVE(storageRepoWrite(), TEST_REPO_PATH PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL, .errorOnMissing = true);
        HRN_STORAGE_REMOVE(storagePgWrite(), PG_PATH_GLOBAL "/" PG_FILE_PGCONTROL, .errorOnMissing = true);
 
        HRN_CFG_LOAD(cfgCmdRestore, argList, .jobRetry = 1);
 
        // Set log level to warn
        harnessLogLevelSet(logLevelWarn);
 
        TEST_ERROR(
            hrnCmdRestore(), FileMissingError,
            "raised from local-1 shim protocol: unable to open missing file"
            " '" TEST_PATH "/repo/backup/test1/20161219-212741F_20161219-212918I/pg_data/global/pg_control' for read\n"
            "[RETRY DETAIL OMITTED]");
 
        // Free local processes that were not freed because of the error
        protocolFree();
    }
 
    // *****************************************************************************************************************************
    if (testBegin("cmdBackup() and cmdRestore()"))
    {
        hrnStorageHelperRepoShimSet(true);
 
        const String *pgPath = STRDEF(TEST_PATH "/pg");
        const String *repoPath = STRDEF(TEST_PATH "/repo");
 
        // Created pg_control and PG_VERSION
        HRN_PG_CONTROL_PUT(storagePgWrite(), PG_VERSION_15);
        HRN_STORAGE_PUT_Z(storagePgWrite(), PG_FILE_PGVERSION, PG_VERSION_15_Z);
 
        // Create encrypted stanza
        StringList *argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawBool(argList, cfgOptOnline, false);
        hrnCfgArgRawZ(argList, cfgOptRepoCipherType, "aes-256-cbc");
        hrnCfgEnvRawZ(cfgOptRepoCipherPass, TEST_CIPHER_PASS);
        HRN_CFG_LOAD(cfgCmdStanzaCreate, argList);
 
        TEST_RESULT_VOID(cmdStanzaCreate(), "stanza create");
 
        // It is better to put as few tests here as possible because cmp/enc makes tests more expensive (especially with valgrind)
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("full backup with block incr");
 
        // Zeroed file large enough to use block incr
        time_t timeBase = time(NULL);
        Buffer *relation = bufNew(256 * 1024);
        memset(bufPtr(relation), 0, bufSize(relation));
        bufUsedSet(relation, bufSize(relation));
 
        HRN_STORAGE_PUT(storagePgWrite(), PG_PATH_BASE "/1/2", relation, .timeModified = timeBase - 2);
 
        // Zeroed file large enough to use block incr (that will be truncated to zero before restore)
        relation = bufNew(16 * 1024);
        memset(bufPtr(relation), 0, bufSize(relation));
        bufUsedSet(relation, bufSize(relation));
 
        HRN_STORAGE_PUT(storagePgWrite(), PG_PATH_BASE "/1/44", relation, .timeModified = timeBase - 2);
 
        // Add postgresql.auto.conf to contain recovery settings
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_FILE_POSTGRESQLAUTOCONF, .timeModified = timeBase - 1);
 
        // Backup
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptRepoRetentionFull, "1");
        hrnCfgArgRawStrId(argList, cfgOptType, backupTypeFull);
        hrnCfgArgRawBool(argList, cfgOptRepoBundle, true);
        hrnCfgArgRawBool(argList, cfgOptRepoBlock, true);
        hrnCfgArgRawBool(argList, cfgOptOnline, false);
        hrnCfgArgRawZ(argList, cfgOptRepoCipherType, "aes-256-cbc");
        hrnCfgEnvRawZ(cfgOptRepoCipherPass, TEST_CIPHER_PASS);
        HRN_CFG_LOAD(cfgCmdBackup, argList);
 
        TEST_RESULT_VOID(hrnCmdBackup(), "backup");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("diff backup with block incr");
 
        // Update timestamps so base/1/3 is slightly older than base/1/2. This will cause base/1/2 to be placed after base/1/3 in
        // the bundle and since base/1/2 did not change it will be stored as a map only, which will cause restore to include it in a
        // single read with base/1/3 (which is too small to be block incremental). This is to test for regression in the fix to
        // separately decrypt block maps when they are in the middle/end of a bundle read.
        HRN_STORAGE_TIME(storagePgWrite(), PG_PATH_BASE "/1/2", timeBase);
        HRN_STORAGE_PUT_Z(storagePgWrite(), PG_PATH_BASE "/1/3", "contents", .timeModified = timeBase - 1);
 
        // Backup
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptRepoRetentionFull, "1");
        hrnCfgArgRawStrId(argList, cfgOptType, backupTypeDiff);
        hrnCfgArgRawBool(argList, cfgOptRepoBundle, true);
        hrnCfgArgRawBool(argList, cfgOptRepoBlock, true);
        hrnCfgArgRawBool(argList, cfgOptOnline, false);
        hrnCfgArgRawZ(argList, cfgOptRepoCipherType, "aes-256-cbc");
        hrnCfgEnvRawZ(cfgOptRepoCipherPass, TEST_CIPHER_PASS);
        HRN_CFG_LOAD(cfgCmdBackup, argList);
 
        TEST_RESULT_VOID(hrnCmdBackup(), "backup");
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("restore with block incr");
 
        // Remove all files from pg path
        HRN_STORAGE_PATH_REMOVE(storagePgWrite(), NULL, .recurse = true);
 
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptSpoolPath, TEST_PATH "/spool");
 
        // Configure a bogus repo1 and restore from repo2 to ensure that restore correctly uses the selected repo
        hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 1, "/bogus");
        hrnCfgArgKeyRaw(argList, cfgOptRepoPath, 2, repoPath);
        hrnCfgArgKeyRawZ(argList, cfgOptRepoCipherType, 2, "aes-256-cbc");
        hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, TEST_CIPHER_PASS);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(hrnCmdRestore(), "restore");
 
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            "PG_VERSION\n"
            "base/\n"
            "base/1/\n"
            "base/1/2\n"
            "base/1/3\n"
            "base/1/44\n"
            "global/\n"
            "global/pg_control\n"
            "postgresql.auto.conf\n",
            .level = storageInfoLevelType);
 
        // -------------------------------------------------------------------------------------------------------------------------
        TEST_TITLE("delta restore with block incr");
 
        // Restore configuration to repo1
        argList = strLstNew();
        hrnCfgArgRawZ(argList, cfgOptStanza, "test1");
        hrnCfgArgRaw(argList, cfgOptRepoPath, repoPath);
        hrnCfgArgRaw(argList, cfgOptPgPath, pgPath);
        hrnCfgArgRawZ(argList, cfgOptSpoolPath, TEST_PATH "/spool");
        hrnCfgArgRawBool(argList, cfgOptDelta, true);
        hrnCfgArgRawZ(argList, cfgOptRepoCipherType, "aes-256-cbc");
        hrnCfgEnvRawZ(cfgOptRepoCipherPass, TEST_CIPHER_PASS);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        // Get last diff backup label and truncate bundle/1
        hrnSleepRemainder();
 
        StringList *backupList = storageListP(
            storageRepo(), STORAGE_REPO_BACKUP_STR, .expression = backupRegExpP(.full = true));
        strLstSort(backupList, sortOrderDesc);
        const String *backupFull = strLstGet(backupList, 0);
 
        HRN_STORAGE_PUT(storageRepoWrite(), strZ(strNewFmt(STORAGE_REPO_BACKUP "/%s/bundle/1", strZ(backupFull))), NULL);
 
        // Make sure restore fails with the invalid file
        TEST_ERROR(hrnCmdRestore(), CryptoError, "raised from local-1 shim protocol: cipher header missing");
 
        TEST_RESULT_UINT(
            cipherSpecType(
                manifestCipherSpec(
                    manifestNewLoad(storageReadIo(storageNewReadP(storagePg(), BACKUP_MANIFEST_FILE_STR)), TEST_CIPHER_SPEC))),
            cipherTypeNone, "manifest in data directory has no file passphrase");
 
        // Use detail log level to catch block incremental restore message
        harnessLogLevelSet(logLevelDetail);
 
        // Truncate file to ensure delta is skipped
        HRN_STORAGE_PUT_EMPTY(storagePgWrite(), PG_PATH_BASE "/1/44");
 
        // Shrink file to make sure block incremental delta will reuse it
        relation = bufNew(128 * 1024);
        memset(bufPtr(relation), 0, bufSize(relation));
        bufUsedSet(relation, bufSize(relation));
 
        HRN_STORAGE_PUT(storagePgWrite(), PG_PATH_BASE "/1/2", relation);
 
        // For this restore to succeed we need to set target time using the timestamp on backup.info
        String *targetTime = strNewTimeP(
            "%Y-%m-%d %H:%M:%S+00", storageInfoP(storageRepo(), INFO_BACKUP_PATH_FILE_STR).timeModified, .utc = true);
 
        hrnCfgArgRawZ(argList, cfgOptRepo, "1");
        hrnCfgArgRaw(argList, cfgOptRepoTargetTime, targetTime);
        HRN_CFG_LOAD(cfgCmdRestore, argList);
 
        TEST_RESULT_VOID(hrnCmdRestore(), "restore");
 
        TEST_STORAGE_LIST(
            storagePg(), NULL,
            "PG_VERSION\n"
            "base/\n"
            "base/1/\n"
            "base/1/2\n"
            "base/1/3\n"
            "base/1/44\n"
            "global/\n"
            "global/pg_control\n"
            "postgresql.auto.conf\n",
            .level = storageInfoLevelType);
 
        // Check that file was restored to full size with a partial write
        TEST_RESULT_LOG_EMPTY_OR_CONTAINS(", bi 128KB/256KB, ");
 
        hrnStorageHelperRepoShimSet(true);
    }
 
    FUNCTION_HARNESS_RETURN_VOID();
}