dwarfs 0.1.0

A library for reading DwarFS archives (aka. DwarFS images)
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
//! The high-level interface for accessing a DwarFS archive.
//!
//! See [`Archive`] and [`ArchiveIndex`].

use std::{
    fmt,
    io::{BufRead, Read},
    iter::FusedIterator,
    num::NonZero,
};

use bstr::BString;
use lru::LruCache;
use positioned_io::{ReadAt, Size};

use crate::{
    bisect_range_by,
    fsst::Decoder as FsstDecoder,
    metadata::{self, Error as ParserMetadataError, Metadata, Schema, StringTable},
    section::{self, HEADER_SIZE, SectionIndexEntry, SectionReader, SectionType},
};

/// Type alias using [`Error`] as the default error type.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// An error raised from parsing or accessing [`Archive`].
pub struct Error(Box<ErrorInner>);

mod sealed {
    pub trait Sealed {}
}

#[derive(Debug)]
enum ErrorInner {
    Section(String, Option<section::Error>),
    MissingSection(SectionType),
    DuplicatedSection(SectionType),
    ParseMetadata(ParserMetadataError),
    UnsupportedFeature(String),
    Validation(&'static str),
    Io(std::io::Error),
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &*self.0 {
            ErrorInner::Section(msg, Some(err)) => write!(f, "{msg}: {err}"),
            ErrorInner::Section(msg, None) => write!(f, "{msg}"),
            ErrorInner::MissingSection(ty) => write!(f, "missing section {ty:?}"),
            ErrorInner::DuplicatedSection(ty) => write!(f, "duplicated sections {ty:?}"),
            ErrorInner::Io(err) => write!(f, "input/outpur error: {err}"),
            ErrorInner::ParseMetadata(err) => write!(f, "failed to parse metadata: {err}"),
            ErrorInner::Validation(err) => write!(f, "malformed metadata: {err}"),
            ErrorInner::UnsupportedFeature(msg) => write!(f, "unsupported feature: {msg}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &*self.0 {
            ErrorInner::Section(_, Some(err)) => Some(err),
            ErrorInner::Io(err) => Some(err),
            ErrorInner::ParseMetadata(err) => Some(err),
            _ => None,
        }
    }
}

impl From<ErrorInner> for Error {
    #[cold]
    fn from(err: ErrorInner) -> Self {
        Self(Box::new(err))
    }
}

impl From<std::io::Error> for Error {
    #[cold]
    fn from(err: std::io::Error) -> Self {
        Self(Box::new(ErrorInner::Io(err)))
    }
}

// Needed for `Read` impl.
impl From<Error> for std::io::Error {
    fn from(err: Error) -> Self {
        std::io::Error::new(std::io::ErrorKind::InvalidData, err)
    }
}

trait ResultExt<T> {
    fn context(self, msg: impl fmt::Display) -> Result<T>;
}

impl<T> ResultExt<T> for Result<T, section::Error> {
    #[inline]
    fn context(self, msg: impl fmt::Display) -> Result<T> {
        match self {
            Ok(v) => Ok(v),
            Err(err) => Err(ErrorInner::Section(msg.to_string(), Some(err)).into()),
        }
    }
}

trait OptionExt<T> {
    fn context(self, msg: &'static str) -> Result<T>;
}
impl<T> OptionExt<T> for Option<T> {
    #[inline]
    fn context(self, msg: &'static str) -> Result<T> {
        match self {
            Some(v) => Ok(v),
            None => Err(ErrorInner::Validation(msg).into()),
        }
    }
}

trait BoolExt {
    fn or_context(self, msg: &'static str) -> Result<()>;
}
impl BoolExt for bool {
    #[inline]
    fn or_context(self, msg: &'static str) -> Result<()> {
        if self {
            Ok(())
        } else {
            Err(ErrorInner::Validation(msg).into())
        }
    }
}

/// Configurations and parameters for [`Archive`] and [`ArchiveIndex`].
#[derive(Debug, Clone)]
pub struct Config {
    section_index_size_limit: usize,
    metadata_schema_size_limit: usize,
    metadata_size_limit: usize,
    block_cache_size_limit: usize,

    section_index_strategy: SectionIndexStrategy,
}

/// Whether to trust the section index embedded in the archive?
#[derive(Default, Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SectionIndexStrategy {
    /// Always use embedded section index.
    ///
    /// This guarantees consistent archive loading performance.
    /// But it is incompatible with archives without section index.
    UseEmbedded,
    /// Use embedded section index, or build our own if it does not exist.
    ///
    /// The default strategy, balancing between performance and compatibility.
    #[default]
    UseEmbeddedIfExists,
    /// Never use embedded section index but always build our own.
    ///
    /// During archive loading, all section headers will be traversed for
    /// building the index. This is the most defensive strategy which is robust
    /// on corrupted or fishy index that "happens to be like an index but there
    /// is actually none". But it significantly slows down archive
    /// loading due to heavy seeks.
    Build,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            // Some arbitrarily chosen numbers. Keep in sync with methods docs below!
            section_index_size_limit: 2 << 20,
            metadata_schema_size_limit: 16 << 10,
            metadata_size_limit: 64 << 20,
            // From `dwarfsextract`: 32 x 16MiB (default size) blocks.
            block_cache_size_limit: 512 << 20,
            section_index_strategy: SectionIndexStrategy::default(),
        }
    }
}

impl Config {
    /// Set the size limit for section index [`SECTION_INDEX`](SectionType::SECTION_INDEX).
    ///
    /// The default value is 2MiB. The section index is never compressed,
    /// and will be kept in memory in [`ArchiveIndex`].
    ///
    /// Each section occupies 8bytes in the index. Assuming the default block
    /// size 16MiB and 10% compression ratio, the default limit corresponds to
    /// 40TiB files compressed into an 4TiB archive, which is enough for typical use.
    pub fn section_index_size_limit(&mut self, limit: usize) -> &mut Self {
        self.section_index_size_limit = limit;
        self
    }

    /// Set the size limit for metadata schema section [`METADATA_V2_SCHEMA`](SectionType::METADATA_V2_SCHEMA).
    ///
    /// The default value is 16KiB. It limits both compressed and decompressed size,
    /// and is only used in duration of parsing metadata.
    ///
    /// The size of schema is almost constant and is typically <2KiB in practice.
    pub fn metadata_schema_size_limit(&mut self, limit: usize) -> &mut Self {
        self.metadata_schema_size_limit = limit;
        self
    }

    /// Set the size limit for metadata section [`METADATA_V2`](SectionType::METADATA_V2).
    ///
    /// The default value is 64MiB. It limits both compressed and decompressed size,
    /// and will be kept in memory in [`ArchiveIndex`].
    /// Note that this does NOT count bit-packing and delta-packing. The memory
    /// footprint can be more than 8 times the packed size, though it is hardly
    /// achievable in practice.
    ///
    /// The size of metadata scales linearly with the directory hierarchy,
    /// number of inodes, and the total file name and symlink length.
    /// Packed metadata may be smaller.
    ///
    /// As a reference, the DwarFS archive of Linux 6.7 source tree has metadata
    /// size of 2.2MiB, which is 1.4% of the 152MiB archive.
    /// You may want to increase this limit for larger archive.
    pub fn metadata_size_limit(&mut self, limit: usize) -> &mut Self {
        self.metadata_size_limit = limit;
        self
    }

