Skip to main content

btrfs_cli/
filesystem.rs

1use crate::{CommandGroup, Runnable, util::SizeFormat};
2use clap::{Args, Parser};
3
4mod commit_stats;
5mod defrag;
6mod df;
7mod du;
8mod label;
9mod mkswapfile;
10mod resize;
11mod show;
12mod sync;
13mod usage;
14
15pub use self::{
16    commit_stats::*, defrag::*, df::*, du::*, label::*, mkswapfile::*,
17    resize::*, show::*, sync::*, usage::*,
18};
19
20/// Overall filesystem tasks and information.
21///
22/// Perform filesystem-level operations including checking available space,
23/// disk usage analysis, defragmentation, resizing, labeling, and
24/// synchronization. These commands provide views into filesystem state and
25/// allow configuration of filesystem-wide settings.
26#[derive(Parser, Debug)]
27#[clap(arg_required_else_help = true)]
28pub struct FilesystemCommand {
29    #[clap(subcommand)]
30    pub subcommand: FilesystemSubcommand,
31}
32
33impl CommandGroup for FilesystemCommand {
34    fn leaf(&self) -> &dyn Runnable {
35        match &self.subcommand {
36            FilesystemSubcommand::Df(cmd) => cmd,
37            FilesystemSubcommand::Du(cmd) => cmd,
38            FilesystemSubcommand::Show(cmd) => cmd,
39            FilesystemSubcommand::Sync(cmd) => cmd,
40            FilesystemSubcommand::Defragment(cmd) => cmd,
41            FilesystemSubcommand::Resize(cmd) => cmd,
42            FilesystemSubcommand::Label(cmd) => cmd,
43            FilesystemSubcommand::Usage(cmd) => cmd,
44            FilesystemSubcommand::Mkswapfile(cmd) => cmd,
45            FilesystemSubcommand::CommitStats(cmd) => cmd,
46        }
47    }
48}
49
50#[derive(Parser, Debug)]
51pub enum FilesystemSubcommand {
52    Df(FilesystemDfCommand),
53    Du(FilesystemDuCommand),
54    Show(FilesystemShowCommand),
55    Sync(FilesystemSyncCommand),
56    #[clap(alias = "defrag")]
57    Defragment(FilesystemDefragCommand),
58    Resize(FilesystemResizeCommand),
59    Label(FilesystemLabelCommand),
60    Usage(FilesystemUsageCommand),
61    Mkswapfile(FilesystemMkswapfileCommand),
62    CommitStats(FilesystemCommitStatsCommand),
63}
64
65/// Unit display mode flags, shared by subcommands that output sizes.
66///
67/// Control how sizes are displayed in output. By default, human-readable
68/// format with base 1024 (KiB, MiB, GiB, TiB) is used. You can specify
69/// exact units or enable base 1000 (kB, MB, GB, TB) with --si.
70#[derive(Args, Debug)]
71#[allow(clippy::struct_excessive_bools)]
72pub struct UnitMode {
73    /// Show raw numbers in bytes
74    #[clap(long, overrides_with_all = ["human_readable", "iec", "si", "kbytes", "mbytes", "gbytes", "tbytes"])]
75    pub raw: bool,
76
77    /// Show human-friendly numbers using base 1024 (default)
78    #[clap(long, overrides_with_all = ["raw", "iec", "si", "kbytes", "mbytes", "gbytes", "tbytes"])]
79    pub human_readable: bool,
80
81    /// Use 1024 as a base (KiB, MiB, GiB, TiB)
82    #[clap(long, overrides_with_all = ["raw", "human_readable", "si", "kbytes", "mbytes", "gbytes", "tbytes"])]
83    pub iec: bool,
84
85    /// Use 1000 as a base (kB, MB, GB, TB)
86    #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "kbytes", "mbytes", "gbytes", "tbytes"])]
87    pub si: bool,
88
89    /// Show sizes in KiB, or kB with --si
90    #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "si", "mbytes", "gbytes", "tbytes"])]
91    pub kbytes: bool,
92
93    /// Show sizes in MiB, or MB with --si
94    #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "si", "kbytes", "gbytes", "tbytes"])]
95    pub mbytes: bool,
96
97    /// Show sizes in GiB, or GB with --si
98    #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "si", "kbytes", "mbytes", "tbytes"])]
99    pub gbytes: bool,
100
101    /// Show sizes in TiB, or TB with --si
102    #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "si", "kbytes", "mbytes", "gbytes"])]
103    pub tbytes: bool,
104}
105
106impl UnitMode {
107    /// Resolve the clap flags into a `SizeFormat`.
108    #[must_use]
109    pub fn resolve(&self) -> SizeFormat {
110        let si = self.si;
111        if self.raw {
112            SizeFormat::Raw
113        } else if self.kbytes {
114            SizeFormat::Fixed(if si { 1000 } else { 1024 })
115        } else if self.mbytes {
116            SizeFormat::Fixed(if si { 1_000_000 } else { 1024 * 1024 })
117        } else if self.gbytes {
118            SizeFormat::Fixed(if si {
119                1_000_000_000
120            } else {
121                1024 * 1024 * 1024
122            })
123        } else if self.tbytes {
124            SizeFormat::Fixed(if si {
125                1_000_000_000_000
126            } else {
127                1024u64.pow(4)
128            })
129        } else if si {
130            SizeFormat::HumanSi
131        } else {
132            SizeFormat::HumanIec
133        }
134    }
135}