2015-01-27 12:20:58 -08:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
|
use prelude::v1::*;
|
|
|
|
|
|
2015-02-06 09:42:57 -08:00
|
|
|
use io::ErrorKind;
|
2015-02-24 23:27:20 -08:00
|
|
|
use io;
|
2015-04-27 13:44:20 -07:00
|
|
|
use libc::funcs::extra::kernel32::{GetCurrentProcess, DuplicateHandle};
|
2015-02-24 23:27:20 -08:00
|
|
|
use libc::{self, HANDLE};
|
|
|
|
|
use mem;
|
2015-02-06 09:42:57 -08:00
|
|
|
use ptr;
|
|
|
|
|
use sys::cvt;
|
2015-01-27 12:20:58 -08:00
|
|
|
|
|
|
|
|
pub struct Handle(HANDLE);
|
|
|
|
|
|
|
|
|
|
unsafe impl Send for Handle {}
|
|
|
|
|
unsafe impl Sync for Handle {}
|
|
|
|
|
|
|
|
|
|
impl Handle {
|
|
|
|
|
pub fn new(handle: HANDLE) -> Handle {
|
|
|
|
|
Handle(handle)
|
|
|
|
|
}
|
2015-02-06 09:42:57 -08:00
|
|
|
|
2015-02-02 21:39:14 -08:00
|
|
|
pub fn raw(&self) -> HANDLE { self.0 }
|
2015-02-06 09:42:57 -08:00
|
|
|
|
2015-02-24 23:27:20 -08:00
|
|
|
pub fn into_raw(self) -> HANDLE {
|
|
|
|
|
let ret = self.0;
|
|
|
|
|
unsafe { mem::forget(self) }
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 09:42:57 -08:00
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
2015-04-14 11:17:47 -07:00
|
|
|
let mut read = 0;
|
|
|
|
|
let res = cvt(unsafe {
|
|
|
|
|
libc::ReadFile(self.0, buf.as_ptr() as libc::LPVOID,
|
|
|
|
|
buf.len() as libc::DWORD, &mut read,
|
|
|
|
|
ptr::null_mut())
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
match res {
|
|
|
|
|
Ok(_) => Ok(read as usize),
|
|
|
|
|
|
|
|
|
|
// The special treatment of BrokenPipe is to deal with Windows
|
|
|
|
|
// pipe semantics, which yields this error when *reading* from
|
|
|
|
|
// a pipe after the other end has closed; we interpret that as
|
|
|
|
|
// EOF on the pipe.
|
|
|
|
|
Err(ref e) if e.kind() == ErrorKind::BrokenPipe => Ok(0),
|
|
|
|
|
|
|
|
|
|
Err(e) => Err(e)
|
|
|
|
|
}
|
2015-02-06 09:42:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
2015-04-14 11:17:47 -07:00
|
|
|
let mut amt = 0;
|
|
|
|
|
try!(cvt(unsafe {
|
|
|
|
|
libc::WriteFile(self.0, buf.as_ptr() as libc::LPVOID,
|
|
|
|
|
buf.len() as libc::DWORD, &mut amt,
|
|
|
|
|
ptr::null_mut())
|
|
|
|
|
}));
|
|
|
|
|
Ok(amt as usize)
|
2015-02-06 09:42:57 -08:00
|
|
|
}
|
2015-04-27 13:44:20 -07:00
|
|
|
|
|
|
|
|
pub fn duplicate(&self, access: libc::DWORD, inherit: bool,
|
|
|
|
|
options: libc::DWORD) -> io::Result<Handle> {
|
|
|
|
|
let mut ret = 0 as libc::HANDLE;
|
|
|
|
|
try!(cvt(unsafe {
|
|
|
|
|
let cur_proc = GetCurrentProcess();
|
|
|
|
|
DuplicateHandle(cur_proc, self.0, cur_proc, &mut ret,
|
|
|
|
|
access, inherit as libc::BOOL,
|
|
|
|
|
options)
|
|
|
|
|
}));
|
|
|
|
|
Ok(Handle::new(ret))
|
|
|
|
|
}
|
2015-01-27 12:20:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Handle {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe { let _ = libc::CloseHandle(self.0); }
|
|
|
|
|
}
|
|
|
|
|
}
|