use crate::error::{Error, Result};
use crate::format;
use crate::vault::{PasswordChange, write_atomically};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
pub const KEPT: usize = 3;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Copy {
pub path: PathBuf,
pub number: usize,
pub taken: Option<SystemTime>,
}
pub fn path(vault: &Path, number: usize) -> PathBuf {
let mut name = vault
.file_name()
.unwrap_or_else(|| std::ffi::OsStr::new("vault"))
.to_os_string();
name.push(format!(".{number}"));
vault.with_file_name(name)
}
pub fn list(vault: &Path) -> Vec<Copy> {
(1..=KEPT)
.filter_map(|number| {
let path = path(vault, number);
let metadata = std::fs::metadata(&path).ok()?;
metadata.is_file().then(|| Copy {
taken: metadata.modified().ok(),
path,
number,
})
})
.collect()
}
pub fn keep(vault: &Path) -> Result<Option<PathBuf>> {
let current = match std::fs::read(vault) {
Ok(bytes) => bytes,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(source) => {
return Err(Error::io(
format!("cannot read {} to keep a copy of it", vault.display()),
source,
));
}
};
for number in (1..KEPT).rev() {
let from = path(vault, number);
if !from.exists() {
continue;
}
let to = path(vault, number + 1);
std::fs::rename(&from, &to).map_err(|source| {
Error::io(
format!("cannot move {} to {}", from.display(), to.display()),
source,
)
})?;
}
let newest = path(vault, 1);
write_atomically(&newest, ¤t)?;
Ok(Some(newest))
}
pub(crate) fn reseal(vault: &Path, old: &[u8], new: &[u8]) -> Result<PasswordChange> {
let mut change = PasswordChange::default();
for copy in list(vault) {
let sealed = std::fs::read(©.path)
.map_err(|source| Error::io(format!("cannot read {}", copy.path.display()), source))?;
let database = match format::decode(old, &sealed) {
Ok(database) => database,
Err(
Error::WrongPasswordOrNotAVault | Error::TooSmall | Error::UnsupportedFormat(_),
) => {
change.left.push(copy.path);
continue;
}
Err(other) => return Err(other),
};
write_atomically(©.path, &format::encode(new, &database)?)?;
if let Some(taken) = copy.taken {
let _ = std::fs::File::options()
.write(true)
.open(©.path)
.and_then(|file| file.set_modified(taken));
}
change.resealed += 1;
}
Ok(change)
}
#[cfg(test)]
mod tests {
use super::*;
fn write(path: &Path, text: &str) {
std::fs::write(path, text).unwrap();
}
fn read(path: &Path) -> String {
std::fs::read_to_string(path).unwrap()
}
#[test]
fn a_copy_is_named_after_the_vault_with_a_number() {
let vault = Path::new("backups").join("notes.bak");
assert_eq!(path(&vault, 1), Path::new("backups").join("notes.bak.1"));
assert_eq!(path(&vault, 3), Path::new("backups").join("notes.bak.3"));
}
#[test]
fn the_name_of_a_copy_says_nothing_about_what_wrote_it() {
let vault = Path::new("notes.bak");
for number in 1..=KEPT {
let name = path(vault, number).display().to_string().to_lowercase();
assert!(!name.contains("sefy"), "{name}");
}
}
#[test]
fn the_first_copy_is_the_file_byte_for_byte() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
std::fs::write(&vault, [0u8, 255, 7, 13, 10]).unwrap();
let kept = keep(&vault).unwrap().expect("there was a file to keep");
assert_eq!(kept, path(&vault, 1));
assert_eq!(std::fs::read(&kept).unwrap(), vec![0u8, 255, 7, 13, 10]);
assert_eq!(
std::fs::read(&vault).unwrap(),
vec![0u8, 255, 7, 13, 10],
"keeping a copy leaves the vault alone"
);
}
#[test]
fn copies_rotate_and_the_oldest_goes() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
for state in ["first", "second", "third", "fourth", "fifth"] {
write(&vault, state);
keep(&vault).unwrap();
}
assert_eq!(read(&path(&vault, 1)), "fifth");
assert_eq!(read(&path(&vault, 2)), "fourth");
assert_eq!(read(&path(&vault, 3)), "third");
assert!(
!path(&vault, KEPT + 1).exists(),
"no copy is kept past the last place"
);
}
#[test]
fn a_gap_in_the_rotation_is_not_filled_with_a_wrong_copy() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
write(&path(&vault, 1), "older");
write(&path(&vault, 3), "oldest");
write(&vault, "now");
keep(&vault).unwrap();
assert_eq!(read(&path(&vault, 1)), "now");
assert_eq!(read(&path(&vault, 2)), "older");
assert_eq!(read(&path(&vault, 3)), "oldest");
}
#[test]
fn a_vault_with_no_file_has_nothing_to_keep() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("gone.bak");
assert_eq!(keep(&vault).unwrap(), None);
assert!(list(&vault).is_empty());
}
#[test]
fn the_listing_runs_most_recent_first_and_skips_what_is_missing() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
write(&path(&vault, 3), "oldest");
write(&path(&vault, 1), "newest");
let numbers: Vec<usize> = list(&vault).iter().map(|copy| copy.number).collect();
assert_eq!(numbers, vec![1, 3]);
}
#[test]
fn nothing_but_the_numbered_copies_is_touched() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
let neighbour = directory.path().join("notes.bak.old");
write(&neighbour, "mine");
for state in ["a", "b", "c", "d"] {
write(&vault, state);
keep(&vault).unwrap();
}
assert_eq!(read(&neighbour), "mine");
}
}