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 <personal@ilanjoselevich.com> Reviewed-by: Connor Brewster <cbrewster@hey.com>
This commit is contained in:
parent
04f04cfc27
commit
080654aaf9
3 changed files with 51 additions and 0 deletions
|
@ -54,6 +54,10 @@ pub trait EvalIO {
|
||||||
/// Open the file at the specified path to a `io::Read`.
|
/// Open the file at the specified path to a `io::Read`.
|
||||||
fn open(&self, path: &Path) -> io::Result<Box<dyn io::Read>>;
|
fn open(&self, path: &Path) -> io::Result<Box<dyn io::Read>>;
|
||||||
|
|
||||||
|
/// Return the [FileType] of the given path, or an error if it doesn't
|
||||||
|
/// exist.
|
||||||
|
fn file_type(&self, path: &Path) -> io::Result<FileType>;
|
||||||
|
|
||||||
/// Read the directory at the specified path and return the names
|
/// Read the directory at the specified path and return the names
|
||||||
/// of its entries associated with their [`FileType`].
|
/// of its entries associated with their [`FileType`].
|
||||||
///
|
///
|
||||||
|
@ -101,6 +105,20 @@ impl EvalIO for StdIO {
|
||||||
Ok(Box::new(File::open(path)?))
|
Ok(Box::new(File::open(path)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn file_type(&self, path: &Path) -> io::Result<FileType> {
|
||||||
|
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<Vec<(bytes::Bytes, FileType)>> {
|
fn read_dir(&self, path: &Path) -> io::Result<Vec<(bytes::Bytes, FileType)>> {
|
||||||
let mut result = vec![];
|
let mut result = vec![];
|
||||||
|
|
||||||
|
@ -150,6 +168,13 @@ impl EvalIO for DummyIO {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn file_type(&self, _: &Path) -> io::Result<FileType> {
|
||||||
|
Err(io::Error::new(
|
||||||
|
io::ErrorKind::Unsupported,
|
||||||
|
"I/O methods are not implemented in DummyIO",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
fn read_dir(&self, _: &Path) -> io::Result<Vec<(bytes::Bytes, FileType)>> {
|
fn read_dir(&self, _: &Path) -> io::Result<Vec<(bytes::Bytes, FileType)>> {
|
||||||
Err(io::Error::new(
|
Err(io::Error::new(
|
||||||
io::ErrorKind::Unsupported,
|
io::ErrorKind::Unsupported,
|
||||||
|
|
|
@ -60,6 +60,10 @@ where
|
||||||
self.actual.as_ref().open(path)
|
self.actual.as_ref().open(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn file_type(&self, path: &Path) -> io::Result<FileType> {
|
||||||
|
self.actual.as_ref().file_type(path)
|
||||||
|
}
|
||||||
|
|
||||||
fn read_dir(&self, path: &Path) -> io::Result<Vec<(bytes::Bytes, FileType)>> {
|
fn read_dir(&self, path: &Path) -> io::Result<Vec<(bytes::Bytes, FileType)>> {
|
||||||
self.actual.as_ref().read_dir(path)
|
self.actual.as_ref().read_dir(path)
|
||||||
}
|
}
|
||||||
|
|
|
@ -531,6 +531,28 @@ impl EvalIO for TvixStoreIO {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self), ret(level = Level::TRACE), err)]
|
||||||
|
fn file_type(&self, path: &Path) -> io::Result<FileType> {
|
||||||
|
if let Ok((store_path, sub_path)) =
|
||||||
|
StorePath::from_absolute_path_full(&path.to_string_lossy())
|
||||||
|
{
|
||||||
|
if let Some(node) = self
|
||||||
|
.tokio_handle
|
||||||
|
.block_on(async { self.store_path_to_node(&store_path, &sub_path).await })?
|
||||||
|
{
|
||||||
|
match node {
|
||||||
|
Node::Directory(_) => Ok(FileType::Directory),
|
||||||
|
Node::File(_) => Ok(FileType::Regular),
|
||||||
|
Node::Symlink(_) => Ok(FileType::Symlink),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.std_io.file_type(path)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.std_io.file_type(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument(skip(self), ret(level = Level::TRACE), err)]
|
#[instrument(skip(self), ret(level = Level::TRACE), err)]
|
||||||
fn read_dir(&self, path: &Path) -> io::Result<Vec<(bytes::Bytes, FileType)>> {
|
fn read_dir(&self, path: &Path) -> io::Result<Vec<(bytes::Bytes, FileType)>> {
|
||||||
if let Ok((store_path, sub_path)) =
|
if let Ok((store_path, sub_path)) =
|
||||||
|
|
Loading…
Reference in a new issue