use std::fs::{File, TryLockError};
use std::io::{self, Write};
use std::path::Path;
use std::thread;
use std::time::Duration;
use super::fileio::{private, private_mkdir};
const RETRY: Duration = Duration::from_millis(10);
pub fn acquire(path: &Path) -> io::Result<File> {
if let Some(parent) = path.parent() {
private_mkdir(parent)?;
}
let mut file = private().read(true).write(true).create(true).open(path)?;
if file.metadata()?.len() == 0 {
file.write_all(b"\0")?;
}
loop {
match file.try_lock() {
Ok(()) => return Ok(file),
Err(TryLockError::WouldBlock) => thread::sleep(RETRY),
Err(TryLockError::Error(error)) => return Err(error),
}
}
}