Skip to main content

btrfs_cli/inspect/
inode_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 file system paths for the given inode
7#[derive(Parser, Debug)]
8pub struct InodeResolveCommand {
9    /// Inode number
10    inode: u64,
11
12    /// Path to a file or directory on the btrfs filesystem
13    path: PathBuf,
14}
15
16impl Runnable for InodeResolveCommand {
17    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
18        let file = File::open(&self.path)
19            .with_context(|| format!("failed to open '{}'", self.path.display()))?;
20        let fd = file.as_fd();
21
22        let paths = btrfs_uapi::inode::ino_paths(fd, self.inode)
23            .context("failed to look up inode paths (is this a btrfs filesystem?)")?;
24
25        if paths.is_empty() {
26            eprintln!("no paths found for inode {}", self.inode);
27        } else {
28            for path in paths {
29                println!("{}", path);
30            }
31        }
32
33        Ok(())
34    }
35}