btrfs-core 0.2.0

Pure-Rust from-scratch btrfs filesystem reader — superblock, chunk-tree logical→physical mapping, B-tree navigation, over any byte source
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
//! P2 root-tree navigation + FS-tree inode / directory / path resolution.
//!
//! The superblock's `root` logical address names the **`ROOT_TREE`**, which holds
//! a `ROOT_ITEM` (key type [`ROOT_ITEM_KEY`]) per tree; the one keyed by
//! objectid [`FS_TREE_OBJECTID`] (5) carries the `FS_TREE`'s own root `bytenr`
//! (logical) and `level`. The `FS_TREE` then holds, per inode, an `INODE_ITEM`
//! ([`INODE_ITEM_KEY`], the metadata), `INODE_REF`s ([`INODE_REF_KEY`],
//! name↔parent links), and `DIR_ITEM`/`DIR_INDEX` entries ([`DIR_ITEM_KEY`] /
//! [`DIR_INDEX_KEY`], directory children).
//!
//! Struct layouts (`btrfs_root_item`, `btrfs_inode_item`, `btrfs_timespec`,
//! `btrfs_inode_ref`, `btrfs_dir_item`) were transcribed from the on-disk ABI
//! and **verified byte-for-byte against `btrfs inspect-internal dump-tree`**
//! plus an independent mount-ro `stat` on the minted oracle (see
//! `tests/data/README.md`, "P2 ground truth").
//!
//! # Scope
//!
//! P2 navigates within the `FS_TREE` **leaf** the oracle presents (a single
//! `nodesize` node). The public `read_inode` / `list_dir` / `read_by_path`
//! entry points take a parsed [`Node`] so they work on the committed fixture and
//! on any `FS_TREE` leaf read via `read_node`. Multi-leaf FS trees (interior
//! descent within the FS tree) arrive with a later phase; here every inode /
//! directory of the oracle lives in one leaf, matching dump-tree.
//!
//! # Safety
//!
//! This parses untrusted, attacker-controllable images. Every field is read
//! through the bounds-checked [`crate::bytes`] helpers, and every `name_len` /
//! `data_len` is clamped to the item data actually present, so a lying length
//! yields a truncated name rather than an over-read or panic (the Paranoid
//! Gatekeeper standard).

use crate::bytes::{le_u16, le_u32, le_u64, u8_at};
use crate::error::BtrfsError;
use crate::node::{read_node, ChunkMap, Node};
use crate::superblock::Superblock;

/// `BTRFS_FS_TREE_OBJECTID` — the default subvolume / FS tree objectid (5).
pub const FS_TREE_OBJECTID: u64 = 5;

/// `BTRFS_FIRST_FREE_OBJECTID` — the `FS_TREE`'s root directory inode (256); the
/// first normal (non-reserved) objectid, where path resolution begins.
pub const FS_TREE_ROOT_DIR_OBJECTID: u64 = 256;

/// `BTRFS_INODE_ITEM_KEY` — a `btrfs_inode_item` (inode metadata).
pub const INODE_ITEM_KEY: u8 = 1;

/// `BTRFS_INODE_REF_KEY` — a `btrfs_inode_ref` (name + parent-dir link).
pub const INODE_REF_KEY: u8 = 12;

/// `BTRFS_XATTR_ITEM_KEY` — an extended attribute.
///
/// The item body is a `btrfs_dir_item`, the very structure a directory entry
/// uses: the same 30-byte prefix, the name, and then `data_len` bytes of value.
/// Btrfs stores the FULL attribute name (`security.selinux`), unlike ext4 and
/// XFS which store a bare name beside a namespace tag.
pub const XATTR_ITEM_KEY: u8 = 24;

/// `BTRFS_DIR_ITEM_KEY` — a `btrfs_dir_item` keyed by name hash.
pub const DIR_ITEM_KEY: u8 = 84;

/// `BTRFS_DIR_INDEX_KEY` — a `btrfs_dir_item` keyed by sequential index.
pub const DIR_INDEX_KEY: u8 = 96;

/// `BTRFS_ROOT_ITEM_KEY` — a `btrfs_root_item` in the root tree (132).
pub const ROOT_ITEM_KEY: u8 = 132;