    /// The size limit for LRU cache of decompressed data blocks.
    ///
    /// The default value is 512MiB. This also serves as a block size limit,
    /// because the cache must be able to contain at least one block.
    ///
    /// Block cache size is highly related with performance. For highly
    /// fragmented files, a too small block cache can severely hurt performance
    /// to more than 10x slower.
    ///
    /// For extraction from archive, sorting file locations
    /// ([`Chunk::section_idx`]) before read can improve locality and mitigate
    /// the drawback of a small cache. But for random access (like FUSE), a
    /// larger cache is always preferred.
    pub fn block_cache_size_limit(&mut self, limit: usize) -> &mut Self {
        self.block_cache_size_limit = limit;
        self
    }

    /// The strategy to use embedded section index.
    ///
    /// The default value is [`SectionIndexStrategy::UseEmbeddedIfExists`].
    /// See [`SectionIndexStrategy`] for details.
    pub fn section_index_strategy(&mut self, strategy: SectionIndexStrategy) -> &mut Self {
        self.section_index_strategy = strategy;
        self
    }
}

/// The index of a DwarFS archive representing the whole hierarchy.
///
/// This struct is immutable, and is separated from [`Archive`] for easier
/// lifetime management.
///
/// Use [`Archive::new`] to parse and create a pair of [`ArchiveIndex`] and
/// [`Archive`].
///
/// You usually start at [`ArchiveIndex::root`] to get the root directory in the
/// archive and walking or searching on the directory tree. [`File`] content can
/// be read via [`AsChunks`], or by manually iterating its [`Chunk`]s and
/// invokes [`Chunk::read_cached`]. You need to supply `&mut Archive`
/// corresponding to this index only when reading file contents.
pub struct ArchiveIndex {
    section_index: Box<[SectionIndexEntry]>,
    metadata: Metadata,

    mtime_only: bool,
    time_resolution: NonZero<u32>,
    timestamp_base_scaled: u64,
    inode_tally: InodeTally,
}

impl fmt::Debug for ArchiveIndex {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let alt = f.alternate();
        let mut d = f.debug_struct("ArchiveIndex");
        if alt {
            // NB. Always hide large structs.
            d.field("mtime_only", &self.mtime_only)
                .field("time_resolution", &self.time_resolution)
                .field("timestamp_base_scaled", &self.timestamp_base_scaled)
                .field("inode_tally", &self.inode_tally);
        }
        d.finish_non_exhaustive()
    }
}

/// Pre-calculated sums for type classification.
#[derive(Debug, Default)]
struct InodeTally {
    /// The number of unique files.
    unique_cnt: u32,

    // ..directories..
    symlink_start: u32,
    // ..symlinks..
    unique_start: u32,
    // ..unique regular files..
    shared_start: u32,
    // ..shared regular files..
    device_start: u32,
    // ..device files..
    ipc_start: u32,
    // ..fifo and sockets..
}

impl ArchiveIndex {
    /// Parse and create the index of a DwarFS archive.
    ///
    /// This is a low-level methods. Usually you should use [`Archive::new`] to
    /// get a pair of [`ArchiveIndex`] and [`Archive`] instead.
    pub fn new_with_config<R: ReadAt + Size>(
        rdr: &mut SectionReader<R>,
        config: &Config,
    ) -> Result<Self> {
        let stream_len = rdr.get_ref().size()?.ok_or_else(|| {
            ErrorInner::Section("cannot get the size of the archive reader".into(), None)
        })?;
        Self::new_inner(rdr, stream_len, config)
    }

    fn new_inner(
        rdr: &mut SectionReader<dyn ReadAt + '_>,
        stream_len: u64,
        config: &Config,
    ) -> Result<Self> {
        trace_time!("initialize ArchiveIndex");

        // Load or build the section index.
        let section_index = (|| {
            use SectionIndexStrategy as S;

            let strategy = &config.section_index_strategy;
            if matches!(strategy, S::UseEmbedded | S::UseEmbeddedIfExists) {
                if let Some((_, index)) = rdr
                    .read_section_index(stream_len, config.section_index_size_limit)
                    .context("failed to read section index")?
                {
                    return Ok(index);
                }
                if *strategy == S::UseEmbedded {
                    bail!(ErrorInner::MissingSection(SectionType::SECTION_INDEX))
                }
            }

            trace_time!("build section index");
            rdr.build_section_index(stream_len, config.section_index_size_limit)
                .context("failed to build section index")
        })()?;

        trace!("archive contains {} sections", section_index.len());
        u32::try_from(section_index.len())
            .ok()
            .context("too many sections")?;
        let section_index = section_index.into_boxed_slice();

        // Find metadata sections.
        let find_unique_section = |sec_ty: SectionType| -> Result<u64> {
            let mut iter = section_index
                .iter()
                .rev()
                .filter_map(|ent| (ent.section_type() == sec_ty).then_some(ent.offset()));
            let off = iter.next().ok_or(ErrorInner::MissingSection(sec_ty))?;
            if iter.next().is_some() {
                bail!(ErrorInner::DuplicatedSection(sec_ty));
            }
            Ok(off)
        };
        let schema_offset = find_unique_section(SectionType::METADATA_V2_SCHEMA)?;
        let metadata_offset = find_unique_section(SectionType::METADATA_V2)?;

        // Load and unpack metadata.
        let metadata = {
            trace_time!("parse schema and metadata");

            let (_, raw_schema) = rdr
                .read_section_at(schema_offset, config.metadata_schema_size_limit)
                .context("failed to read metadata schema section")?;
            let schema = Schema::parse(&raw_schema).map_err(ErrorInner::ParseMetadata)?;

            let (_, raw_metadata) = rdr
                .read_section_at(metadata_offset, config.metadata_size_limit)
                .context("failed to read metadata section")?;
            Metadata::parse(&schema, &raw_metadata).map_err(ErrorInner::ParseMetadata)?
        };

        let mut this = Self {
            section_index,
            metadata,

            mtime_only: false,
            time_resolution: NonZero::new(1).expect("1 is non-zero"),
            timestamp_base_scaled: 0,
            inode_tally: Default::default(),
        };
        this.unpack_validate()?;
        Ok(this)
    }

