use std::fs::{File, OpenOptions};
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::{Error, Result};
const MAX_ATTEMPTS: u32 = 64;
pub(super) fn create(dir: &Path) -> Result<File> {
if !dir.is_dir() {
return Err(Error::io_msg(format!(
"spill directory {} does not exist or is not a directory",
dir.display()
)));
}
let pid = std::process::id();
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| Error::io_msg(e.to_string()))?
.as_nanos();
let mut last_err = None;
for attempt in 0..MAX_ATTEMPTS {
let path = dir.join(format!(".brygge-spill-{pid}-{nanos}-{attempt}"));
match open_new(&path) {
Ok(file) => {
remove_entry(&path)?;
return Ok(file);
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
last_err = Some(e);
}
Err(e) => return Err(Error::io(e)),
}
}
#[allow(clippy::expect_used)]
Err(Error::io(last_err.expect(
"the loop above ran at least once, so a collision on every attempt means it set this",
)))
}
#[cfg(unix)]
fn open_new(path: &Path) -> std::io::Result<File> {
use std::os::unix::fs::OpenOptionsExt;
OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.mode(0o600)
.open(path)
}
#[cfg(windows)]
fn open_new(path: &Path) -> std::io::Result<File> {
use std::os::windows::fs::OpenOptionsExt;
const FILE_FLAG_DELETE_ON_CLOSE: u32 = 0x0400_0000;
OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.custom_flags(FILE_FLAG_DELETE_ON_CLOSE)
.open(path)
}
#[cfg(not(any(unix, windows)))]
fn open_new(path: &Path) -> std::io::Result<File> {
OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(path)
}
#[cfg(unix)]
fn remove_entry(path: &Path) -> Result<()> {
std::fs::remove_file(path).map_err(Error::io)
}
#[cfg(not(unix))]
fn remove_entry(_path: &Path) -> Result<()> {
Ok(())
}
#[cfg(test)]
mod tests;