/// `sizeof(btrfs_inode_item)` on disk = 160 bytes: the scalar block (up to
/// `reserved[4]` = 112 bytes) + four 12-byte [`Timestamp`]s.
pub const INODE_ITEM_SIZE: usize = 160;

/// `sizeof(btrfs_timespec)` on disk: `sec(u64) + nsec(u32)` = 12 bytes.
pub const TIMESTAMP_SIZE: usize = 12;

/// The `btrfs_inode_item.size` (logical file size) field offset — exposed so the
/// `EXTENT_DATA` reader can truncate assembled content to the inode's size without
/// re-decoding the whole inode.
pub(crate) const INODE_SIZE_OFFSET: usize = 16;

// `btrfs_inode_item` field offsets (verified vs dump-tree + stat).
mod inode_off {
    pub const GENERATION: usize = 0;
    pub const TRANSID: usize = 8;
    pub const SIZE: usize = 16;
    pub const NBYTES: usize = 24;
    pub const NLINK: usize = 40;
    pub const UID: usize = 44;
    pub const GID: usize = 48;
    pub const MODE: usize = 52;
    pub const FLAGS: usize = 64;
    // The four timestamps begin after the 112-byte scalar block.
    pub const ATIME: usize = 112;
    pub const CTIME: usize = 124;
    pub const MTIME: usize = 136;
    pub const OTIME: usize = 148;
}

// `btrfs_root_item` field offsets (the leading 160 bytes are an embedded
// `btrfs_inode_item`, so the root-item scalars start at 160).
mod root_off {
    pub const GENERATION: usize = 160;
    pub const ROOT_DIRID: usize = 168;
    pub const BYTENR: usize = 176;
    pub const LEVEL: usize = 238;
}

// `btrfs_dir_item` field offsets: a 17-byte location key, then `transid(u64)`,
// `data_len(u16)@25`, `name_len(u16)@27`, `type(u8)@29`, `name[]@30`.
mod dir_off {
    /// `data_len` — value length, meaningful for an `XATTR_ITEM` and zero for a
    /// plain directory entry.
    pub const DATA_LEN: usize = 25;
    pub const NAME_LEN: usize = 27;
    pub const TYPE: usize = 29;
    pub const NAME: usize = 30;
}

/// A `btrfs_timespec`: seconds since the Unix epoch + nanoseconds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Timestamp {
    /// `sec` — signed seconds since 1970-01-01 (stored as `u64` on disk; btrfs
    /// timestamps are post-epoch on real images).
    pub sec: u64,
    /// `nsec` — the nanosecond fraction (`0..1_000_000_000`).
    pub nsec: u32,
}

impl Timestamp {
    /// Decode a 12-byte `btrfs_timespec` at `off` within `data`
    /// (bounds-checked; out-of-range fields read as 0).
    #[must_use]
    fn parse(data: &[u8], off: usize) -> Self {
        Timestamp {
            sec: le_u64(data, off),
            nsec: le_u32(data, off + 8),
        }
    }
}

/// A decoded `btrfs_inode_item`: file/dir metadata + the four timestamps.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Inode {
    /// The inode's own objectid (the `FS_TREE` key objectid it was found under).
    pub objectid: u64,
    /// `generation` — the transaction id that created the inode.
    pub generation: u64,
    /// `transid` — the transaction id of the last metadata change.
    pub transid: u64,
    /// `size` — the logical file size in bytes (directory entry-name bytes for a
    /// directory).
    pub size: u64,
    /// `nbytes` — bytes actually allocated on disk (0 for a hole-only / empty).
    pub nbytes: u64,
    /// `nlink` — the hard-link count.
    pub nlink: u32,
    /// `uid` — owner user id.
    pub uid: u32,
    /// `gid` — owner group id.
    pub gid: u32,
    /// `mode` — the POSIX mode bits, including the file-type bits (e.g.
    /// `0o100644` for a regular file, `0o40755` for a directory).
    pub mode: u32,
    /// `flags` — the `BTRFS_INODE_*` flag bits.
    pub flags: u64,
    /// `atime` — last access time.
    pub atime: Timestamp,
    /// `ctime` — last status-change (inode) time.
    pub ctime: Timestamp,
    /// `mtime` — last data-modification time.
    pub mtime: Timestamp,
    /// `otime` — creation (birth) time.
    pub otime: Timestamp,
}

