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};
#[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())
}),
}
}
}