ext4fs-core 0.2.4

Forensic-grade ext4 filesystem parser
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
#![forbid(unsafe_code)]
use crate::block::BlockReader;
use crate::error::{Ext4Error, Result};
use crate::ondisk::xattr::XattrEntry;
use crate::ondisk::{ExtentHeader, ExtentIndex, ExtentLeaf, Inode};
use std::io::{Read, Seek};

// ---------------------------------------------------------------------------
// BlockMapping
// ---------------------------------------------------------------------------

/// A single logical→physical block mapping produced by the extent tree or
/// indirect-block walk.
#[derive(Debug, Clone)]
pub struct BlockMapping {
    pub logical_block: u64,
    pub physical_block: u64,
    pub length: u64,
    pub unwritten: bool,
}

// ---------------------------------------------------------------------------
// Inline data overflow helper
// ---------------------------------------------------------------------------

/// Search the ibody xattr region for a `system.data` xattr (inline data overflow).
///
/// The ibody slice should start immediately after the fixed+extended inode
/// fields, i.e. at offset `0x80 + extra_isize` within the raw inode bytes.
fn find_system_data_xattr(ibody: &[u8]) -> Option<Vec<u8>> {
    let mut offset = 0;
    while offset + 16 <= ibody.len() {
        // A zero first byte (name_len == 0) signals end of entries.
        if ibody[offset] == 0 {
            break;
        }
        match XattrEntry::parse(&ibody[offset..]) {
            Ok(entry) => {
                // system.data: System namespace (index 7) with name "data"
                if entry.name == b"data"
                    && matches!(
                        entry.name_index,
                        crate::ondisk::xattr::XattrNamespace::System
                    )
                {
                    let vs = entry.value_offset as usize;
                    let ve = vs + entry.value_size as usize;
                    if ve <= ibody.len() {
                        return Some(ibody[vs..ve].to_vec());
                    }
                }
                offset += entry.entry_size;
            }
            Err(_) => break,
        }
    }
    None
}

// ---------------------------------------------------------------------------
// InodeReader
// ---------------------------------------------------------------------------

pub struct InodeReader<R: Read + Seek> {
    pub(crate) block_reader: BlockReader<R>,
}

impl<R: Read + Seek> InodeReader<R> {
    /// Wrap a `BlockReader` to provide inode-level access.
    pub fn new(br: BlockReader<R>) -> Self {
        Self { block_reader: br }
    }

    /// Borrow the underlying `BlockReader`.
    pub fn block_reader(&self) -> &BlockReader<R> {
        &self.block_reader
    }

    /// Mutably borrow the underlying `BlockReader`.
    pub fn block_reader_mut(&mut self) -> &mut BlockReader<R> {
        &mut self.block_reader
    }

    // -----------------------------------------------------------------------
    // Inode lookup
    // -----------------------------------------------------------------------

    /// Read and parse inode `ino` (1-based).
    ///
    /// Returns `Ext4Error::InodeOutOfRange` when `ino == 0` or exceeds
    /// the filesystem's inode count.
    pub fn read_inode(&self, ino: u64) -> Result<Inode> {
        let sb = self.block_reader.superblock();
        let max = u64::from(sb.inodes_count);
        if ino == 0 || ino > max {
            return Err(Ext4Error::InodeOutOfRange { ino, max });
        }

        let inodes_per_group = u64::from(sb.inodes_per_group);
        let inode_size = u64::from(sb.inode_size);
        let block_size = u64::from(sb.block_size);

        let group = ((ino - 1) / inodes_per_group) as u32;
        let index = (ino - 1) % inodes_per_group;

        let inode_table = self.block_reader.inode_table_block(group)?;
        let offset = inode_table * block_size + index * inode_size;

        let buf = self.block_reader.read_bytes(offset, inode_size as usize)?;
        Inode::parse(&buf, self.block_reader.superblock().inode_size)
    }

    // -----------------------------------------------------------------------
    // Raw inode access
    // -----------------------------------------------------------------------

