Skip to content

Implement GetFileSizeEx compatibility wrapper#62

Open
Jookia wants to merge 1 commit into
rust9x:rust9xfrom
Jookia:jookia_copyfile
Open

Implement GetFileSizeEx compatibility wrapper#62
Jookia wants to merge 1 commit into
rust9x:rust9xfrom
Jookia:jookia_copyfile

Conversation

@Jookia

@Jookia Jookia commented Jul 1, 2026

Copy link
Copy Markdown

Windows before 2000 lacks GetFileSizeEx which Rust relies on. Implement a compatibility function that uses GetFileSize as a backup.

Windows before 2000 lacks GetFileSizeEx which Rust relies on.
Implement a compatibility function that uses GetFileSize as a backup.
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;
}
}

@cher-nov cher-nov Jul 2, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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
}

@cher-nov cher-nov Jul 2, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants