Auto merge of #134722 - ChrisDenton:trunc, r=Amanieu
Windows: Use FILE_ALLOCATION_INFO for truncation We use `FILE_END_OF_FILE_INFO` here only because WINE does not support `FILE_ALLOCATION_INFO`. Instead of going with the one with broadest support, let's just use that as fallback only.
This commit is contained in:
commit
a92c3cf04d
1 changed files with 20 additions and 8 deletions
|
|
@ -316,19 +316,31 @@ impl File {
|
|||
&& api::get_last_error() == WinError::ALREADY_EXISTS
|
||||
{
|
||||
unsafe {
|
||||
// This originally used `FileAllocationInfo` instead of
|
||||
// `FileEndOfFileInfo` but that wasn't supported by WINE.
|
||||
// It's arguable which fits the semantics of `OpenOptions`
|
||||
// better so let's just use the more widely supported method.
|
||||
let eof = c::FILE_END_OF_FILE_INFO { EndOfFile: 0 };
|
||||
// This first tries `FileAllocationInfo` but falls back to
|
||||
// `FileEndOfFileInfo` in order to support WINE.
|
||||
// If WINE gains support for FileAllocationInfo, we should
|
||||
// remove the fallback.
|
||||
let alloc = c::FILE_ALLOCATION_INFO { AllocationSize: 0 };
|
||||
let result = c::SetFileInformationByHandle(
|
||||
handle.as_raw_handle(),
|
||||
c::FileEndOfFileInfo,
|
||||
(&raw const eof).cast::<c_void>(),
|
||||
mem::size_of::<c::FILE_END_OF_FILE_INFO>() as u32,
|
||||
(&raw const alloc).cast::<c_void>(),
|
||||
mem::size_of::<c::FILE_ALLOCATION_INFO>() as u32,
|
||||
);
|
||||
if result == 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
if api::get_last_error().code != 0 {
|
||||
panic!("FILE_ALLOCATION_INFO failed!!!");
|
||||
}
|
||||
let eof = c::FILE_END_OF_FILE_INFO { EndOfFile: 0 };
|
||||
let result = c::SetFileInformationByHandle(
|
||||
handle.as_raw_handle(),
|
||||
c::FileEndOfFileInfo,
|
||||
(&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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue