composefs 0.7.0

Rust library for the composefs filesystem
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
//! Split Stream file format implementation.
//!
//! This module implements the Split Stream format for efficiently storing
//! and transferring data with inline content and external object references,
//! supporting compression and content deduplication.

/* Implementation of the Split Stream file format
 *
 * NB: This format is documented in `doc/splitstream.md`.  Please keep the docs up to date!!
 */

use std::{
    collections::{BTreeMap, HashMap},
    fs::File,
    hash::Hash,
    io::{BufRead, BufReader, Read, Seek, SeekFrom, Take, Write},
    mem::MaybeUninit,
    mem::size_of,
    ops::Range,
    sync::Arc,
};

use anyhow::{Context, Error, Result, bail, ensure};
use fn_error_context::context;
use rustix::{
    buffer::spare_capacity,
    io::{pread, read},
};
use tokio::task::JoinHandle;

use crate::repository::ObjectStoreMethod;
use zerocopy::{
    FromBytes, Immutable, IntoBytes, KnownLayout,
    little_endian::{I64, U16, U64},
};
use zstd::stream::{read::Decoder, write::Encoder};

use crate::{
    fsverity::FsVerityHashValue,
    repository::{Repository, WritableRepo},
    util::read_exactish,
};

const SPLITSTREAM_MAGIC: [u8; 11] = *b"SplitStream";
const LG_BLOCKSIZE: u8 = 12; // TODO: hard-coded 4k.  make this generic later...

// Nearly everything in the file is located at an offset indicated by a FileRange.
#[repr(C)]
#[derive(Debug, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout)]
struct FileRange {
    start: U64,
    end: U64,
}

// The only exception is the header: it is a fixed sized and comes at the start (offset 0).
#[repr(C)]
#[derive(Debug, FromBytes, Immutable, IntoBytes, KnownLayout)]
struct SplitstreamHeader {
    pub magic: [u8; 11],  // Contains SPLITSTREAM_MAGIC
    pub version: u8,      // must always be 0
    pub _flags: U16,      // is currently always 0 (but ignored)
    pub algorithm: u8,    // kernel fs-verity algorithm identifier (1 = sha256, 2 = sha512)
    pub lg_blocksize: u8, // log2 of the fs-verity block size (12 = 4k, 16 = 64k)
    pub info: FileRange,  // can be used to expand/move the info section in the future
}

// The info block can be located anywhere, indicated by the "info" FileRange in the header.
#[repr(C)]
#[derive(Debug, FromBytes, Immutable, IntoBytes, KnownLayout)]
struct SplitstreamInfo {
    pub stream_refs: FileRange, // location of the stream references array
    pub object_refs: FileRange, // location of the object references array
    pub stream: FileRange,      // location of the zstd-compressed stream within the file
    pub named_refs: FileRange,  // location of the compressed named references
    pub content_type: U64,      // user can put whatever magic identifier they want there
    pub stream_size: U64,       // total uncompressed size of inline chunks and external chunks
}

/// Old layout of SplitstreamHeader, without `#[repr(C)]`.
/// The Rust compiler reorders fields for alignment — the const asserts below
/// verify the resulting layout matches what bootc <= 1.15.x actually wrote.
#[derive(Debug, FromBytes, Immutable, IntoBytes, KnownLayout)]
struct OldSplitstreamHeader {
    pub magic: [u8; 11],
    pub version: u8,
    pub _flags: U16,
    pub algorithm: u8,
    pub lg_blocksize: u8,
    pub info: FileRange,
}

/// Old layout of SplitstreamInfo, without `#[repr(C)]`.
#[derive(Debug, FromBytes, Immutable, IntoBytes, KnownLayout)]
struct OldSplitstreamInfo {
    pub stream_refs: FileRange,
    pub object_refs: FileRange,
    pub stream: FileRange,
    pub named_refs: FileRange,
    pub content_type: U64,
    pub stream_size: U64,
}

// FileRange: both old and new layouts should be identical (two U64 fields, uniform alignment)
const _: () = {
    assert!(std::mem::offset_of!(FileRange, start) == 0);
    assert!(std::mem::offset_of!(FileRange, end) == 8);
    assert!(std::mem::size_of::<FileRange>() == 16);
};

// SplitstreamHeader: verify old layout DIFFERS from new layout
const _: () = {
    // New (repr(C)) layout: magic at 0, info at 16
    assert!(std::mem::offset_of!(SplitstreamHeader, magic) == 0);
    assert!(std::mem::offset_of!(SplitstreamHeader, info) == 16);
    assert!(std::mem::size_of::<SplitstreamHeader>() == 32);

    // Old (no repr(C)) layout: info at 0, magic at 18
    assert!(std::mem::offset_of!(OldSplitstreamHeader, info) == 0);
    assert!(std::mem::offset_of!(OldSplitstreamHeader, _flags) == 16);
    assert!(std::mem::offset_of!(OldSplitstreamHeader, magic) == 18);
    assert!(std::mem::offset_of!(OldSplitstreamHeader, version) == 29);
    assert!(std::mem::offset_of!(OldSplitstreamHeader, algorithm) == 30);
    assert!(std::mem::offset_of!(OldSplitstreamHeader, lg_blocksize) == 31);
    assert!(std::mem::size_of::<OldSplitstreamHeader>() == 32);
};

// SplitstreamInfo: verify old and new layouts are IDENTICAL
// (all fields are 8-byte aligned, no reason for compiler to reorder)
const _: () = {
    assert!(
        std::mem::offset_of!(SplitstreamInfo, stream_refs)
            == std::mem::offset_of!(OldSplitstreamInfo, stream_refs)
    );
    assert!(
        std::mem::offset_of!(SplitstreamInfo, object_refs)
            == std::mem::offset_of!(OldSplitstreamInfo, object_refs)
    );
    assert!(
        std::mem::offset_of!(SplitstreamInfo, stream)
            == std::mem::offset_of!(OldSplitstreamInfo, stream)
    );
    assert!(
        std::mem::offset_of!(SplitstreamInfo, named_refs)
            == std::mem::offset_of!(OldSplitstreamInfo, named_refs)
    );
    assert!(
        std::mem::offset_of!(SplitstreamInfo, content_type)
            == std::mem::offset_of!(OldSplitstreamInfo, content_type)
    );
    assert!(
        std::mem::offset_of!(SplitstreamInfo, stream_size)
            == std::mem::offset_of!(OldSplitstreamInfo, stream_size)
    );
    assert!(std::mem::size_of::<SplitstreamInfo>() == std::mem::size_of::<OldSplitstreamInfo>());
};

impl From<OldSplitstreamHeader> for SplitstreamHeader {
    fn from(old: OldSplitstreamHeader) -> Self {
        Self {
            magic: old.magic,
            version: old.version,
            _flags: old._flags,
            algorithm: old.algorithm,
            lg_blocksize: old.lg_blocksize,
            info: old.info,
        }
    }
}

impl FileRange {
    fn len(&self) -> Result<u64> {
        self.end
            .get()
            .checked_sub(self.start.get())
            .context("Negative-sized range in splitstream")
    }
}

