lific 2.8.0

Local-first, lightweight issue tracker. Single binary, SQLite-backed, MCP-native.
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
//! LIF-266: backup as an interface — `lific dump` / `lific restore`.
//!
//! The data set is no longer a single file. Since attachments (LIF-262) it is
//! `lific.db` plus a content-addressed `attachments/` sidecar dir living beside
//! it (blobs named by their sha256; in-progress writes carry a `.tmp`
//! extension — see [`crate::storage`]). A DB snapshot alone would restore
//! metadata rows pointing at missing blobs.
//!
//! This module follows the `gitea dump` pattern: one command produces one
//! self-contained, timestamped `lific_YYYYMMDD_HHMMSS.tar.gz` archive with
//! everything needed to restore — the DB, every attachment blob, and a
//! `manifest.json` describing what's inside. [`restore`] validates and unpacks
//! it back into a data dir. The interval backup task (`src/backup.rs`) emits
//! the *same* artifact via [`write_dump`], so there is exactly one backup shape.

use std::fs::File;
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};

use fs2::FileExt;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::db::DbPool;
use crate::error::LificError;

/// The DB filename inside every archive, independent of the on-disk name.
pub const ARCHIVE_DB_NAME: &str = "lific.db";
/// The manifest filename inside every archive.
pub const ARCHIVE_MANIFEST_NAME: &str = "manifest.json";
/// The prefix under which attachment blobs are stored inside the archive.
pub const ARCHIVE_ATTACHMENTS_PREFIX: &str = "attachments/";

/// Restore limits apply to the uncompressed archive contents. They prevent a
/// tiny gzip/tar upload from allocating unbounded memory or filling the data
/// volume before validation can reject it.
pub const MAX_MANIFEST_BYTES: u64 = 1024 * 1024;
pub const MAX_DB_BYTES: u64 = 512 * 1024 * 1024;
pub const MAX_ATTACHMENT_BYTES: u64 = 64 * 1024 * 1024;
pub const MAX_TOTAL_RESTORE_BYTES: u64 = 1024 * 1024 * 1024;
pub const MAX_RESTORE_ENTRIES: u64 = 10_000;
const ATTACHMENTS_SCHEMA_VERSION: i64 = 31;
const ATTACHMENT_INTEGRITY_SCHEMA_VERSION: i64 = 43;
const TAR_ENTRY_OVERHEAD: u64 = 1024;
const TAR_END_MARKER_BYTES: u64 = 1024;

/// Ceiling used by [`RestoreLimits::trusted`]. Large enough that no dump this
/// binary can produce is categorically unrestorable, small enough that a
/// decompression bomb still hits a wall rather than filling the volume
/// forever.
const TRUSTED_RESTORE_BYTES: u64 = 16 * 1024 * 1024 * 1024 * 1024; // 16 TiB
const TRUSTED_RESTORE_ENTRIES: u64 = 10_000_000;
const TRUSTED_MANIFEST_BYTES: u64 = 64 * 1024 * 1024;

/// Bounds applied to the *uncompressed* contents of an archive being restored.
///
/// [`RestoreLimits::default`] is what every untrusted restore gets: a tiny
/// gzip upload cannot allocate unbounded memory or fill the data volume before
/// validation rejects it. [`RestoreLimits::trusted`] is the `--allow-large`
/// escape hatch for an operator restoring their own legitimately large dump —
/// [`write_dump`] has no size ceiling, so without it a big-but-honest instance
/// could take backups that nothing could ever restore.
#[derive(Debug, Clone, Copy)]
pub struct RestoreLimits {
    pub max_manifest_bytes: u64,
    pub max_db_bytes: u64,
    pub max_attachment_bytes: u64,
    pub max_total_bytes: u64,
    pub max_entries: u64,
}

impl Default for RestoreLimits {
    fn default() -> Self {
        Self {
            max_manifest_bytes: MAX_MANIFEST_BYTES,
            max_db_bytes: MAX_DB_BYTES,
            max_attachment_bytes: MAX_ATTACHMENT_BYTES,
            max_total_bytes: MAX_TOTAL_RESTORE_BYTES,
            max_entries: MAX_RESTORE_ENTRIES,
        }
    }
}

impl RestoreLimits {
    /// Limits for an archive the operator vouches for (`lific restore
    /// --allow-large`). Every structural check still runs — entry names, tar
    /// entry types, manifest/content agreement, database integrity and blob
    /// hashes — only the size ceilings are raised.
    pub fn trusted() -> Self {
        Self {
            max_manifest_bytes: TRUSTED_MANIFEST_BYTES,
            max_db_bytes: TRUSTED_RESTORE_BYTES,
            max_attachment_bytes: TRUSTED_RESTORE_BYTES,
            max_total_bytes: TRUSTED_RESTORE_BYTES,
            max_entries: TRUSTED_RESTORE_ENTRIES,
        }
    }

    /// Hard cap handed to the gzip reader, so a decompression bomb is stopped
    /// by the reader itself rather than by a per-entry check.
    fn max_decompressed_bytes(&self) -> u64 {
        self.max_total_bytes
            .saturating_add(self.max_manifest_bytes)
            .saturating_add(
                self.max_entries
                    .saturating_add(2)
                    .saturating_mul(TAR_ENTRY_OVERHEAD),
            )
            .saturating_add(TAR_END_MARKER_BYTES)
    }
}

/// Everything [`run_restore_with`] needs beyond the paths.
#[derive(Debug, Clone, Copy, Default)]
pub struct RestoreOptions {
    /// Overwrite an existing database, moving the current one aside.
    pub force: bool,
    /// Size bounds for the archive.
    pub limits: RestoreLimits,
}

impl RestoreOptions {
    /// `allow_large` selects [`RestoreLimits::trusted`] over the bounded
    /// defaults.
    pub fn new(force: bool, allow_large: bool) -> Self {
        Self {
            force,
            limits: if allow_large {
                RestoreLimits::trusted()
            } else {
                RestoreLimits::default()
            },
        }
    }
}

/// Metadata describing an archive's contents. Serialized as `manifest.json`
/// at the root of every dump so a restore can validate compatibility and print
/// a summary without opening the DB.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Manifest {
    /// Crate version that produced the archive.
    pub lific_version: String,
    /// Highest applied migration version in the snapshotted DB. A restore onto
    /// an older binary (lower [`crate::db::migrate::latest_version`]) is
    /// refused.
    pub schema_version: i64,
    /// ISO 8601 UTC timestamp the dump was taken.
    pub created_at: String,
    /// Size of the snapshotted DB file in bytes.
    pub db_size_bytes: u64,
    /// Number of attachment blobs included.
    pub attachment_count: u64,
    /// Total bytes across all attachment blobs.
    pub attachment_bytes: u64,
}

/// A UTC timestamp in the archive filename convention (`YYYYMMDD_HHMMSS`),
/// matching the legacy backup naming so both schemes sort together.
pub fn archive_timestamp() -> String {
    chrono::Utc::now().format("%Y%m%d_%H%M%S").to_string()
}

/// The default archive filename for a given DB stem and timestamp, e.g.
/// `lific_20260101_120000.tar.gz`.
pub fn archive_filename(db_stem: &str, timestamp: &str) -> String {
    format!("{db_stem}_{timestamp}.tar.gz")
}

/// Resolve the attachments sidecar dir for a database path (mirrors
/// [`crate::storage::AttachmentStore::from_db_path`]).
fn attachments_dir_for(db_path: &Path) -> PathBuf {
    match db_path.parent() {
        Some(parent) if !parent.as_os_str().is_empty() => parent.join("attachments"),
        _ => PathBuf::from("attachments"),
    }
}

/// Return whether another process currently owns a dump staging lock.
pub(crate) fn staging_is_locked(path: &Path) -> bool {
    let lock = path.join("lock");
    let Ok(file) = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(lock)
    else {
        return true;
    };
    file.try_lock_exclusive().is_err()
}

/// Take a consistent snapshot of the live DB into the already-reserved staging
/// file `dest`.
///
/// The SQLite online backup API holds no long writer lock and copies into a
/// file this process created with `O_CREAT|O_EXCL|O_NOFOLLOW` — so, unlike
/// `VACUUM INTO`, nothing resolves a pathname a second time between reserving
/// the destination and writing it.
///
/// The source is a dedicated read-only connection opened here and dropped when
/// the snapshot finishes, not a pooled reader. Pooled connections carry
/// `mmap_size = 64MB` and never close, and the backup API reads the entire
/// database — so routing snapshots through the pool faulted each reader's full
/// mmap window into RSS permanently. On the production instance that cost
/// ~430MB of resident double-counted page-cache mappings (one 64MB window per
/// reader the 30-minute backup task had ever touched). A short-lived source
/// connection with SQLite's default `mmap_size = 0` reads through plain I/O
/// and gives every page back when it closes; the pooled readers only ever map
/// what their own queries touch.
fn snapshot_db(db_path: &Path, dest: &TempFile) -> Result<(), LificError> {
    let conn = rusqlite::Connection::open_with_flags(
        db_path,
        rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NOFOLLOW,
    )
    .map_err(|e| LificError::Internal(format!("open snapshot source connection: {e}")))?;
    conn.busy_timeout(std::time::Duration::from_millis(5000))
        .map_err(|e| LificError::Internal(format!("set snapshot busy timeout: {e}")))?;
    let mut destination = rusqlite::Connection::open_with_flags(
        dest.path(),
        rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE | rusqlite::OpenFlags::SQLITE_OPEN_NOFOLLOW,
    )
    .map_err(|e| LificError::Internal(format!("open SQLite staging file: {e}")))?;
    let backup = rusqlite::backup::Backup::new(&conn, &mut destination)
        .map_err(|e| LificError::Internal(format!("create SQLite backup: {e}")))?;
    backup
        .run_to_completion(100, std::time::Duration::ZERO, None)
        .map_err(|e| LificError::Internal(format!("SQLite backup failed: {e}")))?;
    Ok(())
}

/// Set 0600 permissions on a file (owner-only) on Unix. No-op elsewhere.
#[cfg_attr(
    not(unix),
    expect(
        clippy::unnecessary_wraps,
        reason = "keep one fallible cross-platform dump API"
    )
)]
fn set_owner_only(path: &Path) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?;
    }
    #[cfg(not(unix))]
    {
        let _ = path;
    }
    Ok(())
}

/// Set 0600 on an already-open handle, so no pathname is resolved again.
#[cfg_attr(
    not(unix),
    expect(clippy::unnecessary_wraps, reason = "fallible on Unix")
)]
fn set_owner_only_file(file: &File) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        file.set_permissions(std::fs::Permissions::from_mode(0o600))?;
    }
    #[cfg(not(unix))]
    {
        let _ = file;
    }
    Ok(())
}

/// Set 0700 permissions on a directory (owner-only) on Unix. No-op elsewhere.
#[cfg_attr(
    not(unix),
    expect(clippy::unnecessary_wraps, reason = "fallible on Unix")
)]
fn set_owner_only_dir(path: &Path) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))?;
    }
    #[cfg(not(unix))]
    {
        let _ = path;
    }
    Ok(())
}

/// fsync a directory so a rename into it is durable. Unix only: Windows has no
/// directory handle to sync, and its rename ordering does not need one.
#[cfg_attr(
    not(unix),
    expect(clippy::unnecessary_wraps, reason = "fallible on Unix")
)]
fn sync_dir(path: &Path) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        File::open(path)?.sync_all()?;
    }
    #[cfg(not(unix))]
    {
        let _ = path;
    }
    Ok(())
}

/// A file this process created exclusively and owns for its lifetime; removed
/// on drop, including while a `?` unwinds out of [`write_dump`].
struct TempFile {
    file: File,
    path: PathBuf,
}

impl TempFile {
    fn create(path: PathBuf) -> Result<Self, LificError> {
        let mut options = std::fs::OpenOptions::new();
        options.write(true).create_new(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            options.mode(0o600).custom_flags(libc::O_NOFOLLOW);
        }
        let file = options.open(&path).map_err(|error| {
            LificError::Internal(format!("create secure staging file: {error}"))
        })?;
        Ok(Self { file, path })
    }

    fn path(&self) -> &Path {
        &self.path
    }
}

impl Drop for TempFile {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.path);
    }
}

/// An advisory exclusive lock held for the lifetime of one dump's staging
/// directory. The interval backup sweep reads it through
/// [`staging_is_locked`], so an in-flight dump is never swept out from under
/// itself, and a crashed one releases the lock with the process.
struct StagingLock {
    _file: File,
}

impl StagingLock {
    fn create(path: &Path) -> Result<Self, LificError> {
        let file = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(path)
            .map_err(|error| LificError::Internal(format!("create dump lock: {error}")))?;
        file.try_lock_exclusive()
            .map_err(|error| LificError::Internal(format!("lock dump staging: {error}")))?;
        Ok(Self { _file: file })
    }
}

/// Touch the staging activity marker so the age-gated sweep in
/// [`crate::backup`] can tell a long-running dump from a crash leftover.
fn refresh_activity(file: &File) -> std::io::Result<()> {
    file.set_modified(std::time::SystemTime::now())
}

/// Write a self-contained dump archive to `out_path`.
///
/// Shared code path used by both `lific dump` and the interval backup task.
/// Produces a gzip-compressed tar containing `lific.db` (a consistent snapshot
/// taken with SQLite's online backup API), every non-`.tmp` attachment blob
/// under `attachments/`, and `manifest.json`. The finished file is chmod 0600
/// (it contains the whole DB).
///
/// Everything is staged in a private directory beside the output and published
/// with a single rename, so nothing partial is ever visible at `out_path`, and
/// the destination is checked for symlink/hard-link tricks first.
///
/// Returns the [`Manifest`] that was written, so callers can log/print it.
pub fn write_dump(pool: &DbPool, db_path: &Path, out_path: &Path) -> Result<Manifest, LificError> {
    // Hold the attachment store's lock for the whole dump: the DB snapshot,
    // the blob scan, the hash verification and the publication. An upload,
    // delete or GC sweep landing between the snapshot and the scan would
    // otherwise produce an archive whose database references a blob the
    // archive does not contain, or whose blob bytes no longer match the hash
    // that was verified. Store lock first, then the dedicated snapshot
    // connection inside `snapshot_db`; the pool is only used for metadata
    // queries after the snapshot exists.
    let store = crate::storage::AttachmentStore::from_db_path(db_path);
    store.with_lock(|_| write_dump_locked(pool, db_path, out_path))
}

