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
//! Sequential apply driver — bridges the parser [`ZiPatchReader`] with the
//! apply layer's [`ApplySession`].
//!
//! The high-level entry points come in two forms:
//!
//! - [`ApplyConfig::apply_patch`] / [`ApplyConfig::resume_apply_patch`] —
//!   consuming convenience methods that materialise a fresh session, run
//!   the patch, and drop the session on return.
//! - [`ApplySession::apply_patch`] / [`ApplySession::resume_apply_patch`] —
//!   `&mut self` methods for callers driving multiple patches against the
//!   same session.
//!
//! The chunk-loop machinery is private to this module; consumers should never
//! need to write the dispatch loop by hand.

use crate::apply::{self, ApplyConfig, ApplySession, Checkpoint, SequentialCheckpoint};
use crate::chunk::{self, ZiPatchReader};
use crate::{ApplyError, ApplyResult, ParseError};
use std::io::{Read, Seek};
use std::ops::ControlFlow;

impl ApplyConfig {
    /// Iterate every chunk in `reader` and apply each one against a freshly
    /// materialised [`ApplySession`].
    ///
    /// Convenience wrapper around [`ApplyConfig::into_session`] followed by
    /// [`ApplySession::apply_patch`]; the session is dropped on return.
    ///
    /// See [`ApplySession::apply_patch`] for the full error contract.
    pub fn apply_patch<R: Read>(self, reader: ZiPatchReader<R>) -> ApplyResult<()> {
        self.into_session().apply_patch(reader)
    }

    /// Resume a previously interrupted apply against a freshly materialised
    /// [`ApplySession`].
    ///
    /// Convenience wrapper around [`ApplyConfig::into_session`] followed by
    /// [`ApplySession::resume_apply_patch`]; the session is dropped on return.
    pub fn resume_apply_patch<R: Read + Seek>(
        self,
        reader: ZiPatchReader<R>,
        from: Option<&SequentialCheckpoint>,
    ) -> ApplyResult<SequentialCheckpoint> {
        self.into_session().resume_apply_patch(reader, from)
    }
}

impl ApplySession {
    /// Iterate every chunk in `reader` and apply each one to this session.
    ///
    /// This is the primary high-level entry point for applying a patch. It
    /// drives the [`ZiPatchReader`] to completion, dispatching each
    /// yielded [`Chunk`](crate::Chunk) through the apply layer in stream order.
    ///
    /// Chunks **must** be applied in order — the `ZiPatch` format is a
    /// sequential log and later chunks may depend on filesystem state produced
    /// by earlier ones.
    ///
    /// # Errors
    ///
    /// Stops at the first parse or apply error and returns it immediately.
    /// Any filesystem changes already applied by earlier chunks are **not**
    /// rolled back.
    pub fn apply_patch<R: Read>(&mut self, mut reader: ZiPatchReader<R>) -> ApplyResult<()> {
        let span = tracing::info_span!(crate::tracing_schema::span_names::APPLY_PATCH);
        let _enter = span.enter();
        let started = std::time::Instant::now();
        self.patch_name = reader.patch_name().map(str::to_owned);
        self.patch_size = None;
        let result = run_apply_loop(&mut reader, self, 0);
        let flush_result = self.flush();
        let (final_result, chunks_applied) = match (result, flush_result) {
            (Ok(n), Ok(())) => (Ok(()), n),
            (Ok(_), Err(e)) => (Err(ApplyError::from(e)), 0),
            (Err(e), _) => (Err(e), 0),
        };
        if final_result.is_ok() {
            tracing::info!(
                chunks = chunks_applied,
                bytes_read = reader.bytes_read(),
                resumed_from_chunk = tracing::field::Empty,
                elapsed_ms = started.elapsed().as_millis() as u64,
                "apply_patch: patch applied"
            );
        }
        final_result
    }

