Implement GetFileSizeEx compatibility wrapper#62
Open
Jookia wants to merge 1 commit into
Open
Conversation
Windows before 2000 lacks GetFileSizeEx which Rust relies on. Implement a compatibility function that uses GetFileSize as a backup.
cher-nov
suggested changes
Jul 2, 2026
Comment on lines
+813
to
+825
| let mut high32 = 0; | ||
| let low32 = unsafe { GetFileSize(hfile, &mut high32) }; | ||
| let full_size: u64 = ((high32 as u64) << 32) | (low32 as u64); | ||
| let last_err = unsafe { GetLastError() }; | ||
| if low32 == INVALID_FILE_SIZE && last_err != NO_ERROR { | ||
| return FALSE; | ||
| } else { | ||
| if !lpfilesize.is_null() { | ||
| unsafe { *lpfilesize = full_size as i64; } | ||
| } | ||
| return TRUE; | ||
| } | ||
| } |
There was a problem hiding this comment.
Better call GetLastError() only on INVALID_FILE_SIZE.
Suggested change
| let mut high32 = 0; | |
| let low32 = unsafe { GetFileSize(hfile, &mut high32) }; | |
| let full_size: u64 = ((high32 as u64) << 32) | (low32 as u64); | |
| let last_err = unsafe { GetLastError() }; | |
| if low32 == INVALID_FILE_SIZE && last_err != NO_ERROR { | |
| return FALSE; | |
| } else { | |
| if !lpfilesize.is_null() { | |
| unsafe { *lpfilesize = full_size as i64; } | |
| } | |
| return TRUE; | |
| } | |
| } | |
| let mut high32 = 0; | |
| let low32 = unsafe { GetFileSize(hfile, &mut high32) }; | |
| if low32 == INVALID_FILE_SIZE { | |
| let last_err = unsafe { GetLastError() }; | |
| if last_err != NO_ERROR { return FALSE; } | |
| } | |
| if !lpfilesize.is_null() { | |
| let full_size: u64 = ((high32 as u64) << 32) | (low32 as u64); | |
| unsafe { *lpfilesize = full_size as i64; } | |
| } | |
| TRUE | |
| } |
There was a problem hiding this comment.
Also, worth noting that genuine GetFileSizeEx(, NULL) segfaults (at least on my Windows 7). It still makes sense to return TRUE instead if nothing else has gone wrong (just as ReactOS does, for example), but should we do at least SetLastError(ERROR_INVALID_PARAMETER) in that case? @seritools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Windows before 2000 lacks GetFileSizeEx which Rust relies on. Implement a compatibility function that uses GetFileSize as a backup.