fn write_dump_locked(
    pool: &DbPool,
    db_path: &Path,
    out_path: &Path,
) -> Result<Manifest, LificError> {
    // A bare filename has an empty parent, which is the current directory.
    let parent = match out_path.parent() {
        Some(parent) if !parent.as_os_str().is_empty() => parent,
        _ => Path::new("."),
    };
    validate_dump_destination(out_path)?;

    // Everything is staged inside a private 0700 directory beside the output.
    // Another user of a shared output directory therefore cannot swap either
    // staging pathname while an open handle to it is live, and a crash leaves
    // exactly one sweepable directory rather than loose `*.tmp` files
    // (LIF-329).
    let staging = tempfile::Builder::new()
        .prefix(".lific-dump-")
        .tempdir_in(parent)
        .map_err(|e| LificError::Internal(format!("create dump staging directory: {e}")))?;
    set_owner_only_dir(staging.path())
        .map_err(|e| LificError::Internal(format!("secure dump staging directory: {e}")))?;
    let _lock = StagingLock::create(&staging.path().join("lock"))?;
    let activity = TempFile::create(staging.path().join("activity"))?;
    refresh_activity(&activity.file)
        .map_err(|e| LificError::Internal(format!("mark dump staging active: {e}")))?;

    let tmp_db = TempFile::create(staging.path().join("dbsnapshot"))?;
    snapshot_db(db_path, &tmp_db)?;
    refresh_activity(&activity.file)
        .map_err(|e| LificError::Internal(format!("refresh dump staging activity: {e}")))?;

    let tmp_archive = TempFile::create(staging.path().join("archive"))?;

    let db_size_bytes = tmp_db
        .file
        .metadata()
        .map(|m| m.len())
        .map_err(|e| LificError::Internal(format!("size db snapshot: {e}")))?;

    // Gather attachment blobs (skip .tmp in-progress writes). Each candidate is
    // opened no-follow and verified here; the manifest is built from what those
    // handles reported, and the archive pass below re-opens each one and
    // refuses to archive anything that is no longer the same object.
    let attachments_dir = attachments_dir_for(db_path);
    let mut blobs: Vec<(String, PathBuf, BlobIdentity)> = Vec::new();
    let mut attachment_bytes: u64 = 0;
    if attachments_dir.is_dir() {
        for entry in std::fs::read_dir(&attachments_dir)
            .map_err(|e| LificError::Internal(format!("read attachments dir: {e}")))?
        {
            let entry =
                entry.map_err(|e| LificError::Internal(format!("read attachments entry: {e}")))?;
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) == Some("tmp") {
                continue;
            }
            let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
                continue;
            };
            // A blob filename is a bare lowercase sha256; anything else in the
            // store is not ours to archive.
            if validate_attachment_entry(&format!("{ARCHIVE_ATTACHMENTS_PREFIX}{name}")).is_err() {
                continue;
            }
            let Some(blob) = open_verified_blob(&path)? else {
                continue;
            };
            attachment_bytes = attachment_bytes
                .checked_add(blob.identity.size)
                .ok_or_else(|| LificError::Internal("attachment size overflow".into()))?;
            blobs.push((name.to_string(), path.clone(), blob.identity));
        }
    }

    let manifest = Manifest {
        lific_version: env!("CARGO_PKG_VERSION").to_string(),
        schema_version: schema_version(pool),
        created_at: chrono::Utc::now().to_rfc3339(),
        db_size_bytes,
        attachment_count: blobs.len() as u64,
        attachment_bytes,
    };
    let manifest_json = serde_json::to_vec_pretty(&manifest)
        .map_err(|e| LificError::Internal(format!("serialize manifest: {e}")))?;

    // Build the archive into the reserved staging file, then atomically rename
    // it into place so a partial write is never observed at the final path.
    {
        let file = tmp_archive
            .file
            .try_clone()
            .map_err(|e| LificError::Internal(format!("open archive staging handle: {e}")))?;
        let enc = flate2::write::GzEncoder::new(file, flate2::Compression::default());
        let mut tar = tar::Builder::new(enc);

        // manifest.json
        append_bytes(&mut tar, ARCHIVE_MANIFEST_NAME, &manifest_json)?;
        // lific.db (from the snapshot file)
        tar.append_path_with_name(tmp_db.path(), ARCHIVE_DB_NAME)
            .map_err(|e| LificError::Internal(format!("append db to archive: {e}")))?;
        // attachments/<sha256>. One handle is open at a time, so a store with
        // thousands of blobs cannot exhaust the process's file descriptors.
        for (name, path, identity) in &blobs {
            refresh_activity(&activity.file)
                .map_err(|e| LificError::Internal(format!("refresh dump staging activity: {e}")))?;
            let Some(mut blob) = open_verified_blob(path)? else {
                return Err(LificError::Internal(format!(
                    "attachment {name} was replaced while the dump was running"
                )));
            };
            if blob.identity != *identity {
                return Err(LificError::Internal(format!(
                    "attachment {name} changed while the dump was running"
                )));
            }
            let entry_name = format!("{ARCHIVE_ATTACHMENTS_PREFIX}{name}");
            blob.append_to(&mut tar, &entry_name, name)?;
        }

        let enc = tar
            .into_inner()
            .map_err(|e| LificError::Internal(format!("finalize tar: {e}")))?;
        enc.finish()
            .map_err(|e| LificError::Internal(format!("finalize gzip: {e}")))?;
    }

    tmp_archive
        .file
        .sync_all()
        .map_err(|e| LificError::Internal(format!("sync archive: {e}")))?;
    set_owner_only_file(&tmp_archive.file)
        .map_err(|e| LificError::Internal(format!("chmod archive: {e}")))?;
    std::fs::rename(tmp_archive.path(), out_path)
        .map_err(|e| LificError::Internal(format!("finalize archive: {e}")))?;
    // The rename is only durable once the directory entry is on disk.
    sync_dir(parent).map_err(|e| LificError::Internal(format!("sync dump destination: {e}")))?;

    Ok(manifest)
}

/// An attachment blob opened once, verified through that same handle, and
/// archived from it.
///
/// Checking a pathname and then handing the pathname to `tar` for a second
/// open is a TOCTOU window: whoever can write the attachments directory can
/// swap the name for a symlink between the two. Everything here — the file
/// type, the Unix link count, and the size written into the tar header — comes
/// from the one descriptor whose bytes end up in the archive.
struct VerifiedBlob {
    file: File,
    identity: BlobIdentity,
    mtime: u64,
}

/// What the scan pass recorded about a blob, so the archive pass can prove it
/// is streaming the same object rather than something swapped in behind it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct BlobIdentity {
    size: u64,
    #[cfg(unix)]
    dev: u64,
    #[cfg(unix)]
    ino: u64,
}

/// Whether an open failure means "that name is a symlink and O_NOFOLLOW
/// refused it". Linux reports `ELOOP`, the BSDs `EMLINK`;
/// `ErrorKind::FilesystemLoop` is still unstable, so match the raw codes.
fn is_symlink(error: &std::io::Error) -> bool {
    #[cfg(unix)]
    {
        matches!(error.raw_os_error(), Some(code) if code == libc::ELOOP || code == libc::EMLINK)
    }
    #[cfg(not(unix))]
    {
        let _ = error;
        false
    }
}

/// Open `path` without following symlinks and validate it as an archivable
/// blob. `Ok(None)` means "not ours to archive" (a symlink, a directory, or an
/// entry that vanished mid-scan); `Err` means the store is in a state a dump
/// must not silently paper over.
fn open_verified_blob(path: &Path) -> Result<Option<VerifiedBlob>, LificError> {
    let mut options = std::fs::OpenOptions::new();
    options.read(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.custom_flags(libc::O_NOFOLLOW);
    }
    let file = match options.open(path) {
        Ok(file) => file,
        // A concurrent GC can remove a blob between readdir and open, and
        // O_NOFOLLOW refuses a symlink. Neither is a reason to fail the dump;
        // both mean "nothing of ours to archive here".
        Err(error) if error.kind() == std::io::ErrorKind::NotFound || is_symlink(&error) => {
            return Ok(None);
        }
        Err(error) => {
            return Err(LificError::Internal(format!(
                "open attachment {}: {error}",
                path.display()
            )));
        }
    };

    let metadata = file
        .metadata()
        .map_err(|e| LificError::Internal(format!("inspect attachment {}: {e}", path.display())))?;
    if !metadata.is_file() {
        return Ok(None);
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        // A hard link means the same inode is reachable from outside the
        // store, so its bytes are not under Lific's control.
        if metadata.nlink() != 1 {
            return Err(LificError::Internal(format!(
                "attachment entry is hard-linked: {}",
                path.display()
            )));
        }
    }
    let mtime = metadata
        .modified()
        .ok()
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map_or(0, |d| d.as_secs());

    #[cfg(unix)]
    let identity = {
        use std::os::unix::fs::MetadataExt;
        BlobIdentity {
            size: metadata.len(),
            dev: metadata.dev(),
            ino: metadata.ino(),
        }
    };
    #[cfg(not(unix))]
    let identity = BlobIdentity {
        size: metadata.len(),
    };

    Ok(Some(VerifiedBlob {
        file,
        identity,
        mtime,
    }))
}

impl VerifiedBlob {
    /// Stream this blob into the archive from its verified handle, hashing the
    /// bytes on the way through.
    ///
    /// `expected_sha` is the blob's filename, which in a content-addressed
    /// store *is* its digest. Hashing during the copy is the only way to be
    /// sure the bytes in the archive are the bytes that were verified: hashing
    /// a second pass over the file would prove something about a different
    /// read. A mismatch fails the dump before publication, so a store that has
    /// been corrupted (bit rot, a hand-edited blob, a name that never matched
    /// its content) can never be published as a valid-looking backup that a
    /// restore would then reject.
    fn append_to<W: Write>(
        &mut self,
        tar: &mut tar::Builder<W>,
        entry_name: &str,
        expected_sha: &str,
    ) -> Result<(), LificError> {
        let size = self.identity.size;
        let mut header = tar::Header::new_gnu();
        header.set_entry_type(tar::EntryType::Regular);
        header.set_size(size);
        header.set_mode(0o600);
        header.set_mtime(self.mtime);
        header.set_cksum();
        self.file
            .rewind()
            .map_err(|e| LificError::Internal(format!("rewind attachment {expected_sha}: {e}")))?;
        let mut hashing = HashingReader::new((&self.file).take(size));
        tar.append_data(&mut header, entry_name, &mut hashing)
            .map_err(|e| LificError::Internal(format!("append attachment {expected_sha}: {e}")))?;
        let digest = hashing.hex_digest();
        // The header already promised `size` bytes; publishing an archive whose
        // body is shorter would leave every later entry misaligned.
        let written = self
            .file
            .stream_position()
            .map_err(|e| LificError::Internal(format!("measure attachment {expected_sha}: {e}")))?;
        if written != size {
            return Err(LificError::Internal(format!(
                "attachment {expected_sha} changed size while being archived \
                 ({written} of {size} bytes)"
            )));
        }
        if digest != expected_sha {
            return Err(LificError::Internal(format!(
                "attachment {expected_sha} does not match its content address (hashed {digest}); \
                 refusing to publish a corrupt archive"
            )));
        }
        Ok(())
    }
}

/// A reader that digests everything it passes through, so a copy and its
/// verification see exactly the same bytes.
struct HashingReader<R> {
    inner: R,
    hasher: Sha256,
}

impl<R: Read> HashingReader<R> {
    fn new(inner: R) -> Self {
        Self {
            inner,
            hasher: Sha256::new(),
        }
    }

    fn hex_digest(self) -> String {
        crate::auth::hex_encode(&self.hasher.finalize())
    }
}

impl<R: Read> Read for HashingReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let read = self.inner.read(buf)?;
        self.hasher.update(&buf[..read]);
        Ok(read)
    }
}

/// Refuse to publish a dump onto a destination another user could have
/// prepared: a symlink or hard link at the target, a symlinked parent, or a
/// group/world-writable parent without the sticky bit.
#[cfg_attr(
    not(unix),
    expect(clippy::unnecessary_wraps, reason = "fallible on Unix")
)]
fn validate_dump_destination(out_path: &Path) -> Result<(), LificError> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        let parent = out_path.parent().unwrap_or_else(|| Path::new("."));
        let parent = if parent.as_os_str().is_empty() {
            Path::new(".")
        } else {
            parent
        };
        let parent_metadata = std::fs::symlink_metadata(parent)
            .map_err(|e| LificError::Internal(format!("inspect dump destination: {e}")))?;
        if parent_metadata.file_type().is_symlink() {
            return Err(LificError::Internal(
                "dump destination parent is a symlink".into(),
            ));
        }
        let mode = parent_metadata.mode();
        if mode & 0o1000 == 0 && mode & 0o022 != 0 {
            return Err(LificError::Internal(
                "dump destination parent is group/world-writable without sticky protection".into(),
            ));
        }
        if let Ok(metadata) = std::fs::symlink_metadata(out_path)
            && (metadata.file_type().is_symlink() || metadata.nlink() > 1)
        {
            return Err(LificError::Internal(
                "dump destination must not be a symlink or hard link".into(),
            ));
        }
    }
    #[cfg(not(unix))]
    let _ = out_path;
    Ok(())
}

/// Append raw bytes as a tar entry with the given name.
fn append_bytes<W: std::io::Write>(
    tar: &mut tar::Builder<W>,
    name: &str,
    bytes: &[u8],
) -> Result<(), LificError> {
    let mut header = tar::Header::new_gnu();
    header.set_size(bytes.len() as u64);
    header.set_mode(0o600);
    header.set_mtime(
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| d.as_secs()),
    );
    header.set_cksum();
    tar.append_data(&mut header, name, bytes)
        .map_err(|e| LificError::Internal(format!("append {name}: {e}")))
}

/// Read the highest applied migration version from the DB (0 if unavailable).
fn schema_version(pool: &DbPool) -> i64 {
    pool.read()
        .ok()
        .and_then(|conn| {
            conn.query_row(
                "SELECT COALESCE(MAX(version), 0) FROM _migrations",
                [],
                |row| row.get::<_, i64>(0),
            )
            .ok()
        })
        .unwrap_or(0)
}

// ── dump (CLI) ───────────────────────────────────────────────

/// Result of a `lific dump`, for printing/JSON output.
pub struct DumpResult {
    pub archive_path: PathBuf,
    pub manifest: Manifest,
}

/// Run `lific dump`: resolve the output path (file or directory), snapshot the
/// DB at `db_path`, and write the archive. `out` may be `None` (current dir), a
/// directory (default filename inside it), or a full target file path.
pub fn run_dump(db_path: &Path, out: Option<&Path>) -> Result<DumpResult, LificError> {
    let pool = crate::db::open(db_path)?;

    let db_stem = db_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("lific");
    let default_name = archive_filename(db_stem, &archive_timestamp());

    let archive_path = match out {
        None => std::env::current_dir()
            .map_err(|e| LificError::Internal(format!("resolve current dir: {e}")))?
            .join(&default_name),
        Some(p) => {
            // A path that exists as a dir, or ends with a separator, is a
            // target directory → use the default filename inside it.
            if p.is_dir() {
                p.join(&default_name)
            } else {
                p.to_path_buf()
            }
        }
    };

    if let Some(parent) = archive_path.parent()
        && !parent.as_os_str().is_empty()
    {
        std::fs::create_dir_all(parent)
            .map_err(|e| LificError::Internal(format!("create output dir: {e}")))?;
    }

    let manifest = write_dump(&pool, db_path, &archive_path)?;
    Ok(DumpResult {
        archive_path,
        manifest,
    })
}

// ── restore (CLI) ────────────────────────────────────────────

/// Summary returned by a successful restore for printing/JSON.
#[derive(Debug)]
pub struct RestoreResult {
    pub manifest: Manifest,
    pub attachment_count: u64,
    pub db_path: PathBuf,
    /// Where the pre-existing DB was moved, if `--force` displaced one.
    pub moved_existing_to: Option<PathBuf>,
}

/// Validate that an attachment entry name is a bare hash under `attachments/`
/// with no path traversal. Returns the bare filename on success.
fn validate_attachment_entry(name: &str) -> Result<String, LificError> {
    let rest = name
        .strip_prefix(ARCHIVE_ATTACHMENTS_PREFIX)
        .ok_or_else(|| LificError::BadRequest(format!("unexpected archive entry: {name}")))?;
    // Reject empty, nested paths, parent refs, absolute paths, or anything with
    // a separator — a blob name is a bare sha256 hex string.
    if rest.is_empty()
        || rest.contains('/')
        || rest.contains('\\')
        || rest.contains("..")
        || rest.starts_with('.')
    {
        return Err(LificError::BadRequest(format!(
            "rejected attachment entry (path traversal or invalid name): {name}"
        )));
    }
    if !crate::storage::valid_sha256(rest) {
        return Err(LificError::BadRequest(format!(
            "rejected attachment entry (expected lowercase SHA-256): {name}"
        )));
    }
    Ok(rest.to_string())
}

fn validate_manifest_limits(manifest: &Manifest, limits: &RestoreLimits) -> Result<(), LificError> {
    if manifest.db_size_bytes > limits.max_db_bytes {
        return Err(LificError::BadRequest(format!(
            "archive database exceeds restore limit ({} > {} bytes)",
            manifest.db_size_bytes, limits.max_db_bytes
        )));
    }
    if manifest.attachment_count > limits.max_entries {
        return Err(LificError::BadRequest(format!(
            "archive has too many attachments ({} > {})",
            manifest.attachment_count, limits.max_entries
        )));
    }
    if manifest.attachment_bytes > limits.max_total_bytes
        || manifest.db_size_bytes > limits.max_total_bytes - manifest.attachment_bytes
    {
        return Err(LificError::BadRequest(
            "archive contents exceed total restore size limit".into(),
        ));
    }
    Ok(())
}

