Skip to content

Commit 73eb2df

Browse files
konstinGankra
andauthored
Remove winsafe (#13779)
We've been using a number of different winapi crates. This PR removes winsafe in favor of the official windows-* crates, so all of uv's own winapi calls go through the official windows-* crates. --------- Co-authored-by: Aria Desires <[email protected]>
1 parent 459c902 commit 73eb2df

File tree

4 files changed

+141
-20
lines changed

4 files changed

+141
-20
lines changed

Cargo.lock

Lines changed: 115 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ url = { version = "2.5.2", features = ["serde"] }
184184
version-ranges = { git = "https://github.com/astral-sh/pubgrub", rev = "06ec5a5f59ffaeb6cf5079c6cb184467da06c9db" }
185185
walkdir = { version = "2.5.0" }
186186
which = { version = "7.0.0", features = ["regex"] }
187+
windows = { version = "0.59.0", features = ["Win32_Storage_FileSystem"] }
188+
windows-core = { version = "0.59.0" }
187189
windows-registry = { version = "0.5.0" }
188190
windows-result = { version = "0.3.0" }
189191
windows-sys = { version = "0.59.0", features = ["Win32_Foundation", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Ioctl", "Win32_System_IO", "Win32_System_Registry"] }
190-
winsafe = { version = "0.0.24", features = ["kernel"] }
191192
wiremock = { version = "0.6.2" }
192193
xz2 = { version = "0.1.7" }
193194
zip = { version = "2.2.3", default-features = false, features = ["deflate", "zstd", "bzip2", "lzma", "xz"] }

crates/uv-fs/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ tempfile = { workspace = true }
3131
tokio = { workspace = true, optional = true}
3232
tracing = { workspace = true }
3333

34-
[target.'cfg(target_os = "windows")'.dependencies]
35-
winsafe = { workspace = true }
36-
3734
[target.'cfg(any(unix, target_os = "wasi", target_os = "redox"))'.dependencies]
3835
rustix = { workspace = true }
3936

4037
[target.'cfg(windows)'.dependencies]
4138
backon = { workspace = true }
4239
junction = { workspace = true }
40+
windows = { workspace = true }
41+
windows-core = { workspace = true }
4342

4443
[features]
4544
default = []

crates/uv-fs/src/which.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
use std::path::Path;
22

3+
#[cfg(windows)]
4+
#[allow(unsafe_code)] // We need to do an FFI call through the windows-* crates.
5+
fn get_binary_type(path: &Path) -> windows::core::Result<u32> {
6+
use std::os::windows::ffi::OsStrExt;
7+
use windows::Win32::Storage::FileSystem::GetBinaryTypeW;
8+
use windows_core::PCWSTR;
9+
10+
// References:
11+
// https://github.com/denoland/deno/blob/01a6379505712be34ebf2cdc874fa7f54a6e9408/runtime/permissions/which.rs#L131-L154
12+
// https://github.com/conradkleinespel/rooster/blob/afa78dc9918535752c4af59d2f812197ad754e5a/src/quale.rs#L51-L77
13+
let mut binary_type = 0u32;
14+
let name = path
15+
.as_os_str()
16+
.encode_wide()
17+
.chain(Some(0))
18+
.collect::<Vec<u16>>();
19+
// SAFETY: winapi call
20+
unsafe { GetBinaryTypeW(PCWSTR(name.as_ptr()), &mut binary_type)? };
21+
Ok(binary_type)
22+
}
23+
324
/// Check whether a path in PATH is a valid executable.
425
///
526
/// Derived from `which`'s `Checker`.
@@ -20,9 +41,7 @@ pub fn is_executable(path: &Path) -> bool {
2041
if !file_type.is_file() && !file_type.is_symlink() {
2142
return false;
2243
}
23-
if path.extension().is_none()
24-
&& winsafe::GetBinaryType(&path.display().to_string()).is_err()
25-
{
44+
if path.extension().is_none() && get_binary_type(path).is_err() {
2645
return false;
2746
}
2847
}

0 commit comments

Comments
 (0)