use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use tracing::{info, warn};
#[derive(Debug)]
pub struct Preserve {
root: PathBuf,
copies: BTreeMap<String, PathBuf>,
attempted: std::collections::BTreeSet<String>,
bytes: u64,
cap: u64,
warned: bool,
}
impl Preserve {
pub fn new(root: PathBuf, cap: u64) -> Self {
Self {
root,
copies: BTreeMap::new(),
attempted: std::collections::BTreeSet::new(),
bytes: 0,
cap,
warned: false,
}
}
pub fn copies(&self) -> &BTreeMap<String, PathBuf> {
&self.copies
}
pub fn bytes(&self) -> u64 {
self.bytes
}
pub fn len(&self) -> usize {
self.copies.len()
}
pub fn is_empty(&self) -> bool {
self.copies.is_empty()
}
pub fn keep(&mut self, served: &str, disk: &Path) {
if self.attempted.contains(served) {
return;
}
self.attempted.insert(served.to_owned());
let Ok(metadata) = std::fs::metadata(disk) else {
warn!(file = %disk.display(), "cannot preserve a file that is not there");
return;
};
let size = metadata.len();
if self.bytes.saturating_add(size) > self.cap {
if !self.warned {
self.warned = true;
warn!(
held = self.bytes,
cap = self.cap,
"the preserve cache is full; further loads will not survive an eject"
);
}
return;
}
let target = self.root.join(flat_name(served));
if let Some(parent) = target.parent()
&& let Err(error) = std::fs::create_dir_all(parent)
{
warn!(directory = %parent.display(), %error, "cannot make the preserve directory");
return;
}
let partial = target.with_extension("part");
let _ = std::fs::remove_file(&partial);
if let Err(error) = std::fs::copy(disk, &partial) {
warn!(file = %disk.display(), %error, "could not preserve a file");
let _ = std::fs::remove_file(&partial);
return;
}
if let Err(error) = std::fs::rename(&partial, &target) {
warn!(file = %target.display(), %error, "could not finish preserving a file");
let _ = std::fs::remove_file(&partial);
return;
}
self.bytes = self.bytes.saturating_add(size);
self.copies.insert(served.to_owned(), target);
info!(
served,
size,
held = self.copies.len(),
"preserved a file a player is using"
);
}
pub fn clear(&mut self) {
for path in self.copies.values() {
let _ = std::fs::remove_file(path);
}
self.copies.clear();
self.attempted.clear();
self.bytes = 0;
self.warned = false;
}
}
fn flat_name(served: &str) -> String {
use std::fmt::Write as _;
use sha2::{Digest, Sha256};
let digest = Sha256::digest(served.as_bytes());
let mut out = String::with_capacity(48);
for byte in digest.iter().take(12) {
let _ = write!(out, "{byte:02x}");
}
if let Some(dot) = served.rfind('.')
&& served.len() - dot <= 6
{
let extension: String = served[dot..]
.chars()
.filter(|c| c.is_ascii_alphanumeric() || *c == '.')
.collect();
out.push_str(&extension);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
struct Scratch(PathBuf);
impl Scratch {
fn new() -> Self {
use std::sync::atomic::{AtomicU32, Ordering};
static NEXT: AtomicU32 = AtomicU32::new(0);
let path = std::env::temp_dir().join(format!(
"prolink-preserve-{}-{}",
std::process::id(),
NEXT.fetch_add(1, Ordering::Relaxed)
));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).expect("scratch directory");
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn a_flat_name_is_a_bare_filename() {
let name = flat_name("/C/Contents/../../etc/passwd");
assert!(!name.contains('/'));
assert!(!name.contains(".."));
}
#[test]
fn two_paths_do_not_collide() {
assert_ne!(flat_name("/C/a/track.mp3"), flat_name("/C/b/track.mp3"));
}
#[test]
fn the_extension_is_kept() {
assert!(flat_name("/C/Contents/x/track.mp3").contains(".mp3"));
assert!(flat_name("/C/PIONEER/USBANLZ/x/ANLZ0000.DAT").contains(".DAT"));
}
#[test]
fn a_name_with_no_extension_is_still_a_name() {
let name = flat_name("/C/Contents/whatever");
assert!(!name.is_empty());
assert!(!name.contains('/'));
}
#[test]
fn a_file_is_copied_once() {
let directory = Scratch::new();
let source = directory.path().join("track.mp3");
std::fs::write(&source, b"0123456789").expect("write");
let mut preserve = Preserve::new(directory.path().join("cache"), 1024);
preserve.keep("/C/Contents/track.mp3", &source);
assert_eq!(preserve.len(), 1);
assert_eq!(preserve.bytes(), 10);
preserve.keep("/C/Contents/track.mp3", &source);
assert_eq!(preserve.len(), 1);
assert_eq!(preserve.bytes(), 10);
}
#[test]
fn the_copy_holds_the_whole_file() {
let directory = Scratch::new();
let source = directory.path().join("track.mp3");
let content: Vec<u8> = (0..=255u8).cycle().take(100_000).collect();
std::fs::write(&source, &content).expect("write");
let mut preserve = Preserve::new(directory.path().join("cache"), 1_000_000);
preserve.keep("/C/Contents/track.mp3", &source);
let copy = preserve
.copies()
.get("/C/Contents/track.mp3")
.expect("copied");
assert_eq!(std::fs::read(copy).expect("read back"), content);
}
#[test]
fn a_missing_file_is_not_an_error_and_is_not_retried() {
let directory = Scratch::new();
let mut preserve = Preserve::new(directory.path().join("cache"), 1024);
preserve.keep("/C/Contents/gone.mp3", &directory.path().join("gone.mp3"));
assert!(preserve.is_empty());
assert!(preserve.attempted.contains("/C/Contents/gone.mp3"));
}
#[test]
fn the_cap_is_not_exceeded() {
let directory = Scratch::new();
let mut preserve = Preserve::new(directory.path().join("cache"), 25);
for index in 0..5 {
let source = directory.path().join(format!("{index}.mp3"));
std::fs::write(&source, vec![0u8; 10]).expect("write");
preserve.keep(&format!("/C/Contents/{index}.mp3"), &source);
}
assert_eq!(preserve.len(), 2);
assert!(preserve.bytes() <= 25);
}
#[test]
fn clearing_removes_the_copies() {
let directory = Scratch::new();
let source = directory.path().join("track.mp3");
std::fs::write(&source, b"bytes").expect("write");
let mut preserve = Preserve::new(directory.path().join("cache"), 1024);
preserve.keep("/C/Contents/track.mp3", &source);
let copy = preserve
.copies()
.get("/C/Contents/track.mp3")
.expect("copied")
.clone();
assert!(copy.exists());
preserve.clear();
assert!(preserve.is_empty());
assert!(!copy.exists());
assert!(source.exists());
}
#[test]
fn a_partial_copy_is_never_left_behind_as_the_real_one() {
let directory = Scratch::new();
let source = directory.path().join("track.mp3");
std::fs::write(&source, b"whole").expect("write");
let mut preserve = Preserve::new(directory.path().join("cache"), 1024);
preserve.keep("/C/Contents/track.mp3", &source);
let copy = preserve
.copies()
.get("/C/Contents/track.mp3")
.expect("copied");
assert_eq!(std::fs::read(copy).expect("read"), b"whole");
assert!(!copy.with_extension("part").exists());
}
}