Skip to main content

littlefs_rust/
config.rs

1/// Filesystem configuration.
2///
3/// Only `block_size` and `block_count` are required; the rest have sensible
4/// defaults. Create with [`Config::new`] and override fields as needed.
5pub struct Config {
6    /// Size of an erasable block in bytes.
7    pub block_size: u32,
8    /// Number of erasable blocks on the device.
9    pub block_count: u32,
10    /// Minimum read size in bytes. Defaults to 16.
11    pub read_size: u32,
12    /// Minimum program (write) size in bytes. Defaults to 16.
13    pub prog_size: u32,
14    /// Number of erase cycles before moving data to a new block.
15    /// Set to `-1` to disable wear leveling.
16    pub block_cycles: i32,
17    /// Size of per-file caches in bytes. `0` (default) uses `block_size`.
18    pub cache_size: u32,
19    /// Size of the block allocator lookahead buffer in bytes.
20    /// `0` (default) uses `block_size`. Must be a multiple of 8.
21    pub lookahead_size: u32,
22    /// Maximum file name length in bytes. Defaults to 255.
23    pub name_max: u32,
24    /// Maximum file size in bytes.
25    pub file_max: u32,
26    /// Maximum size of custom attributes in bytes.
27    pub attr_max: u32,
28}
29
30impl Config {
31    /// Create a configuration with the given block geometry and sensible
32    /// defaults for everything else.
33    pub fn new(block_size: u32, block_count: u32) -> Self {
34        Self {
35            block_size,
36            block_count,
37            read_size: 16,
38            prog_size: 16,
39            block_cycles: -1,
40            cache_size: 0,
41            lookahead_size: 0,
42            name_max: 255,
43            file_max: i32::MAX as u32,
44            attr_max: 1022,
45        }
46    }
47
48    pub(crate) fn resolve_cache_size(&self) -> u32 {
49        if self.cache_size > 0 {
50            self.cache_size
51        } else {
52            self.block_size
53        }
54    }
55
56    pub(crate) fn resolve_lookahead_size(&self) -> u32 {
57        if self.lookahead_size > 0 {
58            self.lookahead_size
59        } else {
60            self.block_size
61        }
62    }
63}