2013-03-01 21:57:05 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* Higher-level interfaces to libc::* functions and operating system services.
|
|
|
|
*
|
|
|
|
* In general these take and return rust types, use rust idioms (enums,
|
|
|
|
* closures, vectors) rather than C idioms, and do more extensive safety
|
|
|
|
* checks.
|
|
|
|
*
|
|
|
|
* This module is not meant to only contain 1:1 mappings to libc entries; any
|
|
|
|
* os-interface code that is reasonably useful and broadly applicable can go
|
|
|
|
* here. Including utility routines that merely build on other os code.
|
|
|
|
*
|
|
|
|
* We assume the general case is that users do not care, and do not want to
|
|
|
|
* be made to care, which operating system they are on. While they may want
|
|
|
|
* to special case various special cases -- and so we will not _hide_ the
|
|
|
|
* facts of which OS the user is on -- they should be given the opportunity
|
|
|
|
* to write OS-ignorant code by default.
|
|
|
|
*/
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use cast;
|
|
|
|
use io;
|
2013-06-09 08:10:50 -05:00
|
|
|
use iterator::IteratorUtil;
|
2012-12-23 16:41:37 -06:00
|
|
|
use libc;
|
2013-03-26 15:38:07 -05:00
|
|
|
use libc::{c_char, c_void, c_int, size_t};
|
2013-04-23 15:22:19 -05:00
|
|
|
use libc::{mode_t, FILE};
|
2013-05-24 21:35:29 -05:00
|
|
|
use local_data;
|
2012-12-23 16:41:37 -06:00
|
|
|
use option;
|
2012-09-04 13:12:17 -05:00
|
|
|
use option::{Some, None};
|
2013-05-24 21:35:29 -05:00
|
|
|
use os;
|
2013-01-08 21:37:25 -06:00
|
|
|
use prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use ptr;
|
|
|
|
use str;
|
|
|
|
use uint;
|
2013-04-10 00:33:21 -05:00
|
|
|
use unstable::finally::Finally;
|
2012-12-23 16:41:37 -06:00
|
|
|
use vec;
|
2012-03-06 20:49:08 -06:00
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
pub use libc::fclose;
|
2012-12-13 15:05:22 -06:00
|
|
|
pub use os::consts::*;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Delegates to the libc close() function, returning the same return value.
|
2013-01-10 23:23:07 -06:00
|
|
|
pub fn close(fd: c_int) -> c_int {
|
|
|
|
unsafe {
|
|
|
|
libc::close(fd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 13:57:50 -06:00
|
|
|
pub mod rustrt {
|
|
|
|
use libc::{c_char, c_int};
|
|
|
|
use libc;
|
|
|
|
|
|
|
|
pub extern {
|
|
|
|
unsafe fn rust_get_argc() -> c_int;
|
|
|
|
unsafe fn rust_get_argv() -> **c_char;
|
|
|
|
unsafe fn rust_path_is_dir(path: *libc::c_char) -> c_int;
|
|
|
|
unsafe fn rust_path_exists(path: *libc::c_char) -> c_int;
|
|
|
|
unsafe fn rust_set_exit_status(code: libc::intptr_t);
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static TMPBUF_SZ : uint = 1000u;
|
|
|
|
static BUF_BYTES : uint = 2048u;
|
2012-03-06 20:49:08 -06:00
|
|
|
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn getcwd() -> Path {
|
2013-02-14 01:00:34 -06:00
|
|
|
let buf = [0 as libc::c_char, ..BUF_BYTES];
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-02-14 01:00:34 -06:00
|
|
|
if(0 as *libc::c_char == libc::getcwd(
|
|
|
|
&buf[0],
|
|
|
|
BUF_BYTES as libc::size_t)) {
|
|
|
|
fail!();
|
|
|
|
}
|
|
|
|
Path(str::raw::from_c_str(&buf[0]))
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-08-24 17:28:43 -05:00
|
|
|
}
|
|
|
|
|
2013-04-22 19:47:55 -05:00
|
|
|
// FIXME: move these to str perhaps? #2620
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
pub fn as_c_charp<T>(s: &str, f: &fn(*c_char) -> T) -> T {
|
2012-06-30 18:19:07 -05:00
|
|
|
str::as_c_str(s, |b| f(b as *c_char))
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool)
|
2012-08-20 14:23:37 -05:00
|
|
|
-> Option<~str> {
|
2013-02-12 17:34:48 -06:00
|
|
|
let mut buf = vec::from_elem(TMPBUF_SZ, 0u8 as c_char);
|
2012-07-24 14:35:34 -05:00
|
|
|
do vec::as_mut_buf(buf) |b, sz| {
|
2013-01-23 13:43:58 -06:00
|
|
|
if f(b, sz as size_t) {
|
|
|
|
unsafe {
|
|
|
|
Some(str::raw::from_buf(b as *u8))
|
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-12-13 15:05:22 -06:00
|
|
|
pub mod win32 {
|
2013-01-04 17:38:56 -06:00
|
|
|
use libc;
|
|
|
|
use vec;
|
|
|
|
use str;
|
2013-01-09 23:14:28 -06:00
|
|
|
use option::{None, Option};
|
2013-01-04 17:38:56 -06:00
|
|
|
use option;
|
2013-02-11 19:07:25 -06:00
|
|
|
use os::TMPBUF_SZ;
|
2012-12-13 15:05:22 -06:00
|
|
|
use libc::types::os::arch::extra::DWORD;
|
2012-03-06 20:49:08 -06:00
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
pub fn fill_utf16_buf_and_decode(f: &fn(*mut u16, DWORD) -> DWORD)
|
2012-08-20 14:23:37 -05:00
|
|
|
-> Option<~str> {
|
2013-01-10 23:58:30 -06:00
|
|
|
unsafe {
|
2013-02-11 19:07:25 -06:00
|
|
|
let mut n = TMPBUF_SZ as DWORD;
|
2013-01-10 23:58:30 -06:00
|
|
|
let mut res = None;
|
|
|
|
let mut done = false;
|
|
|
|
while !done {
|
2013-02-13 13:21:22 -06:00
|
|
|
let mut k: DWORD = 0;
|
2013-02-26 16:50:09 -06:00
|
|
|
let mut buf = vec::from_elem(n as uint, 0u16);
|
2013-01-10 23:58:30 -06:00
|
|
|
do vec::as_mut_buf(buf) |b, _sz| {
|
2013-02-13 13:21:22 -06:00
|
|
|
k = f(b, TMPBUF_SZ as DWORD);
|
2013-01-10 23:58:30 -06:00
|
|
|
if k == (0 as DWORD) {
|
|
|
|
done = true;
|
|
|
|
} else if (k == n &&
|
|
|
|
libc::GetLastError() ==
|
|
|
|
libc::ERROR_INSUFFICIENT_BUFFER as DWORD) {
|
|
|
|
n *= (2 as DWORD);
|
|
|
|
} else {
|
|
|
|
done = true;
|
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
2013-02-13 13:21:22 -06:00
|
|
|
if k != 0 && done {
|
|
|
|
let sub = vec::slice(buf, 0u, k as uint);
|
|
|
|
res = option::Some(str::from_utf16(sub));
|
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2013-01-10 23:58:30 -06:00
|
|
|
return res;
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
pub fn as_utf16_p<T>(s: &str, f: &fn(*u16) -> T) -> T {
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut t = str::to_utf16(s);
|
2012-03-06 20:49:08 -06:00
|
|
|
// Null terminate before passing on.
|
2013-05-29 13:10:16 -05:00
|
|
|
t += [0u16];
|
2012-09-13 18:16:15 -05:00
|
|
|
vec::as_imm_buf(t, |buf, _len| f(buf))
|
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
|
|
|
*/
|
|
|
|
fn with_env_lock<T>(f: &fn() -> T) -> T {
|
2013-05-08 17:26:07 -05:00
|
|
|
use unstable::finally::Finally;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-01-20 00:54:29 -06:00
|
|
|
unsafe {
|
2013-05-08 17:26:07 -05:00
|
|
|
return do (|| {
|
|
|
|
rust_take_env_lock();
|
|
|
|
f()
|
|
|
|
}).finally {
|
|
|
|
rust_drop_env_lock();
|
|
|
|
};
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-08 17:26:07 -05:00
|
|
|
extern {
|
|
|
|
#[fast_ffi]
|
|
|
|
fn rust_take_env_lock();
|
|
|
|
#[fast_ffi]
|
|
|
|
fn rust_drop_env_lock();
|
2013-01-20 00:54:29 -06:00
|
|
|
}
|
2012-07-25 16:37:40 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns a vector of (variable, value) pairs for all the environment
|
|
|
|
/// variables of the current process.
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn env() -> ~[(~str,~str)] {
|
|
|
|
unsafe {
|
2013-02-15 00:16:53 -06:00
|
|
|
#[cfg(windows)]
|
|
|
|
unsafe fn get_env_pairs() -> ~[~str] {
|
|
|
|
use libc::types::os::arch::extra::LPTCH;
|
|
|
|
use libc::funcs::extra::kernel32::{
|
|
|
|
GetEnvironmentStringsA,
|
|
|
|
FreeEnvironmentStringsA
|
|
|
|
};
|
|
|
|
let ch = GetEnvironmentStringsA();
|
|
|
|
if (ch as uint == 0) {
|
2013-05-09 06:52:07 -05:00
|
|
|
fail!("os::env() failure getting env string from OS: %s", os::last_os_error());
|
2013-02-15 00:16:53 -06:00
|
|
|
}
|
|
|
|
let mut curr_ptr: uint = ch as uint;
|
|
|
|
let mut result = ~[];
|
|
|
|
while(*(curr_ptr as *libc::c_char) != 0 as libc::c_char) {
|
|
|
|
let env_pair = str::raw::from_c_str(
|
|
|
|
curr_ptr as *libc::c_char);
|
|
|
|
result.push(env_pair);
|
|
|
|
curr_ptr +=
|
|
|
|
libc::strlen(curr_ptr as *libc::c_char) as uint
|
|
|
|
+ 1;
|
|
|
|
}
|
|
|
|
FreeEnvironmentStringsA(ch);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
unsafe fn get_env_pairs() -> ~[~str] {
|
2013-05-09 16:14:42 -05:00
|
|
|
extern {
|
2013-02-15 00:16:53 -06:00
|
|
|
unsafe fn rust_env_pairs() -> **libc::c_char;
|
|
|
|
}
|
2013-05-09 16:14:42 -05:00
|
|
|
let environ = rust_env_pairs();
|
2013-02-15 00:16:53 -06:00
|
|
|
if (environ as uint == 0) {
|
2013-05-09 06:52:07 -05:00
|
|
|
fail!("os::env() failure getting env string from OS: %s", os::last_os_error());
|
2013-02-15 00:16:53 -06:00
|
|
|
}
|
|
|
|
let mut result = ~[];
|
|
|
|
ptr::array_each(environ, |e| {
|
|
|
|
let env_pair = str::raw::from_c_str(e);
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!("get_env_pairs: %s",
|
|
|
|
env_pair);
|
2013-02-15 00:16:53 -06:00
|
|
|
result.push(env_pair);
|
|
|
|
});
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn env_convert(input: ~[~str]) -> ~[(~str, ~str)] {
|
2013-01-20 00:54:29 -06:00
|
|
|
let mut pairs = ~[];
|
2013-02-15 00:16:53 -06:00
|
|
|
for input.each |p| {
|
2013-06-09 08:10:50 -05:00
|
|
|
let vs: ~[&str] = p.splitn_iter('=', 1).collect();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!("splitting: len: %u",
|
|
|
|
vs.len());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(vs.len(), 2);
|
2013-06-09 08:10:50 -05:00
|
|
|
pairs.push((vs[0].to_owned(), vs[1].to_owned()));
|
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-02-15 00:16:53 -06:00
|
|
|
do with_env_lock {
|
|
|
|
let unparsed_environ = get_env_pairs();
|
|
|
|
env_convert(unparsed_environ)
|
|
|
|
}
|
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.
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn getenv(n: &str) -> Option<~str> {
|
|
|
|
unsafe {
|
|
|
|
do with_env_lock {
|
|
|
|
let s = str::as_c_str(n, |s| libc::getenv(s));
|
2013-04-20 09:27:16 -05:00
|
|
|
if ptr::null::<u8>() == cast::transmute(s) {
|
2013-01-20 00:54:29 -06:00
|
|
|
option::None::<~str>
|
|
|
|
} else {
|
2013-04-20 09:27:16 -05:00
|
|
|
let s = cast::transmute(s);
|
2013-01-20 00:54:29 -06:00
|
|
|
option::Some::<~str>(str::raw::from_buf(s))
|
2012-06-24 22:18:18 -05: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(windows)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Fetches the environment variable `n` from the current process, returning
|
|
|
|
/// None if the variable isn't set.
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn getenv(n: &str) -> Option<~str> {
|
|
|
|
unsafe {
|
|
|
|
do with_env_lock {
|
|
|
|
use os::win32::{as_utf16_p, fill_utf16_buf_and_decode};
|
|
|
|
do as_utf16_p(n) |u| {
|
|
|
|
do fill_utf16_buf_and_decode() |buf, sz| {
|
|
|
|
libc::GetEnvironmentVariableW(u, buf, sz)
|
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
|
|
|
/// Sets the environment variable `n` to the value `v` for the currently running
|
|
|
|
/// process
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn setenv(n: &str, v: &str) {
|
|
|
|
unsafe {
|
|
|
|
do with_env_lock {
|
|
|
|
do str::as_c_str(n) |nbuf| {
|
|
|
|
do str::as_c_str(v) |vbuf| {
|
|
|
|
libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
|
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(windows)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Sets the environment variable `n` to the value `v` for the currently running
|
|
|
|
/// process
|
2013-01-20 00:54:29 -06:00
|
|
|
pub fn setenv(n: &str, v: &str) {
|
|
|
|
unsafe {
|
|
|
|
do with_env_lock {
|
|
|
|
use os::win32::as_utf16_p;
|
|
|
|
do as_utf16_p(n) |nbuf| {
|
|
|
|
do as_utf16_p(v) |vbuf| {
|
|
|
|
libc::SetEnvironmentVariableW(nbuf, vbuf);
|
2012-04-30 19:42:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 12:39:53 -05:00
|
|
|
/// Remove a variable from the environment entirely
|
|
|
|
pub fn unsetenv(n: &str) {
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn _unsetenv(n: &str) {
|
|
|
|
unsafe {
|
|
|
|
do with_env_lock {
|
|
|
|
do str::as_c_str(n) |nbuf| {
|
|
|
|
libc::funcs::posix01::unistd::unsetenv(nbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn _unsetenv(n: &str) {
|
|
|
|
unsafe {
|
|
|
|
do with_env_lock {
|
|
|
|
use os::win32::as_utf16_p;
|
|
|
|
do as_utf16_p(n) |nbuf| {
|
|
|
|
libc::SetEnvironmentVariableW(nbuf, ptr::null());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_unsetenv(n);
|
|
|
|
}
|
|
|
|
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn fdopen(fd: c_int) -> *FILE {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
return do as_c_charp("r") |modebuf| {
|
|
|
|
libc::fdopen(fd, modebuf)
|
|
|
|
};
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-05 15:20:57 -06:00
|
|
|
// fsync related
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn fsync_fd(fd: c_int, _level: io::fsync::Level) -> c_int {
|
2013-01-10 23:49:31 -06:00
|
|
|
unsafe {
|
|
|
|
use libc::funcs::extra::msvcrt::*;
|
|
|
|
return commit(fd);
|
|
|
|
}
|
2012-03-05 15:20:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn fsync_fd(fd: c_int, level: io::fsync::Level) -> c_int {
|
2013-01-10 23:49:31 -06:00
|
|
|
unsafe {
|
|
|
|
use libc::funcs::posix01::unistd::*;
|
|
|
|
match level {
|
|
|
|
io::fsync::FSync
|
|
|
|
| io::fsync::FullFSync => return fsync(fd),
|
|
|
|
io::fsync::FDataSync => return fdatasync(fd)
|
|
|
|
}
|
2012-03-05 15:20:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn fsync_fd(fd: c_int, level: io::fsync::Level) -> c_int {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use libc::consts::os::extra::*;
|
|
|
|
use libc::funcs::posix88::fcntl::*;
|
|
|
|
use libc::funcs::posix01::unistd::*;
|
|
|
|
match level {
|
|
|
|
io::fsync::FSync => return fsync(fd),
|
|
|
|
_ => {
|
|
|
|
// According to man fnctl, the ok retval is only specified to be
|
|
|
|
// !=-1
|
|
|
|
if (fcntl(F_FULLFSYNC as c_int, fd) == -1 as c_int)
|
|
|
|
{ return -1 as c_int; }
|
|
|
|
else
|
|
|
|
{ return 0 as c_int; }
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 15:20:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn fsync_fd(fd: c_int, _l: io::fsync::Level) -> c_int {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use libc::funcs::posix01::unistd::*;
|
|
|
|
return fsync(fd);
|
|
|
|
}
|
2012-03-05 15:20:57 -06:00
|
|
|
}
|
|
|
|
|
2013-05-03 01:12:43 -05:00
|
|
|
pub struct Pipe {
|
|
|
|
in: c_int,
|
|
|
|
out: c_int
|
|
|
|
}
|
2013-02-07 20:23:47 -06:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2013-02-07 20:23:47 -06:00
|
|
|
pub fn pipe() -> Pipe {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-03-01 21:57:05 -06:00
|
|
|
let mut fds = Pipe {in: 0 as c_int,
|
2013-04-30 13:10:21 -05:00
|
|
|
out: 0 as c_int };
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(libc::pipe(&mut fds.in), (0 as c_int));
|
2013-02-07 20:23:47 -06:00
|
|
|
return Pipe {in: fds.in, out: fds.out};
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2013-02-07 20:23:47 -06:00
|
|
|
pub fn pipe() -> Pipe {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
// Windows pipes work subtly differently than unix pipes, and their
|
|
|
|
// inheritance has to be handled in a different way that I do not
|
|
|
|
// fully understand. Here we explicitly make the pipe non-inheritable,
|
|
|
|
// which means to pass it to a subprocess they need to be duplicated
|
2013-04-30 17:00:07 -05:00
|
|
|
// first, as in core::run.
|
2013-03-01 21:57:05 -06:00
|
|
|
let mut fds = Pipe {in: 0 as c_int,
|
|
|
|
out: 0 as c_int };
|
2013-03-26 15:38:07 -05:00
|
|
|
let res = libc::pipe(&mut fds.in, 1024 as ::libc::c_uint,
|
2013-01-10 23:23:07 -06:00
|
|
|
(libc::O_BINARY | libc::O_NOINHERIT) as c_int);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(res, 0 as c_int);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((fds.in != -1 as c_int && fds.in != 0 as c_int));
|
|
|
|
assert!((fds.out != -1 as c_int && fds.in != 0 as c_int));
|
2013-02-07 20:23:47 -06:00
|
|
|
return Pipe {in: fds.in, out: fds.out};
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-21 18:00:14 -05:00
|
|
|
fn dup2(src: c_int, dst: c_int) -> c_int {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
libc::dup2(src, dst)
|
|
|
|
}
|
2012-08-21 18:00:14 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns the proper dll filename for the given basename of a file.
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn dll_filename(base: &str) -> ~str {
|
2013-05-29 13:10:16 -05:00
|
|
|
fmt!("%s%s%s", DLL_PREFIX, base, DLL_SUFFIX)
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Optionally returns the filesystem path to the current executable which is
|
|
|
|
/// running. If any failure occurs, None is returned.
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn self_exe_path() -> Option<Path> {
|
2012-03-02 19:20:00 -06:00
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn load_self() -> Option<~str> {
|
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::*;
|
2012-06-30 18:19:07 -05:00
|
|
|
do fill_charp_buf() |buf, sz| {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mib = ~[CTL_KERN as c_int,
|
2012-06-24 22:18:18 -05:00
|
|
|
KERN_PROC as c_int,
|
2012-06-29 18:26:56 -05:00
|
|
|
KERN_PROC_PATHNAME as c_int, -1 as c_int];
|
2013-02-18 22:12:17 -06:00
|
|
|
let mut sz = sz;
|
2013-05-14 04:52:12 -05:00
|
|
|
sysctl(vec::raw::to_ptr(mib), mib.len() as ::libc::c_uint,
|
2013-02-15 19:28:51 -06:00
|
|
|
buf as *mut c_void, &mut sz, ptr::null(),
|
2013-02-14 18:00:04 -06:00
|
|
|
0u as size_t) == (0 as c_int)
|
2012-06-24 22:18:18 -05:00
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn load_self() -> Option<~str> {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use libc::funcs::posix01::unistd::readlink;
|
2013-01-13 16:33:47 -06:00
|
|
|
|
2013-02-11 19:07:25 -06:00
|
|
|
let mut path_str = str::with_capacity(TMPBUF_SZ);
|
2013-01-13 16:33:47 -06:00
|
|
|
let len = do str::as_c_str(path_str) |buf| {
|
|
|
|
let buf = buf as *mut c_char;
|
2013-01-10 23:23:07 -06:00
|
|
|
do as_c_charp("/proc/self/exe") |proc_self_buf| {
|
2013-02-11 19:07:25 -06:00
|
|
|
readlink(proc_self_buf, buf, TMPBUF_SZ as size_t)
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-13 16:33:47 -06:00
|
|
|
};
|
|
|
|
if len == -1 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
str::raw::set_len(&mut path_str, len as uint);
|
|
|
|
Some(path_str)
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 20:49:08 -06:00
|
|
|
#[cfg(target_os = "macos")]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn load_self() -> Option<~str> {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
do fill_charp_buf() |buf, sz| {
|
2013-02-14 20:34:54 -06:00
|
|
|
let mut sz = sz as u32;
|
2013-01-10 23:23:07 -06:00
|
|
|
libc::funcs::extra::_NSGetExecutablePath(
|
2013-02-14 20:34:54 -06:00
|
|
|
buf, &mut sz) == (0 as c_int)
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
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)]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn load_self() -> Option<~str> {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use os::win32::fill_utf16_buf_and_decode;
|
|
|
|
do fill_utf16_buf_and_decode() |buf, sz| {
|
|
|
|
libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
|
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-09-21 21:37:57 -05:00
|
|
|
do load_self().map |pth| {
|
2012-09-26 18:27:12 -05:00
|
|
|
Path(*pth).dir_path()
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns the path to the user's home directory, if known.
|
|
|
|
*
|
|
|
|
* On Unix, returns the value of the 'HOME' environment variable if it is set
|
|
|
|
* and not equal to the empty string.
|
|
|
|
*
|
|
|
|
* On Windows, returns the value of the 'HOME' environment variable if it is
|
|
|
|
* set and not equal to the empty string. Otherwise, returns the value of the
|
|
|
|
* 'USERPROFILE' environment variable if it is set and not equal to the empty
|
|
|
|
* string.
|
|
|
|
*
|
|
|
|
* Otherwise, homedir returns option::none.
|
|
|
|
*/
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn homedir() -> Option<Path> {
|
2013-05-19 00:07:44 -05:00
|
|
|
return match getenv("HOME") {
|
2012-09-28 15:00:07 -05:00
|
|
|
Some(ref p) => if !str::is_empty(*p) {
|
|
|
|
Some(Path(*p))
|
2012-08-03 21:59:04 -05:00
|
|
|
} else {
|
|
|
|
secondary()
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-20 14:23:37 -05:00
|
|
|
None => secondary()
|
2012-03-02 19:20:00 -06:00
|
|
|
};
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn secondary() -> Option<Path> {
|
|
|
|
None
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-20 14:23:37 -05:00
|
|
|
fn secondary() -> Option<Path> {
|
2013-03-16 14:49:12 -05:00
|
|
|
do getenv(~"USERPROFILE").chain |p| {
|
2012-03-02 19:20:00 -06:00
|
|
|
if !str::is_empty(p) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(Path(p))
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 21:04:26 -05:00
|
|
|
/**
|
2012-08-21 14:23:46 -05:00
|
|
|
* Returns the path to a temporary directory.
|
2012-08-20 21:04:26 -05:00
|
|
|
*
|
|
|
|
* On Unix, returns the value of the 'TMPDIR' environment variable if it is
|
|
|
|
* set and non-empty and '/tmp' otherwise.
|
|
|
|
*
|
|
|
|
* On Windows, returns the value of, in order, the 'TMP', 'TEMP',
|
2012-08-21 14:23:46 -05:00
|
|
|
* 'USERPROFILE' environment variable if any are set and not the empty
|
|
|
|
* string. Otherwise, tmpdir returns the path to the Windows directory.
|
2012-08-20 21:04:26 -05:00
|
|
|
*/
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn tmpdir() -> Path {
|
2012-08-20 21:04:26 -05:00
|
|
|
return lookup();
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn getenv_nonempty(v: &str) -> Option<Path> {
|
2012-08-20 21:04:26 -05:00
|
|
|
match getenv(v) {
|
2013-02-15 02:51:28 -06:00
|
|
|
Some(x) =>
|
2012-08-20 21:04:26 -05:00
|
|
|
if str::is_empty(x) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2012-08-20 21:04:26 -05:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(Path(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-09-02 17:37:15 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-08-21 14:23:46 -05:00
|
|
|
fn lookup() -> Path {
|
2013-03-16 14:49:12 -05:00
|
|
|
getenv_nonempty("TMPDIR").get_or_default(Path("/tmp"))
|
2012-08-20 21:04:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2012-09-02 17:37:15 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
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(
|
|
|
|
getenv_nonempty("WINDIR")))).get_or_default(Path("C:\\Windows"))
|
2012-08-20 21:04:26 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 21:46:54 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Recursively walk a directory structure
|
2013-05-02 17:33:18 -05:00
|
|
|
pub fn walk_dir(p: &Path, f: &fn(&Path) -> bool) -> bool {
|
|
|
|
list_dir(p).each(|q| {
|
|
|
|
let path = &p.push(*q);
|
|
|
|
f(path) && (!path_is_dir(path) || walk_dir(path, f))
|
|
|
|
})
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Indicates whether a path represents a directory
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn path_is_dir(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
do str::as_c_str(p.to_str()) |buf| {
|
|
|
|
rustrt::rust_path_is_dir(buf) != 0 as c_int
|
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Indicates whether a path exists
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn path_exists(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
do str::as_c_str(p.to_str()) |buf| {
|
|
|
|
rustrt::rust_path_exists(buf) != 0 as c_int
|
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Convert a relative path to an absolute path
|
|
|
|
*
|
|
|
|
* If the given path is relative, return it prepended with the current working
|
|
|
|
* directory. If the given path is already an absolute path, return it
|
2013-03-01 21:57:05 -06:00
|
|
|
* as is.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-03-02 19:20:00 -06:00
|
|
|
// NB: this is here rather than in path because it is a form of environment
|
|
|
|
// querying; what it does depends on the process working directory, not just
|
|
|
|
// the input paths.
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn make_absolute(p: &Path) -> Path {
|
2013-03-01 21:57:05 -06:00
|
|
|
if p.is_absolute {
|
|
|
|
copy *p
|
|
|
|
} else {
|
|
|
|
getcwd().push_many(p.components)
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Creates a directory at the specified path
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn make_dir(p: &Path, mode: c_int) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return mkdir(p, mode);
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn mkdir(p: &Path, _mode: c_int) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use os::win32::as_utf16_p;
|
|
|
|
// FIXME: turn mode into something useful? #2623
|
|
|
|
do as_utf16_p(p.to_str()) |buf| {
|
|
|
|
libc::CreateDirectoryW(buf, unsafe {
|
2013-04-20 09:27:16 -05:00
|
|
|
cast::transmute(0)
|
2013-01-10 23:23:07 -06:00
|
|
|
})
|
|
|
|
!= (0 as libc::BOOL)
|
|
|
|
}
|
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(unix)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn mkdir(p: &Path, mode: c_int) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
do as_c_charp(p.to_str()) |c| {
|
|
|
|
libc::mkdir(c, mode as mode_t) == (0 as c_int)
|
|
|
|
}
|
2012-03-06 20:49:08 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-25 19:18:25 -05:00
|
|
|
/// Creates a directory with a given mode.
|
|
|
|
/// Returns true iff creation
|
|
|
|
/// succeeded. Also creates all intermediate subdirectories
|
|
|
|
/// if they don't already exist, giving all of them the same mode.
|
2013-04-28 23:25:35 -05:00
|
|
|
|
|
|
|
// tjc: if directory exists but with different permissions,
|
|
|
|
// should we return false?
|
2013-04-25 19:18:25 -05:00
|
|
|
pub fn mkdir_recursive(p: &Path, mode: c_int) -> bool {
|
|
|
|
if path_is_dir(p) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-28 23:25:35 -05:00
|
|
|
else if p.components.is_empty() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if p.components.len() == 1 {
|
2013-04-25 19:18:25 -05:00
|
|
|
// No parent directories to create
|
2013-04-28 23:25:35 -05:00
|
|
|
path_is_dir(p) || make_dir(p, mode)
|
2013-04-25 19:18:25 -05:00
|
|
|
}
|
|
|
|
else {
|
2013-04-28 23:25:35 -05:00
|
|
|
mkdir_recursive(&p.pop(), mode) && make_dir(p, mode)
|
2013-04-25 19:18:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Lists the contents of a directory
|
2012-09-02 17:37:15 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn list_dir(p: &Path) -> ~[~str] {
|
2013-05-22 20:57:24 -05:00
|
|
|
if p.components.is_empty() && !p.is_absolute() {
|
2013-05-22 15:29:49 -05:00
|
|
|
// Not sure what the right behavior is here, but this
|
|
|
|
// prevents a bounds check failure later
|
|
|
|
return ~[];
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-02-21 00:46:26 -06:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
unsafe fn get_list(p: &Path) -> ~[~str] {
|
2013-03-26 15:38:07 -05:00
|
|
|
use libc::{dirent_t};
|
2013-02-21 00:46:26 -06:00
|
|
|
use libc::{opendir, readdir, closedir};
|
2013-05-09 16:14:42 -05:00
|
|
|
extern {
|
|
|
|
unsafe fn rust_list_dir_val(ptr: *dirent_t) -> *libc::c_char;
|
2013-02-21 00:46:26 -06:00
|
|
|
}
|
|
|
|
let input = p.to_str();
|
|
|
|
let mut strings = ~[];
|
|
|
|
let input_ptr = ::cast::transmute(&input[0]);
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!("os::list_dir -- BEFORE OPENDIR");
|
2013-02-21 00:46:26 -06:00
|
|
|
let dir_ptr = opendir(input_ptr);
|
|
|
|
if (dir_ptr as uint != 0) {
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!("os::list_dir -- opendir() SUCCESS");
|
2013-02-21 00:46:26 -06:00
|
|
|
let mut entry_ptr = readdir(dir_ptr);
|
|
|
|
while (entry_ptr as uint != 0) {
|
2013-05-09 16:14:42 -05:00
|
|
|
strings.push(str::raw::from_c_str(rust_list_dir_val(
|
|
|
|
entry_ptr)));
|
2013-02-21 00:46:26 -06:00
|
|
|
entry_ptr = readdir(dir_ptr);
|
|
|
|
}
|
|
|
|
closedir(dir_ptr);
|
|
|
|
}
|
|
|
|
else {
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!("os::list_dir -- opendir() FAILURE");
|
2013-02-21 00:46:26 -06:00
|
|
|
}
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(
|
|
|
|
"os::list_dir -- AFTER -- #: %?",
|
|
|
|
strings.len());
|
2013-02-21 00:46:26 -06:00
|
|
|
strings
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
#[cfg(windows)]
|
2013-02-21 00:46:26 -06:00
|
|
|
unsafe fn get_list(p: &Path) -> ~[~str] {
|
|
|
|
use libc::types::os::arch::extra::{LPCTSTR, HANDLE, BOOL};
|
|
|
|
use libc::consts::os::extra::INVALID_HANDLE_VALUE;
|
|
|
|
use libc::wcslen;
|
|
|
|
use libc::funcs::extra::kernel32::{
|
|
|
|
FindFirstFileW,
|
|
|
|
FindNextFileW,
|
|
|
|
FindClose,
|
|
|
|
};
|
|
|
|
use os::win32::{
|
|
|
|
as_utf16_p
|
|
|
|
};
|
2013-05-07 17:54:06 -05:00
|
|
|
use rt::global_heap::{malloc_raw, free_raw};
|
2013-02-21 00:46:26 -06:00
|
|
|
#[nolink]
|
2013-05-09 16:14:42 -05:00
|
|
|
extern {
|
2013-02-21 00:46:26 -06:00
|
|
|
unsafe fn rust_list_dir_wfd_size() -> libc::size_t;
|
|
|
|
unsafe fn rust_list_dir_wfd_fp_buf(wfd: *libc::c_void)
|
|
|
|
-> *u16;
|
|
|
|
}
|
|
|
|
fn star(p: &Path) -> Path { p.push("*") }
|
|
|
|
do as_utf16_p(star(p).to_str()) |path_ptr| {
|
|
|
|
let mut strings = ~[];
|
2013-05-09 16:14:42 -05:00
|
|
|
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
|
2013-02-21 00:46:26 -06:00
|
|
|
let find_handle =
|
|
|
|
FindFirstFileW(
|
|
|
|
path_ptr,
|
|
|
|
::cast::transmute(wfd_ptr));
|
|
|
|
if find_handle as int != INVALID_HANDLE_VALUE {
|
|
|
|
let mut more_files = 1 as libc::c_int;
|
|
|
|
while more_files != 0 {
|
2013-05-09 16:14:42 -05:00
|
|
|
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr);
|
2013-02-21 00:46:26 -06:00
|
|
|
if fp_buf as uint == 0 {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("os::list_dir() failure: got null ptr from wfd");
|
2013-02-21 00:46:26 -06:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let fp_vec = vec::from_buf(
|
|
|
|
fp_buf, wcslen(fp_buf) as uint);
|
|
|
|
let fp_str = str::from_utf16(fp_vec);
|
|
|
|
strings.push(fp_str);
|
|
|
|
}
|
|
|
|
more_files = FindNextFileW(
|
|
|
|
find_handle,
|
|
|
|
::cast::transmute(wfd_ptr));
|
|
|
|
}
|
|
|
|
FindClose(find_handle);
|
|
|
|
free_raw(wfd_ptr);
|
|
|
|
}
|
|
|
|
strings
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do get_list(p).filtered |filename| {
|
2013-01-10 23:23:07 -06:00
|
|
|
*filename != ~"." && *filename != ~".."
|
|
|
|
}
|
2012-04-01 10:39:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Lists the contents of a directory
|
|
|
|
*
|
|
|
|
* This version prepends each entry with the directory.
|
|
|
|
*/
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn list_dir_path(p: &Path) -> ~[~Path] {
|
2012-11-17 17:57:14 -06:00
|
|
|
list_dir(p).map(|f| ~p.push(*f))
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-05-02 15:09:28 -05:00
|
|
|
/// Removes a directory at the specified path, after removing
|
|
|
|
/// all its contents. Use carefully!
|
|
|
|
pub fn remove_dir_recursive(p: &Path) -> bool {
|
|
|
|
let mut error_happened = false;
|
|
|
|
for walk_dir(p) |inner| {
|
|
|
|
if !error_happened {
|
|
|
|
if path_is_dir(inner) {
|
|
|
|
if !remove_dir_recursive(inner) {
|
|
|
|
error_happened = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if !remove_file(inner) {
|
|
|
|
error_happened = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Directory should now be empty
|
|
|
|
!error_happened && remove_dir(p)
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Removes a directory at the specified path
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn remove_dir(p: &Path) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return rmdir(p);
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn rmdir(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use os::win32::as_utf16_p;
|
|
|
|
return do as_utf16_p(p.to_str()) |buf| {
|
|
|
|
libc::RemoveDirectoryW(buf) != (0 as libc::BOOL)
|
|
|
|
};
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn rmdir(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
return do as_c_charp(p.to_str()) |buf| {
|
|
|
|
libc::rmdir(buf) == (0 as c_int)
|
|
|
|
};
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Changes the current working directory to the specified path, returning
|
|
|
|
/// whether the change was completed successfully or not.
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn change_dir(p: &Path) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return chdir(p);
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn chdir(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use os::win32::as_utf16_p;
|
|
|
|
return do as_utf16_p(p.to_str()) |buf| {
|
|
|
|
libc::SetCurrentDirectoryW(buf) != (0 as libc::BOOL)
|
|
|
|
};
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn chdir(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
return do as_c_charp(p.to_str()) |buf| {
|
|
|
|
libc::chdir(buf) == (0 as c_int)
|
|
|
|
};
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 19:58:24 -05:00
|
|
|
/// Changes the current working directory to the specified
|
|
|
|
/// path while acquiring a global lock, then calls `action`.
|
|
|
|
/// If the change is successful, releases the lock and restores the
|
|
|
|
/// CWD to what it was before, returning true.
|
|
|
|
/// Returns false if the directory doesn't exist or if the directory change
|
|
|
|
/// is otherwise unsuccessful.
|
|
|
|
pub fn change_dir_locked(p: &Path, action: &fn()) -> bool {
|
|
|
|
use unstable::global::global_data_clone_create;
|
2013-05-09 19:36:45 -05:00
|
|
|
use unstable::sync::{Exclusive, exclusive};
|
2013-04-30 19:58:24 -05:00
|
|
|
|
|
|
|
fn key(_: Exclusive<()>) { }
|
|
|
|
|
2013-05-23 21:12:16 -05:00
|
|
|
unsafe {
|
|
|
|
let result = global_data_clone_create(key, || { ~exclusive(()) });
|
2013-04-30 19:58:24 -05:00
|
|
|
|
2013-05-23 21:12:16 -05:00
|
|
|
do result.with_imm() |_| {
|
|
|
|
let old_dir = os::getcwd();
|
|
|
|
if change_dir(p) {
|
|
|
|
action();
|
|
|
|
change_dir(&old_dir)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
false
|
|
|
|
}
|
2013-04-30 19:58:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Copies a file from one location to another
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn copy_file(from: &Path, to: &Path) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return do_copy_file(from, to);
|
2012-04-02 16:07:07 -05:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn do_copy_file(from: &Path, to: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use os::win32::as_utf16_p;
|
|
|
|
return do as_utf16_p(from.to_str()) |fromp| {
|
|
|
|
do as_utf16_p(to.to_str()) |top| {
|
|
|
|
libc::CopyFileW(fromp, top, (0 as libc::BOOL)) !=
|
|
|
|
(0 as libc::BOOL)
|
|
|
|
}
|
2012-04-02 16:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn do_copy_file(from: &Path, to: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
let istream = do as_c_charp(from.to_str()) |fromp| {
|
|
|
|
do as_c_charp("rb") |modebuf| {
|
|
|
|
libc::fopen(fromp, modebuf)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if istream as uint == 0u {
|
|
|
|
return false;
|
2012-04-02 16:07:07 -05:00
|
|
|
}
|
2013-05-02 15:09:28 -05:00
|
|
|
// Preserve permissions
|
|
|
|
let from_mode = from.get_mode().expect("copy_file: couldn't get permissions \
|
|
|
|
for source file");
|
|
|
|
|
2013-01-10 23:23:07 -06:00
|
|
|
let ostream = do as_c_charp(to.to_str()) |top| {
|
|
|
|
do as_c_charp("w+b") |modebuf| {
|
|
|
|
libc::fopen(top, modebuf)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if ostream as uint == 0u {
|
|
|
|
fclose(istream);
|
|
|
|
return false;
|
2012-04-02 16:07:07 -05:00
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
let bufsize = 8192u;
|
|
|
|
let mut buf = vec::with_capacity::<u8>(bufsize);
|
|
|
|
let mut done = false;
|
|
|
|
let mut ok = true;
|
|
|
|
while !done {
|
|
|
|
do vec::as_mut_buf(buf) |b, _sz| {
|
|
|
|
let nread = libc::fread(b as *mut c_void, 1u as size_t,
|
|
|
|
bufsize as size_t,
|
|
|
|
istream);
|
|
|
|
if nread > 0 as size_t {
|
|
|
|
if libc::fwrite(b as *c_void, 1u as size_t, nread,
|
|
|
|
ostream) != nread {
|
|
|
|
ok = false;
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-04 19:26:17 -05:00
|
|
|
done = true;
|
|
|
|
}
|
2012-04-02 16:07:07 -05:00
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
|
|
|
fclose(istream);
|
|
|
|
fclose(ostream);
|
2013-05-02 15:09:28 -05:00
|
|
|
|
|
|
|
// Give the new file the old file's permissions
|
|
|
|
unsafe {
|
|
|
|
if do str::as_c_str(to.to_str()) |to_buf| {
|
|
|
|
libc::chmod(to_buf, from_mode as mode_t)
|
|
|
|
} != 0 {
|
|
|
|
return false; // should be a condition...
|
|
|
|
}
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
return ok;
|
2012-04-02 16:07:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Deletes an existing file
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn remove_file(p: &Path) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return unlink(p);
|
2012-03-05 15:20:57 -06:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn unlink(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
use os::win32::as_utf16_p;
|
|
|
|
return do as_utf16_p(p.to_str()) |buf| {
|
|
|
|
libc::DeleteFileW(buf) != (0 as libc::BOOL)
|
|
|
|
};
|
|
|
|
}
|
2012-03-05 15:20:57 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn unlink(p: &Path) -> bool {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
return do as_c_charp(p.to_str()) |buf| {
|
|
|
|
libc::unlink(buf) == (0 as c_int)
|
|
|
|
};
|
|
|
|
}
|
2012-03-05 15:20:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-11 19:07:25 -06:00
|
|
|
#[cfg(unix)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns the platform-specific value of errno
|
2013-02-11 19:07:25 -06:00
|
|
|
pub fn errno() -> int {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
fn errno_location() -> *c_int {
|
|
|
|
#[nolink]
|
|
|
|
extern {
|
|
|
|
unsafe fn __error() -> *c_int;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
__error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
fn errno_location() -> *c_int {
|
|
|
|
#[nolink]
|
|
|
|
extern {
|
|
|
|
unsafe fn __errno_location() -> *c_int;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
__errno_location()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
(*errno_location()) as int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Returns the platform-specific value of errno
|
2013-02-11 19:07:25 -06:00
|
|
|
pub fn errno() -> uint {
|
|
|
|
use libc::types::os::arch::extra::DWORD;
|
|
|
|
|
|
|
|
#[link_name = "kernel32"]
|
|
|
|
#[abi = "stdcall"]
|
2013-03-13 21:25:28 -05:00
|
|
|
extern "stdcall" {
|
2013-02-11 19:07:25 -06:00
|
|
|
unsafe fn GetLastError() -> DWORD;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2013-02-11 22:07:29 -06:00
|
|
|
GetLastError() as uint
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Get a string representing the platform-dependent last error
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn last_os_error() -> ~str {
|
2013-02-11 19:07:25 -06:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn strerror() -> ~str {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2013-04-30 13:10:21 -05:00
|
|
|
fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
|
2013-02-11 19:07:25 -06:00
|
|
|
#[nolink]
|
|
|
|
extern {
|
2013-04-30 13:10:21 -05:00
|
|
|
unsafe fn strerror_r(errnum: c_int, buf: *mut c_char,
|
2013-02-11 19:07:25 -06:00
|
|
|
buflen: size_t) -> c_int;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
strerror_r(errnum, buf, buflen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GNU libc provides a non-compliant version of strerror_r by default
|
|
|
|
// and requires macros to instead use the POSIX compliant variant.
|
2013-02-11 22:49:20 -06:00
|
|
|
// So we just use __xpg_strerror_r which is always POSIX compliant
|
2013-02-11 19:07:25 -06:00
|
|
|
#[cfg(target_os = "linux")]
|
2013-04-30 13:10:21 -05:00
|
|
|
fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
|
2013-02-11 19:07:25 -06:00
|
|
|
#[nolink]
|
|
|
|
extern {
|
2013-04-30 13:10:21 -05:00
|
|
|
unsafe fn __xpg_strerror_r(errnum: c_int, buf: *mut c_char,
|
2013-02-11 19:07:25 -06:00
|
|
|
buflen: size_t) -> c_int;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
__xpg_strerror_r(errnum, buf, buflen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut buf = [0 as c_char, ..TMPBUF_SZ];
|
|
|
|
unsafe {
|
2013-04-30 13:10:21 -05:00
|
|
|
let err = strerror_r(errno() as c_int, &mut buf[0],
|
2013-02-11 19:07:25 -06:00
|
|
|
TMPBUF_SZ as size_t);
|
|
|
|
if err < 0 {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("strerror_r failure");
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
2013-02-11 22:07:29 -06:00
|
|
|
|
2013-02-11 19:07:25 -06:00
|
|
|
str::raw::from_c_str(&buf[0])
|
|
|
|
}
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-02-11 19:07:25 -06:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn strerror() -> ~str {
|
2013-02-11 22:07:29 -06:00
|
|
|
use libc::types::os::arch::extra::DWORD;
|
|
|
|
use libc::types::os::arch::extra::LPSTR;
|
|
|
|
use libc::types::os::arch::extra::LPVOID;
|
|
|
|
|
|
|
|
#[link_name = "kernel32"]
|
|
|
|
#[abi = "stdcall"]
|
2013-03-13 21:25:28 -05:00
|
|
|
extern "stdcall" {
|
2013-02-11 22:07:29 -06:00
|
|
|
unsafe fn FormatMessageA(flags: DWORD, lpSrc: LPVOID,
|
|
|
|
msgId: DWORD, langId: DWORD,
|
|
|
|
buf: LPSTR, nsize: DWORD,
|
|
|
|
args: *c_void) -> DWORD;
|
|
|
|
}
|
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
static FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
|
|
|
|
static FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
|
2013-02-11 22:07:29 -06:00
|
|
|
|
|
|
|
let mut buf = [0 as c_char, ..TMPBUF_SZ];
|
|
|
|
|
|
|
|
// This value is calculated from the macro
|
|
|
|
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
|
|
|
|
let langId = 0x0800 as DWORD;
|
|
|
|
let err = errno() as DWORD;
|
2013-02-11 19:07:25 -06:00
|
|
|
unsafe {
|
2013-02-11 22:07:29 -06:00
|
|
|
let res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
ptr::mut_null(), err, langId,
|
2013-02-11 22:49:20 -06:00
|
|
|
&mut buf[0], TMPBUF_SZ as DWORD,
|
|
|
|
ptr::null());
|
2013-02-11 22:07:29 -06:00
|
|
|
if res == 0 {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("[%?] FormatMessage failure", errno());
|
2013-02-11 22:07:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
str::raw::from_c_str(&buf[0])
|
2013-02-11 19:07:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strerror()
|
2012-04-19 03:23:00 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Sets the process exit code
|
|
|
|
*
|
|
|
|
* Sets the exit code returned by the process if all supervised tasks
|
|
|
|
* terminate successfully (without failing). If the current root task fails
|
|
|
|
* and is supervised by the scheduler then any user-specified exit status is
|
|
|
|
* ignored and the process exits with the default failure status
|
|
|
|
*/
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn set_exit_status(code: int) {
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
|
|
|
rustrt::rust_set_exit_status(code as libc::intptr_t);
|
|
|
|
}
|
2012-04-19 03:26:17 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-10-03 16:41:53 -05:00
|
|
|
unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] {
|
|
|
|
let mut args = ~[];
|
|
|
|
for uint::range(0, argc as uint) |i| {
|
|
|
|
vec::push(&mut args, str::raw::from_c_str(*argv.offset(i)));
|
|
|
|
}
|
2013-02-15 02:51:28 -06:00
|
|
|
args
|
2012-10-03 16:41:53 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:34:53 -05:00
|
|
|
/**
|
|
|
|
* Returns the command line arguments
|
|
|
|
*
|
|
|
|
* Returns a list of the command line arguments.
|
|
|
|
*/
|
|
|
|
#[cfg(target_os = "macos")]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub fn real_args() -> ~[~str] {
|
2012-10-01 16:34:53 -05:00
|
|
|
unsafe {
|
2012-10-03 16:41:53 -05:00
|
|
|
let (argc, argv) = (*_NSGetArgc() as c_int,
|
|
|
|
*_NSGetArgv() as **c_char);
|
|
|
|
load_argc_and_argv(argc, argv)
|
2012-10-01 16:34:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 17:34:18 -05:00
|
|
|
#[cfg(target_os = "linux")]
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2012-10-02 12:31:37 -05:00
|
|
|
#[cfg(target_os = "freebsd")]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub fn real_args() -> ~[~str] {
|
2012-10-03 16:41:53 -05:00
|
|
|
unsafe {
|
|
|
|
let argc = rustrt::rust_get_argc();
|
|
|
|
let argv = rustrt::rust_get_argv();
|
|
|
|
load_argc_and_argv(argc, argv)
|
|
|
|
}
|
2012-10-02 12:31:37 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 17:34:18 -05:00
|
|
|
#[cfg(windows)]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub fn real_args() -> ~[~str] {
|
2012-10-01 18:34:54 -05:00
|
|
|
let mut nArgs: c_int = 0;
|
2013-06-08 14:02:32 -05:00
|
|
|
let lpArgCount: *mut c_int = &mut nArgs;
|
2013-01-24 22:38:43 -06:00
|
|
|
let lpCmdLine = unsafe { GetCommandLineW() };
|
|
|
|
let szArgList = unsafe { CommandLineToArgvW(lpCmdLine, lpArgCount) };
|
2012-10-01 18:34:54 -05:00
|
|
|
|
|
|
|
let mut args = ~[];
|
|
|
|
for uint::range(0, nArgs as uint) |i| {
|
|
|
|
unsafe {
|
|
|
|
// Determine the length of this argument.
|
|
|
|
let ptr = *szArgList.offset(i);
|
|
|
|
let mut len = 0;
|
|
|
|
while *ptr.offset(len) != 0 { len += 1; }
|
|
|
|
|
|
|
|
// Push it onto the list.
|
2012-10-03 16:41:53 -05:00
|
|
|
vec::push(&mut args,
|
|
|
|
vec::raw::buf_as_slice(ptr, len,
|
|
|
|
str::from_utf16));
|
2012-10-01 18:34:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
LocalFree(cast::transmute(szArgList));
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
type LPCWSTR = *u16;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[link_name="kernel32"]
|
|
|
|
#[abi="stdcall"]
|
2013-03-13 21:25:28 -05:00
|
|
|
extern "stdcall" {
|
2012-10-01 18:34:54 -05:00
|
|
|
fn GetCommandLineW() -> LPCWSTR;
|
|
|
|
fn LocalFree(ptr: *c_void);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[link_name="shell32"]
|
|
|
|
#[abi="stdcall"]
|
2013-03-13 21:25:28 -05:00
|
|
|
extern "stdcall" {
|
2012-10-01 18:34:54 -05:00
|
|
|
fn CommandLineToArgvW(lpCmdLine: LPCWSTR, pNumArgs: *mut c_int) -> **u16;
|
2012-10-01 17:34:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct OverriddenArgs {
|
|
|
|
val: ~[~str]
|
|
|
|
}
|
|
|
|
|
2012-10-02 13:37:37 -05:00
|
|
|
fn overridden_arg_key(_v: @OverriddenArgs) {}
|
2012-10-01 17:34:18 -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).
|
|
|
|
///
|
|
|
|
/// The return value of the function can be changed by invoking the
|
|
|
|
/// `os::set_args` function.
|
2012-10-01 17:34:18 -05:00
|
|
|
pub fn args() -> ~[~str] {
|
|
|
|
unsafe {
|
2013-05-08 08:02:21 -05:00
|
|
|
match local_data::local_data_get(overridden_arg_key) {
|
2012-10-01 17:34:18 -05:00
|
|
|
None => real_args(),
|
|
|
|
Some(args) => copy args.val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// For the current task, overrides the task-local cache of the arguments this
|
|
|
|
/// program had when it started. These new arguments are only available to the
|
|
|
|
/// current task via the `os::args` method.
|
2012-10-02 13:37:37 -05:00
|
|
|
pub fn set_args(new_args: ~[~str]) {
|
2012-10-01 17:34:18 -05:00
|
|
|
unsafe {
|
|
|
|
let overridden_args = @OverriddenArgs { val: copy new_args };
|
2013-05-08 08:02:21 -05:00
|
|
|
local_data::local_data_set(overridden_arg_key, overridden_args);
|
2012-10-01 17:34:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 00:33:21 -05:00
|
|
|
// FIXME #6100 we should really use an internal implementation of this - using
|
|
|
|
// the POSIX glob functions isn't portable to windows, probably has slight
|
|
|
|
// inconsistencies even where it is implemented, and makes extending
|
|
|
|
// functionality a lot more difficult
|
|
|
|
// FIXME #6101 also provide a non-allocating version - each_glob or so?
|
|
|
|
/// Returns a vector of Path objects that match the given glob pattern
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
pub fn glob(pattern: &str) -> ~[Path] {
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
fn default_glob_t () -> libc::glob_t {
|
|
|
|
libc::glob_t {
|
|
|
|
gl_pathc: 0,
|
|
|
|
gl_pathv: ptr::null(),
|
|
|
|
gl_offs: 0,
|
|
|
|
__unused1: ptr::null(),
|
|
|
|
__unused2: ptr::null(),
|
|
|
|
__unused3: ptr::null(),
|
|
|
|
__unused4: ptr::null(),
|
|
|
|
__unused5: ptr::null(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
fn default_glob_t () -> libc::glob_t {
|
|
|
|
libc::glob_t {
|
|
|
|
gl_pathc: 0,
|
|
|
|
__unused1: 0,
|
|
|
|
gl_offs: 0,
|
|
|
|
__unused2: 0,
|
|
|
|
gl_pathv: ptr::null(),
|
|
|
|
__unused3: ptr::null(),
|
|
|
|
__unused4: ptr::null(),
|
|
|
|
__unused5: ptr::null(),
|
|
|
|
__unused6: ptr::null(),
|
|
|
|
__unused7: ptr::null(),
|
|
|
|
__unused8: ptr::null(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
fn default_glob_t () -> libc::glob_t {
|
|
|
|
libc::glob_t {
|
|
|
|
gl_pathc: 0,
|
|
|
|
__unused1: 0,
|
|
|
|
gl_offs: 0,
|
|
|
|
__unused2: 0,
|
|
|
|
gl_pathv: ptr::null(),
|
|
|
|
__unused3: ptr::null(),
|
|
|
|
__unused4: ptr::null(),
|
|
|
|
__unused5: ptr::null(),
|
|
|
|
__unused6: ptr::null(),
|
|
|
|
__unused7: ptr::null(),
|
|
|
|
__unused8: ptr::null(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut g = default_glob_t();
|
|
|
|
do str::as_c_str(pattern) |c_pattern| {
|
|
|
|
unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) }
|
|
|
|
};
|
|
|
|
do(|| {
|
|
|
|
let paths = unsafe {
|
|
|
|
vec::raw::from_buf_raw(g.gl_pathv, g.gl_pathc as uint)
|
|
|
|
};
|
|
|
|
do paths.map |&c_str| {
|
|
|
|
Path(unsafe { str::raw::from_c_str(c_str) })
|
|
|
|
}
|
|
|
|
}).finally {
|
|
|
|
unsafe { libc::globfree(&mut g) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a vector of Path objects that match the given glob pattern
|
|
|
|
#[cfg(target_os = "win32")]
|
|
|
|
pub fn glob(pattern: &str) -> ~[Path] {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("glob() is unimplemented on Windows")
|
2013-04-10 00:33:21 -05:00
|
|
|
}
|
|
|
|
|
2012-10-01 16:34:53 -05:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
extern {
|
|
|
|
// These functions are in crt_externs.h.
|
|
|
|
pub fn _NSGetArgc() -> *c_int;
|
|
|
|
pub fn _NSGetArgv() -> ***c_char;
|
|
|
|
}
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
pub mod consts {
|
2012-06-06 23:39:41 -05:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
#[cfg(unix)]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::unix::*;
|
2012-06-06 23:39:41 -05:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
#[cfg(windows)]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::windows::*;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
#[cfg(target_os = "macos")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::macos::*;
|
2012-12-20 03:26:27 -06:00
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::freebsd::*;
|
2012-12-20 03:26:27 -06:00
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::linux::*;
|
2012-12-20 03:26:27 -06:00
|
|
|
|
2012-11-29 18:21:49 -06:00
|
|
|
#[cfg(target_os = "android")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::android::*;
|
2012-11-29 18:21:49 -06:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
#[cfg(target_os = "win32")]
|
2013-02-25 13:11:21 -06:00
|
|
|
pub use os::consts::win32::*;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-26 21:53:33 -05:00
|
|
|
#[cfg(target_arch = "x86")]
|
|
|
|
pub use os::consts::x86::*;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
pub use os::consts::x86_64::*;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "arm")]
|
|
|
|
pub use os::consts::arm::*;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "mips")]
|
|
|
|
use os::consts::mips::*;
|
|
|
|
|
|
|
|
pub mod unix {
|
|
|
|
pub static FAMILY: &'static str = "unix";
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod windows {
|
|
|
|
pub static FAMILY: &'static str = "windows";
|
|
|
|
}
|
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
pub mod macos {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "macos";
|
|
|
|
pub static DLL_PREFIX: &'static str = "lib";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".dylib";
|
|
|
|
pub static EXE_SUFFIX: &'static str = "";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod freebsd {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "freebsd";
|
|
|
|
pub static DLL_PREFIX: &'static str = "lib";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".so";
|
|
|
|
pub static EXE_SUFFIX: &'static str = "";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod linux {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "linux";
|
|
|
|
pub static DLL_PREFIX: &'static str = "lib";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".so";
|
|
|
|
pub static EXE_SUFFIX: &'static str = "";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
2012-11-29 18:21:49 -06:00
|
|
|
|
|
|
|
pub mod android {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "android";
|
|
|
|
pub static DLL_PREFIX: &'static str = "lib";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".so";
|
|
|
|
pub static EXE_SUFFIX: &'static str = "";
|
2012-11-29 18:21:49 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-12-20 03:26:27 -06:00
|
|
|
pub mod win32 {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static SYSNAME: &'static str = "win32";
|
|
|
|
pub static DLL_PREFIX: &'static str = "";
|
|
|
|
pub static DLL_SUFFIX: &'static str = ".dll";
|
|
|
|
pub static EXE_SUFFIX: &'static str = ".exe";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub mod x86 {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ARCH: &'static str = "x86";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
pub mod x86_64 {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ARCH: &'static str = "x86_64";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
|
|
|
pub mod arm {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ARCH: &'static str = "arm";
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
2013-01-29 08:28:08 -06:00
|
|
|
pub mod mips {
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ARCH: &'static str = "mips";
|
2013-01-29 08:28:08 -06:00
|
|
|
}
|
2012-12-20 03:26:27 -06:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2012-09-02 18:34:20 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-03-02 19:20:00 -06:00
|
|
|
mod tests {
|
2013-01-08 21:37:25 -06:00
|
|
|
use libc::{c_int, c_void, size_t};
|
2012-12-27 19:53:04 -06:00
|
|
|
use libc;
|
2013-03-26 15:38:07 -05:00
|
|
|
use option::Some;
|
2012-12-27 19:53:04 -06:00
|
|
|
use option;
|
2013-01-08 21:37:25 -06:00
|
|
|
use os::{as_c_charp, env, getcwd, getenv, make_absolute, real_args};
|
2013-05-15 12:39:53 -05:00
|
|
|
use os::{remove_file, setenv, unsetenv};
|
2012-12-27 19:53:04 -06:00
|
|
|
use os;
|
2013-01-08 21:37:25 -06:00
|
|
|
use path::Path;
|
2013-03-12 15:00:50 -05:00
|
|
|
use rand::RngUtil;
|
2012-12-27 19:53:04 -06:00
|
|
|
use rand;
|
|
|
|
use run;
|
|
|
|
use str;
|
|
|
|
use vec;
|
2013-04-28 23:25:35 -05:00
|
|
|
use libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
|
|
|
|
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-04-19 03:23:00 -05:00
|
|
|
#[test]
|
2012-09-27 16:08:37 -05:00
|
|
|
pub fn last_os_error() {
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(os::last_os_error());
|
2012-04-19 03:23:00 -05:00
|
|
|
}
|
2012-03-12 22:04:27 -05:00
|
|
|
|
2012-10-01 18:34:54 -05:00
|
|
|
#[test]
|
|
|
|
pub fn test_args() {
|
|
|
|
let a = real_args();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(a.len() >= 1);
|
2012-10-01 18:34:54 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn make_rand_name() -> ~str {
|
2013-05-07 19:57:58 -05:00
|
|
|
let mut rng = rand::rng();
|
2012-07-14 00:57:48 -05:00
|
|
|
let n = ~"TEST" + rng.gen_str(10u);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(getenv(n).is_none());
|
2013-02-15 02:51:28 -06:00
|
|
|
n
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_setenv() {
|
|
|
|
let n = make_rand_name();
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "VALUE");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::Some(~"VALUE"));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
2013-05-15 12:39:53 -05:00
|
|
|
#[test]
|
|
|
|
fn test_unsetenv() {
|
|
|
|
let n = make_rand_name();
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "VALUE");
|
2013-05-15 12:39:53 -05:00
|
|
|
unsetenv(n);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::None);
|
2013-05-15 12:39:53 -05:00
|
|
|
}
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
#[ignore]
|
2012-03-12 22:04:27 -05:00
|
|
|
fn test_setenv_overwrite() {
|
|
|
|
let n = make_rand_name();
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "1");
|
|
|
|
setenv(n, "2");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::Some(~"2"));
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::Some(~""));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Windows GetEnvironmentVariable requires some extra work to make sure
|
|
|
|
// the buffer the variable is copied into is the right size
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
#[ignore]
|
2012-03-12 22:04:27 -05:00
|
|
|
fn test_getenv_big() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut s = ~"";
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut i = 0;
|
2013-05-27 18:04:00 -05:00
|
|
|
while i < 100 { s += "aaaaaaaaaa"; i += 1; }
|
2012-03-12 22:04:27 -05:00
|
|
|
let n = make_rand_name();
|
|
|
|
setenv(n, s);
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(copy s);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(getenv(n), option::Some(s));
|
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());
|
2012-09-21 21:37:57 -05:00
|
|
|
let path = path.get();
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(copy path);
|
2012-03-12 22:04:27 -05:00
|
|
|
|
|
|
|
// Hard to test this function
|
2013-03-28 20:39:09 -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-05-08 02:23:14 -05:00
|
|
|
for e.each |p| {
|
2012-09-18 23:41:37 -05:00
|
|
|
let (n, v) = copy *p;
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(copy n);
|
2012-03-12 22:04:27 -05:00
|
|
|
let v2 = getenv(n);
|
|
|
|
// MingW seems to set some funky environment variables like
|
|
|
|
// "=C:=C:\MinGW\msys\1.0\bin" and "!::=::\" that are returned
|
|
|
|
// from env() but not visible from getenv().
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(v2.is_none() || v2 == option::Some(v));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_env_setenv() {
|
|
|
|
let n = make_rand_name();
|
|
|
|
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut e = env();
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv(n, "VALUE");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!vec::contains(e, &(copy n, ~"VALUE")));
|
2012-03-12 22:04:27 -05:00
|
|
|
|
|
|
|
e = env();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(vec::contains(e, &(n, ~"VALUE")));
|
2012-03-12 22:04:27 -05:00
|
|
|
}
|
|
|
|
|
2012-03-02 19:20:00 -06:00
|
|
|
#[test]
|
|
|
|
fn test() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((!Path("test-path").is_absolute));
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
debug!("Current working directory: %s", getcwd().to_str());
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(make_absolute(&Path("test-path")));
|
|
|
|
debug!(make_absolute(&Path("/usr/bin")));
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-03-02 19:20:00 -06:00
|
|
|
fn homedir() {
|
2013-05-23 11:39:17 -05:00
|
|
|
let oldhome = getenv("HOME");
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "/home/MountainView");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(os::homedir().is_none());
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
for oldhome.each |s| { setenv("HOME", *s) }
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-03-02 19:20:00 -06:00
|
|
|
fn homedir() {
|
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
let oldhome = getenv("HOME");
|
|
|
|
let olduserprofile = getenv("USERPROFILE");
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "");
|
|
|
|
setenv("USERPROFILE", "");
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(os::homedir().is_none());
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
setenv("HOME", "/home/MountainView");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(os::homedir(), Some(Path("/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");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(os::homedir(), Some(Path("/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");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-23 11:39:17 -05:00
|
|
|
oldhome.each(|s| { setenv("HOME", *s); true });
|
|
|
|
olduserprofile.each(|s| { setenv("USERPROFILE", *s); true });
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-21 13:35:58 -05:00
|
|
|
#[test]
|
|
|
|
fn tmpdir() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!str::is_empty(os::tmpdir().to_str()));
|
2012-08-21 13:35:58 -05:00
|
|
|
}
|
|
|
|
|
2012-03-02 19:20:00 -06:00
|
|
|
// Issue #712
|
|
|
|
#[test]
|
2012-08-24 17:28:43 -05:00
|
|
|
fn test_list_dir_no_invalid_memory_access() {
|
|
|
|
os::list_dir(&Path("."));
|
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_dir() {
|
2012-08-24 17:28:43 -05:00
|
|
|
let dirs = os::list_dir(&Path("."));
|
2012-03-02 19:20:00 -06:00
|
|
|
// Just assuming that we've got some contents in the current directory
|
2013-05-14 04:52:12 -05:00
|
|
|
assert!(dirs.len() > 0u);
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2013-05-08 02:23:14 -05:00
|
|
|
for dirs.each |dir| {
|
2013-03-08 14:39:42 -06:00
|
|
|
debug!(copy *dir);
|
2012-09-18 23:41:37 -05:00
|
|
|
}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2013-05-22 15:29:49 -05:00
|
|
|
#[test]
|
|
|
|
fn list_dir_empty_path() {
|
|
|
|
let dirs = os::list_dir(&Path(""));
|
|
|
|
assert!(dirs.is_empty());
|
|
|
|
}
|
|
|
|
|
2013-05-22 20:57:24 -05:00
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn list_dir_root() {
|
|
|
|
let dirs = os::list_dir(&Path("/"));
|
|
|
|
assert!(dirs.len() > 1);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn list_dir_root() {
|
|
|
|
let dirs = os::list_dir(&Path("C:\\"));
|
|
|
|
assert!(dirs.len() > 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-02 19:20:00 -06:00
|
|
|
#[test]
|
|
|
|
fn path_is_dir() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((os::path_is_dir(&Path("."))));
|
|
|
|
assert!((!os::path_is_dir(&Path("test/stdtest/fs.rs"))));
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn path_exists() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((os::path_exists(&Path("."))));
|
|
|
|
assert!((!os::path_exists(&Path(
|
2013-03-06 21:09:17 -06:00
|
|
|
"test/nonexistent-bogus-path"))));
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-04-02 16:07:07 -05:00
|
|
|
#[test]
|
|
|
|
fn copy_file_does_not_exist() {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!os::copy_file(&Path("test/nonexistent-bogus-path"),
|
2013-03-06 15:58:02 -06:00
|
|
|
&Path("test/other-bogus-path")));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!os::path_exists(&Path("test/other-bogus-path")));
|
2012-04-02 16:07:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_file_ok() {
|
2013-01-11 00:36:54 -06:00
|
|
|
unsafe {
|
|
|
|
let tempdir = getcwd(); // would like to use $TMPDIR,
|
|
|
|
// doesn't seem to work on Linux
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((str::len(tempdir.to_str()) > 0u));
|
2013-01-11 00:36:54 -06:00
|
|
|
let in = tempdir.push("in.txt");
|
|
|
|
let out = tempdir.push("out.txt");
|
|
|
|
|
|
|
|
/* Write the temp input file */
|
|
|
|
let ostream = do as_c_charp(in.to_str()) |fromp| {
|
|
|
|
do as_c_charp("w+b") |modebuf| {
|
|
|
|
libc::fopen(fromp, modebuf)
|
|
|
|
}
|
|
|
|
};
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((ostream as uint != 0u));
|
2013-01-11 00:36:54 -06:00
|
|
|
let s = ~"hello";
|
2013-05-27 18:04:00 -05:00
|
|
|
let mut buf = str::to_bytes(s) + [0 as u8];
|
2013-01-11 00:36:54 -06:00
|
|
|
do vec::as_mut_buf(buf) |b, _len| {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((libc::fwrite(b as *c_void, 1u as size_t,
|
2013-01-11 00:36:54 -06:00
|
|
|
(str::len(s) + 1u) as size_t, ostream)
|
2013-03-06 15:58:02 -06:00
|
|
|
== buf.len() as size_t))
|
|
|
|
}
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(libc::fclose(ostream), (0u as c_int));
|
2013-05-02 15:09:28 -05:00
|
|
|
let in_mode = in.get_mode();
|
2013-01-11 00:36:54 -06:00
|
|
|
let rs = os::copy_file(&in, &out);
|
|
|
|
if (!os::path_exists(&in)) {
|
2013-05-05 17:18:51 -05:00
|
|
|
fail!("%s doesn't exist", in.to_str());
|
2013-01-11 00:36:54 -06:00
|
|
|
}
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((rs));
|
2013-05-12 07:58:00 -05:00
|
|
|
let rslt = run::process_status("diff", [in.to_str(), out.to_str()]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(rslt, 0);
|
|
|
|
assert_eq!(out.get_mode(), in_mode);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((remove_file(&in)));
|
|
|
|
assert!((remove_file(&out)));
|
2013-01-11 00:36:54 -06:00
|
|
|
}
|
2012-04-02 16:07:07 -05:00
|
|
|
}
|
2013-04-25 19:18:25 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-04-28 23:25:35 -05:00
|
|
|
fn recursive_mkdir_slash() {
|
|
|
|
let path = Path("/");
|
|
|
|
assert!(os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
|
|
|
|
}
|
2013-04-25 19:18:25 -05:00
|
|
|
|
2013-04-28 23:25:35 -05:00
|
|
|
#[test]
|
|
|
|
fn recursive_mkdir_empty() {
|
|
|
|
let path = Path("");
|
|
|
|
assert!(!os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
|
2013-04-25 19:18:25 -05:00
|
|
|
}
|
|
|
|
|
2013-04-28 23:25:35 -05:00
|
|
|
// More recursive_mkdir tests are in std::tempfile
|
2012-04-01 10:39:17 -05:00
|
|
|
}
|