diff options
Diffstat (limited to '')
-rw-r--r-- | tvix/derivation/src/derivation.rs | 14 | ||||
-rw-r--r-- | tvix/derivation/src/output.rs | 4 | ||||
-rw-r--r-- | tvix/derivation/src/tests/mod.rs | 6 | ||||
-rw-r--r-- | tvix/derivation/src/validate.rs | 6 | ||||
-rw-r--r-- | tvix/store/src/lib.rs | 2 | ||||
-rw-r--r-- | tvix/store/src/nixbase32.rs | 4 | ||||
-rw-r--r-- | tvix/store/src/proto.rs | 18 | ||||
-rw-r--r-- | tvix/store/src/store_path.rs (renamed from tvix/store/src/nixpath.rs) | 54 | ||||
-rw-r--r-- | tvix/store/src/tests/pathinfo.rs | 25 |
9 files changed, 68 insertions, 65 deletions
diff --git a/tvix/derivation/src/derivation.rs b/tvix/derivation/src/derivation.rs index 2afe672e2e9b..bf26e1baac69 100644 --- a/tvix/derivation/src/derivation.rs +++ b/tvix/derivation/src/derivation.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::{collections::BTreeMap, fmt, fmt::Write}; use tvix_store::nixbase32::NIXBASE32; -use tvix_store::nixpath::{NixPath, ParseNixPathError, STORE_DIR}; +use tvix_store::nixpath::{ParseStorePathError, StorePath, STORE_DIR}; #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Derivation { @@ -34,10 +34,10 @@ fn build_store_path( is_derivation: bool, path_hash: &[u8], name: &str, -) -> Result<NixPath, ParseNixPathError> { +) -> Result<StorePath, ParseStorePathError> { let compressed = nix_hash::compress_hash(path_hash, 20); if is_derivation { - NixPath::from_string( + StorePath::from_string( format!( "{}-{}{}", NIXBASE32.encode(&compressed), @@ -47,7 +47,7 @@ fn build_store_path( .as_str(), ) } else { - NixPath::from_string(format!("{}-{}", NIXBASE32.encode(&compressed), name,).as_str()) + StorePath::from_string(format!("{}-{}", NIXBASE32.encode(&compressed), name,).as_str()) } } @@ -105,8 +105,8 @@ impl Derivation { /// - Write the .drv A-Term contents to a hash function /// - Take the digest, run hash.CompressHash(digest, 20) on it. /// - Encode it with nixbase32 - /// - Use it (and the name) to construct a NixPath. - pub fn calculate_derivation_path(&self, name: &str) -> Result<NixPath, ParseNixPathError> { + /// - Use it (and the name) to construct a [StorePath]. + pub fn calculate_derivation_path(&self, name: &str) -> Result<StorePath, ParseStorePathError> { let mut hasher = Sha256::new(); // collect the list of paths from input_sources and input_derivations @@ -223,7 +223,7 @@ impl Derivation { &mut self, name: &str, drv_replacement_str: &str, - ) -> Result<(), ParseNixPathError> { + ) -> Result<(), ParseStorePathError> { let mut hasher = Sha256::new(); // Check if the Derivation is fixed output, because they cause diff --git a/tvix/derivation/src/output.rs b/tvix/derivation/src/output.rs index b038f6b5fc6d..02ba7e42e885 100644 --- a/tvix/derivation/src/output.rs +++ b/tvix/derivation/src/output.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use tvix_store::nixpath::NixPath; +use tvix_store::nixpath::StorePath; #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Output { @@ -23,7 +23,7 @@ impl Output { } pub fn validate(&self) -> anyhow::Result<()> { - NixPath::from_absolute_path(&self.path)?; + StorePath::from_absolute_path(&self.path)?; Ok(()) } } diff --git a/tvix/derivation/src/tests/mod.rs b/tvix/derivation/src/tests/mod.rs index 700a25860571..8d1771f711f6 100644 --- a/tvix/derivation/src/tests/mod.rs +++ b/tvix/derivation/src/tests/mod.rs @@ -6,7 +6,7 @@ use std::io::Read; use std::path::Path; use test_case::test_case; use test_generator::test_resources; -use tvix_store::nixpath::NixPath; +use tvix_store::nixpath::StorePath; const RESOURCES_PATHS: &str = "src/tests/derivation_tests"; @@ -66,7 +66,7 @@ fn derivation_path(name: &str, expected_path: &str) { assert_eq!( derivation.calculate_derivation_path(name).unwrap(), - NixPath::from_string(expected_path).unwrap() + StorePath::from_string(expected_path).unwrap() ); } @@ -309,7 +309,7 @@ fn output_path_construction() { assert_eq!(foo_drv_expected, foo_drv); assert_eq!( - NixPath::from_string("4wvvbi4jwn0prsdxb7vs673qa5h9gr7x-foo.drv").expect("must succeed"), + StorePath::from_string("4wvvbi4jwn0prsdxb7vs673qa5h9gr7x-foo.drv").expect("must succeed"), foo_drv .calculate_derivation_path("foo") .expect("must succeed") diff --git a/tvix/derivation/src/validate.rs b/tvix/derivation/src/validate.rs index e0f62a323fcf..4e0503833913 100644 --- a/tvix/derivation/src/validate.rs +++ b/tvix/derivation/src/validate.rs @@ -1,6 +1,6 @@ use crate::{derivation::Derivation, write::DOT_FILE_EXT}; use anyhow::bail; -use tvix_store::nixpath::NixPath; +use tvix_store::nixpath::StorePath; impl Derivation { /// validate ensures a Derivation struct is properly populated, @@ -35,7 +35,7 @@ impl Derivation { // Validate all input_derivations for (input_derivation_path, output_names) in &self.input_derivations { // Validate input_derivation_path - NixPath::from_absolute_path(input_derivation_path)?; + StorePath::from_absolute_path(input_derivation_path)?; if !input_derivation_path.ends_with(DOT_FILE_EXT) { bail!( "derivation {} does not end with .drv", @@ -70,7 +70,7 @@ impl Derivation { // Validate all input_sources for (i, input_source) in self.input_sources.iter().enumerate() { - NixPath::from_absolute_path(input_source)?; + StorePath::from_absolute_path(input_source)?; if i > 0 && self.input_sources[i - 1] >= *input_source { bail!( diff --git a/tvix/store/src/lib.rs b/tvix/store/src/lib.rs index de1cd6557331..642d285990dd 100644 --- a/tvix/store/src/lib.rs +++ b/tvix/store/src/lib.rs @@ -1,6 +1,6 @@ pub mod nixbase32; -pub mod nixpath; pub mod proto; +pub mod store_path; pub mod dummy_blob_service; pub mod dummy_directory_service; diff --git a/tvix/store/src/nixbase32.rs b/tvix/store/src/nixbase32.rs index 78a90f605507..913e60714b47 100644 --- a/tvix/store/src/nixbase32.rs +++ b/tvix/store/src/nixbase32.rs @@ -72,7 +72,7 @@ mod tests { // #[test_case("0z", vec![0x1f]; "one byte")] #[test_case("00bgd045z0d4icpbc2yyz4gx48ak44la", vec![ 0x8a, 0x12, 0x32, 0x15, 0x22, 0xfd, 0x91, 0xef, 0xbd, 0x60, 0xeb, 0xb2, 0x48, 0x1a, - 0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00]; "nixpath")] + 0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00]; "store path")] fn encode(enc: &str, dec: Vec<u8>) { assert_eq!(enc, NIXBASE32.encode(&dec)); } @@ -83,7 +83,7 @@ mod tests { // #[test_case("0z", Some(vec![0x1f]); "one byte")] #[test_case("00bgd045z0d4icpbc2yyz4gx48ak44la", Some(vec![ 0x8a, 0x12, 0x32, 0x15, 0x22, 0xfd, 0x91, 0xef, 0xbd, 0x60, 0xeb, 0xb2, 0x48, 0x1a, - 0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00]); "nixpath")] + 0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00]); "store path")] // this is invalid encoding, because it encodes 10 1-bytes, so the carry // would be 2 1-bytes #[test_case("zz", None; "invalid encoding-1")] diff --git a/tvix/store/src/proto.rs b/tvix/store/src/proto.rs index f2af69ab0dbb..f2dfe1a86d1c 100644 --- a/tvix/store/src/proto.rs +++ b/tvix/store/src/proto.rs @@ -5,7 +5,7 @@ use thiserror::Error; use prost::Message; -use crate::nixpath::{NixPath, ParseNixPathError}; +use crate::store_path::{ParseStorePathError, StorePath}; tonic::include_proto!("tvix.store.v1"); @@ -41,7 +41,7 @@ pub enum ValidatePathInfoError { /// Invalid node name encountered. #[error("{0} is an invalid node name: {1}")] - InvalidNodeName(String, ParseNixPathError), + InvalidNodeName(String, ParseStorePathError), /// The digest the (root) node refers to has invalid length. #[error("Invalid Digest length: {0}")] @@ -76,13 +76,13 @@ fn validate_digest<E>(digest: &Vec<u8>, err: fn(usize) -> E) -> Result<(), E> { /// Parses a root node name. /// -/// On success, this returns the parsed [NixPath]. +/// On success, this returns the parsed [StorePath]. /// On error, it returns an error generated from the supplied constructor. fn parse_node_name_root<E>( name: &str, - err: fn(String, ParseNixPathError) -> E, -) -> Result<NixPath, E> { - match NixPath::from_string(name) { + err: fn(String, ParseStorePathError) -> E, +) -> Result<StorePath, E> { + match StorePath::from_string(name) { Ok(np) => Ok(np), Err(e) => Err(err(name.to_string(), e)), } @@ -90,9 +90,9 @@ fn parse_node_name_root<E>( impl PathInfo { /// validate performs some checks on the PathInfo struct, - /// Returning either a [NixPath] of the root node, or a + /// Returning either a [StorePath] of the root node, or a /// [ValidatePathInfoError]. - pub fn validate(&self) -> Result<NixPath, ValidatePathInfoError> { + pub fn validate(&self) -> Result<StorePath, ValidatePathInfoError> { // If there is a narinfo field populated, ensure the number of references there // matches PathInfo.references count. if let Some(narinfo) = &self.narinfo { @@ -106,7 +106,7 @@ impl PathInfo { // FUTUREWORK: parse references in reference_names. ensure they start // with storeDir, and use the same digest as in self.references. - // Ensure there is a (root) node present, and it properly parses to a NixPath. + // Ensure there is a (root) node present, and it properly parses to a [StorePath]. let root_nix_path = match &self.node { None => { return Err(ValidatePathInfoError::NoNodePresent()); diff --git a/tvix/store/src/nixpath.rs b/tvix/store/src/store_path.rs index 54b964e83f04..1d767e3519ac 100644 --- a/tvix/store/src/nixpath.rs +++ b/tvix/store/src/store_path.rs @@ -15,7 +15,7 @@ pub const STORE_DIR_WITH_SLASH: &str = "/nix/store/"; /// Errors that can occur during the validation of name characters. #[derive(Debug, PartialEq, Eq, Error)] -pub enum ParseNixPathError { +pub enum ParseStorePathError { #[error("Dash is missing")] MissingDash(), #[error("Hash encoding is invalid {0}")] @@ -27,57 +27,57 @@ pub enum ParseNixPathError { } #[derive(Debug, PartialEq, Eq)] -pub struct NixPath { +pub struct StorePath { pub digest: [u8; DIGEST_SIZE], pub name: String, } -impl NixPath { - pub fn from_string(s: &str) -> Result<NixPath, ParseNixPathError> { +impl StorePath { + pub fn from_string(s: &str) -> Result<StorePath, ParseStorePathError> { // the whole string needs to be at least: // // - 32 characters (encoded hash) // - 1 dash // - 1 character for the name if s.len() < ENCODED_DIGEST_SIZE + 2 { - return Err(ParseNixPathError::InvalidName("".to_string())); + return Err(ParseStorePathError::InvalidName("".to_string())); } let digest = match NIXBASE32.decode(s[..ENCODED_DIGEST_SIZE].as_bytes()) { Ok(decoded) => decoded, Err(decoder_error) => { - return Err(ParseNixPathError::InvalidHashEncoding(decoder_error)) + return Err(ParseStorePathError::InvalidHashEncoding(decoder_error)) } }; if s.as_bytes()[ENCODED_DIGEST_SIZE] != b'-' { - return Err(ParseNixPathError::MissingDash()); + return Err(ParseStorePathError::MissingDash()); } - NixPath::validate_characters(&s[ENCODED_DIGEST_SIZE + 2..])?; + StorePath::validate_characters(&s[ENCODED_DIGEST_SIZE + 2..])?; - Ok(NixPath { + Ok(StorePath { name: s[ENCODED_DIGEST_SIZE + 1..].to_string(), digest: digest.try_into().expect("size is known"), }) } - /// Construct a NixPath from an absolute store path string. + /// Construct a [StorePath] from an absolute store path string. /// That is a string starting with the store prefix (/nix/store) - pub fn from_absolute_path(s: &str) -> Result<NixPath, ParseNixPathError> { + pub fn from_absolute_path(s: &str) -> Result<StorePath, ParseStorePathError> { match s.strip_prefix(STORE_DIR_WITH_SLASH) { Some(s_stripped) => Self::from_string(s_stripped), - None => Err(ParseNixPathError::MissingStoreDir()), + None => Err(ParseStorePathError::MissingStoreDir()), } } - // Converts the NixPath to an absolute store path string. + // Converts the [StorePath] to an absolute store path string. /// That is a string starting with the store prefix (/nix/store) pub fn to_absolute_path(&self) -> String { format!("{}/{}", STORE_DIR, self) } - fn validate_characters(s: &str) -> Result<(), ParseNixPathError> { + fn validate_characters(s: &str) -> Result<(), ParseStorePathError> { for c in s.chars() { if c.is_ascii_alphanumeric() || c == '-' @@ -90,14 +90,14 @@ impl NixPath { continue; } - return Err(ParseNixPathError::InvalidName(s.to_string())); + return Err(ParseStorePathError::InvalidName(s.to_string())); } Ok(()) } } -impl fmt::Display for NixPath { +impl fmt::Display for StorePath { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, @@ -111,9 +111,9 @@ impl fmt::Display for NixPath { #[cfg(test)] mod tests { use crate::nixbase32::NIXBASE32; - use crate::nixpath::{DIGEST_SIZE, ENCODED_DIGEST_SIZE}; + use crate::store_path::{DIGEST_SIZE, ENCODED_DIGEST_SIZE}; - use super::{NixPath, ParseNixPathError}; + use super::{ParseStorePathError, StorePath}; #[test] fn encoded_digest_size() { @@ -125,7 +125,7 @@ mod tests { let example_nix_path_str = "00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432"; let nixpath = - NixPath::from_string(&example_nix_path_str).expect("Error parsing example string"); + StorePath::from_string(&example_nix_path_str).expect("Error parsing example string"); let expected_digest: [u8; DIGEST_SIZE] = [ 0x8a, 0x12, 0x32, 0x15, 0x22, 0xfd, 0x91, 0xef, 0xbd, 0x60, 0xeb, 0xb2, 0x48, 0x1a, @@ -140,19 +140,19 @@ mod tests { #[test] fn invalid_hash_length() { - NixPath::from_string("00bgd045z0d4icpbc2yy-net-tools-1.60_p20170221182432") + StorePath::from_string("00bgd045z0d4icpbc2yy-net-tools-1.60_p20170221182432") .expect_err("No error raised."); } #[test] fn invalid_encoding_hash() { - NixPath::from_string("00bgd045z0d4icpbc2yyz4gx48aku4la-net-tools-1.60_p20170221182432") + StorePath::from_string("00bgd045z0d4icpbc2yyz4gx48aku4la-net-tools-1.60_p20170221182432") .expect_err("No error raised."); } #[test] fn more_than_just_the_bare_nix_store_path() { - NixPath::from_string( + StorePath::from_string( "00bgd045z0d4icpbc2yyz4gx48aku4la-net-tools-1.60_p20170221182432/bin/arp", ) .expect_err("No error raised."); @@ -160,7 +160,7 @@ mod tests { #[test] fn no_dash_between_hash_and_name() { - NixPath::from_string("00bgd045z0d4icpbc2yyz4gx48ak44lanet-tools-1.60_p20170221182432") + StorePath::from_string("00bgd045z0d4icpbc2yyz4gx48ak44lanet-tools-1.60_p20170221182432") .expect_err("No error raised."); } @@ -168,9 +168,9 @@ mod tests { fn absolute_path() { let example_nix_path_str = "00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432"; - let nixpath_expected = NixPath::from_string(&example_nix_path_str).expect("must parse"); + let nixpath_expected = StorePath::from_string(&example_nix_path_str).expect("must parse"); - let nixpath_actual = NixPath::from_absolute_path( + let nixpath_actual = StorePath::from_absolute_path( "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432", ) .expect("must parse"); @@ -186,8 +186,8 @@ mod tests { #[test] fn absolute_path_missing_prefix() { assert_eq!( - ParseNixPathError::MissingStoreDir(), - NixPath::from_absolute_path("foobar-123").expect_err("must fail") + ParseStorePathError::MissingStoreDir(), + StorePath::from_absolute_path("foobar-123").expect_err("must fail") ); } } diff --git a/tvix/store/src/tests/pathinfo.rs b/tvix/store/src/tests/pathinfo.rs index 8f4e9d399084..a278cf5e8eb0 100644 --- a/tvix/store/src/tests/pathinfo.rs +++ b/tvix/store/src/tests/pathinfo.rs @@ -1,6 +1,6 @@ use crate::{ - nixpath::{NixPath, ParseNixPathError}, proto::{self, Node, PathInfo, ValidatePathInfoError}, + store_path::{ParseStorePathError, StorePath}, }; use lazy_static::lazy_static; use test_case::test_case; @@ -30,7 +30,10 @@ const DUMMY_NAME: &str = "00000000000000000000000000000000-dummy"; Err(ValidatePathInfoError::NoNodePresent()); "No node 2" )] -fn validate_no_node(t_node: Option<proto::Node>, t_result: Result<NixPath, ValidatePathInfoError>) { +fn validate_no_node( + t_node: Option<proto::Node>, + t_result: Result<StorePath, ValidatePathInfoError>, +) { // construct the PathInfo object let p = PathInfo { node: t_node, @@ -45,7 +48,7 @@ fn validate_no_node(t_node: Option<proto::Node>, t_result: Result<NixPath, Valid digest: DUMMY_DIGEST.to_vec(), size: 0, }, - Ok(NixPath::from_string(DUMMY_NAME).expect("must succeed")); + Ok(StorePath::from_string(DUMMY_NAME).expect("must succeed")); "ok" )] #[test_case( @@ -65,13 +68,13 @@ fn validate_no_node(t_node: Option<proto::Node>, t_result: Result<NixPath, Valid }, Err(ValidatePathInfoError::InvalidNodeName( "invalid".to_string(), - ParseNixPathError::InvalidName("".to_string()) + ParseStorePathError::InvalidName("".to_string()) )); "invalid node name" )] fn validate_directory( t_directory_node: proto::DirectoryNode, - t_result: Result<NixPath, ValidatePathInfoError>, + t_result: Result<StorePath, ValidatePathInfoError>, ) { // construct the PathInfo object let p = PathInfo { @@ -90,7 +93,7 @@ fn validate_directory( size: 0, executable: false, }, - Ok(NixPath::from_string(DUMMY_NAME).expect("must succeed")); + Ok(StorePath::from_string(DUMMY_NAME).expect("must succeed")); "ok" )] #[test_case( @@ -110,11 +113,11 @@ fn validate_directory( }, Err(ValidatePathInfoError::InvalidNodeName( "invalid".to_string(), - ParseNixPathError::InvalidName("".to_string()) + ParseStorePathError::InvalidName("".to_string()) )); "invalid node name" )] -fn validate_file(t_file_node: proto::FileNode, t_result: Result<NixPath, ValidatePathInfoError>) { +fn validate_file(t_file_node: proto::FileNode, t_result: Result<StorePath, ValidatePathInfoError>) { // construct the PathInfo object let p = PathInfo { node: Some(Node { @@ -130,7 +133,7 @@ fn validate_file(t_file_node: proto::FileNode, t_result: Result<NixPath, Validat name: DUMMY_NAME.to_string(), ..Default::default() }, - Ok(NixPath::from_string(DUMMY_NAME).expect("must succeed")); + Ok(StorePath::from_string(DUMMY_NAME).expect("must succeed")); "ok" )] #[test_case( @@ -140,13 +143,13 @@ fn validate_file(t_file_node: proto::FileNode, t_result: Result<NixPath, Validat }, Err(ValidatePathInfoError::InvalidNodeName( "invalid".to_string(), - ParseNixPathError::InvalidName("".to_string()) + ParseStorePathError::InvalidName("".to_string()) )); "invalid node name" )] fn validate_symlink( t_symlink_node: proto::SymlinkNode, - t_result: Result<NixPath, ValidatePathInfoError>, + t_result: Result<StorePath, ValidatePathInfoError>, ) { // construct the PathInfo object let p = PathInfo { |