hexz_cli/cmd/data/
unmount.rs1use anyhow::{Context, Result};
4use colored::Colorize;
5use std::path::Path;
6use std::process::Command;
7
8pub fn run(mountpoint: &Path) -> Result<()> {
10 let path_str = mountpoint.to_string_lossy();
11
12 if cfg!(target_os = "linux") {
13 let output = Command::new("fusermount")
14 .arg("-u")
15 .arg(mountpoint)
16 .output();
17
18 if let Ok(output) = output {
19 if output.status.success() {
20 println!(
21 " {} Successfully unmounted {}",
22 "✓".green(),
23 path_str.cyan()
24 );
25 return Ok(());
26 }
27
28 let stderr = String::from_utf8_lossy(&output.stderr);
29 if stderr.contains("not found") {
30 return Ok(());
31 }
32 }
33 }
34
35 let output = Command::new("umount")
36 .arg(mountpoint)
37 .output()
38 .context("Failed to execute unmount command")?;
39
40 if output.status.success() {
41 println!(
42 " {} Successfully unmounted {}",
43 "✓".green(),
44 path_str.cyan()
45 );
46 Ok(())
47 } else {
48 let stderr = String::from_utf8_lossy(&output.stderr);
49 if stderr.contains("not mounted") {
50 return Ok(());
51 }
52
53 eprint!("{stderr}");
54 anyhow::bail!("Failed to unmount {path_str}.");
55 }
56}