impl From<Range<u64>> for FileRange {
    fn from(value: Range<u64>) -> Self {
        Self {
            start: U64::from(value.start),
            end: U64::from(value.end),
        }
    }
}

#[context("Reading range from splitstream file")]
fn read_range(file: &mut File, range: FileRange) -> Result<Vec<u8>> {
    let size: usize = (range.len()?.try_into())
        .context("Unable to allocate buffer for implausibly large splitstream section")?;
    let mut buffer = Vec::with_capacity(size);
    if size > 0 {
        pread(file, spare_capacity(&mut buffer), range.start.get())
            .context("Unable to read section from splitstream file")?;
    }
    ensure!(
        buffer.len() == size,
        "Incomplete read from splitstream file"
    );
    Ok(buffer)
}

/// An array of objects with the following properties:
///   - each item appears only once
///   - efficient insertion and lookup of indexes of existing items
///   - insertion order is maintained, indexes are stable across modification
///   - can do .as_bytes() for items that are IntoBytes + Immutable
struct UniqueVec<T: Clone + Hash + Eq> {
    items: Vec<T>,
    index: HashMap<T, usize>,
}

impl<T: Clone + Hash + Eq + std::fmt::Debug> std::fmt::Debug for UniqueVec<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("UniqueVec")
            .field("items", &self.items)
            .field("index", &self.index)
            .finish()
    }
}

impl<T: Clone + Hash + Eq + IntoBytes + Immutable> UniqueVec<T> {
    fn as_bytes(&self) -> &[u8] {
        self.items.as_bytes()
    }
}

impl<T: Clone + Hash + Eq> UniqueVec<T> {
    fn new() -> Self {
        Self {
            items: Vec::new(),
            index: HashMap::new(),
        }
    }

    fn get(&self, item: &T) -> Option<usize> {
        self.index.get(item).copied()
    }

    fn ensure(&mut self, item: &T) -> usize {
        self.get(item).unwrap_or_else(|| {
            let idx = self.items.len();
            self.index.insert(item.clone(), idx);
            self.items.push(item.clone());
            idx
        })
    }

    /// Get an item by its index.
    fn get_by_index(&self, idx: usize) -> Option<&T> {
        self.items.get(idx)
    }
}

/// Statistics from finalizing a [`SplitStreamBuilder`].
#[derive(Debug, Clone, Default)]
pub struct SplitStreamStats {
    /// Total bytes of inline data written to the stream.
    pub inline_bytes: u64,
    /// Per-external-object (size, method) pairs describing how each was stored.
    pub external_objects: Vec<(u64, ObjectStoreMethod)>,
}

/// An entry in the split stream being built.
///
/// Used by `SplitStreamBuilder` to collect entries before serialization.
pub enum SplitStreamEntry<ObjectID: FsVerityHashValue> {
    /// Inline data (headers, small files, padding)
    Inline(Vec<u8>),
    /// External reference - will be resolved to ObjectID when the handle completes
    External {
        /// Background task that will return the ObjectID and how it was stored
        handle: JoinHandle<Result<(ObjectID, ObjectStoreMethod)>>,
        /// Size of the external object in bytes
        size: u64,
    },
}

impl<ObjectID: FsVerityHashValue> std::fmt::Debug for SplitStreamEntry<ObjectID> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SplitStreamEntry::Inline(data) => {
                f.debug_struct("Inline").field("len", &data.len()).finish()
            }
            SplitStreamEntry::External { size, .. } => f
                .debug_struct("External")
                .field("size", size)
                .finish_non_exhaustive(),
        }
    }
}

/// Builder for constructing a split stream with parallel object storage.
///
/// This builder collects entries (inline data and pending external object handles),
/// then serializes them all at once when `finish()` is called. This approach:
/// - Allows all external handles to be awaited in parallel
/// - Enables proper deduplication of ObjectIDs
/// - Writes the stream in one clean pass after all IDs are known
///
/// # Example
/// ```ignore
/// let mut builder = SplitStreamBuilder::new(repo.clone(), content_type)?;
/// builder.push_inline(header_bytes);
/// builder.push_external(storage_handle, file_size);
/// builder.push_inline(padding);
/// let object_id = builder.finish().await?;
/// ```
pub struct SplitStreamBuilder<ObjectID: FsVerityHashValue> {
    repo: Arc<Repository<ObjectID>>,
    writable: WritableRepo,
    entries: Vec<SplitStreamEntry<ObjectID>>,
    total_external_size: u64,
    total_inline_bytes: u64,
    content_type: u64,
    stream_refs: UniqueVec<ObjectID>,
    named_refs: BTreeMap<Box<str>, usize>,
}

impl<ObjectID: FsVerityHashValue> std::fmt::Debug for SplitStreamBuilder<ObjectID> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SplitStreamBuilder")
            .field("repo", &self.repo)
            .field("entries", &self.entries)
            .field("total_external_size", &self.total_external_size)
            .field("content_type", &self.content_type)
            .finish_non_exhaustive()
    }
}

impl<ObjectID: FsVerityHashValue> SplitStreamBuilder<ObjectID> {
    /// Create a new split stream builder.
    ///
    /// Performs an upfront writable check; the token is carried so that
    /// the final `finish()` call can store objects without redundant checks.
    pub fn new(repo: Arc<Repository<ObjectID>>, content_type: u64) -> Result<Self> {
        let writable = repo.ensure_writable_token()?;
        Ok(Self {
            repo,
            writable,
            entries: Vec::new(),
            total_external_size: 0,
            total_inline_bytes: 0,
            content_type,
            stream_refs: UniqueVec::new(),
            named_refs: Default::default(),
        })
    }

    /// Append inline data to the stream.
    ///
    /// Adjacent inline data will be coalesced to avoid fragmentation.
    pub fn push_inline(&mut self, data: &[u8]) {
        if data.is_empty() {
            return;
        }
        self.total_inline_bytes += data.len() as u64;
        // Coalesce with the previous inline entry if possible
        if let Some(SplitStreamEntry::Inline(existing)) = self.entries.last_mut() {
            existing.extend_from_slice(data);
        } else {
            self.entries.push(SplitStreamEntry::Inline(data.to_vec()));
        }
    }

    /// Append an external object being stored in background.
    ///
    /// The handle should resolve to the (ObjectID, ObjectStoreMethod) when the storage completes.
    pub fn push_external(
        &mut self,
        handle: JoinHandle<Result<(ObjectID, ObjectStoreMethod)>>,
        size: u64,
    ) {
        self.total_external_size += size;
        self.entries
            .push(SplitStreamEntry::External { handle, size });
    }

    /// Add an externally-referenced stream with the given name.
    ///
    /// The name has no meaning beyond the scope of this file: it is meant to be used to link to
    /// associated data when reading the file back again. For example, for OCI config files, this
    /// might refer to a layer splitstream via its DiffId.
    pub fn add_named_stream_ref(&mut self, name: &str, verity: &ObjectID) {
        let idx = self.stream_refs.ensure(verity);
        self.named_refs.insert(Box::from(name), idx);
    }

