Skip to content

Commit 0c1cae8

Browse files
Rollup merge of #158132 - joboet:windows-uwp-filesize, r=ChrisDenton
std: correctly report file size on UWP Found by an LLM in SebTardif#397. UWP uses [`GetFileInformationByHandleEx`](https://learn.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex) instead of [GetFileInformationByHandle](https://learn.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle), but uses the wrong field to determine file size – `EndOfFile` is a different field than `AllocationSize`. [`file_size`](https://doc.rust-lang.org/nightly/std/os/windows/fs/trait.MetadataExt.html#tymethod.file_size) should report the length of the file, not its size on disk, and only `EndOfFile` reports the former (c.f. https://learn.microsoft.com/en-us/windows/win32/api/FileAPI/nf-fileapi-setendoffile#remarks): > Each file stream has the following: > * File size: the size of the data in a file, to the byte. > * Allocation size: the size of the space that is allocated for a file on a disk, which is always an even multiple of the cluster size. > * Valid data length: the length of the data in a file that is actually written, to the byte. This value is always less than or equal to the file size. r? @ChrisDenton
2 parents b10255a + 2203dd7 commit 0c1cae8

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

library/std/src/sys/fs/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl File {
597597
(&raw mut info) as *mut c_void,
598598
size as u32,
599599
))?;
600-
attr.file_size = info.AllocationSize as u64;
600+
attr.file_size = info.EndOfFile as u64;
601601
attr.number_of_links = Some(info.NumberOfLinks);
602602
if attr.attributes & c::FILE_ATTRIBUTE_REPARSE_POINT != 0 {
603603
let mut attr_tag: c::FILE_ATTRIBUTE_TAG_INFO = mem::zeroed();

0 commit comments

Comments
 (0)