    /// Guard on filesystem features, unpack packed fields, build decoders and validate index ranges.
    fn unpack_validate(&mut self) -> Result<()> {
        trace_time!("unpack and validate full metadata content");
        let m = &mut self.metadata;

        // Explicit future-incompatible features.
        if let Some(feat) = &m.features {
            if !feat.is_empty() {
                bail!(ErrorInner::UnsupportedFeature(format!("{feat:?}")));
            }
        }
        m.dir_entries
            .is_some()
            .or_context("dir_entries must be present since DwarFS 2.3")?;

        // Various `FsOptions`.
        if let Some(opts) = &m.options {
            self.mtime_only = opts.mtime_only;
            self.time_resolution = NonZero::new(opts.time_resolution_sec.unwrap_or(1))
                .context("invalid options.time_resolution_sec")?;
            self.timestamp_base_scaled = m
                .timestamp_base
                .checked_mul(self.time_resolution.get().into())
                .context("timestamp_base overflow")?;

            if opts.packed_chunk_table {
                trace_time!("unpack chunk_table");

                let mut sum = 0u32;
                for c in &mut m.chunk_table {
                    sum = c
                        .checked_add(sum)
                        .context("value overflow for packed chunk_table")?;
                    *c = sum;
                }
            }

            if opts.packed_directories {
                trace_time!("unpack directories");

                let mut sum = 0u32;
                for dir in &mut m.directories {
                    sum = sum
                        .checked_add(dir.first_entry)
                        .context("value overflow for packed directories.first_entry")?;
                    dir.first_entry = sum;
                }
            }

            if let Some(shared) = m
                .shared_files_table
                .as_ref()
                .filter(|_| opts.packed_shared_files_table)
            {
                trace_time!("unpack shared files");

                let unpacked_len = std::iter::zip(shared, 2..)
                    .try_fold(0u32, |sum, (&cnt, dups)| {
                        cnt.checked_mul(dups)?.checked_add(sum)
                    })
                    // Use inode count as a loose upper bound, to guard from length exploding.
                    .filter(|&n| n < m.inodes.len() as u32)
                    .context("length overflow for packed shared files")?;
                let mut unpacked = Vec::with_capacity(unpacked_len as usize);
                unpacked.extend(
                    std::iter::zip(shared, 2usize..)
                        .flat_map(|(&cnt, dups)| std::iter::repeat_n(cnt, dups)),
                );
                m.shared_files_table = Some(unpacked);
            }
        }

        // Inode classification ranges.
        {
            trace_time!("classify inode types");

            // NB. Minus the sentinel.
            let dir_cnt = m.directories.len().saturating_sub(1);
            let file_store_cnt = m.chunk_table.len().saturating_sub(1);
            (dir_cnt >= 1).or_context("missing root directory")?;

            // Lengths will not overflow `u32`, checked by metadata parser.
            // And of course they cannot overflow `usize` because they are all in memory.
            let symlink_cnt = m.symlink_table.len();
            let device_cnt = m.devices.as_ref().map_or(0, |t| t.len());
            let inode_cnt = m.inodes.len();
            let shared_cnt = m.shared_files_table.as_ref().map_or(0, |v| v.len());
            let shared_store_cnt = m
                .shared_files_table
                .as_ref()
                .and_then(|v| v.last().copied())
                .map_or(Ok(0), |max_idx| {
                    max_idx
                        .checked_add(1)
                        .context("index out of range in shared_files_table")
                })?;
            let unique_cnt = (file_store_cnt as u32)
                .checked_sub(shared_store_cnt)
                .context("invalid shared file count")?;

            let unique_start = dir_cnt + symlink_cnt;
            let shared_start = unique_start + unique_cnt as usize;
            let device_start = shared_start + shared_cnt;
            let ipc_start = device_start + device_cnt;
            (ipc_start <= inode_cnt).or_context("inodes table too short")?;

            self.inode_tally = InodeTally {
                unique_cnt,
                symlink_start: dir_cnt as u32,
                unique_start: unique_start as u32,
                shared_start: shared_start as u32,
                device_start: device_start as u32,
                ipc_start: ipc_start as u32,
            };
        }

        // Unpack string tables, currently `compact_{names,symlinks}`.
        fn unpack_string_table(
            tbl: &mut Option<StringTable>,
            msg_index: &'static str,
            msg_symtab: &'static str,
            msg_decode: &'static str,
        ) -> Result<()> {
            trace_time!("unpack symtab");

            let Some(tbl) = tbl else { return Ok(()) };
            let len = tbl.buffer.len() as u32;
            if tbl.packed_index {
                let mut sum = 0u32;
                for v in &mut tbl.index {
                    sum = sum
                        .checked_add(*v)
                        .filter(|&i| i <= len)
                        .context(msg_index)?;
                    *v = sum;
                }
            // If symtab is used, the decoding process below will validate index ranges anyway.
            } else if tbl.symtab.is_none() {
                tbl.index.is_sorted().or_context(msg_index)?;
                if let Some(last_idx) = tbl.index.last() {
                    (*last_idx <= len).or_context(msg_index)?;
                }
            }
            if let Some(symtab_bytes) = &tbl.symtab {
                let decoder = FsstDecoder::parse_symtab(symtab_bytes).context(msg_symtab)?;
                let encoded = &tbl.buffer[..];
                // The decoded length must be greater than encoded length to
                // worth it, so 1x is not enough. Pick 2x as the least bound here.
                // Also note that `isize::MAX as usize * 2` never overflows.
                let mut out_buf = Vec::with_capacity(encoded.len() * 2);
                let mut out_index = Vec::with_capacity(tbl.index.len());
                let mut out_len = 0usize;
                out_index.push(0);
                for w in tbl.index.windows(2) {
                    let sym = encoded
                        .get(w[0] as usize..w[1] as usize)
                        .context(msg_index)?;
                    let sym_dec_len = FsstDecoder::max_decode_len(sym.len());
                    out_buf.resize(out_len + sym_dec_len, 0);
                    let sym_out = &mut out_buf[out_len..out_len + sym_dec_len];
                    let len = decoder.decode_into(sym, sym_out).context(msg_decode)?;
                    // Each decoded symbol must be in UTF-8.
                    str::from_utf8(&sym_out[..len]).ok().context(msg_decode)?;
                    out_len += len;

                    // This is suboptimal, because it *is* possible that the total length of
                    // decoded strings overflows u32, that is 4GiB names compressed into 512MiB.
                    // I don't think it's practically viable without exceeding the whole metadata
                    // size limit. Emitting a decoding error in this case seems acceptable to me.
                    let pos = u32::try_from(out_len).ok().context(msg_decode)?;
                    out_index.push(pos);
                }
                debug_assert_eq!(out_index.len(), tbl.index.len());
                out_buf.truncate(out_len);

                tbl.buffer = out_buf.into();
                tbl.index = out_index;
            }
            Ok(())
        }

        (m.compact_names.is_none() || m.names.is_empty())
            .or_context("names must be empty when compact_names is used")?;
        unpack_string_table(
            &mut m.compact_names,
            "invalid index for compact_names.index",
            "failed to parse compact_names.symtab",
            "failed to decode compact_names.buffer using symtab",
        )?;

        (m.compact_symlinks.is_none() || m.symlinks.is_empty())
            .or_context("symlinks must be empty when compact_symlinks is used")?;
        unpack_string_table(
            &mut m.compact_symlinks,
            "invalid index for compact_symlinks.index",
            "failed to parse compact_symlinks.symtab",
            "failed to decode compact_symlinks.buffer using symtab",
        )?;

        // Validate contents, mostly about indexes.
        // TODO: Maybe just cache the indirection result?
        {
            trace_time!("check index and values are in ranges");

            macro_rules! check {
                ($cond:expr, $msg:literal) => {
                    $cond.or_context(concat!("index out of range in ", $msg))?
                };
            }

            let block_size = m.block_size;
            (usize::try_from(block_size).is_ok() && block_size.is_power_of_two())
                .or_context("invalid block_size")?;

            let sections = self.section_index.len() as u32;
            for c in &m.chunks {
                check!(c.block < sections, "chunks.block");
                c.offset
                    .checked_add(c.size)
                    .filter(|&end| end <= block_size)
                    .context("offset out of range in chunks")?;
            }

            let entries = m.dir_entries.as_ref().expect("validated").len() as u32;
            for d in &m.directories {
                check!(d.first_entry <= entries, "directories.first_entry");
                check!(d.parent_entry <= entries, "directories.parent_entry");
                check!(d.self_entry <= entries, "directories.self_entry");
            }

            let uids = m.uids.len() as u32;
            let gids = m.gids.len() as u32;
            let modes = m.modes.len() as u32;
            let check_time = |time_off: u32, msg: &'static str| {
                u64::from(time_off)
                    .checked_mul(self.time_resolution.get().into())
                    .and_then(|x| x.checked_add(m.timestamp_base))
                    .context(msg)
            };
            for ino in &m.inodes {
                check!(ino.owner_index < uids, "inodes.owner_index");
                check!(ino.group_index < gids, "inodes.group_index");
                check!(ino.mode_index < modes, "inodes.mode_index");
                check_time(ino.mtime_offset, "inodes.mtime_offset overflows")?;
                if self.mtime_only {
                    (ino.atime_offset == 0 && ino.ctime_offset == 0).or_context(
                        "inodes.{a,c}time_offset is not zero when options.mtime_only is set",
                    )?;
                } else {
                    check_time(ino.atime_offset, "inodes.atime_offset overflows")?;
                    check_time(ino.ctime_offset, "inodes.ctime_offset overflows")?;
                }
            }

            let chunks = m.chunks.len() as u32;
            for &c in &m.chunk_table {
                check!(c <= chunks, "chunk_table");
            }

            let symlink_targets = m
                .compact_symlinks
                .as_ref()
                .map_or(m.symlinks.len(), |tbl| tbl.index.len().saturating_sub(1))
                as u32;
            for &i in &m.symlink_table {
                check!(i < symlink_targets, "symlink_table");
            }

            let inodes = m.inodes.len() as u32;
            let names = m
                .compact_names
                .as_ref()
                .map_or(m.names.len(), |tbl| tbl.index.len().saturating_sub(1))
                as u32;
            for ent in m.dir_entries.as_ref().expect("validated") {
                check!(ent.inode_num < inodes, "dir_entries.inode_num");
                check!(ent.name_index < names, "dir_entries.name_index");
            }
        }