    /// Read the raw inode bytes for an inode number.
    /// Returns the full inode-sized buffer from the inode table.
    pub fn read_inode_raw(&self, ino: u64) -> Result<Vec<u8>> {
        let sb = self.block_reader.superblock();
        let inode_size = u64::from(sb.inode_size);
        let inodes_per_group = u64::from(sb.inodes_per_group);
        let max = u64::from(sb.inodes_count);
        if ino < 1 || ino > max {
            return Err(Ext4Error::InodeOutOfRange { ino, max });
        }
        let group = ((ino - 1) / inodes_per_group) as u32;
        let index = (ino - 1) % inodes_per_group;
        let inode_table_block = self.block_reader.inode_table_block(group)?;
        let block_size = u64::from(self.block_reader.superblock().block_size);
        let byte_offset = inode_table_block * block_size + index * inode_size;
        self.block_reader
            .read_bytes(byte_offset, inode_size as usize)
    }

    // -----------------------------------------------------------------------
    // Block map / extent tree
    // -----------------------------------------------------------------------

    /// Return the full logical→physical block mapping for inode `ino`.
    pub fn inode_block_map(&self, ino: u64) -> Result<Vec<BlockMapping>> {
        let inode = self.read_inode(ino)?;
        if inode.uses_extents() {
            self.walk_extent_tree(&inode.i_block)
        } else {
            self.walk_indirect_blocks(&inode.i_block)
        }
    }

    /// Walk the extent tree rooted at `i_block` (60-byte raw field).
    ///
    /// The first 12 bytes are the `ExtentHeader`; subsequent 12-byte slots
    /// are either `ExtentLeaf` entries (depth == 0) or `ExtentIndex` entries
    /// (depth > 0) whose child blocks must be read and recursed into.
    pub fn walk_extent_tree(&self, i_block: &[u8; 60]) -> Result<Vec<BlockMapping>> {
        let mut mappings = Vec::new();
        self.walk_extent_node(i_block.as_slice(), &mut mappings)?;
        Ok(mappings)
    }

    fn walk_extent_node(&self, buf: &[u8], out: &mut Vec<BlockMapping>) -> Result<()> {
        let header = ExtentHeader::parse(buf)?;
        let entries = header.entries as usize;

        if header.depth == 0 {
            // Leaf node — entries are ExtentLeaf structs at offsets 12, 24, …
            for i in 0..entries {
                let off = 12 + i * 12;
                if off + 12 > buf.len() {
                    break;
                }
                let leaf = ExtentLeaf::parse(&buf[off..]);
                out.push(BlockMapping {
                    logical_block: u64::from(leaf.logical_block),
                    physical_block: leaf.physical_block,
                    length: u64::from(leaf.length),
                    unwritten: leaf.unwritten,
                });
            }
        } else {
            // Internal node — entries are ExtentIndex structs; recurse into children.
            for i in 0..entries {
                let off = 12 + i * 12;
                if off + 12 > buf.len() {
                    break;
                }
                let idx = ExtentIndex::parse(&buf[off..]);
                let child_data = self.block_reader.read_block(idx.child_block)?;
                self.walk_extent_node(&child_data, out)?;
            }
        }
        Ok(())
    }

