refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
use bstr::ByteSlice;
|
2024-01-08 09:50:13 +01:00
|
|
|
use std::path::Path;
|
|
|
|
use tracing::{debug, instrument};
|
|
|
|
use tvix_castore::{
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
blobservice::BlobService, directoryservice::DirectoryService, import::fs::ingest_path, Node,
|
2024-01-08 09:50:13 +01:00
|
|
|
};
|
|
|
|
|
2024-03-29 00:43:56 +01:00
|
|
|
use nix_compat::{
|
|
|
|
nixhash::{CAHash, NixHash},
|
|
|
|
store_path::{self, StorePath},
|
|
|
|
};
|
2024-01-08 09:50:13 +01:00
|
|
|
|
|
|
|
use crate::{
|
2024-05-10 07:59:25 +02:00
|
|
|
nar::NarCalculationService,
|
2024-01-08 09:50:13 +01:00
|
|
|
pathinfoservice::PathInfoService,
|
|
|
|
proto::{nar_info, NarInfo, PathInfo},
|
|
|
|
};
|
|
|
|
|
2024-03-29 00:43:56 +01:00
|
|
|
impl From<CAHash> for nar_info::Ca {
|
|
|
|
fn from(value: CAHash) -> Self {
|
|
|
|
let hash_type: nar_info::ca::Hash = (&value).into();
|
|
|
|
let digest: bytes::Bytes = value.hash().to_string().into();
|
|
|
|
nar_info::Ca {
|
|
|
|
r#type: hash_type.into(),
|
|
|
|
digest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
pub fn log_node(name: &[u8], node: &Node, path: &Path) {
|
2024-01-08 09:50:13 +01:00
|
|
|
match node {
|
|
|
|
Node::Directory(directory_node) => {
|
|
|
|
debug!(
|
|
|
|
path = ?path,
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
name = %name.as_bstr(),
|
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
|
|
|
digest = %directory_node.digest(),
|
2024-01-08 09:50:13 +01:00
|
|
|
"import successful",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Node::File(file_node) => {
|
|
|
|
debug!(
|
|
|
|
path = ?path,
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
name = %name.as_bstr(),
|
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
|
|
|
digest = %file_node.digest(),
|
2024-01-08 09:50:13 +01:00
|
|
|
"import successful"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Node::Symlink(symlink_node) => {
|
|
|
|
debug!(
|
|
|
|
path = ?path,
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
name = %name.as_bstr(),
|
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
|
|
|
target = ?symlink_node.target(),
|
2024-01-08 09:50:13 +01:00
|
|
|
"import successful"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Transform a path into its base name and returns an [`std::io::Error`] if it is `..` or if the
|
|
|
|
/// basename is not valid unicode.
|
|
|
|
#[inline]
|
|
|
|
pub fn path_to_name(path: &Path) -> std::io::Result<&str> {
|
|
|
|
path.file_name()
|
|
|
|
.and_then(|file_name| file_name.to_str())
|
|
|
|
.ok_or_else(|| {
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::InvalidInput,
|
|
|
|
"path must not be .. and the basename valid unicode",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-29 00:43:56 +01:00
|
|
|
/// Takes the NAR size, SHA-256 of the NAR representation, the root node and optionally
|
|
|
|
/// a CA hash information.
|
|
|
|
///
|
|
|
|
/// Returns the path information object for a NAR-style object.
|
2024-01-08 09:50:13 +01:00
|
|
|
///
|
|
|
|
/// This [`PathInfo`] can be further filled for signatures, deriver or verified for the expected
|
|
|
|
/// hashes.
|
|
|
|
#[inline]
|
2024-03-29 00:43:56 +01:00
|
|
|
pub fn derive_nar_ca_path_info(
|
|
|
|
nar_size: u64,
|
|
|
|
nar_sha256: [u8; 32],
|
2024-06-25 20:48:45 +02:00
|
|
|
ca: Option<&CAHash>,
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
name: bytes::Bytes,
|
2024-03-29 00:43:56 +01:00
|
|
|
root_node: Node,
|
|
|
|
) -> PathInfo {
|
2024-01-08 09:50:13 +01:00
|
|
|
// assemble the [crate::proto::PathInfo] object.
|
|
|
|
PathInfo {
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
node: Some(tvix_castore::proto::Node::from_name_and_node(
|
|
|
|
name, root_node,
|
|
|
|
)),
|
2024-01-08 09:50:13 +01:00
|
|
|
// There's no reference scanning on path contents ingested like this.
|
|
|
|
references: vec![],
|
|
|
|
narinfo: Some(NarInfo {
|
|
|
|
nar_size,
|
|
|
|
nar_sha256: nar_sha256.to_vec().into(),
|
|
|
|
signatures: vec![],
|
|
|
|
reference_names: vec![],
|
|
|
|
deriver: None,
|
2024-03-29 00:43:56 +01:00
|
|
|
ca: ca.map(|ca_hash| ca_hash.into()),
|
2024-01-08 09:50:13 +01:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 15:00:09 +02:00
|
|
|
/// Ingest the contents at the given path `path` into castore, and registers the
|
|
|
|
/// resulting root node in the passed PathInfoService, using the "NAR sha256
|
|
|
|
/// digest" and the passed name for output path calculation.
|
2024-01-17 08:06:30 +01:00
|
|
|
#[instrument(skip_all, fields(store_name=name, path=?path), err)]
|
2024-05-10 07:59:25 +02:00
|
|
|
pub async fn import_path_as_nar_ca<BS, DS, PS, NS, P>(
|
2024-01-08 09:50:13 +01:00
|
|
|
path: P,
|
2024-01-17 08:06:30 +01:00
|
|
|
name: &str,
|
2024-01-08 09:50:13 +01:00
|
|
|
blob_service: BS,
|
|
|
|
directory_service: DS,
|
|
|
|
path_info_service: PS,
|
2024-05-10 07:59:25 +02:00
|
|
|
nar_calculation_service: NS,
|
2024-01-08 09:50:13 +01:00
|
|
|
) -> Result<StorePath, std::io::Error>
|
|
|
|
where
|
|
|
|
P: AsRef<Path> + std::fmt::Debug,
|
2024-04-18 20:51:28 +02:00
|
|
|
BS: BlobService + Clone,
|
2024-05-04 21:23:26 +02:00
|
|
|
DS: DirectoryService,
|
2024-01-08 09:50:13 +01:00
|
|
|
PS: AsRef<dyn PathInfoService>,
|
2024-05-10 07:59:25 +02:00
|
|
|
NS: NarCalculationService,
|
2024-01-08 09:50:13 +01:00
|
|
|
{
|
2024-04-30 17:48:12 +02:00
|
|
|
let root_node = ingest_path(blob_service, directory_service, path.as_ref())
|
|
|
|
.await
|
|
|
|
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
|
2024-01-08 09:50:13 +01:00
|
|
|
|
2024-05-10 07:59:25 +02:00
|
|
|
// Ask for the NAR size and sha256
|
|
|
|
let (nar_size, nar_sha256) = nar_calculation_service.calculate_nar(&root_node).await?;
|
2024-01-08 09:50:13 +01:00
|
|
|
|
|
|
|
// Calculate the output path. This might still fail, as some names are illegal.
|
2024-01-17 08:06:30 +01:00
|
|
|
// FUTUREWORK: express the `name` at the type level to be valid and move the conversion
|
|
|
|
// at the caller level.
|
2024-01-08 09:50:13 +01:00
|
|
|
let output_path = store_path::build_nar_based_store_path(&nar_sha256, name).map_err(|_| {
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::InvalidData,
|
|
|
|
format!("invalid name: {}", name),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
let name = bytes::Bytes::from(output_path.to_string());
|
|
|
|
log_node(name.as_ref(), &root_node, path.as_ref());
|
2024-01-08 09:50:13 +01:00
|
|
|
|
2024-03-29 00:43:56 +01:00
|
|
|
let path_info = derive_nar_ca_path_info(
|
|
|
|
nar_size,
|
|
|
|
nar_sha256,
|
2024-06-25 20:48:45 +02:00
|
|
|
Some(&CAHash::Nar(NixHash::Sha256(nar_sha256))),
|
refactor(tvix/castore): remove `name` from Nodes
Nodes only have names if they're contained inside a Directory, or if
they're a root node and have something else possibly giving them a name
externally.
This removes all `name` fields in the three different Nodes, and instead
maintains it inside a BTreeMap inside the Directory.
It also removes the NamedNode trait (they don't have a get_name()), as
well as Node::rename(self, name), and all [Partial]Ord implementations
for Node (as they don't have names to use for sorting).
The `nodes()`, `directories()`, `files()` iterators inside a `Directory`
now return a tuple of Name and Node, as does the RootNodesProvider.
The different {Directory,File,Symlink}Node struct constructors got
simpler, and the {Directory,File}Node ones became infallible - as
there's no more possibility to represent invalid state.
The proto structs stayed the same - there's now from_name_and_node and
into_name_and_node to convert back and forth between the two `Node`
structs.
Some further cleanups:
The error types for Node validation were renamed. Everything related to
names is now in the DirectoryError (not yet happy about the naming)
There's some leftover cleanups to do:
- There should be a from_(sorted_)iter and into_iter in Directory, so
we can construct and deconstruct in one go.
That should also enable us to implement conversions from and to the
proto representation that moves, rather than clones.
- The BuildRequest and PathInfo structs are still proto-based, so we
still do a bunch of conversions back and forth there (and have some
ugly expect there). There's not much point for error handling here,
this will be moved to stricter types in a followup CL.
Change-Id: I7369a8e3a426f44419c349077cb4fcab2044ebb6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12205
Tested-by: BuildkiteCI
Reviewed-by: yuka <yuka@yuka.dev>
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: benjaminedwardwebb <benjaminedwardwebb@gmail.com>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
2024-08-14 21:00:12 +02:00
|
|
|
output_path.to_string().into_bytes().into(),
|
2024-03-29 00:43:56 +01:00
|
|
|
root_node,
|
|
|
|
);
|
2024-01-08 09:50:13 +01:00
|
|
|
|
|
|
|
// This new [`PathInfo`] that we get back from there might contain additional signatures or
|
|
|
|
// information set by the service itself. In this function, we silently swallow it because
|
2024-06-16 15:00:09 +02:00
|
|
|
// callers don't really need it.
|
2024-01-08 09:50:13 +01:00
|
|
|
let _path_info = path_info_service.as_ref().put(path_info).await?;
|
|
|
|
|
|
|
|
Ok(output_path.to_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::{ffi::OsStr, path::PathBuf};
|
|
|
|
|
|
|
|
use crate::import::path_to_name;
|
2024-04-19 12:52:50 +02:00
|
|
|
use rstest::rstest;
|
2024-01-08 09:50:13 +01:00
|
|
|
|
2024-04-19 12:52:50 +02:00
|
|
|
#[rstest]
|
|
|
|
#[case::simple_path("a/b/c", "c")]
|
|
|
|
#[case::simple_path_containing_dotdot("a/b/../c", "c")]
|
|
|
|
#[case::path_containing_multiple_dotdot("a/b/../c/d/../e", "e")]
|
2024-01-08 09:50:13 +01:00
|
|
|
|
2024-04-19 12:52:50 +02:00
|
|
|
fn test_path_to_name(#[case] path: &str, #[case] expected_name: &str) {
|
2024-01-08 09:50:13 +01:00
|
|
|
let path: PathBuf = path.into();
|
|
|
|
assert_eq!(path_to_name(&path).expect("must succeed"), expected_name);
|
|
|
|
}
|
|
|
|
|
2024-04-19 12:52:50 +02:00
|
|
|
#[rstest]
|
|
|
|
#[case::path_ending_in_dotdot(b"a/b/..")]
|
|
|
|
#[case::non_unicode_path(b"\xf8\xa1\xa1\xa1\xa1")]
|
|
|
|
fn test_invalid_path_to_name(#[case] invalid_path: &[u8]) {
|
2024-01-08 09:50:13 +01:00
|
|
|
let path: PathBuf = unsafe { OsStr::from_encoded_bytes_unchecked(invalid_path) }.into();
|
|
|
|
path_to_name(&path).expect_err("must fail");
|
|
|
|
}
|
|
|
|
}
|