From a11b8764f1be5124b5a81a39809cfeb520f4a96d Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Tue, 21 Jan 2025 16:36:33 -0600 Subject: strip verbatim prefix \\?\ from paths on windows without this, java freaks out (amended to remove an artifact, arguments.txt) --- src/util.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index a7c2d5e..7927620 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,11 +1,13 @@ use std::error::Error; +use std::ffi::OsStr; use std::fmt::{Display, Formatter}; use std::io::ErrorKind; -use std::path::{Component, Path, PathBuf}; +use std::path::{Component, Path, PathBuf, Prefix}; use log::debug; use sha1_smol::{Digest, Sha1}; use tokio::fs::File; use tokio::io::AsyncReadExt; +use crate::util; #[derive(Debug)] pub enum IntegrityError { @@ -128,3 +130,52 @@ pub fn check_path(name: &str) -> Result<&Path, &'static str> { Ok(entry_path) } + +#[cfg(windows)] +pub fn strip_verbatim(path: &Path) -> &Path { + let Some(Component::Prefix(p)) = path.components().next() else { + return path; + }; + + match p.kind() { + Prefix::VerbatimDisk(_) => + Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(&path.as_os_str().as_encoded_bytes()[4..]) }), + _ => path + } +} + +#[cfg(not(windows))] +pub fn strip_verbatim(path: &Path) -> &Path { + path +} + +pub trait AsJavaPath { + fn as_java_path(&self) -> &Path; +} + +impl AsJavaPath for Path { + fn as_java_path(&self) -> &Path { + strip_verbatim(self) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + #[cfg(windows)] + fn test_strip_verbatim() { + let path = Path::new(r"\\?\C:\Some\Verbatim\Path"); + match path.components().next().unwrap() { + Component::Prefix(p) => assert!(matches!(p.kind(), Prefix::VerbatimDisk(_)), "(TEST BUG) path does not start with verbatim disk"), + _ => panic!("(TEST BUG) path does not start with prefix") + } + + let path2 = path.as_java_path(); + match path2.components().next().unwrap() { + Component::Prefix(p) => assert!(matches!(p.kind(), Prefix::Disk(_))), + _ => panic!("path does not begin with prefix") + } + } +} -- cgit v1.2.3-70-g09d2