diff options
Diffstat (limited to '')
-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 |
5 files changed, 53 insertions, 50 deletions
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 { |