refactor(glue/snix_store_io): add node_get_type helper
There's multiple places where we peek at the node to construct a FileType, so move this into a helper. Also, get rid of a async move which didn't move, and use .ok_or_else to make things a bit more readable. Change-Id: I2d24a3291029fdc12e0049398d8d51111e22d3cf Reviewed-on: https://cl.snix.dev/c/snix/+/30548 Autosubmit: Florian Klink <flokli@flokli.de> Reviewed-by: Connor Brewster <cbrewster@hey.com> Tested-by: besadii
This commit is contained in:
parent
59aeeb6fe4
commit
0f9c5f0354
1 changed files with 31 additions and 30 deletions
|
|
@ -307,6 +307,15 @@ impl SnixStoreIO {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper function peeking at a [snix_castore::Node] and returning its [FileType]
|
||||||
|
fn node_get_type(node: &Node) -> FileType {
|
||||||
|
match node {
|
||||||
|
Node::Directory { .. } => FileType::Directory,
|
||||||
|
Node::File { .. } => FileType::Regular,
|
||||||
|
Node::Symlink { .. } => FileType::Symlink,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl EvalIO for SnixStoreIO {
|
impl EvalIO for SnixStoreIO {
|
||||||
#[instrument(skip(self), ret(level = Level::TRACE), err)]
|
#[instrument(skip(self), ret(level = Level::TRACE), err)]
|
||||||
fn path_exists(&self, path: &Path) -> io::Result<bool> {
|
fn path_exists(&self, path: &Path) -> io::Result<bool> {
|
||||||
|
|
@ -389,11 +398,7 @@ impl EvalIO for SnixStoreIO {
|
||||||
.tokio_handle
|
.tokio_handle
|
||||||
.block_on(async { self.store_path_to_path_info(&store_path, sub_path).await })?
|
.block_on(async { self.store_path_to_path_info(&store_path, sub_path).await })?
|
||||||
{
|
{
|
||||||
match path_info.node {
|
Ok(node_get_type(&path_info.node))
|
||||||
Node::Directory { .. } => Ok(FileType::Directory),
|
|
||||||
Node::File { .. } => Ok(FileType::Regular),
|
|
||||||
Node::Symlink { .. } => Ok(FileType::Symlink),
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
self.std_io.file_type(path)
|
self.std_io.file_type(path)
|
||||||
}
|
}
|
||||||
|
|
@ -412,31 +417,27 @@ impl EvalIO for SnixStoreIO {
|
||||||
match path_info.node {
|
match path_info.node {
|
||||||
Node::Directory { digest, .. } => {
|
Node::Directory { digest, .. } => {
|
||||||
// fetch the Directory itself.
|
// fetch the Directory itself.
|
||||||
if let Some(directory) = self.tokio_handle.block_on({
|
let directory = self
|
||||||
let digest = digest.clone();
|
.tokio_handle
|
||||||
async move { self.directory_service.as_ref().get(&digest).await }
|
.block_on(async { self.directory_service.as_ref().get(&digest).await })?
|
||||||
})? {
|
.ok_or_else(|| {
|
||||||
let mut children: Vec<(bytes::Bytes, FileType)> = Vec::new();
|
|
||||||
for (name, node) in directory.into_nodes() {
|
|
||||||
children.push(match node {
|
|
||||||
Node::Directory { .. } => (name.into(), FileType::Directory),
|
|
||||||
Node::File { .. } => (name.clone().into(), FileType::Regular),
|
|
||||||
Node::Symlink { .. } => (name.into(), FileType::Symlink),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Ok(children)
|
|
||||||
} else {
|
|
||||||
// If we didn't get the directory node that's linked, that's a store inconsistency!
|
// If we didn't get the directory node that's linked, that's a store inconsistency!
|
||||||
error!(
|
error!(
|
||||||
directory.digest = %digest,
|
directory.digest = %digest,
|
||||||
path = ?path,
|
path = ?path,
|
||||||
"directory not found",
|
"directory not found",
|
||||||
);
|
);
|
||||||
Err(io::Error::new(
|
io::Error::new(
|
||||||
io::ErrorKind::NotFound,
|
io::ErrorKind::NotFound,
|
||||||
format!("directory {digest} does not exist"),
|
format!("directory {digest} does not exist"),
|
||||||
))?
|
)
|
||||||
}
|
})?;
|
||||||
|
|
||||||
|
// construct children from nodes
|
||||||
|
Ok(directory
|
||||||
|
.into_nodes()
|
||||||
|
.map(|(name, node)| (name.into(), node_get_type(&node)))
|
||||||
|
.collect())
|
||||||
}
|
}
|
||||||
Node::File { .. } => {
|
Node::File { .. } => {
|
||||||
// This would normally be a io::ErrorKind::NotADirectory (still unstable)
|
// This would normally be a io::ErrorKind::NotADirectory (still unstable)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue