about summary refs log tree commit diff
path: root/tvix/store/src
diff options
context:
space:
mode:
authorJürgen Hahn <mail.jhahn@gmail.com>2022-12-30T11·51+0100
committerjrhahn <mail.jhahn@gmail.com>2022-12-30T14·39+0000
commit5520bf3682040fc0eabba9cd55d6c403f35c8775 (patch)
treeb0befe8588d43e7cb9c039dfa9508e8b0694842a /tvix/store/src
parent357c4d4836180975b4699877b2344d1b2c88d690 (diff)
feat(tvix/store): add nixpath r/5551
This implements the NixPath structure. NixPath allow to parse a string to a nix path. If the parsing fails, a DecodeError will be raised.

Change-Id: I28363cdcfb27f04bf21a11c0d130b461667e3720
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7706
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/store/src')
-rw-r--r--tvix/store/src/main.rs1
-rw-r--r--tvix/store/src/nixpath.rs142
2 files changed, 143 insertions, 0 deletions
diff --git a/tvix/store/src/main.rs b/tvix/store/src/main.rs
index cca96c1bbb57..c7823b87e468 100644
--- a/tvix/store/src/main.rs
+++ b/tvix/store/src/main.rs
@@ -1,4 +1,5 @@
 mod nixbase32;
+mod nixpath;
 mod proto;
 
 #[cfg(test)]
diff --git a/tvix/store/src/nixpath.rs b/tvix/store/src/nixpath.rs
new file mode 100644
index 000000000000..8232e3241e11
--- /dev/null
+++ b/tvix/store/src/nixpath.rs
@@ -0,0 +1,142 @@
+use crate::nixbase32::NIXBASE32;
+use data_encoding::DecodeError;
+use std::fmt;
+use thiserror::Error;
+
+const PATH_HASH_SIZE: usize = 20;
+
+/// Errors that can occur during the validation of name characters.
+#[derive(Debug, PartialEq, Eq, Error)]
+pub enum ParseNixPathError {
+    #[error("Dash is missing")]
+    MissingDash(),
+    #[error("Hash encoding is invalid {0}")]
+    InvalidHashEncoding(DecodeError),
+    #[error("Invalid name {0}")]
+    InvalidName(String),
+}
+
+#[derive(Debug, PartialEq, Eq)]
+pub struct NixPath {
+    digest: [u8; PATH_HASH_SIZE],
+    name: String,
+}
+
+impl NixPath {
+    pub fn from_string(s: &str) -> Result<NixPath, ParseNixPathError> {
+        let encoded_path_hash_size = NIXBASE32.encode_len(PATH_HASH_SIZE);
+        let name_offset = encoded_path_hash_size + 1;
+
+        // the whole string needs to be at least:
+        //
+        // - 32 characters (encoded hash)
+        // - 1 dash
+        // - 1 character for the name
+        if s.len() < name_offset + 1 {
+            return Err(ParseNixPathError::InvalidName("".to_string()));
+        }
+
+        let digest = match NIXBASE32.decode(s[..encoded_path_hash_size].as_bytes()) {
+            Ok(decoded) => decoded,
+            Err(decoder_error) => {
+                return Err(ParseNixPathError::InvalidHashEncoding(decoder_error))
+            }
+        };
+
+        if s.as_bytes()[encoded_path_hash_size] != b'-' {
+            return Err(ParseNixPathError::MissingDash());
+        }
+
+        NixPath::validate_characters(&s[name_offset..])?;
+
+        // copy the digest:Vec<u8> to a [u8; PATH_HASH_SIZE]
+        let mut buffer: [u8; PATH_HASH_SIZE] = [0; PATH_HASH_SIZE];
+        buffer[..PATH_HASH_SIZE].copy_from_slice(&digest[..PATH_HASH_SIZE]);
+
+        Ok(NixPath {
+            name: s[name_offset..].to_string(),
+            digest: buffer,
+        })
+    }
+
+    fn validate_characters(s: &str) -> Result<(), ParseNixPathError> {
+        for c in s.chars() {
+            if c.is_ascii_alphanumeric()
+                || c == '-'
+                || c == '_'
+                || c == '.'
+                || c == '+'
+                || c == '?'
+                || c == '='
+            {
+                continue;
+            }
+
+            return Err(ParseNixPathError::InvalidName(s.to_string()));
+        }
+
+        Ok(())
+    }
+}
+
+impl fmt::Display for NixPath {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(
+            f,
+            "{}-{}",
+            crate::nixbase32::NIXBASE32.encode(&self.digest),
+            self.name
+        )
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::nixpath::PATH_HASH_SIZE;
+
+    use super::NixPath;
+
+    #[test]
+    fn happy_path() {
+        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");
+
+        let expected_digest: [u8; PATH_HASH_SIZE] = [
+            0x8a, 0x12, 0x32, 0x15, 0x22, 0xfd, 0x91, 0xef, 0xbd, 0x60, 0xeb, 0xb2, 0x48, 0x1a,
+            0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00,
+        ];
+
+        assert_eq!("net-tools-1.60_p20170221182432", nixpath.name);
+        assert_eq!(nixpath.digest, expected_digest);
+
+        assert_eq!(example_nix_path_str, nixpath.to_string())
+    }
+
+    #[test]
+    fn invalid_hash_length() {
+        NixPath::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")
+            .expect_err("No error raised.");
+    }
+
+    #[test]
+    fn more_than_just_the_bare_nix_store_path() {
+        NixPath::from_string(
+            "00bgd045z0d4icpbc2yyz4gx48aku4la-net-tools-1.60_p20170221182432/bin/arp",
+        )
+        .expect_err("No error raised.");
+    }
+
+    #[test]
+    fn no_dash_between_hash_and_name() {
+        NixPath::from_string("00bgd045z0d4icpbc2yyz4gx48ak44lanet-tools-1.60_p20170221182432")
+            .expect_err("No error raised.");
+    }
+}