|
| 1 | +//! Capacity figures for a mounted filesystem, shaped like POSIX `statfs`. |
| 2 | +//! |
| 3 | +//! It is plain data — no allocation, no device, no feature — so it is |
| 4 | +//! compiled in every configuration and answered by both halves of the |
| 5 | +//! crate: the hosted [`Filesystem::statfs`](crate::fs::Filesystem::statfs) |
| 6 | +//! and, without a heap, [`Volume::statfs`](crate::fs::volume::Volume::statfs) |
| 7 | +//! on the FAT, exFAT and littlefs drivers. |
| 8 | +
|
| 9 | +/// Filesystem-level capacity stats. |
| 10 | +/// |
| 11 | +/// Counts are in allocation units of `block_size` bytes — clusters on FAT |
| 12 | +/// and exFAT, erase blocks on littlefs, filesystem blocks elsewhere — and |
| 13 | +/// are all `u64` so large volumes do not overflow. `name_max` is the longest |
| 14 | +/// filename the filesystem accepts. A filesystem with no inode table (FAT, |
| 15 | +/// exFAT, littlefs) reports 0 for both inode counts. |
| 16 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 17 | +pub struct StatFs { |
| 18 | + /// Bytes in one allocation unit. |
| 19 | + pub block_size: u32, |
| 20 | + /// Allocation units holding data, in all. |
| 21 | + pub blocks: u64, |
| 22 | + /// Allocation units not in use. |
| 23 | + pub blocks_free: u64, |
| 24 | + /// Allocation units an unprivileged writer may use: `blocks_free` on |
| 25 | + /// filesystems with no reserve. |
| 26 | + pub blocks_avail: u64, |
| 27 | + /// Inodes in all, or 0 when the filesystem has no inode table. |
| 28 | + pub inodes: u64, |
| 29 | + /// Inodes free. |
| 30 | + pub inodes_free: u64, |
| 31 | + /// Longest filename accepted. |
| 32 | + pub name_max: u32, |
| 33 | +} |
| 34 | + |
| 35 | +impl StatFs { |
| 36 | + /// Bytes of data the filesystem can hold in all. |
| 37 | + pub fn total_bytes(&self) -> u64 { |
| 38 | + self.blocks.saturating_mul(self.block_size as u64) |
| 39 | + } |
| 40 | + |
| 41 | + /// Bytes not in use. |
| 42 | + pub fn free_bytes(&self) -> u64 { |
| 43 | + self.blocks_free.saturating_mul(self.block_size as u64) |
| 44 | + } |
| 45 | + |
| 46 | + /// Bytes an unprivileged writer may still use. |
| 47 | + pub fn avail_bytes(&self) -> u64 { |
| 48 | + self.blocks_avail.saturating_mul(self.block_size as u64) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl Default for StatFs { |
| 53 | + fn default() -> Self { |
| 54 | + // 4 KiB block, no quota, generous name budget — the same |
| 55 | + // numbers the kernel hands out for tmpfs in a fresh mount. |
| 56 | + Self { |
| 57 | + block_size: 4096, |
| 58 | + blocks: 0, |
| 59 | + blocks_free: 0, |
| 60 | + blocks_avail: 0, |
| 61 | + inodes: 0, |
| 62 | + inodes_free: 0, |
| 63 | + name_max: 255, |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments