Fix resolving NFS key path on Windows

This commit is contained in:
Luke Street 2021-09-08 00:26:22 -04:00
parent 9322b8e55b
commit 50f171dcf5
2 changed files with 24 additions and 14 deletions

View File

@ -7,9 +7,9 @@ use std::{
use clap::{clap_app, AppSettings}; use clap::{clap_app, AppSettings};
use file_size; use file_size;
use nod::{ use nod::{
disc::{new_disc_base, DiscBase, PartReadStream}, disc::{new_disc_base, PartReadStream},
fst::NodeType, fst::NodeType,
io::{has_extension, new_disc_io, DiscIO}, io::{has_extension, new_disc_io},
Result, Result,
}; };

View File

@ -2,7 +2,7 @@ use std::{
fs::File, fs::File,
io, io,
io::{Read, Seek, SeekFrom}, io::{Read, Seek, SeekFrom},
path::{Path, PathBuf}, path::{Component, Path, PathBuf},
}; };
use aes::{Aes128, NewBlockCipher}; use aes::{Aes128, NewBlockCipher};
@ -247,7 +247,14 @@ impl DiscIO for DiscIONFS {
impl DiscIONFS { impl DiscIONFS {
fn get_path<P: AsRef<Path>>(&self, path: P) -> PathBuf { fn get_path<P: AsRef<Path>>(&self, path: P) -> PathBuf {
let mut buf = self.directory.clone(); let mut buf = self.directory.clone();
buf.push(path); for component in path.as_ref().components() {
match component {
Component::ParentDir => {
buf.pop();
}
_ => buf.push(component),
}
}
buf buf
} }
@ -263,25 +270,28 @@ impl DiscIONFS {
pub(crate) fn validate_files(&mut self) -> Result<()> { pub(crate) fn validate_files(&mut self) -> Result<()> {
{ {
// Load key file // Load key file
let mut key_path = self.get_path("../code/htk.bin"); let primary_key_path =
if !key_path.is_file() { self.get_path(["..", "code", "htk.bin"].iter().collect::<PathBuf>());
key_path = self.directory.clone(); let secondary_key_path = self.get_path("htk.bin");
key_path.push("htk.bin"); let mut key_path = primary_key_path.canonicalize();
if key_path.is_err() {
key_path = secondary_key_path.canonicalize();
} }
if !key_path.is_file() { if key_path.is_err() {
return Result::Err(Error::DiscFormat(format!( return Result::Err(Error::DiscFormat(format!(
"Failed to locate {} or {}", "Failed to locate {} or {}",
self.get_path("../code/htk.bin").to_string_lossy(), primary_key_path.to_string_lossy(),
key_path.to_string_lossy() secondary_key_path.to_string_lossy()
))); )));
} }
File::open(key_path.as_path()) let resolved_path = key_path.unwrap();
File::open(resolved_path.as_path())
.map_err(|v| { .map_err(|v| {
Error::Io(format!("Failed to open {}", key_path.to_string_lossy()), v) Error::Io(format!("Failed to open {}", resolved_path.to_string_lossy()), v)
})? })?
.read(&mut self.key) .read(&mut self.key)
.map_err(|v| { .map_err(|v| {
Error::Io(format!("Failed to read {}", key_path.to_string_lossy()), v) Error::Io(format!("Failed to read {}", resolved_path.to_string_lossy()), v)
})?; })?;
} }
{ {