Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use object for parsing archives, and handle macOS fat archives #1776

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ nix = { version = "0.26.2", optional = true }
rouille = { version = "3.5", optional = true, default-features = false, features = ["ssl"] }
syslog = { version = "6", optional = true }
version-compare = { version = "0.1.1", optional = true }
object = "0.30"
memmap2 = "0.6.2"

[dev-dependencies]
assert_cmd = "2.0.10"
Expand Down
40 changes: 24 additions & 16 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
// limitations under the License.

use crate::mock_command::{CommandChild, RunCommand};
use ar::Archive;
use blake3::Hasher as blake3_Hasher;
use byteorder::{BigEndian, ByteOrder};
use fs::File;
use fs_err as fs;
use object::{macho, read::archive::ArchiveFile, read::macho::FatArch};
use serde::Serialize;
use std::ffi::{OsStr, OsString};
use std::hash::Hasher;
Expand Down Expand Up @@ -145,13 +145,24 @@ pub async fn hash_all_archives(
let path = path.clone();
pool.spawn_blocking(move || -> Result<String> {
let mut m = Digest::new();
let reader = File::open(&path)
let archive_file = File::open(&path)
.with_context(|| format!("Failed to open file for hashing: {:?}", path))?;
let mut archive = Archive::new(reader);
while let Some(entry) = archive.next_entry() {
let entry = entry?;
m.update(entry.header().identifier());
update_from_reader(&mut m, entry)?;
let archive_mmap =
unsafe { memmap2::MmapOptions::new().map_copy_read_only(&archive_file)? };
drahnr marked this conversation as resolved.
Show resolved Hide resolved

match macho::FatHeader::parse(&*archive_mmap) {
Ok(h) if h.magic.get(object::endian::BigEndian) == macho::FAT_MAGIC => {
for arch in macho::FatHeader::parse_arch32(&*archive_mmap)? {
hash_regular_archive(&mut m, arch.data(&*archive_mmap)?)?;
}
}
Ok(h) if h.magic.get(object::endian::BigEndian) == macho::FAT_MAGIC_64 => {
for arch in macho::FatHeader::parse_arch64(&*archive_mmap)? {
hash_regular_archive(&mut m, arch.data(&*archive_mmap)?)?;
}
}
// Not a FatHeader at all, regular archive.
_ => hash_regular_archive(&mut m, &archive_mmap)?,
}
Ok(m.finish())
})
Expand All @@ -170,15 +181,12 @@ pub async fn hash_all_archives(
Ok(hashes.into_iter().map(|res| res.unwrap()).collect())
}

/// Update the digest `m` with all data from `reader`.
fn update_from_reader<R: Read>(m: &mut Digest, mut reader: R) -> Result<()> {
loop {
let mut buffer = [0; 1024];
let count = reader.read(&mut buffer[..])?;
if count == 0 {
break;
}
m.update(&buffer[..count]);
fn hash_regular_archive(m: &mut Digest, data: &[u8]) -> Result<()> {
let archive = ArchiveFile::parse(data)?;
for entry in archive.members() {
let entry = entry?;
m.update(entry.name());
m.update(entry.data(data)?);
}
Ok(())
}
Expand Down