        Ok(())
    }

    fn get_from_string_table<'a>(
        loose: &'a [BString],
        compact: &'a Option<StringTable>,
        idx: u32,
    ) -> &'a str {
        let s = if let Some(tbl) = compact {
            let idx_start = tbl.index[idx as usize] as usize;
            let idx_end = tbl.index[idx as usize + 1] as usize;
            &tbl.buffer[idx_start..idx_end]
        } else {
            &loose[idx as usize]
        };
        // TODO: Avoid re-validate the whole symbol?
        str::from_utf8(s).expect("validated")
    }

    /// Get the root directory of the archive.
    #[inline]
    #[must_use]
    pub fn root(&self) -> Dir<'_> {
        Dir(Inode {
            index: self,
            inode_num: 0,
        })
    }

    /// Get the inode under the given path from the root directory of the archive.
    ///
    /// ```
    /// use dwarfs::{ArchiveIndex, Inode};
    ///
    /// # fn work() -> Option<()> {
    /// let index: ArchiveIndex;
    /// # index = unimplemented!();
    /// // These two statements are equivalent.
    /// let baz1: Inode<'_> = index.get_path("src/lib.rs".split('/'))?;
    /// let baz2: Inode<'_> = index.root().get("src")?.inode().as_dir()?.get("lib.rs")?.inode();
    /// # None }
    /// ```
    #[must_use]
    pub fn get_path<I>(&self, path: I) -> Option<Inode<'_>>
    where
        I: IntoIterator,
        I::Item: AsRef<[u8]>,
    {
        path.into_iter()
            .try_fold(Inode::from(self.root()), |inode, name| {
                Some(inode.as_dir()?.get(name)?.inode())
            })
    }

    /// Get an iterator over all inodes in the archive, in ascending inode number.
    #[inline]
    #[must_use]
    pub fn inodes(&self) -> impl ExactSizeIterator<Item = Inode<'_>> + DoubleEndedIterator + '_ {
        let cnt = self.metadata().inodes.len() as u32;
        (0..cnt).map(|inode_num| Inode {
            index: self,
            inode_num,
        })
    }

    /// Get an iterator over all directory inodes in the archive, in ascending inode number.
    #[inline]
    #[must_use]
    pub fn directories(&self) -> impl ExactSizeIterator<Item = Dir<'_>> + DoubleEndedIterator + '_ {
        let cnt = self.inode_tally.symlink_start;
        (0..cnt).map(|inode_num| {
            Dir(Inode {
                index: self,
                inode_num,
            })
        })
    }

    /// Lookup an inode by its inode number.
    #[inline]
    #[must_use]
    pub fn get_inode(&self, inode_num: u32) -> Option<Inode<'_>> {
        (inode_num < self.metadata().inodes.len() as u32).then_some(Inode {
            index: self,
            inode_num,
        })
    }

    /// Get the low-level section index.
    #[inline]
    #[must_use]
    pub fn section_index(&self) -> &[SectionIndexEntry] {
        &self.section_index
    }

    // NB. The stored metadata is partly unpacked and/or modified.
    // It may not be a good to expose it.
    #[inline]
    #[must_use]
    fn metadata(&self) -> &Metadata {
        &self.metadata
    }
}

/// A DwarFS archive wrapping reader `R`.
///
/// Usually, you create a pair of [`Archive`] and [`ArchiveIndex`] using
/// [`Archive::new`]. Directory hierarchy traversal can be done only on
/// `ArchiveIndex`, and this struct is only needed for reading file content.
///
/// See also [`ArchiveIndex`].
#[derive(Debug)]
pub struct Archive<R: ?Sized> {
    /// LRU cache of block idx -> block content.
    cache: LruCache<u32, Vec<u8>>,
    block_size: u32,

    rdr: SectionReader<R>,
}

