2014-05-30 11:07:16 -05:00
|
|
|
// Copyright 2012-2014 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.
|
|
|
|
|
2014-11-25 20:17:11 -06: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
|
|
|
|
2014-06-30 19:22:40 -05:00
|
|
|
#![experimental]
|
|
|
|
|
2014-10-27 17:37:07 -05:00
|
|
|
#![allow(missing_docs)]
|
2014-07-18 07:45:17 -05:00
|
|
|
#![allow(non_snake_case)]
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::MemoryMapKind::*;
|
|
|
|
pub use self::MapOption::*;
|
|
|
|
pub use self::MapError::*;
|
|
|
|
|
2014-02-02 18:32:52 -06:00
|
|
|
use clone::Clone;
|
2014-10-03 16:24:49 -05:00
|
|
|
use error::{FromError, Error};
|
2014-05-16 12:45:16 -05:00
|
|
|
use fmt;
|
2014-11-10 23:38:20 -06:00
|
|
|
use io::{IoResult, IoError};
|
2014-11-06 11:32:37 -06:00
|
|
|
use iter::{Iterator, IteratorExt};
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
use kinds::Copy;
|
2014-05-04 20:14:55 -05:00
|
|
|
use libc::{c_void, c_int};
|
2014-05-16 12:45:16 -05:00
|
|
|
use libc;
|
2014-10-03 16:24:49 -05:00
|
|
|
use boxed::Box;
|
2014-05-16 12:45:16 -05:00
|
|
|
use ops::Drop;
|
2014-11-28 10:57:41 -06:00
|
|
|
use option::Option;
|
|
|
|
use option::Option::{Some, None};
|
2013-05-24 21:35:29 -05:00
|
|
|
use os;
|
2014-05-30 12:31:20 -05:00
|
|
|
use path::{Path, GenericPath, BytesContainer};
|
2014-10-10 12:11:49 -05:00
|
|
|
use sys;
|
2014-09-30 19:03:56 -05:00
|
|
|
use sys::os as os_imp;
|
2014-05-16 12:45:16 -05:00
|
|
|
use ptr::RawPtr;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ptr;
|
2014-11-28 10:57:41 -06:00
|
|
|
use result::Result;
|
|
|
|
use result::Result::{Err, Ok};
|
2014-11-02 19:04:32 -06:00
|
|
|
use slice::{AsSlice, SlicePrelude, PartialEqSlicePrelude};
|
|
|
|
use slice::CloneSliceAllocPrelude;
|
|
|
|
use str::{Str, StrPrelude, StrAllocating};
|
2014-11-15 19:38:03 -06:00
|
|
|
use string::{String, ToString};
|
2014-08-04 17:42:36 -05:00
|
|
|
use sync::atomic::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
|
2014-04-17 17:28:14 -05:00
|
|
|
use vec::Vec;
|
2014-02-02 18:32:52 -06:00
|
|
|
|
2014-09-17 14:56:31 -05:00
|
|
|
#[cfg(unix)] use c_str::ToCStr;
|
|
|
|
#[cfg(unix)] use libc::c_char;
|
2012-03-06 20:49:08 -06:00
|
|
|
|
2014-11-20 20:30:46 -06:00
|
|
|
#[cfg(unix)]
|
|
|
|
pub use sys::ext as unix;
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub use sys::ext as windows;
|
|
|
|
|
2014-06-16 15:36:07 -05:00
|
|
|
/// Get the number of cores available
|
|
|
|
pub fn num_cpus() -> uint {
|
|
|
|
unsafe {
|
2014-08-14 14:18:14 -05:00
|
|
|
return rust_get_num_cpus() as uint;
|
2014-06-16 15:36:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern {
|
|
|
|
fn rust_get_num_cpus() -> libc::uintptr_t;
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const TMPBUF_SZ : uint = 1000u;
|
|
|
|
const BUF_BYTES : uint = 2048u;
|
2012-03-06 20:49:08 -06:00
|
|
|
|
2014-11-10 23:38:20 -06:00
|
|
|
/// Returns the current working directory as a `Path`.
|
2014-06-04 09:33:25 -05:00
|
|
|
///
|
2014-11-10 23:38:20 -06:00
|
|
|
/// # Errors
|
2014-06-04 09:33:25 -05:00
|
|
|
///
|
2014-11-10 23:38:20 -06:00
|
|
|
/// Returns an `Err` if the current working directory value is invalid.
|
2014-11-17 13:41:47 -06:00
|
|
|
/// Possible cases:
|
2014-06-04 09:33:25 -05:00
|
|
|
///
|
|
|
|
/// * Current directory does not exist.
|
|
|
|
/// * There are insufficient permissions to access the current directory.
|
2014-11-10 23:38:20 -06:00
|
|
|
/// * The internal buffer is not large enough to hold the path.
|
2014-06-04 09:33:25 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-06-11 10:52:12 -05:00
|
|
|
/// use std::os;
|
|
|
|
///
|
2014-06-04 09:33:25 -05:00
|
|
|
/// // We assume that we are in a valid directory like "/home".
|
2014-11-10 23:38:20 -06:00
|
|
|
/// let current_working_directory = os::getcwd().unwrap();
|
2014-06-04 09:33:25 -05:00
|
|
|
/// println!("The current directory is {}", current_working_directory.display());
|
|
|
|
/// // /home
|
|
|
|
/// ```
|
2013-10-20 01:02:03 -05:00
|
|
|
#[cfg(unix)]
|
2014-11-10 23:38:20 -06:00
|
|
|
pub fn getcwd() -> IoResult<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() {
|
2014-11-10 23:38:20 -06:00
|
|
|
Err(IoError::last_error())
|
|
|
|
} else {
|
|
|
|
Ok(Path::new(CString::new(buf.as_ptr(), false)))
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2013-12-17 09:13:20 -06:00
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
|
|
|
|
2014-11-10 23:38:20 -06:00
|
|
|
/// Returns the current working directory as a `Path`.
|
2014-06-04 09:33:25 -05:00
|
|
|
///
|
2014-11-10 23:38:20 -06:00
|
|
|
/// # Errors
|
2014-06-04 09:33:25 -05:00
|
|
|
///
|
2014-11-10 23:38:20 -06:00
|
|
|
/// Returns an `Err` if the current working directory value is invalid.
|
|
|
|
/// Possible cases:
|
2014-06-04 09:33:25 -05:00
|
|
|
///
|
|
|
|
/// * Current directory does not exist.
|
|
|
|
/// * There are insufficient permissions to access the current directory.
|
2014-11-10 23:38:20 -06:00
|
|
|
/// * The internal buffer is not large enough to hold the path.
|
2014-06-04 09:33:25 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-06-11 10:52:12 -05:00
|
|
|
/// use std::os;
|
|
|
|
///
|
2014-06-04 09:33:25 -05:00
|
|
|
/// // We assume that we are in a valid directory like "C:\\Windows".
|
2014-11-10 23:38:20 -06:00
|
|
|
/// let current_working_directory = os::getcwd().unwrap();
|
2014-06-04 09:33:25 -05:00
|
|
|
/// println!("The current directory is {}", current_working_directory.display());
|
|
|
|
/// // C:\\Windows
|
|
|
|
/// ```
|
2013-10-20 01:02:03 -05:00
|
|
|
#[cfg(windows)]
|
2014-11-10 23:38:20 -06:00
|
|
|
pub fn getcwd() -> IoResult<Path> {
|
2013-10-20 01:02:03 -05:00
|
|
|
use libc::DWORD;
|
|
|
|
use libc::GetCurrentDirectoryW;
|
2014-11-10 23:38:20 -06:00
|
|
|
use io::OtherIoError;
|
2014-05-07 10:20:22 -05:00
|
|
|
|
2013-10-20 01:02:03 -05:00
|
|
|
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 {
|
2014-11-10 23:38:20 -06:00
|
|
|
return Err(IoError::last_error());
|
2013-10-20 01:02:03 -05:00
|
|
|
}
|
2013-12-17 09:37:30 -06:00
|
|
|
}
|
2014-11-10 23:38:20 -06:00
|
|
|
|
|
|
|
match String::from_utf16(::str::truncate_utf16_at_nul(&buf)) {
|
|
|
|
Some(ref cwd) => Ok(Path::new(cwd)),
|
|
|
|
None => Err(IoError {
|
|
|
|
kind: OtherIoError,
|
|
|
|
desc: "GetCurrentDirectoryW returned invalid UTF-16",
|
|
|
|
detail: None,
|
|
|
|
}),
|
|
|
|
}
|
2013-10-20 01:02:03 -05:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2014-12-07 12:25:51 -06:00
|
|
|
pub mod windoze {
|
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;
|
2014-11-28 10:57:41 -06:00
|
|
|
use option::Option;
|
|
|
|
use option::Option::None;
|
2013-01-04 17:38:56 -06:00
|
|
|
use option;
|
2013-02-11 19:07:25 -06:00
|
|
|
use os::TMPBUF_SZ;
|
2014-11-02 19:04:32 -06:00
|
|
|
use slice::{SlicePrelude};
|
2014-05-22 18:57:53 -05:00
|
|
|
use string::String;
|
2014-11-02 19:04:32 -06:00
|
|
|
use str::StrPrelude;
|
2014-04-17 17:59:07 -05:00
|
|
|
use vec::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)
|
2014-05-22 18:57:53 -05:00
|
|
|
-> Option<String> {
|
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 {
|
2014-04-17 17:59:07 -05: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.
|
2014-07-10 10:43:03 -05:00
|
|
|
let s = String::from_utf16(sub)
|
2014-02-16 16:57:56 -06:00
|
|
|
.expect("fill_utf16_buf_and_decode: closure created invalid UTF-16");
|
2014-11-28 10:57:41 -06:00
|
|
|
res = option::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
|
|
|
}
|
|
|
|
}
|
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-11-24 13:16:40 -06:00
|
|
|
use sync::{StaticMutex, MUTEX_INIT};
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-11-24 13:16:40 -06:00
|
|
|
static LOCK: StaticMutex = MUTEX_INIT;
|
2013-11-14 02:21:43 -06:00
|
|
|
|
2014-11-24 13:16:40 -06:00
|
|
|
let _guard = LOCK.lock();
|
|
|
|
f()
|
2012-07-25 16:37:40 -05:00
|
|
|
}
|
|
|
|
|
2014-06-11 10:52:12 -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
|
|
|
///
|
2014-07-10 11:21:16 -05:00
|
|
|
/// Invalid UTF-8 bytes are replaced with \uFFFD. See `String::from_utf8_lossy()`
|
2014-02-14 17:18:51 -06:00
|
|
|
/// for details.
|
2014-06-05 10:36:15 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-06-11 10:52:12 -05:00
|
|
|
/// use std::os;
|
|
|
|
///
|
|
|
|
/// // We will iterate through the references to the element returned by os::env();
|
|
|
|
/// for &(ref key, ref value) in os::env().iter() {
|
2014-06-05 10:36:15 -05:00
|
|
|
/// println!("'{}': '{}'", key, value );
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn env() -> Vec<(String,String)> {
|
2014-09-14 22:27:36 -05:00
|
|
|
env_as_bytes().into_iter().map(|(k,v)| {
|
2014-07-10 11:21:16 -05:00
|
|
|
let k = String::from_utf8_lossy(k.as_slice()).into_string();
|
|
|
|
let v = String::from_utf8_lossy(v.as_slice()).into_string();
|
2014-02-14 17:18:51 -06:00
|
|
|
(k,v)
|
|
|
|
}).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vector of (variable, value) byte-vector pairs for all the
|
|
|
|
/// environment variables of the current process.
|
2014-05-20 01:19:56 -05:00
|
|
|
pub fn env_as_bytes() -> Vec<(Vec<u8>,Vec<u8>)> {
|
2013-01-20 00:54:29 -06:00
|
|
|
unsafe {
|
2013-02-15 00:16:53 -06:00
|
|
|
#[cfg(windows)]
|
2014-05-20 01:19:56 -05:00
|
|
|
unsafe fn get_env_pairs() -> Vec<Vec<u8>> {
|
2014-11-20 12:11:15 -06:00
|
|
|
use slice;
|
2013-08-14 20:41:40 -05:00
|
|
|
|
2013-02-15 00:16:53 -06:00
|
|
|
use libc::funcs::extra::kernel32::{
|
2014-05-04 20:14:55 -05:00
|
|
|
GetEnvironmentStringsW,
|
|
|
|
FreeEnvironmentStringsW
|
2013-02-15 00:16:53 -06:00
|
|
|
};
|
2014-05-04 20:14:55 -05:00
|
|
|
let ch = GetEnvironmentStringsW();
|
2014-01-19 02:21:14 -06:00
|
|
|
if ch as uint == 0 {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("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
|
|
|
}
|
2014-05-04 20:14:55 -05:00
|
|
|
// Here, we lossily decode the string as UTF16.
|
|
|
|
//
|
|
|
|
// The docs suggest that the result should be in Unicode, but
|
|
|
|
// Windows doesn't guarantee it's actually UTF16 -- it doesn't
|
|
|
|
// validate the environment string passed to CreateProcess nor
|
|
|
|
// SetEnvironmentVariable. Yet, it's unlikely that returning a
|
|
|
|
// raw u16 buffer would be of practical use since the result would
|
|
|
|
// be inherently platform-dependent and introduce additional
|
|
|
|
// complexity to this code.
|
|
|
|
//
|
|
|
|
// Using the non-Unicode version of GetEnvironmentStrings is even
|
|
|
|
// worse since the result is in an OEM code page. Characters that
|
|
|
|
// can't be encoded in the code page would be turned into question
|
|
|
|
// marks.
|
2014-04-17 17:59:07 -05:00
|
|
|
let mut result = Vec::new();
|
2014-05-04 20:14:55 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while *ch.offset(i) != 0 {
|
|
|
|
let p = &*ch.offset(i);
|
2014-09-17 14:56:31 -05:00
|
|
|
let mut len = 0;
|
|
|
|
while *(p as *const _).offset(len) != 0 {
|
|
|
|
len += 1;
|
|
|
|
}
|
2014-11-20 12:11:15 -06:00
|
|
|
let p = p as *const u16;
|
|
|
|
let s = slice::from_raw_buf(&p, len as uint);
|
|
|
|
result.push(String::from_utf16_lossy(s).into_bytes());
|
2014-05-04 20:14:55 -05:00
|
|
|
i += len as int + 1;
|
|
|
|
}
|
|
|
|
FreeEnvironmentStringsW(ch);
|
2013-02-15 00:16:53 -06:00
|
|
|
result
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
2014-05-20 01:19:56 -05:00
|
|
|
unsafe fn get_env_pairs() -> Vec<Vec<u8>> {
|
2014-02-14 17:18:51 -06:00
|
|
|
use c_str::CString;
|
|
|
|
|
2013-05-09 16:14:42 -05:00
|
|
|
extern {
|
2014-06-25 14:47:34 -05:00
|
|
|
fn rust_env_pairs() -> *const *const c_char;
|
2013-02-15 00:16:53 -06:00
|
|
|
}
|
2014-09-17 12:47:05 -05:00
|
|
|
let mut environ = rust_env_pairs();
|
2014-01-19 02:21:14 -06:00
|
|
|
if environ as uint == 0 {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("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
|
|
|
}
|
2014-04-17 17:28:14 -05:00
|
|
|
let mut result = Vec::new();
|
2014-09-17 12:47:05 -05:00
|
|
|
while *environ != 0 as *const _ {
|
2014-05-20 01:19:56 -05:00
|
|
|
let env_pair =
|
2014-09-17 14:56:31 -05:00
|
|
|
CString::new(*environ, false).as_bytes_no_nul().to_vec();
|
2013-02-15 00:16:53 -06:00
|
|
|
result.push(env_pair);
|
2014-09-17 12:47:05 -05:00
|
|
|
environ = environ.offset(1);
|
|
|
|
}
|
2013-02-15 00:16:53 -06:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2014-05-20 01:19:56 -05:00
|
|
|
fn env_convert(input: Vec<Vec<u8>>) -> Vec<(Vec<u8>, Vec<u8>)> {
|
2014-04-17 17:28:14 -05:00
|
|
|
let mut pairs = Vec::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for p in input.iter() {
|
2014-11-27 13:43:55 -06:00
|
|
|
let mut it = p.splitn(1, |b| *b == b'=');
|
2014-09-17 14:56:31 -05:00
|
|
|
let key = it.next().unwrap().to_vec();
|
2014-08-04 07:19:02 -05:00
|
|
|
let default: &[u8] = &[];
|
2014-09-17 14:56:31 -05:00
|
|
|
let val = it.next().unwrap_or(default).to_vec();
|
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();
|
2014-05-04 00:42:27 -05:00
|
|
|
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
|
2014-07-10 11:21:16 -05:00
|
|
|
/// `String::from_utf8_lossy()` for details.
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
2014-11-11 12:36:09 -06:00
|
|
|
/// # Panics
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
2014-11-11 12:36:09 -06:00
|
|
|
/// Panics if `n` has any interior NULs.
|
2014-06-04 07:45:56 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-06-11 10:52:12 -05:00
|
|
|
/// use std::os;
|
|
|
|
///
|
2014-06-04 07:45:56 -05:00
|
|
|
/// let key = "HOME";
|
2014-06-11 10:52:12 -05:00
|
|
|
/// match os::getenv(key) {
|
2014-06-04 07:45:56 -05:00
|
|
|
/// Some(val) => println!("{}: {}", key, val),
|
2014-06-08 12:22:49 -05:00
|
|
|
/// None => println!("{} is not defined in the environment.", key)
|
2014-06-04 07:45:56 -05:00
|
|
|
/// }
|
|
|
|
/// ```
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn getenv(n: &str) -> Option<String> {
|
2014-07-10 11:21:16 -05:00
|
|
|
getenv_as_bytes(n).map(|v| String::from_utf8_lossy(v.as_slice()).into_string())
|
2014-02-14 17:18:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
/// Fetches the environment variable `n` byte vector from the current process,
|
|
|
|
/// returning None if the variable isn't set.
|
|
|
|
///
|
2014-11-11 12:36:09 -06:00
|
|
|
/// # Panics
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
2014-11-11 12:36:09 -06:00
|
|
|
/// Panics if `n` has any interior NULs.
|
2014-05-20 01:19:56 -05:00
|
|
|
pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
|
2014-02-14 17:18:51 -06:00
|
|
|
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-09-17 14:56:31 -05:00
|
|
|
Some(CString::new(s as *const i8, false).as_bytes_no_nul().to_vec())
|
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.
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn getenv(n: &str) -> Option<String> {
|
2013-01-20 00:54:29 -06:00
|
|
|
unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
2014-12-07 12:25:51 -06:00
|
|
|
use os::windoze::{fill_utf16_buf_and_decode};
|
2014-09-17 14:56:31 -05:00
|
|
|
let mut n: Vec<u16> = n.utf16_units().collect();
|
|
|
|
n.push(0);
|
2014-06-02 16:51:58 -05:00
|
|
|
fill_utf16_buf_and_decode(|buf, sz| {
|
|
|
|
libc::GetEnvironmentVariableW(n.as_ptr(), 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.
|
2014-05-20 01:19:56 -05:00
|
|
|
pub fn getenv_as_bytes(n: &str) -> Option<Vec<u8>> {
|
2014-02-14 17:18:51 -06:00
|
|
|
getenv(n).map(|s| s.into_bytes())
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Sets the environment variable `n` to the value `v` for the currently running
|
2014-06-11 10:52:12 -05:00
|
|
|
/// process.
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
2014-06-11 10:52:12 -05:00
|
|
|
/// # Example
|
2014-02-14 17:18:51 -06:00
|
|
|
///
|
2014-06-11 10:52:12 -05:00
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
///
|
|
|
|
/// let key = "KEY";
|
|
|
|
/// os::setenv(key, "VALUE");
|
|
|
|
/// match os::getenv(key) {
|
|
|
|
/// Some(ref val) => println!("{}: {}", key, val),
|
|
|
|
/// None => println!("{} is not defined in the environment.", key)
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-06-30 12:19:02 -05:00
|
|
|
pub fn setenv<T: BytesContainer>(n: &str, v: T) {
|
2014-06-11 10:52:12 -05:00
|
|
|
#[cfg(unix)]
|
2014-06-30 12:19:02 -05:00
|
|
|
fn _setenv(n: &str, v: &[u8]) {
|
2014-06-11 10:52:12 -05:00
|
|
|
unsafe {
|
|
|
|
with_env_lock(|| {
|
|
|
|
n.with_c_str(|nbuf| {
|
|
|
|
v.with_c_str(|vbuf| {
|
2014-11-11 11:50:44 -06:00
|
|
|
if libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1) != 0 {
|
|
|
|
panic!(IoError::last_error());
|
|
|
|
}
|
2014-06-11 10:52:12 -05:00
|
|
|
})
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
|
|
|
})
|
2014-06-11 10:52:12 -05:00
|
|
|
}
|
2013-01-20 00:54:29 -06:00
|
|
|
}
|
2012-04-30 19:42:41 -05:00
|
|
|
|
2014-06-11 10:52:12 -05:00
|
|
|
#[cfg(windows)]
|
2014-06-30 12:19:02 -05:00
|
|
|
fn _setenv(n: &str, v: &[u8]) {
|
2014-09-17 14:56:31 -05:00
|
|
|
let mut n: Vec<u16> = n.utf16_units().collect();
|
|
|
|
n.push(0);
|
|
|
|
let mut v: Vec<u16> = ::str::from_utf8(v).unwrap().utf16_units().collect();
|
|
|
|
v.push(0);
|
2014-06-30 12:19:02 -05:00
|
|
|
|
2014-06-11 10:52:12 -05:00
|
|
|
unsafe {
|
|
|
|
with_env_lock(|| {
|
2014-11-11 11:50:44 -06:00
|
|
|
if libc::SetEnvironmentVariableW(n.as_ptr(), v.as_ptr()) == 0 {
|
|
|
|
panic!(IoError::last_error());
|
|
|
|
}
|
2014-06-11 10:52:12 -05:00
|
|
|
})
|
|
|
|
}
|
2012-04-30 19:42:41 -05:00
|
|
|
}
|
2014-06-30 12:19:02 -05:00
|
|
|
|
|
|
|
_setenv(n, v.container_as_bytes())
|
2012-04-30 19:42:41 -05:00
|
|
|
}
|
|
|
|
|
2014-06-11 10:52:12 -05:00
|
|
|
/// Remove a variable from the environment entirely.
|
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| {
|
2014-11-11 11:50:44 -06:00
|
|
|
if libc::funcs::posix01::unistd::unsetenv(nbuf) != 0 {
|
|
|
|
panic!(IoError::last_error());
|
|
|
|
}
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
|
|
|
})
|
2013-05-15 12:39:53 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-11 10:52:12 -05:00
|
|
|
|
2013-05-15 12:39:53 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn _unsetenv(n: &str) {
|
2014-09-17 14:56:31 -05:00
|
|
|
let mut n: Vec<u16> = n.utf16_units().collect();
|
|
|
|
n.push(0);
|
2013-05-15 12:39:53 -05:00
|
|
|
unsafe {
|
2013-11-20 16:17:12 -06:00
|
|
|
with_env_lock(|| {
|
2014-11-11 11:50:44 -06:00
|
|
|
if libc::SetEnvironmentVariableW(n.as_ptr(), ptr::null()) == 0 {
|
|
|
|
panic!(IoError::last_error());
|
|
|
|
}
|
2013-11-20 16:17:12 -06:00
|
|
|
})
|
2013-05-15 12:39:53 -05:00
|
|
|
}
|
|
|
|
}
|
2014-11-11 11:50:44 -06:00
|
|
|
|
|
|
|
_unsetenv(n)
|
2013-05-15 12:39:53 -05:00
|
|
|
}
|
|
|
|
|
2014-06-30 12:19:02 -05:00
|
|
|
/// Parses input according to platform conventions for the `PATH`
|
|
|
|
/// environment variable.
|
2014-06-11 10:52:12 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
///
|
|
|
|
/// let key = "PATH";
|
2014-06-30 12:19:02 -05:00
|
|
|
/// match os::getenv_as_bytes(key) {
|
2014-06-11 10:52:12 -05:00
|
|
|
/// Some(paths) => {
|
|
|
|
/// for path in os::split_paths(paths).iter() {
|
|
|
|
/// println!("'{}'", path.display());
|
|
|
|
/// }
|
|
|
|
/// }
|
2014-07-02 20:27:07 -05:00
|
|
|
/// None => println!("{} is not defined in the environment.", key)
|
2014-06-11 10:52:12 -05:00
|
|
|
/// }
|
|
|
|
/// ```
|
2014-05-30 12:31:20 -05:00
|
|
|
pub fn split_paths<T: BytesContainer>(unparsed: T) -> Vec<Path> {
|
2014-06-30 12:19:02 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn _split_paths<T: BytesContainer>(unparsed: T) -> Vec<Path> {
|
|
|
|
unparsed.container_as_bytes()
|
|
|
|
.split(|b| *b == b':')
|
|
|
|
.map(Path::new)
|
|
|
|
.collect()
|
|
|
|
}
|
2014-05-30 12:31:20 -05:00
|
|
|
|
2014-06-30 12:19:02 -05:00
|
|
|
#[cfg(windows)]
|
2014-07-03 14:01:33 -05:00
|
|
|
fn _split_paths<T: BytesContainer>(unparsed: T) -> Vec<Path> {
|
2014-06-30 12:19:02 -05:00
|
|
|
// On Windows, the PATH environment variable is semicolon separated. Double
|
|
|
|
// quotes are used as a way of introducing literal semicolons (since
|
|
|
|
// c:\some;dir is a valid Windows path). Double quotes are not themselves
|
|
|
|
// permitted in path names, so there is no way to escape a double quote.
|
|
|
|
// Quoted regions can appear in arbitrary locations, so
|
|
|
|
//
|
|
|
|
// c:\foo;c:\som"e;di"r;c:\bar
|
|
|
|
//
|
|
|
|
// Should parse as [c:\foo, c:\some;dir, c:\bar].
|
|
|
|
//
|
|
|
|
// (The above is based on testing; there is no clear reference available
|
|
|
|
// for the grammar.)
|
|
|
|
|
|
|
|
let mut parsed = Vec::new();
|
|
|
|
let mut in_progress = Vec::new();
|
|
|
|
let mut in_quote = false;
|
|
|
|
|
|
|
|
for b in unparsed.container_as_bytes().iter() {
|
|
|
|
match *b {
|
|
|
|
b';' if !in_quote => {
|
2014-05-30 12:31:20 -05:00
|
|
|
parsed.push(Path::new(in_progress.as_slice()));
|
2014-06-30 12:19:02 -05:00
|
|
|
in_progress.truncate(0)
|
|
|
|
}
|
|
|
|
b'"' => {
|
|
|
|
in_quote = !in_quote;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
in_progress.push(*b);
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 12:19:02 -05:00
|
|
|
}
|
|
|
|
parsed.push(Path::new(in_progress));
|
|
|
|
parsed
|
|
|
|
}
|
|
|
|
|
|
|
|
_split_paths(unparsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Joins a collection of `Path`s appropriately for the `PATH`
|
|
|
|
/// environment variable.
|
|
|
|
///
|
|
|
|
/// Returns a `Vec<u8>` on success, since `Path`s are not utf-8
|
|
|
|
/// encoded on all platforms.
|
|
|
|
///
|
|
|
|
/// Returns an `Err` (containing an error message) if one of the input
|
|
|
|
/// `Path`s contains an invalid character for constructing the `PATH`
|
|
|
|
/// variable (a double quote on Windows or a colon on Unix).
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
/// use std::path::Path;
|
|
|
|
///
|
|
|
|
/// let key = "PATH";
|
|
|
|
/// let mut paths = os::getenv_as_bytes(key).map_or(Vec::new(), os::split_paths);
|
|
|
|
/// paths.push(Path::new("/home/xyz/bin"));
|
|
|
|
/// os::setenv(key, os::join_paths(paths.as_slice()).unwrap());
|
|
|
|
/// ```
|
|
|
|
pub fn join_paths<T: BytesContainer>(paths: &[T]) -> Result<Vec<u8>, &'static str> {
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn _join_paths<T: BytesContainer>(paths: &[T]) -> Result<Vec<u8>, &'static str> {
|
|
|
|
let mut joined = Vec::new();
|
|
|
|
let sep = b';';
|
|
|
|
|
|
|
|
for (i, path) in paths.iter().map(|p| p.container_as_bytes()).enumerate() {
|
|
|
|
if i > 0 { joined.push(sep) }
|
|
|
|
if path.contains(&b'"') {
|
|
|
|
return Err("path segment contains `\"`");
|
|
|
|
} else if path.contains(&sep) {
|
|
|
|
joined.push(b'"');
|
|
|
|
joined.push_all(path);
|
|
|
|
joined.push(b'"');
|
|
|
|
} else {
|
|
|
|
joined.push_all(path);
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-30 12:19:02 -05:00
|
|
|
|
|
|
|
Ok(joined)
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
|
2014-06-30 12:19:02 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn _join_paths<T: BytesContainer>(paths: &[T]) -> Result<Vec<u8>, &'static str> {
|
|
|
|
let mut joined = Vec::new();
|
|
|
|
let sep = b':';
|
|
|
|
|
|
|
|
for (i, path) in paths.iter().map(|p| p.container_as_bytes()).enumerate() {
|
|
|
|
if i > 0 { joined.push(sep) }
|
|
|
|
if path.contains(&sep) { return Err("path segment contains separator `:`") }
|
|
|
|
joined.push_all(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(joined)
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
|
2014-06-30 12:19:02 -05:00
|
|
|
_join_paths(paths)
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
|
2014-03-16 17:59:04 -05:00
|
|
|
/// A low-level OS in-memory pipe.
|
2013-05-03 01:12:43 -05:00
|
|
|
pub struct Pipe {
|
2014-03-25 12:23:51 -05:00
|
|
|
/// A file descriptor representing the reading end of the pipe. Data written
|
|
|
|
/// on the `out` file descriptor can be read from this file descriptor.
|
2014-06-09 15:23:49 -05:00
|
|
|
pub reader: c_int,
|
2014-03-25 12:23:51 -05:00
|
|
|
/// A file descriptor representing the write end of the pipe. Data written
|
|
|
|
/// to this file descriptor can be read from the `input` file descriptor.
|
2014-06-09 15:23:49 -05:00
|
|
|
pub writer: c_int,
|
2013-05-03 01:12:43 -05:00
|
|
|
}
|
2013-02-07 20:23:47 -06:00
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for Pipe {}
|
|
|
|
|
2014-06-09 15:23:49 -05:00
|
|
|
/// Creates a new low-level OS in-memory pipe.
|
|
|
|
///
|
|
|
|
/// This function can fail to succeed if there are no more resources available
|
|
|
|
/// to allocate a pipe.
|
|
|
|
///
|
|
|
|
/// This function is also unsafe as there is no destructor associated with the
|
|
|
|
/// `Pipe` structure will return. If it is not arranged for the returned file
|
|
|
|
/// descriptors to be closed, the file descriptors will leak. For safe handling
|
|
|
|
/// of this scenario, use `std::io::PipeStream` instead.
|
|
|
|
pub unsafe fn pipe() -> IoResult<Pipe> {
|
2014-10-10 12:11:49 -05:00
|
|
|
let (reader, writer) = try!(sys::os::pipe());
|
|
|
|
Ok(Pipe {
|
|
|
|
reader: reader.unwrap(),
|
|
|
|
writer: writer.unwrap(),
|
|
|
|
})
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-06-11 10:52:12 -05:00
|
|
|
/// Returns the proper dll filename for the given basename of a file
|
|
|
|
/// as a String.
|
2014-05-05 02:07:49 -05:00
|
|
|
#[cfg(not(target_os="ios"))]
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn dll_filename(base: &str) -> String {
|
2014-05-27 22:44:58 -05:00
|
|
|
format!("{}{}{}", consts::DLL_PREFIX, base, consts::DLL_SUFFIX)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-06-11 10:52:12 -05:00
|
|
|
/// Optionally returns the filesystem path to the current executable which is
|
|
|
|
/// running but with the executable name.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
///
|
|
|
|
/// match os::self_exe_name() {
|
|
|
|
/// Some(exe_path) => println!("Path of this executable is: {}", exe_path.display()),
|
|
|
|
/// None => println!("Unable to get the path of this executable!")
|
|
|
|
/// };
|
|
|
|
/// ```
|
2014-01-22 15:41:53 -06:00
|
|
|
pub fn self_exe_name() -> Option<Path> {
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-09-29 00:31:50 -05:00
|
|
|
#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
|
2014-05-04 00:42:27 -05:00
|
|
|
fn load_self() -> Option<Vec<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::*;
|
2014-06-25 14:47:34 -05:00
|
|
|
let mut mib = vec![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;
|
2014-06-25 14:47:34 -05:00
|
|
|
let err = sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
|
2014-09-14 22:27:36 -05:00
|
|
|
ptr::null_mut(), &mut sz, ptr::null_mut(),
|
2014-01-23 13:38:22 -06:00
|
|
|
0u as libc::size_t);
|
2013-09-26 19:21:59 -05:00
|
|
|
if err != 0 { return None; }
|
|
|
|
if sz == 0 { return None; }
|
2014-04-18 19:04:18 -05:00
|
|
|
let mut v: Vec<u8> = Vec::with_capacity(sz as uint);
|
2014-06-25 14:47:34 -05:00
|
|
|
let err = sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
|
|
|
|
v.as_mut_ptr() as *mut c_void, &mut sz,
|
2014-09-14 22:27:36 -05:00
|
|
|
ptr::null_mut(), 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
|
2014-05-04 00:42:27 -05:00
|
|
|
Some(v)
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-09-29 00:31:50 -05:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2014-05-04 00:42:27 -05:00
|
|
|
fn load_self() -> Option<Vec<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")) {
|
2014-05-04 00:42:27 -05:00
|
|
|
Ok(path) => Some(path.into_vec()),
|
2014-01-30 18:55:20 -06:00
|
|
|
Err(..) => None
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 00:31:50 -05:00
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
2014-05-04 00:42:27 -05:00
|
|
|
fn load_self() -> Option<Vec<u8>> {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-09-26 19:21:59 -05:00
|
|
|
use libc::funcs::extra::_NSGetExecutablePath;
|
|
|
|
let mut sz: u32 = 0;
|
2014-09-14 22:27:36 -05:00
|
|
|
_NSGetExecutablePath(ptr::null_mut(), &mut sz);
|
2013-09-26 19:21:59 -05:00
|
|
|
if sz == 0 { return None; }
|
2014-04-17 17:28:14 -05:00
|
|
|
let mut v: Vec<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
|
2014-05-04 00:42:27 -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)]
|
2014-05-04 00:42:27 -05:00
|
|
|
fn load_self() -> Option<Vec<u8>> {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2014-12-07 12:25:51 -06:00
|
|
|
use os::windoze::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)
|
2014-05-25 05:17:19 -05:00
|
|
|
}).map(|s| s.into_string().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
|
2014-06-11 10:52:12 -05:00
|
|
|
/// running.
|
|
|
|
///
|
|
|
|
/// Like self_exe_name() but without the binary's name.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
///
|
|
|
|
/// match os::self_exe_path() {
|
|
|
|
/// Some(exe_path) => println!("Executable's Path is: {}", exe_path.display()),
|
|
|
|
/// None => println!("Impossible to fetch the path of this executable.")
|
|
|
|
/// };
|
|
|
|
/// ```
|
2014-01-22 15:41:53 -06:00
|
|
|
pub fn self_exe_path() -> Option<Path> {
|
|
|
|
self_exe_name().map(|mut p| { p.pop(); p })
|
2013-09-26 19:21:59 -05:00
|
|
|
}
|
|
|
|
|
2014-06-11 10:52:12 -05:00
|
|
|
/// Optionally returns the path to the current user's home directory if known.
|
|
|
|
///
|
|
|
|
/// # Unix
|
|
|
|
///
|
|
|
|
/// Returns the value of the 'HOME' environment variable if it is set
|
|
|
|
/// and not equal to the empty string.
|
|
|
|
///
|
|
|
|
/// # 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.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
///
|
|
|
|
/// match os::homedir() {
|
|
|
|
/// Some(ref p) => println!("{}", p.display()),
|
|
|
|
/// None => println!("Impossible to get your home dir!")
|
|
|
|
/// }
|
|
|
|
/// ```
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn homedir() -> Option<Path> {
|
2014-06-11 10:52:12 -05:00
|
|
|
#[inline]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2014-06-11 10:52:12 -05:00
|
|
|
fn _homedir() -> Option<Path> {
|
|
|
|
aux_homedir("HOME")
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-06-11 10:52:12 -05:00
|
|
|
#[inline]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2014-06-11 10:52:12 -05:00
|
|
|
fn _homedir() -> Option<Path> {
|
|
|
|
aux_homedir("HOME").or(aux_homedir("USERPROFILE"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn aux_homedir(home_name: &str) -> Option<Path> {
|
|
|
|
match getenv_as_bytes(home_name) {
|
|
|
|
Some(p) => {
|
|
|
|
if p.is_empty() { None } else { Path::new_opt(p) }
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
2014-06-11 10:52:12 -05:00
|
|
|
_homedir()
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Returns the path to a temporary directory.
|
|
|
|
///
|
|
|
|
/// On Unix, returns the value of the 'TMPDIR' environment variable if it is
|
|
|
|
/// set, otherwise for non-Android it returns '/tmp'. If Android, since there
|
|
|
|
/// is no global temporary folder (it is usually allocated per-app), we return
|
|
|
|
/// '/data/local/tmp'.
|
|
|
|
///
|
|
|
|
/// On Windows, returns the value of, in order, the 'TMP', 'TEMP',
|
|
|
|
/// 'USERPROFILE' environment variable if any are set and not the empty
|
|
|
|
/// string. Otherwise, tmpdir returns the path to the Windows directory.
|
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 {
|
2014-05-08 18:53:04 -05:00
|
|
|
let default = if cfg!(target_os = "android") {
|
|
|
|
Path::new("/data/local/tmp")
|
2013-09-01 06:31:05 -05:00
|
|
|
} else {
|
2014-05-08 18:53:04 -05:00
|
|
|
Path::new("/tmp")
|
|
|
|
};
|
|
|
|
|
|
|
|
getenv_nonempty("TMPDIR").unwrap_or(default)
|
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
|
|
|
|
2014-07-13 04:49:53 -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
|
|
|
|
/// as is.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
/// use std::path::Path;
|
|
|
|
///
|
|
|
|
/// // Assume we're in a path like /home/someuser
|
|
|
|
/// let rel_path = Path::new("..");
|
2014-11-10 23:38:20 -06:00
|
|
|
/// let abs_path = os::make_absolute(&rel_path).unwrap();
|
2014-07-13 04:49:53 -05:00
|
|
|
/// println!("The absolute path is {}", abs_path.display());
|
|
|
|
/// // Prints "The absolute path is /home"
|
|
|
|
/// ```
|
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.
|
2014-11-10 23:38:20 -06:00
|
|
|
pub fn make_absolute(p: &Path) -> IoResult<Path> {
|
2013-09-26 19:21:59 -05:00
|
|
|
if p.is_absolute() {
|
2014-11-10 23:38:20 -06:00
|
|
|
Ok(p.clone())
|
2013-03-01 21:57:05 -06:00
|
|
|
} else {
|
2014-11-10 23:38:20 -06:00
|
|
|
getcwd().map(|mut cwd| {
|
|
|
|
cwd.push(p);
|
|
|
|
cwd
|
|
|
|
})
|
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.
|
2014-07-13 04:49:53 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
/// use std::path::Path;
|
|
|
|
///
|
|
|
|
/// let root = Path::new("/");
|
2014-11-11 02:13:10 -06:00
|
|
|
/// assert!(os::change_dir(&root).is_ok());
|
2014-08-01 18:40:21 -05:00
|
|
|
/// println!("Successfully changed working directory to {}!", root.display());
|
2014-07-13 04:49:53 -05:00
|
|
|
/// ```
|
2014-11-11 02:13:10 -06:00
|
|
|
pub fn change_dir(p: &Path) -> IoResult<()> {
|
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)]
|
2014-11-11 02:13:10 -06:00
|
|
|
fn chdir(p: &Path) -> IoResult<()> {
|
|
|
|
let mut p = p.as_str().unwrap().utf16_units().collect::<Vec<u16>>();
|
|
|
|
p.push(0);
|
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2014-11-11 02:13:10 -06:00
|
|
|
match libc::SetCurrentDirectoryW(p.as_ptr()) != (0 as libc::BOOL) {
|
|
|
|
true => Ok(()),
|
|
|
|
false => Err(IoError::last_error()),
|
|
|
|
}
|
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)]
|
2014-11-11 02:13:10 -06:00
|
|
|
fn chdir(p: &Path) -> IoResult<()> {
|
2013-11-20 16:17:12 -06:00
|
|
|
p.with_c_str(|buf| {
|
2013-06-30 10:25:16 -05:00
|
|
|
unsafe {
|
2014-11-11 02:13:10 -06:00
|
|
|
match libc::chdir(buf) == (0 as c_int) {
|
|
|
|
true => Ok(()),
|
|
|
|
false => Err(IoError::last_error()),
|
|
|
|
}
|
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-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 {
|
2014-09-30 19:03:56 -05:00
|
|
|
os_imp::errno() as uint
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
|
2014-03-24 08:39:40 -05:00
|
|
|
/// Return the string corresponding to an `errno()` value of `errnum`.
|
2014-11-11 12:48:42 -06:00
|
|
|
///
|
2014-07-13 04:49:53 -05:00
|
|
|
/// # Example
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
///
|
|
|
|
/// // Same as println!("{}", last_os_error());
|
|
|
|
/// println!("{}", os::error_string(os::errno() as uint));
|
|
|
|
/// ```
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn error_string(errnum: uint) -> String {
|
2014-09-30 19:03:56 -05:00
|
|
|
return os_imp::error_string(errnum as i32);
|
2014-03-24 08:39:40 -05:00
|
|
|
}
|
2013-02-11 19:07:25 -06:00
|
|
|
|
2014-03-24 08:39:40 -05:00
|
|
|
/// Get a string representing the platform-dependent last error
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn last_os_error() -> String {
|
2014-03-24 08:39:40 -05:00
|
|
|
error_string(errno() as uint)
|
2012-04-19 03:23:00 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-10-10 23:59:10 -05:00
|
|
|
static EXIT_STATUS: AtomicInt = INIT_ATOMIC_INT;
|
2013-12-12 19:51:54 -06:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Sets the process exit code
|
|
|
|
///
|
|
|
|
/// Sets the exit code returned by the process if all supervised tasks
|
|
|
|
/// terminate successfully (without panicking). If the current root task panics
|
|
|
|
/// and is supervised by the scheduler then any user-specified exit status is
|
|
|
|
/// ignored and the process exits with the default panic status.
|
|
|
|
///
|
|
|
|
/// Note that this is not synchronized against modifications of other threads.
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn set_exit_status(code: int) {
|
2014-10-10 23:59:10 -05:00
|
|
|
EXIT_STATUS.store(code, SeqCst)
|
2013-12-12 19:51:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 {
|
2014-10-10 23:59:10 -05:00
|
|
|
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-06-25 14:47:34 -05:00
|
|
|
unsafe fn load_argc_and_argv(argc: int,
|
|
|
|
argv: *const *const c_char) -> Vec<Vec<u8>> {
|
2014-02-14 16:42:40 -06:00
|
|
|
use c_str::CString;
|
|
|
|
|
2014-04-17 17:28:14 -05:00
|
|
|
Vec::from_fn(argc as uint, |i| {
|
2014-09-17 14:56:31 -05:00
|
|
|
CString::new(*argv.offset(i as int), false).as_bytes_no_nul().to_vec()
|
2014-05-04 00:42:27 -05:00
|
|
|
})
|
2012-10-03 16:41:53 -05:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Returns the command line arguments
|
|
|
|
///
|
|
|
|
/// Returns a list of the command line arguments.
|
2012-10-01 16:34:53 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
2014-05-20 01:19:56 -05:00
|
|
|
fn real_args_as_bytes() -> Vec<Vec<u8>> {
|
2012-10-01 16:34:53 -05:00
|
|
|
unsafe {
|
2013-12-12 15:27:26 -06:00
|
|
|
let (argc, argv) = (*_NSGetArgc() as int,
|
2014-06-25 14:47:34 -05:00
|
|
|
*_NSGetArgv() as *const *const c_char);
|
2012-10-03 16:41:53 -05:00
|
|
|
load_argc_and_argv(argc, argv)
|
2012-10-01 16:34:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 02:07:49 -05:00
|
|
|
// As _NSGetArgc and _NSGetArgv aren't mentioned in iOS docs
|
|
|
|
// and use underscores in their names - they're most probably
|
|
|
|
// are considered private and therefore should be avoided
|
|
|
|
// Here is another way to get arguments using Objective C
|
|
|
|
// runtime
|
|
|
|
//
|
|
|
|
// In general it looks like:
|
|
|
|
// res = Vec::new()
|
|
|
|
// let args = [[NSProcessInfo processInfo] arguments]
|
|
|
|
// for i in range(0, [args count])
|
|
|
|
// res.push([args objectAtIndex:i])
|
|
|
|
// res
|
|
|
|
#[cfg(target_os = "ios")]
|
|
|
|
fn real_args_as_bytes() -> Vec<Vec<u8>> {
|
|
|
|
use c_str::CString;
|
|
|
|
use iter::range;
|
|
|
|
use mem;
|
|
|
|
|
|
|
|
#[link(name = "objc")]
|
|
|
|
extern {
|
2014-06-25 14:47:34 -05:00
|
|
|
fn sel_registerName(name: *const libc::c_uchar) -> Sel;
|
2014-05-05 02:07:49 -05:00
|
|
|
fn objc_msgSend(obj: NsId, sel: Sel, ...) -> NsId;
|
2014-06-25 14:47:34 -05:00
|
|
|
fn objc_getClass(class_name: *const libc::c_uchar) -> NsId;
|
2014-05-05 02:07:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[link(name = "Foundation", kind = "framework")]
|
|
|
|
extern {}
|
|
|
|
|
2014-06-25 14:47:34 -05:00
|
|
|
type Sel = *const libc::c_void;
|
|
|
|
type NsId = *const libc::c_void;
|
2014-05-05 02:07:49 -05:00
|
|
|
|
|
|
|
let mut res = Vec::new();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let processInfoSel = sel_registerName("processInfo\0".as_ptr());
|
|
|
|
let argumentsSel = sel_registerName("arguments\0".as_ptr());
|
|
|
|
let utf8Sel = sel_registerName("UTF8String\0".as_ptr());
|
|
|
|
let countSel = sel_registerName("count\0".as_ptr());
|
|
|
|
let objectAtSel = sel_registerName("objectAtIndex:\0".as_ptr());
|
|
|
|
|
|
|
|
let klass = objc_getClass("NSProcessInfo\0".as_ptr());
|
|
|
|
let info = objc_msgSend(klass, processInfoSel);
|
|
|
|
let args = objc_msgSend(info, argumentsSel);
|
|
|
|
|
|
|
|
let cnt: int = mem::transmute(objc_msgSend(args, countSel));
|
|
|
|
for i in range(0, cnt) {
|
|
|
|
let tmp = objc_msgSend(args, objectAtSel, i);
|
2014-06-25 14:47:34 -05:00
|
|
|
let utf_c_str: *const libc::c_char =
|
|
|
|
mem::transmute(objc_msgSend(tmp, utf8Sel));
|
2014-05-05 02:07:49 -05:00
|
|
|
let s = CString::new(utf_c_str, false);
|
2014-09-23 09:36:33 -05:00
|
|
|
res.push(s.as_bytes_no_nul().to_vec())
|
2014-05-05 02:07:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2014-09-29 00:31:50 -05:00
|
|
|
#[cfg(any(target_os = "linux",
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "dragonfly"))]
|
2014-05-20 01:19:56 -05:00
|
|
|
fn real_args_as_bytes() -> Vec<Vec<u8>> {
|
2014-11-14 18:30:16 -06:00
|
|
|
use rustrt;
|
2013-07-02 19:36:58 -05:00
|
|
|
|
2014-11-14 18:30:16 -06:00
|
|
|
match rustrt::args::clone() {
|
2013-08-01 01:12:20 -05:00
|
|
|
Some(args) => args,
|
2014-10-09 14:17:22 -05:00
|
|
|
None => panic!("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))]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn real_args() -> Vec<String> {
|
2014-09-14 22:27:36 -05:00
|
|
|
real_args_as_bytes().into_iter()
|
2014-05-20 01:19:56 -05:00
|
|
|
.map(|v| {
|
2014-07-10 11:21:16 -05:00
|
|
|
String::from_utf8_lossy(v.as_slice()).into_string()
|
2014-05-20 01:19:56 -05:00
|
|
|
}).collect()
|
2014-02-14 16:42:40 -06:00
|
|
|
}
|
|
|
|
|
2012-10-01 17:34:18 -05:00
|
|
|
#[cfg(windows)]
|
2014-05-22 18:57:53 -05:00
|
|
|
fn real_args() -> Vec<String> {
|
2014-03-08 17:11:52 -06:00
|
|
|
use slice;
|
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
|
|
|
|
2014-04-17 17:59:07 -05:00
|
|
|
let args = Vec::from_fn(nArgs as uint, |i| unsafe {
|
|
|
|
// Determine the length of this argument.
|
|
|
|
let ptr = *szArgList.offset(i as int);
|
|
|
|
let mut len = 0;
|
|
|
|
while *ptr.offset(len as int) != 0 { len += 1; }
|
|
|
|
|
|
|
|
// Push it onto the list.
|
2014-11-20 12:11:15 -06:00
|
|
|
let ptr = ptr as *const u16;
|
|
|
|
let buf = slice::from_raw_buf(&ptr, len);
|
|
|
|
let opt_s = String::from_utf16(::str::truncate_utf16_at_nul(buf));
|
2014-04-17 17:59:07 -05:00
|
|
|
opt_s.expect("CommandLineToArgvW returned invalid UTF-16")
|
|
|
|
});
|
2012-10-01 18:34:54 -05:00
|
|
|
|
|
|
|
unsafe {
|
2014-06-25 14:47:34 -05:00
|
|
|
LocalFree(szArgList as *mut c_void);
|
2012-10-01 18:34:54 -05:00
|
|
|
}
|
|
|
|
|
2014-05-04 00:42:27 -05:00
|
|
|
return args
|
2012-10-01 18:34:54 -05:00
|
|
|
}
|
|
|
|
|
2014-02-14 16:42:40 -06:00
|
|
|
#[cfg(windows)]
|
2014-05-20 01:19:56 -05:00
|
|
|
fn real_args_as_bytes() -> Vec<Vec<u8>> {
|
2014-09-14 22:27:36 -05:00
|
|
|
real_args().into_iter().map(|s| s.into_bytes()).collect()
|
2014-02-14 16:42:40 -06:00
|
|
|
}
|
|
|
|
|
2014-06-25 14:47:34 -05:00
|
|
|
type LPCWSTR = *const u16;
|
2012-10-01 18:34:54 -05:00
|
|
|
|
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;
|
2014-06-25 14:47:34 -05:00
|
|
|
fn LocalFree(ptr: *mut c_void);
|
2013-08-12 01:27:46 -05:00
|
|
|
}
|
|
|
|
|
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" {
|
2014-06-25 14:47:34 -05:00
|
|
|
fn CommandLineToArgvW(lpCmdLine: LPCWSTR,
|
|
|
|
pNumArgs: *mut c_int) -> *mut *mut u16;
|
2013-08-12 01:27:46 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
///
|
2014-11-14 00:26:13 -06:00
|
|
|
/// The first element is traditionally the path to the executable, but it can be
|
|
|
|
/// set to arbitrary text, and it may not even exist, so this property should not
|
2014-11-21 20:34:38 -06:00
|
|
|
/// be relied upon for security purposes.
|
2014-11-14 00:26:13 -06:00
|
|
|
///
|
2014-02-14 16:42:40 -06:00
|
|
|
/// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD.
|
2014-07-10 11:21:16 -05:00
|
|
|
/// See `String::from_utf8_lossy` for details.
|
2014-07-13 04:49:53 -05:00
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::os;
|
|
|
|
///
|
|
|
|
/// // Prints each argument on a separate line
|
|
|
|
/// for argument in os::args().iter() {
|
|
|
|
/// println!("{}", argument);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-05-22 18:57:53 -05:00
|
|
|
pub fn args() -> Vec<String> {
|
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.
|
2014-05-20 01:19:56 -05:00
|
|
|
pub fn args_as_bytes() -> Vec<Vec<u8>> {
|
2014-02-14 16:42:40 -06:00
|
|
|
real_args_as_bytes()
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:34:53 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
extern {
|
|
|
|
// These functions are in crt_externs.h.
|
2014-06-25 14:47:34 -05:00
|
|
|
pub fn _NSGetArgc() -> *mut c_int;
|
|
|
|
pub fn _NSGetArgv() -> *mut *mut *mut c_char;
|
2012-10-01 16:34:53 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 17:59:04 -05:00
|
|
|
/// Returns the page size of the current architecture in bytes.
|
2013-06-26 10:42:11 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn page_size() -> uint {
|
|
|
|
unsafe {
|
|
|
|
libc::sysconf(libc::_SC_PAGESIZE) as uint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 17:59:04 -05:00
|
|
|
/// Returns the page size of the current architecture in bytes.
|
2013-06-26 10:42:11 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn page_size() -> uint {
|
2014-02-26 11:58:41 -06:00
|
|
|
use mem;
|
2013-08-28 03:41:26 -05:00
|
|
|
unsafe {
|
2014-05-23 22:53:56 -05:00
|
|
|
let mut info = mem::zeroed();
|
2013-08-28 03:41:26 -05:00
|
|
|
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,
|
2014-08-23 03:12:59 -05:00
|
|
|
/// `VirtualAlloc`/`CreateFileMapping` on Windows). It makes no attempt at
|
2014-01-23 13:38:22 -06:00
|
|
|
/// 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.
|
2014-12-09 16:08:10 -06:00
|
|
|
#[allow(missing_copy_implementations)]
|
2013-06-26 10:42:11 -05:00
|
|
|
pub struct MemoryMap {
|
2014-07-10 15:20:39 -05:00
|
|
|
data: *mut u8,
|
|
|
|
len: uint,
|
|
|
|
kind: MemoryMapKind,
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
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.
|
2014-06-25 14:47:34 -05:00
|
|
|
MapFile(*const 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
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for MemoryMapKind {}
|
|
|
|
|
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.
|
2014-06-25 14:47:34 -05:00
|
|
|
MapAddr(*const u8),
|
2014-12-07 12:25:51 -06:00
|
|
|
/// Create a memory mapping for a file with a given HANDLE.
|
|
|
|
#[cfg(windows)]
|
|
|
|
MapFd(libc::HANDLE),
|
2013-11-08 08:18:52 -06:00
|
|
|
/// Create a memory mapping for a file with a given fd.
|
2014-12-07 12:25:51 -06:00
|
|
|
#[cfg(not(windows))]
|
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
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for MapOption {}
|
|
|
|
|
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),
|
2014-08-23 03:12:59 -05:00
|
|
|
/// ## The following are Windows-specific
|
2013-11-08 08:18:52 -06:00
|
|
|
///
|
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)
|
|
|
|
}
|
|
|
|
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
impl Copy for MapError {}
|
|
|
|
|
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-05-10 16:05:06 -05:00
|
|
|
return write!(out, "Unknown error = {}", code)
|
2014-01-23 13:38:22 -06:00
|
|
|
},
|
|
|
|
ErrVirtualAlloc(code) => {
|
2014-05-10 16:05:06 -05:00
|
|
|
return write!(out, "VirtualAlloc failure = {}", code)
|
2014-01-23 13:38:22 -06:00
|
|
|
},
|
2013-11-13 04:21:38 -06:00
|
|
|
ErrCreateFileMappingW(code) => {
|
2014-05-10 16:05:06 -05:00
|
|
|
return write!(out, "CreateFileMappingW failure = {}", code)
|
2013-11-13 04:21:38 -06:00
|
|
|
},
|
|
|
|
ErrMapViewOfFile(code) => {
|
2014-05-10 16:05:06 -05:00
|
|
|
return write!(out, "MapViewOfFile failure = {}", code)
|
2013-11-13 04:21:38 -06:00
|
|
|
}
|
|
|
|
};
|
2014-05-10 16:05:06 -05:00
|
|
|
write!(out, "{}", str)
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-03 16:24:49 -05:00
|
|
|
impl Error for MapError {
|
|
|
|
fn description(&self) -> &str { "memory map error" }
|
|
|
|
fn detail(&self) -> Option<String> { Some(self.to_string()) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromError<MapError> for Box<Error> {
|
|
|
|
fn from_error(err: MapError) -> Box<Error> {
|
|
|
|
box err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-18 16:50:49 -06:00
|
|
|
if min_len == 0 {
|
|
|
|
return Err(ErrZeroLength)
|
|
|
|
}
|
2014-06-25 14:47:34 -05:00
|
|
|
let mut addr: *const u8 = ptr::null();
|
2013-12-12 15:27:26 -06:00
|
|
|
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-06-17 16:14:55 -05:00
|
|
|
libc::mmap(addr as *mut c_void, len as libc::size_t, prot, flags,
|
|
|
|
fd, offset)
|
2013-06-26 10:42:11 -05:00
|
|
|
};
|
2014-06-17 16:14:55 -05:00
|
|
|
if r == 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 {
|
2014-10-09 14:17:22 -05:00
|
|
|
/// Unmap the mapping. Panics the task if `munmap` panics.
|
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 {
|
2014-10-09 14:17:22 -05:00
|
|
|
// `munmap` only panics due to logic errors
|
2014-06-17 16:14:55 -05:00
|
|
|
libc::munmap(self.data as *mut 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};
|
|
|
|
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut lpAddress: LPVOID = ptr::null_mut();
|
2013-06-26 10:42:11 -05:00
|
|
|
let mut readable = false;
|
|
|
|
let mut writable = false;
|
|
|
|
let mut executable = false;
|
2014-12-07 12:25:51 -06:00
|
|
|
let mut handle: HANDLE = libc::INVALID_HANDLE_VALUE;
|
2013-06-26 10:42:11 -05:00
|
|
|
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; },
|
2014-12-07 12:25:51 -06:00
|
|
|
MapFd(handle_) => { handle = handle_; },
|
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) {
|
2014-12-07 12:25:51 -06:00
|
|
|
(false, false, false) if handle == libc::INVALID_HANDLE_VALUE => libc::PAGE_NOACCESS,
|
2013-06-26 10:42:11 -05:00
|
|
|
(false, true, false) => libc::PAGE_READONLY,
|
|
|
|
(false, true, true) => libc::PAGE_READWRITE,
|
2014-12-07 12:25:51 -06:00
|
|
|
(true, false, false) if handle == libc::INVALID_HANDLE_VALUE => libc::PAGE_EXECUTE,
|
2013-06-26 10:42:11 -05:00
|
|
|
(true, true, false) => libc::PAGE_EXECUTE_READ,
|
|
|
|
(true, true, true) => libc::PAGE_EXECUTE_READWRITE,
|
|
|
|
_ => return Err(ErrUnsupProt)
|
|
|
|
};
|
|
|
|
|
2014-12-07 12:25:51 -06:00
|
|
|
if handle == libc::INVALID_HANDLE_VALUE {
|
2013-06-26 10:42:11 -05:00
|
|
|
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 {
|
2014-12-07 12:25:51 -06:00
|
|
|
let hFile = handle;
|
2013-06-26 10:42:11 -05:00
|
|
|
let mapping = libc::CreateFileMappingW(hFile,
|
2014-09-14 22:27:36 -05:00
|
|
|
ptr::null_mut(),
|
2013-06-26 10:42:11 -05:00
|
|
|
flProtect,
|
2013-08-28 03:41:26 -05:00
|
|
|
0,
|
|
|
|
0,
|
2013-06-26 10:42:11 -05:00
|
|
|
ptr::null());
|
2014-09-14 22:27:36 -05:00
|
|
|
if mapping == ptr::null_mut() {
|
2013-06-26 10:42:11 -05:00
|
|
|
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,
|
2014-06-25 14:47:34 -05:00
|
|
|
kind: MapFile(mapping as *const 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 {
|
2014-02-26 11:58:41 -06:00
|
|
|
use mem;
|
2013-08-28 03:41:26 -05:00
|
|
|
unsafe {
|
2014-05-23 22:53:56 -05:00
|
|
|
let mut info = mem::zeroed();
|
2013-08-28 03:41:26 -05:00
|
|
|
libc::GetSystemInfo(&mut info);
|
|
|
|
|
|
|
|
return info.dwAllocationGranularity as uint;
|
|
|
|
}
|
|
|
|
}
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
impl Drop for MemoryMap {
|
2014-11-11 12:36:09 -06:00
|
|
|
/// Unmap the mapping. Panics the task if any of `VirtualFree`,
|
2014-01-23 13:38:22 -06:00
|
|
|
/// `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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 15:20:39 -05:00
|
|
|
impl MemoryMap {
|
|
|
|
/// Returns the pointer to the memory created or modified by this map.
|
|
|
|
pub fn data(&self) -> *mut u8 { self.data }
|
|
|
|
/// Returns the number of bytes this map applies to.
|
|
|
|
pub fn len(&self) -> uint { self.len }
|
|
|
|
/// Returns the type of mapping this represents.
|
|
|
|
pub fn kind(&self) -> MemoryMapKind { self.kind }
|
|
|
|
}
|
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
#[cfg(target_os = "linux")]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub mod consts {
|
2014-05-25 18:21:07 -05:00
|
|
|
pub use os::arch_consts::ARCH;
|
2012-06-06 23:39:41 -05:00
|
|
|
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const FAMILY: &'static str = "unix";
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// A string describing the specific operating system in use: in this
|
|
|
|
/// case, `linux`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const SYSNAME: &'static str = "linux";
|
2012-12-20 03:26:27 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename prefix used for shared libraries on this
|
|
|
|
/// platform: in this case, `lib`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_PREFIX: &'static str = "lib";
|
2012-12-20 03:26:27 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for shared libraries on this
|
|
|
|
/// platform: in this case, `.so`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_SUFFIX: &'static str = ".so";
|
2012-12-20 03:26:27 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension used for shared libraries on this
|
|
|
|
/// platform that goes after the dot: in this case, `so`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_EXTENSION: &'static str = "so";
|
2012-11-29 18:21:49 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
|
|
|
/// platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_SUFFIX: &'static str = "";
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
|
|
|
/// on this platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_EXTENSION: &'static str = "";
|
2014-04-23 00:01:31 -05:00
|
|
|
}
|
2013-03-26 21:53:33 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
pub mod consts {
|
2014-05-25 18:21:07 -05:00
|
|
|
pub use os::arch_consts::ARCH;
|
2013-03-26 21:53:33 -05:00
|
|
|
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const FAMILY: &'static str = "unix";
|
2013-03-26 21:53:33 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// A string describing the specific operating system in use: in this
|
|
|
|
/// case, `macos`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const SYSNAME: &'static str = "macos";
|
2013-03-26 21:53:33 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename prefix used for shared libraries on this
|
|
|
|
/// platform: in this case, `lib`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_PREFIX: &'static str = "lib";
|
2013-03-26 21:53:33 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for shared libraries on this
|
|
|
|
/// platform: in this case, `.dylib`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_SUFFIX: &'static str = ".dylib";
|
2013-03-26 21:53:33 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension used for shared libraries on this
|
|
|
|
/// platform that goes after the dot: in this case, `dylib`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_EXTENSION: &'static str = "dylib";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
|
|
|
/// platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_SUFFIX: &'static str = "";
|
2014-05-05 02:07:49 -05:00
|
|
|
|
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
|
|
|
/// on this platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_EXTENSION: &'static str = "";
|
2014-05-05 02:07:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "ios")]
|
|
|
|
pub mod consts {
|
|
|
|
pub use os::arch_consts::ARCH;
|
|
|
|
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const FAMILY: &'static str = "unix";
|
2014-05-05 02:07:49 -05:00
|
|
|
|
|
|
|
/// A string describing the specific operating system in use: in this
|
|
|
|
/// case, `ios`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const SYSNAME: &'static str = "ios";
|
2014-05-05 02:07:49 -05:00
|
|
|
|
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
|
|
|
/// platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_SUFFIX: &'static str = "";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
|
|
|
/// on this platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_EXTENSION: &'static str = "";
|
2014-04-23 00:01:31 -05:00
|
|
|
}
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
pub mod consts {
|
2014-05-25 18:21:07 -05:00
|
|
|
pub use os::arch_consts::ARCH;
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const FAMILY: &'static str = "unix";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// A string describing the specific operating system in use: in this
|
|
|
|
/// case, `freebsd`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const SYSNAME: &'static str = "freebsd";
|
2012-12-20 03:26:27 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename prefix used for shared libraries on this
|
|
|
|
/// platform: in this case, `lib`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_PREFIX: &'static str = "lib";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for shared libraries on this
|
|
|
|
/// platform: in this case, `.so`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_SUFFIX: &'static str = ".so";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension used for shared libraries on this
|
|
|
|
/// platform that goes after the dot: in this case, `so`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_EXTENSION: &'static str = "so";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
|
|
|
/// platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_SUFFIX: &'static str = "";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
|
|
|
/// on this platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_EXTENSION: &'static str = "";
|
2014-07-29 09:44:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "dragonfly")]
|
|
|
|
pub mod consts {
|
|
|
|
pub use os::arch_consts::ARCH;
|
|
|
|
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const FAMILY: &'static str = "unix";
|
2014-07-29 09:44:39 -05:00
|
|
|
|
|
|
|
/// A string describing the specific operating system in use: in this
|
|
|
|
/// case, `dragonfly`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const SYSNAME: &'static str = "dragonfly";
|
2014-07-29 09:44:39 -05:00
|
|
|
|
|
|
|
/// Specifies the filename prefix used for shared libraries on this
|
|
|
|
/// platform: in this case, `lib`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_PREFIX: &'static str = "lib";
|
2014-07-29 09:44:39 -05:00
|
|
|
|
|
|
|
/// Specifies the filename suffix used for shared libraries on this
|
|
|
|
/// platform: in this case, `.so`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_SUFFIX: &'static str = ".so";
|
2014-07-29 09:44:39 -05:00
|
|
|
|
|
|
|
/// Specifies the file extension used for shared libraries on this
|
|
|
|
/// platform that goes after the dot: in this case, `so`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_EXTENSION: &'static str = "so";
|
2014-07-29 09:44:39 -05:00
|
|
|
|
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
|
|
|
/// platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_SUFFIX: &'static str = "";
|
2014-07-29 09:44:39 -05:00
|
|
|
|
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
|
|
|
/// on this platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_EXTENSION: &'static str = "";
|
2014-04-23 00:01:31 -05:00
|
|
|
}
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
pub mod consts {
|
2014-05-25 18:21:07 -05:00
|
|
|
pub use os::arch_consts::ARCH;
|
2012-12-20 03:26:27 -06:00
|
|
|
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const FAMILY: &'static str = "unix";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// A string describing the specific operating system in use: in this
|
|
|
|
/// case, `android`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const SYSNAME: &'static str = "android";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename prefix used for shared libraries on this
|
|
|
|
/// platform: in this case, `lib`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_PREFIX: &'static str = "lib";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for shared libraries on this
|
|
|
|
/// platform: in this case, `.so`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_SUFFIX: &'static str = ".so";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension used for shared libraries on this
|
|
|
|
/// platform that goes after the dot: in this case, `so`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_EXTENSION: &'static str = "so";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
|
|
|
/// platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_SUFFIX: &'static str = "";
|
2012-11-29 18:21:49 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
|
|
|
/// on this platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_EXTENSION: &'static str = "";
|
2014-04-23 00:01:31 -05:00
|
|
|
}
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-08-10 23:26:45 -05:00
|
|
|
#[cfg(target_os = "windows")]
|
2014-04-23 00:01:31 -05:00
|
|
|
pub mod consts {
|
2014-05-25 18:21:07 -05:00
|
|
|
pub use os::arch_consts::ARCH;
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const FAMILY: &'static str = "windows";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// A string describing the specific operating system in use: in this
|
2014-08-23 03:12:59 -05:00
|
|
|
/// case, `windows`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const SYSNAME: &'static str = "windows";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename prefix used for shared libraries on this
|
|
|
|
/// platform: in this case, the empty string.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_PREFIX: &'static str = "";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for shared libraries on this
|
|
|
|
/// platform: in this case, `.dll`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_SUFFIX: &'static str = ".dll";
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension used for shared libraries on this
|
|
|
|
/// platform that goes after the dot: in this case, `dll`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const DLL_EXTENSION: &'static str = "dll";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the filename suffix used for executable binaries on this
|
|
|
|
/// platform: in this case, `.exe`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_SUFFIX: &'static str = ".exe";
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
/// Specifies the file extension, if any, used for executable binaries
|
|
|
|
/// on this platform: in this case, `exe`.
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const EXE_EXTENSION: &'static str = "exe";
|
2014-04-23 00:01:31 -05:00
|
|
|
}
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
mod arch_consts {
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const ARCH: &'static str = "x86";
|
2014-04-23 00:01:31 -05:00
|
|
|
}
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
mod arch_consts {
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const ARCH: &'static str = "x86_64";
|
2014-04-23 00:01:31 -05:00
|
|
|
}
|
2014-03-16 17:59:04 -05:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
mod arch_consts {
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const ARCH: &'static str = "arm";
|
2014-04-23 00:01:31 -05:00
|
|
|
}
|
2012-12-20 03:26:27 -06:00
|
|
|
|
2014-04-23 00:01:31 -05:00
|
|
|
#[cfg(target_arch = "mips")]
|
|
|
|
mod arch_consts {
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const ARCH: &'static str = "mips";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-06-17 02:16:03 -05:00
|
|
|
#[cfg(target_arch = "mipsel")]
|
|
|
|
mod arch_consts {
|
2014-10-06 18:29:47 -05:00
|
|
|
pub const ARCH: &'static str = "mipsel";
|
2014-06-17 02:16:03 -05:00
|
|
|
}
|
2014-04-23 00:01:31 -05: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;
|
2014-05-25 18:31:46 -05:00
|
|
|
use os::{env, getcwd, getenv, make_absolute};
|
2014-06-30 12:19:02 -05:00
|
|
|
use os::{split_paths, join_paths, 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
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
fn make_rand_name() -> String {
|
2014-03-01 19:59:35 -06:00
|
|
|
let mut rng = rand::task_rng();
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 03:39:37 -05:00
|
|
|
let n = format!("TEST{}", rng.gen_ascii_chars().take(10u)
|
|
|
|
.collect::<String>());
|
2014-05-13 16:38:13 -05:00
|
|
|
assert!(getenv(n.as_slice()).is_none());
|
2014-05-16 12:45:16 -05:00
|
|
|
n
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
2014-06-16 15:36:07 -05:00
|
|
|
#[test]
|
|
|
|
fn test_num_cpus() {
|
|
|
|
assert!(os::num_cpus() > 0);
|
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
#[test]
|
|
|
|
fn test_setenv() {
|
|
|
|
let n = make_rand_name();
|
2014-05-16 12:45:16 -05:00
|
|
|
setenv(n.as_slice(), "VALUE");
|
2014-11-28 10:57:41 -06:00
|
|
|
assert_eq!(getenv(n.as_slice()), option::Option::Some("VALUE".to_string()));
|
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();
|
2014-05-16 12:45:16 -05:00
|
|
|
setenv(n.as_slice(), "VALUE");
|
|
|
|
unsetenv(n.as_slice());
|
2014-11-28 10:57:41 -06:00
|
|
|
assert_eq!(getenv(n.as_slice()), option::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();
|
2014-05-16 12:45:16 -05:00
|
|
|
setenv(n.as_slice(), "1");
|
|
|
|
setenv(n.as_slice(), "2");
|
2014-11-28 10:57:41 -06:00
|
|
|
assert_eq!(getenv(n.as_slice()), option::Option::Some("2".to_string()));
|
2014-05-16 12:45:16 -05:00
|
|
|
setenv(n.as_slice(), "");
|
2014-11-28 10:57:41 -06:00
|
|
|
assert_eq!(getenv(n.as_slice()), option::Option::Some("".to_string()));
|
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() {
|
2014-05-25 05:17:19 -05:00
|
|
|
let mut s = "".to_string();
|
2014-06-27 14:30:25 -05:00
|
|
|
let mut i = 0i;
|
2013-06-18 11:39:16 -05:00
|
|
|
while i < 100 {
|
2014-05-16 12:45:16 -05:00
|
|
|
s.push_str("aaaaaaaaaa");
|
2013-06-18 11:39:16 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
let n = make_rand_name();
|
2014-05-16 12:45:16 -05:00
|
|
|
setenv(n.as_slice(), s.as_slice());
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("{}", s.clone());
|
2014-11-28 10:57:41 -06:00
|
|
|
assert_eq!(getenv(n.as_slice()), option::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();
|
2014-10-15 02:07:48 -05:00
|
|
|
debug!("{}", path.display());
|
2014-01-22 15:41:53 -06:00
|
|
|
|
|
|
|
// 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();
|
2014-10-15 02:07:48 -05:00
|
|
|
debug!("{}", path.display());
|
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();
|
2014-10-15 02:07:48 -05:00
|
|
|
debug!("{}", n);
|
2014-05-16 12:45:16 -05:00
|
|
|
let v2 = getenv(n.as_slice());
|
2012-03-12 22:04:27 -05:00
|
|
|
// 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().
|
2014-11-28 10:57:41 -06:00
|
|
|
assert!(v2.is_none() || v2 == option::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();
|
2014-05-25 05:17:19 -05:00
|
|
|
let s = "x".repeat(10000).to_string();
|
2014-05-16 12:45:16 -05:00
|
|
|
setenv(n.as_slice(), s.as_slice());
|
|
|
|
assert_eq!(getenv(n.as_slice()), Some(s));
|
|
|
|
unsetenv(n.as_slice());
|
|
|
|
assert_eq!(getenv(n.as_slice()), None);
|
2014-02-19 10:11:00 -06:00
|
|
|
}
|
|
|
|
|
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();
|
2014-05-16 12:45:16 -05:00
|
|
|
setenv(n.as_slice(), "VALUE");
|
2014-05-25 05:17:19 -05:00
|
|
|
assert!(!e.contains(&(n.clone(), "VALUE".to_string())));
|
2012-03-12 22:04:27 -05:00
|
|
|
|
|
|
|
e = env();
|
2014-05-25 05:17:19 -05:00
|
|
|
assert!(e.contains(&(n, "VALUE".to_string())));
|
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
|
|
|
|
2014-11-10 23:38:20 -06:00
|
|
|
let cwd = getcwd().unwrap();
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("Current working directory: {}", cwd.display());
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2014-11-10 23:38:20 -06:00
|
|
|
debug!("{}", make_absolute(&Path::new("test-path")).unwrap().display());
|
|
|
|
debug!("{}", make_absolute(&Path::new("/usr/bin")).unwrap().display());
|
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
|
|
|
|
2014-05-16 12:45:16 -05:00
|
|
|
for s in oldhome.iter() {
|
2014-06-11 10:52:12 -05:00
|
|
|
setenv("HOME", s.as_slice());
|
2014-05-16 12:45:16 -05:00
|
|
|
}
|
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
|
|
|
|
2014-05-20 01:19:56 -05:00
|
|
|
for s in oldhome.iter() {
|
2014-06-11 10:52:12 -05:00
|
|
|
setenv("HOME", s.as_slice());
|
2014-05-20 01:19:56 -05:00
|
|
|
}
|
|
|
|
for s in olduserprofile.iter() {
|
2014-06-11 10:52:12 -05:00
|
|
|
setenv("USERPROFILE", s.as_slice());
|
2014-05-20 01:19:56 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-06-26 10:42:11 -05:00
|
|
|
#[test]
|
|
|
|
fn memory_map_rw() {
|
2014-11-28 10:57:41 -06:00
|
|
|
use result::Result::{Ok, Err};
|
2013-06-26 10:42:11 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
let chunk = match os::MemoryMap::new(16, &[
|
2013-06-26 10:42:11 -05:00
|
|
|
os::MapReadable,
|
|
|
|
os::MapWritable
|
|
|
|
]) {
|
|
|
|
Ok(chunk) => chunk,
|
2014-10-09 14:17:22 -05:00
|
|
|
Err(msg) => panic!("{}", 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() {
|
2014-12-09 12:26:04 -06:00
|
|
|
use libc;
|
2013-06-26 10:42:11 -05:00
|
|
|
use os::*;
|
2014-12-07 12:25:51 -06:00
|
|
|
use io::fs::{File, unlink};
|
|
|
|
use io::SeekStyle::SeekSet;
|
|
|
|
use io::FileMode::Open;
|
|
|
|
use io::FileAccess::ReadWrite;
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
2014-12-09 12:26:04 -06:00
|
|
|
fn get_fd(file: &File) -> libc::c_int {
|
2014-12-07 12:25:51 -06:00
|
|
|
use os::unix::AsRawFd;
|
|
|
|
file.as_raw_fd()
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
2014-12-07 12:25:51 -06:00
|
|
|
|
2013-06-26 10:42:11 -05:00
|
|
|
#[cfg(windows)]
|
2014-12-09 12:26:04 -06:00
|
|
|
fn get_fd(file: &File) -> libc::HANDLE {
|
2014-12-07 12:25:51 -06:00
|
|
|
use os::windows::AsRawHandle;
|
|
|
|
file.as_raw_handle()
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
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;
|
2014-12-07 12:25:51 -06:00
|
|
|
let mut file = File::open_mode(&path, Open, ReadWrite).unwrap();
|
|
|
|
file.seek(size as i64, SeekSet);
|
|
|
|
file.write_u8(0);
|
2013-06-26 10:42:11 -05:00
|
|
|
|
2014-12-07 12:25:51 -06:00
|
|
|
let chunk = MemoryMap::new(size / 2, &[
|
2013-06-26 10:42:11 -05:00
|
|
|
MapReadable,
|
|
|
|
MapWritable,
|
2014-12-07 12:25:51 -06:00
|
|
|
MapFd(get_fd(&file)),
|
2013-06-26 10:42:11 -05:00
|
|
|
MapOffset(size / 2)
|
2014-12-07 12:25:51 -06:00
|
|
|
]).unwrap();
|
2013-06-26 10:42:11 -05:00
|
|
|
assert!(chunk.len > 0);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
*chunk.data = 0xbe;
|
|
|
|
assert!(*chunk.data == 0xbe);
|
|
|
|
}
|
2014-02-01 13:24:42 -06:00
|
|
|
drop(chunk);
|
2013-12-04 17:20:26 -06:00
|
|
|
|
2014-12-07 12:25:51 -06:00
|
|
|
unlink(&path).unwrap();
|
2013-06-26 10:42:11 -05:00
|
|
|
}
|
|
|
|
|
2014-05-30 12:31:20 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn split_paths_windows() {
|
|
|
|
fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
|
|
|
|
split_paths(unparsed) ==
|
2014-11-21 00:20:04 -06:00
|
|
|
parsed.iter().map(|s| Path::new(*s)).collect::<Vec<_>>()
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(check_parse("", &mut [""]));
|
|
|
|
assert!(check_parse(r#""""#, &mut [""]));
|
|
|
|
assert!(check_parse(";;", &mut ["", "", ""]));
|
|
|
|
assert!(check_parse(r"c:\", &mut [r"c:\"]));
|
|
|
|
assert!(check_parse(r"c:\;", &mut [r"c:\", ""]));
|
2014-05-30 12:31:20 -05:00
|
|
|
assert!(check_parse(r"c:\;c:\Program Files\",
|
2014-11-17 02:39:01 -06:00
|
|
|
&mut [r"c:\", r"c:\Program Files\"]));
|
|
|
|
assert!(check_parse(r#"c:\;c:\"foo"\"#, &mut [r"c:\", r"c:\foo\"]));
|
2014-05-30 12:31:20 -05:00
|
|
|
assert!(check_parse(r#"c:\;c:\"foo;bar"\;c:\baz"#,
|
2014-11-17 02:39:01 -06:00
|
|
|
&mut [r"c:\", r"c:\foo;bar\", r"c:\baz"]));
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn split_paths_unix() {
|
|
|
|
fn check_parse(unparsed: &str, parsed: &[&str]) -> bool {
|
|
|
|
split_paths(unparsed) ==
|
2014-11-21 00:20:04 -06:00
|
|
|
parsed.iter().map(|s| Path::new(*s)).collect::<Vec<_>>()
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(check_parse("", &mut [""]));
|
|
|
|
assert!(check_parse("::", &mut ["", "", ""]));
|
|
|
|
assert!(check_parse("/", &mut ["/"]));
|
|
|
|
assert!(check_parse("/:", &mut ["/", ""]));
|
|
|
|
assert!(check_parse("/:/usr/local", &mut ["/", "/usr/local"]));
|
2014-05-30 12:31:20 -05:00
|
|
|
}
|
|
|
|
|
2014-06-30 12:19:02 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn join_paths_unix() {
|
|
|
|
fn test_eq(input: &[&str], output: &str) -> bool {
|
2014-11-27 13:43:55 -06:00
|
|
|
join_paths(input).unwrap() == output.as_bytes()
|
2014-06-30 12:19:02 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(test_eq(&[], ""));
|
|
|
|
assert!(test_eq(&["/bin", "/usr/bin", "/usr/local/bin"],
|
|
|
|
"/bin:/usr/bin:/usr/local/bin"));
|
|
|
|
assert!(test_eq(&["", "/bin", "", "", "/usr/bin", ""],
|
|
|
|
":/bin:::/usr/bin:"));
|
|
|
|
assert!(join_paths(&["/te:st"]).is_err());
|
2014-06-30 12:19:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn join_paths_windows() {
|
|
|
|
fn test_eq(input: &[&str], output: &str) -> bool {
|
2014-11-27 13:43:55 -06:00
|
|
|
join_paths(input).unwrap() == output.as_bytes()
|
2014-06-30 12:19:02 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:49:09 -06:00
|
|
|
assert!(test_eq(&[], ""));
|
|
|
|
assert!(test_eq(&[r"c:\windows", r"c:\"],
|
2014-06-30 12:19:02 -05:00
|
|
|
r"c:\windows;c:\"));
|
2014-11-17 18:49:09 -06:00
|
|
|
assert!(test_eq(&["", r"c:\windows", "", "", r"c:\", ""],
|
2014-06-30 12:19:02 -05:00
|
|
|
r";c:\windows;;;c:\;"));
|
2014-11-17 18:49:09 -06:00
|
|
|
assert!(test_eq(&[r"c:\te;st", r"c:\"],
|
2014-06-30 12:19:02 -05:00
|
|
|
r#""c:\te;st";c:\"#));
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(join_paths(&[r#"c:\te"st"#]).is_err());
|
2014-06-30 12:19:02 -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
|
|
|
}
|