jannie

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 47abb7c4938f19c4e3db44feb882a50af6ff29aa
parent 0a26bea3ce97010f808d4ff772e988d3ccf40499
Author: Andy Khramtsov <>
Date:   Thu, 23 Apr 2026 18:57:12 +0300

fix: don't crash on process path

Diffstat:
Msrc/lib.rs | 6+++---
Msrc/path_processing.rs | 12++++++++++++
Msrc/state.rs | 43++++++++++++++++++++++++++++++++++++-------
3 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs @@ -25,7 +25,7 @@ pub mod state; /// `main`, but returns [`Result`]. pub fn result_main() -> Result<(), Error> { let args = Args::parse(); - let dir = path_processing::process_path(&args.dir)?; + let dir = path_processing::process_path(&args.dir).map_err(Error::ProcessDirPath)?; let config_path = dir.join("config.yaml"); let config = Config::new(&config_path)?; @@ -262,8 +262,8 @@ pub enum Error { Config(#[from] config::Error), #[error(transparent)] State(#[from] state::Error), - #[error(transparent)] - ProcessPath(#[from] path_processing::Error), + #[error("Error processing dir path: {0}")] + ProcessDirPath(path_processing::Error), #[error("Error setting up logging: {0}")] Logging(#[from] logging::Error), #[error("Error building filetree: {0}")] diff --git a/src/path_processing.rs b/src/path_processing.rs @@ -31,3 +31,15 @@ pub enum Error { #[error("Error canonicalizing path: {0}")] Canonicalize(std::io::Error), } + +impl Error { + pub fn is_file_not_found(&self) -> bool { + match *self { + Error::Canonicalize(ref error) => match error.kind() { + std::io::ErrorKind::NotFound => true, + _ => false, + }, + _ => false, + } + } +} diff --git a/src/state.rs b/src/state.rs @@ -35,9 +35,13 @@ impl State { InventoryFormat::Json => config_dir.join("inventory.json"), }; + tracing::trace!("Reading inventory from {inventory_path:?}"); + let inventory = runtime .block_on(tokio::fs::read(inventory_path)) - .map_err(Error::ReadFile)?; + .map_err(Error::ReadInventoryFile)?; + + tracing::trace!("Parsing inventory file contents"); let inventory: Inventory = match config.inventory_format { @@ -47,19 +51,33 @@ impl State { .map_err(Error::ReadInventoryJson)?, }; - let root = path_processing::process_path(&config.root)?; + tracing::trace!("Processing root path"); + + let root = path_processing::process_path(&config.root).map_err(Error::ProcessRootPath)?; let mut filters = Vec::new(); + tracing::debug!("Preprocessing filters"); + for filter in &config.filters { + tracing::trace!("Preprocessing filter {filter:?}"); let mut path = config.root.clone(); path.push(&filter.path); filters.push(Filter { - path: path_processing::process_path(&path)?, + path: match path_processing::process_path(&path) { + Ok(path) => path, + Err(error) if error.is_file_not_found() => { + tracing::warn!("Filter path was not found: {filter:?}"); + continue; + } + Err(error) => Err(Error::ProcessPath(error))?, + }, filter_type: filter.filter_type.clone(), }); } + tracing::debug!("Building trees from filters"); + let mut blacklist_tree = Filetree::new(Node::new("/".into(), ())); let mut whitelist_tree = Filetree::new(Node::new("/".into(), ())); @@ -86,9 +104,18 @@ impl State { } } + tracing::debug!("Building inventory tree"); + let mut inventory_tree = Filetree::new(Node::new("/".into(), None)); for item in &inventory.items { - let path = path_processing::process_path(&item.path)?; + let path = match path_processing::process_path(&item.path) { + Ok(path) => path, + Err(error) if error.is_file_not_found() => { + tracing::warn!("Inventory item was not found: {item:?}"); + continue; + } + Err(error) => Err(Error::ProcessPath(error))?, + }; let mut components = path.components(); let mut path_buf = PathBuf::new(); loop { @@ -152,10 +179,12 @@ fn add_to_tree(tree: &mut Filetree<()>, path: &Path) -> Result<(), Error> { pub enum Error { #[error("Error building runtime: {0}")] Runtime(tokio::io::Error), - #[error("Error reading file: {0}")] - ReadFile(std::io::Error), + #[error("Error reading inventory file: {0}")] + ReadInventoryFile(std::io::Error), #[error("Error processing path: {0}")] - ProcessPath(#[from] path_processing::Error), + ProcessPath(path_processing::Error), + #[error("Error processing root path: {0}")] + ProcessRootPath(path_processing::Error), #[error("Error parsing yaml inventory: {0}")] ReadInventoryYaml(serde_yaml::Error), #[error("Error parsing json inventory: {0}")]