Skip to content

Commit 4c0fdb3

Browse files
committed
fix: quote paths on windows to allow spaces in paths not be treated as multiple paths.
Note that paths that are already quoted will also be quoted, as the current quoting implementation is unconditional.
2 parents b20e01c + e0d5968 commit 4c0fdb3

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/windows.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
use std::{ffi::OsStr, io, os::windows::ffi::OsStrExt, ptr};
1+
use std::{
2+
ffi::{OsStr, OsString},
3+
io,
4+
os::windows::ffi::OsStrExt,
5+
ptr,
6+
};
27

38
use std::os::raw::c_int;
49
use windows_sys::Win32::UI::Shell::ShellExecuteW;
510

611
use crate::IntoResult;
712

813
fn convert_path(path: &OsStr) -> io::Result<Vec<u16>> {
9-
let mut maybe_result: Vec<_> = path.encode_wide().collect();
10-
if maybe_result.iter().any(|&u| u == 0) {
14+
let mut quoted_path = OsString::with_capacity(path.len());
15+
16+
// Surround path with double quotes "" to handle spaces in path.
17+
quoted_path.push("\"");
18+
quoted_path.push(&path);
19+
quoted_path.push("\"");
20+
21+
let mut wide_chars: Vec<_> = quoted_path.encode_wide().collect();
22+
if wide_chars.iter().any(|&u| u == 0) {
1123
return Err(io::Error::new(
1224
io::ErrorKind::InvalidInput,
1325
"path contains NUL byte(s)",
1426
));
1527
}
16-
maybe_result.push(0);
17-
Ok(maybe_result)
28+
wide_chars.push(0);
29+
Ok(wide_chars)
1830
}
1931

2032
pub fn that<T: AsRef<OsStr>>(path: T) -> io::Result<()> {

0 commit comments

Comments
 (0)