Skip to main content

btrfs_cli/qgroup/
clear_stale.rs

1use crate::{RunContext, Runnable, util::open_path};
2use anyhow::{Context, Result};
3use clap::Parser;
4use std::{os::unix::io::AsFd, path::PathBuf};
5
6/// Clear all stale qgroups (level 0/subvolid) without a subvolume
7#[derive(Parser, Debug)]
8pub struct QgroupClearStaleCommand {
9    /// Path to a mounted btrfs filesystem
10    pub path: PathBuf,
11}
12
13impl Runnable for QgroupClearStaleCommand {
14    fn run(&self, _ctx: &RunContext) -> Result<()> {
15        let file = open_path(&self.path)?;
16
17        let n = btrfs_uapi::quota::qgroup_clear_stale(file.as_fd())
18            .with_context(|| {
19                format!(
20                    "failed to clear stale qgroups on '{}'",
21                    self.path.display()
22                )
23            })?;
24
25        if n == 0 {
26            println!("no stale qgroups found");
27        } else {
28            println!(
29                "deleted {} stale qgroup{}",
30                n,
31                if n == 1 { "" } else { "s" }
32            );
33        }
34
35        Ok(())
36    }
37}