rustfs-ecstore 1.0.1

Erasure coding storage backend for RustFS, providing efficient data storage and retrieval with redundancy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::msgp_decode::{read_msgp_ext8_time, skip_msgp_value, write_msgp_time};
use super::object_lock::{ObjectLockApi, ObjectLockStatusExt};
use super::versioning::VersioningApi;
use super::{quota::BucketQuota, target::BucketTargets};
use crate::bucket::replication::invalid_replication_config_status_field;
use crate::bucket::utils::deserialize;
use crate::config::com::{read_config, read_config_preserve_empty, save_config, save_config_with_opts};
use crate::disk::BUCKET_META_PREFIX;
use crate::error::{Error, Result};
use crate::object_api::WriteCompletion;
use crate::runtime::sources as runtime_sources;
use crate::store::ECStore;
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use rustfs_policy::policy::BucketPolicy;
use s3s::dto::{
    AccelerateConfiguration, BucketLifecycleConfiguration, BucketLoggingStatus, BucketVersioningStatus, CORSConfiguration,
    NotificationConfiguration, ObjectLockConfiguration, PublicAccessBlockConfiguration, ReplicationConfiguration,
    RequestPaymentConfiguration, ServerSideEncryptionConfiguration, Tagging, VersioningConfiguration, WebsiteConfiguration,
};
use serde::Serializer;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::io::{Read, Write};
use std::sync::Arc;
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time as CivilTime, UtcOffset};
use tracing::error;
use uuid::Uuid;

// The serving-layer DTO impls for the storage-level Object Lock traits live
// here because this module owns the persisted `ObjectLockConfiguration`
// during the s3s ratchet migration (rustfs/backlog#1842).
impl ObjectLockApi for ObjectLockConfiguration {
    fn enabled(&self) -> bool {
        self.object_lock_enabled
            .as_ref()
            .is_some_and(|v| v.as_str() == s3s::dto::ObjectLockEnabled::ENABLED)
    }
}

impl ObjectLockStatusExt for s3s::dto::ObjectLockLegalHoldStatus {
    fn valid(&self) -> bool {
        matches!(
            self.as_str(),
            s3s::dto::ObjectLockLegalHoldStatus::ON | s3s::dto::ObjectLockLegalHoldStatus::OFF
        )
    }
}

fn read_msgp_str<R: Read>(rd: &mut R) -> Result<String> {
    let len = rmp::decode::read_str_len(rd)? as usize;
    let mut buf = vec![0u8; len];
    rd.read_exact(&mut buf)?;
    Ok(String::from_utf8(buf)?)
}

fn read_msgp_bool<R: Read>(rd: &mut R) -> Result<bool> {
    let marker = rmp::decode::read_marker(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    match marker {
        rmp::Marker::True => Ok(true),
        rmp::Marker::False => Ok(false),
        rmp::Marker::FixPos(v) => Ok(v != 0),
        rmp::Marker::U8 => Ok(read_u8(rd)? != 0),
        rmp::Marker::U16 => Ok(read_u16_raw(rd)? != 0),
        rmp::Marker::U32 => Ok(read_u32_raw(rd)? != 0),
        rmp::Marker::U64 => Ok(read_u64_raw(rd)? != 0),
        rmp::Marker::I8 => Ok(read_i8_raw(rd)? != 0),
        rmp::Marker::I16 => Ok(read_i16_raw(rd)? != 0),
        rmp::Marker::I32 => Ok(read_i32_raw(rd)? != 0),
        rmp::Marker::I64 => Ok(read_i64_raw(rd)? != 0),
        rmp::Marker::FixNeg(v) => Ok(v != 0),
        _ => Err(Error::other(format!("expected bool or int-like bool, got marker: {marker:?}"))),
    }
}

fn read_msgp_time_value<R: Read>(rd: &mut R) -> Result<OffsetDateTime> {
    let marker = rmp::decode::read_marker(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    match marker {
        rmp::Marker::Null => Ok(OffsetDateTime::UNIX_EPOCH),
        rmp::Marker::Ext8 => read_msgp_ext8_time(rd),
        rmp::Marker::FixArray(len) => read_msgp_legacy_compact_time(rd, u32::from(len)),
        rmp::Marker::Array16 => {
            let len = read_u16_raw(rd)?;
            read_msgp_legacy_compact_time(rd, u32::from(len))
        }
        rmp::Marker::Array32 => {
            let len = read_u32_raw(rd)?;
            read_msgp_legacy_compact_time(rd, len)
        }
        rmp::Marker::Bin8 => {
            let len = usize::from(read_u8(rd)?);
            read_msgp_time_value_from_embedded_bin(rd, len)
        }
        rmp::Marker::Bin16 => {
            let len = usize::from(read_u16_raw(rd)?);
            read_msgp_time_value_from_embedded_bin(rd, len)
        }
        rmp::Marker::Bin32 => {
            let len = read_u32_raw(rd)? as usize;
            read_msgp_time_value_from_embedded_bin(rd, len)
        }
        _ => Err(Error::other(format!("expected time ext or nil, got marker: {marker:?}"))),
    }
}

fn read_u8<R: Read>(rd: &mut R) -> Result<u8> {
    let mut buf = [0u8; 1];
    rd.read_exact(&mut buf)?;
    Ok(buf[0])
}

fn read_u16_raw<R: Read>(rd: &mut R) -> Result<u16> {
    let mut buf = [0u8; 2];
    rd.read_exact(&mut buf)?;
    Ok(BigEndian::read_u16(&buf))
}

fn read_u32_raw<R: Read>(rd: &mut R) -> Result<u32> {
    let mut buf = [0u8; 4];
    rd.read_exact(&mut buf)?;
    Ok(BigEndian::read_u32(&buf))
}

fn read_u64_raw<R: Read>(rd: &mut R) -> Result<u64> {
    let mut buf = [0u8; 8];
    rd.read_exact(&mut buf)?;
    Ok(BigEndian::read_u64(&buf))
}

fn read_i8_raw<R: Read>(rd: &mut R) -> Result<i8> {
    Ok(read_u8(rd)? as i8)
}

fn read_i16_raw<R: Read>(rd: &mut R) -> Result<i16> {
    let mut buf = [0u8; 2];
    rd.read_exact(&mut buf)?;
    Ok(BigEndian::read_i16(&buf))
}

fn read_i32_raw<R: Read>(rd: &mut R) -> Result<i32> {
    let mut buf = [0u8; 4];
    rd.read_exact(&mut buf)?;
    Ok(BigEndian::read_i32(&buf))
}

fn read_i64_raw<R: Read>(rd: &mut R) -> Result<i64> {
    let mut buf = [0u8; 8];
    rd.read_exact(&mut buf)?;
    Ok(BigEndian::read_i64(&buf))
}

fn read_msgp_time_value_from_embedded_bin<R: Read>(rd: &mut R, len: usize) -> Result<OffsetDateTime> {
    let mut buf = vec![0u8; len];
    rd.read_exact(&mut buf)?;
    let mut cur = std::io::Cursor::new(buf);
    read_msgp_time_value(&mut cur)
}

fn read_msgp_legacy_compact_time<R: Read>(rd: &mut R, len: u32) -> Result<OffsetDateTime> {
    if len != 9 {
        return Err(Error::other(format!("invalid legacy compact time len: {len}")));
    }

    let year: i32 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    let ordinal: u16 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    let hour: u8 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    let minute: u8 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    let second: u8 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    let nanosecond: u32 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    let offset_hour: i8 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    let offset_minute: i8 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    let offset_second: i8 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;

    let date =
        Date::from_ordinal_date(year, ordinal).map_err(|e| Error::other(format!("invalid legacy compact time date: {e}")))?;
    let time = CivilTime::from_hms_nano(hour, minute, second, nanosecond)
        .map_err(|e| Error::other(format!("invalid legacy compact time time: {e}")))?;
    let offset = UtcOffset::from_hms(offset_hour, offset_minute, offset_second)
        .map_err(|e| Error::other(format!("invalid legacy compact time offset: {e}")))?;

    Ok(PrimitiveDateTime::new(date, time)
        .assume_offset(offset)
        .to_offset(UtcOffset::UTC))
}

fn read_msgp_bin<R: Read>(rd: &mut R) -> Result<Vec<u8>> {
    let marker = rmp::decode::read_marker(rd).map_err(|e| Error::other(format!("{e:?}")))?;
    match marker {
        rmp::Marker::Null => Ok(Vec::new()),
        rmp::Marker::Bin8 => {
            let len = usize::from(read_u8(rd)?);
            read_exact_bytes(rd, len)
        }
        rmp::Marker::Bin16 => {
            let len = usize::from(read_u16_raw(rd)?);
            read_exact_bytes(rd, len)
        }
        rmp::Marker::Bin32 => {
            let len = read_u32_raw(rd)? as usize;
            read_exact_bytes(rd, len)
        }
        rmp::Marker::FixArray(len) => read_msgp_legacy_byte_array(rd, u32::from(len)),
        rmp::Marker::Array16 => {
            let len = read_u16_raw(rd)?;
            read_msgp_legacy_byte_array(rd, u32::from(len))
        }
        rmp::Marker::Array32 => {
            let len = read_u32_raw(rd)?;
            read_msgp_legacy_byte_array(rd, len)
        }
        _ => Err(Error::other(format!("expected bin or byte array, got marker: {marker:?}"))),
    }
}

fn read_exact_bytes<R: Read>(rd: &mut R, len: usize) -> Result<Vec<u8>> {
    let mut buf = vec![0u8; len];
    rd.read_exact(&mut buf)?;
    Ok(buf)
}

fn read_msgp_legacy_byte_array<R: Read>(rd: &mut R, len: u32) -> Result<Vec<u8>> {
    let mut buf = Vec::with_capacity(len as usize);
    for _ in 0..len {
        let value: i64 = rmp::decode::read_int(rd).map_err(|e| Error::other(format!("{e:?}")))?;
        let byte = u8::try_from(value).map_err(|_| Error::other(format!("byte value out of range: {value}")))?;
        buf.push(byte);
    }
    Ok(buf)
}

fn write_bin_field<W: Write>(wr: &mut W, key: &str, val: &[u8]) -> Result<()> {
    rmp::encode::write_str(wr, key)?;
    rmp::encode::write_bin(wr, val)?;
    Ok(())
}

pub const BUCKET_METADATA_FILE: &str = ".metadata.bin";
pub const BUCKET_INCARNATION_FILE: &str = ".bucket-incarnation";
pub const BUCKET_METADATA_FORMAT: u16 = 1;
pub const BUCKET_METADATA_VERSION: u16 = 1;

pub const BUCKET_POLICY_CONFIG: &str = "policy.json";
pub const BUCKET_NOTIFICATION_CONFIG: &str = "notification.xml";
pub const BUCKET_LIFECYCLE_CONFIG: &str = "lifecycle.xml";
pub const BUCKET_SSECONFIG: &str = "bucket-encryption.xml";
pub const BUCKET_TAGGING_CONFIG: &str = "tagging.xml";
pub const BUCKET_QUOTA_CONFIG_FILE: &str = "quota.json";
pub const OBJECT_LOCK_CONFIG: &str = "object-lock.xml";
pub const BUCKET_VERSIONING_CONFIG: &str = "versioning.xml";
pub const BUCKET_REPLICATION_CONFIG: &str = "replication.xml";
pub const BUCKET_TARGETS_FILE: &str = "bucket-targets.json";
pub const BUCKET_CORS_CONFIG: &str = "cors.xml";
pub const BUCKET_LOGGING_CONFIG: &str = "logging.xml";
pub const BUCKET_WEBSITE_CONFIG: &str = "website.xml";
pub const BUCKET_ACCELERATE_CONFIG: &str = "accelerate.xml";
pub const BUCKET_REQUEST_PAYMENT_CONFIG: &str = "request-payment.xml";
pub const BUCKET_PUBLIC_ACCESS_BLOCK_CONFIG: &str = "public-access-block.xml";
pub const BUCKET_ACL_CONFIG: &str = "bucket-acl.json";
pub const BUCKET_TABLE_CONFIG: &str = "table-bucket.json";
pub const BUCKET_DURABILITY_CONFIG: &str = "durability.json";
pub const BUCKET_ON_DEMAND_MIGRATION_CONFIG: &str = "on-demand-migration.json";
pub const BUCKET_TABLE_RESERVED_PREFIX: &str = ".rustfs-table";
pub const BUCKET_TABLE_CATALOG_META_PREFIX: &str = "s3tables/catalog";
pub const BUCKET_TABLE_CATALOG_TABLE_BUCKETS_PREFIX: &str = "table-buckets";

/// Refusal to act on a stored sub-configuration whose bytes exist but cannot
/// be parsed. Carried inside [`Error::other`] so callers can tell it apart
/// from a storage fault with [`is_unreadable_config_error`].
#[derive(Debug, Clone)]
pub struct UnreadableBucketConfig {
    pub bucket: String,
    pub config_file: String,
    /// Length of the stored bytes that failed to parse.
    pub raw_len: usize,
}

impl std::fmt::Display for UnreadableBucketConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "persisted bucket configuration {} ({} bytes) for bucket {} cannot be parsed; back up the stored bytes (rustfs inspect bucket-meta), then replace or delete the configuration",
            self.config_file, self.raw_len, self.bucket
        )
    }
}

impl std::error::Error for UnreadableBucketConfig {}

pub fn is_unreadable_config_error(err: &Error) -> bool {
    unreadable_config_refusal(err).is_some()
}

