1use std::io;
2use std::path::Path;
3use std::process;
4
5use common::BitrustOperation;
6use lockfile::LockFile;
7
8fn lock_file_base_name(op: BitrustOperation) -> String {
9 String::from(match op {
10 BitrustOperation::Create => ".create_lock",
11 BitrustOperation::Write => ".write_lock",
12 BitrustOperation::Merge => ".merge_lock",
13 })
14}
15
16pub fn acquire<P>(
17 data_dir: P,
18 lock_type: BitrustOperation,
19) -> io::Result<LockFile>
20where
21 P: AsRef<Path>,
22{
23 let lockfile_path = data_dir.as_ref().join(lock_file_base_name(lock_type));
24 let pid_str = process::id().to_string();
25 LockFile::new(lockfile_path, Some(pid_str.as_bytes()))
26}