hatchzip-core 0.1.1

The engine behind HatchZip: extract ZIP, 7z and tar archives in place, freeing the archive as it goes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! The archive's bytes, whether one file or a split set (`.001`, `.002`, ...),
//! and the only code allowed to shrink them.

use std::fs::{self, File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use anyhow::{Context, Result, anyhow, bail};

use crate::platform;

#[derive(Clone, Debug)]
pub struct Part {
    pub path: PathBuf,
    /// Where this part begins in the joined archive.
    pub start: u64,
    /// The part's length before extraction started.
    pub len: u64,
}

/// Bytes from the end of the archive saved to a side file, served back at
/// their original offsets once the real ones are truncated away.
pub struct Tail {
    pub start: u64,
    pub bytes: Vec<u8>,
}

pub struct Storage {
    parts: Arc<[Part]>,
    /// The first part stays open and locked for the whole run, so two runs
    /// can't eat the same archive.
    first: File,
}

impl Storage {
    /// Opens `path`, plus its sibling volumes if it is part of a split set.
    pub fn open(path: &Path, writable: bool) -> Result<Self> {
        let paths = volume_set(path)?;
        let mut parts = Vec::with_capacity(paths.len());
        let mut start = 0;
        for p in paths {
            let len = fs::metadata(&p)
                .with_context(|| format!("reading {}", p.display()))?
                .len();
            parts.push(Part {
                path: p,
                start,
                len,
            });
            start += len;
        }
        Self::with_parts(parts, writable)
    }

    /// Reopens a set whose original layout was recorded in the journal.
    pub fn from_parts(recorded: &[(PathBuf, u64)], writable: bool) -> Result<Self> {
        let mut parts = Vec::with_capacity(recorded.len());
        let mut start = 0;
        for (path, len) in recorded {
            parts.push(Part {
                path: path.clone(),
                start,
                len: *len,
            });
            start += len;
        }
        Self::with_parts(parts, writable)
    }

    fn with_parts(parts: Vec<Part>, writable: bool) -> Result<Self> {
        let path = &parts[0].path;
        let first = if writable {
            platform::open_exclusive(path).map_err(|e| match e.kind() {
                io::ErrorKind::WouldBlock => {
                    anyhow!(
                        "{} is in use by another program or HatchZip process",
                        path.display()
                    )
                }
                _ => anyhow!(e).context(format!(
                    "opening {} for reading and writing",
                    path.display()
                )),
            })?
        } else {
            File::open(path).with_context(|| format!("opening {}", path.display()))?
        };
        Ok(Self {
            parts: parts.into(),
            first,
        })
    }

    pub fn primary(&self) -> &Path {
        &self.parts[0].path
    }

    pub fn parts(&self) -> &[Part] {
        &self.parts
    }

    pub fn record(&self) -> Vec<(PathBuf, u64)> {
        self.parts.iter().map(|p| (p.path.clone(), p.len)).collect()
    }

    /// The archive's original length.
    pub fn original_len(&self) -> u64 {
        self.parts.last().map_or(0, |p| p.start + p.len)
    }

    /// How much of the archive is still on disk, checking that what is left
    /// is an untouched prefix of the original.
    pub fn current_len(&self) -> Result<u64> {
        let mut end = 0;
        for (i, p) in self.parts.iter().enumerate() {
            let actual = match fs::metadata(&p.path) {
                Ok(m) => m.len(),
                Err(e) if e.kind() == io::ErrorKind::NotFound && i > 0 => 0,
                Err(e) => return Err(e).with_context(|| format!("reading {}", p.path.display())),
            };
            if actual > p.len {
                bail!(
                    "{} has grown since extraction started and will not be modified",
                    p.path.display()
                );
            }
            if actual == 0 && i > 0 {
                break;
            }
            if end != p.start {
                bail!(
                    "{} is missing a piece before {}",
                    self.primary().display(),
                    p.path.display()
                );
            }
            end = p.start + actual;
            if actual < p.len {
                break;
            }
        }
        Ok(end)
    }

    /// Shrinks the archive to its first `len` bytes: whole volumes past that
    /// point are deleted, the one it falls in is truncated.
    pub fn set_len(&self, len: u64) -> Result<()> {
        for (i, p) in self.parts.iter().enumerate().rev() {
            if p.start + p.len <= len {
                break;
            }
            if i == 0 {
                self.first.set_len(len)?;
            } else if p.start >= len {
                match fs::remove_file(&p.path) {
                    Err(e) if e.kind() != io::ErrorKind::NotFound => {
                        return Err(e).with_context(|| format!("deleting {}", p.path.display()));
                    }
                    _ => {}
                }
            } else {
                let f = OpenOptions::new().write(true).open(&p.path)?;
                if f.metadata()?.len() > len - p.start {
                    f.set_len(len - p.start)?;
                }
            }
        }
        Ok(())
    }

    /// Gives back the disk space of the bytes in `[from, to)` while every
    /// offset stays where it was; they read as zeros afterwards. Returns
    /// false if the filesystem can't. Filesystems free whole blocks only, so
    /// up to a block at each end may stay allocated until truncation.
    pub fn punch(&self, from: u64, to: u64) -> Result<bool> {
        for (i, p) in self.parts.iter().enumerate() {
            let (start, end) = (from.max(p.start), to.min(p.start + p.len));
            if start >= end {
                continue;
            }
            let (offset, len) = (start - p.start, end - start);
            let punched = if i == 0 {
                platform::punch_hole(&self.first, offset, len)
            } else {
                OpenOptions::new()
                    .write(true)
                    .open(&p.path)
                    .and_then(|f| platform::punch_hole(&f, offset, len))
            };
            if !punched.with_context(|| format!("freeing space in {}", p.path.display()))? {
                return Ok(false);
            }
        }
        Ok(true)
    }

    /// Like [`set_len`](Self::set_len), but also cuts volumes that grew past
    /// their original length. Only for undoing an interrupted restore, which
    /// is the one thing that appends to an archive: the volume it appended to
    /// may have been whole, and `set_len` never touches whole volumes.
    pub fn cut_to(&self, len: u64) -> Result<()> {
        for (i, p) in self.parts.iter().enumerate().rev() {
            let keep = len.saturating_sub(p.start).min(p.len);
            if i > 0 && keep == 0 {
                match fs::remove_file(&p.path) {
                    Err(e) if e.kind() != io::ErrorKind::NotFound => {
                        return Err(e).with_context(|| format!("deleting {}", p.path.display()));
                    }
                    _ => {}
                }
                continue;
            }
            let shrink = |f: &File| -> io::Result<()> {
                if f.metadata()?.len() > keep {
                    f.set_len(keep)?;
                    f.sync_all()?;
                }
                Ok(())
            };
            let done = if i == 0 {
                shrink(&self.first)
            } else {
                match OpenOptions::new().write(true).open(&p.path) {
                    Ok(f) => shrink(&f),
                    Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
                    Err(e) => Err(e),
                }
            };
            done.with_context(|| format!("shortening {}", p.path.display()))?;
        }
        Ok(())
    }

    /// Writes `bytes` at `offset` of the joined archive and flushes them to
    /// disk. Bytes inside the archive go to the volume that holds them; bytes
    /// past its current end go to the last volume still there, which grows.
    pub fn write_at(&self, offset: u64, bytes: &[u8]) -> Result<()> {
        let end = self.current_len()?;
        if offset > end {
            bail!(
                "{} is shorter than expected; refusing to write past its end",
                self.primary().display()
            );
        }
        let last = self.parts.partition_point(|p| p.start < end).max(1) - 1;
        let mut at = offset;
        let mut rest = bytes;
        while !rest.is_empty() {
            let i = if at >= end {
                last
            } else {
                self.parts.partition_point(|p| p.start <= at) - 1
            };
            let p = &self.parts[i];
            let room = if i == last {
                rest.len()
            } else {
                ((p.start + p.len - at) as usize).min(rest.len())
            };
            let (now, later) = rest.split_at(room);
            self.write_part(i, at - p.start, now)
                .with_context(|| format!("writing to {}", p.path.display()))?;
            at += room as u64;
            rest = later;
        }
        Ok(())
    }

    fn write_part(&self, i: usize, at: u64, bytes: &[u8]) -> io::Result<()> {
        let write = |mut f: &File| -> io::Result<()> {
            f.seek(SeekFrom::Start(at))?;
            io::Write::write_all(&mut f, bytes)?;
            f.sync_all()
        };
        if i == 0 {
            // On Windows the locked handle is the only one allowed to write.
            write(&self.first)
        } else {
            write(&OpenOptions::new().write(true).open(&self.parts[i].path)?)
        }
    }

    /// Deletes whatever is left of every part.
    pub fn remove(self) -> Result<()> {
        let Self { parts, first } = self;
        drop(first);
        for p in parts.iter() {
            match fs::remove_file(&p.path) {
                Err(e) if e.kind() != io::ErrorKind::NotFound => {
                    return Err(e).with_context(|| format!("deleting {}", p.path.display()));
                }
                _ => {}
            }
        }
        Ok(())
    }

    pub fn reader(&self, tail: Option<Arc<Tail>>) -> Reader {
        Reader {
            parts: self.parts.clone(),
            open: None,
            tail,
            len: self.original_len(),
            pos: 0,
        }
    }
}

/// The path of the first volume, if `path` names any volume of a split set.
pub fn first_volume(path: &Path) -> PathBuf {
    match split_suffix(path) {
        Some((base, width, _)) => numbered(&base, width, 1),
        None => path.to_path_buf(),
    }
}

fn volume_set(path: &Path) -> Result<Vec<PathBuf>> {
    let Some((base, width, n)) = split_suffix(path) else {
        return Ok(vec![path.to_path_buf()]);
    };
    if n != 1 {
        bail!(
            "{} is not the first part. Open {} instead",
            path.display(),
            numbered(&base, width, 1).display()
        );
    }
    let mut parts = vec![path.to_path_buf()];
    loop {
        let next = numbered(&base, width, parts.len() as u64 + 1);
        if !next.exists() {
            return Ok(parts);
        }
        parts.push(next);
    }
}

/// `game.7z.001` is `("game.7z", 3, 1)`. Only names with a real extension
/// before the number count, so a file that just happens to end in digits
/// isn't mistaken for a volume.
fn split_suffix(path: &Path) -> Option<(PathBuf, usize, u64)> {
    let name = path.file_name()?.to_str()?;
    let (stem, digits) = name.rsplit_once('.')?;
    if digits.len() < 3 || !digits.bytes().all(|b| b.is_ascii_digit()) || !stem.contains('.') {
        return None;
    }
    Some((
        path.with_file_name(stem),
        digits.len(),
        digits.parse().ok()?,
    ))
}

fn numbered(base: &Path, width: usize, n: u64) -> PathBuf {
    let mut s = base.as_os_str().to_owned();
    s.push(format!(".{n:0width$}"));
    PathBuf::from(s)
}

/// Reads the joined archive. Parts or regions that were already truncated
/// away read as zeros: format parsers scan backwards over them looking for
/// end records, and zeros never match a signature.
pub struct Reader {
    parts: Arc<[Part]>,
    open: Option<(usize, File)>,
    tail: Option<Arc<Tail>>,
    len: u64,
    pos: u64,
}

impl Read for Reader {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if self.pos >= self.len || buf.is_empty() {
            return Ok(0);
        }
        let live_end = match &self.tail {
            Some(t) if self.pos >= t.start => {
                let at = (self.pos - t.start) as usize;
                let src = t.bytes.get(at..).unwrap_or(&[]);
                let n = src.len().min(buf.len());
                buf[..n].copy_from_slice(&src[..n]);
                self.pos += n as u64;
                return Ok(n);
            }
            Some(t) => t.start,
            None => self.len,
        };
        let i = self.parts.partition_point(|p| p.start <= self.pos) - 1;
        let part = &self.parts[i];
        let room = (part.start + part.len).min(live_end) - self.pos;
        let room = room.min(buf.len() as u64) as usize;
        if self.open.as_ref().is_none_or(|(j, _)| *j != i) {
            self.open = File::open(&part.path).ok().map(|f| (i, f));
        }
        let n = match &mut self.open {
            Some((_, f)) => {
                f.seek(SeekFrom::Start(self.pos - part.start))?;
                f.read(&mut buf[..room])?
            }
            None => 0,
        };
        let n = if n == 0 {
            buf[..room].fill(0);
            room
        } else {
            n
        };
        self.pos += n as u64;
        Ok(n)
    }
}

impl Seek for Reader {
    fn seek(&mut self, to: SeekFrom) -> io::Result<u64> {
        let pos = match to {
            SeekFrom::Start(p) => Some(p),
            SeekFrom::End(d) => self.len.checked_add_signed(d),
            SeekFrom::Current(d) => self.pos.checked_add_signed(d),
        };
        self.pos = pos.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "bad seek"))?;
        Ok(self.pos)
    }
}