hatchzip 0.1.0

Extract huge archives in place: the archive shrinks as your files hatch, so you only need room for the output.
mod cli;

use std::io::{self, BufRead, IsTerminal, Write};
use std::path::Path;
use std::process::ExitCode;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use anstream::{eprintln, print, println};
use anstyle::{AnsiColor, Style};
use anyhow::{Context, Result, bail};
use clap::{CommandFactory, Parser};
use indicatif::{ProgressBar, ProgressStyle};

use cli::{
    Cli, Command, EXIT_DAMAGED, EXIT_FAILURE, EXIT_NO_SPACE, EXIT_PASSWORD, EXIT_PAUSED, Secret,
    Space, Target,
};
use hatchzip_core::archive::{Kind, PasswordNeeded, WrongPassword};
use hatchzip_core::engine::{self, Job, Outcome, Progress, Run};
use hatchzip_core::human;
use hatchzip_core::journal::{Journal, Paths};
use hatchzip_core::session::{self, State, display_name, locate};

const ERROR: Style = AnsiColor::Red.on_default().bold();
const GOOD: Style = AnsiColor::Green.on_default().bold();
const BOLD: Style = Style::new().bold();
const DIM: Style = Style::new().dimmed();

static STOP: AtomicBool = AtomicBool::new(false);
static QUIET: AtomicBool = AtomicBool::new(false);

/// Normal output, silenced by `--quiet`. Errors and prompts always show.
macro_rules! say {
    ($($t:tt)*) => {
        if !QUIET.load(Ordering::Relaxed) {
            println!($($t)*);
        }
    };
}

fn main() -> ExitCode {
    let cli = Cli::parse();
    QUIET.store(cli.quiet, Ordering::Relaxed);
    match run(cli.command) {
        Ok(code) => code,
        Err(e) if e.is::<PasswordNeeded>() || e.is::<WrongPassword>() => {
            eprintln!("{ERROR}error:{ERROR:#} {e:#}");
            if !io::stdin().is_terminal() {
                eprintln!(
                    "Pass --password or set HATCHZIP_PASSWORD when not running in a terminal."
                );
            }
            ExitCode::from(EXIT_PASSWORD)
        }
        Err(e) => {
            eprintln!("{ERROR}error:{ERROR:#} {e:#}");
            ExitCode::from(EXIT_FAILURE)
        }
    }
}

/// Opens the archive, asking for a password when it is encrypted and none
/// was given, and again (up to three times) when one was wrong. Nothing is
/// touched until the password is proven.
fn open(
    archive: &Path,
    output: Option<std::path::PathBuf>,
    writable: bool,
    secret: Secret,
) -> Result<(State, Paths)> {
    let (primary, paths) = locate(archive)?;
    let mut tries = 0;
    let (state, _) = session::open_encrypted(
        &primary,
        &paths,
        output,
        writable,
        secret.password,
        |wrong| {
            if wrong {
                eprintln!("{ERROR}Wrong password.{ERROR:#}");
            }
            tries += 1;
            if tries > 3 || !io::stdin().is_terminal() {
                return None;
            }
            rpassword::prompt_password("Password: ").ok()
        },
    )?;
    Ok((state, paths))
}

fn run(command: Command) -> Result<ExitCode> {
    match command {
        Command::Extract {
            target,
            verify_first,
            space,
            yes,
        } => extract(target, verify_first, space, yes),
        Command::Check { target, space } => check(target, space),
        Command::List { archive, secret } => list(&archive, secret),
        Command::Test { archive, secret } => test_command(&archive, secret),
        Command::Restore { archive } => restore(&archive),
        Command::Completions { shell } => {
            // Named after however we were called, so `hz completions bash`
            // completes the `hz` alias.
            let name = std::env::args_os()
                .next()
                .and_then(|a| {
                    Path::new(&a)
                        .file_stem()
                        .map(|s| s.to_string_lossy().into_owned())
                })
                .filter(|n| n == "hz")
                .unwrap_or_else(|| "hatchzip".into());
            clap_complete::generate(shell, &mut Cli::command(), name, &mut io::stdout());
            Ok(ExitCode::SUCCESS)
        }
        Command::Man => {
            clap_mangen::Man::new(Cli::command()).render(&mut io::stdout())?;
            Ok(ExitCode::SUCCESS)
        }
    }
}