fn read_entry_bounded<R: Read>(
    entry: &mut tar::Entry<'_, R>,
    max_bytes: u64,
    label: &str,
) -> Result<Vec<u8>, LificError> {
    let size = entry.size();
    if size > max_bytes {
        return Err(LificError::BadRequest(format!(
            "{label} exceeds restore limit ({size} > {max_bytes} bytes)"
        )));
    }
    let mut buf = Vec::with_capacity(size as usize);
    entry
        .read_to_end(&mut buf)
        .map_err(|e| LificError::BadRequest(format!("read {label}: {e}")))?;
    Ok(buf)
}

fn bounded_archive_with_limit(
    file: std::fs::File,
    max_decompressed_bytes: u64,
) -> tar::Archive<std::io::Take<flate2::read::GzDecoder<std::fs::File>>> {
    let decoder = flate2::read::GzDecoder::new(file);
    tar::Archive::new(decoder.take(max_decompressed_bytes))
}

fn bounded_archive(
    file: std::fs::File,
    limits: &RestoreLimits,
) -> tar::Archive<std::io::Take<flate2::read::GzDecoder<std::fs::File>>> {
    bounded_archive_with_limit(file, limits.max_decompressed_bytes())
}

fn validate_tar_entry_type<R: Read>(entry: &tar::Entry<'_, R>) -> Result<(), LificError> {
    if !entry.header().entry_type().is_file() {
        return Err(LificError::BadRequest(
            "archive contains an unsupported tar entry type".into(),
        ));
    }
    Ok(())
}

fn copy_entry_bounded<R: Read, W: Write>(
    entry: &mut tar::Entry<'_, R>,
    output: &mut W,
    max_bytes: u64,
    total_bytes: &mut u64,
    total_limit: u64,
    label: &str,
) -> Result<u64, LificError> {
    let size = entry.size();
    if size > max_bytes {
        return Err(LificError::BadRequest(format!(
            "{label} exceeds restore limit ({size} > {max_bytes} bytes)"
        )));
    }
    let new_total = total_bytes
        .checked_add(size)
        .ok_or_else(|| LificError::BadRequest("archive size overflow".into()))?;
    if new_total > total_limit {
        return Err(LificError::BadRequest(
            "archive contents exceed total restore size limit".into(),
        ));
    }

    let copied = std::io::copy(entry, output)
        .map_err(|e| LificError::BadRequest(format!("read {label}: {e}")))?;
    if copied != size {
        return Err(LificError::BadRequest(format!(
            "read {label}: expected {size} bytes, got {copied}"
        )));
    }
    *total_bytes = new_total;
    Ok(copied)
}

fn hash_file(path: &Path) -> Result<String, LificError> {
    let mut file = std::fs::File::open(path)
        .map_err(|e| LificError::BadRequest(format!("open attachment: {e}")))?;
    let mut digest = Sha256::new();
    let mut buffer = [0; 8192];
    loop {
        let read = file
            .read(&mut buffer)
            .map_err(|e| LificError::BadRequest(format!("read attachment: {e}")))?;
        if read == 0 {
            break;
        }
        digest.update(&buffer[..read]);
    }
    Ok(crate::auth::hex_encode(&digest.finalize()))
}

fn validate_attachment_schema(conn: &rusqlite::Connection) -> Result<(), LificError> {
    let sql: String = conn
        .query_row(
            "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'attachments'",
            [],
            |row| row.get(0),
        )
        .map_err(|e| LificError::BadRequest(format!("read staged attachment schema: {e}")))?;
    let probe = rusqlite::Connection::open_in_memory()
        .map_err(|e| LificError::BadRequest(format!("create staged schema probe: {e}")))?;
    probe
        .execute_batch("CREATE TABLE users (id INTEGER PRIMARY KEY);")
        .map_err(|e| LificError::BadRequest(format!("create staged schema probe: {e}")))?;
    probe
        .execute_batch(&sql)
        .map_err(|e| LificError::BadRequest(format!("create staged schema probe: {e}")))?;

    let insert = |sha256: &str, mime: &str, size_bytes: i64| {
        probe.execute(
            "INSERT INTO attachments (sha256, filename, mime, size_bytes)
             VALUES (?1, 'schema-probe', ?2, ?3)",
            rusqlite::params![sha256, mime, size_bytes],
        )
    };
    let valid_sha = crate::storage::AttachmentStore::hash_bytes(b"schema-probe");
    let invalid_mime_sha = "b".repeat(64);
    let invalid_size_sha = "c".repeat(64);
    insert(&valid_sha, "text/plain", 0).map_err(|e| {
        LificError::BadRequest(format!(
            "staged attachment schema rejects valid metadata: {e}"
        ))
    })?;

    for (sha256, mime, size_bytes) in [
        ("../outside", "text/plain", 0),
        (invalid_mime_sha.as_str(), "application/octet-stream", 0),
        (invalid_size_sha.as_str(), "text/plain", -1),
    ] {
        if insert(sha256, mime, size_bytes).is_ok() {
            return Err(LificError::BadRequest(
                "staged attachment table is missing required integrity constraints".into(),
            ));
        }
    }
    Ok(())
}

/// Serializes restores against one data directory.
///
/// The lock lives in the kernel (an fs2 advisory exclusive lock), not in the
/// existence of a path. A crashed restore therefore leaves at most a zero-byte
/// `.lific-restore.lock` file that the next restore reuses immediately, while
/// a restore running *right now* still rejects a second one. Owning a
/// directory instead, as the first cut of this did, made a crash permanently
/// wedge the data dir until someone found and deleted the leftover by hand.
struct RestoreLock {
    file: File,
}

impl RestoreLock {
    fn acquire(data_dir: &Path) -> Result<Self, LificError> {
        let path = data_dir.join(".lific-restore.lock");
        let mut options = std::fs::OpenOptions::new();
        options.read(true).write(true).create(true).truncate(false);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            // No-follow: the lock file must not be a symlink someone planted
            // to have us open (and later truncate-free-write) a file elsewhere.
            options.mode(0o600).custom_flags(libc::O_NOFOLLOW);
        }
        let file = options
            .open(&path)
            .map_err(|error| LificError::Internal(format!("create restore lock: {error}")))?;
        file.try_lock_exclusive().map_err(|_| {
            LificError::Conflict(
                "another restore is already running for this data directory".into(),
            )
        })?;
        Ok(Self { file })
    }
}

impl Drop for RestoreLock {
    fn drop(&mut self) {
        // Releasing the lock is what matters; the (empty) file is left in place
        // deliberately, since unlinking it races another process that already
        // has it open.
        let _ = FileExt::unlock(&self.file);
    }
}

/// Create a staging file this process exclusively owns: never following a
/// symlink, never adopting an existing file, owner-only from creation.
fn create_staging_file(path: &Path) -> Result<File, LificError> {
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600).custom_flags(libc::O_NOFOLLOW);
    }
    options
        .open(path)
        .map_err(|e| LificError::Internal(format!("create staging file: {e}")))
}