    /// Walk legacy indirect-block pointers stored in `i_block` (60 bytes).
    ///
    /// Layout (u32 LE pointers):
    ///   - offsets  0..48 (12 direct pointers)
    ///   - offset  48     single-indirect pointer
    ///   - offset  52     double-indirect pointer
    ///   - offset  56     triple-indirect pointer
    pub fn walk_indirect_blocks(&self, i_block: &[u8; 60]) -> Result<Vec<BlockMapping>> {
        let block_size = self.block_reader.block_size() as usize;
        let ptrs_per_block = block_size / 4;

        let read_u32 = |buf: &[u8], off: usize| -> u32 {
            u32::from_le_bytes([buf[off], buf[off + 1], buf[off + 2], buf[off + 3]])
        };

        let mut out = Vec::new();
        let mut logical = 0u64;

        // 12 direct pointers
        for i in 0..12usize {
            let ptr = u64::from(read_u32(i_block, i * 4));
            if ptr != 0 {
                out.push(BlockMapping {
                    logical_block: logical,
                    physical_block: ptr,
                    length: 1,
                    unwritten: false,
                });
            }
            logical += 1;
        }

        // Single indirect
        let sind = u64::from(read_u32(i_block, 48));
        if sind != 0 {
            let blk = self.block_reader.read_block(sind)?;
            for i in 0..ptrs_per_block {
                let ptr = u64::from(read_u32(&blk, i * 4));
                if ptr != 0 {
                    out.push(BlockMapping {
                        logical_block: logical,
                        physical_block: ptr,
                        length: 1,
                        unwritten: false,
                    });
                }
                logical += 1;
            }
        } else {
            logical += ptrs_per_block as u64;
        }

        // Double indirect
        let dind = u64::from(read_u32(i_block, 52));
        if dind != 0 {
            let l1 = self.block_reader.read_block(dind)?;
            for i in 0..ptrs_per_block {
                let ptr1 = u64::from(read_u32(&l1, i * 4));
                if ptr1 != 0 {
                    let l2 = self.block_reader.read_block(ptr1)?;
                    for j in 0..ptrs_per_block {
                        let ptr2 = u64::from(read_u32(&l2, j * 4));
                        if ptr2 != 0 {
                            out.push(BlockMapping {
                                logical_block: logical,
                                physical_block: ptr2,
                                length: 1,
                                unwritten: false,
                            });
                        }
                        logical += 1;
                    }
                } else {
                    logical += ptrs_per_block as u64;
                }
            }
        } else {
            logical += (ptrs_per_block * ptrs_per_block) as u64;
        }

        // Triple indirect
        let tind = u64::from(read_u32(i_block, 56));
        if tind != 0 {
            let l1 = self.block_reader.read_block(tind)?;
            for i in 0..ptrs_per_block {
                let ptr1 = u64::from(read_u32(&l1, i * 4));
                if ptr1 != 0 {
                    let l2 = self.block_reader.read_block(ptr1)?;
                    for j in 0..ptrs_per_block {
                        let ptr2 = u64::from(read_u32(&l2, j * 4));
                        if ptr2 != 0 {
                            let l3 = self.block_reader.read_block(ptr2)?;
                            for k in 0..ptrs_per_block {
                                let ptr3 = u64::from(read_u32(&l3, k * 4));
                                if ptr3 != 0 {
                                    out.push(BlockMapping {
                                        logical_block: logical,
                                        physical_block: ptr3,
                                        length: 1,
                                        unwritten: false,
                                    });
                                }
                                logical += 1;
                            }
                        } else {
                            logical += ptrs_per_block as u64;
                        }
                    }
                } else {
                    logical += (ptrs_per_block * ptrs_per_block) as u64;
                }
            }
        }

        Ok(out)
    }

    // -----------------------------------------------------------------------
    // Data reading
    // -----------------------------------------------------------------------

    /// Read the entire data of inode `ino`, truncated to `inode.size`.
    ///
    /// For inodes with the `INLINE_DATA` flag the content is the first
    /// `size` bytes of `i_block` (up to 60 bytes), returned directly.
    pub fn read_inode_data(&self, ino: u64) -> Result<Vec<u8>> {
        let inode = self.read_inode(ino)?;
        if inode.has_inline_data() {
            let len = (inode.size as usize).min(60);
            let mut data = inode.i_block[..len].to_vec();
            // For inline data files > 60 bytes, overflow is in system.data xattr
            if inode.size > 60 {
                if let Ok(raw) = self.read_inode_raw(ino) {
                    let inode_size = self.block_reader.superblock().inode_size as usize;
                    let ibody_offset = 0x80 + inode.extra_isize as usize;
                    if inode_size > ibody_offset {
                        let ibody = &raw[ibody_offset..inode_size.min(raw.len())];
                        if let Some(value) = find_system_data_xattr(ibody) {
                            data.extend_from_slice(&value);
                        }
                    }
                }
            }
            data.truncate(inode.size as usize);
            return Ok(data);
        }
        let size = inode.size as usize;
        let _block_size = self.block_reader.block_size() as usize;
        let map = if inode.uses_extents() {
            self.walk_extent_tree(&inode.i_block)?
        } else {
            self.walk_indirect_blocks(&inode.i_block)?
        };

        let mut data: Vec<u8> = Vec::with_capacity(size);
        for mapping in &map {
            for blk_offset in 0..mapping.length {
                let phys = mapping.physical_block + blk_offset;
                let blk_data = self.block_reader.read_block(phys)?;
                data.extend_from_slice(&blk_data);
                if data.len() >= size {
                    data.truncate(size);
                    return Ok(data);
                }
            }
        }
        data.truncate(size);
        Ok(data)
    }

    /// Read a byte range `[offset, offset+len)` from inode `ino`'s data.
    ///
    /// This is more efficient than `read_inode_data` for partial reads
    /// (e.g. FUSE `read` calls) because it skips blocks that fall entirely
    /// outside the requested window.
    pub fn read_inode_data_range(&self, ino: u64, offset: u64, len: usize) -> Result<Vec<u8>> {
        let inode = self.read_inode(ino)?;
        if inode.has_inline_data() {
            let start = offset as usize;
            let end = (start + len).min(60).min(inode.size as usize);
            if start >= end {
                return Ok(Vec::new());
            }
            return Ok(inode.i_block[start..end].to_vec());
        }

        let file_size = inode.size;
        if offset >= file_size {
            return Ok(Vec::new());
        }
        let want_end = (offset + len as u64).min(file_size);
        let want_len = (want_end - offset) as usize;

        let block_size = u64::from(self.block_reader.block_size());
        let map = if inode.uses_extents() {
            self.walk_extent_tree(&inode.i_block)?
        } else {
            self.walk_indirect_blocks(&inode.i_block)?
        };

        let mut out = vec![0u8; want_len];
        let mut written = 0usize;

        for mapping in &map {
            for blk_offset in 0..mapping.length {
                let logical = (mapping.logical_block + blk_offset) * block_size;
                let logical_end = logical + block_size;
                if logical_end <= offset || logical >= want_end {
                    continue;
                }
                let src_start = if logical < offset {
                    (offset - logical) as usize
                } else {
                    0
                };
                let dst_start = if logical > offset {
                    (logical - offset) as usize
                } else {
                    0
                };
                let phys = mapping.physical_block + blk_offset;
                let blk_data = self.block_reader.read_block(phys)?;
                let src_end = blk_data.len().min(src_start + (want_len - dst_start));
                let copy_len = src_end - src_start;
                if dst_start + copy_len <= out.len() {
                    out[dst_start..dst_start + copy_len]
                        .copy_from_slice(&blk_data[src_start..src_end]);
                    written += copy_len;
                }
            }
        }
        out.truncate(written.min(want_len));
        Ok(out)
    }

    // -----------------------------------------------------------------------
    // Bitmap operations
    // -----------------------------------------------------------------------

    /// Return `true` if inode `ino` (1-based) is marked allocated in its
    /// group's inode bitmap.
    pub fn is_inode_allocated(&self, ino: u64) -> Result<bool> {
        let sb = self.block_reader.superblock();
        let max = u64::from(sb.inodes_count);
        if ino == 0 || ino > max {
            return Err(Ext4Error::InodeOutOfRange { ino, max });
        }
        let inodes_per_group = u64::from(sb.inodes_per_group);
        let group = ((ino - 1) / inodes_per_group) as u32;
        let index = ((ino - 1) % inodes_per_group) as usize;

        let bitmap_block = self.block_reader.inode_bitmap_block(group)?;
        let bitmap = self.block_reader.read_block(bitmap_block)?;
        Ok((bitmap[index / 8] >> (index % 8)) & 1 == 1)
    }

    /// Return `true` if block `block` is marked allocated in its group's
    /// block bitmap.
    pub fn is_block_allocated(&self, block: u64) -> Result<bool> {
        let sb = self.block_reader.superblock();
        let blocks_per_group = u64::from(sb.blocks_per_group);
        let group = (block / blocks_per_group) as u32;
        let index = (block % blocks_per_group) as usize;

        let bitmap_block = self.block_reader.block_bitmap_block(group)?;
        let bitmap = self.block_reader.read_block(bitmap_block)?;
        Ok((bitmap[index / 8] >> (index % 8)) & 1 == 1)
    }

    // -----------------------------------------------------------------------
    // Iteration helpers
    // -----------------------------------------------------------------------