/// The POSIX file-type mask (`S_IFMT`): the high bits of `mode` selecting the
/// object kind.
const S_IFMT: u32 = 0o170_000;
/// `S_IFDIR` — the mode bits marking a directory.
const S_IFDIR: u32 = 0o40_000;
/// `S_IFREG` — the mode bits marking a regular file.
const S_IFREG: u32 = 0o100_000;

impl Inode {
    /// Decode a `btrfs_inode_item` (`data`) for objectid `objectid`. All four
    /// timestamps are decoded; out-of-range fields read as 0 (bounds-checked).
    #[must_use]
    fn parse(objectid: u64, data: &[u8]) -> Self {
        Inode {
            objectid,
            generation: le_u64(data, inode_off::GENERATION),
            transid: le_u64(data, inode_off::TRANSID),
            size: le_u64(data, inode_off::SIZE),
            nbytes: le_u64(data, inode_off::NBYTES),
            nlink: le_u32(data, inode_off::NLINK),
            uid: le_u32(data, inode_off::UID),
            gid: le_u32(data, inode_off::GID),
            mode: le_u32(data, inode_off::MODE),
            flags: le_u64(data, inode_off::FLAGS),
            atime: Timestamp::parse(data, inode_off::ATIME),
            ctime: Timestamp::parse(data, inode_off::CTIME),
            mtime: Timestamp::parse(data, inode_off::MTIME),
            otime: Timestamp::parse(data, inode_off::OTIME),
        }
    }

    /// `true` if the mode's type bits mark a directory.
    #[must_use]
    pub fn is_dir(&self) -> bool {
        self.mode & S_IFMT == S_IFDIR
    }

    /// `true` if the mode's type bits mark a regular file.
    #[must_use]
    pub fn is_file(&self) -> bool {
        self.mode & S_IFMT == S_IFREG
    }
}

/// The `btrfs_dir_item` `type` byte — the kind of the child a directory entry
/// points at. Only the members the oracle exercises are named; any other value
/// is preserved in [`DirItemType::Other`] with its raw byte (fail-loud: the
/// unknown value is shown, never dropped).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DirItemType {
    /// A regular file (`BTRFS_FT_REG_FILE` = 1).
    File,
    /// A directory (`BTRFS_FT_DIR` = 2).
    Dir,
    /// A symbolic link (`BTRFS_FT_SYMLINK` = 7).
    Symlink,
    /// Any other `BTRFS_FT_*` value, carrying the raw byte so an "unknown type"
    /// is identifiable rather than silently dropped.
    Other(u8),
}

impl DirItemType {
    /// Classify a raw `btrfs_dir_item` `type` byte.
    #[must_use]
    fn from_byte(b: u8) -> Self {
        match b {
            1 => DirItemType::File,
            2 => DirItemType::Dir,
            7 => DirItemType::Symlink,
            other => DirItemType::Other(other),
        }
    }
}

/// One directory entry: a child's name, its inode objectid, and its declared
/// type.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DirEntry {
    /// The entry name (UTF-8 lossy; a directory entry name is raw bytes on disk,
    /// decoded permissively so a non-UTF-8 name is surfaced, not dropped).
    pub name: String,
    /// The child inode's objectid (`location.objectid` of the dir item).
    pub child: u64,
    /// The child's declared type.
    pub item_type: DirItemType,
}

/// The `FS_TREE` root as located in the root tree: where the FS tree's own root
/// node lives and how deep it is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct FsTreeRoot {
    /// `bytenr` — the `FS_TREE` root node's **logical** address.
    pub bytenr: u64,
    /// `level` — the `FS_TREE` root node's B-tree level (0 = a single leaf).
    pub level: u8,
    /// `root_dirid` — the FS tree's root directory objectid (256).
    pub root_dirid: u64,
    /// `generation` — the transaction id the FS tree root was written in.
    pub generation: u64,
}

/// Locate the `FS_TREE` ([`FS_TREE_OBJECTID`]) `ROOT_ITEM` by walking the root
/// tree from the superblock's `root` logical address, returning where the FS
/// tree's own root node lives.
///
/// Reads the root-tree node via [`read_node`]; if it is a leaf, scans its
/// `ROOT_ITEM`s for the `FS_TREE` entry. (The oracle's root tree is a single
/// leaf; interior root-tree descent is a later phase — an `FS_TREE` `ROOT_ITEM` in
/// an interior root tree would require following key-pointers, out of P2 scope.)
///
/// # Errors
///
/// - Any error from [`read_node`] translating/reading `sb.root`.
/// - [`BtrfsError::Truncated`] naming `FS_TREE ROOT_ITEM` when the root-tree
///   leaf carries no `FS_TREE` `ROOT_ITEM` (a loud miss, never a silent `None`).
pub fn fs_tree_root(
    image: &[u8],
    sb: &Superblock,
    chunk_map: &ChunkMap,
) -> Result<FsTreeRoot, BtrfsError> {
    let node = read_node(image, sb, chunk_map, sb.root)?;
    for (key, data) in node.leaf_items() {
        if key.objectid == FS_TREE_OBJECTID && key.key_type == ROOT_ITEM_KEY {
            return Ok(FsTreeRoot {
                bytenr: le_u64(data, root_off::BYTENR),
                level: u8_at(data, root_off::LEVEL),
                root_dirid: le_u64(data, root_off::ROOT_DIRID),
                generation: le_u64(data, root_off::GENERATION),
            });
        }
    }
    Err(BtrfsError::Truncated {
        structure: "FS_TREE ROOT_ITEM (objectid 5) in root tree",
        need: FS_TREE_OBJECTID as usize,
        have: node.header.nritems as usize,
    })
}

/// Read the `INODE_ITEM` for `objectid` from an `FS_TREE` `leaf`, or `None` if the
/// leaf holds no inode item for it.
#[must_use]
pub fn read_inode(leaf: &Node, objectid: u64) -> Option<Inode> {
    leaf.leaf_items()
        .find(|(key, _)| key.objectid == objectid && key.key_type == INODE_ITEM_KEY)
        .map(|(_, data)| Inode::parse(objectid, data))
}

/// List a directory's entries from an `FS_TREE` `leaf`: every `DIR_ITEM` /
/// `DIR_INDEX` keyed by `dir_objectid`, deduplicated by name (both a `DIR_ITEM`
/// and a `DIR_INDEX` name each child; the listing surfaces each name once).
///
/// Bounds-checked: an out-of-range `name_len` yields a name clamped to the item
/// data actually present, never an over-read.
#[must_use]
pub fn list_dir(leaf: &Node, dir_objectid: u64) -> Vec<DirEntry> {
    let mut out: Vec<DirEntry> = Vec::new();
    for (key, data) in leaf.leaf_items() {
        if key.objectid != dir_objectid
            || (key.key_type != DIR_ITEM_KEY && key.key_type != DIR_INDEX_KEY)
        {
            continue;
        }
        let Some(entry) = parse_dir_item(data) else {
            continue;
        };
        if out
            .iter()
            .any(|e| e.name == entry.name && e.child == entry.child)
        {
            continue;
        }
        out.push(entry);
    }
    out
}

/// Decode a single `btrfs_dir_item` body into a [`DirEntry`], or `None` if the
/// item is too short to hold even the fixed prefix.
///
/// The name is clamped to whatever bytes remain after the fixed prefix, so a
/// lying `name_len` truncates rather than over-reads.
#[must_use]
fn parse_dir_item(data: &[u8]) -> Option<DirEntry> {
    if data.len() < dir_off::NAME {
        return None;
    }
    let child = le_u64(data, 0); // location.objectid (start of the 17-byte key)
    let type_byte = u8_at(data, dir_off::TYPE);
    let name_len = le_u16(data, dir_off::NAME_LEN) as usize;

    // The name occupies `name_len` bytes right after the fixed prefix (the
    // optional xattr `data_len` bytes follow it). Clamp the name to the bytes
    // actually present so a hostile `name_len` truncates rather than over-reads.
    let name_start = dir_off::NAME;
    let avail = data.len().saturating_sub(name_start);
    let take = name_len.min(avail);
    let name_bytes = data.get(name_start..name_start + take).unwrap_or(&[]);

    Some(DirEntry {
        name: String::from_utf8_lossy(name_bytes).into_owned(),
        child,
        item_type: DirItemType::from_byte(type_byte),
    })
}

