use crate::copies;
use crate::error::{Error, Result};
use crate::vault::{SyncStamp, temporary_path};
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
const CHUNK: usize = 64 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
Vault,
Copy(usize),
Unfinished,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Part {
pub path: PathBuf,
pub role: Role,
pub size: Option<u64>,
}
#[derive(Debug, Default)]
pub struct Wiped {
pub destroyed: Vec<Part>,
pub vanished: Vec<Part>,
pub failed: Vec<(Part, Error)>,
pub last_sync: Option<SyncStamp>,
}
impl Wiped {
pub fn complete(&self) -> bool {
self.failed.is_empty()
}
}
pub fn parts(vault: &Path) -> Vec<Part> {
let mut named = vec![(vault.to_path_buf(), Role::Vault)];
named
.extend((1..=copies::KEPT).map(|number| (copies::path(vault, number), Role::Copy(number))));
let unfinished: Vec<(PathBuf, Role)> = named
.iter()
.map(|(path, _)| (temporary_path(path), Role::Unfinished))
.collect();
named.extend(unfinished);
named
.into_iter()
.filter_map(|(path, role)| match fs::metadata(&path) {
Ok(metadata) if metadata.is_dir() => None,
Ok(metadata) => Some(Part {
path,
role,
size: Some(metadata.len()),
}),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
Err(_) => Some(Part {
path,
role,
size: None,
}),
})
.collect()
}
pub(crate) fn carry_out(parts: Vec<Part>, last_sync: Option<SyncStamp>) -> Wiped {
let mut wiped = Wiped {
last_sync,
..Wiped::default()
};
for part in parts {
match destroy(&part.path) {
Ok(true) => wiped.destroyed.push(part),
Ok(false) => wiped.vanished.push(part),
Err(error) => wiped.failed.push((part, error)),
}
}
wiped
}
pub fn destroy(path: &Path) -> Result<bool> {
match overwrite(path) {
Ok(()) => {}
Err(Error::Io { source, .. }) if source.kind() == std::io::ErrorKind::NotFound => {
return Ok(false);
}
Err(other) => return Err(other),
}
fs::remove_file(path).map_err(|source| {
Error::io(
format!("overwrote {} but cannot delete it", path.display()),
source,
)
})?;
Ok(true)
}
fn overwrite(path: &Path) -> Result<()> {
let mut file = fs::OpenOptions::new()
.write(true)
.open(path)
.map_err(|source| {
Error::io(
format!("cannot open {} to overwrite it", path.display()),
source,
)
})?;
let failed = |source| Error::io(format!("cannot overwrite {}", path.display()), source);
let length = file.metadata().map_err(failed)?.len();
let mut noise = vec![0u8; CHUNK];
let mut left = length;
while left > 0 {
let now = usize::try_from(left).map_or(CHUNK, |left| left.min(CHUNK));
getrandom::fill(&mut noise[..now]).map_err(|_| Error::Random)?;
file.write_all(&noise[..now]).map_err(failed)?;
left -= now as u64;
}
file.sync_all().map_err(failed)
}
#[cfg(test)]
mod tests {
use super::*;
fn write(path: &Path, bytes: &[u8]) {
fs::write(path, bytes).unwrap();
}
fn pattern(length: usize) -> Vec<u8> {
(0..length).map(|index| (index % 251) as u8).collect()
}
#[test]
fn the_parts_are_the_vault_its_copies_and_unfinished_writes_in_that_order() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
write(&vault, b"vault");
write(&copies::path(&vault, 1), b"one");
write(&copies::path(&vault, 3), b"three");
write(&temporary_path(&vault), b"crashed save");
write(&temporary_path(&copies::path(&vault, 2)), b"crashed copy");
let found: Vec<(PathBuf, Role)> = parts(&vault)
.into_iter()
.map(|part| (part.path, part.role))
.collect();
assert_eq!(
found,
vec![
(vault.clone(), Role::Vault),
(copies::path(&vault, 1), Role::Copy(1)),
(copies::path(&vault, 3), Role::Copy(3)),
(temporary_path(&vault), Role::Unfinished),
(temporary_path(&copies::path(&vault, 2)), Role::Unfinished),
]
);
}
#[test]
fn a_part_says_how_large_it_is() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
write(&vault, &[7; 1234]);
assert_eq!(parts(&vault)[0].size, Some(1234));
}
#[test]
fn nothing_but_sefys_own_names_is_a_part() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
write(&vault, b"vault");
for neighbour in ["notes.bak.4", "notes.bak.old", "notes.bak.0", "notes.txt"] {
write(&directory.path().join(neighbour), b"mine");
}
fs::create_dir(copies::path(&vault, 2)).unwrap();
let found: Vec<PathBuf> = parts(&vault).into_iter().map(|part| part.path).collect();
assert_eq!(found, vec![vault]);
}
#[test]
fn destroying_a_file_overwrites_what_it_held_before_deleting_it() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("notes.bak");
let witness = directory.path().join("witness");
let original = pattern(3 * CHUNK + 1001);
write(&path, &original);
fs::hard_link(&path, &witness).unwrap();
assert!(destroy(&path).unwrap());
assert!(!path.exists(), "the name is gone");
let left = fs::read(&witness).unwrap();
assert_eq!(left.len(), original.len(), "overwritten, not truncated");
for (block, (now, then)) in left.chunks(4096).zip(original.chunks(4096)).enumerate() {
assert_ne!(now, then, "block {block} still holds what it did");
}
}
#[test]
fn the_overwrite_is_noise_not_a_fill() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("notes.bak");
let witness = directory.path().join("witness");
write(&path, &vec![0u8; CHUNK]);
fs::hard_link(&path, &witness).unwrap();
destroy(&path).unwrap();
let left = fs::read(&witness).unwrap();
let mut seen = [false; 256];
for byte in &left {
seen[usize::from(*byte)] = true;
}
let distinct = seen.iter().filter(|seen| **seen).count();
assert!(distinct > 200, "only {distinct} distinct byte values");
}
#[test]
fn a_file_that_is_not_there_is_reported_rather_than_failed() {
let directory = tempfile::tempdir().unwrap();
assert!(!destroy(&directory.path().join("gone.bak")).unwrap());
}
#[test]
fn an_empty_file_is_destroyed_too() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("notes.bak");
write(&path, b"");
assert!(destroy(&path).unwrap());
assert!(!path.exists());
}
struct Unwritable {
#[cfg(windows)]
_holder: fs::File,
#[cfg(not(windows))]
path: PathBuf,
}
impl Unwritable {
#[cfg(windows)]
fn new(path: &Path) -> Self {
use std::os::windows::fs::OpenOptionsExt;
const READ_AND_DELETE: u32 = 0x1 | 0x4;
let holder = fs::OpenOptions::new()
.read(true)
.share_mode(READ_AND_DELETE)
.open(path)
.unwrap();
Self { _holder: holder }
}
#[cfg(not(windows))]
fn new(path: &Path) -> Self {
use std::os::unix::fs::PermissionsExt;
fs::set_permissions(path, fs::Permissions::from_mode(0o444)).unwrap();
Self {
path: path.to_path_buf(),
}
}
}
#[cfg(not(windows))]
impl Drop for Unwritable {
fn drop(&mut self) {
use std::os::unix::fs::PermissionsExt;
let _ = fs::set_permissions(&self.path, fs::Permissions::from_mode(0o644));
}
}
#[test]
fn a_file_that_cannot_be_overwritten_is_left_as_it_was() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("notes.bak");
write(&path, b"still here");
let _unwritable = Unwritable::new(&path);
let outcome = destroy(&path);
assert!(outcome.is_err(), "{outcome:?}");
assert_eq!(fs::read(&path).unwrap(), b"still here");
}
#[test]
fn a_wipe_carries_on_past_a_part_it_cannot_destroy() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
write(&vault, b"vault");
for number in 1..=copies::KEPT {
write(&copies::path(&vault, number), b"copy");
}
let stuck = copies::path(&vault, 2);
let _unwritable = Unwritable::new(&stuck);
let wiped = carry_out(parts(&vault), None);
let destroyed: Vec<Role> = wiped.destroyed.iter().map(|part| part.role).collect();
assert_eq!(destroyed, vec![Role::Vault, Role::Copy(1), Role::Copy(3)]);
assert_eq!(wiped.failed.len(), 1);
assert_eq!(wiped.failed[0].0.path, stuck);
assert!(!wiped.complete());
assert_eq!(fs::read(&stuck).unwrap(), b"copy");
}
#[test]
#[should_panic(expected = "no file of its own")]
fn a_copy_held_in_memory_cannot_wipe_the_vault_it_came_from() {
let directory = tempfile::tempdir().unwrap();
let path = directory.path().join("notes.bak");
let vault = crate::Vault::create(&path, b"password").unwrap();
let scratch = vault.scratch_copy().unwrap();
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| scratch.wipe()));
assert!(path.exists(), "the real vault was touched");
std::panic::resume_unwind(outcome.expect_err("the wipe went ahead"));
}
#[test]
fn a_part_deleted_before_the_wipe_reached_it_is_named_as_gone() {
let directory = tempfile::tempdir().unwrap();
let vault = directory.path().join("notes.bak");
write(&vault, b"vault");
write(&copies::path(&vault, 1), b"copy");
let listed = parts(&vault);
fs::remove_file(copies::path(&vault, 1)).unwrap();
let wiped = carry_out(listed, None);
assert!(wiped.complete());
assert_eq!(wiped.vanished.len(), 1);
assert_eq!(wiped.vanished[0].role, Role::Copy(1));
}
}