    /// Resume a previously interrupted apply from a [`SequentialCheckpoint`].
    ///
    /// See [`ApplyConfig::resume_apply_patch`] for the conceptual overview
    /// and the resume contract.
    ///
    /// # Errors
    ///
    /// Same vocabulary as [`Self::apply_patch`], plus
    /// [`ApplyError::SchemaVersionMismatch`] when `from.schema_version` does
    /// not equal [`SequentialCheckpoint::CURRENT_SCHEMA_VERSION`].
    #[allow(clippy::too_many_lines)]
    pub fn resume_apply_patch<R: Read + Seek>(
        &mut self,
        mut reader: ZiPatchReader<R>,
        from: Option<&SequentialCheckpoint>,
    ) -> ApplyResult<SequentialCheckpoint> {
        let span = tracing::info_span!(crate::tracing_schema::span_names::RESUME_APPLY_PATCH);
        let _enter = span.enter();
        let started = std::time::Instant::now();

        if let Some(cp) = from {
            if !cp
                .schema_version
                .compatible_with(SequentialCheckpoint::CURRENT_SCHEMA_VERSION)
            {
                return Err(ApplyError::SchemaVersionMismatch {
                    kind: "sequential-checkpoint",
                    found: cp.schema_version,
                    expected: SequentialCheckpoint::CURRENT_SCHEMA_VERSION,
                });
            }
        }

        let reader_name = reader.patch_name().map(str::to_owned);
        let total_size = stream_total_size(&mut reader)?;
        self.patch_name.clone_from(&reader_name);
        self.patch_size = Some(total_size);

        let effective_from = from.and_then(|cp| {
            let name_match = cp.patch_name == reader_name;
            let size_match = match cp.patch_size {
                Some(sz) => sz == total_size,
                None => true,
            };
            if name_match && size_match {
                Some(cp)
            } else {
                tracing::warn!(
                    expected_patch_name = ?reader_name,
                    expected_patch_size = total_size,
                    checkpoint_patch_name = ?cp.patch_name,
                    checkpoint_patch_size = ?cp.patch_size,
                    "resume_apply_patch: stale checkpoint, restarting from chunk 0"
                );
                None
            }
        });

        let resumed_from_chunk = effective_from.map(|cp| cp.next_chunk_index);
        let skipped_bytes_at_start = effective_from.map_or(0, |cp| cp.bytes_read);
        let has_in_flight = effective_from
            .and_then(|cp| cp.in_flight.as_ref())
            .is_some();

        if let Some(cp) = effective_from {
            tracing::info!(
                patch_name = ?reader_name,
                skipped_chunks = cp.next_chunk_index,
                skipped_bytes = cp.bytes_read,
                has_in_flight,
                "resume_apply_patch: resuming patch"
            );
            fast_forward(&mut reader, cp.next_chunk_index, cp.bytes_read)?;
        }

        let start_index = effective_from.map_or(0, |cp| cp.next_chunk_index);
        let in_flight = effective_from.and_then(|cp| cp.in_flight.clone());

        let result: ApplyResult<u64> = (|| {
            if let Some(in_flight) = in_flight {
                resume_in_flight_chunk(&mut reader, self, start_index, &in_flight)?;
                run_apply_loop(&mut reader, self, start_index + 1).map(|n| n + 1)
            } else {
                run_apply_loop(&mut reader, self, start_index)
            }
        })();

        let flush_result = self.flush();
        let (final_result, chunks_applied) = match (result, flush_result) {
            (Ok(n), Ok(())) => (Ok(()), n),
            (Ok(_), Err(e)) => (Err(ApplyError::from(e)), 0),
            (Err(e), _) => (Err(e), 0),
        };

        match final_result {
            Ok(()) => {
                let bytes_read = reader.bytes_read();
                if let Some(from_chunk) = resumed_from_chunk {
                    tracing::info!(
                        chunks = chunks_applied,
                        bytes_read,
                        resumed_from_chunk = from_chunk,
                        skipped_bytes = skipped_bytes_at_start,
                        elapsed_ms = started.elapsed().as_millis() as u64,
                        "resume_apply_patch: patch applied"
                    );
                } else {
                    tracing::info!(
                        chunks = chunks_applied,
                        bytes_read,
                        resumed_from_chunk = tracing::field::Empty,
                        elapsed_ms = started.elapsed().as_millis() as u64,
                        "resume_apply_patch: patch applied"
                    );
                }
                Ok(SequentialCheckpoint {
                    schema_version: SequentialCheckpoint::CURRENT_SCHEMA_VERSION,
                    next_chunk_index: start_index + chunks_applied,
                    bytes_read,
                    patch_name: reader_name,
                    patch_size: Some(total_size),
                    in_flight: None,
                })
            }
            Err(e) => Err(e),
        }
    }
}

