git.delta.rocks / jrsonnet / refs/commits / bfcd43bbf560

difftreelog

feat support imports from fifo on unix

Yaroslav Bolyukin2024-03-17parent: #d78cbb7.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -9,7 +9,8 @@
 
 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,
@@ -82,6 +83,37 @@
 	}
 }
 
+/// 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>() {
@@ -95,50 +127,34 @@
 		} 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");
 		};
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -11,7 +11,9 @@
 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,
modifiedcrates/jrsonnet-parser/src/source.rsdiffbeforeafterboth
7};7};
88
9use jrsonnet_gcmodule::{Trace, Tracer};9use jrsonnet_gcmodule::{Trace, Tracer};
10use jrsonnet_interner::IStr;10use jrsonnet_interner::{IBytes, IStr};
11#[cfg(feature = "serde")]11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};12use serde::{Deserialize, Serialize};
13#[cfg(feature = "structdump")]13#[cfg(feature = "structdump")]
75/// - [`SourceFile`] - for any file75/// - [`SourceFile`] - for any file
76/// - [`SourceDirectory`] - for resolution from CWD76/// - [`SourceDirectory`] - for resolution from CWD
77/// - [`SourceVirtual`] - for stdlib/ext-str77/// - [`SourceVirtual`] - for stdlib/ext-str
78/// - [`SourceFifo`] - for /dev/fd/X (This path may appear with `jrsonnet <(command_that_produces_jsonnet)`)
78///79///
79/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained80/// From all of those, only [`SourceVirtual`] may be constructed manually, any other path kind should be only obtained
80/// from assigned `ImportResolver`81/// from assigned `ImportResolver`
254 any_ext_impl!(SourcePathT);255 any_ext_impl!(SourcePathT);
255}256}
257
258/// Represents resolved FIFO file, those files may only be read once, and this type is only used for
259/// unix, where user might want to do `jrsonnet <(command_that_produces_jsonnet_source)`
260/// In most cases, user most probably want to use `jrsonnet -` instead of `jrsonnet /dev/stdin`
261/// for better cross-platform support.
262// PartialEq is limited to ptr equality
263#[allow(clippy::derived_hash_with_manual_eq)]
264#[derive(Trace, Debug, Hash)]
265pub struct SourceFifo(pub String, pub IBytes);
266impl PartialEq for SourceFifo {
267 fn eq(&self, other: &Self) -> bool {
268 std::ptr::eq(self, other)
269 }
270}
271impl fmt::Display for SourceFifo {
272 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
273 write!(f, "fifo({:?})", self.0)
274 }
275}
276impl SourcePathT for SourceFifo {
277 fn is_default(&self) -> bool {
278 // In case of FD input, user won't expect relative paths to be resolved from /dev/fd/
279 true
280 }
281
282 fn path(&self) -> Option<&Path> {
283 None
284 }
285
286 any_ext_impl!(SourcePathT);
287}
256288
257/// Either real file, or virtual289/// Either real file, or virtual
258/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut290/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut