diff options
author | Florian Klink <flokli@flokli.de> | 2024-08-16T14·32+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-08-17T15·59+0000 |
commit | 5ec93b57e6a263eef91ee583aba9f04581e4a66b (patch) | |
tree | 896407c00900d630a38ee82176ff12e0870f7a20 /tvix/castore/src/path/component.rs | |
parent | 8ea7d2b60eb4052d934820078c31ff25786376a4 (diff) |
refactor(tvix/castore): add PathComponent type for checked components r/8506
This encodes a verified component on the type level. Internally, it contains a bytes::Bytes. The castore Path/PathBuf component() and file_name() methods now return this type, the old ones returning bytes were renamed to component_bytes() and component_file_name() respectively. We can drop the directory_reject_invalid_name test - it's not possible anymore to pass an invalid name to Directories::add. Invalid names in the Directory proto are still being tested to be rejected in the validate_invalid_names tests. Change-Id: Ide4d16415dfd50b7e2d7e0c36d42a3bbeeb9b6c5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12217 Autosubmit: flokli <flokli@flokli.de> Reviewed-by: Connor Brewster <cbrewster@hey.com> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/castore/src/path/component.rs')
-rw-r--r-- | tvix/castore/src/path/component.rs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/tvix/castore/src/path/component.rs b/tvix/castore/src/path/component.rs new file mode 100644 index 000000000000..f755f06e62a8 --- /dev/null +++ b/tvix/castore/src/path/component.rs @@ -0,0 +1,102 @@ +// TODO: split out this error +use crate::DirectoryError; + +use bstr::ByteSlice; +use std::fmt::{self, Debug, Display}; + +/// A wrapper type for validated path components in the castore model. +/// Internally uses a [bytes::Bytes], but disallows +/// slashes, and null bytes to be present, as well as +/// '.', '..' and the empty string. +#[repr(transparent)] +#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] +pub struct PathComponent { + pub(super) inner: bytes::Bytes, +} + +impl AsRef<[u8]> for PathComponent { + fn as_ref(&self) -> &[u8] { + self.inner.as_ref() + } +} + +impl From<PathComponent> for bytes::Bytes { + fn from(value: PathComponent) -> Self { + value.inner + } +} + +pub(super) fn is_valid_name<B: AsRef<[u8]>>(name: B) -> bool { + let v = name.as_ref(); + + !v.is_empty() && v != *b".." && v != *b"." && !v.contains(&0x00) && !v.contains(&b'/') +} + +impl TryFrom<bytes::Bytes> for PathComponent { + type Error = DirectoryError; + + fn try_from(value: bytes::Bytes) -> Result<Self, Self::Error> { + if !is_valid_name(&value) { + return Err(DirectoryError::InvalidName(value)); + } + + Ok(Self { inner: value }) + } +} + +impl TryFrom<&'static [u8]> for PathComponent { + type Error = DirectoryError; + + fn try_from(value: &'static [u8]) -> Result<Self, Self::Error> { + if !is_valid_name(value) { + return Err(DirectoryError::InvalidName(bytes::Bytes::from_static( + value, + ))); + } + Ok(Self { + inner: bytes::Bytes::from_static(value), + }) + } +} + +impl TryFrom<&str> for PathComponent { + type Error = DirectoryError; + + fn try_from(value: &str) -> Result<Self, Self::Error> { + if !is_valid_name(value) { + return Err(DirectoryError::InvalidName(bytes::Bytes::copy_from_slice( + value.as_bytes(), + ))); + } + Ok(Self { + inner: bytes::Bytes::copy_from_slice(value.as_bytes()), + }) + } +} + +impl TryFrom<&std::ffi::CStr> for PathComponent { + type Error = DirectoryError; + + fn try_from(value: &std::ffi::CStr) -> Result<Self, Self::Error> { + if !is_valid_name(value.to_bytes()) { + return Err(DirectoryError::InvalidName(bytes::Bytes::copy_from_slice( + value.to_bytes(), + ))); + } + Ok(Self { + inner: bytes::Bytes::copy_from_slice(value.to_bytes()), + }) + } +} + +impl Debug for PathComponent { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + Debug::fmt(self.inner.as_bstr(), f) + } +} + +impl Display for PathComponent { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + Display::fmt(self.inner.as_bstr(), f) + } +} |