Skip to content

Windows: Use FILE_ALLOCATION_INFO for truncation #134722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c::FileEndOfFileInfo,
c::FileAllocationInfo,

Doesn't this need to be changed to FileAllocationInfo?

Copy link
Member Author

@ChrisDenton ChrisDenton Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, huh. I fixed that locally but somehow forgot to push. Fortunately their associated values are the same size (and if they weren't then it'd return an error).

(&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
Loading