impl<R: ReadAt + Size> Archive<R> {
    /// Load a DwarFS archive from a random-access stream,
    /// typically a [`std::fs::File`].
    ///
    /// Note 1: Do not use [`BufReader`][std::io::BufReader], because
    /// [`Archive`] already has internal caches. Use
    /// [`Config::block_cache_size_limit`] to configure its size.
    ///
    /// Note 2: It's *discouraged* to use [`positioned_io::RandomAccessFile`] on *NIX
    /// platforms because that would disable readahead which can hurt performance on
    /// sequential read inside a several MiB section.
    /// On Windows, however, `RandomAccessFile` is several times faster than `File`.
    pub fn new(rdr: R) -> Result<(ArchiveIndex, Self)> {
        Self::new_with_config(rdr, &Config::default())
    }

    /// Same as [`Archive::new`] but with a non-default [`Config`].
    pub fn new_with_config(rdr: R, config: &Config) -> Result<(ArchiveIndex, Self)> {
        let mut rdr = SectionReader::new(rdr);
        let index = ArchiveIndex::new_with_config(&mut rdr, config)?;
        let this = Self::new_with_index_and_config(rdr, &index, config)?;
        Ok((index, this))
    }

    /// The low-level method for creating an `Archive` with already parsed `ArchiveIndex`.
    pub fn new_with_index_and_config(
        rdr: SectionReader<R>,
        index: &ArchiveIndex,
        config: &Config,
    ) -> Result<Self> {
        let block_size = index.metadata().block_size;
        let cache_len = NonZero::new(config.block_cache_size_limit / block_size as usize)
            .ok_or_else(|| {
                let msg = format!(
                    "block size {}B exceeds cache size limit {}B",
                    block_size, config.block_cache_size_limit
                );
                ErrorInner::Section(msg, None)
            })?;
        Ok(Self {
            cache: LruCache::new(cache_len),
            block_size,
            rdr,
        })
    }
}

impl<R: ReadAt + ?Sized> Archive<R> {
    /// Cache a block section's content if it's not available yet.
    ///
    /// Calculates file offset, handles cache miss and out-of-range errors on short read.
    fn cache_block(&mut self, index: &ArchiveIndex, section_idx: u32) -> Result<()> {
        // NB. Use `get` instead of `contains` to promote it to MRU.
        if self.cache.get(&section_idx).is_some() {
            trace!("block {section_idx}: cache hit");
            return Ok(());
        }

        trace_time!("block {section_idx}: cache miss");

        let section_offset = index.section_index()[section_idx as usize].offset();
        let payload_offset = section_offset + HEADER_SIZE;

        (|| {
            let header = self.rdr.read_header_at(section_offset)?;
            header.check_type(SectionType::BLOCK)?;

            // Reuse existing buffer.
            let mut buf = if self.cache.len() == self.cache.cap().get() {
                let (_, mut buf) = self.cache.pop_lru().expect("not empty");
                buf.resize(self.block_size as usize, 0);
                buf
            } else {
                vec![0u8; self.block_size as usize]
            };
            let len = self
                .rdr
                .read_payload_at_into(&header, payload_offset, &mut buf)?;
            buf.truncate(len);
            self.cache.push(section_idx, buf);

            Ok(())
        })()
        .context(format_args!("failed to read block {section_idx}"))
    }

    /// Get a chunk inside the most recently cached block.
    fn get_chunk_in_cache(&self, start: u32, end: u32) -> Result<&[u8]> {
        let (&section_idx, cache) = self.cache.peek_mru().expect("cache is empty");
        let chunk = cache.get(start as usize..end as usize).ok_or_else(
            #[cold]
            || {
                let cache_len = cache.len();
                let msg = format!(
                    "block {section_idx} has only {cache_len} bytes \
                    but is referenced at {start}..{end}",
                );
                ErrorInner::Section(msg, None)
            },
        )?;
        Ok(chunk)
    }
}

impl<R> Archive<R> {
    /// Retrieve the ownership of the underlying reader.
    #[inline]
    #[must_use]
    pub fn into_inner(self) -> R
    where
        R: Sized,
    {
        self.rdr.into_inner()
    }

    /// Get a reference to the underlying reader.
    #[inline]
    #[must_use]
    pub fn get_ref(&self) -> &R {
        self.rdr.get_ref()
    }

    /// Get a mutable reference to the underlying reader.
    #[inline]
    #[must_use]
    pub fn get_mut(&mut self) -> &mut R {
        self.rdr.get_mut()
    }
}

/// The trait for [`Inode`] sub-types.
pub trait IsInode<'a>: Sized + sealed::Sealed {
    /// Convert into a generic [`Inode`].
    #[must_use]
    fn to_inode(&self) -> Inode<'a>;

    /// Get the inode number in the DwarFS archive.
    #[must_use]
    fn inode_num(&self) -> u32 {
        self.to_inode().inode_num
    }

    /// Get the metadata of this inode.
    #[inline]
    #[must_use]
    fn metadata(&self) -> InodeMetadata<'a> {
        self.to_inode().metadata()
    }
}

macro_rules! impl_inode_subtype {
    ($name:ident) => {
        impl_inode_subtype!($name, self, { self.0 });
    };
    ($name:ident, $self_:tt, $to_inode:block) => {
        impl sealed::Sealed for $name<'_> {}
        impl<'a> IsInode<'a> for $name<'a> {
            #[inline]
            fn to_inode(&$self_) -> Inode<'a> $to_inode
        }
        impl<'a> From<$name<'a>> for Inode<'a> {
            fn from(this: $name<'a>) -> Inode<'a> {
                this.to_inode()
            }
        }
    };
}

/// A generic inode.
#[derive(Debug, Clone, Copy)]
pub struct Inode<'a> {
    index: &'a ArchiveIndex,
    inode_num: u32,
}

// This implementation is specialized. No need to generate `From` impls.
impl sealed::Sealed for Inode<'_> {}
impl<'a> IsInode<'a> for Inode<'a> {
    #[inline]
    fn to_inode(&self) -> Inode<'a> {
        *self
    }

    #[inline]
    fn inode_num(&self) -> u32 {
        self.inode_num
    }

    #[inline]
    fn metadata(&self) -> InodeMetadata<'a> {
        self.metadata()
    }
}

impl<'a> Inode<'a> {
    /// Classify this generic inode to an enum of inode kinds.
    #[must_use]
    pub fn classify(self) -> InodeKind<'a> {
        let Self { index, inode_num } = self;
        let t = &index.inode_tally;
        if inode_num < t.symlink_start {
            InodeKind::Directory(Dir(self))
        } else if inode_num < t.unique_start {
            InodeKind::Symlink(Symlink(self))
        } else if inode_num < t.device_start {
            InodeKind::File(File(self))
        } else if inode_num < t.ipc_start {
            InodeKind::Device(Device(self))
        } else {
            InodeKind::Ipc(Ipc(self))
        }
    }

    /// Shortcut method to check if this is a directory inode.
    #[must_use]
    pub fn is_dir(&self) -> bool {
        matches!(self.classify(), InodeKind::Directory(_))
    }

    /// Shortcut method to check if this is a regular file inode.
    #[must_use]
    pub fn is_file(&self) -> bool {
        matches!(self.classify(), InodeKind::File(_))
    }

    /// Shortcut method to convert to [`Dir`] if it is a directory.
    #[must_use]
    pub fn as_dir(&self) -> Option<Dir<'a>> {
        if let InodeKind::Directory(dir) = self.classify() {
            Some(dir)
        } else {
            None
        }
    }

    /// Shortcut method to convert to [`File`] if it is a regular file.
    #[must_use]
    pub fn as_file(&self) -> Option<File<'a>> {
        if let InodeKind::File(file) = self.classify() {
            Some(file)
        } else {
            None
        }
    }

    /// Get the metadata of this inode.
    #[must_use]
    pub fn metadata(&self) -> InodeMetadata<'a> {
        InodeMetadata::new(self.index, self.inode_num)
    }
}

