Skip to main content

btrfs_cli/inspect/
subvolid_resolve.rs

1use crate::{Format, Runnable, util::open_path};
2use anyhow::{Result, anyhow};
3use btrfs_uapi::inode::subvolid_resolve;
4use clap::Parser;
5use nix::errno::Errno;
6use std::{os::unix::io::AsFd, path::PathBuf};
7
8/// Resolve the path of a subvolume given its ID
9#[derive(Parser, Debug)]
10pub struct SubvolidResolveCommand {
11    /// Subvolume ID to resolve
12    subvolid: u64,
13
14    /// Path to a file or directory on the btrfs filesystem
15    path: PathBuf,
16}
17
18impl Runnable for SubvolidResolveCommand {
19    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
20        let file = open_path(&self.path)?;
21        let fd = file.as_fd();
22
23        let resolved_path =
24            subvolid_resolve(fd, self.subvolid).map_err(|e| match e {
25                Errno::EPERM | Errno::EACCES => {
26                    anyhow!(
27                        "failed to resolve subvolume ID {}: permission denied \
28                         (requires CAP_SYS_ADMIN)",
29                        self.subvolid,
30                    )
31                }
32                _ => anyhow!(
33                    "failed to resolve subvolume ID {}: {}",
34                    self.subvolid,
35                    e,
36                ),
37            })?;
38
39        println!("{resolved_path}");
40        Ok(())
41    }
42}