|
1 |
| -use super::api::{self, WinError}; |
| 1 | +use super::api::{self, WinError, set_file_information_by_handle}; |
2 | 2 | use super::{IoResult, to_u16s};
|
3 | 3 | use crate::alloc::{alloc, handle_alloc_error};
|
4 | 4 | use crate::borrow::Cow;
|
@@ -319,31 +319,17 @@ impl File {
|
319 | 319 | && creation == c::OPEN_ALWAYS
|
320 | 320 | && api::get_last_error() == WinError::ALREADY_EXISTS
|
321 | 321 | {
|
322 |
| - unsafe { |
323 |
| - // This first tries `FileAllocationInfo` but falls back to |
324 |
| - // `FileEndOfFileInfo` in order to support WINE. |
325 |
| - // If WINE gains support for FileAllocationInfo, we should |
326 |
| - // remove the fallback. |
327 |
| - let alloc = c::FILE_ALLOCATION_INFO { AllocationSize: 0 }; |
328 |
| - let result = c::SetFileInformationByHandle( |
329 |
| - handle.as_raw_handle(), |
330 |
| - c::FileAllocationInfo, |
331 |
| - (&raw const alloc).cast::<c_void>(), |
332 |
| - mem::size_of::<c::FILE_ALLOCATION_INFO>() as u32, |
333 |
| - ); |
334 |
| - if result == 0 { |
| 322 | + // This first tries `FileAllocationInfo` but falls back to |
| 323 | + // `FileEndOfFileInfo` in order to support WINE. |
| 324 | + // If WINE gains support for FileAllocationInfo, we should |
| 325 | + // remove the fallback. |
| 326 | + let alloc = c::FILE_ALLOCATION_INFO { AllocationSize: 0 }; |
| 327 | + set_file_information_by_handle(handle.as_raw_handle(), &alloc) |
| 328 | + .or_else(|_| { |
335 | 329 | let eof = c::FILE_END_OF_FILE_INFO { EndOfFile: 0 };
|
336 |
| - let result = c::SetFileInformationByHandle( |
337 |
| - handle.as_raw_handle(), |
338 |
| - c::FileEndOfFileInfo, |
339 |
| - (&raw const eof).cast::<c_void>(), |
340 |
| - mem::size_of::<c::FILE_END_OF_FILE_INFO>() as u32, |
341 |
| - ); |
342 |
| - if result == 0 { |
343 |
| - return Err(io::Error::last_os_error()); |
344 |
| - } |
345 |
| - } |
346 |
| - } |
| 330 | + set_file_information_by_handle(handle.as_raw_handle(), &eof) |
| 331 | + }) |
| 332 | + .io_result()?; |
347 | 333 | }
|
348 | 334 | Ok(File { handle: Handle::from_inner(handle) })
|
349 | 335 | } else {
|
|
0 commit comments