/// Validate the extracted SQLite file before it can replace the live DB. This
/// catches corrupt archives and ensures every metadata attachment reference is
/// a safe content-addressed filename with matching staged bytes.
fn validate_staged_database(
    staging: &Path,
    manifest: &Manifest,
    limits: &RestoreLimits,
) -> Result<(), LificError> {
    let db = staging.join(ARCHIVE_DB_NAME);
    let conn =
        rusqlite::Connection::open_with_flags(&db, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
            .map_err(|e| LificError::BadRequest(format!("open staged database: {e}")))?;
    let check: String = conn
        .query_row("PRAGMA quick_check", [], |row| row.get(0))
        .map_err(|e| LificError::BadRequest(format!("validate staged database: {e}")))?;
    if check != "ok" {
        return Err(LificError::BadRequest(format!(
            "staged database integrity check failed: {check}"
        )));
    }

    let schema_version: i64 = conn
        .query_row(
            "SELECT COALESCE(MAX(version), 0) FROM _migrations",
            [],
            |row| row.get(0),
        )
        .map_err(|e| LificError::BadRequest(format!("read staged schema version: {e}")))?;
    if schema_version != manifest.schema_version {
        return Err(LificError::BadRequest(format!(
            "staged database schema version {schema_version} does not match manifest {}",
            manifest.schema_version
        )));
    }
    if schema_version > crate::db::migrate::latest_version() {
        return Err(LificError::BadRequest(format!(
            "staged database schema version {schema_version} is newer than this binary"
        )));
    }

    let has_attachments: bool = conn
        .query_row(
            "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'attachments')",
            [],
            |row| row.get(0),
        )
        .map_err(|e| LificError::BadRequest(format!("inspect staged schema: {e}")))?;
    if !has_attachments
        && (schema_version >= ATTACHMENTS_SCHEMA_VERSION
            || manifest.attachment_count != 0
            || manifest.attachment_bytes != 0)
    {
        return Err(LificError::BadRequest(
            "staged database is missing the attachments table".into(),
        ));
    }

    if has_attachments && schema_version < ATTACHMENTS_SCHEMA_VERSION {
        return Err(LificError::BadRequest(
            "staged database has attachment tables at an unsupported schema version".into(),
        ));
    }

    if has_attachments && schema_version >= ATTACHMENT_INTEGRITY_SCHEMA_VERSION {
        validate_attachment_schema(&conn)?;
    }
    if has_attachments {
        let mime_placeholders = vec!["?"; crate::storage::ALLOWED_MIMES.len()].join(", ");
        let mut stmt = conn
            .prepare(&format!(
                "SELECT sha256, MIN(size_bytes), MAX(size_bytes),
                        SUM(CASE WHEN mime IN ({mime_placeholders}) THEN 0 ELSE 1 END)
                 FROM attachments
                 GROUP BY sha256"
            ))
            .map_err(|e| LificError::BadRequest(format!("read staged attachment metadata: {e}")))?;
        let rows = stmt
            .query_map(
                rusqlite::params_from_iter(crate::storage::ALLOWED_MIMES.iter().copied()),
                |row| {
                    Ok((
                        row.get::<_, String>(0)?,
                        row.get::<_, i64>(1)?,
                        row.get::<_, i64>(2)?,
                        row.get::<_, i64>(3)?,
                    ))
                },
            )
            .map_err(|e| LificError::BadRequest(format!("read staged attachment metadata: {e}")))?;
        for row in rows {
            let (sha, min_size, max_size, invalid_mimes) = row.map_err(|e| {
                LificError::BadRequest(format!("read staged attachment metadata: {e}"))
            })?;
            if !crate::storage::valid_sha256(&sha)
                || min_size < 0
                || min_size != max_size
                || invalid_mimes != 0
            {
                return Err(LificError::BadRequest(
                    "staged attachment metadata has an invalid content address, MIME, or size"
                        .into(),
                ));
            }
            let size = u64::try_from(max_size).map_err(|_| {
                LificError::BadRequest(
                    "staged attachment metadata has an invalid content address, MIME, or size"
                        .into(),
                )
            })?;
            if size > limits.max_attachment_bytes {
                return Err(LificError::BadRequest(
                    "staged attachment metadata has an invalid content address, MIME, or size"
                        .into(),
                ));
            }
            let path = staging.join("attachments").join(&sha);
            let metadata = std::fs::metadata(&path).map_err(|_| {
                LificError::BadRequest(format!(
                    "staged database references missing attachment {sha}"
                ))
            })?;
            if !metadata.is_file() || metadata.len() != size {
                return Err(LificError::BadRequest(format!(
                    "staged attachment {sha} does not match database metadata"
                )));
            }
            if hash_file(&path)? != sha {
                return Err(LificError::BadRequest(format!(
                    "staged attachment {sha} does not match its content address"
                )));
            }
        }

        let mut mime_stmt = conn
            .prepare("SELECT DISTINCT sha256, mime FROM attachments")
            .map_err(|e| LificError::BadRequest(format!("read staged attachment MIME: {e}")))?;
        let mime_rows = mime_stmt
            .query_map([], |row| {
                Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
            })
            .map_err(|e| LificError::BadRequest(format!("read staged attachment MIME: {e}")))?;
        for row in mime_rows {
            let (sha, declared_mime) = row
                .map_err(|e| LificError::BadRequest(format!("read staged attachment MIME: {e}")))?;
            let path = staging.join("attachments").join(&sha);
            // Streamed, not `std::fs::read`: under `--allow-large` a blob can
            // be far bigger than the bounded defaults, and confirming its
            // content type must not cost an attachment-sized allocation.
            let file = std::fs::File::open(&path).map_err(|e| {
                LificError::BadRequest(format!("read staged attachment {sha}: {e}"))
            })?;
            let detected_mime = crate::storage::sniff_and_validate_stream(
                std::io::BufReader::new(file),
                Some(&declared_mime),
            )?;
            if detected_mime != declared_mime {
                return Err(LificError::BadRequest(format!(
                    "staged attachment {sha} MIME {declared_mime} does not match its content ({detected_mime})"
                )));
            }
        }
    }

    let attachments_dir = staging.join("attachments");
    for entry in std::fs::read_dir(&attachments_dir)
        .map_err(|e| LificError::BadRequest(format!("read staged attachments: {e}")))?
    {
        let entry =
            entry.map_err(|e| LificError::BadRequest(format!("read staged attachment: {e}")))?;
        let path = entry.path();
        let metadata = std::fs::symlink_metadata(&path).map_err(|e| {
            LificError::BadRequest(format!("inspect staged attachment {}: {e}", path.display()))
        })?;
        if !metadata.is_file() {
            return Err(LificError::BadRequest(format!(
                "staged attachment is not a regular file: {}",
                path.display()
            )));
        }
        let name = entry.file_name().into_string().map_err(|_| {
            LificError::BadRequest(format!(
                "staged attachment has a non-UTF-8 name: {}",
                path.display()
            ))
        })?;
        if !crate::storage::valid_sha256(&name) {
            return Err(LificError::BadRequest(format!(
                "staged attachment has an invalid content address: {name}"
            )));
        }
        if metadata.len() > limits.max_attachment_bytes {
            return Err(LificError::BadRequest(format!(
                "staged attachment exceeds restore limit: {name}"
            )));
        }
        if hash_file(&path)? != name {
            return Err(LificError::BadRequest(format!(
                "staged attachment {name} does not match its content address"
            )));
        }
    }
    Ok(())
}

/// Read and validate an archive's manifest + entry list without extracting.
/// Returns the parsed manifest. Rejects archives missing `manifest.json` or
/// `lific.db`, and any attachment entry that fails [`validate_attachment_entry`].
pub fn inspect_archive(archive: &Path, limits: &RestoreLimits) -> Result<Manifest, LificError> {
    let manifest = read_manifest(archive, limits)?;
    validate_manifest_limits(&manifest, limits)?;

    // Second pass: validate every entry name (traversal guard) and require the
    // DB member is present.
    let file = std::fs::File::open(archive)
        .map_err(|e| LificError::BadRequest(format!("open archive: {e}")))?;
    let mut tar = bounded_archive(file, limits);
    let mut has_db = false;
    let mut db_bytes = None;
    let mut manifest_entries = 0u64;
    let mut attachment_names = std::collections::HashSet::new();
    let mut entry_count = 0u64;
    let mut attachment_count = 0u64;
    let mut attachment_bytes = 0u64;
    let mut payload_bytes = 0u64;
    for entry in tar
        .entries()
        .map_err(|e| LificError::BadRequest(format!("read archive entries: {e}")))?
        .raw(true)
    {
        let entry = entry.map_err(|e| LificError::BadRequest(format!("read entry: {e}")))?;
        validate_tar_entry_type(&entry)?;
        entry_count += 1;
        if entry_count > limits.max_entries + 2 {
            return Err(LificError::BadRequest(
                "archive has too many entries".into(),
            ));
        }
        let size = entry.size();
        let path = entry
            .path()
            .map_err(|e| LificError::BadRequest(format!("entry path: {e}")))?;
        let name = path.to_string_lossy().replace('\\', "/");
        if name == ARCHIVE_MANIFEST_NAME {
            manifest_entries += 1;
            if manifest_entries > 1 {
                return Err(LificError::BadRequest(
                    "archive contains duplicate manifests".into(),
                ));
            }
            continue;
        } else if name == ARCHIVE_DB_NAME {
            if has_db {
                return Err(LificError::BadRequest(
                    "archive contains duplicate databases".into(),
                ));
            }
            if size > limits.max_db_bytes {
                return Err(LificError::BadRequest(
                    "archive database exceeds restore limit".into(),
                ));
            }
            has_db = true;
            db_bytes = Some(size);
            payload_bytes = payload_bytes
                .checked_add(size)
                .ok_or_else(|| LificError::BadRequest("archive size overflow".into()))?;
        } else if name.starts_with(ARCHIVE_ATTACHMENTS_PREFIX) {
            validate_attachment_entry(&name)?;
            if !attachment_names.insert(name.clone()) {
                return Err(LificError::BadRequest(format!(
                    "archive contains duplicate attachment: {name}"
                )));
            }
            if size > limits.max_attachment_bytes {
                return Err(LificError::BadRequest(
                    "archive attachment exceeds restore limit".into(),
                ));
            }
            attachment_count += 1;
            attachment_bytes = attachment_bytes
                .checked_add(size)
                .ok_or_else(|| LificError::BadRequest("archive attachment size overflow".into()))?;
            payload_bytes = payload_bytes
                .checked_add(size)
                .ok_or_else(|| LificError::BadRequest("archive size overflow".into()))?;
            if attachment_count > limits.max_entries
                || attachment_bytes > manifest.attachment_bytes
                || payload_bytes > limits.max_total_bytes
            {
                return Err(LificError::BadRequest(
                    "archive attachment contents exceed manifest limits".into(),
                ));
            }
        } else {
            return Err(LificError::BadRequest(format!(
                "rejected unexpected archive entry: {name}"
            )));
        }
    }
    if !has_db {
        return Err(LificError::BadRequest("archive is missing lific.db".into()));
    }
    if db_bytes != Some(manifest.db_size_bytes) {
        return Err(LificError::BadRequest(
            "archive database size does not match manifest".into(),
        ));
    }
    if attachment_count != manifest.attachment_count
        || attachment_bytes != manifest.attachment_bytes
    {
        return Err(LificError::BadRequest(
            "archive attachment entries do not match manifest".into(),
        ));
    }
    Ok(manifest)
}

/// Read just the manifest from the archive (first matching entry).
fn read_manifest(archive: &Path, limits: &RestoreLimits) -> Result<Manifest, LificError> {
    let file = std::fs::File::open(archive)
        .map_err(|e| LificError::BadRequest(format!("open archive: {e}")))?;
    let mut tar = bounded_archive(file, limits);
    let mut entry_count = 0u64;
    for entry in tar
        .entries()
        .map_err(|e| LificError::BadRequest(format!("read archive entries: {e}")))?
        .raw(true)
    {
        let mut entry = entry.map_err(|e| LificError::BadRequest(format!("read entry: {e}")))?;
        validate_tar_entry_type(&entry)?;
        entry_count += 1;
        if entry_count > limits.max_entries + 2 {
            return Err(LificError::BadRequest(
                "archive has too many entries before its manifest".into(),
            ));
        }
        let path = entry
            .path()
            .map_err(|e| LificError::BadRequest(format!("entry path: {e}")))?;
        if path.to_string_lossy() == ARCHIVE_MANIFEST_NAME {
            let bytes = read_entry_bounded(&mut entry, limits.max_manifest_bytes, "manifest")?;
            let buf = String::from_utf8(bytes)
                .map_err(|e| LificError::BadRequest(format!("manifest is not UTF-8: {e}")))?;
            return serde_json::from_str(&buf)
                .map_err(|e| LificError::BadRequest(format!("parse manifest.json: {e}")));
        }
    }
    Err(LificError::BadRequest(
        "archive is missing manifest.json".into(),
    ))
}

/// Whether a hot WAL sidecar is present next to `db_path`, hinting the server
/// may still be running (best-effort — see command help).
fn wal_is_hot(db_path: &Path) -> bool {
    let wal = PathBuf::from(format!("{}-wal", db_path.display()));
    std::fs::metadata(&wal).is_ok_and(|m| m.len() > 0)
}

/// Run `lific restore`: validate the archive, then stage-extract it into the
/// data dir at `db_path`. Refuses to clobber an existing DB unless `force`;
/// with `force`, moves the existing DB + `-wal`/`-shm` aside. Refuses archives
/// created by a newer Lific (higher schema_version than this binary).
// The bounded-default entry point kept for callers with no options to express
// (the tests, and anything embedding a restore); `lific restore` itself goes
// through `run_restore_with` so it can pass `--allow-large`.
#[cfg_attr(not(test), allow(dead_code))]
pub fn run_restore(
    archive: &Path,
    db_path: &Path,
    force: bool,
) -> Result<RestoreResult, LificError> {
    run_restore_with(archive, db_path, &RestoreOptions::new(force, false))
}

/// [`run_restore`] with explicit options, including the size bounds applied to
/// the archive (`lific restore --allow-large` raises them).
pub fn run_restore_with(
    archive: &Path,
    db_path: &Path,
    options: &RestoreOptions,
) -> Result<RestoreResult, LificError> {
    let force = options.force;
    let limits = &options.limits;
    let manifest = inspect_archive(archive, limits)?;

    // Schema compatibility gate.
    let latest = crate::db::migrate::latest_version();
    if manifest.schema_version > latest {
        return Err(LificError::BadRequest(format!(
            "archive was created by a newer Lific (schema v{} > this binary's v{}); \
             upgrade Lific before restoring",
            manifest.schema_version, latest
        )));
    }

    let data_dir = db_path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .map_or_else(|| PathBuf::from("."), |p| p.to_path_buf());
    std::fs::create_dir_all(&data_dir)
        .map_err(|e| LificError::Internal(format!("create data dir: {e}")))?;

    let _restore_lock = RestoreLock::acquire(&data_dir)?;

    if db_path.exists() && !force {
        return Err(LificError::Conflict(format!(
            "{} already exists; pass --force to restore over it (stop the server first)",
            db_path.display()
        )));
    }

    // Stage and validate the complete restore before moving any live state.
    let staging = tempfile::Builder::new()
        .prefix(".lific-restore-")
        .tempdir_in(&data_dir)
        .map_err(|e| LificError::Internal(format!("create staging dir: {e}")))?;
    let staging_path = staging.path();
    std::fs::create_dir(staging_path.join("attachments"))
        .map_err(|e| LificError::Internal(format!("create staging dir: {e}")))?;
    set_owner_only_dir(staging_path)
        .map_err(|e| LificError::Internal(format!("secure restore staging dir: {e}")))?;
    set_owner_only_dir(&staging_path.join("attachments"))
        .map_err(|e| LificError::Internal(format!("secure restore attachments dir: {e}")))?;

    let extract = (|| -> Result<u64, LificError> {
        let file = std::fs::File::open(archive)
            .map_err(|e| LificError::BadRequest(format!("open archive: {e}")))?;
        let mut tar = bounded_archive(file, limits);
        let mut attachment_count = 0u64;
        let mut total_bytes = 0u64;
        for entry in tar
            .entries()
            .map_err(|e| LificError::BadRequest(format!("read archive entries: {e}")))?
            .raw(true)
        {
            let mut entry =
                entry.map_err(|e| LificError::BadRequest(format!("read entry: {e}")))?;
            validate_tar_entry_type(&entry)?;
            let epath = entry
                .path()
                .map_err(|e| LificError::BadRequest(format!("entry path: {e}")))?;
            let name = epath.to_string_lossy().replace('\\', "/");
            if name == ARCHIVE_MANIFEST_NAME {
                // Keep the manifest in staging for validation; return it to the
                // caller in RestoreResult after the install succeeds.
                let buf = read_entry_bounded(&mut entry, limits.max_manifest_bytes, "manifest")?;
                let mut output = create_staging_file(&staging_path.join(ARCHIVE_MANIFEST_NAME))?;
                output
                    .write_all(&buf)
                    .map_err(|e| LificError::Internal(format!("write manifest: {e}")))?;
                output
                    .sync_all()
                    .map_err(|e| LificError::Internal(format!("sync manifest: {e}")))?;
            } else if name == ARCHIVE_DB_NAME {
                let db = staging_path.join(ARCHIVE_DB_NAME);
                let mut output = create_staging_file(&db)?;
                copy_entry_bounded(
                    &mut entry,
                    &mut output,
                    limits.max_db_bytes,
                    &mut total_bytes,
                    limits.max_total_bytes,
                    "database",
                )?;
                set_owner_only(&db)
                    .map_err(|e| LificError::Internal(format!("chmod staged db: {e}")))?;
                output
                    .sync_all()
                    .map_err(|e| LificError::Internal(format!("sync staged db: {e}")))?;
            } else if name.starts_with(ARCHIVE_ATTACHMENTS_PREFIX) {
                let bare = validate_attachment_entry(&name)?;
                let path = staging_path.join("attachments").join(&bare);
                let mut output = create_staging_file(&path)?;
                copy_entry_bounded(
                    &mut entry,
                    &mut output,
                    limits.max_attachment_bytes,
                    &mut total_bytes,
                    limits.max_total_bytes,
                    "attachment",
                )?;
                set_owner_only(&path)
                    .map_err(|e| LificError::Internal(format!("chmod staged attachment: {e}")))?;
                output
                    .sync_all()
                    .map_err(|e| LificError::Internal(format!("sync staged attachment: {e}")))?;
                attachment_count += 1;
            } else {
                return Err(LificError::BadRequest(format!(
                    "rejected unexpected archive entry: {name}"
                )));
            }
        }
        if !staging_path.join(ARCHIVE_DB_NAME).exists() {
            return Err(LificError::BadRequest("archive is missing lific.db".into()));
        }
        validate_staged_database(staging_path, &manifest, limits)?;
        Ok(attachment_count)
    })();

    let attachment_count = match extract {
        Ok(n) => n,
        Err(e) => {
            return Err(e);
        }
    };

    // Recheck after staging in case another process created the destination
    // while the archive was being validated.
    if db_path.exists() && !force {
        return Err(LificError::Conflict(format!(
            "{} already exists; pass --force to restore over it (stop the server first)",
            db_path.display()
        )));
    }

    let restore_id = staging
        .path()
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("restore")
        .to_string();

    // The install phase swaps the live database and the whole attachments
    // directory. A dump, upload, delete or GC sweep crossing that swap would
    // be reading one half of the old data set and one half of the new one, so
    // it runs under the attachment store's lock like every other operation
    // that moves blobs. The lock file is a sibling of `attachments/`, not
    // inside it, so it is the same lock before and after the directory is
    // replaced. Store lock first, then the database work inside: the ordering
    // every other caller uses.
    let store = crate::storage::AttachmentStore::from_db_path(db_path);
    let moved_existing_to =
        store.with_lock(|_| install_restore(staging_path, db_path, &restore_id))?;

    Ok(RestoreResult {
        manifest,
        attachment_count,
        db_path: db_path.to_path_buf(),
        moved_existing_to,
    })
}

/// Move the validated staging tree into place, displacing any live state.
///
/// This is the only part of a restore that touches the user's data, and every
/// failure inside it must leave that data where the user expects to find it.
/// Once the live database has been renamed aside, *every* remaining error path
/// goes through [`fail_after_move`], which renames it back — including the
/// "an attachment backup with this name already exists" refusal, which used to
/// return straight to the caller and leave `db_path` missing entirely.
///
/// Returns where an existing database was moved, if one was.
fn install_restore(
    staging_path: &Path,
    db_path: &Path,
    restore_id: &str,
) -> Result<Option<PathBuf>, LificError> {
    let moved_existing_to = if db_path.exists() {
        checkpoint_db_file(db_path)?;
        let dest = PathBuf::from(format!("{}.pre-restore-{restore_id}", db_path.display()));
        if std::fs::symlink_metadata(&dest).is_ok() {
            return Err(LificError::Conflict(format!(
                "restore backup already exists: {}",
                dest.display()
            )));
        }
        if let Err(error) = std::fs::rename(db_path, &dest) {
            return Err(LificError::Internal(format!(
                "move existing db aside: {error}"
            )));
        }
        // From here on the live database is at `dest`, not `db_path`.
        for ext in ["-wal", "-shm"] {
            let side = PathBuf::from(format!("{}{ext}", db_path.display()));
            if let Err(error) = remove_file_if_present(&side) {
                let cause = LificError::Internal(format!("remove old {ext}: {error}"));
                return Err(rollback_moved_db(&dest, db_path, cause));
            }
        }
        Some(dest)
    } else {
        None
    };
    let moved = moved_existing_to.clone();
    let fail = move |cause: LificError| fail_after_move(moved.as_deref(), db_path, cause);

    // Move restored files into place as one recoverable transaction. Keep the
    // old attachment directory until both the DB and new directory are live so
    // a filesystem failure cannot leave mismatched metadata and blobs.
    let attachments_dest = attachments_dir_for(db_path);
    let attachments_backup = PathBuf::from(format!(
        "{}.pre-restore-{restore_id}",
        attachments_dest.display()
    ));
    let had_attachments = attachments_dest.exists();
    if had_attachments && std::fs::symlink_metadata(&attachments_backup).is_ok() {
        return Err(fail(LificError::Conflict(format!(
            "restore attachment backup already exists: {}",
            attachments_backup.display()
        ))));
    }
    if had_attachments && let Err(e) = std::fs::rename(&attachments_dest, &attachments_backup) {
        return Err(fail(LificError::Internal(format!(
            "move existing attachments aside: {e}"
        ))));
    }

    let install_result = (|| -> Result<(), LificError> {
        std::fs::rename(staging_path.join(ARCHIVE_DB_NAME), db_path)
            .map_err(|e| LificError::Internal(format!("install restored db: {e}")))?;
        set_owner_only(db_path)
            .map_err(|e| LificError::Internal(format!("chmod restored db: {e}")))?;
        std::fs::rename(staging_path.join("attachments"), &attachments_dest)
            .map_err(|e| LificError::Internal(format!("install restored attachments: {e}")))?;
        let data_dir = db_path
            .parent()
            .filter(|path| !path.as_os_str().is_empty())
            .unwrap_or_else(|| Path::new("."));
        sync_dir(data_dir)
            .map_err(|e| LificError::Internal(format!("sync restored data directory: {e}")))?;
        Ok(())
    })();

    if let Err(error) = install_result {
        return Err(rollback_install(
            error,
            staging_path,
            db_path,
            &attachments_dest,
            &attachments_backup,
            had_attachments,
            moved_existing_to.as_deref(),
        ));
    }

    if had_attachments {
        let _ = std::fs::remove_dir_all(&attachments_backup);
    }

    Ok(moved_existing_to)
}

/// Surface `cause`, first putting a moved-aside database back if there is one.
fn fail_after_move(moved: Option<&Path>, db_path: &Path, cause: LificError) -> LificError {
    match moved {
        Some(moved) => rollback_moved_db(moved, db_path, cause),
        None => cause,
    }
}

fn remove_file_if_present(path: &Path) -> std::io::Result<()> {
    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error),
    }
}

fn remove_dir_if_present(path: &Path) -> std::io::Result<()> {
    match std::fs::remove_dir_all(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error),
    }
}

fn rollback_install(
    cause: LificError,
    staging: &Path,
    db_path: &Path,
    attachments_dest: &Path,
    attachments_backup: &Path,
    had_attachments: bool,
    moved_existing_to: Option<&Path>,
) -> LificError {
    let mut failures = Vec::new();
    if let Err(error) = remove_file_if_present(db_path) {
        failures.push(format!("remove installed database: {error}"));
    }
    if let Err(error) = remove_dir_if_present(attachments_dest) {
        failures.push(format!("remove installed attachments: {error}"));
    }
    if had_attachments && let Err(error) = std::fs::rename(attachments_backup, attachments_dest) {
        failures.push(format!("restore previous attachments: {error}"));
    }
    if let Some(moved) = moved_existing_to
        && let Err(error) = std::fs::rename(moved, db_path)
    {
        failures.push(format!(
            "restore previous database from {}: {error}",
            moved.display()
        ));
    }
    if let Err(error) = remove_dir_if_present(staging) {
        failures.push(format!("remove staging directory: {error}"));
    }

    if failures.is_empty() {
        cause
    } else {
        LificError::Internal(format!(
            "restore failed ({cause}); rollback also failed: {}",
            failures.join("; ")
        ))
    }
}

