Skip to content

Commit 66d3b75

Browse files
committed
Win: use existing wrappers for SetFileInformationByHandle in File::open_native
1 parent 97b3a66 commit 66d3b75

File tree

1 file changed

+11
-25
lines changed
  • std/src/sys/pal/windows

1 file changed

+11
-25
lines changed

Diff for: std/src/sys/pal/windows/fs.rs

+11-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::api::{self, WinError};
1+
use super::api::{self, WinError, set_file_information_by_handle};
22
use super::{IoResult, to_u16s};
33
use crate::alloc::{alloc, handle_alloc_error};
44
use crate::borrow::Cow;
@@ -319,31 +319,17 @@ impl File {
319319
&& creation == c::OPEN_ALWAYS
320320
&& api::get_last_error() == WinError::ALREADY_EXISTS
321321
{
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(|_| {
335329
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()?;
347333
}
348334
Ok(File { handle: Handle::from_inner(handle) })
349335
} else {

0 commit comments

Comments
 (0)