1 use std::path::PathBuf;
3 use bch_bindgen::c::BCH_SUBVOL_SNAPSHOT_RO;
4 use clap::{Parser, Subcommand};
6 use crate::wrappers::handle::BcachefsHandle;
8 #[derive(Parser, Debug)]
10 #[command(subcommand)]
11 subcommands: Subcommands,
14 /// Subvolumes-related commands
15 #[derive(Subcommand, Debug)]
17 #[command(visible_aliases = ["new"])]
23 #[command(visible_aliases = ["del"])]
29 #[command(allow_missing_positional = true, visible_aliases = ["snap"])]
31 /// Make snapshot read only
34 source: Option<PathBuf>,
39 pub fn cmd_subvolumes(argv: Vec<String>) -> i32 {
40 let args = Cli::parse_from(argv);
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");
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");
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) };
64 fs.snapshot_subvolume(if read_only { BCH_SUBVOL_SNAPSHOT_RO } else { 0x0 }, source, dest).expect("Failed to snapshot the subvolume");