Skip to main content

btrfs_cli/inspect/
subvolid_resolve.rs

1use crate::{Format, Runnable};
2use anyhow::{Context, Result};
3use clap::Parser;
4use std::{fs::File, os::unix::io::AsFd, path::PathBuf};
5
6/// Get subvolume ID and tree ID of the given path
7#[derive(Parser, Debug)]
8pub struct SubvolidResolveCommand {
9    /// Subvolume ID to resolve
10    subvolid: u64,
11
12    /// Path to a file or directory on the btrfs filesystem
13    path: PathBuf,
14}
15
16impl Runnable for SubvolidResolveCommand {
17    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
18        let file = File::open(&self.path).with_context(|| {
19            format!("failed to open '{}'", self.path.display())
20        })?;
21        let fd = file.as_fd();
22
23        let resolved_path =
24            btrfs_uapi::inode::subvolid_resolve(fd, self.subvolid).context(
25                "failed to resolve subvolume ID (is this a btrfs filesystem?)",
26            )?;
27
28        println!("{}", resolved_path);
29        Ok(())
30    }
31}