#![forbid(unsafe_code)]
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use crate::fuse::mount::{MountParams, mount as do_mount};
use crate::store::{Store, StoreConfig};
#[derive(Debug, Clone, clap::Args)]
pub struct MountArgs {
#[arg(value_name = "STORE")]
pub store: PathBuf,
#[arg(value_name = "MOUNTPOINT")]
pub mountpoint: PathBuf,
#[arg(long)]
pub read_only: bool,
#[arg(long)]
pub allow_other: bool,
#[arg(long, default_value_t = 1)]
pub threads: usize,
#[arg(long, default_value = "entropyfs")]
pub fs_name: String,
#[arg(long)]
pub no_background_optimize: bool,
}
pub fn run(args: &MountArgs) -> Result<(), String> {
let config = StoreConfig::default();
let store = Store::open(&args.store, &config).map_err(|e| e.to_string())?;
let params = MountParams {
store_dir: args.store.clone(),
mountpoint: args.mountpoint.clone(),
read_only: args.read_only,
allow_other: args.allow_other,
threads: args.threads,
fs_name: args.fs_name.clone(),
background_optimize: !args.no_background_optimize,
};
let session = do_mount(¶ms, store).map_err(|e| e.to_string())?;
println!(
"entropyfs mounted: {} -> {} (pid {})",
params.store_dir.display(),
params.mountpoint.display(),
std::process::id()
);
let stop = Arc::new(AtomicBool::new(false));
let stop_handler = Arc::clone(&stop);
ctrlc::set_handler(move || {
stop_handler.store(true, Ordering::SeqCst);
})
.map_err(|e| format!("signal handler: {e}"))?;
while !session.guard.is_finished() && !stop.load(Ordering::SeqCst) {
std::thread::sleep(Duration::from_millis(100));
}
session
.umount_and_join()
.map_err(|e| format!("unmount/join: {e}"))?;
Ok(())
}