summaryrefslogtreecommitdiffstats
path: root/vendor/opener-0.5.2/src/windows.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:24 +0000
commit023939b627b7dc93b01471f7d41fb8553ddb4ffa (patch)
tree60fc59477c605c72b0a1051409062ddecc43f877 /vendor/opener-0.5.2/src/windows.rs
parentAdding debian version 1.72.1+dfsg1-1. (diff)
downloadrustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.tar.xz
rustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/opener-0.5.2/src/windows.rs')
-rw-r--r--vendor/opener-0.5.2/src/windows.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/vendor/opener-0.5.2/src/windows.rs b/vendor/opener-0.5.2/src/windows.rs
new file mode 100644
index 000000000..829bdb0c5
--- /dev/null
+++ b/vendor/opener-0.5.2/src/windows.rs
@@ -0,0 +1,41 @@
+use crate::OpenError;
+use std::ffi::OsStr;
+use std::os::windows::ffi::OsStrExt;
+use std::{io, ptr};
+use winapi::ctypes::c_int;
+use winapi::um::shellapi::ShellExecuteW;
+
+pub(crate) fn open(path: &OsStr) -> Result<(), OpenError> {
+ const SW_SHOW: c_int = 5;
+
+ let path = convert_path(path).map_err(OpenError::Io)?;
+ let operation: Vec<u16> = OsStr::new("open\0").encode_wide().collect();
+ let result = unsafe {
+ ShellExecuteW(
+ ptr::null_mut(),
+ operation.as_ptr(),
+ path.as_ptr(),
+ ptr::null(),
+ ptr::null(),
+ SW_SHOW,
+ )
+ };
+ if result as c_int > 32 {
+ Ok(())
+ } else {
+ Err(OpenError::Io(io::Error::last_os_error()))
+ }
+}
+
+fn convert_path(path: &OsStr) -> io::Result<Vec<u16>> {
+ let mut maybe_result: Vec<u16> = path.encode_wide().collect();
+ if maybe_result.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)
+}