Rollup merge of #137482 - rust9x:win-file-open-truncate, r=ChrisDenton
Windows: use existing wrappers in `File::open_native` Just a small improvement I've noticed - prevents accidents regarding `SetFileInformationByHandle` parameters. Probably ``@ChrisDenton`` since we talked about it on discord :)
This commit is contained in:
commit
56fca2638f
1 changed files with 11 additions and 25 deletions
|
@ -1,4 +1,4 @@
|
||||||
use super::api::{self, WinError};
|
use super::api::{self, WinError, set_file_information_by_handle};
|
||||||
use super::{IoResult, to_u16s};
|
use super::{IoResult, to_u16s};
|
||||||
use crate::alloc::{alloc, handle_alloc_error};
|
use crate::alloc::{alloc, handle_alloc_error};
|
||||||
use crate::borrow::Cow;
|
use crate::borrow::Cow;
|
||||||
|
@ -319,31 +319,17 @@ impl File {
|
||||||
&& creation == c::OPEN_ALWAYS
|
&& creation == c::OPEN_ALWAYS
|
||||||
&& api::get_last_error() == WinError::ALREADY_EXISTS
|
&& api::get_last_error() == WinError::ALREADY_EXISTS
|
||||||
{
|
{
|
||||||
unsafe {
|
// This first tries `FileAllocationInfo` but falls back to
|
||||||
// This first tries `FileAllocationInfo` but falls back to
|
// `FileEndOfFileInfo` in order to support WINE.
|
||||||
// `FileEndOfFileInfo` in order to support WINE.
|
// If WINE gains support for FileAllocationInfo, we should
|
||||||
// If WINE gains support for FileAllocationInfo, we should
|
// remove the fallback.
|
||||||
// remove the fallback.
|
let alloc = c::FILE_ALLOCATION_INFO { AllocationSize: 0 };
|
||||||
let alloc = c::FILE_ALLOCATION_INFO { AllocationSize: 0 };
|
set_file_information_by_handle(handle.as_raw_handle(), &alloc)
|
||||||
let result = c::SetFileInformationByHandle(
|
.or_else(|_| {
|
||||||
handle.as_raw_handle(),
|
|
||||||
c::FileAllocationInfo,
|
|
||||||
(&raw const alloc).cast::<c_void>(),
|
|
||||||
mem::size_of::<c::FILE_ALLOCATION_INFO>() as u32,
|
|
||||||
);
|
|
||||||
if result == 0 {
|
|
||||||
let eof = c::FILE_END_OF_FILE_INFO { EndOfFile: 0 };
|
let eof = c::FILE_END_OF_FILE_INFO { EndOfFile: 0 };
|
||||||
let result = c::SetFileInformationByHandle(
|
set_file_information_by_handle(handle.as_raw_handle(), &eof)
|
||||||
handle.as_raw_handle(),
|
})
|
||||||
c::FileEndOfFileInfo,
|
.io_result()?;
|
||||||
(&raw const eof).cast::<c_void>(),
|
|
||||||
mem::size_of::<c::FILE_END_OF_FILE_INFO>() as u32,
|
|
||||||
);
|
|
||||||
if result == 0 {
|
|
||||||
return Err(io::Error::last_os_error());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(File { handle: Handle::from_inner(handle) })
|
Ok(File { handle: Handle::from_inner(handle) })
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue