Skip to content

Commit e9c7544

Browse files
committed
Move Windows path test function to test code
1 parent d03530f commit e9c7544

File tree

4 files changed

+55
-57
lines changed

4 files changed

+55
-57
lines changed

crates/cargo-test-support/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ termcolor = "1.1.2"
2525
toml_edit = { version = "0.14.3", features = ["serde", "easy", "perf"] }
2626
url = "2.2.2"
2727

28+
[target.'cfg(windows)'.dependencies]
29+
winapi = "0.3"
30+
2831
[features]
2932
deny-warnings = []

crates/cargo-test-support/src/paths.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,54 @@ pub fn sysroot() -> String {
287287
let sysroot = String::from_utf8(output.stdout).unwrap();
288288
sysroot.trim().to_string()
289289
}
290+
291+
/// Returns true if names such as aux.* are allowed.
292+
///
293+
/// Traditionally, Windows did not allow a set of file names (see `is_windows_reserved`
294+
/// for a list). More recent versions of Windows have relaxed this restriction. This test
295+
/// determines whether we are running in a mode that allows Windows reserved names.
296+
#[cfg(windows)]
297+
pub fn windows_reserved_names_are_allowed() -> bool {
298+
use std::ffi::OsStr;
299+
use std::os::windows::ffi::OsStrExt;
300+
use std::ptr;
301+
use winapi::um::fileapi::GetFullPathNameW;
302+
303+
let test_file_name: Vec<_> = OsStr::new("aux.rs").encode_wide().collect();
304+
305+
let buffer_length =
306+
unsafe { GetFullPathNameW(test_file_name.as_ptr(), 0, ptr::null_mut(), ptr::null_mut()) };
307+
308+
if buffer_length == 0 {
309+
// This means the call failed, so we'll conservatively assume reserved names are not allowed.
310+
return false;
311+
}
312+
313+
let mut buffer = vec![0u16; buffer_length as usize];
314+
315+
let result = unsafe {
316+
GetFullPathNameW(
317+
test_file_name.as_ptr(),
318+
buffer_length,
319+
buffer.as_mut_ptr(),
320+
ptr::null_mut(),
321+
)
322+
};
323+
324+
if result == 0 {
325+
// Once again, conservatively assume reserved names are not allowed if the
326+
// GetFullPathNameW call failed.
327+
return false;
328+
}
329+
330+
// Under the old rules, a file name like aux.rs would get converted into \\.\aux, so
331+
// we detect this case by checking if the string starts with \\.\
332+
//
333+
// Otherwise, the filename will be something like C:\Users\Foo\Documents\aux.rs
334+
let prefix: Vec<_> = OsStr::new("\\\\.\\").encode_wide().collect();
335+
if buffer.starts_with(&prefix) {
336+
false
337+
} else {
338+
true
339+
}
340+
}

src/cargo/util/restricted_names.rs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -97,59 +97,3 @@ pub fn is_windows_reserved_path(path: &Path) -> bool {
9797
pub fn is_glob_pattern<T: AsRef<str>>(name: T) -> bool {
9898
name.as_ref().contains(&['*', '?', '[', ']'][..])
9999
}
100-
101-
/// Returns true if names such as aux.* are allowed.
102-
///
103-
/// Traditionally, Windows did not allow a set of file names (see `is_windows_reserved`
104-
/// for a list). More recent versions of Windows have relaxed this restriction. This test
105-
/// determines whether we are running in a mode that allows Windows reserved names.
106-
pub fn windows_reserved_names_are_allowed() -> bool {
107-
#[cfg(windows)]
108-
{
109-
use std::ffi::OsStr;
110-
use std::os::windows::ffi::OsStrExt;
111-
use std::ptr;
112-
use winapi::um::fileapi::GetFullPathNameW;
113-
114-
let test_file_name: Vec<_> = OsStr::new("aux.rs").encode_wide().collect();
115-
116-
let buffer_length = unsafe {
117-
GetFullPathNameW(test_file_name.as_ptr(), 0, ptr::null_mut(), ptr::null_mut())
118-
};
119-
120-
if buffer_length == 0 {
121-
// This means the call failed, so we'll conservatively assume reserved names are not allowed.
122-
return false;
123-
}
124-
125-
let mut buffer = vec![0u16; buffer_length as usize];
126-
127-
let result = unsafe {
128-
GetFullPathNameW(
129-
test_file_name.as_ptr(),
130-
buffer_length,
131-
buffer.as_mut_ptr(),
132-
ptr::null_mut(),
133-
)
134-
};
135-
136-
if result == 0 {
137-
// Once again, conservatively assume reserved names are not allowed if the
138-
// GetFullPathNameW call failed.
139-
return false;
140-
}
141-
142-
// Under the old rules, a file name like aux.rs would get converted into \\.\aux, so
143-
// we detect this case by checking if the string starts with \\.\
144-
//
145-
// Otherwise, the filename will be something like C:\Users\Foo\Documents\aux.rs
146-
let prefix: Vec<_> = OsStr::new("\\\\.\\").encode_wide().collect();
147-
if buffer.starts_with(&prefix) {
148-
false
149-
} else {
150-
true
151-
}
152-
}
153-
#[cfg(not(windows))]
154-
true
155-
}

tests/testsuite/package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,7 @@ src/lib.rs
20112011
fn reserved_windows_name() {
20122012
// If we are running on a version of Windows that allows these reserved filenames,
20132013
// skip this test.
2014-
if cargo::util::restricted_names::windows_reserved_names_are_allowed() {
2014+
if paths::windows_reserved_names_are_allowed() {
20152015
return;
20162016
}
20172017

0 commit comments

Comments
 (0)