use std::collections::HashMap;
use std::fs;
use std::io::{self, Read, Write};
use std::path::Path;
use sha2::{Digest, Sha256};
use super::manifest::{Listed, MANIFEST, MOST_BYTES, Manifest};
use super::{Backup, is_inside, zip};
use crate::durable::sync_directory;
use crate::text::hex;
use crate::{Error, ErrorKind, Result};
pub fn restore(archive: impl AsRef<Path>, dir: impl AsRef<Path>) -> Result<Backup> {
let (archive, dir) = (archive.as_ref(), dir.as_ref());
let within = |error: Error| error.within(format!("restore {} into {}", archive.display(), dir.display()));
let mut zip = io::BufReader::new(fs::File::open(archive).map_err(|error| within(Error::io("open", error)))?);
let (entries, manifest) = read(&mut zip).map_err(|why| within(Error::new(ErrorKind::Corrupt, why)))?;
claim_empty(dir).map_err(within)?;
let lock = crate::store::take_lock(dir).map_err(within)?;
let restored =
manifest.files.iter().try_for_each(|file| put(&mut zip, &entries, dir, file)).and_then(|()| synced(dir));
if let Err(error) = restored {
let emptied = empty(dir);
drop(lock);
let lock_file = dir.join("LOCK");
let error = match emptied.and_then(|()| removed(&lock_file)) {
Ok(()) => error,
Err(left) => error.within(left),
};
return Err(within(error));
}
drop(lock);
Ok(manifest.summary())
}
fn read(zip: &mut (impl Read + io::Seek)) -> std::result::Result<(HashMap<String, zip::Entry>, Manifest), String> {
let entries: HashMap<String, zip::Entry> =
zip::entries(zip)?.into_iter().map(|entry| (entry.name.clone(), entry)).collect();
let listed = entries.get(MANIFEST).ok_or("a zip without the manifest a backup holds")?;
if listed.size > MOST_BYTES {
return Err(format!("a manifest of {} bytes, past the {MOST_BYTES} a restore reads", listed.size));
}
let mut text = Vec::new();
zip::open(zip, listed)?.read_to_end(&mut text).map_err(|error| error.to_string())?;
let manifest = Manifest::read(&text)?;
for file in &manifest.files {
if !is_inside(&file.name) || file.name == MANIFEST || file.name == "LOCK" {
return Err(format!("{:?}: a path that is not a file of a store's directory", file.name));
}
if !entries.contains_key(&file.name) {
return Err(format!("{}: listed by its manifest, and not in the zip", file.name));
}
}
Ok((entries, manifest))
}
fn claim_empty(dir: &Path) -> Result<()> {
match fs::read_dir(dir).map(|mut entries| entries.next().is_some()) {
Ok(true) => Err(Error::invalid("the directory is not empty: a restore writes into an empty one")),
Ok(false) => Ok(()),
Err(error) if error.kind() == io::ErrorKind::NotFound => {
fs::create_dir_all(dir).map_err(|error| Error::io("make the directory", error))
}
Err(error) => Err(Error::io("read the directory", error)),
}
}
fn put(
zip: &mut (impl Read + io::Seek),
entries: &HashMap<String, zip::Entry>,
dir: &Path,
file: &Listed,
) -> Result<()> {
let corrupt = |why: String| Error::new(ErrorKind::Corrupt, format!("{}: {why}", file.name));
let failed = |error: io::Error| Error::io(file.name.clone(), error);
let entry = &entries[&file.name];
if entry.size != file.size {
return Err(corrupt(format!("{} bytes in the zip, where its manifest says {}", entry.size, file.size)));
}
let target = dir.join(&file.name);
if let Some(parent) = target.parent() {
fs::create_dir_all(parent).map_err(failed)?;
}
let partial = target
.with_file_name(format!("{}.partial", target.file_name().and_then(|name| name.to_str()).unwrap_or("file")));
let mut out = owned_file(&partial).map_err(failed)?;
let mut bytes = zip::open(zip, entry).map_err(corrupt)?;
let mut sha = Sha256::new();
let mut buffer = vec![0; 1 << 16];
loop {
let read = match bytes.read(&mut buffer) {
Ok(0) => break,
Ok(read) => read,
Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
Err(error) if matches!(error.kind(), io::ErrorKind::InvalidData | io::ErrorKind::UnexpectedEof) => {
return Err(corrupt(error.to_string()));
}
Err(error) => return Err(failed(error)),
};
sha.update(&buffer[..read]);
out.write_all(&buffer[..read]).map_err(failed)?;
}
if hex(&sha.finalize()) != file.sha256 {
return Err(corrupt("its SHA-256 is not its manifest's".to_owned()));
}
out.sync_all().map_err(failed)?;
drop(out);
fs::rename(&partial, &target).map_err(failed)
}
fn owned_file(path: &Path) -> io::Result<fs::File> {
let mut options = fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
options.open(path)
}
fn synced(dir: &Path) -> Result<()> {
let mut folders = vec![dir.to_path_buf()];
while let Some(folder) = folders.pop() {
for entry in fs::read_dir(&folder).map_err(|error| Error::io("sync", error))? {
let path = entry.map_err(|error| Error::io("sync", error))?.path();
if path.is_dir() {
folders.push(path);
}
}
sync_directory(&folder).map_err(|error| Error::io(format!("sync {}", folder.display()), error))?;
}
Ok(())
}
fn empty(dir: &Path) -> std::result::Result<(), String> {
let entries = fs::read_dir(dir).map_err(|error| format!("{} is left: {error}", dir.display()))?;
for entry in entries {
let path = entry.map_err(|error| format!("something of {} is left: {error}", dir.display()))?.path();
if path.file_name().is_some_and(|name| name == "LOCK") {
continue;
}
let removed = if path.is_dir() { fs::remove_dir_all(&path) } else { fs::remove_file(&path) };
removed.map_err(|error| format!("{} is left: {error}", path.display()))?;
}
Ok(())
}
fn removed(file: &Path) -> std::result::Result<(), String> {
match fs::remove_file(file) {
Err(error) if error.kind() != io::ErrorKind::NotFound => Err(format!("{} is left: {error}", file.display())),
_ => Ok(()),
}
}