zipatch-rs 1.6.0

Parser for FFXIV ZiPatch patch files
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! Filesystem abstraction for the apply layer.
//!
//! The apply layer never touches `std::fs` directly. All I/O is routed
//! through a [`Vfs`] implementation owned by [`ApplySession`](super::ApplySession).
//! Two concrete impls ship with the crate:
//!
//! - [`StdFs`] — wraps `std::fs`. The default.
//! - [`InMemoryFs`] — pure in-memory backing. Useful for tests, dry-run
//!   previews, sandbox/embedded environments, and Tauri-style "preview
//!   what this patch would do" flows.
//!
//! The trait surface is intentionally minimal: every method corresponds
//! directly to a call site in the apply or index-apply layer. New
//! operations should only be added when an apply path needs one.
//!
//! # Handle erasure
//!
//! [`Vfs::open_write`] and [`Vfs::open_read`] return boxed trait objects.
//! The apply hot path is dominated by DEFLATE decompression and syscalls;
//! the virtual-call overhead of a `Box<dyn Write + Seek + Send>` is in
//! the noise. Keeping the handles type-erased keeps [`ApplySession`](super::ApplySession)
//! non-generic and the public API clean — `Chunk::apply(&mut ApplySession)`
//! stays as-is instead of becoming `Chunk::apply<F: Vfs>(&mut ApplySession<F>)`.