/// An inode, classified by its kind.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum InodeKind<'a> {
    /// A directory inode.
    Directory(Dir<'a>),
    /// A symlink inode.
    Symlink(Symlink<'a>),
    /// A regular file inode.
    File(File<'a>),
    /// A block or character device inode.
    Device(Device<'a>),
    /// A pipe or socket inode.
    Ipc(Ipc<'a>),
}

impl_inode_subtype!(InodeKind, self, {
    match self {
        InodeKind::Directory(i) => i.0,
        InodeKind::Symlink(i) => i.0,
        InodeKind::File(i) => i.0,
        InodeKind::Device(i) => i.0,
        InodeKind::Ipc(i) => i.0,
    }
});

/// The minimal wrapper of DwarFS "mode", with `Debug` and `Display` impl.
///
/// In DwarFS, a "mode" encodes both file type (`S_IFMT`) and
/// permission bits, like `stat.st_mode` field from `stat(2)`.
/// This type is named so instead of `Mode` to avoid ambiguaity.
///
/// See: <https://man.archlinux.org/man/inode.7.en#The_file_type_and_mode>
///
/// For high-level wrappers, check
/// [`rustix::fs::Mode`](https://docs.rs/rustix/1/rustix/fs/struct.Mode.html)
/// and
/// [`rustix::fs::FileType`](https://docs.rs/rustix/1/rustix/fs/enum.FileType.html).
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct FileTypeMode(pub u32);

impl fmt::Debug for FileTypeMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0o{:04o}/{}", self.0, self)
    }
}

impl fmt::Octal for FileTypeMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for FileTypeMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Magic numbers are from:
        // <https://man.archlinux.org/man/inode.7.en#The_file_type_and_mode>
        let mut s = [0u8; 10];
        s[0] = match self.type_bits() {
            0o140000 => b's',
            0o120000 => b'l',
            0o100000 => b'-',
            0o060000 => b'b',
            0o040000 => b'd',
            0o020000 => b'c',
            0o010000 => b'p',
            _ => b'?',
        };
        let fmt_perm = |s: &mut [u8], m: u32, alt: u32, altch: u8| {
            s[0] = if m & 4 != 0 { b'r' } else { b'-' };
            s[1] = if m & 2 != 0 { b'w' } else { b'-' };
            let (xset, xunset) = if alt != 0 {
                (altch, altch.to_ascii_uppercase())
            } else {
                (b'x', b'-')
            };
            s[2] = if m & 1 != 0 { xset } else { xunset };
        };
        let m = self.0;
        fmt_perm(&mut s[1..4], m >> 6, m & 0o4000, b's');
        fmt_perm(&mut s[4..7], m >> 3, m & 0o2000, b's');
        fmt_perm(&mut s[7..10], m, m & 0o1000, b't');

        f.pad(str::from_utf8(&s).expect("all ASCII"))
    }
}

impl FileTypeMode {
    /// Get the bits representing the file type (`S_IFMT`).
    #[inline]
    #[must_use]
    pub fn type_bits(self) -> u32 {
        self.0 & 0o170000
    }

    /// Get the bits representing the file mode (`0o7777`).
    #[inline]
    #[must_use]
    pub fn mode_bits(self) -> u32 {
        self.0 & 0o7777
    }

    /// Get the bits representing the file permissions (`0o777`).
    #[inline]
    #[must_use]
    pub fn permission_bits(self) -> u32 {
        self.0 & 0o777
    }
}

/// The metadata of an inode.
#[derive(Debug, Clone, Copy)]
pub struct InodeMetadata<'a> {
    index: &'a ArchiveIndex,
    // Use reference to minimize size.
    data: &'a metadata::InodeData,
}

impl<'a> InodeMetadata<'a> {
    fn new(index: &'a ArchiveIndex, inode_num: u32) -> Self {
        let data = &index.metadata().inodes[inode_num as usize];
        Self { index, data }
    }

    /// The DwarFS mode of the inode, including file type and permissions.
    #[inline]
    #[must_use]
    pub fn file_type_mode(&self) -> FileTypeMode {
        FileTypeMode(self.index.metadata().modes[self.data.mode_index as usize])
    }

    /// The owner user id (uid) of the inode.
    #[inline]
    #[must_use]
    pub fn uid(&self) -> u32 {
        self.index.metadata().uids[self.data.owner_index as usize]
    }

    /// The owner group id (gid) of the inode.
    #[inline]
    #[must_use]
    pub fn gid(&self) -> u32 {
        self.index.metadata().gids[self.data.group_index as usize]
    }

    #[inline]
    fn cvt_time(&self, time_offset: u32) -> u64 {
        self.index.timestamp_base_scaled
            + u64::from(time_offset) * u64::from(self.index.time_resolution.get())
    }

    /// The last modified time, in the seconds since UNIX epoch.
    #[inline]
    #[must_use]
    pub fn mtime(&self) -> u64 {
        self.cvt_time(self.data.mtime_offset)
    }

    /// The last accessed time, in the seconds since UNIX epoch.
    ///
    /// Returns `None` if the archive stores no such information.
    #[inline]
    #[must_use]
    pub fn atime(&self) -> Option<u64> {
        (!self.index.mtime_only).then(|| self.cvt_time(self.data.atime_offset))
    }

    /// The last changed time, in the seconds since UNIX epoch.
    ///
    /// Returns `None` if the archive stores no such information.
    #[inline]
    #[must_use]
    pub fn ctime(&self) -> Option<u64> {
        (!self.index.mtime_only).then(|| self.cvt_time(self.data.ctime_offset))
    }
}

/// A directory inode.
#[derive(Debug, Clone, Copy)]
pub struct Dir<'a>(Inode<'a>);

impl_inode_subtype!(Dir);