pub fn unreadable_config_refusal(err: &Error) -> Option<&UnreadableBucketConfig> {
    match err {
        Error::Io(io) => io.get_ref().and_then(|inner| inner.downcast_ref::<UnreadableBucketConfig>()),
        _ => None,
    }
}

pub(crate) fn unreadable_config_error(bucket: &str, config_file: &str, raw_len: usize) -> Error {
    Error::other(UnreadableBucketConfig {
        bucket: bucket.to_string(),
        config_file: config_file.to_string(),
        raw_len,
    })
}

/// Stored state of one sub-configuration as left by
/// [`BucketMetadata::parse_all_configs`]: a parse failure keeps the raw bytes
/// and leaves the typed field `None`, which is kept distinct from "no bytes".
///
/// Deliberately has no `Option` conversion or defaulting accessor: folding
/// `Unreadable` into `Absent` is exactly the failure this type exists to stop.
#[derive(Debug)]
pub enum ConfigState<'a, T> {
    /// No bytes are stored.
    Absent,
    /// The stored bytes parsed.
    Valid(&'a T),
    /// Bytes are stored but could not be parsed.
    Unreadable { raw_len: usize },
}

impl<'a, T> ConfigState<'a, T> {
    pub fn of(raw: &[u8], parsed: &'a Option<T>) -> Self {
        match parsed {
            Some(config) => Self::Valid(config),
            None if raw.is_empty() => Self::Absent,
            None => Self::Unreadable { raw_len: raw.len() },
        }
    }

    /// `Ok(None)` when absent; a typed [`UnreadableBucketConfig`] refusal
    /// when the stored bytes cannot be parsed.
    pub fn require(self, bucket: &str, config_file: &str) -> Result<Option<&'a T>> {
        match self {
            Self::Absent => Ok(None),
            Self::Valid(config) => Ok(Some(config)),
            Self::Unreadable { raw_len } => Err(unreadable_config_error(bucket, config_file, raw_len)),
        }
    }
}

/// The XML sub-configurations whose parse failure is retained as
/// [`ConfigState::Unreadable`].
pub const XML_BUCKET_CONFIG_FILES: [&str; 13] = [
    BUCKET_NOTIFICATION_CONFIG,
    BUCKET_LIFECYCLE_CONFIG,
    OBJECT_LOCK_CONFIG,
    BUCKET_VERSIONING_CONFIG,
    BUCKET_SSECONFIG,
    BUCKET_TAGGING_CONFIG,
    BUCKET_REPLICATION_CONFIG,
    BUCKET_CORS_CONFIG,
    BUCKET_LOGGING_CONFIG,
    BUCKET_WEBSITE_CONFIG,
    BUCKET_ACCELERATE_CONFIG,
    BUCKET_REQUEST_PAYMENT_CONFIG,
    BUCKET_PUBLIC_ACCESS_BLOCK_CONFIG,
];

impl BucketMetadata {
    /// Whether the stored XML sub-configuration `config_file` has bytes that
    /// cannot be parsed. Non-XML config files report `false`: they carry their
    /// own unreadable handling (policy, quota, bucket targets).
    pub fn xml_config_unreadable(&self, config_file: &str) -> bool {
        self.xml_config_unreadable_len(config_file).is_some()
    }

    /// Length of the stored bytes of XML sub-configuration `config_file` when
    /// they cannot be parsed; `None` when it is absent, readable, or not an
    /// XML config.
    pub fn xml_config_unreadable_len(&self, config_file: &str) -> Option<usize> {
        fn unreadable<T>(raw: &[u8], parsed: &Option<T>) -> Option<usize> {
            match ConfigState::of(raw, parsed) {
                ConfigState::Unreadable { raw_len } => Some(raw_len),
                ConfigState::Absent | ConfigState::Valid(_) => None,
            }
        }
        match config_file {
            BUCKET_NOTIFICATION_CONFIG => unreadable(&self.notification_config_xml, &self.notification_config),
            BUCKET_LIFECYCLE_CONFIG => unreadable(&self.lifecycle_config_xml, &self.lifecycle_config),
            OBJECT_LOCK_CONFIG => unreadable(&self.object_lock_config_xml, &self.object_lock_config),
            BUCKET_VERSIONING_CONFIG => unreadable(&self.versioning_config_xml, &self.versioning_config),
            BUCKET_SSECONFIG => unreadable(&self.encryption_config_xml, &self.sse_config),
            BUCKET_TAGGING_CONFIG => unreadable(&self.tagging_config_xml, &self.tagging_config),
            BUCKET_REPLICATION_CONFIG => unreadable(&self.replication_config_xml, &self.replication_config),
            BUCKET_CORS_CONFIG => unreadable(&self.cors_config_xml, &self.cors_config),
            BUCKET_LOGGING_CONFIG => unreadable(&self.logging_config_xml, &self.logging_config),
            BUCKET_WEBSITE_CONFIG => unreadable(&self.website_config_xml, &self.website_config),
            BUCKET_ACCELERATE_CONFIG => unreadable(&self.accelerate_config_xml, &self.accelerate_config),
            BUCKET_REQUEST_PAYMENT_CONFIG => unreadable(&self.request_payment_config_xml, &self.request_payment_config),
            BUCKET_PUBLIC_ACCESS_BLOCK_CONFIG => {
                unreadable(&self.public_access_block_config_xml, &self.public_access_block_config)
            }
            _ => None,
        }
    }
}

pub fn table_catalog_path_hash(value: &str) -> String {
    let digest = Sha256::digest(value.as_bytes());
    let mut output = String::with_capacity(digest.len() * 2);
    const HEX: &[u8; 16] = b"0123456789abcdef";
    for byte in digest {
        output.push(char::from(HEX[usize::from(byte >> 4)]));
        output.push(char::from(HEX[usize::from(byte & 0x0f)]));
    }
    output
}

pub fn table_bucket_catalog_metadata_prefix(bucket: &str) -> String {
    format!(
        "{}/{}/{}",
        BUCKET_TABLE_CATALOG_META_PREFIX,
        BUCKET_TABLE_CATALOG_TABLE_BUCKETS_PREFIX,
        table_catalog_path_hash(bucket)
    )
}

#[derive(Debug, Clone)]
pub struct BucketMetadata {
    pub name: String,
    pub created: OffsetDateTime,
    pub lock_enabled: bool, // While marked as unused, it may need to be retained
    pub bucket_incarnation_id: Uuid,
    pub(crate) bucket_incarnation_sidecar: bool,
    pub(crate) bucket_creation_committed: bool,
    pub policy_config_json: Vec<u8>,
    pub notification_config_xml: Vec<u8>,
    pub lifecycle_config_xml: Vec<u8>,
    pub object_lock_config_xml: Vec<u8>,
    pub versioning_config_xml: Vec<u8>,
    pub encryption_config_xml: Vec<u8>,
    pub tagging_config_xml: Vec<u8>,
    pub quota_config_json: Vec<u8>,
    pub replication_config_xml: Vec<u8>,
    pub bucket_targets_config_json: Vec<u8>,
    pub bucket_targets_config_meta_json: Vec<u8>,
    pub cors_config_xml: Vec<u8>,
    pub logging_config_xml: Vec<u8>,
    pub website_config_xml: Vec<u8>,
    pub accelerate_config_xml: Vec<u8>,
    pub request_payment_config_xml: Vec<u8>,
    pub public_access_block_config_xml: Vec<u8>,
    pub bucket_acl_config_json: Vec<u8>,
    pub table_bucket_config_json: Vec<u8>,
    pub durability_config_json: Vec<u8>,
    pub on_demand_migration_config_json: Vec<u8>,

    pub policy_config_updated_at: OffsetDateTime,
    pub object_lock_config_updated_at: OffsetDateTime,
    pub encryption_config_updated_at: OffsetDateTime,
    pub tagging_config_updated_at: OffsetDateTime,
    pub quota_config_updated_at: OffsetDateTime,
    pub replication_config_updated_at: OffsetDateTime,
    pub versioning_config_updated_at: OffsetDateTime,
    pub lifecycle_config_updated_at: OffsetDateTime,
    pub notification_config_updated_at: OffsetDateTime,
    pub bucket_targets_config_updated_at: OffsetDateTime,
    pub bucket_targets_config_meta_updated_at: OffsetDateTime,
    pub cors_config_updated_at: OffsetDateTime,
    pub logging_config_updated_at: OffsetDateTime,
    pub website_config_updated_at: OffsetDateTime,
    pub accelerate_config_updated_at: OffsetDateTime,
    pub request_payment_config_updated_at: OffsetDateTime,
    pub public_access_block_config_updated_at: OffsetDateTime,
    pub bucket_acl_config_updated_at: OffsetDateTime,
    pub table_bucket_config_updated_at: OffsetDateTime,
    pub durability_config_updated_at: OffsetDateTime,
    pub on_demand_migration_config_updated_at: OffsetDateTime,

    pub new_field_updated_at: OffsetDateTime,

    pub policy_config: Option<BucketPolicy>,
    pub notification_config: Option<NotificationConfiguration>,
    pub lifecycle_config: Option<BucketLifecycleConfiguration>,
    pub object_lock_config: Option<ObjectLockConfiguration>,
    pub versioning_config: Option<VersioningConfiguration>,
    pub sse_config: Option<ServerSideEncryptionConfiguration>,
    pub tagging_config: Option<Tagging>,
    pub quota_config: Option<BucketQuota>,
    pub replication_config: Option<ReplicationConfiguration>,
    pub bucket_target_config: Option<BucketTargets>,
    pub bucket_target_config_meta: Option<HashMap<String, String>>,
    pub cors_config: Option<CORSConfiguration>,
    pub logging_config: Option<BucketLoggingStatus>,
    pub website_config: Option<WebsiteConfiguration>,
    pub accelerate_config: Option<AccelerateConfiguration>,
    pub request_payment_config: Option<RequestPaymentConfiguration>,
    pub public_access_block_config: Option<PublicAccessBlockConfiguration>,
    pub bucket_acl_config: Option<String>,
}

impl Default for BucketMetadata {
    fn default() -> Self {
        Self {
            name: Default::default(),
            created: OffsetDateTime::UNIX_EPOCH,
            lock_enabled: Default::default(),
            bucket_incarnation_id: Uuid::nil(),
            bucket_incarnation_sidecar: false,
            bucket_creation_committed: false,
            policy_config_json: Default::default(),
            notification_config_xml: Default::default(),
            lifecycle_config_xml: Default::default(),
            object_lock_config_xml: Default::default(),
            versioning_config_xml: Default::default(),
            encryption_config_xml: Default::default(),
            tagging_config_xml: Default::default(),
            quota_config_json: Default::default(),
            replication_config_xml: Default::default(),
            bucket_targets_config_json: Default::default(),
            bucket_targets_config_meta_json: Default::default(),
            cors_config_xml: Default::default(),
            logging_config_xml: Default::default(),
            website_config_xml: Default::default(),
            accelerate_config_xml: Default::default(),
            request_payment_config_xml: Default::default(),
            public_access_block_config_xml: Default::default(),
            bucket_acl_config_json: Default::default(),
            table_bucket_config_json: Default::default(),
            durability_config_json: Default::default(),
            on_demand_migration_config_json: Default::default(),
            policy_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            object_lock_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            encryption_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            tagging_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            quota_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            replication_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            versioning_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            lifecycle_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            notification_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            bucket_targets_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            bucket_targets_config_meta_updated_at: OffsetDateTime::UNIX_EPOCH,
            cors_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            logging_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            website_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            accelerate_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            request_payment_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            public_access_block_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            bucket_acl_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            table_bucket_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            durability_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            on_demand_migration_config_updated_at: OffsetDateTime::UNIX_EPOCH,
            new_field_updated_at: OffsetDateTime::UNIX_EPOCH,
            policy_config: Default::default(),
            notification_config: Default::default(),
            lifecycle_config: Default::default(),
            object_lock_config: Default::default(),
            versioning_config: Default::default(),
            sse_config: Default::default(),
            tagging_config: Default::default(),
            quota_config: Default::default(),
            replication_config: Default::default(),
            bucket_target_config: Default::default(),
            bucket_target_config_meta: Default::default(),
            cors_config: Default::default(),
            logging_config: Default::default(),
            website_config: Default::default(),
            accelerate_config: Default::default(),
            request_payment_config: Default::default(),
            public_access_block_config: Default::default(),
            bucket_acl_config: Default::default(),
        }
    }
}

impl BucketMetadata {
    pub fn new(name: &str) -> Self {
        BucketMetadata {
            name: name.to_string(),
            bucket_incarnation_id: Uuid::new_v4(),
            ..Default::default()
        }
    }

    /// Metadata for a physically new user bucket. Existing or fabricated legacy
    /// metadata must use [`Self::new`] so upgrades do not rewrite their
    /// durability posture.
    pub fn new_with_default_durability(name: &str) -> Self {
        let mut metadata = Self::new(name);
        metadata.durability_config_json = super::durability::new_bucket_durability_config_json(name);
        metadata
    }

    /// Whether this generation still needs its creation-commit proof.
    ///
    /// The proof is only ever written while the caller holds the bucket
    /// metadata transaction fence; see
    /// `BucketMetadataSys::migrate_bucket_creation_commit`.
    ///
    /// Metadata without an authoritative incarnation sidecar is owned by the
    /// legacy migration path, which mints the sidecar first; it must not be
    /// rewritten by this migration.
    pub(crate) fn needs_bucket_creation_commit(&self) -> bool {
        self.lock_enabled
            && !self.bucket_creation_committed
            && self.bucket_incarnation_sidecar
            && !self.bucket_incarnation_id.is_nil()
    }