use std::io::{self, Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

/// Snapshot of a path's metadata. Only the fields the apply layer reads.
#[derive(Debug, Clone, Copy)]
pub struct VfsMetadata {
    /// File length in bytes.
    pub len: u64,
    /// `true` if the entry is a regular file.
    pub is_file: bool,
}

/// Writable, seekable handle returned by [`Vfs::open_write`].
///
/// Inherits `Write + Seek` plus the two extra operations the apply layer
/// needs against a handle: truncate-to-len and durable-sync.
pub trait VfsWrite: Write + Seek + Send {
    /// Truncate the file the handle points at to `len` bytes.
    fn set_len(&mut self, len: u64) -> io::Result<()>;

    /// Force any pending writes (including buffered data inside the
    /// handle, if any) through to durable storage. Equivalent to
    /// `std::fs::File::sync_all` for the std backend.
    fn sync_all(&mut self) -> io::Result<()>;
}

/// Readable, seekable handle returned by [`Vfs::open_read`].
pub trait VfsRead: Read + Seek + Send {}

impl<T: Read + Seek + Send + ?Sized> VfsRead for T {}

/// Filesystem abstraction backing [`ApplySession`](super::ApplySession).
///
/// Implementations must be safe to share across threads (`Send + Sync`)
/// so a configured [`ApplyConfig`](super::ApplyConfig) can be moved to a
/// worker thread for the actual apply.
///
/// # Async usage
///
/// `Vfs` and its handle traits ([`VfsRead`], [`VfsWrite`]) are
/// intentionally synchronous — the apply hot path is dominated by
/// DEFLATE decompression and filesystem syscalls, and modelling the
/// trait as `async` would force every consumer to pull in a runtime.
/// See the crate-level "Async usage" section for the full rationale.
///
/// Async-backed filesystems (e.g. a tokio-based virtual fs or an
/// `object_store`-style remote backend) implement the sync trait on a
/// type that owns a [`tokio::runtime::Handle`-equivalent] and uses
/// `Handle::block_on` inside each method, or — for the common
/// "everything runs on a worker thread anyway" pattern — bridges
/// through an [`mpsc`](std::sync::mpsc) request/response pair against
/// a separate async task. Because the apply driver itself is expected
/// to run inside `tokio::task::spawn_blocking`, neither bridge pattern
/// stalls the reactor.
///
/// [`tokio::runtime::Handle`-equivalent]: https://docs.rs/tokio/latest/tokio/runtime/struct.Handle.html
pub trait Vfs: Send + Sync {
    /// Open `path` for writing, creating it if it does not exist.
    ///
    /// The handle is opened with `write=true, create=true, truncate=false`
    /// — the apply layer manages truncation explicitly via
    /// [`VfsWrite::set_len`] when a chunk demands it.
    fn open_write(&self, path: &Path) -> io::Result<Box<dyn VfsWrite>>;

    /// Open `path` for reading.
    fn open_read(&self, path: &Path) -> io::Result<Box<dyn VfsRead>>;

    /// Create `path` and every missing ancestor directory.
    fn create_dir_all(&self, path: &Path) -> io::Result<()>;

    /// Remove the file at `path`.
    fn remove_file(&self, path: &Path) -> io::Result<()>;

    /// Remove the (empty) directory at `path`.
    fn remove_dir(&self, path: &Path) -> io::Result<()>;

    /// Return entries directly inside `path`. Order is implementation-defined.
    fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>>;

    /// Return `true` if any entry exists at `path`.
    fn exists(&self, path: &Path) -> bool;

    /// Return metadata for `path`. Returns `NotFound` if the path does not exist.
    fn metadata(&self, path: &Path) -> io::Result<VfsMetadata>;
}

// ---------------------------------------------------------------------------
// StdFs
// ---------------------------------------------------------------------------

/// Default [`Vfs`] backed by `std::fs`. Stateless, cheap to clone.
#[derive(Debug, Default, Clone, Copy)]
pub struct StdFs;

impl StdFs {
    /// Construct a new instance.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }
}

impl VfsWrite for std::fs::File {
    fn set_len(&mut self, len: u64) -> io::Result<()> {
        std::fs::File::set_len(self, len)
    }
    fn sync_all(&mut self) -> io::Result<()> {
        std::fs::File::sync_all(self)
    }
}

impl Vfs for StdFs {
    fn open_write(&self, path: &Path) -> io::Result<Box<dyn VfsWrite>> {
        let file = std::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(false)
            .open(path)?;
        Ok(Box::new(file))
    }

    fn open_read(&self, path: &Path) -> io::Result<Box<dyn VfsRead>> {
        Ok(Box::new(std::fs::File::open(path)?))
    }

    fn create_dir_all(&self, path: &Path) -> io::Result<()> {
        std::fs::create_dir_all(path)
    }

    fn remove_file(&self, path: &Path) -> io::Result<()> {
        std::fs::remove_file(path)
    }

    fn remove_dir(&self, path: &Path) -> io::Result<()> {
        std::fs::remove_dir(path)
    }

    fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>> {
        let mut out = Vec::new();
        for entry in std::fs::read_dir(path)? {
            out.push(entry?.path());
        }
        Ok(out)
    }

    fn exists(&self, path: &Path) -> bool {
        path.exists()
    }

    fn metadata(&self, path: &Path) -> io::Result<VfsMetadata> {
        let m = std::fs::metadata(path)?;
        Ok(VfsMetadata {
            len: m.len(),
            is_file: m.is_file(),
        })
    }
}

// ---------------------------------------------------------------------------
// InMemoryFs
// ---------------------------------------------------------------------------

/// File entry inside an [`InMemoryFs`].
#[derive(Debug, Default, Clone)]
struct MemFile {
    bytes: Vec<u8>,
}

/// Shared storage shape for [`InMemoryFs`]. Lives behind an `Arc<Mutex<_>>`
/// so handles can write back into the same map their parent vfs handed out.
#[derive(Debug, Default)]
struct MemFsInner {
    files: std::collections::BTreeMap<PathBuf, MemFile>,
    dirs: std::collections::BTreeSet<PathBuf>,
}

/// In-memory [`Vfs`] backing. Useful for tests, dry-run previews, and
/// sandboxed environments where the apply layer must not touch the host
/// filesystem.
///
/// Cheap to clone — all clones share the same underlying storage, so a
/// caller can hand a clone to [`ApplyConfig::with_vfs`](super::ApplyConfig::with_vfs)
/// and then inspect the in-memory state after the apply returns via the
/// retained clone.
///
/// # Storage shape
///
/// - Files: `BTreeMap<PathBuf, Vec<u8>>`, keyed by the absolute path the
///   apply layer resolved them to.
/// - Directories: `BTreeSet<PathBuf>` of created directories.
/// - Both maps live behind a single `Arc<Mutex<_>>`. Contention is not a
///   concern — the apply layer is single-threaded and holds the lock only
///   for the duration of one syscall-shaped operation.
#[derive(Debug, Default, Clone)]
pub struct InMemoryFs {
    inner: Arc<Mutex<MemFsInner>>,
}

impl InMemoryFs {
    /// Construct an empty in-memory filesystem.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Take a snapshot of every file's contents.
    ///
    /// The returned map is keyed by the absolute path the apply layer
    /// wrote to. Use this after a successful apply to inspect the
    /// resulting layout in tests.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    #[must_use]
    pub fn snapshot_files(&self) -> std::collections::BTreeMap<PathBuf, Vec<u8>> {
        let inner = self.inner.lock().unwrap();
        inner
            .files
            .iter()
            .map(|(k, v)| (k.clone(), v.bytes.clone()))
            .collect()
    }

    /// Take a snapshot of every created directory path.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    #[must_use]
    pub fn snapshot_dirs(&self) -> std::collections::BTreeSet<PathBuf> {
        self.inner.lock().unwrap().dirs.clone()
    }

    /// Read the bytes of a single file. Returns `None` if absent.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    #[must_use]
    pub fn read_file(&self, path: &Path) -> Option<Vec<u8>> {
        self.inner
            .lock()
            .unwrap()
            .files
            .get(path)
            .map(|f| f.bytes.clone())
    }
}

/// Writable handle into an [`InMemoryFs`]. Operates on a per-handle scratch
/// buffer and commits the result back into shared storage on `flush` /
/// `sync_all` / `set_len` / drop.
struct MemFileHandle {
    inner: Arc<Mutex<MemFsInner>>,
    path: PathBuf,
    cursor: u64,
    // Scratch copy of the file's bytes; the canonical store is rewritten
    // from this on every write-through point. Writes happen against this
    // local buffer first so a partial write inside one syscall does not
    // require the global lock around every byte.
    bytes: Vec<u8>,
}

impl MemFileHandle {
    fn commit(&self) {
        let mut inner = self.inner.lock().unwrap();
        inner
            .files
            .entry(self.path.clone())
            .or_default()
            .bytes
            .clone_from(&self.bytes);
    }
}

impl Drop for MemFileHandle {
    fn drop(&mut self) {
        self.commit();
    }
}

impl Write for MemFileHandle {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let pos = usize::try_from(self.cursor)
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "cursor overflow"))?;
        let end = pos
            .checked_add(buf.len())
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "write past usize"))?;
        if end > self.bytes.len() {
            self.bytes.resize(end, 0);
        }
        self.bytes[pos..end].copy_from_slice(buf);
        self.cursor = end as u64;
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        self.commit();
        Ok(())
    }
}

