2013-03-01 21:57:05 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* Higher-level interfaces to libc::* functions and operating system services.
|
|
|
|
*
|
|
|
|
* In general these take and return rust types, use rust idioms (enums,
|
|
|
|
* closures, vectors) rather than C idioms, and do more extensive safety
|
|
|
|
* checks.
|
|
|
|
*
|
|
|
|
* This module is not meant to only contain 1:1 mappings to libc entries; any
|
|
|
|
* os-interface code that is reasonably useful and broadly applicable can go
|
|
|
|
* here. Including utility routines that merely build on other os code.
|
|
|
|
*
|
|
|
|
* We assume the general case is that users do not care, and do not want to
|
|
|
|
* be made to care, which operating system they are on. While they may want
|
|
|
|
* to special case various special cases -- and so we will not _hide_ the
|
|
|
|
* facts of which OS the user is on -- they should be given the opportunity
|
|
|
|
* to write OS-ignorant code by default.
|
|
|
|
*/
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-12-08 01:55:28 -06:00
|
|
|
#[cfg(target_os = "macos")]
|
2014-02-02 18:32:52 -06:00
|
|
|
#[cfg(windows)]
|
2013-09-08 10:01:16 -05:00
|
|
|
use iter::range;
|
2014-02-02 18:32:52 -06:00
|
|
|
|
|
|
|
use clone::Clone;
|
|
|
|
use container::Container;
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
2014-01-23 13:38:22 -06:00
|
|
|
use libc::{c_char, c_void, c_int};
|
2014-02-02 18:32:52 -06:00
|
|
|
use option::{Some, None, Option};
|
2013-05-24 21:35:29 -05:00
|
|
|
use os;
|
2014-02-02 18:32:52 -06:00
|
|
|
use ops::Drop;
|
|
|
|
use result::{Err, Ok, Result};
|
2012-12-23 16:41:37 -06:00
|
|
|
use ptr;
|
|
|
|
use str;
|
2014-02-02 18:32:52 -06:00
|
|
|
use str::{Str, StrSlice};
|
2013-11-13 04:21:38 -06:00
|
|
|
use fmt;
|
2013-12-12 19:51:54 -06:00
|
|
|
use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
|
2014-02-02 18:32:52 -06:00
|
|
|
use path::{Path, GenericPath};
|
|
|
|
use iter::Iterator;
|
|
|
|
use vec::{Vector, CloneableVector, ImmutableVector, MutableVector, OwnedVector};
|
|
|
|
use ptr::RawPtr;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use c_str::ToCStr;
|
2014-02-14 16:42:40 -06:00
|
|
|
#[cfg(windows)]
|
|
|
|
use str::OwnedStr;
|
2012-03-06 20:49:08 -06:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Delegates to the libc close() function, returning the same return value.
|
2013-12-12 15:27:26 -06:00
|
|
|
pub fn close(fd: int) -> int {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-12-12 15:27:26 -06:00
|
|
|
libc::close(fd as c_int) as int
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static TMPBUF_SZ : uint = 1000u;
|
|
|
|
static BUF_BYTES : uint = 2048u;
|
2012-03-06 20:49:08 -06:00
|
|
|
|
2013-10-20 01:02:03 -05:00
|
|
|
#[cfg(unix)]
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn getcwd() -> Path {
|
2013-12-12 19:51:54 -06:00
|
|
|
use c_str::CString;
|
|
|
|
|
2013-12-12 15:27:26 -06:00
|
|
|
let mut buf = [0 as c_char, ..BUF_BYTES];
|
2013-12-17 09:13:20 -06:00
|
|
|
unsafe {
|
2014-01-23 13:38:22 -06:00
|
|
|
if libc::getcwd(buf.as_mut_ptr(), buf.len() as libc::size_t).is_null() {
|
2013-12-17 09:13:20 -06:00
|
|
|
fail!()
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2013-12-17 09:13:20 -06:00
|
|
|
Path::new(CString::new(buf.as_ptr(), false))
|
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
|
|
|
|
2013-10-20 01:02:03 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn getcwd() -> Path {
|
|
|
|
use libc::DWORD;
|
|
|
|
use libc::GetCurrentDirectoryW;
|
|
|
|
let mut buf = [0 as u16, ..BUF_BYTES];
|
2013-12-17 09:37:30 -06:00
|
|
|
unsafe {
|
|
|
|
if libc::GetCurrentDirectoryW(buf.len() as DWORD, buf.as_mut_ptr()) == 0 as DWORD {
|
|
|
|
fail!();
|
2013-10-20 01:02:03 -05:00
|
|
|
}
|
2013-12-17 09:37:30 -06:00
|
|
|
}
|
2014-02-18 05:25:32 -06:00
|
|
|
Path::new(str::from_utf16(str::truncate_utf16_at_nul(buf))
|
|
|
|
.expect("GetCurrentDirectoryW returned invalid UTF-16"))
|
2013-10-20 01:02:03 -05:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-12-13 15:05:22 -06:00
|
|
|
pub mod win32 {
|
2014-01-06 21:05:53 -06:00
|
|
|
use libc::types::os::arch::extra::DWORD;
|
2013-01-04 17:38:56 -06:00
|
|
|
use libc;
|
2013-01-09 23:14:28 -06:00
|
|
|
use option::{None, Option};
|
2013-01-04 17:38:56 -06:00
|
|
|
use option;
|
2013-02-11 19:07:25 -06:00
|
|
|
use os::TMPBUF_SZ;
|
2014-01-06 21:05:53 -06:00
|
|
|
use str::StrSlice;
|
|
|
|
use str;
|
|
|
|
use vec::{MutableVector, ImmutableVector, OwnedVector};
|
|
|
|
use vec;
|
2012-03-06 20:49:08 -06:00
|
|
|
|
2013-11-18 23:15:42 -06:00
|
|
|
pub fn fill_utf16_buf_and_decode(f: |*mut u16, DWORD| -> DWORD)
|
2012-08-20 14:23:37 -05:00
|
|
|
-> Option<~str> {
|
2013-08-14 20:41:40 -05:00
|
|
|
|
2013-01-10 23:58:30 -06:00
|
|
|
unsafe {
|
2013-02-11 19:07:25 -06:00
|
|
|
let mut n = TMPBUF_SZ as DWORD;
|
2013-01-10 23:58:30 -06:00
|
|
|
let mut res = None;
|
|
|
|
let mut done = false;
|
|
|
|
while !done {
|
2013-02-26 16:50:09 -06:00
|
|
|
let mut buf = vec::from_elem(n as uint, 0u16);
|
2014-02-19 10:11:00 -06:00
|
|
|
let k = f(buf.as_mut_ptr(), n);
|
2013-12-17 09:13:20 -06:00
|
|
|
if k == (0 as DWORD) {
|
|
|
|
done = true;
|
2014-01-19 02:21:14 -06:00
|
|
|
} else if k == n &&
|
|
|
|
libc::GetLastError() ==
|
|
|
|
libc::ERROR_INSUFFICIENT_BUFFER as DWORD {
|
2014-02-18 06:40:25 -06:00
|
|
|
n *= 2 as DWORD;
|
2014-02-19 10:11:00 -06:00
|
|
|
} else if k >= n {
|
|
|
|
n = k;
|
2013-12-17 09:13:20 -06:00
|
|
|
} else {
|
|
|
|
done = true;
|
|
|
|
}
|
2013-02-13 13:21:22 -06:00
|
|
|
if k != 0 && done {
|
2013-06-27 04:48:50 -05:00
|
|
|
let sub = buf.slice(0, k as uint);
|
2014-02-16 16:57:56 -06:00
|
|
|
// We want to explicitly catch the case when the
|
|
|
|
// closure returned invalid UTF-16, rather than
|
|
|
|
// set `res` to None and continue.
|
|
|
|
let s = str::from_utf16(sub)
|
|
|
|
.expect("fill_utf16_buf_and_decode: closure created invalid UTF-16");
|
|
|
|
res = option::Some(s)
|
2013-02-13 13:21:22 -06:00
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2013-01-10 23:58:30 -06:00
|
|
|
return res;
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:15:42 -06:00
|
|
|
pub fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
|
2013-06-13 10:44:15 -05:00
|
|
|
let mut t = s.to_utf16();
|
2012-03-06 20:49:08 -06:00
|
|
|
// Null terminate before passing on.
|
2013-06-27 17:07:16 -05:00
|
|
|
t.push(0u16);
|
2013-12-17 08:49:31 -06:00
|
|
|
f(t.as_ptr())
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-06 18:00:29 -06:00
|
|
|
}
|
|
|
|
|
2013-01-20 00:54:29 -06:00
|
|
|
/*
|
|
|
|
Accessing environment variables is not generally threadsafe.
|
2013-05-08 17:26:07 -05:00
|
|
|
Serialize access through a global lock.
|
2013-01-20 00:54:29 -06:00
|
|
|
*/
|
2013-11-18 23:15:42 -06:00
|
|
|
fn with_env_lock<T>(f: || -> T) -> T {
|
2014-02-14 18:18:49 -06:00
|
|
|
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-02-14 18:18:49 -06:00
|
|
|
static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
|
2013-11-14 02:21:43 -06:00
|
|
|
|
2013-01-20 00:54:29 -06:00
|
|
|
unsafe {
|
2014-02-13 00:17:50 -06:00
|
|
|
let _guard = lock.lock();
|
|
|
|
f()
|
2013-05-08 17:26:07 -05:00
|
|
|
}
|
2012-07-25 16:37:40 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns a vector of (variable, value) pairs for all the environment
|
|
|
|
/// variables of the current process.
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
|
|
|
/// Invalid UTF-8 bytes are replaced with \uFFFD. See `str::from_utf8_lossy()`
|
|
|
|
/// for details.
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn env() -> ~[(~str,~str)] {
|
2014-02-14 17:18:51 -06:00
|
|
|
env_as_bytes().move_iter().map(|(k,v)| {
|
|
|
|
let k = str::from_utf8_lossy(k).into_owned();
|
|
|
|
let v = str::from_utf8_lossy(v).into_owned();
|
|
|
|
(k,v)
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vector of (variable, value) byte-vector pairs for all the
|
|
|
|
/// environment variables of the current process.
|
|
|
|
pub fn env_as_bytes() -> ~[(~[u8],~[u8])] {
|
2013-01-20 00:54:29 -06:00
|
|
|
unsafe {
|
2013-02-15 00:16:53 -06:00
|
|
|
#[cfg(windows)]
|
2014-02-14 17:18:51 -06:00
|
|
|
unsafe fn get_env_pairs() -> ~[~[u8]] {
|
2013-10-17 23:08:48 -05:00
|
|
|
use c_str;
|
|
|
|
use str::StrSlice;
|
2013-08-14 20:41:40 -05:00
|
|
|
|
2013-02-15 00:16:53 -06:00
|
|
|
use libc::funcs::extra::kernel32::{
|
|
|
|
GetEnvironmentStringsA,
|
|
|
|
FreeEnvironmentStringsA
|
|
|
|
};
|
|
|
|
let ch = GetEnvironmentStringsA();
|
2014-01-19 02:21:14 -06:00
|
|
|
if ch as uint == 0 {
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!("os::env() failure getting env string from OS: {}",
|
2013-09-27 19:02:31 -05:00
|
|
|
os::last_os_error());
|
2013-02-15 00:16:53 -06:00
|
|
|
}
|
2013-10-16 18:48:30 -05:00
|
|
|
let mut result = ~[];
|
2013-12-12 15:27:26 -06:00
|
|
|
c_str::from_c_multistring(ch as *c_char, None, |cstr| {
|
2014-02-14 17:18:51 -06:00
|
|
|
result.push(cstr.as_bytes_no_nul().to_owned());
|
2013-11-20 16:17:12 -06:00
|
|
|
});
|
2013-02-15 00:16:53 -06:00
|
|
|
FreeEnvironmentStringsA(ch);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
2014-02-14 17:18:51 -06:00
|
|
|
unsafe fn get_env_pairs() -> ~[~[u8]] {
|
|
|
|
use c_str::CString;
|
|
|
|
|
2013-05-09 16:14:42 -05:00
|
|
|
extern {
|
2013-12-12 15:27:26 -06:00
|
|
|
fn rust_env_pairs() -> **c_char;
|
2013-02-15 00:16:53 -06:00
|
|
|
}
|
2013-05-09 16:14:42 -05:00
|
|
|
let environ = rust_env_pairs();
|
2014-01-19 02:21:14 -06:00
|
|
|
if environ as uint == 0 {
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!("os::env() failure getting env string from OS: {}",
|
2013-09-27 19:02:31 -05:00
|
|
|
os::last_os_error());
|
2013-02-15 00:16:53 -06:00
|
|
|
}
|
|
|
|
let mut result = ~[];
|
|
|
|
ptr::array_each(environ, |e| {
|
2014-02-14 17:18:51 -06:00
|
|
|
let env_pair = CString::new(e, false).as_bytes_no_nul().to_owned();
|
2013-02-15 00:16:53 -06:00
|
|
|
result.push(env_pair);
|
|
|
|
});
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2014-02-14 17:18:51 -06:00
|
|
|
fn env_convert(input: ~[~[u8]]) -> ~[(~[u8], ~[u8])] {
|
2013-01-20 00:54:29 -06:00
|
|
|
let mut pairs = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for p in input.iter() {
|
2014-02-14 17:18:51 -06:00
|
|
|
let vs: ~[&[u8]] = p.splitn(1, |b| *b == '=' as u8).collect();
|
|
|
|
let key = vs[0].to_owned();
|
2014-02-18 06:40:25 -06:00
|
|
|
let val = if vs.len() < 2 { ~[] } else { vs[1].to_owned() };
|
2014-02-14 17:18:51 -06:00
|
|
|
pairs.push((key, val));
|
2012-04-30 19:42:41 -05:00
|
|
|
}
|
2013-02-15 02:51:28 -06:00
|
|
|
pairs
|
2012-04-30 19:42:41 -05:00
|
|
|
}
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
2013-02-15 00:16:53 -06:00
|
|
|
let unparsed_environ = get_env_pairs();
|
|
|
|
env_convert(unparsed_environ)
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
2012-04-30 19:42:41 -05:00
|
|
|
}
|
2013-01-20 00:54:29 -06:00
|
|
|
}
|
2012-04-30 19:42:41 -05:00
|
|
|
|
2013-01-20 00:54:29 -06:00
|
|
|
#[cfg(unix)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Fetches the environment variable `n` from the current process, returning
|
|
|
|
/// None if the variable isn't set.
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
|
|
|
/// Any invalid UTF-8 bytes in the value are replaced by \uFFFD. See
|
|
|
|
/// `str::from_utf8_lossy()` for details.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if `n` has any interior NULs.
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn getenv(n: &str) -> Option<~str> {
|
2014-02-14 17:18:51 -06:00
|
|
|
getenv_as_bytes(n).map(|v| str::from_utf8_lossy(v).into_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
/// Fetches the environment variable `n` byte vector from the current process,
|
|
|
|
/// returning None if the variable isn't set.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if `n` has any interior NULs.
|
|
|
|
pub fn getenv_as_bytes(n: &str) -> Option<~[u8]> {
|
|
|
|
use c_str::CString;
|
|
|
|
|
2013-01-20 00:54:29 -06:00
|
|
|
unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
|
|
|
let s = n.with_c_str(|buf| libc::getenv(buf));
|
2013-07-07 11:42:38 -05:00
|
|
|
if s.is_null() {
|
2013-07-22 23:41:46 -05:00
|
|
|
None
|
2013-01-20 00:54:29 -06:00
|
|
|
} else {
|
2014-02-14 17:18:51 -06:00
|
|
|
Some(CString::new(s, false).as_bytes_no_nul().to_owned())
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
2013-01-20 00:54:29 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 19:42:41 -05:00
|
|
|
|
2013-01-20 00:54:29 -06:00
|
|
|
#[cfg(windows)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Fetches the environment variable `n` from the current process, returning
|
|
|
|
/// None if the variable isn't set.
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn getenv(n: &str) -> Option<~str> {
|
|
|
|
unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
2013-01-20 00:54:29 -06:00
|
|
|
use os::win32::{as_utf16_p, fill_utf16_buf_and_decode};
|
2013-11-20 16:17:12 -06:00
|
|
|
as_utf16_p(n, |u| {
|
|
|
|
fill_utf16_buf_and_decode(|buf, sz| {
|
2013-01-20 00:54:29 -06:00
|
|
|
libc::GetEnvironmentVariableW(u, buf, sz)
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2013-01-20 00:54:29 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 19:42:41 -05:00
|
|
|
|
2014-02-14 17:18:51 -06:00
|
|
|
#[cfg(windows)]
|
|
|
|
/// Fetches the environment variable `n` byte vector from the current process,
|
|
|
|
/// returning None if the variable isn't set.
|
|
|
|
pub fn getenv_as_bytes(n: &str) -> Option<~[u8]> {
|
|
|
|
getenv(n).map(|s| s.into_bytes())
|
|
|
|
}
|
|
|
|
|
2012-04-30 19:42:41 -05:00
|
|
|
|
2013-01-20 00:54:29 -06:00
|
|
|
#[cfg(unix)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Sets the environment variable `n` to the value `v` for the currently running
|
|
|
|
/// process
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails if `n` or `v` have any interior NULs.
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn setenv(n: &str, v: &str) {
|
|
|
|
unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
|
|
|
n.with_c_str(|nbuf| {
|
|
|
|
v.with_c_str(|vbuf| {
|
2013-01-20 00:54:29 -06:00
|
|
|
libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2013-01-20 00:54:29 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 19:42:41 -05:00
|
|
|
|
|
|
|
|
2013-01-20 00:54:29 -06:00
|
|
|
#[cfg(windows)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Sets the environment variable `n` to the value `v` for the currently running
|
|
|
|
/// process
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn setenv(n: &str, v: &str) {
|
|
|
|
unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
2013-01-20 00:54:29 -06:00
|
|
|
use os::win32::as_utf16_p;
|
2013-11-20 16:17:12 -06:00
|
|
|
as_utf16_p(n, |nbuf| {
|
|
|
|
as_utf16_p(v, |vbuf| {
|
2013-01-20 00:54:29 -06:00
|
|
|
libc::SetEnvironmentVariableW(nbuf, vbuf);
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2012-04-30 19:42:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 12:39:53 -05:00
|
|
|
/// Remove a variable from the environment entirely
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Fails (on unix) if `n` has any interior NULs.
|
2013-05-15 12:39:53 -05:00
|
|
|
pub fn unsetenv(n: &str) {
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn _unsetenv(n: &str) {
|
|
|
|
unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
|
|
|
n.with_c_str(|nbuf| {
|
2013-05-15 12:39:53 -05:00
|
|
|
libc::funcs::posix01::unistd::unsetenv(nbuf);
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
|
|
|
})
|
2013-05-15 12:39:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn _unsetenv(n: &str) {
|
|
|
|
unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
2013-05-15 12:39:53 -05:00
|
|
|
use os::win32::as_utf16_p;
|
2013-11-20 16:17:12 -06:00
|
|
|
as_utf16_p(n, |nbuf| {
|
2013-05-15 12:39:53 -05:00
|
|
|
libc::SetEnvironmentVariableW(nbuf, ptr::null());
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
|
|
|
})
|
2013-05-15 12:39:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_unsetenv(n);
|
|
|
|
}
|
|
|
|
|
2013-05-03 01:12:43 -05:00
|
|
|
pub struct Pipe {
|
2013-07-31 16:59:59 -05:00
|
|
|
input: c_int,
|
2013-05-03 01:12:43 -05:00
|
|
|
out: c_int
|
|
|
|
}
|
2013-02-07 20:23:47 -06:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2013-02-07 20:23:47 -06:00
|
|
|
pub fn pipe() -> Pipe {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-12-12 15:27:26 -06:00
|
|
|
let mut fds = Pipe {input: 0,
|
|
|
|
out: 0};
|
|
|
|
assert_eq!(libc::pipe(&mut fds.input), 0);
|
2013-07-31 16:59:59 -05:00
|
|
|
return Pipe {input: fds.input, out: fds.out};
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2013-02-07 20:23:47 -06:00
|
|
|
pub fn pipe() -> Pipe {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
// Windows pipes work subtly differently than unix pipes, and their
|
|
|
|
// inheritance has to be handled in a different way that I do not
|
|
|
|
// fully understand. Here we explicitly make the pipe non-inheritable,
|
|
|
|
// which means to pass it to a subprocess they need to be duplicated
|
2013-08-16 00:54:14 -05:00
|
|
|
// first, as in std::run.
|
2013-12-12 15:27:26 -06:00
|
|
|
let mut fds = Pipe {input: 0,
|
|
|
|
out: 0};
|
2013-07-31 16:59:59 -05:00
|
|
|
let res = libc::pipe(&mut fds.input, 1024 as ::libc::c_uint,
|
2013-01-10 23:23:07 -06:00
|
|
|
(libc::O_BINARY | libc::O_NOINHERIT) as c_int);
|
2013-12-12 15:27:26 -06:00
|
|
|
assert_eq!(res, 0);
|
|
|
|
assert!((fds.input != -1 && fds.input != 0 ));
|
|
|
|
assert!((fds.out != -1 && fds.input != 0));
|
2013-07-31 16:59:59 -05:00
|
|
|
return Pipe {input: fds.input, out: fds.out};
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns the proper dll filename for the given basename of a file.
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn dll_filename(base: &str) -> ~str {
|
2013-12-18 11:57:00 -06:00
|
|
|
format!("{}{}{}", consts::DLL_PREFIX, base, consts::DLL_SUFFIX)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-01-22 15:41:53 -06:00
|
|
|
/// Optionally returns the filesystem path of the current executable which is
|
2013-05-28 16:35:52 -05:00
|
|
|
/// running. If any failure occurs, None is returned.
|
2014-01-22 15:41:53 -06:00
|
|
|
pub fn self_exe_name() -> Option<Path> {
|
2012-03-02 19:20:00 -06:00
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2013-09-26 19:21:59 -05:00
|
|
|
fn load_self() -> Option<~[u8]> {
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
2012-09-07 20:08:21 -05:00
|
|
|
use libc::funcs::bsd44::*;
|
|
|
|
use libc::consts::os::extra::*;
|
2013-10-31 17:15:30 -05:00
|
|
|
use vec;
|
2013-09-26 19:21:59 -05:00
|
|
|
let mib = ~[CTL_KERN as c_int,
|
|
|
|
KERN_PROC as c_int,
|
|
|
|
KERN_PROC_PATHNAME as c_int, -1 as c_int];
|
2014-01-23 13:38:22 -06:00
|
|
|
let mut sz: libc::size_t = 0;
|
2013-12-15 06:35:12 -06:00
|
|
|
let err = sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint,
|
2014-01-23 13:38:22 -06:00
|
|
|
ptr::mut_null(), &mut sz, ptr::null(),
|
|
|
|
0u as libc::size_t);
|
2013-09-26 19:21:59 -05:00
|
|
|
if err != 0 { return None; }
|
|
|
|
if sz == 0 { return None; }
|
|
|
|
let mut v: ~[u8] = vec::with_capacity(sz as uint);
|
2013-12-17 09:13:20 -06:00
|
|
|
let err = sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint,
|
2014-01-23 13:38:22 -06:00
|
|
|
v.as_mut_ptr() as *mut c_void, &mut sz, ptr::null(),
|
|
|
|
0u as libc::size_t);
|
2013-09-26 19:21:59 -05:00
|
|
|
if err != 0 { return None; }
|
|
|
|
if sz == 0 { return None; }
|
2013-12-15 06:05:30 -06:00
|
|
|
v.set_len(sz as uint - 1); // chop off trailing NUL
|
2013-09-26 19:21:59 -05:00
|
|
|
Some(v)
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2013-09-26 19:21:59 -05:00
|
|
|
fn load_self() -> Option<~[u8]> {
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io;
|
2013-07-07 15:30:48 -05:00
|
|
|
|
2014-01-30 18:55:20 -06:00
|
|
|
match io::fs::readlink(&Path::new("/proc/self/exe")) {
|
|
|
|
Ok(path) => Some(path.as_vec().to_owned()),
|
|
|
|
Err(..) => None
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 20:49:08 -06:00
|
|
|
#[cfg(target_os = "macos")]
|
2013-09-26 19:21:59 -05:00
|
|
|
fn load_self() -> Option<~[u8]> {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-09-26 19:21:59 -05:00
|
|
|
use libc::funcs::extra::_NSGetExecutablePath;
|
2013-10-31 17:15:30 -05:00
|
|
|
use vec;
|
2013-09-26 19:21:59 -05:00
|
|
|
let mut sz: u32 = 0;
|
|
|
|
_NSGetExecutablePath(ptr::mut_null(), &mut sz);
|
|
|
|
if sz == 0 { return None; }
|
|
|
|
let mut v: ~[u8] = vec::with_capacity(sz as uint);
|
2013-12-17 09:13:20 -06:00
|
|
|
let err = _NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz);
|
2013-09-26 19:21:59 -05:00
|
|
|
if err != 0 { return None; }
|
2013-12-15 06:05:30 -06:00
|
|
|
v.set_len(sz as uint - 1); // chop off trailing NUL
|
2013-09-26 19:21:59 -05:00
|
|
|
Some(v)
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2013-09-26 19:21:59 -05:00
|
|
|
fn load_self() -> Option<~[u8]> {
|
2014-02-02 18:32:52 -06:00
|
|
|
use str::OwnedStr;
|
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use os::win32::fill_utf16_buf_and_decode;
|
2013-11-20 16:17:12 -06:00
|
|
|
fill_utf16_buf_and_decode(|buf, sz| {
|
2013-01-10 23:23:07 -06:00
|
|
|
libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
|
2013-11-20 16:17:12 -06:00
|
|
|
}).map(|s| s.into_bytes())
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-01-22 15:41:53 -06:00
|
|
|
load_self().and_then(Path::new_opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Optionally returns the filesystem path to the current executable which is
|
|
|
|
/// running. Like self_exe_name() but without the binary's name.
|
|
|
|
/// If any failure occurs, None is returned.
|
|
|
|
pub fn self_exe_path() -> Option<Path> {
|
|
|
|
self_exe_name().map(|mut p| { p.pop(); p })
|
2013-09-26 19:21:59 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns the path to the user's home directory, if known.
|
|
|
|
*
|
|
|
|
* On Unix, returns the value of the 'HOME' environment variable if it is set
|
|
|
|
* and not equal to the empty string.
|
|
|
|
*
|
|
|
|
* On Windows, returns the value of the 'HOME' environment variable if it is
|
|
|
|
* set and not equal to the empty string. Otherwise, returns the value of the
|
|
|
|
* 'USERPROFILE' environment variable if it is set and not equal to the empty
|
|
|
|
* string.
|
|
|
|
*
|
|
|
|
* Otherwise, homedir returns option::none.
|
|
|
|
*/
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn homedir() -> Option<Path> {
|
2013-09-26 19:21:59 -05:00
|
|
|
// FIXME (#7188): getenv needs a ~[u8] variant
|
2013-05-19 00:07:44 -05:00
|
|
|
return match getenv("HOME") {
|
2013-12-03 21:15:12 -06:00
|
|
|
Some(ref p) if !p.is_empty() => Path::new_opt(p.as_slice()),
|
2013-09-26 19:21:59 -05:00
|
|
|
_ => secondary()
|
2012-03-02 19:20:00 -06:00
|
|
|
};
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn secondary() -> Option<Path> {
|
|
|
|
None
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn secondary() -> Option<Path> {
|
2013-11-20 16:17:12 -06:00
|
|
|
getenv("USERPROFILE").and_then(|p| {
|
2013-06-09 09:44:58 -05:00
|
|
|
if !p.is_empty() {
|
2013-12-03 21:15:12 -06:00
|
|
|
Path::new_opt(p)
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 21:04:26 -05:00
|
|
|
/**
|
2012-08-21 14:23:46 -05:00
|
|
|
* Returns the path to a temporary directory.
|
2012-08-20 21:04:26 -05:00
|
|
|
*
|
|
|
|
* On Unix, returns the value of the 'TMPDIR' environment variable if it is
|
|
|
|
* set and non-empty and '/tmp' otherwise.
|
2013-09-01 06:31:05 -05:00
|
|
|
* On Android, there is no global temporary folder (it is usually allocated
|
|
|
|
* per-app), hence returns '/data/tmp' which is commonly used.
|
2012-08-20 21:04:26 -05:00
|
|
|
*
|
|
|
|
* On Windows, returns the value of, in order, the 'TMP', 'TEMP',
|
2012-08-21 14:23:46 -05:00
|
|
|
* 'USERPROFILE' environment variable if any are set and not the empty
|
|
|
|
* string. Otherwise, tmpdir returns the path to the Windows directory.
|
2012-08-20 21:04:26 -05:00
|
|
|
*/
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn tmpdir() -> Path {
|
2012-08-20 21:04:26 -05:00
|
|
|
return lookup();
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn getenv_nonempty(v: &str) -> Option<Path> {
|
2012-08-20 21:04:26 -05:00
|
|
|
match getenv(v) {
|
2013-02-15 02:51:28 -06:00
|
|
|
Some(x) =>
|
2013-06-09 09:44:58 -05:00
|
|
|
if x.is_empty() {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2012-08-20 21:04:26 -05:00
|
|
|
} else {
|
2013-12-03 21:15:12 -06:00
|
|
|
Path::new_opt(x)
|
2012-08-20 21:04:26 -05:00
|
|
|
},
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => None
|
2012-08-20 21:04:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
2012-08-21 14:23:46 -05:00
|
|
|
fn lookup() -> Path {
|
2013-09-01 06:31:05 -05:00
|
|
|
if cfg!(target_os = "android") {
|
2013-12-03 21:15:12 -06:00
|
|
|
Path::new("/data/tmp")
|
2013-09-01 06:31:05 -05:00
|
|
|
} else {
|
2013-12-03 21:15:12 -06:00
|
|
|
getenv_nonempty("TMPDIR").unwrap_or(Path::new("/tmp"))
|
2013-09-01 06:31:05 -05:00
|
|
|
}
|
2012-08-20 21:04:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2012-08-21 14:23:46 -05:00
|
|
|
fn lookup() -> Path {
|
2013-03-16 14:49:12 -05:00
|
|
|
getenv_nonempty("TMP").or(
|
|
|
|
getenv_nonempty("TEMP").or(
|
|
|
|
getenv_nonempty("USERPROFILE").or(
|
2013-12-03 21:15:12 -06:00
|
|
|
getenv_nonempty("WINDIR")))).unwrap_or(Path::new("C:\\Windows"))
|
2012-08-20 21:04:26 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 21:46:54 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Convert a relative path to an absolute path
|
|
|
|
*
|
|
|
|
* If the given path is relative, return it prepended with the current working
|
|
|
|
* directory. If the given path is already an absolute path, return it
|
2013-03-01 21:57:05 -06:00
|
|
|
* as is.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-03-02 19:20:00 -06:00
|
|
|
// NB: this is here rather than in path because it is a form of environment
|
|
|
|
// querying; what it does depends on the process working directory, not just
|
|
|
|
// the input paths.
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn make_absolute(p: &Path) -> Path {
|
2013-09-26 19:21:59 -05:00
|
|
|
if p.is_absolute() {
|
|
|
|
p.clone()
|
2013-03-01 21:57:05 -06:00
|
|
|
} else {
|
2013-09-26 19:21:59 -05:00
|
|
|
let mut ret = getcwd();
|
2013-10-07 21:16:58 -05:00
|
|
|
ret.push(p);
|
2013-09-26 19:21:59 -05:00
|
|
|
ret
|
2013-03-01 21:57:05 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Changes the current working directory to the specified path, returning
|
|
|
|
/// whether the change was completed successfully or not.
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn change_dir(p: &Path) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return chdir(p);
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn chdir(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use os::win32::as_utf16_p;
|
2013-11-20 16:17:12 -06:00
|
|
|
return as_utf16_p(p.as_str().unwrap(), |buf| {
|
2013-01-10 23:23:07 -06:00
|
|
|
libc::SetCurrentDirectoryW(buf) != (0 as libc::BOOL)
|
2013-11-20 16:17:12 -06:00
|
|
|
});
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn chdir(p: &Path) -> bool {
|
2013-11-20 16:17:12 -06:00
|
|
|
p.with_c_str(|buf| {
|
2013-06-30 10:25:16 -05:00
|
|
|
unsafe {
|
2013-01-10 23:23:07 -06:00
|
|
|
libc::chdir(buf) == (0 as c_int)
|
2013-06-30 10:25:16 -05:00
|
|
|
}
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-11 19:07:25 -06:00
|
|
|
#[cfg(unix)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns the platform-specific value of errno
|
2013-02-11 19:07:25 -06:00
|
|
|
pub fn errno() -> int {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
fn errno_location() -> *c_int {
|
|
|
|
extern {
|
2013-08-02 16:30:00 -05:00
|
|
|
fn __error() -> *c_int;
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
__error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
fn errno_location() -> *c_int {
|
|
|
|
extern {
|
2013-08-02 16:30:00 -05:00
|
|
|
fn __errno_location() -> *c_int;
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
__errno_location()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
(*errno_location()) as int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns the platform-specific value of errno
|
2013-02-11 19:07:25 -06:00
|
|
|
pub fn errno() -> uint {
|
|
|
|
use libc::types::os::arch::extra::DWORD;
|
|
|
|
|
|
|
|
#[link_name = "kernel32"]
|
2013-11-10 16:57:53 -06:00
|
|
|
extern "system" {
|
2013-08-12 01:27:46 -05:00
|
|
|
fn GetLastError() -> DWORD;
|
|
|
|
}
|
|
|
|
|
2013-02-11 19:07:25 -06:00
|
|
|
unsafe {
|
2013-02-11 22:07:29 -06:00
|
|
|
GetLastError() as uint
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Get a string representing the platform-dependent last error
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn last_os_error() -> ~str {
|
2013-02-11 19:07:25 -06:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn strerror() -> ~str {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2014-01-23 13:38:22 -06:00
|
|
|
fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: libc::size_t)
|
2013-08-02 16:30:00 -05:00
|
|
|
-> c_int {
|
2013-02-11 19:07:25 -06:00
|
|
|
extern {
|
2014-01-23 13:38:22 -06:00
|
|
|
fn strerror_r(errnum: c_int, buf: *mut c_char,
|
|
|
|
buflen: libc::size_t) -> c_int;
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
strerror_r(errnum, buf, buflen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GNU libc provides a non-compliant version of strerror_r by default
|
|
|
|
// and requires macros to instead use the POSIX compliant variant.
|
2013-02-11 22:49:20 -06:00
|
|
|
// So we just use __xpg_strerror_r which is always POSIX compliant
|
2013-02-11 19:07:25 -06:00
|
|
|
#[cfg(target_os = "linux")]
|
2014-01-23 13:38:22 -06:00
|
|
|
fn strerror_r(errnum: c_int, buf: *mut c_char,
|
|
|
|
buflen: libc::size_t) -> c_int {
|
2013-02-11 19:07:25 -06:00
|
|
|
extern {
|
2013-08-02 16:30:00 -05:00
|
|
|
fn __xpg_strerror_r(errnum: c_int,
|
|
|
|
buf: *mut c_char,
|
2014-01-23 13:38:22 -06:00
|
|
|
buflen: libc::size_t)
|
2013-08-02 16:30:00 -05:00
|
|
|
-> c_int;
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
__xpg_strerror_r(errnum, buf, buflen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut buf = [0 as c_char, ..TMPBUF_SZ];
|
2013-02-11 22:07:29 -06:00
|
|
|
|
2013-12-17 09:13:20 -06:00
|
|
|
let p = buf.as_mut_ptr();
|
|
|
|
unsafe {
|
2014-01-23 13:38:22 -06:00
|
|
|
if strerror_r(errno() as c_int, p, buf.len() as libc::size_t) < 0 {
|
2013-12-17 09:13:20 -06:00
|
|
|
fail!("strerror_r failure");
|
2013-07-07 15:30:48 -05:00
|
|
|
}
|
2013-12-17 09:13:20 -06:00
|
|
|
|
|
|
|
str::raw::from_c_str(p as *c_char)
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-02-11 19:07:25 -06:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn strerror() -> ~str {
|
2013-02-11 22:07:29 -06:00
|
|
|
use libc::types::os::arch::extra::DWORD;
|
2013-09-15 10:02:58 -05:00
|
|
|
use libc::types::os::arch::extra::LPWSTR;
|
2013-02-11 22:07:29 -06:00
|
|
|
use libc::types::os::arch::extra::LPVOID;
|
2013-09-15 10:02:58 -05:00
|
|
|
use libc::types::os::arch::extra::WCHAR;
|
2013-02-11 22:07:29 -06:00
|
|
|
|
|
|
|
#[link_name = "kernel32"]
|
2013-11-10 16:57:53 -06:00
|
|
|
extern "system" {
|
2013-09-15 10:02:58 -05:00
|
|
|
fn FormatMessageW(flags: DWORD,
|
2013-08-12 01:27:46 -05:00
|
|
|
lpSrc: LPVOID,
|
|
|
|
msgId: DWORD,
|
|
|
|
langId: DWORD,
|
2013-09-15 10:02:58 -05:00
|
|
|
buf: LPWSTR,
|
2013-08-12 01:27:46 -05:00
|
|
|
nsize: DWORD,
|
|
|
|
args: *c_void)
|
|
|
|
-> DWORD;
|
|
|
|
}
|
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
static FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
|
|
|
|
static FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
|
2013-02-11 22:07:29 -06:00
|
|
|
|
|
|
|
// This value is calculated from the macro
|
|
|
|
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
|
|
|
|
let langId = 0x0800 as DWORD;
|
|
|
|
let err = errno() as DWORD;
|
|
|
|
|
2013-09-15 10:02:58 -05:00
|
|
|
let mut buf = [0 as WCHAR, ..TMPBUF_SZ];
|
2013-07-07 15:30:48 -05:00
|
|
|
|
2013-02-11 19:07:25 -06:00
|
|
|
unsafe {
|
2013-12-17 09:13:20 -06:00
|
|
|
let res = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
ptr::mut_null(),
|
|
|
|
err,
|
|
|
|
langId,
|
|
|
|
buf.as_mut_ptr(),
|
|
|
|
buf.len() as DWORD,
|
|
|
|
ptr::null());
|
|
|
|
if res == 0 {
|
|
|
|
fail!("[{}] FormatMessage failure", errno());
|
|
|
|
}
|
2013-02-11 22:07:29 -06:00
|
|
|
|
2014-02-18 05:25:32 -06:00
|
|
|
str::from_utf16(str::truncate_utf16_at_nul(buf))
|
|
|
|
.expect("FormatMessageW returned invalid UTF-16")
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strerror()
|
2012-04-19 03:23:00 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-12-12 19:51:54 -06:00
|
|
|
static mut EXIT_STATUS: AtomicInt = INIT_ATOMIC_INT;
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Sets the process exit code
|
|
|
|
*
|
|
|
|
* Sets the exit code returned by the process if all supervised tasks
|
|
|
|
* terminate successfully (without failing). If the current root task fails
|
|
|
|
* and is supervised by the scheduler then any user-specified exit status is
|
2013-12-12 19:51:54 -06:00
|
|
|
* ignored and the process exits with the default failure status.
|
|
|
|
*
|
|
|
|
* Note that this is not synchronized against modifications of other threads.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn set_exit_status(code: int) {
|
2013-12-12 19:51:54 -06:00
|
|
|
unsafe { EXIT_STATUS.store(code, SeqCst) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetches the process's current exit code. This defaults to 0 and can change
|
|
|
|
/// by calling `set_exit_status`.
|
|
|
|
pub fn get_exit_status() -> int {
|
|
|
|
unsafe { EXIT_STATUS.load(SeqCst) }
|
2012-04-19 03:26:17 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-12-08 01:55:28 -06:00
|
|
|
#[cfg(target_os = "macos")]
|
2014-02-14 16:42:40 -06:00
|
|
|
unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~[u8]] {
|
|
|
|
use c_str::CString;
|
|
|
|
|
2012-10-03 16:41:53 -05:00
|
|
|
let mut args = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, argc as uint) {
|
2014-02-14 16:42:40 -06:00
|
|
|
args.push(CString::new(*argv.offset(i as int), false).as_bytes_no_nul().to_owned())
|
2012-10-03 16:41:53 -05:00
|
|
|
}
|
2013-02-15 02:51:28 -06:00
|
|
|
args
|
2012-10-03 16:41:53 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:34:53 -05:00
|
|
|
/**
|
|
|
|
* Returns the command line arguments
|
|
|
|
*
|
|
|
|
* Returns a list of the command line arguments.
|
|
|
|
*/
|
|
|
|
#[cfg(target_os = "macos")]
|
2014-02-14 16:42:40 -06:00
|
|
|
fn real_args_as_bytes() -> ~[~[u8]] {
|
2012-10-01 16:34:53 -05:00
|
|
|
unsafe {
|
2013-12-12 15:27:26 -06:00
|
|
|
let (argc, argv) = (*_NSGetArgc() as int,
|
2012-10-03 16:41:53 -05:00
|
|
|
*_NSGetArgv() as **c_char);
|
|
|
|
load_argc_and_argv(argc, argv)
|
2012-10-01 16:34:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 17:34:18 -05:00
|
|
|
#[cfg(target_os = "linux")]
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2012-10-02 12:31:37 -05:00
|
|
|
#[cfg(target_os = "freebsd")]
|
2014-02-14 16:42:40 -06:00
|
|
|
fn real_args_as_bytes() -> ~[~[u8]] {
|
2013-07-02 19:36:58 -05:00
|
|
|
use rt;
|
|
|
|
|
2013-08-01 01:12:20 -05:00
|
|
|
match rt::args::clone() {
|
|
|
|
Some(args) => args,
|
2013-10-21 15:08:31 -05:00
|
|
|
None => fail!("process arguments not initialized")
|
2012-10-03 16:41:53 -05:00
|
|
|
}
|
2012-10-02 12:31:37 -05:00
|
|
|
}
|
|
|
|
|
2014-02-14 16:42:40 -06:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn real_args() -> ~[~str] {
|
|
|
|
real_args_as_bytes().move_iter().map(|v| str::from_utf8_lossy(v).into_owned()).collect()
|
|
|
|
}
|
|
|
|
|
2012-10-01 17:34:18 -05:00
|
|
|
#[cfg(windows)]
|
2013-08-22 04:41:33 -05:00
|
|
|
fn real_args() -> ~[~str] {
|
2013-10-31 17:15:30 -05:00
|
|
|
use vec;
|
2013-08-14 20:41:40 -05:00
|
|
|
|
2012-10-01 18:34:54 -05:00
|
|
|
let mut nArgs: c_int = 0;
|
2013-06-08 14:02:32 -05:00
|
|
|
let lpArgCount: *mut c_int = &mut nArgs;
|
2013-01-24 22:38:43 -06:00
|
|
|
let lpCmdLine = unsafe { GetCommandLineW() };
|
|
|
|
let szArgList = unsafe { CommandLineToArgvW(lpCmdLine, lpArgCount) };
|
2012-10-01 18:34:54 -05:00
|
|
|
|
|
|
|
let mut args = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, nArgs as uint) {
|
2012-10-01 18:34:54 -05:00
|
|
|
unsafe {
|
|
|
|
// Determine the length of this argument.
|
2013-07-29 23:33:52 -05:00
|
|
|
let ptr = *szArgList.offset(i as int);
|
2012-10-01 18:34:54 -05:00
|
|
|
let mut len = 0;
|
2013-07-29 23:33:52 -05:00
|
|
|
while *ptr.offset(len as int) != 0 { len += 1; }
|
2012-10-01 18:34:54 -05:00
|
|
|
|
|
|
|
// Push it onto the list.
|
2014-02-18 05:25:32 -06:00
|
|
|
let opt_s = vec::raw::buf_as_slice(ptr, len, |buf| {
|
|
|
|
str::from_utf16(str::truncate_utf16_at_nul(buf))
|
|
|
|
});
|
2014-02-16 16:57:56 -06:00
|
|
|
args.push(opt_s.expect("CommandLineToArgvW returned invalid UTF-16"));
|
2012-10-01 18:34:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2013-07-07 11:42:38 -05:00
|
|
|
LocalFree(szArgList as *c_void);
|
2012-10-01 18:34:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2014-02-14 16:42:40 -06:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn real_args_as_bytes() -> ~[~[u8]] {
|
|
|
|
real_args().move_iter().map(|s| s.into_bytes()).collect()
|
|
|
|
}
|
|
|
|
|
2012-10-01 18:34:54 -05:00
|
|
|
type LPCWSTR = *u16;
|
|
|
|
|
2013-11-10 16:57:53 -06:00
|
|
|
#[cfg(windows)]
|
2013-08-12 01:27:46 -05:00
|
|
|
#[link_name="kernel32"]
|
2013-11-10 16:57:53 -06:00
|
|
|
extern "system" {
|
2013-08-12 01:27:46 -05:00
|
|
|
fn GetCommandLineW() -> LPCWSTR;
|
|
|
|
fn LocalFree(ptr: *c_void);
|
|
|
|
}
|
|
|
|
|
2013-11-10 16:57:53 -06:00
|
|
|
#[cfg(windows)]
|
2013-08-12 01:27:46 -05:00
|
|
|
#[link_name="shell32"]
|
2013-11-10 16:57:53 -06:00
|
|
|
extern "system" {
|
2013-08-12 01:27:46 -05:00
|
|
|
fn CommandLineToArgvW(lpCmdLine: LPCWSTR, pNumArgs: *mut c_int) -> **u16;
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns the arguments which this program was started with (normally passed
|
|
|
|
/// via the command line).
|
2014-02-14 16:42:40 -06:00
|
|
|
///
|
|
|
|
/// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD.
|
|
|
|
/// See `str::from_utf8_lossy` for details.
|
2012-10-01 17:34:18 -05:00
|
|
|
pub fn args() -> ~[~str] {
|
2013-08-22 04:41:33 -05:00
|
|
|
real_args()
|
2012-10-01 17:34:18 -05:00
|
|
|
}
|
|
|
|
|
2014-02-14 16:42:40 -06:00
|
|
|
/// Returns the arguments which this program was started with (normally passed
|
|
|
|
/// via the command line) as byte vectors.
|
|
|
|
pub fn args_as_bytes() -> ~[~[u8]] {
|
|
|
|
real_args_as_bytes()
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:34:53 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
extern {
|
|
|
|
// These functions are in crt_externs.h.
|
|
|
|
pub fn _NSGetArgc() -> *c_int;
|
|
|
|
pub fn _NSGetArgv() -> ***c_char;
|
|
|
|
}
|
|
|
|
|
2013-06-26 10:42:11 -05:00
|
|
|
// Round up `from` to be divisible by `to`
|
|
|
|
fn round_up(from: uint, to: uint) -> uint {
|
|
|
|
let r = if from % to == 0 {
|
|
|
|
from
|
|
|
|
} else {
|
|
|
|
from + to - (from % to)
|
|
|
|
};
|
|
|
|
if r == 0 {
|
|
|
|
to
|
|
|
|
} else {
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn page_size() -> uint {
|
|
|
|
unsafe {
|
|
|
|
libc::sysconf(libc::_SC_PAGESIZE) as uint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn page_size() -> uint {
|
2013-08-28 03:41:26 -05:00
|
|
|
unsafe {
|
|
|
|
let mut info = libc::SYSTEM_INFO::new();
|
|
|
|
libc::GetSystemInfo(&mut info);
|
2013-06-26 10:42:11 -05:00
|
|
|
|
2013-08-28 03:41:26 -05:00
|
|
|
return info.dwPageSize as uint;
|
|
|
|
}
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-01-23 13:38:22 -06:00
|
|
|
/// A memory mapped file or chunk of memory. This is a very system-specific
|
|
|
|
/// interface to the OS's memory mapping facilities (`mmap` on POSIX,
|
|
|
|
/// `VirtualAlloc`/`CreateFileMapping` on win32). It makes no attempt at
|
|
|
|
/// abstracting platform differences, besides in error values returned. Consider
|
2013-11-08 08:18:52 -06:00
|
|
|
/// yourself warned.
|
|
|
|
///
|
2014-01-23 13:38:22 -06:00
|
|
|
/// The memory map is released (unmapped) when the destructor is run, so don't
|
|
|
|
/// let it leave scope by accident if you want it to stick around.
|
2013-06-26 10:42:11 -05:00
|
|
|
pub struct MemoryMap {
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Pointer to the memory created or modified by this map.
|
2013-06-26 10:42:11 -05:00
|
|
|
data: *mut u8,
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Number of bytes this map applies to
|
2013-12-12 15:27:26 -06:00
|
|
|
len: uint,
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Type of mapping
|
2013-06-26 10:42:11 -05:00
|
|
|
kind: MemoryMapKind
|
|
|
|
}
|
|
|
|
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Type of memory map
|
2013-06-26 10:42:11 -05:00
|
|
|
pub enum MemoryMapKind {
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Virtual memory map. Usually used to change the permissions of a given
|
|
|
|
/// chunk of memory. Corresponds to `VirtualAlloc` on Windows.
|
2013-11-13 04:20:47 -06:00
|
|
|
MapFile(*u8),
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Virtual memory map. Usually used to change the permissions of a given
|
|
|
|
/// chunk of memory, or for allocation. Corresponds to `VirtualAlloc` on
|
|
|
|
/// Windows.
|
2013-06-26 10:42:11 -05:00
|
|
|
MapVirtual
|
|
|
|
}
|
|
|
|
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Options the memory map is created with
|
2013-06-26 10:42:11 -05:00
|
|
|
pub enum MapOption {
|
2013-11-08 08:18:52 -06:00
|
|
|
/// The memory should be readable
|
2013-06-26 10:42:11 -05:00
|
|
|
MapReadable,
|
2013-11-08 08:18:52 -06:00
|
|
|
/// The memory should be writable
|
2013-06-26 10:42:11 -05:00
|
|
|
MapWritable,
|
2013-11-08 08:18:52 -06:00
|
|
|
/// The memory should be executable
|
2013-06-26 10:42:11 -05:00
|
|
|
MapExecutable,
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Create a map for a specific address range. Corresponds to `MAP_FIXED` on
|
|
|
|
/// POSIX.
|
2013-12-12 15:27:26 -06:00
|
|
|
MapAddr(*u8),
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Create a memory mapping for a file with a given fd.
|
2013-06-26 10:42:11 -05:00
|
|
|
MapFd(c_int),
|
2014-01-23 13:38:22 -06:00
|
|
|
/// When using `MapFd`, the start of the map is `uint` bytes from the start
|
|
|
|
/// of the file.
|
2013-11-13 04:20:47 -06:00
|
|
|
MapOffset(uint),
|
2014-01-23 13:38:22 -06:00
|
|
|
/// On POSIX, this can be used to specify the default flags passed to
|
|
|
|
/// `mmap`. By default it uses `MAP_PRIVATE` and, if not using `MapFd`,
|
|
|
|
/// `MAP_ANON`. This will override both of those. This is platform-specific
|
|
|
|
/// (the exact values used) and ignored on Windows.
|
2013-11-13 04:20:47 -06:00
|
|
|
MapNonStandardFlags(c_int),
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Possible errors when creating a map.
|
2013-06-26 10:42:11 -05:00
|
|
|
pub enum MapError {
|
2013-11-08 08:18:52 -06:00
|
|
|
/// ## The following are POSIX-specific
|
|
|
|
///
|
2014-01-23 13:38:22 -06:00
|
|
|
/// fd was not open for reading or, if using `MapWritable`, was not open for
|
|
|
|
/// writing.
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrFdNotAvail,
|
2013-11-08 08:18:52 -06:00
|
|
|
/// fd was not valid
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrInvalidFd,
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Either the address given by `MapAddr` or offset given by `MapOffset` was
|
|
|
|
/// not a multiple of `MemoryMap::granularity` (unaligned to page size).
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrUnaligned,
|
2013-11-08 08:18:52 -06:00
|
|
|
/// With `MapFd`, the fd does not support mapping.
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrNoMapSupport,
|
2014-01-23 13:38:22 -06:00
|
|
|
/// If using `MapAddr`, the address + `min_len` was outside of the process's
|
|
|
|
/// address space. If using `MapFd`, the target of the fd didn't have enough
|
|
|
|
/// resources to fulfill the request.
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrNoMem,
|
2014-01-18 16:50:49 -06:00
|
|
|
/// A zero-length map was requested. This is invalid according to
|
2014-01-23 13:38:22 -06:00
|
|
|
/// [POSIX](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html).
|
|
|
|
/// Not all platforms obey this, but this wrapper does.
|
2014-01-18 16:50:49 -06:00
|
|
|
ErrZeroLength,
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Unrecognized error. The inner value is the unrecognized errno.
|
2013-12-12 15:27:26 -06:00
|
|
|
ErrUnknown(int),
|
2013-11-08 08:18:52 -06:00
|
|
|
/// ## The following are win32-specific
|
|
|
|
///
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Unsupported combination of protection flags
|
|
|
|
/// (`MapReadable`/`MapWritable`/`MapExecutable`).
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrUnsupProt,
|
2014-01-23 13:38:22 -06:00
|
|
|
/// When using `MapFd`, `MapOffset` was given (Windows does not support this
|
|
|
|
/// at all)
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrUnsupOffset,
|
2013-11-08 08:18:52 -06:00
|
|
|
/// When using `MapFd`, there was already a mapping to the file.
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrAlreadyExists,
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Unrecognized error from `VirtualAlloc`. The inner value is the return
|
|
|
|
/// value of GetLastError.
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrVirtualAlloc(uint),
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Unrecognized error from `CreateFileMapping`. The inner value is the
|
|
|
|
/// return value of `GetLastError`.
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrCreateFileMappingW(uint),
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Unrecognized error from `MapViewOfFile`. The inner value is the return
|
|
|
|
/// value of `GetLastError`.
|
2013-06-26 10:42:11 -05:00
|
|
|
ErrMapViewOfFile(uint)
|
|
|
|
}
|
|
|
|
|
2014-01-29 07:46:37 -06:00
|
|
|
impl fmt::Show for MapError {
|
2014-02-05 06:55:13 -06:00
|
|
|
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let str = match *self {
|
2013-11-13 04:21:38 -06:00
|
|
|
ErrFdNotAvail => "fd not available for reading or writing",
|
|
|
|
ErrInvalidFd => "Invalid fd",
|
2014-01-23 13:38:22 -06:00
|
|
|
ErrUnaligned => {
|
|
|
|
"Unaligned address, invalid flags, negative length or \
|
|
|
|
unaligned offset"
|
|
|
|
}
|
2013-11-13 04:21:38 -06:00
|
|
|
ErrNoMapSupport=> "File doesn't support mapping",
|
|
|
|
ErrNoMem => "Invalid address, or not enough available memory",
|
|
|
|
ErrUnsupProt => "Protection mode unsupported",
|
|
|
|
ErrUnsupOffset => "Offset in virtual memory mode is unsupported",
|
|
|
|
ErrAlreadyExists => "File mapping for specified file already exists",
|
2014-01-18 16:50:49 -06:00
|
|
|
ErrZeroLength => "Zero-length mapping not allowed",
|
2014-01-23 13:38:22 -06:00
|
|
|
ErrUnknown(code) => {
|
2014-01-29 18:33:57 -06:00
|
|
|
return write!(out.buf, "Unknown error = {}", code)
|
2014-01-23 13:38:22 -06:00
|
|
|
},
|
|
|
|
ErrVirtualAlloc(code) => {
|
2014-01-29 18:33:57 -06:00
|
|
|
return write!(out.buf, "VirtualAlloc failure = {}", code)
|
2014-01-23 13:38:22 -06:00
|
|
|
},
|
2013-11-13 04:21:38 -06:00
|
|
|
ErrCreateFileMappingW(code) => {
|
2014-01-29 18:33:57 -06:00
|
|
|
return write!(out.buf, "CreateFileMappingW failure = {}", code)
|
2013-11-13 04:21:38 -06:00
|
|
|
},
|
|
|
|
ErrMapViewOfFile(code) => {
|
2014-01-29 18:33:57 -06:00
|
|
|
return write!(out.buf, "MapViewOfFile failure = {}", code)
|
2013-11-13 04:21:38 -06:00
|
|
|
}
|
|
|
|
};
|
2014-01-29 18:33:57 -06:00
|
|
|
write!(out.buf, "{}", str)
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
impl MemoryMap {
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Create a new mapping with the given `options`, at least `min_len` bytes
|
|
|
|
/// long. `min_len` must be greater than zero; see the note on
|
|
|
|
/// `ErrZeroLength`.
|
2013-09-30 00:14:09 -05:00
|
|
|
pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
|
2013-06-26 10:42:11 -05:00
|
|
|
use libc::off_t;
|
2014-02-02 18:32:52 -06:00
|
|
|
use cmp::Equiv;
|
2013-06-26 10:42:11 -05:00
|
|
|
|
2014-01-18 16:50:49 -06:00
|
|
|
if min_len == 0 {
|
|
|
|
return Err(ErrZeroLength)
|
|
|
|
}
|
2013-12-12 15:27:26 -06:00
|
|
|
let mut addr: *u8 = ptr::null();
|
|
|
|
let mut prot = 0;
|
|
|
|
let mut flags = libc::MAP_PRIVATE;
|
|
|
|
let mut fd = -1;
|
|
|
|
let mut offset = 0;
|
2013-11-13 04:20:47 -06:00
|
|
|
let mut custom_flags = false;
|
2013-12-12 15:27:26 -06:00
|
|
|
let len = round_up(min_len, page_size());
|
2013-06-26 10:42:11 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for &o in options.iter() {
|
2013-06-26 10:42:11 -05:00
|
|
|
match o {
|
|
|
|
MapReadable => { prot |= libc::PROT_READ; },
|
|
|
|
MapWritable => { prot |= libc::PROT_WRITE; },
|
|
|
|
MapExecutable => { prot |= libc::PROT_EXEC; },
|
|
|
|
MapAddr(addr_) => {
|
|
|
|
flags |= libc::MAP_FIXED;
|
|
|
|
addr = addr_;
|
|
|
|
},
|
|
|
|
MapFd(fd_) => {
|
|
|
|
flags |= libc::MAP_FILE;
|
|
|
|
fd = fd_;
|
|
|
|
},
|
2013-11-13 04:20:47 -06:00
|
|
|
MapOffset(offset_) => { offset = offset_ as off_t; },
|
|
|
|
MapNonStandardFlags(f) => { custom_flags = true; flags = f },
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-13 04:20:47 -06:00
|
|
|
if fd == -1 && !custom_flags { flags |= libc::MAP_ANON; }
|
2013-06-26 10:42:11 -05:00
|
|
|
|
|
|
|
let r = unsafe {
|
2014-01-23 13:38:22 -06:00
|
|
|
libc::mmap(addr as *c_void, len as libc::size_t, prot, flags, fd,
|
|
|
|
offset)
|
2013-06-26 10:42:11 -05:00
|
|
|
};
|
2013-08-02 23:41:06 -05:00
|
|
|
if r.equiv(&libc::MAP_FAILED) {
|
2013-06-26 10:42:11 -05:00
|
|
|
Err(match errno() as c_int {
|
|
|
|
libc::EACCES => ErrFdNotAvail,
|
|
|
|
libc::EBADF => ErrInvalidFd,
|
|
|
|
libc::EINVAL => ErrUnaligned,
|
|
|
|
libc::ENODEV => ErrNoMapSupport,
|
|
|
|
libc::ENOMEM => ErrNoMem,
|
2013-12-12 15:27:26 -06:00
|
|
|
code => ErrUnknown(code as int)
|
2013-06-26 10:42:11 -05:00
|
|
|
})
|
|
|
|
} else {
|
2013-09-30 00:14:09 -05:00
|
|
|
Ok(MemoryMap {
|
2013-06-26 10:42:11 -05:00
|
|
|
data: r as *mut u8,
|
|
|
|
len: len,
|
|
|
|
kind: if fd == -1 {
|
|
|
|
MapVirtual
|
|
|
|
} else {
|
|
|
|
MapFile(ptr::null())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 03:41:26 -05:00
|
|
|
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Granularity that the offset or address must be for `MapOffset` and
|
|
|
|
/// `MapAddr` respectively.
|
2013-08-28 03:41:26 -05:00
|
|
|
pub fn granularity() -> uint {
|
|
|
|
page_size()
|
|
|
|
}
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
impl Drop for MemoryMap {
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Unmap the mapping. Fails the task if `munmap` fails.
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2014-01-18 16:50:49 -06:00
|
|
|
if self.len == 0 { /* workaround for dummy_stack */ return; }
|
|
|
|
|
2013-06-26 10:42:11 -05:00
|
|
|
unsafe {
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 00:11:44 -06:00
|
|
|
// FIXME: what to do if this fails?
|
|
|
|
let _ = libc::munmap(self.data as *c_void, self.len as libc::size_t);
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl MemoryMap {
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Create a new mapping with the given `options`, at least `min_len` bytes long.
|
2013-09-30 00:14:09 -05:00
|
|
|
pub fn new(min_len: uint, options: &[MapOption]) -> Result<MemoryMap, MapError> {
|
2013-06-26 10:42:11 -05:00
|
|
|
use libc::types::os::arch::extra::{LPVOID, DWORD, SIZE_T, HANDLE};
|
|
|
|
|
|
|
|
let mut lpAddress: LPVOID = ptr::mut_null();
|
|
|
|
let mut readable = false;
|
|
|
|
let mut writable = false;
|
|
|
|
let mut executable = false;
|
|
|
|
let mut fd: c_int = -1;
|
|
|
|
let mut offset: uint = 0;
|
2013-12-12 15:27:26 -06:00
|
|
|
let len = round_up(min_len, page_size());
|
2013-06-26 10:42:11 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for &o in options.iter() {
|
2013-06-26 10:42:11 -05:00
|
|
|
match o {
|
|
|
|
MapReadable => { readable = true; },
|
|
|
|
MapWritable => { writable = true; },
|
|
|
|
MapExecutable => { executable = true; }
|
|
|
|
MapAddr(addr_) => { lpAddress = addr_ as LPVOID; },
|
|
|
|
MapFd(fd_) => { fd = fd_; },
|
2013-11-13 04:20:47 -06:00
|
|
|
MapOffset(offset_) => { offset = offset_; },
|
2014-03-13 01:34:31 -05:00
|
|
|
MapNonStandardFlags(..) => {}
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let flProtect = match (executable, readable, writable) {
|
|
|
|
(false, false, false) if fd == -1 => libc::PAGE_NOACCESS,
|
|
|
|
(false, true, false) => libc::PAGE_READONLY,
|
|
|
|
(false, true, true) => libc::PAGE_READWRITE,
|
|
|
|
(true, false, false) if fd == -1 => libc::PAGE_EXECUTE,
|
|
|
|
(true, true, false) => libc::PAGE_EXECUTE_READ,
|
|
|
|
(true, true, true) => libc::PAGE_EXECUTE_READWRITE,
|
|
|
|
_ => return Err(ErrUnsupProt)
|
|
|
|
};
|
|
|
|
|
|
|
|
if fd == -1 {
|
|
|
|
if offset != 0 {
|
|
|
|
return Err(ErrUnsupOffset);
|
|
|
|
}
|
|
|
|
let r = unsafe {
|
|
|
|
libc::VirtualAlloc(lpAddress,
|
2013-12-12 15:27:26 -06:00
|
|
|
len as SIZE_T,
|
2013-06-26 10:42:11 -05:00
|
|
|
libc::MEM_COMMIT | libc::MEM_RESERVE,
|
|
|
|
flProtect)
|
|
|
|
};
|
|
|
|
match r as uint {
|
|
|
|
0 => Err(ErrVirtualAlloc(errno())),
|
2013-09-30 00:14:09 -05:00
|
|
|
_ => Ok(MemoryMap {
|
2013-06-26 10:42:11 -05:00
|
|
|
data: r as *mut u8,
|
|
|
|
len: len,
|
|
|
|
kind: MapVirtual
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
2013-08-28 03:41:26 -05:00
|
|
|
let dwDesiredAccess = match (executable, readable, writable) {
|
|
|
|
(false, true, false) => libc::FILE_MAP_READ,
|
|
|
|
(false, true, true) => libc::FILE_MAP_WRITE,
|
|
|
|
(true, true, false) => libc::FILE_MAP_READ | libc::FILE_MAP_EXECUTE,
|
|
|
|
(true, true, true) => libc::FILE_MAP_WRITE | libc::FILE_MAP_EXECUTE,
|
|
|
|
_ => return Err(ErrUnsupProt) // Actually, because of the check above,
|
|
|
|
// we should never get here.
|
2013-06-26 10:42:11 -05:00
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
let hFile = libc::get_osfhandle(fd) as HANDLE;
|
|
|
|
let mapping = libc::CreateFileMappingW(hFile,
|
|
|
|
ptr::mut_null(),
|
|
|
|
flProtect,
|
2013-08-28 03:41:26 -05:00
|
|
|
0,
|
|
|
|
0,
|
2013-06-26 10:42:11 -05:00
|
|
|
ptr::null());
|
|
|
|
if mapping == ptr::mut_null() {
|
|
|
|
return Err(ErrCreateFileMappingW(errno()));
|
|
|
|
}
|
|
|
|
if errno() as c_int == libc::ERROR_ALREADY_EXISTS {
|
|
|
|
return Err(ErrAlreadyExists);
|
|
|
|
}
|
|
|
|
let r = libc::MapViewOfFile(mapping,
|
|
|
|
dwDesiredAccess,
|
2013-08-28 03:41:26 -05:00
|
|
|
((len as u64) >> 32) as DWORD,
|
2013-06-26 10:42:11 -05:00
|
|
|
(offset & 0xffff_ffff) as DWORD,
|
|
|
|
0);
|
|
|
|
match r as uint {
|
|
|
|
0 => Err(ErrMapViewOfFile(errno())),
|
2013-09-30 00:14:09 -05:00
|
|
|
_ => Ok(MemoryMap {
|
2013-06-26 10:42:11 -05:00
|
|
|
data: r as *mut u8,
|
|
|
|
len: len,
|
2013-12-12 15:27:26 -06:00
|
|
|
kind: MapFile(mapping as *u8)
|
2013-06-26 10:42:11 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 03:41:26 -05:00
|
|
|
|
|
|
|
/// Granularity of MapAddr() and MapOffset() parameter values.
|
|
|
|
/// This may be greater than the value returned by page_size().
|
|
|
|
pub fn granularity() -> uint {
|
|
|
|
unsafe {
|
|
|
|
let mut info = libc::SYSTEM_INFO::new();
|
|
|
|
libc::GetSystemInfo(&mut info);
|
|
|
|
|
|
|
|
return info.dwAllocationGranularity as uint;
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl Drop for MemoryMap {
|
2014-01-23 13:38:22 -06:00
|
|
|
/// Unmap the mapping. Fails the task if any of `VirtualFree`,
|
|
|
|
/// `UnmapViewOfFile`, or `CloseHandle` fail.
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-06-26 10:42:11 -05:00
|
|
|
use libc::types::os::arch::extra::{LPCVOID, HANDLE};
|
2013-08-28 03:41:26 -05:00
|
|
|
use libc::consts::os::extra::FALSE;
|
2014-01-23 13:38:22 -06:00
|
|
|
if self.len == 0 { return }
|
2013-06-26 10:42:11 -05:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
match self.kind {
|
2013-08-28 03:41:26 -05:00
|
|
|
MapVirtual => {
|
2013-11-13 04:21:38 -06:00
|
|
|
if libc::VirtualFree(self.data as *mut c_void, 0,
|
2014-01-23 13:38:22 -06:00
|
|
|
libc::MEM_RELEASE) == 0 {
|
2014-03-13 01:34:31 -05:00
|
|
|
println!("VirtualFree failed: {}", errno());
|
2013-08-28 03:41:26 -05:00
|
|
|
}
|
2013-06-26 10:42:11 -05:00
|
|
|
},
|
|
|
|
MapFile(mapping) => {
|
2013-08-28 03:41:26 -05:00
|
|
|
if libc::UnmapViewOfFile(self.data as LPCVOID) == FALSE {
|
2014-03-13 01:34:31 -05:00
|
|
|
println!("UnmapViewOfFile failed: {}", errno());
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
2013-08-28 03:41:26 -05:00
|
|
|
if libc::CloseHandle(mapping as HANDLE) == FALSE {
|
2014-03-13 01:34:31 -05:00
|
|
|
println!("CloseHandle failed: {}", errno());
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
pub mod consts {
|
2012-06-06 23:39:41 -05:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
#[cfg(unix)]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::unix::*;
|
2012-06-06 23:39:41 -05:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
#[cfg(windows)]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::windows::*;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
#[cfg(target_os = "macos")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::macos::*;
|
2012-12-20 03:26:27 -06:00
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::freebsd::*;
|
2012-12-20 03:26:27 -06:00
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::linux::*;
|
2012-12-20 03:26:27 -06:00
|
|
|
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::android::*;
|
2012-11-29 18:21:49 -06:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
#[cfg(target_os = "win32")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::win32::*;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-26 21:53:33 -05:00
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
pub use os::consts::x86::*;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub use os::consts::x86_64::*;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
pub use os::consts::arm::*;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "mips")]
|
2013-09-17 12:58:07 -05:00
|
|
|
pub use os::consts::mips::*;
|
2013-03-26 21:53:33 -05:00
|
|
|
|
|
|
|
pub mod unix {
|
|
|
|
pub static FAMILY: &'static str = "unix";
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod windows {
|
|
|
|
pub static FAMILY: &'static str = "windows";
|
|
|
|
}
|
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
pub mod macos {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "macos";
|
|
|
|
pub static DLL_PREFIX: &'static str = "lib";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".dylib";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static DLL_EXTENSION: &'static str = "dylib";
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static EXE_SUFFIX: &'static str = "";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static EXE_EXTENSION: &'static str = "";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod freebsd {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "freebsd";
|
|
|
|
pub static DLL_PREFIX: &'static str = "lib";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".so";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static DLL_EXTENSION: &'static str = "so";
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static EXE_SUFFIX: &'static str = "";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static EXE_EXTENSION: &'static str = "";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod linux {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "linux";
|
|
|
|
pub static DLL_PREFIX: &'static str = "lib";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".so";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static DLL_EXTENSION: &'static str = "so";
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static EXE_SUFFIX: &'static str = "";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static EXE_EXTENSION: &'static str = "";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
2012-11-29 18:21:49 -06:00
|
|
|
|
|
|
|
pub mod android {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "android";
|
|
|
|
pub static DLL_PREFIX: &'static str = "lib";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".so";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static DLL_EXTENSION: &'static str = "so";
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static EXE_SUFFIX: &'static str = "";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static EXE_EXTENSION: &'static str = "";
|
2012-11-29 18:21:49 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
pub mod win32 {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "win32";
|
|
|
|
pub static DLL_PREFIX: &'static str = "";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".dll";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static DLL_EXTENSION: &'static str = "dll";
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static EXE_SUFFIX: &'static str = ".exe";
|
2013-09-26 19:21:59 -05:00
|
|
|
pub static EXE_EXTENSION: &'static str = "exe";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub mod x86 {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ARCH: &'static str = "x86";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
pub mod x86_64 {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ARCH: &'static str = "x86_64";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
pub mod arm {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ARCH: &'static str = "arm";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
2013-01-29 08:28:08 -06:00
|
|
|
pub mod mips {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ARCH: &'static str = "mips";
|
2013-01-29 08:28:08 -06:00
|
|
|
}
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-01-07 00:33:37 -06:00
|
|
|
use prelude::*;
|
2013-08-03 19:13:14 -05:00
|
|
|
use c_str::ToCStr;
|
2012-12-27 19:53:04 -06:00
|
|
|
use option;
|
2013-08-22 04:41:33 -05:00
|
|
|
use os::{env, getcwd, getenv, make_absolute, args};
|
2013-10-25 19:04:37 -05:00
|
|
|
use os::{setenv, unsetenv};
|
2012-12-27 19:53:04 -06:00
|
|
|
use os;
|
2013-09-20 06:47:05 -05:00
|
|
|
use rand::Rng;
|
2012-12-27 19:53:04 -06:00
|
|
|
use rand;
|
2013-04-28 23:25:35 -05:00
|
|
|
|
2012-04-19 03:23:00 -05:00
|
|
|
#[test]
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn last_os_error() {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("{}", os::last_os_error());
|
2012-04-19 03:23:00 -05:00
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
|
2012-10-01 18:34:54 -05:00
|
|
|
#[test]
|
|
|
|
pub fn test_args() {
|
2013-08-22 04:41:33 -05:00
|
|
|
let a = args();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(a.len() >= 1);
|
2012-10-01 18:34:54 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn make_rand_name() -> ~str {
|
2014-03-01 19:59:35 -06:00
|
|
|
let mut rng = rand::task_rng();
|
2013-09-20 06:47:05 -05:00
|
|
|
let n = ~"TEST" + rng.gen_ascii_str(10u);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(getenv(n).is_none());
|
2013-02-15 02:51:28 -06:00
|
|
|
n
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_setenv() {
|
|
|
|
let n = make_rand_name();
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "VALUE");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::Some(~"VALUE"));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
2013-05-15 12:39:53 -05:00
|
|
|
#[test]
|
|
|
|
fn test_unsetenv() {
|
|
|
|
let n = make_rand_name();
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "VALUE");
|
2013-05-15 12:39:53 -05:00
|
|
|
unsetenv(n);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::None);
|
2013-05-15 12:39:53 -05:00
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[ignore]
|
2012-03-12 22:04:27 -05:00
|
|
|
fn test_setenv_overwrite() {
|
|
|
|
let n = make_rand_name();
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "1");
|
|
|
|
setenv(n, "2");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::Some(~"2"));
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::Some(~""));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Windows GetEnvironmentVariable requires some extra work to make sure
|
|
|
|
// the buffer the variable is copied into is the right size
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[ignore]
|
2012-03-12 22:04:27 -05:00
|
|
|
fn test_getenv_big() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut s = ~"";
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0;
|
2013-06-18 11:39:16 -05:00
|
|
|
while i < 100 {
|
|
|
|
s = s + "aaaaaaaaaa";
|
|
|
|
i += 1;
|
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
let n = make_rand_name();
|
|
|
|
setenv(n, s);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("{}", s.clone());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::Some(s));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
2014-01-22 15:41:53 -06:00
|
|
|
#[test]
|
|
|
|
fn test_self_exe_name() {
|
|
|
|
let path = os::self_exe_name();
|
|
|
|
assert!(path.is_some());
|
|
|
|
let path = path.unwrap();
|
|
|
|
debug!("{:?}", path.clone());
|
|
|
|
|
|
|
|
// Hard to test this function
|
|
|
|
assert!(path.is_absolute());
|
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
#[test]
|
|
|
|
fn test_self_exe_path() {
|
|
|
|
let path = os::self_exe_path();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(path.is_some());
|
2013-08-03 18:59:24 -05:00
|
|
|
let path = path.unwrap();
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("{:?}", path.clone());
|
2012-03-12 22:04:27 -05:00
|
|
|
|
|
|
|
// Hard to test this function
|
2013-09-26 19:21:59 -05:00
|
|
|
assert!(path.is_absolute());
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-04-06 13:36:43 -05:00
|
|
|
#[ignore]
|
2012-03-12 22:04:27 -05:00
|
|
|
fn test_env_getenv() {
|
|
|
|
let e = env();
|
2013-05-14 04:52:12 -05:00
|
|
|
assert!(e.len() > 0u);
|
2013-08-03 11:45:23 -05:00
|
|
|
for p in e.iter() {
|
2013-07-02 14:47:32 -05:00
|
|
|
let (n, v) = (*p).clone();
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("{:?}", n.clone());
|
2012-03-12 22:04:27 -05:00
|
|
|
let v2 = getenv(n);
|
|
|
|
// MingW seems to set some funky environment variables like
|
|
|
|
// "=C:=C:\MinGW\msys\1.0\bin" and "!::=::\" that are returned
|
|
|
|
// from env() but not visible from getenv().
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(v2.is_none() || v2 == option::Some(v));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 10:11:00 -06:00
|
|
|
#[test]
|
|
|
|
fn test_env_set_get_huge() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
let s = "x".repeat(10000);
|
|
|
|
setenv(n, s);
|
|
|
|
assert_eq!(getenv(n), Some(s));
|
|
|
|
unsetenv(n);
|
|
|
|
assert_eq!(getenv(n), None);
|
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
#[test]
|
|
|
|
fn test_env_setenv() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut e = env();
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "VALUE");
|
2013-07-02 14:47:32 -05:00
|
|
|
assert!(!e.contains(&(n.clone(), ~"VALUE")));
|
2012-03-12 22:04:27 -05:00
|
|
|
|
|
|
|
e = env();
|
2013-06-28 11:08:32 -05:00
|
|
|
assert!(e.contains(&(n, ~"VALUE")));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
2012-03-02 19:20:00 -06:00
|
|
|
#[test]
|
|
|
|
fn test() {
|
2013-12-03 21:15:12 -06:00
|
|
|
assert!((!Path::new("test-path").is_absolute()));
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
let cwd = getcwd();
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Current working directory: {}", cwd.display());
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-12-03 21:15:12 -06:00
|
|
|
debug!("{:?}", make_absolute(&Path::new("test-path")));
|
|
|
|
debug!("{:?}", make_absolute(&Path::new("/usr/bin")));
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-03-02 19:20:00 -06:00
|
|
|
fn homedir() {
|
2013-05-23 11:39:17 -05:00
|
|
|
let oldhome = getenv("HOME");
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "/home/MountainView");
|
2014-02-28 03:23:06 -06:00
|
|
|
assert!(os::homedir() == Some(Path::new("/home/MountainView")));
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(os::homedir().is_none());
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for s in oldhome.iter() { setenv("HOME", *s) }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-03-02 19:20:00 -06:00
|
|
|
fn homedir() {
|
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
let oldhome = getenv("HOME");
|
|
|
|
let olduserprofile = getenv("USERPROFILE");
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "");
|
|
|
|
setenv("USERPROFILE", "");
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(os::homedir().is_none());
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "/home/MountainView");
|
2014-02-28 03:23:06 -06:00
|
|
|
assert!(os::homedir() == Some(Path::new("/home/MountainView")));
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "");
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("USERPROFILE", "/home/MountainView");
|
2014-02-28 03:23:06 -06:00
|
|
|
assert!(os::homedir() == Some(Path::new("/home/MountainView")));
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "/home/MountainView");
|
|
|
|
setenv("USERPROFILE", "/home/PaloAlto");
|
2014-02-28 03:23:06 -06:00
|
|
|
assert!(os::homedir() == Some(Path::new("/home/MountainView")));
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-09-14 14:51:02 -05:00
|
|
|
for s in oldhome.iter() { setenv("HOME", *s) }
|
|
|
|
for s in olduserprofile.iter() { setenv("USERPROFILE", *s) }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-06-26 10:42:11 -05:00
|
|
|
#[test]
|
|
|
|
fn memory_map_rw() {
|
|
|
|
use result::{Ok, Err};
|
|
|
|
|
2013-09-30 00:14:09 -05:00
|
|
|
let chunk = match os::MemoryMap::new(16, [
|
2013-06-26 10:42:11 -05:00
|
|
|
os::MapReadable,
|
|
|
|
os::MapWritable
|
|
|
|
]) {
|
|
|
|
Ok(chunk) => chunk,
|
2014-01-18 16:50:49 -06:00
|
|
|
Err(msg) => fail!("{}", msg)
|
2013-06-26 10:42:11 -05:00
|
|
|
};
|
|
|
|
assert!(chunk.len >= 16);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
*chunk.data = 0xBE;
|
|
|
|
assert!(*chunk.data == 0xBE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn memory_map_file() {
|
|
|
|
use result::{Ok, Err};
|
|
|
|
use os::*;
|
|
|
|
use libc::*;
|
2013-11-11 00:46:32 -06:00
|
|
|
use io::fs;
|
2013-06-26 10:42:11 -05:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn lseek_(fd: c_int, size: uint) {
|
|
|
|
unsafe {
|
|
|
|
assert!(lseek(fd, size as off_t, SEEK_SET) == size as off_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn lseek_(fd: c_int, size: uint) {
|
|
|
|
unsafe {
|
|
|
|
assert!(lseek(fd, size as c_long, SEEK_SET) == size as c_long);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 19:21:59 -05:00
|
|
|
let mut path = tmpdir();
|
2013-10-05 21:49:32 -05:00
|
|
|
path.push("mmap_file.tmp");
|
2013-08-28 03:41:26 -05:00
|
|
|
let size = MemoryMap::granularity() * 2;
|
2013-06-26 10:42:11 -05:00
|
|
|
|
|
|
|
let fd = unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
let fd = path.with_c_str(|path| {
|
2013-06-26 10:42:11 -05:00
|
|
|
open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)
|
2013-11-20 16:17:12 -06:00
|
|
|
});
|
2013-06-26 10:42:11 -05:00
|
|
|
lseek_(fd, size);
|
2013-11-20 16:17:12 -06:00
|
|
|
"x".with_c_str(|x| assert!(write(fd, x as *c_void, 1) == 1));
|
2013-06-26 10:42:11 -05:00
|
|
|
fd
|
|
|
|
};
|
2013-09-30 00:14:09 -05:00
|
|
|
let chunk = match MemoryMap::new(size / 2, [
|
2013-06-26 10:42:11 -05:00
|
|
|
MapReadable,
|
|
|
|
MapWritable,
|
|
|
|
MapFd(fd),
|
|
|
|
MapOffset(size / 2)
|
|
|
|
]) {
|
|
|
|
Ok(chunk) => chunk,
|
2013-11-13 04:21:38 -06:00
|
|
|
Err(msg) => fail!("{}", msg)
|
2013-06-26 10:42:11 -05:00
|
|
|
};
|
|
|
|
assert!(chunk.len > 0);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
*chunk.data = 0xbe;
|
|
|
|
assert!(*chunk.data == 0xbe);
|
|
|
|
close(fd);
|
|
|
|
}
|
2014-02-01 13:24:42 -06:00
|
|
|
drop(chunk);
|
2013-12-04 17:20:26 -06:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
fs::unlink(&path).unwrap();
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
2013-06-22 04:49:32 -05:00
|
|
|
// More recursive_mkdir tests are in extra::tempfile
|
2012-04-01 10:39:17 -05:00
|
|
|
}
|