    /// Finalize: await all handles, build the splitstream, store it.
    ///
    /// This method:
    /// 1. Awaits all external handles to get ObjectIDs
    /// 2. Builds a UniqueVec<ObjectID> for deduplication
    /// 3. Creates a SplitStreamWriter and replays all entries
    /// 4. Stores the final splitstream in the repository
    ///
    /// Returns the fs-verity object ID of the stored splitstream and
    /// [`SplitStreamStats`] with inline byte counts and per-object storage methods.
    pub async fn finish(self) -> Result<(ObjectID, SplitStreamStats)> {
        let mut stats = SplitStreamStats {
            inline_bytes: self.total_inline_bytes,
            external_objects: Vec::new(),
        };

        // First pass: await all handles to collect ObjectIDs
        // We need to preserve the order of entries, so we process them in sequence
        let mut resolved_entries: Vec<ResolvedEntry<ObjectID>> =
            Vec::with_capacity(self.entries.len());

        for entry in self.entries {
            match entry {
                SplitStreamEntry::Inline(data) => {
                    resolved_entries.push(ResolvedEntry::Inline(data));
                }
                SplitStreamEntry::External { handle, size } => {
                    let (id, method) = handle.await??;
                    stats.external_objects.push((size, method));
                    resolved_entries.push(ResolvedEntry::External { id, size });
                }
            }
        }

        // Second pass: build the splitstream using SplitStreamWriter
        // This gives us proper deduplication through UniqueVec
        let mut writer = SplitStreamWriter::new(&self.repo, self.content_type, self.writable);

        // Copy over stream refs and named refs
        for (name, idx) in &self.named_refs {
            let verity = self
                .stream_refs
                .get_by_index(*idx)
                .expect("named ref index out of bounds");
            writer.add_named_stream_ref(name, verity);
        }

        // Replay all entries
        // Note: write_inline tracks total_size internally, but write_reference doesn't,
        // so we manually add the external size to the writer's total_size.
        for entry in resolved_entries {
            match entry {
                ResolvedEntry::Inline(data) => {
                    writer.write_inline(&data);
                }
                ResolvedEntry::External { id, size } => {
                    // Add size before writing reference (write_reference doesn't track size)
                    writer.add_external_size(size);
                    writer.write_reference(id)?;
                }
            }
        }

        // Finalize and store
        let id = tokio::task::spawn_blocking(move || writer.done()).await??;
        Ok((id, stats))
    }
}

/// Internal type for resolved entries after awaiting handles.
#[derive(Debug)]
enum ResolvedEntry<ObjectID: FsVerityHashValue> {
    Inline(Vec<u8>),
    External { id: ObjectID, size: u64 },
}

/// Writer for creating split stream format files with inline content and external object references.
pub struct SplitStreamWriter<ObjectId: FsVerityHashValue> {
    repo: Arc<Repository<ObjectId>>,
    /// Proof that the writable check was performed when this writer was
    /// created.  Passed to [`Repository::ensure_object_impl`] so that
    /// per-object writes skip redundant `faccessat` calls.
    writable: WritableRepo,
    stream_refs: UniqueVec<ObjectId>,
    object_refs: UniqueVec<ObjectId>,
    named_refs: BTreeMap<Box<str>, usize>, // index into stream_refs
    inline_buffer: Vec<u8>,
    total_size: u64,
    writer: Encoder<'static, Vec<u8>>,
    content_type: u64,
    /// When true, done() writes old-format (pre-repr(C)) headers.
    #[cfg(any(test, feature = "test"))]
    write_old_format: bool,
}

impl<ObjectID: FsVerityHashValue> std::fmt::Debug for SplitStreamWriter<ObjectID> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // writer doesn't impl Debug
        f.debug_struct("SplitStreamWriter")
            .field("repo", &self.repo)
            .field("inline_content", &self.inline_buffer)
            .finish()
    }
}

impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
    /// Get a reference to the repository.
    pub fn repo(&self) -> &Repository<ObjectID> {
        &self.repo
    }

    /// Access the [`WritableRepo`] token carried by this writer.
    pub(crate) fn writable(&self) -> &WritableRepo {
        &self.writable
    }

    /// Create a new split stream writer.
    ///
    /// The `writable` token is carried so that subsequent object writes
    /// (via [`write_external`] / [`done`]) skip redundant writable checks.
    pub(crate) fn new(
        repo: &Arc<Repository<ObjectID>>,
        content_type: u64,
        writable: WritableRepo,
    ) -> Self {
        // SAFETY: we surely can't get an error writing the header to a Vec<u8>
        let writer = Encoder::new(vec![], 0).unwrap();

        Self {
            repo: Arc::clone(repo),
            writable,
            content_type,
            inline_buffer: vec![],
            stream_refs: UniqueVec::new(),
            object_refs: UniqueVec::new(),
            named_refs: Default::default(),
            total_size: 0,
            writer,
            #[cfg(any(test, feature = "test"))]
            write_old_format: repo.write_old_splitstream_format(),
        }
    }

    /// Add an externally-referenced object.
    ///
    /// This establishes a link to an object (ie: raw data file) from this stream.  The link is
    /// given a unique index number, which is returned.  Once assigned, this index won't change.
    /// The same index can be used to find the linked object when reading the file back.
    ///
    /// This is the primary mechanism by which splitstreams reference split external content.
    ///
    /// You usually won't need to call this yourself: if you want to add split external content to
    /// the stream, call `.write_external()` or `._write_external_async()`.
    pub fn add_object_ref(&mut self, verity: &ObjectID) -> usize {
        self.object_refs.ensure(verity)
    }

    /// Find the index of a previously referenced object.
    ///
    /// Finds the previously-assigned index for a linked object, or None if the object wasn't
    /// previously linked.
    pub fn lookup_object_ref(&self, verity: &ObjectID) -> Option<usize> {
        self.object_refs.get(verity)
    }

    /// Add an externally-referenced stream with the given name.
    ///
    /// The name has no meaning beyond the scope of this file: it is meant to be used to link to
    /// associated data when reading the file back again.  For example, for OCI config files, this
    /// might refer to a layer splitstream via its DiffId.
    ///
    /// This establishes a link between the two splitstreams and is considered when performing
    /// garbage collection: the named stream will be kept alive by this stream.
    pub fn add_named_stream_ref(&mut self, name: &str, verity: &ObjectID) {
        let idx = self.stream_refs.ensure(verity);
        self.named_refs.insert(Box::from(name), idx);
    }

    // flush any buffered inline data
    fn flush_inline(&mut self) -> Result<()> {
        let size = self.inline_buffer.len();
        if size > 0 {
            // Inline chunk: stored as negative LE i64 number of bytes (non-zero!)
            // SAFETY: naive - fails on -i64::MIN but we know size was unsigned
            let instruction = -i64::try_from(size).expect("implausibly large inline chunk");
            self.writer.write_all(I64::new(instruction).as_bytes())?;
            self.writer.write_all(&self.inline_buffer)?;
            self.inline_buffer.clear();
        }
        Ok(())
    }

    /// Write inline data to the stream.
    pub fn write_inline(&mut self, data: &[u8]) {
        // SAFETY: We'd have to write a lot of data to get here...
        self.total_size += data.len() as u64;
        self.inline_buffer.extend(data);
    }

    /// Add to the total external size tracked by this writer.
    ///
    /// This is used by `SplitStreamBuilder` when replaying external entries,
    /// since `write_reference` doesn't track size on its own.
    pub fn add_external_size(&mut self, size: u64) {
        self.total_size += size;
    }

    /// Write a reference to an external object that has already been stored.
    ///
    /// This is the common implementation for `.write_external()` and `.write_external_async()`,
    /// and is also used by `SplitStreamBuilder` when replaying resolved entries.
    ///
    /// Note: This does NOT add to total_size - the caller must do that if needed.
    pub fn write_reference(&mut self, id: ObjectID) -> Result<()> {
        // Flush any buffered inline data before we store the external reference.
        self.flush_inline()?;

        // External chunk: non-negative LE i64 index into object_refs array
        let index = self.add_object_ref(&id);
        let instruction = i64::try_from(index).expect("implausibly large external index");
        self.writer.write_all(I64::from(instruction).as_bytes())?;
        Ok(())
    }

    /// Write externally-split data to the stream.
    ///
    /// The data is stored in the repository and a reference is written to the stream.
    /// Uses the carried [`WritableRepo`] token to skip redundant writable checks.
    pub fn write_external(&mut self, data: &[u8]) -> Result<()> {
        self.total_size += data.len() as u64;
        let id = self.repo.ensure_object_impl(data, &self.writable)?;
        self.write_reference(id)
    }

    /// Asynchronously write externally-split data to the stream.
    ///
    /// The data is stored in the repository asynchronously and a reference is written to the stream.
    /// This method awaits the storage operation before returning.
    /// Uses the carried [`WritableRepo`] token to skip redundant writable checks.
    pub async fn write_external_async(&mut self, data: Vec<u8>) -> Result<()> {
        self.total_size += data.len() as u64;
        let self_ = Arc::clone(&self.repo);
        let writable = self.writable;
        let id = tokio::task::spawn_blocking(move || self_.ensure_object_impl(&data, &writable))
            .await??;
        self.write_reference(id)
    }

    fn write_named_refs(named_refs: BTreeMap<Box<str>, usize>) -> Result<Vec<u8>> {
        let mut encoder = Encoder::new(vec![], 0)?;

        for (name, idx) in &named_refs {
            write!(&mut encoder, "{idx}:{name}\0")?;
        }

        Ok(encoder.finish()?)
    }

    /// Finalizes the split stream and returns its object ID.
    ///
    /// Flushes any remaining inline content, validates the SHA256 hash if provided,
    /// and stores the compressed stream in the repository.
    pub fn done(mut self) -> Result<ObjectID> {
        self.flush_inline()?;
        let stream = self.writer.finish()?;

        // Pre-compute the file layout
        let header_start = 0u64;
        let header_end = header_start + size_of::<SplitstreamHeader>() as u64;

        let info_start = header_end;
        let info_end = info_start + size_of::<SplitstreamInfo>() as u64;
        assert_eq!(info_start % 8, 0);

        let stream_refs_size = self.stream_refs.as_bytes().len();
        let stream_refs_start = info_end;
        let stream_refs_end = stream_refs_start + stream_refs_size as u64;
        assert_eq!(stream_refs_start % 8, 0);

        let object_refs_size = self.object_refs.as_bytes().len();
        let object_refs_start = stream_refs_end;
        let object_refs_end = object_refs_start + object_refs_size as u64;
        assert_eq!(object_refs_start % 8, 0);

        let named_refs =
            Self::write_named_refs(self.named_refs).context("Formatting named references")?;
        let named_refs_start = object_refs_end;
        let named_refs_end = named_refs_start + named_refs.len() as u64;
        assert_eq!(named_refs_start % 8, 0);

        let stream_start = named_refs_end;
        let stream_end = stream_start + stream.len() as u64;

        // Write the file out into a Vec<u8>, checking the layout on the way
        let mut buf = vec![];

        assert_eq!(buf.len() as u64, header_start);
        buf.extend_from_slice(
            SplitstreamHeader {
                magic: SPLITSTREAM_MAGIC,
                version: 0,
                _flags: U16::ZERO,
                algorithm: ObjectID::ALGORITHM.kernel_id(),
                lg_blocksize: LG_BLOCKSIZE,
                info: (info_start..info_end).into(),
            }
            .as_bytes(),
        );
        assert_eq!(buf.len() as u64, header_end);

        assert_eq!(buf.len() as u64, info_start);
        buf.extend_from_slice(
            SplitstreamInfo {
                stream_refs: (stream_refs_start..stream_refs_end).into(),
                object_refs: (object_refs_start..object_refs_end).into(),
                stream: (stream_start..stream_end).into(),
                named_refs: (named_refs_start..named_refs_end).into(),
                content_type: self.content_type.into(),
                stream_size: self.total_size.into(),
            }
            .as_bytes(),
        );
        assert_eq!(buf.len() as u64, info_end);

        assert_eq!(buf.len() as u64, stream_refs_start);
        buf.extend_from_slice(self.stream_refs.as_bytes());
        assert_eq!(buf.len() as u64, stream_refs_end);

        assert_eq!(buf.len() as u64, object_refs_start);
        buf.extend_from_slice(self.object_refs.as_bytes());
        assert_eq!(buf.len() as u64, object_refs_end);

        assert_eq!(buf.len() as u64, named_refs_start);
        buf.extend_from_slice(&named_refs);
        assert_eq!(buf.len() as u64, named_refs_end);

        assert_eq!(buf.len() as u64, stream_start);
        buf.extend_from_slice(&stream);
        assert_eq!(buf.len() as u64, stream_end);

        // If test mode requests old-format headers, rewrite the header in place
        #[cfg(any(test, feature = "test"))]
        let buf = if self.write_old_format {
            new_to_old_format(&buf)
        } else {
            buf
        };

        // Store the Vec<u8> into the repository (writable already checked)
        self.repo.ensure_object_impl(&buf, &self.writable)
    }

    /// Finalizes the split stream asynchronously.
    ///
    /// This is an async-friendly version of `done()` that runs the final
    /// object storage on a blocking thread pool.
    ///
    /// Returns the fs-verity object ID of the stored splitstream.
    pub async fn done_async(self) -> Result<ObjectID> {
        tokio::task::spawn_blocking(move || self.done()).await?
    }
}

/// Data fragment from a split stream, either inline content or an external object reference.
#[derive(Debug)]
pub enum SplitStreamData<ObjectID: FsVerityHashValue> {
    /// Inline content stored directly in the stream
    Inline(Box<[u8]>),
    /// Reference to an external object
    External(ObjectID),
}

/// Reader for parsing split stream format files with inline content and external object references.
pub struct SplitStreamReader<ObjectID: FsVerityHashValue> {
    decoder: Decoder<'static, BufReader<Take<File>>>,
    inline_bytes: usize,
    /// The content_type ID given when the splitstream was constructed
    pub content_type: u64,
    /// The total size of the original/merged stream, in bytes
    pub total_size: u64,
    object_refs: Vec<ObjectID>,
    named_refs: HashMap<Box<str>, ObjectID>,
}