impl Seek for MemFileHandle {
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        let new_pos: i128 = match pos {
            io::SeekFrom::Start(n) => i128::from(n),
            io::SeekFrom::Current(d) => i128::from(self.cursor) + i128::from(d),
            io::SeekFrom::End(d) => i128::from(self.bytes.len() as u64) + i128::from(d),
        };
        if new_pos < 0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "negative seek position",
            ));
        }
        self.cursor = new_pos as u64;
        Ok(self.cursor)
    }
}

impl VfsWrite for MemFileHandle {
    fn set_len(&mut self, len: u64) -> io::Result<()> {
        let new_len = usize::try_from(len)
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "len overflow"))?;
        self.bytes.resize(new_len, 0);
        if self.cursor > len {
            self.cursor = len;
        }
        self.commit();
        Ok(())
    }

    fn sync_all(&mut self) -> io::Result<()> {
        self.commit();
        Ok(())
    }
}

struct MemReadHandle {
    bytes: Vec<u8>,
    cursor: u64,
}

impl Read for MemReadHandle {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let pos = usize::try_from(self.cursor).unwrap_or(usize::MAX);
        if pos >= self.bytes.len() {
            return Ok(0);
        }
        let available = &self.bytes[pos..];
        let n = available.len().min(buf.len());
        buf[..n].copy_from_slice(&available[..n]);
        self.cursor += n as u64;
        Ok(n)
    }
}

impl Seek for MemReadHandle {
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        let new_pos: i128 = match pos {
            io::SeekFrom::Start(n) => i128::from(n),
            io::SeekFrom::Current(d) => i128::from(self.cursor) + i128::from(d),
            io::SeekFrom::End(d) => i128::from(self.bytes.len() as u64) + i128::from(d),
        };
        if new_pos < 0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "negative seek position",
            ));
        }
        self.cursor = new_pos as u64;
        Ok(self.cursor)
    }
}

impl Vfs for InMemoryFs {
    fn open_write(&self, path: &Path) -> io::Result<Box<dyn VfsWrite>> {
        let inner = self.inner.lock().unwrap();
        let bytes = inner
            .files
            .get(path)
            .map(|f| f.bytes.clone())
            .unwrap_or_default();
        drop(inner);
        Ok(Box::new(MemFileHandle {
            inner: Arc::clone(&self.inner),
            path: path.to_path_buf(),
            cursor: 0,
            bytes,
        }))
    }

    fn open_read(&self, path: &Path) -> io::Result<Box<dyn VfsRead>> {
        let inner = self.inner.lock().unwrap();
        let Some(f) = inner.files.get(path) else {
            return Err(io::Error::from(io::ErrorKind::NotFound));
        };
        let bytes = f.bytes.clone();
        Ok(Box::new(MemReadHandle { bytes, cursor: 0 }))
    }

    fn create_dir_all(&self, path: &Path) -> io::Result<()> {
        let mut inner = self.inner.lock().unwrap();
        let mut cur = PathBuf::new();
        for comp in path.components() {
            cur.push(comp);
            inner.dirs.insert(cur.clone());
        }
        Ok(())
    }

    fn remove_file(&self, path: &Path) -> io::Result<()> {
        let mut inner = self.inner.lock().unwrap();
        if inner.files.remove(path).is_none() {
            return Err(io::Error::from(io::ErrorKind::NotFound));
        }
        Ok(())
    }

