use std::collections::{HashMap, HashSet};
use std::fs::{self, File};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
use anyhow::{Context, Result, anyhow, bail};
use rayon::prelude::*;
use crate::archive::resumable::{self, Decoder};
use crate::archive::storage::{Reads, Storage, Tail};
use crate::archive::targz::{Event, Profile, Walker};
use crate::archive::{self, Codec, Format, Item, Kind, PasswordCheck, Sink, Stream, Unit};
use crate::journal::{Journal, Link, Paths, Resume, TarAt};
use crate::{fsutil, platform, replace};
const BATCH_MAX_UNITS: usize = 512;
const BATCH_MAX_BYTES: u64 = 2 * 1024 * 1024 * 1024;
const BUF_SIZE: usize = 1024 * 1024;
const MAX_SYMLINK: u64 = 64 * 1024;
const STREAM_STEP: u64 = 256 * 1024 * 1024;
fn stream_step() -> u64 {
test_knob("HATCHZIP_STREAM_STEP").map_or(STREAM_STEP, u64::from)
}
fn stream_threshold() -> u64 {
2 * stream_step()
}
fn always_stream() -> bool {
test_knob("HATCHZIP_STREAM_STEP").is_some()
}
const MAX_TOTAL_SIZE: u64 = 1 << 62;
fn max_tail() -> u64 {
test_knob("HATCHZIP_MAX_TAIL").map_or(1_000_000_000, u64::from)
}
fn on_disk(u: &Unit) -> u64 {
u.items.iter().fold(0u64, |sum, i| {
let size = i.size.checked_next_multiple_of(4096).unwrap_or(u64::MAX);
sum.saturating_add(size)
})
}
pub struct Job {
pub storage: Arc<Storage>,
pub format_name: &'static str,
format: Box<dyn Format>,
pub units: Vec<Unit>,
pub tail_start: u64,
pub top: u64,
punch: OnceLock<Option<u64>>,
resume: Option<Resume>,
started: bool,
}
impl Job {
pub fn open(
path: &Path,
writable: bool,
password: Option<&str>,
reads: Arc<Reads>,
) -> Result<Self> {
let job = Self::open_as_is(path, writable, password, reads)?;
job.check_layout()?;
Ok(job)
}
pub(crate) fn open_as_is(
path: &Path,
writable: bool,
password: Option<&str>,
reads: Arc<Reads>,
) -> Result<Self> {
let mut storage = Storage::open(path, writable)?;
storage.count_reads(reads);
let storage = Arc::new(storage);
let opened = archive::open(&storage, None, password)?;
let top = storage.original_len();
if top.saturating_sub(opened.tail_start) > max_tail() {
bail!(
"this archive holds more than {} after its last entry, which HatchZip would \
have to keep in memory for the whole extraction. The archive was not modified",
human(max_tail())
);
}
Self::build(storage, opened, top)
}
pub fn resume(
journal: &Journal,
tail: Arc<Tail>,
writable: bool,
password: Option<&str>,
reads: Arc<Reads>,
) -> Result<Self> {
let mut storage = Storage::from_parts(&journal.parts, writable)?;
storage.count_reads(reads);
let storage = Arc::new(storage);
let current = storage.current_len()?;
if journal.keep {
storage.protect();
if current != storage.original_len() {
bail!(
"{} has changed since the extraction started. Put the original archive back \
to resume, or run restore to discard the progress",
storage.primary().display()
);
}
}
if current < journal.committed_len {
bail!(
"{} is shorter than HatchZip left it and will not be modified",
storage.primary().display()
);
}
if current > journal.committed_len && writable && !journal.keep {
storage.set_len(journal.committed_len)?;
}
let point = journal.resume.as_ref();
let opened = archive::open(&storage, Some((&journal.format, tail, point)), password)?;
let mut job = Self::build(storage, opened, journal.committed_len)?;
job.started = journal.replace.as_ref().is_none_or(|r| r.moved);
if let Some(r) = &journal.resume {
if job.units.first().map(|u| u.start) != Some(r.unit) {
bail!(
"the journal of {} records a partially extracted file that is not the next one \
in the archive. The archive will not be modified",
job.storage.primary().display()
);
}
job.resume = Some(r.clone());
}
Ok(job)
}
fn build(storage: Arc<Storage>, opened: archive::Opened, top: u64) -> Result<Self> {
let mut units = opened.units;
let len = storage.original_len();
if opened.tail_start > len || units.iter().any(|u| u.start >= len) {
return Err(archive::Damaged(anyhow!(
"this archive records data past the end of the file, so it is damaged or \
incomplete. Nothing was modified"
))
.into());
}
units.retain(|u| u.start < top);
units.sort_by_key(|u| std::cmp::Reverse(u.start));
if units.windows(2).any(|w| w[0].start == w[1].start) {
bail!(
"two parts of this archive start at the same offset, so extracting it in place \
could destroy data before it is extracted; extraction refused"
);
}
let total = units
.iter()
.flat_map(|u| &u.items)
.try_fold(0u64, |sum, i| sum.checked_add(i.size));
if total.is_none_or(|t| t > MAX_TOTAL_SIZE) {
return Err(archive::Damaged(anyhow!(
"the file sizes recorded in this archive add up to more than any disk can hold, \
so the archive is damaged. Nothing was modified"
))
.into());
}
let mut seen = HashSet::new();
for item in units.iter().flat_map(|u| &u.items) {
for part in item.path.iter() {
if let Some(why) = platform::name_problem(&part.to_string_lossy()) {
bail!(
"'{}' cannot be created at this location: {why}. The archive was not modified",
item.path.display()
);
}
}
if item.kind != Kind::Dir && !seen.insert(item.path.to_string_lossy().to_lowercase()) {
bail!(
"'{}' appears more than once in the archive (ignoring case); extraction refused",
item.path.display()
);
}
}
Ok(Self {
storage,
format_name: opened.name,
format: opened.format,
units,
tail_start: opened.tail_start,
top,
punch: OnceLock::new(),
resume: None,
started: false,
})
}
fn check_layout(&self) -> Result<()> {
let key = |p: &Path| p.to_string_lossy().to_lowercase();
let items = || self.units.iter().flat_map(|u| &u.items);
if let Some(item) = items().find(|i| {
i.path
.iter()
.any(|c| c.to_string_lossy().to_lowercase().ends_with(PART_SUFFIX))
}) {
bail!(
"'{}' uses a name ending in {PART_SUFFIX}, which HatchZip gives a file while it \
writes it, so extraction is refused. Extract this archive with another program. \
Nothing was modified",
item.path.display()
);
}
let mut folders: HashMap<String, (&Path, bool)> = HashMap::new();
for item in items() {
if item.kind == Kind::Dir {
folders.insert(key(&item.path), (&item.path, true));
}
for parent in item.path.ancestors().skip(1) {
if parent.as_os_str().is_empty() {
break;
}
folders.entry(key(parent)).or_insert((&item.path, false));
}
}
let files: HashSet<&Path> = items()
.filter(|i| i.kind == Kind::File)
.map(|i| i.path.as_path())
.collect();
for item in items() {
let what = match &item.kind {
Kind::Dir => continue,
Kind::File => "a file",
Kind::Symlink => "a symbolic link",
Kind::HardLink(_) => "a hard link",
};
match folders.get(&key(&item.path)) {
Some((dir, true)) if *dir == item.path => bail!(
"'{}' is both {what} and a folder in the archive; extraction refused",
item.path.display()
),
Some((dir, true)) => bail!(
"'{}' is {what}, and '{}' is a folder in the archive; extraction refused",
item.path.display(),
dir.display()
),
Some((inside, false)) => bail!(
"'{}' is {what}, but '{}' requires a folder at that location; extraction \
refused",
item.path.display(),
inside.display()
),
None => {}
}
if let Kind::HardLink(target) = &item.kind
&& !files.contains(target.as_path())
{
bail!(
"hard link '{}' points to '{}', which is not a file in the archive; \
extraction refused",
item.path.display(),
target.display()
);
}
}
Ok(())
}
pub fn punch_support(&self) -> Option<u64> {
*self.punch.get_or_init(|| {
if test_knob("HATCHZIP_NO_PUNCH").is_some() {
return None;
}
let dir = match self.storage.primary().parent() {
Some(d) if !d.as_os_str().is_empty() => d,
_ => Path::new("."),
};
fsutil::can_punch(dir).then(|| platform::punch_granule(dir))
})
}
pub fn encrypted(&self) -> bool {
self.units.iter().any(|u| u.encrypted)
}
pub fn is_stream(&self) -> bool {
self.format.solid().is_some()
}
pub fn kept_whole(&self, reclaims: bool) -> bool {
self.is_stream() && reclaims && self.punch_support().is_none()
}
pub fn check_password(&self) -> Result<()> {
const ENOUGH: u64 = 64 * 1024 * 1024;
let Some(unit) = self.smallest_encrypted() else {
return Ok(());
};
let left = match self.format.password_check() {
PasswordCheck::Headers => return self.format.prove_password(),
PasswordCheck::Sample => ENOUGH,
PasswordCheck::WholeUnit => u64::MAX,
};
let mut worker = self.format.worker(1)?;
let mut sink = ProbeSink {
left,
buf: vec![0; BUF_SIZE],
};
match worker.extract(unit, &mut sink) {
Ok(()) => Ok(()),
Err(e) if e.chain().any(|c| c.is::<ProbeDone>()) => Ok(()),
Err(e) if e.chain().any(|c| c.is::<archive::PasswordNeeded>()) => Err(e),
Err(_) => Err(archive::WrongPassword.into()),
}
}
fn smallest_encrypted(&self) -> Option<&Unit> {
self.units
.iter()
.filter(|u| u.encrypted)
.min_by_key(|u| (u.size() == 0, u.size()))
}
pub fn slow_password_check(&self) -> Option<(&Path, u64)> {
if !self.encrypted() || self.format.password_check() != PasswordCheck::WholeUnit {
return None;
}
let unit = self.smallest_encrypted()?;
Some((unit.items.first()?.path.as_path(), unit.size()))
}
pub fn in_place_refusal(&self) -> Option<String> {
self.format.refuses_in_place()
}
pub fn item_count(&self) -> usize {
self.units.iter().map(|u| u.items.len()).sum()
}
pub fn remaining_size(&self) -> u64 {
let done = self.resume.as_ref().map_or(0, |r| r.written);
self.units
.iter()
.fold(0u64, |sum, u| sum.saturating_add(u.size()))
.saturating_sub(done)
}
fn resumed(&self, i: usize) -> Option<&Resume> {
self.resume
.as_ref()
.filter(|r| r.unit == self.units[i].start)
}
fn stream_of(&self, i: usize) -> Option<Stream> {
let unit = &self.units[i];
match unit.items.as_slice() {
[item] if item.kind == Kind::File && !unit.encrypted => {
self.format.stream(unit).ok().flatten()
}
_ => None,
}
}
fn streams(&self, i: usize, reclaims: bool) -> Option<Stream> {
if self.resumed(i).is_some() {
return self.stream_of(i);
}
if !reclaims || self.units[i].size() < stream_threshold() {
return None;
}
self.punch_support()?;
self.stream_of(i)
}
fn span(&self, i: usize) -> u64 {
let end = if i == 0 {
self.top
} else {
self.units[i - 1].start
};
end - self.units[i].start
}
pub fn space_needed(&self, fresh: bool, reclaims: bool) -> (u64, i128) {
let tail = self.storage.original_len().saturating_sub(self.tail_start) as i128;
let mut extra: i128 = if fresh { tail } else { 0 };
let mut peak = extra;
for (i, u) in self.units.iter().enumerate() {
let span = self.span(i) as i128;
if let Some(solid) = self.format.solid() {
let (rise, net) = self.solid_cost(i, &solid.profile, span, reclaims);
peak = peak.max(extra + rise);
extra += net;
continue;
}
if let Some(s) = self.streams(i, reclaims) {
let (rise, net) = self.stream_cost(i, &s, span, reclaims);
peak = peak.max(extra + rise);
extra += net;
continue;
}
let on_disk = on_disk(u) as i128;
peak = peak.max(extra + on_disk);
extra += on_disk;
if reclaims {
extra -= span;
}
}
if reclaims {
let stub = self.units.last().map_or(self.top, |u| u.start);
extra -= stub as i128 + tail;
}
(peak.max(0) as u64, extra)
}
fn stream_cost(&self, i: usize, s: &Stream, span: i128, reclaims: bool) -> (i128, i128) {
let at = self.resumed(i);
let done = at.map_or(0, |r| r.written / 4096 * 4096);
let out_left = on_disk(&self.units[i]).saturating_sub(done) as i128;
let read_to = at.map_or(s.data_start, |r| r.bit / 8);
let data_left = (s.data_start + s.packed).saturating_sub(read_to) as i128;
let freed = read_to.saturating_sub(s.data_start) as i128;
let net = if reclaims {
out_left - (span - freed)
} else {
out_left
};
let rise = match self.punch_support() {
Some(granule) if reclaims => {
let slack = match s.codec {
Codec::Stored => 0,
Codec::Deflate => out_left / 1024,
};
let step = (stream_step() + BUF_SIZE as u64 + 2 * granule) as i128;
((out_left - data_left).max(0) + slack + step).min(out_left)
}
_ => out_left,
};
(rise, net)
}
fn solid_cost(&self, i: usize, p: &Profile, span: i128, reclaims: bool) -> (i128, i128) {
let out_left = (p.files + p.links) as i128;
let freed = self.resumed(i).map_or(0, |r| r.bit / 8) as i128;
let net = if reclaims {
out_left - (span - freed)
} else {
out_left
};
let rise = match self.punch_support() {
Some(granule) if reclaims => {
let ahead = stream_step() + p.gap + BUF_SIZE as u64 + 2 * granule;
(p.lead.max(0) + ahead as i128).min(p.files as i128)
}
_ => out_left,
};
(rise.max(net), net)
}
fn open_entry(&self) -> Option<&Item> {
let r = self.resume.as_ref()?;
let inside = match &r.stream {
Some(s) => matches!(s.at, TarAt::File { .. }),
None => true,
};
if !inside {
return None;
}
self.units.first().and_then(|u| u.items.first())
}
pub fn uncommitted(&self, out: &Path) -> Vec<(PathBuf, u64)> {
if !self.started {
return Vec::new();
}
let open = self.open_entry().is_some();
let mut found = Vec::new();
for (i, u) in self.units.iter().enumerate() {
for (k, item) in u.items.iter().enumerate() {
if item.kind != Kind::File || (open && i == 0 && k == 0) {
continue;
}
let dest = out.join(&item.path);
for p in [part_path(&dest), dest] {
if let Ok(m) = fs::symlink_metadata(&p)
&& m.is_file()
{
found.push((p, m.len().next_multiple_of(4096)));
}
}
}
}
found
}
pub fn past_resume_point(&self, out: &Path) -> u64 {
let (Some(r), Some(item)) = (self.resume.as_ref(), self.open_entry()) else {
return 0;
};
let dest = out.join(&item.path);
[part_path(&dest), dest]
.iter()
.find_map(|p| fs::metadata(p).ok())
.map_or(0, |m| {
(m.len().next_multiple_of(4096)).saturating_sub(r.written.next_multiple_of(4096))
})
}
pub fn verify(&self, on_bytes: &(dyn Fn(u64) + Sync)) -> Result<()> {
let failures = if self.format.serial() {
self.verify_serial(on_bytes)?
} else {
self.verify_parallel(on_bytes)
};
if failures.is_empty() {
return Ok(());
}
let mut msg = format!("{} damaged:", failures.len());
for f in failures.iter().take(10) {
msg.push_str("\n ");
msg.push_str(f);
}
Err(anyhow!(msg))
}
fn verify_serial(&self, on_bytes: &(dyn Fn(u64) + Sync)) -> Result<Vec<String>> {
let mut worker = self.format.worker(1)?;
let mut sink = VerifySink {
on_bytes,
buf: vec![0; BUF_SIZE],
};
Ok(self
.units
.iter()
.rev()
.filter_map(|u| worker.extract(u, &mut sink).err())
.map(|e| format!("{e:#}"))
.collect())
}
fn verify_parallel(&self, on_bytes: &(dyn Fn(u64) + Sync)) -> Vec<String> {
let threads = threads_per_unit(self.units.len());
self.units
.par_iter()
.map_init(
|| {
(
self.format.worker(threads),
VerifySink {
on_bytes,
buf: vec![0; BUF_SIZE],
},
)
},
|(worker, sink), unit| {
let mut run = || -> Result<()> {
let w = worker.as_mut().map_err(|e| anyhow!("{e:#}"))?;
w.extract(unit, sink)
};
run().err().map(|e| format!("{e:#}"))
},
)
.flatten()
.collect()
}
}
fn threads_per_unit(units: usize) -> u32 {
let cores = std::thread::available_parallelism().map_or(1, |n| n.get());
(cores / units.max(1)).max(1) as u32
}
const PART_SUFFIX: &str = ".hatchzip-part";
pub(crate) fn part_path(dest: &Path) -> PathBuf {
let mut part = dest.as_os_str().to_owned();
part.push(PART_SUFFIX);
PathBuf::from(part)
}
fn create_part(part: &Path) -> Result<File> {
platform::create_new_no_follow(part).map_err(|e| {
if e.kind() == io::ErrorKind::AlreadyExists {
anyhow!(
"{} already exists, and HatchZip does not replace a file it did not create. \
Move it, then run the extraction again",
part.display()
)
} else {
anyhow::Error::new(e).context(format!("creating {}", part.display()))
}
})
}
fn open_part(dest: &Path, part: &Path, written: u64) -> Result<File> {
if written == 0 {
return create_part(part);
}
if fs::symlink_metadata(part).is_err() && fs::symlink_metadata(dest).is_ok() {
fs::rename(dest, part).with_context(|| format!("reopening {}", dest.display()))?;
}
let mut file = match platform::open_existing_no_follow(part) {
Ok(f) => f,
Err(e) if e.kind() == io::ErrorKind::NotFound => bail!(
"{} is missing, and the archive data it was extracted from is already released, so \
this file cannot be completed",
part.display()
),
Err(e) => return Err(e).with_context(|| format!("reopening {}", part.display())),
};
if file.metadata()?.len() < written {
bail!(
"{} is shorter than when its progress was saved; the extraction cannot continue",
part.display()
);
}
file.set_len(written)?;
file.seek(SeekFrom::Start(written))?;
Ok(file)
}
struct OpenFile {
item: Item,
dest: PathBuf,
part: PathBuf,
file: File,
written: u64,
}
impl OpenFile {
fn finish(self) -> Result<()> {
if self.written != self.item.size {
bail!("it is smaller than the archive records");
}
if let Some(t) = self.item.mtime {
self.file.set_modified(t)?;
}
platform::apply_mode(&self.file, self.item.mode)?;
if platform::SYNC_EACH_FILE {
self.file.sync_all()?;
}
drop(self.file);
fs::rename(&self.part, &self.dest)?;
Ok(())
}
}
fn read_window(file: &mut File, written: u64) -> Result<Vec<u8>> {
let n = written.min(resumable::WINDOW as u64);
let mut window = vec![0; n as usize];
file.seek(SeekFrom::Start(written - n))?;
file.read_exact(&mut window)?;
file.seek(SeekFrom::Start(written))?;
Ok(window)
}
pub struct Progress {
pub on_bytes: Box<dyn Fn(u64) + Sync>,
pub on_commit: Box<dyn Fn(u64) + Sync>,
}
pub enum Outcome {
Finished,
Paused,
}
pub struct Run<'a> {
pub job: Job,
pub journal: Journal,
pub paths: Paths,
pub stop: &'a AtomicBool,
pub progress: Progress,
pub keep_free: u64,
}
impl Run<'_> {
pub fn execute(mut self) -> Result<Outcome> {
let out = self.journal.output.clone();
fs::create_dir_all(&out)
.with_context(|| format!("creating output folder {}", out.display()))?;
let reclaims =
!self.journal.keep && fsutil::same_filesystem(self.job.storage.primary(), &out);
if self.journal.keep {
self.job.storage.protect();
}
if let Some(r) = self.journal.replace.as_mut()
&& !r.moved
{
replace::set_aside(&self.job, &out)?;
r.moved = true;
self.journal.save(&self.paths)?;
self.job.started = true;
}
let mut next = 0;
while next < self.job.units.len() {
if self.stop.load(Ordering::Relaxed) {
return Ok(Outcome::Paused);
}
if let Some(solid) = self.job.format.solid() {
let walk = solid.walk(self.job.resumed(next))?;
let Some(links) = self.stream_solid(next, walk, reclaims, &out)? else {
return Ok(Outcome::Paused);
};
self.commit(next, next + 1, links, &out)?;
next += 1;
continue;
}
if let Some(stream) = self.stream_now(next, reclaims, &out)? {
if !self.stream_unit(next, &stream, &out)? {
return Ok(Outcome::Paused);
}
self.commit(next, next + 1, Vec::new(), &out)?;
next += 1;
continue;
}
let end = self.next_batch(next, &out, reclaims)?;
let batch = &self.job.units[next..end];
let Some(links) = self.extract_batch(batch, &out)? else {
return Ok(Outcome::Paused);
};
self.commit(next, end, links, &out)?;
next = end;
}
self.finish(&out)?;
Ok(Outcome::Finished)
}
fn commit(&mut self, start: usize, end: usize, links: Vec<Link>, out: &Path) -> Result<()> {
let batch = &self.job.units[start..end];
platform::sync_batch(out).context("flushing extracted files to disk")?;
if platform::SYNC_EACH_FILE {
let dirs: HashSet<PathBuf> = batch
.iter()
.flat_map(|u| &u.items)
.filter(|it| matches!(it.kind, Kind::File | Kind::Dir))
.filter_map(|it| out.join(&it.path).parent().map(Path::to_path_buf))
.collect();
for d in &dirs {
fsutil::sync_dir(d)?;
}
}
crash_point("after-extract");
let new_len = batch.last().expect("batches are never empty").start;
self.journal.committed_len = new_len;
self.journal.links.extend(links);
self.journal.resume = None;
self.job.resume = None;
self.journal.save(&self.paths)?;
crash_point("after-journal");
if self.journal.keep {
return Ok(());
}
self.job
.storage
.set_len(new_len)
.context("shrinking the archive")?;
(self.progress.on_commit)(new_len);
Ok(())
}
fn stream_now(&self, i: usize, reclaims: bool, out: &Path) -> Result<Option<Stream>> {
if self.job.resumed(i).is_some() {
return match self.job.stream_of(i) {
Some(s) => Ok(Some(s)),
None => bail!(
"the journal records '{}' as partially extracted, but it cannot be resumed",
self.job.units[i].items[0].path.display()
),
};
}
let Some(stream) = self.job.streams(i, reclaims) else {
return Ok(None);
};
if always_stream() {
return Ok(Some(stream));
}
let avail = fsutil::available_space(out)?.saturating_sub(self.keep_free);
Ok((on_disk(&self.job.units[i]) > avail).then_some(stream))
}
fn stream_unit(&mut self, i: usize, stream: &Stream, out: &Path) -> Result<bool> {
let unit_start = self.job.units[i].start;
let item = self.job.units[i].items[0].clone();
let dest = out.join(&item.path);
let part = part_path(&dest);
let at = self.job.resumed(i).cloned().unwrap_or(Resume {
unit: unit_start,
written: 0,
bit: stream.data_start * 8,
crc: 0,
stream: None,
});
let started_here = at.written == 0;
let what = || format!("extracting '{}'", item.path.display());
if let Some(parent) = dest.parent() {
fs::create_dir_all(parent).with_context(what)?;
}
let file = open_part(&dest, &part, at.written).with_context(what)?;
let result = self
.stream_file(&item, stream, at, file, &dest, &part, out)
.with_context(what);
if result.is_err() && started_here && self.journal.resume.is_none() {
let _ = fs::remove_file(&part);
}
result
}
#[allow(clippy::too_many_arguments)]
fn stream_file(
&mut self,
item: &Item,
stream: &Stream,
at: Resume,
mut file: File,
dest: &Path,
part: &Path,
out: &Path,
) -> Result<bool> {
let window = match stream.codec {
Codec::Stored => Vec::new(),
Codec::Deflate => read_window(&mut file, at.written)?,
};
let mut decoder = Decoder::new(self.job.storage.reader(None), stream, at.bit, &window)?;
let mut crc = crc32fast::Hasher::new_with_initial(at.crc);
let mut written = at.written;
let mut since_point = 0;
let step = stream_step();
let mut punching = self.job.punch_support();
let what = format!("'{}'", item.path.display());
self.ensure_room(out, &what, item.size - written, punching)?;
let mut buf = vec![0; BUF_SIZE];
loop {
if self.stop.load(Ordering::Relaxed) {
return Ok(false);
}
let n = decoder.read(&mut buf)?;
if n == 0 {
break;
}
written += n as u64;
if written > item.size {
bail!("it is larger than the archive records");
}
file.write_all(&buf[..n])?;
crc.update(&buf[..n]);
(self.progress.on_bytes)(n as u64);
since_point += n as u64;
if since_point < step || written == item.size {
continue;
}
match decoder.restart_point() {
Some(bit) => {
let point = Resume {
unit: at.unit,
written,
bit,
crc: crc.clone().finalize(),
stream: None,
};
punching = self.save_point(&file, part, out, point, stream, punching)?;
since_point = 0;
self.ensure_room(out, &what, item.size - written, punching)?;
}
None => {
let avail = fsutil::available_space(out)?.saturating_sub(self.keep_free);
if avail < 2 * BUF_SIZE as u64 {
bail!(
"not enough disk space to continue: the compressed data of this entry has \
no resume point in the last {}, so its archive space cannot be \
released yet. {} more is required (the remainder of this file). \
Progress is saved; free up space and resume the extraction.",
human(since_point),
human(item.size - written),
);
}
}
}
}
if written != item.size {
bail!("it is smaller than the archive records");
}
if let Some(expected) = stream.crc
&& crc.finalize() != expected
{
bail!("its data is damaged (CRC mismatch)");
}
if let Some(t) = item.mtime {
file.set_modified(t)?;
}
platform::apply_mode(&file, item.mode)?;
file.sync_all()?;
drop(file);
fs::rename(part, dest)?;
Ok(true)
}
fn save_point(
&mut self,
file: &File,
part: &Path,
out: &Path,
point: Resume,
stream: &Stream,
punching: Option<u64>,
) -> Result<Option<u64>> {
file.sync_all()
.context("flushing the extracted file to disk")?;
let mut dir = part.parent();
while let Some(d) = dir {
fsutil::sync_dir(d)?;
if d == out {
break;
}
dir = d.parent();
}
crash_point("stream-after-sync");
self.journal.resume = Some(point.clone());
self.journal.save(&self.paths)?;
self.job.resume = Some(point.clone());
crash_point("stream-after-journal");
if punching.is_some()
&& !self
.job
.storage
.punch(stream.data_start, point.bit / 8)
.context("freeing extracted archive bytes")?
{
return Ok(None);
}
crash_point("stream-after-punch");
Ok(punching)
}
fn stream_solid(
&mut self,
i: usize,
mut walk: Walker,
reclaims: bool,
out: &Path,
) -> Result<Option<Vec<Link>>> {
let unit = self.job.units[i].start;
let step = stream_step();
let mut punching = self.job.punch_support().filter(|_| reclaims);
let profile = match self.job.format.solid() {
Some(s) => s.profile.clone(),
None => bail!("the archive is not a compressed stream"),
};
let mut left = profile.files;
let mut punched = 0;
let mut since_point = 0u64;
let mut checked = 0u64;
let mut open: Option<OpenFile> = None;
let mut links = Vec::new();
let mut dirs = HashSet::new();
self.ensure_room(out, "the archive", left, punching)?;
loop {
if self.stop.load(Ordering::Relaxed) {
return Ok(None);
}
match walk.next(BUF_SIZE)? {
Event::Entry { item, link, done } => {
let dest = out.join(&item.path);
let what = || format!("extracting '{}'", item.path.display());
match &item.kind {
Kind::Dir => {
fs::create_dir_all(&dest).with_context(what)?;
dirs.insert(dest);
}
Kind::Symlink => {
let target = String::from_utf8(link.unwrap_or_default())
.map_err(|_| anyhow!("the link target is not valid UTF-8"))
.with_context(what)?;
links.push(Link {
path: item.path.clone(),
target: PathBuf::from(target),
hard: false,
});
}
Kind::HardLink(target) => links.push(Link {
path: item.path.clone(),
target: target.clone(),
hard: true,
}),
Kind::File => {
if let Some(parent) = dest.parent() {
fs::create_dir_all(parent).with_context(what)?;
}
let part = part_path(&dest);
let file = open_part(&dest, &part, done).with_context(what)?;
open = Some(OpenFile {
item,
dest,
part,
file,
written: done,
});
}
}
}
Event::Data(bytes) => {
let f = open
.as_mut()
.ok_or_else(|| anyhow!("the tar stream holds data outside a file"))?;
f.file
.write_all(bytes)
.with_context(|| format!("extracting '{}'", f.item.path.display()))?;
let n = bytes.len() as u64;
f.written += n;
since_point += n;
left = left.saturating_sub(n);
(self.progress.on_bytes)(n);
if punching.is_some()
&& since_point >= step
&& since_point - checked >= BUF_SIZE as u64
{
checked = since_point;
let avail = fsutil::available_space(out)?.saturating_sub(self.keep_free);
if avail < 2 * BUF_SIZE as u64 {
bail!(
"not enough disk space to continue: the compressed data has no \
resume point in the last {}, so its space cannot be released \
yet. {} more is required (the remainder of the archive). \
Progress is saved; free up space and resume the extraction.",
human(since_point),
human(left),
);
}
}
}
Event::FileEnd => {
let f = open
.take()
.ok_or_else(|| anyhow!("the tar stream ends a file it never began"))?;
let pad = f.item.size.next_multiple_of(4096) - f.item.size;
let path = f.item.path.clone();
let parent = f.dest.parent().map(Path::to_path_buf);
f.finish()
.with_context(|| format!("extracting '{}'", path.display()))?;
dirs.extend(parent);
since_point += pad;
left = left.saturating_sub(pad);
}
Event::Point => {
if since_point < step {
continue;
}
let mut point = walk.point()?;
point.unit = unit;
punching = self.solid_point(
point,
open.as_ref(),
&mut links,
&mut dirs,
out,
punching,
&mut punched,
)?;
since_point = 0;
checked = 0;
self.ensure_room(out, "the archive", left, punching)?;
}
Event::End => break,
}
}
if open.is_some() {
bail!("the tar stream ended inside a file");
}
Ok(Some(links))
}
#[allow(clippy::too_many_arguments)]
fn solid_point(
&mut self,
point: Resume,
open: Option<&OpenFile>,
links: &mut Vec<Link>,
dirs: &mut HashSet<PathBuf>,
out: &Path,
punching: Option<u64>,
punched: &mut u64,
) -> Result<Option<u64>> {
if let Some(f) = open {
f.file
.sync_all()
.context("flushing the extracted file to disk")?;
dirs.extend(f.part.parent().map(Path::to_path_buf));
}
platform::sync_batch(out).context("flushing extracted files to disk")?;
if platform::SYNC_EACH_FILE {
let mut done = HashSet::new();
for d in dirs.drain() {
let mut dir = Some(d.as_path());
while let Some(d) = dir {
if !done.insert(d.to_path_buf()) {
break;
}
fsutil::sync_dir(d)?;
if d == out {
break;
}
dir = d.parent();
}
}
}
dirs.clear();
crash_point("stream-after-sync");
let byte = point.bit / 8;
self.journal.links.append(links);
self.journal.resume = Some(point.clone());
self.journal.save(&self.paths)?;
self.job.resume = Some(point);
crash_point("stream-after-journal");
if punching.is_some() {
if !self
.job
.storage
.punch(*punched, byte)
.context("freeing extracted archive bytes")?
{
return Ok(None);
}
*punched = byte;
(self.progress.on_commit)(self.job.top - byte);
}
crash_point("stream-after-punch");
Ok(punching)
}
fn ensure_room(&self, out: &Path, what: &str, left: u64, punching: Option<u64>) -> Result<()> {
let avail = fsutil::available_space(out)?.saturating_sub(self.keep_free);
let left = left.next_multiple_of(4096);
let need = match punching {
Some(granule) => (stream_step() + BUF_SIZE as u64 + 2 * granule).min(left),
None => left,
};
if need > avail {
bail!(
"not enough disk space to continue: the next part of {} requires {} and {} is \
available ({} reserved).\n{} more is required. Progress is saved; free up space \
and resume the extraction.",
what,
human(need),
human(avail),
human(self.keep_free),
human(need - avail),
);
}
Ok(())
}
fn next_batch(&self, start: usize, out: &Path, reclaims: bool) -> Result<usize> {
let avail = fsutil::available_space(out)?.saturating_sub(self.keep_free);
let units = &self.job.units;
let need = on_disk;
let first = need(&units[start]);
if first > avail {
let what = match units[start].items.as_slice() {
[one] => format!("'{}'", one.path.display()),
many => format!("the next block of {} files", many.len()),
};
bail!(
"not enough disk space to continue: {what} requires {} and {} is available \
({} reserved).\n{} more is required. Progress is saved; free up space and \
resume the extraction.",
human(first),
human(avail),
human(self.keep_free),
human(first - avail),
);
}
let budget = avail.min(BATCH_MAX_BYTES.max(first));
let max_units = test_knob("HATCHZIP_BATCH_ENTRIES").map_or(BATCH_MAX_UNITS, |n| n as usize);
let mut used = 0u64;
let mut end = start;
while end < units.len() && end - start < max_units {
let size = need(&units[end]);
if end > start && used.saturating_add(size) > budget {
break;
}
if end > start && always_stream() && self.job.streams(end, reclaims).is_some() {
break;
}
used += size;
end += 1;
}
Ok(end)
}
fn extract_batch(&self, batch: &[Unit], out: &Path) -> Result<Option<Vec<Link>>> {
let on_bytes = &*self.progress.on_bytes;
if self.job.format.serial() {
let mut sink = DiskSink {
out,
stop: self.stop,
on_bytes,
buf: vec![0; BUF_SIZE],
links: Vec::new(),
};
let mut worker = self
.job
.format
.worker(1)
.map_err(|e| anyhow!("opening the archive: {e:#}"))?;
for unit in batch.iter().rev() {
match worker.extract(unit, &mut sink) {
Ok(()) => {}
Err(e) if is_interrupt(&e) => return Ok(None),
Err(e) => return Err(e),
}
}
return Ok(Some(sink.links));
}
let threads = threads_per_unit(batch.len());
let results: Vec<Result<Vec<Link>>> = batch
.par_iter()
.map_init(
|| {
let sink = DiskSink {
out,
stop: self.stop,
on_bytes,
buf: vec![0; BUF_SIZE],
links: Vec::new(),
};
(self.job.format.worker(threads), sink)
},
|(worker, sink), unit| {
let w = worker
.as_mut()
.map_err(|e| anyhow!("opening the archive: {e:#}"))?;
w.extract(unit, sink)?;
Ok(std::mem::take(&mut sink.links))
},
)
.collect();
let mut links = Vec::new();
let mut interrupted = false;
for r in results {
match r {
Ok(l) => links.extend(l),
Err(e) if is_interrupt(&e) => interrupted = true,
Err(e) => return Err(e),
}
}
Ok((!interrupted).then_some(links))
}
fn finish(self, out: &Path) -> Result<()> {
make_final_links(&self.journal.links, out)?;
if let Some(r) = &self.journal.replace {
replace::settle(out, r)?;
replace::remove(out)?;
}
if self.journal.keep {
return self.paths.remove();
}
let Self { job, paths, .. } = self;
let Job {
storage, format, ..
} = job;
drop(format);
let storage = Arc::try_unwrap(storage)
.map_err(|_| anyhow!("archive still in use at the end of extraction"))?;
storage.remove()?;
paths.remove()
}
}
pub fn finish_orphaned(journal: &Journal, paths: &Paths) -> Result<()> {
make_final_links(&journal.links, &journal.output)?;
if let Some(r) = &journal.replace {
replace::settle(&journal.output, r)?;
replace::remove(&journal.output)?;
}
paths.remove()
}
fn make_final_links(links: &[Link], out: &Path) -> Result<()> {
make_links(links, out).map_err(|e| {
anyhow!(
"every file is extracted, but {e:#}\nThe links are kept. Resolve the problem shown for \
each one, for example by moving or renaming a folder that is in the way, then \
resume the extraction to create them"
)
})
}
pub(crate) fn make_links(links: &[Link], out: &Path) -> Result<()> {
let failed: Vec<(&Link, anyhow::Error)> = links
.iter()
.filter_map(|link| make_link(link, out).err().map(|e| (link, e)))
.collect();
if failed.is_empty() {
return Ok(());
}
let mut msg = match failed.len() {
1 => "1 link could not be created:".to_string(),
n => format!("{n} links could not be created:"),
};
for (link, e) in failed.iter().take(10) {
let kind = if link.hard {
"hard link"
} else {
"symbolic link"
};
msg.push_str(&format!(
"\n '{}' ({kind} to '{}'): {e:#}",
link.path.display(),
link.target.display()
));
}
if failed.len() > 10 {
msg.push_str(&format!("\n and {} more", failed.len() - 10));
}
Err(anyhow!(msg))
}
fn make_link(link: &Link, out: &Path) -> Result<()> {
if let Some(via) = link_on_the_way(out, &link.path) {
bail!(
"'{}' is a link, and this one would be created through it",
via.display()
);
}
let path = out.join(&link.path);
if fs::symlink_metadata(&path).is_ok_and(|m| m.is_dir()) {
bail!("a folder already exists at this location");
}
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
if link.hard {
if let Some(via) = link_on_the_way(out, &link.target) {
bail!(
"'{}' is a link, and the target would be reached through it",
via.display()
);
}
let target = out.join(&link.target);
let _ = fs::remove_file(&path);
fs::hard_link(&target, &path).or_else(|_| fs::copy(&target, &path).map(|_| ()))?;
} else {
platform::create_symlink(&path, &link.target)?;
}
Ok(())
}
pub(crate) fn link_on_the_way(out: &Path, rel: &Path) -> Option<PathBuf> {
let mut ancestors: Vec<&Path> = rel
.ancestors()
.skip(1)
.filter(|p| !p.as_os_str().is_empty())
.collect();
ancestors.reverse();
ancestors
.into_iter()
.find(|p| fs::symlink_metadata(out.join(p)).is_ok_and(|m| m.is_symlink()))
.map(Path::to_path_buf)
}
struct DiskSink<'a> {
out: &'a Path,
stop: &'a AtomicBool,
on_bytes: &'a (dyn Fn(u64) + Sync),
buf: Vec<u8>,
links: Vec<Link>,
}
impl Sink for DiskSink<'_> {
fn item(&mut self, item: &Item, data: &mut dyn Read) -> Result<()> {
self.write(item, data)
.with_context(|| format!("extracting '{}'", item.path.display()))
}
}
impl DiskSink<'_> {
fn write(&mut self, item: &Item, data: &mut dyn Read) -> Result<()> {
let dest = self.out.join(&item.path);
match &item.kind {
Kind::Dir => {
fs::create_dir_all(&dest)?;
return Ok(());
}
Kind::Symlink => {
let mut target = Vec::new();
data.take(MAX_SYMLINK).read_to_end(&mut target)?;
let target = PathBuf::from(String::from_utf8(target)?);
self.links.push(Link {
path: item.path.clone(),
target,
hard: false,
});
return Ok(());
}
Kind::HardLink(target) => {
self.links.push(Link {
path: item.path.clone(),
target: target.clone(),
hard: true,
});
return Ok(());
}
Kind::File => {}
}
if let Some(parent) = dest.parent() {
fs::create_dir_all(parent)?;
}
let part = part_path(&dest);
let dst = create_part(&part)?;
let result = self.copy(item, data, dst);
if result.is_err() {
let _ = fs::remove_file(&part);
return result;
}
fs::rename(&part, &dest)?;
Ok(())
}
fn copy(&mut self, item: &Item, data: &mut dyn Read, mut dst: File) -> Result<()> {
let mut written = 0u64;
loop {
if self.stop.load(Ordering::Relaxed) {
return Err(io::Error::from(io::ErrorKind::Interrupted).into());
}
let n = data.read(&mut self.buf)?;
if n == 0 {
break;
}
written += n as u64;
if written > item.size {
bail!("it is larger than the archive records");
}
dst.write_all(&self.buf[..n])?;
(self.on_bytes)(n as u64);
}
if written != item.size {
bail!("it is smaller than the archive records");
}
if let Some(t) = item.mtime {
dst.set_modified(t)?;
}
platform::apply_mode(&dst, item.mode)?;
if platform::SYNC_EACH_FILE {
dst.sync_all()?;
}
Ok(())
}
}
struct ProbeSink {
left: u64,
buf: Vec<u8>,
}
#[derive(Debug)]
struct ProbeDone;
impl std::fmt::Display for ProbeDone {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("read enough to prove the password")
}
}
impl std::error::Error for ProbeDone {}
impl Sink for ProbeSink {
fn item(&mut self, _item: &Item, data: &mut dyn Read) -> Result<()> {
loop {
let n = data.read(&mut self.buf)?;
if n == 0 {
return Ok(());
}
self.left = self.left.saturating_sub(n as u64);
if self.left == 0 {
return Err(ProbeDone.into());
}
}
}
}
struct VerifySink<'a> {
on_bytes: &'a (dyn Fn(u64) + Sync),
buf: Vec<u8>,
}
impl Sink for VerifySink<'_> {
fn item(&mut self, item: &Item, data: &mut dyn Read) -> Result<()> {
let mut read = 0;
loop {
let n = data
.read(&mut self.buf)
.with_context(|| format!("'{}'", item.path.display()))?;
if n == 0 {
break;
}
read += n as u64;
(self.on_bytes)(n as u64);
}
if item.kind == Kind::File && read != item.size {
bail!(
"'{}' is shorter than the archive records",
item.path.display()
);
}
Ok(())
}
}
fn is_interrupt(e: &anyhow::Error) -> bool {
e.chain()
.filter_map(|c| c.downcast_ref::<io::Error>())
.any(|io| io.kind() == io::ErrorKind::Interrupted)
}
fn test_knob(name: &str) -> Option<u32> {
if !cfg!(debug_assertions) {
return None;
}
std::env::var(name).ok()?.parse().ok()
}
pub(crate) fn crash_point(name: &str) {
if !cfg!(debug_assertions) || std::env::var("HATCHZIP_CRASH_AT").map_or(true, |v| v != name) {
return;
}
static PASSED: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
if PASSED.fetch_add(1, Ordering::Relaxed) >= test_knob("HATCHZIP_CRASH_AFTER").unwrap_or(0) {
std::process::abort();
}
}
pub fn human(bytes: u64) -> String {
const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
let mut v = bytes as f64;
let mut unit = 0;
while v >= 1000.0 && unit < UNITS.len() - 1 {
v /= 1000.0;
unit += 1;
}
if unit == 0 {
format!("{bytes} B")
} else {
format!("{v:.1} {}", UNITS[unit])
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::archive::{Opened, Worker};
struct NoFormat;
impl Format for NoFormat {
fn worker(&self, _threads: u32) -> Result<Box<dyn Worker + '_>> {
bail!("nothing is decoded in these tests")
}
}
fn unit(start: u64, name: &str, size: u64) -> Unit {
Unit {
start,
key: 0,
items: vec![Item {
path: name.into(),
size,
kind: Kind::File,
mode: None,
mtime: None,
}],
encrypted: false,
}
}
fn storage(test: &str) -> (PathBuf, Arc<Storage>) {
let dir = std::env::temp_dir().join(format!("hatchzip-{test}-{}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
let path = dir.join("archive.bin");
fs::write(&path, [0u8; 64]).unwrap();
let storage = Arc::new(Storage::open(&path, false).unwrap());
(dir, storage)
}
fn opened(units: Vec<Unit>, tail_start: u64) -> Opened {
Opened {
name: "test",
format: Box::new(NoFormat),
units,
tail_start,
}
}
#[test]
fn units_sharing_a_start_are_refused() {
let (dir, storage) = storage("shared-start");
let shared = vec![unit(10, "a", 1), unit(30, "b", 1), unit(10, "c", 1)];
let err = Job::build(storage.clone(), opened(shared, 40), 64)
.err()
.unwrap();
assert!(err.to_string().contains("same offset"), "{err:#}");
let apart = vec![unit(10, "a", 1), unit(30, "b", 1)];
assert!(Job::build(storage, opened(apart, 40), 64).is_ok());
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn data_past_the_end_of_the_file_is_refused() {
let (dir, storage) = storage("past-the-end");
for (starts, tail_start) in [(vec![10, 64], 40), (vec![10, 70], 40), (vec![10], 65)] {
let units = starts
.iter()
.map(|&s| unit(s, &format!("f{s}"), 1))
.collect();
let err = Job::build(storage.clone(), opened(units, tail_start), 64)
.err()
.unwrap();
assert!(err.is::<archive::Damaged>(), "{starts:?}: {err:#}");
}
let job = Job::build(
storage,
opened(vec![unit(10, "a", 1), unit(50, "b", 1)], 60),
40,
);
assert_eq!(job.unwrap().units.len(), 1);
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn sizes_too_large_to_add_up_are_refused() {
let (dir, storage) = storage("huge-sizes");
let half = 1u64 << 63;
for sizes in [vec![half, half], vec![u64::MAX - 10], vec![(1 << 62) + 1]] {
let units = sizes
.iter()
.enumerate()
.map(|(i, &s)| unit(10 + i as u64, &format!("f{i}"), s))
.collect();
let err = Job::build(storage.clone(), opened(units, 40), 64)
.err()
.unwrap();
assert!(err.is::<archive::Damaged>(), "{sizes:?}: {err:#}");
}
assert_eq!(on_disk(&unit(0, "f", u64::MAX - 10)), u64::MAX);
let mut two = unit(0, "f", half);
two.items.extend(unit(0, "g", half).items);
assert_eq!(two.size(), u64::MAX);
assert_eq!(on_disk(&two), u64::MAX);
fs::remove_dir_all(&dir).unwrap();
}
}