refactor(nix-compat): Properly encapsulate store path construction

Before there was code scattered about (e.g. text hashing module and
derivation output computation) constructing store paths from low level
building blocks --- there was some duplication and it was easy to make
nonsense store paths.

Now, we have roughly the same "safe-ish" ways of constructing them as
C++ Nix, and only those are exposed:

- Make text hashed content-addressed store paths

- Make other content-addressed store paths

- Make input-addressed fixed output hashes

Change-Id: I122a3ee0802b4f45ae386306b95b698991be89c8
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8411
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
John Ericson 2023-03-31 10:20:04 -04:00 committed by John Ericson
parent b4670bfbd1
commit 26c68f8e89
9 changed files with 268 additions and 125 deletions

View file

@ -4,10 +4,7 @@ use thiserror::Error;
mod utils;
pub use utils::{
build_store_path_from_fingerprint, build_store_path_from_references, compress_hash,
hash_placeholder,
};
pub use utils::*;
pub const DIGEST_SIZE: usize = 20;
// lazy_static doesn't allow us to call NIXBASE32.encode_len(), so we ran it
@ -19,19 +16,32 @@ pub const ENCODED_DIGEST_SIZE: usize = 32;
pub const STORE_DIR: &str = "/nix/store";
pub const STORE_DIR_WITH_SLASH: &str = "/nix/store/";
/// Errors that can occur during the validation of name characters.
/// Errors that can occur when parsing a literal store path
#[derive(Debug, PartialEq, Eq, Error)]
pub enum Error {
#[error("Dash is missing between hash and name")]
MissingDash(),
#[error("Hash encoding is invalid: {0}")]
InvalidHashEncoding(Nixbase32DecodeError),
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("{0}")]
InvalidName(NameError),
#[error("Tried to parse an absolute path which was missing the store dir prefix.")]
MissingStoreDir(),
}
/// Errors that can occur during the validation of name characters.
#[derive(Debug, PartialEq, Eq, Error)]
pub enum NameError {
#[error("Invalid name: {0}")]
InvalidName(String),
}
impl From<NameError> for Error {
fn from(e: NameError) -> Self {
Self::InvalidName(e)
}
}
/// Represents a path in the Nix store (a direct child of [STORE_DIR]).
///
/// It starts with a digest (20 bytes), [crate::nixbase32]-encoded,
@ -56,7 +66,7 @@ impl StorePath {
// - 1 dash
// - 1 character for the name
if s.len() < ENCODED_DIGEST_SIZE + 2 {
return Err(Error::InvalidName("".to_string()));
Err(NameError::InvalidName("".to_string()))?;
}
let digest = match nixbase32::decode(s[..ENCODED_DIGEST_SIZE].as_bytes()) {
@ -92,7 +102,7 @@ impl StorePath {
}
/// Checks a given &str to match the restrictions for store path names.
pub fn validate_name(s: &str) -> Result<(), Error> {
pub fn validate_name(s: &str) -> Result<(), NameError> {
for c in s.chars() {
if c.is_ascii_alphanumeric()
|| c == '-'
@ -105,7 +115,7 @@ impl StorePath {
continue;
}
return Err(Error::InvalidName(s.to_string()));
return Err(NameError::InvalidName(s.to_string()));
}
Ok(())