impl<ObjectID: FsVerityHashValue> std::fmt::Debug for SplitStreamReader<ObjectID> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // decoder doesn't impl Debug
        f.debug_struct("SplitStreamReader")
            .field("refs", &self.object_refs)
            .field("inline_bytes", &self.inline_bytes)
            .finish()
    }
}

/// Using the provided [`vec`] as a buffer, read exactly [`size`]
/// bytes of content from [`reader`] into it. Any existing content
/// in [`vec`] will be discarded; however its capacity will be reused,
/// making this function suitable for use in loops.
fn read_into_vec(reader: &mut impl Read, vec: &mut Vec<u8>, size: usize) -> Result<()> {
    vec.resize(size, 0u8);
    reader.read_exact(vec.as_mut_slice())?;
    Ok(())
}

enum ChunkType<ObjectID: FsVerityHashValue> {
    Eof,
    Inline,
    External(ObjectID),
}

impl<ObjectID: FsVerityHashValue> SplitStreamReader<ObjectID> {
    /// Creates a new split stream reader from the provided reader.
    ///
    /// Reads the digest map header from the stream during initialization.
    #[context("Creating new splitstream reader")]
    pub fn new(mut file: File, expected_content_type: Option<u64>) -> Result<Self> {
        let header = SplitstreamHeader::read_from_io(&mut file)
            .map_err(|e| Error::msg(format!("Error reading splitstream header: {e:?}")))?;

        let header = if header.magic != SPLITSTREAM_MAGIC {
            // Try interpreting as old layout (pre-repr(C) fix, bootc <= 1.15.x)
            file.seek(SeekFrom::Start(0))
                .context("Seeking back to start for old-format header")?;
            let old_header = OldSplitstreamHeader::read_from_io(&mut file).map_err(|e| {
                Error::msg(format!(
                    "Error reading old-format splitstream header: {e:?}"
                ))
            })?;
            if old_header.magic != SPLITSTREAM_MAGIC {
                bail!("Invalid splitstream header magic value");
            }
            old_header.into()
        } else {
            header
        };

        if header.version != 0 {
            bail!("Invalid splitstream version {}", header.version);
        }

        if header.algorithm != ObjectID::ALGORITHM.kernel_id() {
            bail!("Invalid splitstream fs-verity algorithm type");
        }

        if header.lg_blocksize != LG_BLOCKSIZE {
            bail!("Invalid splitstream fs-verity block size");
        }

        let info_bytes = read_range(&mut file, header.info)?;
        // NB: We imagine that `info` might grow in the future, so for forward-compatibility we
        // allow that it is larger than we expect it to be.  If we ever expand the info section
        // then we will also need to come up with a mechanism for a smaller info section for
        // backwards-compatibility.
        let (info, _) = SplitstreamInfo::ref_from_prefix(&info_bytes)
            .map_err(|e| Error::msg(format!("Error reading splitstream metadata: {e:?}")))?;

        let content_type: u64 = info.content_type.into();
        if let Some(expected) = expected_content_type {
            ensure!(content_type == expected, "Invalid splitstream content type");
        }

        let total_size: u64 = info.stream_size.into();

        let stream_refs_bytes = read_range(&mut file, info.stream_refs)?;
        let stream_refs = <[ObjectID]>::ref_from_bytes(&stream_refs_bytes)
            .map_err(|e| Error::msg(format!("Error reading splitstream references: {e:?}")))?;

        let object_refs_bytes = read_range(&mut file, info.object_refs)?;
        let object_refs = <[ObjectID]>::ref_from_bytes(&object_refs_bytes)
            .map_err(|e| Error::msg(format!("Error reading object references: {e:?}")))?;

        let named_refs_bytes = read_range(&mut file, info.named_refs)?;
        let named_refs = Self::read_named_references(&named_refs_bytes, stream_refs)
            .map_err(|e| Error::msg(format!("Error reading splitstream mappings: {e:?}")))?;

        file.seek(SeekFrom::Start(info.stream.start.get()))
            .context("Unable to seek to start of splitstream content")?;
        let decoder = Decoder::new(file.take(info.stream.len()?))
            .context("Unable to decode zstd-compressed content in splitstream")?;

        Ok(Self {
            decoder,
            inline_bytes: 0,
            content_type,
            total_size,
            object_refs: object_refs.to_vec(),
            named_refs,
        })
    }

    fn read_named_references<ObjectId: FsVerityHashValue>(
        section: &[u8],
        references: &[ObjectId],
    ) -> Result<HashMap<Box<str>, ObjectId>> {
        let mut map = HashMap::new();
        let mut buffer = vec![];

        let mut reader = BufReader::new(
            Decoder::new(section).context("Creating zstd decoder for named references section")?,
        );

        loop {
            reader
                .read_until(b'\0', &mut buffer)
                .context("Reading named references section")?;

            let Some(item) = buffer.strip_suffix(b"\0") else {
                ensure!(
                    buffer.is_empty(),
                    "Trailing junk in named references section"
                );
                return Ok(map);
            };

            let (idx_str, name) = std::str::from_utf8(item)
                .context("Reading named references section")?
                .split_once(":")
                .context("Named reference doesn't contain a colon")?;

            let idx: usize = idx_str
                .parse()
                .context("Named reference contains a non-integer index")?;
            let object_id = references
                .get(idx)
                .context("Named reference out of bounds")?;

            map.insert(Box::from(name), object_id.clone());
            buffer.clear();
        }
    }

    /// Iterate the list of named references defined on this split stream.
    pub fn iter_named_refs(&self) -> impl Iterator<Item = (&str, &ObjectID)> {
        self.named_refs.iter().map(|(name, id)| (name.as_ref(), id))
    }

    /// Steal the "named refs" table from this splitstream, destructing it in the process.
    pub fn into_named_refs(self) -> HashMap<Box<str>, ObjectID> {
        self.named_refs
    }

    fn ensure_chunk(
        &mut self,
        eof_ok: bool,
        ext_ok: bool,
        expected_bytes: usize,
    ) -> Result<ChunkType<ObjectID>> {
        if self.inline_bytes == 0 {
            let mut value = I64::ZERO;

            if !read_exactish(&mut self.decoder, value.as_mut_bytes())? {
                ensure!(eof_ok, "Unexpected EOF in splitstream");
                return Ok(ChunkType::Eof);
            }

            // Negative values: (non-empty) inline data
            // Non-negative values: index into object_refs array
            match value.get() {
                n if n < 0i64 => {
                    self.inline_bytes = (n.unsigned_abs().try_into())
                        .context("Splitstream inline section is too large")?;
                }
                n => {
                    ensure!(ext_ok, "Unexpected external reference in splitstream");
                    let idx = usize::try_from(n)
                        .context("Splitstream external reference is too large")?;
                    let id: &ObjectID = (self.object_refs.get(idx))
                        .context("Splitstream external reference is out of range")?;
                    return Ok(ChunkType::External(id.clone()));
                }
            }
        }

        if self.inline_bytes < expected_bytes {
            bail!("Unexpectedly small inline content when parsing splitstream");
        }

        Ok(ChunkType::Inline)
    }

