Skip to main content

btrfs_cli/subvolume/
get_default.rs

1use crate::{Format, Runnable};
2use anyhow::{Context, Result};
3use btrfs_uapi::subvolume::{FS_TREE_OBJECTID, subvolume_default_get};
4use clap::Parser;
5use std::{fs::File, os::unix::io::AsFd, path::PathBuf};
6
7/// Show the default subvolume of a filesystem
8#[derive(Parser, Debug)]
9pub struct SubvolumeGetDefaultCommand {
10    /// Path to a mounted btrfs filesystem
11    pub path: PathBuf,
12}
13
14impl Runnable for SubvolumeGetDefaultCommand {
15    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
16        let file = File::open(&self.path).with_context(|| {
17            format!("failed to open '{}'", self.path.display())
18        })?;
19
20        let default_id =
21            subvolume_default_get(file.as_fd()).with_context(|| {
22                format!(
23                    "failed to get default subvolume for '{}'",
24                    self.path.display()
25                )
26            })?;
27
28        if default_id == FS_TREE_OBJECTID {
29            println!("ID 5 (FS_TREE)");
30        } else {
31            // TODO: resolve name via BTRFS_IOC_GET_SUBVOL_INFO + path lookup
32            println!("ID {}", default_id);
33        }
34
35        Ok(())
36    }
37}