From 74b1d255491282bac6f96b4c83e49505dd289c0d Mon Sep 17 00:00:00 2001 From: "Yubing Dong (Tom)" Date: Tue, 1 Feb 2022 00:48:51 -0800 Subject: [PATCH] Don't check file type before attempting to load a tree This fixes #38 --- src/model/mod.rs | 49 +++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/model/mod.rs b/src/model/mod.rs index 158ac84..b4c442f 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -170,10 +170,11 @@ impl Model { /// Deserialize model from the given directory. pub fn load>(dir_path: P) -> io::Result { - info!("Loading model..."); let start_t = time::Instant::now(); let dir_path = dir_path.as_ref(); + info!("Loading model from {}...", dir_path.display()); + let settings = { let settings_path = dir_path.join(MODEL_SETTINGS_FILE_NAME); info!("Loading model settings from {}...", settings_path.display()); @@ -185,29 +186,35 @@ impl Model { let mut trees = Vec::::new(); for entry in dir_path.read_dir()? { let entry = entry?; - if entry.file_type()?.is_file() { + if { let file_name = entry.file_name(); let file_name_str = file_name.to_string_lossy(); - if file_name_str.starts_with(TREE_FILE_NAME_PREFIX) - && file_name_str.ends_with(".cbor") - { - info!("Loading tree from {}...", entry.path().display()); - let reader = std::io::BufReader::new(std::fs::File::open(entry.path())?); - let tree: TreeNode = serde_cbor::from_reader(reader).map_err(|e| { - io::Error::new( - io::ErrorKind::InvalidData, - format!("Unable to deserialize tree: {}", e), - ) - })?; - if !tree.is_valid(settings) { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - format!("Tree loaded from {} is invalid", file_name_str), - )); - } - trees.push(tree); - } + !(file_name_str.starts_with(TREE_FILE_NAME_PREFIX) + && file_name_str.ends_with(".cbor")) + } { + continue; + } + + let tree_path = entry.path(); + info!("Loading tree from {}...", tree_path.display()); + let reader = std::io::BufReader::new(std::fs::File::open(tree_path.as_path())?); + let tree: TreeNode = serde_cbor::from_reader(reader).map_err(|e| { + io::Error::new( + io::ErrorKind::InvalidData, + format!( + "Unable to deserialize tree from {} with error: {}", + tree_path.display(), + e + ), + ) + })?; + if !tree.is_valid(settings) { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + format!("Tree loaded from {} is invalid", tree_path.display()), + )); } + trees.push(tree); } info!(