/// Prints the space plan and returns whether it fits. The verdict prints
/// even under `--quiet`, since it is the whole answer.
fn report(job: &Job, out: &Path, journal: Option<&Journal>, keep_free: u64) -> Result<bool> {
    let plan = session::plan(job, out, journal.is_some(), keep_free)?;
    let left = job.remaining_size();

    say!(
        "{BOLD}{}{BOLD:#}  {} {}, unpacks to {} in {}",
        display_name(job),
        human(job.storage.original_len()),
        job.format_name,
        human(journal.map_or(left, |j| j.total_size)),
        entries(job.item_count())
    );
    if let Some(j) = journal {
        say!(
            "  resuming: {} of {} left",
            human(left),
            human(j.total_size)
        );
    }
    say!("  free space now        {}", human(plan.free));
    say!("  normal extractors     {} free", human(plan.normal_needs));
    say!(
        "  HatchZip needs        {BOLD}{}{BOLD:#} free {DIM}(+{} kept spare){DIM:#}",
        human(plan.needed),
        human(plan.keep_free)
    );
    if !plan.reclaims {
        say!(
            "  {DIM}note: the output is on another disk, so deleting the archive won't make room there{DIM:#}"
        );
    }
    if plan.fits {
        say!("  free after            {}", human(plan.free_after));
    } else {
        let short = human(plan.needed + plan.keep_free - plan.free);
        println!("{ERROR}Not enough space.{ERROR:#} Free up {short} more, or lower --keep-free.");
        if plan.solid_block_bound {
            println!(
                "{DIM}Most of that is one solid block, which can only be freed once all of it is \
                 extracted. Archives packed with smaller solid blocks need far less.{DIM:#}"
            );
        }
    }
    Ok(plan.fits)
}

fn check(target: Target, space: Space) -> Result<ExitCode> {
    let (state, _) = open(&target.archive, target.output, false, target.secret)?;
    let fits = match state {
        State::Fresh(job, out) => report(&job, &out, None, space.keep_free)?,
        State::Resume(job, journal) => {
            report(&job, &journal.output, Some(&journal), space.keep_free)?
        }
        State::Orphan(_) => {
            say!("The archive is already fully extracted; run extract to finish cleaning up.");
            true
        }
    };
    Ok(if fits {
        ExitCode::SUCCESS
    } else {
        ExitCode::from(EXIT_NO_SPACE)
    })
}

fn list(archive: &Path, secret: Secret) -> Result<ExitCode> {
    let job = match open(archive, None, false, secret)?.0 {
        State::Fresh(job, _) | State::Resume(job, _) => job,
        State::Orphan(_) => bail!("nothing is left in this archive"),
    };
    println!("{BOLD}{:>10}  Name{BOLD:#}", "Size");
    for item in job.units.iter().rev().flat_map(|u| &u.items) {
        let path = item.path.display();
        match &item.kind {
            Kind::Dir => println!("{:>10}  {path}/", ""),
            Kind::File => println!("{:>10}  {path}", human(item.size)),
            Kind::Symlink => println!("{:>10}  {path} {DIM}(symlink){DIM:#}", ""),
            Kind::HardLink(t) => {
                println!("{:>10}  {path} {DIM}(link to {}){DIM:#}", "", t.display())
            }
        }
    }
    say!(
        "{DIM}{}, {} unpacked, {} {} archive{DIM:#}",
        entries(job.item_count()),
        human(job.remaining_size()),
        human(job.storage.original_len()),
        job.format_name
    );
    Ok(ExitCode::SUCCESS)
}

fn test_command(archive: &Path, secret: Secret) -> Result<ExitCode> {
    let job = match open(archive, None, false, secret)?.0 {
        State::Fresh(job, _) => job,
        _ => bail!("this archive is half extracted; run extract to continue it"),
    };
    if let Err(e) = verify(&job) {
        eprintln!("{ERROR}error:{ERROR:#} {e:#}");
        return Ok(ExitCode::from(EXIT_DAMAGED));
    }
    match job.item_count() {
        1 => say!("{GOOD}The one entry is intact.{GOOD:#}"),
        n => say!("{GOOD}All {n} entries are intact.{GOOD:#}"),
    }
    Ok(ExitCode::SUCCESS)
}

fn entries(n: usize) -> String {
    if n == 1 {
        "1 entry".into()
    } else {
        format!("{n} entries")
    }
}

fn restore(archive: &Path) -> Result<ExitCode> {
    let r = hatchzip_core::restore::restore(archive)?;
    let name = r.archive.file_name().unwrap_or_default().to_string_lossy();
    say!(
        "{GOOD}Restored.{GOOD:#} {name} is a normal {} archive again, holding the {} not yet \
         extracted.",
        r.format,
        entries(r.entries)
    );
    say!(
        "The entries already extracted stay in {}.",
        r.output.display()
    );
    Ok(ExitCode::SUCCESS)
}