/// One extended attribute: its full name and its value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Xattr {
    /// The complete attribute name as the system presents it, e.g.
    /// `security.selinux`. Btrfs stores it whole, so nothing is reconstructed.
    pub name: String,
    /// The attribute's value bytes.
    pub value: Vec<u8>,
}

/// List every extended attribute attached to `objectid` in this leaf.
///
/// An inode with no attributes yields an empty vector; that is not an error.
///
/// Only items keyed to `objectid` are returned. A leaf interleaves many inodes'
/// items, so filtering on `key_type` alone would hand one file's attributes to
/// another.
///
/// **Several attributes can share one item.** The key is a hash of the name, so
/// a collision chains a second `btrfs_dir_item` immediately after the first —
/// exactly as `DIR_ITEM` does. The walk below continues through the item body
/// rather than stopping at the first record, because stopping would silently
/// drop the colliding attribute.
#[must_use]
pub fn list_xattrs(leaf: &Node, objectid: u64) -> Vec<Xattr> {
    let mut out = Vec::new();
    for (key, data) in leaf.leaf_items() {
        if key.objectid != objectid || key.key_type != XATTR_ITEM_KEY {
            continue;
        }
        let mut off = 0usize;
        // A malformed record ENDS this item's walk: the records are packed with
        // no terminator, so once one length is untrustworthy there is nowhere
        // safe to resume, and continuing would emit an attribute assembled from
        // the wrong bytes.
        while let Some(rest) = data.get(off..) {
            let Some((x, consumed)) = parse_xattr_item(rest) else {
                break;
            };
            out.push(x);
            if consumed == 0 {
                break; // cov:unreachable: parse_xattr_item rejects name_len == 0, so consumed >= dir_off::NAME + 1
            }
            off += consumed;
        }
    }
    out
}

/// Parse one `btrfs_dir_item` as an extended attribute, returning it and the
/// number of bytes it occupied.
///
/// The 30-byte prefix length is not assumed: for every item in the reference
/// leaf `itemsize == 30 + name_len + data_len` holds exactly.
fn parse_xattr_item(data: &[u8]) -> Option<(Xattr, usize)> {
    if data.len() < dir_off::NAME {
        return None;
    }
    let data_len = le_u16(data, dir_off::DATA_LEN) as usize;
    let name_len = le_u16(data, dir_off::NAME_LEN) as usize;
    // A zero-length name is not a valid attribute; treating it as one would
    // emit a nameless entry and, with a zero data_len, never advance.
    if name_len == 0 {
        return None;
    }
    let name_end = dir_off::NAME.checked_add(name_len)?;
    let value_end = name_end.checked_add(data_len)?;
    // Both the name and the value must be fully present. A truncated record is
    // refused rather than padded: a short value would be reported as the
    // attribute's real contents.
    let name = data.get(dir_off::NAME..name_end)?;
    let value = data.get(name_end..value_end)?;
    Some((
        Xattr {
            name: String::from_utf8_lossy(name).into_owned(),
            value: value.to_vec(),
        },
        value_end,
    ))
}

/// Byte offsets within a `btrfs_inode_ref` record (packed, little-endian).
mod inode_ref_off {
    /// `__le64 index` — the directory index this name occupies.
    pub const INDEX: usize = 0x00;
    /// `__le16 name_len`.
    pub const NAME_LEN: usize = 0x08;
    /// The name bytes begin here; the record is `NAME + name_len` long.
    pub const NAME: usize = 0x0A;
}

/// One name an inode is known by, and the directory holding it.
///
/// A btrfs inode with `nlink > 1` has one of these per link. This is the
/// back-reference the filesystem stores deliberately, so the answer is a direct
/// read rather than a directory walk.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InodeRef {
    /// Objectid of the directory containing this name (the item key's offset).
    pub parent: u64,
    /// The directory index this name occupies within `parent`.
    pub index: u64,
    /// The name itself.
    pub name: String,
}

