2023-05-14 13:49:53 +02:00
|
|
|
use super::DirectoryService;
|
2024-05-01 19:23:23 +02:00
|
|
|
use crate::{proto::NamedNode, B3Digest, Error, Path};
|
2023-05-14 13:49:53 +02:00
|
|
|
use tracing::{instrument, warn};
|
|
|
|
|
2023-09-22 00:08:13 +02:00
|
|
|
/// This descends from a (root) node to the given (sub)path, returning the Node
|
2023-05-14 13:49:53 +02:00
|
|
|
/// at that path, or none, if there's nothing at that path.
|
2024-05-01 19:23:23 +02:00
|
|
|
#[instrument(skip(directory_service, path), fields(%path))]
|
2023-12-31 22:30:27 +01:00
|
|
|
pub async fn descend_to<DS>(
|
|
|
|
directory_service: DS,
|
2023-09-19 18:46:41 +02:00
|
|
|
root_node: crate::proto::node::Node,
|
2024-05-01 19:23:23 +02:00
|
|
|
path: impl AsRef<Path> + std::fmt::Display,
|
2023-12-31 22:30:27 +01:00
|
|
|
) -> Result<Option<crate::proto::node::Node>, Error>
|
|
|
|
where
|
2024-01-09 10:14:24 +01:00
|
|
|
DS: AsRef<dyn DirectoryService>,
|
2023-12-31 22:30:27 +01:00
|
|
|
{
|
2023-09-19 18:46:41 +02:00
|
|
|
let mut cur_node = root_node;
|
2024-05-01 19:23:23 +02:00
|
|
|
let mut it = path.as_ref().components();
|
2023-05-14 13:49:53 +02:00
|
|
|
|
2023-09-19 18:46:41 +02:00
|
|
|
loop {
|
|
|
|
match it.next() {
|
|
|
|
None => {
|
|
|
|
// the (remaining) path is empty, return the node we're current at.
|
|
|
|
return Ok(Some(cur_node));
|
|
|
|
}
|
|
|
|
Some(first_component) => {
|
|
|
|
match cur_node {
|
|
|
|
crate::proto::node::Node::File(_) | crate::proto::node::Node::Symlink(_) => {
|
|
|
|
// There's still some path left, but the current node is no directory.
|
|
|
|
// This means the path doesn't exist, as we can't reach it.
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
crate::proto::node::Node::Directory(directory_node) => {
|
|
|
|
let digest: B3Digest = directory_node.digest.try_into().map_err(|_e| {
|
|
|
|
Error::StorageError("invalid digest length".to_string())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// fetch the linked node from the directory_service
|
2024-01-09 10:14:24 +01:00
|
|
|
match directory_service.as_ref().get(&digest).await? {
|
2023-09-19 18:46:41 +02:00
|
|
|
// If we didn't get the directory node that's linked, that's a store inconsistency, bail out!
|
|
|
|
None => {
|
|
|
|
warn!("directory {} does not exist", digest);
|
|
|
|
|
|
|
|
return Err(Error::StorageError(format!(
|
|
|
|
"directory {} does not exist",
|
|
|
|
digest
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
Some(directory) => {
|
|
|
|
// look for first_component in the [Directory].
|
|
|
|
// FUTUREWORK: as the nodes() iterator returns in a sorted fashion, we
|
|
|
|
// could stop as soon as e.name is larger than the search string.
|
2024-05-01 19:23:23 +02:00
|
|
|
let child_node =
|
|
|
|
directory.nodes().find(|n| n.get_name() == first_component);
|
2023-09-19 18:46:41 +02:00
|
|
|
|
|
|
|
match child_node {
|
|
|
|
// child node not found means there's no such element inside the directory.
|
|
|
|
None => {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
// child node found, return to top-of loop to find the next
|
|
|
|
// node in the path.
|
|
|
|
Some(child_node) => {
|
|
|
|
cur_node = child_node;
|
|
|
|
}
|
2023-05-14 13:49:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-03-20 21:36:02 +01:00
|
|
|
use crate::{
|
|
|
|
directoryservice,
|
|
|
|
fixtures::{DIRECTORY_COMPLICATED, DIRECTORY_WITH_KEEP},
|
2024-05-01 19:23:23 +02:00
|
|
|
PathBuf,
|
2024-03-20 21:36:02 +01:00
|
|
|
};
|
2023-05-14 13:49:53 +02:00
|
|
|
|
2023-09-22 00:08:13 +02:00
|
|
|
use super::descend_to;
|
2023-05-14 13:49:53 +02:00
|
|
|
|
2023-09-19 18:46:41 +02:00
|
|
|
#[tokio::test]
|
2023-09-22 00:08:13 +02:00
|
|
|
async fn test_descend_to() {
|
2024-03-20 21:36:02 +01:00
|
|
|
let directory_service = directoryservice::from_addr("memory://").await.unwrap();
|
2023-05-14 13:49:53 +02:00
|
|
|
|
|
|
|
let mut handle = directory_service.put_multiple_start();
|
|
|
|
handle
|
|
|
|
.put(DIRECTORY_WITH_KEEP.clone())
|
2023-09-19 18:46:41 +02:00
|
|
|
.await
|
2023-05-14 13:49:53 +02:00
|
|
|
.expect("must succeed");
|
|
|
|
handle
|
|
|
|
.put(DIRECTORY_COMPLICATED.clone())
|
2023-09-19 18:46:41 +02:00
|
|
|
.await
|
2023-05-14 13:49:53 +02:00
|
|
|
.expect("must succeed");
|
|
|
|
|
2024-03-23 06:07:55 +01:00
|
|
|
handle.close().await.expect("must upload");
|
|
|
|
|
2023-05-14 13:49:53 +02:00
|
|
|
// construct the node for DIRECTORY_COMPLICATED
|
|
|
|
let node_directory_complicated =
|
|
|
|
crate::proto::node::Node::Directory(crate::proto::DirectoryNode {
|
2023-07-18 18:37:25 +02:00
|
|
|
name: "doesntmatter".into(),
|
2023-07-19 17:52:50 +02:00
|
|
|
digest: DIRECTORY_COMPLICATED.digest().into(),
|
2023-05-14 13:49:53 +02:00
|
|
|
size: DIRECTORY_COMPLICATED.size(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// construct the node for DIRECTORY_COMPLICATED
|
|
|
|
let node_directory_with_keep = crate::proto::node::Node::Directory(
|
|
|
|
DIRECTORY_COMPLICATED.directories.first().unwrap().clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// construct the node for the .keep file
|
|
|
|
let node_file_keep =
|
|
|
|
crate::proto::node::Node::File(DIRECTORY_WITH_KEEP.files.first().unwrap().clone());
|
|
|
|
|
|
|
|
// traversal to an empty subpath should return the root node.
|
|
|
|
{
|
2023-09-22 00:08:13 +02:00
|
|
|
let resp = descend_to(
|
2024-01-09 10:14:24 +01:00
|
|
|
&directory_service,
|
2023-05-14 13:49:53 +02:00
|
|
|
node_directory_complicated.clone(),
|
2024-05-01 19:23:23 +02:00
|
|
|
"".parse::<PathBuf>().unwrap(),
|
2023-05-14 13:49:53 +02:00
|
|
|
)
|
2023-09-19 18:46:41 +02:00
|
|
|
.await
|
2023-05-14 13:49:53 +02:00
|
|
|
.expect("must succeed");
|
|
|
|
|
|
|
|
assert_eq!(Some(node_directory_complicated.clone()), resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// traversal to `keep` should return the node for DIRECTORY_WITH_KEEP
|
|
|
|
{
|
2023-09-22 00:08:13 +02:00
|
|
|
let resp = descend_to(
|
2024-01-09 10:14:24 +01:00
|
|
|
&directory_service,
|
2023-05-14 13:49:53 +02:00
|
|
|
node_directory_complicated.clone(),
|
2024-05-01 19:23:23 +02:00
|
|
|
"keep".parse::<PathBuf>().unwrap(),
|
2023-05-14 13:49:53 +02:00
|
|
|
)
|
2023-09-19 18:46:41 +02:00
|
|
|
.await
|
2023-05-14 13:49:53 +02:00
|
|
|
.expect("must succeed");
|
|
|
|
|
2023-08-19 17:16:31 +02:00
|
|
|
assert_eq!(Some(node_directory_with_keep), resp);
|
2023-05-14 13:49:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// traversal to `keep/.keep` should return the node for the .keep file
|
|
|
|
{
|
2023-09-22 00:08:13 +02:00
|
|
|
let resp = descend_to(
|
2024-01-09 10:14:24 +01:00
|
|
|
&directory_service,
|
2023-05-14 13:49:53 +02:00
|
|
|
node_directory_complicated.clone(),
|
2024-05-01 19:23:23 +02:00
|
|
|
"keep/.keep".parse::<PathBuf>().unwrap(),
|
2023-05-14 13:49:53 +02:00
|
|
|
)
|
2023-09-19 18:46:41 +02:00
|
|
|
.await
|
2023-05-14 13:49:53 +02:00
|
|
|
.expect("must succeed");
|
|
|
|
|
|
|
|
assert_eq!(Some(node_file_keep.clone()), resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// traversal to `void` should return None (doesn't exist)
|
|
|
|
{
|
2023-09-22 00:08:13 +02:00
|
|
|
let resp = descend_to(
|
2024-01-09 10:14:24 +01:00
|
|
|
&directory_service,
|
2023-05-14 13:49:53 +02:00
|
|
|
node_directory_complicated.clone(),
|
2024-05-01 19:23:23 +02:00
|
|
|
"void".parse::<PathBuf>().unwrap(),
|
2023-05-14 13:49:53 +02:00
|
|
|
)
|
2023-09-19 18:46:41 +02:00
|
|
|
.await
|
2023-05-14 13:49:53 +02:00
|
|
|
.expect("must succeed");
|
|
|
|
|
|
|
|
assert_eq!(None, resp);
|
|
|
|
}
|
|
|
|
|
2024-05-01 19:23:23 +02:00
|
|
|
// traversal to `v/oid` should return None (doesn't exist)
|
2023-05-14 13:49:53 +02:00
|
|
|
{
|
2023-09-22 00:08:13 +02:00
|
|
|
let resp = descend_to(
|
2024-01-09 10:14:24 +01:00
|
|
|
&directory_service,
|
2023-05-14 13:49:53 +02:00
|
|
|
node_directory_complicated.clone(),
|
2024-05-01 19:23:23 +02:00
|
|
|
"v/oid".parse::<PathBuf>().unwrap(),
|
2023-05-14 13:49:53 +02:00
|
|
|
)
|
2023-09-19 18:46:41 +02:00
|
|
|
.await
|
2023-05-14 13:49:53 +02:00
|
|
|
.expect("must succeed");
|
|
|
|
|
|
|
|
assert_eq!(None, resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// traversal to `keep/.keep/404` should return None (the path can't be
|
|
|
|
// reached, as keep/.keep already is a file)
|
|
|
|
{
|
2023-09-22 00:08:13 +02:00
|
|
|
let resp = descend_to(
|
2024-01-09 10:14:24 +01:00
|
|
|
&directory_service,
|
2023-05-14 13:49:53 +02:00
|
|
|
node_directory_complicated.clone(),
|
2024-05-01 19:23:23 +02:00
|
|
|
"keep/.keep/foo".parse::<PathBuf>().unwrap(),
|
2023-05-14 13:49:53 +02:00
|
|
|
)
|
2023-09-19 18:46:41 +02:00
|
|
|
.await
|
2023-05-14 13:49:53 +02:00
|
|
|
.expect("must succeed");
|
|
|
|
|
|
|
|
assert_eq!(None, resp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|