Skip to content

Commit cd9010c

Browse files
committed
native: Improve windows file handling
This commit splits the file implementation into file_unix and file_win32. The two implementations have diverged to the point that they share almost 0 code at this point, so it's easier to maintain as separate files. The other major change accompanied with this commit is that file::open is no longer based on libc's open function on windows, but rather windows's CreateFile function. This fixes dealing with binary files on windows (test added in previous commit). This also changes the read/write functions to use ReadFile and WriteFile instead of libc's read/write. Closes #12406
1 parent 843c5e6 commit cd9010c

File tree

11 files changed

+1146
-1018
lines changed

11 files changed

+1146
-1018
lines changed

src/libnative/io/file.rs

Lines changed: 0 additions & 995 deletions
This file was deleted.

src/libnative/io/file_unix.rs

Lines changed: 573 additions & 0 deletions
Large diffs are not rendered by default.

src/libnative/io/file_win32.rs

Lines changed: 516 additions & 0 deletions
Large diffs are not rendered by default.

src/libnative/io/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ pub use self::process::Process;
4242

4343
// Native I/O implementations
4444
pub mod addrinfo;
45-
pub mod file;
4645
pub mod net;
4746
pub mod process;
4847

48+
#[cfg(unix)]
49+
#[path = "file_unix.rs"]
50+
pub mod file;
51+
#[cfg(windows)]
52+
#[path = "file_win32.rs"]
53+
pub mod file;
54+
4955
#[cfg(target_os = "macos")]
5056
#[cfg(target_os = "freebsd")]
5157
#[cfg(target_os = "android")]
@@ -97,7 +103,7 @@ fn translate_error(errno: i32, detail: bool) -> IoError {
97103
libc::WSAECONNABORTED => (io::ConnectionAborted, "connection aborted"),
98104
libc::WSAEADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
99105
libc::WSAEADDRINUSE => (io::ConnectionRefused, "address in use"),
100-
libc::ERROR_BROKEN_PIPE => (io::BrokenPipe, "the pipe has ended"),
106+
libc::ERROR_BROKEN_PIPE => (io::EndOfFile, "the pipe has ended"),
101107

102108
x => {
103109
debug!("ignoring {}: {}", x, os::last_os_error());
@@ -185,6 +191,24 @@ fn retry(f: || -> libc::c_int) -> libc::c_int {
185191
}
186192
}
187193

194+
fn keep_going(data: &[u8], f: |*u8, uint| -> i64) -> i64 {
195+
let origamt = data.len();
196+
let mut data = data.as_ptr();
197+
let mut amt = origamt;
198+
while amt > 0 {
199+
let ret = retry(|| f(data, amt) as libc::c_int);
200+
if ret == 0 {
201+
break
202+
} else if ret != -1 {
203+
amt -= ret as uint;
204+
data = unsafe { data.offset(ret as int) };
205+
} else {
206+
return ret as i64;
207+
}
208+
}
209+
return (origamt - amt) as i64;
210+
}
211+
188212
/// Implementation of rt::rtio's IoFactory trait to generate handles to the
189213
/// native I/O functionality.
190214
pub struct IoFactory {

src/libnative/io/net.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[allow(non_camel_case_types)];
12-
1311
use std::cast;
1412
use std::io::net::ip;
1513
use std::io;
@@ -18,8 +16,7 @@ use std::mem;
1816
use std::rt::rtio;
1917
use std::sync::arc::UnsafeArc;
2018

21-
use super::{IoResult, retry};
22-
use super::file::keep_going;
19+
use super::{IoResult, retry, keep_going};
2320

2421
////////////////////////////////////////////////////////////////////////////////
2522
// sockaddr and misc bindings
@@ -323,16 +320,14 @@ impl rtio::RtioTcpStream for TcpStream {
323320
}
324321
}
325322
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
326-
let ret = keep_going(buf, |buf, len| {
327-
unsafe {
328-
libc::send(self.fd(),
329-
buf as *mut libc::c_void,
330-
len as wrlen,
331-
0) as i64
332-
}
323+
let ret = keep_going(buf, |buf, len| unsafe {
324+
libc::send(self.fd(),
325+
buf as *mut libc::c_void,
326+
len as wrlen,
327+
0) as i64
333328
});
334329
if ret < 0 {
335-
Err(last_error())
330+
Err(super::last_error())
336331
} else {
337332
Ok(())
338333
}

src/libnative/io/pipe_unix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use std::rt::rtio;
1717
use std::sync::arc::UnsafeArc;
1818
use std::intrinsics;
1919

20-
use super::{IoResult, retry};
21-
use super::file::{keep_going, fd_t};
20+
use super::{IoResult, retry, keep_going};
21+
use super::file::fd_t;
2222

2323
fn unix_socket(ty: libc::c_int) -> IoResult<fd_t> {
2424
match unsafe { libc::socket(libc::AF_UNIX, ty, 0) } {

src/libnative/io/timer_helper.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
//! can be created in the future and there must be no active timers at that
2121
//! time.
2222
23-
#[allow(non_camel_case_types)];
24-
2523
use std::cast;
2624
use std::rt;
2725
use std::unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
@@ -100,7 +98,6 @@ mod imp {
10098

10199
use io::file::FileDesc;
102100

103-
#[allow(non_camel_case_types)]
104101
pub type signal = libc::c_int;
105102

106103
pub fn new() -> (signal, signal) {

src/libnative/io/timer_other.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
//!
4747
//! Note that all time units in this file are in *milliseconds*.
4848
49-
#[allow(non_camel_case_types)];
50-
5149
use std::comm::Data;
5250
use std::libc;
5351
use std::mem;

src/libnative/io/timer_timerfd.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
//!
2929
//! As with timer_other, all units in this file are in units of millseconds.
3030
31-
#[allow(non_camel_case_types)];
32-
3331
use std::comm::Data;
3432
use std::libc;
3533
use std::ptr;

src/libnative/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
5050
html_root_url = "http://static.rust-lang.org/doc/master")];
5151
#[deny(unused_result, unused_must_use)];
52+
#[allow(non_camel_case_types)];
5253

5354
// NB this crate explicitly does *not* allow glob imports, please seriously
5455
// consider whether they're needed before adding that feature here (the

0 commit comments

Comments
 (0)