use std::collections::BTreeMap;
use std::fs::{self, File};
use std::io::{self, BufWriter, Write};
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use super::ScanOutcome;
use super::bundle::FileStamp;
#[derive(Default, Serialize, Deserialize)]
pub struct Cache {
schema: u32,
library: String,
architecture: String,
pub bundles: BTreeMap<PathBuf, Entry>,
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Entry {
pub fingerprint: Vec<FileStamp>,
pub outcome: ScanOutcome,
pub failures: u32,
}
fn current() -> (String, String) {
(
env!("CARGO_PKG_VERSION").to_owned(),
std::env::consts::ARCH.to_owned(),
)
}
pub fn load(path: &Path) -> Cache {
read(path).unwrap_or_default()
}
fn read(path: &Path) -> io::Result<Cache> {
let bytes = match fs::read(path) {
Ok(bytes) => bytes,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(Cache::default()),
Err(error) => return Err(error),
};
let (library, architecture) = current();
Ok(serde_json::from_slice::<Cache>(&bytes)
.ok()
.filter(|cache| {
cache.schema == 2 && cache.library == library && cache.architecture == architecture
})
.unwrap_or_default())
}
pub(super) struct Writer {
path: PathBuf,
parent: PathBuf,
cache: Cache,
unsynced: bool,
_lock: File,
}
impl Writer {
pub fn open(path: &Path) -> io::Result<Self> {
let parent = path
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or(Path::new("."));
fs::create_dir_all(parent)?;
let mut lock_path = path.as_os_str().to_owned();
lock_path.push(".lock");
let lock = File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(lock_path)?;
lock.try_lock().map_err(io::Error::from)?;
let (library, architecture) = current();
let cache = Cache {
schema: 2,
library,
architecture,
bundles: read(path)?.bundles,
};
Ok(Self {
path: path.to_path_buf(),
parent: parent.to_path_buf(),
cache,
unsynced: false,
_lock: lock,
})
}
pub fn entry(&self, path: &Path) -> Option<&Entry> {
self.cache.bundles.get(path)
}
pub fn update(&mut self, path: &Path, entry: Option<Entry>) -> io::Result<()> {
if self.entry(path) == entry.as_ref() {
return Ok(());
}
match entry {
Some(entry) => self.cache.bundles.insert(path.to_path_buf(), entry),
None => self.cache.bundles.remove(path),
};
let mut temporary = tempfile::Builder::new()
.prefix(".plughost-cache-")
.tempfile_in(&self.parent)?;
{
let mut writer = BufWriter::new(temporary.as_file_mut());
serde_json::to_writer(&mut writer, &self.cache).map_err(io::Error::other)?;
writer.flush()?;
}
let temporary = temporary.into_temp_path();
fs::rename(&temporary, &self.path)?;
self.unsynced = true;
Ok(())
}
pub fn finish(self) -> io::Result<()> {
if self.unsynced {
File::options().write(true).open(&self.path)?.sync_all()?;
#[cfg(unix)]
File::open(&self.parent)?.sync_all()?;
}
Ok(())
}
}