    pub fn save_file_path(&self) -> String {
        format!("{}/{}/{}", BUCKET_META_PREFIX, self.name.as_str(), BUCKET_METADATA_FILE)
    }

    pub fn versioning(&self) -> bool {
        self.lock_enabled
            || (self.object_lock_config.as_ref().is_some_and(|v| v.enabled())
                || self.versioning_config.as_ref().is_some_and(|v| v.enabled()))
    }

    pub fn object_locking(&self) -> bool {
        self.lock_enabled || self.object_lock_config.as_ref().is_some_and(|v| v.enabled())
    }

    /// Whether an operation that may skip Object Lock checks must keep them.
    /// Stored lock bytes that cannot be parsed leave the lock state unknown,
    /// which must not be read as "no Object Lock".
    pub fn object_lock_checks_required(&self) -> bool {
        self.object_locking() || self.xml_config_unreadable(OBJECT_LOCK_CONFIG)
    }

    pub fn table_bucket_enabled(&self) -> bool {
        !self.table_bucket_config_json.is_empty()
    }

    /// `bucket-targets.json` is stored for this bucket but this build cannot
    /// decode it.
    ///
    /// Keeps "no replication targets configured" and "the target
    /// configuration cannot be read" apart, the same distinction the
    /// `fabricated` marker draws for the bucket metadata as a whole. Only
    /// meaningful after [`Self::parse_all_configs`] has run; readers must fail
    /// closed on `true` instead of serving an empty target set.
    pub fn bucket_targets_unreadable(&self) -> bool {
        !self.bucket_targets_config_json.is_empty() && self.bucket_target_config.is_none()
    }

    /// Opaque application-owned configuration with its persisted update time.
    /// Empty bytes mean absent or cleared; decoding belongs to the consumer.
    pub fn on_demand_migration_config(&self) -> Option<(&[u8], OffsetDateTime)> {
        (!self.on_demand_migration_config_json.is_empty()).then_some((
            self.on_demand_migration_config_json.as_slice(),
            self.on_demand_migration_config_updated_at,
        ))
    }

    /// Parsed per-bucket durability override, if a valid one is stored.
    /// Invalid payloads follow the global mode after logging a parse failure.
    pub fn durability_config(&self) -> Option<super::durability::BucketDurabilityConfig> {
        if self.durability_config_json.is_empty() {
            return None;
        }
        match serde_json::from_slice(&self.durability_config_json) {
            Ok(cfg) => Some(cfg),
            Err(e) => {
                tracing::warn!(
                    event = "bucket_metadata_parse_failed",
                    component = "ecstore",
                    subsystem = "bucket_metadata",
                    bucket = %self.name,
                    config = "durability",
                    error = %e,
                    "Failed to parse bucket metadata config"
                );
                None
            }
        }
    }

    /// Decode from msgp bytes. Field order follows MinIO BucketMetadata for compatibility.
    pub fn decode_from<R: Read>(&mut self, rd: &mut R) -> Result<()> {
        let mut fields = rmp::decode::read_map_len(rd)?;
        *self = Self::default();

        while fields > 0 {
            fields -= 1;

            let key_len = rmp::decode::read_str_len(rd)?;
            let mut key_buf = vec![0u8; key_len as usize];
            rd.read_exact(&mut key_buf)?;
            let key = String::from_utf8(key_buf)?;

            match key.as_str() {
                "Name" => self.name = read_msgp_str(rd)?,
                "Created" => self.created = read_msgp_time_value(rd)?,
                "LockEnabled" => self.lock_enabled = read_msgp_bool(rd)?,
                "BucketIncarnationID" => {
                    let bytes = read_msgp_bin(rd)?;
                    self.bucket_incarnation_id =
                        Uuid::from_slice(&bytes).map_err(|err| Error::other(format!("invalid BucketIncarnationID: {err}")))?;
                }
                "PolicyConfigJSON" | "PolicyConfigJson" => self.policy_config_json = read_msgp_bin(rd)?,
                "NotificationConfigXML" | "NotificationConfigXml" => self.notification_config_xml = read_msgp_bin(rd)?,
                "LifecycleConfigXML" | "LifecycleConfigXml" => self.lifecycle_config_xml = read_msgp_bin(rd)?,
                "ObjectLockConfigXML" | "ObjectLockConfigXml" => self.object_lock_config_xml = read_msgp_bin(rd)?,
                "VersioningConfigXML" | "VersioningConfigXml" => self.versioning_config_xml = read_msgp_bin(rd)?,
                "EncryptionConfigXML" | "EncryptionConfigXml" => self.encryption_config_xml = read_msgp_bin(rd)?,
                "TaggingConfigXML" | "TaggingConfigXml" => self.tagging_config_xml = read_msgp_bin(rd)?,
                "QuotaConfigJSON" | "QuotaConfigJson" => self.quota_config_json = read_msgp_bin(rd)?,
                "ReplicationConfigXML" | "ReplicationConfigXml" => self.replication_config_xml = read_msgp_bin(rd)?,
                "BucketTargetsConfigJSON" | "BucketTargetsConfigJson" => self.bucket_targets_config_json = read_msgp_bin(rd)?,
                "BucketTargetsConfigMetaJSON" | "BucketTargetsConfigMetaJson" => {
                    self.bucket_targets_config_meta_json = read_msgp_bin(rd)?
                }
                "PolicyConfigUpdatedAt" => self.policy_config_updated_at = read_msgp_time_value(rd)?,
                "ObjectLockConfigUpdatedAt" => self.object_lock_config_updated_at = read_msgp_time_value(rd)?,
                "EncryptionConfigUpdatedAt" => self.encryption_config_updated_at = read_msgp_time_value(rd)?,
                "TaggingConfigUpdatedAt" => self.tagging_config_updated_at = read_msgp_time_value(rd)?,
                "QuotaConfigUpdatedAt" => self.quota_config_updated_at = read_msgp_time_value(rd)?,
                "ReplicationConfigUpdatedAt" => self.replication_config_updated_at = read_msgp_time_value(rd)?,
                "VersioningConfigUpdatedAt" => self.versioning_config_updated_at = read_msgp_time_value(rd)?,
                "LifecycleConfigUpdatedAt" => self.lifecycle_config_updated_at = read_msgp_time_value(rd)?,
                "NotificationConfigUpdatedAt" => self.notification_config_updated_at = read_msgp_time_value(rd)?,
                "BucketTargetsConfigUpdatedAt" => self.bucket_targets_config_updated_at = read_msgp_time_value(rd)?,
                "BucketTargetsConfigMetaUpdatedAt" => self.bucket_targets_config_meta_updated_at = read_msgp_time_value(rd)?,
                "CorsConfigXML" | "CorsConfigXml" => self.cors_config_xml = read_msgp_bin(rd)?,
                "LoggingConfigXML" | "LoggingConfigXml" => self.logging_config_xml = read_msgp_bin(rd)?,
                "WebsiteConfigXML" | "WebsiteConfigXml" => self.website_config_xml = read_msgp_bin(rd)?,
                "AccelerateConfigXML" | "AccelerateConfigXml" => self.accelerate_config_xml = read_msgp_bin(rd)?,
                "RequestPaymentConfigXML" | "RequestPaymentConfigXml" => self.request_payment_config_xml = read_msgp_bin(rd)?,
                "PublicAccessBlockConfigXML" | "PublicAccessBlockConfigXml" => {
                    self.public_access_block_config_xml = read_msgp_bin(rd)?
                }
                "BucketAclConfigJSON" | "BucketAclConfigJson" => self.bucket_acl_config_json = read_msgp_bin(rd)?,
                "TableBucketConfigJSON" | "TableBucketConfigJson" => self.table_bucket_config_json = read_msgp_bin(rd)?,
                "DurabilityConfigJSON" | "DurabilityConfigJson" => self.durability_config_json = read_msgp_bin(rd)?,
                "OnDemandMigrationConfigJSON" | "OnDemandMigrationConfigJson" => {
                    self.on_demand_migration_config_json = read_msgp_bin(rd)?
                }
                "CorsConfigUpdatedAt" => self.cors_config_updated_at = read_msgp_time_value(rd)?,
                "LoggingConfigUpdatedAt" => self.logging_config_updated_at = read_msgp_time_value(rd)?,
                "WebsiteConfigUpdatedAt" => self.website_config_updated_at = read_msgp_time_value(rd)?,
                "AccelerateConfigUpdatedAt" => self.accelerate_config_updated_at = read_msgp_time_value(rd)?,
                "RequestPaymentConfigUpdatedAt" => self.request_payment_config_updated_at = read_msgp_time_value(rd)?,
                "PublicAccessBlockConfigUpdatedAt" => self.public_access_block_config_updated_at = read_msgp_time_value(rd)?,
                "BucketAclConfigUpdatedAt" => self.bucket_acl_config_updated_at = read_msgp_time_value(rd)?,
                "TableBucketConfigUpdatedAt" => self.table_bucket_config_updated_at = read_msgp_time_value(rd)?,
                "DurabilityConfigUpdatedAt" => self.durability_config_updated_at = read_msgp_time_value(rd)?,
                "OnDemandMigrationConfigUpdatedAt" => self.on_demand_migration_config_updated_at = read_msgp_time_value(rd)?,
                "BucketCreationCommitted" => self.bucket_creation_committed = read_msgp_bool(rd)?,
                other => {
                    tracing::debug!(field = %other, "BucketMetadata decode_from: skipping unknown field");
                    skip_msgp_value(rd)?;
                }
            }
        }

        Ok(())
    }

    /// Encode to msgp bytes. Field order follows MinIO BucketMetadata for compatibility.
    pub fn encode_to<W: Write>(&self, wr: &mut W) -> Result<()> {
        // Map size: MinIO fields (25) + RustFS extensions (22)
        let map_len: u32 = 47;
        rmp::encode::write_map_len(wr, map_len)?;

        // MinIO field order (same as Go struct)
        rmp::encode::write_str(wr, "Name")?;
        rmp::encode::write_str(wr, &self.name)?;

        rmp::encode::write_str(wr, "Created")?;
        write_msgp_time(wr, self.created)?;

        rmp::encode::write_str(wr, "LockEnabled")?;
        rmp::encode::write_bool(wr, self.lock_enabled)?;

        write_bin_field(wr, "BucketIncarnationID", self.bucket_incarnation_id.as_bytes())?;

        write_bin_field(wr, "PolicyConfigJSON", &self.policy_config_json)?;
        write_bin_field(wr, "NotificationConfigXML", &self.notification_config_xml)?;
        write_bin_field(wr, "LifecycleConfigXML", &self.lifecycle_config_xml)?;
        write_bin_field(wr, "ObjectLockConfigXML", &self.object_lock_config_xml)?;
        write_bin_field(wr, "VersioningConfigXML", &self.versioning_config_xml)?;
        write_bin_field(wr, "EncryptionConfigXML", &self.encryption_config_xml)?;
        write_bin_field(wr, "TaggingConfigXML", &self.tagging_config_xml)?;
        write_bin_field(wr, "QuotaConfigJSON", &self.quota_config_json)?;
        write_bin_field(wr, "ReplicationConfigXML", &self.replication_config_xml)?;
        write_bin_field(wr, "BucketTargetsConfigJSON", &self.bucket_targets_config_json)?;
        write_bin_field(wr, "BucketTargetsConfigMetaJSON", &self.bucket_targets_config_meta_json)?;

        rmp::encode::write_str(wr, "PolicyConfigUpdatedAt")?;
        write_msgp_time(wr, self.policy_config_updated_at)?;
        rmp::encode::write_str(wr, "ObjectLockConfigUpdatedAt")?;
        write_msgp_time(wr, self.object_lock_config_updated_at)?;
        rmp::encode::write_str(wr, "EncryptionConfigUpdatedAt")?;
        write_msgp_time(wr, self.encryption_config_updated_at)?;
        rmp::encode::write_str(wr, "TaggingConfigUpdatedAt")?;
        write_msgp_time(wr, self.tagging_config_updated_at)?;
        rmp::encode::write_str(wr, "QuotaConfigUpdatedAt")?;
        write_msgp_time(wr, self.quota_config_updated_at)?;
        rmp::encode::write_str(wr, "ReplicationConfigUpdatedAt")?;
        write_msgp_time(wr, self.replication_config_updated_at)?;
        rmp::encode::write_str(wr, "VersioningConfigUpdatedAt")?;
        write_msgp_time(wr, self.versioning_config_updated_at)?;
        rmp::encode::write_str(wr, "LifecycleConfigUpdatedAt")?;
        write_msgp_time(wr, self.lifecycle_config_updated_at)?;
        rmp::encode::write_str(wr, "NotificationConfigUpdatedAt")?;
        write_msgp_time(wr, self.notification_config_updated_at)?;
        rmp::encode::write_str(wr, "BucketTargetsConfigUpdatedAt")?;
        write_msgp_time(wr, self.bucket_targets_config_updated_at)?;
        rmp::encode::write_str(wr, "BucketTargetsConfigMetaUpdatedAt")?;
        write_msgp_time(wr, self.bucket_targets_config_meta_updated_at)?;

        // RustFS extensions
        write_bin_field(wr, "CorsConfigXML", &self.cors_config_xml)?;
        write_bin_field(wr, "LoggingConfigXML", &self.logging_config_xml)?;
        write_bin_field(wr, "WebsiteConfigXML", &self.website_config_xml)?;
        write_bin_field(wr, "AccelerateConfigXML", &self.accelerate_config_xml)?;
        write_bin_field(wr, "RequestPaymentConfigXML", &self.request_payment_config_xml)?;
        write_bin_field(wr, "PublicAccessBlockConfigXML", &self.public_access_block_config_xml)?;
        write_bin_field(wr, "BucketAclConfigJSON", &self.bucket_acl_config_json)?;
        write_bin_field(wr, "TableBucketConfigJSON", &self.table_bucket_config_json)?;
        write_bin_field(wr, "DurabilityConfigJSON", &self.durability_config_json)?;
        write_bin_field(wr, "OnDemandMigrationConfigJSON", &self.on_demand_migration_config_json)?;
        rmp::encode::write_str(wr, "CorsConfigUpdatedAt")?;
        write_msgp_time(wr, self.cors_config_updated_at)?;
        rmp::encode::write_str(wr, "LoggingConfigUpdatedAt")?;
        write_msgp_time(wr, self.logging_config_updated_at)?;
        rmp::encode::write_str(wr, "WebsiteConfigUpdatedAt")?;
        write_msgp_time(wr, self.website_config_updated_at)?;
        rmp::encode::write_str(wr, "AccelerateConfigUpdatedAt")?;
        write_msgp_time(wr, self.accelerate_config_updated_at)?;
        rmp::encode::write_str(wr, "RequestPaymentConfigUpdatedAt")?;
        write_msgp_time(wr, self.request_payment_config_updated_at)?;
        rmp::encode::write_str(wr, "PublicAccessBlockConfigUpdatedAt")?;
        write_msgp_time(wr, self.public_access_block_config_updated_at)?;
        rmp::encode::write_str(wr, "BucketAclConfigUpdatedAt")?;
        write_msgp_time(wr, self.bucket_acl_config_updated_at)?;
        rmp::encode::write_str(wr, "TableBucketConfigUpdatedAt")?;
        write_msgp_time(wr, self.table_bucket_config_updated_at)?;
        rmp::encode::write_str(wr, "DurabilityConfigUpdatedAt")?;
        write_msgp_time(wr, self.durability_config_updated_at)?;
        rmp::encode::write_str(wr, "OnDemandMigrationConfigUpdatedAt")?;
        write_msgp_time(wr, self.on_demand_migration_config_updated_at)?;
        rmp::encode::write_str(wr, "BucketCreationCommitted")?;
        rmp::encode::write_bool(wr, self.bucket_creation_committed)?;

        Ok(())
    }

