1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(missing_docs)]
12 #![allow(non_camel_case_types)]
13 #![allow(non_snake_case)]
18 use io::{self, ErrorKind};
21 use old_io::{self, IoResult, IoError};
23 use os::windows::OsStrExt;
24 use sync::{Once, ONCE_INIT};
26 macro_rules! helper_init { (static $name:ident: Helper<$m:ty>) => (
27 static $name: Helper<$m> = Helper {
28 lock: ::sync::MUTEX_INIT,
29 cond: ::sync::CONDVAR_INIT,
30 chan: ::cell::UnsafeCell { value: 0 as *mut ::sync::mpsc::Sender<$m> },
31 signal: ::cell::UnsafeCell { value: 0 },
32 initialized: ::cell::UnsafeCell { value: false },
33 shutdown: ::cell::UnsafeCell { value: false },
44 pub mod helper_signal;
54 pub mod stack_overflow;
65 pub use sys_common::net::get_host_addresses;
66 pub use sys_common::net::get_address_name;
69 // FIXME: move these to c module
70 pub type sock_t = libc::SOCKET;
71 pub type wrlen = libc::c_int;
72 pub type msglen_t = libc::c_int;
73 pub unsafe fn close_sock(sock: sock_t) { let _ = libc::closesocket(sock); }
75 // windows has zero values as errors
76 fn mkerr_winbool(ret: libc::c_int) -> IoResult<()> {
84 pub fn last_error() -> IoError {
85 let errno = os::errno() as i32;
86 let mut err = decode_error(errno);
87 err.detail = Some(os::error_string(errno));
91 pub fn last_net_error() -> IoError {
92 let errno = unsafe { c::WSAGetLastError() as i32 };
93 let mut err = decode_error(errno);
94 err.detail = Some(os::error_string(errno));
98 pub fn last_gai_error(_errno: i32) -> IoError {
102 /// Convert an `errno` value into a high-level error variant and description.
103 pub fn decode_error(errno: i32) -> IoError {
104 let (kind, desc) = match errno {
105 libc::EOF => (old_io::EndOfFile, "end of file"),
106 libc::ERROR_NO_DATA => (old_io::BrokenPipe, "the pipe is being closed"),
107 libc::ERROR_FILE_NOT_FOUND => (old_io::FileNotFound, "file not found"),
108 libc::ERROR_INVALID_NAME => (old_io::InvalidInput, "invalid file name"),
109 libc::WSAECONNREFUSED => (old_io::ConnectionRefused, "connection refused"),
110 libc::WSAECONNRESET => (old_io::ConnectionReset, "connection reset"),
111 libc::ERROR_ACCESS_DENIED | libc::WSAEACCES =>
112 (old_io::PermissionDenied, "permission denied"),
113 libc::WSAEWOULDBLOCK => {
114 (old_io::ResourceUnavailable, "resource temporarily unavailable")
116 libc::WSAENOTCONN => (old_io::NotConnected, "not connected"),
117 libc::WSAECONNABORTED => (old_io::ConnectionAborted, "connection aborted"),
118 libc::WSAEADDRNOTAVAIL => (old_io::ConnectionRefused, "address not available"),
119 libc::WSAEADDRINUSE => (old_io::ConnectionRefused, "address in use"),
120 libc::ERROR_BROKEN_PIPE => (old_io::EndOfFile, "the pipe has ended"),
121 libc::ERROR_OPERATION_ABORTED =>
122 (old_io::TimedOut, "operation timed out"),
123 libc::WSAEINVAL => (old_io::InvalidInput, "invalid argument"),
124 libc::ERROR_CALL_NOT_IMPLEMENTED =>
125 (old_io::IoUnavailable, "function not implemented"),
126 libc::ERROR_INVALID_HANDLE =>
127 (old_io::MismatchedFileTypeForOperation,
128 "invalid handle provided to function"),
129 libc::ERROR_NOTHING_TO_TERMINATE =>
130 (old_io::InvalidInput, "no process to kill"),
131 libc::ERROR_ALREADY_EXISTS =>
132 (old_io::PathAlreadyExists, "path already exists"),
134 // libuv maps this error code to EISDIR. we do too. if it is found
135 // to be incorrect, we can add in some more machinery to only
136 // return this message when ERROR_INVALID_FUNCTION after certain
138 libc::ERROR_INVALID_FUNCTION => (old_io::InvalidInput,
139 "illegal operation on a directory"),
141 _ => (old_io::OtherIoError, "unknown error")
143 IoError { kind: kind, desc: desc, detail: None }
146 pub fn decode_error_detailed(errno: i32) -> IoError {
147 let mut err = decode_error(errno);
148 err.detail = Some(os::error_string(errno));
152 pub fn decode_error_kind(errno: i32) -> ErrorKind {
153 match errno as libc::c_int {
154 libc::ERROR_ACCESS_DENIED => ErrorKind::PermissionDenied,
155 libc::ERROR_ALREADY_EXISTS => ErrorKind::PathAlreadyExists,
156 libc::ERROR_BROKEN_PIPE => ErrorKind::BrokenPipe,
157 libc::ERROR_FILE_NOT_FOUND => ErrorKind::FileNotFound,
158 libc::ERROR_INVALID_FUNCTION => ErrorKind::InvalidInput,
159 libc::ERROR_INVALID_HANDLE => ErrorKind::MismatchedFileTypeForOperation,
160 libc::ERROR_INVALID_NAME => ErrorKind::InvalidInput,
161 libc::ERROR_NOTHING_TO_TERMINATE => ErrorKind::InvalidInput,
162 libc::ERROR_NO_DATA => ErrorKind::BrokenPipe,
163 libc::ERROR_OPERATION_ABORTED => ErrorKind::TimedOut,
165 libc::WSAEACCES => ErrorKind::PermissionDenied,
166 libc::WSAEADDRINUSE => ErrorKind::ConnectionRefused,
167 libc::WSAEADDRNOTAVAIL => ErrorKind::ConnectionRefused,
168 libc::WSAECONNABORTED => ErrorKind::ConnectionAborted,
169 libc::WSAECONNREFUSED => ErrorKind::ConnectionRefused,
170 libc::WSAECONNRESET => ErrorKind::ConnectionReset,
171 libc::WSAEINVAL => ErrorKind::InvalidInput,
172 libc::WSAENOTCONN => ErrorKind::NotConnected,
173 libc::WSAEWOULDBLOCK => ErrorKind::ResourceUnavailable,
175 _ => ErrorKind::Other,
181 pub fn retry<I, F>(f: F) -> I where F: FnOnce() -> I { f() } // PR rust-lang/rust/#17020
183 pub fn ms_to_timeval(ms: u64) -> libc::timeval {
185 tv_sec: (ms / 1000) as libc::c_long,
186 tv_usec: ((ms % 1000) * 1000) as libc::c_long,
190 pub fn wouldblock() -> bool {
191 let err = os::errno();
192 err == libc::WSAEWOULDBLOCK as i32
195 pub fn set_nonblocking(fd: sock_t, nb: bool) -> IoResult<()> {
196 let mut set = nb as libc::c_ulong;
197 if unsafe { c::ioctlsocket(fd, c::FIONBIO, &mut set) != 0 } {
206 static START: Once = ONCE_INIT;
209 let mut data: c::WSADATA = mem::zeroed();
210 let ret = c::WSAStartup(0x202, // version 2.2
217 pub fn unimpl() -> IoError {
219 kind: old_io::IoUnavailable,
220 desc: "operation is not implemented",
225 fn to_utf16(s: Option<&str>) -> IoResult<Vec<u16>> {
227 Some(s) => Ok(to_utf16_os(OsStr::from_str(s))),
228 None => Err(IoError {
229 kind: old_io::InvalidInput,
230 desc: "valid unicode input required",
236 fn to_utf16_os(s: &OsStr) -> Vec<u16> {
237 let mut v: Vec<_> = s.encode_wide().collect();
242 // Many Windows APIs follow a pattern of where we hand the a buffer and then
243 // they will report back to us how large the buffer should be or how many bytes
244 // currently reside in the buffer. This function is an abstraction over these
245 // functions by making them easier to call.
247 // The first callback, `f1`, is yielded a (pointer, len) pair which can be
248 // passed to a syscall. The `ptr` is valid for `len` items (u16 in this case).
249 // The closure is expected to return what the syscall returns which will be
250 // interpreted by this function to determine if the syscall needs to be invoked
251 // again (with more buffer space).
253 // Once the syscall has completed (errors bail out early) the second closure is
254 // yielded the data which has been read from the syscall. The return value
255 // from this closure is then the return value of the function.
256 fn fill_utf16_buf_base<F1, F2, T>(mut f1: F1, f2: F2) -> Result<T, ()>
257 where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
258 F2: FnOnce(&[u16]) -> T
260 // Start off with a stack buf but then spill over to the heap if we end up
261 // needing more space.
262 let mut stack_buf = [0u16; 512];
263 let mut heap_buf = Vec::new();
265 let mut n = stack_buf.len();
267 let buf = if n <= stack_buf.len() {
270 let extra = n - heap_buf.len();
271 heap_buf.reserve(extra);
276 // This function is typically called on windows API functions which
277 // will return the correct length of the string, but these functions
278 // also return the `0` on error. In some cases, however, the
279 // returned "correct length" may actually be 0!
281 // To handle this case we call `SetLastError` to reset it to 0 and
282 // then check it again if we get the "0 error value". If the "last
283 // error" is still 0 then we interpret it as a 0 length buffer and
284 // not an actual error.
286 let k = match f1(buf.as_mut_ptr(), n as libc::DWORD) {
287 0 if libc::GetLastError() == 0 => 0,
291 if k == n && libc::GetLastError() ==
292 libc::ERROR_INSUFFICIENT_BUFFER as libc::DWORD {
297 return Ok(f2(&buf[..k]))
303 fn fill_utf16_buf<F1, F2, T>(f1: F1, f2: F2) -> IoResult<T>
304 where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
305 F2: FnOnce(&[u16]) -> T
307 fill_utf16_buf_base(f1, f2).map_err(|()| IoError::last_error())
310 fn fill_utf16_buf_new<F1, F2, T>(f1: F1, f2: F2) -> io::Result<T>
311 where F1: FnMut(*mut u16, libc::DWORD) -> libc::DWORD,
312 F2: FnOnce(&[u16]) -> T
314 fill_utf16_buf_base(f1, f2).map_err(|()| io::Error::last_os_error())
317 fn os2path(s: &[u16]) -> Path {
318 // FIXME: this should not be a panicking conversion (aka path reform)
319 Path::new(String::from_utf16(s).unwrap())
322 pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
323 match v.iter().position(|c| *c == 0) {
324 // don't include the 0
330 fn cvt<I: Int>(i: I) -> io::Result<I> {
331 if i == Int::zero() {
332 Err(io::Error::last_os_error())
338 fn ms_to_filetime(ms: u64) -> libc::FILETIME {
339 // A FILETIME is a count of 100 nanosecond intervals, so we multiply by
340 // 10000 b/c there are 10000 intervals in 1 ms
343 dwLowDateTime: ms as u32,
344 dwHighDateTime: (ms >> 32) as u32,