Skip to content

Commit

Permalink
Auto merge of #134722 - ChrisDenton:trunc, r=Amanieu
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Dec 24, 2024
2 parents 409998c + ca56dc8 commit a92c3cf
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions library/std/src/sys/pal/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
Expand Down

0 comments on commit a92c3cf

Please sign in to comment.