f2fs_reader/
lib.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4mod block_cache;
5mod checkpoint;
6mod crypto;
7mod dir;
8mod fsverity;
9mod inode;
10mod nat;
11mod reader;
12mod superblock;
13mod xattr;
14
15// Explicitly re-export things we want to expose.
16pub use dir::{DirEntry, FileType};
17pub use fsverity::FsVerityDescriptor;
18pub use inode::{AdviseFlags, Flags, InlineFlags, Inode, Mode};
19pub use reader::{F2fsReader, NEW_ADDR, NULL_ADDR};
20pub use superblock::{BLOCK_SIZE, SuperBlock};
21pub use xattr::{Index as XattrIndex, XattrEntry};
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use crate::reader::Reader;
27    use std::sync::Arc;
28    use std::sync::atomic::{AtomicUsize, Ordering};
29
30    // Copied from reader.rs tests, might want to deduplicate later if it grows.
31    fn open_test_image(path: &str) -> storage_device::fake_device::FakeDevice {
32        use storage_device::fake_device::FakeDevice;
33        let path = std::path::PathBuf::from(path);
34        println!("path is {path:?}");
35        FakeDevice::from_image(
36            zstd::Decoder::new(std::fs::File::open(&path).expect("open image"))
37                .expect("decompress image"),
38            BLOCK_SIZE as u32,
39        )
40        .expect("open image")
41    }
42
43    #[fuchsia::test]
44    async fn test_readahead() {
45        let mut device = open_test_image("/pkg/testdata/f2fs.img.zst");
46        let read_count = Arc::new(AtomicUsize::new(0));
47        let read_count_clone = read_count.clone();
48
49        device.set_op_callback(move |op| {
50            if let storage_device::fake_device::Op::Read = op {
51                read_count_clone.fetch_add(1, Ordering::SeqCst);
52            }
53            Ok(())
54        });
55
56        let f2fs = F2fsReader::open_device(Arc::new(device)).await.expect("open ok");
57
58        // Reset counter after initialization (initialization does some reads)
59        read_count.store(0, Ordering::SeqCst);
60
61        // Block 0x1000 = 4096.
62        let start_block = 0x1000;
63
64        // Read start_block. Should trigger readahead for start_block + 0..16.
65        // Total 16 blocks.
66        f2fs.read_raw_block(start_block).await.expect("read start_block");
67        assert_eq!(read_count.load(Ordering::SeqCst), 1, "First read should trigger 1 device read");
68
69        // Read next 3 blocks. Should be cached.
70        for i in 1..4 {
71            f2fs.read_raw_block(start_block + i).await.expect("read cached block");
72            assert_eq!(
73                read_count.load(Ordering::SeqCst),
74                1,
75                "Read {} should be cached",
76                start_block + i
77            );
78        }
79
80        // Read 16th block. Should trigger new readahead.
81        f2fs.read_raw_block(start_block + 16).await.expect("read start_block + 16");
82        assert_eq!(read_count.load(Ordering::SeqCst), 2, "Read should trigger 2nd device read");
83    }
84}