btrfs-cli 0.13.0

User-space command-line tool for inspecting and managing Btrfs filesystems
Documentation
use super::open_path;
use crate::{RunContext, Runnable};
use anyhow::{Context, Result};
use btrfs_uapi::balance::{BalanceCtl, balance_ctl};
use clap::Parser;
use nix::errno::Errno;
use std::{os::unix::io::AsFd, path::PathBuf};

/// Cancel a running balance operation
#[derive(Parser, Debug)]
pub struct BalanceCancelCommand {
    pub path: PathBuf,
}

impl Runnable for BalanceCancelCommand {
    fn run(&self, _ctx: &RunContext) -> Result<()> {
        let file = open_path(&self.path)?;

        match balance_ctl(file.as_fd(), BalanceCtl::Cancel) {
            Ok(()) => Ok(()),
            Err(Errno::ENOTCONN) => {
                anyhow::bail!(
                    "balance cancel on '{}' failed: Not in progress",
                    self.path.display()
                )
            }
            Err(e) => Err(e).with_context(|| {
                format!("balance cancel on '{}' failed", self.path.display())
            }),
        }
    }
}