Skip to content

Commit

Permalink
Improve open()'s Windows path handling
Browse files Browse the repository at this point in the history
Forward slash separators in relative paths are now accepted.
  • Loading branch information
Seeker14491 committed Mar 20, 2023
1 parent 5ddb52d commit 3b48ee0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions opener/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dbus = { version = "0.9", optional = true, features = ["vendored"] }
url = { version = "2", optional = true }

[target.'cfg(windows)'.dependencies]
normpath = "1"
winapi = { version = "0.3", features = ["shellapi"] }
dunce = { version = "1", optional = true }

Expand Down
16 changes: 16 additions & 0 deletions opener/src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::OpenError;
use normpath::PathExt;
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::path::PathBuf;
use std::{io, ptr};
use winapi::ctypes::c_int;
use winapi::um::shellapi::ShellExecuteW;
Expand All @@ -11,6 +13,20 @@ mod reveal;
pub(crate) use self::reveal::reveal;

pub(crate) fn open(path: &OsStr) -> Result<(), OpenError> {
let Err(first_error) = open_helper(path) else {
return Ok(());
};

match PathBuf::from(path).normalize() {
Ok(normalized) => match open_helper(normalized.as_os_str()) {
Ok(()) => Ok(()),
Err(_second_error) => Err(first_error),
},
Err(_) => Err(first_error),
}
}

pub(crate) fn open_helper(path: &OsStr) -> Result<(), OpenError> {
const SW_SHOW: c_int = 5;

let path = convert_path(path).map_err(OpenError::Io)?;
Expand Down

0 comments on commit 3b48ee0

Please sign in to comment.