    fn remove_dir(&self, path: &Path) -> io::Result<()> {
        let mut inner = self.inner.lock().unwrap();
        if !inner.dirs.remove(path) {
            return Err(io::Error::from(io::ErrorKind::NotFound));
        }
        Ok(())
    }

    fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>> {
        let inner = self.inner.lock().unwrap();
        if !inner.dirs.contains(path) && !inner.files.keys().any(|p| p.parent() == Some(path)) {
            return Err(io::Error::from(io::ErrorKind::NotFound));
        }
        let mut out = Vec::new();
        for p in inner.files.keys() {
            if p.parent() == Some(path) {
                out.push(p.clone());
            }
        }
        for d in &inner.dirs {
            if d.parent() == Some(path) {
                out.push(d.clone());
            }
        }
        Ok(out)
    }

    fn exists(&self, path: &Path) -> bool {
        let inner = self.inner.lock().unwrap();
        inner.files.contains_key(path) || inner.dirs.contains(path)
    }

    fn metadata(&self, path: &Path) -> io::Result<VfsMetadata> {
        let inner = self.inner.lock().unwrap();
        if let Some(f) = inner.files.get(path) {
            Ok(VfsMetadata {
                len: f.bytes.len() as u64,
                is_file: true,
            })
        } else if inner.dirs.contains(path) {
            Ok(VfsMetadata {
                len: 0,
                is_file: false,
            })
        } else {
            Err(io::Error::from(io::ErrorKind::NotFound))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::SeekFrom;

    #[test]
    fn memfs_write_then_read_round_trips() {
        let fs = InMemoryFs::new();
        {
            let mut w = fs.open_write(Path::new("/a/b.txt")).unwrap();
            w.write_all(b"hello").unwrap();
            w.flush().unwrap();
        }
        let bytes = fs.read_file(Path::new("/a/b.txt")).unwrap();
        assert_eq!(bytes, b"hello");
    }

    #[test]
    fn memfs_seek_write_extends_with_zeros() {
        let fs = InMemoryFs::new();
        let mut w = fs.open_write(Path::new("/x")).unwrap();
        w.seek(SeekFrom::Start(8)).unwrap();
        w.write_all(b"AB").unwrap();
        w.flush().unwrap();
        let bytes = fs.read_file(Path::new("/x")).unwrap();
        assert_eq!(bytes.len(), 10);
        assert_eq!(&bytes[..8], &[0u8; 8]);
        assert_eq!(&bytes[8..], b"AB");
    }

    #[test]
    fn memfs_set_len_truncates() {
        let fs = InMemoryFs::new();
        let mut w = fs.open_write(Path::new("/t")).unwrap();
        w.write_all(b"abcdef").unwrap();
        w.set_len(3).unwrap();
        drop(w);
        assert_eq!(fs.read_file(Path::new("/t")).unwrap(), b"abc");
    }

    #[test]
    fn memfs_remove_file_clears_entry() {
        let fs = InMemoryFs::new();
        drop(fs.open_write(Path::new("/r")).unwrap());
        assert!(fs.exists(Path::new("/r")));
        fs.remove_file(Path::new("/r")).unwrap();
        assert!(!fs.exists(Path::new("/r")));
    }

    #[test]
    fn memfs_create_dir_all_records_each_ancestor() {
        let fs = InMemoryFs::new();
        fs.create_dir_all(Path::new("/a/b/c")).unwrap();
        let dirs = fs.snapshot_dirs();
        assert!(dirs.contains(Path::new("/a")));
        assert!(dirs.contains(Path::new("/a/b")));
        assert!(dirs.contains(Path::new("/a/b/c")));
    }

    #[test]
    fn memfs_read_dir_enumerates_children() {
        let fs = InMemoryFs::new();
        fs.create_dir_all(Path::new("/p")).unwrap();
        drop(fs.open_write(Path::new("/p/a")).unwrap());
        drop(fs.open_write(Path::new("/p/b")).unwrap());
        let entries = fs.read_dir(Path::new("/p")).unwrap();
        assert_eq!(entries.len(), 2);
    }

    #[test]
    fn stdfs_round_trip_against_tempdir() {
        let tmp = tempfile::tempdir().unwrap();
        let fs = StdFs::new();
        let p = tmp.path().join("hello.txt");
        {
            let mut w = fs.open_write(&p).unwrap();
            w.write_all(b"world").unwrap();
            w.flush().unwrap();
        }
        let mut r = fs.open_read(&p).unwrap();
        let mut buf = Vec::new();
        r.read_to_end(&mut buf).unwrap();
        assert_eq!(buf, b"world");
    }
}