use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::io::Write;
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::paths::{archive_path_bytes_to_display, archive_path_bytes_to_hex};
use crate::{ArchiveError, Result};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct DiffOptions {
pub fingerprint_payloads: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DiffReport {
pub old: String,
pub new: String,
pub comparison: DiffComparison,
pub fingerprint_payloads: bool,
pub added: Vec<DiffEntry>,
pub removed: Vec<DiffEntry>,
pub changed: Vec<DiffChange>,
pub unchanged: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub enum DiffComparison {
MetadataOnly,
PayloadFingerprint,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DiffEntry {
pub path: String,
pub path_bytes_hex: String,
pub size: Option<u64>,
pub compressed_size: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payload_fingerprint: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DiffChange {
pub path: String,
pub path_bytes_hex: String,
pub old: DiffEntryState,
pub new: DiffEntryState,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DiffEntryState {
pub size: Option<u64>,
pub compressed_size: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payload_fingerprint: Option<String>,
}
#[derive(Debug, Clone)]
struct DiffEntryData {
raw_path: Vec<u8>,
size: Option<u64>,
compressed_size: Option<u64>,
payload_fingerprint: Option<String>,
}
pub fn diff_archives(old: &Path, new: &Path, options: &DiffOptions) -> Result<DiffReport> {
let old_archive = crate::loaded::LoadedArchive::open(old)?;
let new_archive = crate::loaded::LoadedArchive::open(new)?;
diff_loaded_archives(
&old.display().to_string(),
old_archive.as_ref(),
&new.display().to_string(),
new_archive.as_ref(),
*options,
)
}
pub(crate) fn diff_loaded_archives(
old_label: &str,
old_archive: crate::loaded::LoadedArchiveRef<'_>,
new_label: &str,
new_archive: crate::loaded::LoadedArchiveRef<'_>,
options: DiffOptions,
) -> Result<DiffReport> {
reject_unnameable_entries(old_archive, old_label)?;
reject_unnameable_entries(new_archive, new_label)?;
let old_entries = diff_entries(old_archive, options.fingerprint_payloads)?;
let new_entries = diff_entries(new_archive, options.fingerprint_payloads)?;
let mut added = Vec::new();
let mut removed = Vec::new();
let mut changed = Vec::new();
let mut unchanged = 0;
for (path, old_entry) in &old_entries {
if let Some(new_entry) = new_entries.get(path) {
if same_entry_state(old_entry, new_entry) {
unchanged += 1;
} else {
changed.push(DiffChange {
path: archive_path_bytes_to_display(path),
path_bytes_hex: archive_path_bytes_to_hex(path),
old: entry_state(old_entry),
new: entry_state(new_entry),
});
}
} else {
removed.push(diff_entry(path, old_entry));
}
}
for (path, new_entry) in &new_entries {
if !old_entries.contains_key(path) {
added.push(diff_entry(path, new_entry));
}
}
Ok(DiffReport {
old: old_label.to_string(),
new: new_label.to_string(),
comparison: if options.fingerprint_payloads {
DiffComparison::PayloadFingerprint
} else {
DiffComparison::MetadataOnly
},
fingerprint_payloads: options.fingerprint_payloads,
added,
removed,
changed,
unchanged,
})
}
fn reject_unnameable_entries(
archive: crate::loaded::LoadedArchiveRef<'_>,
label: &str,
) -> Result<()> {
if archive.has_unnameable_entries() {
return Err(ArchiveError::Archive(format!(
"archive '{label}' contains entries without recoverable paths; refusing to diff it lossy"
)));
}
Ok(())
}
fn diff_entries(
archive: crate::loaded::LoadedArchiveRef<'_>,
fingerprint_payloads: bool,
) -> Result<BTreeMap<Vec<u8>, DiffEntryData>> {
let mut entries = BTreeMap::new();
for entry in archive.list_loaded_entries()? {
let crate::loaded::LoadedEntry {
raw_path,
path,
size,
compressed_size,
} = entry;
let payload_fingerprint = if fingerprint_payloads {
Some(payload_fingerprint(archive, &path)?)
} else {
None
};
let data = DiffEntryData {
raw_path,
size,
compressed_size,
payload_fingerprint,
};
match entries.entry(path) {
Entry::Vacant(slot) => {
slot.insert(data);
}
Entry::Occupied(slot) => {
return Err(ArchiveError::Archive(format!(
"archive contains duplicate normalized path: {} (first raw path hex: {}, duplicate raw path hex: {})",
archive_path_bytes_to_display(slot.key()),
archive_path_bytes_to_hex(&slot.get().raw_path),
archive_path_bytes_to_hex(&data.raw_path)
)));
}
}
}
Ok(entries)
}
fn payload_fingerprint(
archive: crate::loaded::LoadedArchiveRef<'_>,
path: &[u8],
) -> Result<String> {
let mut hasher = Fnv1a64Writer::default();
archive.extract_normalized_entry_path_to_writer(path, &mut hasher)?;
Ok(format!("{:016x}", hasher.finish()))
}
#[derive(Debug)]
struct Fnv1a64Writer {
hash: u64,
}
impl Default for Fnv1a64Writer {
fn default() -> Self {
Self {
hash: FNV1A64_OFFSET,
}
}
}
impl Fnv1a64Writer {
fn finish(self) -> u64 {
self.hash
}
}
impl Write for Fnv1a64Writer {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.hash = fnv1a64_update(self.hash, buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
fn same_entry_state(left: &DiffEntryData, right: &DiffEntryData) -> bool {
left.size == right.size
&& left.compressed_size == right.compressed_size
&& left.payload_fingerprint == right.payload_fingerprint
}
fn diff_entry(path: &[u8], entry: &DiffEntryData) -> DiffEntry {
DiffEntry {
path: archive_path_bytes_to_display(path),
path_bytes_hex: archive_path_bytes_to_hex(path),
size: entry.size,
compressed_size: entry.compressed_size,
payload_fingerprint: entry.payload_fingerprint.clone(),
}
}
fn entry_state(entry: &DiffEntryData) -> DiffEntryState {
DiffEntryState {
size: entry.size,
compressed_size: entry.compressed_size,
payload_fingerprint: entry.payload_fingerprint.clone(),
}
}
#[cfg(test)]
fn fnv1a64(bytes: &[u8]) -> u64 {
fnv1a64_update(FNV1A64_OFFSET, bytes)
}
const FNV1A64_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
fn fnv1a64_update(mut hash: u64, bytes: &[u8]) -> u64 {
for byte in bytes {
hash ^= u64::from(*byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
hash
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_metadata_diff_of_uncompressed_tes4_archives_compares_sizes() {
let dir = std::env::temp_dir().join(format!(
"dream_archivetool-diff-tes4-{}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&dir).unwrap();
let write = |name: &str, payload: &[u8]| {
let path = dir.join(name);
let mut builder = dream_archive::Tes4BsaBuilder::new();
builder.add_bytes("meshes/same.nif", b"same").unwrap();
builder.add_bytes("meshes/grown.nif", payload).unwrap();
builder.write_path(&path).unwrap();
path
};
let old = write("old.bsa", b"old");
let new = write("new.bsa", b"newer");
let report = diff_archives(&old, &new, &DiffOptions::default()).unwrap();
assert_eq!(report.comparison, DiffComparison::MetadataOnly);
assert_eq!(report.unchanged, 1);
assert_eq!(report.changed.len(), 1);
assert_eq!(report.changed[0].path, "meshes/grown.nif");
assert_eq!(report.changed[0].old.size, Some(3));
assert_eq!(report.changed[0].new.size, Some(5));
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn streaming_fnv_matches_whole_buffer_hash() {
let mut writer = Fnv1a64Writer::default();
writer.write_all(b"abc").unwrap();
writer.write_all(b"def").unwrap();
assert_eq!(writer.finish(), fnv1a64(b"abcdef"));
}
}