From 329a7d30a7e11fabe46ab22cdb96ba800f75d49a Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 21 Dec 2023 17:05:54 +0200 Subject: refactor(nix-compat/store_path): centralize self_reference check self_reference being set to true is only allowed for `CAHash::Nar(NixHash::Sha256(_))`, so we can handle this in a check at the front. Change-Id: Ic363ade4789a7767cbe26a6959b143bb53e50e5a Reviewed-on: https://cl.tvl.fyi/c/depot/+/10391 Reviewed-by: edef Autosubmit: flokli Tested-by: BuildkiteCI --- tvix/nix-compat/src/store_path/utils.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'tvix/nix-compat/src') diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs index 2e10c33dcdaa..af593d27affd 100644 --- a/tvix/nix-compat/src/store_path/utils.rs +++ b/tvix/nix-compat/src/store_path/utils.rs @@ -61,29 +61,28 @@ pub fn build_ca_path<'a, S: AsRef, I: IntoIterator>( references: I, self_reference: bool, ) -> Result, BuildStorePathError> { - let (ty, hash) = match &ca_hash { - CAHash::Text(ref digest) => { - if self_reference { - return Err(BuildStorePathError::InvalidReference()); - } + // self references are only allowed for CAHash::Nar(NixHash::Sha256(_)). + if self_reference { + let CAHash::Nar(NixHash::Sha256(_)) = ca_hash else { + return Err(BuildStorePathError::InvalidReference()); + }; + } - ( - make_references_string("text", references, false), - NixHash::Sha256(*digest), - ) - } + let (ty, hash) = match &ca_hash { + CAHash::Text(ref digest) => ( + make_references_string("text", references, false), + NixHash::Sha256(*digest), + ), CAHash::Nar(NixHash::Sha256(ref digest)) => ( make_references_string("source", references, self_reference), NixHash::Sha256(*digest), ), + // for all other CAHash::Nar, another custom scheme is used. CAHash::Nar(ref hash) => { if references.into_iter().next().is_some() { return Err(BuildStorePathError::InvalidReference()); } - if self_reference { - return Err(BuildStorePathError::InvalidReference()); - } ( "output:out".to_string(), @@ -99,9 +98,6 @@ pub fn build_ca_path<'a, S: AsRef, I: IntoIterator>( if references.into_iter().next().is_some() { return Err(BuildStorePathError::InvalidReference()); } - if self_reference { - return Err(BuildStorePathError::InvalidReference()); - } ( "output:out".to_string(), -- cgit 1.4.1