Files
client/core/src/locks.rs
T
2026-11-12 14:03:35 +03:00

24 lines
1.1 KiB
Rust

use std::fs::{File, OpenOptions, TryLockError};
use std::path::Path;
use crate::secure_fs;
/// Cooperates with diagnostic CLI and embedded sessions without copying private identity keys.
pub fn lock_dir(dir: &Path, shared: bool) -> Result<File, String> {
secure_fs::ensure_private_dir(dir).map_err(|e| format!("cannot create data dir {}: {e}", dir.display()))?;
let path = dir.join("core.lock");
let file = OpenOptions::new().create(true).truncate(false).write(true).open(&path)
.map_err(|e| format!("cannot open {}: {e}", path.display()))?;
match crate::file_lock::try_lock(&file, shared) {
Ok(()) => Ok(file),
Err(TryLockError::WouldBlock) => Err(format!("data dir {} is in use by another teamspeach-core", dir.display())),
Err(TryLockError::Error(e)) => Err(format!("cannot lock {}: {e}", path.display())),
}
}
pub fn lock_run(data_dir: &Path, connection_dir: Option<&Path>) -> Result<Vec<File>, String> {
match connection_dir {
None => Ok(vec![lock_dir(data_dir, false)?]),
Some(conn) => Ok(vec![lock_dir(data_dir, true)?, lock_dir(conn, false)?]),
}
}