    /// Return all valid inodes in `group` as `(ino, Inode)` pairs.
    ///
    /// An entry is skipped when `mode == 0 && dtime == 0` (empty slot).
    pub fn iter_inodes_in_group(&self, group: u32) -> Result<Vec<(u64, Inode)>> {
        let sb = self.block_reader.superblock();
        let inodes_per_group = u64::from(sb.inodes_per_group);
        let inode_size = sb.inode_size as usize;
        let block_size = u64::from(sb.block_size);
        let first_ino = u64::from(group) * inodes_per_group + 1;

        let inode_table = self.block_reader.inode_table_block(group)?;
        let table_bytes = inode_size as u64 * inodes_per_group;
        let table_offset = inode_table * block_size;
        let buf = self
            .block_reader
            .read_bytes(table_offset, table_bytes as usize)?;

        let stored_inode_size = self.block_reader.superblock().inode_size;
        let mut result = Vec::new();
        for i in 0..inodes_per_group as usize {
            let off = i * inode_size;
            let slice = &buf[off..off + inode_size];
            // Skip empty slots: mode == 0 and dtime == 0
            let mode = u16::from_le_bytes([slice[0], slice[1]]);
            let dtime = u32::from_le_bytes([slice[0x14], slice[0x15], slice[0x16], slice[0x17]]);
            if mode == 0 && dtime == 0 {
                continue;
            }
            if let Ok(inode) = Inode::parse(slice, stored_inode_size) {
                result.push((first_ino + i as u64, inode));
            }
        }
        Ok(result)
    }

