refactor(tvix/castore): use Directory struct separate from proto one
This uses our own data type to deal with Directories in the castore model.
It makes some undesired states unrepresentable, removing the need for conversions and checking in various places:
- In the protobuf, blake3 digests could have a wrong length, as proto doesn't know fixed-size fields. We now use `B3Digest`, which makes cloning cheaper, and removes the need to do size-checking everywhere.
- In the protobuf, we had three different lists for `files`, `symlinks` and `directories`. This was mostly a protobuf size optimization, but made interacting with them a bit awkward. This has now been replaced with a list of enums, and convenience iterators to get various nodes, and add new ones.
Change-Id: I7b92691bb06d77ff3f58a5ccea94a22c16f84f04
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12057
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
2024-07-29 14:34:50 +02:00
|
|
|
use bstr::ByteSlice;
|
2023-02-12 11:39:31 +01:00
|
|
|
use thiserror::Error;
|
2023-03-25 22:17:23 +01:00
|
|
|
use tokio::task::JoinError;
|
2023-02-12 12:34:15 +01:00
|
|
|
use tonic::Status;
|
2023-02-12 11:39:31 +01:00
|
|
|
|
|
|
|
/// Errors related to communication with the store.
|
2024-03-23 21:49:49 +01:00
|
|
|
#[derive(Debug, Error, PartialEq)]
|
2023-02-12 11:39:31 +01:00
|
|
|
pub enum Error {
|
|
|
|
#[error("invalid request: {0}")]
|
|
|
|
InvalidRequest(String),
|
|
|
|
|
|
|
|
#[error("internal storage error: {0}")]
|
|
|
|
StorageError(String),
|
|
|
|
}
|
refactor(tvix/castore): use Directory struct separate from proto one
This uses our own data type to deal with Directories in the castore model.
It makes some undesired states unrepresentable, removing the need for conversions and checking in various places:
- In the protobuf, blake3 digests could have a wrong length, as proto doesn't know fixed-size fields. We now use `B3Digest`, which makes cloning cheaper, and removes the need to do size-checking everywhere.
- In the protobuf, we had three different lists for `files`, `symlinks` and `directories`. This was mostly a protobuf size optimization, but made interacting with them a bit awkward. This has now been replaced with a list of enums, and convenience iterators to get various nodes, and add new ones.
Change-Id: I7b92691bb06d77ff3f58a5ccea94a22c16f84f04
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12057
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
2024-07-29 14:34:50 +02:00
|
|
|
|
|
|
|
/// Errors that can occur during the validation of [Directory] messages.
|
|
|
|
#[derive(Debug, thiserror::Error, PartialEq)]
|
|
|
|
pub enum ValidateDirectoryError {
|
|
|
|
/// Elements are not in sorted order
|
|
|
|
#[error("{:?} is not sorted", .0.as_bstr())]
|
|
|
|
WrongSorting(Vec<u8>),
|
|
|
|
/// Multiple elements with the same name encountered
|
|
|
|
#[error("{:?} is a duplicate name", .0.as_bstr())]
|
|
|
|
DuplicateName(Vec<u8>),
|
|
|
|
/// Invalid node
|
|
|
|
#[error("invalid node with name {:?}: {:?}", .0.as_bstr(), .1.to_string())]
|
|
|
|
InvalidNode(Vec<u8>, ValidateNodeError),
|
|
|
|
#[error("Total size exceeds u32::MAX")]
|
|
|
|
SizeOverflow,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Errors that occur during Node validation
|
|
|
|
#[derive(Debug, thiserror::Error, PartialEq)]
|
|
|
|
pub enum ValidateNodeError {
|
|
|
|
#[error("No node set")]
|
|
|
|
NoNodeSet,
|
|
|
|
/// Invalid digest length encountered
|
|
|
|
#[error("invalid digest length: {0}")]
|
|
|
|
InvalidDigestLen(usize),
|
|
|
|
/// Invalid name encountered
|
|
|
|
#[error("Invalid name: {}", .0.as_bstr())]
|
|
|
|
InvalidName(bytes::Bytes),
|
|
|
|
/// Invalid symlink target
|
|
|
|
#[error("Invalid symlink target: {}", .0.as_bstr())]
|
|
|
|
InvalidSymlinkTarget(bytes::Bytes),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<crate::digests::Error> for ValidateNodeError {
|
|
|
|
fn from(e: crate::digests::Error) -> Self {
|
|
|
|
match e {
|
|
|
|
crate::digests::Error::InvalidDigestLen(n) => ValidateNodeError::InvalidDigestLen(n),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-03 22:32:07 +01:00
|
|
|
|
2023-03-25 22:17:23 +01:00
|
|
|
impl From<JoinError> for Error {
|
|
|
|
fn from(value: JoinError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 12:34:15 +01:00
|
|
|
impl From<Error> for Status {
|
|
|
|
fn from(value: Error) -> Self {
|
|
|
|
match value {
|
|
|
|
Error::InvalidRequest(msg) => Status::invalid_argument(msg),
|
|
|
|
Error::StorageError(msg) => Status::data_loss(format!("storage error: {}", msg)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 22:31:50 +01:00
|
|
|
|
2023-11-13 13:32:24 +01:00
|
|
|
impl From<crate::tonic::Error> for Error {
|
|
|
|
fn from(value: crate::tonic::Error) -> Self {
|
2023-10-12 19:26:52 +02:00
|
|
|
Self::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-21 00:36:19 +02:00
|
|
|
impl From<redb::Error> for Error {
|
|
|
|
fn from(value: redb::Error) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::DatabaseError> for Error {
|
|
|
|
fn from(value: redb::DatabaseError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::TableError> for Error {
|
|
|
|
fn from(value: redb::TableError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::TransactionError> for Error {
|
|
|
|
fn from(value: redb::TransactionError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::StorageError> for Error {
|
|
|
|
fn from(value: redb::StorageError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::CommitError> for Error {
|
|
|
|
fn from(value: redb::CommitError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 17:30:55 +01:00
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(value: std::io::Error) -> Self {
|
|
|
|
if value.kind() == std::io::ErrorKind::InvalidInput {
|
|
|
|
Error::InvalidRequest(value.to_string())
|
|
|
|
} else {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-26 22:31:50 +01:00
|
|
|
// TODO: this should probably go somewhere else?
|
|
|
|
impl From<Error> for std::io::Error {
|
|
|
|
fn from(value: Error) -> Self {
|
|
|
|
match value {
|
|
|
|
Error::InvalidRequest(msg) => Self::new(std::io::ErrorKind::InvalidInput, msg),
|
|
|
|
Error::StorageError(msg) => Self::new(std::io::ErrorKind::Other, msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|