@@ -126,67 +126,16 @@ pub fn try_canonicalize<P: AsRef<Path>>(path: P) -> std::io::Result<PathBuf> {
126
126
#[ cfg( windows) ]
127
127
#[ inline]
128
128
pub fn try_canonicalize < P : AsRef < Path > > ( path : P ) -> std:: io:: Result < PathBuf > {
129
- use std:: ffi:: OsString ;
130
129
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 ;
135
131
136
132
// On Windows `canonicalize` may fail, so we fall back to getting an absolute path.
137
133
std:: fs:: canonicalize ( & path) . or_else ( |_| {
138
134
// Return an error if a file does not exist for better compatibility with `canonicalize`
139
135
if !path. as_ref ( ) . try_exists ( ) ? {
140
136
return Err ( Error :: new ( ErrorKind :: NotFound , "the path was not found" ) ) ;
141
137
}
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)
190
139
} )
191
140
}
192
141
0 commit comments