Skip to main content

btrfs_cli/replace/
cancel.rs

1use crate::{Format, Runnable};
2use anyhow::{Context, Result};
3use btrfs_uapi::replace::replace_cancel;
4use clap::Parser;
5use std::{fs::File, os::unix::io::AsFd, path::PathBuf};
6
7/// Cancel a running device replace operation.
8///
9/// If a replace operation is in progress on the filesystem mounted at
10/// mount_point, it is cancelled. The target device is left in an undefined
11/// state and should not be used further without reformatting.
12#[derive(Parser, Debug)]
13pub struct ReplaceCancelCommand {
14    /// Path to the mounted btrfs filesystem
15    pub mount_point: PathBuf,
16}
17
18impl Runnable for ReplaceCancelCommand {
19    fn run(&self, _format: Format, _dry_run: bool) -> Result<()> {
20        let file = File::open(&self.mount_point).with_context(|| {
21            format!("failed to open '{}'", self.mount_point.display())
22        })?;
23
24        let was_running = replace_cancel(file.as_fd()).with_context(|| {
25            format!(
26                "failed to cancel replace on '{}'",
27                self.mount_point.display()
28            )
29        })?;
30
31        if was_running {
32            println!("replace cancelled on '{}'", self.mount_point.display());
33        } else {
34            println!("no replace operation was in progress");
35        }
36
37        Ok(())
38    }
39}