fn run_apply_loop<R: Read>(
    reader: &mut ZiPatchReader<R>,
    session: &mut ApplySession,
    start_index: u64,
) -> ApplyResult<u64> {
    let mut index = start_index;
    while let Some(rec) = reader.next_chunk()? {
        session.current_chunk_index = index;
        session.current_chunk_bytes_read = rec.bytes_read;
        rec.chunk.apply(session)?;
        let bytes_read = rec.bytes_read;
        let next_chunk_index = index + 1;
        let checkpoint = Checkpoint::Sequential(SequentialCheckpoint {
            schema_version: SequentialCheckpoint::CURRENT_SCHEMA_VERSION,
            next_chunk_index,
            bytes_read,
            patch_name: session.patch_name.clone(),
            patch_size: session.patch_size,
            in_flight: None,
        });
        tracing::debug!(
            next_chunk_index,
            bytes_read,
            in_flight = false,
            "apply_patch: checkpoint recorded"
        );
        session.record_checkpoint(&checkpoint)?;
        let event = apply::ChunkEvent {
            index: index as usize,
            kind: rec.tag,
            bytes_read,
        };
        if let ControlFlow::Break(()) = session.observer_mut().on_chunk_applied(event) {
            return Err(ApplyError::Cancelled);
        }
        if session.cancel_requested() {
            return Err(ApplyError::Cancelled);
        }
        index += 1;
    }
    Ok(index - start_index)
}

fn stream_total_size<R: Read + Seek>(reader: &mut ZiPatchReader<R>) -> ApplyResult<u64> {
    let inner = reader.inner_mut();
    let current = inner.stream_position()?;
    let end = inner.seek(std::io::SeekFrom::End(0))?;
    inner.seek(std::io::SeekFrom::Start(current))?;
    Ok(end)
}

fn fast_forward<R: Read>(
    reader: &mut ZiPatchReader<R>,
    target_chunks: u64,
    expected_bytes_read: u64,
) -> ApplyResult<()> {
    let mut consumed: u64 = 0;
    while consumed < target_chunks {
        match reader.next_chunk()? {
            Some(_) => consumed += 1,
            None => {
                return Err(ApplyError::Parse(ParseError::TruncatedPatch));
            }
        }
    }
    if reader.bytes_read() != expected_bytes_read {
        tracing::warn!(
            actual_bytes_read = reader.bytes_read(),
            expected_bytes_read,
            target_chunks,
            "resume_apply_patch: bytes_read drift during fast-forward"
        );
    }
    tracing::debug!(
        skipped_chunks = target_chunks,
        bytes_read = reader.bytes_read(),
        "resume_apply_patch: fast-forward complete"
    );
    Ok(())
}

