Skip to main content

nmrs_gui/
file_lock.rs

1use fs2::FileExt;
2use std::fs::File;
3
4pub fn acquire_app_lock() -> Result<File, String> {
5    let mut lock_path = dirs::data_local_dir().unwrap_or(std::env::temp_dir());
6    lock_path.push("my_app.lock");
7
8    let file = File::create(&lock_path).map_err(|e| format!("Failed to create lock file: {e}"))?;
9
10    // Exclusive lock; fails if another instance holds it
11    file.try_lock_exclusive()
12        .map_err(|_| "Another instance is already running".to_string())?;
13
14    Ok(file)
15}