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:
Florian Klink 2025-06-02 17:26:23 +02:00 committed by clbot
parent 59aeeb6fe4
commit 0f9c5f0354

View file

@ -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(); // If we didn't get the directory node that's linked, that's a store inconsistency!
for (name, node) in directory.into_nodes() { error!(
children.push(match node { directory.digest = %digest,
Node::Directory { .. } => (name.into(), FileType::Directory), path = ?path,
Node::File { .. } => (name.clone().into(), FileType::Regular), "directory not found",
Node::Symlink { .. } => (name.into(), FileType::Symlink), );
}) io::Error::new(
} io::ErrorKind::NotFound,
Ok(children) format!("directory {digest} does not exist"),
} else { )
// If we didn't get the directory node that's linked, that's a store inconsistency! })?;
error!(
directory.digest = %digest, // construct children from nodes
path = ?path, Ok(directory
"directory not found", .into_nodes()
); .map(|(name, node)| (name.into(), node_get_type(&node)))
Err(io::Error::new( .collect())
io::ErrorKind::NotFound,
format!("directory {digest} does not exist"),
))?
}
} }
Node::File { .. } => { Node::File { .. } => {
// This would normally be a io::ErrorKind::NotADirectory (still unstable) // This would normally be a io::ErrorKind::NotADirectory (still unstable)