fn resume_in_flight_chunk<R: Read>(
    reader: &mut ZiPatchReader<R>,
    session: &mut ApplySession,
    chunk_index: u64,
    in_flight: &apply::InFlightAddFile,
) -> ApplyResult<()> {
    let Some(rec) = reader.next_chunk()? else {
        return Err(ApplyError::Parse(ParseError::TruncatedPatch));
    };

    session.current_chunk_index = chunk_index;
    session.current_chunk_bytes_read = rec.bytes_read;

    let (start_block, start_bytes) = match resolve_in_flight_resume(&rec.chunk, session, in_flight)
    {
        InFlightResume::Resume {
            start_block,
            start_bytes,
        } => (start_block, start_bytes),
        InFlightResume::Restart => (0, 0),
    };

    match &rec.chunk {
        chunk::Chunk::Sqpk(chunk::SqpkCommand::File(file))
            if matches!(
                file.operation,
                crate::chunk::sqpk::SqpkFileOperation::AddFile
            ) =>
        {
            apply::sqpk::apply_file_add_from(file, session, start_block, start_bytes)?;
        }
        _ => rec.chunk.apply(session)?,
    }

    let bytes_read = rec.bytes_read;
    let tag = rec.tag;
    let next_chunk_index = chunk_index + 1;
    let checkpoint = Checkpoint::Sequential(SequentialCheckpoint {
        schema_version: SequentialCheckpoint::CURRENT_SCHEMA_VERSION,
        next_chunk_index,
        bytes_read,
        patch_name: session.patch_name.clone(),
        patch_size: session.patch_size,
        in_flight: None,
    });
    session.record_checkpoint(&checkpoint)?;
    let event = apply::ChunkEvent {
        index: chunk_index as usize,
        kind: tag,
        bytes_read,
    };
    if let ControlFlow::Break(()) = session.observer_mut().on_chunk_applied(event) {
        return Err(ApplyError::Cancelled);
    }
    if session.cancel_requested() {
        return Err(ApplyError::Cancelled);
    }
    Ok(())
}

enum InFlightResume {
    Resume {
        start_block: usize,
        start_bytes: u64,
    },
    Restart,
}

fn resolve_in_flight_resume(
    chunk: &chunk::Chunk,
    session: &ApplySession,
    in_flight: &apply::InFlightAddFile,
) -> InFlightResume {
    let chunk::Chunk::Sqpk(chunk::SqpkCommand::File(file)) = chunk else {
        tracing::warn!(
            "resume_apply_patch: in-flight chunk is not an SqpkFile; discarding in-flight state"
        );
        return InFlightResume::Restart;
    };
    if !matches!(
        file.operation,
        crate::chunk::sqpk::SqpkFileOperation::AddFile
    ) {
        tracing::warn!(
            "resume_apply_patch: in-flight chunk is not an AddFile; discarding in-flight state"
        );
        return InFlightResume::Restart;
    }

    let expected_path = apply::path::generic_path(session, &file.path);
    if expected_path != in_flight.target_path {
        tracing::warn!(
            chunk_path = %expected_path.display(),
            in_flight_path = %in_flight.target_path.display(),
            "resume_apply_patch: in-flight target path does not match chunk; discarding"
        );
        return InFlightResume::Restart;
    }
    let chunk_offset = file.file_offset;
    if chunk_offset != in_flight.file_offset {
        tracing::warn!(
            chunk_offset,
            in_flight_offset = in_flight.file_offset,
            "resume_apply_patch: in-flight file_offset does not match chunk; discarding"
        );
        return InFlightResume::Restart;
    }
    if in_flight.block_idx as usize > file.blocks.len() {
        tracing::warn!(
            block_idx = in_flight.block_idx,
            block_count = file.blocks.len(),
            "resume_apply_patch: in-flight block_idx out of range; discarding"
        );
        return InFlightResume::Restart;
    }
    if chunk_offset == 0 && in_flight.bytes_into_target > 0 {
        let on_disk_len = session
            .vfs()
            .metadata(&in_flight.target_path)
            .map_or(0, |m| m.len);
        if on_disk_len < in_flight.bytes_into_target {
            tracing::warn!(
                target = %in_flight.target_path.display(),
                on_disk_len,
                bytes_into_target = in_flight.bytes_into_target,
                "resume_apply_patch: target file truncated or missing since checkpoint; restarting AddFile"
            );
            return InFlightResume::Restart;
        }
    }

    InFlightResume::Resume {
        start_block: in_flight.block_idx as usize,
        start_bytes: in_flight.bytes_into_target,
    }
}