From be7f64ac87ee00aa13b0629ef4b927e14cc30e05 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 23 Jan 2023 15:19:27 +0300 Subject: refactor(tvix/derivation): minor structure simplification Fixes some module comments and embeds the `compress_hash` function in the `derivation` module, as it was not used outside of this module anyways. Change-Id: I6c5c92b3f0c03c2cdcbcfc2f813909a968c4d44c Reviewed-on: https://cl.tvl.fyi/c/depot/+/7905 Tested-by: BuildkiteCI Reviewed-by: flokli --- tvix/derivation/src/derivation.rs | 24 ++++++++++++++++++++++-- tvix/derivation/src/lib.rs | 1 - tvix/derivation/src/nix_hash.rs | 15 --------------- tvix/derivation/src/write.rs | 5 +++++ 4 files changed, 27 insertions(+), 18 deletions(-) delete mode 100644 tvix/derivation/src/nix_hash.rs diff --git a/tvix/derivation/src/derivation.rs b/tvix/derivation/src/derivation.rs index d937bd91a7fa..74e68ce1d7c5 100644 --- a/tvix/derivation/src/derivation.rs +++ b/tvix/derivation/src/derivation.rs @@ -1,6 +1,6 @@ use crate::output::{Hash, Output}; use crate::write; -use crate::{nix_hash, DerivationError}; +use crate::DerivationError; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::collections::BTreeSet; @@ -29,6 +29,26 @@ pub struct Derivation { pub system: String, } +/// compress_hash takes an arbitrarily long sequence of bytes (usually +/// a hash digest), and returns a sequence of bytes of length +/// output_size. +/// +/// It's calculated by rotating through the bytes in the output buffer +/// (zero- initialized), and XOR'ing with each byte of the passed +/// input. It consumes 1 byte at a time, and XOR's it with the current +/// value in the output buffer. +/// +/// This mimics equivalent functionality in C++ Nix. +fn compress_hash(input: &[u8], output_size: usize) -> Vec { + let mut output: Vec = vec![0; output_size]; + + for (ii, ch) in input.iter().enumerate() { + output[ii % output_size] ^= ch; + } + + output +} + /// This returns a store path, either of a derivation or a regular output. /// The path_hash is compressed to 20 bytes, and nixbase32-encoded (32 characters) fn build_store_path( @@ -36,7 +56,7 @@ fn build_store_path( path_hash: &[u8], name: &str, ) -> Result { - let compressed = nix_hash::compress_hash(path_hash, 20); + let compressed = compress_hash(path_hash, 20); if is_derivation { StorePath::from_string( format!( diff --git a/tvix/derivation/src/lib.rs b/tvix/derivation/src/lib.rs index 01b7552a6f18..4f17c3906e4c 100644 --- a/tvix/derivation/src/lib.rs +++ b/tvix/derivation/src/lib.rs @@ -1,6 +1,5 @@ mod derivation; mod errors; -mod nix_hash; mod output; mod string_escape; mod validate; diff --git a/tvix/derivation/src/nix_hash.rs b/tvix/derivation/src/nix_hash.rs deleted file mode 100644 index a49d444faa53..000000000000 --- a/tvix/derivation/src/nix_hash.rs +++ /dev/null @@ -1,15 +0,0 @@ -/// CompressHash takes an arbitrary long sequence of bytes (usually a hash -/// digest), and returns a sequence of bytes of length output_size. -/// It's calculated by rotating through the bytes in the output buffer (zero- -/// initialized), and XOR'ing with each byte of the passed input. -/// It consumes 1 byte at a time, and XOR's it with the current value in the -/// output buffer. -pub fn compress_hash(input: &[u8], output_size: usize) -> Vec { - let mut output: Vec = vec![0; output_size]; - - for (ii, ch) in input.iter().enumerate() { - output[ii % output_size] ^= ch; - } - - output -} diff --git a/tvix/derivation/src/write.rs b/tvix/derivation/src/write.rs index daca5ece2194..8874b5d35a0a 100644 --- a/tvix/derivation/src/write.rs +++ b/tvix/derivation/src/write.rs @@ -1,3 +1,8 @@ +//! This module implements the serialisation of derivations into the +//! [ATerm][] format used by C++ Nix. +//! +//! [ATerm]: http://program-transformation.org/Tools/ATermFormat.html + use crate::output::Output; use crate::string_escape::escape_string; use std::collections::BTreeSet; -- cgit 1.4.1