/// List every name `objectid` is known by within this leaf.
///
/// An inode the leaf does not describe yields an empty vector; that is not an
/// error. Only items keyed to `objectid` are returned — a leaf interleaves many
/// inodes' items, so filtering on `key_type` alone would hand one file's names
/// to another.
///
/// **Several names can share one item.** `INODE_REF` items are keyed by
/// `(objectid, INODE_REF, parent_dir)`, so every link inside the *same*
/// directory is packed into one item as consecutive `btrfs_inode_ref` records.
/// In the reference leaf item 15 is 42 bytes holding both `hardlink.txt`
/// (10 + 12) and `target.txt` (10 + 10). A walk that stopped at the first
/// record would report two links where the volume has three.
#[must_use]
pub fn list_inode_refs(leaf: &Node, objectid: u64) -> Vec<InodeRef> {
    let mut out = Vec::new();
    for (key, data) in leaf.leaf_items() {
        if key.objectid != objectid || key.key_type != INODE_REF_KEY {
            continue;
        }
        let mut off = 0usize;
        // A malformed record ENDS this item's walk: the records are packed with
        // no terminator, so once one length is untrustworthy there is nowhere
        // safe to resume, and continuing would emit a name assembled from the
        // wrong bytes.
        while let Some(rest) = data.get(off..) {
            let Some((r, consumed)) = parse_inode_ref(rest, key.offset) else {
                break;
            };
            out.push(r);
            if consumed == 0 {
                break; // cov:unreachable: parse_inode_ref rejects name_len == 0, so consumed >= inode_ref_off::NAME + 1
            }
            off += consumed;
        }
    }
    out
}

/// Parse one `btrfs_inode_ref`, returning it and the bytes it occupied.
///
/// `parent` comes from the item key's `offset` field, not the record: the record
/// carries only the index and the name.
fn parse_inode_ref(data: &[u8], parent: u64) -> Option<(InodeRef, usize)> {
    if data.len() < inode_ref_off::NAME {
        return None;
    }
    let index = le_u64(data, inode_ref_off::INDEX);
    let name_len = le_u16(data, inode_ref_off::NAME_LEN) as usize;
    // A zero-length name is not a valid link; emitting one would produce a
    // nameless entry and never advance the walk.
    if name_len == 0 {
        return None;
    }
    let name_end = inode_ref_off::NAME.checked_add(name_len)?;
    // A truncated record is refused rather than padded: a short name would be
    // reported as the link's real name.
    let name = data.get(inode_ref_off::NAME..name_end)?;
    Some((
        InodeRef {
            parent,
            index,
            name: String::from_utf8_lossy(name).into_owned(),
        },
        name_end,
    ))
}

/// Resolve a slash-separated `path` to `(objectid, Inode)` within an `FS_TREE`
/// `leaf`, starting from the `FS_TREE` root directory ([`FS_TREE_ROOT_DIR_OBJECTID`]).
///
/// A leading slash is optional and redundant / trailing separators are ignored.
/// The empty path (`""` or `"/"`) resolves to the root directory itself.
/// Returns `None` if any component is missing or descends through a non-directory.
#[must_use]
pub fn read_by_path(leaf: &Node, path: &str) -> Option<(u64, Inode)> {
    let mut current = FS_TREE_ROOT_DIR_OBJECTID;
    let mut inode = read_inode(leaf, current)?;

    for component in path.split('/').filter(|c| !c.is_empty()) {
        // To descend, the current inode must be a directory.
        if !inode.is_dir() {
            return None;
        }
        let entry = list_dir(leaf, current)
            .into_iter()
            .find(|e| e.name == component)?;
        current = entry.child;
        inode = read_inode(leaf, current)?;
    }

    Some((current, inode))
}

#[cfg(test)]
mod unit {
    use super::{dir_off, inode_ref_off, parse_inode_ref, parse_xattr_item};

    #[test]
    fn an_xattr_record_with_a_zero_length_name_is_refused() {
        // A full 30-byte prefix with name_len = 0 and data_len = 4.
        let mut rec = vec![0u8; dir_off::NAME + 4];
        rec[dir_off::DATA_LEN..dir_off::DATA_LEN + 2].copy_from_slice(&4u16.to_le_bytes());
        assert!(parse_xattr_item(&rec).is_none());
    }

    #[test]
    fn an_inode_ref_with_a_zero_length_name_is_refused() {
        let rec = vec![0u8; inode_ref_off::NAME + 4]; // name_len = 0
        assert!(parse_inode_ref(&rec, 256).is_none());
    }
}