impl<'a> Dir<'a> {
    /// Iterate all entries in this directory, in ascending order of names.
    #[inline]
    #[must_use]
    pub fn entries(&self) -> DirEntryIter<'a> {
        let ino = self.0.inode_num as usize;
        let dirs = &self.0.index.metadata().directories;
        DirEntryIter {
            index: self.0.index,
            start: dirs[ino].first_entry,
            end: dirs[ino + 1].first_entry,
        }
    }

    /// Find the entry of given name in this directory.
    ///
    /// In DwarFS, directory entries are listed in ascending order of names.
    /// So `get` performs a binary search and the time complexity is
    /// `O(min(L, L0) log N)` where `L` is the max length of entry names, `L0`
    /// is the `name.len()` and `N` is the number of entries in this directory.
    #[inline]
    #[must_use]
    pub fn get(&self, name: impl AsRef<[u8]>) -> Option<DirEntry<'a>> {
        // Manually outline.
        self.get_inner(name.as_ref())
    }

    fn get_inner(&self, name: &[u8]) -> Option<DirEntry<'a>> {
        let iter = self.entries();
        let range = iter.start as usize..iter.end as usize;
        let idx = bisect_range_by(range, |idx| {
            Ord::cmp(
                DirEntry::new(self.0.index, idx as u32).name().as_bytes(),
                name,
            )
        })?;
        Some(DirEntry::new(self.0.index, idx as u32))
    }
}

/// The iterator of directory entries.
#[derive(Debug, Clone)]
pub struct DirEntryIter<'a> {
    index: &'a ArchiveIndex,
    start: u32,
    end: u32,
}

macro_rules! impl_range_iterator {
    ($iter:ident, $item:ident) => {
        impl<'a> Iterator for $iter<'a> {
            type Item = $item<'a>;

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                (self.len(), Some(self.len()))
            }

            #[inline]
            fn next(&mut self) -> Option<Self::Item> {
                if self.start < self.end {
                    let ent = $item::new(self.index, self.start);
                    self.start += 1;
                    Some(ent)
                } else {
                    None
                }
            }

            #[inline]
            fn nth(&mut self, n: usize) -> Option<Self::Item> {
                if n < self.len() {
                    self.start += n as u32 + 1;
                    Some($item::new(self.index, self.start - 1))
                } else {
                    self.start = self.end;
                    None
                }
            }

            #[inline]
            fn count(self) -> usize
            where
                Self: Sized,
            {
                self.len()
            }
        }

        impl DoubleEndedIterator for $iter<'_> {
            #[inline]
            fn next_back(&mut self) -> Option<Self::Item> {
                if self.start < self.end {
                    self.end -= 1;
                    let ent = $item::new(self.index, self.end);
                    Some(ent)
                } else {
                    None
                }
            }

            #[inline]
            fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
                if n < self.len() {
                    self.end -= n as u32 + 1;
                    Some($item::new(self.index, self.end))
                } else {
                    self.end = self.start;
                    None
                }
            }
        }

        impl ExactSizeIterator for $iter<'_> {
            #[inline]
            fn len(&self) -> usize {
                // Never overflows because it's in memory.
                (self.end - self.start) as usize
            }
        }

        impl FusedIterator for $iter<'_> {}
    };
}

impl_range_iterator!(DirEntryIter, DirEntry);

/// An entry in a directory.
#[derive(Debug, Clone, Copy)]
pub struct DirEntry<'a> {
    index: &'a ArchiveIndex,
    // Inline `metadata::DirEntry` for `Copy`.
    name_index: u32,
    inode_num: u32,
}

impl<'a> DirEntry<'a> {
    fn new(index: &'a ArchiveIndex, ent_idx: u32) -> Self {
        let metadata::DirEntry {
            name_index,
            inode_num,
        } = index.metadata().dir_entries.as_ref().expect("validated")[ent_idx as usize].clone();
        Self {
            index,
            name_index,
            inode_num,
        }
    }

    /// Get the name of this entry.
    #[inline]
    #[must_use]
    pub fn name(&self) -> &'a str {
        let m = self.index.metadata();
        ArchiveIndex::get_from_string_table(&m.names, &m.compact_names, self.name_index)
    }

    /// Get the inode of this entry.
    #[inline]
    #[must_use]
    pub fn inode(&self) -> Inode<'a> {
        Inode {
            index: self.index,
            inode_num: self.inode_num,
        }
    }
}

/// A symlink inode.
#[derive(Debug, Clone, Copy)]
pub struct Symlink<'a>(Inode<'a>);

impl_inode_subtype!(Symlink);

impl<'a> Symlink<'a> {
    /// Get the target of this symlink.
    #[inline]
    #[must_use]
    pub fn target(&self) -> &'a str {
        let m = self.0.index.metadata();
        let symlink_idx = self.0.inode_num - self.0.index.inode_tally.symlink_start;
        let tgt_idx = m.symlink_table[symlink_idx as usize];
        ArchiveIndex::get_from_string_table(&m.symlinks, &m.compact_symlinks, tgt_idx)
    }
}

/// A character or block device inode.
///
/// To distinguish from block device and character device, you can call
/// [`metadata()`][IsInode::metadata] and check
/// [`file_type_mode()`][InodeMetadata::file_type_mode] for its file type.
#[derive(Debug, Clone, Copy)]
pub struct Device<'a>(Inode<'a>);

impl_inode_subtype!(Device);

impl Device<'_> {
    /// Get the device id this special inode represents.
    #[inline]
    #[must_use]
    pub fn device_id(&self) -> u64 {
        let device_idx = self.0.inode_num - self.0.index.inode_tally.device_start;
        self.0.index.metadata().devices.as_ref().expect("validated")[device_idx as usize]
    }
}

/// A pipe or socket inode.
///
/// To distinguish from pipe and socket inodes, you can call
/// [`metadata()`][IsInode::metadata] and check
/// [`file_type_mode()`][InodeMetadata::file_type_mode] for its file type.
#[derive(Debug, Clone, Copy)]
pub struct Ipc<'a>(Inode<'a>);

impl_inode_subtype!(Ipc);

/// A regular file inode.
///
/// This type implements [`AsChunks`], you can call
/// [`as_chunks`][AsChunks::as_chunks] or [`read_to_vec`][AsChunks::read_to_vec]
/// to read its content.
#[derive(Debug, Clone, Copy)]
pub struct File<'a>(Inode<'a>);

impl_inode_subtype!(File);

impl<'a> AsChunks<'a> for File<'a> {
    fn as_chunks(&self) -> ChunkIter<'a> {
        let tally = &self.0.index.inode_tally;
        let m = self.0.index.metadata();
        let file_idx = if let Some(shared_idx) = self.0.inode_num.checked_sub(tally.shared_start) {
            m.shared_files_table.as_ref().expect("validated")[shared_idx as usize]
                + tally.unique_cnt
        } else {
            self.0.inode_num - tally.unique_start
        };
        ChunkIter {
            index: self.0.index,
            start: m.chunk_table[file_idx as usize],
            end: m.chunk_table[file_idx as usize + 1],
        }
    }
}

/// Iterator of file content chunks.
#[derive(Debug, Clone)]
pub struct ChunkIter<'a> {
    index: &'a ArchiveIndex,
    start: u32,
    end: u32,
}

impl ChunkIter<'_> {
    /// Iterate over all chunks and return the sum of all chunks' byte length.
    pub fn total_size(&self) -> u64 {
        self.clone().map(|c| u64::from(c.size())).sum::<u64>()
    }
}