/// Put the database that `--force` moved aside back at `db_path` after a failed
/// restore, and decide which error the caller should surface.
///
/// On success the original failure (`cause`) is returned unchanged. If the
/// rollback rename itself fails, swallowing it would tell the user only that
/// the restore failed while their database sits at a path they were never
/// shown, reading exactly like data loss. So that case returns a combined
/// error carrying both failures and the exact path the original
/// database still occupies, plus where to move it back to.
fn rollback_moved_db(moved: &Path, db_path: &Path, cause: LificError) -> LificError {
    match std::fs::rename(moved, db_path) {
        Ok(()) => cause,
        Err(rollback_err) => LificError::Internal(format!(
            "restore failed ({cause}), and rolling the previous database back to {} failed too \
             ({rollback_err}). Your original database was NOT deleted: it is still at {}. \
             Move it back to {} by hand to recover it.",
            db_path.display(),
            moved.display(),
            db_path.display()
        )),
    }
}

/// Checkpoint a database file's WAL into the main file, so the `.db` is
/// self-contained (used before moving an existing db aside under `--force`).
/// Failing closed is important: deleting the sidecars after a failed
/// checkpoint could discard committed WAL-backed data.
fn checkpoint_db_file(db_path: &Path) -> Result<(), LificError> {
    let conn = rusqlite::Connection::open(db_path)
        .map_err(|e| LificError::Internal(format!("open database for WAL checkpoint: {e}")))?;
    let busy: i64 = conn
        .query_row("PRAGMA wal_checkpoint(TRUNCATE)", [], |row| row.get(0))
        .map_err(|e| LificError::Internal(format!("checkpoint database WAL: {e}")))?;
    if busy != 0 {
        return Err(LificError::Conflict(
            "database WAL is busy; stop the server before forcing a restore".into(),
        ));
    }
    Ok(())
}