    /// Reads the exact number of inline bytes
    /// Assumes that the data cannot be split across chunks
    pub fn read_inline_exact(&mut self, buffer: &mut [u8]) -> Result<bool> {
        if let ChunkType::Inline = self.ensure_chunk(true, false, buffer.len())? {
            // SAFETY: ensure_chunk() already verified the number of bytes for us
            self.inline_bytes -= buffer.len();
            self.decoder.read_exact(buffer)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    fn discard_padding(&mut self, size: usize) -> Result<()> {
        let mut buf = [0u8; 512];
        assert!(size <= 512);
        self.ensure_chunk(false, false, size)?;
        self.decoder.read_exact(&mut buf[0..size])?;
        self.inline_bytes -= size;
        Ok(())
    }

    /// Reads an exact amount of data, which may be inline or external.
    ///
    /// The stored_size is the size as recorded in the stream (including any padding),
    /// while actual_size is the actual content size without padding.
    /// Returns either inline content or an external object reference.
    pub fn read_exact(
        &mut self,
        actual_size: usize,
        stored_size: usize,
    ) -> Result<SplitStreamData<ObjectID>> {
        if let ChunkType::External(id) = self.ensure_chunk(false, true, stored_size)? {
            // ...and the padding
            if actual_size < stored_size {
                self.discard_padding(stored_size - actual_size)?;
            }
            Ok(SplitStreamData::External(id))
        } else {
            let mut content = vec![];
            read_into_vec(&mut self.decoder, &mut content, stored_size)?;
            content.truncate(actual_size);
            self.inline_bytes -= stored_size;
            Ok(SplitStreamData::Inline(content.into()))
        }
    }

    /// Concatenates the entire split stream content to the output writer.
    ///
    /// Inline content is written directly, while external references are resolved
    /// using the provided load_data callback function.
    #[context("Concatenating splitstream to output")]
    pub fn cat(&mut self, repo: &Repository<ObjectID>, output: &mut impl Write) -> Result<()> {
        let mut buffer = vec![];

        loop {
            match self.ensure_chunk(true, true, 0)? {
                ChunkType::Eof => break Ok(()),
                ChunkType::Inline => {
                    read_into_vec(&mut self.decoder, &mut buffer, self.inline_bytes)?;
                    self.inline_bytes = 0;
                    output.write_all(&buffer)?;
                }
                ChunkType::External(ref id) => {
                    let mut buffer = [MaybeUninit::<u8>::uninit(); 1024 * 1024];
                    let fd = repo.open_object(id)?;

                    loop {
                        let (result, _) = read(&fd, &mut buffer)?;
                        if result.is_empty() {
                            break;
                        }
                        output.write_all(result)?;
                    }
                }
            }
        }
    }

    /// Traverses the split stream and calls the callback for each object reference.
    ///
    /// This includes both references from the digest map and external references in the stream.
    #[context("Getting object references from splitstream")]
    pub fn get_object_refs(&mut self, mut callback: impl FnMut(&ObjectID)) -> Result<()> {
        for entry in &self.object_refs {
            callback(entry);
        }
        Ok(())
    }

    /// Looks up a named reference
    ///
    /// Returns None if no such reference exists
    pub fn lookup_named_ref(&self, name: &str) -> Option<&ObjectID> {
        self.named_refs.get(name)
    }
}

impl<ObjectID: FsVerityHashValue> Read for SplitStreamReader<ObjectID> {
    fn read(&mut self, data: &mut [u8]) -> std::io::Result<usize> {
        match self.ensure_chunk(true, false, 1) {
            Ok(ChunkType::Eof) => Ok(0),
            Ok(ChunkType::Inline) => {
                let n_bytes = std::cmp::min(data.len(), self.inline_bytes);
                self.decoder.read_exact(&mut data[0..n_bytes])?;
                self.inline_bytes -= n_bytes;
                Ok(n_bytes)
            }
            Ok(ChunkType::External(..)) => unreachable!(),
            Err(e) => Err(std::io::Error::other(e)),
        }
    }
}

/// Convert a new-format splitstream to old (pre-repr(C)) header layout.
/// Takes the raw bytes of a valid new-format splitstream and returns bytes
/// with the header rewritten to match the compiler-reordered layout.
/// Only the 32-byte header changes; the rest of the file is identical.
#[cfg(any(test, feature = "test"))]
pub fn new_to_old_format(new_format: &[u8]) -> Vec<u8> {
    assert!(new_format.len() >= size_of::<SplitstreamHeader>());
    let (header, _) = SplitstreamHeader::ref_from_prefix(new_format).unwrap();
    assert_eq!(header.magic, SPLITSTREAM_MAGIC, "input must be new-format");

    let old_header = OldSplitstreamHeader {
        info: header.info,
        _flags: header._flags,
        magic: header.magic,
        version: header.version,
        algorithm: header.algorithm,
        lg_blocksize: header.lg_blocksize,
    };

    let mut result = Vec::with_capacity(new_format.len());
    result.extend_from_slice(old_header.as_bytes());
    result.extend_from_slice(&new_format[size_of::<SplitstreamHeader>()..]);
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fsverity::{Sha256HashValue, compute_verity};
    use crate::test::tempdir;
    use rustix::fs::CWD;
    use std::io::Cursor;
    use std::path::Path;

    #[test]
    fn test_read_into_vec() -> Result<()> {
        // Test with an empty reader
        let mut reader = Cursor::new(vec![]);
        let mut vec = Vec::new();
        let result = read_into_vec(&mut reader, &mut vec, 0);
        assert!(result.is_ok());
        assert_eq!(vec.len(), 0);

        // Test with a reader that has some data
        let mut reader = Cursor::new(vec![1, 2, 3, 4, 5]);
        let mut vec = Vec::new();
        let result = read_into_vec(&mut reader, &mut vec, 3);
        assert!(result.is_ok());
        assert_eq!(vec.len(), 3);
        assert_eq!(vec, vec![1, 2, 3]);

        // Test reading more than the reader has
        let mut reader = Cursor::new(vec![1, 2, 3]);
        let mut vec = Vec::new();
        let result = read_into_vec(&mut reader, &mut vec, 5);
        assert!(result.is_err());

        // Test reading exactly what the reader has
        let mut reader = Cursor::new(vec![1, 2, 3]);
        let mut vec = Vec::new();
        let result = read_into_vec(&mut reader, &mut vec, 3);
        assert!(result.is_ok());
        assert_eq!(vec.len(), 3);
        assert_eq!(vec, vec![1, 2, 3]);

        // Test reading into a vector with existing capacity
        let mut reader = Cursor::new(vec![1, 2, 3, 4, 5]);
        let mut vec = Vec::with_capacity(10);
        let result = read_into_vec(&mut reader, &mut vec, 4);
        assert!(result.is_ok());
        assert_eq!(vec.len(), 4);
        assert_eq!(vec, vec![1, 2, 3, 4]);
        assert_eq!(vec.capacity(), 10);

        // Test reading into a vector with existing data
        let mut reader = Cursor::new(vec![1, 2, 3]);
        let mut vec = vec![9, 9, 9];
        let result = read_into_vec(&mut reader, &mut vec, 2);
        assert!(result.is_ok());
        assert_eq!(vec.len(), 2);
        assert_eq!(vec, vec![1, 2]);

        Ok(())
    }

    /// Create a test repository in insecure mode (no fs-verity required).
    fn create_test_repo(path: &Path) -> Result<Arc<Repository<Sha256HashValue>>> {
        let (repo, _) = Repository::init_path(
            CWD,
            path,
            crate::repository::RepositoryConfig::default().set_insecure(),
        )?;
        Ok(Arc::new(repo))
    }

    /// Generate deterministic test data of a given size.
    fn generate_test_data(size: usize, seed: u8) -> Vec<u8> {
        (0..size)
            .map(|i| ((i as u8).wrapping_add(seed)).wrapping_mul(17))
            .collect()
    }

    #[test]
    fn test_splitstream_inline_only() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let inline1 = generate_test_data(32, 0xAB);
        let inline2 = generate_test_data(48, 0xCD);

        let mut writer = repo.create_stream(0)?;
        writer.write_inline(&inline1);
        writer.write_inline(&inline2);
        let stream_id = repo.write_stream(writer, "test-inline", None)?;

        // Read it back via cat()
        let mut reader = repo.open_stream("test-inline", Some(&stream_id), None)?;
        let mut output = Vec::new();
        reader.cat(&repo, &mut output)?;

        let mut expected = inline1.clone();
        expected.extend(&inline2);
        assert_eq!(output, expected, "inline-only roundtrip must be exact");
        Ok(())
    }

    #[test]
    fn test_splitstream_large_external() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        // 128KB of data
        let large_content = generate_test_data(128 * 1024, 0x42);

        // Compute expected fs-verity digest for this content
        let expected_digest: Sha256HashValue = compute_verity(&large_content);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&large_content)?;
        let stream_id = repo.write_stream(writer, "test-external", None)?;

