Skip to content

Commit

Permalink
feat: support imports from fifo on unix
Browse files Browse the repository at this point in the history
  • Loading branch information
CertainLach committed Mar 17, 2024
1 parent d78cbb7 commit bfcd43b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 35 deletions.
82 changes: 49 additions & 33 deletions crates/jrsonnet-evaluator/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use std::{

use fs::File;
use jrsonnet_gcmodule::Trace;
use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath, SourceFifo};

use crate::{
bail,
Expand Down Expand Up @@ -82,6 +83,37 @@ impl FileImportResolver {
}
}

/// Create `SourcePath` from path, handling directories/Fifo files (on unix)/etc
fn check_path(path: &Path) -> Result<Option<SourcePath>> {
let meta = match fs::metadata(path) {
Ok(v) => v,
Err(e) if e.kind() == ErrorKind::NotFound => {
return Ok(None);
}
Err(e) => bail!(ImportIo(e.to_string())),
};
let ty = meta.file_type();
if ty.is_file() {
return Ok(Some(SourcePath::new(SourceFile::new(
path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
))));
}
let ty = meta.file_type();
#[cfg(unix)]
{
use std::os::unix::fs::FileTypeExt;
if ty.is_fifo() {
let file = fs::read(path).map_err(|e| ImportIo(format!("FIFO read failed: {e}")))?;
return Ok(Some(SourcePath::new(SourceFifo(
format!("{}", path.display()),
IBytes::from(file.as_slice()),
))));
}
}
// Block device/some other magic thing.
Err(RuntimeError("special file can't be imported".into()).into())
}

impl ImportResolver for FileImportResolver {
fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
Expand All @@ -95,50 +127,34 @@ impl ImportResolver for FileImportResolver {
} else {
unreachable!("resolver can't return this path")
};

direct.push(path);
if direct.is_file() {
Ok(SourcePath::new(SourceFile::new(
direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
)))
} else {
for library_path in self.library_paths.borrow().iter() {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
return Ok(SourcePath::new(SourceFile::new(
cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
)));
}
if let Some(direct) = check_path(&direct)? {
return Ok(direct);
}
for library_path in self.library_paths.borrow().iter() {
let mut cloned = library_path.clone();
cloned.push(path);
if let Some(cloned) = check_path(&cloned)? {
return Ok(cloned);
}
bail!(ImportFileNotFound(from.clone(), path.to_owned()))
}
bail!(ImportFileNotFound(from.clone(), path.to_owned()))
}
fn resolve(&self, path: &Path) -> Result<SourcePath> {
let meta = match fs::metadata(path) {
Ok(v) => v,
Err(e) if e.kind() == ErrorKind::NotFound => {
bail!(AbsoluteImportFileNotFound(path.to_owned()))
}
Err(e) => bail!(ImportIo(e.to_string())),
let Some(source) = check_path(path)? else {
bail!(AbsoluteImportFileNotFound(path.to_owned()))
};
if meta.is_file() {
Ok(SourcePath::new(SourceFile::new(
path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
)))
} else if meta.is_dir() {
Ok(SourcePath::new(SourceDirectory::new(
path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,
)))
} else {
unreachable!("this can't be a symlink")
}
Ok(source)
}

fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
let path = if let Some(f) = id.downcast_ref::<SourceFile>() {
f.path()
} else if id.downcast_ref::<SourceDirectory>().is_some() || id.is_default() {
} else if id.downcast_ref::<SourceDirectory>().is_some() {
bail!(ImportIsADirectory(id.clone()))
} else if let Some(f) = id.downcast_ref::<SourceFifo>() {
return Ok(f.1.to_vec());
} else {
unreachable!("other types are not supported in resolve");
};
Expand Down
4 changes: 3 additions & 1 deletion crates/jrsonnet-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ mod location;
mod source;
mod unescape;
pub use location::CodeLocation;
pub use source::{Source, SourceDirectory, SourceFile, SourcePath, SourcePathT, SourceVirtual};
pub use source::{
Source, SourceDirectory, SourceFifo, SourceFile, SourcePath, SourcePathT, SourceVirtual,
};

pub struct ParserSettings {
pub source: Source,
Expand Down
34 changes: 33 additions & 1 deletion crates/jrsonnet-parser/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
};

use jrsonnet_gcmodule::{Trace, Tracer};
use jrsonnet_interner::IStr;
use jrsonnet_interner::{IBytes, IStr};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "structdump")]
Expand Down Expand Up @@ -75,6 +75,7 @@ any_ext!(SourcePathT);
/// - [`SourceFile`] - for any file
/// - [`SourceDirectory`] - for resolution from CWD
/// - [`SourceVirtual`] - for stdlib/ext-str
/// - [`SourceFifo`] - for /dev/fd/X (This path may appear with `jrsonnet <(command_that_produces_jsonnet)`)
///
/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained
/// from assigned `ImportResolver`
Expand Down Expand Up @@ -254,6 +255,37 @@ impl SourcePathT for SourceVirtual {
any_ext_impl!(SourcePathT);
}

/// Represents resolved FIFO file, those files may only be read once, and this type is only used for
/// unix, where user might want to do `jrsonnet <(command_that_produces_jsonnet_source)`
/// In most cases, user most probably want to use `jrsonnet -` instead of `jrsonnet /dev/stdin`
/// for better cross-platform support.
// PartialEq is limited to ptr equality
#[allow(clippy::derived_hash_with_manual_eq)]
#[derive(Trace, Debug, Hash)]
pub struct SourceFifo(pub String, pub IBytes);
impl PartialEq for SourceFifo {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
}
impl fmt::Display for SourceFifo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "fifo({:?})", self.0)
}
}
impl SourcePathT for SourceFifo {
fn is_default(&self) -> bool {
// In case of FD input, user won't expect relative paths to be resolved from /dev/fd/
true
}

fn path(&self) -> Option<&Path> {
None
}

any_ext_impl!(SourcePathT);
}

/// Either real file, or virtual
/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut
#[cfg_attr(feature = "structdump", derive(Codegen))]
Expand Down

0 comments on commit bfcd43b

Please sign in to comment.