/// True when a hot WAL warns the server may be running. Exposed so the CLI can
/// print the best-effort warning documented in the command help.
pub fn server_maybe_running(db_path: &Path) -> bool {
    wal_is_hot(db_path)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    /// Scratch directory for one test. The guard removes it on Drop, which
    /// also runs while a failed assertion unwinds, so nothing is left for a
    /// later run to trip over.
    fn temp_dir(tag: &str) -> TempDir {
        tempfile::Builder::new()
            .prefix(&format!("lific_dump_{tag}_"))
            .tempdir()
            .unwrap()
    }

    /// Build a real on-disk DB with a seeded project, plus an attachments dir
    /// containing one real blob and one `.tmp` stray. Returns (dir guard,
    /// db_path).
    fn seed_data_dir(tag: &str) -> (TempDir, PathBuf) {
        let tmp = temp_dir(tag);
        let dir = tmp.path();
        let db_path = dir.join("lific.db");
        {
            let pool = crate::db::open(&db_path).unwrap();
            let conn = pool.write().unwrap();
            crate::db::queries::create_project(
                &conn,
                &crate::db::models::CreateProject {
                    name: "DumpTest".into(),
                    identifier: "DMP".into(),
                    description: String::new(),
                    emoji: None,
                    lead_user_id: None,
                },
            )
            .unwrap();
        }
        let att = dir.join("attachments");
        fs::create_dir_all(&att).unwrap();
        let first_sha = crate::storage::AttachmentStore::hash_bytes(b"blob one");
        let second_sha = crate::storage::AttachmentStore::hash_bytes(b"second blob bytes");
        fs::write(att.join(&first_sha), b"blob one").unwrap();
        fs::write(att.join(&second_sha), b"second blob bytes").unwrap();
        fs::write(att.join(format!("{second_sha}.tmp")), b"partial write").unwrap();
        (tmp, db_path)
    }

    /// List the entry names inside an archive.
    fn archive_entries(archive: &Path) -> Vec<String> {
        let file = fs::File::open(archive).unwrap();
        let dec = flate2::read::GzDecoder::new(file);
        let mut tar = tar::Archive::new(dec);
        tar.entries()
            .unwrap()
            .map(|e| {
                e.unwrap()
                    .path()
                    .unwrap()
                    .to_string_lossy()
                    .replace('\\', "/")
            })
            .collect()
    }

    #[test]
    fn dump_archive_contains_db_manifest_and_blobs_excluding_tmp() {
        let (dir_tmp, db_path) = seed_data_dir("contents");
        let dir = dir_tmp.path();
        let out = dir.join("out.tar.gz");
        let manifest = write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap();

        let entries = archive_entries(&out);
        assert!(entries.contains(&ARCHIVE_DB_NAME.to_string()));
        assert!(entries.contains(&ARCHIVE_MANIFEST_NAME.to_string()));
        assert!(entries.contains(&format!(
            "{ARCHIVE_ATTACHMENTS_PREFIX}{}",
            crate::storage::AttachmentStore::hash_bytes(b"blob one")
        )));
        assert!(entries.contains(&format!(
            "{ARCHIVE_ATTACHMENTS_PREFIX}{}",
            crate::storage::AttachmentStore::hash_bytes(b"second blob bytes")
        )));
        assert!(
            !entries.iter().any(|e| e.ends_with(".tmp")),
            "in-progress .tmp writes must be excluded: {entries:?}"
        );

        assert_eq!(manifest.attachment_count, 2);
        assert_eq!(
            manifest.attachment_bytes,
            (b"blob one".len() + b"second blob bytes".len()) as u64
        );
        assert_eq!(manifest.lific_version, env!("CARGO_PKG_VERSION"));
        assert_eq!(
            manifest.schema_version,
            crate::db::migrate::latest_version()
        );
        assert!(manifest.db_size_bytes > 0);
    }

    #[cfg(unix)]
    #[test]
    fn dump_archive_is_owner_only_0600() {
        use std::os::unix::fs::PermissionsExt;
        let (dir_tmp, db_path) = seed_data_dir("perms");
        let dir = dir_tmp.path();
        let out = dir.join("out.tar.gz");
        write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap();
        let mode = fs::metadata(&out).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o600, "archive must be chmod 0600");
    }

    /// Whether any dump staging directory is left in `dir`.
    fn has_dump_staging(dir: &Path) -> bool {
        fs::read_dir(dir).unwrap().any(|entry| {
            entry
                .unwrap()
                .file_name()
                .to_string_lossy()
                .starts_with(".lific-dump-")
        })
    }

    #[test]
    fn failed_dump_cleans_up_its_staging_files() {
        // LIF-329: a dump that cannot publish its archive must leave nothing
        // behind. Squatting the final path with a directory is a destination
        // no dump may write to, on every platform.
        let (dir_tmp, db_path) = seed_data_dir("errclean");
        let dir = dir_tmp.path();
        let out = dir.join("blocked.tar.gz");
        fs::create_dir_all(&out).unwrap();

        let result = write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out);
        assert!(result.is_err(), "a directory is not a dump destination");

        assert!(
            !has_dump_staging(dir),
            "the private staging directory must not survive a failed dump"
        );
        assert!(
            !out.with_extension("archive.tmp").exists(),
            "no loose staging file may be left beside the output"
        );
        assert!(!out.with_extension("dbsnapshot.tmp").exists());
    }

    #[test]
    fn successful_dump_leaves_no_staging_directory_behind() {
        let (dir_tmp, db_path) = seed_data_dir("staging_clean");
        let dir = dir_tmp.path();
        let out = dir.join("out.tar.gz");
        write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap();
        assert!(out.exists());
        assert!(
            !has_dump_staging(dir),
            "staging is removed on the happy path"
        );
    }

    #[cfg(unix)]
    #[test]
    fn dump_failing_after_staging_exists_still_cleans_up() {
        // A hard-linked blob fails the scan, which happens *after* the private
        // staging directory and its reserved files exist — the case a loose
        // `*.tmp` scheme used to strand for the backup sweep to find.
        let (dir_tmp, db_path) = seed_data_dir("errclean_late");
        let dir = dir_tmp.path();
        let outside = dir.join("outside");
        fs::write(&outside, b"outside").unwrap();
        fs::hard_link(&outside, dir.join("attachments").join("c".repeat(64))).unwrap();

        let out = dir.join("late.tar.gz");
        let error = write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap_err();

        assert!(error.to_string().contains("hard-linked"), "got {error}");
        assert!(!out.exists(), "a failed dump publishes nothing");
        assert!(
            !has_dump_staging(dir),
            "staging must not survive the failure"
        );
    }

    #[cfg(unix)]
    #[test]
    fn dump_staging_lock_is_visible_to_the_backup_sweep() {
        // `backup::sweep_stale_tmps` skips a staging directory whose lock is
        // held, so a slow dump is never swept out from under itself. A crashed
        // dump releases the lock with its process and becomes sweepable.
        let dir_tmp = temp_dir("staging_lock");
        let staging = dir_tmp.path().join(".lific-dump-probe");
        fs::create_dir(&staging).unwrap();
        assert!(
            !staging_is_locked(&staging),
            "an abandoned staging dir must be sweepable"
        );

        let lock = StagingLock::create(&staging.join("lock")).unwrap();
        assert!(
            staging_is_locked(&staging),
            "an in-flight dump must not be swept"
        );
        drop(lock);
        assert!(!staging_is_locked(&staging));
    }

    #[cfg(unix)]
    #[test]
    fn dump_rejects_unsafe_destinations() {
        use std::os::unix::fs::{PermissionsExt, symlink};

        let (dir_tmp, db_path) = seed_data_dir("unsafe_destinations");
        let dir = dir_tmp.path();
        let target = dir.join("target.tar.gz");
        fs::write(&target, b"existing").unwrap();

        // A symlink at the destination would redirect the whole database dump
        // to wherever it points.
        let link = dir.join("link.tar.gz");
        symlink(&target, &link).unwrap();
        assert!(write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &link).is_err());
        assert_eq!(fs::read(&target).unwrap(), b"existing");

        // Same for a hard link: the other name would see the dump's bytes.
        let hardlink = dir.join("hardlink.tar.gz");
        fs::hard_link(&target, &hardlink).unwrap();
        assert!(write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &hardlink).is_err());

        // And for a symlinked parent directory.
        let real_parent = dir.join("real-parent");
        fs::create_dir(&real_parent).unwrap();
        let parent_link = dir.join("parent-link");
        symlink(&real_parent, &parent_link).unwrap();
        let nested = parent_link.join("nested.tar.gz");
        assert!(write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &nested).is_err());

        // A world-writable parent without the sticky bit lets anyone swap the
        // name out from under the rename.
        let loose_parent = dir.join("loose-parent");
        fs::create_dir(&loose_parent).unwrap();
        fs::set_permissions(&loose_parent, fs::Permissions::from_mode(0o777)).unwrap();
        assert!(
            write_dump(
                &crate::db::open(&db_path).unwrap(),
                &db_path,
                &loose_parent.join("out.tar.gz")
            )
            .is_err()
        );

        // An ordinary private parent still works.
        let safe_parent = dir.join("safe-parent");
        fs::create_dir(&safe_parent).unwrap();
        fs::set_permissions(&safe_parent, fs::Permissions::from_mode(0o755)).unwrap();
        write_dump(
            &crate::db::open(&db_path).unwrap(),
            &db_path,
            &safe_parent.join("out.tar.gz"),
        )
        .expect("a private parent directory is a fine destination");
    }

    #[cfg(unix)]
    #[test]
    fn dump_skips_symlinked_blobs_and_refuses_hard_linked_ones() {
        use std::os::unix::fs::symlink;

        let (dir_tmp, db_path) = seed_data_dir("unsafe_attachments");
        let dir = dir_tmp.path();
        let attachments = dir.join("attachments");
        let name = "c".repeat(64);
        let entry = attachments.join(&name);
        let outside = dir.join("outside");
        fs::write(&outside, b"outside secret").unwrap();
        symlink(&outside, &entry).unwrap();

        // A symlinked store entry is not attachment data; archiving it would
        // copy a file from outside the store into the backup.
        let out = dir.join("symlink.tar.gz");
        let manifest = write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap();
        assert!(!archive_entries(&out).contains(&format!("attachments/{name}")));
        assert_eq!(manifest.attachment_count, 2, "only the real blobs count");

        // A hard link means the same bytes are reachable and mutable from
        // outside the store, so the dump refuses rather than guessing.
        fs::remove_file(&entry).unwrap();
        fs::hard_link(&outside, &entry).unwrap();
        let out = dir.join("hardlink.tar.gz");
        let error = write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap_err();
        assert!(error.to_string().contains("hard-linked"), "got {error}");
    }

    #[test]
    fn dump_refuses_a_blob_whose_bytes_do_not_match_its_name() {
        // The name of a blob IS its digest. Publishing an archive where that
        // is untrue would ship a backup every restore refuses, discovered only
        // on the day someone needs it.
        let (dir_tmp, db_path) = seed_data_dir("corrupt_blob");
        let dir = dir_tmp.path();
        let lying_name = crate::storage::AttachmentStore::hash_bytes(b"the honest bytes");
        fs::write(dir.join("attachments").join(&lying_name), b"tampered bytes").unwrap();

        let out = dir.join("corrupt.tar.gz");
        let error = write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap_err();

        assert!(
            error.to_string().contains("content address"),
            "the failure must name the mismatch: {error}"
        );
        assert!(!out.exists(), "a corrupt store must not publish an archive");
        assert!(!has_dump_staging(dir));
    }

    #[test]
    fn dump_hashes_every_blob_it_archives_and_still_round_trips() {
        // Positive control for the check above: honest blobs still dump and
        // restore, bytes intact.
        let (src_tmp, src_db) = seed_data_dir("hash_round_trip");
        let archive = src_tmp.path().join("backup.tar.gz");
        let manifest = write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &archive).unwrap();
        assert_eq!(manifest.attachment_count, 2);

        let dst = temp_dir("hash_round_trip_dst");
        let dst_db = dst.path().join(ARCHIVE_DB_NAME);
        run_restore(&archive, &dst_db, false).unwrap();
        let sha = crate::storage::AttachmentStore::hash_bytes(b"blob one");
        assert_eq!(
            fs::read(dst.path().join("attachments").join(&sha)).unwrap(),
            b"blob one"
        );
    }

    #[test]
    fn dump_holds_the_attachment_store_lock_for_its_whole_run() {
        // An upload, delete or GC sweep landing between the database snapshot
        // and the blob scan would archive a database referencing blobs the
        // archive does not contain. The dump takes the same store lock those
        // operations take, so it cannot interleave with them.
        use std::sync::mpsc::sync_channel;
        use std::time::Duration;

        let (dir_tmp, db_path) = seed_data_dir("dump_store_lock");
        let store = crate::storage::AttachmentStore::from_db_path(&db_path);
        let out = dir_tmp.path().join("locked.tar.gz");

        let (holding_tx, holding_rx) = sync_channel::<()>(1);
        let (release_tx, release_rx) = sync_channel::<()>(1);
        let holder = std::thread::spawn(move || {
            store.with_lock(|_| {
                holding_tx.send(()).unwrap();
                let _ = release_rx.recv_timeout(Duration::from_secs(10));
                Ok(())
            })
        });
        holding_rx.recv_timeout(Duration::from_secs(10)).unwrap();

        let (done_tx, done_rx) = sync_channel::<()>(1);
        let dumper = std::thread::spawn({
            let out = out.clone();
            move || {
                let result = write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out);
                done_tx.send(()).unwrap();
                result
            }
        });

        assert!(
            done_rx.recv_timeout(Duration::from_millis(250)).is_err(),
            "the dump must wait for the store lock instead of racing the store"
        );

        release_tx.send(()).unwrap();
        holder.join().unwrap().unwrap();
        done_rx
            .recv_timeout(Duration::from_secs(10))
            .expect("the dump proceeds once the store lock is free");
        dumper.join().unwrap().expect("dump succeeds");
        assert!(out.exists());
    }

    #[test]
    fn restore_install_waits_for_the_attachment_store_lock() {
        // The install phase swaps the attachments directory out from under the
        // whole instance. Anything holding the store (a dump, an upload, a GC
        // sweep) must finish first, or it reads half of each data set.
        use std::sync::mpsc::sync_channel;
        use std::time::Duration;

        let (src_tmp, src_db) = seed_data_dir("restore_store_lock_src");
        let archive = src_tmp.path().join("backup.tar.gz");
        write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &archive).unwrap();

        let dst = temp_dir("restore_store_lock_dst");
        let dst_db = dst.path().join(ARCHIVE_DB_NAME);
        let holder_store = crate::storage::AttachmentStore::from_db_path(&dst_db);
        let observer = crate::storage::AttachmentStore::from_db_path(&dst_db);

        let (entered_tx, entered_rx) = sync_channel::<()>(1);
        let (release_tx, release_rx) = sync_channel::<()>(1);
        let holder = std::thread::spawn(move || {
            holder_store.with_lock(|_| {
                entered_tx.send(()).unwrap();
                let _ = release_rx.recv_timeout(Duration::from_secs(10));
                Ok(())
            })
        });
        entered_rx.recv_timeout(Duration::from_secs(10)).unwrap();

        let (done_tx, done_rx) = sync_channel::<()>(1);
        let restorer = std::thread::spawn({
            let dst_db = dst_db.clone();
            move || {
                let result = run_restore(&archive, &dst_db, false);
                done_tx.send(()).unwrap();
                result
            }
        });

        assert!(
            done_rx.recv_timeout(Duration::from_millis(250)).is_err(),
            "the install must wait for the store lock rather than swap under it"
        );
        assert!(
            !dst_db.exists(),
            "nothing is installed while the store is held"
        );

        release_tx.send(()).unwrap();
        holder.join().unwrap().unwrap();
        done_rx
            .recv_timeout(Duration::from_secs(10))
            .expect("the restore proceeds once the store lock is free");
        let result = restorer.join().unwrap().expect("restore succeeds");
        assert_eq!(result.attachment_count, 2);

        // The lock is a sibling of `attachments/`, so replacing that directory
        // did not replace the lock: it still coordinates afterwards.
        assert!(observer.lock_path().is_file());
        assert!(!observer.lock_is_held());
        let after = crate::storage::AttachmentStore::from_db_path(&dst_db);
        let (held_tx, held_rx) = sync_channel::<()>(1);
        let (drop_tx, drop_rx) = sync_channel::<()>(1);
        let second = std::thread::spawn(move || {
            after.with_lock(|_| {
                held_tx.send(()).unwrap();
                let _ = drop_rx.recv_timeout(Duration::from_secs(10));
                Ok(())
            })
        });
        held_rx.recv_timeout(Duration::from_secs(10)).unwrap();
        assert!(
            observer.lock_is_held(),
            "the same lock still serializes work after the attachments swap"
        );
        drop_tx.send(()).unwrap();
        second.join().unwrap().unwrap();
    }

    #[test]
    fn dump_skips_store_entries_that_are_not_content_addressed() {
        let (dir_tmp, db_path) = seed_data_dir("bad_blob_names");
        let dir = dir_tmp.path();
        fs::write(dir.join("attachments").join("not-a-hash"), b"junk").unwrap();
        fs::create_dir(dir.join("attachments").join("nested")).unwrap();

        let out = dir.join("out.tar.gz");
        let manifest = write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap();

        assert_eq!(manifest.attachment_count, 2);
        assert!(
            !archive_entries(&out)
                .iter()
                .any(|entry| entry.contains("not-a-hash") || entry.contains("nested")),
            "only bare sha256 blobs belong in an archive"
        );
        // The archive still round-trips through the full validator.
        inspect_archive(&out, &RestoreLimits::default()).unwrap();
    }

    #[test]
    fn dumped_db_is_openable_sqlite_with_seeded_data() {
        let (dir_tmp, db_path) = seed_data_dir("snapshot");
        let dir = dir_tmp.path();
        let out = dir.join("snap.tar.gz");
        write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap();

        // Extract the db member and open it.
        let extract_dir = dir.join("extracted");
        fs::create_dir_all(&extract_dir).unwrap();
        let file = fs::File::open(&out).unwrap();
        let dec = flate2::read::GzDecoder::new(file);
        let mut tar = tar::Archive::new(dec);
        for entry in tar.entries().unwrap() {
            let mut entry = entry.unwrap();
            if entry.path().unwrap().to_string_lossy() == ARCHIVE_DB_NAME {
                let mut buf = Vec::new();
                std::io::Read::read_to_end(&mut entry, &mut buf).unwrap();
                fs::write(extract_dir.join("lific.db"), &buf).unwrap();
            }
        }
        let conn = rusqlite::Connection::open(extract_dir.join("lific.db")).unwrap();
        let name: String = conn
            .query_row(
                "SELECT name FROM projects WHERE identifier = 'DMP'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(name, "DumpTest");
    }

    #[test]
    fn run_dump_into_directory_uses_default_filename() {
        let (dir_tmp, db_path) = seed_data_dir("outdir");
        let dir = dir_tmp.path();
        let target = dir.join("dumps");
        fs::create_dir_all(&target).unwrap();
        let res = run_dump(&db_path, Some(&target)).unwrap();
        assert_eq!(res.archive_path.parent().unwrap(), target);
        let fname = res.archive_path.file_name().unwrap().to_string_lossy();
        assert!(fname.starts_with("lific_"));
        assert!(fname.ends_with(".tar.gz"));
        assert!(res.archive_path.exists());
    }

    #[test]
    fn restore_round_trip_matches_entities_and_blob_bytes() {
        let (src_dir_tmp, src_db) = seed_data_dir("rt_src");
        let src_dir = src_dir_tmp.path();
        let out = src_dir.join("backup.tar.gz");
        write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &out).unwrap();

        // Fresh, empty destination dir.
        let dst_dir_tmp = temp_dir("rt_dst");
        let dst_dir = dst_dir_tmp.path();
        let dst_db = dst_dir.join("lific.db");
        let res = run_restore(&out, &dst_db, false).unwrap();
        assert_eq!(res.attachment_count, 2);

        // Entities: the seeded project is present.
        let pool = crate::db::open(&dst_db).unwrap();
        let conn = pool.read().unwrap();
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM projects WHERE identifier = 'DMP'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(count, 1);

        // Blob bytes identical.
        assert_eq!(
            fs::read(
                dst_dir
                    .join("attachments")
                    .join(crate::storage::AttachmentStore::hash_bytes(b"blob one"),)
            )
            .unwrap(),
            b"blob one"
        );
        assert_eq!(
            fs::read(dst_dir.join("attachments").join(
                crate::storage::AttachmentStore::hash_bytes(b"second blob bytes"),
            ))
            .unwrap(),
            b"second blob bytes"
        );
    }

    #[test]
    fn restore_refuses_existing_db_without_force() {
        let (src_dir_tmp, src_db) = seed_data_dir("guard_src");
        let src_dir = src_dir_tmp.path();
        let out = src_dir.join("b.tar.gz");
        write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &out).unwrap();

        // Destination already has a db.
        let dst_dir_tmp = temp_dir("guard_dst");
        let dst_dir = dst_dir_tmp.path();
        let dst_db = dst_dir.join("lific.db");
        let _ = crate::db::open(&dst_db).unwrap();

        let err = run_restore(&out, &dst_db, false).unwrap_err();
        assert!(matches!(err, LificError::Conflict(_)), "got {err:?}");
    }

    #[cfg(unix)]
    #[test]
    fn restore_force_moves_existing_db_aside() {
        let (src_dir_tmp, src_db) = seed_data_dir("force_src");
        let src_dir = src_dir_tmp.path();
        let out = src_dir.join("b.tar.gz");
        write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &out).unwrap();

        let dst_dir_tmp = temp_dir("force_dst");
        let dst_dir = dst_dir_tmp.path();
        let dst_db = dst_dir.join("lific.db");
        // Seed a DIFFERENT project so we can tell the old db apart.
        {
            let pool = crate::db::open(&dst_db).unwrap();
            let conn = pool.write().unwrap();
            crate::db::queries::create_project(
                &conn,
                &crate::db::models::CreateProject {
                    name: "OldData".into(),
                    identifier: "OLD".into(),
                    description: String::new(),
                    emoji: None,
                    lead_user_id: None,
                },
            )
            .unwrap();
        }

        let res = run_restore(&out, &dst_db, true).unwrap();
        let moved = res
            .moved_existing_to
            .expect("existing db should be moved aside");
        assert!(moved.exists(), "moved-aside db must still exist");
        assert!(
            moved
                .file_name()
                .unwrap()
                .to_string_lossy()
                .contains("pre-restore-"),
            "moved db name must include pre-restore-: {}",
            moved.display()
        );
        // The moved-aside db still has the OLD project.
        let conn = rusqlite::Connection::open(&moved).unwrap();
        let old: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM projects WHERE identifier='OLD'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(old, 1);
        // The live db now has the restored project.
        let pool = crate::db::open(&dst_db).unwrap();
        let conn = pool.read().unwrap();
        let dmp: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM projects WHERE identifier='DMP'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(dmp, 1);
    }

    #[test]
    fn force_restore_refuses_an_uncheckpointable_database() {
        let (src_dir_tmp, src_db) = seed_data_dir("checkpoint_src");
        let archive = src_dir_tmp.path().join("backup.tar.gz");
        write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &archive).unwrap();

        let destination = temp_dir("checkpoint_dst");
        let db_path = destination.path().join(ARCHIVE_DB_NAME);
        fs::create_dir(&db_path).unwrap();
        let sentinel = db_path.join("must-survive");
        fs::write(&sentinel, b"original state").unwrap();

        let error = run_restore(&archive, &db_path, true).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("open database for WAL checkpoint")
        );
        assert!(db_path.is_dir());
        assert_eq!(fs::read(sentinel).unwrap(), b"original state");
    }

    #[test]
    fn restore_refuses_newer_schema_version() {
        let (src_dir_tmp, src_db) = seed_data_dir("newer_src");
        let src_dir = src_dir_tmp.path();
        let out = src_dir.join("b.tar.gz");
        write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &out).unwrap();

        // Rewrite the archive with a bumped schema_version to simulate a dump
        // from a newer Lific.
        let bumped = src_dir.join("bumped.tar.gz");
        rewrite_archive_with_schema(&out, &bumped, crate::db::migrate::latest_version() + 5);

        let dst_dir_tmp = temp_dir("newer_dst");
        let dst_dir = dst_dir_tmp.path();
        let dst_db = dst_dir.join("lific.db");
        let err = run_restore(&bumped, &dst_db, false).unwrap_err();
        assert!(matches!(err, LificError::BadRequest(_)), "got {err:?}");
        assert!(
            !dst_db.exists(),
            "nothing should be restored on schema refusal"
        );
    }

    #[test]
    fn restore_accepts_pre_attachment_schema_without_attachment_table() {
        let dir_tmp = temp_dir("legacy_schema");
        let dir = dir_tmp.path();
        let legacy_db = dir.join("legacy.db");
        {
            let conn = rusqlite::Connection::open(&legacy_db).unwrap();
            conn.execute_batch(
                "CREATE TABLE _migrations (version INTEGER PRIMARY KEY);
                 INSERT INTO _migrations (version) VALUES (30);",
            )
            .unwrap();
        }

        let archive = dir.join("legacy.tar.gz");
        let manifest = Manifest {
            lific_version: "old".into(),
            schema_version: 30,
            created_at: "now".into(),
            db_size_bytes: fs::metadata(&legacy_db).unwrap().len(),
            attachment_count: 0,
            attachment_bytes: 0,
        };
        let file = fs::File::create(&archive).unwrap();
        let encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default());
        let mut builder = tar::Builder::new(encoder);
        append_bytes(
            &mut builder,
            ARCHIVE_MANIFEST_NAME,
            &serde_json::to_vec(&manifest).unwrap(),
        )
        .unwrap();
        builder
            .append_path_with_name(&legacy_db, ARCHIVE_DB_NAME)
            .unwrap();
        builder.into_inner().unwrap().finish().unwrap();

        let destination = temp_dir("legacy_schema_dst");
        let db_path = destination.path().join(ARCHIVE_DB_NAME);
        run_restore(&archive, &db_path, false).expect("older empty backups remain restorable");
        assert!(db_path.exists());
        assert!(
            !destination
                .path()
                .join("attachments")
                .join("unexpected")
                .exists()
        );
    }

    #[test]
    fn staged_database_accepts_pre_integrity_attachment_schema() {
        let dir_tmp = temp_dir("legacy_attachments");
        let dir = dir_tmp.path();
        let db = dir.join(ARCHIVE_DB_NAME);
        let sha = crate::storage::AttachmentStore::hash_bytes(b"legacy");
        {
            let conn = rusqlite::Connection::open(&db).unwrap();
            conn.execute_batch(
                "CREATE TABLE _migrations (version INTEGER PRIMARY KEY);
                 INSERT INTO _migrations (version) VALUES (42);
                 CREATE TABLE attachments (
                     sha256 TEXT NOT NULL,
                     filename TEXT NOT NULL,
                     mime TEXT NOT NULL,
                     size_bytes INTEGER NOT NULL
                 );",
            )
            .unwrap();
            conn.execute(
                "INSERT INTO attachments (sha256, filename, mime, size_bytes)
                 VALUES (?1, 'legacy.txt', 'text/plain', 6)",
                rusqlite::params![sha],
            )
            .unwrap();
        }

        let staging = dir.join("staging");
        fs::create_dir_all(staging.join("attachments")).unwrap();
        fs::copy(&db, staging.join(ARCHIVE_DB_NAME)).unwrap();
        fs::write(staging.join("attachments").join(&sha), b"legacy").unwrap();
        let manifest = Manifest {
            lific_version: "old".into(),
            schema_version: 42,
            created_at: "now".into(),
            db_size_bytes: fs::metadata(&db).unwrap().len(),
            attachment_count: 1,
            attachment_bytes: 6,
        };

        validate_staged_database(&staging, &manifest, &RestoreLimits::default())
            .expect("pre-integrity archives remain restorable");
    }

    #[test]
    fn restore_rejects_path_traversal_entry() {
        let dir_tmp = temp_dir("traversal");
        let dir = dir_tmp.path();
        let archive = dir.join("evil.tar.gz");
        // Craft an archive with a manifest, a db, and a malicious attachment
        // entry that tries to escape the attachments dir.
        {
            let file = fs::File::create(&archive).unwrap();
            let enc = flate2::write::GzEncoder::new(file, flate2::Compression::default());
            let mut tar = tar::Builder::new(enc);
            let manifest = Manifest {
                lific_version: "x".into(),
                schema_version: 1,
                created_at: "now".into(),
                db_size_bytes: 1,
                attachment_count: 1,
                attachment_bytes: 1,
            };
            let mj = serde_json::to_vec(&manifest).unwrap();
            append_bytes(&mut tar, ARCHIVE_MANIFEST_NAME, &mj).unwrap();
            append_bytes(&mut tar, ARCHIVE_DB_NAME, b"not a real db").unwrap();
            // A nested path under attachments/ — tar permits writing it, but a
            // blob name must be a bare hash, so the validator must reject it
            // (this is the class of entry a path-traversal payload uses).
            append_bytes(&mut tar, "attachments/sub/escape", b"pwned").unwrap();
            let enc = tar.into_inner().unwrap();
            enc.finish().unwrap();
        }

        // inspect_archive must reject it.
        let err = inspect_archive(&archive, &RestoreLimits::default()).unwrap_err();
        assert!(matches!(err, LificError::BadRequest(_)), "got {err:?}");

        // And a full restore attempt must also refuse, leaving no db behind.
        let dst_dir_tmp = temp_dir("traversal_dst");
        let dst_dir = dst_dir_tmp.path();
        let dst_db = dst_dir.join("lific.db");
        assert!(run_restore(&archive, &dst_db, false).is_err());
        assert!(!dst_db.exists());
        assert!(
            !dst_dir.join("attachments").join("sub").exists(),
            "traversal entry must not write nested dirs"
        );
    }

    #[cfg(unix)]
    #[test]
    fn restore_mid_extract_failure_leaves_original_untouched() {
        // A corrupt archive (valid manifest+db header claim, truncated body)
        // must fail extraction WITHOUT clobbering the pre-existing db when
        // --force moved it aside — the rollback restores it.
        let (src_dir_tmp, src_db) = seed_data_dir("midfail_src");
        let src_dir = src_dir_tmp.path();
        let good = src_dir.join("good.tar.gz");
        write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &good).unwrap();

        // Truncate the good archive to corrupt it mid-stream.
        let corrupt = src_dir.join("corrupt.tar.gz");
        let bytes = fs::read(&good).unwrap();
        fs::write(&corrupt, &bytes[..bytes.len() / 2]).unwrap();

        let dst_dir_tmp = temp_dir("midfail_dst");
        let dst_dir = dst_dir_tmp.path();
        let dst_db = dst_dir.join("lific.db");
        {
            let pool = crate::db::open(&dst_db).unwrap();
            let conn = pool.write().unwrap();
            crate::db::queries::create_project(
                &conn,
                &crate::db::models::CreateProject {
                    name: "Original".into(),
                    identifier: "ORG".into(),
                    description: String::new(),
                    emoji: None,
                    lead_user_id: None,
                },
            )
            .unwrap();
        }
        // Force restore of the corrupt archive should error but roll back.
        let result = run_restore(&corrupt, &dst_db, true);
        assert!(result.is_err(), "corrupt archive must fail");
        // Original db must still be present and openable with its project.
        assert!(dst_db.exists(), "original db must be restored on rollback");
        let pool = crate::db::open(&dst_db).unwrap();
        let conn = pool.read().unwrap();
        let org: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM projects WHERE identifier='ORG'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(org, 1, "original data must survive a failed restore");
    }

    #[test]
    fn failed_rollback_error_names_both_failures_and_the_surviving_db_path() {
        // LIF-371: when the moved-aside db cannot be renamed back, the user
        // must be told the restore failed AND where their database still is.
        let dir_tmp = temp_dir("rollback_fail");
        let dir = dir_tmp.path();
        let db_path = dir.join("lific.db");
        // A source that does not exist makes the rollback rename fail.
        let moved = dir.join("lific.db.pre-restore-gone");

        let err = rollback_moved_db(
            &moved,
            &db_path,
            LificError::BadRequest("read db from archive: unexpected eof".into()),
        );
        let msg = err.to_string();

        assert!(matches!(err, LificError::Internal(_)), "got {err:?}");
        assert!(
            msg.contains("read db from archive: unexpected eof"),
            "must carry the original failure: {msg}"
        );
        assert!(
            msg.contains(&moved.display().to_string()),
            "must name the path the original db still lives at: {msg}"
        );
        assert!(
            msg.contains(&db_path.display().to_string()),
            "must name where to move it back to: {msg}"
        );
    }

    #[test]
    fn successful_rollback_surfaces_the_original_error_unchanged() {
        let dir_tmp = temp_dir("rollback_ok");
        let dir = dir_tmp.path();
        let db_path = dir.join("lific.db");
        let moved = dir.join("lific.db.pre-restore-1");
        fs::write(&moved, b"original db bytes").unwrap();

        let err = rollback_moved_db(
            &moved,
            &db_path,
            LificError::BadRequest("archive is missing lific.db".into()),
        );

        assert!(matches!(err, LificError::BadRequest(ref m) if m == "archive is missing lific.db"));
        assert_eq!(fs::read(&db_path).unwrap(), b"original db bytes");
        assert!(!moved.exists());
    }

    #[test]
    fn validate_attachment_entry_accepts_bare_hash_rejects_traversal() {
        assert!(
            validate_attachment_entry(
                "attachments/0000000000000000000000000000000000000000000000000000000000000001"
            )
            .is_ok()
        );
        assert!(validate_attachment_entry("attachments/../etc/passwd").is_err());
        assert!(validate_attachment_entry("attachments/sub/dir").is_err());
        assert!(validate_attachment_entry("attachments/").is_err());
        assert!(validate_attachment_entry("notattachments/x").is_err());
        assert!(validate_attachment_entry("attachments/.hidden").is_err());
    }

    #[test]
    fn inspect_rejects_manifest_size_bomb() {
        let (dir_tmp, db_path) = seed_data_dir("manifest_limit");
        let dir = dir_tmp.path();
        let out = dir.join("source.tar.gz");
        write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &out).unwrap();
        let mut manifest = read_manifest(&out, &RestoreLimits::default()).unwrap();
        manifest.attachment_bytes = MAX_TOTAL_RESTORE_BYTES;
        let rewritten = dir.join("oversized.tar.gz");
        rewrite_archive_manifest(&out, &rewritten, &manifest);
        let error = inspect_archive(&rewritten, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("total restore size"));
    }

    #[test]
    fn compressed_archive_expansion_is_bounded() {
        const TEST_LIMIT: u64 = 1024 * 1024;
        let dir_tmp = temp_dir("compressed_expansion");
        let archive = dir_tmp.path().join("bomb.tar.gz");
        let expanded_bytes = vec![b'x'; (TEST_LIMIT * 2) as usize];
        let file = fs::File::create(&archive).unwrap();
        let encoder = flate2::write::GzEncoder::new(file, flate2::Compression::best());
        let mut builder = tar::Builder::new(encoder);
        append_bytes(&mut builder, "payload", &expanded_bytes).unwrap();
        builder.into_inner().unwrap().finish().unwrap();

        assert!(
            fs::metadata(&archive).unwrap().len() < expanded_bytes.len() as u64 / 100,
            "the fixture must be much smaller while expanding past the reader limit"
        );
        let file = fs::File::open(&archive).unwrap();
        let mut archive = bounded_archive_with_limit(file, TEST_LIMIT);
        let mut entries = archive.entries().unwrap().raw(true);
        let mut entry = entries.next().unwrap().unwrap();
        let mut total_bytes = 0;
        let error = copy_entry_bounded(
            &mut entry,
            &mut std::io::sink(),
            MAX_ATTACHMENT_BYTES,
            &mut total_bytes,
            MAX_TOTAL_RESTORE_BYTES,
            "payload",
        )
        .unwrap_err();
        assert!(error.to_string().contains("expected 2097152 bytes"));
    }

    #[test]
    fn inspect_rejects_tar_extension_entries_before_reading_their_body() {
        let dir_tmp = temp_dir("tar_extension");
        let archive = dir_tmp.path().join("extension.tar.gz");
        let file = fs::File::create(&archive).unwrap();
        let encoder = flate2::write::GzEncoder::new(file, flate2::Compression::best());
        let mut builder = tar::Builder::new(encoder);
        let extension = vec![b'x'; MAX_MANIFEST_BYTES as usize + 1];
        let mut header = tar::Header::new_gnu();
        header.set_entry_type(tar::EntryType::XHeader);
        header.set_size(extension.len() as u64);
        header.set_mode(0o600);
        header.set_cksum();
        builder
            .append_data(&mut header, "pax", extension.as_slice())
            .unwrap();
        builder.into_inner().unwrap().finish().unwrap();

        let error = inspect_archive(&archive, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("unsupported tar entry type"));
    }

    #[test]
    fn manifest_scan_rejects_excessive_entries_before_manifest() {
        let dir_tmp = temp_dir("manifest_entries");
        let archive = dir_tmp.path().join("too-many.tar.gz");
        let file = fs::File::create(&archive).unwrap();
        let enc = flate2::write::GzEncoder::new(file, flate2::Compression::default());
        let mut builder = tar::Builder::new(enc);
        for index in 0..(MAX_RESTORE_ENTRIES + 3) {
            append_bytes(&mut builder, &format!("ignored-{index}"), &[0]).unwrap();
        }
        let encoder = builder.into_inner().unwrap();
        encoder.finish().unwrap();
        let error = inspect_archive(&archive, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("before its manifest"));
    }

    #[test]
    fn inspect_rejects_duplicate_control_entries() {
        let dir_tmp = temp_dir("duplicate_entries");
        let archive = dir_tmp.path().join("duplicate.tar.gz");
        let file = fs::File::create(&archive).unwrap();
        let enc = flate2::write::GzEncoder::new(file, flate2::Compression::default());
        let mut builder = tar::Builder::new(enc);
        let manifest = Manifest {
            lific_version: env!("CARGO_PKG_VERSION").into(),
            schema_version: 1,
            created_at: String::new(),
            db_size_bytes: 1,
            attachment_count: 0,
            attachment_bytes: 0,
        };
        let bytes = serde_json::to_vec(&manifest).unwrap();
        append_bytes(&mut builder, ARCHIVE_MANIFEST_NAME, &bytes).unwrap();
        append_bytes(&mut builder, ARCHIVE_MANIFEST_NAME, &bytes).unwrap();
        let encoder = builder.into_inner().unwrap();
        encoder.finish().unwrap();
        let error = inspect_archive(&archive, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("duplicate manifest"));
    }

    #[test]
    fn restore_rejects_manifest_schema_that_does_not_match_database() {
        let (dir_tmp, db_path) = seed_data_dir("schema_mismatch");
        let archive = dir_tmp.path().join("source.tar.gz");
        write_dump(&crate::db::open(&db_path).unwrap(), &db_path, &archive).unwrap();
        let rewritten = dir_tmp.path().join("mismatch.tar.gz");
        rewrite_archive_with_schema(
            &archive,
            &rewritten,
            crate::db::migrate::latest_version() - 1,
        );

        let destination = temp_dir("schema_mismatch_dst");
        let error =
            run_restore(&rewritten, &destination.path().join(ARCHIVE_DB_NAME), false).unwrap_err();
        assert!(error.to_string().contains("does not match manifest"));
    }

    #[test]
    fn staged_database_rejects_unconstrained_attachment_schema() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        conn.execute_batch(
            "CREATE TABLE attachments (
                sha256 TEXT NOT NULL,
                filename TEXT NOT NULL,
                mime TEXT NOT NULL /* mime TEXT NOT NULL CHECK (mime IN ( */,
                size_bytes INTEGER NOT NULL /* size_bytes INTEGER NOT NULL CHECK (size_bytes >= 0) */
                /* CHECK (length(sha256) = 64 AND sha256 NOT GLOB '*[^0-9a-f]*') */
            )",
        )
        .unwrap();

        let error = validate_attachment_schema(&conn).unwrap_err();
        assert!(error.to_string().contains("integrity constraints"));
    }

    #[test]
    fn staged_schema_probe_does_not_trust_source_triggers() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        conn.execute_batch(
            "CREATE TABLE attachments (
                sha256 TEXT NOT NULL,
                filename TEXT NOT NULL,
                mime TEXT NOT NULL,
                size_bytes INTEGER NOT NULL
            );
            CREATE TRIGGER reject_probe_values BEFORE INSERT ON attachments
            WHEN NEW.sha256 = '../outside'
              OR NEW.mime = 'application/octet-stream'
              OR NEW.size_bytes < 0
            BEGIN
                SELECT RAISE(ABORT, 'probe value rejected');
            END;",
        )
        .unwrap();

        let error = validate_attachment_schema(&conn).unwrap_err();
        assert!(error.to_string().contains("integrity constraints"));
    }

    #[test]
    fn restore_lock_serializes_restores() {
        let dir = temp_dir("restore_lock");
        let first = RestoreLock::acquire(dir.path()).unwrap();
        assert!(matches!(
            RestoreLock::acquire(dir.path()),
            Err(LificError::Conflict(_))
        ));
        drop(first);
        RestoreLock::acquire(dir.path()).expect("lock is released after restore");
    }

    #[test]
    fn a_lock_file_left_by_a_crashed_restore_is_immediately_reusable() {
        // The lock is advisory and lives in the kernel, so a process that dies
        // mid-restore releases it. All it leaves on disk is the file itself,
        // and that must not wedge the data directory: owning a *directory*
        // instead meant a crash locked the user out until they found and
        // deleted the leftover by hand.
        let dir = temp_dir("restore_lock_stale");
        let path = dir.path().join(".lific-restore.lock");
        fs::write(&path, b"").unwrap();

        let lock = RestoreLock::acquire(dir.path())
            .expect("a stale lock file must not block the next restore");
        drop(lock);

        assert!(path.is_file(), "the lock file is reused, not unlinked");
        RestoreLock::acquire(dir.path()).expect("still reusable after release");
    }

    #[test]
    fn restore_refuses_to_start_while_another_restore_holds_the_lock() {
        let (src_dir_tmp, src_db) = seed_data_dir("lock_busy_src");
        let archive = src_dir_tmp.path().join("backup.tar.gz");
        write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &archive).unwrap();

        let dst = temp_dir("lock_busy_dst");
        let dst_db = dst.path().join(ARCHIVE_DB_NAME);
        let held = RestoreLock::acquire(dst.path()).unwrap();

        let error = run_restore(&archive, &dst_db, false).unwrap_err();
        assert!(matches!(error, LificError::Conflict(_)), "got {error:?}");
        assert!(error.to_string().contains("another restore"));
        assert!(!dst_db.exists(), "a rejected restore installs nothing");

        drop(held);
        run_restore(&archive, &dst_db, false).expect("restore proceeds once the lock is free");
    }

    /// A live data dir with a seeded database and one attachment blob, plus a
    /// validated staging tree ready to be installed over it. Returns the guard,
    /// the data dir, the db path, the staging path and the blob's sha.
    fn seed_install_fixture(tag: &str) -> (TempDir, PathBuf, PathBuf, String) {
        let dir_tmp = temp_dir(tag);
        let root = dir_tmp.path().to_path_buf();
        let db_path = root.join(ARCHIVE_DB_NAME);
        {
            let pool = crate::db::open(&db_path).unwrap();
            let conn = pool.write().unwrap();
            crate::db::queries::create_project(
                &conn,
                &crate::db::models::CreateProject {
                    name: "LiveData".into(),
                    identifier: "LIV".into(),
                    description: String::new(),
                    emoji: None,
                    lead_user_id: None,
                },
            )
            .unwrap();
        }
        let sha = crate::storage::AttachmentStore::hash_bytes(b"live blob");
        fs::create_dir(root.join("attachments")).unwrap();
        fs::write(root.join("attachments").join(&sha), b"live blob").unwrap();

        let staging = root.join("staging");
        fs::create_dir_all(staging.join("attachments")).unwrap();
        fs::write(staging.join(ARCHIVE_DB_NAME), b"restored database bytes").unwrap();
        (dir_tmp, db_path, staging, sha)
    }

    #[test]
    fn install_rolls_back_the_moved_db_when_the_attachment_backup_path_is_taken() {
        // The refusal happens *after* the live database has been renamed
        // aside. Returning it straight to the caller left the user with no
        // database at db_path at all, which reads exactly like data loss.
        let (dir_tmp, db_path, staging, sha) = seed_install_fixture("install_backup_clash");
        let root = dir_tmp.path();
        fs::create_dir(root.join("attachments.pre-restore-clash")).unwrap();

        let error = install_restore(&staging, &db_path, "clash").unwrap_err();

        assert!(matches!(error, LificError::Conflict(_)), "got {error:?}");
        assert!(
            error
                .to_string()
                .contains("attachment backup already exists"),
            "the original cause must survive the rollback: {error}"
        );
        assert!(
            db_path.exists(),
            "the live database must be rolled back into place"
        );
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        let live: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM projects WHERE identifier = 'LIV'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(
            live, 1,
            "and it must be the user's database, not the dump's"
        );
        assert!(
            !root
                .join(format!("{}.pre-restore-clash", ARCHIVE_DB_NAME))
                .exists(),
            "nothing may be stranded under the pre-restore name"
        );
        assert_eq!(
            fs::read(root.join("attachments").join(&sha)).unwrap(),
            b"live blob",
            "the live attachments dir is untouched"
        );
    }

    #[test]
    fn install_rolls_back_the_moved_db_when_the_db_backup_path_is_taken() {
        let (dir_tmp, db_path, staging, _sha) = seed_install_fixture("install_db_clash");
        let root = dir_tmp.path();
        fs::write(
            root.join(format!("{}.pre-restore-clash", ARCHIVE_DB_NAME)),
            b"someone else's file",
        )
        .unwrap();

        let error = install_restore(&staging, &db_path, "clash").unwrap_err();

        assert!(matches!(error, LificError::Conflict(_)), "got {error:?}");
        assert!(db_path.exists(), "nothing was moved, nothing is missing");
        let conn = rusqlite::Connection::open(&db_path).unwrap();
        let live: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM projects WHERE identifier = 'LIV'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(live, 1);
        assert_eq!(
            fs::read(root.join(format!("{}.pre-restore-clash", ARCHIVE_DB_NAME))).unwrap(),
            b"someone else's file",
            "an unrelated file at the backup path is never clobbered"
        );
    }

    #[test]
    fn install_moves_live_state_aside_and_publishes_the_staged_tree() {
        // Positive control for the two rollback tests above.
        let (dir_tmp, db_path, staging, sha) = seed_install_fixture("install_ok");
        let root = dir_tmp.path();
        let restored_sha = crate::storage::AttachmentStore::hash_bytes(b"restored blob");
        fs::write(
            staging.join("attachments").join(&restored_sha),
            b"restored blob",
        )
        .unwrap();

        let moved = install_restore(&staging, &db_path, "ok")
            .unwrap()
            .expect("the live db is moved aside");

        assert_eq!(fs::read(&db_path).unwrap(), b"restored database bytes");
        assert!(moved.exists(), "the previous database is kept, not deleted");
        assert!(
            root.join("attachments").join(&restored_sha).exists(),
            "restored blobs are live"
        );
        assert!(
            !root.join("attachments").join(&sha).exists(),
            "the old attachments dir is replaced wholesale"
        );
        assert!(
            !root.join("attachments.pre-restore-ok").exists(),
            "its backup is cleaned up on success"
        );
    }

    #[test]
    fn restore_options_default_to_the_bounded_limits() {
        let bounded = RestoreOptions::new(false, false);
        assert_eq!(bounded.limits.max_db_bytes, MAX_DB_BYTES);
        assert_eq!(bounded.limits.max_attachment_bytes, MAX_ATTACHMENT_BYTES);
        assert_eq!(bounded.limits.max_total_bytes, MAX_TOTAL_RESTORE_BYTES);
        assert_eq!(bounded.limits.max_entries, MAX_RESTORE_ENTRIES);
        assert!(!bounded.force);

        let large = RestoreOptions::new(true, true);
        assert!(large.force);
        assert!(large.limits.max_db_bytes > MAX_DB_BYTES);
        assert!(large.limits.max_total_bytes > MAX_TOTAL_RESTORE_BYTES);
        assert!(large.limits.max_entries > MAX_RESTORE_ENTRIES);
        // Still finite: a decompression bomb must still hit a wall.
        assert!(large.limits.max_decompressed_bytes() < u64::MAX);
    }

    #[test]
    fn allow_large_restores_an_archive_the_default_limits_refuse() {
        // `write_dump` has no size ceiling, so a big enough instance can take
        // an honest backup that the bounded defaults reject. Standing in for a
        // 512 MiB database here: limits this small, perfectly good archive
        // exceeds.
        let (src_dir_tmp, src_db) = seed_data_dir("allow_large_src");
        let archive = src_dir_tmp.path().join("backup.tar.gz");
        let manifest = write_dump(&crate::db::open(&src_db).unwrap(), &src_db, &archive).unwrap();
        let tight = RestoreLimits {
            max_db_bytes: manifest.db_size_bytes - 1,
            ..RestoreLimits::default()
        };

        let error = inspect_archive(&archive, &tight).unwrap_err();
        assert!(
            error.to_string().contains("exceeds restore limit"),
            "got {error}"
        );
        inspect_archive(&archive, &RestoreLimits::trusted())
            .expect("--allow-large accepts a trusted oversized archive");

        let dst = temp_dir("allow_large_dst");
        let dst_db = dst.path().join(ARCHIVE_DB_NAME);
        let bounded = RestoreOptions {
            force: false,
            limits: tight,
        };
        assert!(run_restore_with(&archive, &dst_db, &bounded).is_err());
        assert!(!dst_db.exists(), "a refused restore installs nothing");

        let result = run_restore_with(&archive, &dst_db, &RestoreOptions::new(false, true))
            .expect("--allow-large completes the restore");
        assert_eq!(result.attachment_count, 2);
        assert!(dst_db.exists());
    }

    #[test]
    fn allow_large_still_rejects_a_hostile_archive() {
        // The escape hatch raises size ceilings and nothing else: every
        // structural check still runs under trusted limits.
        let dir_tmp = temp_dir("allow_large_hostile");
        let dir = dir_tmp.path();
        let archive = dir.join("evil.tar.gz");
        {
            let file = fs::File::create(&archive).unwrap();
            let enc = flate2::write::GzEncoder::new(file, flate2::Compression::default());
            let mut tar = tar::Builder::new(enc);
            let manifest = Manifest {
                lific_version: "x".into(),
                schema_version: 1,
                created_at: "now".into(),
                db_size_bytes: 13,
                attachment_count: 1,
                attachment_bytes: 5,
            };
            let mj = serde_json::to_vec(&manifest).unwrap();
            append_bytes(&mut tar, ARCHIVE_MANIFEST_NAME, &mj).unwrap();
            append_bytes(&mut tar, ARCHIVE_DB_NAME, b"not a real db").unwrap();
            append_bytes(&mut tar, "attachments/sub/escape", b"pwned").unwrap();
            tar.into_inner().unwrap().finish().unwrap();
        }

        let error = inspect_archive(&archive, &RestoreLimits::trusted()).unwrap_err();
        assert!(matches!(error, LificError::BadRequest(_)), "got {error:?}");

        let dst = temp_dir("allow_large_hostile_dst");
        let dst_db = dst.path().join(ARCHIVE_DB_NAME);
        assert!(
            run_restore_with(&archive, &dst_db, &RestoreOptions::new(true, true)).is_err(),
            "--allow-large is not --skip-validation"
        );
        assert!(!dst_db.exists());
        assert!(!dst.path().join("attachments").join("sub").exists());
    }

    #[cfg(unix)]
    #[test]
    fn staging_file_does_not_follow_symlinks() {
        let dir = temp_dir("staging_symlink");
        let target = dir.path().join("outside");
        let link = dir.path().join("staged");
        fs::write(&target, b"untouched").unwrap();
        std::os::unix::fs::symlink(&target, &link).unwrap();

        assert!(create_staging_file(&link).is_err());
        assert_eq!(fs::read(target).unwrap(), b"untouched");
    }

    #[test]
    fn staged_database_rejects_blob_content_hash_mismatch() {
        let (dir_tmp, db_path) = seed_data_dir("content_hash");
        let dir = dir_tmp.path();
        let pool = crate::db::open(&db_path).unwrap();
        let sha = crate::storage::AttachmentStore::hash_bytes(b"good");
        {
            let conn = pool.write().unwrap();
            crate::db::queries::attachments::create_attachment(
                &conn,
                &sha,
                "note.txt",
                "text/plain",
                4,
                None,
            )
            .unwrap();
        }
        checkpoint_db_file(&db_path).unwrap();
        let staging = dir.join("staging");
        fs::create_dir_all(staging.join("attachments")).unwrap();
        fs::copy(&db_path, staging.join(ARCHIVE_DB_NAME)).unwrap();
        fs::write(staging.join("attachments").join(&sha), b"evil").unwrap();
        let manifest = Manifest {
            lific_version: env!("CARGO_PKG_VERSION").into(),
            schema_version: crate::db::migrate::latest_version(),
            created_at: String::new(),
            db_size_bytes: fs::metadata(&db_path).unwrap().len(),
            attachment_count: 1,
            attachment_bytes: 4,
        };

        let error =
            validate_staged_database(&staging, &manifest, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("content address"));
    }

    #[test]
    fn staged_database_rejects_blob_content_mime_mismatch() {
        let (dir_tmp, db_path) = seed_data_dir("content_mime");
        let dir = dir_tmp.path();
        let pool = crate::db::open(&db_path).unwrap();
        let sha = crate::storage::AttachmentStore::hash_bytes(b"good");
        {
            let conn = pool.write().unwrap();
            crate::db::queries::attachments::create_attachment(
                &conn,
                &sha,
                "image.png",
                "image/png",
                4,
                None,
            )
            .unwrap();
        }
        checkpoint_db_file(&db_path).unwrap();
        let staging = dir.join("staging");
        fs::create_dir_all(staging.join("attachments")).unwrap();
        fs::copy(&db_path, staging.join(ARCHIVE_DB_NAME)).unwrap();
        fs::write(staging.join("attachments").join(&sha), b"good").unwrap();
        let manifest = Manifest {
            lific_version: env!("CARGO_PKG_VERSION").into(),
            schema_version: crate::db::migrate::latest_version(),
            created_at: String::new(),
            db_size_bytes: fs::metadata(&db_path).unwrap().len(),
            attachment_count: 1,
            attachment_bytes: 4,
        };

        let error =
            validate_staged_database(&staging, &manifest, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("MIME image/png"));
    }

    #[test]
    fn staged_database_rejects_duplicate_hashes_with_inconsistent_sizes() {
        let (dir_tmp, db_path) = seed_data_dir("duplicate_sizes");
        let dir = dir_tmp.path();
        let pool = crate::db::open(&db_path).unwrap();
        let sha = crate::storage::AttachmentStore::hash_bytes(b"good");
        {
            let conn = pool.write().unwrap();
            for (filename, size) in [("one.txt", 4), ("two.txt", 5)] {
                crate::db::queries::attachments::create_attachment(
                    &conn,
                    &sha,
                    filename,
                    "text/plain",
                    size,
                    None,
                )
                .unwrap();
            }
        }
        checkpoint_db_file(&db_path).unwrap();
        let staging = dir.join("staging");
        fs::create_dir_all(staging.join("attachments")).unwrap();
        fs::copy(&db_path, staging.join(ARCHIVE_DB_NAME)).unwrap();
        fs::write(staging.join("attachments").join(&sha), b"good").unwrap();
        let manifest = Manifest {
            lific_version: env!("CARGO_PKG_VERSION").into(),
            schema_version: crate::db::migrate::latest_version(),
            created_at: String::new(),
            db_size_bytes: fs::metadata(&db_path).unwrap().len(),
            attachment_count: 1,
            attachment_bytes: 4,
        };

        let error =
            validate_staged_database(&staging, &manifest, &RestoreLimits::default()).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("invalid content address, MIME, or size")
        );
    }

    #[test]
    fn staged_database_rejects_unreferenced_blob_content_hash_mismatch() {
        let (dir_tmp, db_path) = seed_data_dir("unreferenced_content_hash");
        let dir = dir_tmp.path();
        let pool = crate::db::open(&db_path).unwrap();
        checkpoint_db_file(&db_path).unwrap();
        drop(pool);

        let staging = dir.join("staging");
        fs::create_dir_all(staging.join("attachments")).unwrap();
        fs::copy(&db_path, staging.join(ARCHIVE_DB_NAME)).unwrap();
        let sha = crate::storage::AttachmentStore::hash_bytes(b"good");
        fs::write(staging.join("attachments").join(&sha), b"evil").unwrap();
        let manifest = Manifest {
            lific_version: env!("CARGO_PKG_VERSION").into(),
            schema_version: crate::db::migrate::latest_version(),
            created_at: String::new(),
            db_size_bytes: fs::metadata(&db_path).unwrap().len(),
            attachment_count: 1,
            attachment_bytes: 4,
        };

        let error =
            validate_staged_database(&staging, &manifest, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("content address"));
    }

    #[test]
    fn staged_database_requires_attachment_table() {
        let (dir_tmp, db_path) = seed_data_dir("missing_attachments_table");
        let dir = dir_tmp.path();
        checkpoint_db_file(&db_path).unwrap();
        let staging = dir.join("staging");
        fs::create_dir_all(staging.join("attachments")).unwrap();
        fs::copy(&db_path, staging.join(ARCHIVE_DB_NAME)).unwrap();
        {
            let conn = rusqlite::Connection::open(staging.join(ARCHIVE_DB_NAME)).unwrap();
            conn.execute_batch(
                "DROP TRIGGER IF EXISTS attachments_fts_ai;
                 DROP TRIGGER IF EXISTS attachments_fts_au;
                 DROP TRIGGER IF EXISTS attachments_fts_ad;
                 DROP TABLE attachments;",
            )
            .unwrap();
        }
        let manifest = Manifest {
            lific_version: env!("CARGO_PKG_VERSION").into(),
            schema_version: crate::db::migrate::latest_version(),
            created_at: String::new(),
            db_size_bytes: 0,
            attachment_count: 0,
            attachment_bytes: 0,
        };

        let error =
            validate_staged_database(&staging, &manifest, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("attachments table"));
    }

    #[test]
    fn staged_database_rejects_missing_or_mismatched_attachment_metadata() {
        let (dir_tmp, db_path) = seed_data_dir("staged_metadata");
        let dir = dir_tmp.path();
        let pool = crate::db::open(&db_path).unwrap();
        let sha = "a".repeat(64);
        {
            let conn = pool.write().unwrap();
            crate::db::queries::attachments::create_attachment(
                &conn,
                &sha,
                "note.txt",
                "text/plain",
                4,
                None,
            )
            .unwrap();
        }
        checkpoint_db_file(&db_path).unwrap();
        let staging = dir.join("staging");
        fs::create_dir_all(staging.join("attachments")).unwrap();
        fs::copy(&db_path, staging.join(ARCHIVE_DB_NAME)).unwrap();
        let manifest = Manifest {
            lific_version: env!("CARGO_PKG_VERSION").into(),
            schema_version: crate::db::migrate::latest_version(),
            created_at: String::new(),
            db_size_bytes: fs::metadata(&db_path).unwrap().len(),
            attachment_count: 1,
            attachment_bytes: 4,
        };
        let error =
            validate_staged_database(&staging, &manifest, &RestoreLimits::default()).unwrap_err();
        assert!(error.to_string().contains("missing attachment"));
    }

    // Test helper: re-pack an archive but overwrite the manifest's
    // schema_version, to simulate an archive from a newer binary.
    fn rewrite_archive_with_schema(src: &Path, dst: &Path, schema_version: i64) {
        let mut manifest = read_manifest(src, &RestoreLimits::default()).unwrap();
        manifest.schema_version = schema_version;
        rewrite_archive_manifest(src, dst, &manifest);
    }

    fn rewrite_archive_manifest(src: &Path, dst: &Path, manifest: &Manifest) {
        let mj = serde_json::to_vec_pretty(&manifest).unwrap();

        let out = fs::File::create(dst).unwrap();
        let enc = flate2::write::GzEncoder::new(out, flate2::Compression::default());
        let mut builder = tar::Builder::new(enc);
        append_bytes(&mut builder, ARCHIVE_MANIFEST_NAME, &mj).unwrap();

        // Copy the db + attachments through unchanged.
        let file = fs::File::open(src).unwrap();
        let dec = flate2::read::GzDecoder::new(file);
        let mut tar = tar::Archive::new(dec);
        for entry in tar.entries().unwrap() {
            let mut entry = entry.unwrap();
            let name = entry.path().unwrap().to_string_lossy().to_string();
            if name == ARCHIVE_MANIFEST_NAME {
                continue;
            }
            let mut buf = Vec::new();
            std::io::Read::read_to_end(&mut entry, &mut buf).unwrap();
            append_bytes(&mut builder, &name, &buf).unwrap();
        }
        let enc = builder.into_inner().unwrap();
        enc.finish().unwrap();
    }
}