difftreelog
feat support imports from fifo on unix
in: master
3 files changed
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth9910use fs::File;10use fs::File;11use jrsonnet_gcmodule::Trace;11use jrsonnet_gcmodule::Trace;12use jrsonnet_interner::IBytes;12use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};13use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath, SourceFifo};131414use crate::{15use crate::{15 bail,16 bail,82 }83 }83}84}8586/// Create `SourcePath` from path, handling directories/Fifo files (on unix)/etc87fn check_path(path: &Path) -> Result<Option<SourcePath>> {88 let meta = match fs::metadata(path) {89 Ok(v) => v,90 Err(e) if e.kind() == ErrorKind::NotFound => {91 return Ok(None);92 }93 Err(e) => bail!(ImportIo(e.to_string())),94 };95 let ty = meta.file_type();96 if ty.is_file() {97 return Ok(Some(SourcePath::new(SourceFile::new(98 path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,99 ))));100 }101 let ty = meta.file_type();102 #[cfg(unix)]103 {104 use std::os::unix::fs::FileTypeExt;105 if ty.is_fifo() {106 let file = fs::read(path).map_err(|e| ImportIo(format!("FIFO read failed: {e}")))?;107 return Ok(Some(SourcePath::new(SourceFifo(108 format!("{}", path.display()),109 IBytes::from(file.as_slice()),110 ))));111 }112 }113 // Block device/some other magic thing.114 Err(RuntimeError("special file can't be imported".into()).into())115}8411685impl ImportResolver for FileImportResolver {117impl ImportResolver for FileImportResolver {86 fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {118 fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {97 };129 };13098 direct.push(path);131 direct.push(path);99 if direct.is_file() {132 if let Some(direct) = check_path(&direct)? {100 Ok(SourcePath::new(SourceFile::new(133 return Ok(direct);101 direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?,134 }102 )))103 } else {104 for library_path in self.library_paths.borrow().iter() {135 for library_path in self.library_paths.borrow().iter() {105 let mut cloned = library_path.clone();136 let mut cloned = library_path.clone();106 cloned.push(path);137 cloned.push(path);107 if cloned.exists() {138 if let Some(cloned) = check_path(&cloned)? {108 return Ok(SourcePath::new(SourceFile::new(139 return Ok(cloned);109 cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?,110 )));111 }140 }112 }141 }113 bail!(ImportFileNotFound(from.clone(), path.to_owned()))142 bail!(ImportFileNotFound(from.clone(), path.to_owned()))114 }115 }143 }116 fn resolve(&self, path: &Path) -> Result<SourcePath> {144 fn resolve(&self, path: &Path) -> Result<SourcePath> {117 let meta = match fs::metadata(path) {145 let Some(source) = check_path(path)? else {118 Ok(v) => v,119 Err(e) if e.kind() == ErrorKind::NotFound => {120 bail!(AbsoluteImportFileNotFound(path.to_owned()))146 bail!(AbsoluteImportFileNotFound(path.to_owned()))121 }147 };122 Err(e) => bail!(ImportIo(e.to_string())),123 };124 if meta.is_file() {125 Ok(SourcePath::new(SourceFile::new(148 Ok(source)126 path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,127 )))128 } else if meta.is_dir() {129 Ok(SourcePath::new(SourceDirectory::new(130 path.canonicalize().map_err(|e| ImportIo(e.to_string()))?,131 )))132 } else {133 unreachable!("this can't be a symlink")134 }135 }149 }136150137 fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {151 fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {138 let path = if let Some(f) = id.downcast_ref::<SourceFile>() {152 let path = if let Some(f) = id.downcast_ref::<SourceFile>() {139 f.path()153 f.path()140 } else if id.downcast_ref::<SourceDirectory>().is_some() || id.is_default() {154 } else if id.downcast_ref::<SourceDirectory>().is_some() {141 bail!(ImportIsADirectory(id.clone()))155 bail!(ImportIsADirectory(id.clone()))142 } else {156 } else if let Some(f) = id.downcast_ref::<SourceFifo>() {157 return Ok(f.1.to_vec());158 } else {143 unreachable!("other types are not supported in resolve");159 unreachable!("other types are not supported in resolve");144 };160 };145 let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;161 let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth12mod unescape;12mod unescape;13pub use location::CodeLocation;13pub use location::CodeLocation;14pub use source::{Source, SourceDirectory, SourceFile, SourcePath, SourcePathT, SourceVirtual};14pub use source::{15 Source, SourceDirectory, SourceFifo, SourceFile, SourcePath, SourcePathT, SourceVirtual,16};151716pub struct ParserSettings {18pub struct ParserSettings {crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth7};7};889use 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 file76/// - [`SourceDirectory`] - for resolution from CWD76/// - [`SourceDirectory`] - for resolution from CWD77/// - [`SourceVirtual`] - for stdlib/ext-str77/// - [`SourceVirtual`] - for stdlib/ext-str78/// - [`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 obtained80/// from assigned `ImportResolver`81/// from assigned `ImportResolver`254 any_ext_impl!(SourcePathT);255 any_ext_impl!(SourcePathT);255}256}257258/// Represents resolved FIFO file, those files may only be read once, and this type is only used for259/// 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 equality263#[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 true280 }281282 fn path(&self) -> Option<&Path> {283 None284 }285286 any_ext_impl!(SourcePathT);287}256288257/// Either real file, or virtual289/// Either real file, or virtual258/// 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