Support switching file pathes to wsl if file can not be located

This commit is contained in:
Jaby 2024-05-06 19:53:57 +02:00
parent 4d5a7814f7
commit eba022bc10
4 changed files with 33 additions and 10 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "tool_helper"
version = "0.9.2"
version = "0.9.5"
edition = "2021"
[profile.release]
@ -13,3 +13,4 @@ colored = "2.0.4"
envmnt = "0.10.4"
lz4 = "1.24.0"
paste = "1.0.14"
wslpath = {path = "../wslpath"}

View File

@ -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<BufWriter<std::fs::File>, 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<PathBuf>) -> Result<Output, Error> {
@ -191,7 +191,7 @@ pub fn open_input(input_file: Option<PathBuf>) -> Result<Input, Error> {
}
pub fn open_input_file_buffered(input_path: &PathBuf) -> Result<BufferedInputFile, Error> {
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<String, Error> {
@ -221,14 +221,14 @@ pub fn read_file(file_path: &PathBuf) -> Result<Vec<u8>, 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<String, Error> {
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),
}
@ -237,3 +237,17 @@ pub fn read_file_to_string(file_path: &PathBuf) -> Result<String, Error> {
fn create_file_read_error<T>(file_path: &PathBuf, error: std::io::Error) -> Result<T, Error> {
Err(Error::from_text(format!("Failed reading file {} with error: \"{}\"", file_path.display(), error)))
}
fn win_or_wsl_path(path: &PathBuf) -> Result<PathBuf, Error> {
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)))
}
}
}

View File

@ -1,6 +1,6 @@
[package]
name = "wslpath"
version = "1.0.0"
version = "1.1.0"
edition = "2021"
[profile.release]

View File

@ -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