    pub fn marshal_msg(&self) -> Result<Vec<u8>> {
        let mut buf = Vec::new();
        self.encode_to(&mut buf)?;
        Ok(buf)
    }

    pub fn unmarshal(buf: &[u8]) -> Result<Self> {
        let mut bm = Self::default();
        let mut cur = std::io::Cursor::new(buf);
        bm.decode_from(&mut cur)?;
        Ok(bm)
    }

    pub fn check_header(buf: &[u8]) -> Result<()> {
        if buf.len() <= 4 {
            return Err(Error::other("read_bucket_metadata: data invalid"));
        }

        let format = LittleEndian::read_u16(&buf[0..2]);
        let version = LittleEndian::read_u16(&buf[2..4]);

        match format {
            BUCKET_METADATA_FORMAT => {}
            _ => return Err(Error::other("read_bucket_metadata: format invalid")),
        }

        match version {
            BUCKET_METADATA_VERSION => {}
            _ => return Err(Error::other("read_bucket_metadata: version invalid")),
        }

        Ok(())
    }

    pub(crate) fn default_timestamps(&mut self) {
        if self.policy_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.policy_config_updated_at = self.created
        }
        if self.encryption_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.encryption_config_updated_at = self.created
        }

        if self.tagging_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.tagging_config_updated_at = self.created
        }
        if self.object_lock_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.object_lock_config_updated_at = self.created
        }
        if self.quota_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.quota_config_updated_at = self.created
        }

        if self.replication_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.replication_config_updated_at = self.created
        }

        if self.versioning_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.versioning_config_updated_at = self.created
        }

        if self.lifecycle_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.lifecycle_config_updated_at = self.created
        }
        if self.notification_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.notification_config_updated_at = self.created
        }

        if self.bucket_targets_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.bucket_targets_config_updated_at = self.created
        }
        if self.bucket_targets_config_meta_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.bucket_targets_config_meta_updated_at = self.created
        }
        if self.public_access_block_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.public_access_block_config_updated_at = self.created
        }
        if self.logging_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.logging_config_updated_at = self.created
        }
        if self.website_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.website_config_updated_at = self.created
        }
        if self.accelerate_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.accelerate_config_updated_at = self.created
        }
        if self.request_payment_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.request_payment_config_updated_at = self.created
        }
        if self.bucket_acl_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.bucket_acl_config_updated_at = self.created
        }
        if self.table_bucket_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.table_bucket_config_updated_at = self.created
        }
        if self.durability_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.durability_config_updated_at = self.created
        }
        if self.on_demand_migration_config_updated_at == OffsetDateTime::UNIX_EPOCH {
            self.on_demand_migration_config_updated_at = self.created
        }
    }

    /// Replace one config payload and stamp its `*_config_updated_at` with the
    /// local clock. This is the entry for edits that originate here: the
    /// local write time is the edit's source time.
    pub fn update_config(&mut self, config_file: &str, data: Vec<u8>) -> Result<OffsetDateTime> {
        self.update_config_at(config_file, data, OffsetDateTime::now_utc())
    }

    /// [`Self::update_config`] with an explicit `updated_at` stamp.
    ///
    /// For a config replicated from another site the edit's source time is
    /// the peer's `updated_at`, not the moment it lands here: staleness of
    /// the next incoming item is judged against the stored stamp, so stamping
    /// the local apply time would reject a newer source edit that was merely
    /// delivered late (backlog#2292). Only replication receivers should pass
    /// a foreign time; local edits keep [`Self::update_config`].
    pub fn update_config_at(&mut self, config_file: &str, data: Vec<u8>, updated: OffsetDateTime) -> Result<OffsetDateTime> {
        match config_file {
            BUCKET_POLICY_CONFIG => {
                self.policy_config_json = data;
                self.policy_config_updated_at = updated;
            }
            BUCKET_NOTIFICATION_CONFIG => {
                self.notification_config_xml = data;
                self.notification_config_updated_at = updated;
            }
            BUCKET_LIFECYCLE_CONFIG => {
                self.lifecycle_config_xml = data;
                self.lifecycle_config = None;
                self.lifecycle_config_updated_at = updated;
            }
            BUCKET_SSECONFIG => {
                self.encryption_config_xml = data;
                self.encryption_config_updated_at = updated;
            }
            BUCKET_TAGGING_CONFIG => {
                self.tagging_config_xml = data;
                // Drop the parsed form (like lifecycle above) so clearing the
                // payload can't leave stale parsed tags to be cached.
                self.tagging_config = None;
                self.tagging_config_updated_at = updated;
            }
            BUCKET_QUOTA_CONFIG_FILE => {
                self.quota_config_json = data;
                self.quota_config_updated_at = updated;
            }
            OBJECT_LOCK_CONFIG => {
                self.object_lock_config = None;
                if !data.is_empty() {
                    self.lock_enabled = true;
                    // Enabling Object Lock is only reachable for a bucket that
                    // already exists: the caller validated physical presence and
                    // holds the incarnation fence, so this generation is committed
                    // and must never be mistaken for a pre-physical creation intent.
                    self.bucket_creation_committed = true;
                }
                self.object_lock_config_xml = data;
                self.object_lock_config_updated_at = updated;
            }
            BUCKET_VERSIONING_CONFIG => {
                let config = if data.is_empty() {
                    None
                } else {
                    let config = deserialize::<VersioningConfiguration>(&data)?;
                    if config.status.as_ref().is_some_and(|status| {
                        !matches!(status.as_str(), BucketVersioningStatus::ENABLED | BucketVersioningStatus::SUSPENDED)
                    }) {
                        return Err(Error::other("bucket versioning configuration has an invalid status"));
                    }
                    Some(config)
                };
                self.versioning_config_xml = data;
                self.versioning_config = config;
                self.versioning_config_updated_at = updated;
            }
            BUCKET_REPLICATION_CONFIG => {
                let config = if data.is_empty() {
                    None
                } else {
                    let config = deserialize::<ReplicationConfiguration>(&data)?;
                    if let Some(field) = invalid_replication_config_status_field(&config) {
                        return Err(Error::other(format!("replication field {field} has an invalid status")));
                    }
                    Some(config)
                };
                self.replication_config_xml = data;
                self.replication_config = config;
                self.replication_config_updated_at = updated;
            }
            BUCKET_TARGETS_FILE => {
                // let x = data.clone();
                // let str = std::str::from_utf8(&x).expect("Invalid UTF-8");
                // println!("update config:{}", str);
                self.bucket_targets_config_json = data;
                self.bucket_targets_config_updated_at = updated;
            }
            BUCKET_CORS_CONFIG => {
                self.cors_config_xml = data;
                self.cors_config_updated_at = updated;
            }
            BUCKET_LOGGING_CONFIG => {
                self.logging_config_xml = data;
                self.logging_config_updated_at = updated;
            }
            BUCKET_WEBSITE_CONFIG => {
                self.website_config_xml = data;
                self.website_config_updated_at = updated;
            }
            BUCKET_ACCELERATE_CONFIG => {
                self.accelerate_config_xml = data;
                self.accelerate_config_updated_at = updated;
            }
            BUCKET_REQUEST_PAYMENT_CONFIG => {
                self.request_payment_config_xml = data;
                self.request_payment_config_updated_at = updated;
            }
            BUCKET_PUBLIC_ACCESS_BLOCK_CONFIG => {
                self.public_access_block_config_xml = data;
                self.public_access_block_config_updated_at = updated;
            }
            BUCKET_ACL_CONFIG => {
                self.bucket_acl_config_json = data;
                self.bucket_acl_config_updated_at = updated;
            }
            BUCKET_TABLE_CONFIG => {
                self.table_bucket_config_json = data;
                self.table_bucket_config_updated_at = updated;
            }
            BUCKET_DURABILITY_CONFIG => {
                self.durability_config_json = data;
                self.durability_config_updated_at = updated;
            }
            BUCKET_ON_DEMAND_MIGRATION_CONFIG => {
                self.on_demand_migration_config_json = data;
                self.on_demand_migration_config_updated_at = updated;
            }
            _ => return Err(Error::other(format!("config file not found : {config_file}"))),
        }

        Ok(updated)
    }

    pub fn set_created(&mut self, created: Option<OffsetDateTime>) {
        self.created = created.unwrap_or_else(OffsetDateTime::now_utc)
    }

    pub async fn save(&mut self) -> Result<()> {
        let Some(store) = runtime_sources::object_store_handle() else {
            return Err(Error::other("errServerNotInitialized"));
        };

        self.save_with_store(store).await
    }

    /// Persist this metadata through an explicit store (backlog#1052 S7): the
    /// owning instance's metadata system passes its own store so a second
    /// server's bucket metadata lands in that server's `.rustfs.sys`, not the
    /// ambient (first) one. [`BucketMetadata::save`] keeps the ambient default.
    pub async fn save_with_store(&mut self, store: std::sync::Arc<crate::store::ECStore>) -> Result<()> {
        self.save_with_store_completion(store, WriteCompletion::Quorum).await
    }

    /// Persist a committed creation record and drain the rename fan-out before
    /// returning. A quorum ACK is insufficient here: later degraded reads may
    /// have to reconstruct this record from any read quorum of surviving shards.
    pub(crate) async fn save_with_store_committed(&mut self, store: std::sync::Arc<crate::store::ECStore>) -> Result<()> {
        self.save_with_store_completion(store, WriteCompletion::TailDrained).await
    }

    async fn save_with_store_completion(
        &mut self,
        store: std::sync::Arc<crate::store::ECStore>,
        write_completion: WriteCompletion,
    ) -> Result<()> {
        self.parse_all_configs()?;
        let mut buf: Vec<u8> = vec![0; 4];

        LittleEndian::write_u16(&mut buf[0..2], BUCKET_METADATA_FORMAT);

        LittleEndian::write_u16(&mut buf[2..4], BUCKET_METADATA_VERSION);

        let data = self
            .marshal_msg()
            .map_err(|e| Error::other(format!("save bucket metadata failed: {e}")))?;

        buf.extend_from_slice(&data);

        save_config_with_opts(
            store,
            self.save_file_path().as_str(),
            buf,
            &crate::object_api::ObjectOptions {
                max_parity: true,
                write_completion,
                ..Default::default()
            },
        )
        .await?;

        Ok(())
    }

    fn parse_policy_config(&mut self) -> Result<()> {
        if !self.policy_config_json.is_empty() {
            self.policy_config = Some(serde_json::from_slice(&self.policy_config_json)?);
        } else {
            self.policy_config = None;
        }
        Ok(())
    }

    /// Decode every stored sub-configuration into its typed field.
    ///
    /// A decode failure never fails the whole load: this runs on every bucket
    /// metadata read, including startup and peer reload, so one bucket's
    /// corrupt sub-configuration must not make the bucket — or the node —
    /// unloadable. Instead the failure is *retained*: the raw bytes stay
    /// untouched and the typed field stays `None`, so `!raw.is_empty() &&
    /// typed.is_none()` is the durable "exists but cannot be read" signal that
    /// each accessor keys off. Which accessors must fail closed on it:
    ///
    /// | Config | Verdict |
    /// |---|---|
    /// | policy | Fails closed: `get_bucket_policy` re-parses the raw JSON and propagates the error; `get_bucket_policy_raw` returns the stored bytes. |
    /// | object lock | Fails closed in `object_lock_config_state_from_authoritative_metadata`; a retention decision may never be taken on a guess. |
    /// | versioning | Fails closed in `get_versioning_config` and the delete-time snapshot; guessing Unversioned would make delete markers and version ids diverge from what is on disk. Object writes lay out versions through `BucketVersioningSys::get_for_write`, which refuses in strict mode and, in the default permissive mode, keeps the historical unversioned write (see `config_parse_mode`). |
    /// | replication | Fails closed in `get_replication_config`. |
    /// | bucket targets | Fails closed in `get_bucket_targets_config`, and `sync_bucket_target_sys` marks the bucket unreadable in `BucketTargetSys` instead of publishing an empty target set (rustfs/backlog#2282). |
    /// | encryption | Fails closed in `get_sse_config`: degrading to "no default encryption" stores plaintext objects the operator required to be encrypted. |
    /// | public access block | Fails closed in `get_public_access_block_config`: degrading grants the anonymous access the operator asked to block. |
    /// | quota | Fails closed in `get_quota_config`; the enforcement path in `quota::checker` already re-parses the raw JSON and refuses on error. |
    /// | lifecycle | `get_lifecycle_config` fails closed, so GetBucketLifecycle reports the fault instead of NoSuchLifecycleConfiguration. ILM, scanner and expiry-header consumers still degrade to "no rules": nothing is deleted or moved on the strength of an unreadable rule set, and the bucket keeps serving reads and writes. |
    /// | notification | `get_notification_config` fails closed, so GetBucketNotificationConfiguration reports the fault and startup leaves that one bucket's rules unchanged instead of clearing them; other buckets are unaffected. |
    /// | tagging, CORS, logging, website, accelerate, request payment | The getter fails closed so the matching GET API reports the fault instead of "not configured". Per-request consumers (CORS response headers) still degrade to "not configured", which is the restrictive direction. |
    /// | bucket ACL | Safe to degrade: it only shapes an optional response. |
    ///
    /// Independently of the table, `update_config_with` refuses a
    /// read-modify-write of an unreadable XML config before its mutate step
    /// runs, so the unreadable bytes are never replaced by a rewrite that saw
    /// them as absent. Other configs of the same bucket stay writable.
    ///
    /// Every unreadable XML config found here is counted in
    /// `rustfs_bucket_metadata_parse_failed_total`, reflected in
    /// `rustfs_bucket_metadata_unparsable_current`, and logged at error level.
    pub(super) fn parse_all_configs(&mut self) -> Result<()> {
        if let Err(e) = self.parse_policy_config() {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "policy",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.notification_config_xml.is_empty()
            && let Err(e) = deserialize::<NotificationConfiguration>(&self.notification_config_xml)
                .map(|c| self.notification_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "notification",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.lifecycle_config_xml.is_empty()
            && let Err(e) =
                deserialize::<BucketLifecycleConfiguration>(&self.lifecycle_config_xml).map(|c| self.lifecycle_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "lifecycle",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.object_lock_config_xml.is_empty()
            && let Err(e) =
                deserialize::<ObjectLockConfiguration>(&self.object_lock_config_xml).map(|c| self.object_lock_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "object_lock",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        self.versioning_config = None;
        if !self.versioning_config_xml.is_empty()
            && let Err(e) =
                deserialize::<VersioningConfiguration>(&self.versioning_config_xml).map(|c| self.versioning_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "versioning",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.encryption_config_xml.is_empty()
            && let Err(e) =
                deserialize::<ServerSideEncryptionConfiguration>(&self.encryption_config_xml).map(|c| self.sse_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "encryption",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.tagging_config_xml.is_empty()
            && let Err(e) = deserialize::<Tagging>(&self.tagging_config_xml).map(|c| self.tagging_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "tagging",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.quota_config_json.is_empty()
            && let Err(e) = serde_json::from_slice(&self.quota_config_json).map(|c| self.quota_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "quota",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        self.replication_config = None;
        if !self.replication_config_xml.is_empty()
            && let Err(e) =
                deserialize::<ReplicationConfiguration>(&self.replication_config_xml).map(|c| self.replication_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "replication",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        // A stored targets blob that cannot be decoded must not collapse into
        // the empty target set: that is indistinguishable from "no replication
        // configured", so replication stops and no caller ever sees an error
        // (rustfs/backlog#2282). Leaving the typed field `None` while the raw
        // bytes stay non-empty is the retained parse failure every targets
        // reader keys off; the bytes are preserved so the configuration is
        // still recoverable.
        self.bucket_target_config = None;
        if !self.bucket_targets_config_json.is_empty() {
            match serde_json::from_slice::<BucketTargets>(&self.bucket_targets_config_json) {
                Ok(targets) => self.bucket_target_config = Some(targets),
                Err(e) => tracing::error!(
                    event = "bucket_metadata_parse_failed",
                    component = "ecstore",
                    subsystem = "bucket_metadata",
                    bucket = %self.name,
                    config = "bucket_targets",
                    error = %e,
                    "Bucket replication targets are unreadable; replication for this bucket fails closed"
                ),
            }
        } else {
            self.bucket_target_config = Some(BucketTargets::default());
        }
        if !self.cors_config_xml.is_empty()
            && let Err(e) = deserialize::<CORSConfiguration>(&self.cors_config_xml).map(|c| self.cors_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "cors",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.logging_config_xml.is_empty()
            && let Err(e) = deserialize::<BucketLoggingStatus>(&self.logging_config_xml).map(|c| self.logging_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "logging",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.website_config_xml.is_empty()
            && let Err(e) = deserialize::<WebsiteConfiguration>(&self.website_config_xml).map(|c| self.website_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "website",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.accelerate_config_xml.is_empty()
            && let Err(e) =
                deserialize::<AccelerateConfiguration>(&self.accelerate_config_xml).map(|c| self.accelerate_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "accelerate",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.request_payment_config_xml.is_empty()
            && let Err(e) = deserialize::<RequestPaymentConfiguration>(&self.request_payment_config_xml)
                .map(|c| self.request_payment_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "request_payment",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.public_access_block_config_xml.is_empty()
            && let Err(e) = deserialize::<PublicAccessBlockConfiguration>(&self.public_access_block_config_xml)
                .map(|c| self.public_access_block_config = Some(c))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "public_access_block",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }
        if !self.bucket_acl_config_json.is_empty()
            && let Err(e) = String::from_utf8(self.bucket_acl_config_json.clone()).map(|acl| self.bucket_acl_config = Some(acl))
        {
            tracing::warn!(
                event = "bucket_metadata_parse_failed",
                component = "ecstore",
                subsystem = "bucket_metadata",
                bucket = %self.name,
                config = "bucket_acl",
                error = %e,
                "Failed to parse bucket metadata config"
            );
        }

        if !self.name.is_empty() {
            let unreadable: Vec<(&'static str, usize)> = XML_BUCKET_CONFIG_FILES
                .iter()
                .filter_map(|config| self.xml_config_unreadable_len(config).map(|raw_len| (*config, raw_len)))
                .collect();
            super::config_parse_mode::record_bucket_config_parse_state(&self.name, &unreadable);
        }

        Ok(())
    }
}

pub(crate) async fn load_bucket_incarnation(api: Arc<ECStore>, bucket: &str) -> Result<Option<Uuid>> {
    let path = format!("{BUCKET_META_PREFIX}/{bucket}/{BUCKET_INCARNATION_FILE}");
    let data = match read_config_preserve_empty(api, &path).await {
        Ok(data) => data,
        Err(Error::ConfigNotFound) => return Ok(None),
        Err(err) => return Err(err),
    };
    let incarnation =
        Uuid::from_slice(&data).map_err(|err| Error::other(format!("persisted bucket incarnation is invalid: {err}")))?;
    if incarnation.is_nil() {
        return Err(Error::other("persisted bucket incarnation is nil"));
    }
    Ok(Some(incarnation))
}

pub(crate) async fn save_bucket_incarnation(api: Arc<ECStore>, bucket: &str, incarnation: Uuid) -> Result<()> {
    if incarnation.is_nil() {
        return Err(Error::other("cannot persist a nil bucket incarnation"));
    }
    let path = format!("{BUCKET_META_PREFIX}/{bucket}/{BUCKET_INCARNATION_FILE}");
    save_config(api, &path, incarnation.as_bytes().to_vec()).await
}

pub async fn load_bucket_metadata(api: Arc<ECStore>, bucket: &str) -> Result<BucketMetadata> {
    load_bucket_metadata_parse(api, bucket, true).await
}

pub async fn load_bucket_metadata_parse(api: Arc<ECStore>, bucket: &str, parse: bool) -> Result<BucketMetadata> {
    Ok(load_bucket_metadata_parse_with_presence(api, bucket, parse).await?.0)
}

/// The returned `bool` reports whether the metadata was actually read from
/// persisted storage; `false` means no metadata exists for this bucket on this
/// store and the returned value is a fabricated in-memory default.
pub(crate) async fn load_bucket_metadata_parse_with_presence(
    api: Arc<ECStore>,
    bucket: &str,
    parse: bool,
) -> Result<(BucketMetadata, bool)> {
    let (mut bm, persisted) = match read_bucket_metadata(api.clone(), bucket).await {
        Ok(res) => (res, true),
        Err(err) => {
            if err != Error::ConfigNotFound {
                return Err(err);
            }

            (BucketMetadata::new(bucket), false)
        }
    };

    let incarnation = load_bucket_incarnation(api.clone(), bucket).await?;
    if persisted {
        if bm.name.is_empty() {
            bm.name = bucket.to_string();
        } else if bm.name != bucket {
            return Err(Error::FileCorrupt);
        }
        if let Some(incarnation) = incarnation {
            if !bm.bucket_incarnation_id.is_nil() && bm.bucket_incarnation_id != incarnation {
                return Err(Error::other("bucket incarnation sidecar does not match bucket metadata"));
            }
            bm.bucket_incarnation_id = incarnation;
            bm.bucket_incarnation_sidecar = true;
        } else if !bm.bucket_incarnation_id.is_nil() {
            return Err(Error::other(format!(
                "bucket incarnation sidecar is missing for new-format metadata: {bucket}"
            )));
        }
    }
    // A sidecar without `.metadata.bin` is the window between the two legacy
    // migration writes (rustfs/rustfs#8003): the sidecar lands first, and a
    // crash or lost namespace lease before the metadata write leaves the
    // bucket in this state on every node. Report it as not persisted so the
    // migration runs again; the writers adopt the stored incarnation instead
    // of minting a new one, so the sidecar keeps its authority.

    bm.default_timestamps();

    if parse {
        bm.parse_all_configs()?;
    }

    Ok((bm, persisted))
}

async fn read_bucket_metadata(api: Arc<ECStore>, bucket: &str) -> Result<BucketMetadata> {
    if bucket.is_empty() {
        error!("bucket name empty");
        return Err(Error::other("invalid argument"));
    }

    let bm = BucketMetadata::new(bucket);
    let file_path = bm.save_file_path();

    let data = read_config(api, &file_path).await?;

    BucketMetadata::check_header(&data)?;

    let bm = BucketMetadata::unmarshal(&data[4..])?;

    Ok(bm)
}
fn _write_time<S>(t: &OffsetDateTime, s: S) -> std::result::Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let mut buf = vec![0x0; 15];

    let sec = t.unix_timestamp() - 62135596800;
    let nsec = t.nanosecond();
    buf[0] = 0xc7; // mext8
    buf[1] = 0x0c; // Length
    buf[2] = 0x05; // Time extension type
    BigEndian::write_u64(&mut buf[3..], sec as u64);
    BigEndian::write_u32(&mut buf[11..], nsec);
    s.serialize_bytes(&buf)
}

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

    /// rustfs/backlog#1734: `StorageError::clone` rebuilds I/O errors from
    /// their text. The unreadable-config refusal must stay typed across that
    /// clone, or a cloned error stops mapping to its retryable S3 response.
    #[test]
    fn unreadable_config_refusal_survives_storage_error_clone() {
        let err = unreadable_config_error("b", BUCKET_TAGGING_CONFIG, 7);
        let cloned = err.clone();
        assert!(is_unreadable_config_error(&cloned), "clone lost the typed refusal: {cloned}");
        assert_eq!(unreadable_config_refusal(&cloned).map(|r| r.raw_len), Some(7));
        assert!(is_unreadable_config_error(&err));
    }

    /// Decode a whitespace-tolerant hex fixture into bytes.
    fn decode_hex(s: &str) -> Vec<u8> {
        let s: String = s.chars().filter(|c| !c.is_whitespace()).collect();
        (0..s.len())
            .step_by(2)
            .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("valid hex fixture"))
            .collect()
    }

    /// backlog#580: prove RustFS parses a real MinIO-written bucket `.metadata.bin`
    /// blob without loss. The fixture is the raw `.metadata.bin` object body
    /// (4-byte `format|version` header + msgpack) carved from the
    /// `.minio.sys/buckets/interop/.metadata.bin` object written by MinIO
    /// `RELEASE.2025-07-23` — see `tests/fixtures/minio/README.md`.
    #[test]
    fn parses_real_minio_bucket_metadata_blob_without_loss() {
        let blob = decode_hex(include_str!("../../tests/fixtures/minio/bucket_metadata.blob.hex"));

        // Same 4-byte format|version header (1|1) and msgpack layout as MinIO.
        BucketMetadata::check_header(&blob).expect("valid .metadata.bin header");
        let mut bm = BucketMetadata::unmarshal(&blob[4..]).expect("unmarshal MinIO bucket metadata");
        assert!(
            bm.bucket_incarnation_id.is_nil(),
            "legacy MinIO metadata has no RustFS bucket incarnation field"
        );

        // Raw config fields survive the msgpack decode (PascalCase MinIO field names).
        assert_eq!(bm.name, "interop");
        assert!(!bm.policy_config_json.is_empty(), "policy JSON present");
        assert!(!bm.lifecycle_config_xml.is_empty(), "lifecycle XML present");
        assert!(!bm.object_lock_config_xml.is_empty(), "object-lock XML present");
        assert!(!bm.versioning_config_xml.is_empty(), "versioning XML present");
        assert!(!bm.tagging_config_xml.is_empty(), "tagging XML present");
        assert!(!bm.quota_config_json.is_empty(), "quota JSON present");

        // Typed parse of each stored config must succeed. `parse_all_configs`
        // logs+skips on error, so a None here means a real MinIO-compat parse gap.
        bm.parse_all_configs().expect("parse_all_configs");
        assert!(bm.policy_config.is_some(), "policy parsed");
        assert!(bm.versioning_config.is_some(), "versioning parsed");
        assert!(bm.object_lock_config.is_some(), "object-lock parsed");
        assert!(bm.tagging_config.is_some(), "tagging parsed");
        assert!(bm.quota_config.is_some(), "quota parsed");
        assert!(
            bm.lifecycle_config.is_some(),
            "lifecycle parsed (MinIO writes an <ExpiryUpdatedAt> extension element)"
        );
        assert!(bm.notification_config.is_some(), "notification parsed");
        assert!(bm.sse_config.is_some(), "encryption (SSE) parsed");
        assert!(bm.replication_config.is_some(), "replication parsed");

        // Object lock is expressed through the parsed config, not the legacy
        // `LockEnabled` flag (MinIO leaves that false for config-based locks).
        assert!(bm.object_locking(), "object lock active via parsed config");
    }

    /// backlog#580: KNOWN GAP (flagged 2026-03-06: "inline_data 前缀不同"). RustFS's
    /// inline-data extraction does not yet recover the object body from a
    /// MinIO-written bucket-metadata object: `into_fileinfo(read_data=true).data`
    /// returns bytes that are not the `.metadata.bin` blob (no `format|version`
    /// header). Kept as an ignored, documented reproduction until the MinIO
    /// inline-data framing is handled on the read path.
    /// backlog#580: prove RustFS reads a MinIO-written **inlined** bucket-metadata
    /// object end-to-end. MinIO stores inline data as `[bitrot hash][object body]`
    /// (the "`inline_data` 前缀不同" gap flagged on 2026-03-06 is that
    /// bitrot prefix, not a format incompatibility). Running the raw inline shard
    /// through RustFS's `BitrotReader` with the default `HighwayHash256S` must
    /// verify the checksum and yield the exact `.metadata.bin` blob.
    #[tokio::test]
    async fn reads_minio_inline_bucket_metadata_via_bitrot() {
        use crate::erasure::coding::BitrotReader;
        use rustfs_utils::HashAlgorithm;

        let xlmeta = decode_hex(include_str!("../../tests/fixtures/minio/bucket_metadata_full.xlmeta.hex"));
        let fm = rustfs_filemeta::FileMeta::load(&xlmeta).expect("parse MinIO xl.meta");
        let fi = fm
            .into_fileinfo("interop", ".metadata.bin", "", true, false, false)
            .expect("into_fileinfo");
        // The raw inline shard is `[HighwayHash256 (32B)][object body]`.
        let inline = fi.data.expect("inline shard present");
        let algo = HashAlgorithm::HighwayHash256S;
        let body_len = inline.len() - algo.size();

        let mut reader = BitrotReader::new(std::io::Cursor::new(inline.to_vec()), body_len, algo, false);
        let mut body = vec![0u8; body_len];
        let read = reader.read(&mut body).await.expect("bitrot verify + read MinIO inline shard");
        assert_eq!(read, body_len);

        // The verified body is exactly the `.metadata.bin` blob, and it parses.
        BucketMetadata::check_header(&body).expect("recovered body is a valid .metadata.bin");
        let mut bm = BucketMetadata::unmarshal(&body[4..]).expect("unmarshal recovered blob");
        assert_eq!(bm.name, "interop");
        assert!(!bm.bucket_creation_committed, "legacy metadata must default to uncommitted");
        bm.parse_all_configs().expect("parse recovered configs");
        assert!(bm.lifecycle_config.is_some());
    }

    #[tokio::test]
    async fn marshal_msg() {
        // write_time(OffsetDateTime::UNIX_EPOCH).unwrap();

        let mut bm = BucketMetadata::new("dada");
        bm.bucket_creation_committed = true;

        let buf = bm.marshal_msg().unwrap();

        let new = BucketMetadata::unmarshal(&buf).unwrap();

        assert_eq!(bm.name, new.name);
        assert!(new.bucket_creation_committed);
        assert!(!bm.bucket_incarnation_id.is_nil());
        assert_eq!(bm.bucket_incarnation_id, new.bucket_incarnation_id);
    }

    #[test]
    fn bucket_incarnation_msgpack_rejects_invalid_binary_length() {
        let mut fixture = Vec::new();
        rmp::encode::write_map_len(&mut fixture, 1).unwrap();
        rmp::encode::write_str(&mut fixture, "BucketIncarnationID").unwrap();
        rmp::encode::write_bin(&mut fixture, &[0_u8; 15]).unwrap();

        let err = BucketMetadata::unmarshal(&fixture).expect_err("non-UUID incarnation bytes must fail closed");
        assert!(err.to_string().contains("invalid BucketIncarnationID"));
    }

    #[test]
    fn same_name_bucket_metadata_gets_a_new_incarnation() {
        let old = BucketMetadata::new("recreated");
        let new = BucketMetadata::new("recreated");

        assert!(!old.bucket_incarnation_id.is_nil());
        assert!(!new.bucket_incarnation_id.is_nil());
        assert_ne!(old.bucket_incarnation_id, new.bucket_incarnation_id);
    }

    #[test]
    fn regular_bucket_metadata_constructor_does_not_seed_durability() {
        temp_env::with_var_unset(crate::bucket::durability::ENV_NEW_BUCKET_DURABILITY_MODE, || {
            let metadata = BucketMetadata::new("legacy-or-fabricated");
            assert!(metadata.durability_config_json.is_empty());
            assert!(metadata.durability_config().is_none());
        });
    }

    #[test]
    fn new_bucket_metadata_constructor_inherits_default_durability() {
        temp_env::with_var_unset(crate::bucket::durability::ENV_NEW_BUCKET_DURABILITY_MODE, || {
            let metadata = BucketMetadata::new_with_default_durability("new-user-bucket");
            assert!(metadata.durability_config().is_none());

            let encoded = metadata.marshal_msg().expect("marshal metadata");
            let decoded = BucketMetadata::unmarshal(&encoded).expect("unmarshal metadata");
            assert_eq!(decoded.durability_config_json, metadata.durability_config_json);
            assert!(decoded.durability_config().is_none());
        });
    }

    #[test]
    fn new_bucket_metadata_constructor_can_inherit_global_durability() {
        temp_env::with_var(crate::bucket::durability::ENV_NEW_BUCKET_DURABILITY_MODE, Some("inherit"), || {
            let metadata = BucketMetadata::new_with_default_durability("strict-fleet-new-bucket");
            assert!(metadata.durability_config_json.is_empty());
            assert!(metadata.durability_config().is_none());
        });
    }

    #[test]
    fn site_replication_config_updates_cannot_replace_bucket_incarnation() {
        let mut metadata = BucketMetadata::new("site-replication-update");
        let incarnation = metadata.bucket_incarnation_id;

        metadata
            .update_config(BUCKET_POLICY_CONFIG, br#"{"Version":"2012-10-17","Statement":[]}"#.to_vec())
            .unwrap();
        metadata.update_config(OBJECT_LOCK_CONFIG, Vec::new()).unwrap();

        assert_eq!(metadata.bucket_incarnation_id, incarnation);
    }

    /// backlog#2292: a replicated config is stamped with the source
    /// `updated_at` it was given, not the local clock, while the plain
    /// `update_config` entry keeps stamping the local clock.
    #[test]
    fn update_config_at_stamps_the_given_time_and_update_config_stamps_now() {
        let source_time = OffsetDateTime::now_utc() - time::Duration::hours(3);
        let mut metadata = BucketMetadata::new("source-stamped");

        let stamped = metadata
            .update_config_at(BUCKET_POLICY_CONFIG, br#"{"Version":"2012-10-17","Statement":[]}"#.to_vec(), source_time)
            .unwrap();
        assert_eq!(stamped, source_time);
        assert_eq!(metadata.policy_config_updated_at, source_time);

        let tagging = b"<Tagging><TagSet><Tag><Key>k</Key><Value>v</Value></Tag></TagSet></Tagging>".to_vec();
        let stamped = metadata
            .update_config_at(BUCKET_TAGGING_CONFIG, tagging, source_time)
            .unwrap();
        assert_eq!(stamped, source_time);
        assert_eq!(metadata.tagging_config_updated_at, source_time);

        let before = OffsetDateTime::now_utc();
        let stamped = metadata
            .update_config(BUCKET_POLICY_CONFIG, br#"{"Version":"2012-10-17","Statement":[]}"#.to_vec())
            .unwrap();
        assert!(stamped >= before, "a local edit is stamped with the local clock");
        assert_eq!(metadata.policy_config_updated_at, stamped);
        assert_eq!(
            metadata.tagging_config_updated_at, source_time,
            "restamping one config must not move another config's stamp"
        );
    }

    #[test]
    fn object_locking_requires_lock_metadata_not_plain_versioning() {
        use s3s::dto::ObjectLockEnabled;

        let mut bm = BucketMetadata::new("test-bucket");
        bm.versioning_config = Some(VersioningConfiguration {
            status: Some(s3s::dto::BucketVersioningStatus::from_static("Enabled")),
            ..Default::default()
        });
        assert!(!bm.object_locking());

        bm.object_lock_config = Some(ObjectLockConfiguration {
            object_lock_enabled: Some(ObjectLockEnabled::from_static(ObjectLockEnabled::ENABLED)),
            ..Default::default()
        });
        assert!(bm.object_locking());
    }

    #[test]
    fn parse_all_configs_parses_stored_configs_without_store_dependency() {
        let mut bm = BucketMetadata::new("test-bucket");
        bm.policy_config_json = br#"{"Version":"2012-10-17","Statement":[]}"#.to_vec();
        bm.bucket_targets_config_json =
            br#"{"targets":[{"endpoint":"s3.amazonaws.com","targetbucket":"target-bucket","arn":"arn:aws:s3:::target-bucket"}]}"#
                .to_vec();

        bm.parse_all_configs().unwrap();

        assert!(bm.policy_config.is_some());
        let bucket_targets = bm.bucket_target_config.unwrap();
        assert_eq!(bucket_targets.targets.len(), 1);
        assert_eq!(bucket_targets.targets[0].endpoint, "s3.amazonaws.com");
        assert_eq!(bucket_targets.targets[0].target_bucket, "target-bucket");
    }

    /// rustfs/backlog#2282: a stored targets blob this build cannot decode
    /// must not become the empty target set, and must stay distinguishable
    /// from a bucket that never configured a target.
    #[test]
    fn unreadable_bucket_targets_never_degrade_to_an_empty_target_set() {
        let truncated = br#"{"targets":[{"endpoint":"s3.example.com","#.to_vec();
        let mut corrupt = BucketMetadata::new("corrupt-targets");
        corrupt.bucket_targets_config_json = truncated.clone();

        corrupt
            .parse_all_configs()
            .expect("one unreadable sub-config must not fail the whole metadata load");

        assert!(
            corrupt.bucket_target_config.is_none(),
            "an undecodable targets blob must not produce a target set at all"
        );
        assert!(corrupt.bucket_targets_unreadable());
        assert_eq!(
            corrupt.bucket_targets_config_json, truncated,
            "the raw bytes must survive so the configuration stays recoverable"
        );

        // The genuinely-absent case is unchanged, and the two now diverge.
        let mut absent = BucketMetadata::new("no-targets");
        absent.parse_all_configs().expect("absent targets parse");
        assert!(
            absent.bucket_target_config.as_ref().is_some_and(BucketTargets::is_empty),
            "a bucket that configured no target still reads as an empty target set"
        );
        assert!(!absent.bucket_targets_unreadable());
    }

    /// `Credentials` carries no struct-level `serde(default)`, so one target
    /// missing `secretKey` is a hard parse error for the whole document. That
    /// must surface as "unreadable", never as "no targets configured".
    #[test]
    fn bucket_targets_missing_secret_key_are_unreadable_not_empty() {
        let mut bm = BucketMetadata::new("missing-secret-key");
        bm.bucket_targets_config_json = br#"{"targets":[{"endpoint":"s3.example.com","targetbucket":"remote","arn":"arn:rustfs:replication:us-east-1:src:1","credentials":{"accessKey":"AKIAEXAMPLE"}}]}"#.to_vec();

        bm.parse_all_configs()
            .expect("a rejected targets document must not fail the whole metadata load");

        assert!(
            bm.bucket_targets_unreadable(),
            "a targets document rejected for a missing secretKey is unreadable, not empty"
        );
        assert!(bm.bucket_target_config.is_none());
    }

    /// rustfs/backlog#2309: the MinIO-origin `.metadata.bin` this repository
    /// already carries as a compatibility fixture stores
    /// `BucketTargetsConfigJSON` as a bare JSON array, which `BucketTargets`
    /// (a `{"targets":[…]}` struct with no array fallback) cannot decode. The
    /// bytes below are the exact payload the fixture in
    /// `metadata_test.rs::TEST_BUCKET_METADATA_HEX` decodes to, so if RustFS
    /// ever grows the array-shaped compatibility parse, this test is where the
    /// upgrade break is pinned and where the decision has to be recorded.
    #[test]
    fn minio_array_shaped_bucket_targets_are_unreadable() {
        let minio_array = br#"[{"endpoint":"http://target.example.com","targetBucket":"tb","region":"us-east-1"}]"#.to_vec();
        let mut bm = BucketMetadata::new("minio-array-targets");
        bm.bucket_targets_config_json = minio_array.clone();

        bm.parse_all_configs()
            .expect("a MinIO-shaped targets blob must not fail the whole metadata load");

        assert!(
            bm.bucket_targets_unreadable(),
            "an array-shaped MinIO targets blob is unreadable, not an empty target set"
        );
        assert!(bm.bucket_target_config.is_none());
        assert_eq!(
            bm.bucket_targets_config_json, minio_array,
            "the raw MinIO bytes must survive so the configuration stays recoverable"
        );
    }

    /// The invariant every branch of `parse_all_configs` shares: a stored but
    /// undecodable payload keeps its raw bytes and leaves the typed field
    /// `None`, so no branch fabricates a value. What a reader may then do with
    /// that state is decided per config; see the table on `parse_all_configs`.
    #[test]
    fn every_config_branch_retains_its_parse_failure_instead_of_defaulting() {
        let malformed_xml = b"<not-a-valid-document".to_vec();
        let malformed_json = b"{not-json".to_vec();

        let mut bm = BucketMetadata::new("all-configs-malformed");
        bm.policy_config_json = malformed_json.clone();
        bm.quota_config_json = malformed_json.clone();
        bm.bucket_targets_config_json = malformed_json.clone();
        bm.notification_config_xml = malformed_xml.clone();
        bm.lifecycle_config_xml = malformed_xml.clone();
        bm.object_lock_config_xml = malformed_xml.clone();
        bm.versioning_config_xml = malformed_xml.clone();
        bm.encryption_config_xml = malformed_xml.clone();
        bm.tagging_config_xml = malformed_xml.clone();
        bm.replication_config_xml = malformed_xml.clone();
        bm.cors_config_xml = malformed_xml.clone();
        bm.logging_config_xml = malformed_xml.clone();
        bm.website_config_xml = malformed_xml.clone();
        bm.accelerate_config_xml = malformed_xml.clone();
        bm.request_payment_config_xml = malformed_xml.clone();
        bm.public_access_block_config_xml = malformed_xml.clone();
        // `bucket_acl_config_json` is only checked for UTF-8, so only invalid
        // UTF-8 exercises its failure branch.
        bm.bucket_acl_config_json = vec![0xff, 0xfe];

        bm.parse_all_configs()
            .expect("a bucket whose every config is corrupt must still load its metadata");

        let cleared: [(&str, bool); 17] = [
            ("policy", bm.policy_config.is_none()),
            ("quota", bm.quota_config.is_none()),
            ("bucket_targets", bm.bucket_target_config.is_none()),
            ("notification", bm.notification_config.is_none()),
            ("lifecycle", bm.lifecycle_config.is_none()),
            ("object_lock", bm.object_lock_config.is_none()),
            ("versioning", bm.versioning_config.is_none()),
            ("encryption", bm.sse_config.is_none()),
            ("tagging", bm.tagging_config.is_none()),
            ("replication", bm.replication_config.is_none()),
            ("cors", bm.cors_config.is_none()),
            ("logging", bm.logging_config.is_none()),
            ("website", bm.website_config.is_none()),
            ("accelerate", bm.accelerate_config.is_none()),
            ("request_payment", bm.request_payment_config.is_none()),
            ("public_access_block", bm.public_access_block_config.is_none()),
            ("bucket_acl", bm.bucket_acl_config.is_none()),
        ];
        for (config, is_cleared) in cleared {
            assert!(is_cleared, "{config}: a corrupt payload must not be replaced by a default");
        }

        assert_eq!(bm.bucket_targets_config_json, malformed_json, "raw bytes are retained");
        assert_eq!(bm.lifecycle_config_xml, malformed_xml, "raw bytes are retained");
    }

    #[test]
    fn lifecycle_update_config_clears_parsed_config_on_delete() {
        let mut bm = BucketMetadata::new("test-bucket");
        let lifecycle_xml = br#"<LifecycleConfiguration><Rule><ID>rule1</ID><Status>Enabled</Status><Expiration><Days>30</Days></Expiration></Rule></LifecycleConfiguration>"#;

        bm.update_config(BUCKET_LIFECYCLE_CONFIG, lifecycle_xml.to_vec())
            .expect("lifecycle config should update");
        bm.parse_all_configs().expect("lifecycle config should parse");
        assert!(bm.lifecycle_config.is_some());

        bm.update_config(BUCKET_LIFECYCLE_CONFIG, Vec::new())
            .expect("lifecycle config delete should update metadata");

        assert!(bm.lifecycle_config_xml.is_empty());
        assert!(bm.lifecycle_config.is_none());
    }

    /// Companion to the lifecycle case above. `parse_all_configs` skips empty
    /// XML rather than clearing, so without the explicit reset a cleared
    /// tagging config would keep serving the previously parsed tags.
    #[test]
    fn tagging_update_config_clears_parsed_config_on_delete() {
        let mut bm = BucketMetadata::new("test-bucket");
        let tagging_xml = br#"<Tagging><TagSet><Tag><Key>env</Key><Value>prod</Value></Tag></TagSet></Tagging>"#;

        bm.update_config(BUCKET_TAGGING_CONFIG, tagging_xml.to_vec())
            .expect("tagging config should update");
        bm.parse_all_configs().expect("tagging config should parse");
        assert!(bm.tagging_config.is_some());

        bm.update_config(BUCKET_TAGGING_CONFIG, Vec::new())
            .expect("tagging config delete should update metadata");

        assert!(bm.tagging_config_xml.is_empty());
        assert!(bm.tagging_config.is_none());

        // A re-parse must not resurrect them either.
        bm.parse_all_configs().expect("cleared tagging should parse");
        assert!(bm.tagging_config.is_none());
    }

    #[test]
    fn delete_admission_configs_update_parsed_state_atomically() {
        let mut bm = BucketMetadata::new("test-bucket");
        let versioning_xml = b"<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>";
        let replication_xml = b"<ReplicationConfiguration><Role>arn:aws:s3:::target-bucket</Role><Rule><ID>rule1</ID><Status>Enabled</Status><Prefix></Prefix><Destination><Bucket>arn:aws:s3:::target-bucket</Bucket></Destination></Rule></ReplicationConfiguration>";

        bm.update_config(BUCKET_VERSIONING_CONFIG, versioning_xml.to_vec())
            .expect("valid versioning config should update parsed state");
        bm.update_config(BUCKET_REPLICATION_CONFIG, replication_xml.to_vec())
            .expect("valid replication config should update parsed state");

        assert!(bm.versioning_config.as_ref().is_some_and(VersioningConfiguration::enabled));
        assert_eq!(
            bm.replication_config.as_ref().map(|config| config.role.as_str()),
            Some("arn:aws:s3:::target-bucket")
        );

        assert!(
            bm.update_config(BUCKET_VERSIONING_CONFIG, b"<VersioningConfiguration>".to_vec())
                .is_err()
        );
        assert!(
            bm.update_config(BUCKET_REPLICATION_CONFIG, b"<ReplicationConfiguration>".to_vec())
                .is_err()
        );

        assert_eq!(bm.versioning_config_xml, versioning_xml);
        assert_eq!(bm.replication_config_xml, replication_xml);
        assert!(bm.versioning_config.as_ref().is_some_and(VersioningConfiguration::enabled));
        assert_eq!(
            bm.replication_config.as_ref().map(|config| config.role.as_str()),
            Some("arn:aws:s3:::target-bucket")
        );

        assert!(
            bm.update_config(
                BUCKET_VERSIONING_CONFIG,
                b"<VersioningConfiguration><Status>Enabld</Status></VersioningConfiguration>".to_vec(),
            )
            .is_err()
        );
        assert!(
            bm.update_config(
                BUCKET_REPLICATION_CONFIG,
                b"<ReplicationConfiguration><Role>arn:aws:s3:::target-bucket</Role><Rule><ID>rule1</ID><Status>Enabld</Status><Prefix></Prefix><Destination><Bucket>arn:aws:s3:::target-bucket</Bucket></Destination></Rule></ReplicationConfiguration>".to_vec(),
            )
            .is_err()
        );
        assert_eq!(bm.versioning_config_xml, versioning_xml);
        assert_eq!(bm.replication_config_xml, replication_xml);

        bm.versioning_config_xml = b"<VersioningConfiguration>".to_vec();
        bm.replication_config_xml = b"<ReplicationConfiguration>".to_vec();
        bm.parse_all_configs()
            .expect("bulk config parsing reports malformed fields through cleared typed state");

        assert!(bm.versioning_config.is_none());
        assert!(bm.replication_config.is_none());
    }

    #[tokio::test]
    async fn marshal_msg_complete_example() {
        // Create a complete BucketMetadata with various configurations
        let mut bm = BucketMetadata::new("test-bucket");

        // Set creation time to current time
        bm.created = OffsetDateTime::now_utc();
        bm.lock_enabled = true;

        // Add policy configuration
        let policy_json = r#"{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::test-bucket/*"}]}"#;
        bm.policy_config_json = policy_json.as_bytes().to_vec();
        bm.policy_config_updated_at = OffsetDateTime::now_utc();

        // Add lifecycle configuration
        let lifecycle_xml = r#"<LifecycleConfiguration><Rule><ID>rule1</ID><Status>Enabled</Status><Expiration><Days>30</Days></Expiration></Rule></LifecycleConfiguration>"#;
        bm.lifecycle_config_xml = lifecycle_xml.as_bytes().to_vec();
        bm.lifecycle_config_updated_at = OffsetDateTime::now_utc();

        // Add versioning configuration
        let versioning_xml = r#"<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>"#;
        bm.versioning_config_xml = versioning_xml.as_bytes().to_vec();
        bm.versioning_config_updated_at = OffsetDateTime::now_utc();

        // Add encryption configuration
        let encryption_xml = r#"<ServerSideEncryptionConfiguration><Rule><ApplyServerSideEncryptionByDefault><SSEAlgorithm>AES256</SSEAlgorithm></ApplyServerSideEncryptionByDefault></Rule></ServerSideEncryptionConfiguration>"#;
        bm.encryption_config_xml = encryption_xml.as_bytes().to_vec();
        bm.encryption_config_updated_at = OffsetDateTime::now_utc();

        // Add tagging configuration
        let tagging_xml = r#"<Tagging><TagSet><Tag><Key>Environment</Key><Value>Test</Value></Tag><Tag><Key>Owner</Key><Value>RustFS</Value></Tag></TagSet></Tagging>"#;
        bm.tagging_config_xml = tagging_xml.as_bytes().to_vec();
        bm.tagging_config_updated_at = OffsetDateTime::now_utc();

        // Add quota configuration
        let quota_json =
            r#"{"quota":1073741824,"quota_type":"Hard","created_at":"2024-01-01T00:00:00Z","updated_at":"2024-01-01T00:00:00Z"}"#; // 1GB quota
        bm.quota_config_json = quota_json.as_bytes().to_vec();
        bm.quota_config_updated_at = OffsetDateTime::now_utc();

        // Add object lock configuration
        let object_lock_xml = r#"<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled><Rule><DefaultRetention><Mode>GOVERNANCE</Mode><Days>7</Days></DefaultRetention></Rule></ObjectLockConfiguration>"#;
        bm.object_lock_config_xml = object_lock_xml.as_bytes().to_vec();
        bm.object_lock_config_updated_at = OffsetDateTime::now_utc();

        // Add notification configuration
        let notification_xml = r#"<NotificationConfiguration><CloudWatchConfiguration><Id>notification1</Id><Event>s3:ObjectCreated:*</Event><CloudWatchConfiguration><LogGroupName>test-log-group</LogGroupName></CloudWatchConfiguration></CloudWatchConfiguration></NotificationConfiguration>"#;
        bm.notification_config_xml = notification_xml.as_bytes().to_vec();
        bm.notification_config_updated_at = OffsetDateTime::now_utc();

        // Add replication configuration
        let replication_xml = r#"<ReplicationConfiguration><Role>arn:aws:iam::123456789012:role/replication-role</Role><Rule><ID>rule1</ID><Status>Enabled</Status><Prefix>documents/</Prefix><Destination><Bucket>arn:aws:s3:::destination-bucket</Bucket></Destination></Rule></ReplicationConfiguration>"#;
        bm.replication_config_xml = replication_xml.as_bytes().to_vec();
        bm.replication_config_updated_at = OffsetDateTime::now_utc();

        // Add bucket targets configuration
        let bucket_targets_json = r#"[{"endpoint":"http://target1.example.com","credentials":{"accessKey":"key1","secretKey":"secret1"},"targetBucket":"target-bucket-1","region":"us-east-1"},{"endpoint":"http://target2.example.com","credentials":{"accessKey":"key2","secretKey":"secret2"},"targetBucket":"target-bucket-2","region":"us-west-2"}]"#;
        bm.bucket_targets_config_json = bucket_targets_json.as_bytes().to_vec();
        bm.bucket_targets_config_updated_at = OffsetDateTime::now_utc();

        // Add bucket targets meta configuration
        let bucket_targets_meta_json = r#"{"replicationId":"repl-123","syncMode":"async","bandwidth":"100MB"}"#;
        bm.bucket_targets_config_meta_json = bucket_targets_meta_json.as_bytes().to_vec();
        bm.bucket_targets_config_meta_updated_at = OffsetDateTime::now_utc();

        // Add public access block configuration
        let public_access_block_xml = r#"<PublicAccessBlockConfiguration><BlockPublicAcls>true</BlockPublicAcls><IgnorePublicAcls>true</IgnorePublicAcls><BlockPublicPolicy>true</BlockPublicPolicy><RestrictPublicBuckets>false</RestrictPublicBuckets></PublicAccessBlockConfiguration>"#;
        bm.public_access_block_config_xml = public_access_block_xml.as_bytes().to_vec();
        bm.public_access_block_config_updated_at = OffsetDateTime::now_utc();

        let bucket_acl = r#"{"owner":{"id":"rustfsadmin","display_name":"RustFS Tester"},"grants":[{"grantee":{"grantee_type":"CanonicalUser","id":"rustfsadmin","display_name":"RustFS Tester","uri":null,"email_address":null},"permission":"FULL_CONTROL"}]}"#;
        bm.bucket_acl_config_json = bucket_acl.as_bytes().to_vec();
        bm.bucket_acl_config_updated_at = OffsetDateTime::now_utc();

        let table_bucket_marker = r#"{"enabled":true}"#;
        bm.table_bucket_config_json = table_bucket_marker.as_bytes().to_vec();
        bm.table_bucket_config_updated_at = OffsetDateTime::now_utc();

        // Test serialization
        let buf = bm.marshal_msg().unwrap();
        assert!(!buf.is_empty(), "Serialized buffer should not be empty");

        // Test deserialization
        let deserialized_bm = BucketMetadata::unmarshal(&buf).unwrap();

        // Verify all fields are correctly serialized and deserialized
        assert_eq!(bm.name, deserialized_bm.name);
        assert_eq!(bm.created.unix_timestamp(), deserialized_bm.created.unix_timestamp());
        assert_eq!(bm.lock_enabled, deserialized_bm.lock_enabled);

        // Verify configuration data
        assert_eq!(bm.policy_config_json, deserialized_bm.policy_config_json);
        assert_eq!(bm.lifecycle_config_xml, deserialized_bm.lifecycle_config_xml);
        assert_eq!(bm.versioning_config_xml, deserialized_bm.versioning_config_xml);
        assert_eq!(bm.encryption_config_xml, deserialized_bm.encryption_config_xml);
        assert_eq!(bm.tagging_config_xml, deserialized_bm.tagging_config_xml);
        assert_eq!(bm.quota_config_json, deserialized_bm.quota_config_json);
        assert_eq!(bm.public_access_block_config_xml, deserialized_bm.public_access_block_config_xml);
        assert_eq!(bm.bucket_acl_config_json, deserialized_bm.bucket_acl_config_json);
        assert_eq!(bm.table_bucket_config_json, deserialized_bm.table_bucket_config_json);
        assert_eq!(bm.object_lock_config_xml, deserialized_bm.object_lock_config_xml);
        assert_eq!(bm.notification_config_xml, deserialized_bm.notification_config_xml);
        assert_eq!(bm.replication_config_xml, deserialized_bm.replication_config_xml);
        assert_eq!(bm.bucket_targets_config_json, deserialized_bm.bucket_targets_config_json);
        assert_eq!(bm.bucket_targets_config_meta_json, deserialized_bm.bucket_targets_config_meta_json);

        // Verify timestamps (comparing unix timestamps to avoid precision issues)
        assert_eq!(
            bm.policy_config_updated_at.unix_timestamp(),
            deserialized_bm.policy_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.lifecycle_config_updated_at.unix_timestamp(),
            deserialized_bm.lifecycle_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.versioning_config_updated_at.unix_timestamp(),
            deserialized_bm.versioning_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.encryption_config_updated_at.unix_timestamp(),
            deserialized_bm.encryption_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.tagging_config_updated_at.unix_timestamp(),
            deserialized_bm.tagging_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.quota_config_updated_at.unix_timestamp(),
            deserialized_bm.quota_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.object_lock_config_updated_at.unix_timestamp(),
            deserialized_bm.object_lock_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.notification_config_updated_at.unix_timestamp(),
            deserialized_bm.notification_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.replication_config_updated_at.unix_timestamp(),
            deserialized_bm.replication_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.bucket_targets_config_updated_at.unix_timestamp(),
            deserialized_bm.bucket_targets_config_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.bucket_targets_config_meta_updated_at.unix_timestamp(),
            deserialized_bm.bucket_targets_config_meta_updated_at.unix_timestamp()
        );
        assert_eq!(
            bm.table_bucket_config_updated_at.unix_timestamp(),
            deserialized_bm.table_bucket_config_updated_at.unix_timestamp()
        );
        assert!(deserialized_bm.table_bucket_enabled());

        // Test that the serialized data contains expected content
        let buf_str = String::from_utf8_lossy(&buf);
        assert!(buf_str.contains("test-bucket"), "Serialized data should contain bucket name");

        // Verify the buffer size is reasonable (should be larger due to all the config data)
        assert!(buf.len() > 1000, "Buffer should be substantial in size due to all configurations");

        println!("✅ Complete BucketMetadata serialization test passed");
        println!("   - Bucket name: {}", deserialized_bm.name);
        println!("   - Lock enabled: {}", deserialized_bm.lock_enabled);
        println!("   - Policy config size: {} bytes", deserialized_bm.policy_config_json.len());
        println!("   - Lifecycle config size: {} bytes", deserialized_bm.lifecycle_config_xml.len());
        println!("   - Serialized buffer size: {} bytes", buf.len());
    }

    #[test]
    fn table_bucket_marker_tracks_config_presence() {
        let mut bm = BucketMetadata::new("table-bucket");
        assert!(!bm.table_bucket_enabled());

        bm.update_config(BUCKET_TABLE_CONFIG, br#"{"enabled":true}"#.to_vec())
            .unwrap();
        assert!(bm.table_bucket_enabled());
        assert!(!bm.table_bucket_config_json.is_empty());

        bm.update_config(BUCKET_TABLE_CONFIG, Vec::new()).unwrap();
        assert!(!bm.table_bucket_enabled());
    }

    const ODM_JSON: &[u8] = br#"{"version":1,"enabled":true,"source":{"provider":"minio","endpoint":"https://legacy.example.com:9000","region":"auto","bucket":"legacy-bucket","credentials":{"access_key":"AK","secret_key":"SK"}}}"#;

    /// The metadata codec preserves application-owned bytes and timestamps.
    #[test]
    fn on_demand_migration_config_round_trips_and_tracks_updates() {
        let mut bm = BucketMetadata::new("odm-bucket");
        assert_eq!(bm.on_demand_migration_config(), None, "fresh metadata carries no config");
        bm.update_config(BUCKET_ON_DEMAND_MIGRATION_CONFIG, ODM_JSON.to_vec())
            .expect("opaque config is accepted");
        let stamped = bm.on_demand_migration_config_updated_at;
        assert_ne!(stamped, OffsetDateTime::UNIX_EPOCH);
        assert_eq!(bm.on_demand_migration_config(), Some((ODM_JSON, stamped)));
        let back = BucketMetadata::unmarshal(&bm.marshal_msg().unwrap()).unwrap();
        assert_eq!(back.on_demand_migration_config_json, bm.on_demand_migration_config_json);
        assert_eq!(back.on_demand_migration_config_updated_at.unix_timestamp(), stamped.unix_timestamp());
        bm.update_config(BUCKET_ON_DEMAND_MIGRATION_CONFIG, Vec::new()).unwrap();
        assert!(bm.on_demand_migration_config_json.is_empty());
        assert_eq!(bm.on_demand_migration_config(), None);
        assert!(bm.on_demand_migration_config_updated_at >= stamped);
        bm.update_config(BUCKET_ON_DEMAND_MIGRATION_CONFIG, b"not-json".to_vec())
            .unwrap();
        let back = BucketMetadata::unmarshal(&bm.marshal_msg().unwrap()).unwrap();
        assert_eq!(
            back.on_demand_migration_config_json, b"not-json",
            "metadata must not reinterpret application bytes"
        );
    }

    /// rustfs/backlog#2148: a `.metadata.bin` written before the on-demand
    /// migration keys existed decodes with an empty blob and an epoch
    /// timestamp that `default_timestamps` back-fills from `created`.
    #[test]
    fn on_demand_migration_config_absent_in_legacy_blob_defaults_to_created() {
        let blob = decode_hex(include_str!("../../tests/fixtures/minio/bucket_metadata.blob.hex"));
        let mut bm = BucketMetadata::unmarshal(&blob[4..]).expect("unmarshal MinIO bucket metadata");
        assert!(bm.on_demand_migration_config_json.is_empty());
        assert_eq!(bm.on_demand_migration_config_updated_at, OffsetDateTime::UNIX_EPOCH);
        assert_eq!(bm.on_demand_migration_config(), None);

        bm.default_timestamps();
        assert_ne!(bm.created, OffsetDateTime::UNIX_EPOCH, "fixture must carry a real creation time");
        assert_eq!(bm.on_demand_migration_config_updated_at, bm.created);

        // A metadata blob from this build with no config set stays
        // indistinguishable from the legacy one for these fields.
        let fresh = BucketMetadata::unmarshal(&BucketMetadata::new("fresh").marshal_msg().unwrap()).unwrap();
        assert!(fresh.on_demand_migration_config_json.is_empty());
        assert_eq!(fresh.on_demand_migration_config_updated_at, OffsetDateTime::UNIX_EPOCH);
    }

    /// rustfs/backlog#2148: a reader that predates the two on-demand
    /// migration keys takes `decode_from`'s unknown-field branch, which is
    /// `skip_msgp_value`. Walk the new-format blob with exactly that
    /// primitive and prove both keys are skipped without desynchronising the
    /// stream, so the fields that follow them still decode.
    #[test]
    fn old_decoder_skips_on_demand_migration_fields_without_desync() {
        let mut bm = BucketMetadata::new("odm-skip");
        bm.update_config(BUCKET_ON_DEMAND_MIGRATION_CONFIG, ODM_JSON.to_vec())
            .unwrap();
        bm.update_config(BUCKET_DURABILITY_CONFIG, br#"{"mode":"relaxed"}"#.to_vec())
            .unwrap();
        let buf = bm.marshal_msg().unwrap();

        let mut rd = std::io::Cursor::new(buf.as_slice());
        let fields = rmp::decode::read_map_len(&mut rd).unwrap();
        let mut skipped = Vec::new();
        let mut durability_json = Vec::new();
        for _ in 0..fields {
            let key_len = rmp::decode::read_str_len(&mut rd).unwrap();
            let mut key = vec![0u8; key_len as usize];
            rd.read_exact(&mut key).unwrap();
            let key = String::from_utf8(key).unwrap();
            match key.as_str() {
                // The field an old reader knows that is encoded *after* the
                // unknown JSON key and *before* the unknown timestamp key.
                "DurabilityConfigJSON" => durability_json = read_msgp_bin(&mut rd).unwrap(),
                other => {
                    if other.starts_with("OnDemandMigration") {
                        skipped.push(other.to_string());
                    }
                    skip_msgp_value(&mut rd).unwrap();
                }
            }
        }
        assert_eq!(skipped, ["OnDemandMigrationConfigJSON", "OnDemandMigrationConfigUpdatedAt"]);
        assert_eq!(durability_json, br#"{"mode":"relaxed"}"#);
        assert_eq!(rd.position() as usize, buf.len(), "old-style walk must consume the blob exactly");
    }

    /// HP-5b (rustfs/backlog#938): the durability override is a RustFS
    /// extension entry and must survive an encode/decode round trip.
    #[test]
    fn durability_config_round_trips_and_tracks_updates() {
        let mut bm = BucketMetadata::new("durability-bucket");
        assert!(bm.durability_config().is_none(), "fresh metadata carries no override");

        bm.update_config(BUCKET_DURABILITY_CONFIG, br#"{"mode":"relaxed"}"#.to_vec())
            .unwrap();
        assert_ne!(bm.durability_config_updated_at, OffsetDateTime::UNIX_EPOCH);

        let buf = bm.marshal_msg().unwrap();
        let back = BucketMetadata::unmarshal(&buf).unwrap();
        assert_eq!(back.durability_config_json, bm.durability_config_json);
        assert_eq!(
            back.durability_config_updated_at.unix_timestamp(),
            bm.durability_config_updated_at.unix_timestamp()
        );
        assert_eq!(back.durability_config().and_then(|c| c.normalized_mode()).as_deref(), Some("relaxed"));

        // Clearing the entry removes the override.
        bm.update_config(BUCKET_DURABILITY_CONFIG, Vec::new()).unwrap();
        assert!(bm.durability_config().is_none());

        // Corrupted payloads must degrade to "no override", never to a tier.
        bm.durability_config_json = b"not-json".to_vec();
        assert!(bm.durability_config().is_none());
    }

    /// After policy deletion (policy_config_json cleared), parse_policy_config sets policy_config to None.
    #[test]
    fn test_parse_policy_config_clears_cache_when_json_empty() {
        let policy_json = r#"{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::b/*"}]}"#;
        let mut bm = BucketMetadata::new("b");
        bm.policy_config_json = policy_json.as_bytes().to_vec();
        bm.parse_policy_config().unwrap();
        assert!(bm.policy_config.is_some(), "policy_config should be set when JSON non-empty");

        bm.policy_config_json.clear();
        bm.parse_policy_config().unwrap();
        assert!(
            bm.policy_config.is_none(),
            "policy_config should be None after JSON cleared (e.g. policy deleted)"
        );
    }
}