Skip to content

Commit

Permalink
Don't check file type before attempting to load a tree
Browse files Browse the repository at this point in the history
This fixes #38
  • Loading branch information
tomtung committed Feb 1, 2022
1 parent 8f7420f commit 74b1d25
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ impl Model {

/// Deserialize model from the given directory.
pub fn load<P: AsRef<std::path::Path>>(dir_path: P) -> io::Result<Self> {
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());
Expand All @@ -185,29 +186,35 @@ impl Model {
let mut trees = Vec::<TreeNode>::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!(
Expand Down

0 comments on commit 74b1d25

Please sign in to comment.