Skip to content

Commit 03e9157

Browse files
committed
Don't read forever on a file descriptor
Similarly to the recent commit to do this for networking, there's no reason that a read on a file descriptor should continue reading until the entire buffer is full. This makes sense when dealing with literal files, but when dealing with things like stdin this doesn't make sense.
1 parent 3afa0b9 commit 03e9157

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/libnative/io/file.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ impl FileDesc {
7979
pub fn inner_read(&mut self, buf: &mut [u8]) -> Result<uint, IoError> {
8080
#[cfg(windows)] type rlen = libc::c_uint;
8181
#[cfg(not(windows))] type rlen = libc::size_t;
82-
let ret = keep_going(buf, |buf, len| {
83-
unsafe {
84-
libc::read(self.fd, buf as *mut libc::c_void, len as rlen) as i64
85-
}
82+
let ret = retry(|| unsafe {
83+
libc::read(self.fd,
84+
buf.as_ptr() as *mut libc::c_void,
85+
buf.len() as rlen) as libc::c_int
8686
});
8787
if ret == 0 {
8888
Err(io::standard_error(io::EndOfFile))

src/libnative/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub mod io;
3434
pub mod task;
3535

3636
// XXX: this should not exist here
37-
#[cfg(stage0)]
37+
#[cfg(stage0, nativestart)]
3838
#[lang = "start"]
3939
pub fn lang_start(main: *u8, argc: int, argv: **u8) -> int {
4040
use std::cast;

src/libstd/io/pipe.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,25 @@ impl Writer for PipeStream {
8080
}
8181
}
8282
}
83+
84+
#[cfg(test)]
85+
mod test {
86+
iotest!(fn partial_read() {
87+
use os;
88+
use io::pipe::PipeStream;
89+
90+
let os::Pipe { input, out } = os::pipe();
91+
let out = PipeStream::open(out);
92+
let mut input = PipeStream::open(input);
93+
let (p, c) = Chan::new();
94+
do spawn {
95+
let mut out = out;
96+
out.write([10]);
97+
p.recv(); // don't close the pipe until the other read has finished
98+
}
99+
100+
let mut buf = [0, ..10];
101+
input.read(buf);
102+
c.send(());
103+
})
104+
}

0 commit comments

Comments
 (0)