Skip to main content

btrfs_cli/inspect/
inode_resolve.rs

1use crate::{Format, Runnable, util::open_path};
2use anyhow::{Context, Result};
3use clap::Parser;
4use std::{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 = open_path(&self.path)?;
19        let fd = file.as_fd();
20
21        let paths = btrfs_uapi::inode::ino_paths(fd, self.inode).context(
22            "failed to look up inode paths (is this a btrfs filesystem?)",
23        )?;
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}