impl sealed::Sealed for ChunkIter<'_> {}
impl<'a> AsChunks<'a> for ChunkIter<'a> {
    fn as_chunks(&self) -> ChunkIter<'a> {
        self.clone()
    }
}

impl_range_iterator!(ChunkIter, Chunk);

/// The description of a chunk of bytes.
#[derive(Debug, Clone, Copy)]
pub struct Chunk<'a> {
    index: &'a ArchiveIndex,
    // Inline `metadata::Chunk` for `Copy`.
    block: u32,
    offset: u32,
    size: u32,
    // For `HasChunks` impl.
    chunk_idx: u32,
}

impl<'a> Chunk<'a> {
    fn new(index: &'a ArchiveIndex, chunk_idx: u32) -> Self {
        let metadata::Chunk {
            block,
            offset,
            size,
        } = index.metadata().chunks[chunk_idx as usize].clone();
        Self {
            index,
            block,
            offset,
            size,
            chunk_idx,
        }
    }

    /// Get the number (0-based) of section this chunk resides in.
    #[inline]
    #[must_use]
    pub fn section_idx(&self) -> u32 {
        self.block
    }

    /// Get the start offset of chunk data in the decompressed section payload.
    #[inline]
    #[must_use]
    pub fn offset(&self) -> u32 {
        self.offset
    }

    /// Get the byte size of this chunk.
    #[inline]
    #[must_use]
    pub fn size(&self) -> u32 {
        self.size
    }

    /// Read this chunk into [`Archive`]'s cache if needed and return the bytes.
    ///
    /// If the section (block) containing this chunk is already in cache, this
    /// function performs no read on the underlying stream.
    pub fn read_cached<'b, R: ReadAt>(&self, archive: &'b mut Archive<R>) -> Result<&'b [u8]> {
        archive.cache_block(self.index, self.section_idx())?;
        // Chunk offsets will not overflow, checked by `unpack_validate`.
        archive.get_chunk_in_cache(self.offset(), self.offset() + self.size())
    }
}

impl sealed::Sealed for Chunk<'_> {}
impl<'a> AsChunks<'a> for Chunk<'a> {
    fn as_chunks(&self) -> ChunkIter<'a> {
        ChunkIter {
            index: self.index,
            start: self.chunk_idx,
            end: self.chunk_idx + 1,
        }
    }
}

/// Trait for data-bearing objects, notably [`File`]s and [`Chunk`]s.
///
/// In DwarFS, regular files consist of multiple chunks of data concatenated for
/// deduplication. You can iterate over these chunks and locate section index
/// and offsets in order to retrieve the actual bytes.
///
/// This trait provides some convenient methods to access all these bytes as a
/// [`Read`] instance via [`AsChunks::as_reader`] so you can easily
/// [`std::io::copy`] it as a whole to the destination.
///
/// [`AsChunks::read_to_vec`] can also be used to efficiently read all data into
/// memory.
pub trait AsChunks<'a>: Sized + sealed::Sealed {
    /// Iterate over all chunks this object consists of.
    fn as_chunks(&self) -> ChunkIter<'a>;

    /// Get a [`Read`] instance representing the concatenation of all chunks
    /// this object consists of.
    ///
    /// The user must guarantee the owning [`ArchiveIndex`] of `self` and
    /// `archive` must come from the same DwarFS archive, or the behavior is
    /// unspecified: it may return garbage data, panic, or fail.
    fn as_reader<'b, R: ?Sized>(&self, archive: &'b mut Archive<R>) -> ChunksReader<'a, 'b, R> {
        ChunksReader {
            archive,
            chunks: self.as_chunks(),
            in_section_offset: 0,
            chunk_rest_size: 0,
        }
    }

    /// Read all data from this object into a `Vec`.
    ///
    /// This is a convenient shortcut method wrapping [`AsChunks::as_reader`]
    /// and [`Read::read_to_end`].
    ///
    /// See [`AsChunks::as_reader`] for the validity requirement on `archive`.
    fn read_to_vec<R: ReadAt + ?Sized>(
        &self,
        archive: &mut Archive<R>,
    ) -> std::io::Result<Vec<u8>> {
        let mut out = Vec::new();
        self.as_reader(archive).read_to_end(&mut out)?;
        Ok(out)
    }
}

fn read_to_end_via_buf_read(
    rdr: &mut dyn BufRead,
    out: &mut Vec<u8>,
    size: usize,
) -> std::io::Result<()> {
    out.reserve(size);
    let mut total_size = 0usize;
    loop {
        let chunk = rdr.fill_buf()?;
        if chunk.is_empty() {
            break;
        }
        out.extend_from_slice(chunk);
        let len = chunk.len();
        total_size += len;
        rdr.consume(len);
    }
    assert_eq!(total_size, size, "short read should fail in Read impl");
    Ok(())
}

/// A reader returned from [`AsChunks::as_reader`].
///
/// - This implements [`Read`] and [`BufRead`] thus can be used as a source for
///   [`std::io::copy`].
/// - This implements [`Iterator`] to read in chunks.
#[derive(Debug)]
pub struct ChunksReader<'a, 'b, R: ?Sized> {
    chunks: ChunkIter<'a>,
    in_section_offset: u32,
    chunk_rest_size: u32,
    archive: &'b mut Archive<R>,
}

impl<R: ?Sized> ChunksReader<'_, '_, R> {
    /// Iterate over all chunks and return the sum of all chunks' byte length.
    ///
    /// This number is exact, unless the DwarFS archive is changed during access
    /// or some underlying I/O failure occurs.
    pub fn total_size(&self) -> u64 {
        self.chunks.total_size() + u64::from(self.chunk_rest_size)
    }
}

impl<R: ReadAt + ?Sized> Read for ChunksReader<'_, '_, R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let cache = self.fill_buf()?;
        let len = cache.len().min(buf.len());
        buf[..len].copy_from_slice(&cache[..len]);
        self.consume(len);
        Ok(len)
    }

    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> std::io::Result<usize> {
        // `usize::MAX` will trigger a `reserve` panic in `read_to_end_via_buf_read`.
        let size = usize::try_from(self.total_size()).unwrap_or(usize::MAX);
        read_to_end_via_buf_read(self, buf, size)?;
        Ok(size)
    }
}

impl<R: ReadAt + ?Sized> BufRead for ChunksReader<'_, '_, R> {
    fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
        if self.chunk_rest_size == 0 {
            let Some(chunk) = self.chunks.next() else {
                return Ok(&[]);
            };
            self.in_section_offset = chunk.offset();
            self.chunk_rest_size = chunk.size();
            self.archive.cache_block(chunk.index, chunk.section_idx())?;
        }
        let chunk = self.archive.get_chunk_in_cache(
            self.in_section_offset,
            // Chunk offsets will not overflow, checked by `unpack_validate`.
            self.in_section_offset + self.chunk_rest_size,
        )?;
        Ok(chunk)
    }

    #[inline]
    fn consume(&mut self, amt: usize) {
        assert!(amt <= self.chunk_rest_size as usize);
        self.in_section_offset += amt as u32;
        self.chunk_rest_size -= amt as u32;
    }
}