Skip to content
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

Fix reading/writing 4 GiB or larger files on Windows 64-bit #31825

Merged
merged 1 commit into from
Feb 23, 2016
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
16 changes: 11 additions & 5 deletions src/libstd/sys/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use cmp;
use io::ErrorKind;
use io;
use mem;
use ops::Deref;
use ptr;
use sys::c;
use sys::cvt;
use u32;

/// An owned container for `HANDLE` object, closing them on Drop.
///
Expand Down Expand Up @@ -64,10 +66,12 @@ impl RawHandle {

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
let mut read = 0;
// ReadFile takes a DWORD (u32) for the length so it only supports
// reading u32::MAX bytes at a time.
let len = cmp::min(buf.len(), u32::MAX as usize) as c::DWORD;
let res = cvt(unsafe {
c::ReadFile(self.0, buf.as_ptr() as c::LPVOID,
buf.len() as c::DWORD, &mut read,
ptr::null_mut())
c::ReadFile(self.0, buf.as_mut_ptr() as c::LPVOID,
len, &mut read, ptr::null_mut())
});

match res {
Expand All @@ -85,10 +89,12 @@ impl RawHandle {

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
let mut amt = 0;
// WriteFile takes a DWORD (u32) for the length so it only supports
// writing u32::MAX bytes at a time.
let len = cmp::min(buf.len(), u32::MAX as usize) as c::DWORD;
try!(cvt(unsafe {
c::WriteFile(self.0, buf.as_ptr() as c::LPVOID,
buf.len() as c::DWORD, &mut amt,
ptr::null_mut())
len, &mut amt, ptr::null_mut())
}));
Ok(amt as usize)
}
Expand Down