refactor(nix-compat): Avoid encoding round trip

When building store paths we can just construct the thing.

Change-Id: Ife5d461d6a440ecbb22f32a86a6d51d212a2035b
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8409
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
John Ericson 2023-03-30 19:32:45 -04:00 committed by John Ericson
parent b932cf2d85
commit a8bd926e0f

View file

@ -7,7 +7,7 @@ use super::Error;
/// compress_hash takes an arbitrarily long sequence of bytes (usually /// compress_hash takes an arbitrarily long sequence of bytes (usually
/// a hash digest), and returns a sequence of bytes of length /// a hash digest), and returns a sequence of bytes of length
/// output_size. /// OUTPUT_SIZE.
/// ///
/// It's calculated by rotating through the bytes in the output buffer /// It's calculated by rotating through the bytes in the output buffer
/// (zero- initialized), and XOR'ing with each byte of the passed /// (zero- initialized), and XOR'ing with each byte of the passed
@ -15,11 +15,11 @@ use super::Error;
/// value in the output buffer. /// value in the output buffer.
/// ///
/// This mimics equivalent functionality in C++ Nix. /// This mimics equivalent functionality in C++ Nix.
pub fn compress_hash(input: &[u8], output_size: usize) -> Vec<u8> { pub fn compress_hash<const OUTPUT_SIZE: usize>(input: &[u8]) -> [u8; OUTPUT_SIZE] {
let mut output: Vec<u8> = vec![0; output_size]; let mut output = [0; OUTPUT_SIZE];
for (ii, ch) in input.iter().enumerate() { for (ii, ch) in input.iter().enumerate() {
output[ii % output_size] ^= ch; output[ii % OUTPUT_SIZE] ^= ch;
} }
output output
@ -56,8 +56,12 @@ pub fn build_store_path_from_fingerprint(
let hasher = Sha256::new_with_prefix(fingerprint); let hasher = Sha256::new_with_prefix(fingerprint);
hasher.finalize() hasher.finalize()
}; };
let compressed = compress_hash(&digest, 20); let compressed = compress_hash::<20>(&digest);
StorePath::from_string(format!("{}-{}", nixbase32::encode(&compressed), name).as_str()) StorePath::validate_name(name)?;
Ok(StorePath {
digest: compressed,
name: name.to_string(),
})
} }
/// This contains the Nix logic to create "text hash strings", which are used /// This contains the Nix logic to create "text hash strings", which are used