diff options
| author | 2025-01-21 16:36:33 -0600 | |
|---|---|---|
| committer | 2025-01-21 17:07:07 -0600 | |
| commit | a11b8764f1be5124b5a81a39809cfeb520f4a96d (patch) | |
| tree | 7465f8c1c4913bcb668b66eb5f8230cee59211e1 /src/util.rs | |
| parent | random small changes (diff) | |
strip verbatim prefix \\?\ from paths on windows
without this, java freaks out
(amended to remove an artifact, arguments.txt)
Diffstat (limited to 'src/util.rs')
| -rw-r--r-- | src/util.rs | 53 |
1 files changed, 52 insertions, 1 deletions
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") + } + } +} |