fn extract(target: Target, verify_first: bool, space: Space, yes: bool) -> Result<ExitCode> {
    let keep_free = space.keep_free;
    let (state, paths) = open(&target.archive, target.output, true, target.secret)?;
    let (job, journal) = match state {
        State::Orphan(journal) => {
            engine::finish_orphaned(&journal, &paths)?;
            say!(
                "{GOOD}Done.{GOOD:#} Everything is in {}.",
                journal.output.display()
            );
            return Ok(ExitCode::SUCCESS);
        }
        State::Resume(job, journal) => {
            if !report(&job, &journal.output, Some(&journal), keep_free)? {
                return Ok(ExitCode::from(EXIT_NO_SPACE));
            }
            session::clear_uncommitted(&job, &journal.output)?;
            (job, journal)
        }
        State::Fresh(job, out) => {
            if !report(&job, &out, None, keep_free)? {
                return Ok(ExitCode::from(EXIT_NO_SPACE));
            }
            if verify_first && let Err(e) = verify(&job) {
                eprintln!("{ERROR}error:{ERROR:#} {e:#}");
                return Ok(ExitCode::from(EXIT_DAMAGED));
            }
            if !yes {
                confirm(&display_name(&job), &out)?;
            }
            let journal = session::begin(&job, &paths, out)?;
            (job, journal)
        }
    };

    ctrlc::set_handler(|| {
        if STOP.swap(true, Ordering::Relaxed) {
            std::process::exit(EXIT_PAUSED.into());
        }
        eprintln!(
            "\nPausing. Files that were half written will be redone next time. \
             Press Ctrl+C again to quit now."
        );
    })
    .context("installing Ctrl+C handler")?;

    let bar = progress_bar(journal.total_size, "Hatching");
    bar.set_position(journal.total_size - job.remaining_size());
    bar.set_message(format!("archive {}", human(journal.committed_len)));

    let output = journal.output.clone();
    let name = display_name(&job);
    let (b1, b2) = (bar.clone(), bar.clone());
    let run = Run {
        job,
        journal,
        paths,
        stop: &STOP,
        progress: Progress {
            on_bytes: Box::new(move |n| b1.inc(n)),
            on_commit: Box::new(move |len| b2.set_message(format!("archive {}", human(len)))),
        },
        keep_free,
    };
    let outcome = run.execute();
    bar.finish_and_clear();

    match outcome {
        Ok(Outcome::Finished) => {
            say!(
                "{GOOD}Done.{GOOD:#} Everything is in {}, and {name} is gone.",
                output.display()
            );
            Ok(ExitCode::SUCCESS)
        }
        Ok(Outcome::Paused) => {
            say!("Paused. Run the same command again to continue.");
            Ok(ExitCode::from(EXIT_PAUSED))
        }
        Err(e) => {
            eprintln!("{ERROR}error:{ERROR:#} {e:#}");
            eprintln!("Progress is saved. Run the same command again to continue.");
            Ok(ExitCode::from(EXIT_FAILURE))
        }
    }
}

fn verify(job: &Job) -> Result<()> {
    let bar = progress_bar(job.remaining_size(), "Checking");
    let b = bar.clone();
    let result = job.verify(&move |n| b.inc(n));
    bar.finish_and_clear();
    result.context("the archive is damaged, so nothing was extracted")
}

fn confirm(archive: &str, output: &Path) -> Result<()> {
    if !io::stdin().is_terminal() {
        bail!("HatchZip deletes the archive as it extracts; pass --yes to confirm");
    }
    print!(
        "\nThis extracts into {} and deletes {BOLD}{archive}{BOLD:#} as it goes. Continue? [y/N] ",
        output.display()
    );
    io::stdout().flush()?;
    let mut answer = String::new();
    io::stdin().lock().read_line(&mut answer)?;
    if !matches!(answer.trim(), "y" | "Y" | "yes") {
        bail!("cancelled; nothing was changed");
    }
    Ok(())
}

fn progress_bar(total: u64, prefix: &'static str) -> ProgressBar {
    if QUIET.load(Ordering::Relaxed) {
        return ProgressBar::hidden();
    }
    let bar = ProgressBar::new(total);
    bar.set_style(
        ProgressStyle::with_template(
            "{prefix} [{bar:32}] {binary_bytes}/{binary_total_bytes}  {binary_bytes_per_sec}  eta {eta}  {msg}",
        )
        .expect("valid template")
        .progress_chars("=> "),
    );
    bar.set_prefix(prefix);
    bar.enable_steady_tick(Duration::from_millis(200));
    bar
}