    /// Return all valid inodes across all block groups.
    pub fn iter_all_inodes(&self) -> Result<Vec<(u64, Inode)>> {
        let group_count = self.block_reader.group_count();
        let mut all = Vec::new();
        for g in 0..group_count {
            let inodes = self.iter_inodes_in_group(g)?;
            all.extend(inodes);
        }
        Ok(all)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dir::DirReader;
    use crate::ondisk::FileType;
    use std::io::Cursor;

    fn open_minimal() -> InodeReader<Cursor<Vec<u8>>> {
        let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/data/minimal.img");
        let data = std::fs::read(path).expect("minimal.img required");
        let br = BlockReader::open(Cursor::new(data)).unwrap();
        InodeReader::new(br)
    }

    /// Resolve a path on minimal.img and return its inode number.
    fn resolve_minimal(path: &str) -> u64 {
        let img_path = concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/data/minimal.img");
        let data = std::fs::read(img_path).expect("minimal.img required");
        let br = BlockReader::open(Cursor::new(data)).unwrap();
        let ir = InodeReader::new(br);
        let dr = DirReader::new(ir);
        dr.resolve_path(path).unwrap()
    }

    /// Resolve a path on forensic.img and return its inode number.
    fn resolve_forensic(path: &str) -> u64 {
        let img_path = concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/data/forensic.img");
        let data = std::fs::read(img_path).expect("forensic.img required");
        let br = BlockReader::open(Cursor::new(data)).unwrap();
        let ir = InodeReader::new(br);
        let dr = DirReader::new(ir);
        dr.resolve_path(path).unwrap()
    }

    #[test]
    fn read_root_inode() {
        let r = open_minimal();
        let inode = r.read_inode(2).unwrap();
        assert_eq!(inode.file_type(), FileType::Directory);
        assert!(inode.links_count >= 2);
    }

    #[test]
    fn read_inode_out_of_range() {
        let r = open_minimal();
        let err = r.read_inode(0).unwrap_err();
        assert!(matches!(err, Ext4Error::InodeOutOfRange { .. }));
    }

    #[test]
    fn read_file_data() {
        let r = open_minimal();
        let data = r.read_inode_data(2).unwrap();
        assert!(!data.is_empty());
    }

    #[test]
    fn inode_block_map_for_root() {
        let r = open_minimal();
        let inode = r.read_inode(2).unwrap();
        if inode.uses_extents() {
            let map = r.inode_block_map(2).unwrap();
            assert!(!map.is_empty());
            assert!(map[0].physical_block > 0);
        }
    }

    #[test]
    fn is_inode_allocated() {
        let r = open_minimal();
        assert!(r.is_inode_allocated(2).unwrap());
    }

    #[test]
    fn read_inode_data_extent_path_returns_data() {
        let reader = open_minimal();
        // Root inode (2) uses extents — verify the non-inline path works
        let inode = reader.read_inode(2).unwrap();
        assert!(!inode.has_inline_data());
        assert!(inode.uses_extents());
        let data = reader.read_inode_data(2).unwrap();
        assert_eq!(data.len(), inode.size as usize);
        assert!(!data.is_empty());
    }

    // -------------------------------------------------------------------
    // Helper: open forensic.img
    // -------------------------------------------------------------------

    fn open_forensic() -> InodeReader<Cursor<Vec<u8>>> {
        let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../tests/data/forensic.img");
        let data = std::fs::read(path).expect("forensic.img required");
        let br = BlockReader::open(Cursor::new(data)).unwrap();
        InodeReader::new(br)
    }

    // -------------------------------------------------------------------
    // 1. block_reader() and block_reader_mut() accessors
    // -------------------------------------------------------------------

    #[test]
    fn block_reader_accessor() {
        let r = open_minimal();
        let sb = r.block_reader().superblock();
        assert!(sb.block_size >= 1024, "block size should be at least 1024");
        assert!(sb.inodes_count > 0);
    }

    #[test]
    fn block_reader_mut_accessor() {
        let mut r = open_minimal();
        let sb = r.block_reader_mut().superblock();
        assert!(sb.block_size >= 1024);
    }

    // -------------------------------------------------------------------
    // 2. read_inode_raw — raw bytes for inode 2
    // -------------------------------------------------------------------

    #[test]
    fn read_inode_raw_minimal() {
        let r = open_minimal();
        let inode_size = r.block_reader().superblock().inode_size as usize;
        let raw = r.read_inode_raw(2).unwrap();
        assert_eq!(
            raw.len(),
            inode_size,
            "raw inode length should equal inode_size"
        );
        // The mode field (u16 LE at offset 0) should be non-zero for root dir
        let mode = u16::from_le_bytes([raw[0], raw[1]]);
        assert_ne!(mode, 0, "root inode mode should be non-zero");
    }

    #[test]
    fn read_inode_raw_out_of_range() {
        let r = open_minimal();
        assert!(r.read_inode_raw(0).is_err());
        assert!(r.read_inode_raw(u64::MAX).is_err());
    }

    // -------------------------------------------------------------------
    // 3. inode_block_map — hello.txt (inode 12) on minimal.img
    // -------------------------------------------------------------------

    #[test]
    fn inode_block_map_hello_txt() {
        let hello_ino = resolve_minimal("/hello.txt");
        let r = open_minimal();
        let map = r.inode_block_map(hello_ino).unwrap();
        assert!(
            !map.is_empty(),
            "hello.txt should have at least one block mapping"
        );
        for m in &map {
            assert!(m.physical_block > 0, "physical block should be non-zero");
            assert!(m.length > 0, "mapping length should be positive");
        }
    }

    // -------------------------------------------------------------------
    // 4. read_inode_data_range — first 5 bytes of hello.txt
    // -------------------------------------------------------------------

    #[test]
    fn read_inode_data_range_hello_prefix() {
        let hello_ino = resolve_minimal("/hello.txt");
        let r = open_minimal();
        let data = r.read_inode_data_range(hello_ino, 0, 5).unwrap();
        assert_eq!(
            &data, b"Hello",
            "first 5 bytes of hello.txt should be 'Hello'"
        );
    }

    #[test]
    fn read_inode_data_range_past_eof() {
        let hello_ino = resolve_minimal("/hello.txt");
        let r = open_minimal();
        let inode = r.read_inode(hello_ino).unwrap();
        let data = r
            .read_inode_data_range(hello_ino, inode.size + 100, 10)
            .unwrap();
        assert!(data.is_empty(), "reading past EOF should return empty");
    }

    // -------------------------------------------------------------------
    // 5. is_block_allocated — block 0 should be allocated
    // -------------------------------------------------------------------

    #[test]
    fn is_block_allocated_block_zero() {
        let r = open_minimal();
        let alloc = r.is_block_allocated(0).unwrap();
        assert!(alloc, "block 0 (superblock) should be allocated");
    }

    // -------------------------------------------------------------------
    // 6. iter_inodes_in_group(0) — includes root inode 2
    // -------------------------------------------------------------------

    #[test]
    fn iter_inodes_in_group_zero() {
        let r = open_minimal();
        let inodes = r.iter_inodes_in_group(0).unwrap();
        assert!(!inodes.is_empty(), "group 0 should have inodes");
        let inos: Vec<u64> = inodes.iter().map(|(ino, _)| *ino).collect();
        assert!(inos.contains(&2), "group 0 should contain root inode 2");
    }

    // -------------------------------------------------------------------
    // 7. iter_all_inodes — multiple inodes including inode 2
    // -------------------------------------------------------------------

    #[test]
    fn iter_all_inodes_includes_root() {
        let r = open_minimal();
        let all = r.iter_all_inodes().unwrap();
        assert!(all.len() >= 2, "should return multiple inodes");
        let inos: Vec<u64> = all.iter().map(|(ino, _)| *ino).collect();
        assert!(inos.contains(&2), "should include root inode 2");
    }

    // -------------------------------------------------------------------
    // 8. read_inode_data for a directory (inode 2)
    // -------------------------------------------------------------------

    #[test]
    fn read_inode_data_directory() {
        let r = open_minimal();
        let inode = r.read_inode(2).unwrap();
        assert_eq!(inode.file_type(), FileType::Directory);
        let data = r.read_inode_data(2).unwrap();
        assert!(!data.is_empty(), "root directory data should not be empty");
        assert_eq!(data.len(), inode.size as usize);
    }

    // -------------------------------------------------------------------
    // 9. read_inode for various inodes
    // -------------------------------------------------------------------

    #[test]
    fn read_inode_hello_txt() {
        let hello_ino = resolve_minimal("/hello.txt");
        let r = open_minimal();
        let inode = r.read_inode(hello_ino).unwrap();
        assert_eq!(inode.file_type(), FileType::RegularFile);
        // "Hello, ext4!" without or with trailing newline
        assert!(
            inode.size == 11 || inode.size == 12,
            "hello.txt should be 11 or 12 bytes, got {}",
            inode.size
        );
    }

    #[test]
    fn read_inode_lost_found() {
        let lf_ino = resolve_minimal("/lost+found");
        let r = open_minimal();
        let inode = r.read_inode(lf_ino).unwrap();
        assert_eq!(inode.file_type(), FileType::Directory);
        assert!(
            inode.links_count >= 2,
            "lost+found should have at least 2 links"
        );
    }

    // -------------------------------------------------------------------
    // 10. read_inode_raw on forensic.img
    // -------------------------------------------------------------------

    #[test]
    fn read_inode_raw_forensic() {
        let r = open_forensic();
        let inode_size = r.block_reader().superblock().inode_size as usize;
        let raw = r.read_inode_raw(2).unwrap();
        assert_eq!(raw.len(), inode_size);
        // Verify mode is non-zero for root dir
        let mode = u16::from_le_bytes([raw[0], raw[1]]);
        assert_ne!(mode, 0);
    }

    // -------------------------------------------------------------------
    // 11. read_inode_data_range partial read on forensic.img
    // -------------------------------------------------------------------

    #[test]
    fn read_inode_data_range_forensic_middle() {
        let hello_ino = resolve_forensic("/hello.txt");
        let r = open_forensic();
        // hello.txt = "Hello, forensic world!\n" (23 bytes)
        // Read bytes 7..15 → "forensic"
        let data = r.read_inode_data_range(hello_ino, 7, 8).unwrap();
        assert_eq!(
            std::str::from_utf8(&data).unwrap(),
            "forensic",
            "middle bytes of forensic hello.txt"
        );
    }

    #[test]
    fn read_inode_data_range_forensic_start() {
        let hello_ino = resolve_forensic("/hello.txt");
        let r = open_forensic();
        let data = r.read_inode_data_range(hello_ino, 0, 5).unwrap();
        assert_eq!(&data, b"Hello");
    }

    // -------------------------------------------------------------------
    // 12. is_block_allocated on forensic.img — various blocks
    // -------------------------------------------------------------------

    #[test]
    fn is_block_allocated_forensic_block_zero() {
        let r = open_forensic();
        assert!(
            r.is_block_allocated(0).unwrap(),
            "block 0 should be allocated on forensic.img"
        );
    }

    #[test]
    fn is_block_allocated_forensic_high_block() {
        let r = open_forensic();
        // The forensic image is 32MB with 4096-byte blocks = 8192 blocks.
        // The last block should be unallocated (unused space beyond fs data).
        let sb = r.block_reader().superblock();
        let total_blocks = sb.blocks_count;
        // Check a block near the end — likely unallocated
        if total_blocks > 100 {
            let result = r.is_block_allocated(total_blocks - 1);
            // Just verify it doesn't panic/error — the value depends on layout
            assert!(result.is_ok());
        }
    }

    #[test]
    fn is_block_allocated_forensic_superblock_area() {
        let r = open_forensic();
        // Block 1 on a 4096-byte blocksize fs holds the superblock backup or GDT
        let alloc = r.is_block_allocated(1).unwrap();
        assert!(alloc, "block 1 should be allocated (GDT/superblock area)");
    }
}