2022-12-12 15:19:27 +01:00
|
|
|
//! Interface for injecting I/O-related functionality into tvix-eval.
|
|
|
|
//!
|
|
|
|
//! The Nix language contains several builtins (e.g. `builtins.readDir`), as
|
|
|
|
//! well as language feature (e.g. string-"coercion" of paths) that interact
|
|
|
|
//! with the filesystem.
|
|
|
|
//!
|
|
|
|
//! The language evaluator implemented by this crate does not depend on any
|
|
|
|
//! particular filesystem interaction model. Instead, this module provides a
|
|
|
|
//! trait that can be implemented by tvix-eval callers to provide the
|
|
|
|
//! functionality they desire.
|
|
|
|
//!
|
|
|
|
//! In theory this can be used to implement "mocked" filesystem interactions, or
|
|
|
|
//! interaction with remote filesystems, etc.
|
|
|
|
//!
|
|
|
|
//! In the context of Nix builds, callers also use this interface to determine
|
|
|
|
//! how store paths are opened and so on.
|
|
|
|
|
2023-02-03 12:57:15 +01:00
|
|
|
use crate::errors::ErrorKind;
|
2022-12-12 18:02:39 +01:00
|
|
|
use smol_str::SmolStr;
|
2022-12-12 22:12:13 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-12-12 15:19:27 +01:00
|
|
|
|
2023-02-03 12:57:15 +01:00
|
|
|
#[cfg(feature = "impure")]
|
|
|
|
use std::rc::Rc;
|
2022-12-12 15:19:27 +01:00
|
|
|
|
2022-12-12 18:02:39 +01:00
|
|
|
/// Types of files as represented by `builtins.readDir` in Nix.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FileType {
|
|
|
|
Directory,
|
|
|
|
Regular,
|
|
|
|
Symlink,
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
2022-12-12 15:19:27 +01:00
|
|
|
/// Defines how filesystem interaction occurs inside of tvix-eval.
|
|
|
|
pub trait EvalIO {
|
2022-12-12 17:36:45 +01:00
|
|
|
/// Verify whether the file at the specified path exists.
|
2023-05-14 18:55:17 +02:00
|
|
|
fn path_exists(&mut self, path: PathBuf) -> Result<bool, ErrorKind>;
|
2022-12-12 17:36:45 +01:00
|
|
|
|
2022-12-12 15:19:27 +01:00
|
|
|
/// Read the file at the specified path to a string.
|
2023-05-14 18:55:17 +02:00
|
|
|
fn read_to_string(&mut self, path: PathBuf) -> Result<String, ErrorKind>;
|
2022-12-12 18:02:39 +01:00
|
|
|
|
|
|
|
/// Read the directory at the specified path and return the names
|
|
|
|
/// of its entries associated with their [`FileType`].
|
2023-05-14 18:55:17 +02:00
|
|
|
fn read_dir(&mut self, path: PathBuf) -> Result<Vec<(SmolStr, FileType)>, ErrorKind>;
|
2022-12-12 19:33:08 +01:00
|
|
|
|
2022-12-12 22:12:13 +01:00
|
|
|
/// Import the given path. What this means depends on the
|
|
|
|
/// implementation, for example for a `std::io`-based
|
|
|
|
/// implementation this might be a no-op, while for a Tvix store
|
|
|
|
/// this might be a copy of the given files to the store.
|
|
|
|
///
|
|
|
|
/// This is primarily used in the context of things like coercing
|
|
|
|
/// a local path to a string, or builtins like `path`.
|
2023-05-14 18:55:17 +02:00
|
|
|
fn import_path(&mut self, path: &Path) -> Result<PathBuf, ErrorKind>;
|
2022-12-12 22:12:13 +01:00
|
|
|
|
2022-12-12 19:33:08 +01:00
|
|
|
/// Returns the root of the store directory, if such a thing
|
|
|
|
/// exists in the evaluation context.
|
|
|
|
fn store_dir(&self) -> Option<String> {
|
|
|
|
None
|
|
|
|
}
|
2022-12-12 15:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of [`EvalIO`] that simply uses the equivalent
|
|
|
|
/// standard library functions, i.e. does local file-IO.
|
2022-12-12 17:10:55 +01:00
|
|
|
#[cfg(feature = "impure")]
|
2022-12-12 15:38:28 +01:00
|
|
|
pub struct StdIO;
|
2022-12-12 15:19:27 +01:00
|
|
|
|
2022-12-12 17:10:55 +01:00
|
|
|
#[cfg(feature = "impure")]
|
2022-12-12 15:19:27 +01:00
|
|
|
impl EvalIO for StdIO {
|
2023-05-14 18:55:17 +02:00
|
|
|
fn path_exists(&mut self, path: PathBuf) -> Result<bool, ErrorKind> {
|
2022-12-12 17:36:45 +01:00
|
|
|
path.try_exists().map_err(|e| ErrorKind::IO {
|
|
|
|
path: Some(path),
|
2022-12-12 18:02:39 +01:00
|
|
|
error: Rc::new(e),
|
2022-12-12 17:36:45 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-14 18:55:17 +02:00
|
|
|
fn read_to_string(&mut self, path: PathBuf) -> Result<String, ErrorKind> {
|
2022-12-12 15:19:27 +01:00
|
|
|
std::fs::read_to_string(&path).map_err(|e| ErrorKind::IO {
|
|
|
|
path: Some(path),
|
2022-12-12 18:02:39 +01:00
|
|
|
error: Rc::new(e),
|
2022-12-12 15:19:27 +01:00
|
|
|
})
|
|
|
|
}
|
2022-12-12 18:02:39 +01:00
|
|
|
|
2023-05-14 18:55:17 +02:00
|
|
|
fn read_dir(&mut self, path: PathBuf) -> Result<Vec<(SmolStr, FileType)>, ErrorKind> {
|
2022-12-12 18:02:39 +01:00
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
let mk_err = |err| ErrorKind::IO {
|
|
|
|
path: Some(path.clone()),
|
|
|
|
error: Rc::new(err),
|
|
|
|
};
|
|
|
|
|
|
|
|
for entry in path.read_dir().map_err(mk_err)? {
|
|
|
|
let entry = entry.map_err(mk_err)?;
|
|
|
|
let file_type = entry
|
|
|
|
.metadata()
|
|
|
|
.map_err(|err| ErrorKind::IO {
|
|
|
|
path: Some(entry.path()),
|
|
|
|
error: Rc::new(err),
|
|
|
|
})?
|
|
|
|
.file_type();
|
|
|
|
|
|
|
|
let val = 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
|
|
|
|
};
|
|
|
|
|
|
|
|
result.push((SmolStr::new(entry.file_name().to_string_lossy()), val));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
2022-12-12 22:12:13 +01:00
|
|
|
|
|
|
|
// this is a no-op for `std::io`, as the user can already refer to
|
|
|
|
// the path directly
|
2023-05-14 18:55:17 +02:00
|
|
|
fn import_path(&mut self, path: &Path) -> Result<PathBuf, ErrorKind> {
|
2022-12-12 22:12:13 +01:00
|
|
|
Ok(path.to_path_buf())
|
|
|
|
}
|
2022-12-12 15:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Dummy implementation of [`EvalIO`], can be used in contexts where
|
|
|
|
/// IO is not available but code should "pretend" that it is.
|
2022-12-12 15:38:28 +01:00
|
|
|
pub struct DummyIO;
|
2022-12-12 15:19:27 +01:00
|
|
|
|
|
|
|
impl EvalIO for DummyIO {
|
2023-05-14 18:55:17 +02:00
|
|
|
fn path_exists(&mut self, _: PathBuf) -> Result<bool, ErrorKind> {
|
2022-12-12 17:36:45 +01:00
|
|
|
Err(ErrorKind::NotImplemented(
|
|
|
|
"I/O methods are not implemented in DummyIO",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-05-14 18:55:17 +02:00
|
|
|
fn read_to_string(&mut self, _: PathBuf) -> Result<String, ErrorKind> {
|
2022-12-12 15:19:27 +01:00
|
|
|
Err(ErrorKind::NotImplemented(
|
|
|
|
"I/O methods are not implemented in DummyIO",
|
|
|
|
))
|
|
|
|
}
|
2022-12-12 18:02:39 +01:00
|
|
|
|
2023-05-14 18:55:17 +02:00
|
|
|
fn read_dir(&mut self, _: PathBuf) -> Result<Vec<(SmolStr, FileType)>, ErrorKind> {
|
2022-12-12 18:02:39 +01:00
|
|
|
Err(ErrorKind::NotImplemented(
|
|
|
|
"I/O methods are not implemented in DummyIO",
|
|
|
|
))
|
|
|
|
}
|
2022-12-12 22:12:13 +01:00
|
|
|
|
2023-05-14 18:55:17 +02:00
|
|
|
fn import_path(&mut self, _: &Path) -> Result<PathBuf, ErrorKind> {
|
2022-12-12 22:12:13 +01:00
|
|
|
Err(ErrorKind::NotImplemented(
|
|
|
|
"I/O methods are not implemented in DummyIO",
|
|
|
|
))
|
|
|
|
}
|
2022-12-12 15:19:27 +01:00
|
|
|
}
|