Skip to content

Commit

Permalink
fix: quote paths on windows to allow spaces in paths not be treated a…
Browse files Browse the repository at this point in the history
…s multiple paths.

Note that paths that are already quoted will also be quoted, as the
current quoting implementation is unconditional.
  • Loading branch information
Byron committed Sep 16, 2022
2 parents b20e01c + e0d5968 commit 4c0fdb3
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
use std::{ffi::OsStr, io, os::windows::ffi::OsStrExt, ptr};
use std::{
ffi::{OsStr, OsString},
io,
os::windows::ffi::OsStrExt,
ptr,
};

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

use crate::IntoResult;

fn convert_path(path: &OsStr) -> io::Result<Vec<u16>> {
let mut maybe_result: Vec<_> = path.encode_wide().collect();
if maybe_result.iter().any(|&u| u == 0) {
let mut quoted_path = OsString::with_capacity(path.len());

// Surround path with double quotes "" to handle spaces in path.
quoted_path.push("\"");
quoted_path.push(&path);
quoted_path.push("\"");

let mut wide_chars: Vec<_> = quoted_path.encode_wide().collect();
if wide_chars.iter().any(|&u| u == 0) {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"path contains NUL byte(s)",
));
}
maybe_result.push(0);
Ok(maybe_result)
wide_chars.push(0);
Ok(wide_chars)
}

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

0 comments on commit 4c0fdb3

Please sign in to comment.