From eba022bc100f4e00a8748a3e6f0655104db2f128 Mon Sep 17 00:00:00 2001 From: Jaby Date: Mon, 6 May 2024 19:53:57 +0200 Subject: [PATCH] Support switching file pathes to wsl if file can not be located --- src/Tools/tool_helper/Cargo.toml | 5 +++-- src/Tools/tool_helper/src/lib.rs | 24 +++++++++++++++++++----- src/Tools/wslpath/Cargo.toml | 2 +- src/Tools/wslpath/src/lib.rs | 12 ++++++++++-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/Tools/tool_helper/Cargo.toml b/src/Tools/tool_helper/Cargo.toml index 9d9b7913..349bb2c6 100644 --- a/src/Tools/tool_helper/Cargo.toml +++ b/src/Tools/tool_helper/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tool_helper" -version = "0.9.2" +version = "0.9.5" edition = "2021" [profile.release] @@ -12,4 +12,5 @@ cdtypes = {path = "../cdtypes"} colored = "2.0.4" envmnt = "0.10.4" lz4 = "1.24.0" -paste = "1.0.14" \ No newline at end of file +paste = "1.0.14" +wslpath = {path = "../wslpath"} \ No newline at end of file diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 43f51930..6d2cb102 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -1,6 +1,6 @@ use colored::*; use envmnt::{ExpandOptions, ExpansionType}; -use std::{boxed::Box, io::{BufRead, BufReader, BufWriter, Read, Write}, path::PathBuf}; +use std::{boxed::Box, io::{BufRead, BufReader, BufWriter, Read, Write}, path::PathBuf, str::FromStr}; pub mod bits; pub mod compress; @@ -173,7 +173,7 @@ pub fn path_with_env_from(path: &str) -> PathBuf { } pub fn open_output_file(output_path: &PathBuf) -> Result, Error> { - Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?)) + Ok(std::io::BufWriter::new(std::fs::File::create(win_or_wsl_path(output_path)?)?)) } pub fn open_output(output_file: &Option) -> Result { @@ -191,7 +191,7 @@ pub fn open_input(input_file: Option) -> Result { } pub fn open_input_file_buffered(input_path: &PathBuf) -> Result { - Ok(BufReader::new(std::fs::File::open(input_path)?)) + Ok(BufReader::new(std::fs::File::open(win_or_wsl_path(input_path)?)?)) } pub fn os_str_to_string(input: &std::ffi::OsStr, name: &str) -> Result { @@ -221,14 +221,14 @@ pub fn read_file(file_path: &PathBuf) -> Result, Error> { } } - match std::fs::read(file_path) { + match std::fs::read(win_or_wsl_path(file_path)?) { Ok(data) => Ok(data), Err(error) => create_file_read_error(file_path, error), } } pub fn read_file_to_string(file_path: &PathBuf) -> Result { - match std::fs::read_to_string(file_path) { + match std::fs::read_to_string(win_or_wsl_path(file_path)?) { Ok(string) => Ok(string), Err(error) => create_file_read_error(file_path, error), } @@ -236,4 +236,18 @@ pub fn read_file_to_string(file_path: &PathBuf) -> Result { fn create_file_read_error(file_path: &PathBuf, error: std::io::Error) -> Result { Err(Error::from_text(format!("Failed reading file {} with error: \"{}\"", file_path.display(), error))) +} + +fn win_or_wsl_path(path: &PathBuf) -> Result { + let path = path.clone(); + if path.exists() { + Ok(path) + } + + else { + match path.into_os_string().into_string() { + Ok(path) => Ok(PathBuf::from_str(wslpath::force_convert(path).as_str())?), + Err(error) => Err(Error::from_text(format!("Failed converting {:?} to string for win/wsl mapping", error))) + } + } } \ No newline at end of file diff --git a/src/Tools/wslpath/Cargo.toml b/src/Tools/wslpath/Cargo.toml index 41248a7b..4c27bd2b 100644 --- a/src/Tools/wslpath/Cargo.toml +++ b/src/Tools/wslpath/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wslpath" -version = "1.0.0" +version = "1.1.0" edition = "2021" [profile.release] diff --git a/src/Tools/wslpath/src/lib.rs b/src/Tools/wslpath/src/lib.rs index 66402776..db73cbc7 100644 --- a/src/Tools/wslpath/src/lib.rs +++ b/src/Tools/wslpath/src/lib.rs @@ -2,12 +2,15 @@ pub fn convert(path: String) -> String { replace_drive_letter(convert_slashes(path)) } +pub fn force_convert(path: String) -> String { + force_replace_drive_letter(convert_slashes(path)) +} + fn convert_slashes(path: String) -> String { path.replace('\\', "/") } -#[cfg(target_os = "windows")] -fn replace_drive_letter(mut path: String) -> String { +fn force_replace_drive_letter(mut path: String) -> String { let has_drive_letter = { let drive_letter = path.get(0..2); @@ -39,6 +42,11 @@ fn replace_drive_letter(mut path: String) -> String { path } +#[cfg(target_os = "windows")] +fn replace_drive_letter(path: String) -> String { + force_replace_drive_letter(path) +} + #[cfg(not(target_os = "windows"))] fn replace_drive_letter(path: String) -> String { path