        // Verify the object reference matches the expected digest
        let mut reader = repo.open_stream("test-external", Some(&stream_id), None)?;
        let mut refs = Vec::new();
        reader.get_object_refs(|id| refs.push(id.clone()))?;
        assert_eq!(refs.len(), 1);
        assert_eq!(
            refs[0], expected_digest,
            "external object must have correct fs-verity digest"
        );

        // Verify roundtrip
        let mut reader = repo.open_stream("test-external", Some(&stream_id), None)?;
        let mut output = Vec::new();
        reader.cat(&repo, &mut output)?;

        assert_eq!(output.len(), large_content.len());
        assert_eq!(
            output, large_content,
            "large external content must roundtrip exactly"
        );
        Ok(())
    }

    #[test]
    fn test_splitstream_mixed_content() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        // Simulate a tar-like structure: header (inline) + file content (external) + trailer
        let header = generate_test_data(512, 0x01);
        let file_content = generate_test_data(64 * 1024, 0x02);
        let trailer = generate_test_data(1024, 0x03);

        // Compute expected digest for the external content
        let expected_digest: Sha256HashValue = compute_verity(&file_content);

        let mut writer = repo.create_stream(0)?;
        writer.write_inline(&header);
        writer.write_external(&file_content)?;
        writer.write_inline(&trailer);
        let stream_id = repo.write_stream(writer, "test-mixed", None)?;

        // Verify the external object has the correct digest
        let mut reader = repo.open_stream("test-mixed", Some(&stream_id), None)?;
        let mut refs = Vec::new();
        reader.get_object_refs(|id| refs.push(id.clone()))?;
        assert_eq!(refs.len(), 1);
        assert_eq!(
            refs[0], expected_digest,
            "external object must have correct fs-verity digest"
        );

        // Verify roundtrip
        let mut reader = repo.open_stream("test-mixed", Some(&stream_id), None)?;
        let mut output = Vec::new();
        reader.cat(&repo, &mut output)?;

        let mut expected = header.clone();
        expected.extend(&file_content);
        expected.extend(&trailer);

        assert_eq!(output.len(), expected.len());
        assert_eq!(output, expected, "mixed content must roundtrip exactly");
        Ok(())
    }

    #[test]
    fn test_splitstream_multiple_externals() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let file1 = generate_test_data(32 * 1024, 0x10);
        let file2 = generate_test_data(256 * 1024, 0x20);
        let file3 = generate_test_data(8 * 1024, 0x30);
        let separator = generate_test_data(64, 0xFF);

        // Compute expected digests
        let expected_digest1: Sha256HashValue = compute_verity(&file1);
        let expected_digest2: Sha256HashValue = compute_verity(&file2);
        let expected_digest3: Sha256HashValue = compute_verity(&file3);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&file1)?;
        writer.write_inline(&separator);
        writer.write_external(&file2)?;
        writer.write_inline(&separator);
        writer.write_external(&file3)?;
        let stream_id = repo.write_stream(writer, "test-multi", None)?;

        // Verify the object references have correct digests
        let mut reader = repo.open_stream("test-multi", Some(&stream_id), None)?;
        let mut refs = Vec::new();
        reader.get_object_refs(|id| refs.push(id.clone()))?;
        assert_eq!(refs.len(), 3);
        assert_eq!(refs[0], expected_digest1, "file1 digest mismatch");
        assert_eq!(refs[1], expected_digest2, "file2 digest mismatch");
        assert_eq!(refs[2], expected_digest3, "file3 digest mismatch");

        // Verify roundtrip
        let mut reader = repo.open_stream("test-multi", Some(&stream_id), None)?;
        let mut output = Vec::new();
        reader.cat(&repo, &mut output)?;

        let mut expected = file1.clone();
        expected.extend(&separator);
        expected.extend(&file2);
        expected.extend(&separator);
        expected.extend(&file3);

        assert_eq!(output.len(), expected.len());
        assert_eq!(
            output, expected,
            "multiple externals must roundtrip exactly"
        );
        Ok(())
    }

    #[test]
    fn test_splitstream_deduplication() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        // Same chunk appearing multiple times should be deduplicated
        let repeated_chunk = generate_test_data(64 * 1024, 0xDE);
        let unique_chunk = generate_test_data(32 * 1024, 0xAD);

        // Compute expected digests
        let repeated_digest: Sha256HashValue = compute_verity(&repeated_chunk);
        let unique_digest: Sha256HashValue = compute_verity(&unique_chunk);

        let mut writer = repo.create_stream(0)?;
        writer.write_external(&repeated_chunk)?;
        writer.write_external(&unique_chunk)?;
        writer.write_external(&repeated_chunk)?; // duplicate
        writer.write_external(&repeated_chunk)?; // another duplicate
        let stream_id = repo.write_stream(writer, "test-dedup", None)?;

        // Verify deduplication: only 2 unique objects should be referenced
        let mut reader = repo.open_stream("test-dedup", Some(&stream_id), None)?;
        let mut refs = Vec::new();
        reader.get_object_refs(|id| refs.push(id.clone()))?;
        assert_eq!(refs.len(), 2, "should only have 2 unique object refs");
        assert_eq!(
            refs[0], repeated_digest,
            "first ref should be repeated chunk"
        );
        assert_eq!(refs[1], unique_digest, "second ref should be unique chunk");

        // Verify roundtrip still works
        let mut reader = repo.open_stream("test-dedup", Some(&stream_id), None)?;
        let mut output = Vec::new();
        reader.cat(&repo, &mut output)?;

        let mut expected = repeated_chunk.clone();
        expected.extend(&unique_chunk);
        expected.extend(&repeated_chunk);
        expected.extend(&repeated_chunk);

        assert_eq!(output.len(), expected.len());
        assert_eq!(
            output, expected,
            "deduplicated content must still roundtrip exactly"
        );
        Ok(())
    }

    #[test]
    fn test_splitstream_get_object_refs() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let chunk1 = generate_test_data(16 * 1024, 0x11);
        let chunk2 = generate_test_data(24 * 1024, 0x22);
        let inline_data = generate_test_data(128, 0x33);

        // Compute expected digests
        let expected_digest1: Sha256HashValue = compute_verity(&chunk1);
        let expected_digest2: Sha256HashValue = compute_verity(&chunk2);

        let mut writer = repo.create_stream(0)?;
        writer.write_inline(&inline_data);
        writer.write_external(&chunk1)?;
        writer.write_external(&chunk2)?;
        let stream_id = repo.write_stream(writer, "test-refs", None)?;

        let mut reader = repo.open_stream("test-refs", Some(&stream_id), None)?;

        let mut refs = Vec::new();
        reader.get_object_refs(|id| refs.push(id.clone()))?;

        // Should have 2 external references with correct digests
        assert_eq!(refs.len(), 2);
        assert_eq!(refs[0], expected_digest1, "chunk1 digest mismatch");
        assert_eq!(refs[1], expected_digest2, "chunk2 digest mismatch");

        // Verify content can be read back via the digests
        let obj1 = repo.read_object(&refs[0])?;
        let obj2 = repo.read_object(&refs[1])?;

        assert_eq!(obj1, chunk1, "first external reference must match");
        assert_eq!(obj2, chunk2, "second external reference must match");

        Ok(())
    }

    #[test]
    fn test_splitstream_boundary_sizes() -> Result<()> {
        // Test with sizes around common boundaries (4KB page, 64KB chunk)
        let sizes = [4095, 4096, 4097, 65535, 65536, 65537];

        for size in sizes {
            let tmp = tempdir();
            let repo = create_test_repo(&tmp.path().join("repo"))?;
            let data = generate_test_data(size, size as u8);

            // Compute expected digest
            let expected_digest: Sha256HashValue = compute_verity(&data);

            let mut writer = repo.create_stream(0)?;
            writer.write_external(&data)?;
            let stream_id = repo.write_stream(writer, "test-boundary", None)?;

            // Verify the digest
            let mut reader = repo.open_stream("test-boundary", Some(&stream_id), None)?;
            let mut refs = Vec::new();
            reader.get_object_refs(|id| refs.push(id.clone()))?;
            assert_eq!(refs.len(), 1);
            assert_eq!(
                refs[0], expected_digest,
                "size {} must have correct digest",
                size
            );

            // Verify roundtrip
            let mut reader = repo.open_stream("test-boundary", Some(&stream_id), None)?;
            let mut output = Vec::new();
            reader.cat(&repo, &mut output)?;

            assert_eq!(
                output.len(),
                data.len(),
                "size {} must roundtrip with correct length",
                size
            );
            assert_eq!(output, data, "size {} must roundtrip exactly", size);
        }

        Ok(())
    }

    #[test]
    fn test_splitstream_content_type() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;
        let content_type = 0xDEADBEEF_u64;

        let mut writer = repo.create_stream(content_type)?;
        writer.write_inline(b"test data");
        let stream_id = repo.write_stream(writer, "test-ctype", None)?;

        let reader = repo.open_stream("test-ctype", Some(&stream_id), Some(content_type))?;
        assert_eq!(reader.content_type, content_type);

        Ok(())
    }

    #[test]
    fn test_splitstream_total_size_tracking() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let inline_data = generate_test_data(100, 0x01);
        let external_data = generate_test_data(1000, 0x02);

        let mut writer = repo.create_stream(0)?;
        writer.write_inline(&inline_data);
        writer.write_external(&external_data)?;
        let stream_id = repo.write_stream(writer, "test-size", None)?;

        let reader = repo.open_stream("test-size", Some(&stream_id), None)?;
        assert_eq!(reader.total_size, 1100, "total size should be tracked");

        Ok(())
    }

    #[test]
    fn test_old_format_header_differs_from_new() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let mut writer = repo.create_stream(0)?;
        writer.write_inline(b"hello");
        let stream_id = repo.write_stream(writer, "test-old-hdr", None)?;

        let new_bytes = repo.read_object(&stream_id)?;
        let old_bytes = new_to_old_format(&new_bytes);

        // New format starts with magic, old format should NOT
        assert_eq!(&new_bytes[..11], b"SplitStream");
        assert_ne!(
            &old_bytes[..11],
            b"SplitStream",
            "old format should NOT start with magic"
        );
        Ok(())
    }

    #[test]
    fn test_read_old_format_splitstream() -> Result<()> {
        use std::io::Write as _;

        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let inline_data = b"hello";
        let mut writer = repo.create_stream(0)?;
        writer.write_inline(inline_data);
        let stream_id = repo.write_stream(writer, "test-old-read", None)?;

        // Read the raw stored bytes, convert to old format, write to a temp file
        let new_bytes = repo.read_object(&stream_id)?;
        let old_bytes = new_to_old_format(&new_bytes);

        let mut tmpfile = tempfile::NamedTempFile::new()?;
        tmpfile.write_all(&old_bytes)?;
        tmpfile.flush()?;

        // Read back via the old-format file
        let file = std::fs::File::open(tmpfile.path())?;
        let mut old_reader = SplitStreamReader::<Sha256HashValue>::new(file, None)?;

        assert_eq!(old_reader.total_size, inline_data.len() as u64);
        assert_eq!(old_reader.content_type, 0);

        let mut old_output = Vec::new();
        std::io::Read::read_to_end(&mut old_reader, &mut old_output)?;

        // Read back via the new-format (repo) for comparison
        let mut new_reader = repo.open_stream("test-old-read", Some(&stream_id), None)?;
        let mut new_output = Vec::new();
        std::io::Read::read_to_end(&mut new_reader, &mut new_output)?;

        assert_eq!(
            old_output, new_output,
            "old and new format must produce identical data"
        );
        assert_eq!(&old_output, inline_data);
        Ok(())
    }

    #[test]
    fn test_new_format_still_works() -> Result<()> {
        let tmp = tempdir();
        let repo = create_test_repo(&tmp.path().join("repo"))?;

        let inline_data = b"world";
        let mut writer = repo.create_stream(0)?;
        writer.write_inline(inline_data);
        let stream_id = repo.write_stream(writer, "test-new-fmt", None)?;

        // Verify the stored bytes start with magic (new format)
        let raw_bytes = repo.read_object(&stream_id)?;
        assert_eq!(&raw_bytes[..11], b"SplitStream");

        // Read back and verify content
        let mut reader = repo.open_stream("test-new-fmt", Some(&stream_id), None)?;
        assert_eq!(reader.total_size, inline_data.len() as u64);

        let mut output = Vec::new();
        std::io::Read::read_to_end(&mut reader, &mut output)?;
        assert_eq!(&output, inline_data);
        Ok(())
    }
}