use anyhow::Result;
use std::fs::{remove_file, File, OpenOptions};
use std::io::{Read, Seek, Write};
use std::ops::{Deref, DerefMut, Drop};
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct LockedFile {
file: File,
path: PathBuf,
lock_file_path: PathBuf,
}
impl LockedFile {
pub fn open_read_write<T: AsRef<Path>>(path: T) -> Result<Option<LockedFile>> {
let path = path.as_ref().to_path_buf();
let lock_file_path = path.with_extension("lock");
if Path::exists(&lock_file_path) {
Ok(None)
} else {
OpenOptions::new()
.create(true)
.write(true)
.open(&lock_file_path)?;
let file = OpenOptions::new()
.create(true)
.read(true)
.write(true)
.open(&path)?;
Ok(Some(LockedFile {
file,
path,
lock_file_path,
}))
}
}
}
impl Deref for LockedFile {
type Target = File;
fn deref(&self) -> &File {
&self.file
}
}
impl DerefMut for LockedFile {
fn deref_mut(&mut self) -> &mut File {
&mut self.file
}
}
impl Drop for LockedFile {
fn drop(&mut self) {
if self.lock_file_path.exists() {
remove_file(&self.lock_file_path).unwrap_or_else(|_| {
panic!(
"Unable to delete lock file for {:?}, something went wrong",
self.path
)
});
}
}
}
impl Read for LockedFile {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.file.read(buf)
}
}
impl Write for LockedFile {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.file.write(buf)
}
fn flush(&mut self) -> std::io::Result<()> {
self.file.flush()
}
}
impl Seek for LockedFile {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
self.file.seek(pos)
}
}