]> git.sesse.net Git - bcachefs-tools-debian/blob - src/commands/cmd_subvolume.rs
feat: add aliases for a few subcommands
[bcachefs-tools-debian] / src / commands / cmd_subvolume.rs
1 use std::path::PathBuf;
2
3 use bch_bindgen::c::BCH_SUBVOL_SNAPSHOT_RO;
4 use clap::{Parser, Subcommand};
5
6 use crate::wrappers::handle::BcachefsHandle;
7
8 #[derive(Parser, Debug)]
9 pub struct Cli {
10     #[command(subcommand)]
11     subcommands: Subcommands,
12 }
13
14 /// Subvolumes-related commands
15 #[derive(Subcommand, Debug)]
16 enum Subcommands {
17     #[command(visible_aliases = ["new"])]
18     Create {
19         /// Paths
20         targets: Vec<PathBuf>
21     },
22
23     #[command(visible_aliases = ["del"])]
24     Delete {
25         /// Path
26         target: PathBuf
27     },
28
29     #[command(allow_missing_positional = true, visible_aliases = ["snap"])]
30     Snapshot {
31         /// Make snapshot read only
32         #[arg(long, short)]
33         read_only: bool,
34         source: Option<PathBuf>,
35         dest: PathBuf
36     }
37 }
38
39 pub fn cmd_subvolumes(argv: Vec<String>) -> i32 {
40     let args = Cli::parse_from(argv);
41
42     match args.subcommands {
43         Subcommands::Create { targets } => {
44             for target in targets {
45                 if let Some(dirname) = target.parent() {
46                     let fs = unsafe { BcachefsHandle::open(dirname) };
47                     fs.create_subvolume(target).expect("Failed to create the subvolume");
48                 }
49             }
50         }
51         ,
52         Subcommands::Delete { target } => {
53             if let Some(dirname) = target.parent() {
54                 let fs = unsafe { BcachefsHandle::open(dirname) };
55                 fs.delete_subvolume(target).expect("Failed to delete the subvolume");
56             }
57         },
58         Subcommands::Snapshot { read_only, source, dest } => {
59             if let Some(dirname) = dest.parent() {
60                 let dot = PathBuf::from(".");
61                 let dir = if dirname.as_os_str().is_empty() { &dot } else { dirname };
62                 let fs = unsafe { BcachefsHandle::open(dir) };
63
64                 fs.snapshot_subvolume(if read_only { BCH_SUBVOL_SNAPSHOT_RO } else { 0x0 }, source, dest).expect("Failed to snapshot the subvolume");
65             }
66         }
67     }
68
69     0
70 }