tinystore 0.10.0

A small storage runtime for applications on SQLite: SQL, key-value state, durable jobs, files, search, logs and metrics in one directory
Documentation
//! A backup put back into a directory before a store opens there: every
//! file checked against its manifest's size and SHA-256 and its zip's
//! CRC-32 as it is written, and nothing left in the directory when one does
//! not match. A backup checked alone, `verify`, makes each of those checks
//! and writes nothing.

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, Manifest};
use super::{Backup, is_inside, zip};
use crate::durable::sync_directory;
use crate::store::{HALF_RESTORED, RESTORING};
use crate::text::{hex, is_device};
use crate::{Error, ErrorKind, Result};

/// Restores the backup at `archive` into `dir`, which is empty or absent,
/// before a store opens there; the store's `LOCK` is held meanwhile, so that
/// none opens before the restore ends. A file that does not match what the
/// backup says of it fails the restore `Corrupt`, and leaves `dir` empty.
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)))?;
    refuse_devices(&manifest).map_err(within)?;
    claim_empty(dir).map_err(within)?;
    let lock = crate::store::take_lock(dir).map_err(within)?;
    let restored = begun(dir)
        .and_then(|()| manifest.files.iter().try_for_each(|file| put(&mut zip, &entries, dir, file)))
        .and_then(|()| synced(dir))
        .and_then(|()| finished(dir));
    if let Err(error) = restored {
        // Emptied while the LOCK is held, so no store opens the half restored;
        // the marker goes last, so that a directory emptied in part opens no more.
        let emptied = empty(dir).and_then(|()| removed(&dir.join(RESTORING)));
        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())
}

/// Checks the backup at `archive` as [`restore`] checks it, and writes
/// nothing: its zip's directory and manifest, each file's size, CRC-32 and
/// SHA-256, and on Windows the names Windows reads as devices. What it
/// answers is what a restore of it writes; a file that does not match what
/// the backup says of it is `Corrupt`, naming the file.
///
/// ```no_run
/// # fn main() -> tinystore::Result<()> {
/// let backup = tinystore::backup::verify("backup-2026-10-13.zip")?;
/// println!("{} files, {} bytes a restore writes", backup.files, backup.bytes);
/// # Ok(())
/// # }
/// ```
pub fn verify(archive: impl AsRef<Path>) -> Result<Backup> {
    let archive = archive.as_ref();
    let within = |error: Error| error.within(format!("verify {}", archive.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)))?;
    refuse_devices(&manifest).map_err(within)?;
    for file in &manifest.files {
        copy_checked(&mut zip, &entries, file, &mut io::sink()).map_err(within)?;
    }
    Ok(manifest.summary())
}

/// The zip's files by name, and its manifest, checked.
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")?;
    // a manifest lists files its zip holds, so no more than it holds
    let manifest = Manifest::read(zip::open(zip, listed)?, entries.len())?;
    for file in &manifest.files {
        if !is_inside(&file.name) || [MANIFEST, "LOCK", RESTORING].contains(&file.name.as_str()) {
            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))
}

/// Refuses, on Windows, a backup that holds a file a part of whose path
/// Windows reads as a device, before anything is written: there, before
/// Windows 11, `sql/nul.db` is the device NUL and not a file. A store on
/// another system may hold such a database, made by a release before a new
/// one was refused the name, and its backup restores there as it was.
fn refuse_devices(manifest: &Manifest) -> Result<()> {
    if !cfg!(windows) {
        return Ok(());
    }
    for file in &manifest.files {
        // Windows reads a part as a device whatever its case and extension
        let mut stems =
            file.name.split('/').map(|part| part.split('.').next().unwrap_or_default().to_ascii_lowercase());
        let Some(device) = stems.find(|stem| is_device(stem)) else { continue };
        let database = file.name.strip_prefix("sql/").and_then(|rest| rest.split(['/', '.']).next());
        let whose = match database {
            Some(name) if name.eq_ignore_ascii_case(&device) => format!("a database's name {name:?}, of {}", file.name),
            _ => format!("{}: {device:?}", file.name),
        };
        return Err(Error::invalid(format!(
            "{whose}: Windows reads it as a device, not a file: restore the backup on another system"
        )));
    }
    Ok(())
}

/// Makes `dir` when it is absent; one that holds anything is refused, and
/// one a restore did not finish in says what to do.
fn claim_empty(dir: &Path) -> Result<()> {
    match fs::read_dir(dir).map(|mut entries| entries.next().is_some()) {
        Ok(true) if dir.join(RESTORING).exists() => {
            Err(Error::invalid(format!("the directory is not empty: {HALF_RESTORED}")))
        }
        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)),
    }
}

/// Writes one file beside its name, checks it, syncs it, and only then gives
/// it its name.
fn put(
    zip: &mut (impl Read + io::Seek),
    entries: &HashMap<String, zip::Entry>,
    dir: &Path,
    file: &Listed,
) -> Result<()> {
    let failed = |error: io::Error| Error::io(file.name.clone(), error);
    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)?;
    copy_checked(zip, entries, file, &mut out)?;
    out.sync_all().map_err(failed)?;
    drop(out);
    fs::rename(&partial, &target).map_err(failed)
}

/// Copies one file of the zip into `out`, checked as it goes by: its size
/// against its manifest's, its CRC-32 against the zip's as `zip::open`'s
/// reader ends, and its SHA-256 against its manifest's once all of it went.
fn copy_checked(
    zip: &mut (impl Read + io::Seek),
    entries: &HashMap<String, zip::Entry>,
    file: &Listed,
    out: &mut impl Write,
) -> 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 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()));
    }
    Ok(())
}

/// A new file only its owner reads and writes, as the files of a store and
/// its key are kept.
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)
}

/// Syncs every directory the restore wrote names in, so that the names
/// survive a power loss as the bytes do.
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(())
}

/// Puts `RESTORING` in `dir` before any file of the backup's, its name
/// synced, so that a restore whose process dies leaves a directory no store
/// opens.
fn begun(dir: &Path) -> Result<()> {
    let failed = |error: io::Error| Error::io(RESTORING, error);
    owned_file(&dir.join(RESTORING)).and_then(|marker| marker.sync_all()).map_err(failed)?;
    sync_directory(dir).map_err(failed)
}

/// Removes `RESTORING` once every file and name is synced, and syncs its
/// removal: the directory opens from then on.
fn finished(dir: &Path) -> Result<()> {
    let failed = |error: io::Error| Error::io(RESTORING, error);
    fs::remove_file(dir.join(RESTORING)).map_err(failed)?;
    sync_directory(dir).map_err(failed)
}

/// Removes what a restore that failed wrote, `dir` itself left; what will
/// not go is said.
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();
        // The LOCK is held while this runs, and goes once let go; the marker
        // goes once all else has.
        if path.file_name().is_some_and(|name| name == "LOCK" || name == RESTORING) {
            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(()),
    }
}