From 080654aaf9bd2f94d634008afd1dc26c74752eec Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Tue, 25 Jun 2024 20:37:43 +0300 Subject: feat(tvix/eval): add file_type method This allows peeking of the type at a given path. It's necessary, as an open() might not fail until you try to read() from it, and generally, stat'ing can be faster in some cases. Change-Id: Ib002da3194a3546ca286de49aac8d1022ec5560f Reviewed-on: https://cl.tvl.fyi/c/depot/+/11871 Tested-by: BuildkiteCI Reviewed-by: Ilan Joselevich Reviewed-by: Connor Brewster --- tvix/eval/src/io.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/io.rs b/tvix/eval/src/io.rs index 58586030aa08..9acfd6eeba02 100644 --- a/tvix/eval/src/io.rs +++ b/tvix/eval/src/io.rs @@ -54,6 +54,10 @@ pub trait EvalIO { /// Open the file at the specified path to a `io::Read`. fn open(&self, path: &Path) -> io::Result>; + /// Return the [FileType] of the given path, or an error if it doesn't + /// exist. + fn file_type(&self, path: &Path) -> io::Result; + /// Read the directory at the specified path and return the names /// of its entries associated with their [`FileType`]. /// @@ -101,6 +105,20 @@ impl EvalIO for StdIO { Ok(Box::new(File::open(path)?)) } + fn file_type(&self, path: &Path) -> io::Result { + let file_type = File::open(path)?.metadata()?.file_type(); + + Ok(if file_type.is_dir() { + FileType::Directory + } else if file_type.is_file() { + FileType::Regular + } else if file_type.is_symlink() { + FileType::Symlink + } else { + FileType::Unknown + }) + } + fn read_dir(&self, path: &Path) -> io::Result> { let mut result = vec![]; @@ -150,6 +168,13 @@ impl EvalIO for DummyIO { )) } + fn file_type(&self, _: &Path) -> io::Result { + Err(io::Error::new( + io::ErrorKind::Unsupported, + "I/O methods are not implemented in DummyIO", + )) + } + fn read_dir(&self, _: &Path) -> io::Result> { Err(io::Error::new( io::ErrorKind::Unsupported, -- cgit 1.4.1