Skip to content

Commit a526aa1

Browse files
committed
Implement named pipes for windows, touch up unix
* Implementation of pipe_win32 filled out for libnative * Reorganize pipes to be clone-able * Fix a few file descriptor leaks on error * Factor out some common code into shared functions * Make use of the if_ok!() macro for less indentation Closes #11201
1 parent 94b2d9d commit a526aa1

File tree

8 files changed

+780
-288
lines changed

8 files changed

+780
-288
lines changed

src/libnative/io/mod.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ pub mod timer;
6161
pub mod timer;
6262

6363
#[cfg(unix)]
64-
#[path = "path_unix.rs"]
64+
#[path = "pipe_unix.rs"]
6565
pub mod pipe;
6666

6767
#[cfg(windows)]
68-
#[path = "path_win32.rs"]
68+
#[path = "pipe_win32.rs"]
6969
pub mod pipe;
7070

7171
mod timer_helper;
@@ -85,6 +85,9 @@ fn translate_error(errno: i32, detail: bool) -> IoError {
8585
fn get_err(errno: i32) -> (io::IoErrorKind, &'static str) {
8686
match errno {
8787
libc::EOF => (io::EndOfFile, "end of file"),
88+
libc::ERROR_NO_DATA => (io::BrokenPipe, "the pipe is being closed"),
89+
libc::ERROR_FILE_NOT_FOUND => (io::FileNotFound, "file not found"),
90+
libc::ERROR_INVALID_NAME => (io::InvalidInput, "invalid file name"),
8891
libc::WSAECONNREFUSED => (io::ConnectionRefused, "connection refused"),
8992
libc::WSAECONNRESET => (io::ConnectionReset, "connection reset"),
9093
libc::WSAEACCES => (io::PermissionDenied, "permission denied"),
@@ -94,6 +97,7 @@ fn translate_error(errno: i32, detail: bool) -> IoError {
9497
libc::WSAECONNABORTED => (io::ConnectionAborted, "connection aborted"),
9598
libc::WSAEADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
9699
libc::WSAEADDRINUSE => (io::ConnectionRefused, "address in use"),
100+
libc::ERROR_BROKEN_PIPE => (io::BrokenPipe, "the pipe has ended"),
97101

98102
x => {
99103
debug!("ignoring {}: {}", x, os::last_os_error());
@@ -116,6 +120,7 @@ fn translate_error(errno: i32, detail: bool) -> IoError {
116120
libc::ECONNABORTED => (io::ConnectionAborted, "connection aborted"),
117121
libc::EADDRNOTAVAIL => (io::ConnectionRefused, "address not available"),
118122
libc::EADDRINUSE => (io::ConnectionRefused, "address in use"),
123+
libc::ENOENT => (io::FileNotFound, "no such file or directory"),
119124

120125
// These two constants can have the same value on some systems, but
121126
// different values on others, so we can't use a match clause

src/libnative/io/net.rs

-53
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::c_str::CString;
1211
use std::cast;
1312
use std::io::net::ip;
1413
use std::io;
1514
use std::libc;
1615
use std::mem;
1716
use std::rt::rtio;
1817
use std::sync::arc::UnsafeArc;
19-
use std::unstable::intrinsics;
2018

2119
use super::{IoResult, retry};
2220
use super::file::keep_going;
@@ -90,30 +88,6 @@ fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
9088
}
9189
}
9290

93-
fn addr_to_sockaddr_un(addr: &CString) -> IoResult<(libc::sockaddr_storage, uint)> {
94-
// the sun_path length is limited to SUN_LEN (with null)
95-
if addr.len() > libc::sun_len -1 {
96-
return Err(io::IoError {
97-
kind: io::OtherIoError,
98-
desc: "path must be smaller than SUN_LEN",
99-
detail: None,
100-
})
101-
}
102-
unsafe {
103-
let storage: libc::sockaddr_storage = intrinsics::init();
104-
let s: *mut libc::sockaddr_un = cast::transmute(&storage);
105-
(*s).sun_family = libc::AF_UNIX as libc::sa_family_t;
106-
let mut i = 0;
107-
for c in addr.iter() {
108-
(*s).sun_path[i] = c;
109-
i += 1;
110-
}
111-
112-
let len = mem::size_of::<libc::sa_family_t>() + i + 1; //count the null terminator
113-
return Ok((storage, len));
114-
}
115-
}
116-
11791
fn socket(addr: ip::SocketAddr, ty: libc::c_int) -> IoResult<sock_t> {
11892
unsafe {
11993
let fam = match addr.ip {
@@ -127,15 +101,6 @@ fn socket(addr: ip::SocketAddr, ty: libc::c_int) -> IoResult<sock_t> {
127101
}
128102
}
129103

130-
fn unix_socket(ty: libc::c_int) -> IoResult<sock_t> {
131-
unsafe {
132-
match libc::socket(libc::AF_UNIX, ty, 0) {
133-
-1 => Err(super::last_error()),
134-
fd => Ok(fd)
135-
}
136-
}
137-
}
138-
139104
fn setsockopt<T>(fd: sock_t, opt: libc::c_int, val: libc::c_int,
140105
payload: T) -> IoResult<()> {
141106
unsafe {
@@ -228,24 +193,6 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
228193
}
229194
}
230195

231-
fn sockaddr_to_unix(storage: &libc::sockaddr_storage,
232-
len: uint) -> IoResult<CString> {
233-
match storage.ss_family as libc::c_int {
234-
libc::AF_UNIX => {
235-
assert!(len as uint <= mem::size_of::<libc::sockaddr_un>());
236-
let storage: &libc::sockaddr_un = unsafe {
237-
cast::transmute(storage)
238-
};
239-
unsafe {
240-
Ok(CString::new(storage.sun_path.to_owned().as_ptr(), false))
241-
}
242-
}
243-
_ => {
244-
Err(io::standard_error(io::OtherIoError))
245-
}
246-
}
247-
}
248-
249196
#[cfg(unix)]
250197
pub fn init() {}
251198

0 commit comments

Comments
 (0)