Skip to content

Commit 6a43035

Browse files
committed
Auto merge of #14075 - ChrisDenton:absolute, r=weihanglo
Use `std::fs::absolute` instead of reimplementing it [`std::fs::absolute`](https://doc.rust-lang.org/std/path/fn.absolute.html) was stabilize in Rust 1.79 and so Cargo no longer needs its own implementation.
2 parents a1f47ec + 306018d commit 6a43035

File tree

1 file changed

+2
-53
lines changed

1 file changed

+2
-53
lines changed

src/cargo/util/mod.rs

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -126,67 +126,16 @@ pub fn try_canonicalize<P: AsRef<Path>>(path: P) -> std::io::Result<PathBuf> {
126126
#[cfg(windows)]
127127
#[inline]
128128
pub fn try_canonicalize<P: AsRef<Path>>(path: P) -> std::io::Result<PathBuf> {
129-
use std::ffi::OsString;
130129
use std::io::Error;
131-
use std::os::windows::ffi::{OsStrExt, OsStringExt};
132-
use std::{io::ErrorKind, ptr};
133-
use windows_sys::Win32::Foundation::{GetLastError, SetLastError};
134-
use windows_sys::Win32::Storage::FileSystem::GetFullPathNameW;
130+
use std::io::ErrorKind;
135131

136132
// On Windows `canonicalize` may fail, so we fall back to getting an absolute path.
137133
std::fs::canonicalize(&path).or_else(|_| {
138134
// Return an error if a file does not exist for better compatibility with `canonicalize`
139135
if !path.as_ref().try_exists()? {
140136
return Err(Error::new(ErrorKind::NotFound, "the path was not found"));
141137
}
142-
143-
// This code is based on the unstable `std::path::absolute` and could be replaced with it
144-
// if it's stabilized.
145-
146-
let path = path.as_ref().as_os_str();
147-
let mut path_u16 = Vec::with_capacity(path.len() + 1);
148-
path_u16.extend(path.encode_wide());
149-
if path_u16.iter().find(|c| **c == 0).is_some() {
150-
return Err(Error::new(
151-
ErrorKind::InvalidInput,
152-
"strings passed to WinAPI cannot contain NULs",
153-
));
154-
}
155-
path_u16.push(0);
156-
157-
loop {
158-
unsafe {
159-
SetLastError(0);
160-
let len =
161-
GetFullPathNameW(path_u16.as_ptr(), 0, &mut [] as *mut u16, ptr::null_mut());
162-
if len == 0 {
163-
let error = GetLastError();
164-
if error != 0 {
165-
return Err(Error::from_raw_os_error(error as i32));
166-
}
167-
}
168-
let mut result = vec![0u16; len as usize];
169-
170-
let write_len = GetFullPathNameW(
171-
path_u16.as_ptr(),
172-
result.len().try_into().unwrap(),
173-
result.as_mut_ptr().cast::<u16>(),
174-
ptr::null_mut(),
175-
);
176-
if write_len == 0 {
177-
let error = GetLastError();
178-
if error != 0 {
179-
return Err(Error::from_raw_os_error(error as i32));
180-
}
181-
}
182-
183-
if write_len <= len {
184-
return Ok(PathBuf::from(OsString::from_wide(
185-
&result[0..(write_len as usize)],
186-
)));
187-
}
188-
}
189-
}
138+
std::path::absolute(&path)
190139
})
191140
}
192141

0 commit comments

Comments
 (0)